code wiki / _hdl_build / nx_capreg_librarian_test.nx
nx_capreg_librarian_test.nx source
↩ module page · 72 lines · 3585 B
1// nx_capreg_librarian_test.nx -- ENGINEER's gate for the Librarian registry repair. Real KATs on a
2// synthetic journal reproducing BOTH observed failure modes from the crash: byte-identical snapshot
3// re-appends (the 416->51 bloat) and true cross-session idx collisions (the 212-227 bug).
4// PASS = regen dedups + renumbers exactly, allocator hands out max+1, dual-write hits both files,
5// and a second regen after new registrations is idempotent. license_tier: ORIGINAL
6import "nx_capreg_librarian.nx"
7import "nx_syscalls.nx"
8
9// does buf contain nul-terminated needle? (flat scan, no deep nesting)
10func ts_find(b: *u8, n: i64, s: *u8) -> i64 {
11 var i: i64 = 0
12 while i < n {
13 var k: i64 = 0
14 var ok: i64 = 1
15 while s[k] != (0 as u8) {
16 if i + k >= n { ok = 0 }
17 if ok == 1 { if b[i+k] != s[k] { ok = 0 } }
18 k = k + 1
19 }
20 if ok == 1 { return 1 }
21 i = i + 1
22 }
23 return 0
24}
25
26func main() -> i64 {
27 let jp: *u8 = "/tmp/_cl_journal_kat.log" as *u8
28 let lp: *u8 = "/tmp/_cl_live_kat.log" as *u8
29 // build the synthetic journal: ALPHA, BETA, ALPHA-again (snapshot dup), GAMMA colliding with
30 // BETA's idx 11 (parallel-session bug), GAMMA-again (snapshot dup of the collision)
31 let jf: i64 = sys_openat_wr(jp, 0x1a4)
32 if jf < 0 { sys_exit(9) }
33 cl_w(jf, "CAPREG idx=10 layer=2 status=2 name=ALPHA\n" as *u8)
34 cl_w(jf, "CAPREG idx=11 layer=3 status=2 name=BETA\n" as *u8)
35 cl_w(jf, "CAPREG idx=10 layer=2 status=2 name=ALPHA\n" as *u8)
36 cl_w(jf, "CAPREG idx=11 layer=5 status=2 name=GAMMA\n" as *u8)
37 cl_w(jf, "CAPREG idx=11 layer=5 status=2 name=GAMMA\n" as *u8)
38 sys_close(jf)
39
40 var pass: i64 = 0
41 // KAT 1: regen -> exactly 3 unique entries (2 snapshot dups dropped)
42 if cl_regen(jp, lp) == 3 { pass = pass + 1 }
43 // KAT 2: GAMMA renumbered past the global max -> live max idx = 12
44 if cl_max_idx(lp) == 12 { pass = pass + 1 }
45 // KAT 3: the renumber is traceable -- live carries remapped_from=11
46 let lenp: *i64 = sys_mmap(16) as *i64
47 let lb: *u8 = sys_read_file(lp, lenp)
48 if ts_find(lb, lenp[0], "idx=12 layer=5 status=2 name=GAMMA remapped_from=11" as *u8) == 1 { pass = pass + 1 }
49 // KAT 4: BETA kept its idx untouched (first-seen wins)
50 if ts_find(lb, lenp[0], "idx=11 layer=3 status=2 name=BETA" as *u8) == 1 { pass = pass + 1 }
51 // KAT 5: the allocator hands out max(live, journal)+1 = 13
52 if cl_next_idx(lp, jp) == 13 { pass = pass + 1 }
53 // KAT 6: CANONICALIZE -- clean set becomes the journal (collisions repaired FOR GOOD), raw archived
54 if cl_canonicalize(jp, lp) == 3 { pass = pass + 1 }
55 if cl_max_idx(jp) == 12 { pass = pass + 1 }
56 // KAT 7: dual-write at the allocated idx lands in BOTH files
57 if cl_register_dual(lp, jp, cl_next_idx(lp, jp), 4, 2, "DELTA" as *u8) == 1 { pass = pass + 1 }
58 if cl_max_idx(lp) == 13 { pass = pass + 1 }
59 if cl_max_idx(jp) == 13 { pass = pass + 1 }
60 // KAT 8: regen over the canonical journal is STABLE -- 4 uniques, GAMMA keeps idx 12 (no drift)
61 if cl_regen(jp, lp) == 4 { pass = pass + 1 }
62 let l2p: *i64 = sys_mmap(16) as *i64
63 let l2: *u8 = sys_read_file(lp, l2p)
64 if ts_find(l2, l2p[0], "idx=12 layer=5 status=2 name=GAMMA remapped_from=11" as *u8) == 1 { pass = pass + 1 }
65 // KAT 9: allocator after the stable regen = 14
66 if cl_next_idx(lp, jp) == 14 { pass = pass + 1 }
67
68 cl_w(1, "LIBRARIAN KATs passed=" as *u8); cl_wn(1, pass); cl_w(1, "/13\n" as *u8)
69 if pass == 13 { sys_exit(0) }
70 sys_exit(1)
71 return 1
72}