diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 23:28:13 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 23:42:48 -0300 |
| commit | 9b20d00ca5ffbab640b87249d65e4165414d22c1 (patch) | |
| tree | e62644195d40b5a41ed92a5e19548fa84cff4821 /src/tape.zig | |
| parent | 6c61169832d6f396ee7a6e9e0b09d9c055d08f8d (diff) | |
| download | pardes-9b20d00ca5ffbab640b87249d65e4165414d22c1.tar.gz pardes-9b20d00ca5ffbab640b87249d65e4165414d22c1.zip | |
replica: a record of a MiB or more crosses to a client by its hash, beside its unit (tape.zig by_hash_min, Big; wire blob): a file over the wire's 16 MiB bound opened while a client was attached was one unit, past the bound, so a Debug session panicked and a release one dropped it, its client diverging at the next unit and joining again to be sent the whole file in the dump; now the read's answer is recorded as the file's BLAKE3, a client on the session's machine reads the path itself and checks it, a far one is sent it in 1 MiB pieces before the unit and keeps it in its cache.xitdb (XDG_CACHE_HOME/pardes), so a far client that joins after is sent none of it; any other record that long (a reload's text, a paste) goes to every client so; test/daemon.py by_hash: a 17 MiB file under a near and a far client, both agree, neither joins again, the near one reads it itself, the far one is sent 8848 KB of zstd and a far client after it none of its 2 blobs; the slot's own BLAKE3 the brief asked for was built and taken out (a hash a kind's open, 7 instructions a byte on the perf build's CPU, for no reader until a client renders kinds); perf --only medium --reps 10 instructions:u 2,459.25M -> 2,459.36M, --terminal-mib 8 3,928.50M -> 3,928.51M; tui core-test 811/950 (139 skipped), shell-test passes, replicas pass (diverged 0, checked/wrong 0), behaviour-test 269/273 (the four addon ABI tests the base fails), --replay 20 seeds 0 failures, process-test 2 of 144 failed (lspcomplete and lspcompletemoved, the base's; headersweep and headersweep-restored failed once at load 14 and pass alone), test/daemon.py passes
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/tape.zig')
| -rw-r--r-- | src/tape.zig | 120 |
1 files changed, 110 insertions, 10 deletions
diff --git a/src/tape.zig b/src/tape.zig index eaae601e..9dca5ee8 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -183,7 +183,27 @@ fn Watched(comptime name: []const u8) type { return if (@hasField(Pardes.View, name)) @FieldType(Pardes.View, name) else @FieldType(Pardes, name); } -const Kind = enum(u8) { input, done, host, ret, set, digest }; +/// `blob`: a record of `by_hash_min` bytes or more, in the replica stream, +/// as its kind and its bytes' BLAKE3 (`Big`): its bytes go beside the unit. +const Kind = enum(u8) { input, done, host, ret, set, digest, blob }; +/// A record at least this long crosses to a replica by hash: a unit stays +/// under the wire's bound (16 MiB) whatever a file read holds. +pub const by_hash_min = 1 << 20; +/// A record's bytes by hash: the session's (`Tape.big`), to send; a +/// replica's (`Tape.files`), to replay. `path`: a file read's answer, which +/// a replica on the session's machine reads itself, its hash the file's +/// own bytes' (after the answer's 9: yes, a length), as a dump's text is. +pub const Big = struct { + hash: [32]u8, + bytes: []u8, + path: bool = false, + + pub fn hashOf(bytes: []const u8, path: bool) [32]u8 { + var h: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(if (path) bytes[9..] else bytes, &h, .{}); + return h; + } +}; /// A digest record's kind byte (replica.zig reads a unit's last). pub const digest_kind: u8 = @backingInt(Kind.digest); pub const set_kind: u8 = @backingInt(Kind.set); @@ -275,13 +295,36 @@ pub const Tape = struct { /// time, so the guess a speculation recorded is what the authority /// records (a message's millisecond, an edit's second never miss). stamp: ?Guess = null, + /// The replica stream's records by hash (`unit_digests`), until the + /// unit is sent (src/replica.zig `Peers.unit`). + big: std.ArrayListUnmanaged(Big) = .empty, + /// A replica's: the last few records by hash it was sent or read. + files: std.ArrayListUnmanaged(Big) = .empty, + /// A replica on the session's machine: a file read by hash is read + /// here, by its path, and checked. + local_files: bool = false, + /// The path a `read_file` site reads, while it is recorded or replayed. + reading: ?[:0]const u8 = null, fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { t.scratch.clearRetainingCapacity(); walk.encode(T, v, &t.scratch.writer) catch @panic("tape: out of memory"); - const payload = t.scratch.written(); + var payload = t.scratch.written(); + var kind_ = kind; + var by_hash: [34]u8 = undefined; + if (t.unit_digests and payload.len >= by_hash_min) { + // A file read's yes, by the file's bytes' hash. + const path = t.reading != null and kind == .ret and payload[0] == 1; + by_hash[0] = @backingInt(kind); + by_hash[1..33].* = Big.hashOf(payload, path); + by_hash[33] = @intFromBool(path); + const bytes = t.gpa.dupe(u8, payload) catch @panic("tape: out of memory"); + t.big.append(t.gpa, .{ .hash = by_hash[1..33].*, .bytes = bytes, .path = path }) catch @panic("tape: out of memory"); + payload = &by_hash; + kind_ = .blob; + } const len: u32 = @intCast(payload.len); - t.buf.append(t.gpa, @backingInt(kind)) catch @panic("tape: out of memory"); + t.buf.append(t.gpa, @backingInt(kind_)) catch @panic("tape: out of memory"); t.buf.appendSlice(t.gpa, std.mem.asBytes(&len)) catch @panic("tape: out of memory"); t.buf.appendSlice(t.gpa, payload) catch @panic("tape: out of memory"); if (t.buf.items.len > 1 << 20) t.flush(); @@ -386,12 +429,45 @@ pub const Tape = struct { const head = t.in.take(5) catch return null; const len = std.mem.bytesToValue(u32, head[1..5]); const payload = t.in.take(len) catch return null; - return .{ .kind = @fromBackingInt(@intCast(head[0])), .payload = .{ .bytes = payload } }; + const kind: Kind = @fromBackingInt(@intCast(head[0])); + if (kind != .blob) return .{ .kind = kind, .payload = .{ .bytes = payload } }; + if (payload.len != 34) { + t.diverge("a record by hash is short", .{}); + return null; + } + const bytes = t.byHash(payload[1..33], payload[33] != 0) orelse { + t.diverge("a record by hash {x} is not here", .{payload[1..9]}); + return null; + }; + return .{ .kind = @fromBackingInt(@intCast(payload[0])), .payload = .{ .bytes = bytes } }; } fn peek(t: *const Tape) ?Kind { if (t.in.bytes.len - t.in.at < 5) return null; - return @fromBackingInt(@intCast(t.in.bytes[t.in.at])); + const kind: Kind = @fromBackingInt(@intCast(t.in.bytes[t.in.at])); + if (kind == .blob and t.in.bytes.len - t.in.at > 5) return @fromBackingInt(@intCast(t.in.bytes[t.in.at + 5])); + return kind; + } + + /// The bytes of a record by hash: sent, or read here (a file read's + /// answer, on the session's machine), each checked against its hash. + fn byHash(t: *Tape, hash: *const [32]u8, is_path: bool) ?[]const u8 { + for (t.files.items) |f| if (std.mem.eql(u8, &f.hash, hash)) return f.bytes; + const path = t.reading orelse return null; + if (!t.local_files or !is_path) return null; + const file = pardes.filesystem.readHost(t.gpa, path, pardes.memory.limits.max_file_bytes) catch return null; + defer t.gpa.free(file); + // The answer as `record` wrote it: an error union's yes, a length, the bytes. + const bytes = t.gpa.alloc(u8, 9 + file.len) catch return null; + bytes[0] = 1; + std.mem.writeInt(u64, bytes[1..9], file.len, .little); + @memcpy(bytes[9..], file); + if (!std.mem.eql(u8, &Big.hashOf(bytes, true), hash)) { + t.gpa.free(bytes); + return null; + } + keepFile(t, .{ .hash = hash.*, .bytes = bytes, .path = true }); + return bytes; } fn diverge(t: *Tape, comptime why: []const u8, args: anytype) void { @@ -412,7 +488,7 @@ pub const Tape = struct { .set => t.applySet(&rec.payload), .host => t.skipHost(), .digest => t.check(rec.payload.bytes), - .done => return t.diverge("a host call's records are out of order", .{}), + .done, .blob => return t.diverge("a host call's records are out of order", .{}), } } } @@ -444,7 +520,7 @@ pub const Tape = struct { .host => if (t.skips_render) t.skipHost() else return t.diverge("the core did not make the host call {d} the tape has", .{std.mem.bytesToValue(u16, rec.payload.bytes[0..2])}), .input => t.apply(&rec.payload), .digest => t.check(rec.payload.bytes), - .ret => return t.diverge("the core's input ended before the recorded one", .{}), + .ret, .blob => return t.diverge("the core's input ended before the recorded one", .{}), } } } @@ -465,7 +541,7 @@ pub const Tape = struct { .input => t.apply(&rec.payload), .set => t.applySet(&rec.payload), .digest => t.check(rec.payload.bytes), - .host, .done => { + .host, .done, .blob => { t.diverge("a host call's records are out of order", .{}); return false; }, @@ -917,6 +993,10 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt // No tape where there is no host to record or replay one. const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); const id: u16 = @as(u16, host_fns.len) + @backingInt(s); + if (comptime s == .read_file) t.reading = args[1]; + defer if (comptime s == .read_file) { + t.reading = null; + }; if (t.replay) { // Diverged (said in `diverged`): what a speculation gets, harmless, // for the rest of the input. A replica joins again; a replay says so. @@ -955,6 +1035,13 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt pub const Guess = struct { real_ns: i64, mono_ns: i64, gmtoff: i32 }; +/// A record's bytes by hash kept for a replica's replays (`files`, the +/// tape's allocator's): the last eight, a rollback's units' and the next's. +pub fn keepFile(t: *Tape, b: Big) void { + if (t.files.items.len == 8) t.gpa.free(t.files.orderedRemove(0).bytes); + t.files.append(t.gpa, b) catch t.gpa.free(b.bytes); +} + /// A clock read of the session's, as a replica client guesses it: a file's /// mtime (realtime seconds), a message's clock (monotonic ms), a message's /// time of day (in the session's zone). @@ -1129,6 +1216,10 @@ pub fn memory(gpa: std.mem.Allocator) Tape { /// What a tape in memory holds, given back (its core is gone already). pub fn release(t: *Tape) void { + for (t.big.items) |b| t.gpa.free(b.bytes); + t.big.deinit(t.gpa); + for (t.files.items) |b| t.gpa.free(b.bytes); + t.files.deinit(t.gpa); t.answers.deinit(t.gpa); t.buf.deinit(t.gpa); t.scratch.deinit(); @@ -1154,7 +1245,7 @@ pub fn replayUnit(t: *Tape, bytes: []const u8) ?[]const u8 { }, .set => t.applySet(&rec.payload), .digest => t.check(rec.payload.bytes), - .host, .ret, .done => t.diverge("a {t} record at the top", .{kind}), + .host, .ret, .done, .blob => t.diverge("a {t} record at the top", .{kind}), } } return t.diverged; @@ -1210,7 +1301,16 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { while (true) { const head = r.take(5) catch return; const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return; - const kind: Kind = @fromBackingInt(@intCast(head[0])); + const raw: Kind = @fromBackingInt(@intCast(head[0])); + // A record by hash, as the kind it stands for (its bytes are not here). + const kind: Kind = if (raw == .blob and payload.len > 0) @fromBackingInt(@intCast(payload[0])) else raw; + if (raw == .blob) { + w.splatByteAll(' ', depth * 2) catch {}; + w.print("{t} by hash\n", .{kind}) catch {}; + if (kind == .done or kind == .ret) depth -|= 1; + if (kind == .input or kind == .host) depth += 1; + continue; + } if (kind == .done or kind == .ret) depth -|= 1; w.splatByteAll(' ', depth * 2) catch {}; switch (kind) { |
