From aa3ff349f840f568ea0f052c1f051cdc0dbb18c1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 26 Nov 2026 09:16:28 -0300 Subject: Kind, dump, replica: a plain kind's state opened or closed in a pane that stays (a picker's preview of a PDF, Pick.showKind; a file changed under its pane; an addon reloaded) is a write outside a client's rollback copy, so a client lets the copy go with that unit and never replays it over the copy (agent T23's find, which blocked the install: the soak `replicas -- --clients 5 --rtt 30,80,207` died with `allocator leaks detected` at seed 0 rtt 80 on T1's base and on M2, MuPDF's allocations of documents a picker previewed: contexts, documents, lanes, from Kind.reopen under Pick.showKind); THE CAUSE: no path of the preview forgets its close (the next row and a source change close through Pick.preview, Esc and every roll-up through restore and putBack, ⏎ through land's roll-up, a pane's end through teardownPane, the session's through Core.deinit; a dump carries the slot and the client's join opens it, closed the same ways), but replica.confirm's `ahead` rule: a client idle with its copy taken ahead (Replica.ready) replays the session's units on it and keeps each in `since` to replay again when the copy is put back, unless the unit wrote outside the copy (Pardes.outside, counted by the emulators alone) or opened or closed a pane (its serials); a preview opens the pdf addon's state in the pane the picker lands in, which stays, and that state (the document and its lanes, MuPDF's on the kind heap, the addon's `Own` found by its block's address) is no copy's; so a rollback (another's unit before the client's own key) put the copy back, the epoch freed the block, and the replay opened the document again for a new block: the first was never closed (and its `Own`, keyed by an address the epoch hands out again, could be found for another block); THE FIX: Kind.reopen and Kind.close count a plain kind's open and close in Pardes.outside, by the registry's PA_PLAIN, on every side alike whether the kind's code runs there or not (a client without the library counts as the session does), so confirm's one question lets such a unit go with the copy; dump.plainHere, a client's own open of a plain kind's pane at its join and no step of the session's, puts the count back to the session's as dumped; Term.Outside's and Pardes.outside's docs say so; RUN (TEST FIRST): replica.zig "a picker's preview opens and closes a plain kind's state in the pane it lands in, no pane opened or closed: ..." (a plain test kind `Owned` noting each state it opens and letting it go at its close, as the pdf addon's `Own`; files `.own`): a session at notes.txt in a work tree with paper.own, a client joined in its own view running the kind, idle with its copy taken ahead while the session types `SPC f f p` (the preview through the kind in notes.txt's pane), then a key of the client's own on top and the session's own key first (a rollback), the client's key, Esc; asks the digests equal at each end and no state left noted after both cores end: fails on M2 (8959b031dc33) at the last check (expected 0, found 1), passes now; the soak with the pdf addon (`zig build replicas -Dplatform=tui -- --clients 5 --rtt 30,80,207`, every addon the build has): diverged 0, unequal 0, quiet checks 508/0 474/0 665/0, stops not on may_wait 0, the corpus 22 of 22 scenes the same, no leak, the pdf panes the session opened 8, 6 and 5; a user could hit it as a client of a session whose picker previewed a PDF (a far client in a pair, a window joined to a terminal's session): a document leaked in the client at each rollback over such a preview, and pages drawn from another document's state where an address came back; NEW HOT PATHS: none per key or frame (a plain kind's open and close count one integer; a client lets its copy go at such a unit, as at a pane's open); GATES: core-test 1163/1337 (174 skipped: +1), its globals test (`nothing of a core is a process global`: the test kind's var in its test); no dump layout change (Pardes.outside was dumped and digested already; its values move at a plain kind's open and close, on every side alike); load 1.0 to 8.4 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Core.zig | 7 ++-- core/Kind.zig | 13 ++++++- core/Term.zig | 8 ++-- core/dump.zig | 4 ++ core/replica.zig | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/Core.zig b/core/Core.zig index 3a2e1f51..b80fec2b 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -304,9 +304,10 @@ n_look_src: usize = 0, look_walk_owner: ?u32 = null, /// hands out Pane.serial; monotonic, never reused next_serial: u32 = 0, -/// The writes of its terminals' emulators, which no rollback copy holds -/// (Term.Outside): digested, dumped and copied as any field, so the same on -/// every side; the replica engine's one question of a unit reads it. +/// The writes of its terminals' emulators, and the opens and closes of its +/// plain kinds' states (Kind.reopen, Kind.close), which no rollback copy +/// holds (Term.Outside): digested, dumped and copied as any field, so the +/// same on every side; the replica engine's one question of a unit reads it. outside: pardes.Term.Outside = .{}, /// The column whose tag the running command came from, while it runs: /// the column acme's commands take from their Text's col. diff --git a/core/Kind.zig b/core/Kind.zig index 4841a55c..22a7ebe0 100644 --- a/core/Kind.zig +++ b/core/Kind.zig @@ -320,10 +320,16 @@ pub fn reopen(p: *Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool { if (p.kinds.byName(s.nameOf())) |k| { // Its code: its library loaded at the first pane of its kind. if (s.ops == null) s.ops = Addon.need(&p.kinds, p.tape, k, &cl.refused, p.opts.distrusted); - // Its block made afresh by this open. + // Its block made afresh by this open. And what a plain kind keeps + // beside it (a PDF's document), made in every core that runs it, is + // no rollback copy's: a write outside the copy (Pardes.outside), + // counted by the registry's word on every side alike, the kind's + // code here or not. A client that kept this step to replay over its + // copy would make that state again and never close the first. if (p.kinds.kinds[k].caps.plain) { if (s.plain) |b| if (b.len > 0) s.gpa.free(b); s.plain = &.{}; + p.outside.n +%= 1; } // Its tag's computed words (UX K'3): its words, then Del. var w: std.Io.Writer = .fixed(&s.tail.bytes); @@ -416,6 +422,11 @@ pub fn close(p: *Pardes, pane: *Pane) void { // own memory (its pictures) is left be, as the rollback brings the pane // back; the session's order closes it. const kept = s.plain != null and s.ops != null and s.ops.?.close != null and p.tape.authorityOnly("a kind's close"); + // What a plain kind keeps beside its block let go: outside the copy, as + // its open was (reopen). + if (p.kinds.byName(s.nameOf())) |k| if (p.kinds.kinds[k].caps.plain) { + p.outside.n +%= 1; + }; if (!kept) { var i: usize = 0; while (i < ready.items.len) if (ready.items[i].draw == p and ready.items[i].serial == pane.serial) ready.orderedRemove(i).drop() else { diff --git a/core/Term.zig b/core/Term.zig index 509da6d9..2f0a0f3f 100644 --- a/core/Term.zig +++ b/core/Term.zig @@ -19,9 +19,11 @@ const iface = plain.term; /// digested and dumped, a function of the tape, the same on every side (one /// side's emulator written where the others' are not is a digest that says /// `outside`). Every write below takes it and counts, and so does a write a -/// speculation owes (`resize`): over an input's apply, the count moved is -/// "this input wrote, or owes, state outside the copy", the one question -/// core/replica.zig `confirm` asks a unit. A load sets it from the dump. +/// speculation owes (`resize`), and a plain kind's state opened or closed +/// (Kind.reopen, Kind.close: what it keeps beside its block, a PDF's +/// document): over an input's apply, the count moved is "this input wrote, +/// or owes, state outside the copy", the one question core/replica.zig +/// `confirm` asks a unit. A load sets it from the dump. pub const Outside = struct { n: u64 = 0 }; /// The reloads this process made (a join's, on every side) and their time, /// for the soak's join line (test/replicas.zig `--bytes`). diff --git a/core/dump.zig b/core/dump.zig index 0cefa157..389d948f 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -1010,6 +1010,10 @@ pub fn plainHere(p: *Pardes, id: usize, old: *Kind.Slot, ops: *const Kind.Ops) ! s.selection = try p.gpa.dupe(u8, old.selection); // (and its place: the open's own `where` is of a block not yet the session's) const kept = .{ old.next_tick, old.views, old.focused, old.state, old.where_text }; + // The count of writes outside the copy stays the session's, as dumped: + // this open is the join's, no step the session took (Kind.reopen). + const outside = p.outside; + defer p.outside = outside; if (!Kind.reopen(p, id, s, old.file)) return error.KindRefused; // Its block the session's, byte for byte (its record), never through // the kind's load, which may tidy it. diff --git a/core/replica.zig b/core/replica.zig index 6c037cd2..9e6183a1 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -9203,3 +9203,115 @@ test "a client joined after its session's shell was found to have no prompt mark _ = try n.serve(.{ .tag = 4, .op = .release, .node = run, .handle = h, .opened = true }, true); try n.agree(); } + +test "a picker's preview opens and closes a plain kind's state in the pane it lands in, no pane opened or closed: a client with a copy ahead never replays it over the copy, and every state the kind opens is closed once" { + // The soak's (test/replicas.zig with the pdf addon): `allocator leaks + // detected` at seed 0 rtt 80, the documents of PDFs a picker previewed + // (Pick.showKind) on a client, opened by a unit it kept to replay over + // its copy and opened again by the replay, the first never closed. + // A plain kind that holds something of its own outside its block for + // each state it opens, as the pdf addon holds a document (its `Own`): + // each open notes its block, each close lets it go, so a state opened + // and never closed stays noted. Its files are `.own`. + const Owned = struct { + const pa = pardes.Kind.c; + var noted: [8]?*anyopaque = @splat(null); + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + const st = pardes.Kind.api.alloc.?(4); + for (¬ed) |*n| if (n.* == null) { + n.* = st; + break; + }; + return st; + } + fn close(st: ?*anyopaque) callconv(.c) void { + for (¬ed) |*n| if (n.* == st) { + n.* = null; + return; + }; + } + fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {} + fn held() usize { + var n: usize = 0; + for (noted) |st| n += @intFromBool(st != null); + return n; + } + 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 = "owned".* ++ @as([11]u8, @splat(0)), .version = 1, .exts = ".own", .flags = pa.PA_PLAIN }), + .open = open, + .close = close, + .paint = paint, + }); + }; + const gpa = std.testing.allocator; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.createDir(std.testing.io, ".git", .default_dir); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "paper.own", .data = "own" }); + var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var line: [std.Io.Dir.max_path_bytes + 32]u8 = undefined; + { + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const owned = pardes.Kind.loadedOf(&Owned.ops, "/x/owned.so", @splat(7), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &owned } }); + _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/notes.txt", .{dir}) }); + // A client in a view of its own, running the kind's code. + const joined = (try a.join(1, null)).?; + defer gpa.free(joined.dump); + const r = try Replica.initDump(gpa, 1, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + r.core().kinds.ops[r.core().kinds.byName("owned").?] = &Owned.ops; + // SPC f f p at the session, in its view: its picker lists the tree, + // and paper.own, the one row, shows through the kind in the pane it + // lands in. The client idle meanwhile: its copy taken ahead, each + // unit replayed on top of it. + const At = struct { + fn sessionView(au: *Authority) void { + const was = Stepping.begin(&au.t, null); + au.core.toView(.at(0)); + was.end(); + } + }; + At.sessionView(a); + for (" ffp") |c| { + r.ready(); + r.confirm(try a.local(.{ .key = .{ .cp = c, .text = &.{c} } })); + r.ready(); + r.confirm(try a.local(.tick)); + } + const landing = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.addon != null) break id; + } else return error.NoPreview; + try std.testing.expect(r.core().panes[landing].?.addon != null); + // A key of the client's own on top, and the session's own key + // first: the client back to its copy, the units since replayed. + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try key(r, 'j', &sent); + try std.testing.expectEqual(@as(usize, 1), r.applied); + const foreign = r.stats.foreign; + r.confirm(try a.local(.{ .key = .{ .cp = 'q', .text = "q" } })); + r.settle(); + try std.testing.expectEqual(foreign + 1, r.stats.foreign); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + try agrees(a, r); + // Esc in the picker's view: the preview's state closed, the pane's + // own text back. + At.sessionView(a); + r.confirm(try a.local(.{ .key = .{ .cp = pardes.Key.escape } })); + r.settle(); + try agrees(a, r); + try std.testing.expect(r.core().panes[landing].?.addon == null); + } + try std.testing.expectEqual(@as(usize, 0), Owned.held()); +} -- cgit v1.3