summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 14:29:10 -0300
committerGabriel Schneider <[email protected]>2026-11-20 14:55:38 -0300
commite3d0daa9f89f41b41b7d05d510db553cbd8816d9 (patch)
tree6602e8aca69ec3c160021d4975c43456d38801df /core/replica.zig
parent8a8ce0a6740e9a98ea37a70d401c3ba3fffe0ee1 (diff)
parent27837e6345b51ceb2ceaaee58fd593024feb07c8 (diff)
downloadpardes-e3d0daa9f89f41b41b7d05d510db553cbd8816d9.tar.gz
pardes-e3d0daa9f89f41b41b7d05d510db553cbd8816d9.zip
integration: a client that joins a session with 9P work under way has its 9P state, on the budget as a count and the window's pair: pardes-faults-4 (the open records and each pane's 9P state in the dumps and the digest, dropped again at a revive on a new host; hand-written digests so a step's counts do not move) and pardes-merge39; two conflicts, both the wire's version: the pair's close byte and the 9P state each took 24 on their own lines: the close byte keeps 24, the 9P state is 25, abi/wire.zig's version and test/daemon.py's WIRE are 25; tui core-test 871/1028 (157 skipped), shell-test 1070/1073, term-test 17/17, both soaks diverged 0, unequal 0, unlisted 0, the window builds; the coordinator's gate in the ledger (reorg/merge40)
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig337
1 files changed, 337 insertions, 0 deletions
diff --git a/core/replica.zig b/core/replica.zig
index 49787c76..ff1e138e 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -7097,3 +7097,340 @@ test "a far client's Del with panes above and below is applied at once, and its
try agrees(a, r);
try std.testing.expectEqual(Clocked.now_ns + pardes.config.del_question_delay_ns, r.core().panes[mid].?.question_due_ns);
}
+
+// ---- a client that joins a session with 9P work under way ----
+//
+// A 9P request is an input every replica replays, so the state the 9P
+// tree builds from the requests (the open records, each pane's `fs`) is
+// a client's for everything after its join: what was made before it
+// comes in the join's dump, or the client steps the next request
+// otherwise than its session.
+
+/// A session with a file pane and its 9P server, and a client joined
+/// from its dump (`join`) that takes every unit after.
+const NinepJoin = struct {
+ a: *Authority,
+ late: ?*Replica = null,
+ serial: u32,
+
+ fn init(text: []const u8, host: pardes.Host) !NinepJoin {
+ const a = try Authority.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, host, false);
+ errdefer a.deinit();
+ const was = Stepping.begin(&a.t, null);
+ const pane = try a.core.setTestFile(text);
+ was.end();
+ _ = try a.unit(0, 0);
+ return .{ .a = a, .serial = pane.serial };
+ }
+
+ fn deinit(n: *NinepJoin) void {
+ if (n.late) |r| r.deinit();
+ n.a.deinit();
+ }
+
+ fn node(n: *const NinepJoin, file: pardes.ctlfs.PaneFile) u64 {
+ return pardes.ctlfs.Node.of(n.serial, file);
+ }
+
+ /// One request as the session's 9P server serves it, a step of its
+ /// own, then (`pumped`) its loop's pass: a join can come between the
+ /// two, the session's loop being elsewhere. Its unit to the client.
+ fn serve(n: *NinepJoin, req: pardes.ctlfs.Req, pumped: bool) !pardes.ctlfs.Reply {
+ const a = n.a;
+ const was = Stepping.begin(&a.t, null);
+ const reply = a.core.serveFs(req);
+ if (pumped) a.core.pump(a.host) catch {};
+ was.end();
+ a.seq += 1;
+ try n.send(try a.unit(0, a.seq));
+ return reply;
+ }
+
+ /// A pass of the session's loop with no request: its unit.
+ fn pump(n: *NinepJoin) !void {
+ n.a.pump();
+ try n.send((try n.a.pending()).?);
+ }
+
+ fn send(n: *NinepJoin, u: []const u8) !void {
+ const r = n.late orelse return;
+ const kept = try std.testing.allocator.dupe(u8, u);
+ defer std.testing.allocator.free(kept);
+ r.confirm(kept);
+ r.settle();
+ }
+
+ /// A client joins now, from the session's dump, at the digest the
+ /// session had.
+ fn join(n: *NinepJoin) !void {
+ const joined = (try n.a.join(1, null)).?;
+ defer std.testing.allocator.free(joined.dump);
+ const r = try Replica.initDump(std.testing.allocator, 1, joined.view, joined.dump, .{ .cols = 80, .rows = 24 });
+ r.t.screen_state = false;
+ r.core().echo = null;
+ n.late = r;
+ try std.testing.expectEqual(joined.digest, r.digest());
+ }
+
+ fn agree(n: *NinepJoin) !void {
+ try agrees(n.a, n.late.?);
+ }
+};
+
+test "a client joined while a script's open of a pane's exec holds half a line runs the line its session runs" {
+ var n = try NinepJoin.init("one\n", TestHost.host());
+ defer n.deinit();
+ const exec = n.node(.exec);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = exec, .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = exec, .handle = h, .data = "Ne" }, true);
+ try n.join();
+ _ = try n.serve(.{ .tag = 3, .op = .write, .node = exec, .handle = h, .data = "w\n" }, true);
+ _ = try n.serve(.{ .tag = 4, .op = .release, .node = exec, .handle = h, .opened = true }, true);
+ try n.agree();
+}
+
+test "a client joined between two writes of one open to a body makes them one undo step, as its session does" {
+ var n = try NinepJoin.init("one\n", TestHost.host());
+ defer n.deinit();
+ const body = n.node(.body);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = body, .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = body, .handle = h, .data = "a\n" }, true);
+ try n.join();
+ _ = try n.serve(.{ .tag = 3, .op = .write, .node = body, .handle = h, .data = "b\n" }, true);
+ _ = try n.serve(.{ .tag = 4, .op = .release, .node = body, .handle = h, .opened = true }, true);
+ _ = try n.serve(.{ .tag = 5, .op = .write, .node = n.node(.ctl), .data = "Undo" }, true);
+ try n.agree();
+}
+
+test "a client joined while a script's ctl open holds a pane's lock refuses another open's lock, as its session does" {
+ var n = try NinepJoin.init("one\n", TestHost.host());
+ defer n.deinit();
+ const ctl = n.node(.ctl);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = ctl, .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = ctl, .handle = h, .data = "lock\n" }, true);
+ try n.join();
+ const g = (try n.serve(.{ .tag = 3, .op = .open, .node = ctl, .omode = 1 }, true)).handle;
+ try std.testing.expectEqual(pardes.ctlfs.Status.err, (try n.serve(.{ .tag = 4, .op = .write, .node = ctl, .handle = g, .data = "lock\n" }, true)).status);
+ // Who holds it, by name: on the base no digest covered it.
+ const id = n.a.core.paneBySerial(n.serial).?;
+ try std.testing.expectEqual(n.a.core.panes[id].?.fs.lock, n.late.?.core().panes[id].?.fs.lock);
+ try n.agree();
+ _ = try n.serve(.{ .tag = 5, .op = .release, .node = ctl, .handle = g, .opened = true }, true);
+ _ = try n.serve(.{ .tag = 6, .op = .release, .node = ctl, .handle = h, .opened = true }, true);
+ try n.agree();
+}
+
+test "a client joined after a script set a pane's addr, its limit, or truncated its dot writes where its session writes" {
+ // addr: the next data write replaces what it says.
+ {
+ var n = try NinepJoin.init("one two\nthree\n", TestHost.host());
+ defer n.deinit();
+ _ = try n.serve(.{ .tag = 1, .op = .write, .node = n.node(.addr), .data = "/two/" }, true);
+ try n.join();
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = n.node(.data), .data = "2" }, true);
+ try n.agree();
+ }
+ // limit: an address is looked for inside it.
+ {
+ var n = try NinepJoin.init("one two\nthree\n", TestHost.host());
+ defer n.deinit();
+ _ = try n.serve(.{ .tag = 1, .op = .write, .node = n.node(.limit), .data = "#0,#3" }, true);
+ try n.join();
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = n.node(.addr), .data = "/two/" }, true);
+ _ = try n.serve(.{ .tag = 3, .op = .write, .node = n.node(.data), .data = "2" }, true);
+ try n.agree();
+ }
+ // dot truncated: its open's release with no write empties it.
+ {
+ var n = try NinepJoin.init("one two\nthree\n", TestHost.host());
+ defer n.deinit();
+ const dot = n.node(.dot);
+ _ = try n.serve(.{ .tag = 1, .op = .write, .node = dot, .data = "#0,#3" }, true);
+ const h = (try n.serve(.{ .tag = 2, .op = .open, .node = dot, .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 3, .op = .setattr, .node = dot, .handle = h, .truncate = true }, true);
+ try n.join();
+ _ = try n.serve(.{ .tag = 4, .op = .release, .node = dot, .handle = h, .opened = true }, true);
+ try n.agree();
+ }
+}
+
+test "a client joined between two writes of one open to a pane's sel runs the second on from the first, as its session does" {
+ var n = try NinepJoin.init("one two\n", TestHost.host());
+ defer n.deinit();
+ const sel = n.node(.sel);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = sel, .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = sel, .handle = h, .data = "a" }, true);
+ try n.join();
+ _ = try n.serve(.{ .tag = 3, .op = .write, .node = sel, .handle = h, .data = "b" }, true);
+ _ = try n.serve(.{ .tag = 4, .op = .release, .node = sel, .handle = h, .opened = true }, true);
+ try n.agree();
+}
+
+test "a client joined after a script's nomark or noscroll writes as its session does" {
+ // nomark: writes under it are one undo step.
+ {
+ var n = try NinepJoin.init("one\n", TestHost.host());
+ defer n.deinit();
+ _ = try n.serve(.{ .tag = 1, .op = .write, .node = n.node(.ctl), .data = "nomark" }, true);
+ try n.join();
+ // Two opens' writes, each its own undo step but for nomark.
+ const body = n.node(.body);
+ for ([_][]const u8{ "two\n", "three\n" }) |text| {
+ const h = (try n.serve(.{ .tag = 2, .op = .open, .node = body, .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 3, .op = .write, .node = body, .handle = h, .data = text }, true);
+ _ = try n.serve(.{ .tag = 4, .op = .release, .node = body, .handle = h, .opened = true }, true);
+ }
+ _ = try n.serve(.{ .tag = 5, .op = .write, .node = n.node(.ctl), .data = "mark" }, true);
+ _ = try n.serve(.{ .tag = 6, .op = .write, .node = n.node(.ctl), .data = "Undo" }, true);
+ try n.agree();
+ }
+ // noscroll: a write past the screen leaves the view where it was.
+ {
+ var n = try NinepJoin.init("one\n", TestHost.host());
+ defer n.deinit();
+ _ = try n.serve(.{ .tag = 1, .op = .write, .node = n.node(.scroll), .data = "0" }, true);
+ try n.join();
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = n.node(.body), .data = &@as([100]u8, @splat('\n')) }, true);
+ try n.agree();
+ }
+}
+
+test "a client joined after a script truncated a pane's tag writes the tag its session writes" {
+ var n = try NinepJoin.init("one\n", TestHost.host());
+ defer n.deinit();
+ const tag = n.node(.tag);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = tag, .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 2, .op = .setattr, .node = tag, .handle = h, .truncate = true }, true);
+ try n.join();
+ _ = try n.serve(.{ .tag = 3, .op = .write, .node = tag, .handle = h, .data = "Get Put\n" }, true);
+ _ = try n.serve(.{ .tag = 4, .op = .release, .node = tag, .handle = h, .opened = true }, true);
+ try n.agree();
+}
+
+test "a client joined after a script truncated the workspace's tag writes the tag its session writes" {
+ var n = try NinepJoin.init("one\n", TestHost.host());
+ defer n.deinit();
+ const tag: u64 = @backingInt(pardes.ctlfs.TopFile.tag);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = tag, .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 2, .op = .setattr, .node = tag, .handle = h, .truncate = true }, true);
+ try n.join();
+ _ = try n.serve(.{ .tag = 3, .op = .write, .node = tag, .handle = h, .data = "Exit\n" }, true);
+ _ = try n.serve(.{ .tag = 4, .op = .release, .node = tag, .handle = h, .opened = true }, true);
+ try n.agree();
+}
+
+test "a client joined while a chorded exec waits for its event reader's write-back runs it with its argument, as its session does" {
+ var n = try NinepJoin.init("Msg said\narg-text\n", TestHost.host());
+ defer n.deinit();
+ const event = n.node(.event);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = event, .omode = 2 }, true)).handle;
+ // The session's chorded click, told to the reader, its argument kept.
+ {
+ const was = Stepping.begin(&n.a.t, null);
+ const id = n.a.core.paneBySerial(n.serial).?;
+ try std.testing.expect(pardes.mouse.reportRange(n.a.core, id, pardes.config.exec_cmd, "Msg", false, .{ .q0 = 0, .q1 = 3 }, "arg-text"));
+ was.end();
+ n.a.seq += 1;
+ _ = try n.a.unit(0, n.a.seq);
+ }
+ try n.join();
+ // The command's record written back alone: its argument is the one kept.
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = event, .handle = h, .data = "MX0 3 9 3 Msg\n" }, true);
+ _ = try n.serve(.{ .tag = 3, .op = .release, .node = event, .handle = h, .opened = true }, true);
+ try n.agree();
+}
+
+test "a client joined while a script's writes to a body are held to go in as one edit puts them in as its session does" {
+ // A host with a clock: held writes go in once they have paused
+ // (pane.batch_pause_ns), and the join came before that.
+ const Clock = struct {
+ var now_ns: u64 = 5 * std.time.ns_per_s;
+ fn now(_: ?*anyopaque) u64 {
+ return now_ns;
+ }
+ const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .now = now };
+ };
+ var n = try NinepJoin.init("one\n", .{ .vtable = &Clock.vtable });
+ defer n.deinit();
+ const body = n.node(.body);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = body, .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = body, .handle = h, .data = "two\n" }, true);
+ try std.testing.expect(n.a.core.fs.batch.bytes.items.len > 0);
+ try n.join();
+ Clock.now_ns += std.time.ns_per_s;
+ try n.pump();
+ try n.agree();
+ _ = try n.serve(.{ .tag = 3, .op = .release, .node = body, .handle = h, .opened = true }, true);
+ try n.agree();
+}
+
+/// A session with a shell, for a run's 9P state across a join: a host
+/// whose pty takes the line and whose tty nobody else holds.
+const RunJoin = struct {
+ fn taken(_: ?*anyopaque, _: pardes.Pane.Id) bool {
+ return false;
+ }
+ fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {}
+ const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write };
+ const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
+
+ /// The session at its shell's first prompt, and the shell's slot.
+ fn init(n: *NinepJoin) !u8 {
+ const a = try Authority.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &vtable }, false);
+ n.* = .{ .a = a, .serial = 0 };
+ _ = try a.unit(0, 0);
+ const shell: u8 = for (a.core.panes, 0..) |slot, id| {
+ if (slot) |pane| if (pane.isTerminal()) break @intCast(id);
+ } else return error.NoShell;
+ n.serial = a.core.panes[shell].?.serial;
+ return shell;
+ }
+
+ fn output(n: *NinepJoin, shell: u8, bytes: []const u8) !void {
+ n.a.output(.{ .output = .{ .pane = .at(shell), .bytes = bytes } });
+ try n.send((try n.a.pending()).?);
+ }
+};
+
+test "a client joined while a script's pty/run waits on the command it sent ends the run as its session does" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ var n: NinepJoin = undefined;
+ const shell = try RunJoin.init(&n);
+ defer n.deinit();
+ // The shell's greeting (`ls`) run at its first prompt, done.
+ try RunJoin.output(&n, shell, RunJoin.prompt ++ "ls\r\n\x1b]133;C;aid=pardes\x07a b\r\n\x1b]133;D;0;aid=pardes\x07" ++ RunJoin.prompt);
+ const run = n.node(.pty_run);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = run, .omode = 2 }, true)).handle;
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = run, .handle = h, .data = "true\n" }, true);
+ try n.join();
+ try RunJoin.output(&n, shell, "true\r\n\x1b]133;C;aid=pardes\x07\x1b]133;D;0;aid=pardes\x07" ++ RunJoin.prompt);
+ const answer = try n.serve(.{ .tag = 3, .op = .read, .node = run, .handle = h, .size = 64 }, true);
+ try std.testing.expectEqualStrings("exit 0\n", n.a.core.fsPayload(answer));
+ // The open's next line, sent at the prompt the first left.
+ _ = try n.serve(.{ .tag = 4, .op = .write, .node = run, .handle = h, .data = "false\n" }, true);
+ try RunJoin.output(&n, shell, "false\r\n\x1b]133;C;aid=pardes\x07\x1b]133;D;1;aid=pardes\x07" ++ RunJoin.prompt);
+ _ = try n.serve(.{ .tag = 5, .op = .release, .node = run, .handle = h, .opened = true }, true);
+ try n.agree();
+}
+
+test "a client joined after its session's shell was found to have no prompt marks answers a pty/run as its session does" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ var n: NinepJoin = undefined;
+ const shell = try RunJoin.init(&n);
+ defer n.deinit();
+ {
+ const was = Stepping.begin(&n.a.t, null);
+ n.a.core.acknowledgeShell(.at(shell), "/bin/sh", false);
+ was.end();
+ n.a.seq += 1;
+ _ = try n.a.unit(0, n.a.seq);
+ }
+ try n.join();
+ const run = n.node(.pty_run);
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = run, .omode = 2 }, true)).handle;
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = run, .handle = h, .data = "true\n" }, true);
+ const answer = try n.serve(.{ .tag = 3, .op = .read, .node = run, .handle = h, .size = 64 }, true);
+ try std.testing.expectEqualStrings("error no prompt marks\n", n.a.core.fsPayload(answer));
+ _ = try n.serve(.{ .tag = 4, .op = .release, .node = run, .handle = h, .opened = true }, true);
+ try n.agree();
+}