const std = @import("std"); const zat = @import("zat"); const archive_mod = @import("../storage/archive.zig"); const cursor_mod = @import("cursor.zig"); const ingest = @import("ingest.zig"); const meta_store = @import("../storage/meta_store.zig"); const repair = @import("repair.zig"); const repos = @import("backfill/repos.zig"); const segment = @import("../storage/segment.zig"); const tail_mod = @import("../serve/tail.zig"); const verify = @import("verify.zig"); const Io = std.Io; test "repair completion durably emits tombstone then replacement offline" { const testing = std.testing; var threaded: Io.Threaded = .init(testing.allocator, .{}); defer threaded.deinit(); const io = threaded.io(); var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); var root_buf: [Io.Dir.max_path_bytes]u8 = undefined; const root_len = try tmp.dir.realPath(io, &root_buf); const root = root_buf[0..root_len]; var meta = try meta_store.Store.open(testing.allocator, root); defer meta.deinit(); var archive = try archive_mod.Archive.init(testing.allocator, io, root); defer archive.deinit(); var tail = tail_mod.Tail.init(testing.allocator, io, 8 * 1024 * 1024); defer tail.deinit(); tail.enableDurabilityGate(0); var cursor = cursor_mod.Store.init(&meta); var consumer: ingest.Consumer = .{ .allocator = testing.allocator, .io = io, .tail = &tail, .upstream = "ws://offline.invalid", .cursor_store = &cursor, .archive = &archive, }; defer consumer.deinit(); archive.on_durable_ctx = &consumer; archive.on_durable = ingest.Consumer.onArchiveDurable; const prepared_arena = try testing.allocator.create(std.heap.ArenaAllocator); prepared_arena.* = std.heap.ArenaAllocator.init(testing.allocator); defer { prepared_arena.deinit(); testing.allocator.destroy(prepared_arena); } const alloc = prepared_arena.allocator(); const did = "did:plc:durable-repair"; const record = try zat.cbor.encodeAlloc(alloc, .{ .map = &.{ .{ .key = "$type", .value = .{ .text = "app.bsky.feed.post" } }, .{ .key = "text", .value = .{ .text = "durable offline repair" } }, } }); const record_cid = try zat.cbor.Cid.forDagCbor(alloc, record); var tree = zat.mst.Mst.init(alloc); try tree.put("app.bsky.feed.post/3k2abcdefghij", record_cid); const data_cid = try tree.rootCid(); const keypair = try zat.Keypair.fromSecretKey(.p256, @splat(31)); const signed = try zat.signCommit(alloc, .{ .did = did, .rev = "3k2abcdefghij", .data = data_cid, }, &keypair); var blocks: std.ArrayList(zat.car.Block) = .empty; try blocks.append(alloc, .{ .cid_raw = signed.cid.raw, .data = signed.bytes }); try tree.collectBlocks(&blocks); try blocks.append(alloc, .{ .cid_raw = record_cid.raw, .data = record }); const car_bytes = try zat.car.writeAlloc(alloc, .{ .roots = &.{signed.cid}, .blocks = blocks.items }); const prepared = try repos.prepareRepo(alloc, car_bytes); var chain: verify.ChainUpdate = .{ .rev = undefined, .data_len = @intCast(data_cid.raw.len) }; @memcpy(&chain.rev, "3k2abcdefghij"); @memcpy(chain.data[0..data_cid.raw.len], data_cid.raw); var completion: repair.Completion = .{ .allocator = testing.allocator, .io = io, .did = @constCast(did), .prepared_arena = prepared_arena, .prepared = .{ .fetched = undefined, .repo = prepared }, .fetched_chain = chain, .witnessed_at = 1_784_160_000_000_000, .after_ticket = 0, .upstream_seq = 77, }; try ingest.Consumer.emitRepair(&consumer, &completion, @import("../runtime/observability.zig").root_context); try testing.expectEqual(@as(u64, 2), archive.committed_seq.load(.acquire)); try testing.expectEqual(@as(?i64, 77), try cursor.load(io)); try testing.expectEqual(@as(usize, 2), tail.gauges().entries); const retained = tail.entries.items[tail.head..]; try testing.expectEqual(@as(usize, 0), retained[0].json.len); try testing.expect(retained[0].json_v2.len > 0); try testing.expectEqual(@as(usize, 0), retained[1].json.len); try testing.expect(retained[1].json_v2.len > 0); var reloaded = verify.Verifier.init(testing.allocator, io, "http://offline.invalid", &meta); defer reloaded.deinit(); const durable_chain = (try reloaded.chainFor(did)).?; try testing.expectEqualStrings("3k2abcdefghij", &durable_chain.rev); try testing.expectEqualSlices(u8, data_cid.raw, durable_chain.dataSlice()); try archive.rotate(); var name_buf: [64]u8 = undefined; const name = archive_mod.formatSegmentName(&name_buf, 0); const bytes = try archive.dir.readFileAlloc(io, name, testing.allocator, .limited(16 * 1024 * 1024)); defer testing.allocator.free(bytes); var sealed = try segment.Sealed.parse(testing.allocator, bytes); defer sealed.deinit(testing.allocator); var block = try sealed.readBlock(testing.allocator, 0); defer block.deinit(testing.allocator); try testing.expectEqual(@as(usize, 2), block.events.len); try testing.expectEqual(segment.Kind.sync, block.events[0].kind); try testing.expectEqual(segment.Kind.create_resync, block.events[1].kind); try testing.expectEqual(@as(u64, 1), block.events[0].seq); try testing.expectEqual(@as(u64, 2), block.events[1].seq); } test "large repair keeps wire encoding scratch bounded" { const testing = std.testing; var threaded: Io.Threaded = .init(testing.allocator, .{}); defer threaded.deinit(); const io = threaded.io(); var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); var root_buf: [Io.Dir.max_path_bytes]u8 = undefined; const root_len = try tmp.dir.realPath(io, &root_buf); const root = root_buf[0..root_len]; var meta = try meta_store.Store.open(testing.allocator, root); defer meta.deinit(); var archive = try archive_mod.Archive.init(testing.allocator, io, root); defer archive.deinit(); // Make every 1,024-row batch durable so the tiny tail can retire it. The // cap below therefore measures repair-owned scratch, not the configured // subscriber retention window. archive.flush_interval_us = 0; var tail = tail_mod.Tail.init(testing.allocator, io, 64 * 1024); defer tail.deinit(); tail.enableDurabilityGate(0); var cursor = cursor_mod.Store.init(&meta); var repair_alloc = std.heap.DebugAllocator(.{ .enable_memory_limit = true }){}; defer testing.expect(repair_alloc.deinit() == .ok) catch @panic("repair allocator leak"); repair_alloc.requested_memory_limit = 1 * 1024 * 1024; var consumer: ingest.Consumer = .{ .allocator = repair_alloc.allocator(), .io = io, .tail = &tail, .upstream = "ws://offline.invalid", .cursor_store = &cursor, .archive = &archive, }; defer consumer.deinit(); archive.on_durable_ctx = &consumer; archive.on_durable = ingest.Consumer.onArchiveDurable; const prepared_arena = try testing.allocator.create(std.heap.ArenaAllocator); prepared_arena.* = std.heap.ArenaAllocator.init(testing.allocator); defer { prepared_arena.deinit(); testing.allocator.destroy(prepared_arena); } const alloc = prepared_arena.allocator(); const did = "did:plc:large-durable-repair"; const body: [512]u8 = @splat('x'); const record = try zat.cbor.encodeAlloc(alloc, .{ .map = &.{ .{ .key = "$type", .value = .{ .text = "app.bsky.feed.post" } }, .{ .key = "text", .value = .{ .text = &body } }, } }); const record_cid = try zat.cbor.Cid.forDagCbor(alloc, record); var tree = zat.mst.Mst.init(alloc); for (0..4096) |i| { const key = try std.fmt.allocPrint(alloc, "app.bsky.feed.post/r{d}", .{i}); try tree.put(key, record_cid); } const data_cid = try tree.rootCid(); const keypair = try zat.Keypair.fromSecretKey(.p256, @splat(29)); const signed = try zat.signCommit(alloc, .{ .did = did, .rev = "3k2abcdefghij", .data = data_cid, }, &keypair); var blocks: std.ArrayList(zat.car.Block) = .empty; try blocks.append(alloc, .{ .cid_raw = signed.cid.raw, .data = signed.bytes }); try tree.collectBlocks(&blocks); try blocks.append(alloc, .{ .cid_raw = record_cid.raw, .data = record }); const car_bytes = try zat.car.writeAlloc(alloc, .{ .roots = &.{signed.cid}, .blocks = blocks.items }); const prepared = try repos.prepareRepo(alloc, car_bytes); var chain: verify.ChainUpdate = .{ .rev = undefined, .data_len = @intCast(data_cid.raw.len) }; @memcpy(&chain.rev, "3k2abcdefghij"); @memcpy(chain.data[0..data_cid.raw.len], data_cid.raw); var completion: repair.Completion = .{ .allocator = testing.allocator, .io = io, .did = @constCast(did), .prepared_arena = prepared_arena, .prepared = .{ .fetched = undefined, .repo = prepared }, .fetched_chain = chain, .witnessed_at = 1_784_160_000_000_000, .after_ticket = 0, .upstream_seq = 4096, }; try ingest.Consumer.emitRepair(&consumer, &completion, @import("../runtime/observability.zig").root_context); try testing.expectEqual(@as(u64, 4097), archive.committed_seq.load(.acquire)); try testing.expectEqual(@as(?i64, 4096), try cursor.load(io)); try testing.expect(repair_alloc.total_requested_bytes < 256 * 1024); }