From 2bbcc0a9aca11a418b8e51fd5d3158f2bc632d4b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 00:30:35 -0300 Subject: tape: the calls out to the host by the Host table's own names, step 6a (the design of reports/pardes-handle-69-design.md, P33): Host gains what a call names, gives and gets, derived from its table (Fn, Args, Ret, has, mask, call), the tape one method, p.tape.host(p, .tty_taken, .{.at(id)}), whose absent arm is the caller's after orelse, and the 7 calls made straight on the table (hostForegroundName, hostTtyTaken, hostKillJob, Adopt, Upgrade, clockNow, askAhead) go through it; hostCall is the one body (the wrappers forward to it until 6c deletes them), its text in place; the tape's bytes do not move (semantics 32640d4a722b811c unmoved, cross_replay both ways) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 9 +++----- core/Stream.zig | 2 +- core/builtins.zig | 6 ++--- core/exec.zig | 2 +- core/tape.zig | 66 ++++++++++++++++++++++++++++++++----------------------- 5 files changed, 46 insertions(+), 39 deletions(-) (limited to 'core') diff --git a/core/Core.zig b/core/Core.zig index 351c3626..db0c5794 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1322,18 +1322,15 @@ fn nextQueued(p: *Pardes) ?Event { } pub fn hostForegroundName(p: *const Pardes, id: usize, buf: []u8) ?[]const u8 { - const f = p.host.vtable.fg_name orelse return null; - return f(p.host.ctx, .at(id), buf); + return p.tape.host(p, .fg_name, .{ .at(id), buf }) orelse null; } pub fn hostTtyTaken(p: *const Pardes, id: usize) bool { - const f = p.host.vtable.tty_taken orelse return false; - return f(p.host.ctx, .at(id)); + return p.tape.host(p, .tty_taken, .{.at(id)}) orelse false; } pub fn hostKillJob(p: *const Pardes, id: usize) bool { - const f = p.host.vtable.kill_job orelse return false; - return f(p.host.ctx, .at(id)); + return p.tape.host(p, .kill_job, .{.at(id)}) orelse false; } /// A terminal whose shell could not be started: said, and a run waiting diff --git a/core/Stream.zig b/core/Stream.zig index 5960d249..dae61a8b 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -343,7 +343,7 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont pub fn askAhead(p: *Pardes) void { const gen = p.stream.ahead_ask orelse return; p.stream.ahead_ask = null; - if (p.host.vtable.search) |f| f(p.host.ctx, gen); + _ = p.tape.host(p, .search, .{gen}); } /// A file's colours for the query rows that show its lines, the whole diff --git a/core/builtins.zig b/core/builtins.zig index a7340352..7122cd02 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -547,8 +547,7 @@ pub const Adopt = struct { pub const scope: Scope = .session; pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { - const adopt = c.p.host.vtable.adopt orelse return c.p.reportFailure(c.id, "Adopt: no pardes daemon is keeping shells for this session"); - adopt(c.p.host.ctx, .at(c.id)); + c.p.tape.host(c.p, .adopt, .{.at(c.id)}) orelse c.p.reportFailure(c.id, "Adopt: no pardes daemon is keeping shells for this session"); } }; @@ -559,8 +558,7 @@ pub const Upgrade = struct { pub const scope: Scope = .session; pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { - const upgrade = c.p.host.vtable.upgrade orelse return c.p.reportFailure(c.id, "Upgrade: this pardes cannot move to another build in place"); - upgrade(c.p.host.ctx, .at(c.id)); + c.p.tape.host(c.p, .upgrade, .{.at(c.id)}) orelse c.p.reportFailure(c.id, "Upgrade: this pardes cannot move to another build in place"); } }; diff --git a/core/exec.zig b/core/exec.zig index 1ec47eb3..197827bb 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -535,7 +535,7 @@ const repl_quiet_ns: u64 = 100 * std.time.ns_per_ms; /// The host's clock now; core time (`now_ns`, `stepped_ns`) is as old as /// the last frame, seconds in an idle session. pub fn clockNow(p: *Pardes) u64 { - return if (p.host.vtable.now) |clock| clock(p.host.ctx) else p.clock.now_ns; + return p.tape.host(p, .now, .{}) orelse p.clock.now_ns; } /// Sends the Enters a paste into pane `id`'s REPL is owed: when the REPL diff --git a/core/tape.zig b/core/tape.zig index 61bdcc08..b74915e5 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; @@ -178,7 +179,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); }; @@ -388,10 +389,7 @@ pub const Tape = struct { 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; - }; + const mask = p.host.mask(); p.host = t.hostOf(mask); const set: struct { u8, u64 } = .{ set_mask, mask }; t.record(.set, @TypeOf(set), &set); @@ -641,8 +639,8 @@ 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; + const h = t.hostOf(std.mem.bytesToValue(u64, r.take(8) catch return)); + if (t.core) |p| p.host = h; return; } const p = t.core orelse return; @@ -835,6 +833,19 @@ 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. (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); + } + /// Asked by `turn.yield`: a syscall a replica must never make. pub fn replaying(t: *const Tape) bool { return t.replay or t.speculating; @@ -1123,34 +1134,34 @@ fn tagOf(comptime entry: anytype, comptime Made: type) [:0]const u8 { /// 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) { + 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(i, R, ctx, .{}); + return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{}); } }, 2 => struct { - fn f(ctx: ?*anyopaque, a: P[1].?) R { - return hostCall(i, R, ctx, .{a}); + fn f(ctx: ?*anyopaque, a: A[1].?) R { + return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{a}); } }, 3 => struct { - fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?) R { - return hostCall(i, R, ctx, .{ a, b }); + 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: P[1].?, b: P[2].?, c: P[3].?) R { - return hostCall(i, R, ctx, .{ a, b, c }); + 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: P[1].?, b: P[2].?, c: P[3].?, d: P[4].?) R { - return hostCall(i, R, ctx, .{ a, b, c, d }); + 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"), @@ -1202,9 +1213,10 @@ 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 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, 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) { @@ -1214,7 +1226,7 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype 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. @@ -1226,7 +1238,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 -- cgit v1.3 From 866e9ed6762f42a32907d63d783464f2c90d5b85 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 01:12:56 -0300 Subject: tape: Effect.perform's 20 calls out to the host and pump's 8 through the tape's one call, step 6b (reports/pardes-handle-69-design.md): each arm is p.tape.host(p, .name, .{ args }) with its absent arm after orelse (nothing, the in-process fallback, an answer at once, a message), Effect's two replaying() are the handle's, and the pump's other view presents through a four-line presentTaped so the present stays inside views.within; a new core test pumps a core twice with one host and once with another (the mask said at the first and the third, the clock, watch and present at each, a replica replaying the three units to the same digest); the tape's bytes do not move (semantics 32640d4a722b811c unmoved, cross_replay both ways) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 33 ++++++++++++++++------------ core/Effect.zig | 62 ++++++++++++++++++++++------------------------------ core/tape.zig | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 50 deletions(-) (limited to 'core') diff --git a/core/Core.zig b/core/Core.zig index db0c5794..3fc5b192 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1466,25 +1466,22 @@ pub fn animationActive(p: *const Pardes) bool { } pub fn pump(p: *Pardes, host: Host) !void { - // By hand, not `taped`: the host is set before the enter, which wraps it, and is no input. + // By hand, not `taped`: the host is set before the enter, which says it if it is another, and is no input. p.host = host; const taped = pardes.tape.enter(p, .pump); defer pardes.tape.leave(p, taped); - // The host as the tape sees it, while it records. - const h = p.host; - const v = h.vtable; - if (v.wait_input) |f| { + if (p.host.has(.wait_input)) { // Asleep until input or until the core's next deadline // (nextWake); 0 is no timeout at all. // The clock read whether or not it is wanted: what a replica // waits on is its own (a picture decoding wakes only a frame // that draws it), and the tape has the same calls either way. - const now = if (v.now) |clock| clock(h.ctx) else p.clock.now_ns; + const now = p.tape.host(p, .now, .{}) orelse p.clock.now_ns; const timeout: u32 = if (p.nextWake()) |due| wake: { const ms = ((due -| now) + std.time.ns_per_ms - 1) / std.time.ns_per_ms; break :wake @intCast(std.math.clamp(ms, 1, std.math.maxInt(u32))); } else 0; - f(h.ctx, timeout); + _ = p.tape.host(p, .wait_input, .{timeout}); } // Detached, nobody's key dismisses a message: they go on the clock, // here in the step, where the clock read is taped (the host's wait @@ -1492,46 +1489,54 @@ pub fn pump(p: *Pardes, host: Host) !void { // go). Messages.expireUnattended(p); p.frames.mark_start = FrameStats.nowNs(); - if (v.now) |clock| p.advance(clock(h.ctx)); + if (p.tape.host(p, .now, .{})) |now| p.advance(now); p.settle(); // A quitting frame has already freed what it would draw. if (p.quit) return; - if (v.poll_frame) |f| f(h.ctx); + _ = p.tape.host(p, .poll_frame, .{}); // Nothing to draw: do not spend a frame, and above all do not block on // the display for one. This is what keeps the frame rate governing // rendering and nothing else. // A shell with a clock draws what `advance` stepped; one without // steps its own `.tick`s and draws while a deadline is due. - if (!p.needs_frame and (v.now != null or !p.animationActive())) return p.frames.idle(); + if (!p.needs_frame and (p.host.has(.now) or !p.animationActive())) return p.frames.idle(); // No one to see it: the frame stays owed (needs_frame), not drawn. // A pty streaming into an unwatched session spent most of its time // drawing what nobody looked at. - if (v.watched) |f| if (!f(h.ctx)) return; + if (!(p.tape.host(p, .watched, .{}) orelse true)) return; // The one view this process shows (a replica client's is its own // process's to draw), drawn as it is if another's is live. // Not shown by the frontend (Host.present): owed. var skipped = false; const shown = Views.shown(p); if (shown != p.live) { - if (shown) |k| skipped = !try Views.drawOne(p, k, .{ .ctx = h.ctx, .f = v.present }); + if (shown) |k| skipped = !try Views.drawOne(p, k, .{ .ctx = p, .f = presentTaped }); } else { _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept }); p.frames.mark_update = FrameStats.nowNs(); p.frames.view = if (p.panes[p.view().active]) |pane| pane.scroll() else 0; const surface = try p.render(p.frame_arena.allocator()); p.frames.mark_render = FrameStats.nowNs(); - if (v.present) |f| skipped = !f(h.ctx, surface); + skipped = !(p.tape.host(p, .present, .{surface}) orelse true); Stream.askAhead(p); } // Settled before post_present: a shell ticks there, and what that // tick moves owes the next frame. p.needs_frame = skipped; - if (v.post_present) |f| f(h.ctx); + _ = p.tape.host(p, .post_present, .{}); // Still moving: the next frame is owed a period from now. const owed = if (p.nextWake()) |due| due <= p.clock.stepped_ns + frame_ns else false; p.frames.record(FrameStats.nowNs(), owed, skipped); } +/// The pump's other view presented as its own is, through the tape: +/// `present` stays inside `views.within`, whose live view the host's +/// `presented` reads. +fn presentTaped(ctx: ?*anyopaque, surface: *const pardes.Surface) bool { + const p: *Pardes = @ptrCast(@alignCast(ctx.?)); + return p.tape.host(p, .present, .{surface}) orelse true; +} + /// What a pump does between its wait and its frame: the queued inputs /// and the effects. Also the session at a step boundary, taken now (an /// Upgrade's dump, upgrade.zig). diff --git a/core/Effect.zig b/core/Effect.zig index 5e821065..a6fcc0e5 100644 --- a/core/Effect.zig +++ b/core/Effect.zig @@ -116,34 +116,33 @@ pub const perform = tape.taped(performInner); fn performInner(p: *Pardes, e: Effect) void { // A Save performed on one twin is every twin's (File.syncTwins). defer panes.File.syncTwins(p); - const v = p.host.vtable; switch (e) { // The host reads the pane (its command, its shell) as it forks, // so the slot must still hold the pane the spawn was for. - .spawn => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else if (v.spawn) |f| f(p.host.ctx, s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane.index()] orelse "") else s.cwd.slice()) else { + .spawn => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .spawn, .{ s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane.index()] orelse "") else s.cwd.slice() }) orelse { p.fallback.spawned[s.pane.index()] = true; }, // A pane with no child is silent: nothing invents output on its // screen, and the bytes are dropped rather than transcribed. - .write => |w| if (v.pty_write) |f| f(p.host.ctx, w.pane, w.bytes.slice()), - .resize_pty => |r| if (v.pty_resize) |f| f(p.host.ctx, r.pane, r.cols, r.rows), + .write => |w| _ = p.tape.host(p, .pty_write, .{ w.pane, w.bytes.slice() }), + .resize_pty => |r| _ = p.tape.host(p, .pty_resize, .{ r.pane, r.cols, r.rows }), // A signal for a pane that closed, its slot retaken in the same // step, would reach another pane's child. - .signal_pty => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else if (v.pty_signal) |f| f(p.host.ctx, s.pane, s.sig), - .close_pty => |c| if (v.close_pty) |f| f(p.host.ctx, c.pane), + .signal_pty => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .pty_signal, .{ s.pane, s.sig }) orelse {}, + .close_pty => |c| _ = p.tape.host(p, .close_pty, .{c.pane}), // The view's frontend opens it, yanks and pastes, on its own // machine (View.yanks); the host here only for a view no client // draws (tape.hostCall). .open_link => |u| { p.view().links +%= 1; p.view().link = u; - if (v.open_link) |f| f(p.host.ctx, u.slice()) else p.fallback.setLink(u.slice()); + p.tape.host(p, .open_link, .{u.slice()}) orelse p.fallback.setLink(u.slice()); }, // Plan 9's plumber's, detached (host_io.startDetached); in-process // (a test), kept to look at. .start => |st| { var why_buf: [64]u8 = undefined; - const why = if (v.start) |f| f(p.host.ctx, st.line.slice(), &why_buf) else p.fallback.start(st.line.slice()); + const why = p.tape.host(p, .start, .{ st.line.slice(), &why_buf }) orelse p.fallback.start(st.line.slice()); const w = why orelse return; var words = std.mem.splitScalar(u8, st.line.slice(), 0); _ = words.next(); // its directory @@ -191,37 +190,31 @@ fn performInner(p: *Pardes, e: Effect) void { }, .write_dump => { const out = p.dump_out orelse return; - if (v.write_dump) |f| { - f(p.host.ctx, out); - } else { + p.tape.host(p, .write_dump, .{out}) orelse { _ = p.fallback.writeFile(host_io.Fallback.dump_path, out); p.setLastDump(host_io.Fallback.dump_path); - } + }; }, .set_clipboard => { p.view().yanks +%= 1; const text = p.registers.text(p.gpa, '+') orelse ""; - if (v.set_clipboard) |f| f(p.host.ctx, text) else p.fallback.setClipboard(text); + p.tape.host(p, .set_clipboard, .{text}) orelse p.fallback.setClipboard(text); }, // No desktop to ask: answer from the in-process clipboard at once, // which is the same shape as a host answering later. .read_clipboard => { p.view().pastes +%= 1; - if (v.read_clipboard) |f| f(p.host.ctx) else p.update(.{ .paste = p.fallback.clipboard.items }); + p.tape.host(p, .read_clipboard, .{}) orelse p.update(.{ .paste = p.fallback.clipboard.items }); }, - .lsp => |q| if (v.lsp) |f| - f(p.host.ctx, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }) - else + .lsp => |q| p.tape.host(p, .lsp, .{.{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }}) orelse p.update(.{ .lsp_resp = .{ .id = q.id, .rows = "" } }), - .pipe => |q| if (v.pipe) |f| - f(p.host.ctx, q.id) - else + .pipe => |q| p.tape.host(p, .pipe, .{q.id}) orelse p.update(.{ .pipe_resp = .{ .id = q.id, .success = false, .outputs = &.{} } }), // No workers here (a test, a board): it runs on the loop. - .search => |q| if (v.search) |f| f(p.host.ctx, q.gen) else Stream.runHere(p, q.gen, false), + .search => |q| p.tape.host(p, .search, .{q.gen}) orelse Stream.runHere(p, q.gen, false), // No windows here (a test, the tty): it stays. - .hand_over => |h| if (v.hand_over) |f| { - if (p.handover.bytes) |bytes| f(p.host.ctx, h.seq, bytes); + .hand_over => |h| if (p.host.has(.hand_over)) { + if (p.handover.bytes) |bytes| _ = p.tape.host(p, .hand_over, .{ h.seq, bytes }); } else p.update(.{ .handed = .{ .seq = h.seq, .failure = "no other windows here" } }), .edit_write => { // Each write takes the bytes `w` took when it ran: the text @@ -247,24 +240,21 @@ fn performInner(p: *Pardes, e: Effect) void { } else ctlfs.events.noteLogAs(p, .save, pane, w.path); } }, - .watch => |w| if (v.watch_file) |f| { + .watch => |w| { // What a watch is about: a real file, or a kind's (a PDF's). const path: []const u8 = if (p.panes[w.pane.index()]) |pane| (if (pane.file) |file| (if (file.output == null) file.path else "") else if (pane.addon) |k| k.path else "") else ""; - f(p.host.ctx, w.pane, path, w.on, w.mode); - } else { - p.fallback.watched[w.pane.index()] = w.on; + p.tape.host(p, .watch_file, .{ w.pane, path, w.on, w.mode }) orelse { + p.fallback.watched[w.pane.index()] = w.on; + }; }, - .theme_file => |t| if (v.watch_theme) |f| f(p.host.ctx, t.generation, t.on), - .dump_themes => |d| if (v.dump_themes) |f| f(p.host.ctx, d.pane), - .detach => |d| if (v.detach) |f| - f(p.host.ctx) - else - p.reportError(d.pane, "detach", error.NotAttached), + .theme_file => |t| _ = p.tape.host(p, .watch_theme, .{ t.generation, t.on }), + .dump_themes => |d| _ = p.tape.host(p, .dump_themes, .{d.pane}), + .detach => |d| p.tape.host(p, .detach, .{}) orelse p.reportError(d.pane, "detach", error.NotAttached), // dlopen is the same on every host, so none has a method of its // own; a replica's core holds no library (what it brought comes in // the event this taped). - .addon => |a| if (!tape.replaying()) @import("Addon.zig").perform(p, a.op, a.pane.index(), a.arg.slice()), - .job => |j| if (v.job) |f| f(p.host.ctx, j.pane, j.serial, j.token) else if (!tape.replaying()) host_io.KindJob.runHere(p, j.pane.index(), j.serial, j.token), + .addon => |a| if (!p.tape.replaying()) @import("Addon.zig").perform(p, a.op, a.pane.index(), a.arg.slice()), + .job => |j| p.tape.host(p, .job, .{ j.pane, j.serial, j.token }) orelse if (!p.tape.replaying()) host_io.KindJob.runHere(p, j.pane.index(), j.serial, j.token), // the loop's own condition; a host tears down after its own loop .quit => p.endSession(), } @@ -282,7 +272,7 @@ pub fn writeChecked(p: *Pardes, id: u8, serial: u32, path: []const u8, bytes: [] if (filesystem.localPath(path)) |local| filesystem.makeDirs(p.tape, std.Io.Dir.path.dirname(local) orelse "/"); // The in-process filesystem reports the same way a real host does, // so an OOM there leaves the pane dirty rather than looking saved. - if (p.host.vtable.write_file) |f| f(p.host.ctx, .at(id), path, bytes) else if (!p.fallback.writeFile(path, bytes)) p.saveFailed(.at(id), path, error.OutOfMemory); + p.tape.host(p, .write_file, .{ .at(id), path, bytes }) orelse if (!p.fallback.writeFile(path, bytes)) p.saveFailed(.at(id), path, error.OutOfMemory); const pane = p.panes[id] orelse return null; if (pane.serial != serial) return null; if (p.fs.failures == failures) return pane; diff --git a/core/tape.zig b/core/tape.zig index b74915e5..18b5e296 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -1783,6 +1783,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"); -- cgit v1.3 From c77e5a09af27976b2375a902ecb8efa428395a0c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 02:02:19 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 6 +- core/Effect.zig | 2 +- core/File.zig | 2 +- core/Stream.zig | 8 +-- core/exec.zig | 2 +- core/replica.zig | 1 - core/root.zig | 15 +++-- core/tape.zig | 164 ++++++++++++++++++++----------------------------------- 8 files changed, 77 insertions(+), 123 deletions(-) (limited to 'core') diff --git a/core/Core.zig b/core/Core.zig index 3fc5b192..64eb7b02 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1466,11 +1466,11 @@ pub fn animationActive(p: *const Pardes) bool { } pub fn pump(p: *Pardes, host: Host) !void { - // By hand, not `taped`: the host is set before the enter, which says it if it is another, and is no input. + // By hand, not `taped`: the host is set before the enter, which says which functions it has if it is another, and is no input. p.host = host; const taped = pardes.tape.enter(p, .pump); defer pardes.tape.leave(p, taped); - if (p.host.has(.wait_input)) { + if (p.tape.has(p, .wait_input)) { // Asleep until input or until the core's next deadline // (nextWake); 0 is no timeout at all. // The clock read whether or not it is wanted: what a replica @@ -1499,7 +1499,7 @@ pub fn pump(p: *Pardes, host: Host) !void { // rendering and nothing else. // A shell with a clock draws what `advance` stepped; one without // steps its own `.tick`s and draws while a deadline is due. - if (!p.needs_frame and (p.host.has(.now) or !p.animationActive())) return p.frames.idle(); + if (!p.needs_frame and (p.tape.has(p, .now) or !p.animationActive())) return p.frames.idle(); // No one to see it: the frame stays owed (needs_frame), not drawn. // A pty streaming into an unwatched session spent most of its time // drawing what nobody looked at. diff --git a/core/Effect.zig b/core/Effect.zig index a6fcc0e5..cabfe8ea 100644 --- a/core/Effect.zig +++ b/core/Effect.zig @@ -213,7 +213,7 @@ fn performInner(p: *Pardes, e: Effect) void { // No workers here (a test, a board): it runs on the loop. .search => |q| p.tape.host(p, .search, .{q.gen}) orelse Stream.runHere(p, q.gen, false), // No windows here (a test, the tty): it stays. - .hand_over => |h| if (p.host.has(.hand_over)) { + .hand_over => |h| if (p.tape.has(p, .hand_over)) { if (p.handover.bytes) |bytes| _ = p.tape.host(p, .hand_over, .{ h.seq, bytes }); } else p.update(.{ .handed = .{ .seq = h.seq, .failure = "no other windows here" } }), .edit_write => { diff --git a/core/File.zig b/core/File.zig index 75d8049b..325abee9 100644 --- a/core/File.zig +++ b/core/File.zig @@ -1579,7 +1579,7 @@ pub fn refreshHighlights(p: *Pardes) void { // itself on the frame, a third of the work; the stretch around it // comes painted ahead (paintAhead) where there are workers. const far = f.highlights.len == 0 or need_end <= f.highlight_start or need_start >= f.highlight_start + f.highlights.len; - const slack: usize = if (far and p.host.vtable.search != null) 0 else if (f.highlights.len == 0) 0 else pane.rows; + const slack: usize = if (far and p.tape.has(p, .search)) 0 else if (f.highlights.len == 0) 0 else pane.rows; const whole = pane.colorAlgo() == .locations or f.tree_context; // Painted ahead on a worker (Stream.paintAhead): taken, no parse. if (!whole and pane.colorAlgo() == .source) if (Stream.takeAhead(p, pane.serial, f.revision, need_start, need_end)) |got| { diff --git a/core/Stream.zig b/core/Stream.zig index dae61a8b..6f99b504 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -311,7 +311,7 @@ pub fn takeAhead(p: *Pardes, serial: u32, revision: u32, need_start: usize, need /// workers, and only for a grammar a worker can paint (syntax.readyApart). pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, content: []const u8, from: usize, to: usize) void { const s = &p.stream; - if (p.host.vtable.search == null or p.fs.serving or to <= from or to > content.len) return; + if (!p.tape.has(p, .search) or p.fs.serving or to <= from or to > content.len) return; const a = s.ahead_for; if (s.ahead.len != 0 and a.serial == serial and a.revision == revision and from >= a.start and to <= a.start + s.ahead.len) return; // A stretch bigger than this (a few lines of a minified file) is not @@ -355,7 +355,7 @@ pub fn askAhead(p: *Pardes) void { /// one revision of a file. pub fn paintRows(p: *Pardes, path: []const u8) void { const s = &p.stream; - if (s.rows_paint != null or p.host.vtable.search == null) return; + if (s.rows_paint != null or !p.tape.has(p, .search)) return; // Asked while a 9P write is served (a script's Grep): not now, as no // paint is (the write would wait for it to settle); the next step asks. if (p.fs.serving) { @@ -452,7 +452,7 @@ pub fn parseFor(p: *Pardes, id: usize) void { s.parse_next = .{ .serial = pane.serial, .revision = f.revision }; return; } - const workers = p.host.vtable.search != null and !p.fs.serving; + const workers = p.tape.has(p, .search) and !p.fs.serving; if (!workers and f.content.len > 64 << 10) return; const job = TextJob.make(p.gpa, pane.serial, f.revision, 0, f.path, f.content) orelse return; s.gen +%= 1; @@ -559,7 +559,7 @@ pub fn step(p: *Pardes) void { s.state = .searching; // A script's write (whose answer says whether it found anything) and a // host with no workers: done before this returns. - if (p.host.vtable.search != null and !p.fs.serving) p.emit(.{ .search = .{ .gen = s.gen } }) else runHere(p, s.gen, false); + if (p.tape.has(p, .search) and !p.fs.serving) p.emit(.{ .search = .{ .gen = s.gen } }) else runHere(p, s.gen, false); } /// When the core next needs a tick for the stream: the end of the pause. diff --git a/core/exec.zig b/core/exec.zig index 197827bb..36879b70 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -418,7 +418,7 @@ fn refusedPlacement(p: *Pardes, id: usize) bool { /// Whether a shell would be started in `dir` and `dir` is not there: asked /// only of a host that starts shells, and only of a local path. pub fn missingDir(p: *Pardes, dir: []const u8) bool { - if (p.host.vtable.spawn == null) return false; + if (!p.tape.has(p, .spawn)) return false; // A peer's directory (`/n/peer/x`) is no directory on this host, where // the shell would start: no command or terminal runs there, and none // is made to say `exit 127`. diff --git a/core/replica.zig b/core/replica.zig index 3aeb21af..559ae8f5 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -2426,7 +2426,6 @@ pub const Peers = struct { if (!ps.recording) return; pardes.panes.resizeDeferred(core); ps.screens.deinit(core); - if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; tape.on = null; core.tape = core.opts.tape orelse &tape.none; tape.release(&ps.t); diff --git a/core/root.zig b/core/root.zig index 632385a0..5130cde8 100644 --- a/core/root.zig +++ b/core/root.zig @@ -599,10 +599,13 @@ test "nothing of a core is a process global: every container-level var in core/ /// The lines of each file of core/ that name the machine outside a test: /// `std.c.`, `libc.`, `std.posix.`, `getenv`, std.Io's File, Dir, net and -/// Threaded (a path's text, `Dir.path`, and `max_path_bytes` are none), and -/// `@import("dev")` and `@import("runtime")`, by file, how many and why. A -/// step's read of its own machine with no tape diverges a far client, and -/// the soak (one machine) never sees it: a new line here makes someone look. +/// Threaded (a path's text, `Dir.path`, and `max_path_bytes` are none), +/// `@import("dev")` and `@import("runtime")`, and the host's table +/// (`host.vtable`, `host.ctx`: only the tape's own lines call the host, a +/// call made past it is no call a replica replays), by file, how many and +/// why. A step's read of its own machine with no tape diverges a far +/// client, and the soak (one machine) never sees it: a new line here makes +/// someone look. /// The count is a file's, so it cannot tell a step's read from its host /// half's; the reason says which the file's are. const machine_lines = [_]struct { []const u8, usize, []const u8 }{ @@ -632,7 +635,7 @@ const machine_lines = [_]struct { []const u8, usize, []const u8 }{ .{ "rollback.zig", 1, "a test helper's directory" }, .{ "root.zig", 4, "dev's modules re-exported (host_io, file_watch, glide, fonts)" }, .{ "selection_pipe.zig", 14, "a pipe's child processes and their files: the host's `pipe` entry" }, - .{ "tape.zig", 11, "the tape's own files and host_io's types: a recording, a replay; the `io` a tape hands a site's body" }, + .{ "tape.zig", 12, "the tape's own files and host_io's types: a recording, a replay; the `io` a tape hands a site's body; which host the core holds (noteHost), the one file that names the host's table" }, .{ "lsp/lsp.zig", 1, "the language server's client (dev): the host's `lsp` entry" }, .{ "ninep/ctl.zig", 1, "the session's pid in a 9P read: the session's server" }, .{ "ninep/events.zig", 2, "the 9P event log's clock: the session's server" }, @@ -644,7 +647,7 @@ const machine_lines = [_]struct { []const u8, usize, []const u8 }{ /// The lines of `src` that name the machine (`machine_lines`'s rules). fn machineLines(src: []const u8, gpa: std.mem.Allocator, out: *std.ArrayList(usize)) !void { - const names = [_][]const u8{ "std.c.", "libc.", "std.posix.", "getenv", "std.Io.File", "std.Io.Dir", "std.Io.net", "std.Io.Threaded", "Io.File", "Io.Dir", "Io.net", "Io.Threaded" }; + const names = [_][]const u8{ "std.c.", "libc.", "std.posix.", "getenv", "std.Io.File", "std.Io.Dir", "std.Io.net", "std.Io.Threaded", "Io.File", "Io.Dir", "Io.net", "Io.Threaded", "host.vtable", "host.ctx" }; const pure = [_][]const u8{ "std.Io.Dir.path.", "Io.Dir.path.", "std.Io.Dir.max_path_bytes", "Io.Dir.max_path_bytes" }; var open: std.ArrayList(u8) = .empty; defer open.deinit(gpa); 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 `: Args()` 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; -- cgit v1.3 From 8fb080c6208bccf9ef12eca630b0534312ae5fe7 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 03:10:38 -0300 Subject: tape: the 7 free calls the same under either terminal design on the handle, step 8a (reports/pardes-handle-69-design.md section 4): a terminal's output, its close (the poison and the refusal) read p.tape, the refusal to open one moves out of createTerminal into its two callers (newShell, newCommand: no parameter, no test changed), and the session's fonts and its shell checks (exec's Font and Shell, a 9P exec) are p.tape.site; the 3 the bytes would delete and the 11 that wait are left; the tape's bytes do not move (semantics 32640d4a722b811c unmoved, cross_replay both ways) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/exec.zig | 4 ++-- core/ninep/pty.zig | 2 +- core/panes.zig | 10 ++++++---- 3 files changed, 9 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/exec.zig b/core/exec.zig index 36879b70..74037db0 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -1018,7 +1018,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Settings.Setting, arg: ?[ const spec = config.Settings.FontSpec.parse(arg orelse return) orelse return; // The session's machine's fonts, a question to its disk: a // client's replay takes the session's answer, never its own. - const found = pardes.tape.site(.fonts, p.scratch.allocator(), fontNamed, .{ p.scratch.allocator(), spec.name }); + const found = p.tape.site(.fonts, p.scratch.allocator(), fontNamed, .{ p.scratch.allocator(), spec.name }); const nul = std.mem.indexOfScalar(u8, found, 0) orelse return; if (!p.settings.requestFont(found[0..nul], found[nul + 1 ..], spec.size_hundredths)) return; p.font_request_taken = false; @@ -1035,7 +1035,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Settings.Setting, arg: ?[ return p.setMessage(.at(v.active), said.print("Shell: the default again, {s} ($SHELL, else /bin/sh)", .{p.sessionShell()[0..@min(p.sessionShell().len, 256)]})); } var why: [320]u8 = undefined; - if (pardes.tape.site(.shell, null, @import("dev").host_io.Shell.refusal, .{ want[0..@min(want.len, 200)], &why })) |refused| { + if (p.tape.site(.shell, null, @import("dev").host_io.Shell.refusal, .{ want[0..@min(want.len, 200)], &why })) |refused| { var text: pardes.Kind.Buf(340) = .{}; return p.reportFailure(v.active, text.print("Shell: {s}", .{refused})); } diff --git a/core/ninep/pty.zig b/core/ninep/pty.zig index 26bb7661..938f3366 100644 --- a/core/ninep/pty.zig +++ b/core/ninep/pty.zig @@ -66,7 +66,7 @@ pub fn writeCtl(p: *Pardes, req: Req, id: usize) Reply { // interpreter is not: refused, and the running shell kept. const bin = pane.shell orelse p.shellBin(); var why: [320]u8 = undefined; - if (pardes.tape.site(.shell, null, @import("dev").host_io.Shell.refusal, .{ bin[0..@min(bin.len, 200)], &why })) |refused| + if (p.tape.site(.shell, null, @import("dev").host_io.Shell.refusal, .{ bin[0..@min(bin.len, 200)], &why })) |refused| return tree.failText(req.tag, E.NOENT, std.fmt.bufPrint(&p.fs.ename, "exec: {s}", .{refused[0..@min(refused.len, 300)]}) catch &p.fs.ename); // cut to ename's 320 }; if (verb(p, id, line, apply)) |why| return tree.failText(req.tag, E.INVAL, why); diff --git a/core/panes.zig b/core/panes.zig index 72ca3eea..7eb6cad2 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -709,7 +709,7 @@ pub const Pane = struct { pub fn feedOutput(pane: *Pane, p: *Pardes, bytes: []const u8) void { // There are no pty reads at all without an emulator to parse them into. const state = pane.terminal orelse return; - if (tape.authorityOnly("a terminal's output")) return; + if (p.tape.authorityOnly("a terminal's output")) return; const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pane.pointerRowCount() > 0; @@ -1133,7 +1133,6 @@ pub fn createDoc(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { /// A terminal pane: a pane and its emulator. Slot ownership, serial /// assignment and spawn effects remain core lifecycle invariants. pub fn createTerminal(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { - if (tape.authorityOnly("open a terminal")) return error.OutOfMemory; const pane = try createDoc(gpa, cols, rows); errdefer gpa.destroy(pane); pane.terminal = try Term.open(emulatorGpa(gpa), cols, rows); @@ -1196,7 +1195,7 @@ pub fn retirePane(p: *Pardes, pane: *Pane) void { if (pane.isTerminal()) p.emit(.{ .close_pty = .{ .pane = .at(id) } }); // Its pty's end and its emulator are the authority's: a replica's // speculation that closes a terminal waits for it. - if (pane.terminal != null) tape.poison("close a terminal"); + if (pane.terminal != null) p.tape.poison("close a terminal"); ctlfs.events.noteRetire(p, pane); // ...and so do bytes still queued for the pty it no longer has. p.dropPendingWrite(id); @@ -1237,7 +1236,7 @@ pub fn teardownPane(p: *Pardes, pane: *Pane) void { } // A pane with none (a file's, a picker's, a preview's) closes in a // speculation as anywhere. - if (pane.terminal) |state| if (!tape.authorityOnly("close a terminal")) { + if (pane.terminal) |state| if (!p.tape.authorityOnly("close a terminal")) { Term.close(state, p.gpa); pane.terminal = null; }; @@ -1273,6 +1272,8 @@ pub fn installPane(p: *Pardes, id: usize, pane: *Pane) void { pub fn newShell(p: *Pardes, id: usize, cwd: []const u8) !*Pane { std.debug.assert(p.panes[id] == null); if (cwd.len > limits.host_path_cap) return error.PathTooLong; + // A speculation opens no emulator: a terminal is the authority's to open. + if (p.tape.authorityOnly("open a terminal")) return error.OutOfMemory; const pane = try panes.createTerminal(p.gpa, p.view().screen_w, p.view().screen_h); // Named by the directory it is started in from the first, so the // shell saying it is there is no rename (`new N /` then `rename`); @@ -1305,6 +1306,7 @@ pub fn newCommand(p: *Pardes, id: usize, cwd: []const u8, line: []const u8) !*Pa if (cwd.len > limits.host_path_cap) return error.PathTooLong; const owned = try p.gpa.dupe(u8, line); errdefer p.gpa.free(owned); + if (p.tape.authorityOnly("open a terminal")) return error.OutOfMemory; const pane = try panes.createTerminal(p.gpa, p.view().screen_w, p.view().screen_h); pane.command = owned; pane.command_pty = true; -- cgit v1.3