From 64184a16e58094f6b861cd1027920ddf21d9df0d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 21:20:35 -0300 Subject: screen-state replicas: a rollback never brings a terminal's past back: the rows the core keeps read of a terminal marked stale after a delta and after a copy is put back, and a test that a delta applied under a speculation outlives its rollback replica.zig: the core's cache of a terminal's rows (shell_rows) is invalidated by feedOutput, which a replica no longer calls; a delta and fetched rows now mark it, and so does a rollback, whose copy holds whatever rows were read before it was taken (a replica draws between steps). The test types into the text pane, lets the shell print under the held copy, lands its delta, reads the rows, and rolls back: the terminal is the authority's newest, the history as long, the rows the screen's. Without the stale mark it reads an older grid's rows; without the epoch's pause the rollback frees the emulator's pages and it crashes. Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) (limited to 'src') diff --git a/src/replica.zig b/src/replica.zig index a18391d4..92ee7b37 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -571,6 +571,9 @@ pub const Replica = struct { if (r.copy.held) { r.copy.restore(); rollback.Epoch.select(null); + // The copy's terminal rows are of a grid the deltas since moved + // on from: a rollback never brings a terminal's past back. + r.core().shell_rows.stale = true; } r.applied = 0; for (r.since.items) |u| if (tape.replayUnit(&r.t, u)) |why| return r.fail(why); @@ -703,6 +706,8 @@ pub const Replica = struct { r.stats.apply_ns.append(r.gpa, nowNs() - t0) catch {}; sc.refused = false; terminal.takeMarks(state, applied.extra); + // Its rows as the core keeps them read are another grid's. + c.shell_rows.markStale(pane); var seq: [4]u8 = undefined; std.mem.writeInt(u32, &seq, applied.seq, .little); return r.screenMessage(m.pane, m.serial, kind_ack, &seq); @@ -710,6 +715,7 @@ pub const Replica = struct { kind_rows => { sc.asked = null; sc.rep.applyRows(r.coreGpa(), &state.vt, m.payload) catch r.fail("a terminal's rows did not apply"); + c.shell_rows.markStale(pane); return null; }, else => return null, @@ -855,3 +861,105 @@ test "another client's input first: a replica rolls back, replays it and applies try std.testing.expect(r1.stats.foreign >= 1); try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); } + +fn screensTo(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.flush(a.core, peer, &msgs); + for (msgs.items) |m| if (r.screen(m)) |reply| { + _ = try screens.take(a.core, peer, reply); + }; +} + +test "a delta applied under a speculation outlives its rollback: the terminal stays the authority's newest" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .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; + // Typing goes to the text pane under the shell (the host's write of + // `active`, taped). + a.core.active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.file != null) break id; + } else return error.NoTextPane; + r.confirm(try a.local(.{ .focus = true })); + try screensTo(a, &screens, peer, r); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Typed into the file, at once: a copy is held. + try key(r, 'o', &sent); + try std.testing.expect(r.copy.held); + var line: [64]u8 = undefined; + for (0..3) |round| { + // The shell prints: its unit rolls the replica back and applies the + // key again; then its delta lands, under the new copy. + for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "round {d} line {d}\r\n", .{ round, i }) } }); + r.confirm((try a.pending()).?); + r.settle(); + try std.testing.expect(r.copy.held); + try screensTo(a, &screens, peer, r); + // The rows the core keeps, read once under the copy. + _ = try terminal.shellRows(r.core(), r.core().panes[shell].?); + } + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + gpa.free(m); + } + sent.clearRetainingCapacity(); + try screensTo(a, &screens, peer, r); + // Drawn (its rows read, between steps), then typed into: the copy keeps + // those rows. The shell prints, its delta lands under the copy, and then + // another's unit puts the copy back: the rows it kept are of an older + // grid and must not be read. + r.confirm(try a.local(.{ .focus = false })); + _ = try terminal.shellRows(r.core(), r.core().panes[shell].?); + try key(r, 'x', &sent); + for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "last line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + r.settle(); + try screensTo(a, &screens, peer, r); + r.confirm(try a.local(.{ .focus = true })); + r.settle(); + const n = a.core.panes[shell].?.rows; + const back = try terminal.shellRows(r.core(), r.core().panes[shell].?); + const now = try terminal.shellRows(a.core, a.core.panes[shell].?); + for (now[now.len - n ..], back[back.len - n ..]) |x, y| try std.testing.expectEqualStrings(x, y); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + 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()); + var dump_a: std.Io.Writer.Allocating = .init(gpa); + defer dump_a.deinit(); + var dump_r: std.Io.Writer.Allocating = .init(gpa); + defer dump_r.deinit(); + try screen_state.dumpScreen(&a.core.panes[shell].?.terminal.?.vt, &dump_a.writer); + try screen_state.dumpScreen(&r.core().panes[shell].?.terminal.?.vt, &dump_r.writer); + try std.testing.expectEqualStrings(dump_a.written(), dump_r.written()); + const rows_a = try terminal.shellRows(a.core, a.core.panes[shell].?); + const rows_r = try terminal.shellRows(r.core(), r.core().panes[shell].?); + // As many rows (the history is the authority's to the row), the screen's + // the same; above it, rows scrolled past more than a screen at once are + // holes until fetched. + try std.testing.expectEqual(rows_a.len, rows_r.len); + const shown = a.core.panes[shell].?.rows; + for (rows_a[rows_a.len - shown ..], rows_r[rows_r.len - shown ..]) |x, y| try std.testing.expectEqualStrings(x, y); +} -- cgit v1.3