diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 14:29:10 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 14:55:38 -0300 |
| commit | e3d0daa9f89f41b41b7d05d510db553cbd8816d9 (patch) | |
| tree | 6602e8aca69ec3c160021d4975c43456d38801df /core | |
| parent | 8a8ce0a6740e9a98ea37a70d401c3ba3fffe0ee1 (diff) | |
| parent | 27837e6345b51ceb2ceaaee58fd593024feb07c8 (diff) | |
| download | pardes-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')
| -rw-r--r-- | core/Core.zig | 10 | ||||
| -rw-r--r-- | core/Plumb.zig | 4 | ||||
| -rw-r--r-- | core/dump.zig | 5 | ||||
| -rw-r--r-- | core/exec.zig | 2 | ||||
| -rw-r--r-- | core/fs.zig | 63 | ||||
| -rw-r--r-- | core/ninep/events.zig | 4 | ||||
| -rw-r--r-- | core/ninep/pane.zig | 22 | ||||
| -rw-r--r-- | core/ninep/pty.zig | 1 | ||||
| -rw-r--r-- | core/ninep/tree.zig | 96 | ||||
| -rw-r--r-- | core/panes.zig | 5 | ||||
| -rw-r--r-- | core/replica.zig | 337 | ||||
| -rw-r--r-- | core/walk.zig | 6 |
12 files changed, 507 insertions, 48 deletions
diff --git a/core/Core.zig b/core/Core.zig index e736d43b..ff704bc6 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -469,11 +469,11 @@ pub const replica = .{ "tape", }, .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome" }, - // The host's handles, the 9P server's fids and log, the state file - // every session of the user shares (and Recent read from it), this - // session's closed panes, and the files of a host with no file - // system of its own. - .authority = .{ "host", "fs", "store", "recent", "db", "fallback" }, + // The host's handles, the state file every session of the user + // shares (and Recent read from it), this session's closed panes, and + // the files of a host with no file system of its own. (The 9P + // server's own part of `fs` says so in fs.Namespace.) + .authority = .{ "host", "store", "recent", "db", "fallback" }, .len = .{ .{ "look_src", "n_look_src" }, .{ "session_dir", "session_dir_len" }, .{ "session_home", "session_home_len" }, .{ "session_shell", "session_shell_len" } }, }; diff --git a/core/Plumb.zig b/core/Plumb.zig index f51ddace..e1b09e69 100644 --- a/core/Plumb.zig +++ b/core/Plumb.zig @@ -593,7 +593,7 @@ pub fn portName(p: *const Pardes, index: u8) ?[]const u8 { /// Whether a program reads port `index` (an open of its file): the /// authority's 9P state, asked through the tape. fn portRead(p: *Pardes, index: u8) bool { - for (&p.fs.opens) |*o| if (o.node != 0 and o.what == .port and o.what.port.index == index) return true; + for (p.fs.opens[0..p.fs.opens_len]) |*o| if (o.node != 0 and o.what == .port and o.what.port.index == index) return true; return false; } @@ -611,7 +611,7 @@ fn enqueue(p: *Pardes, q: *events.Queue, lead: []const u8, m: *const Msg) bool { /// A copy of `m` to every reader of port `index` (plumber(4)'s queuesend: /// every fid open when it was sent), each read held for one answered. fn fanOut(p: *Pardes, index: u8, m: *const Msg) void { - for (&p.fs.opens) |*o| if (o.node != 0 and o.what == .port and o.what.port.index == index) { + for (p.fs.opens[0..p.fs.opens_len]) |*o| if (o.node != 0 and o.what == .port and o.what.port.index == index) { if (!enqueue(p, &o.what.port.queue, "", m)) { var said: Kind.Buf(160) = .{}; events.notePath(p, "err -", said.print("plumb/{s}: a message of {d} bytes is over a reader's {d} KiB, not sent to it", .{ portName(p, index).?, m.data.len, events.queue_cap / 1024 })); diff --git a/core/dump.zig b/core/dump.zig index 7ce54bce..4fc7fc4d 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -577,6 +577,10 @@ fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previ var on_disk: OnDisk = @splat(false); const p = try loadKnowing(gpa, opts, bytes, ¬es, &on_disk); errdefer p.deinit(); + // The 9P state its requests made was its connections', which do not + // come with it (a client's join loads alone, and keeps it). + p.fs.forget(gpa); + for (p.panes) |slot| if (slot) |pane| pane.fs.deinit(gpa); // Its kinds are this process's, not the dump's: the session's it // replaces, or what the scan found (a dump of another session's). p.kinds = if (previous) |old| old.kinds else .started(p.tape, opts.addons); @@ -753,6 +757,7 @@ pub fn loadPane(p: *Pardes, id: usize, bytes: []const u8) !*pardes.panes.Pane { pane.command_from = 0; pane.places.from = 0; pane.collapsed = false; + pane.fs.deinit(p.gpa); pane.prompt_view = p.live; try ensureKind(p, pane); p.installPane(id, pane); diff --git a/core/exec.zig b/core/exec.zig index 18b519ff..52f54541 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -799,7 +799,7 @@ pub fn runCommand(p: *Pardes, from: usize, line: []const u8) ?usize { /// Whether an open of a look or exec file holds `serial` from its own write /// (tree.Open.results): its client is reading that pane. fn leased(p: *const Pardes, serial: u32) bool { - for (p.fs.opens) |o| if (o.node != 0 and o.results.wrote) { + for (p.fs.opens[0..p.fs.opens_len]) |o| if (o.node != 0 and o.results.wrote) { if (std.mem.indexOfScalar(u32, o.results.list[0..o.results.len], serial) != null) return true; }; return false; diff --git a/core/fs.zig b/core/fs.zig index 6e5829e1..eadb7c19 100644 --- a/core/fs.zig +++ b/core/fs.zig @@ -841,6 +841,33 @@ 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 (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 + /// on their own), the log and what a reader or a listener is answered + /// with, and the request being served's own flags, set and read in + /// its step. ponytail: `edit_hold`, set by the session's listener + /// between steps and read by the retried write's step, is no + /// replica's (reports/pardes-faults-4.md). + pub const replica = .{ + .authority = .{ + "socket_path", "tcp_address", "mounts", "node_name", "os_paths", "out", + "listeners", "origin", "no_prompt", "session_write", "unlogged", "capturing", + "serving", "refused", "failure", "failure_len", "no_pane_slot", "no_pane_slot_len", + "said", "said_len", "said_at", "late_failure", "late_failure_len", + "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", + "started", + }, + .len = .{ .{ "opens", "opens_len" }, .{ "results", "results_len" } }, + }; socket_path: []const u8 = "", tcp_address: ?std.Io.net.IpAddress = null, mounts: std.ArrayList(Mount) = .empty, @@ -848,6 +875,9 @@ pub const Namespace = struct { os_paths: std.ArrayList(OsPath) = .empty, /// What each open holds between its open and its release. opens: [tree.open_slots]tree.Open = @splat(.{}), + /// The records past it are free: a digest and a dump take the opens + /// this far (an idle session's none). + opens_len: u8 = 0, out: std.ArrayList(u8) = .empty, listeners: u16 = 0, origin: u8 = 'K', @@ -995,6 +1025,39 @@ pub const Namespace = struct { st.out.deinit(gpa); } + /// 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), 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 != 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 }; + h.update(std.mem.asBytes(&words)); + } + + /// What the requests of connections gone made, let go: a session + /// revived from a dump has none of the connections that held its opens. + 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 { if (!validMountName(name)) return error.BadMountName; try ninep_io.Client.validateDial(dial); diff --git a/core/ninep/events.zig b/core/ninep/events.zig index 1951a434..1fd05b54 100644 --- a/core/ninep/events.zig +++ b/core/ninep/events.zig @@ -332,7 +332,7 @@ fn newest(p: *Pardes) ?struct { at: usize, seq: u64, text: []const u8 } { /// Whether a follower has read the record `seq`, or is part way through it. fn followerRead(p: *Pardes, seq: u64) bool { - for (p.fs.opens) |o| if (o.node != 0 and o.what == .log) { + for (p.fs.opens[0..p.fs.opens_len]) |o| if (o.node != 0 and o.what == .log) { const l = o.what.log; if (l.follow and (l.next > seq or (l.next == seq and l.part > 0))) return true; }; @@ -437,6 +437,8 @@ fn pushLog(p: *Pardes, raw: []u8) void { /// What an open of /log holds. pub const LogOpen = struct { + /// Its reader's alone: what it is answered with (the log is the session's). + pub const replica = .{ .authority = .{ "bytes", "follow", "next", "part", "pos" } }; /// The ring's text when it was opened. bytes: []const u8 = &.{}, /// This open waits for records after `bytes`, from `next`. diff --git a/core/ninep/pane.zig b/core/ninep/pane.zig index 1d514a3c..d47b637b 100644 --- a/core/ninep/pane.zig +++ b/core/ninep/pane.zig @@ -30,6 +30,28 @@ const Node = tree.Node; /// Filesystem state a pane carries beside its editor state. pub const State = struct { + /// What a step reads of it is digested and dumped (fs.Namespace); the + /// readers' records and the server's say of them are the session's. + pub const replica = .{ .authority = .{ "readers", "event_reader", "events", "tag_snap", "pty_reader", "pty_out", "winsize", "unannounced" } }; + + /// Its digest (core/walk.zig): nothing for a pane no script has set + /// anything of (most of them, at every unit a replica confirms); the + /// rest's fields as one run of bytes, not a hash's call a field. + pub fn digest(st: *const State, h: *pardes.walk.Hasher) void { + const untouched: State = .{}; + var touched = false; + inline for (comptime pardes.walk.fieldNames(State)) |name| touched = touched or !std.meta.eql(@field(st, name), @field(untouched, name)); + if (!touched) return; + var buf: [256]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + // Longer than the room (a chord's text): field by field. + if (encoded(st, &w)) |_| h.update(w.buffered()) else |_| inline for (comptime pardes.walk.fieldNames(State)) |name| pardes.walk.hash(@FieldType(State, name), &@field(st, name), h); + } + + fn encoded(st: *const State, w: *std.Io.Writer) std.Io.Writer.Error!void { + inline for (comptime pardes.walk.fieldNames(State)) |name| try pardes.walk.encode(@FieldType(State, name), &@field(st, name), w); + } + /// The range `data` and `xdata` read and write through. acme clears it /// when the first client opens `addr` (editors/acme/xfid.c:105-108), /// which suits a client that holds the fid open; a shell cannot, and diff --git a/core/ninep/pty.zig b/core/ninep/pty.zig index 4b0cee66..ec752311 100644 --- a/core/ninep/pty.zig +++ b/core/ninep/pty.zig @@ -164,6 +164,7 @@ pub const output_rows = output_cap / 8; /// What an open of run holds (tree.zig's open records). pub const Run = struct { + pub const replica = .{ .len = .{.{ "answer", "len" }} }; /// `waiting`: the line is kept until the shell's first prompt (a new /// terminal's shell takes a moment to draw it), then sent. phase: enum { idle, waiting, sent, done } = .idle, diff --git a/core/ninep/tree.zig b/core/ninep/tree.zig index 1eaaa8cb..3eeb5d3f 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 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: 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,40 +196,27 @@ 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 }; 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 @@ -230,7 +253,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 +637,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 +918,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 +957,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 +1101,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 { diff --git a/core/panes.zig b/core/panes.zig index 1c103bd1..68304437 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -27,12 +27,11 @@ pub const EditText = struct { text: []u8, row0: i32 }; pub const Pane = struct { /// What the replay digest leaves out (core/walk.zig): the view's geometry, - /// made again from the rest (File.geometry); the 9P server's record of - /// the pane's opens and readers; the other views' share of it (views.zig). + /// made again from the rest (File.geometry); the other views' share of + /// it (views.zig). (The 9P server's part of `fs` says so in its type.) pub const replica = .{ .local = .{ "pv", "pv_known" }, .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "notices", "pan" }, - .authority = .{"fs"}, .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, // A picture's memory is its own allocator's (a dump loaded makes // it there). 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(); +} diff --git a/core/walk.zig b/core/walk.zig index 6d7c594d..9b760144 100644 --- a/core/walk.zig +++ b/core/walk.zig @@ -31,8 +31,9 @@ //! .blob = .{ "content" }, //! }; //! -//! None of the three kinds is in a digest or a dump. A type with a `pub fn -//! digest(*const T, *Hasher) void` of its own hashes itself; one with +//! None of the three kinds is in a digest or a dump. A type (a struct or a +//! tagged union) with a `pub fn digest(*const T, *Hasher) void` of its own +//! hashes itself; one with //! `dumpWrite`, `dumpRead` (and `dumpFree`) dumps itself as bytes. const std = @import("std"); const xitdb = @import("xitdb"); @@ -242,6 +243,7 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void { }, .@"union" => |u| { if (u.tag_type == null) @compileError("walk: an untagged union: " ++ @typeName(T)); + if (comptime @hasDecl(T, "digest") and @TypeOf(T.digest) == fn (*const T, *Hasher) void) return v.digest(h); const tag: u.tag_type.? = v.*; hash(u.tag_type.?, &tag, h); switch (v.*) { |
