From ef0fb2f9614bd6ad35991022d7be1cb40ca156ae Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 18:11:57 -0300 Subject: terminal: an authority with screen-state replicas sizes a terminal after the step's unit, not inside it (resize_to, made by Peers.unit, null from `create` on): a client typing at another size than the last typist read history rows its replay held as holes, and the rows the session sent for them came in the new grid's numbering, which the replica cannot take, so its replay read blank text (the soak's local80 cell: jumps, njumps, jcur, pane 1.body at a b; fixer 2's H18, pane N.ovl); test/replicas.zig sizes them after its fill too; an in-process test reads the history in the step that resizes, with the session's order of flush, fill and unit, and another makes a terminal from 0xAA memory and finds no size waiting Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tape.zig | 5 +++ src/terminal.zig | 59 +++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) (limited to 'src') diff --git a/src/replica.zig b/src/replica.zig index c44eebd7..1cf68db2 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1655,6 +1655,8 @@ pub const Peers = struct { const head = wire.header(@backingInt(wire.ServerTag.unit), unit_head + bytes.len); for (ps.list) |slot| if (slot) |peer| peer.send(&.{ &head, &ps.head, bytes }); tape.clearTaken(&ps.t); + // The sizes its steps gave terminals, now: the next flush says so. + if (ps.t.core) |core| terminal.resizeDeferred(core); } /// After each of the session's pumps: its unit sent, the clients that @@ -1989,6 +1991,7 @@ pub const Peers = struct { fn stop(ps: *Peers, core: *Pardes) void { if (!ps.recording) return; + terminal.resizeDeferred(core); ps.screens.deinit(core); if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; tape.on = null; @@ -3121,6 +3124,102 @@ test "a view that sized a terminal closing leaves nothing queued: the others' re try std.testing.expectEqual(digestOf(a), r1.digest()); } +/// What the session sends a peer around a step's unit (Peers.input): the +/// rows the step read that the peer lacks, then the sizes the step gave +/// terminals (`terminal.resizeDeferred`), then the unit. +fn fillTo(a: *Authority, screens: *Screens, peer: u16, r: *Replica) !void { + const gpa = std.testing.allocator; + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + try screens.fill(a.core, peer, &msgs); + for (msgs.items) |m| if (r.screen(m)) |reply| { + _ = try screens.take(a.core, peer, reply); + }; + screens.filled(a.core); + terminal.resizeDeferred(a.core); +} + +test "a terminal sized again by a client's key: the step reads the grid it had, on both sides" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 50, .rows = 20, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.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; + // History the replica holds as holes, of lines a narrow grid wraps in + // two, which a wider one unwraps. + var line: [96]u8 = undefined; + for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "{d:0>4} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + // Out of terminal mode in the session's view, which sizes it; then a + // client's own view (a copy of it), wider: its first key sizes the + // terminal again, in that key's step (the soak's tty and window at two + // sizes, 2026-10-30: the client's replay read holes the session's step + // had read as text, and the fill came in the new grid's numbers). + r.confirm(try a.local(.{ .key = .{ .cp = 'b', .ctrl = true } })); + for ([_]u21{ 'g', 'g' }) |cp| r.confirm(try a.local(.{ .key = .{ .cp = cp, .text = &.{@intCast(cp)} } })); + const opened = try a.openView(1); + r.view = opened.view; + r.confirm(opened.unit); + try screensTo(a, &screens, peer, r); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + const Step = struct { + fn take(au: *Authority, sc: *Screens, pe: u16, rr: *Replica, msg: []const u8) !void { + try screensTo(au, sc, pe, rr); + const u = try std.testing.allocator.dupe(u8, try au.take(msg)); + defer std.testing.allocator.free(u); + try fillTo(au, sc, pe, rr); + rr.confirm(u); + rr.settle(); + } + }; + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .resize = .{ .cols = 120, .rows = 30 } }))); + try key(r, 'g', &sent); + try key(r, 'g', &sent); + for (sent.items) |m| try Step.take(a, &screens, peer, r, m); + for (sent.items) |m| gpa.free(m); + sent.clearRetainingCapacity(); + // The session's view types into it, which sizes it back (the client's + // copy of its history all holes again, a new numbering); then the + // client's key that reads all of it, and sizes it again. + try screensTo(a, &screens, peer, r); + { + const was = Stepping.begin(&a.t, null); + a.core.toView(0); + a.core.update(.{ .key = .{ .cp = 'k', .text = "k" } }); + a.core.step(); + was.end(); + } + a.seq += 1; + const back = try gpa.dupe(u8, try a.unit(0, a.seq)); + defer gpa.free(back); + try fillTo(a, &screens, peer, r); + r.confirm(back); + r.settle(); + try screensTo(a, &screens, peer, r); + try key(r, 'w', &sent); + try key(r, 'w', &sent); + for (sent.items) |m| try Step.take(a, &screens, peer, r, m); + try screensTo(a, &screens, peer, r); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + /// p50 and p99 of `samples`, sorted in place. fn spread(samples: []u64) [2]u64 { std.mem.sort(u64, samples, {}, std.sort.asc(u64)); diff --git a/src/tape.zig b/src/tape.zig index 9152bcb0..1ca064bc 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -990,6 +990,11 @@ pub fn screenReplica() bool { return if (on) |t| t.screen_state and (t.replay or t.speculating) else false; } +/// An authority recording for screen-state replicas. +pub fn screenAuthority() bool { + return if (on) |t| t.screen_state and !t.replay and !t.speculating else false; +} + /// What a speculation reached is the authority's: the input is undone and /// waits for the authority's order. pub fn poison() void { diff --git a/src/terminal.zig b/src/terminal.zig index ade2311d..e9d628aa 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -172,6 +172,11 @@ pub const State = struct { /// step must hold them, so its authority sends the ones it does not /// before the step's unit (replica.Screens.fill). read: bool = false, + /// The size a step gave it, on an authority with screen-state replicas: + /// made after the step's unit (`resizeDeferred`), so that the rest of + /// the step reads the grid it had, as its replicas' replays do, and the + /// rows sent them for it (Screens.fill) are of that grid's numbering. + resize_to: ?struct { cols: u16, rows: u16 } = null, /// The emulator as its program has made it, for the replay digest /// (src/walk.zig): the active screen's text, how much history is above @@ -646,11 +651,30 @@ pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) voi }; // A replica's emulator is sized by its authority: the next delta says. if (pardes.tape.screenReplica()) return; + // ...and the authority's once the step's unit is out (a client typing + // at another size than the last typist's read history rows its replay + // held as holes, and the rows came renumbered, 2026-10-30). + if (pardes.tape.screenAuthority()) { + state.resize_to = .{ .cols = cols, .rows = rows }; + return; + } state.changes +%= 1; _ = gpa; state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {}; } +/// The sizes steps gave terminals (`resizeGrid`), made: between units, on an +/// authority with screen-state replicas (replica.Peers.unit). +pub fn resizeDeferred(p: *Pardes) void { + if (comptime !enabled) return; + for (p.panes) |slot| if (slot) |pane| if (pane.terminal) |state| if (state.resize_to) |size| { + state.resize_to = null; + state.changes +%= 1; + p.shell_rows.markStale(pane); + state.vt.resize(state.gpa, .{ .cols = size.cols, .rows = size.rows }) catch {}; + }; +} + /// DECSET 2004: the program wants its pastes bracketed. pub fn bracketedPaste(pane: *const Pane) bool { if (comptime !enabled) return false; @@ -888,6 +912,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { state.reply_len = 0; state.changes = 0; state.read = false; + state.resize_to = null; // Every field its default (create leaves them what the heap had, which // a digest then reads: two replicas must agree, src/replica.zig). state.reply_dropped = 0; @@ -960,6 +985,40 @@ test "replay ownership is terminal-only and construction rolls back on allocatio try std.testing.checkAllAllocationFailures(std.testing.allocator, Case.run, .{true}); } +test "a terminal made fresh has no size waiting: one never resized keeps its grid when a unit is cut" { + if (comptime !enabled) return error.SkipZigTest; + // Fresh memory as a Debug build's allocator hands it out: 0xAA. A field + // `create` left unset read a size of 43690x43690 (fixer 2's, 2026-10-30). + const Filling = struct { + child: std.mem.Allocator, + fn alloc(ctx: *anyopaque, len: usize, a: std.mem.Alignment, ra: usize) ?[*]u8 { + const self: *@This() = @ptrCast(@alignCast(ctx)); + const ptr = self.child.rawAlloc(len, a, ra) orelse return null; + @memset(ptr[0..len], 0xAA); + return ptr; + } + fn resize(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) bool { + const self: *@This() = @ptrCast(@alignCast(ctx)); + return self.child.rawResize(buf, a, n, ra); + } + fn remap(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) ?[*]u8 { + const self: *@This() = @ptrCast(@alignCast(ctx)); + return self.child.rawRemap(buf, a, n, ra); + } + fn free(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, ra: usize) void { + const self: *@This() = @ptrCast(@alignCast(ctx)); + self.child.rawFree(buf, a, ra); + } + const vtable: std.mem.Allocator.VTable = .{ .alloc = alloc, .resize = resize, .remap = remap, .free = free }; + }; + var filling: Filling = .{ .child = std.testing.allocator }; + const gpa: std.mem.Allocator = .{ .ptr = &filling, .vtable = &Filling.vtable }; + const pane = try create(gpa, 40, 12); + defer gpa.destroy(pane); + defer deinitEmulator(pane, gpa); + try std.testing.expect(pane.terminal.?.resize_to == null); +} + test "document construction allocates only the pane and has inert terminal defaults" { var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 1 }); const allocator = failing.allocator(); -- cgit v1.3