summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 18:11:57 -0300
committerGabriel Schneider <[email protected]>2026-10-30 19:12:48 -0300
commitef0fb2f9614bd6ad35991022d7be1cb40ca156ae (patch)
tree0f814fd7832e875a680bb5cc6d024fb8facfa301 /src/replica.zig
parent782b1ef2e40b83e115c460f55e31c5c6bde2b62f (diff)
downloadpardes-ef0fb2f9614bd6ad35991022d7be1cb40ca156ae.tar.gz
pardes-ef0fb2f9614bd6ad35991022d7be1cb40ca156ae.zip
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 <[email protected]>
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig99
1 files changed, 99 insertions, 0 deletions
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));