From 60caa16b99c60713c5ae27832d76809a2b4a2d5a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 16:15:14 -0300 Subject: blob cache: the trim keeps the most recently used texts that fit under the cap, the newest first, and lets the rest go, where it let the least recently used go until the rest was under it: a text past the cap alone (a big file's, edited since the client joined) took every older one with it, the small ones that fit too, so a client after it with that cap was sent them all again (daemon.py's far clients E and F at a 1 MiB cap: F found nothing on disk, the 6.5 KB notes gone with the 1.1 MB file's texts; now the notes stay); tests: replica.zig's 'the blob cache stays under its cap, the least recently used going first, the file compacted' becomes '... going first and one past the cap alone without them, the file compacted', a 20 KB text past its 15 KB cap put newest, gone, and the three older ones kept Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/replica.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index b98d792b..e104391f 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -3555,7 +3555,7 @@ test "a cached blob is hashed again as it is read: one kept cut short, corrupted try std.testing.expectEqualSlices(u8, &text, &out); } -test "the blob cache stays under its cap, the least recently used going first, the file compacted" { +test "the blob cache stays under its cap, the least recently used going first and one past the cap alone without them, the file compacted" { const io = std.testing.io; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); @@ -3583,6 +3583,15 @@ test "the blob cache stays under its cap, the least recently used going first, t try std.testing.expect(!c.has(io, &hashes[1], 5000)); try std.testing.expect(c.has(io, &hashes[2], 5000) and c.has(io, &hashes[3], 5000)); try std.testing.expect((try std.Io.Dir.cwd().statFile(io, c.path(), .{})).size < before); + // The newest past the cap alone (an edited big file's text): it goes, + // and the older ones, which fit, stay. + const big: [4 * 5000]u8 = @splat('z'); + var big_hash: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(&big, &big_hash, .{}); + c.put(io, &.{.{ .hash = big_hash, .bytes = &big }}, &.{}); + c.trim(io); + try std.testing.expect(!c.has(io, &big_hash, big.len)); + for ([_]usize{ 0, 2, 3 }) |k| try std.testing.expect(c.has(io, &hashes[k], 5000)); // Room for nothing: everything goes. Cache.of(c.path(), 0).trim(io); for (&hashes) |*h| try std.testing.expect(!c.has(io, h, 5000)); -- cgit v1.3