diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 23:57:53 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-18 00:29:40 -0300 |
| commit | 137be286869241d19ea60ecb7737957d861162d7 (patch) | |
| tree | 854878c3a2152195b95d6b469c011db5a0d7e6a7 /src | |
| parent | 9b20d00ca5ffbab640b87249d65e4165414d22c1 (diff) | |
| download | pardes-137be286869241d19ea60ecb7737957d861162d7.tar.gz pardes-137be286869241d19ea60ecb7737957d861162d7.zip | |
kinds: a PA_PLAIN kind runs in the client that shows it (the games and veil): its head says its whole state is the one block its open allocates, plain bytes changed only by its calls, each a function of the block and what the call is handed (no statics, clock, randomness or file of its own; tick is not told PA_FRAMED); the core keeps the block (Kind.Slot.plain, the core's memory, a client's rollback epoch, which the copy saves) and its hash after each call is the call's taped outcome and in the digest (Slot.state), so a client whose block came out otherwise is caught at that call, joins again, and runs the session's frames of it from then (Addon.distrust); a client loads its own code for the kind by name, version, ABI and functions (Addon.plainOps; its scan only when the session has one), opens each such pane from the session's block byte for byte (dump.plainHere; the block is the dump's record, the kind's load given it at a Restore alone), speculates its keys, mouse and the tick after them, and draws it; one without the addon draws a line saying so; the session sends no Shadow for these panes; snake, life, snake_shader and veil say PA_PLAIN; test/liar.zig, a kind whose tick reads the clock; test/daemon.py --games: snake and life focused in two clients while a third types text for 30 s, keys stopped 0 and 0 of 451 and 450 (the base: 33 of 36 stopped in a tty client), rollbacks 385, 345, 366, every digest agreed, and the liar caught at its tick, its client joined again once and agreed; the GPU snake focused in a window client (offscreen) of a terminal's session and of a window's steps every 200 ms (2, 3, 4, 5, 7, 8, 9, 10, 12, 13), keys stopped 0 of 27, its digest agreed; the snake_shader's load tidying hover and timers made a client's block another than the session's at the first tick, caught so, hence the block itself in the dump; running.typ says a plain addon runs in a client; perf --only medium --reps 10 instructions:u 2,459.25M (base) -> 2,459.23M, --terminal-mib 8 3,928.50M -> 3,928.50M; tui core-test 811/950 (139 skipped), shell-test 993/996, pane-test 180/181, replicas pass (diverged 0, checked/wrong 0), behaviour-test 269/273 (the four addon ABI tests the base fails), --replay 20 seeds 0 failures, addon-test 157/157 steps 5/5, process-test 2 of 144 failed (lspcomplete and lspcompletemoved, the base's; winch failed once under load and passes alone), test/daemon.py, --far and --games pass; gui unit-test 1582/1590 (the base's), the frame corpus 77/77 against mbase, test/daemon.py --window-clients passes (its PDF window case skipped: no pdf.so built)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src')
| -rw-r--r-- | src/Addon.zig | 33 | ||||
| -rw-r--r-- | src/Kind.zig | 98 | ||||
| -rw-r--r-- | src/dump.zig | 37 | ||||
| -rw-r--r-- | src/kinds/addon.zig | 6 | ||||
| -rw-r--r-- | src/kinds/pardes_addon.h | 14 | ||||
| -rw-r--r-- | src/replica.zig | 12 | ||||
| -rw-r--r-- | src/tape.zig | 6 |
7 files changed, 174 insertions, 32 deletions
diff --git a/src/Addon.zig b/src/Addon.zig index 80d8fdd8..a2c46b35 100644 --- a/src/Addon.zig +++ b/src/Addon.zig @@ -174,6 +174,7 @@ var found: [Kind.max_kinds]Loaded = undefined; var found_words: [Kind.max_kinds][64]LoadedWord = undefined; var found_text: [Kind.max_kinds][2][4096]u8 = undefined; var nfound: usize = 0; +var scanned = false; /// The addons compiled in (`zig build static`, and any build off Linux): /// the image and pdf modules' `pardes_addon` declarations, which only a @@ -191,6 +192,8 @@ else /// library of the two directories, in name order each, the user's first. A /// library that crashed at its last load is listed, not loaded. pub fn scan() []const Loaded { + if (scanned) return found[0..nfound]; + scanned = true; for (compiled_in) |ops| { found[nfound] = loadedOf(ops, "builtin", @splat(0), &found_words[nfound]); if (ops.init) |init| if (init(&Kind.api) != Kind.c.PA_OK) { @@ -365,10 +368,37 @@ pub fn loadedOf(ops: *const Kind.Ops, path: []const u8, hash: [32]u8, words: *[6 .address = ops.address != null, .view = ops.view != null, .pictures = h.flags & Kind.c.PA_PICTURES != 0, + .plain = h.flags & Kind.c.PA_PLAIN != 0, }, }; } +/// This process's code for a kind a session's core lists, which a client +/// runs itself: PA_PLAIN, of the same name, version, ABI and functions (a +/// compiled-in addon's library hash is 0: never by the hash), and not +/// caught breaking its promise here (`distrust`). Null: its panes say so. +pub fn plainOps(e: *const Kind.Entry) ?*const Kind.Ops { + if (!e.caps.plain or e.refused) return null; + // A client scans for its addons only when its session has a plain kind. + _ = scan(); + for (distrusted[0..ndistrusted]) |*d| if (std.mem.eql(u8, std.mem.sliceTo(d, 0), e.nameOf())) return null; + for (found[0..nfound]) |l| if (l.why.len == 0 and std.mem.eql(u8, l.name, e.nameOf()) and l.version == e.version and + l.abi == e.abi and @as(u16, @bitCast(l.caps)) == @as(u16, @bitCast(e.caps))) return @ptrFromInt(l.ops); + return null; +} + +/// Kinds a client found breaking PA_PLAIN: shown by the session's line +/// from its next join on. +var distrusted: [Kind.max_kinds][16]u8 = undefined; +var ndistrusted: usize = 0; + +pub fn distrust(name: []const u8) void { + if (ndistrusted == distrusted.len) return; + distrusted[ndistrusted] = @splat(0); + @memcpy(distrusted[ndistrusted][0..@min(15, name.len)], name[0..@min(15, name.len)]); + ndistrusted += 1; +} + /// The kind that opens `path`, by its extension (any case). pub fn forPath(r: *const Kind.Registry, path: []const u8) ?u8 { const dot = std.mem.lastIndexOfScalar(u8, path, '.') orelse return null; @@ -411,7 +441,8 @@ pub fn take(r: *Kind.Registry, l: Loaded, except: []const u8) ?[]const u8 { return e.why.slice(); } e.why.set("loaded"); - r.ops[r.n] = if (tape.replaying()) null else @ptrFromInt(l.ops); + // A client's: its own code for a plain kind, or none (the session's). + r.ops[r.n] = if (tape.replaying()) plainOps(e) else @ptrFromInt(l.ops); for (l.words) |w| { r.words[r.nwords] = .{ .kind = r.n, .arg = w.arg, .opens = w.opens }; r.words[r.nwords].name.set(w.name); diff --git a/src/Kind.zig b/src/Kind.zig index 04db2579..14a3fc18 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -5,14 +5,16 @@ //! (`Registry`, what libraries brought, with their words and extensions), //! and a pane's `Slot`, the state the core keeps round the kind's own. //! -//! The core is deterministic and a kind is only the authority's: a -//! replica's core has the same registry (its words, its extensions) but no -//! code for a kind a library brought, and draws its panes from the -//! authority's frames (a Shadow, screen_state.zig). So every call into a -//! kind whose answer the core acts on is made at a place both cores reach, -//! and its `Outcome` (the answer, what it said, a Look) is taped: a replica -//! takes it off the tape. Paint and the reads (body, ctl, a dump) change -//! nothing and are not taped. +//! The core is deterministic and a kind is the authority's: a replica's +//! core has the same registry (its words, its extensions) but no code for a +//! kind a library brought, and draws its panes from the authority's frames +//! (a Shadow, screen_state.zig); but a PA_PLAIN kind, whose state is one +//! plain block the core keeps (`Slot.plain`), runs in a client that has it, +//! as the session runs it. So every call into a kind whose answer the core +//! acts on is made at a place both cores reach, and its `Outcome` (the +//! answer, what it said, a Look, a plain kind's state's hash) is taped: a +//! replica takes it off the tape. Paint and the reads (body, ctl, a dump) +//! change nothing and are not taped. const std = @import("std"); const builtin = @import("builtin"); pub const c = @import("pardes_addon"); @@ -61,7 +63,9 @@ pub const Caps = packed struct(u16) { view: bool = false, /// PA_PICTURES: its tag says how its pictures show, from the first frame. pictures: bool = false, - pad: u6 = 0, + /// PA_PLAIN: its state is its block, which the core keeps (`Slot.plain`). + plain: bool = false, + pad: u5 = 0, }; pub const Entry = struct { @@ -133,7 +137,7 @@ pub const Slot = struct { // jobs' inputs, its file's hash, a placeholder's record), and what // a frame makes (the thumb, the pointer, whether one was drawn: a // view's, never a step's). - .local = .{ "ops", "st", "gpa", "arena", "opened", "reloads", "jobs", "tokens", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "shadow", "used" }, + .local = .{ "ops", "st", "gpa", "arena", "plain", "opened", "reloads", "jobs", "tokens", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "shadow", "used" }, }; /// Its kind's code here; null in a placeholder (`live` false: its kind /// not in the registry, its record kept for the day it comes) or a @@ -141,6 +145,14 @@ pub const Slot = struct { ops: ?*const Ops = null, live: bool = false, st: ?*anyopaque = null, + /// A PA_PLAIN kind's whole state, the one block its open allocated + /// (empty until it has): the core's memory (a client's rollback copy + /// saves it), whose hash is `state`. Null for another kind. + plain: ?[]align(16) u8 = null, + /// The hash of `plain` after the kind's last call, as the session's + /// was (the call's outcome, on the tape): in the digest, so a client + /// whose block came out otherwise is caught at that call. + state: u64 = 0, /// The kind's allocations are its arena's, freed at the close. gpa: std.mem.Allocator, arena: std.heap.ArenaAllocator, @@ -223,6 +235,7 @@ pub const Slot = struct { s.gpa.free(s.selection); if (checked) s.used.deinit(std.heap.page_allocator); if (s.shadow) |sh| sh.deinit(s.gpa); + if (s.plain) |b| if (b.len > 0) s.gpa.free(b); s.arena.deinit(); s.gpa.free(s.path); if (s.record) |r| s.gpa.free(r); @@ -241,8 +254,9 @@ fn caps(p: *const Pardes, s: *const Slot) Caps { /// word opened) from `bytes`: made whatever the kind says, with what it /// refused in `slot.refused` for the caller to say. pub fn open(p: *Pardes, id: usize, k: u8, path: ?[]const u8, bytes: ?[]const u8) !*Pane { - const gpa = p.image_gpa; const e = &p.kinds.kinds[k]; + // A plain kind's slot and block are the core's (a client's epoch). + const gpa = if (e.caps.plain) p.gpa else p.image_gpa; const s = try gpa.create(Slot); s.* = .{ .ops = p.kinds.ops[k], .live = true, .gpa = gpa, .arena = .init(gpa), .version = e.version, .gen = e.gen, .name = e.name }; errdefer { @@ -279,6 +293,11 @@ pub fn reopen(p: ?*Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool { var cl: Call = .{ .p = p, .id = id, .slot = s }; const prev = cl.begin("open"); if (p) |core| if (core.kinds.byName(s.nameOf())) |k| { + // Its block made afresh by this open. + if (core.kinds.kinds[k].caps.plain) { + if (s.plain) |b| if (b.len > 0) s.gpa.free(b); + s.plain = &.{}; + } var w: std.Io.Writer = .fixed(&s.tail.bytes); w.writeAll(" Term") catch {}; for (core.kinds.words[0..core.kinds.nwords]) |*kw| if (kw.kind == k and kw.opens == 0) w.print(" {s}", .{kw.name.slice()}) catch {}; @@ -386,9 +405,14 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa const fg: surface.Color = if (th.fg) |f| .{ .rgb = f } else .default; const bg: surface.Color = if (th.bg) |b| .{ .rgb = b } else .default; var frame: Frame = .{ .cols = cols, .rows = rows }; - if (slot.ops == null and slot.live) { + if (slot.ops == null and slot.live and !caps(p, slot).plain) { const sh = slot.shadow orelse return; frame = .{ .cells = sh.cells, .cols = sh.cols, .rows = sh.rows, .places = sh.places.items, .n = @intCast(sh.places.items.len), .gpu = sh.gpu }; + } else if (slot.ops == null and slot.live) { + // A plain kind runs where it is shown, and this client lacks it. + var line: [160]u8 = undefined; + _ = s.print(x, y, cols, std.fmt.bufPrint(&line, "addon {s} {d} does not run in this client: it is not in its addons, or it broke its promise here", .{ slot.nameOf(), slot.version }) catch &line, .{ .fg = fg, .bg = bg }); + return; } else if (slot.ops == null) { var line: [160]u8 = undefined; const why = if (slot.refused.len > 0) slot.refused.slice() else "Addon load <path>"; @@ -530,7 +554,7 @@ fn prefixOf(p: *const Pardes, slot: *const Slot, buf: *[400]u8) []const u8 { pub fn key(p: *Pardes, id: usize, k: pardes.Key) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; if (!caps(p, slot).key) return false; - if (tape.authorityOnly("a key into a kind's pane")) return true; + if (!runsHere(slot) and tape.authorityOnly("a key into a kind's pane")) return true; const pk: c.pa_key = .{ .cp = k.cp, .ctrl = @intFromBool(k.ctrl), .alt = @intFromBool(k.alt), .shift = @intFromBool(k.shift), .view = p.live + 1 }; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("key"); @@ -587,7 +611,7 @@ pub fn takesMouse(p: *const Pardes, s: *const Slot) bool { pub fn mouse(p: *Pardes, id: usize, m: pardes.Mouse, col: u16, row: u16) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; if (!caps(p, slot).mouse) return false; - if (tape.authorityOnly("the mouse in a kind's pane")) return true; + if (!runsHere(slot) and tape.authorityOnly("the mouse in a kind's pane")) return true; const pm: c.pa_mouse = .{ .button = switch (m.button) { .left => c.PA_LEFT, @@ -627,13 +651,13 @@ pub fn mouse(p: *Pardes, id: usize, m: pardes.Mouse, col: u16, row: u16) bool { /// units of the session's origin, which a replica replays (17.2). pub fn step(p: *Pardes, due: bool) void { const now = p.clock.stepped_ns; - // A kind's call is the authority's alone: a speculation's tick after a - // call into the pane waits with that call's input (it stopped there). - if (tape.speculating()) { - for (p.panes) |pslot| if (pslot) |pane| if (pane.addon) |slot| if (slot.next_tick == 1) return tape.poison("a kind's tick"); - return; - } + // A kind's call is the authority's alone but a plain kind's here: a + // speculation's tick after a call into the pane waits with that call's + // input (it stopped there). + const speculating = tape.speculating(); + if (speculating) for (p.panes) |pslot| if (pslot) |pane| if (pane.addon) |slot| if (slot.next_tick == 1 and !runsHere(slot)) return tape.poison("a kind's tick"); for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |slot| { + if (speculating and !runsHere(slot)) continue; if (due and caps(p, slot).view) for (0..views.max) |k| { var v = viewOf(p, @intCast(k), id); if (seen(&v) == slot.views[k]) continue; @@ -655,7 +679,7 @@ pub fn step(p: *Pardes, due: bool) void { var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("tick"); // A frame drawn since its last tick: a fling's frames are counted by them. - const flags = (if (focused) c.PA_FOCUSED else 0) | (if (slot.painted) c.PA_FRAMED else 0); + const flags = (if (focused) c.PA_FOCUSED else 0) | (if (slot.painted and slot.plain == null) c.PA_FRAMED else 0); slot.painted = false; // The clock's now for a tick after an event (a key, a focus): the // stepped frame lags it while the next wake is still to come. @@ -672,6 +696,12 @@ pub fn step(p: *Pardes, due: bool) void { }; } +/// A plain kind with its code here: a client runs it as the session does, +/// its calls speculated as the text's are. +fn runsHere(slot: *const Slot) bool { + return slot.plain != null and slot.ops != null; +} + /// Whether some view's facts are not what its kind was told. pub fn viewOwed(p: *const Pardes, slot: *const Slot, id: usize) bool { if (caps(p, slot).view) for (0..views.max) |k| if (seen(&viewOf(p, @intCast(k), id)) != slot.views[k]) return true; @@ -859,6 +889,8 @@ pub const Outcome = struct { rows: []const u8 = "", /// A tick's: the start of its tag (pa_tick.prefix). prefix: []const u8 = "", + /// A plain kind's block's hash after the call (Slot.state). + state: u64 = 0, /// Its `where` after the call (a key, a view, a job's answer may move /// it); null for a refused call. where: ?[]const u8 = null, @@ -913,8 +945,18 @@ const Call = struct { var place: [32]u8 = undefined; // A refused open closed its state: no `where` to ask. const at: ?[]const u8 = if (cl.refused.len == 0) where(cl.slot, &place) else null; - const o = tape.value(.addon, same, .{Outcome{ .ret = cl.ret, .text = cl.refused.slice(), .said = cl.said.slice(), .err = cl.err, .look = cl.look.slice(), .pane_name = cl.pane_name.slice(), .pane_text = cl.pane_text, .exec = cl.exec.slice(), .selected = cl.selected, .rows = cl.rows, .prefix = cl.prefix, .where = at, .jobs = cl.jobs, .cancels = cl.cancels }}); + // Hashed while it records or replays: no client, no cost. + const state: u64 = if (tape.on == null) 0 else if (cl.slot.plain) |b| std.hash.Wyhash.hash(0, b) else 0; + const o = tape.value(.addon, same, .{Outcome{ .state = state, .ret = cl.ret, .text = cl.refused.slice(), .said = cl.said.slice(), .err = cl.err, .look = cl.look.slice(), .pane_name = cl.pane_name.slice(), .pane_text = cl.pane_text, .exec = cl.exec.slice(), .selected = cl.selected, .rows = cl.rows, .prefix = cl.prefix, .where = at, .jobs = cl.jobs, .cancels = cl.cancels }}); if (o.where) |w| cl.slot.where_text.set(w); + // Run here, it came out otherwise than the session's: it broke its + // promise (PA_PLAIN), and the client joins again without it. + if (cl.slot.ops != null and cl.slot.plain != null and tape.replaying() and o.state != state) { + pardes.Addon.distrust(cl.slot.nameOf()); + var why: Buf(160) = .{}; + tape.diverged(why.print("addon {s}: its state after {s} is not the session's (it breaks PA_PLAIN): the session's alone now", .{ cl.slot.nameOf(), cl.name })); + } + cl.slot.state = o.state; if (cl.p) |p| if (p.panes[cl.id]) |pane| for (o.jobs) |token| if (token != 0) p.emit(.{ .job = .{ .pane = @intCast(cl.id), .serial = pane.serial, .token = token } }); for (o.cancels) |token| if (token != 0) { for (&cl.slot.jobs) |*j| if (j.*) |job| if (job.token == token) { @@ -1000,7 +1042,15 @@ pub const api: c.pa_api = .{ defer if (checked) { cl.began += @import("FrameStats.zig").nowNs() -| t0; }; - const mem = cl.slot.arena.allocator().alignedAlloc(u8, .@"16", @max(n, 1)) catch return null; + // A plain kind's one block, the core's; no other. + const mem = mem: { + if (cl.slot.plain) |b| { + if (b.len > 0) return null; + cl.slot.plain = cl.slot.gpa.alignedAlloc(u8, .@"16", @max(n, 1)) catch return null; + break :mem cl.slot.plain.?; + } + break :mem cl.slot.arena.allocator().alignedAlloc(u8, .@"16", @max(n, 1)) catch return null; + }; @memset(mem, 0); if (checked) cl.slot.used.append(std.heap.page_allocator, mem) catch {}; return mem.ptr; @@ -1010,6 +1060,8 @@ pub const api: c.pa_api = .{ fn f(ptr: ?*anyopaque, n: usize) callconv(.c) void { const cl = current orelse return; const at: [*]align(16) u8 = @ptrCast(@alignCast(ptr orelse return)); + // A plain kind's block goes with its pane. + if (cl.slot.plain) |b| if (b.ptr == at) return; if (checked) for (cl.slot.used.items, 0..) |m, i| if (m.ptr == at) { _ = cl.slot.used.swapRemove(i); break; diff --git a/src/dump.zig b/src/dump.zig index d1662e24..3269cc59 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -780,16 +780,19 @@ const SlotRecord = struct { /// way and its yank took nothing. tail: []const u8 = "", selection: []const u8 = "", + /// A plain kind's block's hash (Kind.Slot.state). + state: u64 = 0, }; pub fn slotWrite(s: *const Kind.Slot, gpa: std.mem.Allocator, w: *std.Io.Writer) !void { var place: [32]u8 = undefined; - var r: SlotRecord = .{ .name = s.nameOf(), .version = s.version, .gen = s.gen, .path = s.path, .art = s.art, .record = s.record, .where = s.where_text.slice(), .values = s.values.slice(), .live = s.live, .next_tick = s.next_tick, .views = s.views, .focused = s.focused, .cancelled = s.cancelled, .prefix = s.prefix.slice(), .refused = s.refused.slice(), .tail = s.tail.slice(), .selection = s.selection }; + var r: SlotRecord = .{ .name = s.nameOf(), .version = s.version, .gen = s.gen, .path = s.path, .art = s.art, .record = s.record, .where = s.where_text.slice(), .values = s.values.slice(), .live = s.live, .next_tick = s.next_tick, .views = s.views, .focused = s.focused, .cancelled = s.cancelled, .prefix = s.prefix.slice(), .refused = s.refused.slice(), .tail = s.tail.slice(), .selection = s.selection, .state = s.state }; var owned: ?[]u8 = null; defer if (owned) |o| s.gpa.free(o); if (s.ops != null) { r.where = Kind.where(@constCast(s), &place); - owned = try slotRecord(@constCast(s)); + // A plain kind's record is its block, as it is (Kind.Slot.plain). + owned = if (s.plain) |b| (if (b.len > 0) try s.gpa.dupe(u8, b) else null) else try slotRecord(@constCast(s)); r.record = owned; } const bytes = try walk.save(SlotRecord, gpa, &r); @@ -816,7 +819,7 @@ pub fn slotRead(out: *Kind.Slot, gpa: std.mem.Allocator, bytes: []const u8) !voi var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); const r = try walk.load(SlotRecord, arena.allocator(), bytes, null); - out.* = .{ .gpa = gpa, .arena = .init(gpa), .version = r.version, .gen = r.gen, .art = r.art, .live = r.live, .next_tick = r.next_tick, .views = r.views, .focused = r.focused, .cancelled = r.cancelled }; + out.* = .{ .gpa = gpa, .arena = .init(gpa), .version = r.version, .gen = r.gen, .art = r.art, .live = r.live, .next_tick = r.next_tick, .views = r.views, .focused = r.focused, .cancelled = r.cancelled, .state = r.state }; errdefer out.free(); @memcpy(out.name[0..@min(15, r.name.len)], r.name[0..@min(15, r.name.len)]); out.path = try gpa.dupe(u8, r.path); @@ -865,6 +868,34 @@ pub fn reviveSlot(p: *Pardes, id: usize, s: *Kind.Slot) void { } } +/// A replica client's pane of a plain kind it has the code of (`ops`), +/// loaded as the session dumped it: a slot of its core's own memory (the +/// rollback epoch), opened here, its block the record's bytes, its state +/// the session's. The loaded one (the C heap's) is freed. +pub fn plainHere(p: *Pardes, id: usize, old: *Kind.Slot, ops: *const Kind.Ops) !*Kind.Slot { + const s = try p.gpa.create(Kind.Slot); + s.* = .{ .ops = ops, .live = true, .gpa = p.gpa, .arena = .init(p.gpa), .name = old.name, .version = old.version, .gen = old.gen, .art = old.art, .where_text = old.where_text, .values = old.values, .prefix = old.prefix, .tail = old.tail, .opened = old.opened, .cancelled = old.cancelled, .tokens = old.tokens }; + errdefer { + s.free(); + p.gpa.destroy(s); + } + s.path = try p.gpa.dupe(u8, old.path); + s.selection = try p.gpa.dupe(u8, old.selection); + const kept = .{ old.next_tick, old.views, old.focused, old.state }; + if (!Kind.reopen(p, id, s, null)) return error.KindRefused; + // Its block the session's, byte for byte (its record), never through + // the kind's load, which may tidy it. + const block = s.plain orelse return error.KindRefused; + const record = old.record orelse return error.KindRefused; + if (block.len != record.len) return error.KindRefused; + @memcpy(block, record); + s.next_tick, s.views, s.focused, s.state = kept; + const gpa = old.gpa; + old.free(); + gpa.destroy(old); + return s; +} + /// A PDF pane of a dump from before the pdf addon: a pane of the addon's /// kind `pdf` on its file, at its page, its fit and tint given again as /// its words; a placeholder until the addon is loaded. diff --git a/src/kinds/addon.zig b/src/kinds/addon.zig index 54f26893..557547f6 100644 --- a/src/kinds/addon.zig +++ b/src/kinds/addon.zig @@ -45,7 +45,9 @@ comptime { /// `ctl`; `leader` is the keys after Space that run it ("ng": SPC n g). pub const Word = struct { name: [:0]const u8, leader: ?[:0]const u8 = null, arg: enum(u8) { none, optional, required } = .none, opens: bool = false, doc: [:0]const u8 = "" }; /// `pictures`: its panes place pictures, and their tag says how they show. -pub const Head = struct { name: []const u8, version: u32, exts: [:0]const u8 = "", words: []const Word = &.{}, pictures: bool = false }; +/// `plain`: PA_PLAIN, the kind is its extern struct and its step pure (the +/// header says what that promises): a client runs it itself. +pub const Head = struct { name: []const u8, version: u32, exts: [:0]const u8 = "", words: []const Word = &.{}, pictures: bool = false, plain: bool = false }; /// pardes's side, kept from init: about the pane of the current call. pub var api: *const c.pa_api = undefined; @@ -352,7 +354,7 @@ pub fn ops(comptime T: type) c.pa_ops { if (T.head.name.len > 15) @compileError(@typeName(T) ++ "'s name " ++ T.head.name ++ " is over 15 bytes"); var name: [16]u8 = @splat(0); @memcpy(name[0..T.head.name.len], T.head.name); - var o: c.pa_ops = .{ .init = F.init, .open = F.open, .paint = F.paint, .head = .{ .size = @sizeOf(c.pa_head), .abi = c.PA_ABI, .name = name, .version = T.head.version, .flags = if (T.head.pictures) c.PA_PICTURES else 0, .exts = T.head.exts, .words = &F.words, .nwords = F.words.len } }; + var o: c.pa_ops = .{ .init = F.init, .open = F.open, .paint = F.paint, .head = .{ .size = @sizeOf(c.pa_head), .abi = c.PA_ABI, .name = name, .version = T.head.version, .flags = (if (T.head.pictures) c.PA_PICTURES else 0) | (if (T.head.plain) c.PA_PLAIN else 0), .exts = T.head.exts, .words = &F.words, .nwords = F.words.len } }; // The kind's function, then the pa_ops field its wrapper fills. const fns = .{ .{ "close", "close" }, .{ "tick", "tick" }, .{ "prefix", "tick" }, .{ "view", "view" }, .{ "key", "key" }, diff --git a/src/kinds/pardes_addon.h b/src/kinds/pardes_addon.h index 9c101fee..586b03d3 100644 --- a/src/kinds/pardes_addon.h +++ b/src/kinds/pardes_addon.h @@ -275,6 +275,20 @@ typedef struct pa_tick { /* head.flags: its panes show pictures, and the core says how in their tag * (`petscii:on palette:commodore ascii:off`, Petscii, Palette, Ascii). */ #define PA_PICTURES 0x1u +/* head.flags: its state is plain and its step pure. The kind's whole state + * is the one block its `open` allocates with api->alloc (any other alloc + * is refused): bytes with no pointer or handle in them, changed only by its + * calls, each a function of the block and what the call is handed (no + * statics, no clock, randomness or file of its own: the time is tick's + * `now`, a seed api->seed's; tick is never told PA_FRAMED, a frame being + * one view's). pardes keeps the block where its own state is (its digest, a + * client's rollback copy) and dumps it as it is, `dump` uncalled and `load` + * given it at a Restore; so a client of the session runs the kind itself + * from the session's very bytes, its keys at once + * and its pictures its own, and a client without the addon shows a line. + * A kind that breaks the promise is caught: the client's state is not the + * session's, and it joins again without the kind. */ +#define PA_PLAIN 0x2u typedef struct pa_head { uint32_t size, abi; diff --git a/src/replica.zig b/src/replica.zig index fa888021..f84e61e8 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -425,7 +425,8 @@ pub const Screens = struct { fn each(x: Ctx, p: *Pardes) anyerror!void { var arena: std.heap.ArenaAllocator = .init(x.s.gpa); defer arena.deinit(); - for (p.panes, 0..) |slot, id| if (slot) |pane| if (pane.addon) |k| if (k.ops != null and !pane.collapsed) { + // A plain kind's is drawn where it is shown (Kind.runsHere). + for (p.panes, 0..) |slot, id| if (slot) |pane| if (pane.addon) |k| if (k.ops != null and k.plain == null and !pane.collapsed) { const pp = &x.s.peers.items[x.peer][id]; if (pp.shadow.serial != pane.serial) { pp.shadow.deinit(x.s.gpa); @@ -761,8 +762,13 @@ pub const Replica = struct { const c = try pardes.dump.load(epoch, o, bytes, null); // The chrome's colours are local, never dumped: the restored theme's. pardes.colors.finishThemeInitialization(c); - // It runs no kind's code: its panes of a kind are Shadows. - c.kinds.ops = @splat(null); + // It runs a plain kind's code of its own (Addon.plainOps), each pane + // of one opened here from its record; another's panes are Shadows. + for (c.kinds.kinds[0..c.kinds.n], c.kinds.ops[0..c.kinds.n]) |*e, *ops| ops.* = pardes.Addon.plainOps(e); + @memset(c.kinds.ops[c.kinds.n..], null); + for (c.panes, 0..) |pslot, at| if (pslot) |pane| if (pane.addon) |s| if (c.kinds.byName(s.nameOf())) |k| if (c.kinds.ops[k]) |ops| { + pane.addon = try pardes.dump.plainHere(c, at, s, ops); + }; // What loading queued (a watch, a theme asked for) is the // authority's errand, done there already: the session was dumped // between steps, with nothing queued. diff --git a/src/tape.zig b/src/tape.zig index 9dca5ee8..2875b156 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -1148,6 +1148,12 @@ pub fn poison(why: []const u8) void { }; } +/// A replica's replay came to another state than the authority's (`why`): +/// it joins again. +pub fn diverged(why: []const u8) void { + if (on) |t| if (t.replay) t.diverge("{s}", .{why}); +} + /// A replica's speculation leaves what is the authority's be (a terminal's /// emulator, a kind's state): true, and the input waits for its order. pub fn authorityOnly(comptime why: []const u8) bool { |
