nx_verify.nx source
↩ module page · 462 lines · 29453 B
1// nx_verify.nx -- CONSOLIDATED Nishi-unique verification tool (per NISHI_TOOL_ARCHITECTURE: 50+
2// organs -> 15 verb-based tools; "new capability = a VERB, not a new micro-organ"). This is the
3// nx_verify tool: ONE entry point, verbs, that ABSORBS the session's separate self-analysis organs
4// so they stop sprawling as individual stubs. Imports the canonical nx_capability_triage_core (no
5// reinvention). SUPERSEDES the standalone nx_capability_triage.nx + nx_triage_ground.nx CLIs (their
6// cores stay as libs; these two verbs are now the interface).
7// nx_verify triage [registry] -> route/wire/build/measure per capability + operator proposals
8// (anti-navel-gazing: SOTA credited ONLY on 3rd-party bench)
9// nx_verify ground [registry] -> cross-check has_organ vs DISK FACTS (gate>organ>absent); flags
10// any claim the disk can't back (DISCREPANCY)
11// Future verify verbs (claim / crossval / census / adversary) fold in HERE, not as new organs.
12// Always exit 0; journals a summary. license_tier: ORIGINAL expect_exit: 0
13import "nx_syscalls.nx"
14import "nx_capability_triage_core.nx" // tr_decide / tr_proposal / tr_parse_row / verdict names + ccz_*
15// RESTORED 2026-08-15. The `coverage` verb existed in the DEPLOYED binary and in NO source copy --
16// neither nx_verify.nx nor its .bak-itoa2 twin imported this lib, so every rebuild silently dropped it.
17// Found by nx_artifactdrift (served 51,445 > built 49,085 => AHEAD) and NAMED by nx_contentdiff, which
18// listed the exact lost runs: PARITY / EXCEED / UNIQUE / "=== BENCH-COVERAGE:" / the UNMEASURED banner.
19// ******THE SOURCE DESCRIBED AS FUTURE WORK WHAT THE DEPLOYED BINARY ALREADY DID -- the header below
20// still lists census among "future verify verbs" while the live tool has been answering it. A header
21// that predicts a feature the artifact already ships is indistinguishable from one that never got built.
22// **THE DEFINITION WAS NEVER LOST, ONLY THE CALL SITE: nx_bench_census_lib.nx has always been present
23// and nx_bench_census_gate.nx still calls it. Verify the call site, not the definition.
24import "nx_bench_census_lib.nx" // bc_census / bc_report -- the BENCH-COVERAGE census engine
25const V_MAGIC_1048576: i64 = 1048576
26const V_MAGIC_1048575: i64 = 1048575
27const V_MAGIC_65536: i64 = 65536
28const V_MAGIC_65535: i64 = 65535
29const V_MAGIC_1024: i64 = 1024
30
31func v_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
32func v_putn(v: i64) -> i64 { nxi_out(v); return 0 }
33func v_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 }
34// count occurrences of lit in buf[0,n). Used by the `portability` verb (RESTORED 2026-08-15).
35func v_countlit(buf: *u8, n: i64, lit: *u8) -> i64 {
36 var ll: i64 = 0
37 while lit[ll] != (0 as u8) { ll = ll + 1 }
38 if ll == 0 { return 0 }
39 var c: i64 = 0
40 var i: i64 = 0
41 while i + ll <= n {
42 var k: i64 = 0
43 var hit: i64 = 1
44 while k < ll { if buf[i+k] != lit[k] { hit = 0; k = ll } else { k = k + 1 } }
45 if hit == 1 { c = c + 1 }
46 i = i + 1
47 }
48 return c
49}
50// does s contain lit anywhere? 1/0
51func v_hasstr(s: *u8, lit: *u8) -> i64 {
52 var sl: i64 = 0
53 while s[sl] != (0 as u8) { sl = sl + 1 }
54 if v_countlit(s, sl, lit) > 0 { return 1 }
55 return 0
56}
57
58// --- shared: read registry + a per-row driver (verb-specific body via mode) ---
59// print the task_class (field 0) of a line
60func v_print_task(buf: *u8, ls: i64, le: i64) -> i64 {
61 var i: i64 = ls
62 while i < le { if buf[i] == (124 as u8) { i = le } else { sys_write(1, (buf as i64 + i) as *u8, 1); i = i + 1 } }
63 return 0
64}
65// copy the idx-th '|' field of buf[ls..le) into out; returns length or -1
66func v_field(buf: *u8, ls: i64, le: i64, idx: i64, out: *u8) -> i64 {
67 var f: i64 = 0; var s: i64 = ls; var i: i64 = ls; var go: i64 = 1
68 while go == 1 { go = 0
69 if i <= le {
70 var atend: i64 = 0; if i == le { atend = 1 }
71 var atbar: i64 = 0; if i < le { if buf[i] == (124 as u8) { atbar = 1 } }
72 if atend == 1 { if f == idx { var o: i64 = 0; var k: i64 = s; while k < i { out[o]=buf[k]; o=o+1; k=k+1 } out[o]=0 as u8; return o } f = f + 1 }
73 else { if atbar == 1 { if f == idx { var o2: i64=0; var k2: i64=s; while k2<i { out[o2]=buf[k2]; o2=o2+1; k2=k2+1 } out[o2]=0 as u8; return o2 } f=f+1; s=i+1 } i=i+1; go=1 }
74 }
75 }
76 return 0 - 1
77}
78func v_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
79
80// nth (0-based) space-delimited token of buf[ls..le) parsed as an unsigned int; -1 if absent.
81func v_line_int(buf: *u8, ls: i64, le: i64, tok: i64) -> i64 {
82 var t: i64 = 0
83 var i: i64 = ls
84 // skip leading spaces
85 var go: i64 = 1
86 while go == 1 { go = 0; if i < le { if buf[i] == (32 as u8) { i = i + 1; go = 1 } } }
87 while t < tok {
88 // advance past this token
89 go = 1
90 while go == 1 { go = 0; if i < le { if buf[i] != (32 as u8) { i = i + 1; go = 1 } } }
91 go = 1
92 while go == 1 { go = 0; if i < le { if buf[i] == (32 as u8) { i = i + 1; go = 1 } } }
93 t = t + 1
94 }
95 if i >= le { return 0 - 1 }
96 let ep: *i64 = sys_mmap(16) as *i64
97 return ccz_num_at(buf, le, i, ep)
98}
99// does buf[ls..le) start with prefix (after leading ws)?
100func v_starts(buf: *u8, ls: i64, le: i64, pre: *u8) -> i64 {
101 var i: i64 = ls
102 var go: i64 = 1
103 while go == 1 { go = 0; if i < le { if buf[i] == (32 as u8) { i = i + 1; go = 1 } } }
104 var k: i64 = 0
105 while pre[k] != (0 as u8) { if i + k >= le { return 0 } if buf[i+k] != pre[k] { return 0 } k = k + 1 }
106 return 1
107}
108
109// ground helpers (inlined disk-fact grade -- mirrors cc_our_grade: gate>organ>absent)
110func v_exists(path: *u8) -> i64 { let sb: *u8 = sys_mmap(256); if sys_fstatat(path, sb) < 0 { return 0 } return 1 }
111func v_probeable(name: *u8, n: i64) -> i64 {
112 if n < 4 { return 0 }
113 if name[0] != (110 as u8) { return 0 }
114 if name[1] != (120 as u8) { return 0 }
115 if name[2] != (95 as u8) { return 0 }
116 var i: i64 = 0
117 while i < n { let c: i64 = name[i] as i64; var ok: i64 = 0; if c >= 97 { if c <= 122 { ok = 1 } } if c >= 48 { if c <= 57 { ok = 1 } } if c == 95 { ok = 1 } if ok == 0 { return 0 } i = i + 1 }
118 return 1
119}
120func v_grade(name: *u8) -> i64 {
121 let p: *u8 = sys_mmap(512); var o: i64 = 0
122 o = ccz_cat_str(p, 0, "runtime/" as *u8); o = ccz_cat_str(p, o, name); o = ccz_cat_str(p, o, "_gate.nx" as *u8)
123 if v_exists(p) == 1 { return 4 }
124 o = ccz_cat_str(p, 0, "runtime/" as *u8); o = ccz_cat_str(p, o, name); o = ccz_cat_str(p, o, ".nx" as *u8)
125 if v_exists(p) == 1 { return 3 }
126 o = ccz_cat_str(p, 0, "runtime/" as *u8); o = ccz_cat_str(p, o, name); o = ccz_cat_str(p, o, "_core.nx" as *u8)
127 if v_exists(p) == 1 { return 3 }
128 return 0
129}
130
131// --- magic-number detector (rule-11 enforcement). Builds on the team's mask-51 predicate + adds the
132// ALLOWLIST the nx_magicnum_preventer header names as the "next rung": a numeric literal is a MAGIC
133// number unless it is on a const-line, part of an identifier, hex (0x), a cast (`N as T`), or a
134// mmap/syscall argument (buffer size / syscall number). Budget is a NAMED const (no magic threshold).
135const V_MAGIC_BUDGET: i64 = 0 // zero-tolerance default for a checked file/diff
136const V_DATA_DENS_X100: i64 = 80 // >=0.8 magic/line = a data table (bias/mesh/font/KAT), not logic -> EXEMPT
137const V_DATA_MIN_CNT: i64 = 50 // ...AND has many literals: floors out line-packed logic one-liners gaming density
138const V_DENS_SCALE: i64 = 100 // density is reported x100 (fixed-point, no floats)
139const V_RADIX10_LEN: i64 = 2 // "10" is two chars (the radix-extraction allowlist match)
140const V_ASCII_NL: i64 = 10
141const V_ASCII_SP: i64 = 32
142const V_ASCII_LP: i64 = 40 // '('
143const V_ASCII_0: i64 = 48
144const V_ASCII_1: i64 = 49
145const V_ASCII_2: i64 = 50
146const V_ASCII_9: i64 = 57
147const V_ASCII_x: i64 = 120
148const V_ASCII_PCT: i64 = 37 // '%'
149const V_ASCII_SLASH: i64 = 47 // '/'
150const V_ASCII_DQ: i64 = 34 // '"' string delimiter -- numbers inside strings are text, not magic (skip like comments)
151const V_ASCII_BS: i64 = 92 // '\' escape -- an escaped quote does not close the string
152
153func v_isdig(c: u8) -> i64 { if c >= (V_ASCII_0 as u8) { if c <= (V_ASCII_9 as u8) { return 1 } } return 0 }
154func v_isaln(c: u8) -> i64 {
155 if c >= (V_ASCII_0 as u8) { if c <= (V_ASCII_9 as u8) { return 1 } }
156 if c >= (65 as u8) { if c <= (90 as u8) { return 1 } }
157 if c >= (97 as u8) { if c <= (122 as u8) { return 1 } }
158 if c == (95 as u8) { return 1 }
159 return 0
160}
161func v_matchat(buf: *u8, i: i64, n: i64, lit: *u8) -> i64 { var k: i64 = 0; while lit[k] != (0 as u8) { if i + k >= n { return 0 } if buf[i+k] != lit[k] { return 0 } k = k + 1 } return 1 }
162func v_scan_magic(buf: *u8, n: i64) -> i64 {
163 var count: i64 = 0; var i: i64 = 0; var incomm: i64 = 0; var lconst: i64 = 0; var instr: i64 = 0
164 // ★★★★★★A COUNT WITHOUT A WORKLIST IS NOT ACTIONABLE. This gate reported `magic_numbers=69` and said
165 // "move each to a named const" without ever saying WHICH -- so the owner of a failing file had no
166 // place to start, and the offenders stayed. THE ENFORCER'S OWN UNACTIONABILITY IS PART OF WHY THE
167 // THING IT FORBIDS PERSISTS. Line tracking costs one counter on a walk that already visits every byte.
168 var line: i64 = 1
169 while i < n {
170 let c: u8 = buf[i]
171 if c == (V_ASCII_NL as u8) { incomm = 0; lconst = 0; instr = 0; line = line + 1; i = i + 1 } else {
172 if incomm == 1 { i = i + 1 } else {
173 if instr == 1 {
174 if c == (V_ASCII_DQ as u8) { var esc: i64 = 0; if i > 0 { if buf[i-1] == (V_ASCII_BS as u8) { esc = 1 } } if esc == 0 { instr = 0 } }
175 i = i + 1
176 } else {
177 if c == (V_ASCII_DQ as u8) { instr = 1; i = i + 1 } else {
178 if v_matchat(buf, i, n, "//" as *u8) == 1 { incomm = 1; i = i + 2 } else {
179 if v_matchat(buf, i, n, "const " as *u8) == 1 { lconst = 1; i = i + 6 } else {
180 if v_isdig(c) == 1 {
181 var prevaln: i64 = 0
182 if i > 0 { if v_isaln(buf[i-1]) == 1 { prevaln = 1 } }
183 var ishex: i64 = 0
184 if i >= 2 { if buf[i-1] == (V_ASCII_x as u8) { if buf[i-2] == (V_ASCII_0 as u8) { ishex = 1 } } }
185 let start: i64 = i
186 var go: i64 = 1
187 while go == 1 { go = 0; if i < n { if v_isdig(buf[i]) == 1 { i = i + 1; go = 1 } } }
188 let runlen: i64 = i - start
189 var v2: i64 = 0
190 if runlen >= 2 { v2 = 1 } else { if buf[start] >= (V_ASCII_2 as u8) { v2 = 1 } }
191 var j: i64 = i
192 var g2: i64 = 1
193 while g2 == 1 { g2 = 0; if j < n { if buf[j] == (V_ASCII_SP as u8) { j = j + 1; g2 = 1 } } }
194 var castnext: i64 = 0
195 if v_matchat(buf, j, n, "as " as *u8) == 1 { castnext = 1 }
196 var argctx: i64 = 0
197 if start >= 1 { if buf[start-1] == (V_ASCII_LP as u8) {
198 if start >= 5 { if v_matchat(buf, start-5, n, "mmap(" as *u8) == 1 { argctx = 1 } }
199 if start >= 8 { if v_matchat(buf, start-8, n, "syscall(" as *u8) == 1 { argctx = 1 } }
200 } }
201 // radix-10: `% 10` / `/ 10` is decimal-string conversion (structural radix, not a tunable
202 // threshold). allowlisting it keeps the gate from crying wolf on every putn -- a noisy gate
203 // gets disabled, which is exactly how the original preventer ended up off.
204 var radix10: i64 = 0
205 if runlen == V_RADIX10_LEN { if buf[start] == (V_ASCII_1 as u8) { if buf[start+1] == (V_ASCII_0 as u8) {
206 var p: i64 = start - 1
207 var gp: i64 = 1
208 while gp == 1 { gp = 0; if p >= 0 { if buf[p] == (V_ASCII_SP as u8) { p = p - 1; gp = 1 } } }
209 if p >= 0 { if buf[p] == (V_ASCII_PCT as u8) { radix10 = 1 } }
210 if p >= 0 { if buf[p] == (V_ASCII_SLASH as u8) { radix10 = 1 } }
211 } } }
212 var magic: i64 = 1
213 if lconst == 1 { magic = 0 }
214 if prevaln == 1 { magic = 0 }
215 if ishex == 1 { magic = 0 }
216 if castnext == 1 { magic = 0 }
217 if argctx == 1 { magic = 0 }
218 if radix10 == 1 { magic = 0 }
219 if v2 == 0 { magic = 0 }
220 if magic == 1 {
221 count = count + 1
222 // NAME IT: line + the literal itself, so the remedy is a lookup instead of a hunt. Printed
223 // BEFORE the verdict line, which stays LAST -- the estate's readers anchor by position.
224 v_puts(" MAGIC line=" as *u8); v_putn(line)
225 v_puts(" value=" as *u8); sys_write(1, ((buf as i64) + start) as *u8, runlen)
226 v_puts("\n" as *u8)
227 }
228 } else { i = i + 1 } } } } } } }
229 }
230 return count
231}
232
233func main(argc: i64, argv: *i64) -> i64 {
234 if argc < 2 { v_puts("usage: nx_verify triage|ground|lap|magic [args]\n" as *u8); return 2 }
235 let verb: *u8 = argv[1] as *u8
236 var reg: *u8 = "knowledge/capability_triage.reg" as *u8
237 // only triage/ground take an optional registry as argv[2]; for `lap`, argv[2] is the TARGET
238 if argc >= 3 { if v_streq(verb, "triage" as *u8) == 1 { reg = argv[2] as *u8 } if v_streq(verb, "ground" as *u8) == 1 { reg = argv[2] as *u8 } }
239
240 // magic: rule-11 gate. dispatched BEFORE the registry read -- it needs no registry and must run from
241 // ANY cwd (the pre-commit ratchet / build lane call it on arbitrary files). exit 1 = enforcement.
242 if v_streq(verb, "magic" as *u8) == 1 {
243 if argc < 3 { v_puts("usage: nx_verify magic <file> [budget]\n" as *u8); return 2 }
244 let mfile: *u8 = argv[2] as *u8
245 var mbudget: i64 = V_MAGIC_BUDGET
246 if argc >= 4 { let ep2: *i64 = sys_mmap(16) as *i64; let b: i64 = ccz_num_at(argv[3] as *u8, v_slen(argv[3] as *u8), 0, ep2); if b >= 0 { mbudget = b } }
247 let mbuf: *u8 = sys_mmap(V_MAGIC_1048576)
248 let mn: i64 = ccz_read(mfile, mbuf, V_MAGIC_1048575)
249 if mn <= 0 { v_puts("nx_verify magic: cannot read " as *u8); v_puts(mfile); v_puts("\n" as *u8); return 0 }
250 let cnt: i64 = v_scan_magic(mbuf, mn)
251 var lines: i64 = 1
252 var li: i64 = 0
253 while li < mn { if mbuf[li] == (V_ASCII_NL as u8) { lines = lines + 1 } li = li + 1 }
254 var dens: i64 = 0
255 if lines > 0 { dens = (cnt * V_DENS_SCALE) / lines }
256 var isdata: i64 = 0
257 if dens >= V_DATA_DENS_X100 { if cnt >= V_DATA_MIN_CNT { isdata = 1 } }
258 v_puts("VERIFY-MAGIC file=" as *u8); v_puts(mfile); v_puts(" magic_numbers=" as *u8); v_putn(cnt); v_puts(" lines=" as *u8); v_putn(lines); v_puts(" dens_x100=" as *u8); v_putn(dens); v_puts(" budget=" as *u8); v_putn(mbudget); v_puts(" -> " as *u8)
259 if isdata == 1 { v_puts("EXEMPT (data table, not logic thresholds)\n" as *u8); return 0 }
260 if cnt <= mbudget { v_puts("PASS (rule-11 clean)\n" as *u8); return 0 }
261 v_puts("FAIL (move each to a named const)\n" as *u8)
262 return 1
263 }
264
265 // --- coverage: the BENCH-COVERAGE census (the anti-navel-gazing engine). RESTORED 2026-08-15.
266 // Of the LIVE tool surface, how many are graded against an EXTERNAL SOTA oracle? An ungraded tool is
267 // PRINTED debt, never silently GREEN. Defaults are the paths the live binary uses, so the verb answers
268 // with no args; both stay overridable because the denominator is a DATA choice, not a law.
269 // ⚠PLACEMENT IS LOAD-BEARING: this MUST sit ABOVE the capability-registry pre-read below. This verb
270 // reads its OWN two files and has nothing to do with that registry, but the pre-read returns early on
271 // `no-registry`, so a coverage block placed after it is UNREACHABLE. Measured when I first restored it
272 // one block too low: staged output was exactly 22 bytes -- the length of "nx_verify no-registry" plus
273 // newline -- while the live binary emitted 38,044. **AN EARLY RETURN IN A SHARED PREAMBLE SILENTLY
274 // DISABLES EVERY VERB DECLARED BELOW IT, AND THE SYMPTOM IS A SHORT PLAUSIBLE MESSAGE, NOT AN ERROR.**
275 // --- portability: is this organ NishiOS-portable, or is it welded to a host OS? RESTORED 2026-08-15
276 // (second verb found missing from source; see the coverage note below). Counts raw OS couplings in
277 // LOGIC and names the two legitimate seams instead of flagging them -- ★A PORTABILITY RULE WITHOUT AN
278 // EXEMPT SEAM FLAGS THE ONE FILE THAT IS SUPPOSED TO HOLD THE COUPLING, AND THEN EVERYONE IGNORES IT.
279 if v_streq(verb, "portability" as *u8) == 1 {
280 if argc < 3 { v_puts("usage: nx_verify portability <file.nx>\n" as *u8); return 0 }
281 let pf: *u8 = argv[2] as *u8
282 v_puts("VERIFY-PORT file=" as *u8); v_puts(pf)
283 // SEAM 1: the ABI seam. Every syscall in the estate goes through here BY DESIGN.
284 if v_hasstr(pf, "nx_syscalls" as *u8) == 1 { v_puts(" -> EXEMPT (the ABI seam)\n" as *u8); return 0 }
285 // SEAM 2: the OS-feature seam -- nx_os_* is the ONE place OS-specifics are allowed to live.
286 if v_hasstr(pf, "nx_os_" as *u8) == 1 { v_puts(" -> EXEMPT (OS-feature seam: the ONE place OS-specifics live)\n" as *u8); return 0 }
287 // SEAM 3: gate fixtures legitimately touch the build host they run on.
288 if v_hasstr(pf, "_gate" as *u8) == 1 { v_puts(" -> EXEMPT (gate fixture, runs on the build host)\n" as *u8); return 0 }
289 // SEAM 4: THE SELF-REFERENCE GUARD. The triage core NAMES these patterns in order to hunt them,
290 // so a scanner without this exemption reports its own pattern table as the worst offender in the
291 // estate. ★★A DETECTOR THAT SCANS SOURCE WILL FIND ITSELF, AND THE FIRST THING IT ACCUSES IS THE
292 // LIST OF THINGS IT WAS BUILT TO ACCUSE.
293 if v_hasstr(pf, "triage_core" as *u8) == 1 { v_puts(" -> EXEMPT (scanner core: names the coupling patterns it hunts)\n" as *u8); return 0 }
294 let plen: *i64 = sys_mmap(16) as *i64
295 let pbuf: *u8 = sys_read_file(pf, plen)
296 if (pbuf as i64) == 0 { v_puts(" -> cannot read\n" as *u8); return 0 }
297 let pn: i64 = plen[0]
298 // PATH LITERALS, not prose: the leading quote is part of each pattern, so a COMMENT mentioning
299 // /proc does not score. ★A SCANNER THAT DOES NOT SKIP COMMENTS MEASURES THE DOCUMENTATION.
300 var coup: i64 = 0
301 coup = coup + v_countlit(pbuf, pn, "\"/proc" as *u8)
302 coup = coup + v_countlit(pbuf, pn, "\"/sys" as *u8)
303 coup = coup + v_countlit(pbuf, pn, "\"/dev" as *u8)
304 coup = coup + v_countlit(pbuf, pn, "\"/tmp" as *u8)
305 coup = coup + v_countlit(pbuf, pn, "__syscall(" as *u8)
306 v_puts(" couplings=" as *u8); v_putn(coup)
307 if coup == 0 { v_puts(" -> PASS (OS-agnostic logic; NishiOS-portable)\n" as *u8); return 0 }
308 v_puts(" -> FAIL (raw /proc|/sys|/dev|/tmp|__syscall in LOGIC -- move behind an nx_os_* seam)\n" as *u8)
309 return 0
310 }
311 if v_streq(verb, "coverage" as *u8) == 1 {
312 // BOTH paths are REQUIRED, matching the deployed contract. I first gave them defaults as a
313 // nicety; the live binary's own usage line (recovered from the lost-run list) proves it demands
314 // them. ★WHEN A DEPLOYED BINARY IS THE SPEC, FIDELITY BEATS CONVENIENCE -- a silent default would
315 // let a caller compute a coverage ratio over a denominator they did not choose.
316 if argc < 4 { v_puts("usage: nx_verify coverage <bench_registry.conf> <tool_allowlist.conf>\n" as *u8); return 0 }
317 let regp: *u8 = argv[2] as *u8
318 let allp: *u8 = argv[3] as *u8
319 let rlen: *i64 = sys_mmap(16) as *i64
320 let rbuf: *u8 = sys_read_file(regp, rlen)
321 if (rbuf as i64) == 0 { v_puts("nx_verify coverage: cannot read registry " as *u8); v_puts(regp); v_puts(" -- REFUSING rather than reporting a coverage number over a denominator I could not load\n" as *u8); return 0 }
322 let alen: *i64 = sys_mmap(16) as *i64
323 let abuf: *u8 = sys_read_file(allp, alen)
324 if (abuf as i64) == 0 { v_puts("nx_verify coverage: cannot read allowlist " as *u8); v_puts(allp); v_puts(" -- REFUSING: the allowlist IS the denominator\n" as *u8); return 0 }
325 let counts: *i64 = sys_mmap(8 * 8) as *i64
326 bc_census(abuf, alen[0], rbuf, rlen[0], counts)
327 bc_report(counts)
328 return 0
329 }
330
331 let buf: *u8 = sys_mmap(V_MAGIC_65536)
332 let n: i64 = ccz_read(reg, buf, V_MAGIC_65535)
333 if n <= 0 { v_puts("nx_verify no-registry\n" as *u8); return 0 }
334
335 let out5: *i64 = sys_mmap(64) as *i64
336 let organ: *u8 = sys_mmap(128)
337 let hstr: *u8 = sys_mmap(32)
338 let ep: *i64 = sys_mmap(16) as *i64
339
340 if v_streq(verb, "triage" as *u8) == 1 {
341 let cnt: *i64 = sys_mmap(80) as *i64
342 var k: i64 = 0; while k < 7 { cnt[k] = 0; k = k + 1 }
343 var rows: i64 = 0; var raises: i64 = 0
344 var ls: i64 = 0
345 while ls < n {
346 var le: i64 = ls; var go: i64 = 1
347 while go == 1 { go = 0; if le < n { if buf[le] != (10 as u8) { le = le + 1; go = 1 } } }
348 var i: i64 = ls; go = 1
349 while go == 1 { go = 0; if i < le { let c: i64 = buf[i] as i64; if c == 32 { i = i + 1; go = 1 } else { if c == 9 { i = i + 1; go = 1 } } } }
350 var skip: i64 = 0; if i >= le { skip = 1 } if skip == 0 { if buf[i] == (35 as u8) { skip = 1 } }
351 if skip == 0 { if tr_parse_row(buf, ls, le, out5) == 1 {
352 let v: i64 = tr_decide(out5[0], out5[1], out5[2], out5[3])
353 let prop: i64 = tr_proposal(v, out5[4])
354 cnt[v] = cnt[v] + 1; rows = rows + 1; if prop == 1 { raises = raises + 1 }
355 v_puts("VERIFY-TRIAGE " as *u8); v_print_task(buf, ls, le); v_puts(" -> " as *u8); v_puts(tr_verdict_name(v))
356 if tr_routes_to_nishi(v) == 1 { v_puts(" [Nishi handles]" as *u8) } else { v_puts(" [GAP]" as *u8) }
357 if prop == 1 { v_puts(" ***PROPOSE-INVESTMENT***" as *u8) }
358 v_puts("\n" as *u8)
359 } }
360 ls = le + 1
361 }
362 v_puts("VERIFY-TRIAGE-SUM rows=" as *u8); v_putn(rows)
363 v_puts(" ROUTE=" as *u8); v_putn(cnt[5]); v_puts(" USE_IMPROVE=" as *u8); v_putn(cnt[4])
364 v_puts(" MEASURE=" as *u8); v_putn(cnt[3]); v_puts(" WIRE=" as *u8); v_putn(cnt[2])
365 v_puts(" BUILD=" as *u8); v_putn(cnt[1]); v_puts(" SUSPECT=" as *u8); v_putn(cnt[6])
366 v_puts(" PROPOSALS=" as *u8); v_putn(raises); v_puts("\n" as *u8)
367 return 0
368 }
369 if v_streq(verb, "ground" as *u8) == 1 {
370 var rows: i64 = 0; var grounded: i64 = 0; var discrep: i64 = 0; var unprob: i64 = 0
371 var ls: i64 = 0
372 while ls < n {
373 var le: i64 = ls; var go: i64 = 1
374 while go == 1 { go = 0; if le < n { if buf[le] != (10 as u8) { le = le + 1; go = 1 } } }
375 var i: i64 = ls; go = 1
376 while go == 1 { go = 0; if i < le { let c: i64 = buf[i] as i64; if c == 32 { i = i + 1; go = 1 } else { if c == 9 { i = i + 1; go = 1 } } } }
377 var skip: i64 = 0; if i >= le { skip = 1 } if skip == 0 { if buf[i] == (35 as u8) { skip = 1 } }
378 if skip == 0 { if v_field(buf, ls, le, 1, organ) >= 0 { if v_field(buf, ls, le, 2, hstr) >= 0 {
379 let claimed: i64 = ccz_num_at(hstr, v_slen(hstr), 0, ep)
380 let prob: i64 = v_probeable(organ, v_slen(organ))
381 var grade: i64 = 0; if prob == 1 { grade = v_grade(organ) }
382 var present: i64 = 0; if grade >= 3 { present = 1 }
383 rows = rows + 1
384 var verd: *u8 = "GROUNDED" as *u8
385 if prob == 0 { verd = "UNPROBEABLE" as *u8; unprob = unprob + 1 } else {
386 if claimed == 1 { if present == 1 { grounded = grounded + 1 } else { verd = "DISCREPANCY(claim>disk)" as *u8; discrep = discrep + 1 } } else { grounded = grounded + 1 }
387 }
388 v_puts("VERIFY-GROUND " as *u8); v_puts(organ); v_puts(" claim=" as *u8); v_putn(claimed); v_puts(" disk=" as *u8); v_putn(grade); v_puts(" -> " as *u8); v_puts(verd); v_puts("\n" as *u8)
389 } } }
390 ls = le + 1
391 }
392 v_puts("VERIFY-GROUND-SUM rows=" as *u8); v_putn(rows); v_puts(" grounded=" as *u8); v_putn(grounded); v_puts(" DISCREPANCY=" as *u8); v_putn(discrep); v_puts(" unprobeable=" as *u8); v_putn(unprob); v_puts("\n" as *u8)
393 return 0
394 }
395 // --- lap: the RACING-CREW loop. grade a target's progress toward SOTA across deploy laps ---
396 // ledger knowledge/lap-<target>.ledger: "SOTA <val> <dir 0=lower-better|1=higher-better>" + "LAP <val> <note>".
397 // nx_verify lap <target> -> where-was / where-is / where-to-go + verdict
398 // nx_verify lap <target> record <val> .. -> append a lap after a deploy
399 if v_streq(verb, "lap" as *u8) == 1 {
400 if argc < 3 { v_puts("usage: nx_verify lap <target> [record <val> <note>]\n" as *u8); return 2 }
401 let target: *u8 = argv[2] as *u8
402 let lp: *u8 = sys_mmap(V_MAGIC_1024); var lo: i64 = 0
403 lo = ccz_cat_str(lp, 0, "knowledge/lap-" as *u8); lo = ccz_cat_str(lp, lo, target); lo = ccz_cat_str(lp, lo, ".ledger" as *u8)
404 // NOTE: reg buf `n` above was for the triage registry; re-read the ledger here.
405 let lbuf: *u8 = sys_mmap(V_MAGIC_65536)
406 let ln: i64 = ccz_read(lp, lbuf, V_MAGIC_65535)
407 if argc >= 5 { if v_streq(argv[3] as *u8, "record" as *u8) == 1 {
408 let rec: *u8 = sys_mmap(V_MAGIC_1024); var ro: i64 = 0
409 ro = ccz_cat_str(rec, 0, "LAP " as *u8); ro = ccz_cat_str(rec, ro, argv[4] as *u8)
410 var a: i64 = 5; while a < argc { ro = ccz_cat_str(rec, ro, " " as *u8); ro = ccz_cat_str(rec, ro, argv[a] as *u8); a = a + 1 }
411 ro = ccz_cat_str(rec, ro, "\n" as *u8)
412 let fd: i64 = sys_openat_append(lp, 420)
413 if fd < 0 { v_puts("lap: cannot open ledger\n" as *u8); return 1 }
414 sys_write(fd, rec, ro); sys_close(fd)
415 v_puts("nx_verify lap: recorded -> " as *u8); v_puts(target); v_puts(" = " as *u8); v_puts(argv[4] as *u8); v_puts("\n" as *u8)
416 return 0
417 } }
418 if ln <= 0 { v_puts("nx_verify lap: no ledger for " as *u8); v_puts(target); v_puts(" (record the first lap)\n" as *u8); return 0 }
419 // parse SOTA + laps
420 var sota: i64 = 0 - 1; var dir: i64 = 0
421 let vals: *i64 = sys_mmap(8*512) as *i64
422 var nl: i64 = 0
423 var ls: i64 = 0
424 while ls < ln {
425 var le: i64 = ls; var go: i64 = 1
426 while go == 1 { go = 0; if le < ln { if lbuf[le] != (10 as u8) { le = le + 1; go = 1 } } }
427 if v_starts(lbuf, ls, le, "SOTA" as *u8) == 1 { sota = v_line_int(lbuf, ls, le, 1); let d: i64 = v_line_int(lbuf, ls, le, 2); if d >= 0 { dir = d } }
428 else { if v_starts(lbuf, ls, le, "LAP" as *u8) == 1 { let vv: i64 = v_line_int(lbuf, ls, le, 1); if vv >= 0 { if nl < 512 { vals[nl] = vv; nl = nl + 1 } } } }
429 ls = le + 1
430 }
431 if nl == 0 { v_puts("nx_verify lap: ledger has no LAP rows\n" as *u8); return 0 }
432 let first: i64 = vals[0]
433 let last: i64 = vals[nl-1]
434 var prev: i64 = last; if nl >= 2 { prev = vals[nl-2] }
435 v_puts("=== RACING-CREW LAP REPORT: " as *u8); v_puts(target); v_puts(" (laps=" as *u8); v_putn(nl)
436 if dir == 0 { v_puts(", lower=better" as *u8) } else { v_puts(", higher=better" as *u8) }
437 v_puts(") ===\n" as *u8)
438 v_puts(" WHERE-IT-WAS (lap " as *u8); v_putn(nl-1); v_puts("): " as *u8); v_putn(prev); v_puts("\n" as *u8)
439 v_puts(" WHERE-IT-IS (lap " as *u8); v_putn(nl); v_puts("): " as *u8); v_putn(last)
440 let delta: i64 = last - prev
441 var improving: i64 = 0
442 if dir == 0 { if delta < 0 { improving = 1 } } else { if delta > 0 { improving = 1 } }
443 v_puts(" [delta " as *u8); v_putn(delta); if improving == 1 { v_puts(" IMPROVING]" as *u8) } else { if delta == 0 { v_puts(" HELD]" as *u8) } else { v_puts(" REGRESSED]" as *u8) } }
444 v_puts("\n" as *u8)
445 v_puts(" WHERE-TO-GO (SOTA): " as *u8); v_putn(sota)
446 var gap: i64 = last - sota; if dir == 1 { gap = sota - last }
447 v_puts(" [gap " as *u8); v_putn(gap)
448 var atsota: i64 = 0
449 if dir == 0 { if last <= sota { atsota = 1 } } else { if last >= sota { atsota = 1 } }
450 v_puts("]\n" as *u8)
451 // progress permille of the original gap closed
452 var span: i64 = first - sota; if dir == 1 { span = sota - first }
453 if span > 0 { var closed: i64 = span - gap; if closed < 0 { closed = 0 }
454 v_puts(" PROGRESS: " as *u8); v_putn(closed * 1000 / span); v_puts("/1000 of the original gap closed since lap 1\n" as *u8) }
455 v_puts(" VERDICT: " as *u8)
456 if atsota == 1 { v_puts("AT-OR-BEYOND-SOTA (podium)\n" as *u8) } else { if improving == 1 { v_puts("APPROACHING-SOTA (converging -- attack the largest remaining gap next lap)\n" as *u8) } else { v_puts("STALLED/REGRESSED (change the lever -- re-run census+adversary for new information)\n" as *u8) } }
457 return 0
458 }
459
460 v_puts("usage: nx_verify triage|ground|lap|magic [args]\n" as *u8)
461 return 2
462}