nx_debt_recurrence_gate.nx source
↩ module page · 235 lines · 15251 B
1// nx_debt_recurrence_gate.nx -- CE3 (codeeffectiveness ce_recurrence): recurrence per defect class, PROVEN on planted
2// planes with hand-computed answers. In-process over nx_debt_recurrence_lib (the ONE ruler nx_debtmine composes).
3// The accept rule, tooth by tooth: a planted recurring class shows a NON-DECREASING count across two beats (F1 -> F2),
4// a genuinely resolved class FALLS (F1 -> F3), and the measure NAMES the prior filing id and the recurring ids rather
5// than a bare number. Plus: a scope with no resolved prior never counts, the first eaten row is the prior and never
6// its own recurrence, a malformed row is counted not dropped, the emitted JSON carries the ids, the spine sidecar
7// takes one row per measured run and the production spine is untouched. Rows are written with real TAB bytes.
8// F4 (2026-09-06): a plane with MORE distinct scopes than the retired 256-slot table held is counted IN FULL and
9// reports scopes_capped=0 -- the first live beat (4,353 rows) hit that bound and published a floor wearing a total;
10// this tooth is the bite that proves the row-derived capacity REMOVED the cap rather than moved it. F4 also carries
11// one id burst (two rows filed in one epoch-second share an id), the class the live plane showed under stale-mirror.
12// exit: 0 GREEN . 1 RED . 3 SKIP license_tier: ORIGINAL No hw writes.
13import "nx_syscalls.nx"
14import "nx_gate_verdict.nx"
15import "nx_debt_recurrence_lib.nx"
16
17const RG_ROOT: *u8 = "/tmp/nx_debt_recurrence_gate"
18const RG_F1: *u8 = "/tmp/nx_debt_recurrence_gate/f1.plane"
19const RG_F2: *u8 = "/tmp/nx_debt_recurrence_gate/f2.plane"
20const RG_F3: *u8 = "/tmp/nx_debt_recurrence_gate/f3.plane"
21const RG_F4: *u8 = "/tmp/nx_debt_recurrence_gate/f4.plane"
22const RG_F1_SPINE: *u8 = "/tmp/nx_debt_recurrence_gate/f1.plane.recurrence.spine"
23const RG_MODE_RWX: i64 = 493
24const RG_MODE_RW: i64 = 420
25const RG_LINE: i64 = 512
26const RG_DOC: i64 = 65536
27const RG_TAB: i64 = 9
28const RG_NOW: i64 = 1788676000
29const RG_TOPN: i64 = 20
30// the fixture's ids, decided before the run
31const RG_ALPHA_PRIOR: i64 = 100
32const RG_ALPHA_RECUR: i64 = 200
33const RG_ALPHA_AGAIN: i64 = 500
34const RG_GAMMA_PRIOR: i64 = 300
35const RG_F1_ROWS: i64 = 6
36const RG_F1_SCOPES: i64 = 3
37// F4: more distinct scopes than the retired table bound, so a bound would provably bind; every scope recurs once and
38// scope s0 carries the burst pair on top (recur_total = scopes + 2)
39const RG_F4_SCOPES: i64 = 300
40const RG_F4_ID0: i64 = 1000
41const RG_F4_BURST_ID: i64 = 2000
42const RG_F4_BURST_ROWS: i64 = 2
43const RG_OLD_TABLE_BOUND: i64 = 256
44// measure out-slots: the mappings the lookups need, handed back beside the count
45const RG_M_ST: i64 = 0
46const RG_M_IDS: i64 = 1
47const RG_M_Q: i64 = 2
48const RG_M_N: i64 = 3
49const RG_M_CAP: i64 = 4
50const RG_M_SLOTS: i64 = 8
51
52func rg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
53func rg_w(fd: i64, s: *u8) -> i64 { return sys_write(fd, s, rg_slen(s)) }
54func rg_tab(fd: i64) -> i64 { let b: *u8 = sys_mmap(4); b[0] = RG_TAB as u8; return sys_write(fd, b, 1) }
55func rg_nl(fd: i64) -> i64 { let b: *u8 = sys_mmap(4); b[0] = DRC_NL as u8; return sys_write(fd, b, 1) }
56func rg_n(fd: i64, v: i64) -> i64 { let b: *u8 = sys_mmap(RG_LINE); let o: i64 = sj_catn(b, 0, v); return sys_write(fd, b, o) }
57// one plane row in the wire shape: id TAB sev TAB scope TAB status TAB desc
58func rg_row(fd: i64, id: i64, sev: i64, scope: *u8, status: *u8, desc: *u8) -> i64 {
59 rg_n(fd, id); rg_tab(fd); rg_n(fd, sev); rg_tab(fd); rg_w(fd, scope); rg_tab(fd); rg_w(fd, status); rg_tab(fd); rg_w(fd, desc); rg_nl(fd)
60 return 0
61}
62func rg_open(path: *u8) -> i64 { sys_unlinkat(path); return sys_openat_wr(path, RG_MODE_RW) }
63// the shared six rows: alpha eaten-then-open, beta open-open (no prior), gamma eaten-eaten, plus one malformed row
64func rg_base(fd: i64, alpha_second_status: *u8) -> i64 {
65 rg_row(fd, RG_ALPHA_PRIOR, 7, "alpha" as *u8, "eaten" as *u8, "first filing" as *u8)
66 rg_row(fd, RG_ALPHA_RECUR, 7, "alpha" as *u8, alpha_second_status, "filed again" as *u8)
67 rg_row(fd, 150, 6, "beta" as *u8, "open" as *u8, "one" as *u8)
68 rg_row(fd, 250, 6, "beta" as *u8, "open" as *u8, "two" as *u8)
69 rg_row(fd, RG_GAMMA_PRIOR, 5, "gamma" as *u8, "eaten" as *u8, "done" as *u8)
70 rg_row(fd, 400, 5, "gamma" as *u8, "eaten" as *u8, "done again" as *u8)
71 rg_w(fd, "nocolumns" as *u8); rg_nl(fd)
72 return 0
73}
74// F4: RG_F4_SCOPES distinct scopes s0..s(N-1), each eaten then re-filed, then the burst pair on s0 (one buffer, reused)
75func rg_f4(fd: i64) -> i64 {
76 let nm: *u8 = sys_mmap(RG_LINE)
77 var k: i64 = 0
78 while k < RG_F4_SCOPES {
79 var o: i64 = sj_cat(nm, 0, "s" as *u8)
80 o = sj_catn(nm, o, k)
81 nm[o] = 0 as u8
82 rg_row(fd, RG_F4_ID0 + k * 2, 5, nm, "eaten" as *u8, "prior" as *u8)
83 rg_row(fd, RG_F4_ID0 + k * 2 + 1, 5, nm, "open" as *u8, "again" as *u8)
84 k = k + 1
85 }
86 rg_row(fd, RG_F4_BURST_ID, 5, "s0" as *u8, "open" as *u8, "burst one" as *u8)
87 rg_row(fd, RG_F4_BURST_ID, 5, "s0" as *u8, "open" as *u8, "burst two" as *u8)
88 return 0
89}
90func rg_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
91func rg_size(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 } let s: i64 = sys_lseek(fd, 0, 2); sys_close(fd); return s }
92func rg_lines(path: *u8) -> i64 {
93 let ln: *i64 = sys_mmap(16) as *i64
94 let b: *u8 = sys_read_file(path, ln)
95 if (b as i64) == 0 { return 0 - 1 }
96 var c: i64 = 0
97 var i: i64 = 0
98 while i < ln[0] { if b[i] == (DRC_NL as u8) { c = c + 1 } i = i + 1 }
99 return c
100}
101func rg_find(buf: *u8, n: i64, needle: *u8) -> i64 {
102 let m: i64 = rg_slen(needle)
103 if m == 0 { return 0 }
104 var i: i64 = 0
105 while i + m <= n { var j: i64 = 0; var ok: i64 = 1; while j < m { if buf[i + j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } if ok == 1 { return 1 } i = i + 1 }
106 return 0
107}
108// measure a plane file in-process with a table sized from ITS rows (the same derivation the miner uses); the out slots
109// hand back the mappings the scope-name lookups need. Returns the scope count, or -1 when the plane is unreadable.
110func rg_measure(path: *u8, m: *i64, r: *i64) -> i64 {
111 let ln: *i64 = sys_mmap(16) as *i64
112 let b: *u8 = sys_read_file(path, ln)
113 if (b as i64) == 0 { return 0 - 1 }
114 let cap: i64 = drc_capacity(b, ln[0])
115 let st: *i64 = sys_mmap(cap * DRC_S_SLOTS * DRC_WORD) as *i64
116 let ids: *i64 = sys_mmap(cap * DRC_IDS_PER_SCOPE * DRC_WORD) as *i64
117 m[RG_M_ST] = st as i64
118 m[RG_M_IDS] = ids as i64
119 m[RG_M_Q] = b as i64
120 m[RG_M_N] = ln[0]
121 m[RG_M_CAP] = cap
122 return drc_scan(b, ln[0], cap, st, ids, r)
123}
124func rg_scope_slot(q: *u8, st: *i64, nsc: i64, name: *u8) -> i64 {
125 var k: i64 = 0
126 while k < nsc {
127 let b: i64 = k * DRC_S_SLOTS
128 if st[b + DRC_S_LEN] == rg_slen(name) {
129 var m: i64 = 0
130 var eq: i64 = 1
131 while m < st[b + DRC_S_LEN] { if q[st[b + DRC_S_OFF] + m] != name[m] { eq = 0; m = st[b + DRC_S_LEN] } else { m = m + 1 } }
132 if eq == 1 { return b }
133 }
134 k = k + 1
135 }
136 return 0 - 1
137}
138
139func main(argc: i64, argv: *i64) -> i64 {
140 let ctr: *i64 = gv_ctr()
141 gv_head("NX-DEBT-RECURRENCE-GATE: recurrence per defect class -- a planted recurring class never falls, a resolved one does, the ids are named, and a 300-scope plane is counted in full" as *u8)
142 sys_mkdir(RG_ROOT, RG_MODE_RWX)
143 var fd: i64 = rg_open(RG_F1); rg_base(fd, "open" as *u8); sys_close(fd)
144 fd = rg_open(RG_F2); rg_base(fd, "open" as *u8); rg_row(fd, RG_ALPHA_AGAIN, 7, "alpha" as *u8, "open" as *u8, "and again" as *u8); sys_close(fd)
145 fd = rg_open(RG_F3); rg_base(fd, "eaten" as *u8); sys_close(fd)
146 fd = rg_open(RG_F4); rg_f4(fd); sys_close(fd)
147 sys_unlinkat(RG_F1_SPINE)
148 if gv_need("fixture planes written under /tmp" as *u8, rg_exists(RG_F3) * rg_exists(RG_F4), ctr) == 0 { return gv_verdict("nx_debt_recurrence_gate" as *u8, ctr, "fixture tree unwritable" as *u8) }
149 let m1: *i64 = sys_mmap(RG_M_SLOTS * DRC_WORD) as *i64
150 let m2: *i64 = sys_mmap(RG_M_SLOTS * DRC_WORD) as *i64
151 let m3: *i64 = sys_mmap(RG_M_SLOTS * DRC_WORD) as *i64
152 let m4: *i64 = sys_mmap(RG_M_SLOTS * DRC_WORD) as *i64
153 let r1: *i64 = sys_mmap(DRC_R_SLOTS * DRC_WORD) as *i64
154 let r2: *i64 = sys_mmap(DRC_R_SLOTS * DRC_WORD) as *i64
155 let r3: *i64 = sys_mmap(DRC_R_SLOTS * DRC_WORD) as *i64
156 let r4: *i64 = sys_mmap(DRC_R_SLOTS * DRC_WORD) as *i64
157 let n1: i64 = rg_measure(RG_F1, m1, r1)
158 let n2: i64 = rg_measure(RG_F2, m2, r2)
159 let n3: i64 = rg_measure(RG_F3, m3, r3)
160 let n4: i64 = rg_measure(RG_F4, m4, r4)
161 if gv_need("every fixture plane readable" as *u8, ((n1 >= 0) as i64) * ((n2 >= 0) as i64) * ((n3 >= 0) as i64) * ((n4 >= 0) as i64), ctr) == 0 { return gv_verdict("nx_debt_recurrence_gate" as *u8, ctr, "fixture plane unreadable" as *u8) }
162 // the plane bytes stay mapped for the scope-name lookups below
163 let q1: *u8 = m1[RG_M_Q] as *u8
164 let st1: *i64 = m1[RG_M_ST] as *i64
165 let id1: *i64 = m1[RG_M_IDS] as *i64
166 let st2: *i64 = m2[RG_M_ST] as *i64
167 let st3: *i64 = m3[RG_M_ST] as *i64
168 // ---- F1 exact counts ----
169 gv_check_eq("f1-rows-counted-comment-free" as *u8, r1[DRC_R_ROWS], RG_F1_ROWS, ctr)
170 gv_check_eq("f1-malformed-row-counted-not-dropped" as *u8, r1[DRC_R_MALFORMED], 1, ctr)
171 gv_check_eq("f1-three-scopes" as *u8, n1, RG_F1_SCOPES, ctr)
172 let ba: i64 = rg_scope_slot(q1, st1, n1, "alpha" as *u8)
173 let bb: i64 = rg_scope_slot(q1, st1, n1, "beta" as *u8)
174 let bg: i64 = rg_scope_slot(q1, st1, n1, "gamma" as *u8)
175 gv_check("f1-every-scope-resolves-by-name" as *u8, (ba >= 0) as i64 * (bb >= 0) as i64 * (bg >= 0) as i64, ctr)
176 gv_check_eq("alpha-prior-is-the-first-eaten-id-NAMED" as *u8, st1[ba + DRC_S_PRIOR], RG_ALPHA_PRIOR, ctr)
177 gv_check_eq("alpha-one-recurrence-total" as *u8, st1[ba + DRC_S_RTOTAL], 1, ctr)
178 gv_check_eq("alpha-one-recurrence-open" as *u8, st1[ba + DRC_S_ROPEN], 1, ctr)
179 gv_check_eq("alpha-recurring-id-NAMED" as *u8, id1[(ba / DRC_S_SLOTS) * DRC_IDS_PER_SCOPE], RG_ALPHA_RECUR, ctr)
180 gv_check_eq("beta-no-resolved-prior-so-no-recurrence" as *u8, st1[bb + DRC_S_RTOTAL], 0, ctr)
181 gv_check_eq("gamma-prior-named-and-its-later-eaten-filing-is-history-not-live" as *u8, st1[bg + DRC_S_PRIOR] * 100 + st1[bg + DRC_S_RTOTAL] * 10 + st1[bg + DRC_S_ROPEN], RG_GAMMA_PRIOR * 100 + 10, ctr)
182 gv_check_eq("f1-totals-two-scopes-recur-one-open-two-total" as *u8, r1[DRC_R_SCOPES_RECUR] * 100 + r1[DRC_R_ROPEN] * 10 + r1[DRC_R_RTOTAL], 212, ctr)
183 gv_check_eq("f1-no-id-burst" as *u8, r1[DRC_R_IDBURST], 0, ctr)
184 // ---- two beats: planted recurring class NON-DECREASING (F1 -> F2), resolved class FALLS (F1 -> F3) ----
185 let ba2: i64 = rg_scope_slot(q1, st2, n2, "alpha" as *u8)
186 gv_check("planted-recurring-class-non-decreasing-across-two-beats-open" as *u8, (st2[ba2 + DRC_S_ROPEN] >= st1[ba + DRC_S_ROPEN]) as i64, ctr)
187 gv_check("planted-recurring-class-non-decreasing-across-two-beats-total" as *u8, (st2[ba2 + DRC_S_RTOTAL] >= st1[ba + DRC_S_RTOTAL]) as i64, ctr)
188 gv_check_eq("second-beat-alpha-open-recurrence-is-two" as *u8, st2[ba2 + DRC_S_ROPEN], 2, ctr)
189 let ba3: i64 = rg_scope_slot(q1, st3, n3, "alpha" as *u8)
190 gv_check("genuinely-resolved-class-falls" as *u8, (st3[ba3 + DRC_S_ROPEN] < st1[ba + DRC_S_ROPEN]) as i64, ctr)
191 gv_check_eq("resolved-class-open-recurrence-zero-history-kept" as *u8, st3[ba3 + DRC_S_ROPEN] * 10 + st3[ba3 + DRC_S_RTOTAL], 1, ctr)
192 // ---- F4: the row-derived capacity counts every scope where the retired bound would have bound ----
193 gv_check("f4-fixture-exceeds-the-retired-table-bound" as *u8, (RG_F4_SCOPES > RG_OLD_TABLE_BOUND) as i64, ctr)
194 gv_check("f4-capacity-derived-from-rows-is-at-least-the-rows" as *u8, (m4[RG_M_CAP] >= r4[DRC_R_ROWS]) as i64, ctr)
195 gv_check_eq("f4-every-scope-counted-none-lost" as *u8, n4, RG_F4_SCOPES, ctr)
196 gv_check_eq("f4-scopes-capped-is-zero-by-construction" as *u8, r4[DRC_R_CAPPED], 0, ctr)
197 gv_check_eq("f4-every-scope-recurs" as *u8, r4[DRC_R_SCOPES_RECUR], RG_F4_SCOPES, ctr)
198 gv_check_eq("f4-recur-total-is-scopes-plus-the-burst-pair" as *u8, r4[DRC_R_RTOTAL], RG_F4_SCOPES + RG_F4_BURST_ROWS, ctr)
199 gv_check_eq("f4-one-adjacent-id-burst-counted-and-declared" as *u8, r4[DRC_R_IDBURST], 1, ctr)
200 // ---- the document names the ids ----
201 let d: *u8 = sys_mmap(RG_DOC)
202 var p: i64 = 0
203 d[p] = DRC_LBRACE as u8; p = p + 1
204 p = drc_emit_json(d, p, q1, st1, id1, n1, r1, RG_TOPN)
205 d[p] = DRC_RBRACE as u8; p = p + 1
206 gv_check("json-names-the-prior-id" as *u8, rg_find(d, p, "\"prior_id\":100" as *u8), ctr)
207 gv_check("json-names-the-recurring-ids" as *u8, rg_find(d, p, "\"recur_ids\":[200]" as *u8), ctr)
208 gv_check("json-declares-close-time-unrecorded-and-the-upper-bound" as *u8, rg_find(d, p, "\"close_time\":\"UNRECORDED\",\"bound\":\"UPPER\"" as *u8), ctr)
209 gv_check("json-ranks-alpha-first-by-open-recurrence" as *u8, rg_find(d, p, "\"by_scope\":[{\"scope\":\"alpha\"" as *u8), ctr)
210 gv_check("json-declares-scopes-capped-zero-and-the-burst-count" as *u8, rg_find(d, p, "\"scopes_capped\":0" as *u8) * rg_find(d, p, "\"id_bursts_adjacent\":0" as *u8), ctr)
211 // ---- the spine: sidecar beside the fixture, one row per measured run, production untouched ----
212 let prod_before: i64 = rg_size(DRC_SPINE_PROD)
213 var ps: i64 = 0
214 ps = drc_spine_json(d, 0, RG_F1, RG_NOW, r1)
215 ps = drc_spine_json(d, 0, RG_F1, RG_NOW + 1, r1)
216 let prod_after: i64 = rg_size(DRC_SPINE_PROD)
217 gv_check_eq("two-measured-runs-leave-two-sidecar-rows" as *u8, rg_lines(RG_F1_SPINE), 2, ctr)
218 gv_check("neg-control-production-spine-unchanged-by-fixture-runs" as *u8, (prod_before == prod_after) as i64, ctr)
219 let r0: *i64 = sys_mmap(DRC_R_SLOTS * DRC_WORD) as *i64
220 let pz: i64 = drc_spine_json(d, 0, RG_F2, RG_NOW, r0)
221 gv_check("no-rows-measured-writes-no-trend-point" as *u8, rg_find(d, pz, "\"path\":\"NONE\"" as *u8), ctr)
222 gv_bite("recurrence-fires-on-the-refiled-class-and-is-silent-on-the-never-resolved-one" as *u8, (st1[ba + DRC_S_RTOTAL] > 0) as i64, (st1[bb + DRC_S_RTOTAL] > 0) as i64, ctr)
223 gv_values_head()
224 gv_kv("f1_recur_open" as *u8, r1[DRC_R_ROPEN])
225 gv_kv("f1_recur_total" as *u8, r1[DRC_R_RTOTAL])
226 gv_kv("f2_alpha_recur_open" as *u8, st2[ba2 + DRC_S_ROPEN])
227 gv_kv("f3_alpha_recur_open" as *u8, st3[ba3 + DRC_S_ROPEN])
228 gv_kv("f4_scopes" as *u8, n4)
229 gv_kv("f4_capacity" as *u8, m4[RG_M_CAP])
230 gv_kv("f4_rows" as *u8, r4[DRC_R_ROWS])
231 gv_kv("f4_id_bursts_adjacent" as *u8, r4[DRC_R_IDBURST])
232 gv_kv("prod_spine_size_before" as *u8, prod_before)
233 gv_kv("prod_spine_size_after" as *u8, prod_after)
234 return gv_verdict("nx_debt_recurrence_gate" as *u8, ctr, "recurrence per class reproduces hand-computed answers on four planted planes; the planted class never falls across two beats, the resolved class does, ids are named, close time is declared unrecorded, a 300-scope plane is counted in full with no cap, and the trend is written only beside a fixture" as *u8)
235}