From edbdfdd988870caaa11f4676d48c6cecc16e45be Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 18:15:20 -0300 Subject: terminal: a step that reads a terminal's rows as the core keeps them marks it read too: `fill` sent a client that came since (its history all holes) none of the rows the step read shellRows set `read` only when it built the rows, not when it handed out the ones kept since the last write, so after a client joined (or was started afresh) a step reading through the cache left the client's holes unsent, and it replayed the step on blanks. Found on the way to H18, whose fix is fixer 1's (wwkwwlzt). Test (failing before): a step that reads a terminal's rows as the core keeps them sends a new client the ones it holds as holes. Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/terminal.zig | 7 +++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/replica.zig b/src/replica.zig index 6a01ec26..c44eebd7 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -3635,3 +3635,72 @@ test "a client leaves, and one joins: what the leave queued (a terminal's resize try agrees(a, old); try agrees(a, joiner); } + +/// An input of a replica's in the session's order, as `Peers.input` takes +/// it: the terminals flushed to every peer, the step, the rows it read that +/// each peer holds as holes (`fill`), then its unit to every replica. +fn takeAll(a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, msg: []const u8) !void { + const gpa = std.testing.allocator; + for (rs, ps) |r, peer| try screensTo(a, screens, peer, r); + const u = try gpa.dupe(u8, try a.take(msg)); + defer gpa.free(u); + var rows: std.ArrayListUnmanaged([]u8) = .empty; + defer rows.deinit(gpa); + for (rs, ps) |r, peer| { + rows.clearRetainingCapacity(); + try screens.fill(a.core, peer, &rows); + for (rows.items) |m| { + _ = r.screen(m); + gpa.free(m); + } + } + screens.filled(a.core); + for (rs) |r| { + r.confirm(u); + r.settle(); + } +} + +fn sendAs(r: *Replica, a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, ev: pardes.Event) !void { + const msg = try std.testing.allocator.dupe(u8, try r.input(ev)); + defer std.testing.allocator.free(msg); + try takeAll(a, screens, rs, ps, msg); +} + +test "a step that reads a terminal's rows as the core keeps them sends a new client the ones it holds as holes" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r1 = try Replica.init(gpa, 1, first, true); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first, true); + defer r2.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.active = shell; + var line: [64]u8 = undefined; + for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "line {d} of the shell's history\r\n", .{i}) } }); + const out = try gpa.dupe(u8, (try a.pending()).?); + defer gpa.free(out); + for ([_]*Replica{ r1, r2 }) |r| r.confirm(out); + // The first client alone has the terminal; it reads all of its rows + // (its text, the cursor on the first): the core keeps them. + const p1 = [_]u16{try screens.addPeer()}; + for ([_]pardes.Event{ .{ .key = .{ .cp = 'b', .ctrl = true } }, .{ .key = .{ .cp = 'g', .text = "g" } }, .{ .key = .{ .cp = 'g', .text = "g" } } }) |ev| { + const msg = try gpa.dupe(u8, try r1.input(ev)); + defer gpa.free(msg); + try takeAll(a, &screens, &.{r1}, &p1, msg); + r2.confirm(a.out.items); + } + // A second comes: its history all holes. The first reads the rows the + // core kept, nothing written since. + const ps = [_]u16{ p1[0], try screens.addPeer() }; + try sendAs(r1, a, &screens, &.{ r1, r2 }, &ps, .{ .key = .{ .cp = 'O', .text = "O" } }); + for ([_]*Replica{ r1, r2 }) |r| try agrees(a, r); +} diff --git a/src/terminal.zig b/src/terminal.zig index 01a9777e..ade2311d 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -1800,7 +1800,12 @@ pub fn shellRows(p: *Pardes, pane: *Pane) ![]const []const u8 { if (comptime !enabled) return &empty_grid; if (pane.terminal == null) return &empty_grid; const c = &p.shell_rows; - if (!c.stale and c.pane == pane) return c.rows; + if (!c.stale and c.pane == pane) { + // A step's read whether the rows are kept or made: a client that + // came since holds them as holes, and is sent them (Screens.fill). + if (!drawing) pane.terminal.?.read = true; + return c.rows; + } // Another pane's rows, or a dropped pane's still waiting for the step's // sweep (dropPane keeps them, as they may still be borrowed): this // step's rows are scratch, and nothing in the slot is written over. -- cgit v1.3