summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 02:31:55 -0300
committerGabriel Schneider <[email protected]>2026-11-06 04:04:25 -0300
commit9c66cdc9049b49f128336dcf5046e2bb1321e281 (patch)
tree7ff664677eb7291558f71a8f824d0ce6a922779a /src/replica.zig
parenta6f294ef5b50aeb5b69e486838378654e318ed04 (diff)
downloadpardes-9c66cdc9049b49f128336dcf5046e2bb1321e281.tar.gz
pardes-9c66cdc9049b49f128336dcf5046e2bb1321e281.zip
kinds #5, ABI 2 and the seam: src/kinds/pardes_addon.h is ABI 2 as architecture §16.9 has it (pa_window 24 bytes and no art bytes, pa_placement 80 with at, from and dirty_y/dirty_h and no stride, pa_word 32 with no size and PA_WORD, tick(st, now_ns, flags) with PA_FOCUSED, PA_OK and the conventions, the dump and load rules, pa_put with attrs, pa_copy and PA_POD, whose fragment gcc -Werror refused as written and is ternaries here) and fake.c and image's table speak it; tick is called at its deadline and once after open, a taken key or click, a word, a job's answer, a load and a change of PA_FOCUSED (the pane has the keyboard in some view, its window focused), nextWake folds the deadlines, and a Debug build hashes the kind's allocations across paint and panics naming it; paint runs once a frame, the slot keeps its prefix and whether it placed a picture, the tag is that prefix, the core's art words when it did and the path, and a frame that changes it owes a step to lay the tag out again; Addon load and reload are an addon effect the host performs (memfd, BLAKE3, dlopen, the head checks and init, the bytes a process holds shared and counted) answered by an addon event carrying what the library brings as data, whose update does the collisions, the reload's validate-then-swap at a new generation and the waiting panes deterministically, while unload is the update's alone and lets the library go by effect; a kind's job is Effect.job run on the host's workers (host_io.KindJob: tui and gui on Io.concurrent, macOS and tests on the loop) answered by Event.job, stale generations dropped and a library closed after its last job; each core has its own Registry (16 kinds, their words, leaders and extensions, digested and dumped; their code a local side array a replica has none of), so a test session no longer unloads the world and Restore and -l keep the process's kinds; every call into a kind whose answer the core acts on is taped as an Outcome (its answer, refusal, message, Look, text pane) and the prefix a step reads is taped too, so a replica's core, which runs no kind's code, steps alike; a pane of a kind on a replica is a Shadow drawn from the authority's frames, painted for the peer's view and sent by Screens as row hashes with the rows that changed and each raster once a key (screen_state.Shadow), its keys going to the authority; the tail, reload and the Addon word and event are Addon.zig's, the slot's record dump.zig's, a tail word's run exec.zig's; --addon-test gains focus, blur, dump, body, ctl and the window line, its jobs done as a host does them; fake.out gains its window line, loses a body row once the tag carries the art words and lands FakeJob at once; tests rewritten: the image dump test (the replica-style load keeps the record), the Petscii/Palette/Ascii tag test in ctl.zig and test/panes.zig's image tag test (a picture decoded and shown first), fs_namespace's decode test (the job performed as a host would), Kind.zig's registry test and Addon.zig's crash-mark test, the addons suite's ABI numbers; new: a job under way across a reload (T1), a Shadow's frame codec, and a replica with no code for a kind showing its pane and passing its keys; gates: core-test 958/958 and behaviour-test 249/249 on both, unit-test 1490/1491 tui and 1533/1534 gui, pane-test 171/172, snap all 105 ok, process-test 0 of 37 failed on both, addon-test ok, hxdiff 19 and hxparity 6 mismatches by the base's names, hxgolf 0, replicas, gui-golden, monkey-gui, daemon.py main and window-clients, soak local pass, perf medium within 1% of the base
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig154
1 files changed, 147 insertions, 7 deletions
diff --git a/src/replica.zig b/src/replica.zig
index d30d0e8a..6de125bd 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -298,7 +298,9 @@ pub const Authority = struct {
// A message of either side is u8 pane, u32 the pane's serial (a slot's
// next pane is another), u8 kind, and the kind's bytes:
// to a replica: kind 1, a delta; kind 2, rows (encodeRows): asked for,
-// or a step read them;
+// or a step read them; kind 4, a pane of a kind's frame
+// (a Shadow, screen_state.encodeShadow), at the size the
+// peer's view gives the pane;
// to the authority: kind 1, an acknowledgement (u32 seq); kind 2, a
// request for rows (u64 lo, u64 hi, absolute); kind 3, a
// delta refused: start this terminal afresh.
@@ -312,6 +314,7 @@ pub const Authority = struct {
const kind_delta = 1;
const kind_rows = 2;
+const kind_shadow = 4;
const kind_ack = 1;
const kind_reset = 3;
@@ -343,7 +346,7 @@ pub const Screens = struct {
encode_ns: u64 = 0,
const Tracked = struct { serial: u32, state: *terminal.State, track: screen_state.Track };
- const PeerOf = struct { serial: u32 = 0, peer: screen_state.Peer = .{} };
+ const PeerOf = struct { serial: u32 = 0, peer: screen_state.Peer = .{}, shadow: screen_state.ShadowSent = .{} };
pub fn init(gpa: Allocator) Screens {
return .{ .gpa = gpa, .enc = .init(gpa) };
@@ -353,7 +356,10 @@ pub const Screens = struct {
pub fn deinit(s: *Screens, core: *Pardes) void {
for (&s.tracks, 0..) |*slot, id| if (slot.*) |*tr| tr.track.deinit(s.gpa, if (s.live(core, id, tr)) &tr.state.vt else null);
for (s.peers.items) |p| {
- for (p) |*pp| pp.peer.deinit(s.gpa);
+ for (p) |*pp| {
+ pp.peer.deinit(s.gpa);
+ pp.shadow.deinit(s.gpa);
+ }
s.gpa.destroy(p);
}
s.peers.deinit(s.gpa);
@@ -380,16 +386,20 @@ pub const Screens = struct {
/// Peer `peer` gone: what it was sent forgotten, its number free for
/// the next `addPeer` (a socket's clients come and go).
pub fn dropPeer(s: *Screens, peer: u16) void {
- for (s.peers.items[peer]) |*pp| pp.peer.deinit(s.gpa);
+ for (s.peers.items[peer]) |*pp| {
+ pp.peer.deinit(s.gpa);
+ pp.shadow.deinit(s.gpa);
+ }
s.peers.items[peer].* = @splat(.{});
s.free_peers.append(s.gpa, peer) catch {};
}
/// What `peer` is owed of every terminal, each a message onto `out`
/// (owned): nothing for one it has as it is, or with `bound` in flight.
- pub fn flush(s: *Screens, core: *Pardes, peer: u16, out: *std.ArrayListUnmanaged([]u8)) !void {
+ pub fn flush(s: *Screens, core: *Pardes, peer: u16, view: u8, out: *std.ArrayListUnmanaged([]u8)) !void {
const t0 = nowNs();
defer s.encode_ns += nowNs() - t0;
+ try s.shadows(core, peer, view, out);
for (core.panes, 0..) |slot, id| {
const pane = slot orelse continue;
const state = pane.terminal orelse continue;
@@ -409,6 +419,41 @@ pub const Screens = struct {
}
}
+ /// What `peer` is owed of each pane of a kind this core runs: its
+ /// frame as the peer's view shows it, when it is not what it was sent.
+ fn shadows(s: *Screens, core: *Pardes, peer: u16, view: u8, out: *std.ArrayListUnmanaged([]u8)) !void {
+ const any = for (core.panes) |slot| {
+ if (slot) |pane| if (pane.addon) |k| if (k.ops != null) break true;
+ } else false;
+ if (!any) return;
+ const Ctx = struct { s: *Screens, peer: u16, out: *std.ArrayListUnmanaged([]u8) };
+ try pardes.views.within(core, view, Ctx{ .s = s, .peer = peer, .out = out }, 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) {
+ const pp = &x.s.peers.items[x.peer][id];
+ if (pp.shadow.serial != pane.serial) {
+ pp.shadow.deinit(x.s.gpa);
+ pp.shadow.serial = pane.serial;
+ }
+ const r = p.rects[id];
+ const cols = r.w -| pardes.config.GUTTER;
+ const rows = (r.h -| pane.tag_rows) -| pane.notices.span();
+ if (cols == 0 or rows == 0) continue;
+ var frame: pardes.Kind.Frame = .{ .cols = cols, .rows = rows };
+ frame.cells = pardes.Kind.paint(p, k, id, arena.allocator(), cols, rows, &frame) orelse continue;
+ x.s.out.clearRetainingCapacity();
+ try writeScreenHead(&x.s.out, x.s.gpa, @intCast(id), pane.serial, kind_shadow);
+ var w: std.Io.Writer.Allocating = .fromArrayList(x.s.gpa, &x.s.out);
+ defer x.s.out = w.toArrayList();
+ if (!try screen_state.encodeShadow(x.s.gpa, &pp.shadow, &frame, k.prefix.slice(), k.placed, &w.writer)) continue;
+ try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written()));
+ };
+ }
+ }.each);
+ }
+
/// Rows a peer holds as holes of every terminal a step read (its rows,
/// history and all: text mode over a shell), each a rows message onto
/// `out` (owned): sent after the step and before its unit, so the
@@ -675,6 +720,8 @@ pub const Replica = struct {
pardes.dump.terminal_gpa = terminal.emulator_gpa;
defer pardes.dump.terminal_gpa = null;
const c = try pardes.dump.load(epoch, o, bytes, null);
+ // It runs no kind's code: its panes of a kind are Shadows.
+ c.kinds.ops = @splat(null);
// 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.
@@ -1061,6 +1108,17 @@ pub const Replica = struct {
const c = r.t.core orelse return null;
const pane = c.panes[m.pane] orelse return null;
if (pane.serial != m.serial) return null; // a pane gone: its slot's next is another
+ // A pane of a kind: the authority's frame of it, drawn as it is.
+ if (m.kind == kind_shadow) if (pane.addon) |k| {
+ const sh = k.shadow orelse sh: {
+ const made = k.gpa.create(screen_state.Shadow) catch return null;
+ made.* = .{};
+ k.shadow = made;
+ break :sh made;
+ };
+ sh.apply(k.gpa, m.payload) catch r.fail("a kind's frame did not apply");
+ return null;
+ };
const state = pane.terminal orelse return null;
const slot = &r.screens[m.pane];
if (slot.* == null or slot.*.?.serial != m.serial) {
@@ -1857,7 +1915,7 @@ pub const Peers = struct {
ps.msgs.clearRetainingCapacity();
}
(switch (what) {
- .flush => ps.screens.flush(core, peer.sid, &ps.msgs),
+ .flush => ps.screens.flush(core, peer.sid, peer.view, &ps.msgs),
.fill => ps.screens.fill(core, peer.sid, &ps.msgs),
}) catch return;
for (ps.msgs.items) |m| {
@@ -3264,7 +3322,7 @@ fn deliver(a: *Authority, screens: *Screens, peer: u16, r: *Replica, comptime wh
msgs.deinit(gpa);
}
switch (what) {
- .flush => try screens.flush(a.core, peer, &msgs),
+ .flush => try screens.flush(a.core, peer, r.view, &msgs),
.fill => {
try screens.fill(a.core, peer, &msgs);
screens.filled(a.core);
@@ -4018,3 +4076,85 @@ test "a step that reads a terminal's rows as the core keeps them sends a new cli
try sendAs(r1, a, &screens, &.{ r1, r2 }, &ps, .{ .key = .{ .cp = 'O', .text = "O" } });
for ([_]*Replica{ r1, r2 }) |r| try agrees(a, r);
}
+
+/// A kind as a library brings one, for the Shadow's test: a counter its
+/// keys move, painted as `count N`.
+const Counter = struct {
+ const pa = pardes.Kind.c;
+ fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque {
+ return pardes.Kind.api.alloc.?(4);
+ }
+ fn paint(st: ?*anyopaque, win: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {
+ const n: *u32 = @ptrCast(@alignCast(st.?));
+ var line: [16]u8 = undefined;
+ const text = std.fmt.bufPrint(&line, "count {d}", .{n.*}) catch unreachable;
+ for (text, 0..) |ch, i| win.*.cells[i] = std.mem.zeroInit(pa.pa_cell, .{ .text = .{ ch, 0, 0, 0, 0, 0, 0, 0 }, .len = 1, .flags = pa.PA_PAINTED, .fg = pa.PA_DEFAULT, .bg = pa.PA_DEFAULT });
+ }
+ fn press(st: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int {
+ const n: *u32 = @ptrCast(@alignCast(st.?));
+ n.* += 1;
+ return 1;
+ }
+ const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Counter", .opens = 1, .doc = "a counter" })};
+ const ops = std.mem.zeroInit(pardes.Kind.Ops, .{
+ .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "counter".* ++ @as([9]u8, @splat(0)), .version = 1, .exts = "", .words = &words, .nwords = 1 }),
+ .open = open,
+ .paint = paint,
+ .key = press,
+ });
+};
+
+fn shows(p: *Pardes, text: []const u8) !bool {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const s = try p.render(arena.allocator());
+ var row: [512]u8 = undefined;
+ for (0..s.rows) |y| {
+ var n: usize = 0;
+ for (0..s.cols) |x| {
+ const g = s.at(@intCast(x), @intCast(y)).grapheme();
+ if (n + g.len > row.len) break;
+ @memcpy(row[n..][0..g.len], g);
+ n += g.len;
+ }
+ if (std.mem.indexOf(u8, row[0..n], text) != null) return true;
+ }
+ return false;
+}
+
+test "a replica with no code for a kind draws its pane from the authority's frames, and its keys go to the authority" {
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
+ defer a.deinit();
+ var screens: Screens = .init(gpa);
+ defer screens.deinit(a.core);
+ const peer = try screens.addPeer();
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), false);
+ defer r.deinit();
+ // What the host read off the library, as the `addon` event: the
+ // replica's core takes the kind's words, never its code.
+ var words: [64]pardes.Kind.LoadedWord = undefined;
+ const counter = pardes.Kind.loadedOf(&Counter.ops, "/x/counter.so", @splat(7), &words);
+ r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &counter } }));
+ r.confirm(try a.local(.{ .command = "Counter" }));
+ try std.testing.expect(r.core().kinds.ops[r.core().kinds.n - 1] == null);
+ try deliver(a, &screens, peer, r, .flush);
+ try std.testing.expect(try shows(r.core(), "count 0"));
+ // A key into it, typed at the replica: the authority's kind takes it.
+ var sent: std.ArrayListUnmanaged([]u8) = .empty;
+ defer {
+ for (sent.items) |m| gpa.free(m);
+ sent.deinit(gpa);
+ }
+ try key(r, 'x', &sent);
+ for (sent.items) |m| {
+ r.confirm(try a.take(m));
+ r.settle();
+ }
+ try deliver(a, &screens, peer, r, .flush);
+ try std.testing.expect(try shows(a.core, "count 1"));
+ try std.testing.expect(try shows(r.core(), "count 1"));
+ if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why});
+ try std.testing.expect(r.diverged == null);
+ try std.testing.expectEqual(digestOf(a), r.digest());
+}