summaryrefslogtreecommitdiff
path: root/core/tape.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-19 22:06:32 -0300
committerGabriel Schneider <[email protected]>2026-11-20 00:38:51 -0300
commit2662a45ac245e574987bf6876f4470aed89da09c (patch)
treec582032cde0cb742a67e2dd548022f7b83f8d1fd /core/tape.zig
parentba10cc4ff30d08d27ad35576df04a03282b6521b (diff)
downloadpardes-2662a45ac245e574987bf6876f4470aed89da09c.tar.gz
pardes-2662a45ac245e574987bf6876f4470aed89da09c.zip
replica: the bytes kind of replica made right and joinable (P29, P32; the brief's step 2: 2a, 2b, 2c and 2d in one change): the one rule in place of two, the faults shown first (a session's output kept over a copy held ahead was fed again by a rollback: the digests apart; the pages an emulator gained while a copy was held were the epoch's and freed with it: a crash in PageList.increaseCapacity in the rollback's replay): every replica's emulators on the C heap (panes.emulatorGpa), Term.writes counting the writes into emulators, a unit whose replay wrote one never kept over a copy taken ahead, never taken as moving nothing (tape.feedsTerminal), a client's input that owed a reflow always replayed when confirmed; a speculation moves a terminal's view under bytes as under screen state; tape.unit_max (1 MiB) and Tape.cut, which the session's Peers sets, so a pump's reads are cut into units under it; the join: whole terminals in a joiner's dump, every replica the session included reloading each live terminal in the step that opens or reopens a view (panes.reloadTerminals), the session's hash taped (Site.reload) and compared, kitty records left out of it, the cell pixels set first as a feed does; .positions and .prompt compared with the replica's own emulator on replay; replicas load kitty images by direct data only (Term.direct); Peers.bytes for the soak; six tests (core/replica.zig, bytes:); tui core-test 848/997 (149 skipped), shell-test 1033/1036, pane-test 183/184, term-test 17/17, kitty-test 45/45, behaviour-test 275/275 and --replay 0, process-test 2 of 144 (the base's), image- and kitty-harness pass, unit-test bytes: 22/22; gui unit-test 1633/1637 (the perf check's BaselineHarnessMismatch); the soaks in klrvlrly's
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/tape.zig')
-rw-r--r--core/tape.zig60
1 files changed, 49 insertions, 11 deletions
diff --git a/core/tape.zig b/core/tape.zig
index 00486666..69b74bc4 100644
--- a/core/tape.zig
+++ b/core/tape.zig
@@ -131,6 +131,10 @@ pub const Site = enum(u16) {
/// What a call into a kind did (Kind.Outcome), and a kind's pane's
/// tag prefix: the authority's, whose core alone runs the kind.
addon,
+ /// The hash of a terminal's snapshot as the session reloaded it at a
+ /// join (panes.reloadTerminals), which every replica's own reload
+ /// compares.
+ reload,
};
/// What the sites return. wire.semantics hashes their shape with the
@@ -297,6 +301,10 @@ pub const Tape = struct {
/// not recorded, and no digest covers it. A replica's core never writes
/// one but by `apply`.
screen_state: bool = false,
+ /// The session's (replica.Peers): called between two of its inputs once
+ /// what it recorded since its last unit passes `unit_max`, to send that
+ /// as a unit: a pump of a flood's reads is cut into units under it.
+ cut: ?struct { ctx: *anyopaque, f: *const fn (*anyopaque) void } = null,
/// A digest as one word (screen-state replicas: every unit of a pty's
/// output carries one, at the pty's read rate), not one a field; what
/// differs is then not named.
@@ -440,6 +448,7 @@ pub const Tape = struct {
if ((t.depth == 0 or t.every) and !t.no_digest and !t.unit_digests) recordDigest(t);
if (t.depth == 0) t.flush();
t.seeFields();
+ if (t.depth == 0 and t.buf.items.len >= unit_max) if (t.cut) |c| c.f(c.ctx);
}
// ---- replaying ----
@@ -803,6 +812,16 @@ pub const Tape = struct {
// Diverged (said in `diverged`): what a speculation gets, harmless,
// for the rest of the input. A replica joins again; a replay says so.
if (!t.expectHost(id, @tagName(s))) return poisonOf(R);
+ // What a terminal told (its rows moved, its prompt, a command's
+ // output) is the session's answer, kept, and a replica whose
+ // emulators are fed the same bytes asks its own and compares: a
+ // check a unit, for nothing (P30: a digest, not state).
+ if (comptime s == .positions or s == .prompt) if (!t.screen_state) {
+ const mine = @call(.auto, f, full);
+ t.scratch.clearRetainingCapacity();
+ walk.encode(R, &mine, &t.scratch.writer) catch @panic("tape: out of memory");
+ if (!std.mem.eql(u8, t.scratch.written(), t.last_ret.bytes[t.last_ret.at..])) t.diverge("what this replica's terminal told ({t}) is not what the session's did", .{s});
+ };
var ret = t.last_ret;
return walk.decode(R, &ret, a orelse t.arena.allocator()) catch {
t.diverge("the tape's answer to {t} is short", .{s});
@@ -882,17 +901,12 @@ pub const Tape = struct {
return true;
}
- /// A speculation that moves a terminal's view: a screen-state replica's
- /// may (its rollback puts the view back, replica.zig `views`, and a delta
- /// sets it), any other stops (`authorityOnly`).
- pub fn viewOnly(t: *Tape, comptime why: []const u8) bool {
- if (!t.speculating) return false;
- if (t.screen_state) {
- t.touched = true;
- return false;
- }
- t.poison(why);
- return true;
+ /// A speculation that moves a terminal's view: it may (its rollback
+ /// puts the view back, replica.zig `views`); a screen-state replica's
+ /// says it did (a delta sets the view again).
+ pub fn viewOnly(t: *Tape, comptime _: []const u8) bool {
+ if (t.speculating and t.screen_state) t.touched = true;
+ return false;
}
/// A speculation read a terminal's rows or moved its view (`Tape.touched`).
@@ -1505,6 +1519,30 @@ pub fn endSpeculation(t: *Tape) bool {
return !t.poisoned;
}
+/// About what a unit of the session's holds at most (`Tape.cut`): a pump
+/// takes up to 496 reads of 64 KiB, which as one unit could pass the 16 MiB
+/// a message may be (replica.zig `lag_max`'s neighbour, wire's bound).
+pub const unit_max = 1 << 20;
+
+/// Whether a unit feeds a terminal its program's output (a pty's read; an
+/// input kept by hash is taken to): never taken as moving nothing (the
+/// replica engine's one rule, core/replica.zig `confirm`).
+pub fn feedsTerminal(bytes: []const u8) bool {
+ const Tag = std.meta.Tag(Input);
+ const EventTag = std.meta.Tag(pardes.Event);
+ var r: walk.Reader = .{ .bytes = bytes };
+ while (nextRecord(&r)) |rec| switch (rec.kind) {
+ .blob => if (rec.payload.len > 0 and rec.payload[0] == @backingInt(Kind.input)) return true,
+ .input => {
+ if (rec.payload.len < @sizeOf(Tag) + @sizeOf(EventTag)) continue;
+ if (!std.mem.eql(u8, rec.payload[0..@sizeOf(Tag)], std.mem.asBytes(&@as(Tag, .update)))) continue;
+ if (std.mem.eql(u8, rec.payload[@sizeOf(Tag)..][0..@sizeOf(EventTag)], std.mem.asBytes(&@as(EventTag, .output)))) return true;
+ },
+ else => {},
+ };
+ return false;
+}
+
/// Whether two tapes record the same inputs, host calls and answers,
/// their digests aside.
pub fn sameRecords(a: []const u8, b: []const u8) bool {