summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 04:21:37 -0300
committerGabriel Schneider <[email protected]>2026-11-20 04:36:28 -0300
commite1a47cef599070e3ea268255ef4c1164e4a3169b (patch)
treee8f31d971181f1e988bea94f5ab112333c566b2d /core/replica.zig
parent6e8735895d04c0073eb28cc1a2f431a2ba587e9d (diff)
downloadpardes-e1a47cef599070e3ea268255ef4c1164e4a3169b.tar.gz
pardes-e1a47cef599070e3ea268255ef4c1164e4a3169b.zip
ninep: a client attached while a script's pty/run runs in a shell replays it as its session does: the open's handle is the session's 9P server's answer (tape.authority), not the client's first free open record, which a client joined since (holding none of the opens made before its join) chose otherwise, so its write named no record and its run never went (the base, the attach agent's probe: 6 of 6 runs a client diverged, "the core called prompt where the tape has a done"); and a run's step reads the shell's marks through the tape's prompt site (pty.marksOf, as the greeting and the run's answer did), not the emulator here, which on a client of screen-state terminals is the last delta's and comes a frame after the unit that replays the output (with the handle alone 3 of 6 still diverged: sent_command's count and the busy check read marks behind the session's); Pane.noteCommand takes the count from its caller; a client whose rejoin lands as its session ends goes as at a hang-up, in the same words ("let this client go"), never "could not join again: the session hung up before the dump was whole" (replica.ended: hung up, nothing answers, no such session); tests: a client joined from a dump with a listing open held across the join takes a run as the session does (failed on the base: its digest), the same with screen-state terminals and their deltas after each unit (failed with the handle fix alone: "the core did not make the host call the tape has, in output"), and the rejoin test says a session gone and one hung up mid-dump are its end; the tape's results list Term.Prompt, so the semantics word moves: 7a610621b2f37456 -> 92963bab2e4436fb; probe (exitclient.py, 6 runs): 0 of 6 diverge, 0 of 3 with a digest every unit (PARDES_REPLICA_TRACE); a pty/data-only variant diverged 0 of 2 on this build too; tui core-test 856/1009 (153 skipped: the two new tests need a shell), shell-test 1049/1052 (load 15-20)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig175
1 files changed, 175 insertions, 0 deletions
diff --git a/core/replica.zig b/core/replica.zig
index 14757d9a..818e375a 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -2946,6 +2946,17 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime
if (!post(ctx, .{ .refused = words })) std.heap.c_allocator.free(words);
}
+/// A join's failure that is its session's end, whenever it came (the
+/// session or its daemon gone, or hung up before the dump was whole): a
+/// client joining again then goes as at a hang-up (`.gone`), in the same
+/// words, never as a join that failed.
+pub fn ended(why: []const u8) bool {
+ const words = std.mem.sliceTo(why, 0);
+ for ([_][]const u8{ "the session hung up", "nothing answers at ", "no session called " }) |w|
+ if (std.mem.startsWith(u8, words, w)) return true;
+ return false;
+}
+
/// The words a client goes with when its session runs another build now.
const upgrade_words = "session upgraded to a newer pardes: upgrade this client too";
@@ -2963,6 +2974,7 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o
const t0 = nowNs();
if (old) |prev| {
j.* = redial(gpa, path, session, why, have, prev.diverged == null, j.view) orelse {
+ if (ended(why)) return null;
var words: [600]u8 = undefined;
const said_ = std.mem.sliceTo(why, 0);
@memcpy(words[0..said_.len], said_);
@@ -3571,6 +3583,14 @@ test "a client joining again after a divergence is told at once that its session
const t0 = nowNs();
try std.testing.expect(redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null);
try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms);
+ try std.testing.expect(ended(&why));
+ // Its session ending as it joins: hung up before the dump was whole,
+ // its end too, said as the end it is.
+ var fds: [2]c_int = undefined;
+ try std.testing.expectEqual(0, libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM, 0, &fds));
+ _ = libc.close(fds[1]);
+ try std.testing.expect(joinOn(std.testing.allocator, fds[0], "gone", &why, null, 1) == null);
+ try std.testing.expect(ended(&why));
}
test "a replica's own keys apply at once, confirm on the fast path, and end at the authority's digest" {
@@ -6703,3 +6723,158 @@ test "a far client's replay of a message its session's host stamped takes the se
try agrees(a, r);
try std.testing.expect(try shows(r.core(), "--:--:-- yanked x"));
}
+
+// A script's `pty/run` in a shell (ninep/pty.zig): the open, the line, the
+// marks that end it, its answer read and the open released, each a step of
+// the session's, with a client that joined from a dump taking every unit.
+test "a client joined from a dump takes a 9P pty/run in a shell as the session does: the line sent at the prompt, its end answered" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const gpa = std.testing.allocator;
+ const RunHost = 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 a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &RunHost.vtable }, false);
+ defer a.deinit();
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), false);
+ 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;
+ const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
+ a.output(.{ .output = .{ .pane = .at(shell), .bytes = prompt ++ "ls\r\n\x1b]133;C;aid=pardes\x07a b\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt } });
+ r.confirm((try a.pending()).?);
+ r.settle();
+ // A script's listing open, held across the join (a reader of /index).
+ {
+ const was = Stepping.begin(&a.t, null);
+ _ = a.core.serveFs(.{ .tag = 9, .op = .open, .node = @backingInt(pardes.ctlfs.TopFile.index) });
+ was.end();
+ a.seq += 1;
+ r.confirm(try a.unit(0, a.seq));
+ r.settle();
+ }
+ const joined = (try a.join(2, null)).?;
+ defer gpa.free(joined.dump);
+ r.confirm(joined.unit);
+ const late = try Replica.initDump(gpa, 2, joined.view, joined.dump, .{ .cols = 80, .rows = 24 });
+ defer late.deinit();
+ late.t.screen_state = false;
+ late.core().echo = null;
+ try std.testing.expectEqual(joined.digest, late.digest());
+ const node = pardes.ctlfs.Node.of(a.core.panes[shell].?.serial, .pty_run);
+ const Serve = struct {
+ fn serve(s: *Authority, rs: []const *Replica, req: pardes.ctlfs.Req) !pardes.ctlfs.Reply {
+ const was = Stepping.begin(&s.t, null);
+ const reply = s.core.serveFs(req);
+ s.core.pump(s.host) catch {};
+ was.end();
+ s.seq += 1;
+ const u = try s.unit(0, s.seq);
+ for (rs) |x| {
+ x.confirm(u);
+ x.settle();
+ }
+ return reply;
+ }
+ fn output(s: *Authority, rs: []const *Replica, pane: u8, bytes: []const u8) !void {
+ s.output(.{ .output = .{ .pane = .at(pane), .bytes = bytes } });
+ const u = (try s.pending()).?;
+ for (rs) |x| {
+ x.confirm(u);
+ x.settle();
+ }
+ }
+ };
+ const both = [_]*Replica{ r, late };
+ const h = (try Serve.serve(a, &both, .{ .tag = 1, .op = .open, .node = node, .omode = 2 })).handle;
+ _ = try Serve.serve(a, &both, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "true\n" });
+ try Serve.output(a, &both, shell, "true\r\n\x1b]133;C;aid=pardes\x07\x1b]133;D;0;aid=pardes\x07" ++ prompt);
+ const answer = try Serve.serve(a, &both, .{ .tag = 3, .op = .read, .node = node, .handle = h, .size = 64 });
+ try std.testing.expectEqualStrings("exit 0\n", a.core.fsPayload(answer));
+ _ = try Serve.serve(a, &both, .{ .tag = 4, .op = .release, .node = node, .handle = h, .opened = true });
+ try Serve.output(a, &both, shell, "\x1b]133;A;cl=line;aid=pardes\x07");
+ try agrees(a, r);
+ try agrees(a, late);
+}
+
+// The same run with the terminals crossing as screen state (a session's
+// clients', until the switch to bytes): a replica's emulator is the
+// deltas', its marks not the session's.
+test "a client of screen-state terminals takes a 9P pty/run in a shell as the session does" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const gpa = std.testing.allocator;
+ const RunHost = 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 a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &RunHost.vtable }, 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;
+ const Step = struct {
+ var second: ?*Replica = null;
+ fn take(s: *Authority, sc: *Screens, p: u16, x: *Replica, u: []const u8) !void {
+ const gpa_ = std.testing.allocator;
+ const kept = try gpa_.dupe(u8, u);
+ defer gpa_.free(kept);
+ // The unit first, its screens at the frame after (Peers).
+ x.confirm(kept);
+ x.settle();
+ if (second) |y| {
+ y.confirm(kept);
+ y.settle();
+ }
+ try deliver(s, sc, p, x, .flush);
+ if (second) |y| try deliver(s, sc, p + 1, y, .flush);
+ }
+ fn serve(s: *Authority, sc: *Screens, p: u16, x: *Replica, req: pardes.ctlfs.Req) !pardes.ctlfs.Reply {
+ const was = Stepping.begin(&s.t, null);
+ const reply = s.core.serveFs(req);
+ s.core.pump(s.host) catch {};
+ was.end();
+ s.seq += 1;
+ try take(s, sc, p, x, try s.unit(0, s.seq));
+ return reply;
+ }
+ fn output(s: *Authority, sc: *Screens, p: u16, x: *Replica, pane: u8, bytes: []const u8) !void {
+ s.output(.{ .output = .{ .pane = .at(pane), .bytes = bytes } });
+ try take(s, sc, p, x, (try s.pending()).?);
+ }
+ };
+ const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
+ try Step.output(a, &screens, peer, r, shell, prompt ++ "ls\r\n\x1b]133;C;aid=pardes\x07a b\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt);
+ // A second client joins now, from the session's dump.
+ const joined = (try a.join(2, null)).?;
+ defer gpa.free(joined.dump);
+ try Step.take(a, &screens, peer, r, joined.unit);
+ const late = try Replica.initDump(gpa, 2, joined.view, joined.dump, .{ .cols = 80, .rows = 24 });
+ defer late.deinit();
+ const peer2 = try screens.addPeer();
+ try deliver(a, &screens, peer2, late, .flush);
+ Step.second = late;
+ defer Step.second = null;
+ const node = pardes.ctlfs.Node.of(a.core.panes[shell].?.serial, .pty_run);
+ for (0..2) |_| {
+ const h = (try Step.serve(a, &screens, peer, r, .{ .tag = 1, .op = .open, .node = node, .omode = 2 })).handle;
+ _ = try Step.serve(a, &screens, peer, r, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "true\n" });
+ try Step.output(a, &screens, peer, r, shell, "true\r\n\x1b]133;C;aid=pardes\x07\x1b]133;D;0;aid=pardes\x07" ++ prompt);
+ const answer = try Step.serve(a, &screens, peer, r, .{ .tag = 3, .op = .read, .node = node, .handle = h, .size = 64 });
+ try std.testing.expectEqualStrings("exit 0\n", a.core.fsPayload(answer));
+ _ = try Step.serve(a, &screens, peer, r, .{ .tag = 4, .op = .release, .node = node, .handle = h, .opened = true });
+ }
+ try agrees(a, r);
+ try agrees(a, late);
+}