summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-22 17:55:03 -0300
committerGabriel Schneider <[email protected]>2026-11-22 19:48:03 -0300
commit8e850e6c923dcd84b7a5483f73377bd192b94a2b (patch)
tree0730a84095f75d8a46e4dcdfebddb8d8b90af7c6 /core/replica.zig
parentbad506bd45846e012246a33b5d3097b3e92f02bc (diff)
downloadpardes-8e850e6c923dcd84b7a5483f73377bd192b94a2b.tar.gz
pardes-8e850e6c923dcd84b7a5483f73377bd192b94a2b.zip
terminals: where a view stands in a terminal's history is the view's, not the emulator's (pardes-term-join-10, item 1; the note's point 3, the coordinator's choice): `Pane.scrolled_to` (core/panes.zig), a per-view field (views.zig `pane_fields`, `PaneView`: in the rollback copy, the digest and the dump, each view's own), the row its top shows or null on the output, written only by a step (`scrollBy`, `followOutput`), and carried with every open view's through each write of the emulator that moves rows, once, in the confirmed unit's apply, on every side alike (`Term.feed`/`Term.resize` take the places; core/term/terminal.zig `carry`/`carried` pin each place's row on the primary screen and read the pin back: up by the rows the history drops, 0 when its own row goes, null in the active area or after a reset: a function of the fed bytes); the draw reads the rows from the view's place (`viewport`, `paint`, `ImagesAt.place`, terminal.zig `shown`), and ghostty's own viewport is never moved, read, digested or dumped (`Grid.offset`/`view`, `Parts.view` and the digest's offset gone): two clients no longer scroll one viewport (the new views.zig test failed on the base, `expected 80, found 77`), and a speculation's scroll is in the copy, so `putViews`, the view half of `keepViews` (now `keepSerials`), `viewTo`/`scroll`/`follow` (ops, wrappers, bodies), the Debug screen hash's view exclusion, `reflow`'s column-0 cure and `feedOutput`'s follow-after-read are deleted, and S1's `calls` lists no view write (the rows `viewTo`, `scroll`, `follow` gone); tests: the two-view test (with a dump), the place at the history's cap (`setMaxBytes(1)`), a 300-seed fuzz of the carried place against ghostty's own pinned view, a client's wheel over a shared terminal through output and a rollback, round 6's order explorer (30 seeds a run); the wire at 31 (abi/wire.zig, test/daemon.py `WIRE`): an installed build's dump loads with every view on the output; IN THE WORKSPACE core-test 912/1072 (160 skipped), shell-test 1114/1117 (3 skipped), term-test 16/16, kitty-test 45/45, pane-test 181/182, behaviour-test 275/275, --replay 275/275, process-test 0 of 146 failed, as pairs 0 of 146; gui unit-test 1707/1711, its one error BaselineHarnessMismatch (every base's)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig206
1 files changed, 182 insertions, 24 deletions
diff --git a/core/replica.zig b/core/replica.zig
index 3aac5e82..6072eea4 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();
}
}
@@ -4006,7 +3993,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();
@@ -4024,13 +4011,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);
@@ -4040,6 +4026,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;
@@ -4331,7 +4489,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.