summaryrefslogtreecommitdiff
path: root/src/Handover.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 14:42:45 -0300
committerGabriel Schneider <[email protected]>2026-11-05 19:16:37 -0300
commit15a1b94eaa73747afaa299262a2d6e4fd6af42fd (patch)
tree46bff4f35b642521eead62bc3b5657902e5fee0b /src/Handover.zig
parent4e096f6dc6db043caa630514fa75bd18438cb15b (diff)
downloadpardes-15a1b94eaa73747afaa299262a2d6e4fd6af42fd.tar.gz
pardes-15a1b94eaa73747afaa299262a2d6e4fd6af42fd.zip
dumps: a Dump is a labelled entry of the state file (src/Db.zig, architecture §14.3), one fsynced transaction: `dumps/<label>` holds the session as a database of its own, which the walk writes by field name into xitdb's maps (walk.Put: a struct a map keyed by its fields' names, a union a map of its one field, records a list, plain numbers one bytes value tagged with their kind and width, an enum its name, a number a uint or an int, an optional an absent key, a type with dumpWrite its bytes, a .blob text of 4 KiB and up its BLAKE3 hash) and another build reads by name (walk.Get: defaults, notes, a pane it cannot read left out, as the byte loader did), with the hashes of the texts it keeps once under `blobs/`, so an unchanged text is written once across dumps; labels are `pardes-<date>-<time>` (`-2` and on in one second), `last` names the newest, the newest 32 are kept and the texts no kept one names are dropped in the same transaction, and at a session's end the file is compacted into `pardes.xitdb.new` and renamed over once it is past twice what it holds plus 64 MiB (another session notices the new file by its inode); a dump travels as an image, a database in memory holding it alone, deterministic (std.Io.failing's zero ids, so the replay's fixpoint holds): what Dump hands its host, what `Dump export <path>` and $PARDES_DUMP write whole (the snapshot goldens' `Restore <path>` unchanged), what a Restore of a label is read into, what a handover carries (`handover-<seq>`), and what `-l` and `Restore <path>` read; Restore takes a label or a path (bare: this session's last dump, else the newest), a label the file keeps none of refused in words (`Restore: no dump labelled X`, asked of the authority as the closed panes are); `/dumps` lists them, `<label> <date> <size> <panes>`, the newest first; Upgrade commits `upgrade-<pid>` before the exec, its record naming the state file and the label (a record without one, an older build's, names its `.dump` file, read as before), and the label stays; the byte dump stays for the join, the closed panes and the old `.dump` files (dump.byteDump), and dump.outPath, uniqueIn and upgradePath go for dump.label; measured (Debug, a session with a 3.2 MB file and three scratch panes, 20 dumps): the image on the loop 64-66 ms (59 ms of it BLAKE3 of the file, 0.65 ms in ReleaseFast) where the byte dump was 0.7 ms, the commit 9-16 ms with its fsyncs where the file was written in 2, a Restore 1.8 ms to read and 11.4 to load, and the file 3.55 MB after the first dump and 379 KB a dump after it with the big text unchanged (3.58 MB a dump with it edited each time, as twenty `.dump` files would be); tests: dump.zig's 'two dumps in one second get names of their own' and 'a dump makes every directory missing on the way to its DumpDir' become Db.zig's 'two dumps with one label get labels of their own' and 'a Dump makes every directory missing on the way to its DumpDir, and the file there', 'a session read by name, field by field as another build reads it, is the same session' becomes 'a session's byte dump read by name...' beside the new 'a session's image, read by name as every load reads it, is the same session, and dumps to the same bytes again', two replica tests compare the join's stream with dump.byteDump, the Handover tests read the carried image, upgrade's record test carries a label, the root listing tests and the monkey list `dumps`, the behaviour suite gains five root-ctl tests (a Dump and Restore round trip through 9P with `/dumps` and the tag's word, a label kept none of refused in words, the 33rd dump dropping the first, an unchanged text written once across two dumps by the file's size, a killed Dump leaving the one before it whole), the test host performs a Dump's write and a Restore as a host does, the monkey lets go of its log follower at a Restore, and daemon.py's upgrade case finds the kept dump in `/dumps`
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/Handover.zig')
-rw-r--r--src/Handover.zig61
1 files changed, 49 insertions, 12 deletions
diff --git a/src/Handover.zig b/src/Handover.zig
index d245a6af..2233d228 100644
--- a/src/Handover.zig
+++ b/src/Handover.zig
@@ -1,7 +1,8 @@
//! Panes between windows, as a browser's tabs go between its windows. A
//! pane's grip, or a column's, released past the window's edge carries it
-//! out: dumped alone (`Carried`, each pane as dump.savePane makes it), it
-//! opens in a new window of its own, `pardes-gui -l` of that dump. The pane
+//! out: dumped alone (`Carried`, each pane as dump.savePane makes it, in
+//! the image of one dump, `handover-<seq>`: Db.zig), it opens in a new
+//! window of its own, `pardes-gui -l` of that dump. The pane
//! closes here only once
//! the new window answers on its 9P socket, so its text is never in no
//! window at all; a failed handover leaves it where it was, and a pane
@@ -35,7 +36,8 @@ const libc = std.c;
/// to it, or a whole column.
pub const Kind = enum { pane, column };
-/// A handover's dump: `carried_magic`, then this.
+/// What a handover's image holds, as its root `carried` (a build before
+/// the image's, 0.24, wrote `carried_magic`, then this as a byte dump).
const Carried = struct {
kind: Kind,
/// A carried column's tag.
@@ -114,7 +116,7 @@ pub fn begin(h: *Handover, p: *Pardes, carried: Kind, at: usize) void {
else
"only text panes go to another window, for now");
}
- const bytes = dumpCarried(p, carried, at, ids[0..n]) catch |err| return p.reportError(said, "handover", err);
+ const bytes = dumpCarried(p, carried, at, ids[0..n], h.seq +% 1) catch |err| return p.reportError(said, "handover", err);
h.deinit(p.gpa);
h.bytes = bytes;
h.seq +%= 1;
@@ -180,8 +182,8 @@ pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]co
}
/// The dump of panes `ids` (gpa's), carried as `kind` from column `at`'s
-/// (a pane's, `at` is its slot).
-fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize) ![]u8 {
+/// (a pane's, `at` is its slot): an image of one dump, handover `seq`.
+fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize, seq: u32) ![]u8 {
const arena = p.scratch.allocator();
const bytes = try arena.alloc([]const u8, ids.len);
const weights = try arena.alloc(f32, ids.len);
@@ -200,8 +202,44 @@ fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize) ![]u8 {
.weights = weights,
.owners = owners,
};
- const body = try walk.save(Carried, arena, &value);
- return std.mem.concat(p.gpa, u8, &.{ carried_magic, body });
+ var buf: std.Io.Writer.Allocating = .init(p.gpa);
+ defer buf.deinit();
+ var db = try walk.Mem.init(std.Io.failing, .{ .buffer = &buf, .file_id = 0 });
+ var w: walk.Put = .{ .gpa = p.gpa };
+ defer w.deinit();
+ try db.runContext(struct {
+ w: *walk.Put,
+ value: *const Carried,
+ pub fn run(ctx: @This(), cursor: *walk.Mem.Cursor(.read_write)) !void {
+ try ctx.w.member(try walk.Mem.HashMap(.read_write).init(cursor.*), "carried", Carried, ctx.value);
+ }
+ }{ .w = &w, .value = &value });
+ var name: [32]u8 = undefined;
+ return pardes.Db.image(p.gpa, std.fmt.bufPrint(&name, "handover-{d}", .{seq}) catch "handover", buf.written(), w.blobs.items, ids.len);
+}
+
+/// Whether `bytes` are a handover's dump (`dumpCarried`'s, or an older
+/// build's), not a session's.
+pub fn carries(bytes: []const u8) bool {
+ if (std.mem.startsWith(u8, bytes, carried_magic)) return true;
+ if (!pardes.Db.isImage(bytes)) return false;
+ const r = pardes.Db.Read.open(std.heap.page_allocator, bytes, null) catch return false;
+ defer r.close();
+ return (walk.Get.member(r.root(), "carried") catch null) != null;
+}
+
+/// The carried panes in `bytes`, made in `a`.
+fn readCarried(a: std.mem.Allocator, bytes: []const u8) !Carried {
+ if (pardes.Db.isImage(bytes)) {
+ const r = pardes.Db.Read.open(a, bytes, null) catch return error.NotCarried;
+ defer r.close();
+ const at = (walk.Get.member(r.root(), "carried") catch return error.NotCarried) orelse return error.NotCarried;
+ var l: walk.Get = .init(a);
+ defer l.deinit();
+ return l.get(Carried, at) catch error.NotCarried;
+ }
+ if (!std.mem.startsWith(u8, bytes, carried_magic)) return error.NotCarried;
+ return walk.load(Carried, a, bytes[carried_magic.len..], null);
}
// ---- the receiving window ----
@@ -212,10 +250,9 @@ fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize) ![]u8 {
/// alone. Every other pane stays where it was. Answers the slot of the
/// first pane taken; on an error nothing of it is left here.
pub fn adopt(p: *Pardes, bytes: []const u8) !usize {
- if (!std.mem.startsWith(u8, bytes, carried_magic)) return error.NotCarried;
var arena: std.heap.ArenaAllocator = .init(p.gpa);
defer arena.deinit();
- const st = try walk.load(Carried, arena.allocator(), bytes[carried_magic.len..], null);
+ const st = try readCarried(arena.allocator(), bytes);
const carried = st.kind;
if (st.panes.len == 0 or st.weights.len != st.panes.len or st.owners.len != st.panes.len) return error.NotCarried;
// Where the pointer is, else the middle of the pane with the keyboard.
@@ -327,7 +364,7 @@ pub const Job = struct {
pub fn dumpPath(gpa: std.mem.Allocator, seq: u32) ![:0]u8 {
var dir_buf: [1024]u8 = undefined;
const dir: []const u8 = if (libc.getenv("XDG_RUNTIME_DIR")) |x| std.mem.span(x) else dump.defaultDirectory(&dir_buf) orelse return error.NoDirectory;
- return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.dump", .{ dir, libc.getpid(), seq }, 0);
+ return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.xitdb", .{ dir, libc.getpid(), seq }, 0);
}
/// Runs the handover's carrier on `io` (io.concurrent), kept in `h.task`
@@ -494,7 +531,7 @@ test "a grip let go past the window's edge dumps its pane alone, and closes it o
try std.testing.expectEqual(before, p.rects[other]);
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
- const carried = try walk.load(Carried, arena.allocator(), p.handover.bytes.?[carried_magic.len..], null);
+ const carried = try readCarried(arena.allocator(), p.handover.bytes.?);
try std.testing.expectEqual(Kind.pane, carried.kind);
try std.testing.expectEqual(@as(usize, 1), carried.panes.len);
const pane = try walk.load(pardes.Pane, arena.allocator(), carried.panes[0], null);