diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-23 02:23:34 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-23 02:23:34 -0300 |
| commit | 1ebae0db0a185455b2a03f22f5106113c6407a2f (patch) | |
| tree | db0e473c2c060e6bf7d4f4d169ffff11178056c3 /core/replica.zig | |
| parent | 5393ce743d7848d26e60fd7d4d27a4119272df9f (diff) | |
| parent | de0e85daa8ad0bf9618734e16568823aa2b61a54 (diff) | |
| download | pardes-1ebae0db0a185455b2a03f22f5106113c6407a2f.tar.gz pardes-1ebae0db0a185455b2a03f22f5106113c6407a2f.zip | |
integration (a base for the next rounds, not yet the gathered line): the one decision on the tree to install: pardes-merge60 (`krovvoum`: the viewport, a client's way back at an Upgrade, the pair's round 12, the lanes on the glide; in its whole gate) and pardes-one-decision (`ltvyvwru` on `wllwukuk` on `trmpwtso`, on pardes-viewport: one count of an emulator's writes in the core (`Pardes.outside`), copied, digested and dumped, which `Replica.confirm` reads once a unit where four spread checks stood; the rollback copy walks every type unless it declares `outside_copy`: the Changes layer's heap was not walked and was freed twice); no conflict; core-test 919/1083 (164 skipped), shell-test 1125/1128 (2026-11-23)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 187 |
1 files changed, 139 insertions, 48 deletions
diff --git a/core/replica.zig b/core/replica.zig index 9067a12a..ae93be17 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -413,9 +413,10 @@ pub const Stats = struct { } }; -/// `terminal`: its speculation read a terminal or moved one's view (or a -/// 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 }; +/// `outside`: how far its speculation moved its core's count of writes +/// outside the rollback copy (Pardes.outside): not 0, it owes an emulator +/// a write (a grid's size), which only its replay makes. +const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, outside: u64 = 0, guess: ?tape.Guess = null }; pub fn parseUnit(msg: []const u8) ?Unit { if (msg.len < unit_head) return null; @@ -607,10 +608,7 @@ pub const Replica = struct { if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); // 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 + // put-off frees done (a held copy across an input of its own, a // rejoin's old replica). if (r.copy.held) r.copy.release(); c.deinit(); @@ -690,9 +688,14 @@ pub const Replica = struct { r.stats.units += 1; r.since_check +|= 1; if (u.origin == r.id) if (r.t.core) |c| r.echo.confirmed(c, u.seq); - // The pty's bytes fed its emulators: its predicted echo judged - // against them. - defer if (r.diverged == null and tape.feedsTerminal(u.tape)) if (r.t.core) |c| if (c.echo != null) r.echo.fed(c); + // THE ONE QUESTION of a unit: did applying it write state outside + // the rollback copy (an emulator: Pardes.outside, the count of those + // writes, moved over its apply, read off the core that applied it)? + // Such a unit is applied once, to the confirmed core, never kept to + // replay over a copy; and its emulators' new state is what the + // predicted echo is judged against. + var moved = false; + defer if (r.diverged == null and moved) if (r.t.core) |c| if (c.echo != null) r.echo.fed(c); // 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 @@ -716,19 +719,20 @@ pub const Replica = struct { // nothing ends on again (`neutral`); none, not known. r.last_digest.clearRetainingCapacity(); if (lastDigest(u.tape)) |d| r.last_digest.appendSlice(r.gpa, d) catch {}; - if (r.pending.items[0].terminal) { - // Right, but what it did wrote an emulator (or owes one a - // write: a grid's size): replayed later (a rollback's - // `since`) it would write it again. So it is taken now, over - // the emulator its step read, and the ones on top applied - // again over it. + // Its speculation's own count (`Pending.outside`: the fast + // path's premise, the same records, makes it this unit's): it + // owes an emulator a write (a grid's size), which a speculation + // never makes: kept, the write would be missing; replayed later + // (a rollback's `since`), made then. So it is taken now, on the + // confirmed core, and the ones on top applied again over it. + moved = r.pending.items[0].outside != 0; + if (moved) { r.toConfirmed(); if (tape.replayUnit(&r.t, u.tape)) |why| return r.fail(why); r.freePending(r.pending.orderedRemove(0)); 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) { @@ -752,11 +756,10 @@ pub const Replica = struct { // 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); + // not once a key. Not 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 (!r.panesMoved()) return r.keepSince(u.tape); r.copy.release(); rollback.Epoch.select(null); r.clearSince(); @@ -771,13 +774,11 @@ pub const Replica = struct { r.last_digest.clearRetainingCapacity(); if (ends) |d| r.last_digest.appendSlice(r.gpa, d) catch {}; } - // A terminal's output, or a join's reloads, never taken so (the one - // rule): what it writes outside the copy is applied once, at its - // place, and much of it no digest covers (a colour set, a mode, the - // pages a reload remakes). - if (r.applied > 0 and u.origin != r.id and ends != null and std.mem.eql(u8, ends.?, r.last_digest.items) and - !tape.feedsTerminal(u.tape)) - { + // The one question needs no read here: the session's digest after + // the unit holds its count of writes outside the copy, so a unit that + // wrote an emulator (a terminal's output, a colour set no other + // digested field shows, a join's reloads) never ends where it began. + if (r.applied > 0 and u.origin != r.id and ends != null and std.mem.eql(u8, ends.?, r.last_digest.items)) { // Nothing a digest covers moved: what was applied on top is // what the session will apply on top of it. Replayed when the // copy is next put back. @@ -806,7 +807,7 @@ pub const Replica = struct { // replayed by a rollback were, if at all, as they came). const check = ends != null and r.checkDue(); r.t.checks = check; - const writes = Term.writes; + const outside = if (r.t.core) |c| c.outside.n else 0; const replayed = tape.replayUnit(&r.t, u.tape); r.t.checks = false; if (replayed) |why| { @@ -820,6 +821,7 @@ pub const Replica = struct { // draw nothing. Read after the replay: before it, it is what the // units before asked (and a first unit has no core yet). if (r.core().needs_frame) r.moved = true; + moved = r.core().outside.n != outside; if (check) { r.since_check = 0; r.stats.checks += 1; @@ -837,15 +839,15 @@ pub const Replica = struct { // 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. - // The one rule: a unit that wrote an emulator (a terminal's output, - // a grid reflowed, a view moved, one reloaded at a join) wrote what - // no copy holds: kept, a rollback would replay it into what it - // wrote already (the bytes fed twice). It goes with the copy: the - // state is the confirmed one, nothing being on top. + // The one question: a unit that wrote an emulator (a terminal's + // output, a grid reflowed, one opened, closed or reloaded at a join) + // wrote what no copy holds: kept, a rollback would replay it into + // what it wrote already (the bytes fed twice). It goes with the + // copy: the state is the confirmed one, nothing being on top. 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 or Term.writes != writes) { + if (retiring or moved) { r.copy.release(); rollback.Epoch.select(null); return r.clearSince(); @@ -896,11 +898,6 @@ pub const Replica = struct { if (!whole) return r.fail("out of memory for a rollback"); // The frame its host drew is of the memory put back: drawn again. r.moved = true; - // The rows a terminal was read as are no copy's: what the - // speculation built was the epoch's, freed with it, and what - // was there before is of a grid the deltas since moved on - // from (a rollback never brings a terminal's past back). - r.core().shell_rows = .{}; } r.applied = 0; for (r.since.items) |u| if (tape.replayUnit(&r.t, u)) |why| { @@ -937,8 +934,6 @@ pub const Replica = struct { const c = r.core(); if (!r.copy.held) { const t0 = nowNs(); - // A grid's worth of text the copy would save, and put back stale. - c.shell_rows.reset(c.gpa); r.copy.take(&r.epoch, c) catch return false; r.keepSerials(); rollback.Epoch.select(&r.epoch); @@ -952,6 +947,7 @@ pub const Replica = struct { tape.beginSpeculation(&r.t); // The time it was sent with, every time it is applied. r.t.guess = p.guess; + const outside = c.outside.n; c.toView(.at(r.view)); // Input into a terminal: what reaches its program (a key in tty // mode, a paste, a mouse report) is a pty write, the authority's, @@ -960,9 +956,9 @@ pub const Replica = struct { // the same bytes in the same order as the session's: it is applied. c.update(in.event); c.step(); - // It owes an emulator a write it skipped (a grid's size): replayed - // when confirmed. - p.terminal = r.t.touched; + // What it owes outside the copy (an emulator's grid: Term.resize + // counts the write a speculation skips): replayed when confirmed. + p.outside = c.outside.n -% outside; const clean = tape.endSpeculation(&r.t); const dt = nowNs() - t0; // Where a blob was written, should one be: the input that did. @@ -3983,6 +3979,101 @@ test "bytes: a session's output taken over a copy held ahead is fed once: a roll try sameTerminals(b.a, b.r); } +test "bytes: a session's reflow of a terminal's grid over a copy held ahead is the count's to decide: the copy goes, and a rollback does not reflow the grid again" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const b = try BytesPair.init(); + defer b.deinit(); + try b.output("a line long enough to wrap once the grid is narrower than it is now, and then some more\r\n"); + b.r.ready(); + try std.testing.expect(b.r.copy.held); + // The unit carries no output: its sync reflows the grid, which no record + // names; the core that applied it counted the write. + const before = b.r.core().outside.n; + b.r.confirm(try b.a.own(.{ .resize = .{ .cols = 40, .rows = 20 } })); + b.r.settle(); + try std.testing.expect(b.r.core().outside.n != before); + try std.testing.expect(!b.r.copy.held); + try b.rollback(); + try agrees(b.a, b.r); + try sameTerminals(b.a, b.r); +} + +test "the count of writes outside the copy is digested: a session's emulator written where its client's is not (a colour set, which no other digested field shows) is a digest that names `outside`" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const b = try BytesPair.init(); + defer b.deinit(); + try std.testing.expectEqualStrings("", try partsApart(b.a, b.r, arena.allocator())); + // A write of one side's emulator that no unit carries, as a stray one + // would be: the count alone says it. + const pane = b.a.core.panes[b.shell.index()].?; + _ = pardes.Term.feed(&b.a.core.outside, pane.terminal.?, "\x1b[31m", &.{}, &.{}, &.{}, &.{}); + try std.testing.expectEqualStrings("outside ", try partsApart(b.a, b.r, arena.allocator())); +} + +test "bytes: a terminal's rows read before an idle client's copy are the copy's: a rollback puts them back, neither dropped nor freed twice" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const b = try BytesPair.init(); + defer b.deinit(); + try b.output("a line of the program's\r\nand another\r\n"); + // Its rows read between steps (a draw's), then the copy taken while it + // idles (`ready`): the rows are what the core holds at the copy. + const pane = b.r.core().panes[b.shell.index()].?; + _ = try pardes.edit.shellRows(b.r.core(), pane); + const cached = b.r.core().shell_rows.text_alloc.ptr; + b.r.ready(); + try std.testing.expect(b.r.copy.held); + // A key of the client's on top, the session's own key first: the copy + // put back, the rows with it (not left orphaned in the epoch). + try b.rollback(); + try std.testing.expectEqual(cached, b.r.core().shell_rows.text_alloc.ptr); + try agrees(b.a, b.r); + try b.output("after\r\n"); + _ = try pardes.edit.shellRows(b.r.core(), pane); + try agrees(b.a, b.r); +} + +test "bytes: a client's key that reads a terminal's rows, the session's output before it in the session's order: the output rolls it back, its read is made again over the fed rows, and the input is kept on the fast path with the session's reading" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const b = try BytesPair.init(); + defer b.deinit(); + try b.output("l1\r\nl2\r\nl3\r\n"); + // The shell's pane focused and out of its tty mode (^B): a cursor over + // its rows. + const id = b.shell.index(); + const rect = b.a.core.view().rects[id]; + for ([_]pardes.Event{ + .{ .mouse = .{ .button = .left, .kind = .press, .col = rect.x + 2, .row = rect.y + 1 } }, + .{ .mouse = .{ .button = .left, .kind = .release, .col = rect.x + 2, .row = rect.y + 1 } }, + .{ .key = .{ .cp = 'b', .ctrl = true } }, + }) |ev| { + b.r.confirm(try b.a.own(ev)); + b.r.settle(); + } + try agrees(b.a, b.r); + // G: to the last of its rows, which it reads (edit.shellRows). + const last = try gpa.dupe(u8, try b.r.input(.{ .key = .{ .cp = 'G', .text = "G" } })); + defer gpa.free(last); + try std.testing.expectEqual(@as(usize, 1), b.r.applied); + const read_before = b.r.core().panes[id].?.body.cur_row; + // The program prints two lines before the session takes the G: the + // emulator fed (its count moved, so never a neutral unit), the G + // undone and read again over the rows as fed. + b.a.output(.{ .output = .{ .pane = b.shell, .bytes = "l4\r\nl5\r\n" } }); + b.r.confirm((try b.a.pending()).?); + b.r.settle(); + try std.testing.expectEqual(@as(usize, 1), b.r.applied); + try std.testing.expect(b.r.core().panes[id].?.body.cur_row > read_before); + const fast = b.r.stats.fast; + b.r.confirm(try b.a.take(last)); + b.r.settle(); + try std.testing.expectEqual(fast + 1, b.r.stats.fast); + try agrees(b.a, b.r); + try std.testing.expectEqual(b.a.core.panes[id].?.body.cur_row, b.r.core().panes[id].?.body.cur_row); +} + test "bytes: what a terminal's emulator gains while a copy is held is its own: a rollback frees none of it" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const b = try BytesPair.init(); @@ -4255,12 +4346,12 @@ test "bytes: a client joins from a dump with its terminals whole, and every side var line: [128]u8 = undefined; for (0..400) |i| try b.output(try std.fmt.bufPrint(&line, "\x1b[3{d}m{d}\x1b]8;;https://example.com/{d}\x1b\\ link\x1b]8;;\x1b\\\x1b[0m\r\n", .{ i % 8, i, i })); try b.output("half a colour: \x1b[38;2;10;20"); - const reloads = pardes.Term.writes; + const reloads = b.r.core().outside.n; const joined = (try b.a.join(2, null)).?; defer gpa.free(joined.dump); b.r.confirm(joined.unit); try std.testing.expect(b.r.diverged == null); - try std.testing.expect(pardes.Term.writes != reloads); + try std.testing.expect(b.r.core().outside.n != reloads); const late = try Replica.initDump(gpa, 2, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); defer late.deinit(); late.core().echo = null; |
