summaryrefslogtreecommitdiff
path: root/src/tape.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 11:14:38 -0300
committerGabriel Schneider <[email protected]>2026-10-29 15:57:34 -0300
commit044da0f75ab5fb5be0213e512741860d95dad8e5 (patch)
tree98c1dd8ddf2095ead2e783577124313b88eae9ef /src/tape.zig
parentc4f3749c058121ba086a851188691e7ae40d2018 (diff)
downloadpardes-044da0f75ab5fb5be0213e512741860d95dad8e5.tar.gz
pardes-044da0f75ab5fb5be0213e512741860d95dad8e5.zip
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=<prefix> names a tape (<prefix><pid>.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=<tape> 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 <[email protected]>
Diffstat (limited to 'src/tape.zig')
-rw-r--r--src/tape.zig741
1 files changed, 741 insertions, 0 deletions
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=<tape> 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, `<prefix><pid>.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=<tape>`: 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;
+}