diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-18 11:58:32 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-18 12:05:57 -0300 |
| commit | f890732116c69c4020c3d0a666f445c7993003a3 (patch) | |
| tree | 3b3a2ddc636994fbc554cf462d83083799fb3d86 /src | |
| parent | 49afe2947bb944c05fc33ff24e0e45099d624bf6 (diff) | |
| download | pardes-f890732116c69c4020c3d0a666f445c7993003a3.tar.gz pardes-f890732116c69c4020c3d0a666f445c7993003a3.zip | |
replica: a copy taken ahead is kept over the session's units again, but never across a delta or a pane's end: the stale state round 2 could not name was the terminal's emulator (outside the copy, outside every digest), which a unit kept in since had read and a rollback replayed over the rows a later delta had redrawn (the soak, three clients: another client's key over a shell's text, then the full-screen program's redraw, then a rollback); and a unit that closed or tore down a pane (its emulator freed on the C heap) was replayed again over the freed emulator; a delta now lets the kept units go first (the copy let go when nothing is on top, else replayed), and an ahead unit that opened, closed or tore down a pane lets the copy go; the views kept per delta lose their index; round 2's misreading came from toConfirmed returning on a failed replay before its check, the next unit blamed: a trace names the kept unit now; Replica.confirm's fast path sets last_digest
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src')
| -rw-r--r-- | src/replica.zig | 205 |
1 files changed, 175 insertions, 30 deletions
diff --git a/src/replica.zig b/src/replica.zig index cf382eed..e5d5ad16 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -620,11 +620,6 @@ pub const Stats = struct { /// terminal had the keyboard), so what it did depends on the emulator. const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, terminal: bool = false, guess: ?tape.Guess = null }; -/// A terminal's view as the authority's was once `after` of the units -/// since the copy were taken (`Replica.since`): a delta's comes after the -/// units it shows, so those before it are not replayed over it. -const View = struct { at: ?usize = null, after: usize = 0 }; - pub fn parseUnit(msg: []const u8) ?Unit { if (msg.len < unit_head) return null; return .{ @@ -651,7 +646,11 @@ pub const Replica = struct { /// The pending input that waits for the authority (it stopped at a host /// call, or is the view's), and those after it with it. blocked: ?u32 = null, - /// Confirmed units fast-pathed since the copy was taken. + /// Confirmed units since the copy was taken, replayed over it when it + /// is put back: a unit of another's that came while the client was + /// idle (`ready`), one of its own fast-pathed, one that moved nothing + /// (`neutral`). Each read the terminals as they are now: a delta lets + /// them go first (`screen`). since: std.ArrayListUnmanaged([]u8) = .empty, /// Pending inputs to apply again (after a rollback). dirty: bool = false, @@ -686,7 +685,7 @@ pub const Replica = struct { /// Each terminal's view (`Term.grid`'s view) as the copy was taken, or /// as the last delta since set it: the copy holds no emulator, and a /// speculation may move a view, so a rollback puts these back. - views: [pardes.MAX_PANES]View = @splat(.{}), + views: [pardes.MAX_PANES]?usize = @splat(null), screen_out: std.ArrayListUnmanaged(u8) = .empty, /// Its Shadows' rasters, from the session's strips (17.3). blobs: pardes.Kind.Blobs = .{}, @@ -852,12 +851,10 @@ pub const Replica = struct { const u = parseUnit(msg) orelse return r.fail("a unit too short to read"); r.stats.units += 1; if (u.origin == r.id) if (r.t.core) |c| r.echo.confirmed(c, u.seq); - // A copy taken ahead (`ready`), nothing on top: let go once the unit - // is replayed, and taken again when the client is idle. Held over - // the session's units, kept in `since` to replay, it put back a - // state a later step read otherwise than the session's (the soak, - // three clients, with `ready` as the clients call it: diverged; every - // rollback's digest agreed, so what differs is no digest's). + // A copy taken ahead (`ready`), nothing on top: it stays the point a + // rollback goes back to, the unit replayed on it and kept in `since`, + // not let go and taken again (a megabyte walked) for each unit of + // the session's that comes while the client is idle. const ahead = r.applied == 0 and r.copy.held; if (r.guess) { r.stamp = .{ .real_ns = u.real_ns, .mono_ns = u.mono_ns, .at_ns = nowNs() }; @@ -873,6 +870,12 @@ pub const Replica = struct { tape.sameRecords(r.pending.items[0].spec.?, u.tape)) { r.stats.fast += 1; + // The state it ends on, which a unit of another's that moves + // nothing ends on again (`neutral`). + if (lastDigest(u.tape)) |d| { + r.last_digest.clearRetainingCapacity(); + r.last_digest.appendSlice(r.gpa, d) catch {}; + } if (r.pending.items[0].terminal and r.applied > 1) { // Right, but what it did read a terminal: replayed later (a // rollback's `since`) it would read the deltas that came @@ -880,10 +883,6 @@ pub const Replica = struct { // read, and the ones on top applied again over it. r.toConfirmed(); if (tape.replayUnit(&r.t, u.tape)) |why| return r.fail(why); - if (lastDigest(u.tape)) |d| { - r.last_digest.clearRetainingCapacity(); - r.last_digest.appendSlice(r.gpa, d) catch {}; - } r.freePending(r.pending.orderedRemove(0)); r.dirty = true; return; @@ -930,11 +929,20 @@ pub const Replica = struct { } r.recovering = nowNs(); r.toConfirmed(); + if (r.diverged != null) return; } // What it shows moved only when what a digest covers did: a unit of // the session's pump that read its clock and moved nothing draws // no frame. if (ends == null or !std.mem.eql(u8, ends.?, r.last_digest.items)) r.moved = true; + // What the unit opens or closes outside the copy (`keepSince` below). + var serials: [pardes.MAX_PANES]u32 = undefined; + var retiring = false; + if (ahead) { + const c = r.core(); + for (c.panes, &serials) |slot, *n| n.* = if (slot) |pane| pane.serial else 0; + for (c.retired_panes.current, c.retired_panes.previous) |x, y| retiring = retiring or x != null or y != null; + } if (tape.replayUnit(&r.t, u.tape)) |why| { trace(r.gpa, "replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }, &.{u.tape}); return r.fail(why); @@ -945,11 +953,22 @@ pub const Replica = struct { r.freePending(r.pending.orderedRemove(0)); } r.dirty = true; - if (ahead) { + if (!ahead) return; + // A pane opened, closed, or torn down (two syncs after it closed, + // `reapPanes`): what it holds outside the copy (a terminal's + // emulator, a picture's pixels, the store's record of it) was made + // or freed here, and is not put back with the copy: a rollback + // replaying the unit again would close a terminal freed already (the + // soak, a `Del` of the shell). Let go with the copy. + const c = r.core(); + for (c.retired_panes.current, c.retired_panes.previous) |x, y| retiring = retiring or x != null or y != null; + for (c.panes, serials) |slot, n| retiring = retiring or (if (slot) |pane| pane.serial else 0) != n; + if (retiring) { r.copy.release(); rollback.Epoch.select(null); - r.clearSince(); + return r.clearSince(); } + r.keepSince(u.tape); } fn fail(r: *Replica, why: []const u8) void { @@ -996,13 +1015,13 @@ pub const Replica = struct { r.core().shell_rows = .{}; } r.applied = 0; - // Its views as the copy was taken, or as a delta set them between - // the units since (which may move them again). - for (r.since.items, 0..) |u, i| { - if (held) r.putViews(i); - if (tape.replayUnit(&r.t, u)) |why| return r.fail(why); - } - if (held) r.putViews(r.since.items.len); + // Its views as the copy was taken, or as the last delta set them + // (which the units since may move again). + if (held) r.putViews(); + for (r.since.items) |u| if (tape.replayUnit(&r.t, u)) |why| { + trace(r.gpa, "replica {d}: a unit kept since the copy came to another state replayed again: {s}\n", .{ r.id, why }, &.{u}); + return r.fail(why); + }; r.clearSince(); for (r.pending.items) |*p| if (p.spec) |s| { r.gpa.free(s); @@ -1132,12 +1151,12 @@ pub const Replica = struct { fn keepViews(r: *Replica) void { if (!r.t.screen_state) return; - for (r.core().panes, &r.views) |slot, *v| v.* = .{ .at = if (slot) |pane| if (pane.terminal) |state| pardes.Term.grid(state).view else null else null }; + for (r.core().panes, &r.views) |slot, *v| v.* = if (slot) |pane| if (pane.terminal) |state| pardes.Term.grid(state).view else null else null; } - fn putViews(r: *Replica, after: usize) void { + fn putViews(r: *Replica) void { if (!r.t.screen_state) return; - for (r.core().panes, r.views) |slot, v| if (v.after == after) if (slot) |pane| if (pane.terminal) |state| pardes.Term.viewTo(state, v.at); + for (r.core().panes, r.views) |slot, at| if (slot) |pane| if (pane.terminal) |state| pardes.Term.viewTo(state, at); } /// Whether its core loaded from the dump is the session as it was @@ -1214,6 +1233,27 @@ pub const Replica = struct { // of what this writes (State.gpa). switch (m.kind) { kind_delta => { + // A unit kept to replay (`since`) read the emulator as it is + // now, and its step may have read it (a key over a shell's + // text reads its rows, an output its prompt): replayed over + // this delta it would read another (the soak, a copy taken + // ahead, three clients: another's key that read the rows, + // replayed by a rollback after the full-screen program + // redrew them, came to another state). So the units since go + // before it: let go with the copy when nothing is on top (the + // state is the confirmed one), else replayed now. + if (r.copy.held and r.since.items.len > 0) { + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + if (r.applied == 0) { + r.copy.release(); + rollback.Epoch.select(null); + r.clearSince(); + } else { + r.toConfirmed(); + r.dirty = true; + } + } const t0 = nowNs(); const applied = Term.apply(state, sc.rep, m.payload) catch |err| { if (err != error.BaseMismatch) r.fail("a terminal's delta did not apply"); @@ -1228,7 +1268,7 @@ pub const Replica = struct { c.shell_rows.markStale(pane); // Its view is the authority's now: a rollback puts it back // here, and what was applied over a terminal goes on top. - if (r.copy.held) r.views[m.pane] = .{ .at = if (pane.terminal) |held| pardes.Term.grid(held).view else null, .after = r.since.items.len }; + if (r.copy.held) r.views[m.pane] = pardes.Term.grid(state).view; r.overTerminals(); var seq: [4]u8 = undefined; std.mem.writeInt(u32, &seq, applied.seq, .little); @@ -3848,6 +3888,111 @@ test "pardes's own keys over a terminal's history apply at once: its view moves try std.testing.expectEqual(pardes.Term.grid(a.core.panes[shell].?.terminal).view, pardes.Term.grid(r.core().panes[shell].?.terminal).view); } +test "a unit kept over a copy taken ahead is never replayed over a delta that came after it: the session's key read a shell's rows, the program redrew them" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.view().active = shell; + r.confirm(try a.local(.{ .focus = true })); + var line: [96]u8 = undefined; + for (0..30) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "line {d} {s}\r\n", .{ i, "words of a length its own"[0 .. i % 20 + 4] }) } }); + r.confirm((try a.pending()).?); + try deliver(a, &screens, peer, r, .flush); + // Out of the shell's tty mode, in the session's view: keys over its text. + r.confirm(try a.local(.{ .key = .{ .cp = a.core.opts.tty_toggle, .ctrl = true } })); + r.settle(); + // The client idle: a copy taken ahead. The session's keys read the rows + // (up, to the line's end): replayed here over the copy, and kept. + r.ready(); + try std.testing.expect(r.copy.held); + for ([_]u21{ 'k', 'k', '$' }) |cp| { + const u = try gpa.dupe(u8, try a.local(.{ .key = .{ .cp = cp, .text = &.{@intCast(cp)} } })); + defer gpa.free(u); + try deliver(a, &screens, peer, r, .fill); + r.confirm(u); + r.settle(); + } + // The program redraws the screen: other rows where those keys read. + a.output(.{ .output = .{ .pane = shell, .bytes = "\x1b[H\x1b[2J" } }); + for (0..30) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "{d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + // The client types, and the session's key comes first: a rollback, which + // must not replay the keys that read the rows over the redrawn ones. + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try key(r, 'h', &sent); + try std.testing.expectEqual(@as(usize, 1), r.applied); + { + const u = try gpa.dupe(u8, try a.local(.{ .key = .{ .cp = '0', .text = "0" } })); + defer gpa.free(u); + try deliver(a, &screens, peer, r, .fill); + r.confirm(u); + r.settle(); + } + try deliver(a, &screens, peer, r, .flush); + r.confirm(try a.take(sent.items[0])); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "a unit that closes or tears down a terminal is never kept over a copy taken ahead: a rollback would close its emulator again" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const text = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.file != null) break id; + } else return error.NoTextPane; + a.core.view().active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break id; + } else return error.NoShell; + // The session closes its shell, and its next steps tear it down, while + // the client is idle (a copy taken ahead each time). + r.ready(); + r.confirm(try a.local(.{ .command = "Del" })); + a.core.view().active = text; + for (0..3) |_| { + r.ready(); + r.confirm(try a.local(.{ .key = .{ .cp = 'j', .text = "j" } })); + } + // The client types, and the session's key comes first: a rollback. + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + r.ready(); + try key(r, 'l', &sent); + r.confirm(try a.local(.{ .key = .{ .cp = 'o', .text = "o" } })); + r.settle(); + r.confirm(try a.take(sent.items[0])); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + // The shell closed once here as there (the store's record of it is made + // as it closes, on the host's heap, which no copy puts back). + try std.testing.expectEqual(a.core.store.closed.items.len, r.core().store.closed.items.len); +} + test "a replica joined from the session's dump replays the units after it" { const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); |
