code wiki / _hdl_build / nx_ims_monitor_gate.nx

nx_ims_monitor_gate.nx source

↩ module page · 241 lines · 13177 B

1// nx_ims_monitor_gate.nx -- REFEREE for the IMS A1 orphan + link-rot monitor. 2// 3// Proves the PURE logic with in-memory fixtures (no I/O) AND with TEETH -- a 4// monitor that always says "0 orphans / 0 dead" would pass a clean fixture, so 5// every clean assertion is paired with a PLANTED-fault assertion that FORCES a 6// specific non-zero finding at a known identity: 7// 8// T1 CLEAN corpus (every non-root page linked, no dead links) 9// -> orphans == 0 AND dead == 0 10// T2 PLANTED ORPHAN (page "lonely" that NOBODY links) 11// -> orphans == 1 AND the orphan is exactly rowid(lonely) [TEETH] 12// T3 PLANTED DEAD LINK (a page hrefs "/wiki/ghost.html", no such page) 13// -> dead == 1 AND the dead target is exactly "ghost" [TEETH] 14// T4 NEG-CONTROL: the root (start) is linked by NOBODY but is NOT flagged 15// -> start NOT in orphans, while a real orphan IS (proves the root 16// exclusion is real, not "flag nothing") [TEETH] 17// T5 NEG-CONTROL: the monitor does NOT touch the corpus -- a self-link does 18// not rescue an orphan, and a present target is NOT a dead link. 19// 20// GREEN iff T1..T5 with EXACT counts + identities. exit 0 GREEN / 1 RED. Logs to 21// knowledge/status/ims_monitor_gate.log. Sovereign: nx_ims_monitor (which 22// composes nx_wiki_index_builder + nx_wiki_backlinks) + nx_syscalls. 23// license_tier: ORIGINAL 24import "nx_ims_monitor.nx" 25import "nx_syscalls.nx" 26 27// ---- dual-sink (stdout + log) print helpers (nx_blob_gate idiom) ---- 28func gp(logfd: i64, s: *u8) -> i64 { 29 var n: i64 = 0 30 while s[n] != (0 as u8) { n = n + 1 } 31 sys_write(1, s, n) 32 if logfd > 0 { sys_write(logfd, s, n) } 33 return 0 34} 35func gn(logfd: i64, v: i64) -> i64 { 36 var m: i64 = v 37 if m < 0 { gp(logfd, "-\x00" as *u8); m = 0 - m } 38 let t: *u8 = sys_mmap(28) 39 var k: i64 = 0 40 if m == 0 { t[0] = 48 as u8; k = 1 } 41 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 42 let bb: *u8 = sys_mmap(28) 43 var i: i64 = 0 44 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 45 sys_write(1, bb, k) 46 if logfd > 0 { sys_write(logfd, bb, k) } 47 return 0 48} 49// emit a (ptr,len) raw byte run to both sinks 50func gb(logfd: i64, p: *u8, n: i64) -> i64 { 51 var i: i64 = 0 52 while i < n { 53 if i >= 4096 { i = n } 54 if i < n { 55 sys_write(1, (p as i64 + i) as *u8, 1) 56 if logfd > 0 { sys_write(logfd, (p as i64 + i) as *u8, 1) } 57 } 58 i = i + 1 59 } 60 return 0 61} 62func grow(logfd: i64, name: *u8, ok: i64) -> i64 { 63 if ok == 1 { gp(logfd, " PASS \x00" as *u8) } 64 if ok != 1 { gp(logfd, " FAIL \x00" as *u8) } 65 gp(logfd, name) 66 gp(logfd, "\n\x00" as *u8) 67 return ok 68} 69 70func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 71 72// add a page (title==url slug for brevity) to the store; body supplied raw. 73func add_page(st: *NxWikiDocStore, url: *u8, body: *u8) -> i64 { 74 return nx_wiki_doc_store_add(st, url, slen(url), url, slen(url), body, slen(body)) 75} 76 77func main() -> i64 { 78 let logfd: i64 = sys_openat_append("knowledge/status/ims_monitor_gate.log\x00" as *u8, 0x1a4) 79 gp(logfd, "IMS-MONITOR GATE (A1) orphan + link-rot, in-memory fixtures + planted-fault TEETH\n\x00" as *u8) 80 81 var pass: i64 = 0 82 83 // =================================================================== 84 // T1: CLEAN corpus. Pages: start(root), alpha, beta, gamma. 85 // start -> [[alpha]] [[beta]] alpha -> [[gamma]] beta -> [[gamma]] 86 // gamma -> [[alpha]] (so alpha,beta,gamma ALL have >=1 inbound; start is 87 // the root, linked by nobody, but excluded). NO dead links. 88 // =================================================================== 89 let s1: *NxWikiDocStore = sys_mmap(512) as *NxWikiDocStore 90 nx_wiki_doc_store_init(s1, 16, 4096, 4096, 65536) 91 add_page(s1, "/wiki/start.html\x00" as *u8, "Front door. See [[alpha]] and [[beta]].\x00" as *u8) 92 add_page(s1, "/wiki/alpha.html\x00" as *u8, "Alpha leads to [[gamma]].\x00" as *u8) 93 add_page(s1, "/wiki/beta.html\x00" as *u8, "Beta also leads to [[gamma]].\x00" as *u8) 94 add_page(s1, "/wiki/gamma.html\x00" as *u8, "Gamma circles back to [[alpha]].\x00" as *u8) 95 96 // resolve root rowid (start) 97 let roots: *i64 = sys_mmap(NX_IMS_MAX_ROOTS * 8) as *i64 98 roots[0] = nx_ims_resolve_root(s1, "start\x00" as *u8, 5) 99 gp(logfd, "T1 root(start) rowid=\x00" as *u8); gn(logfd, roots[0]); gp(logfd, "\n\x00" as *u8) 100 101 let orph: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 102 let oc: *i64 = sys_mmap(8) as *i64 103 nx_ims_orphans(s1, roots, 1, orph, NX_IMS_MAX_PAGES, oc) 104 let dsrc: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 105 let doff: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 106 let dlen: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 107 let dc1: *i64 = sys_mmap(8) as *i64 108 nx_ims_dead_links(s1, dsrc, doff, dlen, NX_IMS_MAX_PAGES, dc1) 109 gp(logfd, "T1 CLEAN orphans=\x00" as *u8); gn(logfd, oc[0]); gp(logfd, " dead=\x00" as *u8); gn(logfd, dc1[0]); gp(logfd, "\n\x00" as *u8) 110 var t1: i64 = 0 111 if oc[0] == 0 { if dc1[0] == 0 { t1 = 1 } } 112 pass = pass + grow(logfd, "T1 clean corpus -> 0 orphans, 0 dead\x00" as *u8, t1) 113 114 // =================================================================== 115 // T2: PLANTED ORPHAN. Same as T1 + page "lonely" that NOBODY links. 116 // Expect EXACTLY one orphan == rowid(lonely). (start still excluded.) 117 // =================================================================== 118 let s2: *NxWikiDocStore = sys_mmap(512) as *NxWikiDocStore 119 nx_wiki_doc_store_init(s2, 16, 4096, 4096, 65536) 120 add_page(s2, "/wiki/start.html\x00" as *u8, "Front door. See [[alpha]] and [[beta]].\x00" as *u8) 121 add_page(s2, "/wiki/alpha.html\x00" as *u8, "Alpha leads to [[gamma]].\x00" as *u8) 122 add_page(s2, "/wiki/beta.html\x00" as *u8, "Beta also leads to [[gamma]].\x00" as *u8) 123 add_page(s2, "/wiki/gamma.html\x00" as *u8, "Gamma circles back to [[alpha]].\x00" as *u8) 124 let lonely_rowid: i64 = add_page(s2, "/wiki/lonely.html\x00" as *u8, "Nobody links to me. I do link [[alpha]] though.\x00" as *u8) 125 126 let roots2: *i64 = sys_mmap(NX_IMS_MAX_ROOTS * 8) as *i64 127 roots2[0] = nx_ims_resolve_root(s2, "start\x00" as *u8, 5) 128 let orph2: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 129 let oc2: *i64 = sys_mmap(8) as *i64 130 nx_ims_orphans(s2, roots2, 1, orph2, NX_IMS_MAX_PAGES, oc2) 131 gp(logfd, "T2 planted-orphan lonely_rowid=\x00" as *u8); gn(logfd, lonely_rowid) 132 gp(logfd, " orphans_found=\x00" as *u8); gn(logfd, oc2[0]) 133 gp(logfd, " orphan[0]_rowid=\x00" as *u8); if oc2[0] > 0 { gn(logfd, orph2[0]) } else { gp(logfd, "(none)\x00" as *u8) } 134 gp(logfd, "\n\x00" as *u8) 135 var t2: i64 = 0 136 if oc2[0] == 1 { if orph2[0] == lonely_rowid { t2 = 1 } } 137 pass = pass + grow(logfd, "T2 planted orphan -> EXACTLY rowid(lonely) flagged (TEETH)\x00" as *u8, t2) 138 139 // =================================================================== 140 // T3: PLANTED DEAD LINK. Clean-ish corpus where alpha hrefs a /wiki/ghost 141 // page that does NOT exist. Expect EXACTLY one dead link whose normalised 142 // target == "ghost". (Uses the HTML href convention to exercise that path.) 143 // =================================================================== 144 let s3: *NxWikiDocStore = sys_mmap(512) as *NxWikiDocStore 145 nx_wiki_doc_store_init(s3, 16, 4096, 4096, 65536) 146 add_page(s3, "/wiki/start.html\x00" as *u8, "Front door <a href=\"/wiki/alpha.html\">a</a> <a href=\"/wiki/beta.html\">b</a>\x00" as *u8) 147 add_page(s3, "/wiki/alpha.html\x00" as *u8, "Alpha points at <a href=\"/wiki/ghost.html\">ghost</a> (does not exist) and <a href=\"/wiki/beta.html\">beta</a>\x00" as *u8) 148 add_page(s3, "/wiki/beta.html\x00" as *u8, "Beta links <a href=\"/wiki/alpha.html\">alpha</a>\x00" as *u8) 149 150 let dsrc3: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 151 let doff3: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 152 let dlen3: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 153 let dc3: *i64 = sys_mmap(8) as *i64 154 nx_ims_dead_links(s3, dsrc3, doff3, dlen3, NX_IMS_MAX_PAGES, dc3) 155 gp(logfd, "T3 planted dead-link dead_found=\x00" as *u8); gn(logfd, dc3[0]) 156 gp(logfd, " dead[0] src_rowid=\x00" as *u8); if dc3[0] > 0 { gn(logfd, dsrc3[0]) } else { gp(logfd, "(none)\x00" as *u8) } 157 gp(logfd, " target=\x00" as *u8) 158 // normalise the reported dead target and check it equals "ghost" 159 var t3: i64 = 0 160 if dc3[0] == 1 { 161 // fetch alpha's body to point at the offending token 162 let tp: *i64 = sys_mmap(8) as *i64; let tn: *i64 = sys_mmap(8) as *i64 163 let up: *i64 = sys_mmap(8) as *i64; let un: *i64 = sys_mmap(8) as *i64 164 let bp: *i64 = sys_mmap(8) as *i64; let bn: *i64 = sys_mmap(8) as *i64 165 nx_wiki_doc_store_lookup(s3, dsrc3[0], tp, tn, up, un, bp, bn) 166 let tok: *u8 = (bp[0] + doff3[0]) as *u8 167 let nb: *i64 = sys_mmap(8) as *i64 168 let nlen: i64 = nx_ims_norm_slug(tok, dlen3[0], nb) 169 gb(logfd, nb[0] as *u8, nlen) 170 // compare normalised target to "ghost" 171 if nlen == 5 { 172 if nx_ims_slug_eq(nb[0] as *u8, nlen, "ghost\x00" as *u8, 5) == 1 { t3 = 1 } 173 } 174 } 175 gp(logfd, "\n\x00" as *u8) 176 pass = pass + grow(logfd, "T3 planted dead link -> EXACTLY target 'ghost' flagged (TEETH)\x00" as *u8, t3) 177 178 // =================================================================== 179 // T4: NEG-CONTROL root exclusion. In s2 (which HAS a real orphan), prove 180 // the root 'start' -- linked by nobody -- is NOT in orphans, while the 181 // real orphan IS. (If the monitor naively flagged every inbound==0 page, 182 // start would appear; if it "flagged nothing", lonely would not.) 183 // =================================================================== 184 var start_flagged: i64 = 0 185 var lonely_flagged: i64 = 0 186 var fi: i64 = 0 187 while fi < oc2[0] { 188 if fi >= NX_IMS_MAX_PAGES { fi = oc2[0] } 189 if fi < oc2[0] { 190 if orph2[fi] == roots2[0] { start_flagged = 1 } 191 if orph2[fi] == lonely_rowid { lonely_flagged = 1 } 192 } 193 fi = fi + 1 194 } 195 gp(logfd, "T4 neg-control start_flagged=\x00" as *u8); gn(logfd, start_flagged) 196 gp(logfd, " lonely_flagged=\x00" as *u8); gn(logfd, lonely_flagged); gp(logfd, "\n\x00" as *u8) 197 var t4: i64 = 0 198 if start_flagged == 0 { if lonely_flagged == 1 { t4 = 1 } } 199 pass = pass + grow(logfd, "T4 NEG-CONTROL root not false-flagged while real orphan IS (TEETH)\x00" as *u8, t4) 200 201 // =================================================================== 202 // T5: NEG-CONTROL no-false-positive. Build a corpus where a page self-links 203 // and every target exists -> 0 dead, and a self-link does NOT rescue the 204 // self-linker from orphanhood (proving self-links are excluded from inbound). 205 // Pages: start(root)->[[solo]]; solo->[[solo]] (self) + [[start]]; island 206 // (linked by nobody, links nobody) -> island is the ONLY orphan. 207 // =================================================================== 208 let s5: *NxWikiDocStore = sys_mmap(512) as *NxWikiDocStore 209 nx_wiki_doc_store_init(s5, 16, 4096, 4096, 65536) 210 add_page(s5, "/wiki/start.html\x00" as *u8, "Root links [[solo]].\x00" as *u8) 211 add_page(s5, "/wiki/solo.html\x00" as *u8, "Solo self-links [[solo]] and back to [[start]].\x00" as *u8) 212 let island_rowid: i64 = add_page(s5, "/wiki/island.html\x00" as *u8, "An island. No links in or out.\x00" as *u8) 213 214 let roots5: *i64 = sys_mmap(NX_IMS_MAX_ROOTS * 8) as *i64 215 roots5[0] = nx_ims_resolve_root(s5, "start\x00" as *u8, 5) 216 let orph5: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 217 let oc5: *i64 = sys_mmap(8) as *i64 218 nx_ims_orphans(s5, roots5, 1, orph5, NX_IMS_MAX_PAGES, oc5) 219 let dsrc5: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 220 let doff5: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 221 let dlen5: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 222 let dc5: *i64 = sys_mmap(8) as *i64 223 nx_ims_dead_links(s5, dsrc5, doff5, dlen5, NX_IMS_MAX_PAGES, dc5) 224 gp(logfd, "T5 self-link/no-fp orphans=\x00" as *u8); gn(logfd, oc5[0]) 225 gp(logfd, " dead=\x00" as *u8); gn(logfd, dc5[0]) 226 gp(logfd, " orphan[0]=\x00" as *u8); if oc5[0] > 0 { gn(logfd, orph5[0]) } else { gp(logfd, "(none)\x00" as *u8) } 227 gp(logfd, "\n\x00" as *u8) 228 // solo self-links but is also linked by start -> NOT orphan. island IS the only orphan. dead==0. 229 var t5: i64 = 0 230 if dc5[0] == 0 { if oc5[0] == 1 { if orph5[0] == island_rowid { t5 = 1 } } } 231 pass = pass + grow(logfd, "T5 NEG-CONTROL self-link != inbound; present targets != dead (TEETH)\x00" as *u8, t5) 232 233 // =================================================================== 234 if pass == 5 { 235 gp(logfd, "IMS-MONITOR GATE GREEN 5/5 (orphans + link-rot detected EXACTLY; root excluded; no false-flag)\n\x00" as *u8) 236 sys_exit(0) 237 } 238 gp(logfd, "IMS-MONITOR GATE RED pass=\x00" as *u8); gn(logfd, pass); gp(logfd, "/5\n\x00" as *u8) 239 sys_exit(1) 240 return 1 241}