code wiki / _hdl_build / nx_examiner_arcs.nx

nx_examiner_arcs.nx source

↩ module page · 150 lines · 7729 B

1// nx_examiner_arcs.nx -- the EXAMINER grades TEAMMATES PER-ARC, mechanically (the scorecard's 2// named race-to-exceed for the role, made real). Doctrine: an arc is only as good as its gate's 3// LAST verdict -- so each arc row names its gate log, an anchor line, and the green pattern: 4// GREEN = gated and passing (the arc's owner is delivering, provably) 5// RED = gated and FAILING (the gate caught something -- fix the arc, never the grader) 6// ABSENT = no anchor line found = A CLAIM WITHOUT A GATE (the worst grade; names the gap) 7// Never self-asserts: every grade is read from another role's durable gate output, like 8// nx_examiner_report -- this organ extends it from 5 fixed dims to a DATA-DRIVEN arc table 9// (knowledge/status/examiner_arcs.conf; add an arc = add a row). 10// Durable: EXAMINER-ARC lines + one EXAMINER-ARCS verdict -> knowledge/status/examiner_arcs.log. 11// Exit 0 iff every arc GREEN. Sovereign (syscalls only). license_tier: ORIGINAL 12import "nx_syscalls.nx" 13import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 14const K_MAGIC_65536: i64 = 65536 15const K_MAGIC_65520: i64 = 65520 16const K_MAGIC_16384: i64 = 16384 17const K_MAGIC_1048592: i64 = 1048592 18const K_MAGIC_1048576: i64 = 1048576 19func _p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 20// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 21// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 22// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 23// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 24func _pn(v: i64) -> i64 { nxi_out(v); return 0 } 25func _fp(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 26// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 27// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 28// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 29// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 30func _fn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 } 31func ea_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 32func ea_read_all(path: *u8, buf: *u8, cap: i64) -> i64 { 33 let fd: i64 = sys_openat_rd(path) 34 if fd < 0 { return 0 } 35 var n: i64 = 0 36 var go: i64 = 1 37 while go == 1 { let base: i64 = buf as i64; let r: i64 = sys_read(fd, (base + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } } 38 sys_close(fd) 39 return n 40} 41// does [a,b) of hay contain pat? 42func ea_slice_contains(hay: *u8, a: i64, b: i64, pat: *u8) -> i64 { 43 let pl: i64 = ea_len(pat) 44 if pl == 0 { return 0 } 45 var i: i64 = a 46 while i + pl <= b { 47 var k: i64 = 0 48 var hit: i64 = 1 49 while k < pl { if hay[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } } 50 if hit == 1 { return 1 } 51 i = i + 1 52 } 53 return 0 54} 55// find the LAST line in buf containing pat; writes [start,end) through out[0],out[1]; 1=found 56func ea_last_line_with(hay: *u8, n: i64, pat: *u8, out: *i64) -> i64 { 57 var found: i64 = 0 58 var i: i64 = 0 59 while i < n { 60 var le: i64 = i 61 var scan: i64 = 1 62 while scan == 1 { 63 if le >= n { scan = 0 } 64 else { if hay[le] == (10 as u8) { scan = 0 } else { le = le + 1 } } 65 } 66 if ea_slice_contains(hay, i, le, pat) == 1 { out[0] = i; out[1] = le; found = 1 } 67 i = le + 1 68 } 69 return found 70} 71// copy next space-or-newline-delimited token from conf[pos..le) into pool; returns token end pos 72func ea_token(conf: *u8, pos: i64, le: i64, pool: *u8, poff: *i64) -> i64 { 73 var t: i64 = pos 74 var s: i64 = 1 75 while s == 1 { 76 if t >= le { s = 0 } 77 else { if conf[t] == (32 as u8) { s = 0 } else { pool[poff[0]] = conf[t]; poff[0] = poff[0] + 1; t = t + 1 } } 78 } 79 pool[poff[0]] = 0 as u8; poff[0] = poff[0] + 1 80 return t 81} 82func main() -> i64 { 83 _p("=== EXAMINER ARCS: every arc graded by its gate's LAST verdict (claims need gates) ===\n" as *u8) 84 let conf: *u8 = sys_mmap(K_MAGIC_65536) 85 let cn: i64 = ea_read_all("knowledge/status/examiner_arcs.conf" as *u8, conf, K_MAGIC_65520) 86 if cn <= 0 { _p(" arcs conf MISSING -- fail loud\n" as *u8); sys_exit(1); return 1 } 87 let pool: *u8 = sys_mmap(K_MAGIC_16384) 88 let poffb: *i64 = sys_mmap(16) as *i64 89 poffb[0] = 0 90 let lfd: i64 = sys_openat_append("knowledge/status/examiner_arcs.log" as *u8, 0x1a4) 91 if lfd < 0 { _p(" arcs log open failed\n" as *u8); sys_exit(1); return 1 } 92 let lb: *u8 = sys_mmap(K_MAGIC_1048592) 93 let span: *i64 = sys_mmap(16) as *i64 94 var narcs: i64 = 0 95 var ngreen: i64 = 0 96 var nred: i64 = 0 97 var nabsent: i64 = 0 98 var ci: i64 = 0 99 while ci < cn { 100 var le: i64 = ci 101 var scan: i64 = 1 102 while scan == 1 { 103 if le >= cn { scan = 0 } 104 else { if conf[le] == (10 as u8) { scan = 0 } else { le = le + 1 } } 105 } 106 if conf[ci] == (65 as u8) { 107 // A <arc> <gatelog> <anchor> <green-pattern> 108 let arc: i64 = (pool as i64) + poffb[0] 109 var t: i64 = ea_token(conf, ci + 2, le, pool, poffb) 110 let logp: i64 = (pool as i64) + poffb[0] 111 t = ea_token(conf, t + 1, le, pool, poffb) 112 let anchor: i64 = (pool as i64) + poffb[0] 113 t = ea_token(conf, t + 1, le, pool, poffb) 114 let green: i64 = (pool as i64) + poffb[0] 115 t = ea_token(conf, t + 1, le, pool, poffb) 116 narcs = narcs + 1 117 let ln: i64 = ea_read_all(logp as *u8, lb, K_MAGIC_1048576) 118 var grade: i64 = 2 // 0 GREEN / 1 RED / 2 ABSENT 119 if ln > 0 { 120 if ea_last_line_with(lb, ln, anchor as *u8, span) == 1 { 121 if ea_slice_contains(lb, span[0], span[1], green as *u8) == 1 { grade = 0 } else { grade = 1 } 122 } 123 } 124 _p(" " as *u8); _p(arc as *u8); _p(": " as *u8) 125 if grade == 0 { _p("GREEN\n" as *u8); ngreen = ngreen + 1 } 126 if grade == 1 { _p("RED (gate failing -- fix the arc)\n" as *u8); nred = nred + 1 } 127 if grade == 2 { _p("ABSENT (claim without a gate!)\n" as *u8); nabsent = nabsent + 1 } 128 _fp(lfd, "EXAMINER-ARC arc=" as *u8); _fp(lfd, arc as *u8) 129 _fp(lfd, " grade=" as *u8) 130 if grade == 0 { _fp(lfd, "GREEN" as *u8) } 131 if grade == 1 { _fp(lfd, "RED" as *u8) } 132 if grade == 2 { _fp(lfd, "ABSENT" as *u8) } 133 _fp(lfd, "\n" as *u8) 134 } 135 ci = le + 1 136 } 137 _fp(lfd, "EXAMINER-ARCS epoch=" as *u8); _fn(lfd, sys_now_realtime_sec()) 138 _fp(lfd, " arcs=" as *u8); _fn(lfd, narcs) 139 _fp(lfd, " green=" as *u8); _fn(lfd, ngreen) 140 _fp(lfd, " red=" as *u8); _fn(lfd, nred) 141 _fp(lfd, " absent=" as *u8); _fn(lfd, nabsent) 142 if ngreen == narcs { _fp(lfd, " verdict=ALL-ARCS-GATED-GREEN\n" as *u8) } else { _fp(lfd, " verdict=ATTENTION\n" as *u8) } 143 sys_close(lfd) 144 _p(" --- ARCS: " as *u8); _pn(ngreen); _p("/" as *u8); _pn(narcs); _p(" gated-green (red=" as *u8); _pn(nred); _p(" absent=" as *u8); _pn(nabsent); _p(") ---\n" as *u8) 145 _p(" durable: knowledge/status/examiner_arcs.log\n" as *u8) 146 if ngreen == narcs { _p(" EXAMINER ARCS: ALL-ARCS-GATED-GREEN\n" as *u8); sys_exit(0); return 0 } 147 _p(" EXAMINER ARCS: ATTENTION (a red or ungated arc -- the named owner has work)\n" as *u8) 148 sys_exit(1) 149 return 1 150}