diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-22 20:21:32 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-22 20:21:32 -0300 |
| commit | 3f250d0f963f375d699f11ffc02b07f2e2cbbbee (patch) | |
| tree | 4e5a1f577fd3dd247f3643d979868347524ce703 /core/replica.zig | |
| parent | 34e2d82a322bd3767a8ec62b3a4c0894399e8c04 (diff) | |
| parent | c607dc14502a6956bdfa3575bc7363514a7abeb4 (diff) | |
| download | pardes-3f250d0f963f375d699f11ffc02b07f2e2cbbbee.tar.gz pardes-3f250d0f963f375d699f11ffc02b07f2e2cbbbee.zip | |
integration (a base for the terminals' next round, not yet the gathered line): the viewport in the view and the patches in the semantics hash on merge58 with the terminal put back: pardes-raw-tty (`muwnrqro`, on pardes-merge58: a TERM that reaches a session while it leaves its terminal no longer leaves it raw: `Killed.disarm` puts the cooked modes back itself, `leave` calls it last) and pardes-term-join-10's two changes (`xktusoyq` on `qtxssxyk`, on pardes-term-join-9: where a view stands in a terminal's history is the view's (`Pane.scrolled_to`), carried once a confirmed write that moves rows, the wire at 31; build/patches hashed into a replica's semantics); no conflict; core-test 917/1077 (160 skipped), shell-test 1119/1122, pane-test 182/183, term-test 16/16; NOT gated whole: the viewport's gate had one perf count over its budget ("terminal on all" +0.235%), which the next round brings down before this is the gathered line (2026-11-22)
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 | 206 |
1 files changed, 182 insertions, 24 deletions
diff --git a/core/replica.zig b/core/replica.zig index 71a8de71..f4a3284c 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -479,10 +479,6 @@ pub const Replica = struct { rtt_ns: u64 = 0, gmtoff: i32 = 0, - /// Each terminal's view (`Term.grid`'s view) as the copy was taken: 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), @@ -907,9 +903,6 @@ pub const Replica = struct { r.core().shell_rows = .{}; } r.applied = 0; - // 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); @@ -947,7 +940,7 @@ pub const Replica = struct { // 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.keepViews(); + r.keepSerials(); rollback.Epoch.select(&r.epoch); if (r.stats.samples) r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; r.stats.copy_bytes += r.copy.savedBytes(); @@ -1033,23 +1026,22 @@ pub const Replica = struct { rollback.Epoch.select(&r.epoch); defer rollback.Epoch.select(null); r.copy.take(&r.epoch, c) catch return; - r.keepViews(); + r.keepSerials(); if (r.stats.samples) r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; r.stats.copy_bytes += r.copy.savedBytes(); r.stats.copies += 1; } - fn keepViews(r: *Replica) void { + fn keepSerials(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; - 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`). + /// taken (`keepSerials`). fn panesMoved(r: *const Replica) bool { const c = r.core(); if (r.retiring) return true; @@ -1058,10 +1050,6 @@ pub const Replica = struct { return false; } - fn putViews(r: *Replica) void { - 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 /// dumped, by the welcome's `digest`. pub fn loaded(r: *Replica, want: u64) bool { @@ -1138,8 +1126,7 @@ fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void { const pane = slot orelse continue; const state = pane.terminal orelse continue; var h = walk.Hasher.init(0); - // Its view aside: a speculation may move it (`views`). - state.digestScreen(&h, false); + state.digest(&h); d.* = h.final(); } } @@ -4023,7 +4010,7 @@ test "bytes: a session's output that moves nothing the digest covers (a colour s try sameTerminals(b.a, b.r); } -test "bytes: a terminal's view a rollback puts back is the session's through a reflow: a view on a wrapped line's second row, joined into the row above by the session's widening, is the same row after its narrowing on every side" { +test "bytes: a terminal's view is the same row on every side through a reflow and a rollback: a view on a wrapped line's second row, joined into the row above by the session's widening, is the same row after its narrowing on every side" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const b = try BytesPair.init(); defer b.deinit(); @@ -4041,13 +4028,12 @@ test "bytes: a terminal's view a rollback puts back is the session's through a r } b.r.confirm((try b.a.pending()).?); b.r.settle(); - for ([_]*Pardes{ b.a.core, b.r.core() }) |core| pardes.Term.viewTo(core.panes[id].?.terminal.?, 1); - // The session's frontend widens: the line one row, the view at row 0, - // its cell mid-row. + for ([_]*Pardes{ b.a.core, b.r.core() }) |core| core.panes[id].?.scrolled_to = 1; + // The session's frontend widens: the line one row, the view at row 0. b.r.confirm(try b.a.own(.{ .resize = .{ .cols = 200, .rows = 24 } })); b.r.settle(); try agrees(b.a, b.r); - try std.testing.expectEqual(@as(?usize, 0), pardes.Term.grid(b.a.core.panes[id].?.terminal).view); + try std.testing.expectEqual(@as(?usize, 0), b.a.core.panes[id].?.scrolled_to); // A key of the client's on top, the session's first: the copy put back. try b.rollback(); try agrees(b.a, b.r); @@ -4057,6 +4043,178 @@ test "bytes: a terminal's view a rollback puts back is the session's through a r try agrees(b.a, b.r); } +/// Where view `k` stands in terminal pane `id`'s history (Pane.scrolled_to): +/// the pane's own field while `k` is live, its share's else. +fn placeIn(p: *const Pardes, k: u8, id: usize) ?usize { + const pane = p.panes[id].?; + return if (p.live == k) pane.scrolled_to else pane.pv[k].scrolled_to; +} + +test "bytes: a client's wheel over a terminal is its own view's: applied at once, the session's view on the output and following it, the client's on its row through output and a rollback" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const b = try BytesPair.init(); + defer b.deinit(); + const id = b.shell.index(); + var line: [32]u8 = undefined; + for (0..100) |i| b.a.output(.{ .output = .{ .pane = b.shell, .bytes = try std.fmt.bufPrint(&line, "line-{d}\r\n", .{i}) } }); + b.r.confirm((try b.a.pending()).?); + b.r.settle(); + // The client in a view of its own, laid out by a step of its own. + const opened = try b.a.openView(1); + b.r.view = opened.view; + b.r.confirm(opened.unit); + b.r.settle(); + const focus = try gpa.dupe(u8, try b.r.input(.{ .focus = true })); + defer gpa.free(focus); + b.r.confirm(try b.a.take(focus)); + b.r.settle(); + const rect = b.a.core.views[b.r.view].?.rects[id]; + const wheel = try gpa.dupe(u8, try b.r.input(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = rect.x + 2, .row = rect.y + rect.h -| 1 } })); + defer gpa.free(wheel); + // Applied at once, nothing of it outside the copy: the session's own + // view (the replica's core holds it too) is where it was. + try std.testing.expectEqual(@as(usize, 1), b.r.applied); + const back = placeIn(b.r.core(), b.r.view, id).?; + try std.testing.expectEqual(@as(?usize, null), placeIn(b.r.core(), 0, id)); + b.r.confirm(try b.a.take(wheel)); + b.r.settle(); + try agrees(b.a, b.r); + // The program prints: the view on the output follows, the client's stays. + try b.output("more-1\r\nmore-2\r\n"); + try agrees(b.a, b.r); + for ([_]*Pardes{ b.a.core, b.r.core() }) |core| { + try std.testing.expectEqual(@as(?usize, null), placeIn(core, 0, id)); + try std.testing.expectEqual(@as(?usize, back), placeIn(core, b.r.view, id)); + } + // A wheel of the client's on top, the session's own key first: the + // copy put back, the wheel applied again over it. + const again = try gpa.dupe(u8, try b.r.input(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = rect.x + 2, .row = rect.y + rect.h -| 1 } })); + defer gpa.free(again); + b.r.confirm(try b.a.local(.{ .key = .{ .cp = 'j', .text = "j" } })); + b.r.settle(); + b.r.confirm(try b.a.take(again)); + b.r.settle(); + try agrees(b.a, b.r); + try std.testing.expect(placeIn(b.r.core(), b.r.view, id).? < back); +} + +/// Seeds of the explorer below a run of the suite takes; 3,000 were run +/// once (reports/pardes-term-join-10.md). +const view_orders = 30; + +test "bytes: terminal views under random orders of the session's output, each view's wheels, keys and resizes, a copy taken ahead and rollbacks, the history at its cap: every side agrees (an explorer)" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const Ev = pardes.Event; + const K = pardes.Key; + var seed: u64 = 0; + while (seed < view_orders) : (seed += 1) { + const b = try BytesPair.init(); + defer b.deinit(); + var prng: std.Random.DefaultPrng = .init(seed); + const rnd = prng.random(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + var units: std.ArrayListUnmanaged([]u8) = .empty; + var trail: std.ArrayListUnmanaged(u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + for (units.items) |m| gpa.free(m); + units.deinit(gpa); + trail.deinit(gpa); + } + var line: [64]u8 = undefined; + // The history at its cap: the emulator's byte limit at its least, + // on both sides alike, then flooded past it. + for ([_]*Pardes{ b.a.core, b.r.core() }) |core| pardes.Term.emulatorOf(core.panes[b.shell.index()].?.terminal.?).vt.screens.active.pages.setMaxBytes(1); + var n_out: usize = 0; + while (n_out < 6000) : (n_out += 1) b.a.output(.{ .output = .{ .pane = b.shell, .bytes = try std.fmt.bufPrint(&line, "line {d}\r\n", .{n_out}) } }); + b.r.confirm((try b.a.pending()).?); + b.r.settle(); + // The session's view at or near the history's top, on both sides. + const top = rnd.uintLessThan(usize, 4); + for ([_]*Pardes{ b.a.core, b.r.core() }) |core| core.panes[b.shell.index()].?.scrolled_to = top; + b.a.core.view().active = b.shell.index(); + b.r.confirm(try b.a.own(.{ .focus = true })); + const opened = try b.a.openView(1); + b.r.view = opened.view; + b.r.confirm(opened.unit); + b.r.settle(); + for (0..150) |_| { + const who = rnd.uintLessThan(u8, 10); + if (who < 2) { + // the session's own pty, or its taking a client's input + if (rnd.boolean()) { + try trail.print(gpa, "So ", .{}); + for (0..rnd.intRangeAtMost(usize, 1, 600)) |_| { + b.a.output(.{ .output = .{ .pane = b.shell, .bytes = try std.fmt.bufPrint(&line, "line {d}\r\n", .{n_out}) } }); + n_out += 1; + } + try units.append(gpa, try gpa.dupe(u8, (try b.a.pending()).?)); + } else if (sent.items.len > 0) { + try trail.print(gpa, "St ", .{}); + const m = sent.orderedRemove(0); + defer gpa.free(m); + try units.append(gpa, try gpa.dupe(u8, try b.a.take(m))); + } + continue; + } + if (who < 5) { + if (rnd.uintLessThan(u8, 4) == 0) { + try trail.print(gpa, "Rr ", .{}); + b.r.ready(); + } else if (units.items.len > 0) { + try trail.print(gpa, "R ", .{}); + const u = units.orderedRemove(0); + defer gpa.free(u); + b.r.confirm(u); + b.r.settle(); + } + continue; + } + // an input, the session's frontend's (view 0) or the client's + const client = who >= 7; + const k: u8 = if (client) b.r.view else 0; + const rect = b.a.core.views[k].?.rects[b.shell.index()]; + const ev: Ev, const name: []const u8 = switch (rnd.uintLessThan(u8, 20)) { + 14 => .{ .{ .key = .{ .cp = 'g', .text = "g" } }, "g" }, + 15 => .{ .{ .key = .{ .cp = 'G', .text = "G" } }, "G" }, + 16 => .{ .{ .key = .{ .cp = 'n', .text = "n" } }, "n" }, + 17 => .{ .{ .key = .{ .cp = 'N', .text = "N" } }, "N" }, + 18 => .{ .{ .key = .{ .cp = '/', .text = "/" } }, "/" }, + 19 => .{ .{ .paste = "line 1" }, "p" }, + 0, 1, 2, 3 => .{ .{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = rect.x + 2, .row = rect.y + rect.h -| 1 } }, "wu" }, + 4, 5 => .{ .{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = rect.x + 2, .row = rect.y + rect.h -| 1 } }, "wd" }, + 6 => .{ .{ .key = .{ .cp = 'b', .ctrl = true } }, "^b" }, + 7 => .{ .{ .key = .{ .cp = 'j', .text = "j" } }, "j" }, + 8 => .{ .{ .key = .{ .cp = 'k', .text = "k" } }, "k" }, + 9 => .{ .{ .key = .{ .cp = 'u', .ctrl = true } }, "^u" }, + 10 => .{ .{ .key = .{ .cp = 'd', .ctrl = true } }, "^d" }, + 11 => .{ .{ .key = .{ .cp = K.escape } }, "esc" }, + 13 => .{ .{ .key = .{ .cp = K.enter } }, "ent" }, + 12 => .{ .{ .resize = .{ .cols = rnd.intRangeAtMost(u16, 60, 90), .rows = rnd.intRangeAtMost(u16, 6, 14) } }, "rs" }, + else => .{ .{ .mouse = .{ .button = .left, .kind = .press, .col = rect.x + 2, .row = rect.y + rect.h -| 1 } }, "cl" }, + }; + try trail.print(gpa, "{s}{s} ", .{ if (client) "C" else "S", name }); + if (client) { + try sent.append(gpa, try gpa.dupe(u8, try b.r.input(ev))); + } else { + try units.append(gpa, try gpa.dupe(u8, try b.a.own(ev))); + if (ev == .mouse) try units.append(gpa, try gpa.dupe(u8, try b.a.own(.{ .mouse = .{ .button = .left, .kind = .release, .col = ev.mouse.col, .row = ev.mouse.row } }))); + } + if (b.r.diverged) |why| { + std.debug.print("seed {d}: diverged {s}: {s}\n", .{ seed, why, trail.items }); + return error.TestExpectedEqual; + } + if (sent.items.len == 0 and units.items.len == 0 and b.r.quiet() and digestOf(b.a) != b.r.digest()) { + std.debug.print("seed {d}: digests differ: {s}\n", .{ seed, trail.items }); + return error.TestExpectedEqual; + } + } + } +} + test "bytes: a mouse motion a speculation told a terminal's program is told again when its input is replayed: the cell last told is the pane's, put back with it" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; @@ -4348,7 +4506,7 @@ fn firstOnPair(by_dump: bool) !void { try std.testing.expectEqual(reloads, pardes.Term.reloads.n); var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); - const rows = try pardes.Term.viewport(r.core().panes[shell].?.terminal.?, arena.allocator(), false); + const rows = try pardes.Term.viewport(r.core().panes[shell].?.terminal.?, arena.allocator(), false, r.core().panes[shell].?.scrolled_to); try std.testing.expect(std.mem.indexOf(u8, rows[0], "first$") != null); if (!by_dump) return; // One that comes after the shell's bytes: every side reloads. |
