From ceed9ad65c34a222f3b4e8c68ecaff595db17ffc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 13:52:55 -0300 Subject: replica: the copy taken ahead of an input of its own stays once that input is confirmed with nothing on top, the input kept in since to replay over it as a unit of the session's that comes while idle is (round 2's design): the walk once in rebase_after units, not once a key; never over an input that read a terminal, nor once a pane opened, closed or is being torn down since the copy (Replica.panesMoved, the panes' serials and retired panes kept as the copy is taken); its gate found what restoring a held copy at Replica.deinit did: it brought back the shell's rows that were freed between steps meanwhile, which toConfirmed drops and deinit freed again (shell-test's 'a delta applied under a speculation outlives its rollback' aborted on a corrupted heap; a rejoin's old replica and a quitting client reach it, with a copy taken while idle before this change too): deinit lets the copy go instead; tty client instructions against the own path (ReleaseFast, 50,000 lines, load 5-15): key-down +65% -> +6%, type +49% -> +8%, resize +45% -> +14%, edit +40% unchanged (its own digest check at each confirmed input, item 2's), page-down -10%, wheel -27%; the own path 3.86/11.55/14.51/52.03/41.09/5.72 M as round 2's 3.86/11.62/14.57/52.6/41.5/5.72; replicas five clients diverged 0, unequal 0, checked/wrong 1782/0, 1769/0, 1871/0, quiet checks 582/0, 773/0, 791/0; three clients 1690/0, 1650/0, 1833/0; tui core-test 822/963, shell-test 998/1001, behaviour --replay 20 seeds 0 failures Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/replica.zig | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index 4140beca..3af1caac 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -741,6 +741,10 @@ pub const Replica = struct { /// 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]?usize = @splat(null), + /// Its panes' serials as the copy was taken, and whether one was being + /// torn down then (`panesMoved`). + serials: [pardes.MAX_PANES]u32 = @splat(0), + retiring: bool = false, screen_out: std.ArrayListUnmanaged(u8) = .empty, /// Its Shadows' rasters, from the session's strips (17.3). blobs: pardes.Kind.Blobs = .{}, @@ -842,7 +846,13 @@ pub const Replica = struct { for (&r.screens) |*slot| if (slot.*) |*sc| Term.replicaFree(sc.rep, pardes.panes.emulator_gpa); if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); - if (r.copy.held) _ = r.copy.restore(); + // The state it ends on is whole as it is: the copy let go, its + // put-off frees done. Put back, it would bring back what was + // freed between steps meanwhile (the shell's rows a draw read + // again after a delta), which `toConfirmed` drops and this + // freed again (a held copy across an input of its own, a + // rejoin's old replica). + if (r.copy.held) r.copy.release(); c.deinit(); was.end(); } @@ -942,6 +952,7 @@ pub const Replica = struct { r.dirty = true; return; } + const read_terminal = r.pending.items[0].terminal; r.freePending(r.pending.orderedRemove(0)); r.applied -= 1; if (r.applied == 0) { @@ -957,6 +968,15 @@ pub const Replica = struct { return r.fail(why); } } + // The copy stays the point a rollback goes back to, this + // input kept to replay over it as a unit of the session's + // that comes while idle is: a step writes some bytes of the + // megabyte the copy is, walked once in `rebase_after` units, + // not once a key. Not over one that read a terminal (replayed + // later it would read the deltas since), nor once a pane + // opened, closed or was torn down since the copy (what it + // holds outside the copy was made or freed: `ahead`'s rule). + if (!read_terminal and !r.panesMoved()) return r.keepSince(u.tape); r.copy.release(); rollback.Epoch.select(null); r.clearSince(); @@ -1208,10 +1228,25 @@ pub const Replica = struct { } fn keepViews(r: *Replica) void { + const c = r.core(); + for (c.panes, &r.serials) |slot, *n| n.* = if (slot) |pane| pane.serial else 0; + r.retiring = for (c.retired_panes.current, c.retired_panes.previous) |x, y| { + if (x != null or y != null) break true; + } else false; if (!r.t.screen_state) return; 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; } + /// Whether a pane opened, closed or is torn down since the copy was + /// taken (`keepViews`). + fn panesMoved(r: *const Replica) bool { + const c = r.core(); + if (r.retiring) return true; + for (c.retired_panes.current, c.retired_panes.previous) |x, y| if (x != null or y != null) return true; + for (c.panes, r.serials) |slot, n| if ((if (slot) |pane| pane.serial else 0) != n) return true; + return false; + } + fn putViews(r: *Replica) void { if (!r.t.screen_state) return; for (r.core().panes, r.views) |slot, at| if (slot) |pane| if (pane.terminal) |state| pardes.Term.viewTo(state, at); @@ -3616,6 +3651,36 @@ test "another client's input first: a replica rolls back, replays it and applies try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); } +test "the copy taken for an input of its own stays once the input is confirmed: the next takes none, and a rollback replays the first over it" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r = try Replica.init(gpa, 1, first, false); + defer r.deinit(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + freshSecond(); + try key(r, 'j', &sent); + const copies = r.stats.copies; + r.confirm(try a.take(sent.items[0])); + try std.testing.expect(r.copy.held); + try std.testing.expectEqual(@as(usize, 1), r.since.items.len); + try key(r, 'j', &sent); + try std.testing.expectEqual(copies, r.stats.copies); + // The session's own key first: back to the copy, the first replayed. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + r.settle(); + r.confirm(try a.take(sent.items[1])); + r.settle(); + try agrees(a, r); + try std.testing.expectEqual(@as(u32, 1), r.stats.foreign); +} + test "a rollback over a Tab it applied puts the process's tab width back as its core has it" { // config.tab_now is the process's, written by the step that set the // width (the soak, five clients: a Tab undone left it, and the next -- cgit v1.3