code wiki / _hdl_build / nx_suitebench_lib.nx
nx_suitebench_lib.nx source
↩ module page · 155 lines · 6193 B
1// nx_suitebench_lib.nx -- the pure core of the suite ruler: file reading, literal citation search, the
2// in-place row splitter, and the two ranking keys. NO main, so BOTH the CLI (nx_suitebench) and the gate
3// (nx_suitebench_gate) drive the SAME bytes. This is the library-split the CONNECT lane already paid for:
4// an organ that carries main() is unimportable, so its gate ends up re-implementing the logic it claims to
5// verify -- and then the gate proves a copy, not the shipped code.
6// license_tier: ORIGINAL
7import "nx_syscalls.nx"
8
9const SB_NFIELD: i64 = 8
10const SB_HAS: i64 = 1
11const SB_LACKS: i64 = 0
12const SB_UNKNOWN: i64 = 9
13const SB_PARITY: i64 = 2
14const SB_MAXLVL: i64 = 3
15const SB_PIPE: i64 = 124
16const SB_HASH: i64 = 35
17const SB_NL: i64 = 10
18
19func sb_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
20func sb_eq(a: *u8, b: *u8) -> i64 {
21 var i: i64=0
22 while 1==1 {
23 if a[i]!=b[i] { return 0 }
24 if a[i]==(0 as u8) { return 1 }
25 i=i+1
26 }
27 return 0
28}
29func sb_atoi(s: *u8) -> i64 {
30 var v: i64=0; var i: i64=0
31 while s[i]!=(0 as u8) {
32 let c: i64 = s[i] as i64
33 if c>=48 { if c<=57 { v=v*10+(c-48) } }
34 i=i+1
35 }
36 return v
37}
38// whole-file read into buf; returns bytes (0 if the file will not open -- callers must fail loud on 0)
39func sb_read(path: *u8, buf: *u8, cap: i64) -> i64 {
40 let fd: i64 = sys_openat_rd(path)
41 if fd<0 { return 0 }
42 var total: i64=0
43 while total<cap {
44 let r: i64 = sys_read(fd, (((buf as i64)+total) as *u8), cap-total)
45 if r<=0 { break }
46 total=total+r
47 }
48 sys_close(fd)
49 return total
50}
51// PORTABLE EVIDENCE ROOT. The ruler was born on the laptop, where the corpus sits under the working
52// directory. On the NAS the same tree unpacks under buildroot/runtime, so a hard-coded relative path made
53// the instrument unable to run where the SSOT actually lives -- and a gate that cannot run beside the code
54// it grades will eventually go red for a filesystem reason indistinguishable from a real regression
55// (debt seq884). So the reader TRIES a short, ordered list of roots and reports which one answered. This
56// is rule 17 applied to evidence: same instrument, wherever it lands, and it says out loud where it looked.
57// ⚠ NISHILANG GOTCHA, measured 2026-07-25 with nx_sbroot_probe: an EMPTY string literal is not a distinct
58// pointer -- `return "" as *u8` handed back a pointer to the NEXT literal in the pool, so the "here" root
59// silently resolved to "runtime/" and every rooted read failed while the code read as obviously correct.
60// The local root is therefore spelled "./", which is a real two-byte literal and means the same thing.
61const SB_ROOTS: i64 = 4
62func sb_root(i: i64) -> *u8 {
63 if i==0 { return "./" as *u8 }
64 if i==1 { return "./runtime/" as *u8 }
65 if i==2 { return "./buildroot/runtime/" as *u8 }
66 return "./buildroot/" as *u8
67}
68func sb_join(dst: *u8, pre: *u8, path: *u8) -> i64 {
69 var k: i64=0
70 var i: i64=0
71 while pre[i]!=(0 as u8) { dst[k]=pre[i]; k=k+1; i=i+1 }
72 i=0
73 while path[i]!=(0 as u8) { dst[k]=path[i]; k=k+1; i=i+1 }
74 dst[k]=0 as u8
75 return k
76}
77// read `path` from whichever known root holds it; scratch must be >= the longest joined path
78func sb_read_rooted(path: *u8, buf: *u8, cap: i64, scratch: *u8) -> i64 {
79 var r: i64=0
80 while r<SB_ROOTS {
81 sb_join(scratch, sb_root(r), path)
82 let n: i64 = sb_read(scratch, buf, cap)
83 if n>0 { return n }
84 r=r+1
85 }
86 return 0
87}
88// which root answered for this path? -1 = nowhere. Callers print this so "the corpus is missing" and
89// "the corpus moved" are never the same message.
90func sb_which_root(path: *u8, scratch: *u8, probe: *u8, probecap: i64) -> i64 {
91 var r: i64=0
92 while r<SB_ROOTS {
93 sb_join(scratch, sb_root(r), path)
94 if sb_read(scratch, probe, probecap)>0 { return r }
95 r=r+1
96 }
97 return 0-1
98}
99
100// literal substring search over n bytes. This is the citation check: an incumbent claim is only allowed to
101// count if its phrase is actually present in the banked corpus.
102func sb_find(buf: *u8, n: i64, needle: *u8) -> i64 {
103 let m: i64 = sb_len(needle)
104 if m==0 { return 0 }
105 var i: i64=0
106 while i+m<=n {
107 var j: i64=0; var hit: i64=1
108 while j<m { if buf[i+j]!=needle[j] { hit=0; j=m } else { j=j+1 } }
109 if hit==1 { return 1 }
110 i=i+1
111 }
112 return 0
113}
114// Split one '|'-separated row IN PLACE: separators and the newline become NUL, so each field is a normal
115// C string pointing into the buffer -- no copying, no second allocation. Returns the field count.
116func sb_split(buf: *u8, start: i64, end: i64, fld: **u8) -> i64 {
117 var nf: i64=0
118 var fs: i64=start
119 var i: i64=start
120 while i<=end {
121 var cut: i64=0
122 if i==end { cut=1 }
123 if buf[i]==(SB_PIPE as u8) { cut=1 }
124 if cut==1 {
125 if nf<SB_NFIELD {
126 buf[i]=0 as u8
127 fld[nf] = ((buf as i64)+fs) as *u8
128 nf=nf+1
129 }
130 fs=i+1
131 }
132 i=i+1
133 }
134 return nf
135}
136// how far short of production-grade this axis is
137func sb_shortfall(honest: i64) -> i64 { return SB_MAXLVL - honest }
138// the MEASURED ranking key. +1 so an axis the operator asked for still ranks by shortfall even when no
139// incumbent ships it -- a component nobody else has is not automatically a component nobody needs.
140func sb_rank(has: i64, honest: i64) -> i64 { return (has+1)*sb_shortfall(honest) }
141// does this axis enter the build queue at all?
142func sb_admit(has: i64, op: i64, honest: i64) -> i64 {
143 if sb_shortfall(honest)<=0 { return 0 }
144 if has>0 { return 1 }
145 if op==1 { return 1 }
146 return 0
147}
148// EXT-ONLY LAW: a rival cell contributes to the head-to-head denominator only when the rival actually ships
149// the capability AND the claim is cited. LACKS and UNMEASURED cells contribute nothing, so we can never
150// score well by grading ourselves against a capability nobody ships.
151func sb_is_ext(state: i64, cited: i64) -> i64 {
152 if state!=SB_HAS { return 0 }
153 if cited!=1 { return 0 }
154 return 1
155}