code wiki / _hdl_build / nx_livemap_fast_gate.nx
nx_livemap_fast_gate.nx source
↩ module page · 134 lines · 7287 B
1// nx_livemap_fast_gate.nx -- prove the LINEAR live-doc map is byte-identical to the quadratic one.
2//
3// ss_open2 already built the authoritative maps at h[1 + 8*ns + s] using the existing O(keys x
4// segments) shadow scan. This gate rebuilds the same maps using the newest-first hash table from
5// nx_livemap_fast and compares EVERY BYTE. Any difference is a correctness failure, not a perf note:
6// these maps decide which documents are visible, so a faster map that is subtly different does not
7// make search quick, it makes search WRONG.
8//
9// Run against the REAL shard, not a fixture. A synthetic 2-segment store would not exercise the
10// shadow logic that only appears with many overlapping segments, and this session already learned
11// that a gate is only as honest as its ugliest input.
12// license_tier: ORIGINAL No hw writes (Rule 26).
13import "nx_livemap_fast.nx"
14import "nx_gate_verdict.nx"
15
16func lg_eq(a: i64, b: i64) -> i64 { if a == b { return 1 } return 0 }
17
18func main(argc: i64, argv: *i64) -> i64 {
19 let ctr: *i64 = gv_ctr()
20 gv_head("nx_livemap_fast -- linear live-doc map must be BYTE-IDENTICAL to the quadratic one on the real shard" as *u8)
21
22 // ⚠DEFAULT PATH MUST TOLERATE THE CWD ANCHOR -- FIXED 2026-08-15, AND IT HAD MADE THIS GATE VACUOUS.
23 // The bare path resolves against the CALLER's cwd. nx_sov_build_run (and therefore every rostered or
24 // ad-hoc run of this gate) chdir's into buildroot/, where knowledge/store/ does not exist -- the store
25 // is at the nishihost root. MEASURED: with the bare default the gate reported segments=0, total key
26 // entries=0, bytes compared=0, and T7 "BYTE-IDENTICAL to the quadratic implementation" PASSED ON THE
27 // EMPTY SET. Its anti-vacuity teeth (T2/T3/T6) correctly forced the verdict RED, so it was never
28 // silently green -- it was loudly red and nobody ran it, which is the same outcome for a gate that
29 // guards WHICH DOCUMENTS ARE VISIBLE AT ALL.
30 // ★★★★★★A GATE THAT CANNOT FIND ITS SUBJECT STILL RUNS, STILL PRINTS, AND STILL PASSES THE ONE
31 // TOOTH THAT MATTERS -- the anti-vacuity teeth are the only thing standing between that and a
32 // fabricated GREEN. This is the THIRD instance of the bare-path-under-anchor defect found today
33 // (tree_canon had it, organ_kind had it, now this).
34 // PROVEN with the right path on the real shard: segments=14, total key entries=5282736, distinct keys
35 // 4841995, bytes compared=369993949, MISMATCHED=0, 7/7 GREEN. Same run, bare path: 4/7 RED, vacuous.
36 // Probe the manifest to decide, so the gate is correct from EITHER cwd rather than trading one broken
37 // vantage for the other. argv[1] still overrides, for a caller pointing at some other shard.
38 var prefix: *u8 = "knowledge/store/dp-web-pub-" as *u8
39 let anch: i64 = sys_openat_rd("../knowledge/store/dp-web-pub-manifest.txt" as *u8)
40 if anch >= 0 { sys_close(anch); prefix = "../knowledge/store/dp-web-pub-" as *u8 }
41 if argc >= 2 { prefix = argv[1] as *u8 }
42 gv_puts(" shard prefix=" as *u8); gv_puts(prefix); gv_puts("\n" as *u8)
43
44 let h: *i64 = ss_open2(prefix, 1)
45 var t1: i64 = 0
46 if (h as i64) != 0 { t1 = 1 }
47 gv_check("T1 the shard opens (nothing below means anything if it does not)" as *u8, t1, ctr)
48 if t1 == 0 {
49 let rcx: i64 = gv_verdict("LIVEMAPFAST" as *u8, ctr, "linear live-doc map equivalence" as *u8)
50 sys_exit(rcx)
51 return rcx
52 }
53
54 let ns: i64 = h[0]
55 gv_puts(" segments=" as *u8); gv_num(ns); gv_puts("\n" as *u8)
56 gv_check("T2 the shard has MULTIPLE segments, so the shadow path is actually exercised" as *u8, lg_eq(ns > 1, 1), ctr)
57
58 let total: i64 = lmf_total_keys(h, ns)
59 gv_puts(" total key entries=" as *u8); gv_num(total); gv_puts("\n" as *u8)
60 let cap: i64 = lmf_pow2(total * LMF_LOAD_NUM + 16)
61 let tbl_seg: *i64 = sys_mmap(8 * cap) as *i64
62 let tbl_eo: *i64 = sys_mmap(8 * cap) as *i64
63 let ins: i64 = lmf_build(h, ns, tbl_seg, tbl_eo, cap)
64 gv_puts(" distinct keys inserted=" as *u8); gv_num(ins); gv_puts("\n" as *u8)
65 gv_check("T3 the table inserted at least one key and no more than the total entry count" as *u8, lg_eq(ins > 0, 1), ctr)
66 gv_check("T4 distinct keys cannot exceed total entries (a table that over-counts is broken)" as *u8, lg_eq(ins <= total, 1), ctr)
67
68 // Rebuild each segment's live map with the LINEAR shadow test and diff against ss_open2's.
69 var mismatched_bytes: i64 = 0
70 var compared_bytes: i64 = 0
71 var segs_done: i64 = 0
72 var s: i64 = 0
73 while s < ns {
74 let kb: *u8 = h[1 + 8 * s] as *u8
75 let ksz: i64 = h[2 + 8 * s]
76 let pb: *u8 = h[7 + 8 * s] as *u8
77 let dsz: i64 = h[4 + 8 * s]
78 let want: *u8 = h[1 + 8 * ns + s] as *u8
79 let ndoc: i64 = dsz / 9 + 16
80 let got: *u8 = sys_mmap(ndoc)
81 var z: i64 = 0
82 while z < ndoc { got[z] = 0 as u8; z = z + 1 }
83 if ksz >= 8 { if h[8 + 8 * s] >= 8 {
84 let nd9: i64 = ss_r32(pb, 4)
85 let m9: i64 = ss_r32(kb, 4)
86 var e9: i64 = 0
87 while e9 < m9 {
88 let eo: i64 = 8 + 4 * m9 + ss_r32(kb, 8 + 4 * e9)
89 let kind9: i64 = kb[eo]
90 let kl9: i64 = ss_r32(kb, eo + 1)
91 let vof: i64 = ss_r32(kb, eo + 5 + kl9)
92 if kind9 == 1 { if kl9 < 500 {
93 let newest: i64 = lmf_lookup(h, tbl_seg, tbl_eo, cap, kb, eo)
94 var shadowed: i64 = 0
95 if newest > s { shadowed = 1 }
96 if shadowed == 0 {
97 let ioff: i64 = vof - 9 - kl9
98 var lo9: i64 = 0
99 var hi9: i64 = nd9 - 1
100 while lo9 <= hi9 {
101 let mid9: i64 = (lo9 + hi9) / 2
102 let dv: i64 = ss_r32(pb, 8 + 4 * mid9)
103 if dv == ioff { got[mid9] = 1 as u8; lo9 = hi9 + 1 } else {
104 if dv < ioff { lo9 = mid9 + 1 } else { hi9 = mid9 - 1 }
105 }
106 }
107 }
108 } }
109 e9 = e9 + 1
110 }
111 } }
112 var b: i64 = 0
113 while b < ndoc {
114 compared_bytes = compared_bytes + 1
115 if got[b] != want[b] { mismatched_bytes = mismatched_bytes + 1 }
116 b = b + 1
117 }
118 sys_munmap(got, ndoc)
119 segs_done = segs_done + 1
120 s = s + 1
121 }
122
123 gv_puts(" segments compared=" as *u8); gv_num(segs_done)
124 gv_puts(" bytes compared=" as *u8); gv_num(compared_bytes)
125 gv_puts(" MISMATCHED=" as *u8); gv_num(mismatched_bytes); gv_puts("\n" as *u8)
126
127 gv_check("T5 every segment was compared, none skipped" as *u8, lg_eq(segs_done, ns), ctr)
128 gv_check("T6 a non-trivial number of bytes was actually compared (a zero-byte diff proves nothing)" as *u8, lg_eq(compared_bytes > 0, 1), ctr)
129 gv_check("T7 BYTE-IDENTICAL to the quadratic implementation across the whole shard" as *u8, lg_eq(mismatched_bytes, 0), ctr)
130
131 let rc: i64 = gv_verdict("LIVEMAPFAST" as *u8, ctr, "linear live-doc map is byte-identical to the quadratic one on the real shard" as *u8)
132 sys_exit(rc)
133 return rc
134}