diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 04:16:41 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 04:16:41 -0300 |
| commit | 5b7124d10f9b2db8d1c5e99eacb9b6d2fffff0cc (patch) | |
| tree | dc50936490ad8636f5431da30ec3aaa38339c936 /core/tape.zig | |
| parent | 6e8735895d04c0073eb28cc1a2f431a2ba587e9d (diff) | |
| parent | 8fb080c6208bccf9ef12eca630b0534312ae5fe7 (diff) | |
| download | pardes-5b7124d10f9b2db8d1c5e99eacb9b6d2fffff0cc.tar.gz pardes-5b7124d10f9b2db8d1c5e99eacb9b6d2fffff0cc.zip | |
integration: the core's calls out to the host go through the tape's handle, on the devices once and the attach that is always a client: pardes-handle-6 (6a, 6b, 6c, 8a: p.tape.host(p, .name, .{ args }) derived from the Host table; Wrap, hostOf and wrap deleted; no line of core/ outside the tape names the host's table, held by root.zig's scan; the tape's bytes unmoved) and pardes-merge30, merged without a textual conflict; tui core-test 857/1008 (151 skipped), shell-test 1048/1051, the window builds; the coordinator's gate in the ledger (reorg/merge31)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/tape.zig')
| -rw-r--r-- | core/tape.zig | 249 |
1 files changed, 140 insertions, 109 deletions
diff --git a/core/tape.zig b/core/tape.zig index f705636f..f9303d67 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -25,7 +25,8 @@ const pardes = @import("root.zig"); const walk = @import("walk.zig"); const host_io = @import("dev").host_io; const Pardes = pardes.Pardes; -const VTable = pardes.Host.VTable; +const Host = pardes.Host; +const VTable = Host.VTable; const libc = std.c; pub var on: ?*Tape = null; @@ -185,7 +186,7 @@ fn ErrorOf(comptime f: anytype) type { pub const shape = blk: { var types: []const type = &.{ Input, Site, []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 (host_fns) |f| types = types ++ .{Host.Ret(@field(Host.Fn, f))}; for (watched) |name| types = types ++ .{Watched(name)}; break :blk walk.shape(types); }; @@ -245,18 +246,24 @@ pub const Tape = struct { /// 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 = .{}, + /// The host whose functions were last said (`noteHost`). + real: Host = .{}, seen: [watched.len]u64 = @splat(0), // Replaying. in: walk.Reader = .{ .bytes = &.{} }, arena: std.heap.ArenaAllocator, last_ret: walk.Reader = .{ .bytes = &.{} }, + /// Which functions the session's host has, a bit each in the table's + /// order (`Host.mask`): what a replica's core answers by (`has`). mask: u64 = 0, + /// A replica's (a client's, a replay's), from its first replay or its + /// join (`replayUnit`, `adoptCore`): its core has no host, and asks + /// the tape what the session's has, between its steps too (a frame). + replica: bool = false, skips_render: bool = false, digests: u64 = 0, diverged: ?[]const u8 = null, @@ -388,38 +395,17 @@ pub const Tape = struct { inline for (watched, 0..) |name, i| t.seen[i] = walk.digest(Watched(name), watchedOf(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; - } + /// Which functions the session's host has, said at an entry whose + /// core holds another host than the last said (each pump is given its + /// host again: then nothing). + fn noteHost(t: *Tape, p: *const Pardes) void { + if (p.host.vtable == t.real.vtable and p.host.ctx == t.real.ctx) 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.mask = p.host.mask(); + const set: struct { u8, u64 } = .{ set_mask, t.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.skips_render 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; @@ -428,7 +414,7 @@ pub const Tape = struct { if (p) |core| { if (t.core == null) t.core = core; t.noteWrites(); - t.wrap(core); + t.noteHost(core); } // A terminal's output crosses as screen state, not as its bytes. if (t.screen_state and input.* == .update and input.update.args[0] == .output) { @@ -445,7 +431,7 @@ pub const Tape = struct { if (t.replay) return; if (p) |core| if (t.core == null) { t.core = core; - t.wrap(core); + t.noteHost(core); }; t.record(.done, void, &{}); if ((t.depth == 0 or t.every) and !t.no_digest and !t.unit_digests) recordDigest(t); @@ -653,8 +639,7 @@ pub const Tape = struct { 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; + t.mask = std.mem.bytesToValue(u64, r.take(8) catch return); return; } const p = t.core orelse return; @@ -697,7 +682,6 @@ pub const Tape = struct { .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 => {}, @@ -709,7 +693,6 @@ pub const Tape = struct { const nc = pardes.dump.restore(p, a.args[0], a.args[1]) catch |err| return t.diverge("restore failed: {t}", .{err}); t.core = nc; nc.tape = t; - nc.host = t.hostOf(t.mask); }, // The session is over (or the core a Restore replaced is // freed): nothing done now is compared. @@ -720,7 +703,7 @@ pub const Tape = struct { } return t.skip(); }, - .pump => p.pump(t.hostOf(t.mask)) catch {}, + .pump => p.pump(.{}) catch {}, .render => if (t.skips_render) return t.skip() else { _ = p.frame_arena.reset(.retain_capacity); _ = p.render(p.frame_arena.allocator()) catch {}; @@ -857,6 +840,26 @@ pub const Tape = struct { return r; } + /// One call out to the session's host, `f(args)`: made and recorded + /// with what it answered; answered from the tape on a replay; refused + /// to a speculation, or recorded as made (`keeps_stopping`). Null: the + /// session's host has no such function. + pub inline fn host(t: *Tape, p: *const Pardes, comptime f: Host.Fn, args: Host.Args(f)) ?Host.Ret(f) { + // Nobody records: the call it always was. + if (t.through) return p.host.call(f, args); + if (!t.has(p, f)) return null; + return hostCall(t, p, f, args); + } + + /// Whether the session's host has `f`, asked apart from a call: a + /// replica's core has no host, and answers as its session's did. + pub fn has(t: *const Tape, p: *const Pardes, comptime f: Host.Fn) bool { + if (!t.replica) return p.host.has(f); + // A replica that never draws asks, and is told no one watches (`hostCall`). + if (comptime f == .watched) if (t.skips_render) return true; + return t.mask & (@as(u64, 1) << @backingInt(f)) != 0; + } + /// Asked by `turn.yield`: a syscall a replica must never make. pub fn replaying(t: *const Tape) bool { return t.replay or t.speculating; @@ -922,7 +925,7 @@ pub const Tape = struct { p.tape = t; if (t.replay or t.through) return; t.core = p; - t.wrap(p); + t.noteHost(p); } /// Whether `p` is the core being recorded (a Restore's old one is not). @@ -1138,100 +1141,64 @@ fn tagOf(comptime entry: anytype, comptime Made: type) [:0]const u8 { return found orelse @compileError("tape: no variant of Input names the entry " ++ @typeName(Made) ++ " (a line `<tag>: Args(<the entry>)` in tape.Input)"); } -/// 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"), - }; -} - /// The Host entries that return nothing and still stop a speculation, each /// with why; a speculation records any other as made and goes on (`hostCall`). /// The entries that answer stop by nature: the answer is the session's. -const keeps_stopping = .{ +const keeps_stopping = [_]Host.Fn{ // A shell's input: a device by the user's line (a far shell may feel // like ssh), and off the tape with screen-state terminals. - "pty_write", + .pty_write, // Its host says into the core on every call that the shell started // and where (host_io.forkShell: acknowledgeShell, setCwd), and gives // the turn away: a rollback every time, nothing gained. - "spawn", + .spawn, // Its host writes the core outside any record (exec.reapplyInitFile, // Plumb.saved, fs.writeSelf) besides `saved` or the failure, every call. - "write_file", + .write_file, // Their hosts say done or failed into the core on every call // (setLastDump, dumpFailed; setMessage, reportError): a rollback every // time, nothing gained. - "write_dump", - "dump_themes", + .write_dump, + .dump_themes, // The session's own: it execs itself after the step, carries a window // away, or lets its frontend go (writing `frontend` and `unattended`). - "upgrade", - "hand_over", - "detach", + .upgrade, + .hand_over, + .detach, // The loop's, never a step's. - "wait_input", - "present", - "post_present", - "poll_frame", + .wait_input, + .present, + .post_present, + .poll_frame, }; -fn keepsStopping(comptime name: []const u8, args: anytype) bool { +fn keepsStopping(comptime f: Host.Fn, args: Host.Args(f)) bool { // Turned on, its host loads the theme file into the core outside any // record (colors.loadThemeFile: custom_theme, theme_file_loaded); // turned off it only lets the file go. - if (comptime std.mem.eql(u8, name, "watch_theme")) return args[1]; - inline for (keeps_stopping) |s| if (comptime std.mem.eql(u8, s, name)) return true; - return false; + if (comptime f == .watch_theme) return args[1]; + return comptime std.mem.indexOfScalar(Host.Fn, &keeps_stopping, f) != null; } /// The entries a view's frontend makes on its own machine (P16). -fn forTheView(comptime name: []const u8) bool { - return std.mem.eql(u8, name, "set_clipboard") or std.mem.eql(u8, name, "read_clipboard") or std.mem.eql(u8, name, "open_link"); +fn forTheView(comptime f: Host.Fn) bool { + return f == .set_clipboard or f == .read_clipboard or f == .open_link; } -fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype) R { - const t: *Tape = @ptrCast(@alignCast(ctx.?)); - const name = host_fns[i]; +/// The one body of a call out to the host through the tape (`Tape.host`). +fn hostCall(t: *Tape, p: *const Pardes, comptime f: Host.Fn, args: Host.Args(f)) Host.Ret(f) { + const R = Host.Ret(f); + const name = @tagName(f); // 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); + if (!t.replay) _ = p.host.call(f, 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.skips_render) return false; - if (!t.expectHost(i, name)) return std.mem.zeroes(R); + if (!t.expectHost(@backingInt(f), 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. @@ -1243,7 +1210,7 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype } return walk.decode(R, &ret, t.arena.allocator()) catch std.mem.zeroes(R); } - const id: u16 = i; + const id: u16 = @backingInt(f); 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 @@ -1258,7 +1225,7 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype // on. The session's unit records the same, or more where its host // wrote the core during the call (an input it made, a watched field // set), and the client rolls back. - if (R == void and !keepsStopping(name, args)) { + if (R == void and !keepsStopping(f, args)) { t.record(.host, u16, &id); t.record(.ret, void, &{}); return; @@ -1270,17 +1237,16 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype // A client's yank, paste or link is its own frontend's, made on its own // machine from its view's record (Pardes.View.yanks): never the // session's, whose host makes them for a view no client draws. - if (comptime forTheView(name)) if (t.core) |p| if (p.undrawn & (@as(u8, 1) << @intCast(p.live)) != 0) { + if (comptime forTheView(f)) if (p.undrawn & (@as(u8, 1) << @intCast(p.live)) != 0) { t.record(.host, u16, &id); t.record(.ret, void, &{}); return; }; - 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); + const r = p.host.call(f, args).?; t.host_side = !outer; t.noteWrites(); t.record(.ret, R, &r); @@ -1347,7 +1313,7 @@ pub fn recordDigest(t: *Tape) void { pub fn adoptCore(t: *Tape, p: *Pardes) void { t.core = p; p.tape = t; - p.host = t.hostOf(t.mask); + t.replica = true; } /// A replica joins the session recorded on `t` (core/replica.zig): what the @@ -1355,9 +1321,6 @@ pub fn adoptCore(t: *Tape, p: *Pardes) void { /// 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 = .{}; } @@ -1484,6 +1447,7 @@ pub fn release(t: *Tape) void { /// 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.replica = true; t.replay = true; t.speculating = false; defer t.replay = false; @@ -1818,6 +1782,73 @@ test "every place that sets the global tape aside sets the core's field aside: a try std.testing.expectEqual(@as(usize, 0), wrong); } +/// The pump's own calls: a host with a clock and a frontend that shows +/// every frame; `more` has one function besides. +const Pumped = struct { + ns: u64, + fn now(ctx: ?*anyopaque) u64 { + const h: *Pumped = @ptrCast(@alignCast(ctx.?)); + h.ns += std.time.ns_per_ms; + return h.ns; + } + fn seen(_: ?*anyopaque) bool { + return true; + } + fn present(_: ?*anyopaque, _: *const pardes.Surface) bool { + return true; + } + fn poll(_: ?*anyopaque) void {} + const one: VTable = .{ .now = now, .watched = seen, .present = present }; + const more: VTable = .{ .now = now, .watched = seen, .present = present, .poll_frame = poll }; + + /// Recorded `bytes`: how many times which functions the host has was + /// said, the last said, and the calls of each function. + fn said(bytes: []const u8) struct { sets: usize = 0, mask: u64 = 0, calls: [host_fns.len]usize = @splat(0) } { + var got: @typeInfo(@TypeOf(said)).@"fn".return_type.? = .{}; + var r: walk.Reader = .{ .bytes = bytes }; + while (nextRecord(&r)) |rec| { + if (rec.kind == .set and rec.payload[0] == set_mask) { + got.sets += 1; + got.mask = std.mem.bytesToValue(u64, rec.payload[1..9]); + } + if (rec.kind == .host) { + const id = std.mem.bytesToValue(u16, rec.payload[0..2]); + if (id < host_fns.len) got.calls[id] += 1; + } + } + return got; + } +}; + +test "a core pumped twice with one host and once with another: which functions its host has is said at the first pump and the third, its clock, watch and present at each; a replica replays the three units to the same digest" { + const gpa = std.testing.allocator; + const R = pardes.replica; + var first: Pumped = .{ .ns = 0 }; + var second: Pumped = .{ .ns = 1 << 40 }; + const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24 }, .{ .ctx = &first, .vtable = &Pumped.one }, false); + defer a.deinit(); + const boot = Pumped.said(a.t.buf.items); + try std.testing.expectEqual(Host.mask(.{ .vtable = &Pumped.one }), boot.mask); + const r = try R.Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + const hosts = [_]Host{ .{ .ctx = &first, .vtable = &Pumped.one }, .{ .ctx = &second, .vtable = &Pumped.more } }; + for (hosts, [_]usize{ 0, 1 }, 1..) |h, sets, seq| { + a.host = h; + a.apply(.{ .resize = .{ .cols = @intCast(80 + seq), .rows = 24 } }); + const got = Pumped.said(a.t.buf.items); + try std.testing.expectEqual(sets, got.sets); + if (sets > 0) try std.testing.expectEqual(h.mask(), got.mask); + inline for (.{ .now, .watched, .present }) |f| try std.testing.expect(got.calls[@backingInt(@as(Host.Fn, f))] > 0); + try std.testing.expectEqual(@intFromBool(h.has(.poll_frame)), got.calls[@backingInt(Host.Fn.poll_frame)]); + a.seq += 1; + r.confirm(try a.unit(0, a.seq)); + } + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(R.digestOf(a), r.digest()); +} + test "a client's frame drawn between its steps records nothing into its idle tape" { const gpa = std.testing.allocator; const p = try @import("ninep/harness.zig").withFile(gpa, "x\n"); |
