From 7e6834ba5eff5c0e61891ec1935e5e0ca8291c2f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 12:32:23 -0300 Subject: tests on FreeBSD: Linux's /proc/1 fixtures, the builtins lists, daemon.py and fs.py without /proc - The refusing directory some tests name is Linux's /proc/1/root: that part skips elsewhere. The committed builtins-*.json are Linux builds' (Tty9p): their check skips, and a key the docs give a word this build leaves out is found in the other's list. - kitty: a shared memory object is sized before it is written (FreeBSD's write does not grow it); a FIFO in /dev/shm is Linux's case. - daemon.py asks ps and procstat where there is no /proc, and finds bash on PATH. fs.py runs printenv once a name, and skips two permission checks as root, who searches and writes anywhere. Co-Authored-By: Claude Opus 5.5 --- src/fs.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/fs.zig') diff --git a/src/fs.zig b/src/fs.zig index 08379697..9104c93f 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -708,7 +708,8 @@ pub fn deniedAbove(dir: []const u8) bool { } test "a directory under one that may not be searched or written is denied, one merely missing is not" { - try std.testing.expect(deniedAbove("/proc/1/root/x/y")); + // The refusing directory is Linux's /proc/1/root. + if (comptime @import("builtin").os.tag == .linux) try std.testing.expect(deniedAbove("/proc/1/root/x/y")); try std.testing.expect(!deniedAbove("/tmp/pardes-surely-missing-dir/a/b")); } -- cgit v1.3 From 044da0f75ab5fb5be0213e512741860d95dad8e5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 11:14:38 -0300 Subject: tape: every input the core consumes recorded in turn order, replayed into replicas that must reach the same digests; the core's leaks of render, clock and host closed The recorder (src/tape.zig) is null unless PARDES_RECORD= names a tape (.tape); the local path pays a null check a call. It records a tree at the host boundary, both ways: each call the host makes into the core (`Input`: update, serveFs, pump, perform, the shell's acknowledgements and failures, a Restore, ...) with its arguments, and each call the core makes out -- a Host function (the vtable wrapped generically while recording) or a syscall of its own (`tape.site` for every old `turn.yield()`, `tape.value` for a clock or environment read) -- with its result; `done`/`ret` close each, a 9P request served while a step is out lands inside that step's syscall, and fields a host writes between calls (native_images, present_skipped, ...) are found by a diff at every crossing. After each call the host made at the top, the session's digest (src/walk.zig), one per field of the core, its stream and each pane, file and places. `PARDES_REPLAY= pardes` (main.zig) replays it with the same build into three replicas: two at different addresses (the first's memory held while the second runs) and one that never draws. A host call answers what it answered, a site returns what it returned, nothing is performed and no syscall is made; `turn.yield` panics in a replica, so a syscall no site covers is a stack trace; a call the recording did not make, or a digest that differs, is a divergence that names the fields. The headless replica steps over the presents it does not make, applying what the host told the core inside them. What the replicas found, fixed: - Render wrote core state. The view's geometry (wrap map, context rows, body layer rows) is File.geometry, a lazy cache keyed by a hash of what it is made of (revision, scroll, size, tags, tables read, context declarations, the session's view: wrap, layered context, row metrics, rect); the render makes it afresh, a step reading it makes it on demand (wrapAt, wrapRow, ensureCursorVisible). Context declarations, notice bands and the header's reveal are the step's (sync), the tables' reading the tick's (a first look of 4096 lines, a count, wherever it is asked; the rest a frame's clock budget a tick), and a view's paint-ahead is asked of the host as the frame is drawn rather than queued as an effect. - The clock: advance's wakes are the recording session's (a picture decoding or a fade adds no tick to a replica), as are the budgets of Places, Names and Tables (`tape.over`), a message's time of day (the stamp; msg_said_ms and the unattended expiry use the session clock), File.mtime, and the pump reads its clock whether or not it waits. - The host: the session directory (getcwd, asked once at init), $SHELL and its existence, $HOME for `~`, `Shell` naming one that is not there, a missing interpreter, a directory refused above, a deleted file, a pointer seen through a sliding pane (Pardes.pointerCell). - Worker handles (futures, the Io), the 9P server's state, the store file every window shares (a replica keeps its own in memory) and Recent read from it are authority-only; frames, animations and the render's caches local. Query rows' paints count apart from a view's. Terminals replay from their taped pty bytes and are digested as their active screen, cursor and scroll; kitty images are left out until phase 5's screen-state terminals. tagnav.golden: the header's caret reveal now runs every step, as a pane's does, not once per frame drawn, so after `g l B B` typed in one burst the bar stays scrolled as a human typing it sees it. Co-Authored-By: Claude Opus 5.5 --- src/Dir.zig | 8 +- src/File.zig | 106 ++++++- src/Messages.zig | 69 ++-- src/Names.zig | 6 +- src/Output.zig | 3 +- src/Places.zig | 8 +- src/Query.zig | 6 +- src/Stream.zig | 82 ++++- src/Tables.zig | 41 ++- src/Text.zig | 4 + src/body_layer.zig | 25 +- src/builtins.zig | 12 +- src/config.zig | 5 + src/draw.zig | 21 +- src/dump.zig | 13 +- src/exec.zig | 13 +- src/fs.zig | 143 +++++---- src/image.zig | 4 + src/look.zig | 2 +- src/main.zig | 2 + src/mouse.zig | 5 +- src/ninep/pane.zig | 3 +- src/ninep/tree.zig | 2 + src/normal.zig | 1 + src/panes.zig | 29 +- src/pardes.zig | 159 +++++++++- src/pdf.zig | 3 + src/pdf_view.zig | 4 + src/selection_pipe.zig | 1 + src/tape.zig | 741 +++++++++++++++++++++++++++++++++++++++++++ src/terminal.zig | 24 ++ test/panes.zig | 11 +- test/snapshots/tagnav.golden | 4 +- 33 files changed, 1341 insertions(+), 219 deletions(-) create mode 100644 src/tape.zig (limited to 'src/fs.zig') diff --git a/src/Dir.zig b/src/Dir.zig index b994bc06..ebe6b69c 100644 --- a/src/Dir.zig +++ b/src/Dir.zig @@ -53,6 +53,10 @@ pub fn read(gpa: std.mem.Allocator, dir: []const u8) ![]u8 { var own: [4096]u8 = undefined; if (local.len > own.len) return error.NameTooLong; @memcpy(own[0..local.len], local); + return pardes.tape.site(.dir_listing, gpa, readHost, .{ gpa, own[0..local.len] }); +} + +fn readHost(gpa: std.mem.Allocator, local: []const u8) ![]u8 { var entries: std.ArrayList([]u8) = .empty; defer { for (entries.items) |entry| gpa.free(entry); @@ -60,10 +64,8 @@ pub fn read(gpa: std.mem.Allocator, dir: []const u8) ![]u8 { } var total: usize = 0; { - pardes.turn.yield(); - defer pardes.turn.back(); const io = std.Io.Threaded.global_single_threaded.io(); - var d = try std.Io.Dir.cwd().openDir(io, own[0..local.len], .{ .iterate = true }); + var d = try std.Io.Dir.cwd().openDir(io, local, .{ .iterate = true }); defer d.close(io); var it = d.iterate(); while (try it.next(io)) |entry| { diff --git a/src/File.zig b/src/File.zig index e4bca000..d795eaa1 100644 --- a/src/File.zig +++ b/src/File.zig @@ -104,6 +104,7 @@ fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Snapshot) void { } pub const History = struct { + pub const replica = .{ .len = .{ .{ "undo", "undo_len" }, .{ "redo", "redo_len" } } }; undo: [limits.undo_max]Snapshot = undefined, undo_len: usize = 0, redo: [limits.undo_max]Snapshot = undefined, @@ -137,6 +138,10 @@ pub const History = struct { /// A file pane's backing: owned content, its derived caches, and undo history. pub const State = struct { + /// What the replay digest leaves out (src/walk.zig): what is made again + /// from the text whenever it is wanted. + pub const replica = .{ .derived = .{ "line_starts", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" } }; + path: []u8, content: []u8, revision: u32 = 0, @@ -1252,7 +1257,8 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { if (f.saved_hash) |h| if ((f.saved_len orelse new.len) == new.len and std.hash.Wyhash.hash(0, new) == h) { f.saved_revision = f.revision; }; - f.mtime = pardes.ctlfs.events.now(); + // The clock the session that recorded read (src/tape.zig). + f.mtime = pardes.tape.value(.mtime, pardes.ctlfs.events.now, .{}); if (f.line_starts.len > 0) p.gpa.free(f.line_starts); f.line_starts = starts; if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); @@ -1647,6 +1653,8 @@ pub fn redo(p: *Pardes, pane: *Pane) void { /// ` deleted on disk (Save writes it again)`, logged `changed /// deleted`. pub fn deleted(p: *Pardes, id: u8) void { + const taped = pardes.tape.enter(p, .{ .file_deleted = id }); + defer pardes.tape.leave(p, taped); const pane = p.panes[id] orelse return; const f = if (pane.file) |*file| file else return; if (f.disk_gone) return; @@ -1703,6 +1711,17 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void { /// re-highlight the visible window of any file whose syntax went stale /// (edit, scroll, load) — visible-range-first so big files stay snappy +/// The declarations a TreeContext pane's context rows are drawn from, for +/// its text as it is now: found in the step (Pardes.sync), never only by a +/// render, since the view's geometry is made of them (`geometry`). +pub fn refreshContext(p: *Pardes, pane: *Pane, f: *State) void { + if (!f.tree_context or !supportsContext(pane) or f.context_revision == f.revision) return; + const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch return; + if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); + f.context_declarations = declarations; + f.context_revision = f.revision; +} + pub fn refreshHighlights(p: *Pardes) void { const tz = tracy.zone(@src(), "refreshHighlights"); defer tz.end(); @@ -1710,12 +1729,6 @@ pub fn refreshHighlights(p: *Pardes) void { const pane = slot orelse continue; if (pane.file == null) continue; const f = &pane.file.?; - if (f.tree_context and supportsContext(pane) and f.context_revision != f.revision) { - const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch continue; - if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); - f.context_declarations = declarations; - f.context_revision = f.revision; - } if (f.mini != null) { f.syntax_dirty = false; continue; @@ -1907,18 +1920,79 @@ pub fn visualRow(line: []const u8, col: usize, width: usize) VisualRow { } } -pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State, wrap: bool) ![]const u8 { - const width = wrapWidth(pane, wrap); +/// The body's text as it is shown: its geometry's rows, each a line's +/// number and its stretch of the line. +pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State) ![]const u8 { + geometry(pane); + const out = try arena.alloc(u8, pane.geometry_len); + const filled = fillBody(out, pane, f, wrapWidth(pane, pane.view.wrap), false); + std.debug.assert(filled == out.len); + return out; +} + +/// The pane's view geometry for its state as it is now: the wrap map (the +/// line, column and pad each body row starts at), the context rows, and how +/// many rows the body layer has. Made again when anything it is made of +/// changes (`geometryKey`), by whoever asks first, the render or a step that +/// reads it: so a core that never draws has the geometry of one that does +/// (docs/next-daemon.md). What it reads of the session is the pane's `view`, +/// which each step sets. +pub fn geometry(pane: *Pane) void { + const f = if (pane.file) |*file| file else return; + const key = geometryKey(pane, f); + if (pane.geometry_key == key) return; + pane.geometry_key = key; + pane.body_rows = 0; + pane.body_visible_rows = 0; + pane.context_row_limit = null; + pane.geometry_len = mapRows(pane, f); + // A body with context rows is a layer taller than the grid's rows, its + // context rows at the tagline's height (body_layer.joinBody). + const v = pane.view; + if (!v.layered or pane.collapsed or v.rect_h <= pane.tag_rows) return; + const logical_rows = @min(@as(usize, pane.rows) + pane.context_rows, pane.wrap_line.len); + if (pane.context_rows == 0 or logical_rows <= pane.context_rows) return; + pane.body_rows = @intCast(logical_rows); + const bh: usize = @max(1, v.body_h); + const th: usize = std.math.clamp(v.tagline_h, 1, bh); + const body_pixels = @as(usize, v.rect_h - pane.tag_rows) * bh; + const limit: u16 = @intCast(@min(pane.context_rows, (body_pixels -| bh) / th)); + pane.context_row_limit = limit; + pane.body_visible_rows = @intCast(@min(logical_rows, limit + @max(1, (body_pixels -| @as(usize, limit) * th) / bh))); + pane.geometry_len = mapRows(pane, f); +} + +/// One walk of the rows the body shows, recording the wrap map: the bytes +/// its text takes. +fn mapRows(pane: *Pane, f: *State) usize { + const width = wrapWidth(pane, pane.view.wrap); pane.body_wrapped = width > 0; f.tables.ensure(pane.gpa, f); prepareContextRows(pane, f); - pane.wrap_n = 0; - - const len = fillBody(null, pane, f, width, false); - const out = try arena.alloc(u8, len); - const filled = fillBody(out, pane, f, width, true); - std.debug.assert(filled == out.len); - return out; + return fillBody(null, pane, f, width, true); +} + +/// Everything the geometry is made of, hashed: the text (its revision and +/// length), where it is scrolled, the pane's size, its tables as far as +/// they have been read, its context declarations, the session's view. +fn geometryKey(pane: *const Pane, f: *const State) u64 { + const t = &f.tables; + const facts = .{ + f.revision, f.content.len, f.scroll, pane.hscroll, + pane.rows, pane.cols, pane.tag_rows, pane.collapsed, + pane.query != null, f.pager, f.mini != null, f.output != null, + if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context_revision orelse 0, + f.context_declarations.len, + f.line_starts.len, t.revision orelse 0, t.list.items.len, if (t.pending) |r| r.at else std.math.maxInt(usize), + pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h, + pane.view.rect_h, + }; + // One hash of them laid out as words: every reader of the map asks for + // this key, a hundred times a frame. + var words: [facts.len]u64 = undefined; + inline for (facts, &words) |fact, *w| w.* = if (@TypeOf(fact) == bool) @intFromBool(fact) else if (@typeInfo(@TypeOf(fact)).int.signedness == .signed) @bitCast(@as(i64, fact)) else fact; + // Never the "made for nothing" key a reset leaves (`geometry_key = 0`). + return std.hash.Wyhash.hash(0x9e0, std.mem.sliceAsBytes(&words)) | 1; } /// Run the file-body row walk. With no destination it is the exact sizing diff --git a/src/Messages.zig b/src/Messages.zig index cb52907a..4cf81ac5 100644 --- a/src/Messages.zig +++ b/src/Messages.zig @@ -31,6 +31,7 @@ len: usize = 0, pub const LoggedMessage = struct { pub const cap = 256; + pub const replica = .{ .len = .{.{ "text", "len" }} }; text: [cap]u8 = undefined, len: u16 = 0, /// The serial of the pane that said it, or 0 for none in particular. A @@ -117,6 +118,24 @@ pub const Message = struct { }; extern "c" fn localtime_r(timep: *const libc.time_t, result: *Tm) ?*Tm; + fn timeOfDay() [8]u8 { + var clock: [8]u8 = "--:--:--".*; + const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false; + if (notime) return clock; + var ts: libc.timespec = undefined; + _ = libc.clock_gettime(.REALTIME, &ts); + const secs: libc.time_t = ts.sec; + var tm: Tm = undefined; + if (localtime_r(&secs, &tm) != null) { + _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ + @as(u32, @intCast(tm.hour)), + @as(u32, @intCast(tm.min)), + @as(u32, @intCast(tm.sec)), + }) catch {}; + } + return clock; + } + pub fn body(text: []const u8) []const u8 { if (text.len < 10) return text; if (text[2] != ':' or text[5] != ':' or text[8] != ' ' or text[9] != ' ') return text; @@ -127,21 +146,9 @@ pub const Message = struct { } pub fn stamp(buf: []u8, verb: []const u8, subject: []const u8) []const u8 { - var clock: [8]u8 = "--:--:--".*; - const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false; - if (!notime) { - var ts: libc.timespec = undefined; - _ = libc.clock_gettime(.REALTIME, &ts); - const secs: libc.time_t = ts.sec; - var tm: Tm = undefined; - if (localtime_r(&secs, &tm) != null) { - _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ - @as(u32, @intCast(tm.hour)), - @as(u32, @intCast(tm.min)), - @as(u32, @intCast(tm.sec)), - }) catch {}; - } - } + // The time of day as the session that said it read it, in its + // zone: a replica's clock and TZ are its own (src/tape.zig). + const clock = pardes.tape.value(.clock, timeOfDay, .{}); const head = std.fmt.bufPrint(buf, "{s} {s} ", .{ &clock, verb }) catch return buf[0..0]; const room = buf.len - head.len; const tail = if (subject.len <= room) subject else subject[subject.len - room ..]; @@ -153,10 +160,14 @@ pub const Message = struct { /// A status updates the message line in place (a progress count does not /// pile up); a message is an event, and stacks. pub fn setStatus(p: *Pardes, id: usize, text: []const u8) void { + const taped = pardes.tape.enter(p, .{ .set_status = .{ .id = id, .text = text } }); + defer pardes.tape.leave(p, taped); showMessage(p, id, text, false); } pub fn setMessage(p: *Pardes, id: usize, text: []const u8) void { + const taped = pardes.tape.enter(p, .{ .set_message = .{ .id = id, .text = text } }); + defer pardes.tape.leave(p, taped); logMessage(p, id, text); showMessage(p, id, text, true); } @@ -186,7 +197,7 @@ fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void { const arrives = pane.msg_len == 0 or pane.msg_life.phase == .leaving; for (text[0..n], pane.msg[0..n]) |c, *cell| cell.* = if (c < 0x20 or c == 0x7f) ' ' else c; pane.msg_len = @intCast(n); - pane.msg_said_ms = nowMs(); + pane.msg_said_ms = @intCast(p.now_ns / std.time.ns_per_ms); p.needs_frame = true; if (arrives) { pane.msg_life = .{ .phase = if (p.settings.message_animation and n > 0) .entering else .shown }; @@ -200,13 +211,6 @@ fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void { } } -fn nowMs() i64 { - if (comptime !pardes.hosted) return 0; - var ts: std.c.timespec = undefined; - if (std.c.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), 1_000_000); -} - /// With no frontend attached no input dismisses a message, and a detached /// session's /screen and messages kept what was said an hour ago: each /// pane's lines go once its newest has been up `message_linger_ms`, on the @@ -217,7 +221,9 @@ fn nowMs() i64 { /// clock; the render pipeline's clock will give every line its own. pub fn expireUnattended(p: *Pardes) void { if (!p.unattended) return; - const now = nowMs(); + // The session's clock (`advance`), as every tick's: no replica reads + // its own. + const now: i64 = @intCast(p.now_ns / std.time.ns_per_ms); for (p.panes) |slot| { const pane = slot orelse continue; if (pane.msg_len == 0 and pane.msg_older_len == 0) continue; @@ -453,6 +459,8 @@ pub fn dialReason(err: anyerror) ?[]const u8 { } pub fn reportError(p: *Pardes, id: usize, operation: []const u8, err: anyerror) void { + const taped = pardes.tape.enter(p, .{ .report_error = .{ .id = id, .operation = operation, .err = err } }); + defer pardes.tape.leave(p, taped); // Room for an operation naming the longest path and the reason after // it: a shorter one cut the reason off (`Save /long…`), and the reason // is what a failure is for. Where it must be shorter, the path gives. @@ -575,6 +583,19 @@ test "an error reads as words and a failure is cut between them" { /// so it does not paint under them, and the paint pass so it can stack /// them instead of letting the last one win the row. pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect, id: usize) void { + collectBands(p, pane, r, id); + // Each band's chip at the right edge, as wide as its own text: a click + // finds it whether or not a frame has drawn it. + const tx = r.x + config.GUTTER; + const tw = r.w -| config.GUTTER; + var leader_buf: [16]u8 = undefined; + for (0..pane.notices.len) |i| { + const chip = noticeCols(p, noticeText(p, pane, i, &leader_buf), tw); + pane.notices.left[i] = if (chip == 0) std.math.maxInt(u16) else tx + tw - chip; + } +} + +fn collectBands(p: *Pardes, pane: *Pane, r: Rect, id: usize) void { pane.notices.len = 0; if (r.w <= config.GUTTER or r.h <= pane.tag_rows) return; // Oldest message line on top, so lines already up do not move when a diff --git a/src/Names.zig b/src/Names.zig index 0c8ba075..66f71bb9 100644 --- a/src/Names.zig +++ b/src/Names.zig @@ -146,8 +146,7 @@ pub fn typed(n: *Names, gpa: std.mem.Allocator, text: []const u8) void { pub fn rescore(n: *Names, gpa: std.mem.Allocator) void { const q = n.queryText(); const empty = std.mem.trim(u8, q, " ").len == 0; - const io = std.Io.Threaded.global_single_threaded.io(); - const started = std.Io.Clock.awake.now(io); + const started = pardes.FrameStats.nowNs(); const had = n.hits.items.len; const was_scored = n.scored; var count: usize = 0; @@ -164,8 +163,7 @@ pub fn rescore(n: *Names, gpa: std.mem.Allocator) void { const s = if (empty) 0 else match(q, key, n.lower.items[c.start + c.key ..][0..c.key_len], c.base, null, null, null); if (s) |score| n.hits.append(gpa, .{ .cand = i, .score = score }) catch break; // The clock every 256 candidates: reading it is not free either. - if (!empty and count % 256 == 255 and - started.durationTo(std.Io.Clock.awake.now(io)).toNanoseconds() > score_budget_ns) break; + if (!empty and count % 256 == 255 and pardes.tape.over(started, score_budget_ns)) break; } // An empty query keeps the source's order (the walk's, recency): no // row moves as the walk goes on, it only grows at the foot. diff --git a/src/Output.zig b/src/Output.zig index a17b0459..83914d52 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -43,6 +43,7 @@ pub const Origin = union(enum) { pub const max_arg = dump.max_origin_arg; pub const State = struct { + pub const replica = .{ .len = .{.{ "arg_buf", "arg_len" }} }; from: Origin, arg_buf: [max_arg]u8 = undefined, arg_len: u16 = 0, @@ -1115,7 +1116,7 @@ pub fn resetBody(p: *Pardes, pane: *Pane) void { pane.body.cur_col = 0; pane.body.cur_pinned = true; pane.hscroll = 0; - pane.wrap_n = 0; + pane.geometry_key = 0; pane.body.msel = .{}; pane.body.vsel = .{}; pane.body.nsel = 0; diff --git a/src/Places.zig b/src/Places.zig index cff7c22e..3dca3adf 100644 --- a/src/Places.zig +++ b/src/Places.zig @@ -67,6 +67,10 @@ pub fn nowNs() u64 { return @as(u64, @intCast(ts.sec)) *| std.time.ns_per_s +| @as(u64, @intCast(ts.nsec)); } +/// What the replay digest leaves out (src/walk.zig): the rail's ticks, +/// made again from the places when they are drawn. +pub const replica = .{ .derived = .{ "ticks", "ticks_gen", "re" } }; + started: bool = false, provenance: Provenance = .search, /// The query pane that fed them, by serial; 0 for `*` and the register. @@ -477,7 +481,9 @@ pub fn fill(set: *Places, p: *Pardes, pane: *Pane, bytes: usize, steps: u64) voi if (short) break; // A frame's time spent (a whole search's for n and N, whose steps // are mvzr's whole budget). - if (started_ns != 0 and bytes != std.math.maxInt(usize) and nowNs() - started_ns > frame_ns) break; + // The clock's verdict is the session's (src/tape.zig): a replica + // finds as many places a frame as this did. + if (started_ns != 0 and bytes != std.math.maxInt(usize) and pardes.tape.over(started_ns, frame_ns)) break; } if (set.hits.items.len != found_before) { set.gen +%= 1; diff --git a/src/Query.zig b/src/Query.zig index 09b46f3b..7440dd0e 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -1743,11 +1743,7 @@ fn displayPreview(p: *Pardes, qid: usize, path: []const u8, at: look.Spot) void const f = &pane.file.?; if (!std.mem.eql(u8, f.path, path)) { if (exec.kindOf(path)) |kind| if (kind != .file) return; - const bytes = blk: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :blk filesystem.read(p, path) catch return; - }; + const bytes = pardes.tape.site(.preview, p.gpa, filesystem.read, .{ p, path }) catch return; if (std.mem.indexOfScalar(u8, bytes[0..@min(bytes.len, 8000)], 0) != null) { p.gpa.free(bytes); return; diff --git a/src/Stream.zig b/src/Stream.zig index da7d12eb..8d9663dd 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -104,7 +104,13 @@ pub const ParseJob = struct { } }; -const ParseTask = struct { job: *ParseJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +const ParseTask = struct { + // The worker the host started for it, live: a replica has none. + pub const replica = .{ .authority = .{ "future", "io" } }; + job: *ParseJob, + future: ?std.Io.Future(void) = null, + io: ?std.Io = null, +}; /// The colours of the text a scroll is heading into, worked out on a /// worker before it gets there, as a game streams in the level ahead of @@ -134,12 +140,21 @@ pub const PaintJob = struct { } }; -const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +const PaintTask = struct { + // The worker the host started for it, live: a replica has none. + pub const replica = .{ .authority = .{ "future", "io" } }; + job: *PaintJob, + future: ?std.Io.Future(void) = null, + io: ?std.Io = null, +}; /// A paint's generation has the top bit, so it can never be a search's or /// a parse's (those count from 0 in `gen`, and it would take two billion /// searches to reach it). const paint_bit: u32 = 1 << 31; +/// A query rows' paint's has the next bit too: counted apart from the +/// paints a view's frames ask for (`rows_count`), which are its own. +const rows_bit: u32 = 1 << 30; const max_paint_bytes = 512 << 10; /// Where a worker hands a batch: false when it was turned away (the inbox @@ -172,6 +187,19 @@ pub const Job = struct { const Task = struct { job: *Job, future: std.Io.Future(void), io: std.Io }; +/// What the replay digest leaves out (src/walk.zig): the workers' timings, +/// and the colours painted ahead of a view for its frames, which is the +/// render's own prefetch (a replica that draws nothing paints none). +pub const replica = .{ + // The worker a search runs on, which the host starts. + .authority = .{"task"}, + .local = .{ + "started_ns", "first_ns", "finished_ns", "worker_ns", "parse_ns", + "paint", "paint_retired", "paint_count", "ahead", "ahead_for", + "painted_ahead", "painted_on_loop", "ahead_seen", "ahead_ask", + }, +}; + gen: u32 = 0, state: State = .idle, kind: Kind = .grep, @@ -210,6 +238,10 @@ paint: ?PaintTask = null, /// loop for half a second). No new paint starts while it is out. paint_retired: ?PaintTask = null, paint_count: u32 = 0, +rows_count: u32 = 0, +/// The paint a frame asked for, its worker started once the frame is +/// presented (`askAhead`): starting one is a thread, not a frame's to wait. +ahead_ask: ?u32 = null, ahead: []u8 = &.{}, ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{}, /// Stretches a view took painted ahead, and painted on the loop itself @@ -321,7 +353,17 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont const gen = paint_bit | (s.paint_count & ~paint_bit); job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = serial, .revision = revision, .start = from, .path = path_copy, .source = source }; s.paint = .{ .job = job }; - p.emit(.{ .search = .{ .gen = gen } }); + // Asked of the host once the frame is presented, not queued as an + // effect of the session's: a replica that draws nothing asks for none. + s.ahead_ask = gen; +} + +/// The worker for the paint the last frame asked for: the host starts it +/// after the present (Pardes.pump), off the keystroke's way to the screen. +pub fn askAhead(p: *Pardes) void { + const gen = p.stream.ahead_ask orelse return; + p.stream.ahead_ask = null; + if (p.host.vtable.search) |f| f(p.host.ctx, gen); } /// A file's colours for the query rows that show its lines, the whole @@ -363,8 +405,8 @@ pub fn paintRows(p: *Pardes, path: []const u8) void { }; s.rows_asked[s.rows_asked_at] = key; s.rows_asked_at +%= 1; - s.paint_count +%= 1; - const gen = paint_bit | (s.paint_count & ~paint_bit); + s.rows_count +%= 1; + const gen = paint_bit | rows_bit | (s.rows_count & ~(paint_bit | rows_bit)); job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = 0, .revision = revision, .start = 0, .path = path_copy, .source = source }; s.rows_paint = .{ .job = job }; p.emit(.{ .search = .{ .gen = gen } }); @@ -1145,22 +1187,30 @@ const testing = std.testing; /// worker, hand what it posts to the core. const TestShell = struct { q: host_io.Inbox, + p: ?*Pardes = null, fn init() TestShell { return .{ .q = .{ .gpa = testing.allocator, .lsp_allocator = testing.allocator, .io = testing.io } }; } + /// A shell's search: a worker started for the generation, as the tty's. const vtable: pardes.Host.VTable = .{ .search = struct { - fn f(_: ?*anyopaque, _: u32) void {} + fn f(ctx: ?*anyopaque, gen: u32) void { + const t: *TestShell = @ptrCast(@alignCast(ctx.?)); + start(testing.io, testing.allocator, t.p.?, gen, &t.q, host_io.Inbox.pushSearch); + } }.f }; - fn attach(p: *Pardes) void { - p.host = .{ .vtable = &vtable }; + fn attach(t: *TestShell, p: *Pardes) void { + t.p = p; + p.host = .{ .ctx = t, .vtable = &vtable }; } fn pump(t: *TestShell, p: *Pardes) void { + // What the last frame asked for, as Pardes.pump asks after a present. + askAhead(p); while (p.nextEffect()) |e| switch (e) { - .search => |s| start(testing.io, testing.allocator, p, s.gen, &t.q, host_io.Inbox.pushSearch), + .search => p.perform(e), else => {}, }; var batch: host_io.Inbox.Batch = .{}; @@ -1226,7 +1276,7 @@ test "Stream: a stale generation's rows never land in the query pane, however tw pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); shell.q.deinit(); @@ -1328,7 +1378,7 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop, pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1367,7 +1417,7 @@ test "Stream: a scroll's colours are painted ahead on a worker, so PageDown afte pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1410,7 +1460,7 @@ test "zz stream timings on the stress file with a worker (QUERY_TIMES=)" { pane.file.?.path = try p.gpa.dupe(u8, path); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1464,7 +1514,7 @@ test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); shell.q.deinit(); @@ -1508,7 +1558,7 @@ test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a work pane.file.?.path = try p.gpa.dupe(u8, path); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1604,7 +1654,7 @@ test "Stream: a Grep's rows are coloured as their file's language colours them, p.gpa.free(pane.file.?.path); pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelPaint(); diff --git a/src/Tables.zig b/src/Tables.zig index 2e65a716..44918559 100644 --- a/src/Tables.zig +++ b/src/Tables.zig @@ -95,8 +95,13 @@ pending: ?Resume = null, /// and whether the line before ended a block. const Resume = struct { at: usize, n: u32, fence: ?Fence, prev_blank: bool }; -/// What one frame spends reading a big text from the top. +/// What one frame spends reading a big text from the top: on the session's +/// tick, by the clock (src/tape.zig keeps a replica's reading in step). pub const frame_ns: u64 = 4 * std.time.ns_per_ms; +/// What a new text's first look reads at once, wherever it is asked for +/// (the render, or a step reading the view's geometry): a count, never the +/// clock, so whoever asks first finds the same tables. +pub const first_lines: u32 = 4096; pub fn deinit(t: *Tables, gpa: std.mem.Allocator) void { t.list.deinit(gpa); @@ -130,10 +135,7 @@ pub fn find(t: *const Tables, line: usize) ?*const Table { /// touch shift and only the block around it is read again (`reflow`): a /// keystroke costs its table, not the file. pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { - if (t.current(f)) { - if (t.pending != null) t.go(gpa, f.content, frame_ns); - return; - } + if (t.current(f)) return; defer { t.revision = f.revision; t.content = @intFromPtr(f.content.ptr); @@ -157,13 +159,20 @@ pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { // read: it goes on. if (lineHolding(starts, f.splice.head) >= r.n) { t.lines = starts.len; - t.go(gpa, f.content, frame_ns); + t.go(gpa, f.content, 0, first_lines); return; } } else if (t.reflow(gpa, f)) |_| return else |_| {} } t.begin(starts.len); - t.go(gpa, f.content, frame_ns); + t.go(gpa, f.content, 0, first_lines); +} + +/// A frame's more of a reading under way: on the session's tick, so a core +/// that draws reads exactly as far as one that does not. +pub fn step(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { + t.ensure(gpa, f); + if (t.pending != null) t.go(gpa, f.content, frame_ns, std.math.maxInt(u32)); } /// Whether a reading of the text is still under way (Pardes.nextWake). @@ -183,11 +192,11 @@ fn begin(t: *Tables, lines: usize) void { t.pending = .{ .at = 0, .n = 0, .fence = null, .prev_blank = true }; } -/// Read on for about `budget_ns` (0: to the end). On no memory there are -/// no tables (the text is shown as it is). -fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64) void { +/// Read on for about `budget_ns` (0: no clock) or `max_lines`, whichever +/// ends first. On no memory there are no tables (the text is shown as it is). +fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64, max_lines: u32) void { var at = t.pending orelse return; - const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns) catch { + const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns, max_lines) catch { t.list.clearRetainingCapacity(); t.pending = null; return; @@ -290,7 +299,7 @@ fn reflow(t: *Tables, gpa: std.mem.Allocator, f: *File.State) !void { const prev_blank = r0 == 0 or blank(prev) or closesAny(prev) or std.mem.startsWith(u8, prev[@min(indentOf(prev), prev.len)..], "#"); const head = lineOf(content, starts, r0); var at: Resume = .{ .at = head.start, .n = head.n, .fence = null, .prev_blank = prev_blank }; - _ = try t.scanFrom(gpa, content, &at, @intCast(r1), false, 0); + _ = try t.scanFrom(gpa, content, &at, @intCast(r1), false, 0, std.math.maxInt(u32)); const fresh = try gpa.dupe(Table, t.list.items[before..]); defer gpa.free(fresh); t.list.shrinkRetainingCapacity(before); @@ -500,7 +509,7 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { t.aligns.clearRetainingCapacity(); t.begin(std.mem.count(u8, content, "\n") + 1); var at = t.pending.?; - _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0); + _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0, std.math.maxInt(u32)); t.pending = null; } @@ -508,7 +517,7 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { /// appending them; with `fences`, keep the fences. With a budget, stop /// when it is spent, `at` where to go on from. Whether it reached the end /// (or `stop`). -fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64) !bool { +fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64, max_lines: u32) !bool { var fence = at.fence; var prev_blank = at.prev_blank; var line = lineAt(content, at.at, at.n); @@ -516,7 +525,7 @@ fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume var lines: usize = 0; while (line) |l| : (lines += 1) { if (l.n > stop) break; - if (budget_ns > 0 and lines % 256 == 255 and panes.Places.nowNs() -| t0 > budget_ns) { + if (lines % 256 == 255 and (lines >= max_lines or (budget_ns > 0 and pardes.tape.over(t0, budget_ns)))) { at.* = .{ .at = l.start, .n = l.n, .fence = fence, .prev_blank = prev_blank }; return false; } @@ -1049,6 +1058,7 @@ test "tables: a big text's tables are read a frame's budget at a time from the t File.setContent(p, f, new); edits += 1; } + p.update(.tick); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); try testing.expect(frames < 10_000); @@ -1077,6 +1087,7 @@ test "tables: a table wider than a wrapping pane is shown as its source, laid ou // Source: a pipe is a pipe, at its source column. try testing.expectEqual(@as(i32, 7), File.displayOffset(pane, 5, 0, 7)); try testing.expect(p.executeBuiltinLine(0, "Wrap off")); + p.sync(); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); try testing.expect(!pane.body_wrapped); diff --git a/src/Text.zig b/src/Text.zig index b57c953a..3f107542 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -49,6 +49,10 @@ pub const SelRange = struct { sticky: i32 = -1, }; +/// The replay digest's walk (src/walk.zig) of the histories: as far as +/// they go. +pub const replica = .{ .len = .{ .{ "ed_undo", "ed_undo_len" }, .{ "ed_redo", "ed_redo_len" } } }; + what: What = .body, /// The characters of a text that holds its own (gpa-owned): everything but /// a body, which reads its pane's. A tag nobody has changed has none yet and diff --git a/src/body_layer.zig b/src/body_layer.zig index bfca268a..5bffb691 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -169,22 +169,17 @@ pub fn buttonInks(th: *const colors.Theme) [3][3]u8 { /// rows the grid cannot show, and is painted straight onto the grid. The /// rail is the grid's alone. pub fn joinBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rail: pardes.Region) !void { - const file = if (pane.file) |*f| f else return renderBody(p, s, arena, pane, r, id, active, rail, null); - if (!p.settings.tree_context_tag_style or pane.collapsed or r.h <= pane.tag_rows) + if (pane.file == null) return renderBody(p, s, arena, pane, r, id, active, rail, null); + // Made afresh for every frame from the pane as it is, as a step reading + // it would make it (File.geometry): never only by drawing. + pane.geometry_key = 0; + panes.File.geometry(pane); + // Which rows are context rows is decided by the text the body shows: a + // layer taller than the grid's rows when there are any. + if (!p.settings.tree_context_tag_style or pane.collapsed or r.h <= pane.tag_rows or pane.body_rows == 0) return renderBody(p, s, arena, pane, r, id, active, rail, null); - // Which rows are context rows is decided by the text the body shows. - const text = try panes.File.bodyText(arena, pane, file, p.settings.wrap); const viewport_rows = r.h - pane.tag_rows; - const logical_rows = @min(@as(usize, pane.rows) + pane.context_rows, pane.wrap_line.len); - if (pane.context_rows == 0 or logical_rows <= pane.context_rows) - return renderBody(p, s, arena, pane, r, id, active, rail, text); - pane.body_rows = @intCast(logical_rows); - const bh: usize = @max(1, p.row_metrics.body_h); - const th: usize = std.math.clamp(p.row_metrics.tagline_h, 1, bh); - const body_pixels = @as(usize, viewport_rows) * bh; - pane.context_row_limit = @intCast(@min(pane.context_rows, (body_pixels -| bh) / th)); - const context_pixels = @as(usize, pane.context_row_limit.?) * th; - pane.body_visible_rows = @intCast(@min(logical_rows, pane.context_row_limit.? + @max(1, (body_pixels -| context_pixels) / bh))); + const logical_rows: usize = pane.body_rows; const width = r.w - config.GUTTER; const layer = &s.body_layers[id]; const count = logical_rows * width; @@ -512,6 +507,6 @@ pub fn bodyText(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) ![]const u8 { ); unreachable; } - if (pane.file) |*f| return panes.File.bodyText(arena, pane, f, p.settings.wrap); + if (pane.file) |*f| return panes.File.bodyText(arena, pane, f); return panes.terminal.bodyText(arena, pane); } diff --git a/src/builtins.zig b/src/builtins.zig index 3a66a316..a89b1180 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -793,11 +793,7 @@ pub const Mount = struct { }; // A mount that cannot be reached is said now, and taken back, not // found out by the first look through it. - const reached = reached: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :reached @import("9p_io.zig").Client.probe(c.p.gpa, dial); - }; + const reached = pardes.tape.site(.probe, null, @import("9p_io.zig").Client.probe, .{ c.p.gpa, dial }); reached catch |err| { if (c.p.fs.mounts.pop()) |gone| { c.p.gpa.free(gone.name); @@ -927,11 +923,7 @@ pub const TreeContext = struct { }; file.tree_context = on; file.syntax_dirty = true; - c.pane.wrap_n = 0; - c.pane.context_rows = 0; - c.pane.body_rows = 0; - c.pane.body_visible_rows = 0; - c.pane.context_row_limit = null; + c.pane.geometry_key = 0; c.p.surface.body_layers[c.id].rows = 0; } }; diff --git a/src/config.zig b/src/config.zig index f0023193..13404461 100644 --- a/src/config.zig +++ b/src/config.zig @@ -455,6 +455,11 @@ test "wheel drift guard" { /// The shell when no `Shell` is set: the user's login shell ($SHELL), as a /// terminal would start, else /bin/sh. An explicit `Shell` overrides it. pub fn defaultShell() []const u8 { + // As the session that recorded found it (src/tape.zig). + return pardes.tape.value(.env, defaultShellHost, .{}); +} + +fn defaultShellHost() []const u8 { if (comptime pardes.hosted) if (std.c.getenv("SHELL")) |s| { const shell = std.mem.span(s); // One that is not there, or not executable, is no shell: /bin/sh. diff --git a/src/draw.zig b/src/draw.zig index e141ce31..4f9c6c87 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -96,12 +96,10 @@ pub fn place(p: *Pardes, s: *Surface) void { if (r.h <= pane.tag_rows) continue; const tx = r.x + config.GUTTER; const tw = r.w - config.GUTTER; - var leader_buf: [16]u8 = undefined; for (0..pane.notices.len) |i| { - const text = Messages.noticeText(p, pane, i, &leader_buf); - const chip = Messages.noticeCols(p, text, tw); - if (chip == 0) continue; - pane.notices.left[i] = tx + tw - chip; + const left = pane.notices.left[i]; + if (left == std.math.maxInt(u16)) continue; + const chip = tx + tw - left; s.addRegion(.{ .kind = .notice, .owner = owner, .serial = pane.serial, .active = active, .notice = @intCast(i), .rect = .{ .x = tx + tw - chip, .y = body_y + @as(u16, pane.notices.row[i]), .w = chip, .h = BOX_H } }); } } @@ -521,6 +519,8 @@ fn resolved(color: Color, default: ?[3]u8, palette: ?[16][3]u8) ?[3]u8 { } pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { + const taped = pardes.tape.enter(p, .render); + defer pardes.tape.leave(p, taped); panes.File.refreshHighlights(p); const s = &p.surface; const ncells = @as(usize, p.screen_w) * p.screen_h; @@ -606,9 +606,6 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { for (&p.panes, 0..) |*slot, id| { const pane = slot.* orelse continue; - pane.body_rows = 0; - pane.body_visible_rows = 0; - pane.context_row_limit = null; s.body_layers[id].rows = 0; try renderPane(p, s, arena, pane, p.rects[id], id, id == p.active); } @@ -976,7 +973,6 @@ pub fn paintHeaders(p: *Pardes, s: *Surface, arena: std.mem.Allocator) !void { const chrome = p.chromeTheme(); // global tagbar: full width, top rows — unless a native shell has // taken its commands over, in which case the rows are not ours to draw - tagline.revealHeader(p); if (s.region(.workspace_tag, Region.none)) |region| { const top = if (p.header_focus and p.header_column == null) p.header_top else 0; const layer = &s.tag_layers[MAX_PANES + MAX_COLS]; @@ -2286,12 +2282,9 @@ test "a body with context rows is painted once, and the grid's rows are what a p const cells = try arena.allocator().alloc(Cell, s.cells.len); @memset(cells, .{}); var plain: Surface = .{ .cols = s.cols, .rows = s.rows, .cells = cells }; - const saved = .{ pane.body_rows, pane.body_visible_rows, pane.context_row_limit }; - pane.body_rows = 0; - pane.body_visible_rows = 0; - pane.context_row_limit = null; + pane.view.layered = false; try body_layer.renderBody(p, &plain, arena.allocator(), pane, p.rects[0], 0, true, null, null); - pane.body_rows, pane.body_visible_rows, pane.context_row_limit = saved; + pane.view.layered = true; for (0..body.rect.h) |row| { const y = body.rect.y + row; for (0..body.rect.w) |col| { diff --git a/src/dump.zig b/src/dump.zig index 0da2513f..f9f01950 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -880,12 +880,16 @@ fn applySettings(p: *Pardes, lines: []const []const u8) void { /// The replacement core for a Restore of the dump at `from`, whose log says /// so after its panes' `new`s, for a client that reconnects to read. pub fn restore(p: *Pardes, zon_bytes: []const u8, from: []const u8) !*Pardes { + const taped = pardes.tape.enter(p, .{ .restore = .{ .bytes = zon_bytes, .from = from } }); + defer pardes.tape.leave(p, taped); var opts = p.opts; opts.cols = p.screen_w; opts.rows = p.screen_h; var old: OldSerials = .{}; const replacement = try initDump(p.gpa, opts, zon_bytes, p, &old); replacement.pointRestoreAt(from); + // The tape follows the replacement from here (it is the session). + pardes.tape.adopt(replacement); pardes.ctlfs.events.notePath(replacement, "restore", from); // Which serial each pane and column had, for a script that held them. for (replacement.panes, old.panes) |slot, was| if (slot) |pane| if (was != 0) { @@ -900,7 +904,14 @@ pub fn restore(p: *Pardes, zon_bytes: []const u8, from: []const u8) !*Pardes { } pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8) !*Pardes { - return initDump(gpa, opts, zon_bytes, null, null); + pardes.tape.startFromEnv(); + const taped = pardes.tape.enter(null, .{ .init_from_dump = .{ .opts = opts, .bytes = zon_bytes } }); + const p = initDump(gpa, opts, zon_bytes, null, null) catch |err| { + pardes.tape.leave(null, taped); + return err; + }; + pardes.tape.leave(p, taped); + return p; } /// The serials a dump's panes and columns had, read as it is restored. diff --git a/src/exec.zig b/src/exec.zig index c14a648c..8f84d2c6 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -485,8 +485,10 @@ pub fn isFile(path: []const u8) bool { pub fn kindOf(path: []const u8) ?std.Io.File.Kind { var buf: [4096]u8 = undefined; const z = std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0) catch return null; - pardes.turn.yield(); - defer pardes.turn.back(); + return pardes.tape.site(.kind, null, kindHost, .{z}); +} + +fn kindHost(z: [:0]const u8) ?std.Io.File.Kind { const stat = @import("fs.zig").statPath(std.Io.Threaded.global_single_threaded.io(), z, .{}) catch return null; return stat.kind; } @@ -1116,7 +1118,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[] } if (comptime pardes.hosted) { var why: [320]u8 = undefined; - if (@import("host_io.zig").Shell.refusal(want[0..@min(want.len, 200)], &why)) |refused| { + if (pardes.tape.value(.shell, @import("host_io.zig").Shell.refusal, .{ want[0..@min(want.len, 200)], &why })) |refused| { var text: [340]u8 = undefined; return p.reportFailure(p.active, std.fmt.bufPrint(&text, "Shell: {s}", .{refused}) catch "Shell: no such shell"); } @@ -1161,10 +1163,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[] for (p.panes, 0..) |slot, id| { p.surface.body_layers[id].rows = 0; const pane = slot orelse continue; - pane.body_rows = 0; - pane.body_visible_rows = 0; - pane.context_row_limit = null; - pane.wrap_n = 0; + pane.geometry_key = 0; } p.abandonPanelAnimations(); } diff --git a/src/fs.zig b/src/fs.zig index 9104c93f..e4373bf0 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -171,11 +171,7 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { const path = osPath(p, req.node) orelse return Reply.fail(req.tag, E.NOENT); if (req.op == .release) return .{ .tag = req.tag }; const io = std.Io.Threaded.global_single_threaded.io(); - const stat = stat: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :stat statPath(io, path, .{}) catch return Reply.fail(req.tag, E.NOENT); - }; + const stat = pardes.tape.site(.os_stat, null, statPath, .{ io, path, std.Io.Dir.StatFileOptions{} }) catch return Reply.fail(req.tag, E.NOENT); const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 }; switch (req.op) { .getattr => return .{ .tag = req.tag, .attr = attr }, @@ -199,32 +195,10 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { // Listed into a buffer of this request's own first; the shared // reply buffer is another request's to use while the turn is out. var listing: [16 * 1024]u8 = undefined; - var fixed: std.heap.FixedBufferAllocator = .init(&listing); - var listed: std.ArrayList(u8) = .empty; - { - pardes.turn.yield(); - defer pardes.turn.back(); - var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return Reply.fail(req.tag, E.NOTDIR); - defer dir.close(io); - var it = dir.iterate(); - var skip = req.off; - while (it.next(io) catch return Reply.fail(req.tag, E.IO)) |entry| { - var buf: [4096]u8 = undefined; - const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue; - var normalized_buf: [4096]u8 = undefined; - const normalized = resolveOs(joined, &normalized_buf) orelse continue; - const child = dir.statFile(io, entry.name, .{}) catch continue; - if (skip > 0) { - skip -= 1; - continue; - } - const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1)); - tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name); - if (listed.items.len >= @max(req.size, 512)) break; - } - } + const listed = pardes.tape.site(.os_listing, null, osListing, .{ io, path, req.off, req.size, &listing }) catch |err| + return Reply.fail(req.tag, if (err == error.NotDir) E.NOTDIR else E.IO); const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, listed.items) catch return Reply.fail(req.tag, E.NOMEM); + out.appendSlice(p.gpa, listed) catch return Reply.fail(req.tag, E.NOMEM); return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } }; }, .read, .write, .setattr => { @@ -237,37 +211,70 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { var z: [4096]u8 = undefined; const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return Reply.fail(req.tag, E.NOENT); var buf: [ninep_io.msize]u8 = undefined; // a read never asks for more than a frame - const got = io: { - pardes.turn.yield(); - defer pardes.turn.back(); - const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true }); - if (fd < 0) return Reply.fail(req.tag, E.PERM); - defer _ = libc.close(fd); - if (req.op == .setattr) { - if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero"); - var truncated = attr; - truncated.size = 0; - return .{ .tag = req.tag, .attr = truncated }; - } - if (req.off > std.math.maxInt(i64)) return tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold"); - if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return Reply.fail(req.tag, E.IO); - if (req.op == .write) { - const written = libc.write(fd, req.data.ptr, req.data.len); - if (written < 0) return Reply.fail(req.tag, E.IO); - return .{ .tag = req.tag, .written = @intCast(written) }; - } - const got = libc.read(fd, &buf, @min(req.size, buf.len)); - if (got < 0) return Reply.fail(req.tag, E.IO); - break :io @as(usize, @intCast(got)); + const got = switch (pardes.tape.site(.os_rw, null, osReadWrite, .{ path_z, req, attr, &buf })) { + .reply => |reply| return reply, + .read => |bytes| bytes, }; const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, buf[0..got]) catch return Reply.fail(req.tag, E.NOMEM); - return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got) } }; + out.appendSlice(p.gpa, got) catch return Reply.fail(req.tag, E.NOMEM); + return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got.len) } }; }, else => return Reply.fail(req.tag, E.PERM), } } +/// A directory's listing under /os, the entries from `off` until `size` +/// bytes of them are staged, into `listing`: the syscalls of a readdir. +fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * 1024]u8) error{ NotDir, Io }![]const u8 { + var fixed: std.heap.FixedBufferAllocator = .init(listing); + var listed: std.ArrayList(u8) = .empty; + var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return error.NotDir; + defer dir.close(io); + var it = dir.iterate(); + var skip = off; + while (it.next(io) catch return error.Io) |entry| { + var buf: [4096]u8 = undefined; + const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue; + var normalized_buf: [4096]u8 = undefined; + const normalized = resolveOs(joined, &normalized_buf) orelse continue; + const child = dir.statFile(io, entry.name, .{}) catch continue; + if (skip > 0) { + skip -= 1; + continue; + } + const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1)); + tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name); + if (listed.items.len >= @max(size, 512)) break; + } + return listed.items; +} + +const OsReadWrite = union(enum) { reply: Reply, read: []const u8 }; + +/// The syscalls of an /os read, write or truncate: the reply, or the bytes +/// read into `buf`. +fn osReadWrite(path_z: [:0]const u8, req: Req, attr: Reply.Attr, buf: []u8) OsReadWrite { + const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true }); + if (fd < 0) return .{ .reply = Reply.fail(req.tag, E.PERM) }; + defer _ = libc.close(fd); + if (req.op == .setattr) { + if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero") }; + var truncated = attr; + truncated.size = 0; + return .{ .reply = .{ .tag = req.tag, .attr = truncated } }; + } + if (req.off > std.math.maxInt(i64)) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold") }; + if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return .{ .reply = Reply.fail(req.tag, E.IO) }; + if (req.op == .write) { + const written = libc.write(fd, req.data.ptr, req.data.len); + if (written < 0) return .{ .reply = Reply.fail(req.tag, E.IO) }; + return .{ .reply = .{ .tag = req.tag, .written = @intCast(written) } }; + } + const got = libc.read(fd, buf.ptr, @min(req.size, buf.len)); + if (got < 0) return .{ .reply = Reply.fail(req.tag, E.IO) }; + return .{ .read = buf[0..@intCast(got)] }; +} + pub fn validMountName(name: []const u8) bool { if (name.len == 0 or name.len > name_capacity or std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return false; if (std.mem.eql(u8, name, "os") or std.mem.eql(u8, name, "self")) return false; @@ -687,8 +694,10 @@ test "a path with a part over 255 bytes is NameTooLong, never a panic" { pub fn makeDirs(dir: []const u8) void { if (comptime !platform_has_fs) return; if (dir.len == 0) return; - pardes.turn.yield(); - defer pardes.turn.back(); + pardes.tape.site(.make_dirs, null, makeDirsHost, .{dir}); +} + +fn makeDirsHost(dir: []const u8) void { _ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o755)) catch {}; } @@ -798,6 +807,12 @@ pub fn dotRelative(word: []const u8) bool { /// `~` to expand, or no home for it. pub fn expandHome(path: []const u8, out: *[4096]u8) []const u8 { if (comptime !pardes.hosted) return path; + if (path.len == 0 or path[0] != '~') return path; + // Home as the session that recorded found it (src/tape.zig). + return pardes.tape.value(.env, expandHomeHost, .{ path, out }); +} + +fn expandHomeHost(path: []const u8, out: *[4096]u8) []const u8 { return expandHomeFrom(path, out, std.c.getenv("HOME")); } @@ -846,8 +861,10 @@ pub fn resolveOs(path: []const u8, out: *[4096]u8) ?Resolved { if (comptime !platform_has_fs) return null; var z: [4096]u8 = undefined; const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return null; - pardes.turn.yield(); - defer pardes.turn.back(); + return pardes.tape.site(.resolve, null, resolveHost, .{ path_z, out }); +} + +fn resolveHost(path_z: [:0]const u8, out: *[4096]u8) ?Resolved { const resolved = realpath(path_z, out) orelse return null; return .{ .path = std.mem.span(resolved), .dir = isDir(resolved) }; } @@ -904,9 +921,7 @@ pub fn readLimit(p: *pardes.Pardes, path: []const u8, max_bytes: usize) ![]u8 { if (!std.mem.eql(u8, name, mount.name)) continue; // A peer answers on its own time, and the peer may be this very // editor: the turn goes out with the request. - pardes.turn.yield(); - defer pardes.turn.back(); - return ninep_io.Client.readLimit(p.gpa, mount.dial, remote_path, path, limit); + return pardes.tape.site(.read_mount, p.gpa, ninep_io.Client.readLimit, .{ p.gpa, mount.dial, remote_path, path, limit }); } return error.FileNotFound; } @@ -1233,8 +1248,10 @@ fn readFileLimit(gpa: std.mem.Allocator, path: []const u8, limit: usize) ![]u8 { const path_z = std.fmt.bufPrintSentinel(&pathbuf, "{s}", .{native}, 0) catch return error.PathTooLong; // The path may be a mount this editor serves: the turn goes out with the // syscalls, and the bytes come back into memory of the caller's own. - pardes.turn.yield(); - defer pardes.turn.back(); + return pardes.tape.site(.read_file, gpa, readHost, .{ gpa, path_z, limit }); +} + +fn readHost(gpa: std.mem.Allocator, path_z: [:0]const u8, limit: usize) ![]u8 { const fd = libc.open(path_z, .{ .ACCMODE = .RDONLY, .NONBLOCK = true }); if (fd < 0) return switch (libc.errno(fd)) { .ACCES, .PERM => error.PermissionDenied, diff --git a/src/image.zig b/src/image.zig index e334965a..20d6d6f0 100644 --- a/src/image.zig +++ b/src/image.zig @@ -931,6 +931,10 @@ const GridKey = struct { }; pub const State = struct { + /// What the replay digest leaves out (src/walk.zig): the picture as + /// decoded from its bytes, off the loop as the render first wants it, + /// and the glyph art drawn from that. + pub const replica = .{ .derived = .{ "tried", "rgba", "iw", "ih", "grid", "grid_key", "decoder", "decoded", "answer" } }; path: []u8, glyph_art: bool = false, pmode: PaletteMode = .commodore, diff --git a/src/look.zig b/src/look.zig index 1a574212..226c96e3 100644 --- a/src/look.zig +++ b/src/look.zig @@ -954,7 +954,7 @@ fn noteLookHover(p: *Pardes, col: u16, row: u16, body_hit: ?Mouse.BodyHit, tag_h pub fn refreshLookHoverFromRaw(p: *Pardes) void { if (!p.pointer_inside or !p.raw_hover_intent) return; - const mapped = p.presentation.pointer(p.screen_w, p.screen_h, p.pointer_raw_col, p.pointer_raw_row) orelse + const mapped = p.pointerCell(p.pointer_raw_col, p.pointer_raw_row) orelse return cancelLookHover(p); p.hover_col = mapped.col; p.hover_row = mapped.row; diff --git a/src/main.zig b/src/main.zig index 3d26d1fd..378dbea8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -333,6 +333,8 @@ fn webMain() !void { fn nativeMain(std_init: std.process.Init) !void { var init = std_init; if (builtin.mode != .debug) init.gpa = pardes.memory.host(std.heap.c_allocator); + // A recorded session's tape replayed, no session of its own (src/tape.zig). + if (init.environ_map.get("PARDES_REPLAY")) |tape| std.process.exit(pardes.tape.replayMain(init.io, tape)); var stderr_probe: [64]u8 = undefined; _ = std.debug.lockStderr(&stderr_probe); std.debug.unlockStderr(); diff --git a/src/mouse.zig b/src/mouse.zig index 2ff4fef3..67ebf7cf 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -380,7 +380,7 @@ fn programMouse(p: *Pardes, m: Mouse) bool { else => return false, }; const bit: u3 = if (button) |b| @as(u3, 1) << @intCast(@backingInt(b)) else 0; - const at = p.presentation.pointer(p.screen_w, p.screen_h, @min(m.col, p.screen_w -| 1), @min(m.row, p.screen_h -| 1)); + const at = p.pointerCell(@min(m.col, p.screen_w -| 1), @min(m.row, p.screen_h -| 1)); var mods: panes.terminal.MouseMods = .{ .shift = m.shift, .alt = m.alt, .ctrl = m.ctrl }; var buf: [64]u8 = undefined; if (p.mouse_grab) |*grab| { @@ -535,7 +535,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { return; } p.raw_hover_intent = false; - const mapped = p.presentation.pointer(p.screen_w, p.screen_h, raw_col, raw_row) orelse { + const mapped = p.pointerCell(raw_col, raw_row) orelse { look.cancelLookHover(p); if (m.kind == .release) { if (p.header_sweep) |sweep| if (sweep.button == m.button) { @@ -931,6 +931,7 @@ pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) vo const r = p.rects[s.id]; const b = @backingInt(s.button); const tag_rows: i32 = pane.tag_rows; + panes.File.geometry(pane); if (pane.body_rows > 0 and pane.sel[b].r0 >= tag_rows) if (body_hit) |supplied| { const mapped_hit = bodyHitForPane(p, s.id, supplied, true) orelse return; pane.sel[b].c1 = @as(i32, mapped_hit.col) - config.GUTTER; diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index f9112ae2..33f816fe 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -1002,8 +1002,7 @@ pub fn nameBuffer(p: *Pardes, id: usize, full: []const u8, preserve_tag: bool) ! f.highlight_start = 0; f.syntax_dirty = true; f.context_revision = null; - pane.context_rows = 0; - pane.wrap_n = 0; + pane.geometry_key = 0; look.invalidateLookHover(p, id); p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); events.noteLog(p, .rename, pane); diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index 7b107ea7..bda2c1d1 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -1192,6 +1192,8 @@ fn linesOpen(target: Target, req: Req) bool { /// The editor's step: the last lines of closed opens run, each as a write /// of its own; a refusal is the log's err, as no open is left to answer. pub fn runClosedLines(p: *Pardes) void { + const taped = pardes.tape.enter(p, .run_closed_lines); + defer pardes.tape.leave(p, taped); for (p.fs.closed_lines.items) |*line| { defer line.bytes.deinit(p.gpa); const t = Node.target(line.node) orelse continue; diff --git a/src/normal.zig b/src/normal.zig index 9cac18b0..f214121a 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -713,6 +713,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void .middle => { // helix align_view_middle: the cursor's column to the middle // of an unwrapped view + panes.File.geometry(pane); if (pane.body_wrapped) return; const line = text[modal.lineStartOffset(text, modal.lineAtOffset(text, cur))..modal.lineEndOffset(text, modal.lineAtOffset(text, cur))]; const w: usize = @intCast(@max(1, @as(i32, pane.cols) - @as(i32, panes.File.gutterWidth(pane)))); diff --git a/src/panes.zig b/src/panes.zig index 51710d08..a782e3e0 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -24,6 +24,14 @@ const Key = pardes.Key; pub const EditText = struct { text: []u8, row0: i32 }; pub const Pane = struct { + /// What the replay digest leaves out (src/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. + pub const replica = .{ + .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", "view", "notices" }, + .authority = .{"fs"}, + .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, + }; /// The words that throw unsaved text away (builtins.zig warnModified). pub const Discarding = enum { Exit, Restore, get, Del, Delcol }; @@ -128,7 +136,12 @@ pub const Pane = struct { /// The last command line pardes typed into this terminal (an exec, a /// middle click, a pty/run): its first word, which Kill matches, and /// the shell's command count its start (C mark) reaches. - sent_command: ?struct { word: [64]u8 = undefined, len: u8 = 0, seq: u32 = 0 } = null, + sent_command: ?struct { + pub const replica = .{ .len = .{.{ "word", "len" }} }; + word: [64]u8 = undefined, + len: u8 = 0, + seq: u32 = 0, + } = null, /// A command pane (exec.zig runCommand): the line its child runs as /// `$SHELL -c`, owned, where an interactive terminal has null. command: ?[]u8 = null, @@ -245,6 +258,12 @@ pub const Pane = struct { body_rows: u16 = 0, body_visible_rows: u16 = 0, context_row_limit: ?u16 = null, + /// What the geometry above was made for (File.geometry), and the bytes + /// its text takes; 0 has it made again by whoever reads it next. + geometry_key: u64 = 0, + geometry_len: usize = 0, + /// The session's part of the geometry, as of the last step (`sync`). + view: struct { wrap: bool = true, layered: bool = true, body_h: u32 = 0, tagline_h: u32 = 0, rect_h: u16 = 0 } = .{}, sel: [3]Sel = @splat(.{}), pointer_selections: [3]?PointerSelection = @splat(null), next_pointer_selection: u64 = 0, @@ -292,6 +311,7 @@ pub const Pane = struct { /// Older message lines kept on screen under a new one. pub const message_stack = 3; pub const OlderMessage = struct { + pub const replica = .{ .len = .{.{ "text", "len" }} }; text: [256]u8 = undefined, len: u16 = 0, life: MessageLife = .{}, @@ -515,6 +535,7 @@ pub const Pane = struct { } pub fn wrapAt(pane: *Pane, vr: i32) struct { line: i32, at: i32, pad: i32 = 0 } { + File.geometry(pane); if (pane.wrap_n > 0 and vr >= 0 and vr < @as(i32, pane.wrap_n)) return .{ .line = pane.wrap_line[@intCast(vr)], .at = pane.wrap_col[@intCast(vr)], .pad = pane.wrap_pad[@intCast(vr)] }; // unwrapped: rows ARE lines, and the byte column a row starts at is the @@ -523,6 +544,7 @@ pub const Pane = struct { } pub fn wrapRow(pane: *Pane, line: i32, col: i32) struct { row: i32, at: i32, pad: i32 = 0 } { + File.geometry(pane); if (pane.wrap_n == 0) return .{ .row = line - pane.scroll(), .at = pane.hscroll }; var i: u16 = 0; while (i < pane.wrap_n) : (i += 1) { @@ -572,10 +594,11 @@ pub const Pane = struct { } pub fn ensureCursorVisible(pane: *Pane) void { + File.geometry(pane); const off = pane.scroll(); var in_context = false; - // Maps survive until the next render. A jump may have changed scroll - // already, so only treat an ancestor as pinned in the matching view. + // The map is the view's as it is now (File.geometry): an ancestor is + // pinned only in the view it was found for. if (pane.context_rows < pane.wrap_n and pane.wrap_line[pane.context_rows] == off) { for (pane.wrap_line[0..pane.context_rows]) |row| { if (row == pane.body.cur_row) { diff --git a/src/pardes.zig b/src/pardes.zig index 2bee1f98..b5e729bb 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -61,6 +61,8 @@ pub const config = @import("config.zig"); pub const pdf_enabled = panes.Pdf.enabled; pub const pdf = panes.Pdf.pdf; pub const memory = @import("memory.zig"); +pub const tape = @import("tape.zig"); +pub const walk = @import("walk.zig"); pub const image = @import("image.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -165,6 +167,8 @@ pub const Turn = struct { /// a step. Nests: a directory listing that resolves each entry yields /// inside a yield, and only the outermost one lets go. pub fn yield(t: *Turn) void { + // A replica makes no syscall: each is a site of the tape's. + if (tape.replaying()) @panic("tape: a syscall that is no tape site, made replaying"); const io = t.io orelse return; yield_depth += 1; if (yield_depth != 1) return; @@ -504,6 +508,7 @@ test { _ = @import("Finder.zig"); _ = @import("Tables.zig"); _ = @import("Handover.zig"); + _ = @import("walk.zig"); } pub const Builtin = builtins.registry.Builtin(); @@ -759,6 +764,7 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest pane.file.?.tree_context = true; pane.file.?.scroll = 8; pane.msg_len = 0; + p.sync(); _ = try p.render(p.scratch.allocator()); try std.testing.expect(p.settings.tree_context_tag_style); const full_height = p.surface.body_layers[0].viewport.h; @@ -766,6 +772,7 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest try std.testing.expectEqual(FontRole.tagline, p.surface.body_layers[0].cells[0].style.font_role); pane.msg[0] = '!'; pane.msg_len = 1; + p.sync(); _ = try p.render(p.scratch.allocator()); // An overlay takes nothing: the message is a chip OVER the body's first // row, so the viewport keeps every row it had. @@ -783,10 +790,12 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest try std.testing.expectEqualStrings("!", band.cells[p.tagCapacity(band.viewport.w) - 2].grapheme()); pane.msg_len = 0; p.leader_on = true; + p.sync(); _ = try p.render(p.scratch.allocator()); try std.testing.expectEqual(full_height, p.surface.body_layers[0].viewport.h); p.leader_on = false; try std.testing.expect(p.executeBuiltinLine(0, "TreeContextTagStyle")); + p.sync(); _ = try p.render(p.scratch.allocator()); try std.testing.expectEqual(@as(u16, 0), p.surface.body_layers[0].rows); try std.testing.expectEqual(@as(u16, 0), pane.body_rows); @@ -3763,6 +3772,7 @@ pub const Effect = union(enum) { fn Buf(comptime n: usize) type { return struct { + pub const replica = .{ .len = .{.{ "data", "len" }} }; data: [n]u8 = undefined, len: u16 = 0, pub const cap = n; @@ -4701,6 +4711,26 @@ pub const Pardes = struct { /// The message row's entry points, called from everywhere as /// `p.setMessage(...)`; they live with the rest of it in Messages.zig. + /// What the replay digest leaves out (src/walk.zig): what drawing + /// keeps, what is recomputed, and what only the session that performs + /// effects has. + pub const replica = .{ + .local = .{ + // A frame, and the animations and timings that make it. + "surface", "presentation", "frames", "chrome_animation", "lifts", "lift_serials", + "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "frame_arena", "scratch", + "needs_frame", "present_skipped", "shell_continuous", "native_image_patches", + // Drained by every pump before its digest; a replica's queues + // are its own. + "effects", "in_q", + }, + .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 store file + // every window of the user shares (and Recent read from it). + .authority = .{ "host", "fs", "store", "recent" }, + .len = .{ .{ "jumps", "njumps" }, .{ "look_src", "n_look_src" }, .{ "leader_keys", "leader_n" }, .{ "col_x", "ncol" }, .{ "col_w", "ncol" }, .{ "session_dir", "session_dir_len" } }, + }; + pub const setStatus = Messages.setStatus; pub const setMessage = Messages.setMessage; pub const reportError = Messages.reportError; @@ -4739,12 +4769,26 @@ pub const Pardes = struct { return p; } + /// A new session, its tape written from here when PARDES_RECORD asks. pub fn init(gpa: std.mem.Allocator, opts: Options) !*Pardes { + tape.startFromEnv(); + const taped = tape.enter(null, .{ .init = opts }); + const p = initSession(gpa, opts) catch |err| { + tape.leave(null, taped); + return err; + }; + tape.leave(p, taped); + return p; + } + + fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes { // A new session draws a tab at the default until `Tab` says otherwise. config.tab_now = config.tab_width; const p = try create(gpa, opts); errdefer p.deinit(); p.boot = Boot.of(opts); + // Asked once, at the start, whoever asks first after. + _ = p.sessionDir(); switch (p.boot) { .document => { const path = opts.file.?; @@ -4877,7 +4921,9 @@ pub const Pardes = struct { /// The store read once at start: a new session's, Recent from it. pub fn loadRecent(p: *Pardes) void { - p.store = Store.init(p.opts.store_path); + // A replica's store is its own, in memory: the recording session's + // file is every window's of its user (src/tape.zig). + p.store = Store.init(if (tape.replaying()) null else p.opts.store_path); p.store.load(p); } @@ -4941,6 +4987,9 @@ pub const Pardes = struct { } pub fn deinit(p: *Pardes) void { + const taped = tape.enter(p, .{ .deinit = tape.current(p) }); + tape.forget(p); + defer tape.leave(null, taped); if (comptime hosted) if (std.c.getenv("PARDES_FRAMES")) |path| p.frames.dump(std.mem.span(path)); // Every file open now is closed with its dot, and the list kept. for (p.panes) |slot| if (slot) |pane| p.noteClosed(pane); @@ -5001,6 +5050,8 @@ pub const Pardes = struct { /// shell feedback after write_dump: remember where the dump landed (the /// topbar grows a `Restore ` word pointing at it) pub fn setLastDump(p: *Pardes, path: []const u8) void { + const taped = tape.enter(p, .{ .set_last_dump = path }); + defer tape.leave(p, taped); p.pointRestoreAt(path); ctlfs.events.notePath(p, "dump", path); } @@ -5028,6 +5079,8 @@ pub const Pardes = struct { /// the shell polls this each frame: a pending Restore's dump path, or null pub fn takeRestore(p: *Pardes) ?[]const u8 { + const taped = tape.enter(p, .take_restore); + defer tape.leave(p, taped); const r = p.restore_req; p.restore_req = null; return r; @@ -5386,6 +5439,8 @@ pub const Pardes = struct { /// The shell reports the spawned pane's working directory (and later cwd /// changes it observes, e.g. via /proc//cwd before each frame). pub fn setCwd(p: *Pardes, id: usize, cwd: []const u8) void { + const taped = tape.enter(p, .{ .set_cwd = .{ .id = id, .cwd = cwd } }); + defer tape.leave(p, taped); const pane = p.panes[id] orelse return; // A command pane stays the directory's it was run for (newCommand). if (pane.command != null) return; @@ -5415,6 +5470,8 @@ pub const Pardes = struct { } pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_marks: bool) void { + const taped = tape.enter(p, .{ .acknowledge_shell = .{ .id = id, .executable = executable, .prompt_marks = prompt_marks } }); + defer tape.leave(p, taped); if (id < MAX_PANES) if (p.panes[id]) |pane| { ctlfs.pty.shellGone(p, pane, true); // a respawn replaced whatever ran pane.shell_failed = false; @@ -5511,10 +5568,14 @@ pub const Pardes = struct { /// file reloading under it, a theme file rewritten on disk. `pump` draws /// only when it has a reason to, and this is the reason. pub fn invalidate(p: *Pardes) void { + const taped = tape.enter(p, .invalidate); + defer tape.leave(p, taped); p.needs_frame = true; } pub fn reloadWatchedFile(p: *Pardes, id: u8, bytes: []const u8) bool { + const taped = tape.enter(p, .{ .reload_watched_file = .{ .id = id, .bytes = bytes } }); + defer tape.leave(p, taped); p.invalidate(); look.invalidateLookHover(p, id); const applied = p.applyWatchedFileChanged(id, bytes); @@ -5531,6 +5592,8 @@ pub const Pardes = struct { } pub fn nextEffect(p: *Pardes) ?Effect { + const taped = tape.enter(p, .next_effect); + defer tape.leave(p, taped); p.refillPendingWrites(); if (p.effects_len == 0) { p.effects_head = 0; @@ -5667,6 +5730,8 @@ pub const Pardes = struct { /// on it, or written to it from now on, hears `error shell gone` /// rather than wait for a prompt that never comes. pub fn shellFailed(p: *Pardes, id: u8, err: anyerror) void { + const taped = tape.enter(p, .{ .shell_failed = .{ .id = id, .err = err } }); + defer tape.leave(p, taped); const pane = p.panes[id] orelse return; pane.shell_failed = true; ctlfs.pty.shellGone(p, pane, false); @@ -5688,6 +5753,8 @@ pub const Pardes = struct { /// A shell started again in a terminal whose shell runs (pty/ctl's /// exec) did not start: the host kept the running one, so only said. pub fn restartFailed(p: *Pardes, id: u8, err: anyerror) void { + const taped = tape.enter(p, .{ .restart_failed = .{ .id = id, .err = err } }); + defer tape.leave(p, taped); if (p.panes[id] == null) return; p.sayShellFailure(id, err); } @@ -5700,9 +5767,7 @@ pub const Pardes = struct { // The script is there; the program its `#!` names is not. var said: [320]u8 = undefined; var interp: [256]u8 = undefined; - var buf: [std.fs.max_path_bytes]u8 = undefined; - const Shell = if (comptime hosted) @import("host_io.zig").Shell else void; - const name: ?[]const u8 = if (comptime hosted) (if (Shell.find(pane.shell orelse p.shellBin(), &buf)) |f| Shell.missingInterpreter(f, &interp) else null) else null; + const name: ?[]const u8 = if (comptime hosted) tape.value(.interpreter, missingInterpreter, .{ pane.shell orelse p.shellBin(), &interp }) else null; p.reportFailure(id, if (name) |n| std.fmt.bufPrint(&said, "shell: interpreter {s} not found", .{n}) catch "shell: interpreter not found" else "shell: interpreter not found"); } else { p.reportError(id, "shell", err); @@ -5712,6 +5777,13 @@ pub const Pardes = struct { p.noteLateFailure(id); } + /// The program the shell's `#!` names that is not there, in `out`. + fn missingInterpreter(bin: []const u8, out: []u8) ?[]const u8 { + const Shell = @import("host_io.zig").Shell; + var buf: [std.fs.max_path_bytes]u8 = undefined; + return Shell.missingInterpreter(Shell.find(bin, &buf) orelse return null, out); + } + /// A Save makes the directories its file goes in, and any above them, /// first: one rule, for the pane's own name and `Save path` alike. A /// directory it cannot make leaves the write to fail and say why. @@ -5729,11 +5801,13 @@ pub const Pardes = struct { const local = if (comptime hosted) filesystem.localPath(dir) else null; const kind: ?std.Io.File.Kind = if (comptime hosted) (if (local) |l| exec.kindOf(l) else .directory) else .directory; const missing = err == error.FileNotFound or err == error.NotDir or kind != .directory; - const denied = missing and (if (comptime hosted) (if (local) |l| filesystem.deniedAbove(l) else false) else false); + const denied = missing and (if (comptime hosted) (if (local) |l| tape.value(.denied, filesystem.deniedAbove, .{l}) else false) else false); return .{ .dir = dir, .kind = kind, .missing = missing, .denied = denied }; } pub fn saveFailed(p: *Pardes, id: u8, path: []const u8, err: anyerror) void { + const taped = tape.enter(p, .{ .save_failed = .{ .id = id, .path = path, .err = err } }); + defer tape.leave(p, taped); if (p.panes[id]) |pane| if (pane.file) |*f| { // The `-%` spelling fs.zig already uses for "make this dirty". f.saved_revision = f.revision -% 1; @@ -5755,6 +5829,8 @@ pub const Pardes = struct { /// log, and the 9P write that asked for it fails with it, as a failed /// Save's does. pub fn dumpFailed(p: *Pardes, path: []const u8, err: anyerror) void { + const taped = tape.enter(p, .{ .dump_failed = .{ .path = path, .err = err } }); + defer tape.leave(p, taped); var what: [limits.host_path_cap + 8]u8 = undefined; const operation = std.fmt.bufPrint(&what, "Dump {s}", .{path}) catch "Dump"; // Into a DumpDir that is not there: said so, ENOENT, as a Save is; @@ -5797,6 +5873,8 @@ pub const Pardes = struct { } pub fn perform(p: *Pardes, e: Effect) void { + const taped = tape.enter(p, .{ .perform = e }); + defer tape.leave(p, taped); // A Save performed on one twin is every twin's (File.syncTwins). defer panes.File.syncTwins(p); const v = p.host.vtable; @@ -5927,16 +6005,23 @@ pub const Pardes = struct { } } - pub fn pump(p: *Pardes, h: Host) !void { - p.host = h; + pub fn pump(p: *Pardes, host: Host) !void { + p.host = host; + const taped = tape.enter(p, .pump); + defer tape.leave(p, taped); + // The host as the tape sees it, while it records. + const h = p.host; const v = h.vtable; if (v.wait_input) |f| { // Asleep until input or until core animation next needs a // frame; 0 is no timeout at all. // A render-time animation draws every loop: the wait only takes // what input is there. + // The clock read whether or not it is wanted: what a replica + // waits on is its own (a picture decoding wakes only a frame + // that draws it), and the tape has the same calls either way. + const now = if (v.now) |clock| clock(h.ctx) else p.now_ns; const timeout: u32 = if (p.continuous()) 1 else if (p.nextWake()) |due| wake: { - const now = if (v.now) |clock| clock(h.ctx) else p.now_ns; const ms = ((due -| now) + std.time.ns_per_ms - 1) / std.time.ns_per_ms; break :wake @intCast(std.math.clamp(ms, 1, std.math.maxInt(u32))); } else 0; @@ -5972,6 +6057,7 @@ pub const Pardes = struct { const surface = try p.render(p.frame_arena.allocator()); p.frames.mark_render = FrameStats.nowNs(); if (v.present) |f| f(h.ctx, surface); + Stream.askAhead(p); // Settled before post_present: a shell ticks there, and what that // tick moves owes the next frame. const skipped = p.present_skipped; @@ -5990,6 +6076,8 @@ pub const Pardes = struct { /// that changes a pane costs a frame, which is why a round trip on a /// local socket costs microseconds and not a vsync. pub fn serveFs(p: *Pardes, req: ctlfs.Req) ctlfs.Reply { + const taped = tape.enter(p, .{ .serve_fs = req }); + defer tape.leave(p, taped); // The held write of an Edit whose commands run, asked again by a // retry of its park: still waiting, or its answer, never run twice. if (p.fs.edit_hold) |*h| if (h.names(req)) { @@ -6028,6 +6116,8 @@ pub const Pardes = struct { } pub fn update(p: *Pardes, ev: Event) void { + const taped = tape.enter(p, .{ .update = ev }); + defer tape.leave(p, taped); defer panes.File.syncTwins(p); defer p.reportFocus(); // A tick with nothing animating is the one event that cannot change @@ -6074,6 +6164,7 @@ pub const Pardes = struct { .tick => { Stream.step(p); Messages.advanceMessages(p); + for (p.panes) |slot| if (slot) |pane| if (pane.file) |*f| f.tables.step(pane.gpa, f); for (0..p.panes.len) |id| exec.flushReplEnter(p, id, false); }, else => {}, @@ -6661,10 +6752,16 @@ pub const Pardes = struct { pub fn sessionDir(p: *Pardes) []const u8 { if (p.session_dir_len) |n| return p.session_dir[0..n]; if (comptime !hosted) return "/"; - if (std.c.getcwd(&p.session_dir, p.session_dir.len) == null) return "/"; - const n: u16 = @intCast(std.mem.indexOfScalar(u8, &p.session_dir, 0) orelse return "/"); - p.session_dir_len = n; - return p.session_dir[0..n]; + // The directory the session that recorded was started in. + const dir = tape.value(.cwd, processDir, .{&p.session_dir}) orelse return "/"; + std.mem.copyForwards(u8, &p.session_dir, dir); + p.session_dir_len = @intCast(dir.len); + return p.session_dir[0..dir.len]; + } + + fn processDir(buf: *[limits.host_path_cap]u8) ?[]const u8 { + if (std.c.getcwd(buf, buf.len) == null) return null; + return buf[0 .. std.mem.indexOfScalar(u8, buf, 0) orelse return null]; } /// The directory a word run from pane `id`'s text or tag runs in, or, @@ -7007,6 +7104,8 @@ pub const Pardes = struct { /// What was said of a failure just now on pane `id` is also the waiting /// 9P write's (9p_io, `fs.late_failure`). pub fn noteLateFailure(p: *Pardes, id: usize) void { + const taped = tape.enter(p, .{ .note_late_failure = id }); + defer tape.leave(p, taped); // Said by reportFailure just now: its words fitted whole, the // reason kept; else what the message row has. const said = if (p.fs.said_at == p.fs.failures and p.fs.said_len > 0) @@ -7182,9 +7281,11 @@ pub const Pardes = struct { // ---- dumb undo/redo: whole-state snapshots, one per edit op ---- pub fn acknowledgePanelPresentation(p: *Pardes, tracks: []const animation.Track) void { + const taped = tape.enter(p, .{ .acknowledge_panel_presentation = tracks }); + defer tape.leave(p, taped); p.presentation.acknowledge(p, tracks); if (p.pointer_inside and p.drag != .none) { - if (p.presentation.pointer(p.screen_w, p.screen_h, p.pointer_raw_col, p.pointer_raw_row)) |mapped| { + if (p.pointerCell(p.pointer_raw_col, p.pointer_raw_row)) |mapped| { p.hover_col = mapped.col; p.hover_row = mapped.row; mouse.dragUpdate(p, mapped.col, mapped.row, p.pointer_body_hit); @@ -7193,6 +7294,14 @@ pub const Pardes = struct { look.refreshLookHoverFromRaw(p); } + /// The cell of the session's grid a pointer at this cell of the frame + /// is over, seen through the frame (a pane sliding in): the recording + /// session's answer, whose frames these were (src/tape.zig). Null over + /// a pane on its way out. + pub fn pointerCell(p: *const Pardes, col: u16, row: u16) ?Presentation.CellPosition { + return tape.value(.pointer, Presentation.pointer, .{ &p.presentation, p.screen_w, p.screen_h, col, row }); + } + pub fn abandonPanelAnimations(p: *Pardes) void { p.presentation.cancel(); look.cancelLookHover(p); @@ -7588,6 +7697,10 @@ pub const Pardes = struct { state.reply_len = 0; }; const r = p.rects[id]; + // The session's part of the pane's view geometry (File.geometry), + // and its context declarations. + if (pane.file) |*f| panes.File.refreshContext(p, pane, f); + pane.view = .{ .wrap = p.settings.wrap, .layered = p.settings.tree_context_tag_style, .body_h = p.row_metrics.body_h, .tagline_h = p.row_metrics.tagline_h, .rect_h = r.h }; // The tag takes a row per line, leaving the body one row if it // had any; a folded pane shows its first line only. // Measured on the stack: this runs on every event, and not @@ -7661,6 +7774,9 @@ pub const Pardes = struct { // columns do (Dir.fit). if (pane.file) |file| if (file.listing != null and r.h > pane.tag_rows) panes.Dir.fit(p, pane, pane.cols); panes.terminal.releasePendingCommandIfReady(p, id, pane); + // Which bands a pane shows is the step's, as its geometry is: + // what is drawn under them, and where a click lands, read it. + Messages.collectNotices(p, pane, r, id); // The greeting `ls` of a new terminal in a directory is typed at // its shell's FIRST prompt, or never: typed later it would land // in whatever runs then, or in the middle of a line someone @@ -7675,6 +7791,9 @@ pub const Pardes = struct { }, }; } + // A header's caret kept in its view, as a pane's is: the step's, not + // the frame's (paintHeaders draws what this leaves). + tagline.revealHeader(p); } pub fn theme(p: *const Pardes) *const Theme { @@ -7780,6 +7899,8 @@ pub const Pardes = struct { /// the look-hover delay) is jumped to its end in one step and drawn /// once. Idle, time is not banked: the next animation starts from now. pub fn advance(p: *Pardes, now_ns: u64) void { + const taped = tape.enter(p, .{ .advance = now_ns }); + defer tape.leave(p, taped); // The first time a shell's clock is seen, core time starts there. const started = p.clock_started; p.clock_started = true; @@ -7787,7 +7908,10 @@ pub const Pardes = struct { if (!started) p.stepped_ns = now_ns; const frame = animation.frame_ns; var moving: u32 = 0; - while (p.nextWake()) |due| { + // The session's ticks are the recording session's: a picture still + // decoding or a fade still moving asks for frames the way a message + // waiting to go does, and a replica that draws nothing has none. + while (tape.value(.wake, nextWake, .{p})) |due| { if (due > now_ns) break; // Only waiting until `due`: its counters move there at once. const held = (due - p.stepped_ns) / frame; @@ -8136,8 +8260,12 @@ test "a prompt chip is a band with its caret, still on the grid, and only the ch // Inside it is chrome: mapped as body it would land on Sel row 0, which is // the TAG row, so a sweep there would highlight the tagline instead. + // The sweep closed the prompt, and its chip with it: open it again. pane.clearPointerSelection(0); pane.sel[0] = .{}; + p.startPrompt(pane, &pane.body, .{ .search = "/" }); + p.sync(); + try std.testing.expectEqual(left, pane.notices.left[0]); p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = r.x + r.w - 3, .row = row } }); p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = r.x + r.w - 2, .row = row } }); p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = r.x + r.w - 2, .row = row } }); @@ -8523,6 +8651,9 @@ test "Esc back into a file leaves its view where it was" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "src/memory.zig" }); defer p.deinit(); + // Rows are lines: the centring below counts lines, and a wrapped line + // above the cursor would push it out of the view it centres. + p.settings.wrap = false; p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell under the doc const shell = p.active; diff --git a/src/pdf.zig b/src/pdf.zig index 18037bb7..d3796d70 100644 --- a/src/pdf.zig +++ b/src/pdf.zig @@ -703,6 +703,9 @@ pub const Document = struct { handle: *c.pardes_pdf_document, pages: usize, + /// The open document is MuPDF's, made from the file's bytes (src/walk.zig). + pub const replica = .{ .derived = .{"handle"} }; + pub fn deinit(document: *Document) void { c.pardes_pdf_close(document.handle); document.* = undefined; diff --git a/src/pdf_view.zig b/src/pdf_view.zig index f6d26eac..4ddbf299 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -84,6 +84,7 @@ pub const TintKey = if (enabled) struct { pub const Highlight = if (enabled) pdf.Highlight else void; pub const Highlights = if (enabled) struct { const Page = struct { page: usize, items: []const Highlight }; + pub const replica = .{ .len = .{.{ "pages", "len" }} }; pages: [3]Page = undefined, len: usize = 0, /// Repaint whenever the set changes, not only when a page's raster was @@ -619,6 +620,9 @@ pub const SectionsOutput = if (enabled) struct { pub const SelectionUpdate = enum { failed, stationary, unchanged, changed }; pub const State = if (enabled) struct { + /// What the replay digest leaves out (src/walk.zig): the pages' pixels, + /// rendered from the document, and the lanes rendering them. + pub const replica = .{ .derived = .{ "rasters", "rasters_len" }, .local = .{"lanes"} }; path: []u8, document: Document, page: usize = 0, diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index 73bac520..37cf22d0 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -184,6 +184,7 @@ pub const Tasks = struct { future: std.Io.Future(void), }; + pub const replica = .{ .len = .{.{ "items", "len" }} }; items: [capacity]Task = undefined, len: usize = 0, diff --git a/src/tape.zig b/src/tape.zig new file mode 100644 index 00000000..eaf1a9fa --- /dev/null +++ b/src/tape.zig @@ -0,0 +1,741 @@ +//! The recorder and its tape (docs/next-daemon.md, phase 1). Recording, +//! everything the core consumes is appended in the order it consumed it, +//! which is the turn's order (a 9P request served while a step is out in a +//! syscall lands inside that syscall's record): +//! +//! - each call the host makes into the core (`Input`), with its arguments; +//! - each call the core makes out into the host -- a Host function, or a +//! syscall of its own (`Site`) -- and what it returned; +//! - each field the host wrote in the core between calls (`watched`); +//! - after every call the host made at the top, the session's digest. +//! +//! Replaying (`PARDES_REPLAY= pardes`, the same build), a core takes +//! its inputs off the tape instead: a host call answers what it answered, a +//! site returns what it returned, nothing is performed and no syscall is +//! made, and each digest is checked against the recorded one -- by two +//! replicas at different addresses and a third that never draws. +//! +//! The local path pays a null check a call: `on` is null unless +//! PARDES_RECORD names where to write, `.tape`. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const walk = @import("walk.zig"); +const Pardes = pardes.Pardes; +const VTable = pardes.Host.VTable; +const libc = std.c; + +pub var on: ?*Tape = null; + +/// Every call into the core a host makes (a 9P connection task's too). +pub const Input = union(enum) { + init: pardes.Options, + init_from_dump: struct { opts: pardes.Options, bytes: []const u8 }, + restore: struct { bytes: []const u8, from: []const u8 }, + /// Whether it was the session's own core, or one a Restore replaced. + deinit: bool, + update: pardes.Event, + serve_fs: pardes.ctlfs.Req, + pump, + perform: pardes.Effect, + next_effect, + advance: u64, + render, + take_restore, + set_cwd: struct { id: usize, cwd: []const u8 }, + acknowledge_shell: struct { id: usize, executable: []const u8, prompt_marks: bool }, + shell_failed: struct { id: u8, err: anyerror }, + restart_failed: struct { id: u8, err: anyerror }, + save_failed: struct { id: u8, path: []const u8, err: anyerror }, + dump_failed: struct { path: []const u8, err: anyerror }, + set_last_dump: []const u8, + set_message: struct { id: usize, text: []const u8 }, + set_status: struct { id: usize, text: []const u8 }, + report_error: struct { id: usize, operation: []const u8, err: anyerror }, + reload_watched_file: struct { id: u8, bytes: []const u8 }, + note_late_failure: usize, + invalidate, + acknowledge_panel_presentation: []const pardes.animation.Track, + run_closed_lines, + file_deleted: u8, +}; + +/// The core's own syscalls (each `turn.yield()`s around itself), numbered +/// after the Host functions on the tape. +pub const Site = enum(u16) { + // Out of the turn (`site`). + os_stat, + os_listing, + os_rw, + make_dirs, + resolve, + read_mount, + read_file, + dir_listing, + kind, + probe, + preview, + // Read in place (`value`). + clock, + budget, + wake, + env, + interpreter, + denied, + shell, + mtime, + cwd, + pointer, +}; + +/// Fields a host writes straight into the core between its calls. +const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active" }; + +const Kind = enum(u8) { input, done, host, ret, set, digest }; +const host_fns = @typeInfo(VTable).@"struct".field_names; +/// The `set` that says which Host functions the recording host had. +const set_mask = 0xff; + +pub const Tape = struct { + gpa: std.mem.Allocator, + replay: bool, + /// The core whose digest is taken (a Restore replaces it). + core: ?*Pardes = null, + /// The host has the turn: a call into the core now is an input, and a + /// call out of it is the host's to answer. + host_side: bool = true, + depth: u32 = 0, + vtable: VTable = .{}, + // Recording. + fd: c_int = -1, + buf: std.ArrayList(u8) = .empty, + scratch: std.Io.Writer.Allocating, + real: pardes.Host = .{}, + seen: [watched.len]u64 = @splat(0), + // Replaying. + in: walk.Reader = .{ .bytes = &.{} }, + arena: std.heap.ArenaAllocator, + last_ret: walk.Reader = .{ .bytes = &.{} }, + mask: u64 = 0, + headless: bool = false, + digests: u64 = 0, + diverged: ?[]const u8 = null, + /// The input being applied, for a divergence's report. + applying: []const u8 = "", + /// PARDES_RECORD_EVERY: a digest after every input, a nested one's too + /// (to find the step a divergence starts at), not only the host's last. + every: bool = false, + + fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { + t.scratch.clearRetainingCapacity(); + walk.encode(T, v, &t.scratch.writer) catch @panic("tape: out of memory"); + const payload = t.scratch.written(); + const len: u32 = @intCast(payload.len); + t.buf.append(t.gpa, @intFromEnum(kind)) catch @panic("tape: out of memory"); + t.buf.appendSlice(t.gpa, std.mem.asBytes(&len)) catch @panic("tape: out of memory"); + t.buf.appendSlice(t.gpa, payload) catch @panic("tape: out of memory"); + if (t.buf.items.len > 1 << 20) t.flush(); + } + + fn flush(t: *Tape) void { + var at: usize = 0; + while (at < t.buf.items.len) { + const n = libc.write(t.fd, t.buf.items[at..].ptr, t.buf.items.len - at); + if (n <= 0) break; + at += @intCast(n); + } + t.buf.clearRetainingCapacity(); + } + + /// What the host wrote between calls, said before the core goes on. + fn noteWrites(t: *Tape) void { + const p = t.core orelse return; + inline for (watched, 0..) |name, i| { + const now = walk.digest(@FieldType(Pardes, name), &@field(p, name)); + if (now != t.seen[i]) { + t.seen[i] = now; + const set: struct { u8, @FieldType(Pardes, name) } = .{ i, @field(p, name) }; + t.record(.set, @TypeOf(set), &set); + } + } + } + + fn seeFields(t: *Tape) void { + const p = t.core orelse return; + inline for (watched, 0..) |name, i| t.seen[i] = walk.digest(@FieldType(Pardes, name), &@field(p, name)); + } + + /// The host's functions go through the tape while it records, whoever + /// set `p.host` last. + fn wrap(t: *Tape, p: *Pardes) void { + if (p.host.vtable == &t.vtable) return; + // The same host given again (each pump is given its host): only + // the wrapper put back. + if (p.host.vtable == t.real.vtable and p.host.ctx == t.real.ctx) { + p.host = .{ .ctx = t, .vtable = &t.vtable }; + return; + } + t.real = p.host; + var mask: u64 = 0; + inline for (host_fns, 0..) |f, i| if (@field(t.real.vtable.*, f) != null) { + mask |= @as(u64, 1) << i; + }; + p.host = t.hostOf(mask); + const set: struct { u8, u64 } = .{ set_mask, mask }; + t.record(.set, @TypeOf(set), &set); + } + + /// The tape as a host with these functions. + fn hostOf(t: *Tape, mask: u64) pardes.Host { + t.mask = mask; + t.vtable = .{}; + inline for (host_fns, 0..) |f, i| if (mask & (@as(u64, 1) << i) != 0 or + (t.headless and comptime std.mem.eql(u8, f, "watched"))) + { + @field(t.vtable, f) = &Wrap(i).f; + }; + return .{ .ctx = t, .vtable = &t.vtable }; + } + + fn enterInput(t: *Tape, p: ?*Pardes, input: *const Input) Entered { + if (!t.host_side) return .{}; + t.host_side = false; + t.depth += 1; + if (t.replay) return .{ .outer = true }; + if (p) |core| { + if (t.core == null) t.core = core; + t.noteWrites(); + t.wrap(core); + } + t.record(.input, Input, input); + return .{ .outer = true }; + } + + fn leaveInput(t: *Tape, p: ?*Pardes) void { + t.depth -= 1; + t.host_side = true; + if (t.replay) return; + if (p) |core| if (t.core == null) { + t.core = core; + t.wrap(core); + }; + t.record(.done, void, &{}); + if (t.depth == 0 or t.every) if (t.core) |core| { + var parts: Parts = undefined; + const d = digestParts(core, &parts); + t.record(.digest, []const u64, &d); + }; + if (t.depth == 0) t.flush(); + t.seeFields(); + } + + // ---- replaying ---- + + const Record = struct { kind: Kind, payload: walk.Reader }; + + fn next(t: *Tape) ?Record { + const head = t.in.take(5) catch return null; + const len = std.mem.bytesToValue(u32, head[1..5]); + const payload = t.in.take(len) catch return null; + return .{ .kind = @enumFromInt(head[0]), .payload = .{ .bytes = payload } }; + } + + fn peek(t: *const Tape) ?Kind { + if (t.in.bytes.len - t.in.at < 5) return null; + return @enumFromInt(t.in.bytes[t.in.at]); + } + + fn diverge(t: *Tape, comptime why: []const u8, args: anytype) void { + if (t.diverged != null) return; + t.diverged = std.fmt.allocPrint(t.gpa, why ++ " (tape byte {d}, in {s})", args ++ .{ t.in.at, t.applying }) catch why; + } + + /// A host call the headless replica does not make (it draws no frame, + /// so presents none): what the host told the core inside it (what it + /// presented, `acknowledge_panel_presentation`) is applied all the + /// same, the host's own calls below it stepped over. + fn skipHost(t: *Tape) void { + while (t.diverged == null) { + var rec = t.next() orelse return; + switch (rec.kind) { + .ret => return, + .input => t.apply(&rec.payload), + .set => t.applySet(&rec.payload), + .host => t.skipHost(), + .digest => t.check(&rec.payload), + .done => return t.diverge("a host call's records are out of order", .{}), + } + } + } + + /// Over a call the replica does not make (the headless one's present), + /// what was applied inside it too. + fn skip(t: *Tape) void { + var depth: usize = 1; + while (depth > 0) { + const rec = t.next() orelse return; + switch (rec.kind) { + .host, .input => depth += 1, + .ret, .done => depth -= 1, + else => {}, + } + } + } + + /// The records of an input up to its end: what was applied inside it, + /// as it was; the headless replica steps over the host calls it did + /// not make. + fn finishInput(t: *Tape) void { + while (t.diverged == null) { + const kind = t.peek() orelse return t.diverge("the tape ends inside an input", .{}); + var rec = t.next().?; + switch (kind) { + .done => return, + .set => t.applySet(&rec.payload), + .host => if (t.headless) t.skipHost() else return t.diverge("the core did not make the host call {d} the tape has", .{std.mem.bytesToValue(u16, rec.payload.bytes[0..2])}), + .input => t.apply(&rec.payload), + .digest => t.check(&rec.payload), + .ret => return t.diverge("the core's input ended before the recorded one", .{}), + } + } + } + + /// The records inside a host call up to its return, whose result is + /// left in `last_ret`. + fn finishHost(t: *Tape) bool { + while (t.diverged == null) { + var rec = t.next() orelse { + t.diverge("the tape ends inside a host call", .{}); + return false; + }; + switch (rec.kind) { + .ret => { + t.last_ret = rec.payload; + return true; + }, + .input => t.apply(&rec.payload), + .set => t.applySet(&rec.payload), + .digest => t.check(&rec.payload), + .host, .done => { + t.diverge("a host call's records are out of order", .{}); + return false; + }, + } + } + return false; + } + + fn expectHost(t: *Tape, id: u16, comptime what: []const u8) bool { + if (t.diverged != null) return false; + const rec = t.next() orelse { + t.diverge("the core called " ++ what ++ " past the tape's end", .{}); + return false; + }; + if (rec.kind != .host or std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) != id) { + const other: u16 = if (rec.kind == .host) std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) else 0; + t.diverge("the core called " ++ what ++ " where the tape has a {t} ({d})", .{ rec.kind, other }); + return false; + } + const outer = !t.host_side; + t.host_side = true; + defer t.host_side = !outer; + return t.finishHost(); + } + + /// A recorded digest against the replica's own. + fn check(t: *Tape, r: *walk.Reader) void { + const want = walk.decode([]const u64, r, t.arena.allocator()) catch return; + const p = t.core orelse return; + var parts: Parts = undefined; + const got = digestParts(p, &parts); + if (!std.mem.eql(u64, got, want)) t.diverge("digest {d} differs in {s}", .{ t.digests, partsDiffer(got, want, t.arena.allocator()) }); + t.digests += 1; + } + + fn applySet(t: *Tape, r: *walk.Reader) void { + const which = (r.take(1) catch return)[0]; + if (which == set_mask) { + const host = t.hostOf(std.mem.bytesToValue(u64, r.take(8) catch return)); + if (t.core) |p| p.host = host; + return; + } + const p = t.core orelse return; + inline for (watched, 0..) |name, i| if (i == which) { + @field(p, name) = walk.decode(@FieldType(Pardes, name), r, t.arena.allocator()) catch return; + }; + } + + /// One input, applied to the replica as the host applied it. + fn apply(t: *Tape, r: *walk.Reader) void { + // A session's options are kept by its core (`opts`): theirs for good. + const Tag = std.meta.Tag(Input); + const tag = std.mem.bytesToValue(Tag, (r.take(@sizeOf(Tag)) catch return t.diverge("a short input", .{}))); + r.at -= @sizeOf(Tag); + const kept = tag == .init or tag == .init_from_dump or tag == .restore; + const input = walk.decode(Input, r, if (kept) t.gpa else t.arena.allocator()) catch return t.diverge("a short input", .{}); + const outer = t.applying; + t.applying = switch (input) { + .update => |ev| @tagName(ev), + else => @tagName(input), + }; + defer t.applying = outer; + t.host_side = true; + defer t.host_side = true; + switch (input) { + .init, .init_from_dump => { + var opts = switch (input) { + .init => |o| o, + .init_from_dump => |d| d.opts, + else => unreachable, + }; + opts.image_allocator = t.gpa; + opts.pdf_allocator = t.gpa; + opts.tree_sitter_allocator = t.gpa; + opts.frame_allocator = t.gpa; + t.core = switch (input) { + .init => Pardes.init(t.gpa, opts), + .init_from_dump => |d| pardes.dump.initFromDump(t.gpa, opts, d.bytes), + else => unreachable, + } catch |err| return t.diverge("init failed: {t}", .{err}); + t.core.?.host = t.hostOf(t.mask); + return t.finishInput(); + }, + else => {}, + } + const p = t.core orelse return t.diverge("an input before the core", .{}); + switch (input) { + .init, .init_from_dump => unreachable, + .restore => |rs| { + const nc = pardes.dump.restore(p, rs.bytes, rs.from) catch |err| return t.diverge("restore failed: {t}", .{err}); + t.core = nc; + nc.host = t.hostOf(t.mask); + }, + // The session is over (or the core a Restore replaced is + // freed): nothing done now is compared. + .deinit => |own| { + if (own) t.core = null; + return t.skip(); + }, + .update => |ev| p.update(ev), + .serve_fs => |req| _ = p.serveFs(req), + .pump => p.pump(t.hostOf(t.mask)) catch {}, + .perform => |e| p.perform(e), + .next_effect => _ = p.nextEffect(), + .advance => |now| p.advance(now), + .render => if (t.headless) return t.skip() else { + _ = p.frame_arena.reset(.retain_capacity); + _ = p.render(p.frame_arena.allocator()) catch {}; + }, + .take_restore => _ = p.takeRestore(), + .set_cwd => |a| p.setCwd(a.id, a.cwd), + .acknowledge_shell => |a| p.acknowledgeShell(a.id, a.executable, a.prompt_marks), + .shell_failed => |a| p.shellFailed(a.id, a.err), + .restart_failed => |a| p.restartFailed(a.id, a.err), + .save_failed => |a| p.saveFailed(a.id, a.path, a.err), + .dump_failed => |a| p.dumpFailed(a.path, a.err), + .set_last_dump => |path| p.setLastDump(path), + .set_message => |a| p.setMessage(a.id, a.text), + .set_status => |a| p.setStatus(a.id, a.text), + .report_error => |a| p.reportError(a.id, a.operation, a.err), + .reload_watched_file => |a| _ = p.reloadWatchedFile(a.id, a.bytes), + .note_late_failure => |id| p.noteLateFailure(id), + .invalidate => p.invalidate(), + .acknowledge_panel_presentation => |tracks| p.acknowledgePanelPresentation(tracks), + .run_closed_lines => pardes.ctlfs.runClosedLines(p), + .file_deleted => |id| pardes.panes.File.deleted(p, id), + } + t.finishInput(); + } +}; + +const pardes_fields = walk.fieldNames(Pardes); +const stream_fields = walk.fieldNames(pardes.Stream); +const pane_fields = walk.fieldNames(pardes.Pane); +const file_fields = walk.fieldNames(pardes.panes.File.State); +const places_fields = walk.fieldNames(pardes.panes.Places); +const Parts = [pardes_fields.len + stream_fields.len + pardes.MAX_PANES * (2 + pane_fields.len + file_fields.len + places_fields.len)]u64; + +/// The session's digest as one per field of the core's, its stream's, and +/// each pane's and its file's, so a divergence says where it is. +fn digestParts(p: *const Pardes, parts: *Parts) []const u64 { + var n = walk.fieldDigests(Pardes, p, parts[0..pardes_fields.len]).len; + n += walk.fieldDigests(pardes.Stream, &p.stream, parts[n..][0..stream_fields.len]).len; + for (p.panes, 0..) |slot, id| { + const pane = slot orelse continue; + parts[n] = id; + parts[n + 1] = @intFromBool(pane.file != null); + n += 2; + n += walk.fieldDigests(pardes.Pane, pane, parts[n..][0..pane_fields.len]).len; + if (pane.file) |*f| n += walk.fieldDigests(pardes.panes.File.State, f, parts[n..][0..file_fields.len]).len; + n += walk.fieldDigests(pardes.panes.Places, &pane.places, parts[n..][0..places_fields.len]).len; + } + return parts[0..n]; +} + +/// The fields whose digests differ, named. +fn partsDiffer(got: []const u64, want: []const u64, a: std.mem.Allocator) []const u8 { + var said: std.ArrayList(u8) = .empty; + if (got.len != want.len) said.print(a, "the panes there are ({d} parts, {d} recorded); ", .{ got.len, want.len }) catch {}; + const n = @min(got.len, want.len); + var i: usize = 0; + var count: usize = 0; + while (i < pardes_fields.len + stream_fields.len and i < n) : (i += 1) { + if (got[i] == want[i]) continue; + const name = if (i < pardes_fields.len) pardes_fields[i] else stream_fields[i - pardes_fields.len]; + said.print(a, "{s}{s} ", .{ if (i < pardes_fields.len) "" else "stream.", name }) catch {}; + count += 1; + } + while (i + 2 <= n and count < 16) { + const id = got[i]; + const has_file = got[i + 1] != 0; + i += 2; + for (pane_fields) |name| { + if (i >= n) break; + if (got[i] != want[i]) { + said.print(a, "pane {d}.{s} ", .{ id, name }) catch {}; + count += 1; + } + i += 1; + } + if (has_file) for (file_fields) |name| { + if (i >= n) break; + if (got[i] != want[i]) { + said.print(a, "pane {d}.file.{s} ", .{ id, name }) catch {}; + count += 1; + } + i += 1; + }; + for (places_fields) |name| { + if (i >= n) break; + if (got[i] != want[i]) { + said.print(a, "pane {d}.places.{s} ", .{ id, name }) catch {}; + count += 1; + } + i += 1; + } + } + return said.items; +} + +/// A call into the core: the host's, and recorded, or the core's own. +pub const Entered = struct { outer: bool = false }; + +pub inline fn enter(p: ?*Pardes, input: Input) Entered { + const t = on orelse return .{}; + return t.enterInput(p, &input); +} + +pub inline fn leave(p: ?*Pardes, e: Entered) void { + if (e.outer) on.?.leaveInput(p); +} + +/// The core a Restore made is the session's from here. +pub fn adopt(p: *Pardes) void { + const t = on orelse return; + if (t.replay) return; + t.core = p; + t.wrap(p); +} + +/// Whether `p` is the core being recorded (a Restore's old one is not). +pub fn current(p: *const Pardes) bool { + const t = on orelse return true; + return t.core == null or t.core == p; +} + +/// The session's core is freed: no digest of it after. +pub fn forget(p: *Pardes) void { + const t = on orelse return; + if (t.core == p) t.core = null; +} + +/// The Host function `i`, through the tape. +fn Wrap(comptime i: usize) type { + const Fn = @typeInfo(@typeInfo(@FieldType(VTable, host_fns[i])).optional.child).pointer.child; + const info = @typeInfo(Fn).@"fn"; + const R = info.return_type.?; + const P = info.param_types; + return switch (P.len) { + 1 => struct { + fn f(ctx: ?*anyopaque) R { + return hostCall(i, R, ctx, .{}); + } + }, + 2 => struct { + fn f(ctx: ?*anyopaque, a: P[1].?) R { + return hostCall(i, R, ctx, .{a}); + } + }, + 3 => struct { + fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?) R { + return hostCall(i, R, ctx, .{ a, b }); + } + }, + 4 => struct { + fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?) R { + return hostCall(i, R, ctx, .{ a, b, c }); + } + }, + 5 => struct { + fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?, d: P[4].?) R { + return hostCall(i, R, ctx, .{ a, b, c, d }); + } + }, + else => @compileError("tape: a host function of more arguments"), + }; +} + +fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype) R { + const t: *Tape = @ptrCast(@alignCast(ctx.?)); + const name = host_fns[i]; + if (t.replay) { + // A replica that never draws is never watched: it asks no one. + if (comptime std.mem.eql(u8, name, "watched")) if (t.headless) return false; + if (!t.expectHost(i, name)) return std.mem.zeroes(R); + var ret = t.last_ret; + if (R == ?[]const u8) { + // fg_name's answer is written into the caller's buffer. + const text = (walk.decode(R, &ret, t.arena.allocator()) catch return null) orelse return null; + const buf = args[args.len - 1]; + const n = @min(text.len, buf.len); + @memcpy(buf[0..n], text[0..n]); + return buf[0..n]; + } + return walk.decode(R, &ret, t.arena.allocator()) catch std.mem.zeroes(R); + } + const real = @field(t.real.vtable.*, name).?; + const id: u16 = i; + t.record(.host, u16, &id); + t.seeFields(); + const outer = !t.host_side; + t.host_side = true; + const r = @call(.auto, real, .{t.real.ctx} ++ args); + t.host_side = !outer; + t.noteWrites(); + t.record(.ret, R, &r); + return r; +} + +/// A syscall of the core's own, `f(args)`, made out of the turn (a 9P +/// request through a mount of this very session is answered meanwhile): +/// recorded with what it returned, or answered from the tape and never +/// made. A result with slices is decoded into `a` on replay, the allocator +/// `f` would have used. +pub fn site(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + return call(s, a, f, args, true); +} + +/// A read of the host's that is no syscall out of the turn (the clock, the +/// environment) or yields for itself: as `site`. +pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + return call(s, null, f, args, false); +} + +fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; + const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); + const id: u16 = @as(u16, host_fns.len) + @intFromEnum(s); + if (t.replay) { + if (!t.expectHost(id, @tagName(s))) { + std.debug.print("replay: {s}\n", .{t.diverged orelse "?"}); + @panic("tape: the replay diverged at a host call of the core's own"); + } + var ret = t.last_ret; + return walk.decode(R, &ret, a orelse t.arena.allocator()) catch @panic("tape: a short result"); + } + // The host's own (a shell's spawn asking the shell), or one inside a + // site's syscall: the outer call is the record. + if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args); + t.record(.host, u16, &id); + t.seeFields(); + t.host_side = true; + const r = if (yields) out(f, args) else @call(.auto, f, args); + t.host_side = false; + t.noteWrites(); + t.record(.ret, R, &r); + return r; +} + +/// A budget of `budget_ns` from `since_ns` on the monotonic clock is spent: +/// what the clock said is taped, so a replica stops its work where this did. +pub fn over(since_ns: u64, budget_ns: u64) bool { + return value(.budget, overNow, .{ since_ns, budget_ns }); +} + +fn overNow(since_ns: u64, budget_ns: u64) bool { + return pardes.FrameStats.nowNs() -| since_ns > budget_ns; +} + +fn out(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + pardes.turn.yield(); + defer pardes.turn.back(); + return @call(.auto, f, args); +} + +/// Asked by `turn.yield`: a syscall a replica must never make. +pub fn replaying() bool { + return if (on) |t| t.replay else false; +} + +/// PARDES_RECORD set: the tape is written from the session's first call. +pub fn startFromEnv() void { + if (on != null) return; + const prefix = std.mem.span(libc.getenv("PARDES_RECORD") orelse return); + if (prefix.len == 0) return; + const gpa = std.heap.c_allocator; + var path_buf: [4096]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "{s}{d}.tape", .{ prefix, libc.getpid() }, 0) catch return; + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + const t = gpa.create(Tape) catch return; + t.* = .{ .gpa = gpa, .replay = false, .fd = fd, .scratch = .init(gpa), .arena = .init(gpa), .every = libc.getenv("PARDES_RECORD_EVERY") != null }; + on = t; +} + +/// `PARDES_REPLAY=`: the tape replayed into three replicas, each +/// digest checked; 0 when none diverged. +pub fn replayMain(io: std.Io, path: []const u8) u8 { + const gpa = std.heap.c_allocator; + // What a shell starts before its core (tty.zig's localSession). + pardes.image.start(io, gpa); + if (comptime pardes.pdf_enabled) pardes.pdf.start(gpa); + pardes.syntax.start(gpa); + const bytes = std.Io.Dir.cwd().readFileAlloc(std.Io.Threaded.global_single_threaded.io(), path, gpa, .unlimited) catch |err| { + std.debug.print("replay: cannot read {s}: {t}\n", .{ path, err }); + return 2; + }; + const runs = [_]struct { name: []const u8, headless: bool }{ + .{ .name = "first", .headless = false }, + // The first replica's memory is still held: this one's lies elsewhere. + .{ .name = "second", .headless = false }, + .{ .name = "headless", .headless = true }, + }; + var failed = false; + for (runs) |run| { + var t: Tape = .{ .gpa = gpa, .replay = true, .in = .{ .bytes = bytes }, .headless = run.headless, .scratch = .init(gpa), .arena = .init(gpa) }; + on = &t; + defer on = null; + while (t.diverged == null) { + const kind = t.peek() orelse break; + var rec = t.next().?; + switch (kind) { + .input => { + t.apply(&rec.payload); + _ = t.arena.reset(.retain_capacity); + }, + .set => t.applySet(&rec.payload), + .digest => t.check(&rec.payload), + .host, .ret, .done => t.diverge("a {t} record at the top", .{kind}), + } + } + // A session killed mid-step leaves its last input cut short: the + // end of the tape, not a divergence. + if (t.diverged) |why| if (std.mem.startsWith(u8, why, "the tape ends")) { + t.diverged = null; + }; + if (t.diverged) |why| { + failed = true; + std.debug.print("replay {s}: {s} replica diverged after {d} digests: {s}\n", .{ path, run.name, t.digests, why }); + } else std.debug.print("replay {s}: {s} replica: {d} digests agree\n", .{ path, run.name, t.digests }); + } + return if (failed) 1 else 0; +} diff --git a/src/terminal.zig b/src/terminal.zig index ce41bd95..0afbe43a 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -159,6 +159,30 @@ pub const State = struct { mouse_cell: if (enabled) ?ghostty_vt.Coordinate else void = if (enabled) null else {}, /// The kitty graphics its program placed, as drawn (TerminalImages.zig). images: TerminalImages = if (images_enabled) .{} else {}, + + /// The emulator as its program has made it, for the replay digest + /// (src/walk.zig): the active screen's text, how much history is above + /// it and where the view is, the cursor, the answers not yet written. + /// Not its pages (their addresses are its own), nor the history's text + /// (the screen's was all of it once, and a session's digest is taken + /// every step), nor the kitty images, whose media is read off the host: + /// a replica's terminal is the authority's screen state from phase 5 + /// on (docs/next-daemon.md). + pub fn digest(s: *const State, h: *pardes.walk.Hasher) void { + if (comptime !enabled) return; + const screen = s.vt.screens.active; + const sb = screen.pages.scrollbar(); + const cur = screen.cursor; + const facts = .{ sb.total, sb.offset, sb.len, cur.x, cur.y, s.vt.modes.get(.cursor_visible), s.vt.screens.active_key, s.reply_dropped }; + inline for (facts) |fact| h.update(std.mem.asBytes(&fact)); + h.update(s.reply[0..s.reply_len]); + var buf: [4096]u8 = undefined; + var text: std.Io.Writer.Hashing(pardes.walk.Hasher) = .initHasher(h.*, &buf); + if (screen.pages.getBottomRight(.active)) |br| + screen.dumpString(&text.writer, .{ .tl = screen.pages.getTopLeft(.active), .br = br, .unwrap = false }) catch {}; + text.writer.flush() catch {}; + h.* = text.hasher; + } }; const GColor = ghostty_vt.color; diff --git a/test/panes.zig b/test/panes.zig index 0379a03d..996accf1 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -587,6 +587,7 @@ const BodyHitTests = struct { pane.scrollBy(10); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); _ = try p.render(frame.allocator()); const layer = &p.surface.body_layers[0]; try std.testing.expect(layer.context_rows >= 4); @@ -635,6 +636,7 @@ const LineNumberLookTests = struct { pane.scrollBy(30); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); _ = try p.render(frame.allocator()); const layer = &p.surface.body_layers[0]; try std.testing.expect(layer.context_rows >= 2); @@ -693,6 +695,7 @@ const TreeContextTests = struct { pane.scrollBy(10); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); _ = try p.render(frame.allocator()); const layer = &p.surface.body_layers[0]; try std.testing.expectEqual(@as(u16, 4), layer.context_rows); @@ -747,9 +750,11 @@ const TreeContextTests = struct { var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); pane.scrollBy(7); + p.sync(); _ = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 0), pane.context_rows); try std.testing.expect(p.executeBuiltinLine(0, "TreeContext")); + p.sync(); _ = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 4), pane.context_rows); for (0..4) |row| try std.testing.expectEqual(@as(i32, @intCast(row)), pane.wrapAt(@intCast(row)).line); @@ -803,6 +808,7 @@ const TreeContextTests = struct { pane.hscroll = 4; var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); const surface = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 3), pane.context_rows); const rect = p.rects[0]; @@ -822,6 +828,7 @@ const TreeContextTests = struct { // builtin above put up, so the body moved with it. try std.testing.expectEqual(p.bodyTop(pane, p.rects[0]), cursor.y); panes.File.setContent(p, &pane.file.?, try p.gpa.dupe(u8, std.mem.asBytes(&@as([18][28]u8, @splat("// no enclosing declaration\n".*))))); + p.sync(); _ = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 0), pane.context_rows); try std.testing.expectEqual(pane.scroll(), pane.wrapAt(0).line); @@ -840,6 +847,7 @@ const TreeContextTests = struct { pane.scrollBy(2); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); const surface = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 1), pane.context_rows); try std.testing.expectEqual(@as(i32, 2), pane.wrapAt(1).line); @@ -2871,7 +2879,7 @@ const OutputTests = struct { if (w.line == 0 and w.pad > 0 and w.at < file.location_rows[0].location_end) break vr; } else return error.MissingWrappedLocation; const w = result.wrapAt(@intCast(wrapped)); - const shown = try panes.File.bodyText(frame.allocator(), result, file, true); + const shown = try panes.File.bodyText(frame.allocator(), result, file); const shown_line = modal.lineSlice(shown, wrapped); try std.testing.expect(std.mem.startsWith(u8, shown_line[panes.File.gutterWidth(result)..], "│ │ ")); const rect = p.rects[result_id]; @@ -4897,6 +4905,7 @@ test "Zig keywords keep every rendered byte colored across line-number widths" { p.settings.colors = true; p.settings.wrap = view.wrap; p.presentation.enabled = false; + p.sync(); var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); const surface = try p.render(frame.allocator()); diff --git a/test/snapshots/tagnav.golden b/test/snapshots/tagnav.golden index e7c6eca4..8b6752bd 100644 --- a/test/snapshots/tagnav.golden +++ b/test/snapshots/tagnav.golden @@ -37,9 +37,11 @@ == snap topbar-exec grid=100x31 cursor=7,4 |0: Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit Restore /tmp/pardes-sn |4: 1 alpha Dump ls -== snap topbar-restore-word grid=100x31 cursor=78,0 +== snap topbar-restore-word grid=100x31 cursor=60,0 +|0: d Grep Help Changelog Tutor Dump Themes Config Debug Exit Restore /tmp/pardes-snap/tagnav/dump.zon |4: 1 alpha ls == snap topbar-restored grid=100x31 cursor=7,4 +|0: Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit Restore /tmp/pardes-sn |4: 1 alpha $ ls |6: 3 gamma $ |7: /tmp/pardes-snap/tagnav/cwd Tty+bash Save Mode F ── restored history ── -- cgit v1.3 From 79efd33ba0964796d4dc4653b68435755b35f834 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 00:01:29 -0300 Subject: 9p: a mount keeps its connection and sends each operation's walk, stat, open and first read or write in one flight, a new connection's version and attach before them: an operation through it is one round trip where it was six or seven (a directory, or a file of no stated length, two: 9P's only end is an empty read); a file whose stat gave its length is read ahead, its Treads 14 at a time; reads are timed by their progress, so a large remote file opens at 80 and 207 ms (1.3 MB timed out); a write refused unopened behind a truncating open that parked is sent again after it; Unmount lets the connection go; the CLI, --wait and Handover dial afresh, pipelined; reference.typ says how a session elsewhere mounts through ssh -L Co-Authored-By: Claude Opus 5.5 --- docs/typ/reference.typ | 8 +- src/9p_io.zig | 684 +++++++++++++++++++++++++++++++++++++++++++------ src/CHANGELOG.md | 6 + src/builtins.zig | 2 +- src/fs.zig | 5 +- 5 files changed, 620 insertions(+), 85 deletions(-) (limited to 'src/fs.zig') diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index 2e395cc6..2d8658c6 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -338,7 +338,13 @@ nothing answers (`dial failed: no answer`, `timed out`, `hung up`); with no dial it is `wrong #args`, and any other `x!y` `bad dial address`, both EINVAL. There are eight named mounts; `os` and `self` are reserved. #word("Unmount") refuses a mount a pane, a working directory or a pending -#word("Save") still uses. Mounts are dumped. +#word("Save") still uses. Mounts are dumped. A mount keeps its connection, +and sends each walk, open and first read or write together: an operation +through it costs one round trip (a directory, or a file of no stated +length, one more to see its end). A session on another machine mounts +through a forwarded socket: `ssh -C -N -L /tmp/box.sock: host`, +then #word("Mount") `box /tmp/box.sock` (`-C`: a link far away carries +fewer bytes in each round trip). A session may open its own tree through a mount (a #word("Look") of `$m/pane/2/body` from the editor serving `$m`): requests are answered on the connection's diff --git a/src/9p_io.zig b/src/9p_io.zig index ef6385fc..c3af07d3 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -1980,6 +1980,87 @@ test "a change waits while the editor is out mid-step, a read does not, and the try testing.expectEqualStrings("after\n", pane.file.?.content); } +test "a mount's connection is kept between operations, and one the peer hung up is dialled again" { + if (comptime !supported) return error.SkipZigTest; + const gpa = testing.allocator; + var directory: [64:0]u8 = undefined; + _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-kept-XXXXXX", .{}, 0); + if (mkdtemp(&directory) == null) return error.TempDirectoryFailed; + defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)}); + const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; + defer { + if (old_runtime) |v| { + _ = setenv("XDG_RUNTIME_DIR", v, 1); + gpa.free(v); + } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); + } + try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); + + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("before\n"); + while (p.nextEffect()) |_| {} + const l = listen(testing.io, gpa, p, "kept", "", null, null) orelse return error.ListenFailed; + defer { + l.reset(p); + l.deinit(gpa); + } + var body_buf: [64]u8 = undefined; + const body = try std.fmt.bufPrint(&body_buf, "/pane/{d}/body", .{pane.serial}); + const long = try gpa.alloc(u8, 2 * msize + 5); + defer gpa.free(long); + @memset(long, 'k'); + long[long.len - 1] = '\n'; + + const Worker = struct { + l: *Listener, + body: []const u8, + long: []const u8, + done: std.atomic.Value(bool) = .init(false), + failure: ?anyerror = null, + fn run(w: *@This()) void { + defer w.done.store(true, .release); + w.check() catch |err| { + w.failure = err; + }; + } + fn read(w: *@This(), want: []const u8) !void { + const got = try Client.readMount(testing.allocator, w.l.path(), w.body, w.body, limits.max_file_bytes); + defer testing.allocator.free(got); + try testing.expectEqualStrings(want, got); + } + fn check(w: *@This()) !void { + defer Client.forget(w.l.path()); + try w.read("before\n"); + try w.read("before\n"); + // A write longer than a message, and a miss, on the same one. + try Client.writeMount(testing.allocator, w.l.path(), w.body, w.long); + try w.read(w.long); + try testing.expectError(error.NotFound, Client.readMount(testing.allocator, w.l.path(), "/pane/no-such", "/pane/no-such", 64)); + try testing.expectEqual(@as(u64, 1), w.l.accepts.load(.acquire)); + // The peer hangs up (a Restore cuts its clients): the next + // operation dials again and is answered. + for (&w.l.runner.conns) |*conn| if (conn.live()) conn.close(); + const by = Client.nowMs() + Client.budget_ms; + while (w.l.runner.count() != 0 and Client.nowMs() < by) Client.nap(1); + try w.read(w.long); + try testing.expectEqual(@as(u64, 2), w.l.accepts.load(.acquire)); + } + }; + var worker: Worker = .{ .l = l, .body = body, .long = long }; + pardes.turn.rest(); + const thread = try std.Thread.spawn(.{}, Worker.run, .{&worker}); + const deadline = Client.nowMs() + 3 * Client.budget_ms; + while (!worker.done.load(.acquire) and Client.nowMs() < deadline) Client.nap(1); + thread.join(); + // Forgotten: the kept connection is closed, and its task sees it. + const settle_by = Client.nowMs() + Client.budget_ms; + while (l.runner.count() != 0 and Client.nowMs() < settle_by) Client.nap(1); + pardes.turn.wake(); + if (worker.failure) |err| return err; + try testing.expectEqual(@as(usize, 0), l.runner.count()); +} + test "a connection holds up to 128 reads, and the next is refused in words" { try testing.expectEqual(@as(usize, 128), ninep.max_held); if (comptime supported) try testing.expectEqualStrings("too many reads waiting: 128", Runner.Engine.e_waiting); @@ -2318,11 +2399,20 @@ pub const Client = struct { } pub fn readLimit(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { + return readWith(false, gpa, dial, path, display_path, max_bytes); + } + + /// readLimit through a mount: its connection kept for the next (`kept`). + pub fn readMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { + return readWith(true, gpa, dial, path, display_path, max_bytes); + } + + fn readWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { if (comptime !supported) return error.Unsupported; var names: [max_depth][]const u8 = undefined; const n = try elements(path, &names); var remote: RemoteError = .{}; - return fetchBytes(gpa, dial, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes)) catch |err| { + return fetchBytes(gpa, dial, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes), keep) catch |err| { if (err == error.Remote) said = remote; return err; }; @@ -2338,7 +2428,16 @@ pub const Client = struct { /// Whether a peer answers at `dial`: a version and attach, and its root /// read. Any answer from the peer, an error of its own included, is one. pub fn probe(gpa: std.mem.Allocator, dial: []const u8) !void { - const got = readLimit(gpa, dial, "", "", 0) catch |err| switch (err) { + return probeWith(false, gpa, dial); + } + + /// probe for a Mount: the connection kept as the mount's. + pub fn probeMount(gpa: std.mem.Allocator, dial: []const u8) !void { + return probeWith(true, gpa, dial); + } + + fn probeWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8) !void { + const got = readWith(keep, gpa, dial, "", "", 0) catch |err| switch (err) { error.IsDirectory, error.NotFound, error.Remote, error.FileTooLarge => return, else => return err, }; @@ -2346,11 +2445,20 @@ pub const Client = struct { } pub fn write(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { + return writeWith(false, gpa, dial, path, bytes); + } + + /// write through a mount: its connection kept for the next (`kept`). + pub fn writeMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { + return writeWith(true, gpa, dial, path, bytes); + } + + fn writeWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { if (comptime !supported) return error.Unsupported; var names: [max_depth][]const u8 = undefined; const n = try elements(path, &names); var remote: RemoteError = .{}; - const result = fetchBytes(gpa, dial, names[0..n], &remote, bytes, path, limits.max_file_bytes) catch |err| { + const result = fetchBytes(gpa, dial, names[0..n], &remote, bytes, path, limits.max_file_bytes, keep) catch |err| { if (err == error.Remote) said = remote; return err; }; @@ -2362,9 +2470,13 @@ pub const Client = struct { len: usize = 0, fn set(r: *RemoteError, msg: []const u8) error{Remote} { + r.keep(msg); + return error.Remote; + } + + fn keep(r: *RemoteError, msg: []const u8) void { r.len = @min(msg.len, r.buf.len); @memcpy(r.buf[0..r.len], msg[0..r.len]); - return error.Remote; } }; @@ -2418,6 +2530,18 @@ pub const Client = struct { in: [msize]u8 = undefined, out: [msize]u8 = undefined, stage: [msize]u8 = undefined, + /// Version and attach answered: fid 0 is the peer's root, and each + /// operation walks a fid of its own from it, so a kept connection + /// (`kept`) pays for neither again. + attached: bool = false, + root_qid: ninep.Qid = undefined, + next_fid: u32 = 1, + /// Tags whose answers nobody waits for (a clunk's): dropped by + /// whoever settles next. + unwaited: u32 = 0, + /// The dial a kept connection answers for. + dial_buf: [sun_path_len + 32]u8 = undefined, + dial_len: usize = 0, fn wait(s: *Session, events: i16) Error!void { while (true) { @@ -2470,7 +2594,13 @@ pub const Client = struct { while (true) { if (nowMs() >= s.deadline) return Error.Timeout; try s.flush(); - if (s.cl.take()) |done| return done; + if (s.cl.take()) |done| { + if (done.tag < 32 and s.unwaited & bit(done.tag) != 0) { + s.unwaited &= ~bit(done.tag); + continue; + } + return done; + } if (s.cl.dead) return Error.Botch; const room = s.cl.in.len - s.cl.in_len; if (room == 0) return Error.Botch; @@ -2508,39 +2638,290 @@ pub const Client = struct { } fn dropNoWait(s: *Session, fid: u32) void { - _ = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + const tag = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + s.unwaited |= bit(tag); s.flush() catch {}; } + + fn send(s: *Session, req: ninep.Client.Request) Error!u16 { + return s.cl.submit(req) catch Error.Botch; + } + + fn newFid(s: *Session) u32 { + const fid = s.next_fid; + s.next_fid = if (fid >= 1 << 20) 1 else fid + 1; + return fid; + } + + /// A new connection's Tversion and Tattach, sent with the first + /// operation's requests behind them in one flight (`land`). + fn greet(s: *Session, f: *Flight) Error!void { + if (s.attached) return; + _ = f.add(.version, try s.send(.{ .version = .{} })); + // ponytail: cloud9's client submits nothing while a Tversion is + // out, so its msize is taken as asked until the Rversion lands + // (`land` puts it back); only a pardes is dialled here, and every + // one offers ninep.msize. A Twrite in this flight stays under the + // least a listener offers (`flight_write`). The fix is cloud9's + // client taking requests while it versions. + s.cl.versioning = false; + s.cl.msize = s.cl.asked; + _ = f.add(.attach, try s.send(.{ .attach = .{ .fid = 0, .uname = uname } })); + } + + /// The walk to a new fid at `names` (two Twalks past 16 names, the + /// second from the fid to itself), from the root. + fn walkNew(s: *Session, f: *Flight, names: []const []const u8) Error!u32 { + const fid = s.newFid(); + const first = names[0..@min(names.len, ninep.max_welem)]; + f.add(.walk, try s.send(.{ .walk = .{ .fid = 0, .newfid = fid, .names = first } })).asked = @intCast(first.len); + if (names.len > first.len) + f.add(.walk, try s.send(.{ .walk = .{ .fid = fid, .newfid = fid, .names = names[first.len..] } })).asked = @intCast(names.len - first.len); + return fid; + } + + /// Sends the flight and takes every answer of it, a read's bytes + /// into `sink` as they come (each valid only until the next), so + /// that the connection is left with nothing out but its clunks. A + /// refused Tversion is Remote at once: nothing after it is answered. + fn land(s: *Session, f: *Flight, sink: anytype) Error!void { + if (f.get(.version) != null) s.cl.versioning = true; + while (f.open()) { + const done = try s.settle(); + const item = f.find(done.tag) orelse return Error.Botch; + item.done = true; + switch (done.result) { + .fail => |words| { + item.failed = true; + item.said.keep(words); + if (item.step == .version) return Error.Remote; + }, + .version => { + if (item.step != .version or s.cl.msize == 0) return Error.Botch; + }, + .attach => |qid| { + if (item.step != .attach) return Error.Botch; + s.root_qid = qid; + s.attached = true; + }, + .walk => |w| { + if (item.step != .walk) return Error.Botch; + item.n = w.nwqid; + if (w.nwqid > 0) item.qid = w.wqid[w.nwqid - 1]; + }, + .stat => |st| { + if (item.step != .stat) return Error.Botch; + item.n = st.length; + }, + .open => |o| { + if (item.step != .open) return Error.Botch; + item.qid = o.qid; + }, + .read => |data| { + if (item.step != .read) return Error.Botch; + item.n = data.len; + sink.take(item, data); + // A read is timed by its progress, as a long write + // is: each answer gives the rest the budget again, so + // a large file over a slow link is not cut off. + s.deadline = @max(s.deadline, nowMs() +| budget_ms); + }, + .write => |count| { + if (item.step != .write) return Error.Botch; + item.n = count; + }, + else => return Error.Botch, + } + } + } }; - /// Version, attach and a walk to `names`: the fid there and its qid. - fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !struct { fid: u32, qid: ninep.Qid } { - _ = try s.ask(.{ .version = .{} }, remote); - if (s.cl.msize == 0) return Error.Botch; + fn bit(tag: u16) u32 { + return @as(u32, 1) << @intCast(tag); + } + + /// The requests of one flight, each its own tag, sent back to back: a + /// pardes takes one connection's requests in order (cloud9's engine + /// starts a request once the one before it is answered or parked), so + /// a walk, the open of its fid and the first read or write need no + /// answer between them, and an operation costs one round trip. + const Flight = struct { + items: [ninep.Client.window]Item = undefined, + len: usize = 0, - const root: u32 = 0; - var here = (try s.ask(.{ .attach = .{ .fid = root, .uname = uname } }, remote)).attach; - var cur: u32 = root; - var next: u32 = 1; + const Step = enum { version, attach, walk, stat, open, read, write }; + const Item = struct { + step: Step, + tag: u16, + done: bool = false, + failed: bool = false, + said: RemoteError = .{}, + /// walk: names asked; read: its offset and count. + asked: u16 = 0, + off: u64 = 0, + count: u32 = 0, + /// walk: qids answered; stat: the length; read, write: bytes. + n: u64 = 0, + qid: ninep.Qid = undefined, + }; - var i: usize = 0; - while (i < names.len) { - const n = @min(ninep.max_welem, names.len - i); - const w = (try s.ask(.{ .walk = .{ - .fid = cur, - .newfid = next, - .names = names[i..][0..n], - } }, remote)).walk; - if (w.nwqid != n) return Error.NotFound; - here = w.wqid[n - 1]; - if (cur != root) s.drop(cur); - cur = next; - next = if (next == 1) 2 else 1; - i += n; - } - return .{ .fid = cur, .qid = here }; + fn add(f: *Flight, step: Step, tag: u16) *Item { + f.items[f.len] = .{ .step = step, .tag = tag }; + f.len += 1; + return &f.items[f.len - 1]; + } + + fn find(f: *Flight, tag: u16) ?*Item { + for (f.items[0..f.len]) |*item| if (item.tag == tag and !item.done) return item; + return null; + } + + fn get(f: *Flight, step: Step) ?*Item { + for (f.items[0..f.len]) |*item| if (item.step == step) return item; + return null; + } + + fn open(f: *const Flight) bool { + for (f.items[0..f.len]) |item| if (!item.done) return true; + return false; + } + + /// The first refusal in the order asked: a version's or an attach's + /// words, a walk short of its names, an open's refusal. + fn check(f: *Flight, remote: *RemoteError) Error!void { + for (f.items[0..f.len]) |*item| switch (item.step) { + .version, .attach, .open => if (item.failed) return remote.set(item.said.buf[0..item.said.len]), + .walk => { + if (item.failed) return remote.set(item.said.buf[0..item.said.len]); + if (item.n != item.asked) return Error.NotFound; + }, + .stat, .read, .write => {}, + }; + } + + /// The walked file's qid: the last walk's, or the root's for none. + fn qid(f: *Flight, root: ninep.Qid) ninep.Qid { + var at = root; + for (f.items[0..f.len]) |item| if (item.step == .walk and item.n > 0) { + at = item.qid; + }; + return at; + } + + /// Whether the walk made the fid, so that it is owed a clunk. + fn made(f: *Flight) bool { + const first = f.get(.walk) orelse return false; + return !first.failed and first.n == first.asked; + } + }; + + /// A write in a new connection's first flight is sent before the msize + /// is known: no larger than the least a pardes listener offers. + const flight_write = ninep.min_msize - 23; + + /// Reads ahead this many Treads at once (the window, less a clunk + /// still out and one spare). + const read_ahead = ninep.Client.window - 2; + + /// Version and attach on a new connection, and a walk to `names`, in + /// one flight: the fid there and its qid. + fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !struct { fid: u32, qid: ninep.Qid } { + var f: Flight = .{}; + try s.greet(&f); + const fid = try s.walkNew(&f, names); + try landChecked(s, &f, NoSink{}, remote); + f.check(remote) catch |err| { + if (f.made()) s.dropNoWait(fid); + return err; + }; + return .{ .fid = fid, .qid = f.qid(s.root_qid) }; + } + + /// `land`, saying a refused Tversion in its own words: nothing else of + /// that flight is answered. + fn landChecked(s: *Session, f: *Flight, sink: anytype, remote: *RemoteError) Error!void { + s.land(f, sink) catch |err| { + if (err == Error.Remote) try f.check(remote); + return err; + }; } + const NoSink = struct { + fn take(_: NoSink, _: *Flight.Item, _: []const u8) void {} + }; + + /// A read's bytes taken in order: a file's as they are, a directory's + /// entries as rows of names. A read ahead that came back out of order, + /// or past one that came back short, is not taken: what it held is + /// read again from where the taken bytes end. + const Taker = struct { + out: *std.Io.Writer.Allocating, + display_path: []const u8, + limit: usize, + /// The flight with the walk, whose answer says what was reached: + /// it lands before any read (a walk never parks). + flight: *Flight, + s: *const Session, + /// Bytes taken from the start; a directory's next offset. + end: u64 = 0, + /// The read that ended at `end` came back short, or empty (the end). + short: bool = false, + empty: bool = false, + /// What taking said (too large, a bad entry): the operation's + /// answer once every read out has landed. + err: ?Error = null, + + fn take(t: *Taker, item: *Flight.Item, data: []const u8) void { + if (t.err != null or item.off != t.end or t.short) return; + t.put(data) catch |err| { + t.err = err; + return; + }; + t.end += data.len; + t.short = data.len < item.count; + t.empty = data.len == 0; + } + + fn directory(t: *const Taker) bool { + return t.flight.qid(t.s.root_qid).type & ninep.qtdir != 0; + } + + fn maxBytes(t: *const Taker) u64 { + return @min(t.limit, @as(usize, if (t.directory()) limits.max_stream_bytes else limits.max_file_bytes)); + } + + fn wireLimit(t: *const Taker) u64 { + return if (t.directory()) limits.max_stream_bytes else t.maxBytes(); + } + + fn put(t: *Taker, data: []const u8) Error!void { + if (t.end + data.len > t.wireLimit()) return Error.FileTooLarge; + if (t.directory()) { + var pos: usize = 0; + while (pos < data.len) { + if (data.len - pos < 2) return Error.Botch; + const len: usize = 2 + @as(usize, std.mem.readInt(u16, data[pos..][0..2], .little)); + if (len > data.len - pos) return Error.Botch; + const entry = ninep.Stat.decode(data[pos..][0..len]) catch return Error.Botch; + const display_dir = std.mem.trimEnd(u8, t.display_path, "/"); + const row_len = display_dir.len + entry.name.len + 2 + @as(usize, @intFromBool(entry.qid.type & ninep.qtdir != 0)); + if (row_len > t.maxBytes() - t.out.written().len) return Error.FileTooLarge; + t.out.writer.print("{s}/{s}", .{ display_dir, entry.name }) catch return Error.Botch; + if (entry.qid.type & ninep.qtdir != 0) t.out.writer.writeByte('/') catch return Error.Botch; + t.out.writer.writeByte('\n') catch return Error.Botch; + pos += len; + } + } else t.out.writer.writeAll(data) catch return Error.Botch; + } + }; + + /// One operation in as few round trips as its answers allow: the walk, + /// a read's stat, the open and the first read or write go in one flight + /// (behind a new connection's version and attach). A file of the + /// length its stat said is whole when a read comes back short at that + /// length; a longer one is read ahead, the Treads for the rest sent at + /// once. Anything else (a directory, a file of no stated length) reads + /// on until a read comes back empty, the only end 9P has. fn transact( s: *Session, names: []const []const u8, @@ -2549,23 +2930,52 @@ pub const Client = struct { write_bytes: ?[]const u8, read_limit: usize, ) !void { - const at = try walkTo(s, names, remote); - const cur = at.fid; - const here = at.qid; - defer s.dropNoWait(cur); - - const directory = here.type & ninep.qtdir != 0; - - const mode: u8 = if (write_bytes != null) ninep.owrite else ninep.oread; - const truncate = write_bytes != null and names.len > 0 and + const writing = write_bytes != null; + const mode: u8 = if (writing) ninep.owrite else ninep.oread; + const truncate = writing and names.len > 0 and (std.mem.eql(u8, names[0], "os") or std.mem.eql(u8, names[names.len - 1], "body")); - _ = try s.ask(.{ .open = .{ .fid = cur, .mode = mode | if (truncate) ninep.otrunc else 0 } }, remote); + const fresh = !s.attached; + var f: Flight = .{}; + try s.greet(&f); + const fid = try s.walkNew(&f, names); + if (!writing) _ = f.add(.stat, try s.send(.{ .stat = .{ .fid = fid } })); + _ = f.add(.open, try s.send(.{ .open = .{ .fid = fid, .mode = mode | if (truncate) ninep.otrunc else 0 } })); + const chunk_max: usize = if (fresh) flight_write else s.cl.maxWrite(); + const first: []const u8 = if (write_bytes) |bytes| bytes[0..@min(bytes.len, chunk_max)] else ""; + if (writing and first.len != 0) { + // Out of the client's buffer first (it holds one msize): still + // sent without waiting. + try s.flush(); + _ = f.add(.write, try s.send(.{ .write = .{ .fid = fid, .offset = 0, .data = first } })); + } + // A read's count before the msize is known is the asked msize's. + if (!writing) { + const item = f.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = 0, .count = s.cl.maxRead() } })); + item.count = s.cl.maxRead(); + } + var taker: Taker = .{ .out = out, .display_path = s.display_path, .limit = read_limit, .flight = &f, .s = s }; + try landChecked(s, &f, &taker, remote); + defer if (f.made()) s.dropNoWait(fid); + try f.check(remote); + if (taker.err) |err| return err; + const directory = taker.directory(); + if (write_bytes) |bytes| { if (directory) return Error.IsDirectory; var written: usize = 0; + if (f.get(.write)) |w| { + // Refused unopened, the open parked (a truncating one while + // the editor is out): sent again below, after it. + if (w.failed and !std.mem.eql(u8, w.said.buf[0..w.said.len], ninep.fs.e_bad_use)) return remote.set(w.said.buf[0..w.said.len]); + if (!w.failed) { + if (w.n == 0 or w.n > first.len) return Error.Botch; + written = w.n; + } + } while (written < bytes.len) { + s.deadline = nowMs() +| write_budget_ms; const chunk = bytes[written..][0..@min(bytes.len - written, s.cl.maxWrite())]; - const count = (try s.ask(.{ .write = .{ .fid = cur, .offset = written, .data = chunk } }, remote)).write; + const count = (try s.ask(.{ .write = .{ .fid = fid, .offset = written, .data = chunk } }, remote)).write; if (count == 0 or count > chunk.len) return Error.Botch; written += count; // A long write is timed by its progress: each answer gives @@ -2573,36 +2983,36 @@ pub const Client = struct { // in through one open, whole, however long it takes. The // budget is a write's: a server putting in tens of MiB it // held (pane.zig batchFor) answers that one write late. - s.deadline = nowMs() +| write_budget_ms; } return; } - const max_bytes: u64 = @min(read_limit, @as(usize, if (directory) limits.max_stream_bytes else limits.max_file_bytes)); - const wire_limit: u64 = if (directory) limits.max_stream_bytes else max_bytes; - var off: u64 = 0; + if (f.get(.read)) |r| if (r.failed and !std.mem.eql(u8, r.said.buf[0..r.said.len], ninep.fs.e_bad_use)) + return remote.set(r.said.buf[0..r.said.len]); + const length: u64 = if (f.get(.stat)) |st| (if (st.failed or directory) 0 else st.n) else 0; + const step: u64 = s.cl.maxRead(); while (true) { - const want: u32 = @intCast(@min(@as(u64, s.cl.maxRead()), wire_limit + 1 - off)); - const data = (try s.ask(.{ .read = .{ .fid = cur, .offset = off, .count = want } }, remote)).read; - if (data.len == 0) return; - if (off + data.len > wire_limit) return Error.FileTooLarge; - if (directory) { - var pos: usize = 0; - while (pos < data.len) { - if (data.len - pos < 2) return Error.Botch; - const len: usize = 2 + @as(usize, std.mem.readInt(u16, data[pos..][0..2], .little)); - if (len > data.len - pos) return Error.Botch; - const entry = ninep.Stat.decode(data[pos..][0..len]) catch return Error.Botch; - const display_dir = std.mem.trimEnd(u8, s.display_path, "/"); - const row_len = display_dir.len + entry.name.len + 2 + @as(usize, @intFromBool(entry.qid.type & ninep.qtdir != 0)); - if (row_len > max_bytes - out.written().len) return Error.FileTooLarge; - try out.writer.print("{s}/{s}", .{ display_dir, entry.name }); - if (entry.qid.type & ninep.qtdir != 0) try out.writer.writeByte('/'); - try out.writer.writeByte('\n'); - pos += len; - } - } else try out.writer.writeAll(data); - off += data.len; + if (taker.empty) return; + if (taker.short and length != 0 and taker.end == length) return; + // The rest of a file of known length at once; a directory, a + // file of none, or the end to be seen, a read at a time. + // ponytail: a file of no stated length is not read ahead. + const want: u64 = if (directory or taker.short or length <= taker.end) 1 else @min(read_ahead, (length - taker.end) / step + 1); + var batch: Flight = .{}; + var off = taker.end; + for (0..want) |_| { + if (off > taker.wireLimit()) break; + const count: u32 = @intCast(@min(step, taker.wireLimit() + 1 - off)); + const item = batch.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = off, .count = count } })); + item.off = off; + item.count = count; + off += count; + } + taker.short = false; + try s.land(&batch, &taker); + if (taker.err) |err| return err; + for (batch.items[0..batch.len]) |item| if (item.failed and item.off == taker.end and !taker.short) + return remote.set(item.said.buf[0..item.said.len]); } } @@ -2614,24 +3024,58 @@ pub const Client = struct { write_bytes: ?[]const u8, display_path: []const u8, read_limit: usize, + keep: bool, ) ![]u8 { if (comptime !supported) return Error.Dial; - const s = try startSession(gpa, dial, display_path); - defer endSession(gpa, s); + if (keep) if (kept.take(dial)) |s| { + if (fetchOn(s, gpa, names, remote, write_bytes, read_limit, display_path, true)) |bytes| return bytes else |err| switch (err) { + // Let go of by the peer since (its hangup still on the way): + // a read is asked again on a new connection. A write is not, + // as it may have been done. + error.Hangup, error.Botch => if (write_bytes != null) return err, + else => return err, + } + }; + return fetchOn(try startSession(dial, display_path), gpa, names, remote, write_bytes, read_limit, display_path, keep); + } + + fn fetchOn( + s: *Session, + gpa: std.mem.Allocator, + names: []const []const u8, + remote: *RemoteError, + write_bytes: ?[]const u8, + read_limit: usize, + display_path: []const u8, + keep: bool, + ) ![]u8 { + s.deadline = nowMs() +| budget_ms; + s.display_path = display_path; + var clean = false; + defer endSession(s, keep and clean); var out: std.Io.Writer.Allocating = .init(gpa); errdefer out.deinit(); - try transact(s, names, &out, remote, write_bytes, read_limit); + transact(s, names, &out, remote, write_bytes, read_limit) catch |err| { + // Refused with every answer in: the connection is as good as before. + clean = switch (err) { + error.Remote, error.NotFound, error.IsDirectory, error.FileTooLarge => true, + else => false, + }; + return err; + }; + clean = true; return out.toOwnedSlice(); } - /// A connected session at `dial` whose requests have the usual deadline. - fn startSession(gpa: std.mem.Allocator, dial: []const u8, display_path: []const u8) !*Session { + /// A new connection at `dial` whose requests have the usual deadline. + fn startSession(dial: []const u8, display_path: []const u8) !*Session { var sock_buf: [sun_path_len]u8 = undefined; const sock = try resolve(&sock_buf, dial); const deadline = nowMs() +| budget_ms; - const s = try gpa.create(Session); + // Not the caller's allocator: a kept one outlives the call (an arena's). + const s = try std.heap.page_allocator.create(Session); s.* = .{ .fd = -1, .deadline = deadline, .display_path = display_path }; - errdefer endSession(gpa, s); + errdefer endSession(s, false); if (sock == .quic) { if (comptime quic_enabled) { s.quic = quic.Connection.dial(sock.quic) catch return Error.Dial; @@ -2639,14 +3083,91 @@ pub const Client = struct { } else return Error.QuicUnavailable; } else s.fd = try connect(sock, deadline); s.cl = .init(.{ .in = &s.in, .out = &s.out }); + if (dial.len <= s.dial_buf.len) { + @memcpy(s.dial_buf[0..dial.len], dial); + s.dial_len = dial.len; + } return s; } - fn endSession(gpa: std.mem.Allocator, s: *Session) void { + fn endSession(s: *Session, keep: bool) void { + if (keep and s.attached and s.dial_len != 0 and !(quic_enabled and s.quic != null) and kept.put(s)) return; if (quic_enabled and s.quic != null) { s.quic.?.deinit(); } else if (s.fd >= 0) _ = libc.close(s.fd); - gpa.destroy(s); + std.heap.page_allocator.destroy(s); + } + + /// Connections kept open between operations, a few dials' (a mount's, + /// as Plan 9's mount keeps its channel): each costs the peer one of its + /// `max_conns` while kept, so only mounts and the Mount probe keep one. + const kept = struct { + const max = 8; + var lock: std.atomic.Mutex = .unlocked; + var slots: [max]?*Session = @splat(null); + + fn hold() void { + while (!lock.tryLock()) std.atomic.spinLoopHint(); + } + + /// The session kept for `dial`, out of the table, if it is still + /// there: what came while it waited (a clunk's answer) taken, and + /// a hangup or anything else seen as its end. + fn take(dial: []const u8) ?*Session { + hold(); + const s = for (&slots) |*slot| { + const got = slot.* orelse continue; + if (std.mem.eql(u8, got.dial_buf[0..got.dial_len], dial)) { + slot.* = null; + break got; + } + } else null; + lock.unlock(); + const found = s orelse return null; + if (usable(found)) return found; + endSession(found, false); + return null; + } + + fn usable(s: *Session) bool { + while (true) { + var fds = [1]libc.pollfd{.{ .fd = s.fd, .events = poll_in, .revents = 0 }}; + if (libc.poll(&fds, 1, 0) < 0) return false; + if (fds[0].revents == 0) return true; + if (fds[0].revents & poll_in == 0) return false; + const room = s.cl.in.len - s.cl.in_len; + if (room == 0) return false; + const got = (transport.read(s.fd, s.stage[0..@min(room, s.stage.len)]) catch return false) orelse return true; + if (got == 0) return false; + _ = s.cl.push(s.stage[0..got]); + while (s.cl.take()) |done| { + if (done.tag >= 32 or s.unwaited & bit(done.tag) == 0) return false; + s.unwaited &= ~bit(done.tag); + } + if (s.cl.dead) return false; + } + } + + fn put(s: *Session) bool { + hold(); + defer lock.unlock(); + for (&slots) |*slot| if (slot.* == null) { + slot.* = s; + return true; + }; + return false; + } + + /// Closes what is kept for `dial` (an Unmount). + fn forget(dial: []const u8) void { + while (take(dial)) |s| endSession(s, false); + } + }; + + /// Lets go of a connection kept for `dial`: its mount is gone. + pub fn forget(dial: []const u8) void { + if (comptime !supported) return; + kept.forget(dial); } /// Writes `request`, then `more`, to `path` and reads the answer on the @@ -2660,8 +3181,8 @@ pub const Client = struct { const n = try elements(path, &names); var remote: RemoteError = .{}; errdefer said = remote; - const s = try startSession(gpa, dial, path); - defer endSession(gpa, s); + const s = try startSession(dial, path); + defer endSession(s, false); const fid = (try walkTo(s, names[0..n], &remote)).fid; _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); var written: usize = 0; @@ -2701,8 +3222,9 @@ pub const Client = struct { var names: [max_depth][]const u8 = undefined; const n = try elements(path, &names); var remote: RemoteError = .{}; - const s = try startSession(gpa, dial, path); - defer endSession(gpa, s); + _ = gpa; + const s = try startSession(dial, path); + defer endSession(s, false); const fid = (try walkTo(s, names[0..n], &remote)).fid; _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); _ = try s.ask(.{ .write = .{ .fid = fid, .offset = 0, .data = first } }, &remote); @@ -2778,7 +3300,7 @@ pub const Client = struct { const before = nowMs(); try testing.expectError( Error.Dial, - fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes), + fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes, false), ); try testing.expect(nowMs() - before < budget_ms); } @@ -2787,7 +3309,7 @@ pub const Client = struct { const transport_bytes = if (quic_enabled) @sizeOf(?quic.Connection) else 0; try testing.expectEqual(@as(usize, msize), @as(usize, (Session{ .fd = -1, .deadline = 0, .display_path = "" }).in.len)); try testing.expect(transport_bytes <= 64); - try testing.expect(@sizeOf(Session) <= 3 * msize + @sizeOf(ninep.Client) + 128 + transport_bytes); + try testing.expect(@sizeOf(Session) <= 3 * msize + @sizeOf(ninep.Client) + 128 + @sizeOf(@FieldType(Session, "dial_buf")) + transport_bytes); try testing.expect(@sizeOf(ninep.Client) <= 512); } diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 27337781..86cc8733 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +- A mount keeps its connection, and sends each walk, open and first + read or write together: an operation through it is one round trip + (two for a directory or a file of no stated length) where it was six + or seven, and a file of known length is read ahead. A large remote + file opens however slow the link (reads are timed by their progress; + 1.3 MB at 80 ms used to time out). docs/next-daemon.md, phase 6. - The daemon also listens on `pardes-replica.sock`, a stream socket `ssh -L` can forward, for the replicated clients to come (docs/next-daemon.md, phase 5): a hello names a session and the client's build, another diff --git a/src/builtins.zig b/src/builtins.zig index 1dc428da..239edeaf 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -806,7 +806,7 @@ pub const Mount = struct { }; // A mount that cannot be reached is said now, and taken back, not // found out by the first look through it. - const reached = pardes.tape.site(.probe, null, @import("9p_io.zig").Client.probe, .{ c.p.gpa, dial }); + const reached = pardes.tape.site(.probe, null, @import("9p_io.zig").Client.probeMount, .{ c.p.gpa, dial }); reached catch |err| { if (c.p.fs.mounts.pop()) |gone| { c.p.gpa.free(gone.name); diff --git a/src/fs.zig b/src/fs.zig index e4373bf0..4bbf09bb 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -921,7 +921,7 @@ pub fn readLimit(p: *pardes.Pardes, path: []const u8, max_bytes: usize) ![]u8 { if (!std.mem.eql(u8, name, mount.name)) continue; // A peer answers on its own time, and the peer may be this very // editor: the turn goes out with the request. - return pardes.tape.site(.read_mount, p.gpa, ninep_io.Client.readLimit, .{ p.gpa, mount.dial, remote_path, path, limit }); + return pardes.tape.site(.read_mount, p.gpa, ninep_io.Client.readMount, .{ p.gpa, mount.dial, remote_path, path, limit }); } return error.FileNotFound; } @@ -1009,7 +1009,7 @@ fn writeOut(p: *pardes.Pardes, dial: ?[]const u8, path: []const u8, bytes: []con defer p.gpa.free(copy); pardes.turn.rest(); defer pardes.turn.wake(); - if (dial) |d| return ninep_io.Client.write(p.gpa, d, path, copy); + if (dial) |d| return ninep_io.Client.writeMount(p.gpa, d, path, copy); return writeFile(path, copy); } @@ -1542,6 +1542,7 @@ pub fn unmount(p: *Pardes, name: []const u8) !void { (path.len == prefix.len or path[prefix.len] == '/')) return error.MountInUse; } const removed = p.fs.mounts.orderedRemove(index); + if (comptime pardes.hosted) ninep_io.Client.forget(removed.dial); p.gpa.free(removed.name); p.gpa.free(removed.dial); return; -- cgit v1.3 From f7a0b6d902f001cff0343e11263868c5356f33c6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 15:57:57 -0300 Subject: tape: an error crosses by its name, a stat as fs.OsStat, and the tape's shape is in wire.semantics: a replica of another build read its session's units as its own build's, so a window client of a terminal's session, or of the box's, read every error by a number its build gives another (451 of 505 names numbered otherwise in the Linux terminal's and window's builds, 244 between Linux and FreeBSD: pardes-gui replaying a terminal's tape panicked at `unexpected error code, found error.NonLocalPath`, and so did main's window client of a session on the box at its Look of a fifo) and a FreeBSD stat out of step (nlink 8 bytes there, 4 here); a name this build's set lacks is a divergence (error.NoSuchError), never another error; an anyerror (a host's report) reads back as one of tape.Failure's, a Debug build panicking at one outside it as it is taped; tape.shape (each taped type's fields, tags, enum names and numbers) in semantics refuses, in words, builds whose tapes differ (the stat taped as before: the Linux and FreeBSD builds hash to two), and a site whose result tape.results does not list is a compile error; walk's test of an error by its name Co-Authored-By: Claude Opus 5.5 --- src/detached/wire.zig | 14 ++++-- src/fs.zig | 16 ++++-- src/ninep/pty.zig | 2 +- src/tape.zig | 52 ++++++++++++++++++++ src/terminal.zig | 2 +- src/walk.zig | 132 ++++++++++++++++++++++++++++++++++++++++++++++---- 6 files changed, 199 insertions(+), 19 deletions(-) (limited to 'src/fs.zig') diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 119601fc..1c9e554e 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -29,9 +29,13 @@ const family = switch (config.platform) { /// What makes two cores step differently, hashed: a replica's hello carries /// its own, and one of another build is refused. The protocol, the release /// and commit, the platform and build options, every dependency's content -/// hash (grammars, ghostty, Unicode) and the walker codec's assumptions -/// (pointer width, byte order). Not the OS: a Linux client of a FreeBSD -/// session is the main case, and what differs there is the host's, taped. +/// hash (grammars, ghostty, Unicode), the walker codec's assumptions +/// (pointer width, byte order) and the tape's shape (tape.shape: what each +/// input and each answer of the host's is laid out as). Not the OS: a Linux +/// client of a FreeBSD session is the main case, and what differs there is +/// the host's, taped, in a shape every OS shares (an error by its name, a +/// stat as fs.OsStat). Nor the build mode: a Debug build steps as a +/// release one does (regexp.zig's one budget). /// Nor the terminal's build against the window's: their cores step alike, /// the GUI's own words by the view's frontend (builtins.zig `here`), so a /// window replicates a terminal's session and a terminal a window's. @@ -41,14 +45,14 @@ const family = switch (config.platform) { pub const semantics: u64 = blk: { @setEvalBranchQuota(1_000_000); break :blk std.hash.Wyhash.hash(0, std.fmt.comptimePrint( - "wire {d}\npardes {s} {s}\n{s} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}", + "wire {d}\npardes {s} {s}\n{s} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}\n{s}", .{ version, config.version, config.commit orelse "unstamped", family, config.syntax_highlighting, config.syntax_zig_grammar, config.syntax_minimal_grammars, config.syntax_full_grammars, config.mupdf, config.theme_animation, config.workspace_tag, config.embed_sources, config.esp32p4_cols, config.esp32p4_rows, @sizeOf(usize), - builtin.cpu.arch.endian(), config.deps, + builtin.cpu.arch.endian(), config.deps, pardes.tape.shape, }, )); }; diff --git a/src/fs.zig b/src/fs.zig index 4bbf09bb..733418da 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -171,8 +171,8 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { const path = osPath(p, req.node) orelse return Reply.fail(req.tag, E.NOENT); if (req.op == .release) return .{ .tag = req.tag }; const io = std.Io.Threaded.global_single_threaded.io(); - const stat = pardes.tape.site(.os_stat, null, statPath, .{ io, path, std.Io.Dir.StatFileOptions{} }) catch return Reply.fail(req.tag, E.NOENT); - const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 }; + const stat = pardes.tape.site(.os_stat, null, osStat, .{ io, path }) catch return Reply.fail(req.tag, E.NOENT); + const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = stat.mtime }; switch (req.op) { .getattr => return .{ .tag = req.tag, .attr = attr }, .open => { @@ -223,6 +223,16 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { } } +/// What /os keeps of a stat, as the tape carries it to a replica: the same +/// bytes on every OS, which std.Io.File.Stat is not (a FreeBSD one's +/// `nlink` and `permissions` are another width than a Linux one's). +pub const OsStat = struct { kind: std.Io.File.Kind, size: u64, mtime: u32 }; + +fn osStat(io: std.Io, path: []const u8) !OsStat { + const stat = try statPath(io, path, .{}); + return .{ .kind = stat.kind, .size = stat.size, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 }; +} + /// A directory's listing under /os, the entries from `off` until `size` /// bytes of them are staged, into `listing`: the syscalls of a readdir. fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * 1024]u8) error{ NotDir, Io }![]const u8 { @@ -249,7 +259,7 @@ fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * return listed.items; } -const OsReadWrite = union(enum) { reply: Reply, read: []const u8 }; +pub const OsReadWrite = union(enum) { reply: Reply, read: []const u8 }; /// The syscalls of an /os read, write or truncate: the reply, or the bytes /// read into `buf`. diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig index e68ad84f..53fc5c68 100644 --- a/src/ninep/pty.zig +++ b/src/ninep/pty.zig @@ -411,7 +411,7 @@ fn finish(p: *Pardes, slot: *Run, pane: *Pane, status_code: ?i32) void { answer(p, slot, "exit {s}", .{status}); } -const Printed = struct { text: []const u8, whole: bool }; +pub const Printed = struct { text: []const u8, whole: bool }; /// What pane `pane`'s command printed (terminal.commandOutput), in `arena`. fn printedBy(pane: *Pane, arena: std.mem.Allocator) ?Printed { diff --git a/src/tape.zig b/src/tape.zig index 9f1cde42..3c8dd765 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -20,6 +20,8 @@ const std = @import("std"); const pardes = @import("pardes.zig"); const walk = @import("walk.zig"); +const host_io = @import("host_io.zig"); +const terminal = @import("terminal.zig"); const Pardes = pardes.Pardes; const VTable = pardes.Host.VTable; const libc = std.c; @@ -107,6 +109,50 @@ pub const Site = enum(u16) { prompt, }; +/// What the sites return. wire.semantics hashes their shape with the +/// rest of the tape's (`shape`), so two builds whose tapes differ in it +/// refuse each other's replicas in words, and a site returning a type not +/// listed is a compile error (`call`). An error union is listed as +/// `anyerror!` its payload: an error goes by its name (walk.encode). +pub const results = .{ + void, bool, + u32, i64, + [8]u8, ?u64, + []const u8, ?[]const u8, + ?std.Io.File.Kind, ?pardes.Presentation.CellPosition, + ?pardes.filesystem.Resolved, pardes.filesystem.OsReadWrite, + anyerror!pardes.filesystem.OsStat, anyerror![]const u8, + anyerror![]u8, anyerror!void, + terminal.Told, ?pardes.ctlfs.pty.Printed, + @typeInfo(@TypeOf(terminal.greeting)).@"fn".return_type.?, +}; + +/// What a host reports to the core as failed (`shell_failed`, `save_failed`, +/// `dump_failed`, `report_error`), as a replica reads it back: an error is +/// taped by its name, and `anyerror` keeps no list of names to look one up +/// in. ponytail: kept by hand, the sets of what a host reports from; one +/// outside them is a Debug build's panic as it is taped (walk.encode) and a +/// release build's divergence (the replica joins again). No host, none. +pub const Failure = if (!pardes.hosted) error{} else ErrorOf(host_io.forkShell) || ErrorOf(pardes.filesystem.write) || + ErrorOf(pardes.filesystem.readRestore) || ErrorOf(pardes.dump.restore) || + @typeInfo(@TypeOf(pardes.config.User.dumpThemes(undefined, undefined, undefined, pardes.themes))).error_union.error_set || + ErrorOf(host_io.Lsp.snapshot) || + ErrorOf(host_io.Inbox.startReader) || std.Io.File.ReadStreamingError || + error{ NoDumpDirectory, ListenFailed, WorkersUnavailable, WorkersBusy }; + +fn ErrorOf(comptime f: anytype) type { + return @typeInfo(@typeInfo(@TypeOf(f)).@"fn".return_type.?).error_union.error_set; +} + +/// Every type the tape carries, as walk.shape says it: wire.semantics's. +pub const shape = blk: { + var types: []const type = &.{ Input, []const u64, struct { u8, u64 } }; + for (results) |R| types = types ++ .{R}; + for (host_fns) |f| types = types ++ .{@typeInfo(@typeInfo(@typeInfo(@FieldType(VTable, f)).optional.child).pointer.child).@"fn".return_type.?}; + for (watched) |name| types = types ++ .{@FieldType(Pardes, name)}; + break :blk walk.shape(types); +}; + /// Fields a host writes straight into the core between its calls. const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active", "unattended" }; @@ -769,6 +815,12 @@ pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@Ty fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; + comptime { + const Listed = if (@typeInfo(R) == .error_union) anyerror!@typeInfo(R).error_union.payload else R; + for (results) |T| { + if (T == Listed) break; + } else @compileError("tape: the site ." ++ @tagName(s) ++ " returns " ++ @typeName(R) ++ ", which tape.results does not list"); + } // No tape where there is no host to record or replay one. if (comptime !pardes.hosted) return if (yields) out(f, args) else @call(.auto, f, args); const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); diff --git a/src/terminal.zig b/src/terminal.zig index acff68f4..63ce4f36 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -1188,7 +1188,7 @@ fn pointerRowCount(pane: *const Pane) usize { /// erased it, whether the screen was cleared, whether its pages lived and /// whether a pin could not be made. Taped: a screen-state replica, which /// ingests nothing, is told the authority's. -const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool }; +pub const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool }; fn told(t: Told) Told { return t; diff --git a/src/walk.zig b/src/walk.zig index 1c5bbbfc..88f62736 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -388,9 +388,14 @@ fn hashMap(comptime T: type, v: *const T, h: *Hasher) void { h.update(std.mem.asBytes(&sum)); } -/// The value as bytes, the same walk: what the tape carries (src/tape.zig). -/// Only for the process that wrote it, or one of the same build: native -/// byte order, and an error as its number in this build. +/// The value as bytes, the same walk: what the tape carries (src/tape.zig), +/// to a replica of another build too (a window of a terminal's session, a +/// Linux client of a FreeBSD one). Native byte order, which every machine +/// pardes runs on shares, and an error by its name: its number is its +/// build's own (a Linux terminal's FileNotFound is a window's +/// NoDefaultValue). What a field is laid out as (a std.Io.File.Stat's +/// `nlink` is another width on FreeBSD) is the tape's shape, which +/// wire.semantics hashes (`shape`). pub fn encode(comptime T: type, v: *const T, w: *std.Io.Writer) std.Io.Writer.Error!void { switch (@typeInfo(T)) { .void, .null, .undefined => {}, @@ -424,18 +429,91 @@ pub fn encode(comptime T: type, v: *const T, w: *std.Io.Writer) std.Io.Writer.Er inline else => |*payload| try encode(@TypeOf(payload.*), payload, w), } }, - .error_set => try w.writeAll(std.mem.asBytes(&@as(u16, @intFromError(v.*)))), + .error_set => try encodeError(T, v.*, w), .error_union => |e| if (v.*) |*payload| { try w.writeByte(1); try encode(e.payload, payload, w); } else |err| { try w.writeByte(0); - try w.writeAll(std.mem.asBytes(&@as(u16, @intFromError(err)))); + try encodeError(e.error_set, err, w); }, else => @compileError("walk: cannot encode " ++ @typeName(T)), } } +/// What `encode` lays each of `types` out as, in words: each field, element +/// and tag where it goes, an enum's names and numbers, an error as an +/// error (it goes by its name). wire.semantics hashes the tape's: builds +/// whose tapes differ in shape, as a FreeBSD std.Io.File.Stat differs from +/// a Linux one, refuse each other's replicas in words, not read each other +/// out of step. +pub fn shape(comptime types: []const type) []const u8 { + comptime { + @setEvalBranchQuota(10_000_000); + var out: []const u8 = ""; + for (types) |T| out = out ++ shapeOf(T, 0) ++ "\n"; + return out; + } +} + +fn shapeOf(comptime T: type, comptime depth: u8) []const u8 { + comptime { + if (depth > 32) @compileError("walk: the tape's shape nests past 32 deep at " ++ @typeName(T) ++ ": a type that holds itself?"); + return switch (@typeInfo(T)) { + .void, .null, .undefined => "v", + .bool => "b", + .int => |i| std.fmt.comptimePrint("{t}{d}", .{ i.signedness, i.bits }), + .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}), + .@"enum" => |e| blk: { + var out: []const u8 = shapeOf(e.tag_type, depth + 1) ++ "{"; + for (e.field_names, e.field_values) |n, v| out = out ++ std.fmt.comptimePrint("{s}={d},", .{ n, v }); + break :blk out ++ "}"; + }, + .optional => |o| if (machinery(o.child)) "0" else "?" ++ shapeOf(o.child, depth + 1), + .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ shapeOf(a.child, depth + 1), + .pointer => |p| switch (p.size) { + .one => shapeOf(p.child, depth + 1), + .slice => "[]" ++ shapeOf(p.child, depth + 1), + else => @compileError("walk: a many-pointer has no length: " ++ @typeName(T)), + }, + .@"struct" => |s| blk: { + if (machinery(T)) break :blk ""; + var out: []const u8 = if (s.layout == .@"packed") "p{" else "{"; + for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime" or excluded(T, name)) continue; + out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ","; + } + break :blk out ++ "}"; + }, + .@"union" => |u| blk: { + var out: []const u8 = shapeOf(u.tag_type.?, depth + 1) ++ "("; + for (u.field_names, u.field_types) |name, F| out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ","; + break :blk out ++ ")"; + }, + .error_set => "error", + .error_union => |e| "!" ++ shapeOf(e.payload, depth + 1), + else => @compileError("walk: cannot encode " ++ @typeName(T)), + }; + } +} + +/// An error by its name. An `anyerror` must be one `decode` can read back +/// (`errorNamed`): a Debug build stops at one that is not, as it is taped. +fn encodeError(comptime E: type, err: E, w: *std.Io.Writer) std.Io.Writer.Error!void { + if (comptime E == anyerror and builtin.mode == .debug) if (errorNamed(E, @errorName(err)) == null) + std.debug.panic("walk: error.{s} is taped as an anyerror, which a replica reads back only as one of tape.Failure's: add the set it comes from", .{@errorName(err)}); + try writeName(w, @errorName(err)); +} + +/// The error of `E` called `name`. `anyerror` keeps no list of its names +/// to look one up in: one is read back as one of tape.Failure's, what a +/// host reports as failed (the tape's only anyerrors). +fn errorNamed(comptime E: type, name: []const u8) ?E { + const Named = if (E == anyerror) @import("tape.zig").Failure else E; + inline for (@typeInfo(Named).error_set.error_names.?) |n| if (std.mem.eql(u8, name, n)) return @field(Named, n); + return null; +} + pub const Reader = struct { bytes: []const u8, at: usize = 0, @@ -451,9 +529,13 @@ pub const Reader = struct { } }; +/// What an error read back is when this build has none of that name (an +/// OS's own, a newer build's): the tape is another build's, and diverges. +pub const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError }; + /// The inverse of `encode`; slices (and what a pointer points at) are made /// in `a`. A field `encode` left out is its default. -pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTape, OutOfMemory }!T { +pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T { switch (@typeInfo(T)) { .void, .null, .undefined => return {}, .bool => return (try r.take(1))[0] != 0, @@ -500,7 +582,7 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTa @field(out, name) = @as(*const F, @ptrCast(@alignCast(default))).*; } else if (comptime @typeInfo(F) == .error_set) { // No `!anyerror` to return it through: read in place. - @field(out, name) = @errorCast(@errorFromInt(try r.scalar(u16))); + try decodeError(F, r, &@field(out, name)); } else @field(out, name) = try decode(F, r, a); } return out; @@ -512,17 +594,24 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTa }; unreachable; }, - .error_set => return @errorCast(@errorFromInt(try r.scalar(u16))), .error_union => |e| { if ((try r.take(1))[0] != 0) return try decode(e.payload, r, a); // The value's error, not decode's own. - const err: e.error_set = @errorCast(@errorFromInt(try r.scalar(u16))); + var err: e.error_set = undefined; + try decodeError(e.error_set, r, &err); return @as(T, err); }, else => @compileError("walk: cannot decode " ++ @typeName(T)), } } +/// An error by its name, as `encode` wrote it, into `out`: no error union +/// holds an error set. +fn decodeError(comptime E: type, r: *Reader, out: *E) TapeError!void { + const len = try r.scalar(u16); + out.* = errorNamed(E, try r.take(len)) orelse return error.NoSuchError; +} + test "a value comes back from its bytes" { const E = union(enum) { key: struct { cp: u21, text: []const u8 }, tick, failed: anyerror!u32 }; var buf: [256]u8 = undefined; @@ -536,6 +625,31 @@ test "a value comes back from its bytes" { try std.testing.expectEqual(r.bytes.len, r.at); } +test "an error is taped by its name, which another build reads back as its own" { + // The build's number for an error is its own: 451 of 505 names were + // numbered otherwise in a Linux window's build than a terminal's, so a + // window replica read a terminal session's FileNotFound as another. + var buf: [64]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + const failed: error{ FileNotFound, NotDir }!u32 = error.NotDir; + try encode(@TypeOf(failed), &failed, &w); + try std.testing.expectEqualSlices(u8, "\x00\x06\x00NotDir", w.buffered()); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + var r: Reader = .{ .bytes = w.buffered() }; + try std.testing.expectError(error.NotDir, try decode(@TypeOf(failed), &r, arena.allocator())); + // A name this build's set lacks (an OS's own): the tape is another + // build's, and the replay diverges rather than read some other error. + r = .{ .bytes = "\x00\x0c\x00FileTooLarge" }; + try std.testing.expectError(error.NoSuchError, decode(@TypeOf(failed), &r, arena.allocator())); + // An anyerror (a host's report) is read back as one of tape.Failure's. + const Report = struct { err: anyerror }; + w = .fixed(&buf); + try encode(Report, &.{ .err = error.FileNotFound }, &w); + r = .{ .bytes = w.buffered() }; + try std.testing.expectEqual(error.FileNotFound, (try decode(Report, &r, arena.allocator())).err); +} + test "a digest is of values: the same text at two addresses is one digest" { const S = struct { name: []const u8, n: u32, next: ?*const u32 }; const a = "same text"; -- cgit v1.3