diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 02:31:55 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 04:04:25 -0300 |
| commit | 9c66cdc9049b49f128336dcf5046e2bb1321e281 (patch) | |
| tree | 7ff664677eb7291558f71a8f824d0ce6a922779a /src/screen_state.zig | |
| parent | a6f294ef5b50aeb5b69e486838378654e318ed04 (diff) | |
| download | pardes-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/screen_state.zig')
| -rw-r--r-- | src/screen_state.zig | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/src/screen_state.zig b/src/screen_state.zig index 53288c7e..07d4153b 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -1292,3 +1292,200 @@ test "a replica's terminal pane draws as its authority's does" { }; } } + +// ---- a pane of a kind, as a Shadow (architecture §16.5) ---- +// +// A replica's core has no code for a library's kind: such a pane is drawn +// from the authority's frame of it, painted at the size the peer's view +// gives it. A frame crosses as its size, the prefix the kind gave it and +// whether it placed a picture, each row by its hash (with its cells when +// the peer's row there is another), then each placement, with its raster's +// bytes when the peer was not sent that key with the frame before. The +// stream is ordered and loses nothing, so what the authority last sent a +// peer is what the peer holds. + +const Kind = @import("Kind.zig"); +const Cell = Kind.c.pa_cell; +const Place = Kind.c.pa_placement; + +/// A replica's frame of a pane of a kind: what Kind.draw draws for it. +pub const Shadow = struct { + cols: u16 = 0, + rows: u16 = 0, + cells: []Cell = &.{}, + hashes: []Hash = &.{}, + places: std.ArrayList(Place) = .empty, + /// Each placement's raster, owned, beside it. + rasters: std.ArrayList([]u8) = .empty, + prefix: Kind.Buf(96) = .{}, + placed: bool = false, + + pub fn deinit(sh: *Shadow, gpa: std.mem.Allocator) void { + gpa.free(sh.cells); + gpa.free(sh.hashes); + for (sh.rasters.items) |r| gpa.free(r); + sh.rasters.deinit(gpa); + sh.places.deinit(gpa); + gpa.destroy(sh); + } + + /// A frame message applied (`encodeShadow`'s). + pub fn apply(sh: *Shadow, gpa: std.mem.Allocator, bytes: []const u8) !void { + var r: std.Io.Reader = .fixed(bytes); + const cols = try r.takeInt(u16, .little); + const rows = try r.takeInt(u16, .little); + if (cols != sh.cols or rows != sh.rows) { + gpa.free(sh.cells); + gpa.free(sh.hashes); + sh.cells = try gpa.alloc(Cell, @as(usize, cols) * rows); + sh.hashes = try gpa.alloc(Hash, rows); + @memset(std.mem.sliceAsBytes(sh.cells), 0); + @memset(sh.hashes, 0); + sh.cols = cols; + sh.rows = rows; + } + sh.placed = try r.takeByte() != 0; + sh.prefix.set(try r.take(try r.takeByte())); + for (0..rows) |y| { + sh.hashes[y] = try r.takeInt(u64, .little); + if (try r.takeByte() == 0) continue; + for (sh.cells[y * cols ..][0..cols]) |*c| { + @memcpy(&c.text, try r.take(8)); + c.fg = try r.takeInt(u32, .little); + c.bg = try r.takeInt(u32, .little); + c.attrs = try r.takeInt(u16, .little); + c.len = try r.takeByte(); + c.flags = try r.takeByte(); + } + } + var places: std.ArrayList(Place) = .empty; + errdefer places.deinit(gpa); + var rasters: std.ArrayList([]u8) = .empty; + defer { + for (sh.rasters.items) |old| if (for (rasters.items) |kept| { + if (kept.ptr == old.ptr) break false; + } else true) gpa.free(old); + sh.rasters.deinit(gpa); + sh.rasters = rasters; + sh.places.deinit(gpa); + sh.places = places; + } + for (0..try r.takeByte()) |_| { + var pl = std.mem.zeroes(Place); + pl.w = try r.takeInt(u32, .little); + pl.h = try r.takeInt(u32, .little); + inline for (.{ &pl.src.x, &pl.src.y, &pl.src.w, &pl.src.h, &pl.at.x, &pl.at.y, &pl.at.w, &pl.at.h }) |f| f.* = try r.takeInt(u32, .little); + inline for (.{ &pl.dst.x, &pl.dst.y, &pl.dst.w, &pl.dst.h }) |f| f.* = try r.takeInt(u16, .little); + pl.key = try r.takeInt(u64, .little); + const raster = if (try r.takeByte() != 0) try gpa.dupe(u8, try r.take(@as(usize, pl.w) * pl.h * 4)) else for (sh.places.items, sh.rasters.items) |old, held| { + if (old.key == pl.key) break held; + } else return error.BadShadow; + pl.rgba = raster.ptr; + try rasters.append(gpa, raster); + try places.append(gpa, pl); + } + } +}; + +/// What the authority last sent one peer of a pane of a kind: its rows' +/// hashes and its placements' keys. +pub const ShadowSent = struct { + serial: u32 = 0, + cols: u16 = 0, + hashes: std.ArrayList(Hash) = .empty, + keys: std.ArrayList(u64) = .empty, + prefix: Hash = 0, + + pub fn deinit(s: *ShadowSent, gpa: std.mem.Allocator) void { + s.hashes.deinit(gpa); + s.keys.deinit(gpa); + s.* = .{}; + } +}; + +/// Frame `f` of a pane (its prefix and `placed` from its slot) for a peer +/// that was last sent `sent`: false, and nothing written, when it holds +/// this frame already. +pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Frame, prefix: []const u8, placed: bool, w: *std.Io.Writer) !bool { + var hashes: std.ArrayList(Hash) = .empty; + errdefer hashes.deinit(gpa); + for (0..f.rows) |y| try hashes.append(gpa, std.hash.Wyhash.hash(0, std.mem.sliceAsBytes(f.cells[y * f.cols ..][0..f.cols]))); + const places = f.places[0..f.n]; + const prefix_hash = std.hash.Wyhash.hash(@intFromBool(placed), prefix); + var same = sent.cols == f.cols and std.mem.eql(Hash, sent.hashes.items, hashes.items) and sent.prefix == prefix_hash and sent.keys.items.len == places.len; + if (same) for (places, sent.keys.items) |pl, k| { + if (pl.key != k) same = false; + }; + if (same) { + hashes.deinit(gpa); + return false; + } + try w.writeInt(u16, f.cols, .little); + try w.writeInt(u16, f.rows, .little); + try w.writeByte(@intFromBool(placed)); + try w.writeByte(@intCast(@min(prefix.len, 96))); + try w.writeAll(prefix[0..@min(prefix.len, 96)]); + for (hashes.items, 0..) |h, y| { + try w.writeInt(u64, h, .little); + const held = sent.cols == f.cols and y < sent.hashes.items.len and sent.hashes.items[y] == h; + try w.writeByte(@intFromBool(!held)); + if (!held) for (f.cells[y * f.cols ..][0..f.cols]) |*c| { + try w.writeAll(&c.text); + try w.writeInt(u32, c.fg, .little); + try w.writeInt(u32, c.bg, .little); + try w.writeInt(u16, c.attrs, .little); + try w.writeByte(c.len); + try w.writeByte(c.flags); + }; + } + try w.writeByte(@intCast(places.len)); + for (places) |pl| { + try w.writeInt(u32, pl.w, .little); + try w.writeInt(u32, pl.h, .little); + inline for (.{ pl.src.x, pl.src.y, pl.src.w, pl.src.h, pl.at.x, pl.at.y, pl.at.w, pl.at.h }) |v| try w.writeInt(u32, v, .little); + inline for (.{ pl.dst.x, pl.dst.y, pl.dst.w, pl.dst.h }) |v| try w.writeInt(u16, v, .little); + try w.writeInt(u64, pl.key, .little); + const held = std.mem.indexOfScalar(u64, sent.keys.items, pl.key) != null; + try w.writeByte(@intFromBool(!held)); + if (!held) try w.writeAll(pl.rgba[0 .. @as(usize, pl.w) * pl.h * 4]); + } + sent.hashes.deinit(gpa); + sent.hashes = hashes; + sent.cols = f.cols; + sent.prefix = prefix_hash; + sent.keys.clearRetainingCapacity(); + for (places) |pl| try sent.keys.append(gpa, pl.key); + return true; +} + +test "a pane of a kind crosses as its frame, a row or a raster the peer holds as its hash" { + const gpa = std.testing.allocator; + var cells: [3 * 2]Cell = @splat(std.mem.zeroes(Cell)); + cells[0] = .{ .text = "a".* ++ @as([7]u8, @splat(0)), .fg = 1, .bg = 2, .attrs = 0, .len = 1, .flags = Kind.c.PA_PAINTED }; + var pixels: [4]u8 = .{ 1, 2, 3, 4 }; + var f: Kind.Frame = .{ .cells = &cells, .cols = 3, .rows = 2, .n = 1 }; + f.buf[0] = std.mem.zeroInit(Place, .{ .rgba = &pixels, .w = 1, .h = 1, .key = 9 }); + f.places = &f.buf; + var sent: ShadowSent = .{}; + defer sent.deinit(gpa); + const sh = try gpa.create(Shadow); + sh.* = .{}; + defer sh.deinit(gpa); + var out: std.Io.Writer.Allocating = .init(gpa); + defer out.deinit(); + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, &out.writer)); + try sh.apply(gpa, out.written()); + try std.testing.expectEqualStrings("a", sh.cells[0].text[0..1]); + try std.testing.expectEqualStrings("fake", sh.prefix.slice()); + try std.testing.expectEqual(@as(u8, 3), sh.rasters.items[0][2]); + // The same frame again: nothing to send. + out.clearRetainingCapacity(); + try std.testing.expect(!try encodeShadow(gpa, &sent, &f, "fake", true, &out.writer)); + // A cell of the second row moves: the first row and the raster go as hashes. + cells[4].flags = Kind.c.PA_PAINTED; + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, &out.writer)); + try std.testing.expect(out.written().len < 20 + 2 * 9 + 3 * 20 + 60); + try sh.apply(gpa, out.written()); + try std.testing.expectEqual(@as(u8, Kind.c.PA_PAINTED), sh.cells[4].flags); + try std.testing.expectEqual(@as(u8, 3), sh.rasters.items[0][2]); +} |
