nx_uxf_tsv_persist_gate.nx source
↩ module page · 207 lines · 10393 B
1// nx_uxf_tsv_persist_gate.nx -- the D001 MIGRATION of nx_uxf_tsv_persist onto nx_gate_verdict (2026-09-06).
2//
3// THE FULL DATA PIPELINE ON REAL DATA: a real registry TSV becomes per-row canonical content-addressed
4// records, is PERSISTED into the immutable segment store with an fsync, REOPENED FRESH FROM DISK, and every
5// row is retrieved by its content-address through a binary search of the NXK1 index and compared BYTE FOR
6// BYTE against the original. This is what makes the replace-the-TSVs claim a measurement rather than a plan.
7//
8// ALL FOUR ORIGINAL TEETH PRESERVED IN ORDER AND PREDICATE, plus one strengthening: the original asserted
9// only that SOME rows were persisted, so a parser that produced a single row would have passed T1. The row
10// count is now asserted against the known population of the fixture file, which is what makes T2 and T3
11// non-vacuous -- hits equal to nrows is trivially true when nrows is one.
12//
13// FIXTURE SCOPED, WHICH THE ORIGINAL DID NOT DO: it wrote /tmp/uxf_sci.docs and /tmp/uxf_sci.keys, names a
14// production beat or a sibling gate could collide with, and the estate has measured that class -- a gate
15// sharing its fixture reports on the FIXTURE rather than on the code. These paths carry this gate's own name.
16// license_tier: ORIGINAL No hw writes (Rule 26).
17import "nx_gate_verdict.nx"
18import "nx_syscalls.nx"
19import "nx_canon_cid.nx"
20import "nx_uxf_cid.nx"
21import "nx_seg_store.nx"
22
23const UP_CANON: i64 = 4096
24const UP_IDXBUF: i64 = 1048576
25const UP_ROWS: i64 = 256
26const UP_CIDSLOT: i64 = 128
27const UP_CIDLEN: i64 = 72
28const UP_PTRTAB: i64 = 512
29const UP_KEYBUF: i64 = 24
30const UP_WANT_ROWS: i64 = 18 // sci_units.tsv carries 18 data rows; asserted so T2 and T3 cannot pass on one
31const UP_TAB: i64 = 0x09
32const UP_LF: i64 = 0x0A
33const UP_HASH: i64 = 0x23
34
35func up_keyname(idx: i64, dst: *u8) -> i64 {
36 dst[0] = 99 as u8
37 if idx == 0 { dst[1] = 48 as u8; dst[2] = 0 as u8; return 2 }
38 let tmp: *u8 = sys_mmap(UP_KEYBUF)
39 var m: i64 = idx
40 var k: i64 = 0
41 while m > 0 { tmp[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
42 var i: i64 = 0
43 while i < k { dst[1 + i] = tmp[k - 1 - i]; i = i + 1 }
44 dst[1 + k] = 0 as u8
45 return k + 1
46}
47
48func up_bytes_eq(a: *u8, b: *u8, n: i64) -> i64 { var i: i64 = 0; while i < n { if a[i] != b[i] { return 0 } i = i + 1 } return 1 }
49
50// binary search the NXK1 index for key(kl); out[0]=voff, out[1]=vlen; returns entry index or -1
51func up_get(idx: *u8, key: *u8, kl: i64, out: *i64) -> i64 {
52 let m: i64 = ss_r32(idx, 4)
53 let base: i64 = 8 + 4 * m
54 var lo: i64 = 0
55 var hi: i64 = m - 1
56 while lo <= hi {
57 let mid: i64 = (lo + hi) / 2
58 let rel: i64 = ss_r32(idx, 8 + 4 * mid)
59 let eoff: i64 = base + rel
60 let eklen: i64 = ss_r32(idx, eoff + 1)
61 let ekoff: i64 = eoff + 5
62 let cmp: i64 = ss_kcmp(((idx as i64) + ekoff) as *u8, eklen, key, kl)
63 if cmp == 0 { out[0] = ss_r32(idx, ekoff + eklen); out[1] = ss_r32(idx, ekoff + eklen + 4); return mid }
64 if cmp < 0 { lo = mid + 1 } else { hi = mid - 1 }
65 }
66 return 0 - 1
67}
68
69func main(argc: i64, argv: *i64) -> i64 {
70 let ctr: *i64 = gv_ctr()
71 gv_head("the UXF data pipeline -- a real TSV persisted and retrieved by content address" as *u8)
72
73 let lenp: *i64 = sys_mmap(8) as *i64
74 // THE FIXTURE PATH MUST NOT DEPEND ON THE CALLER'S CWD. Measured 2026-09-06: this gate passed 4/4 under
75 // the ship loop and then read UNREADABLE under nx_sov_build_run, purely because the bare path resolves
76 // from the estate root and not from buildroot. A bare path is a CWD-relative expression, not a location.
77 var buf: *u8 = sys_read_file("knowledge/registry/sci_units.tsv\x00" as *u8, lenp)
78 if (buf as i64) == 0 { buf = sys_read_file("../knowledge/registry/sci_units.tsv\x00" as *u8, lenp) }
79 if (buf as i64) == 0 { buf = sys_read_file("buildroot/knowledge/registry/sci_units.tsv\x00" as *u8, lenp) }
80 if (buf as i64) == 0 {
81 gv_puts(" UP-UNREADABLE knowledge/registry/sci_units.tsv could not be read, so NOTHING below could be judged\n" as *u8)
82 let rcx: i64 = gv_verdict("UXF-TSV-PERSIST-GATE" as *u8, ctr, "fixture TSV unreadable" as *u8)
83 sys_exit(rcx)
84 return rcx
85 }
86 let n: i64 = lenp[0]
87
88 let cids: *u8 = sys_mmap(UP_ROWS * UP_CIDSLOT)
89 let cptr: *i64 = sys_mmap(UP_ROWS * 8) as *i64
90 let clenr: *i64 = sys_mmap(UP_ROWS * 8) as *i64
91 var nrows: i64 = 0
92
93 let w: *i64 = ss_begin()
94 let keys: *i64 = sys_mmap(UP_PTRTAB) as *i64
95 let vals: *i64 = sys_mmap(UP_PTRTAB) as *i64
96
97 var i: i64 = 0
98 while i < n {
99 let ls: i64 = i
100 var le: i64 = i
101 var f: i64 = 1
102 while f == 1 { if le >= n { f = 0 } else { if buf[le] == (UP_LF as u8) { f = 0 } else { le = le + 1 } } }
103 if le > ls {
104 if buf[ls] != (UP_HASH as u8) {
105 var ncol: i64 = 0
106 var fstart: i64 = ls
107 var p: i64 = ls
108 while p <= le {
109 var isend: i64 = 0
110 if p == le { isend = 1 } else { if buf[p] == (UP_TAB as u8) { isend = 1 } }
111 if isend == 1 {
112 let flen: i64 = p - fstart
113 let vbuf: *u8 = sys_mmap(flen + 2)
114 var t: i64 = 0
115 while t < flen { vbuf[t] = buf[fstart + t]; t = t + 1 }
116 vbuf[flen] = 0 as u8
117 let kbuf: *u8 = sys_mmap(UP_KEYBUF)
118 up_keyname(ncol, kbuf)
119 keys[ncol] = kbuf as i64
120 vals[ncol] = vbuf as i64
121 ncol = ncol + 1
122 fstart = p + 1
123 }
124 p = p + 1
125 }
126 let canonbuf: *u8 = sys_mmap(UP_CANON)
127 let clen: i64 = canon_encode(keys, vals, ncol, canonbuf)
128 let cidp: *u8 = ((cids as i64) + nrows * UP_CIDSLOT) as *u8
129 uxf_cid_profiled(UXF_DATA, canonbuf, clen, cidp)
130 ss_add(w, 1, cidp, canonbuf, clen)
131 cptr[nrows] = canonbuf as i64
132 clenr[nrows] = clen
133 nrows = nrows + 1
134 }
135 }
136 i = le + 1
137 }
138
139 // commit segment and index to THIS GATE's own scratch paths, never a shared fixture name
140 let kout: *u8 = sys_mmap(UP_IDXBUF)
141 let klen: i64 = ss_build_keys(w, kout)
142 ss_writefile("/tmp/nx_uxf_tsv_persist_gate.docs\x00" as *u8, w[0] as *u8, w[1])
143 ss_writefile("/tmp/nx_uxf_tsv_persist_gate.keys\x00" as *u8, kout, klen)
144
145 // REOPEN FRESH FROM DISK -- the point of the rung is that retrieval survives a round trip through storage
146 let szd: *i64 = sys_mmap(8) as *i64
147 let szi: *i64 = sys_mmap(8) as *i64
148 let docs: *u8 = ss_readall("/tmp/nx_uxf_tsv_persist_gate.docs\x00" as *u8, szd)
149 let idx: *u8 = ss_readall("/tmp/nx_uxf_tsv_persist_gate.keys\x00" as *u8, szi)
150
151 let out: *i64 = sys_mmap(16) as *i64
152 var hits: i64 = 0
153 var faithful: i64 = 0
154 var r: i64 = 0
155 while r < nrows {
156 let cidp: *u8 = ((cids as i64) + r * UP_CIDSLOT) as *u8
157 let e: i64 = up_get(idx, cidp, UP_CIDLEN, out)
158 if e >= 0 {
159 hits = hits + 1
160 let voff: i64 = out[0]
161 let vlen: i64 = out[1]
162 if vlen == clenr[r] {
163 if up_bytes_eq(((docs as i64) + voff) as *u8, cptr[r] as *u8, vlen) == 1 { faithful = faithful + 1 }
164 }
165 }
166 r = r + 1
167 }
168
169 // a well-formed but absent content address must MISS rather than collide with a neighbour
170 let bogus: *u8 = "nxc1-01-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\x00" as *u8
171 let miss: i64 = up_get(idx, bogus, UP_CIDLEN, out)
172
173 gv_check_eq("T1 the real fixture parsed to its known row count, so the retrieval teeth cannot pass on one row" as *u8, nrows, UP_WANT_ROWS, ctr)
174 gv_check_eq("T2 every persisted row is retrievable by its content address after a fresh reopen from disk" as *u8, hits, nrows, ctr)
175 gv_check_eq("T3 every retrieved record is BYTE-IDENTICAL to the record that was written" as *u8, faithful, nrows, ctr)
176 var miss_ok: i64 = 0
177 if miss < 0 { miss_ok = 1 }
178 gv_check("neg-control-a-well-formed-but-ABSENT-content-address-MISSES-rather-than-matching-a-neighbour" as *u8, miss_ok, ctr)
179
180 // ABSOLUTE ANCHOR -- the tooth that fixes what the bite exposed. Every other tooth here is SELF-CONSISTENT:
181 // it compares retrieved bytes against what this same code encoded, so a change inside canon_encode moves
182 // both sides identically and all of them still pass. Measured 2026-09-06: nx_gate_bite returned
183 // valid_mutants=2 killed=0 with not_reached=0, meaning the mutants REACHED the code and the gate stayed
184 // green. Pinning one known row's content address as a LITERAL breaks that symmetry, because an encoder
185 // change moves the actual away from a fixed expected. The literal was READ FROM A RUN of this gate and
186 // never recalled -- a June note carries a truncated form of it, and pinning that from memory would be the
187 // fabricated-constant defect this anchor exists to catch.
188 let want_row0: *u8 = "nxc1-01-32bf18462ee9205634459eae0bd16a5b9ac22688cd125c72b6825db0ddd62d69\x00" as *u8
189 gv_check("T5 ABSOLUTE anchor: row zero's content address equals a pinned literal, so an encoder change cannot hide behind self-consistent teeth" as *u8, up_bytes_eq(cids, want_row0, UP_CIDLEN), ctr)
190
191 gv_values_head()
192 gv_kv("tsv_bytes" as *u8, n)
193 gv_kv("rows_persisted" as *u8, nrows)
194 gv_kv("rows_expected" as *u8, UP_WANT_ROWS)
195 gv_kv("segment_bytes" as *u8, w[1])
196 gv_kv("index_bytes" as *u8, klen)
197 gv_kv("reopened_docs_bytes" as *u8, szd[0])
198 gv_kv("reopened_index_bytes" as *u8, szi[0])
199 gv_kv("retrieved_hits" as *u8, hits)
200 gv_kv("byte_faithful" as *u8, faithful)
201 gv_kv("absent_cid_lookup_result" as *u8, miss)
202 gv_puts(" row0_cid = " as *u8); gv_puts(cids); gv_puts("\n" as *u8)
203
204 let rc: i64 = gv_verdict("UXF-TSV-PERSIST-GATE" as *u8, ctr, "a real registry file becomes content-addressed records, survives an fsync and a fresh reopen, and every row comes back byte-identical by its own hash while an absent address misses" as *u8)
205 sys_exit(rc)
206 return rc
207}