From 7b2f856eda8d0c14162bc5c8ac2836b6cd73d37f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 12:42:50 -0300 Subject: replicas: the soak does what the real clients do, and the tab width a rollback left behind: each client a process of its own (config.tab_now its own) joined from the session's dump through the real replica.Link on a socket pair the soak reads as the session's Peer does, a terminal's loop or a window's by turns (its view drawn, the rows it lacks asked after the draw, ready while idle, within and its resize for a window, the frontend input and its size at the join, the quiet digest check), the session's own frontend typing in its view and drawing it at its frames, one client joining late, one leaving, one put off the session (PARDES_TEST_DIVERGE) and joining again for its view, and those past the three views a session gives clients refused until one is let go (five by default; --shared gone: no session does it); Authority.join, leave and own as Peers.join, Peers.leave and the own frontend's input do it; pardes.wire exported for the soak; what it found: config.tab_now, a process global a Tab step writes, was not put back by a rollback (toConfirmed): with tabs in its files the soak's fifth client diverged four times at rtt 207 (seed 2), and with the global shared as before the client's draw hit File.bodyText's assert; fixed in toConfirmed, with a test that fails without it; replicas 4 seeds, five clients, rtt 30/207: diverged 0, unequal 0, deltas checked/wrong 1782/0, 1871/0, quiet checks 582/0, 791/0, joins 18/20, refused 313/282, rejoined 4/4, left 4/4 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/pardes.zig | 1 + src/replica.zig | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) (limited to 'src') diff --git a/src/pardes.zig b/src/pardes.zig index 48c87bf6..0bedab03 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -77,6 +77,7 @@ pub const config = @import("config.zig"); pub const memory = @import("memory.zig"); pub const tape = @import("tape.zig"); pub const walk = @import("walk.zig"); +pub const wire = @import("wire.zig"); pub const views = @import("views.zig"); pub const rollback = @import("rollback.zig"); pub const replica = @import("replica.zig"); diff --git a/src/replica.zig b/src/replica.zig index e5d5ad16..4140beca 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -218,6 +218,16 @@ pub const Authority = struct { return a.unit(0, a.seq); } + /// An input of its own frontend's, in the view that frontend shows + /// (the first: tui.zig `Shell.handle` goes into `s.own`), as its unit. + pub fn own(a: *Authority, ev: pardes.Event) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + a.core.toView(0); + was.end(); + return a.local(ev); + } + /// A step of its own (a pty's output) into the open batch: under a /// flood a replica gets a unit a frame, not one a read (each unit of the /// authority's rolls back a replica's pending inputs), and its digest @@ -256,6 +266,51 @@ pub const Authority = struct { return .{ .view = k, .unit = try a.unit(0, a.seq) }; } + /// A client joining as `Peers.join` takes one: its view kept for it + /// (`back`, after a divergence) or a new one, null when every view is + /// taken (refused); the unit that says so to the others (valid until the + /// next call), and the dump it loads, its blobs in place (`gpa`'s), with + /// the digest it checks its load by. + pub fn join(a: *Authority, client: u16, back: ?u8) !?struct { view: u8, unit: []const u8, dump: []u8, digest: u64 } { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const again = if (back) |v| a.core.rejoinView(v) else false; + const k = (if (again) back else a.core.openView()) orelse return null; + a.core.undrawn |= @as(u8, 1) << @intCast(k); + a.view_of[client] = k; + a.seq += 1; + const u = try a.unit(0, a.seq); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + tape.resync(&a.t); + var whole: std.ArrayListUnmanaged(u8) = .empty; + errdefer whole.deinit(a.gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try whole.appendSlice(a.gpa, state[pos..b.at]); + try whole.appendSlice(a.gpa, b.bytes); + pos = b.at; + } + try whole.appendSlice(a.gpa, state[pos..]); + return .{ .view = k, .unit = u, .dump = try whole.toOwnedSlice(a.gpa), .digest = tape.digestOf(a.core) }; + } + + /// Client `client` gone as `Peers.leave` lets one go: its view kept for + /// it (`again`: it joins again for it) or closed; the unit. + pub fn leave(a: *Authority, client: u16, again: bool) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + const k = a.view_of[client]; + a.core.undrawn &= ~(@as(u8, 1) << @intCast(k)); + if (again) a.core.awayView(k) else a.core.closeView(k); + was.end(); + a.seq += 1; + return a.unit(0, a.seq); + } + /// Client `client`'s view gone (its link ended), as its unit. pub fn closeView(a: *Authority, client: u16) ![]const u8 { std.debug.assert(a.batched == 0); @@ -1013,6 +1068,9 @@ pub const Replica = struct { // was there before is of a grid the deltas since moved on // from (a rollback never brings a terminal's past back). r.core().shell_rows = .{}; + // A tab's width is the process's (config.tab_now), which a + // step that set it (Tab) wrote and the copy does not hold. + pardes.config.tab_now = r.core().settings.tab; } r.applied = 0; // Its views as the copy was taken, or as the last delta set them @@ -3558,6 +3616,30 @@ test "another client's input first: a replica rolls back, replays it and applies try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); } +test "a rollback over a Tab it applied puts the process's tab width back as its core has it" { + // config.tab_now is the process's, written by the step that set the + // width (the soak, five clients: a Tab undone left it, and the next + // replayed step measured the files' tabs otherwise). + const gpa = std.testing.allocator; + defer pardes.config.tab_now = pardes.config.tab_width; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r = try Replica.init(gpa, 1, first, false); + defer r.deinit(); + pardes.config.tab_now = r.core().settings.tab; + const was = r.core().settings.tab; + const msg = try gpa.dupe(u8, try r.input(.{ .command = "Tab 2" })); + defer gpa.free(msg); + try std.testing.expectEqual(@as(usize, 1), r.applied); + try std.testing.expectEqual(@as(u16, 2), pardes.config.tab_now); + // The session's own key first: back to the confirmed position. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + try std.testing.expectEqual(was, r.core().settings.tab); + try std.testing.expectEqual(was, pardes.config.tab_now); +} + test "a delta applied under a speculation outlives its rollback: the terminal stays the authority's newest" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; -- cgit v1.3