code wiki / _hdl_build / nx_capcliff_gate.nx

nx_capcliff_gate.nx source

↩ module page · 74 lines · 5008 B

1// nx_capcliff_gate.nx -- END-TO-END gate for the reader-cap cliff detector and its remedy census (/compare/dataio 2// DI8 + DI13): forks the SUBJECT ARTIFACT's own selftest (the size ruler and its opposites, the anchoring classifier 3// on planted read sites, the sizeguard forked in both directions, the scan verdict that carries RED only for an 4// ungoverned or breached cliff) and inherits its verdict, then pins the artifact to THIS generation by a tooth only 5// this generation prints. Same shape as nx_io_envelope_census_gate. 6// exit: 0 GREEN / 1 RED (gv_verdict law). 7import "nx_syscalls.nx" 8import "nx_gate_verdict.nx" 9import "nx_tool_run.nx" 10import "nx_srcfresh.nx" 11 12const CG_OUT: i64 = 262144 13 14func cg_find(q: *u8, n: i64, lit: *u8) -> i64 { 15 var ll: i64 = 0 16 while lit[ll] != (0 as u8) { ll = ll + 1 } 17 if ll == 0 { return 0 } 18 var i: i64 = 0 19 while i + ll <= n { 20 var j: i64 = 0 21 var ok: i64 = 1 22 while j < ll { if q[i + j] != lit[j] { ok = 0; j = ll } else { j = j + 1 } } 23 if ok == 1 { return 1 } 24 i = i + 1 25 } 26 return 0 27} 28func cg_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 } 29 30func main(argc: i64, argv: *i64) -> i64 { 31 let ctr: *i64 = gv_ctr() 32 gv_head("nx_capcliff_gate -- forks the detector's own selftest; the exit code carries the verdict" as *u8) 33 let built: *u8 = "buildroot/_build/nx_capcliff.sov.elf" as *u8 34 let built_local: *u8 = "_build/nx_capcliff.sov.elf" as *u8 35 let promoted: *u8 = "./nx_capcliff.elf" as *u8 36 var subject: *u8 = promoted 37 var which: *u8 = "PROMOTED" as *u8 38 // A JUST-BUILT fossil is the subject ONLY when it is NEWER than the promoted binary: a refused build leaves the 39 // previous generation's fossil standing, and a gate that prefers it reports on bytes nothing serves (the sibling 40 // census gate read RED 6/8 on the NAS that way, 2026-09-06). An absent promoted binary reads mtime -1. 41 if cg_exists(built_local) == 1 { if sf_mtime_ns(built_local) > sf_mtime_ns(promoted) { subject = built_local; which = "JUST-BUILT-local(newer than promoted)" as *u8 } } 42 if cg_exists(built) == 1 { if sf_mtime_ns(built) > sf_mtime_ns(promoted) { subject = built; which = "JUST-BUILT(newer than promoted)" as *u8 } } 43 let have: i64 = cg_exists(subject) 44 gv_puts("SUBJECT " as *u8); gv_puts(which); gv_puts(" " as *u8); gv_puts(subject); gv_puts("\n" as *u8) 45 gv_check("T1 subject artifact resolved and openable" as *u8, have, ctr) 46 if have == 0 { return gv_verdict("nx_capcliff_gate" as *u8, ctr, "no subject artifact at any root; nothing was proven" as *u8) } 47 let av: *i64 = sys_mmap(32) as *i64 48 av[0] = subject as i64 49 av[1] = "selftest" as *u8 as i64 50 av[2] = 0 51 let cout: *u8 = sys_mmap(CG_OUT) 52 let colen: *i64 = sys_mmap(16) as *i64 53 colen[0] = 0 54 let ex: i64 = tr_run_capture(subject, av, cout, CG_OUT - 8, colen) 55 let n: i64 = colen[0] 56 gv_kv("selftest_exit" as *u8, ex) 57 gv_kv("selftest_bytes" as *u8, n) 58 gv_check("T2 subject selftest exit 0" as *u8, (ex == 0) as i64, ctr) 59 var ls: i64 = n 60 if ls > 0 { if cout[ls - 1] == (10 as u8) { ls = ls - 1 } } 61 var lb: i64 = ls 62 var scanning: i64 = 1 63 while scanning == 1 { if lb == 0 { scanning = 0 } else { if cout[lb - 1] == (10 as u8) { scanning = 0 } else { lb = lb - 1 } } } 64 gv_check("T3 the LAST line is the verdict line and reads verdict=GREEN" as *u8, cg_find(((cout as i64) + lb) as *u8, ls - lb, "verdict=GREEN" as *u8), ctr) 65 gv_check("T4 artifact identity, DI13 generation: the forked-guard tooth (T9) and the breached tooth (T10) are present" as *u8, cg_find(cout, n, "T9 a HEAD reader behind a sizeguard reading GREEN is GOVERNED" as *u8) * cg_find(cout, n, "neg-control-T10 a HEAD reader behind a sizeguard reading RED is BREACHED" as *u8), ctr) 66 gv_check("T4b the subject's remedy partition reconciled on its fixture roster (remedy_partition=RECONCILES witnessed)" as *u8, cg_find(cout, n, "remedy_partition=RECONCILES" as *u8), ctr) 67 // the selftest's fixture scan prints a RED verdict line ON PURPOSE (a planted ungoverned ledger), so "no verdict=RED 68 // anywhere" is the wrong control for this subject -- the first NAS run failed exactly that tooth. The control is a 69 // failing tooth: none may be present, and the planted RED must be there (the fixture reached the condition). 70 gv_check("neg-control-no-failing-tooth: subject output carries no ': FAIL' tooth line" as *u8, (cg_find(cout, n, ": FAIL" as *u8) == 0) as i64, ctr) 71 gv_check("T5b the fixture scan reached its RED condition (the planted ungoverned ledger printed verdict=RED)" as *u8, cg_find(cout, n, "over_ungoverned=1" as *u8), ctr) 72 gv_check("T6 non-vacuity: captured output is not empty" as *u8, (n > 0) as i64, ctr) 73 return gv_verdict("nx_capcliff_gate" as *u8, ctr, "end-to-end: the detector's own selftest is the proof; T4 pins the tested bytes to this generation" as *u8) 74}