summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 15:15:01 -0300
committerGabriel Schneider <[email protected]>2026-10-30 16:49:16 -0300
commit5af775e9817395af39792cc609b832394d4a4fb8 (patch)
treed1aad3579b8b16ebd74c80567ae386fa7d3825d8 /src/replica.zig
parent3f62f4e2a0f40e9d92f79665c2e87804c2d55357 (diff)
downloadpardes-5af775e9817395af39792cc609b832394d4a4fb8.tar.gz
pardes-5af775e9817395af39792cc609b832394d4a4fb8.zip
replica: what only the authority holds, a step reads off the tape (tape.authority): the store's closed panes and Recent, a script's hold on a pane's events, the 9P socket, the session's config directory and startup file; a replica's own are never read, and never put back by a rollback
The Panes picker's closed rows, Recent's rows, a closed pane brought back (a Look at @pN, a Panes row), a jump to a closed file and the +Jumps rows read `p.store` and `p.recent`, `.authority` state a replica keeps on its own: its store has no file, its Recent only what it replayed since it joined. The rows made of them went into digested state, so every client of a session diverged together after a Del (H4), every client that joined after one diverged at its first unit forever (H11: trackJump dropped a closed file's jumps at every step when Recent did not know the file), a client's Del of a file the session's Recent knew made the stat the session skipped (H12: noteClosed looked in Recent first), and a client on another machine read its own store (H13). - tape.authority: poison, then a value read: the authority's answer is taped, a replay takes it, a speculation waits for it. One site, `.authority`. - Readers through it: Query's Recent and closed-pane rows, Store.reopen's record, Pardes.recentByHash (jumps, +Jumps), mouse.reportRange's readers check (a script reading a pane's events takes a look or exec: `pane.fs.readers`, the 9P server's), Tty9p's socket check, Pardes.configDir and startupPath (ThemeFile, DumpThemes, Config, DumpConfig, an init file saved), DumpConfig's dump directory. - trackJump no longer reads Recent at every step: a closed file's jump stays; the jump to it drops that file's entries when Recent has forgotten it, as when the file is gone. - noteClosed asks the host (recentKeeps, a taped stat) before it looks in Recent: the same syscall on every side. - The tty toggle, read on every key, is session state: Pardes.tty_toggle, from the options when the core is made, dumped. - The rollback copy no longer saves `store` and `recent` (rollback.zig `unsaved`): their bytes put back pointed at lists the host's heap (recentGpa) had freed, which the epoch never holds. A speculated Del then a Panes listing read a freed name (H8, the segfault). Tests, each failing before: a rollback leaves the store and Recent as a speculation left them; a replica joined after a pane closed has the session's jump to it, its Panes and Recent rows, asked by either (and the variant hunt's Recent written by another window); a replica's Del of a file its session's Recent knows asks the host what the session asked. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/replica.zig b/src/replica.zig
index d6a2e0ef..8b1d84f4 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -3337,3 +3337,105 @@ test "a far join is sent each blob once: joined again with them on disk or in th
try std.testing.expectEqual(@as(u64, 0), j.sent_bytes);
endJoin(&j, &have);
}
+
+/// A replica joined from the session's dump as it is now, between units,
+/// as `Peers.join` sends it: its state, its blobs put back where they go.
+fn joinedNow(a: *Authority, id: u16, view: u8) !*Replica {
+ const gpa = std.testing.allocator;
+ const was = Stepping.begin(&a.t, null);
+ var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty;
+ defer blobs.deinit(a.core.gpa);
+ const state = try pardes.dump.dumpJoin(a.core, &blobs);
+ defer a.core.gpa.free(state);
+ tape.resync(&a.t);
+ was.end();
+ var whole: std.ArrayListUnmanaged(u8) = .empty;
+ defer whole.deinit(gpa);
+ var pos: usize = 0;
+ for (blobs.items) |b| {
+ try whole.appendSlice(gpa, state[pos..b.at]);
+ try whole.appendSlice(gpa, b.bytes);
+ pos = b.at;
+ }
+ try whole.appendSlice(gpa, state[pos..]);
+ return Replica.initDump(gpa, id, view, whole.items, .{ .cols = 80, .rows = 24 });
+}
+
+fn agrees(a: *Authority, r: *Replica) !void {
+ if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why});
+ try std.testing.expect(r.diverged == null);
+ try std.testing.expect(r.quiet());
+ try std.testing.expectEqual(digestOf(a), r.digest());
+}
+
+test "a replica joined after a pane closed: the session's jump to it, its Panes and Recent rows, asked by either" {
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
+ defer a.deinit();
+ // A file looked at and closed: a jump to it, Recent's, a closed pane.
+ var cwd: [std.fs.max_path_bytes]u8 = undefined;
+ var line: [std.fs.max_path_bytes + 32]u8 = undefined;
+ const dir = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd)));
+ _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/build.zig.zon", .{dir}) });
+ _ = try a.local(.{ .command = "Del" });
+ try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len);
+ try std.testing.expect(a.core.njumps > 1);
+ // What another window of the user's wrote to the store, which the
+ // session read (src/variants_test.zig's case, the variant hunt's).
+ a.core.recent.opened(a.core.recentGpa(), try std.fmt.bufPrint(&line, "{s}/build.zig", .{dir}));
+ const r = try joinedNow(a, 1, 0);
+ defer r.deinit();
+ try std.testing.expectEqual(digestOf(a), r.digest());
+ r.confirm(try a.local(.{ .key = .{ .cp = 'j', .text = "j" } }));
+ r.confirm(try a.local(.{ .command = "Search Panes" }));
+ r.confirm(try a.local(.{ .command = "Search Recent" }));
+ try agrees(a, r);
+ // The replica's own: it waits for the session's rows.
+ for ([_][]const u8{ "Search Panes", "Search Recent" }) |asked| {
+ const msg = try gpa.dupe(u8, try r.input(.{ .command = asked }));
+ defer gpa.free(msg);
+ r.confirm(try a.take(msg));
+ r.settle();
+ }
+ try agrees(a, r);
+ // The closed pane brought back by the replica's Look at it: the
+ // session's record of it, off the tape.
+ {
+ const closed = try std.fmt.bufPrint(&line, "Look @p{d}", .{a.core.store.closed.items[0].serial});
+ const msg = try gpa.dupe(u8, try r.input(.{ .command = closed }));
+ defer gpa.free(msg);
+ r.confirm(try a.take(msg));
+ r.settle();
+ try std.testing.expectEqual(@as(usize, 0), a.core.store.closed.items.len);
+ try agrees(a, r);
+ r.confirm(try a.local(.{ .command = "Del" }));
+ }
+ // A jump back onto the closed file: opened again, from Recent.
+ for (0..3) |_| {
+ const msg = try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'o', .ctrl = true } }));
+ defer gpa.free(msg);
+ r.confirm(try a.take(msg));
+ r.settle();
+ }
+ try agrees(a, r);
+}
+
+test "a replica's Del of a file its session's Recent knows asks the host what the session asked" {
+ 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 cwd: [std.fs.max_path_bytes]u8 = undefined;
+ var line: [std.fs.max_path_bytes + 32]u8 = undefined;
+ const dir = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd)));
+ _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/build.zig.zon", .{dir}) });
+ // Joined while it is open: in the session's Recent, in no replica's.
+ const r = try joinedNow(a, 1, 0);
+ defer r.deinit();
+ const msg = try gpa.dupe(u8, try r.input(.{ .command = "Del" }));
+ defer gpa.free(msg);
+ r.confirm(try a.take(msg));
+ r.settle();
+ try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len);
+ try agrees(a, r);
+}
+