code wiki / _hdl_build / nx_ims_monitor_live.nx

nx_ims_monitor_live.nx source

↩ module page · 211 lines · 9390 B

1// nx_ims_monitor_live.nx -- IMS A1 LIVE RUN over the REAL published wiki corpus. 2// 3// READ-ONLY by construction: reads ONE framed corpus snapshot via sys_read_file 4// and computes in memory. Writes NOTHING to the corpus / NAS / anything (the 5// only output is the analysis report to stdout + knowledge/status log). 6// 7// The snapshot knowledge/status/ims_live_corpus.txt is a read-only capture of 8// the live HTTPS pages (each page's exact served bytes), framed as: 9// ###PAGE /wiki/<url> <bytelen>\n <bytelen bytes of page> \n###ENDPAGE\n 10// The bytes ARE the live pages; the framing is just a transport so a sovereign 11// .nx organ can ingest the corpus without a network/CA-bundle dependency. The 12// link-graph ANALYSIS -- the actual A1 capability -- runs 100% in nx_ims_monitor. 13// 14// Populates NxWikiDocStore from the frames, resolves the root (start), then runs 15// nx_ims_orphans + nx_ims_dead_links and prints the findings (orphans[] + 16// dead_links[] with the exact offending target). Sovereign: nx_ims_monitor 17// (composing nx_wiki_index_builder + nx_wiki_backlinks) + nx_syscalls. 18// license_tier: ORIGINAL 19import "nx_ims_monitor.nx" 20import "nx_syscalls.nx" 21const LIVE_MAGIC_8192: i64 = 8192 22const LIVE_MAGIC_16384: i64 = 16384 23 24const LIVE_MAX_BYTES: i64 = 4194304 // 4 MB snapshot cap 25const LIVE_MARK_PAGE_N: i64 = 8 // len("###PAGE ") 26 27func lp(logfd: i64, s: *u8) -> i64 { 28 var n: i64 = 0 29 while s[n] != (0 as u8) { n = n + 1 } 30 sys_write(1, s, n) 31 if logfd > 0 { sys_write(logfd, s, n) } 32 return 0 33} 34func ln_(logfd: i64, v: i64) -> i64 { 35 var m: i64 = v 36 if m < 0 { lp(logfd, "-\x00" as *u8); m = 0 - m } 37 let t: *u8 = sys_mmap(28) 38 var k: i64 = 0 39 if m == 0 { t[0] = 48 as u8; k = 1 } 40 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 41 let bb: *u8 = sys_mmap(28) 42 var i: i64 = 0 43 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 44 sys_write(1, bb, k) 45 if logfd > 0 { sys_write(logfd, bb, k) } 46 return 0 47} 48func lb(logfd: i64, p: *u8, n: i64) -> i64 { 49 var i: i64 = 0 50 while i < n { 51 if i >= LIVE_MAGIC_8192 { i = n } 52 if i < n { 53 sys_write(1, (p as i64 + i) as *u8, 1) 54 if logfd > 0 { sys_write(logfd, (p as i64 + i) as *u8, 1) } 55 } 56 i = i + 1 57 } 58 return 0 59} 60 61// parse a base-10 integer starting at buf[off]; stop at first non-digit. 62// returns value; writes the STOP offset (position of the first non-digit, NOT 63// lim) via out_off so the caller can resume scanning right after the number. 64func parse_int(buf: *u8, off: i64, lim: i64, out_off: *i64) -> i64 { 65 var i: i64 = off 66 var v: i64 = 0 67 var done: i64 = 0 68 while done == 0 { 69 if i >= lim { done = 1 } 70 if done == 0 { 71 let c: i64 = buf[i] as i64 72 if c < 0x30 { done = 1 } 73 if done == 0 { if c > 0x39 { done = 1 } } 74 if done == 0 { 75 v = v * 10 + (c - 0x30) 76 i = i + 1 77 } 78 } 79 } 80 out_off[0] = i 81 return v 82} 83 84// match "###PAGE " at buf[off] ? 1/0 85func is_page_mark(buf: *u8, off: i64, lim: i64) -> i64 { 86 let mk: *u8 = "###PAGE \x00" as *u8 87 if off + LIVE_MARK_PAGE_N > lim { return 0 } 88 var i: i64 = 0 89 var ok: i64 = 1 90 while i < LIVE_MARK_PAGE_N { 91 if buf[off + i] != mk[i] { ok = 0 } 92 i = i + 1 93 } 94 return ok 95} 96 97func main() -> i64 { 98 let logfd: i64 = sys_openat_append("knowledge/status/ims_monitor_live.log\x00" as *u8, 0x1a4) 99 lp(logfd, "IMS-MONITOR LIVE RUN over real published wiki corpus (READ-ONLY)\n\x00" as *u8) 100 101 // ---- read the framed snapshot (READ-ONLY) ---- 102 let lenbox: *i64 = sys_mmap(16) as *i64 103 lenbox[0] = 0 104 let buf: *u8 = sys_read_file("knowledge/status/ims_live_corpus.txt\x00" as *u8, lenbox) 105 if (buf as i64) == 0 { lp(logfd, "FATAL: cannot read ims_live_corpus.txt snapshot\n\x00" as *u8); sys_exit(2) } 106 let total: i64 = lenbox[0] 107 lp(logfd, "snapshot bytes=\x00" as *u8); ln_(logfd, total); lp(logfd, "\n\x00" as *u8) 108 if total < 1 { lp(logfd, "FATAL: empty snapshot\n\x00" as *u8); sys_exit(2) } 109 if total > LIVE_MAX_BYTES { lp(logfd, "FATAL: snapshot too large\n\x00" as *u8); sys_exit(2) } 110 111 // ---- populate the doc store from ###PAGE frames ---- 112 let store: *NxWikiDocStore = sys_mmap(512) as *NxWikiDocStore 113 nx_wiki_doc_store_init(store, 64, LIVE_MAGIC_16384, LIVE_MAGIC_16384, LIVE_MAX_BYTES) 114 115 var i: i64 = 0 116 var npages: i64 = 0 117 let oo: *i64 = sys_mmap(8) as *i64 118 while i < total { 119 if is_page_mark(buf, i, total) == 1 { 120 // url starts after "###PAGE " 121 let url_off: i64 = i + LIVE_MARK_PAGE_N 122 // url runs to the next space 123 var u: i64 = url_off 124 var done_u: i64 = 0 125 while done_u == 0 { 126 if u >= total { done_u = 1 } 127 if done_u == 0 { 128 if buf[u] == (0x20 as u8) { done_u = 1 } 129 if done_u == 0 { u = u + 1 } 130 } 131 } 132 let url_n: i64 = u - url_off 133 // byte length integer follows the space 134 let blen: i64 = parse_int(buf, u + 1, total, oo) 135 // body starts after the newline that ends the header line 136 var hdr_end: i64 = oo[0] 137 // advance to the \n 138 var done_h: i64 = 0 139 while done_h == 0 { 140 if hdr_end >= total { done_h = 1 } 141 if done_h == 0 { 142 if buf[hdr_end] == (0x0A as u8) { done_h = 1 } 143 if done_h == 0 { hdr_end = hdr_end + 1 } 144 } 145 } 146 let body_off: i64 = hdr_end + 1 147 var body_n: i64 = blen 148 if body_off + body_n > total { body_n = total - body_off } 149 // url + body pointers into the buffer (no copy; store copies internally) 150 let url_p: *u8 = (buf as i64 + url_off) as *u8 151 let body_p: *u8 = (buf as i64 + body_off) as *u8 152 // title = url (we don't need markdown title extraction for HTML graph) 153 let rid: i64 = nx_wiki_doc_store_add(store, url_p, url_n, url_p, url_n, body_p, body_n) 154 if rid >= 0 { npages = npages + 1 } 155 lp(logfd, " ingested rowid=\x00" as *u8); ln_(logfd, rid); lp(logfd, " url=\x00" as *u8); lb(logfd, url_p, url_n); lp(logfd, " body_n=\x00" as *u8); ln_(logfd, body_n); lp(logfd, "\n\x00" as *u8) 156 // jump past the body to keep scanning for the next ###PAGE 157 i = body_off + body_n 158 } else { 159 i = i + 1 160 } 161 } 162 lp(logfd, "pages ingested=\x00" as *u8); ln_(logfd, npages); lp(logfd, "\n\x00" as *u8) 163 164 // ---- designate root = start.html ---- 165 let roots: *i64 = sys_mmap(NX_IMS_MAX_ROOTS * 8) as *i64 166 roots[0] = nx_ims_resolve_root(store, "start\x00" as *u8, 5) 167 lp(logfd, "root(start) rowid=\x00" as *u8); ln_(logfd, roots[0]); lp(logfd, "\n\x00" as *u8) 168 169 // ---- ORPHANS ---- 170 let orph: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 171 let oc: *i64 = sys_mmap(8) as *i64 172 let rc_o: i64 = nx_ims_orphans(store, roots, 1, orph, NX_IMS_MAX_PAGES, oc) 173 lp(logfd, "--- ORPHANS (published pages with 0 inbound links, excluding root) ---\n\x00" as *u8) 174 lp(logfd, "orphans_rc=\x00" as *u8); ln_(logfd, rc_o); lp(logfd, " count=\x00" as *u8); ln_(logfd, oc[0]); lp(logfd, "\n\x00" as *u8) 175 // lookup out-params for printing orphan urls 176 let tp: *i64 = sys_mmap(8) as *i64; let tn: *i64 = sys_mmap(8) as *i64 177 let up: *i64 = sys_mmap(8) as *i64; let un: *i64 = sys_mmap(8) as *i64 178 let bp: *i64 = sys_mmap(8) as *i64; let bn: *i64 = sys_mmap(8) as *i64 179 var oi: i64 = 0 180 while oi < oc[0] { 181 if oi >= NX_IMS_MAX_PAGES { oi = oc[0] } 182 if oi < oc[0] { 183 nx_wiki_doc_store_lookup(store, orph[oi], tp, tn, up, un, bp, bn) 184 lp(logfd, " ORPHAN rowid=\x00" as *u8); ln_(logfd, orph[oi]); lp(logfd, " url=\x00" as *u8); lb(logfd, up[0] as *u8, un[0]); lp(logfd, "\n\x00" as *u8) 185 } 186 oi = oi + 1 187 } 188 189 // ---- DEAD LINKS ---- 190 let dsrc: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 191 let doff: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 192 let dlen: *i64 = sys_mmap(NX_IMS_MAX_PAGES * 8) as *i64 193 let dc: *i64 = sys_mmap(8) as *i64 194 let rc_d: i64 = nx_ims_dead_links(store, dsrc, doff, dlen, NX_IMS_MAX_PAGES, dc) 195 lp(logfd, "--- DEAD LINKS (outbound /wiki/ targets resolving to no published page) ---\n\x00" as *u8) 196 lp(logfd, "dead_rc=\x00" as *u8); ln_(logfd, rc_d); lp(logfd, " count=\x00" as *u8); ln_(logfd, dc[0]); lp(logfd, "\n\x00" as *u8) 197 var di: i64 = 0 198 while di < dc[0] { 199 if di >= NX_IMS_MAX_PAGES { di = dc[0] } 200 if di < dc[0] { 201 nx_wiki_doc_store_lookup(store, dsrc[di], tp, tn, up, un, bp, bn) 202 let tok: *u8 = (bp[0] + doff[di]) as *u8 203 lp(logfd, " DEAD src=\x00" as *u8); lb(logfd, up[0] as *u8, un[0]); lp(logfd, " -> target=\x00" as *u8); lb(logfd, tok, dlen[di]); lp(logfd, "\n\x00" as *u8) 204 } 205 di = di + 1 206 } 207 208 lp(logfd, "LIVE RUN DONE pages=\x00" as *u8); ln_(logfd, npages); lp(logfd, " orphans=\x00" as *u8); ln_(logfd, oc[0]); lp(logfd, " dead_links=\x00" as *u8); ln_(logfd, dc[0]); lp(logfd, " (read-only; corpus untouched)\n\x00" as *u8) 209 sys_exit(0) 210 return 0 211}