diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 12:19:03 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 12:35:01 -0300 |
| commit | e0ed81fc02b7fcf9f20f9be1c0080bd1339d80fb (patch) | |
| tree | cd04bb0ab4d55cbb67f5c43b1898d640bce1fa22 | |
| parent | f922f8b5db1f3b92d954c43860c32cd251d5bc55 (diff) | |
| download | pardes-e0ed81fc02b7fcf9f20f9be1c0080bd1339d80fb.tar.gz pardes-e0ed81fc02b7fcf9f20f9be1c0080bd1339d80fb.zip | |
kinds #21, a PDF across replicas: a pane of a kind's frame crosses within the wire's bound (encodeShadow takes a budget and leaves out a raster past it, where a 17 MB page in a 110-column window ended every client's link and every join's after), a join's dump keeps a kind's tag words and selection (SlotRecord tail and selection, which only Kind.reopen made: a joiner's tag wrapped another way and it diverged on tag_rows at each join), and a speculation whose step would tick a kind waits for the session's unit (Kind.step's rule, read only) where it guessed no tick and rolled back, every visible rollback of the 30 ms soak cell
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/Kind.zig | 15 | ||||
| -rw-r--r-- | src/dump.zig | 10 | ||||
| -rw-r--r-- | src/replica.zig | 5 | ||||
| -rw-r--r-- | src/screen_state.zig | 94 | ||||
| -rw-r--r-- | test/panes.zig | 82 |
5 files changed, 187 insertions, 19 deletions
diff --git a/src/Kind.zig b/src/Kind.zig index 2add017a..de5323bb 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -600,8 +600,11 @@ pub fn mouse(p: *Pardes, id: usize, m: pardes.Mouse, col: u16, row: u16) bool { /// The end of a step: each pane of a kind whose focus changed, or whose /// time has come, ticks (16.2). pub fn step(p: *Pardes) void { - if (tape.speculating()) return; const now = p.clock.stepped_ns; + // A tick is the authority's kind's alone: a speculation whose step + // would tick one waits for the authority's unit (guessed untouched, the + // unit rolled it back, a visible rollback an input). + if (tape.speculating()) return if (wouldTick(p, now)) tape.poison("a kind's tick"); for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |slot| { // Its tag laid out again by this step's sync, and drawn. if (slot.retag) p.needs_frame = true; @@ -629,6 +632,16 @@ pub fn step(p: *Pardes) void { }; } +/// Whether `step` would tick some pane of a kind now (its rule, read only). +fn wouldTick(p: *const Pardes, now: u64) bool { + for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |slot| { + if (!caps(p, slot).tick) continue; + if (focusedAnywhere(p, id) != slot.focused or std.hash.Wyhash.hash(0, std.mem.asBytes(&viewOf(p, id))) != slot.view) return true; + if (slot.next_tick == 1 or (slot.next_tick > 1 and now >= slot.next_tick)) return true; + }; + return false; +} + /// Pane `id` as its next paint sees it: its body below its notices, as /// draw.zig paints it, a cell's pixels, the theme's colours and the /// raster this frontend wants a picture at. diff --git a/src/dump.zig b/src/dump.zig index 1d4ee2c2..0507fb4f 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -925,11 +925,17 @@ const SlotRecord = struct { view: u64 = 0, focused: bool = false, told: []const u8 = "", + /// What a step reads of it that its kind's open made (Kind.reopen) or + /// a call set: its tag's default words and what it has selected. A + /// replica's load opens no kind; without them its tag wrapped another + /// way and its yank took nothing. + tail: []const u8 = "", + selection: []const u8 = "", }; 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, .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, .view = s.view, .focused = s.focused, .told = s.told.slice() }; + var r: SlotRecord = .{ .name = s.nameOf(), .version = s.version, .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, .view = s.view, .focused = s.focused, .told = s.told.slice(), .tail = s.tail.slice(), .selection = s.selection }; var owned: ?[]u8 = null; defer if (owned) |o| s.gpa.free(o); if (s.ops != null) { @@ -972,7 +978,9 @@ pub fn slotRead(out: *Kind.Slot, gpa: std.mem.Allocator, bytes: []const u8) !voi out.path = try gpa.dupe(u8, r.path); out.where_text.set(r.where); out.told.set(r.told); + out.tail.set(r.tail); out.values.set(r.values); + out.selection = try gpa.dupe(u8, r.selection); if (r.record) |rec| out.record = try gpa.dupe(u8, rec); } diff --git a/src/replica.zig b/src/replica.zig index e5dbe310..c5e3fd34 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -446,7 +446,10 @@ pub const Screens = struct { 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, k.pointer, &w.writer)) continue; + // Within the wire's bound, its head (6 bytes) aside: a page of a + // PDF in a wide window is more, and a message past it ends + // the peer's link (its reader stops at it). + if (!try screen_state.encodeShadow(x.s.gpa, &pp.shadow, &frame, k.prefix.slice(), k.placed, k.pointer, wire.max_payload - 6, &w.writer)) continue; try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written())); }; } diff --git a/src/screen_state.zig b/src/screen_state.zig index 1c86c216..bcf4b137 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -1408,19 +1408,48 @@ pub const ShadowSent = struct { /// 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, pointer: u8, w: *std.Io.Writer) !bool { +/// this frame already. The message is `budget` bytes at most (the wire's +/// bound, which a peer's reader takes as the link's end past it): each +/// placement the peer holds crosses as its key, and a new one's raster +/// only while it fits; one that does not is left out of the frame (the +/// peer shows the pane without it) and is not tried again until it moves. +pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Frame, prefix: []const u8, placed: bool, pointer: u8, budget: usize, 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_len = @min(prefix.len, 96); // Whether it placed a picture, and the pointer it asks for, one byte. const flags: u8 = @intFromBool(placed) | @as(u8, @min(pointer, 127)) << 1; const prefix_hash = std.hash.Wyhash.hash(flags, 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; - }; + const rowHeld = struct { + fn at(s: *const ShadowSent, cols: u16, hs: []const Hash, y: usize) bool { + return s.cols == cols and y < s.hashes.items.len and s.hashes.items[y] == hs[y]; + } + }.at; + // Its size but for the placements: the head, each row's hash and the + // rows the peer does not hold, the placements' count. + var size: usize = 6 + prefix_len + 1; + for (0..f.rows) |y| size += 9 + if (rowHeld(sent, f.cols, hashes.items, y)) 0 else @as(usize, f.cols) * 20; + const place_head = 57; + var crosses: [64]bool = @splat(false); + var keys: usize = 0; + for (places, 0..) |pl, i| { + const held = std.mem.indexOfScalar(u64, sent.keys.items, pl.key) != null; + const more = place_head + if (held) 0 else @as(usize, pl.w) * pl.h * 4; + if (i >= crosses.len or size + more > budget) continue; + crosses[i] = true; + size += more; + keys += 1; + } + 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 == keys; + if (same) { + var k: usize = 0; + for (places, 0..) |pl, i| if (i < crosses.len and crosses[i]) { + if (pl.key != sent.keys.items[k]) same = false; + k += 1; + }; + } if (same) { hashes.deinit(gpa); return false; @@ -1428,11 +1457,11 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr try w.writeInt(u16, f.cols, .little); try w.writeInt(u16, f.rows, .little); try w.writeByte(flags); - try w.writeByte(@intCast(@min(prefix.len, 96))); - try w.writeAll(prefix[0..@min(prefix.len, 96)]); + try w.writeByte(@intCast(prefix_len)); + try w.writeAll(prefix[0..prefix_len]); 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; + const held = rowHeld(sent, f.cols, hashes.items, y); try w.writeByte(@intFromBool(!held)); if (!held) for (f.cells[y * f.cols ..][0..f.cols]) |*c| { try w.writeAll(&c.text); @@ -1443,8 +1472,8 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr try w.writeByte(c.flags); }; } - try w.writeByte(@intCast(places.len)); - for (places) |pl| { + try w.writeByte(@intCast(keys)); + for (places, 0..) |pl, i| if (i < crosses.len and crosses[i]) { 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); @@ -1453,13 +1482,13 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr 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); + for (places, 0..) |pl, i| if (i < crosses.len and crosses[i]) try sent.keys.append(gpa, pl.key); return true; } @@ -1478,19 +1507,52 @@ test "a pane of a kind crosses as its frame, a row or a raster the peer holds as 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, 0, &out.writer)); + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, 0, 1 << 20, &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, 0, &out.writer)); + try std.testing.expect(!try encodeShadow(gpa, &sent, &f, "fake", true, 0, 1 << 20, &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, 0, &out.writer)); + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, 0, 1 << 20, &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]); } + +test "a pane of a kind's raster past the message's bound is left out of its frame, the frame within it" { + const gpa = std.testing.allocator; + var cells: [2 * 1]Cell = @splat(std.mem.zeroes(Cell)); + var small: [4]u8 = .{ 1, 2, 3, 4 }; + var big: [4 * 64]u8 = @splat(7); + var f: Kind.Frame = .{ .cells = &cells, .cols = 2, .rows = 1, .n = 2 }; + f.buf[0] = std.mem.zeroInit(Place, .{ .rgba = &big, .w = 8, .h = 8, .key = 1 }); + f.buf[1] = std.mem.zeroInit(Place, .{ .rgba = &small, .w = 1, .h = 1, .key = 2 }); + 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(); + // Room for the frame and the small raster, not the 256-byte one. + const budget = 200; + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "p", false, 0, budget, &out.writer)); + try std.testing.expect(out.written().len <= budget); + try sh.apply(gpa, out.written()); + try std.testing.expectEqual(@as(usize, 1), sh.places.items.len); + try std.testing.expectEqual(@as(u64, 2), sh.places.items[0].key); + // Nothing moved: nothing to send, the big one not tried again. + out.clearRetainingCapacity(); + try std.testing.expect(!try encodeShadow(gpa, &sent, &f, "p", false, 0, budget, &out.writer)); + // With room, it crosses, the small one as its key. + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "p", false, 0, 1 << 20, &out.writer)); + try sh.apply(gpa, out.written()); + try std.testing.expectEqual(@as(usize, 2), sh.places.items.len); + try std.testing.expectEqual(@as(u8, 7), sh.rasters.items[0][5]); +} diff --git a/test/panes.zig b/test/panes.zig index 7592742c..15eca652 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -3835,6 +3835,88 @@ const PdfAddon = struct { try std.testing.expect(r.diverged == null); } + /// A session with design.pdf open in a pane of the addon, its pages + /// rendered; `land` takes each of its units. + fn opened(a: *pardes.replica.Authority, land: anytype) !usize { + var words: [64]pardes.Kind.LoadedWord = undefined; + var why: pardes.Kind.Buf(160) = .{}; + const lib = pardes.Addon.readLoaded(pdf_lib, &words, &why); + try land.step(.{ .addon = .{ .op = .load, .pane = 0, .kind = &lib } }); + var buf: [4096]u8 = undefined; + const real = try std.Io.Dir.cwd().realPathFile(std.testing.io, "docs/design.pdf", &buf); + var line: [4200]u8 = undefined; + try land.step(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{buf[0..real]}) }); + for (0..3) |_| { + try land.step(.tick); + try jobs(a.core, land); + } + return for (a.core.panes, 0..) |slot, i| { + if (slot) |pane| if (pane.addon != null) break i; + } else error.NoPdfPane; + } + + test "a client that joins a session with a PDF open has its pane's tag words and selection as the session has them" { + if (!addons.has_pdf) return error.SkipZigTest; + const R = pardes.replica; + const gpa = std.testing.allocator; + const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &host_vtable }, false); + defer a.deinit(); + const Land = struct { + a: *R.Authority, + fn step(l: @This(), ev: pardes.Event) !void { + _ = try l.a.local(ev); + } + }; + const id = try opened(a, Land{ .a = a }); + const slot = a.core.panes[id].?.addon.?; + slot.gpa.free(slot.selection); + slot.selection = try slot.gpa.dupe(u8, "a word it selected"); + // The dump a join is sent (Peers.join's), as the session makes it. + const was = pardes.tape.on; + pardes.tape.on = &a.t; + const bytes = try pardes.dump.byteDump(a.core); + pardes.tape.resync(&a.t); + pardes.tape.on = was; + defer a.core.gpa.free(bytes); + const r = try R.Replica.initDump(gpa, 2, 0, bytes, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + try std.testing.expect(r.loaded(pardes.tape.digestOf(a.core))); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const theirs = r.core().panes[id].?; + try std.testing.expectEqualStrings(try pardes.tagline.tagText(a.core, arena.allocator(), a.core.panes[id].?), try pardes.tagline.tagText(r.core(), arena.allocator(), theirs)); + try std.testing.expectEqualStrings("a word it selected", theirs.addon.?.selection); + r.confirm(try a.local(.{ .key = .{ .cp = 'j', .text = "j" } })); + if (r.diverged) |w| std.debug.print("diverged: {s}\n", .{w}); + try std.testing.expect(r.diverged == null); + } + + test "a client's input whose session step ticks a PDF's pane waits for that step, never guessed and rolled back" { + if (!addons.has_pdf) return error.SkipZigTest; + const R = pardes.replica; + const gpa = std.testing.allocator; + const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &host_vtable }, false); + defer a.deinit(); + const r = try R.Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + const Land = struct { + a: *R.Authority, + r: *R.Replica, + fn step(l: @This(), ev: pardes.Event) !void { + l.r.confirm(try l.a.local(ev)); + } + }; + _ = try opened(a, Land{ .a = a, .r = r }); + const guessed = r.stats.mispredicted; + // Its window resized: the pane's view moves, and the session's + // step ticks its kind, which only the session runs. + r.confirm(try a.take(try r.input(.{ .resize = .{ .cols = 100, .rows = 30 } }))); + r.settle(); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(guessed, r.stats.mispredicted); + try std.testing.expect(r.stats.stopped >= 1); + } + test "a PDF pane whose file changes on disk opens again from it at its page, its fit kept" { if (!addons.has_pdf) return error.SkipZigTest; const gpa = std.testing.allocator; |
