1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//! Database-wide offline JSS inspection.
//!
//! This is the Stream storage-layout equivalent of upstream `inspect-all`:
//! aggregate every segment in the steady tree and the bootstrap live-capture
//! tree without opening RocksDB or starting any network component.
const std = @import("std");
const operational = @import("operational.zig");
const segment_writer = @import("../storage/segment_writer.zig");
const Allocator = std.mem.Allocator;
const Io = std.Io;
pub const Options = struct {
skip_unsealed: bool = false,
};
pub const SegmentSummary = struct {
index: u64,
sealed: bool,
event_count: u64,
block_count: usize,
size_bytes: usize,
};
pub const TreeAggregate = struct {
dir: []const u8,
sealed_count: usize = 0,
active_count: usize = 0,
compressed_bytes: u64 = 0,
uncompressed_bytes: u64 = 0,
disk_bytes: u64 = 0,
event_count: u64 = 0,
block_count: u64 = 0,
oldest_mtime_us: i64 = 0,
newest_mtime_us: i64 = 0,
min_seq: u64 = 0,
max_seq: u64 = 0,
min_witnessed_at: i64 = 0,
max_witnessed_at: i64 = 0,
latest_segment: ?SegmentSummary = null,
};
pub const CollectionAggregate = struct {
nsid: []const u8,
event_count: u64 = 0,
segment_count: usize = 0,
block_count: u64 = 0,
};
pub const NetworkTotals = struct {
segments: usize = 0,
sealed_segments: usize = 0,
active_segments: usize = 0,
blocks: u64 = 0,
events: u64 = 0,
collections: usize = 0,
compressed_bytes: u64 = 0,
uncompressed_bytes: u64 = 0,
disk_bytes: u64 = 0,
min_seq: u64 = 0,
max_seq: u64 = 0,
min_witnessed_at: i64 = 0,
max_witnessed_at: i64 = 0,
};
pub const Aggregate = struct {
arena: std.heap.ArenaAllocator,
trees: []const TreeAggregate,
collections: []const CollectionAggregate,
network: NetworkTotals,
warnings: []const []const u8,
pub fn deinit(self: *Aggregate) void {
self.arena.deinit();
}
};
const FileRecord = struct {
index: u64,
name: []const u8,
mtime_us: i64,
};
pub fn inspectAll(allocator: Allocator, io: Io, data_dir: []const u8, options: Options) !Aggregate {
var arena = std.heap.ArenaAllocator.init(allocator);
errdefer arena.deinit();
const alloc = arena.allocator();
const steady = try std.fs.path.join(alloc, &.{ data_dir, "segments" });
const bootstrap = try std.fs.path.join(alloc, &.{ data_dir, "backfill", "live", "segments" });
const roots = [_][]const u8{ steady, bootstrap };
var trees: std.ArrayList(TreeAggregate) = .empty;
var collections: std.ArrayList(CollectionAggregate) = .empty;
var collection_ids: std.StringHashMapUnmanaged(usize) = .empty;
var warnings: std.ArrayList([]const u8) = .empty;
for (roots) |root| {
try trees.append(alloc, try scanTree(
alloc,
io,
root,
options,
&collections,
&collection_ids,
&warnings,
));
}
std.mem.sort(CollectionAggregate, collections.items, {}, struct {
fn lessThan(_: void, a: CollectionAggregate, b: CollectionAggregate) bool {
return a.event_count > b.event_count or
(a.event_count == b.event_count and std.mem.order(u8, a.nsid, b.nsid) == .lt);
}
}.lessThan);
const collection_slice = try collections.toOwnedSlice(alloc);
const tree_slice = try trees.toOwnedSlice(alloc);
return .{
.arena = arena,
.trees = tree_slice,
.collections = collection_slice,
.network = computeNetwork(tree_slice, collection_slice.len),
.warnings = try warnings.toOwnedSlice(alloc),
};
}
fn scanTree(
allocator: Allocator,
io: Io,
root: []const u8,
options: Options,
collections: *std.ArrayList(CollectionAggregate),
collection_ids: *std.StringHashMapUnmanaged(usize),
warnings: *std.ArrayList([]const u8),
) !TreeAggregate {
var tree: TreeAggregate = .{ .dir = root };
var dir = Io.Dir.cwd().openDir(io, root, .{ .iterate = true }) catch |err| switch (err) {
error.FileNotFound => return tree,
else => return err,
};
defer dir.close(io);
var files: std.ArrayList(FileRecord) = .empty;
var iterator = dir.iterate();
while (try iterator.next(io)) |entry| {
if (entry.kind == .directory) continue;
const index = parseSegmentIndex(entry.name) orelse continue;
const stat = try dir.statFile(io, entry.name, .{});
try files.append(allocator, .{
.index = index,
.name = try allocator.dupe(u8, entry.name),
.mtime_us = stat.mtime.toMicroseconds(),
});
}
if (files.items.len == 0) return tree;
std.mem.sort(FileRecord, files.items, {}, struct {
fn lessThan(_: void, a: FileRecord, b: FileRecord) bool {
return a.index < b.index;
}
}.lessThan);
tree.oldest_mtime_us = files.items[0].mtime_us;
tree.newest_mtime_us = files.items[0].mtime_us;
for (files.items, 0..) |file, at| {
tree.oldest_mtime_us = @min(tree.oldest_mtime_us, file.mtime_us);
tree.newest_mtime_us = @max(tree.newest_mtime_us, file.mtime_us);
const path = try std.fs.path.join(allocator, &.{ root, file.name });
var inspection = operational.inspectSegment(allocator, io, path) catch |err| {
// The highest-index file may rotate between readdir and inspect.
if (at + 1 != files.items.len)
try warnings.append(allocator, try std.fmt.allocPrint(allocator, "{s}: {s}", .{ path, @errorName(err) }));
continue;
};
defer inspection.deinit();
tree.disk_bytes += inspection.file_size;
if (inspection.sealed)
tree.sealed_count += 1
else
tree.active_count += 1;
tree.latest_segment = .{
.index = file.index,
.sealed = inspection.sealed,
.event_count = inspection.total_events,
.block_count = inspection.blocks.len,
.size_bytes = inspection.file_size,
};
if (!inspection.sealed and options.skip_unsealed) continue;
try foldInspection(&tree, &inspection, allocator, collections, collection_ids);
}
return tree;
}
fn foldInspection(
tree: *TreeAggregate,
inspection: *const operational.Inspection,
allocator: Allocator,
collections: *std.ArrayList(CollectionAggregate),
collection_ids: *std.StringHashMapUnmanaged(usize),
) !void {
tree.event_count += inspection.total_events;
tree.block_count += inspection.blocks.len;
for (inspection.blocks) |block| {
tree.compressed_bytes += block.compressed_size;
tree.uncompressed_bytes += block.uncompressed_size;
}
if (inspection.total_events > 0) {
if (tree.min_seq == 0 or inspection.min_seq < tree.min_seq) tree.min_seq = inspection.min_seq;
tree.max_seq = @max(tree.max_seq, inspection.max_seq);
if (tree.min_witnessed_at == 0 or inspection.min_witnessed_at < tree.min_witnessed_at)
tree.min_witnessed_at = inspection.min_witnessed_at;
tree.max_witnessed_at = @max(tree.max_witnessed_at, inspection.max_witnessed_at);
}
const block_counts = try allocator.alloc(u64, inspection.collections.len);
@memset(block_counts, 0);
for (inspection.block_collections) |ids| for (ids) |id| {
if (id < block_counts.len) block_counts[id] += 1;
};
for (inspection.collections, 0..) |collection, collection_id| {
if (isSentinel(collection.nsid)) continue;
const aggregate_id = collection_ids.get(collection.nsid) orelse new: {
const owned = try allocator.dupe(u8, collection.nsid);
const id = collections.items.len;
try collections.append(allocator, .{ .nsid = owned });
try collection_ids.put(allocator, owned, id);
break :new id;
};
collections.items[aggregate_id].event_count += collection.event_count;
collections.items[aggregate_id].segment_count += 1;
collections.items[aggregate_id].block_count += block_counts[collection_id];
}
}
fn computeNetwork(trees: []const TreeAggregate, collection_count: usize) NetworkTotals {
var network: NetworkTotals = .{ .collections = collection_count };
for (trees) |tree| {
network.segments += tree.sealed_count + tree.active_count;
network.sealed_segments += tree.sealed_count;
network.active_segments += tree.active_count;
network.blocks += tree.block_count;
network.events += tree.event_count;
network.compressed_bytes += tree.compressed_bytes;
network.uncompressed_bytes += tree.uncompressed_bytes;
network.disk_bytes += tree.disk_bytes;
if (tree.event_count == 0) continue;
if (network.min_seq == 0 or tree.min_seq < network.min_seq) network.min_seq = tree.min_seq;
network.max_seq = @max(network.max_seq, tree.max_seq);
if (network.min_witnessed_at == 0 or tree.min_witnessed_at < network.min_witnessed_at)
network.min_witnessed_at = tree.min_witnessed_at;
network.max_witnessed_at = @max(network.max_witnessed_at, tree.max_witnessed_at);
}
return network;
}
pub fn renderInspectAll(
writer: *Io.Writer,
data_dir: []const u8,
aggregate: *const Aggregate,
generated_us: i64,
truncate: usize,
) !void {
var time_buffer: [40]u8 = undefined;
try writer.writeAll("inspect-all\n");
try writer.print("data-dir: {s}\n", .{data_dir});
try writer.print("generated: {s}\n", .{formatMicros(&time_buffer, generated_us)});
try renderNetwork(writer, aggregate.network);
try renderTrees(writer, aggregate.trees);
try renderCollections(writer, aggregate.collections, truncate);
if (aggregate.warnings.len > 0) {
try writer.print("\nwarnings ({d}):\n", .{aggregate.warnings.len});
for (aggregate.warnings) |warning| try writer.print(" {s}\n", .{warning});
}
}
fn renderNetwork(writer: *Io.Writer, network: NetworkTotals) !void {
try writer.writeAll("\nnetwork totals:\n");
try writer.print(" segments: {d} ({d} sealed, {d} active)\n", .{ network.segments, network.sealed_segments, network.active_segments });
try writer.print(" blocks: {f}\n", .{commaInt(network.blocks)});
try writer.print(" events: {f}\n", .{commaInt(network.events)});
try writer.print(" collections: {d} distinct NSIDs\n", .{network.collections});
if (network.events > 0) {
try writer.print(" seq range: [{d}, {d}]\n", .{ network.min_seq, network.max_seq });
var min_buffer: [40]u8 = undefined;
var max_buffer: [40]u8 = undefined;
try writer.print(" witnessed_at range: {s} → {s}\n", .{
formatMicros(&min_buffer, network.min_witnessed_at),
formatMicros(&max_buffer, network.max_witnessed_at),
});
}
try writer.print(" payload (uncompressed): {f}\n", .{humanBytes(network.uncompressed_bytes)});
try writer.print(" payload (compressed): {f}\n", .{humanBytes(network.compressed_bytes)});
try writer.print(" disk usage: {f}\n", .{humanBytes(network.disk_bytes)});
if (network.compressed_bytes > 0) {
const ratio: f64 = @as(f64, @floatFromInt(network.uncompressed_bytes)) /
@as(f64, @floatFromInt(network.compressed_bytes));
try writer.print(" compression ratio: {d:.2}x\n", .{ratio});
}
}
fn renderTrees(writer: *Io.Writer, trees: []const TreeAggregate) !void {
try writer.writeAll("\ntrees:\n");
if (trees.len == 0) return writer.writeAll(" (none)\n");
for (trees, 0..) |tree, index| {
try writer.print(" [{d}] {s}\n", .{ index, tree.dir });
if (tree.sealed_count + tree.active_count == 0) {
try writer.writeAll(" (empty)\n");
continue;
}
try writer.print(" files: {d} sealed + {d} active\n", .{ tree.sealed_count, tree.active_count });
try writer.print(" events: {f}\n", .{commaInt(tree.event_count)});
try writer.print(" blocks: {f}\n", .{commaInt(tree.block_count)});
if (tree.event_count > 0) {
try writer.print(" seq range: [{d}, {d}]\n", .{ tree.min_seq, tree.max_seq });
var min_buffer: [40]u8 = undefined;
var max_buffer: [40]u8 = undefined;
try writer.print(" witnessed_at: {s} → {s}\n", .{
formatMicros(&min_buffer, tree.min_witnessed_at),
formatMicros(&max_buffer, tree.max_witnessed_at),
});
}
if (tree.oldest_mtime_us != 0) {
var oldest_buffer: [40]u8 = undefined;
var newest_buffer: [40]u8 = undefined;
try writer.print(" oldest mtime: {s}\n", .{formatMicros(&oldest_buffer, tree.oldest_mtime_us)});
try writer.print(" newest mtime: {s}\n", .{formatMicros(&newest_buffer, tree.newest_mtime_us)});
}
try writer.print(" compressed: {f}\n", .{humanBytes(tree.compressed_bytes)});
try writer.print(" uncompressed: {f}\n", .{humanBytes(tree.uncompressed_bytes)});
try writer.print(" disk: {f}\n", .{humanBytes(tree.disk_bytes)});
if (tree.latest_segment) |latest| {
try writer.print(" latest: idx={d} {s} events={f} blocks={d} size={f}\n", .{
latest.index,
if (latest.sealed) "sealed" else "active",
commaInt(latest.event_count),
latest.block_count,
humanBytes(latest.size_bytes),
});
}
}
}
fn renderCollections(writer: *Io.Writer, collections: []const CollectionAggregate, truncate: usize) !void {
try writer.print("\ncollections ({d} distinct NSIDs):\n", .{collections.len});
if (collections.len == 0) return writer.writeAll(" (none)\n");
var nsid_width: usize = "NSID".len;
for (collections) |collection| nsid_width = @max(nsid_width, collection.nsid.len);
const half = truncate / 2;
for (collections, 0..) |collection, index| {
if (truncate != 0 and collections.len > truncate and index == half)
try writer.print(" ... ({d} rows omitted) ...\n", .{collections.len - 2 * half});
if (truncate != 0 and collections.len > truncate and index >= half and index < collections.len - half) continue;
var event_buffer: [32]u8 = undefined;
var block_buffer: [32]u8 = undefined;
try writer.print(" [{d:>3}] {s:<[5]} events: {s:>12} segments: {d:>5} blocks: {s:>12}\n", .{
index,
collection.nsid,
formatComma(&event_buffer, collection.event_count),
collection.segment_count,
formatComma(&block_buffer, collection.block_count),
nsid_width,
});
}
}
const CommaInt = struct {
value: u64,
pub fn format(self: CommaInt, writer: *Io.Writer) Io.Writer.Error!void {
var buffer: [32]u8 = undefined;
try writer.writeAll(formatComma(&buffer, self.value));
}
};
fn commaInt(value: u64) CommaInt {
return .{ .value = value };
}
const HumanBytes = struct {
value: u64,
pub fn format(self: HumanBytes, writer: *Io.Writer) Io.Writer.Error!void {
if (self.value < 1024) return writer.print("{d} B", .{self.value});
const suffixes = [_][]const u8{ "KiB", "MiB", "GiB", "TiB", "PiB" };
var divisor: u64 = 1024;
var exponent: usize = 0;
var reduced = self.value / 1024;
while (reduced >= 1024 and exponent + 1 < suffixes.len) : (reduced /= 1024) {
divisor *= 1024;
exponent += 1;
}
const scaled: f64 = @as(f64, @floatFromInt(self.value)) / @as(f64, @floatFromInt(divisor));
try writer.print("{d:.2} {s}", .{ scaled, suffixes[exponent] });
}
};
fn humanBytes(value: u64) HumanBytes {
return .{ .value = value };
}
fn formatComma(buffer: []u8, value: u64) []const u8 {
var raw_buffer: [32]u8 = undefined;
const raw = std.fmt.bufPrint(&raw_buffer, "{d}", .{value}) catch unreachable;
const commas = if (raw.len == 0) 0 else (raw.len - 1) / 3;
const out = buffer[0 .. raw.len + commas];
var source = raw.len;
var target = out.len;
var digits: usize = 0;
while (source > 0) {
if (digits == 3) {
target -= 1;
out[target] = ',';
digits = 0;
}
source -= 1;
target -= 1;
out[target] = raw[source];
digits += 1;
}
return out;
}
fn formatMicros(buffer: []u8, micros: i64) []const u8 {
if (micros == 0) return "0";
const seconds: u64 = @intCast(@divFloor(micros, std.time.us_per_s));
const fraction: u64 = @intCast(@mod(micros, std.time.us_per_s));
const epoch_seconds: std.time.epoch.EpochSeconds = .{ .secs = seconds };
const year_day = epoch_seconds.getEpochDay().calculateYearDay();
const month_day = year_day.calculateMonthDay();
const day_seconds = epoch_seconds.getDaySeconds();
return std.fmt.bufPrint(buffer, "{d:0>4}-{d:0>2}-{d:0>2}T{d:0>2}:{d:0>2}:{d:0>2}.{d:0>6}Z", .{
year_day.year,
month_day.month.numeric(),
month_day.day_index + 1,
day_seconds.getHoursIntoDay(),
day_seconds.getMinutesIntoHour(),
day_seconds.getSecondsIntoMinute(),
fraction,
}) catch unreachable;
}
fn isSentinel(nsid: []const u8) bool {
return std.mem.eql(u8, nsid, "$account") or
std.mem.eql(u8, nsid, "$identity") or
std.mem.eql(u8, nsid, "$sync");
}
fn parseSegmentIndex(name: []const u8) ?u64 {
if (!std.mem.startsWith(u8, name, "seg_") or !std.mem.endsWith(u8, name, ".jss")) return null;
const digits = name[4 .. name.len - 4];
if (digits.len != 10) return null;
return std.fmt.parseInt(u64, digits, 36) catch null;
}
test "inspect-all scans steady and real bootstrap trees" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var steady = try tmp.dir.createDirPathOpen(std.testing.io, "data/segments", .{});
steady.close(std.testing.io);
var bootstrap = try tmp.dir.createDirPathOpen(std.testing.io, "data/backfill/live/segments", .{});
bootstrap.close(std.testing.io);
const fixture = try Io.Dir.cwd().readFileAlloc(std.testing.io, "tests/fixtures/seg_0000000000.jss", std.testing.allocator, .limited(1 << 20));
defer std.testing.allocator.free(fixture);
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "data/segments/seg_0000000000.jss", .data = fixture });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "data/backfill/live/segments/seg_0000000000.jss", .data = fixture });
const data_dir = try tmp.dir.realPathFileAlloc(std.testing.io, "data", std.testing.allocator);
defer std.testing.allocator.free(data_dir);
var aggregate = try inspectAll(std.testing.allocator, std.testing.io, data_dir, .{});
defer aggregate.deinit();
try std.testing.expectEqual(@as(usize, 2), aggregate.network.segments);
try std.testing.expectEqual(@as(u64, 14_036), aggregate.network.events);
try std.testing.expectEqual(@as(usize, 5), aggregate.network.collections);
try std.testing.expectEqual(@as(u64, 8_210), aggregate.collections[0].event_count);
try std.testing.expectEqualStrings("app.bsky.feed.post", aggregate.collections[0].nsid);
}
test "inspect-all skip-unsealed keeps file bookkeeping but excludes active payload" {
var active = try segment_writer.ActiveWriter.init(std.testing.allocator);
defer active.deinit();
try active.append(.{
.seq = 1,
.witnessed_at = 1_700_000_000_000_001,
.indexed_at = 0,
.kind = .create,
.did = "did:plc:active",
.collection = "app.bsky.feed.post",
.rkey = "r",
.rev = "v",
.payload = "p",
});
try active.flush();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var steady = try tmp.dir.createDirPathOpen(std.testing.io, "data/segments", .{});
steady.close(std.testing.io);
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "data/segments/seg_0000000000.jss", .data = active.bytes() });
const data_dir = try tmp.dir.realPathFileAlloc(std.testing.io, "data", std.testing.allocator);
defer std.testing.allocator.free(data_dir);
var complete = try inspectAll(std.testing.allocator, std.testing.io, data_dir, .{});
defer complete.deinit();
try std.testing.expectEqual(@as(u64, 1), complete.network.events);
try std.testing.expectEqual(@as(u64, 1), complete.network.blocks);
var skipped = try inspectAll(std.testing.allocator, std.testing.io, data_dir, .{ .skip_unsealed = true });
defer skipped.deinit();
try std.testing.expectEqual(@as(usize, 1), skipped.network.active_segments);
try std.testing.expect(skipped.network.disk_bytes > 0);
try std.testing.expectEqual(@as(u64, 0), skipped.network.events);
try std.testing.expectEqual(@as(u64, 0), skipped.network.blocks);
try std.testing.expectEqual(@as(u64, 1), skipped.trees[0].latest_segment.?.event_count);
}
test "inspect-all warns on corrupt non-tail and silently tolerates corrupt tail" {
const fixture = try Io.Dir.cwd().readFileAlloc(std.testing.io, "tests/fixtures/seg_0000000000.jss", std.testing.allocator, .limited(1 << 20));
defer std.testing.allocator.free(fixture);
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var steady = try tmp.dir.createDirPathOpen(std.testing.io, "data/segments", .{});
steady.close(std.testing.io);
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "data/segments/seg_0000000000.jss", .data = fixture });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "data/segments/seg_0000000001.jss", .data = "bad" });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "data/segments/seg_0000000002.jss", .data = fixture });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "data/segments/seg_0000000003.jss", .data = "bad tail" });
const data_dir = try tmp.dir.realPathFileAlloc(std.testing.io, "data", std.testing.allocator);
defer std.testing.allocator.free(data_dir);
var aggregate = try inspectAll(std.testing.allocator, std.testing.io, data_dir, .{});
defer aggregate.deinit();
try std.testing.expectEqual(@as(usize, 2), aggregate.network.sealed_segments);
try std.testing.expectEqual(@as(usize, 1), aggregate.warnings.len);
try std.testing.expect(std.mem.indexOf(u8, aggregate.warnings[0], "seg_0000000001.jss") != null);
try std.testing.expect(std.mem.indexOf(u8, aggregate.warnings[0], "seg_0000000003.jss") == null);
}
test "inspect-all collection truncation keeps both ends" {
const collections = [_]CollectionAggregate{
.{ .nsid = "a", .event_count = 5 },
.{ .nsid = "b", .event_count = 4 },
.{ .nsid = "c", .event_count = 3 },
.{ .nsid = "d", .event_count = 2 },
.{ .nsid = "e", .event_count = 1 },
};
var out: Io.Writer.Allocating = .init(std.testing.allocator);
defer out.deinit();
try renderCollections(&out.writer, &collections, 2);
try std.testing.expect(std.mem.indexOf(u8, out.written(), "[ 0] a") != null);
try std.testing.expect(std.mem.indexOf(u8, out.written(), "... (3 rows omitted) ...") != null);
try std.testing.expect(std.mem.indexOf(u8, out.written(), "[ 4] e") != null);
try std.testing.expect(std.mem.indexOf(u8, out.written(), "[ 2] c") == null);
}
test "inspect-all renderer matches upstream golden text" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const trees = [_]TreeAggregate{
.{
.dir = "/data/segments",
.sealed_count = 3,
.active_count = 1,
.compressed_bytes = 2 * 1024 * 1024,
.uncompressed_bytes = 6 * 1024 * 1024,
.disk_bytes = 3 * 1024 * 1024,
.event_count = 1234,
.block_count = 12,
.min_seq = 10,
.max_seq = 1243,
.min_witnessed_at = 1_779_840_000_000_000,
.max_witnessed_at = 1_779_926_400_000_000,
.oldest_mtime_us = 1_779_840_060_000_000,
.newest_mtime_us = 1_779_926_460_000_000,
.latest_segment = .{ .index = 4, .sealed = false, .event_count = 300, .block_count = 3, .size_bytes = 768 * 1024 },
},
.{ .dir = "/data/backfill/live_segments" },
};
const collections = [_]CollectionAggregate{
.{ .nsid = "app.bsky.feed.post", .event_count = 900, .segment_count = 4, .block_count = 9 },
.{ .nsid = "app.bsky.feed.like", .event_count = 300, .segment_count = 2, .block_count = 2 },
.{ .nsid = "app.bsky.graph.follow", .event_count = 34, .segment_count = 1, .block_count = 1 },
};
const warnings = [_][]const u8{"/data/segments/seg_0000000002.jss: corrupt segment: bad magic \"XXXX\""};
var aggregate: Aggregate = .{
.arena = arena,
.trees = &trees,
.collections = &collections,
.network = .{
.segments = 4,
.sealed_segments = 3,
.active_segments = 1,
.blocks = 12,
.events = 1234,
.collections = 3,
.compressed_bytes = 2 * 1024 * 1024,
.uncompressed_bytes = 6 * 1024 * 1024,
.disk_bytes = 3 * 1024 * 1024,
.min_seq = 10,
.max_seq = 1243,
.min_witnessed_at = 1_779_840_000_000_000,
.max_witnessed_at = 1_779_926_400_000_000,
},
.warnings = &warnings,
};
// The local defer owns this arena; prevent Aggregate.deinit here.
_ = &aggregate;
var out: Io.Writer.Allocating = .init(std.testing.allocator);
defer out.deinit();
try renderInspectAll(&out.writer, "/data", &aggregate, 1_779_971_696_000_000, 100);
const expected =
\\inspect-all
\\data-dir: /data
\\generated: 2026-05-28T12:34:56.000000Z
\\
\\network totals:
\\ segments: 4 (3 sealed, 1 active)
\\ blocks: 12
\\ events: 1,234
\\ collections: 3 distinct NSIDs
\\ seq range: [10, 1243]
\\ witnessed_at range: 2026-05-27T00:00:00.000000Z → 2026-05-28T00:00:00.000000Z
\\ payload (uncompressed): 6.00 MiB
\\ payload (compressed): 2.00 MiB
\\ disk usage: 3.00 MiB
\\ compression ratio: 3.00x
\\
\\trees:
\\ [0] /data/segments
\\ files: 3 sealed + 1 active
\\ events: 1,234
\\ blocks: 12
\\ seq range: [10, 1243]
\\ witnessed_at: 2026-05-27T00:00:00.000000Z → 2026-05-28T00:00:00.000000Z
\\ oldest mtime: 2026-05-27T00:01:00.000000Z
\\ newest mtime: 2026-05-28T00:01:00.000000Z
\\ compressed: 2.00 MiB
\\ uncompressed: 6.00 MiB
\\ disk: 3.00 MiB
\\ latest: idx=4 active events=300 blocks=3 size=768.00 KiB
\\ [1] /data/backfill/live_segments
\\ (empty)
\\
\\collections (3 distinct NSIDs):
\\ [ 0] app.bsky.feed.post events: 900 segments: 4 blocks: 9
\\ [ 1] app.bsky.feed.like events: 300 segments: 2 blocks: 2
\\ [ 2] app.bsky.graph.follow events: 34 segments: 1 blocks: 1
\\
\\warnings (1):
\\ /data/segments/seg_0000000002.jss: corrupt segment: bad magic "XXXX"
\\
;
try std.testing.expectEqualStrings(expected, out.written());
}