From 5671a8275203334594b6f3b548e6a6012ef9fae6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 07:33:55 -0300 Subject: ninep: a client that joins a session with 9P work under way has the 9P state the session has: what a step reads that the requests made is the core's state like any other (P30, P35), so the walk sees it: fs leaves Pardes's and Pane's authority lists and the 9P server's own part says so in its types (fs.Namespace: the sockets, mounts, log, the request being served's flags and the rest; pane.State: the event and pty readers' queues and counts, winsize, unannounced; Open: the listener's held read and a stream's last answer; LogOpen whole), so the open records, the writes held to go in as one edit (fs.batch) and each pane's addr, limit, dot truncation, wrsel, joined, nomark, noscroll, lock, run, unmarked, tag truncation and chord are in every dump and in the digest; the digest is cheap by type: a pane's 9P state adds nothing for a pane no script has touched and its fields as one run of bytes for the rest (pane.State.digest), the namespace the opens in use and held writes by their length (Namespace.digest: a 9P body append hashed its whole batch, +0.14% on body-appends), so the frame and perf gate's 121 per-operation counts are within 0.1% of tree2 (largest client-key-down +0.054%; the plain walk of the same state cost client-key-down +0.73%); an open's union is named (Open.What) and digests what a step reads of it (its kind, a run, an event reader's flag, a pager's serial, a port's index), never the text its reader is answered with, which each process makes of its own state (walk.hash takes a tagged union's own digest as it takes a struct's); the opens are taken as far as fs.opens_len (an idle session's none; the scans that read records bound by it, openOf too, and an open lets go of what a rollback left in its record past the length); a session revived from a dump (Restore, an Upgrade, a start from one: dump.loadAndRevive; a closed pane brought back: loadPane) drops the opens and each pane's 9P state, as today, its connections gone (fs.Namespace.forget): a client's join loads alone and keeps them; not the join's dump alone: the tape's dump round trip at a replay's end wants the image to carry what the digest covers; pty.Run's answer cut at its length; the wire version 24, so the semantics word moves 64e89d6b25a84796 -> 18636a639ac075de (it did not move with the walk alone: a base client would have digested other fields than its session; test/daemon.py's WIRE with it, and its upgrade case takes a client of the other side's protocol saying `run the same pardes at both ends`, as its third terminal already did); the eleven tests of the change before pass; a join's dump (the probe, Debug): idle 63258 -> 64612 bytes, 50 opens held 63270 -> 66744; the image idle 233426 -> 237001, 50 opens 233738 -> 295089; tui core-test 867/1022 (155 skipped), shell-test 1062/1065, behaviour-test 275/275 and --replay 275/275, replicas --clients 5 diverged 0/0/0, unequal 0, unlisted stops 0, corpus 22 of 22, no leak (load 30-40, before the digests were made cheap; the whole gate on the change after) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/ninep/tree.zig | 95 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 34 deletions(-) (limited to 'core/ninep/tree.zig') diff --git a/core/ninep/tree.zig b/core/ninep/tree.zig index 1eaaa8cb..107479f6 100644 --- a/core/ninep/tree.zig +++ b/core/ninep/tree.zig @@ -124,9 +124,45 @@ pub const out_reserve = 4 * 1024; /// (editors/acme/dat.h:373-385). An open that holds something answers its /// record's index plus one as the handle; one that holds nothing answers 0. pub const Open = struct { + /// A client's join carries it and the digest covers it (fs.Namespace), + /// but what only its reader is answered with: the listener's held + /// read, the stream's last answer, the panes a look or exec touched. + pub const replica = .{ .authority = .{ "results", "held", "replay" } }; /// The node it was opened on, which every use checks; 0 is a free record. node: u64 = 0, - what: union(enum) { + what: What = .{ .snapshot = null }, + /// What a write to a file of command lines left unfinished: a last line + /// with no newline yet, or an Edit block still open. A write through a + /// mount is cut at the 9P message size, so one `echo` of many lines may + /// come as several writes, cut anywhere; the rest waits for the open's + /// next write, or its release, and never runs as a fragment. + pending: std.ArrayList(u8) = .empty, + /// The panes this open's last write touched, which a read of look or + /// exec on it answers, as /net/tcp/clone answers on the open that + /// asked; a fresh open holds the session's last, from before it opened. + results: Results = .{}, + /// A read that found nothing yet (`.again`), kept to be answered when + /// this open's file has something, the way factotum keeps its log's + /// waiting reads and answers them on append (security/auth/factotum/ + /// log.c:4-52) and acme an event read (editors/acme/xfid.c:994). One at a + /// time, as acme's window keeps one `eventx`: a newer read takes the + /// place of the older. The listener fills it in and answers it + /// (runtime/9p_io.zig, `answerHeld`). + held: ?Held = null, + /// The last answer a stream open (a followed log, event, pty/data) gave, + /// and the offset it was read at. A read at an offset inside it is + /// answered from it again: bash's `read` takes a chunk, keeps a line and + /// lseeks back to just past it, and a stream has already moved on. + replay: ?Replay = null, + /// A `name` open has named its pane: one name an open, so a second line + /// on it (bash writes `printf 'a\nb\n'` a line at a time) is refused. + named: bool = false, + /// A line over 1 MiB was refused: what follows of it, through its + /// newline, is dropped rather than run as a line of its own. + discarding: bool = false, + + /// What it is an open of, and what that holds. + pub const What = union(enum) { /// /screen's frame from the open, a terminal body's history from its /// first read. snapshot: ?[]const u8, @@ -160,36 +196,22 @@ pub const Open = struct { /// An open reading a plumb port (Plumb.zig's ports, plumber(4)'s): /// the messages sent to it since the open, packed, a read each. port: struct { index: u8, queue: events.Queue = .{} }, - } = .{ .snapshot = null }, - /// What a write to a file of command lines left unfinished: a last line - /// with no newline yet, or an Edit block still open. A write through a - /// mount is cut at the 9P message size, so one `echo` of many lines may - /// come as several writes, cut anywhere; the rest waits for the open's - /// next write, or its release, and never runs as a fragment. - pending: std.ArrayList(u8) = .empty, - /// The panes this open's last write touched, which a read of look or - /// exec on it answers, as /net/tcp/clone answers on the open that - /// asked; a fresh open holds the session's last, from before it opened. - results: Results = .{}, - /// A read that found nothing yet (`.again`), kept to be answered when - /// this open's file has something, the way factotum keeps its log's - /// waiting reads and answers them on append (security/auth/factotum/ - /// log.c:4-52) and acme an event read (editors/acme/xfid.c:994). One at a - /// time, as acme's window keeps one `eventx`: a newer read takes the - /// place of the older. The listener fills it in and answers it - /// (runtime/9p_io.zig, `answerHeld`). - held: ?Held = null, - /// The last answer a stream open (a followed log, event, pty/data) gave, - /// and the offset it was read at. A read at an offset inside it is - /// answered from it again: bash's `read` takes a chunk, keeps a line and - /// lseeks back to just past it, and a stream has already moved on. - replay: ?Replay = null, - /// A `name` open has named its pane: one name an open, so a second line - /// on it (bash writes `printf 'a\nb\n'` a line at a time) is refused. - named: bool = false, - /// A line over 1 MiB was refused: what follows of it, through its - /// newline, is dropped rather than run as a line of its own. - discarding: bool = false, + + /// What a step reads of it: not the text a reader is answered from + /// (a screen, a listing, the log, a port's messages), which each + /// process makes of its own state, and a replica never answers. + pub fn digest(w: *const What, h: *pardes.walk.Hasher) void { + const tag = std.meta.activeTag(w.*); + pardes.walk.hash(@TypeOf(tag), &tag, h); + switch (w.*) { + .run => |*run| pardes.walk.hash(pty.Run, run, h), + .event => |*reader| pardes.walk.hash(bool, reader, h), + .pager => |*serial| pardes.walk.hash(u32, serial, h), + .port => |*port| pardes.walk.hash(u8, &port.index, h), + .snapshot, .log, .frozen, .pty_data, .ctl, .lines, .writes, .plumb => {}, + } + } + }; pub const Replay = struct { off: u64, bytes: []u8 }; @@ -230,7 +252,7 @@ pub const open_slots = 64; /// The record a request's handle names, when it was opened on that node. pub fn openOf(p: *Pardes, req: Req) ?*Open { - if (req.handle == 0 or req.handle > p.fs.opens.len) return null; + if (req.handle == 0 or req.handle > p.fs.opens_len) return null; const o = &p.fs.opens[req.handle - 1]; return if (o.node == req.node) o else null; } @@ -614,7 +636,7 @@ fn runAttr(p: *Pardes, req: Req, target: Target) ?Reply.Attr { .top, .col, .port => return null, }; if (t.file != .pty_run) return null; - const run = if (req.handle != 0) openOf(p, req) else for (&p.fs.opens) |*o| { + const run = if (req.handle != 0) openOf(p, req) else for (p.fs.opens[0..p.fs.opens_len]) |*o| { if (o.node == req.node and o.what == .run) break o; } else null; if ((run orelse return null).what != .run) return null; @@ -895,7 +917,7 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { .ctl => if (linesOpen(target, req)) .lines else .{ .frozen = &.{} }, // One writer of the rules at a time, as plumber(4)'s. .rules, .send => if (req.omode & 3 == 0) return .{ .tag = req.tag } else plumb: { - if (f == .rules) for (p.fs.opens) |o| if (o.node == req.node and o.what == .plumb) return failText(req.tag, E.BUSY, e_in_use); + if (f == .rules) for (p.fs.opens[0..p.fs.opens_len]) |o| if (o.node == req.node and o.what == .plumb) return failText(req.tag, E.BUSY, e_in_use); break :plumb .plumb; }, else => if (linesOpen(target, req)) .lines else return .{ .tag = req.tag }, @@ -934,7 +956,11 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { // record would be another than the session's. const i = p.tape.authority(freeOpen, .{p}) orelse return Reply.fail(req.tag, E.NFILE); const o = &p.fs.opens[i]; + // Past `opens_len` a replica's record may hold what a rollback did not + // put back (rollback.zig saves records as far as it says). + if (o.node != 0) o.deinit(p.gpa); o.* = .{ .node = req.node, .what = what }; + p.fs.opens_len = @max(p.fs.opens_len, @as(u8, @intCast(i + 1))); if (resultsFile(target)) { @memcpy(o.results.list[0..p.fs.results_len], p.fs.results[0..p.fs.results_len]); o.results.len = p.fs.results_len; @@ -1074,6 +1100,7 @@ fn releaseHandle(p: *Pardes, req: Req) void { .snapshot, .log, .lines, .writes, .frozen, .pager, .plumb, .port => {}, }; o.deinit(p.gpa); + while (p.fs.opens_len > 0 and p.fs.opens[p.fs.opens_len - 1].node == 0) p.fs.opens_len -= 1; } fn setattr(p: *Pardes, req: Req, target: Target) Reply { -- cgit v1.3 From 27837e6345b51ceb2ceaaee58fd593024feb07c8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 08:13:35 -0300 Subject: ninep: the audit's two more finds ride with the rest: the panes the last look or exec touched (fs.results and each open's) and a column's or the workspace's tag truncation (fs.header_rewrite, header_trunc_pending, header_held) are digested and dumped, dropped at a revive as the opens are; found by reading the namespace field by field (reports/pardes-faults-4.md section 3): exec.leased reads an open's results to keep a done command pane a script reads from being taken again, and a new open's results are the session's last (no test: it needs a finished command pane read by a script); a header's truncation is the pane tag's twin: test "a client joined after a script truncated the workspace's tag writes the tag its session writes" (unequal on the base); the rest of the namespace is no fault (set and read in one request's step, or replies, the log, the listener) but edit_hold (listed: the session's listener sets it between steps); gate on this tip (load 5-18): tui core-test 868/1023 (155 skipped), shell-test 1063/1066, pane-test 183/184, behaviour-test 275/275 and --replay 275/275, term-test 17/17, kitty-test 45/45, addon-test 5/5, process-test 1 of 144 (pty/run's "busy while a job holds the tty", at load 18; 3 of 3 alone), replicas --clients 5, --bytes --clients 5 and both with --seeds 8: diverged 0, unequal 0, unlisted stops 0, corpus 22 of 22, no leak; attach probe 0 of 6; cross_replay 29 digests agree on each replica; daemon.py plain, --far, --games, --window-clients, --upgrade base -> this and this -> base pass; the macOS (Linux host), macOS arm and FreeBSD builds; the frame and perf gate against tree2 PASS: 77/77 frames, gui unit-test 1660/1664, perfab corpus2x +0.009%, perfops 121 within 0.1% (largest client-key-down on medium +0.054%) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- abi/wire.zig | 3 ++- core/fs.zig | 23 ++++++++++++++++++----- core/ninep/tree.zig | 5 +++-- core/replica.zig | 12 ++++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) (limited to 'core/ninep/tree.zig') diff --git a/abi/wire.zig b/abi/wire.zig index 4055e42d..e68e8462 100644 --- a/abi/wire.zig +++ b/abi/wire.zig @@ -11,7 +11,8 @@ const config = @import("pardes_options"); const build_info = @import("build_info"); /// 24: the 9P state a session's requests made (its open records, each -/// pane's) is in its join's dump and its digest (core/fs.zig Namespace); +/// pane's, the panes the last look or exec touched, a header's +/// truncation) is in its join's dump and its digest (core/fs.zig Namespace); /// 23: a pane of a kind's frame names its rasters by key, their pixels /// crossing before it as strips of 4 MiB at most over the rows it shows /// (screen kind 5), and a replica lacking one asks for its pane again diff --git a/core/fs.zig b/core/fs.zig index dcc33cd6..eadb7c19 100644 --- a/core/fs.zig +++ b/core/fs.zig @@ -843,7 +843,10 @@ pub const EditHold = struct { pub const Namespace = struct { /// What a step reads that 9P requests made (every request an input /// each replica replays) is the core's like any state: the open - /// records and the writes held to go in as one edit, digested and dumped (a client's join takes them from + /// records (a command pane a script reads is not taken again: + /// exec.leased), the panes the last look or exec touched (a new open's + /// own), the writes held to go in as one edit and a header tag's + /// truncation, digested and dumped (a client's join takes them from /// the dump; a session revived from one drops the opens with the /// connections that held them: dump.zig `loadAndRevive`). The rest is /// the server's (core/walk.zig): its sockets and the mounts (dumped @@ -861,9 +864,9 @@ pub const Namespace = struct { "lsp_answer_at", "edit_started", "edit_hold", "edit_said", "lsp_result", "write_room", "write_waits", "log_full_hash", "pages", "closed_lines", "ran_at", "refilled", "failures", "ename", "news", "hanging_up", "log", "plumb_held", - "results", "results_len", "started", "header_rewrite", "header_trunc_pending", "header_held", + "started", }, - .len = .{.{ "opens", "opens_len" }}, + .len = .{ .{ "opens", "opens_len" }, .{ "results", "results_len" } }, }; socket_path: []const u8 = "", tcp_address: ?std.Io.net.IpAddress = null, @@ -1025,11 +1028,17 @@ pub const Namespace = struct { /// Its digest (core/walk.zig): the opens in use, and writes held by /// their length (their bytes are the same writes on every side; a /// stream of 8 KB appends hashed whole cost each its length again; - /// none held, the rest of `batch` is nothing a step reads). Nothing at + /// none held, the rest of `batch` is nothing a step reads), the panes + /// the last look or exec touched, a header's truncation. Nothing at /// all for a session no script holds anything of. pub fn digest(st: *const Namespace, h: *pardes.walk.Hasher) void { - comptime if (pardes.walk.fieldNames(Namespace).len != 3) @compileError("Namespace.digest: hash every field the walk does not leave out"); + comptime if (pardes.walk.fieldNames(Namespace).len != 8) @compileError("Namespace.digest: hash every field the walk does not leave out"); for (st.opens[0..st.opens_len]) |*o| pardes.walk.hash(tree.Open, o, h); + h.update(std.mem.sliceAsBytes(st.results[0..st.results_len])); + if (st.header_rewrite != null or st.header_trunc_pending != null or st.header_held) { + const words = [_]u64{ st.header_rewrite orelse std.math.maxInt(u64), st.header_trunc_pending orelse std.math.maxInt(u64), @intFromBool(st.header_held) }; + h.update(std.mem.asBytes(&words)); + } const b = &st.batch; if (b.bytes.items.len == 0) return; const words = [_]u64{ b.serial, b.node, b.handle, b.at, b.bytes.items.len, b.last_ns }; @@ -1041,8 +1050,12 @@ pub const Namespace = struct { pub fn forget(st: *Namespace, gpa: std.mem.Allocator) void { for (&st.opens) |*o| o.deinit(gpa); st.opens_len = 0; + st.results_len = 0; st.batch.bytes.deinit(gpa); st.batch = .{}; + st.header_rewrite = null; + st.header_trunc_pending = null; + st.header_held = false; } pub fn mount(st: *Namespace, gpa: std.mem.Allocator, name: []const u8, dial: []const u8) !void { diff --git a/core/ninep/tree.zig b/core/ninep/tree.zig index 107479f6..3eeb5d3f 100644 --- a/core/ninep/tree.zig +++ b/core/ninep/tree.zig @@ -126,8 +126,8 @@ pub const out_reserve = 4 * 1024; pub const Open = struct { /// A client's join carries it and the digest covers it (fs.Namespace), /// but what only its reader is answered with: the listener's held - /// read, the stream's last answer, the panes a look or exec touched. - pub const replica = .{ .authority = .{ "results", "held", "replay" } }; + /// read and the stream's last answer. + pub const replica = .{ .authority = .{ "held", "replay" } }; /// The node it was opened on, which every use checks; 0 is a free record. node: u64 = 0, what: What = .{ .snapshot = null }, @@ -216,6 +216,7 @@ pub const Open = struct { pub const Replay = struct { off: u64, bytes: []u8 }; pub const Results = struct { + pub const replica = .{ .len = .{.{ "list", "len" }} }; list: [pardes.MAX_PANES]u32 = undefined, len: u8 = 0, /// Written on: reads are a stream from the write on, whatever diff --git a/core/replica.zig b/core/replica.zig index 50615fc0..4d1a635c 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -7133,6 +7133,18 @@ test "a client joined after a script truncated a pane's tag writes the tag its s 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(); -- cgit v1.3