diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 02:02:19 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 02:32:57 -0300 |
| commit | c77e5a09af27976b2375a902ecb8efa428395a0c (patch) | |
| tree | cb36b7b3c691b4904d2a1c2a182cd747e7c82602 /core/tape.zig | |
| parent | 866e9ed6762f42a32907d63d783464f2c90d5b85 (diff) | |
| download | pardes-c77e5a09af27976b2375a902ecb8efa428395a0c.tar.gz pardes-c77e5a09af27976b2375a902ecb8efa428395a0c.zip | |
tape: the wrappers deleted, step 6c (reports/pardes-handle-69-design.md): a core's host is always its own (a replica's none), the tape only says which functions it has when the core holds another than the last said (noteHost, today's comparison, so a mask lands where it did) and a replica answers by that word (the bool replica, set by its first replay or its join; Tape.has), so Wrap's arity table, hostOf, wrap, the tape's vtable, resync's and Peers.stop's unwrap and apply's three hostOf go; the 9 presence questions are p.tape.has(p, .search); keeps_stopping and forTheView are lists of the table's names, a renamed field no longer compiling there; root.zig's machine scan names host.vtable and host.ctx, which only tape.zig's noteHost line names now (40 lines of core/ named the table at the base); the tape's bytes do not move (semantics 32640d4a722b811c unmoved, cross_replay both ways)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/tape.zig')
| -rw-r--r-- | core/tape.zig | 164 |
1 files changed, 58 insertions, 106 deletions
diff --git a/core/tape.zig b/core/tape.zig index 18b5e296..80a14079 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -239,18 +239,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, @@ -378,35 +384,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; - const mask = p.host.mask(); - 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; @@ -415,7 +403,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) { @@ -432,7 +420,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); @@ -639,8 +627,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 h = t.hostOf(std.mem.bytesToValue(u64, r.take(8) catch return)); - if (t.core) |p| p.host = h; + t.mask = std.mem.bytesToValue(u64, r.take(8) catch return); return; } const p = t.core orelse return; @@ -683,7 +670,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 => {}, @@ -695,7 +681,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. @@ -706,7 +691,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 {}; @@ -838,12 +823,19 @@ pub const Tape = struct { /// 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. (Until the wrappers go, - // a core's host is this tape's table while it records or replays, - // and any other is called as it is.) - if (t.through or p.host.vtable != &t.vtable) return p.host.call(f, args); - if (!p.host.has(f)) return null; - return hostCall(t, f, args); + // 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. @@ -916,7 +908,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). @@ -1132,95 +1124,58 @@ 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 h = @field(Host.Fn, host_fns[i]); - const R = Host.Ret(h); - const P = @typeInfo(@typeInfo(@FieldType(VTable, host_fns[i])).optional.child).pointer.child; - const A = @typeInfo(P).@"fn".param_types; - return switch (A.len) { - 1 => struct { - fn f(ctx: ?*anyopaque) R { - return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{}); - } - }, - 2 => struct { - fn f(ctx: ?*anyopaque, a: A[1].?) R { - return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{a}); - } - }, - 3 => struct { - fn f(ctx: ?*anyopaque, a: A[1].?, b: A[2].?) R { - return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{ a, b }); - } - }, - 4 => struct { - fn f(ctx: ?*anyopaque, a: A[1].?, b: A[2].?, c: A[3].?) R { - return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{ a, b, c }); - } - }, - 5 => struct { - fn f(ctx: ?*anyopaque, a: A[1].?, b: A[2].?, c: A[3].?, d: A[4].?) R { - return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{ 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; } /// The one body of a call out to the host through the tape (`Tape.host`). -fn hostCall(t: *Tape, comptime f: Host.Fn, args: Host.Args(f)) Host.Ret(f) { +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) { @@ -1253,7 +1208,7 @@ fn hostCall(t: *Tape, comptime f: Host.Fn, args: Host.Args(f)) Host.Ret(f) { // 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; @@ -1265,17 +1220,16 @@ fn hostCall(t: *Tape, comptime f: Host.Fn, args: Host.Args(f)) Host.Ret(f) { // 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); @@ -1336,7 +1290,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 @@ -1344,9 +1298,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 = .{}; } @@ -1473,6 +1424,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; |
