summaryrefslogtreecommitdiff
path: root/src/tape.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
committerGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
commita479792a1ab2b593642d7f648194bfa23ce01dba (patch)
tree96ddee7d879a9084daa07db43b0b7910b072a785 /src/tape.zig
parente179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff)
parent4c761893e5b3fdae422622cfa02104ab36633045 (diff)
downloadpardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz
pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'src/tape.zig')
-rw-r--r--src/tape.zig1350
1 files changed, 1350 insertions, 0 deletions
diff --git a/src/tape.zig b/src/tape.zig
new file mode 100644
index 00000000..74576eed
--- /dev/null
+++ b/src/tape.zig
@@ -0,0 +1,1350 @@
+//! 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 host_io = @import("host_io.zig");
+const terminal = @import("terminal.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,
+ step,
+ 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 },
+ /// A dead session's shell taken in (Adopt), and a terminal's reader
+ /// that failed: what a host writes into the core, made inputs.
+ adopt_shell,
+ reader_failed: struct { id: u8, err: anyerror },
+ 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,
+ open_view,
+ close_view: u8,
+ leave_view: u8,
+ away_view: u8,
+ rejoin_view: u8,
+ to_view: u8,
+ /// What a window says of its font and its opacity (gui.zig).
+ take_font_request,
+ observe_font: Font,
+ acknowledge_font: Font,
+ reject_font,
+ disable_scene_effects,
+ window_opacity_done: struct { applied: u8, failure: ?[]const u8 },
+
+ pub const Font = struct { name: []const u8, size: u16, unit: pardes.config.Runtime.FontSizeUnit };
+};
+
+/// 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,
+ /// A search run on the loop (Stream.runHere: a 9P write's Grep or Find).
+ search,
+ // Read in place (`value`).
+ clock,
+ budget,
+ wake,
+ env,
+ interpreter,
+ denied,
+ shell,
+ mtime,
+ cwd,
+ pointer,
+ /// What a terminal's ingest told the positions pardes keeps over it.
+ positions,
+ /// Whether a terminal is at its prompt, as a step reads it after its
+ /// output (a greeting, a command waiting for the prompt), and what its
+ /// command printed (a pty/run's answer): a screen-state replica's
+ /// emulator is the last delta's until the next.
+ prompt,
+ /// What the authority alone holds and a step reads (`authority`).
+ authority,
+};
+
+/// 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.?,
+ []pardes.Stream.Batch,
+ // What only the authority holds (`authority`): a closed pane's record,
+ // Recent's entry for a closed jump, the closed panes' names.
+ ?@import("Store.zig").Record, ?@import("recent.zig").Entry,
+ []const @import("Store.zig").Named,
+};
+
+/// 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" };
+
+const Kind = enum(u8) { input, done, host, ret, set, digest };
+/// A digest record's kind byte (replica.zig reads a unit's last).
+pub const digest_kind: u8 = @backingInt(Kind.digest);
+pub const set_kind: u8 = @backingInt(Kind.set);
+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,
+ /// A replica applying its own input before the authority has ordered it
+ /// (src/replica.zig): recorded as the authority would record it, so the
+ /// two can be compared, but nothing is performed. A host call or a
+ /// syscall stops it (`poisoned`): what the call returns is the
+ /// authority's to say, and the input waits for it. A read of the clock
+ /// or the environment (`value`) is made here, a guess the comparison
+ /// checks.
+ speculating: bool = false,
+ poisoned: bool = false,
+ /// What stopped it: a host function's or a site's name, or a poison's
+ /// reason (PARDES_REPLICA_STATS counts them).
+ poisoned_by: []const u8 = "",
+ /// The authority's last yes or no to each question a replayed unit
+ /// asked it (`authority`), a speculation's guess.
+ answers: std.AutoHashMapUnmanaged(u64, bool) = .empty,
+ /// It read a terminal (its rows) or moved one's view: what it did
+ /// depends on the emulator, which deltas write (`touchTerminal`).
+ touched: bool = false,
+ /// The allocator of the core a replay makes (a replica's rollback epoch,
+ /// src/rollback.zig), when it is not the tape's own.
+ core_gpa: ?std.mem.Allocator = null,
+ /// Where a replay keeps what a core keeps of an input (its options),
+ /// for a tape that is let go; else the tape's allocator, for good.
+ kept_arena: ?std.heap.ArenaAllocator = null,
+ /// No digests recorded (a speculation's: a replica compares what it
+ /// was given and what it asked, from a state the authority shares).
+ no_digest: bool = false,
+ /// PARDES_REPLAY_DUMP (phase 2's gate): at the session's end, its dump
+ /// loaded must be it, digest for digest, and dump again byte for byte.
+ dump_check: bool = false,
+ dumps: usize = 0,
+ dump_bytes: usize = 0,
+ /// Screen-state terminals (src/screen_state.zig, docs/next-daemon.md
+ /// phase 5): an authority recording for replicas, or a replica. A
+ /// terminal's emulator is the authority's alone: its output is recorded
+ /// without its bytes (they cross as screen state), its pty writes are
+ /// not recorded, and no digest covers it. A replica's core never writes
+ /// one but by `apply`.
+ screen_state: bool = false,
+ /// A digest as one word (screen-state replicas: every unit of a pty's
+ /// output carries one, at the pty's read rate), not one a field; what
+ /// differs is then not named.
+ whole_digest: bool = false,
+ /// A digest only when asked (`recordDigest`): the replica stream's,
+ /// one a unit, not one after each of its inputs.
+ unit_digests: bool = false,
+ /// A replica client's guess of the session's clocks for a speculation
+ /// (src/replica.zig): its clock reads (a message's stamp, a file's
+ /// mtime) answer this instead of the clocks here.
+ guess: ?Guess = null,
+ /// The same guess, as the client sent it with its input, while the
+ /// authority steps that input: the step's clock reads are the input's
+ /// time, so the guess a speculation recorded is what the authority
+ /// records (a message's millisecond, an edit's second never miss).
+ stamp: ?Guess = null,
+
+ 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, @backingInt(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 {
+ if (comptime !pardes.hosted) return;
+ // A tape in memory (a replica's, an authority's stream) is taken
+ // by whoever holds it.
+ if (t.fd < 0) return;
+ 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);
+ }
+ // A terminal's output crosses as screen state, not as its bytes.
+ if (t.screen_state and input.* == .update and input.update == .output) {
+ var bare = input.*;
+ bare.update.output.bytes = &.{};
+ t.record(.input, Input, &bare);
+ } else 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) and !t.no_digest and !t.unit_digests) if (t.core) |core| {
+ var parts: Parts = undefined;
+ const whole = [1]u64{digestOf(core)};
+ const d: []const u64 = if (t.whole_digest) &whole else 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 = @fromBackingInt(@intCast(head[0])), .payload = .{ .bytes = payload } };
+ }
+
+ fn peek(t: *const Tape) ?Kind {
+ if (t.in.bytes.len - t.in.at < 5) return null;
+ return @fromBackingInt(@intCast(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 whole = [1]u64{digestOf(p)};
+ const got: []const u64 = if (want.len == 1) &whole else digestParts(p, &parts);
+ if (!std.mem.eql(u64, got, want)) t.diverge("digest {d} differs in {s}", .{ t.digests, if (want.len == 1) "the session (a digest of one word: run the authority with whole_digest off to name the field)" else partsDiffer(got, want, t.arena.allocator()) });
+ t.digests += 1;
+ }
+
+ /// The session's dump loaded into a core of its own, which must be the
+ /// same session (its digest) and dump to the same bytes. The tape is
+ /// put aside meanwhile: a dump is no input.
+ fn dumpRoundTrip(t: *Tape, p: *Pardes) void {
+ on = null;
+ defer on = t;
+ const bytes = pardes.dump.dumpBytes(p) catch |err| return t.diverge("dump failed: {t}", .{err});
+ defer t.gpa.free(bytes);
+ var notes: std.ArrayList(u8) = .empty;
+ defer notes.deinit(t.gpa);
+ const q = pardes.dump.load(t.gpa, p.opts, bytes, &notes) catch |err| return t.diverge("dump load failed: {t}", .{err});
+ defer q.deinit();
+ if (notes.items.len > 0) return t.diverge("dump of this build had notes: {s}", .{notes.items});
+ var want: Parts = undefined;
+ var got: Parts = undefined;
+ const a = digestParts(p, &want);
+ const b = digestParts(q, &got);
+ if (!std.mem.eql(u64, a, b)) return t.diverge("dump round trip: the loaded digest differs in {s}", .{partsDiffer(b, a, t.arena.allocator())});
+ const again = pardes.dump.dumpBytes(q) catch |err| return t.diverge("dump of the loaded failed: {t}", .{err});
+ defer t.gpa.free(again);
+ if (!std.mem.eql(u8, bytes, again)) {
+ const at = std.mem.indexOfDiff(u8, bytes, again) orelse 0;
+ return t.diverge("dump is no fixpoint: {d} bytes then {d}, first differing at {d}", .{ bytes.len, again.len, at });
+ }
+ t.dumps += 1;
+ t.dump_bytes += bytes.len;
+ }
+
+ 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 keep = if (t.kept_arena) |*k| k.allocator() else t.gpa;
+ const input = walk.decode(Input, r, if (kept) keep 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,
+ };
+ const gpa = t.core_gpa orelse t.gpa;
+ opts.image_allocator = gpa;
+ opts.pdf_allocator = gpa;
+ opts.tree_sitter_allocator = gpa;
+ opts.frame_allocator = gpa;
+ t.core = switch (input) {
+ .init => Pardes.init(gpa, opts),
+ .init_from_dump => |d| pardes.dump.initFromDump(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) {
+ if (t.dump_check) if (t.core) |core| t.dumpRoundTrip(core);
+ 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 {},
+ .step => p.step(),
+ .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),
+ .adopt_shell => _ = p.adoptShell(),
+ .reader_failed => |a| p.readerFailed(a.id, a.err),
+ .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),
+ .open_view => _ = p.openView(),
+ .close_view => |k| p.closeView(k),
+ .leave_view => |k| p.leaveView(k),
+ .away_view => |k| p.awayView(k),
+ .rejoin_view => |k| _ = p.rejoinView(k),
+ .to_view => |k| p.toView(k),
+ .take_font_request => _ = p.takeFontRequest(),
+ .observe_font => |f| _ = p.observeFont(f.name, f.size, f.unit),
+ .acknowledge_font => |f| _ = p.acknowledgeFont(f.name, f.size, f.unit),
+ .reject_font => p.rejectFont(),
+ .disable_scene_effects => p.disableSceneEffects(),
+ .window_opacity_done => |w| p.windowOpacityDone(w.applied, w.failure),
+ }
+ 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];
+ // A terminal's answers and what is typed at it from its output are the
+ // authority's to write, never a replica's: off the tape on both sides.
+ if (comptime std.mem.eql(u8, name, "pty_write")) if (t.screen_state and !t.speculating) {
+ if (!t.replay) @call(.auto, @field(t.real.vtable.*, name).?, .{t.real.ctx} ++ args);
+ return;
+ };
+ 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 id: u16 = i;
+ if (t.speculating) {
+ // Nobody watches a replica's step: its frames are its own, drawn
+ // outside it (and the authority's host says so too, or the
+ // comparison fails).
+ if (comptime std.mem.eql(u8, name, "watched")) {
+ const r = false;
+ t.record(.host, u16, &id);
+ t.record(.ret, R, &r);
+ return r;
+ }
+ t.poisoned = true;
+ t.poisoned_by = name;
+ return poisonOf(R);
+ }
+ const real = @field(t.real.vtable.*, name).?;
+ 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);
+}
+
+/// A read of what the authority alone holds (`.authority` state a step
+/// makes digested state from: the store's closed panes and Recent, which
+/// every window of its user writes; the 9P server's readers and socket),
+/// taped as `value` is; a speculation that reaches it waits for the
+/// authority's answer, as at a host call. A replica's own is never read.
+/// What it returns is the tape's arena's on a replay: `f` returns what
+/// lives to the step's end (scratch, or the authority's own), and the
+/// caller copies what it keeps.
+///
+/// A yes or no (whether a script reads a pane's events, whether the
+/// session serves a socket) is guessed by a speculation, mosh's way: the
+/// authority's last answer to that question, as a replayed unit gave it,
+/// recorded as the answer, so its unit confirms the guess or rolls it back.
+/// A Look or a click's command in a pane then shows at once, not a round
+/// trip late. ponytail: bools alone; a list (the closed panes, Recent)
+/// changes with what the user does, and is asked of the authority.
+pub fn authority(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
+ if (comptime @typeInfo(@TypeOf(f)).@"fn".return_type.? == bool) if (on) |t| {
+ const key = question(f, args);
+ if (t.speculating) if (t.answers.get(key)) |was| {
+ const id: u16 = @as(u16, host_fns.len) + @backingInt(Site.authority);
+ t.record(.host, u16, &id);
+ t.record(.ret, bool, &was);
+ return was;
+ };
+ if (t.replay) {
+ const got = value(.authority, f, args);
+ t.answers.put(t.gpa, key, got) catch {};
+ return got;
+ }
+ };
+ poison("the authority's own state");
+ return value(.authority, f, args);
+}
+
+/// What `authority` was asked: `f` and its arguments as they are.
+fn question(comptime f: anytype, args: anytype) u64 {
+ var h = std.hash.Wyhash.init(@intFromPtr(&f));
+ inline for (args) |a| h.update(std.mem.asBytes(&a));
+ return h.final();
+}
+
+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);
+ const id: u16 = @as(u16, host_fns.len) + @backingInt(s);
+ if (t.replay) {
+ // Diverged (said in `diverged`): what a speculation gets, harmless,
+ // for the rest of the input. A replica joins again; a replay says so.
+ if (!t.expectHost(id, @tagName(s))) return poisonOf(R);
+ var ret = t.last_ret;
+ return walk.decode(R, &ret, a orelse t.arena.allocator()) catch {
+ t.diverge("the tape's answer to {t} is short", .{s});
+ return poisonOf(R);
+ };
+ }
+ // 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);
+ // A syscall a speculating replica must not make: its input waits for
+ // the authority's answer.
+ if (yields and t.speculating) {
+ t.poisoned = true;
+ t.poisoned_by = @tagName(s);
+ return poisonOf(R);
+ }
+ if (comptime s == .clock or s == .mtime) if (if (t.speculating) t.guess else t.stamp) |g| {
+ const r = guessed(R, g);
+ t.record(.host, u16, &id);
+ t.record(.ret, R, &r);
+ return r;
+ };
+ 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;
+}
+
+pub const Guess = struct { real_ns: i64, mono_ns: i64, gmtoff: i32 };
+
+/// A clock read of the session's, as a replica client guesses it: a file's
+/// mtime (realtime seconds), a message's clock (monotonic ms), a message's
+/// time of day (in the session's zone).
+fn guessed(comptime R: type, g: Guess) R {
+ if (R == u32) return std.math.cast(u32, @divFloor(g.real_ns, std.time.ns_per_s)) orelse 0;
+ if (R == i64) return @divFloor(g.mono_ns, std.time.ns_per_ms);
+ if (R == [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;
+ const day = @mod(@divFloor(g.real_ns, std.time.ns_per_s) + g.gmtoff, 86400);
+ _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ @as(u32, @intCast(@divFloor(day, 3600))), @as(u32, @intCast(@mod(@divFloor(day, 60), 60))), @as(u32, @intCast(@mod(day, 60))) }) catch {};
+ return clock;
+ }
+ @compileError("tape: no guess of a clock read as " ++ @typeName(R));
+}
+
+/// The session's digest recorded now (a unit's end: the replica stream's
+/// one, `unit_digests`).
+pub fn recordDigest(t: *Tape) void {
+ const core = t.core orelse return;
+ var parts: Parts = undefined;
+ const whole = [1]u64{digestOf(core)};
+ const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts);
+ t.record(.digest, []const u64, &d);
+}
+
+/// The core's digest against a digest record's payload (a unit's last): a
+/// difference is `diverged`, its fields named when the record has them.
+pub fn checkDigest(t: *Tape, payload: []const u8) void {
+ var r: walk.Reader = .{ .bytes = payload };
+ t.check(&r);
+}
+
+/// A core made outside the tape (a replica client's, from a dump) is the
+/// one it replays into.
+pub fn adoptCore(t: *Tape, p: *Pardes) void {
+ t.core = p;
+ p.host = t.hostOf(t.mask);
+}
+
+/// A replica joins the session recorded on `t` (src/replica.zig): what the
+/// host wrote in the core and which host it is are said again at the next
+/// input, for a core made from a dump, which has neither.
+pub fn resync(t: *Tape) void {
+ t.seen = @splat(0);
+ if (t.core) |p| if (p.host.vtable == &t.vtable) {
+ p.host = t.real;
+ };
+ t.real = .{};
+}
+
+/// 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 or t.speculating else false;
+}
+
+/// A syscall of the core's that is no site, made by a replica: what it
+/// returns is the authority's to say. A speculation waits for it, and a
+/// replay has diverged.
+pub fn stray() void {
+ const t = on orelse return;
+ if (t.speculating) t.poisoned = true else t.diverge("a syscall that is no tape site, made replaying", .{});
+}
+
+/// A replica speculating (src/replica.zig): state only the authority
+/// changes (a terminal's emulator) is left alone, and the input waits.
+pub fn speculating() bool {
+ return if (on) |t| t.speculating else false;
+}
+
+/// Screen-state terminals, on either side (`Tape.screen_state`).
+pub fn screenState() bool {
+ return if (on) |t| t.screen_state else false;
+}
+
+/// A replica's core under screen-state terminals: its emulators are
+/// written by `apply` alone, never by its own steps.
+pub fn screenReplica() bool {
+ return if (on) |t| t.screen_state and (t.replay or t.speculating) else false;
+}
+
+/// An authority recording for screen-state replicas.
+pub fn screenAuthority() bool {
+ return if (on) |t| t.screen_state and !t.replay and !t.speculating else false;
+}
+
+/// What a speculation reached is the authority's (`why`): the input is
+/// undone and waits for the authority's order.
+pub fn poison(why: []const u8) void {
+ if (on) |t| if (t.speculating) {
+ t.poisoned = true;
+ t.poisoned_by = why;
+ };
+}
+
+/// A speculation read a terminal's rows or moved its view (`Tape.touched`).
+pub fn touchTerminal() void {
+ if (on) |t| if (t.speculating) {
+ t.touched = true;
+ };
+}
+
+/// A harmless stand-in for what a host call or a syscall would have
+/// returned to a speculation that is thrown away once it returns: an
+/// error where one can be said, else zeroes.
+fn poisonOf(comptime R: type) R {
+ return switch (@typeInfo(R)) {
+ .error_union => |e| if (@typeInfo(e.error_set).error_set.error_names) |errors|
+ (if (errors.len > 0) @field(e.error_set, errors[0]) else std.mem.zeroes(R))
+ else
+ error.Unexpected,
+ .optional => null,
+ .void => {},
+ .@"union" => |u| inline for (u.field_names, u.field_types) |name, F| {
+ if (comptime zeroable(F)) break @unionInit(R, name, std.mem.zeroes(F));
+ } else @compileError("tape: no harmless value of " ++ @typeName(R)),
+ else => std.mem.zeroes(R),
+ };
+}
+
+fn zeroable(comptime T: type) bool {
+ return switch (@typeInfo(T)) {
+ .int, .float, .bool, .optional, .void => true,
+ .pointer => |p| p.size == .slice,
+ .array => |a| zeroable(a.child),
+ .@"struct" => |s| blk: {
+ for (s.field_types) |F| if (!zeroable(F)) break :blk false;
+ break :blk true;
+ },
+ else => false,
+ };
+}
+
+// ---- a replica's tapes in memory (src/replica.zig) ----
+
+/// A tape recorded in memory: an authority's stream, taken unit by unit.
+pub fn memory(gpa: std.mem.Allocator) Tape {
+ return .{ .gpa = gpa, .replay = false, .scratch = .init(gpa), .arena = .init(gpa), .kept_arena = .init(gpa) };
+}
+
+/// What a tape in memory holds, given back (its core is gone already).
+pub fn release(t: *Tape) void {
+ t.answers.deinit(t.gpa);
+ t.buf.deinit(t.gpa);
+ t.scratch.deinit();
+ t.arena.deinit();
+ if (t.kept_arena) |*k| k.deinit();
+}
+
+/// The session's digest recorded now: a batch of steps recorded without
+/// one each (`no_digest`) checked once, at its end.
+pub fn digestHere(t: *Tape) void {
+ const core = t.core orelse return;
+ var parts: Parts = undefined;
+ const whole = [1]u64{digestOf(core)};
+ const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts);
+ t.record(.digest, []const u64, &d);
+}
+
+/// What was recorded since the last take; the tape's to reuse after.
+pub fn taken(t: *Tape) []const u8 {
+ return t.buf.items;
+}
+
+pub fn clearTaken(t: *Tape) void {
+ t.buf.clearRetainingCapacity();
+}
+
+/// The records of `bytes`, top-level inputs and all, applied to the
+/// tape's core as the authority applied them, each digest checked; why
+/// they diverged, or null.
+pub fn replayUnit(t: *Tape, bytes: []const u8) ?[]const u8 {
+ t.replay = true;
+ t.speculating = false;
+ defer t.replay = false;
+ t.in = .{ .bytes = bytes };
+ 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}),
+ }
+ }
+ return t.diverged;
+}
+
+/// Starts recording `t`'s core as a speculation: the host's watched
+/// fields as they are now, nothing recorded yet.
+pub fn beginSpeculation(t: *Tape) void {
+ t.replay = false;
+ t.speculating = true;
+ t.poisoned = false;
+ t.poisoned_by = "";
+ t.touched = false;
+ t.no_digest = true;
+ t.buf.clearRetainingCapacity();
+ t.seeFields();
+}
+
+/// Whether it stayed clean (no host call, no syscall, no authority state).
+pub fn endSpeculation(t: *Tape) bool {
+ t.speculating = false;
+ t.no_digest = false;
+ return !t.poisoned;
+}
+
+/// Whether two tapes record the same inputs, host calls and answers,
+/// their digests aside.
+pub fn sameRecords(a: []const u8, b: []const u8) bool {
+ var x: walk.Reader = .{ .bytes = a };
+ var y: walk.Reader = .{ .bytes = b };
+ while (true) {
+ const ra = nextRecord(&x);
+ const rb = nextRecord(&y);
+ if (ra == null or rb == null) return ra == null and rb == null;
+ if (ra.?.kind != rb.?.kind or !std.mem.eql(u8, ra.?.payload, rb.?.payload)) return false;
+ }
+}
+
+fn nextRecord(r: *walk.Reader) ?struct { kind: Kind, payload: []const u8 } {
+ while (true) {
+ const head = r.take(5) catch return null;
+ const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return null;
+ const kind: Kind = @fromBackingInt(@intCast(head[0]));
+ if (kind != .digest) return .{ .kind = kind, .payload = payload };
+ }
+}
+
+/// A tape's records, one a line (kind; an input's or a host call's name),
+/// for a divergence's report.
+pub fn describe(bytes: []const u8, w: *std.Io.Writer) void {
+ var r: walk.Reader = .{ .bytes = bytes };
+ var depth: usize = 0;
+ while (true) {
+ const head = r.take(5) catch return;
+ const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return;
+ const kind: Kind = @fromBackingInt(@intCast(head[0]));
+ if (kind == .done or kind == .ret) depth -|= 1;
+ w.splatByteAll(' ', depth * 2) catch {};
+ switch (kind) {
+ .input => {
+ const tag = std.mem.bytesToValue(std.meta.Tag(Input), payload[0..@sizeOf(std.meta.Tag(Input))]);
+ w.print("input {t}", .{tag}) catch {};
+ if (tag == .update) w.print(" {t}", .{std.mem.bytesToValue(std.meta.Tag(pardes.Event), payload[@sizeOf(std.meta.Tag(Input))..][0..@sizeOf(std.meta.Tag(pardes.Event))])}) catch {};
+ // A key's or a pointer's own words: which one a divergence
+ // starts at.
+ var room: [4096]u8 = undefined;
+ var fixed: std.heap.FixedBufferAllocator = .init(&room);
+ var in: walk.Reader = .{ .bytes = payload };
+ if (tag == .update) if (walk.decode(Input, &in, fixed.allocator())) |input| switch (input.update) {
+ .key => |k| w.print(" {any}", .{k}) catch {},
+ .mouse => |m| w.print(" {t} {t} {d},{d} body {any} tag {any}", .{ m.button, m.kind, m.col, m.row, m.body_hit != null, m.tag_hit != null }) catch {},
+ else => {},
+ } else |_| {};
+ },
+ .host => {
+ const id = std.mem.bytesToValue(u16, payload[0..2]);
+ w.print("host {s}", .{if (id < host_fns.len) host_fns[id] else @tagName(@as(Site, @fromBackingInt(@intCast(id - host_fns.len))))}) catch {};
+ },
+ .ret => if (payload.len <= 8) w.print("ret {x}", .{payload}) catch {} else w.print("ret ({d} bytes)", .{payload.len}) catch {},
+ else => w.print("{t} ({d} bytes)", .{ kind, payload.len }) catch {},
+ }
+ w.writeByte('\n') catch {};
+ if (kind == .input or kind == .host) depth += 1;
+ }
+}
+
+/// The digest a replica and its authority must agree on.
+pub fn digestOf(p: *const Pardes) u64 {
+ return walk.digest(Pardes, p);
+}
+
+/// PARDES_RECORD set: the tape is written from the session's first call.
+pub fn startFromEnv() void {
+ if (comptime !pardes.hosted) return;
+ 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) };
+ t.dump_check = std.mem.eql(u8, run.name, "first") and libc.getenv("PARDES_REPLAY_DUMP") != null;
+ 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;
+ };
+ // One killed (as the harnesses end theirs) has its end state here.
+ if (t.dump_check and t.diverged == null) if (t.core) |core| t.dumpRoundTrip(core);
+ 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 });
+ if (t.dump_check and t.diverged == null) std.debug.print("replay {s}: {d} dumps loaded to their digests and dumped to their bytes again ({d} bytes)\n", .{ path, t.dumps, t.dump_bytes });
+ }
+ return if (failed) 1 else 0;
+}
+
+test "a syscall that is no site, made replaying, diverges the replay and poisons a speculation" {
+ if (comptime !pardes.hosted) return;
+ const gpa = std.testing.allocator;
+ var t: Tape = .{ .gpa = gpa, .replay = true, .scratch = .init(gpa), .arena = .init(gpa) };
+ defer t.scratch.deinit();
+ defer t.arena.deinit();
+ defer if (t.diverged) |why| gpa.free(why);
+ on = &t;
+ defer on = null;
+ pardes.turn.yield();
+ pardes.turn.back();
+ try std.testing.expect(t.diverged != null);
+ t.replay = false;
+ t.speculating = true;
+ pardes.turn.yield();
+ pardes.turn.back();
+ try std.testing.expect(t.poisoned);
+}
+
+test "a replay that finds no answer where its core reads one diverges, and the read goes on harmless" {
+ if (comptime !pardes.hosted) return;
+ const gpa = std.testing.allocator;
+ var t: Tape = .{ .gpa = gpa, .replay = true, .scratch = .init(gpa), .arena = .init(gpa) };
+ defer t.scratch.deinit();
+ defer t.arena.deinit();
+ defer if (t.diverged) |why| gpa.free(why);
+ // The input's end where the core reads a value of the host's (a
+ // window client's step that took a turn the session's had not, H9 of
+ // 2026-10-30: it aborted the client).
+ const bytes = [5]u8{ @backingInt(Kind.done), 0, 0, 0, 0 };
+ t.in = .{ .bytes = &bytes };
+ t.host_side = false;
+ on = &t;
+ defer on = null;
+ const read = struct {
+ fn f() bool {
+ return true;
+ }
+ }.f;
+ try std.testing.expect(!value(.denied, read, .{}));
+ try std.testing.expect(t.diverged != null);
+ try std.testing.expect(!value(.denied, read, .{}));
+}