code wiki / _hdl_build / nx_collision_watchdog_gate.nx
nx_collision_watchdog_gate.nx source
↩ module page · 238 lines · 12613 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"
34import "nx_gate_verdict.nx"
35
36const CWG_LOG: *u8 = "knowledge/status/collision_watchdog.log"
37const KIWD_PREFIX: *u8 = "knowledge/store/kiwdgate-"
38
39func 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 }
40func g_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
41func g_streq(a: *u8, b: *u8) -> i64 {
42 var i: i64 = 0
43 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 }
44 if b[i] != (0 as u8) { return 0 }
45 return 1
46}
47func g_row(name: *u8, pass: i64) -> i64 {
48 g_w(" \x00" as *u8); g_w(name)
49 if pass == 1 { g_w(" PASS\n\x00" as *u8) } else { g_w(" FAIL\n\x00" as *u8) }
50 return 0
51}
52
53// idempotent hermetic-store value equality (mirrors rr_streq_store).
54func g_streq_store(key: *u8, val: *u8) -> i64 {
55 let pq: *i64 = sys_mmap(16) as *i64
56 let lq: *i64 = sys_mmap(16) as *i64
57 if ss_get(KIWD_PREFIX, key, pq, lq) != 1 { return 0 }
58 let b: *u8 = pq[0] as *u8
59 let n: i64 = lq[0]
60 let vl: i64 = g_len(val)
61 if n != vl { return 0 }
62 var i: i64 = 0
63 while i < n { if b[i] != val[i] { return 0 } i = i + 1 }
64 return 1
65}
66
67// build a fixed-shape lineage node (id + parents) into the synthetic in-memory tree.
68func g_node(idbase: i64, parbase: i64, r: i64, id: *u8, par: *u8) -> i64 {
69 rt_set(idbase, RT_IDCAP, r, id)
70 rt_set(parbase, RT_PARCAP, r, par)
71 return 0
72}
73
74func main() -> i64 {
75 // ---------------------------------------------------------------
76 // seed the hermetic known-issue catalogue (idempotent / additive)
77 // ---------------------------------------------------------------
78 let keys: *i64 = sys_mmap(8 * 8) as *i64
79 let vals: *i64 = sys_mmap(8 * 8) as *i64
80 keys[0] = "ki:ids" as *u8 as i64
81 vals[0] = "WD-SEGV\tWD-TORN" as *u8 as i64
82 keys[1] = "ki:WD-SEGV" as *u8 as i64
83 vals[1] = "WD-SEGV\tTOOLCHAIN\tOPEN\tSIGSEGV\tnxasm-prologue-encoding-fix" as *u8 as i64
84 keys[2] = "ki:WD-TORN" as *u8 as i64
85 vals[2] = "WD-TORN\tENV\tDISCIPLINE\ttorn-line\tuse-fa_appendz-locked-write" as *u8 as i64
86 let nrec: i64 = 3
87 let w: *i64 = ss_begin()
88 var towrite: i64 = 0
89 var si: i64 = 0
90 while si < nrec {
91 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 }
92 si = si + 1
93 }
94 if towrite > 0 { ss_commit(KIWD_PREFIX, w, ki_seg_next_p(KIWD_PREFIX)) }
95
96 let outid: *u8 = sys_mmap(64)
97 let outrem: *u8 = sys_mmap(256)
98 let outstat: *u8 = sys_mmap(64)
99
100 // shared head/tail for the torn fixtures (the NOTES frame shape).
101 let head: *u8 = "NOTES epoch=\x00" as *u8
102 let tail: *u8 = "DRILL-CALLED\x00" as *u8
103
104 // ---------------------------------------------------------------
105 // T1 torn-detect (POS): a buffer whose ONE line carries TWO heads (the interleave shape).
106 // "NOTES epoch=1 NOTES epoch=2 ...DRILL-CALLED\n" -> two heads, one tail -> torn.
107 // ---------------------------------------------------------------
108 let tornbuf: *u8 = sys_mmap(256)
109 var to: i64 = 0
110 to = fa_cat(tornbuf, to, "NOTES epoch=1 weakest_arc=A NOTES epoch=2 weakest_arc=B verdict=DRILL-CALLED\n\x00" as *u8)
111 let t1c: i64 = cw_count_torn(tornbuf, to, head, tail)
112 var t1: i64 = 0
113 if t1c >= 1 { t1 = 1 }
114
115 // ---------------------------------------------------------------
116 // T2 floating-detect (POS): synthetic tree spore S, child C<-S, plus FLOATER F parents "-"
117 // (NOT the spore) -> exactly one floating capability.
118 // ---------------------------------------------------------------
119 let fib: i64 = sys_mmap(8 * RT_IDCAP) as i64
120 let fpb: i64 = sys_mmap(8 * RT_PARCAP) as i64
121 let ftr: *i64 = sys_mmap(8 * 8) as *i64
122 g_node(fib, fpb, 0, "S" as *u8, "-" as *u8) // the spore (legal "-" root)
123 g_node(fib, fpb, 1, "C" as *u8, "S" as *u8) // grounded child
124 g_node(fib, fpb, 2, "F" as *u8, "-" as *u8) // FLOATER: "-" but not the spore
125 let t2c: i64 = cw_count_floating(fib, fpb, 3, "S" as *u8, ftr)
126 var t2: i64 = 0
127 if t2c == 1 { t2 = 1 }
128
129 // ---------------------------------------------------------------
130 // T3 red-detect (POS): a buffer with a verdict=RED line.
131 // ---------------------------------------------------------------
132 let redbuf: *u8 = sys_mmap(256)
133 var ro: i64 = 0
134 ro = fa_cat(redbuf, ro, "SOMEGATE step=ok verdict=GREEN\nSOMEGATE step=bad verdict=RED reason=x\n\x00" as *u8)
135 let lr: *u8 = sys_mmap(512)
136 let t3c: i64 = cw_count_red(redbuf, ro, lr)
137 var t3: i64 = 0
138 if t3c >= 1 { t3 = 1 }
139
140 // ---------------------------------------------------------------
141 // T4 known-issue ROUTED (POS): a diagnostic embedding SIGSEGV -> HIT, id=WD-SEGV (NAMED).
142 // ---------------------------------------------------------------
143 let d_segv: *u8 = "watchdog: gate child terminated by signal 11 SIGSEGV at canary\n\x00" as *u8
144 let r4: i64 = cw_route(KIWD_PREFIX, d_segv, g_len(d_segv), outid, outrem, outstat)
145 var t4: i64 = 0
146 if r4 == KI_HIT { if g_streq(outid, "WD-SEGV" as *u8) == 1 { t4 = 1 } }
147
148 // ---------------------------------------------------------------
149 // T5 clean-log (NEG CONTROL): every line one head+one tail, NO verdict=RED -> torn==0 AND red==0.
150 // ---------------------------------------------------------------
151 let clean: *u8 = sys_mmap(512)
152 var co: i64 = 0
153 co = fa_cat(clean, co, "NOTES epoch=1 weakest_arc=A verdict=DRILL-CALLED\n\x00" as *u8)
154 co = fa_cat(clean, co, "NOTES epoch=2 weakest_arc=B verdict=DRILL-CALLED\n\x00" as *u8)
155 let t5torn: i64 = cw_count_torn(clean, co, head, tail)
156 let lr5: *u8 = sys_mmap(512)
157 let t5red: i64 = cw_count_red(clean, co, lr5)
158 var t5: i64 = 0
159 if t5torn == 0 { if t5red == 0 { t5 = 1 } }
160
161 // ---------------------------------------------------------------
162 // T6 grounded-ladder (NEG CONTROL): every node traces to the spore -> floating==0.
163 // ---------------------------------------------------------------
164 let gib: i64 = sys_mmap(8 * RT_IDCAP) as i64
165 let gpb: i64 = sys_mmap(8 * RT_PARCAP) as i64
166 let gtr: *i64 = sys_mmap(8 * 8) as *i64
167 g_node(gib, gpb, 0, "S" as *u8, "-" as *u8) // spore
168 g_node(gib, gpb, 1, "C" as *u8, "S" as *u8) // child of spore
169 g_node(gib, gpb, 2, "D" as *u8, "C" as *u8) // grandchild -- all rooted
170 let t6c: i64 = cw_count_floating(gib, gpb, 3, "S" as *u8, gtr)
171 var t6: i64 = 0
172 if t6c == 0 { t6 = 1 }
173
174 // ---------------------------------------------------------------
175 // T7 novel-input (NEG CONTROL / honest UNKNOWN): no catalogued signature -> UNKNOWN, outid="UNKNOWN".
176 // ---------------------------------------------------------------
177 let d_novel: *u8 = "brand new failure zzqq-unseen-novel-9 nothing in the catalogue at all\n\x00" as *u8
178 let r7: i64 = cw_route(KIWD_PREFIX, d_novel, g_len(d_novel), outid, outrem, outstat)
179 var t7: i64 = 0
180 if r7 == KI_UNKNOWN { if g_streq(outid, "UNKNOWN" as *u8) == 1 { t7 = 1 } }
181
182 // ---------------------------------------------------------------
183 // T8 torn-tamper: REPAIR the torn line (one head, one tail) -> cw_count_torn==0.
184 // Proves T1 detected REAL framing, not a constant >=1.
185 // ---------------------------------------------------------------
186 let fixbuf: *u8 = sys_mmap(256)
187 var fo: i64 = 0
188 fo = fa_cat(fixbuf, fo, "NOTES epoch=1 weakest_arc=A verdict=DRILL-CALLED\n\x00" as *u8)
189 let t8c: i64 = cw_count_torn(fixbuf, fo, head, tail)
190 var t8: i64 = 0
191 if t8c == 0 { t8 = 1 }
192
193 // ---------------------------------------------------------------
194 // T9 recall-tamper: corrupt the signature byte (SIGSEXV) -> route MISSES (UNKNOWN).
195 // Proves the substring match is real, not a constant TRUE.
196 // ---------------------------------------------------------------
197 let d_tamper: *u8 = "watchdog: gate child terminated by signal 11 SIGSEXV at canary\n\x00" as *u8
198 let r9: i64 = cw_route(KIWD_PREFIX, d_tamper, g_len(d_tamper), outid, outrem, outstat)
199 var t9: i64 = 0
200 if r9 == KI_UNKNOWN { t9 = 1 }
201
202 // ---------------------------------------------------------------
203 // flat pass-tally (LM-002 safe)
204 // ---------------------------------------------------------------
205 var passes: i64 = 0
206 if t1 == 1 { passes = passes + 1 }
207 if t2 == 1 { passes = passes + 1 }
208 if t3 == 1 { passes = passes + 1 }
209 if t4 == 1 { passes = passes + 1 }
210 if t5 == 1 { passes = passes + 1 }
211 if t6 == 1 { passes = passes + 1 }
212 if t7 == 1 { passes = passes + 1 }
213 if t8 == 1 { passes = passes + 1 }
214 if t9 == 1 { passes = passes + 1 }
215 var ok: i64 = 0
216 if passes == 9 { ok = 1 }
217
218 // ---- stdout report ----
219 g_w("COLLISION-WATCHDOG gate (collision / regression / floater + recall routing)\n\x00" as *u8)
220 g_row("T1 torn-detect-POS \x00" as *u8, t1)
221 g_row("T2 floating-detect-POS \x00" as *u8, t2)
222 g_row("T3 red-detect-POS \x00" as *u8, t3)
223 g_row("T4 known-issue-ROUTED \x00" as *u8, t4)
224 g_row("T5 clean-log-NEG \x00" as *u8, t5)
225 g_row("T6 grounded-ladder-NEG \x00" as *u8, t6)
226 g_row("T7 novel-input-UNKNOWN \x00" as *u8, t7)
227 g_row("T8 torn-tamper \x00" as *u8, t8)
228 g_row("T9 recall-tamper \x00" as *u8, t9)
229 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
230 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
231 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
232 let ctr__dry: *i64 = gv_ctr()
233 ctr__dry[0] = ok
234 ctr__dry[1] = 1
235 let rc__dry: i64 = gv_verdict("COLLISION-WATCHDOG-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
236 sys_exit(rc__dry)
237 return rc__dry
238}