summaryrefslogtreecommitdiff
path: root/src/Kind.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 23:57:53 -0300
committerGabriel Schneider <[email protected]>2026-11-18 00:29:40 -0300
commit137be286869241d19ea60ecb7737957d861162d7 (patch)
tree854878c3a2152195b95d6b469c011db5a0d7e6a7 /src/Kind.zig
parent9b20d00ca5ffbab640b87249d65e4165414d22c1 (diff)
downloadpardes-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/Kind.zig')
-rw-r--r--src/Kind.zig98
1 files changed, 75 insertions, 23 deletions
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;