code wiki / _hdl_build / nx_gate_baseline_sweep.nx

nx_gate_baseline_sweep.nx source

↩ module page · 364 lines · 17000 B

1// nx_gate_baseline_sweep.nx -- MASS-WRAP orchestrator: roll nx_gate_baseline_harness across the whole 2// gate universe in bounded, resumable batches (the rollout engine for the 87%-no-baseline blind spot, 3// [[project-nishi-sensor-gap-census-2026-07-14]]). 4// 5// usage: nx_gate_baseline_sweep <budget> budget = max gates to wrap this run; 0 = DRY RUN (list only) 6// 7// Walks runtime/_hdl_build/*.nx and classifies each file: 8// NON-GATE -- no verdict marker (libs/specs) -> never wrapped (running a lib = rc-102 noise) 9// INFRA -- the builder / the harness / this sweep itself -> skipped by name 10// RISKY -- name has daemon|serve|bench|monitor|watch OR source touches sockets/https: 11// blind-running those can HANG (listeners), hit the network, or run for minutes. 12// SKIPPED BY POLICY and PRINTED (no silent caps) -- they need supervised wrapping. 13// DONE -- ledger knowledge/status/gatebase_<gate>.tsv already exists (resume for free) 14// WRAP -- safe + unwrapped: fork _offc/nx_gate_baseline_harness.elf <gate> (which itself forks 15// the sovereign lane, captures rc+telemetry, banks the baseline). Harness rc!=0 => 16// REFUSED (a broken gate can't become a baseline) -> reported: the sweep doubles as a 17// live BROKEN-GATES census. 18// Never-brick: the sweep itself writes NOTHING (all writes happen inside the harness: /tmp captures + 19// new gatebase_*.tsv ledgers, atomic). Deterministic classification; bounded loops; budget-bounded forks. 20// Sovereign nx_cc->nxasm; run with CWD=nxc2 root. license_tier: ORIGINAL 21import "syscalls.nx" 22import "runtime.nx" 23import "nx_dirent.nx" 24import "nx_fcntl.nx" 25import "nx_handoff_gate.nx" 26const K_MAGIC_16384: i64 = 16384 27const K_MAGIC_1024: i64 = 1024 28const K_MAGIC_1048576: i64 = 1048576 29 30// ---- helpers (census-proven idioms) ---------------------------------- 31func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 32 33func lc(c: i64) -> i64 { if c >= 65 { if c <= 90 { return c + 32 } } return c } 34 35func is_alnum(c: i64) -> i64 { 36 if c >= 48 { if c <= 57 { return 1 } } 37 if c >= 97 { if c <= 122 { return 1 } } 38 if c >= 65 { if c <= 90 { return 1 } } 39 if c == 95 { return 1 } 40 return 0 41} 42 43func streqz(a: *u8, b: *u8) -> i64 { 44 var i: i64 = 0 45 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 46 if b[i] != (0 as u8) { return 0 } 47 return 1 48} 49 50func byte_copy(dst: *u8, src: *u8, n: i64) -> i64 { 51 var i: i64 = 0 52 while i < n { dst[i] = src[i]; i = i + 1 } 53 return 0 54} 55 56// case-insensitive substring; ndl lowercase; word==1 -> non-alnum boundaries (the census liar-killed matcher) 57func find_ci(buf: *u8, lo: i64, hi: i64, ndl: *u8, word: i64) -> i64 { 58 let nl: i64 = slen(ndl) 59 if nl == 0 { return 1 } 60 var i: i64 = lo 61 while i + nl <= hi { 62 var j: i64 = 0 63 var ok: i64 = 1 64 while j < nl { 65 if lc(buf[i+j] as i64) != (ndl[j] as i64) { ok = 0; j = nl } else { j = j + 1 } 66 } 67 if ok == 1 { 68 if word == 0 { return 1 } 69 var lb: i64 = 1 70 var rb: i64 = 1 71 if i > lo { if is_alnum(buf[i-1] as i64) == 1 { lb = 0 } } 72 let r: i64 = i + nl 73 if r < hi { if is_alnum(buf[r] as i64) == 1 { rb = 0 } } 74 if lb == 1 { if rb == 1 { return 1 } } 75 } 76 i = i + 1 77 } 78 return 0 79} 80 81// verdict-emitter check (same markers as nx_sensor_gap_census -> the two organs agree on the universe) 82func is_gate(buf: *u8, len: i64) -> i64 { 83 if find_ci(buf, 0, len, "verdict" as *u8, 0) == 1 { return 1 } 84 if find_ci(buf, 0, len, "expect_exit" as *u8, 0) == 1 { return 1 } 85 if find_ci(buf, 0, len, "[pass]" as *u8, 0) == 1 { return 1 } 86 if find_ci(buf, 0, len, "[fail]" as *u8, 0) == 1 { return 1 } 87 if find_ci(buf, 0, len, "green" as *u8, 1) == 1 { return 1 } 88 if find_ci(buf, 0, len, "red" as *u8, 1) == 1 { return 1 } 89 return 0 90} 91 92// blind-run risk policy: listeners hang, network gates fetch, benches run long -> supervised wrapping only 93func is_risky(name: *u8, nlen: i64, buf: *u8, len: i64) -> i64 { 94 if find_ci(name, 0, nlen, "daemon" as *u8, 0) == 1 { return 1 } 95 if find_ci(name, 0, nlen, "serve" as *u8, 0) == 1 { return 1 } 96 if find_ci(name, 0, nlen, "bench" as *u8, 0) == 1 { return 1 } 97 if find_ci(name, 0, nlen, "monitor" as *u8, 0) == 1 { return 1 } 98 if find_ci(name, 0, nlen, "watch" as *u8, 0) == 1 { return 1 } 99 if find_ci(buf, 0, len, "sys_listen" as *u8, 0) == 1 { return 1 } 100 if find_ci(buf, 0, len, "sys_bind" as *u8, 0) == 1 { return 1 } 101 if find_ci(buf, 0, len, "sys_accept" as *u8, 0) == 1 { return 1 } 102 if find_ci(buf, 0, len, "sys_connect" as *u8, 0) == 1 { return 1 } 103 if find_ci(buf, 0, len, "https" as *u8, 0) == 1 { return 1 } 104 if find_ci(buf, 0, len, "sys_sendto" as *u8, 0) == 1 { return 1 } 105 if find_ci(buf, 0, len, "nxsecret" as *u8, 0) == 1 { return 1 } 106 return 0 107} 108 109// author-declared build-only artifact (e.g. DLL-export sources whose main is a linker stub never meant 110// to run -- proven: nishi_export_lib SIGSEGVs if executed). Evidence = the author's own "--build-only" 111// marker in the source; running such a file tells us nothing and can crash. 112func is_build_only(buf: *u8, len: i64) -> i64 { 113 return find_ci(buf, 0, len, "--build-only" as *u8, 0) 114} 115 116func ends_nx(name: *u8, len: i64) -> i64 { 117 if len < 3 { return 0 } 118 if name[len-3] != (0x2E as u8) { return 0 } 119 if name[len-2] != (0x6E as u8) { return 0 } 120 if name[len-1] != (0x78 as u8) { return 0 } 121 return 1 122} 123 124// bounded whole-file read into reused buf; -1 if unopenable 125func read_src(path: *u8, buf: *u8, cap: i64) -> i64 { 126 let fd: i64 = sys_openat_rd(path) 127 if fd < 0 { return 0 - 1 } 128 var total: i64 = 0 129 var go: i64 = 1 130 while go == 1 { 131 go = 0 132 let tail: *u8 = ((buf as i64) + total) as *u8 133 let n: i64 = sys_read(fd, tail, cap - total) 134 if n > 0 { total = total + n; if total < cap { go = 1 } } 135 } 136 sys_close(fd) 137 return total 138} 139 140// fork+wait the harness on one gate; returns WEXITSTATUS or 128+sig (signal-aware, proven idiom) 141func run_harness(gname: *u8, envp: *i64) -> i64 { 142 let hpath: *u8 = "_offc/nx_gate_baseline_harness.elf" as *u8 143 let av: *i64 = sys_mmap(8 * 4) as *i64 144 av[0] = hpath as i64 145 av[1] = gname as i64 146 av[2] = 0 147 let pid: i64 = sys_fork() 148 if pid == 0 { 149 sys_execve(hpath, av, envp) 150 sys_exit(127) 151 } 152 let st: *i64 = sys_mmap(16) as *i64 153 sys_wait4(pid, st, 0) 154 let sig: i64 = st[0] & 0x7f 155 if sig != 0 { return 128 + sig } 156 return (st[0] >> 8) & 0xff 157} 158 159func main(argc: i64, argv: *i64) -> i64 { 160 var budget: i64 = 8 161 if argc >= 2 { 162 let a1: *u8 = argv[1] as *u8 163 let bl: i64 = slen(a1) 164 let bv: *i64 = sys_mmap(8 * 2) as *i64 165 let bn: i64 = hg_parse_ints(a1, bl, bv, 1) 166 if bn >= 1 { budget = bv[0] } 167 } 168 print("=== nx_gate_baseline_sweep: budget=" as *u8) 169 print_i64(budget) 170 if budget == 0 { print(" (DRY RUN)" as *u8) } 171 print(" ===\n" as *u8) 172 173 let envp: *i64 = sys_mmap(8 * 4) as *i64 174 envp[0] = ("PATH=/usr/bin:/bin" as *u8) as i64 175 envp[1] = 0 176 177 let dirp: *u8 = "runtime/_hdl_build" as *u8 178 let dlen: i64 = slen(dirp) 179 let dfd: i64 = nx_openat(NX_AT_FDCWD, dirp, NX_O_RDONLY | NX_O_DIRECTORY, 0) 180 if dfd < 0 { 181 print(" [RED] cannot open runtime/_hdl_build (run from nxc2 root)\n" as *u8) 182 sys_exit(1) 183 return 1 184 } 185 let dbuf: *u8 = sys_mmap(K_MAGIC_16384) 186 let dr_raw: *u8 = sys_mmap(NX_DIRENT_BYTES) 187 let dr: *NxDirent = dr_raw as *NxDirent 188 let path: *u8 = sys_mmap(K_MAGIC_1024) 189 let gname: *u8 = sys_mmap(256) 190 let ledger: *u8 = sys_mmap(K_MAGIC_1024) 191 let fbuf: *u8 = sys_mmap(K_MAGIC_1048576 + 16) 192 193 var n_nongate: i64 = 0 194 var n_infra: i64 = 0 195 var n_risky: i64 = 0 196 var n_done: i64 = 0 197 var n_att: i64 = 0 198 var n_wrapped: i64 = 0 199 var n_refused: i64 = 0 200 var n_known: i64 = 0 201 var n_bonly: i64 = 0 202 var n_remaining: i64 = 0 203 var shown_risky: i64 = 0 204 var shown_known: i64 = 0 205 var shown_bonly: i64 = 0 206 var shown_would: i64 = 0 207 208 var batch: i64 = nx_dirent_read(dfd, dbuf, K_MAGIC_16384) 209 while batch > 0 { 210 var off: i64 = 0 211 while off < batch { 212 let next_off: i64 = nx_dirent_iter(dbuf, off, batch, dr) 213 if next_off <= 0 { off = batch + 1 } 214 if off <= batch { 215 let nmlen: i64 = nx_dirent_name_len(dr) 216 if ends_nx(dr.name, nmlen) == 1 { 217 // full path + bare gate name (strip .nx) 218 byte_copy(path, dirp, dlen) 219 path[dlen] = 47 as u8 220 byte_copy(((path as i64) + dlen + 1) as *u8, dr.name, nmlen) 221 path[dlen + 1 + nmlen] = 0 as u8 222 byte_copy(gname, dr.name, nmlen - 3) 223 gname[nmlen - 3] = 0 as u8 224 225 var infra: i64 = 0 226 if streqz(gname, "nx_gate_baseline_sweep" as *u8) == 1 { infra = 1 } 227 if streqz(gname, "nx_gate_baseline_harness" as *u8) == 1 { infra = 1 } 228 if streqz(gname, "nx_sov_build_run" as *u8) == 1 { infra = 1 } 229 230 let flen: i64 = read_src(path, fbuf, K_MAGIC_1048576) 231 if flen >= 0 { 232 var bonly: i64 = 0 233 if infra == 0 { bonly = is_build_only(fbuf, flen) } 234 if bonly == 1 { 235 n_bonly = n_bonly + 1 236 if shown_bonly < 6 { 237 print(" [build-only-skip] " as *u8) 238 print(gname) 239 print("\n" as *u8) 240 shown_bonly = shown_bonly + 1 241 } 242 } 243 if infra == 1 { n_infra = n_infra + 1 } 244 var live: i64 = 0 245 if infra == 0 { if bonly == 0 { live = 1 } } 246 if live == 1 { 247 if is_gate(fbuf, flen) == 0 { n_nongate = n_nongate + 1 } 248 if is_gate(fbuf, flen) == 1 { 249 if is_risky(dr.name, nmlen, fbuf, flen) == 1 { 250 n_risky = n_risky + 1 251 if shown_risky < 10 { 252 print(" [risky-skip] " as *u8) 253 print(gname) 254 print("\n" as *u8) 255 shown_risky = shown_risky + 1 256 } 257 } 258 if is_risky(dr.name, nmlen, fbuf, flen) == 0 { 259 // ledger present? (resume) 260 var lo: i64 = 0 261 lo = 0 262 let pre: *u8 = "knowledge/status/gatebase_" as *u8 263 var pi: i64 = 0 264 while pre[pi] != (0 as u8) { ledger[lo] = pre[pi]; lo = lo + 1; pi = pi + 1 } 265 var gi: i64 = 0 266 while gname[gi] != (0 as u8) { ledger[lo] = gname[gi]; lo = lo + 1; gi = gi + 1 } 267 let suf: *u8 = ".tsv" as *u8 268 var si: i64 = 0 269 while suf[si] != (0 as u8) { ledger[lo] = suf[si]; lo = lo + 1; si = si + 1 } 270 ledger[lo] = 0 as u8 271 let has_l: i64 = hg_evidence_file(ledger) 272 // known-broken marker (harness-banked on refusal): skip, count, stay visible. 273 // a later successful capture writes the .tsv which supersedes the marker. 274 var known: i64 = 0 275 if has_l == 0 { 276 let msuf: *u8 = ".refused" as *u8 277 var mi: i64 = 0 278 while msuf[mi] != (0 as u8) { ledger[lo] = msuf[mi]; lo = lo + 1; mi = mi + 1 } 279 ledger[lo] = 0 as u8 280 if hg_evidence_file(ledger) == 1 { known = 1 } 281 } 282 if has_l == 1 { n_done = n_done + 1 } 283 if known == 1 { 284 n_known = n_known + 1 285 if shown_known < 10 { 286 print(" [known-broken-skip] " as *u8) 287 print(gname) 288 print("\n" as *u8) 289 shown_known = shown_known + 1 290 } 291 } 292 if has_l == 0 { if known == 0 { 293 if n_att < budget { 294 // budget bounds ATTEMPTS (gate executions) = bounded wall-clock per run 295 n_att = n_att + 1 296 print(" [attempt " as *u8) 297 print_i64(n_att) 298 print("/" as *u8) 299 print_i64(budget) 300 print("] " as *u8) 301 print(gname) 302 print(" -> " as *u8) 303 let hrc: i64 = run_harness(gname, envp) 304 print("harness rc=" as *u8) 305 print_i64(hrc) 306 print("\n" as *u8) 307 if hrc == 0 { n_wrapped = n_wrapped + 1 } 308 if hrc != 0 { 309 n_refused = n_refused + 1 310 print(" [REFUSED/BROKEN-GATE] " as *u8) 311 print(gname) 312 print(" (gate rc!=0 -> not banked; fix or supervise)\n" as *u8) 313 } 314 } else { 315 n_remaining = n_remaining + 1 316 if budget == 0 { 317 if shown_would < 16 { 318 print(" [would-wrap] " as *u8) 319 print(gname) 320 print("\n" as *u8) 321 shown_would = shown_would + 1 322 } 323 } 324 } 325 } 326 } 327 } 328 } 329 } 330 } 331 } 332 off = next_off 333 } 334 } 335 batch = nx_dirent_read(dfd, dbuf, K_MAGIC_16384) 336 } 337 sys_close(dfd) 338 339 print("---- sweep tally ----\n" as *u8) 340 print(" non-gate files = " as *u8) 341 print_i64(n_nongate) 342 print("\n infra (self/builder) = " as *u8) 343 print_i64(n_infra) 344 print("\n RISKY skipped = " as *u8) 345 print_i64(n_risky) 346 print(" (listeners/network/bench -> supervised wrapping only)\n" as *u8) 347 print(" build-only artifacts = " as *u8) 348 print_i64(n_bonly) 349 print(" (author-declared; running tells nothing, can crash)\n" as *u8) 350 print(" already DONE = " as *u8) 351 print_i64(n_done) 352 print("\n wrapped this run = " as *u8) 353 print_i64(n_wrapped) 354 print("\n REFUSED this run = " as *u8) 355 print_i64(n_refused) 356 print("\n known-broken (marked)= " as *u8) 357 print_i64(n_known) 358 print(" (see gatebase_*.tsv.refused for class; re-test = run harness directly)\n" as *u8) 359 print(" remaining unwrapped = " as *u8) 360 print_i64(n_remaining) 361 print("\n=== verdict: GREEN (sweep mechanics; refusals above are FINDINGS, not sweep failures) ===\n" as *u8) 362 sys_exit(0) 363 return 0 364}