code wiki / _hdl_build / nx_collision_watchdog_gate.nx

nx_collision_watchdog_gate.nx source

↩ module page · 271 lines · 13811 B

1// nx_collision_watchdog_gate.nx -- WMS-M3 REFEREE: proves the collision/regression/floater WATCHDOG 2// raises all three failure classes correctly AND routes a known issue NAMED via recall, with the 3// MANDATORY negative controls (clean log + grounded ladder + novel input -> no false alarm / honest 4// UNKNOWN) and tampers (proving detection is real, not constant). 5// 6// NO-FALSE-GREEN discipline (the law): 7// POSITIVE (must raise): 8// T1 torn-detect : an injected torn buffer (two heads on one line) -> cw_count_torn >= 1 9// T2 floating-detect : a synthetic lineage with node F parents "-" (not the spore) -> floating==1 10// T3 red-detect : a buffer with a "verdict=RED" line -> cw_count_red >= 1 11// T4 known-issue ROUTE: seed a hermetic catalogue with a WD-SEGV / SIGSEGV signature; feed a 12// diagnostic embedding SIGSEGV -> cw_route == KI_HIT AND outid == "WD-SEGV" 13// (NAMED, not UNKNOWN -- proves recall + routing are wired in). 14// NEGATIVE CONTROL (must NOT false-alarm / honest UNKNOWN): 15// T5 clean-log : a clean buffer (every line one head+one tail, no verdict=RED) -> 16// cw_count_torn==0 AND cw_count_red==0 (detector is not constant-positive) 17// T6 grounded-ladder : synthetic lineage where every node traces to the spore -> floating==0 18// T7 novel-input : a diagnostic with NO catalogued signature -> cw_route==KI_UNKNOWN, 19// outid filled "UNKNOWN" (genuinely novel stays legal -- no rubber-stamp) 20// TAMPER (proves matches are real, not constants): 21// T8 torn-tamper : repair the injected torn line (one head/one tail) -> cw_count_torn==0 22// T9 recall-tamper : corrupt the signature byte (SIGSEXV) -> cw_route==KI_UNKNOWN 23// 24// GREEN iff passes==9. Evidence -> knowledge/status/collision_watchdog.log via the locked atomic 25// fa_appendz (the gate eats its own dogfood) AND a UNIQUE scratch path (epoch+pid) so concurrent 26// self-runs never collide. Flat pass-tally (avoids the LM-002 deep-nest landmine). exit 0 GREEN / 1 RED. 27// 28// HERMETIC: every fixture is an in-memory buffer or a hermetic store prefix (knowledge/store/kiwdgate-) 29// seeded idempotently -- the gate never asserts on production state. Sovereign: only the named nx 30// organs + raw syscalls. license_tier: ORIGINAL 31import "nx_collision_watchdog.nx" // cw_count_torn / cw_count_red / cw_count_floating / cw_route / cw_emit_alert 32import "nx_seg_store.nx" // ss_begin / ss_add / ss_commit (hermetic catalogue seed) 33import "nx_syscalls.nx" 34 35const CWG_LOG: *u8 = "knowledge/status/collision_watchdog.log" 36const KIWD_PREFIX: *u8 = "knowledge/store/kiwdgate-" 37 38func g_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 39func g_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 40func g_streq(a: *u8, b: *u8) -> i64 { 41 var i: i64 = 0 42 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 43 if b[i] != (0 as u8) { return 0 } 44 return 1 45} 46func g_row(name: *u8, pass: i64) -> i64 { 47 g_w(" \x00" as *u8); g_w(name) 48 if pass == 1 { g_w(" PASS\n\x00" as *u8) } else { g_w(" FAIL\n\x00" as *u8) } 49 return 0 50} 51 52// idempotent hermetic-store value equality (mirrors rr_streq_store). 53func g_streq_store(key: *u8, val: *u8) -> i64 { 54 let pq: *i64 = sys_mmap(16) as *i64 55 let lq: *i64 = sys_mmap(16) as *i64 56 if ss_get(KIWD_PREFIX, key, pq, lq) != 1 { return 0 } 57 let b: *u8 = pq[0] as *u8 58 let n: i64 = lq[0] 59 let vl: i64 = g_len(val) 60 if n != vl { return 0 } 61 var i: i64 = 0 62 while i < n { if b[i] != val[i] { return 0 } i = i + 1 } 63 return 1 64} 65 66// build a fixed-shape lineage node (id + parents) into the synthetic in-memory tree. 67func g_node(idbase: i64, parbase: i64, r: i64, id: *u8, par: *u8) -> i64 { 68 rt_set(idbase, RT_IDCAP, r, id) 69 rt_set(parbase, RT_PARCAP, r, par) 70 return 0 71} 72 73func main() -> i64 { 74 // --------------------------------------------------------------- 75 // seed the hermetic known-issue catalogue (idempotent / additive) 76 // --------------------------------------------------------------- 77 let keys: *i64 = sys_mmap(8 * 8) as *i64 78 let vals: *i64 = sys_mmap(8 * 8) as *i64 79 keys[0] = "ki:ids" as *u8 as i64 80 vals[0] = "WD-SEGV\tWD-TORN" as *u8 as i64 81 keys[1] = "ki:WD-SEGV" as *u8 as i64 82 vals[1] = "WD-SEGV\tTOOLCHAIN\tOPEN\tSIGSEGV\tnxasm-prologue-encoding-fix" as *u8 as i64 83 keys[2] = "ki:WD-TORN" as *u8 as i64 84 vals[2] = "WD-TORN\tENV\tDISCIPLINE\ttorn-line\tuse-fa_appendz-locked-write" as *u8 as i64 85 let nrec: i64 = 3 86 let w: *i64 = ss_begin() 87 var towrite: i64 = 0 88 var si: i64 = 0 89 while si < nrec { 90 if g_streq_store(keys[si] as *u8, vals[si] as *u8) == 0 { ss_add(w, 1, keys[si] as *u8, vals[si] as *u8, g_len(vals[si] as *u8)); towrite = towrite + 1 } 91 si = si + 1 92 } 93 if towrite > 0 { ss_commit(KIWD_PREFIX, w, ki_seg_next_p(KIWD_PREFIX)) } 94 95 let outid: *u8 = sys_mmap(64) 96 let outrem: *u8 = sys_mmap(256) 97 let outstat: *u8 = sys_mmap(64) 98 99 // shared head/tail for the torn fixtures (the NOTES frame shape). 100 let head: *u8 = "NOTES epoch=\x00" as *u8 101 let tail: *u8 = "DRILL-CALLED\x00" as *u8 102 103 // --------------------------------------------------------------- 104 // T1 torn-detect (POS): a buffer whose ONE line carries TWO heads (the interleave shape). 105 // "NOTES epoch=1 NOTES epoch=2 ...DRILL-CALLED\n" -> two heads, one tail -> torn. 106 // --------------------------------------------------------------- 107 let tornbuf: *u8 = sys_mmap(256) 108 var to: i64 = 0 109 to = fa_cat(tornbuf, to, "NOTES epoch=1 weakest_arc=A NOTES epoch=2 weakest_arc=B verdict=DRILL-CALLED\n\x00" as *u8) 110 let t1c: i64 = cw_count_torn(tornbuf, to, head, tail) 111 var t1: i64 = 0 112 if t1c >= 1 { t1 = 1 } 113 114 // --------------------------------------------------------------- 115 // T2 floating-detect (POS): synthetic tree spore S, child C<-S, plus FLOATER F parents "-" 116 // (NOT the spore) -> exactly one floating capability. 117 // --------------------------------------------------------------- 118 let fib: i64 = sys_mmap(8 * RT_IDCAP) as i64 119 let fpb: i64 = sys_mmap(8 * RT_PARCAP) as i64 120 let ftr: *i64 = sys_mmap(8 * 8) as *i64 121 g_node(fib, fpb, 0, "S" as *u8, "-" as *u8) // the spore (legal "-" root) 122 g_node(fib, fpb, 1, "C" as *u8, "S" as *u8) // grounded child 123 g_node(fib, fpb, 2, "F" as *u8, "-" as *u8) // FLOATER: "-" but not the spore 124 let t2c: i64 = cw_count_floating(fib, fpb, 3, "S" as *u8, ftr) 125 var t2: i64 = 0 126 if t2c == 1 { t2 = 1 } 127 128 // --------------------------------------------------------------- 129 // T3 red-detect (POS): a buffer with a verdict=RED line. 130 // --------------------------------------------------------------- 131 let redbuf: *u8 = sys_mmap(256) 132 var ro: i64 = 0 133 ro = fa_cat(redbuf, ro, "SOMEGATE step=ok verdict=GREEN\nSOMEGATE step=bad verdict=RED reason=x\n\x00" as *u8) 134 let lr: *u8 = sys_mmap(512) 135 let t3c: i64 = cw_count_red(redbuf, ro, lr) 136 var t3: i64 = 0 137 if t3c >= 1 { t3 = 1 } 138 139 // --------------------------------------------------------------- 140 // T4 known-issue ROUTED (POS): a diagnostic embedding SIGSEGV -> HIT, id=WD-SEGV (NAMED). 141 // --------------------------------------------------------------- 142 let d_segv: *u8 = "watchdog: gate child terminated by signal 11 SIGSEGV at canary\n\x00" as *u8 143 let r4: i64 = cw_route(KIWD_PREFIX, d_segv, g_len(d_segv), outid, outrem, outstat) 144 var t4: i64 = 0 145 if r4 == KI_HIT { if g_streq(outid, "WD-SEGV" as *u8) == 1 { t4 = 1 } } 146 147 // --------------------------------------------------------------- 148 // T5 clean-log (NEG CONTROL): every line one head+one tail, NO verdict=RED -> torn==0 AND red==0. 149 // --------------------------------------------------------------- 150 let clean: *u8 = sys_mmap(512) 151 var co: i64 = 0 152 co = fa_cat(clean, co, "NOTES epoch=1 weakest_arc=A verdict=DRILL-CALLED\n\x00" as *u8) 153 co = fa_cat(clean, co, "NOTES epoch=2 weakest_arc=B verdict=DRILL-CALLED\n\x00" as *u8) 154 let t5torn: i64 = cw_count_torn(clean, co, head, tail) 155 let lr5: *u8 = sys_mmap(512) 156 let t5red: i64 = cw_count_red(clean, co, lr5) 157 var t5: i64 = 0 158 if t5torn == 0 { if t5red == 0 { t5 = 1 } } 159 160 // --------------------------------------------------------------- 161 // T6 grounded-ladder (NEG CONTROL): every node traces to the spore -> floating==0. 162 // --------------------------------------------------------------- 163 let gib: i64 = sys_mmap(8 * RT_IDCAP) as i64 164 let gpb: i64 = sys_mmap(8 * RT_PARCAP) as i64 165 let gtr: *i64 = sys_mmap(8 * 8) as *i64 166 g_node(gib, gpb, 0, "S" as *u8, "-" as *u8) // spore 167 g_node(gib, gpb, 1, "C" as *u8, "S" as *u8) // child of spore 168 g_node(gib, gpb, 2, "D" as *u8, "C" as *u8) // grandchild -- all rooted 169 let t6c: i64 = cw_count_floating(gib, gpb, 3, "S" as *u8, gtr) 170 var t6: i64 = 0 171 if t6c == 0 { t6 = 1 } 172 173 // --------------------------------------------------------------- 174 // T7 novel-input (NEG CONTROL / honest UNKNOWN): no catalogued signature -> UNKNOWN, outid="UNKNOWN". 175 // --------------------------------------------------------------- 176 let d_novel: *u8 = "brand new failure zzqq-unseen-novel-9 nothing in the catalogue at all\n\x00" as *u8 177 let r7: i64 = cw_route(KIWD_PREFIX, d_novel, g_len(d_novel), outid, outrem, outstat) 178 var t7: i64 = 0 179 if r7 == KI_UNKNOWN { if g_streq(outid, "UNKNOWN" as *u8) == 1 { t7 = 1 } } 180 181 // --------------------------------------------------------------- 182 // T8 torn-tamper: REPAIR the torn line (one head, one tail) -> cw_count_torn==0. 183 // Proves T1 detected REAL framing, not a constant >=1. 184 // --------------------------------------------------------------- 185 let fixbuf: *u8 = sys_mmap(256) 186 var fo: i64 = 0 187 fo = fa_cat(fixbuf, fo, "NOTES epoch=1 weakest_arc=A verdict=DRILL-CALLED\n\x00" as *u8) 188 let t8c: i64 = cw_count_torn(fixbuf, fo, head, tail) 189 var t8: i64 = 0 190 if t8c == 0 { t8 = 1 } 191 192 // --------------------------------------------------------------- 193 // T9 recall-tamper: corrupt the signature byte (SIGSEXV) -> route MISSES (UNKNOWN). 194 // Proves the substring match is real, not a constant TRUE. 195 // --------------------------------------------------------------- 196 let d_tamper: *u8 = "watchdog: gate child terminated by signal 11 SIGSEXV at canary\n\x00" as *u8 197 let r9: i64 = cw_route(KIWD_PREFIX, d_tamper, g_len(d_tamper), outid, outrem, outstat) 198 var t9: i64 = 0 199 if r9 == KI_UNKNOWN { t9 = 1 } 200 201 // --------------------------------------------------------------- 202 // flat pass-tally (LM-002 safe) 203 // --------------------------------------------------------------- 204 var passes: i64 = 0 205 if t1 == 1 { passes = passes + 1 } 206 if t2 == 1 { passes = passes + 1 } 207 if t3 == 1 { passes = passes + 1 } 208 if t4 == 1 { passes = passes + 1 } 209 if t5 == 1 { passes = passes + 1 } 210 if t6 == 1 { passes = passes + 1 } 211 if t7 == 1 { passes = passes + 1 } 212 if t8 == 1 { passes = passes + 1 } 213 if t9 == 1 { passes = passes + 1 } 214 var ok: i64 = 0 215 if passes == 9 { ok = 1 } 216 217 // ---- stdout report ---- 218 g_w("COLLISION-WATCHDOG gate (collision / regression / floater + recall routing)\n\x00" as *u8) 219 g_row("T1 torn-detect-POS \x00" as *u8, t1) 220 g_row("T2 floating-detect-POS \x00" as *u8, t2) 221 g_row("T3 red-detect-POS \x00" as *u8, t3) 222 g_row("T4 known-issue-ROUTED \x00" as *u8, t4) 223 g_row("T5 clean-log-NEG \x00" as *u8, t5) 224 g_row("T6 grounded-ladder-NEG \x00" as *u8, t6) 225 g_row("T7 novel-input-UNKNOWN \x00" as *u8, t7) 226 g_row("T8 torn-tamper \x00" as *u8, t8) 227 g_row("T9 recall-tamper \x00" as *u8, t9) 228 if ok == 1 { g_w("verdict=GREEN\n\x00" as *u8) } else { g_w("verdict=RED\n\x00" as *u8) } 229 230 // ---- evidence: ONE locked atomic fa_appendz record (eat our own dogfood) ---- 231 let epoch: i64 = sys_now_realtime_sec() 232 let pid: i64 = __syscall(39, 0, 0, 0, 0, 0, 0) 233 let rec: *u8 = sys_mmap(CW_REC_CAP + 16) 234 var o: i64 = 0 235 o = fa_cat(rec, o, "WATCHDOG-GATE authored=organ epoch=\x00" as *u8) 236 o = fa_catn(rec, o, epoch) 237 o = fa_cat(rec, o, " pid=\x00" as *u8) 238 o = fa_catn(rec, o, pid) 239 o = fa_cat(rec, o, " passes=\x00" as *u8) 240 o = fa_catn(rec, o, passes) 241 o = fa_cat(rec, o, "/9 t1torn=\x00" as *u8) 242 o = fa_catn(rec, o, t1c) 243 o = fa_cat(rec, o, " t2float=\x00" as *u8) 244 o = fa_catn(rec, o, t2c) 245 o = fa_cat(rec, o, " t3red=\x00" as *u8) 246 o = fa_catn(rec, o, t3c) 247 o = fa_cat(rec, o, " t4id=\x00" as *u8) 248 if t4 == 1 { o = fa_cat(rec, o, "WD-SEGV\x00" as *u8) } else { o = fa_cat(rec, o, outid) } 249 o = fa_cat(rec, o, " t8tamper_torn=\x00" as *u8) 250 o = fa_catn(rec, o, t8c) 251 o = fa_cat(rec, o, " verdict=\x00" as *u8) 252 if ok == 1 { o = fa_cat(rec, o, "GREEN\x00" as *u8) } else { o = fa_cat(rec, o, "RED\x00" as *u8) } 253 rec[o] = 0 as u8 254 255 // (a) the canonical evidence channel 256 fa_appendz(CWG_LOG, rec, CW_REC_CAP) 257 258 // (b) a UNIQUE scratch path (epoch+pid) so concurrent self-runs never collide -- dogfood. 259 let scratch: *u8 = sys_mmap(256) 260 var so: i64 = 0 261 so = fa_cat(scratch, so, "/tmp/cw_gate_\x00" as *u8) 262 so = fa_catn(scratch, so, epoch) 263 so = fa_cat(scratch, so, "_\x00" as *u8) 264 so = fa_catn(scratch, so, pid) 265 so = fa_cat(scratch, so, ".log\x00" as *u8) 266 scratch[so] = 0 as u8 267 fa_appendz(scratch, rec, CW_REC_CAP) 268 269 if ok == 1 { return 0 } 270 return 1 271}