code wiki / _hdl_build / _wiki_frontend_gate.nx

_wiki_frontend_gate.nx source

↩ module page · 274 lines · 14703 B

1// _wiki_frontend_gate.nx -- THE S-CLASS WIKI-FRONTEND GATE. 2// 3// Re-proves, from a REAL run (never a fabricated GREEN), that the served wiki 4// page is world-class AND that the two render bugs are dead: 5// 6// clean_ok the served nishi-canonical <meta> value contains ONLY a 7// valid canonical URL ("https://nishifamily.com/wiki/<slug>") 8// and NONE of the garbage bytes that the const-*u8-index bug 9// used to emit (we assert the exact clean URL is present AND 10// that no 0xEF/0xBF/0x00 byte sits inside the meta value). 11// title_ok the served <title> contains the CLEAN heading TEXT and does 12// NOT begin with a raw markdown '#'/"# " (the title-leak bug). 13// semantic_ok the served HTML contains <main and <article (HTML5 semantic 14// structure) -- the world-class chrome. 15// cite_wired_ok a page body with [[cite:<cid>]] citing a HOSTABLE archived 16// source renders the inline nx-cite card (the supporting-source 17// bytes appear INSIDE a <details ... nx-cite> block) -- the 18// cite pass that is wired into the render pipeline. 19// 20// Judged on GROUND-TRUTH SUBSTRINGS in the actually-rendered HTML, never a bare 21// rc==const. The page render is driven DIRECTLY via nx_wiki_doc_handle (the same 22// handler the daemon forks) -- no live socket. The cite check seeds a fresh 23// per-run archive prefix (empty each run => idempotent, Rule 10). 24// 25// Verdict line (stdout + knowledge/status/wiki_frontend_gate.log): 26// WIKIFRONTEND clean_ok=<0|1> title_ok=<0|1> semantic_ok=<0|1> cite_wired_ok=<0|1> verdict=GREEN|RED 27// 28// Pure NishiLang, NO SQL, NO .sh/.py/.js, no new .tsv/.conf. nx_sites_daemon 29// UNTOUCHED. license_tier: ORIGINAL 30import "nx_syscalls.nx" 31import "wiki/nx_wiki_index_builder.nx" 32import "wiki/nx_wiki_content_loader.nx" 33import "wiki/nx_wiki_doc_handler.nx" 34import "wiki/nx_wiki_cite_render.nx" 35import "wiki/nx_artifact_store.nx" 36 37const WFG_LOG: *u8 = "knowledge/status/wiki_frontend_gate.log" 38const WFG_RESP_CAP: i64 = 2097152 // 2 MiB served-response buffer 39const WFG_OUTCAP: i64 = 1048576 // 1 MiB cite-render output buffer 40const WFG_SCAN_CAP: i64 = 64 // archive segment-scan cap 41 42// ---- io helpers (mirror _wiki_cite_gate.nx) ---- 43func wfg_w(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 } 44func wfg_n(fd: i64, v: i64) -> i64 { 45 let bb: *u8 = sys_mmap(28); var m: i64 = v 46 if m < 0 { m = 0 - m; sys_write(fd, "-" as *u8, 1) } 47 let t: *u8 = sys_mmap(28); var k: i64 = 0 48 if m == 0 { t[0] = 48 as u8; k = 1 } 49 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 50 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 51 sys_write(fd, bb, k); return 0 52} 53func wfg_w2(lfd: i64, s: *u8) -> i64 { wfg_w(1, s); if lfd >= 0 { wfg_w(lfd, s) } return 0 } 54func wfg_n2(lfd: i64, v: i64) -> i64 { wfg_n(1, v); if lfd >= 0 { wfg_n(lfd, v) } return 0 } 55func wfg_p(s: *u8) -> i64 { wfg_w(1, s); return 0 } 56func wfg_pn(v: i64) -> i64 { wfg_n(1, v); return 0 } 57func wfg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 58 59func wfg_cat(dst: *u8, off: i64, s: *u8) -> i64 { 60 var i: i64 = 0 61 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 } 62 return off + i 63} 64func wfg_catn(dst: *u8, off: i64, v: i64) -> i64 { 65 var m: i64 = v; var o: i64 = off 66 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m } 67 let t: *u8 = sys_mmap(28); var k: i64 = 0 68 if m == 0 { t[0] = 48 as u8; k = 1 } 69 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 70 var i: i64 = 0; while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 } 71 return o + k 72} 73 74// ===== GROUND-TRUTH SUBSTRING SEARCH (judge on rendered HTML) ================= 75func wfg_find(hay: *u8, hn: i64, needle: *u8) -> i64 { 76 let nn: i64 = wfg_slen(needle) 77 if nn == 0 { return 0 - 1 } 78 if nn > hn { return 0 - 1 } 79 var i: i64 = 0 80 let last: i64 = hn - nn 81 while i <= last { 82 var j: i64 = 0 83 var eq: i64 = 1 84 while j < nn { 85 if eq == 1 { if hay[i + j] != needle[j] { eq = 0 } } 86 j = j + 1 87 } 88 if eq == 1 { return i } 89 i = i + 1 90 } 91 return 0 - 1 92} 93func wfg_has(hay: *u8, hn: i64, needle: *u8) -> i64 { 94 if wfg_find(hay, hn, needle) >= 0 { return 1 } 95 return 0 96} 97 98func main() -> i64 { 99 wfg_p("WIKIFRONTEND-GATE: start (world-class served render + bug-kill + inline cite)\n" as *u8) 100 101 // ===== STEP A: render a REAL discovered page via the served handler ======== 102 let doc_store: *NxWikiDocStore = (sys_mmap(256)) as *NxWikiDocStore 103 let s_rc: i64 = nx_wiki_doc_store_init(doc_store, 100, 65536, 65536, 4194304) 104 let builder: *NxWikiIndexBuilder = (sys_mmap(64)) as *NxWikiIndexBuilder 105 let b_rc: i64 = nx_wiki_index_builder_init(builder, doc_store, 100) 106 let n_loaded: i64 = nx_wcl_load_discover_tree(builder) 107 let f_rc: i64 = nx_wiki_index_builder_finalize(builder) 108 wfg_p(" discovered pages="); wfg_pn(n_loaded); 109 wfg_p(" store_rc="); wfg_pn(s_rc); wfg_p(" builder_rc="); wfg_pn(b_rc); wfg_p(" finalize_rc="); wfg_pn(f_rc); wfg_p("\n" as *u8) 110 111 let art: *NxArtifactStore = (sys_mmap(512)) as *NxArtifactStore 112 let a_rc: i64 = nx_artifact_store_init(art, 1000) 113 114 // serve /wiki/nishi-intelligent-wiki-charter (a stable seeded charter). 115 let url: *u8 = "/wiki/nishi-intelligent-wiki-charter" as *u8 116 let url_n: i64 = 36 117 let resp: *u8 = sys_mmap(WFG_RESP_CAP) 118 let resp_n: *i64 = (sys_mmap(8)) as *i64 119 resp_n[0] = 0 120 let h_rc: i64 = nx_wiki_doc_handle(doc_store, art, url, url_n, resp, WFG_RESP_CAP, resp_n) 121 let hn: i64 = resp_n[0] 122 wfg_p(" handle rc="); wfg_pn(h_rc); wfg_p(" resp_len="); wfg_pn(hn); wfg_p("\n" as *u8) 123 124 // ---- clean_ok: the nishi-canonical meta holds the EXACT clean URL, and no 125 // garbage byte (0xEF / 0xBF replacement-char bytes, or a NUL) appears in the 126 // meta value region. We assert the clean URL is present; then we scan the 127 // meta-content window for a forbidden byte (the old bug wrote 23 garbage 128 // bytes right before the URL inside content="..."). 129 var clean_ok: i64 = 0 130 let canon_clean: *u8 = "content=\"https://nishifamily.com/wiki/nishi-intelligent-wiki-charter\"" as *u8 131 let cc_at: i64 = wfg_find(resp, hn, canon_clean) 132 // locate the nishi-canonical meta name and scan from there to the URL start 133 // for any 0xEF/0xBF/0x00 garbage byte (must be NONE). 134 let name_at: i64 = wfg_find(resp, hn, "name=\"nishi-canonical\"" as *u8) 135 var garbage_seen: i64 = 0 136 if name_at >= 0 { 137 var gi: i64 = name_at 138 let glast: i64 = name_at + 140 139 while gi < glast { 140 if gi < hn { 141 let bb: i64 = resp[gi] as i64 142 if bb == 0xEF { garbage_seen = 1 } 143 if bb == 0xBF { garbage_seen = 1 } 144 if bb == 0 { garbage_seen = 1 } 145 } 146 gi = gi + 1 147 } 148 } 149 if cc_at >= 0 { if garbage_seen == 0 { clean_ok = 1 } } 150 wfg_p(" clean: canon_url@"); wfg_pn(cc_at); wfg_p(" name@"); wfg_pn(name_at); wfg_p(" garbage_byte="); wfg_pn(garbage_seen); wfg_p(" -> clean_ok="); wfg_pn(clean_ok); wfg_p("\n" as *u8) 151 152 // ---- title_ok: <title> contains the clean heading text AND the char right 153 // after "<title>" is NOT '#' (no raw markdown leak). The charter's first 154 // heading is "# NISHI INTELLIGENT WIKI CHARTER" -> clean title text. 155 var title_ok: i64 = 0 156 let t_at: i64 = wfg_find(resp, hn, "<title>" as *u8) 157 let clean_title: i64 = wfg_find(resp, hn, "<title>NISHI INTELLIGENT WIKI CHARTER" as *u8) 158 var leading_hash: i64 = 0 159 if t_at >= 0 { 160 let after: i64 = t_at + 7 // byte just past "<title>" 161 if after < hn { if resp[after] == (35 as u8) { leading_hash = 1 } } // '#' 162 } 163 if clean_title >= 0 { if leading_hash == 0 { title_ok = 1 } } 164 wfg_p(" title: title@"); wfg_pn(t_at); wfg_p(" clean_title@"); wfg_pn(clean_title); wfg_p(" leading_hash="); wfg_pn(leading_hash); wfg_p(" -> title_ok="); wfg_pn(title_ok); wfg_p("\n" as *u8) 165 166 // ---- semantic_ok: HTML5 semantic structure present (<main and <article). ---- 167 var semantic_ok: i64 = 0 168 let main_at: i64 = wfg_find(resp, hn, "<main" as *u8) 169 let art_at: i64 = wfg_find(resp, hn, "<article" as *u8) 170 if main_at >= 0 { if art_at >= 0 { semantic_ok = 1 } } 171 wfg_p(" semantic: main@"); wfg_pn(main_at); wfg_p(" article@"); wfg_pn(art_at); wfg_p(" -> semantic_ok="); wfg_pn(semantic_ok); wfg_p("\n" as *u8) 172 173 // ===== STEP B: cite_wired_ok -- inline supporting-source card, SERVED ====== 174 // MEASUREMENT-ARTIFACT FIX: this check USED to assert the card substring in 175 // the PRE-markdown nx_wiki_cite_render buffer -- which could pass while the 176 // SERVED card was html-escaped (&lt;details). It now drives the REAL served 177 // handler (nx_wiki_doc_handle_cp) and asserts on the post-markdown SERVED 178 // HTML: the card must be LIVE ("<details class=\"nx-cite\"", NOT "&lt;details") 179 // with the evidence INSIDE it, AND a plain [[wikilink]] in the same body still 180 // resolves (coexistence) -- so the check can no longer pass on escaped output. 181 // 182 // Seed a FRESH per-run archive prefix (idempotent, never production WAR_PREFIX); 183 // archive a HOSTABLE (CC-BY) source; seed a doc-store page citing it (plus a 184 // resolvable [[wikilink]]); render via the cite-prefix-injectable handler. 185 let prefix: *u8 = sys_mmap(256) 186 var po: i64 = 0 187 po = wfg_cat(prefix, po, "knowledge/store/wikiarchive-frontendgate-" as *u8) 188 po = wfg_catn(prefix, po, sys_now_realtime_sec()) 189 po = wfg_cat(prefix, po, "-" as *u8) 190 prefix[po] = 0 as u8 191 192 let s1_slug: *u8 = "src-fe1" as *u8 193 let s1_body: *u8 = "Supporting evidence: FRONTEND-CITE-EVIDENCE-24680 (full text of the hostable cited source).\n" as *u8 194 let cid_s1: *u8 = sys_mmap(80) 195 let w1: *i64 = ss_begin() 196 let r1: i64 = war_archive_page(w1, s1_slug, s1_body, wfg_slen(s1_body), cid_s1) 197 let c1: i64 = ss_commit(prefix, w1, 0) 198 let lset1: i64 = nx_wiki_license_set(prefix, cid_s1, NXLIC_CC_BY, 1) 199 wfg_p(" cite-seed: archive_rc="); wfg_pn(r1); wfg_p(" commit="); wfg_pn(c1); wfg_p(" cid="); wfg_p(cid_s1); wfg_p(" license_get="); wfg_pn(nx_wiki_license_get(prefix, cid_s1)); wfg_p("\n" as *u8) 200 201 // seed a small doc store: the cited page + a real "fe-target" page so the 202 // in-body [[fe-target]] wikilink resolves LIVE (coexistence proof). 203 let cstore: *NxWikiDocStore = sys_mmap(2048) as *NxWikiDocStore 204 let cs_rc: i64 = nx_wiki_doc_store_init(cstore, 16, 4096, 4096, 65536) 205 let tgt_body: *u8 = "# FE Target\n\nThe resolvable cross-reference target.\n" as *u8 206 let tgt_rid: i64 = nx_wiki_doc_store_add(cstore, 207 "FE Target" as *u8, 9, "/wiki/fe-target" as *u8, 15, tgt_body, wfg_slen(tgt_body)) 208 // page body: a heading + a paragraph citing S1 + a resolvable [[fe-target]]. 209 let pg1: *u8 = sys_mmap(512) 210 var g1: i64 = 0 211 g1 = wfg_cat(pg1, g1, "# FE Cite Page\n\nSee the supporting source here: [[cite:" as *u8) 212 g1 = wfg_cat(pg1, g1, cid_s1) 213 g1 = wfg_cat(pg1, g1, "]] -- read in place. Also a plain [[fe-target]] resolves.\n" as *u8) 214 pg1[g1] = 0 as u8 215 let pg1_rid: i64 = nx_wiki_doc_store_add(cstore, 216 "FE Cite Page" as *u8, 12, "/wiki/fe-cite" as *u8, 13, pg1, g1) 217 wfg_p(" cite-store: target_rid="); wfg_pn(tgt_rid); wfg_p(" cite_rid="); wfg_pn(pg1_rid); wfg_p(" init_rc="); wfg_pn(cs_rc); wfg_p("\n" as *u8) 218 219 // RENDER via the SAME served handler the daemon forks, but cite-prefix-injected. 220 let cresp: *u8 = sys_mmap(WFG_RESP_CAP) 221 let cresp_n: *i64 = (sys_mmap(8)) as *i64 222 cresp_n[0] = 0 223 let cite_url: *u8 = "/wiki/fe-cite" as *u8 224 let ch_rc: i64 = nx_wiki_doc_handle_cp(cstore, 0 as *NxArtifactStore, cite_url, 13, 225 cresp, WFG_RESP_CAP, cresp_n, prefix) 226 let cn: i64 = cresp_n[0] 227 var cite_wired_ok: i64 = 0 228 if ch_rc == NX_WDH_OK { 229 let det_at: i64 = wfg_find(cresp, cn, "<details class=\"nx-cite\"" as *u8) 230 let esc_at: i64 = wfg_find(cresp, cn, "&lt;details" as *u8) // MUST be absent 231 let wl_at: i64 = wfg_find(cresp, cn, "<a href=\"/wiki/fe-target\"" as *u8) // coexistence: wikilink resolved 232 // scope card-body + evidence to the region AT/AFTER the details-open (the 233 // string "nx-cite-body" also occurs in the <head> CSS .nx-cite-body{...}). 234 var body_at: i64 = 0 - 1 235 var ev_at: i64 = 0 - 1 236 if det_at >= 0 { 237 let ctail: *u8 = (cresp as i64 + det_at) as *u8 238 let ctail_n: i64 = cn - det_at 239 let rb: i64 = wfg_find(ctail, ctail_n, "nx-cite-body" as *u8) 240 if rb >= 0 { body_at = det_at + rb } 241 let re: i64 = wfg_find(ctail, ctail_n, "FRONTEND-CITE-EVIDENCE-24680" as *u8) 242 if re >= 0 { ev_at = det_at + re } 243 } 244 if det_at >= 0 { if esc_at < 0 { if body_at > det_at { if ev_at > body_at { if wl_at >= 0 { cite_wired_ok = 1 } } } } } 245 wfg_p(" cite(SERVED): handle_rc="); wfg_pn(ch_rc); wfg_p(" resp_len="); wfg_pn(cn); wfg_p(" details@"); wfg_pn(det_at); wfg_p(" &lt;details@(should be -1)="); wfg_pn(esc_at); wfg_p(" card-body@"); wfg_pn(body_at); wfg_p(" evidence@"); wfg_pn(ev_at); wfg_p(" wikilink_live@"); wfg_pn(wl_at); wfg_p("\n" as *u8) 246 } 247 wfg_p(" -> cite_wired_ok="); wfg_pn(cite_wired_ok); wfg_p("\n" as *u8) 248 249 // ===== EVIDENCE: echo the served <head> + a body chunk for visual judging == 250 wfg_p(" ---- served HTML head+chrome (first 1200 bytes) ----\n" as *u8) 251 var dh: i64 = hn 252 if dh > 1200 { dh = 1200 } 253 sys_write(1, resp, dh) 254 wfg_p("\n ---- /served ----\n" as *u8) 255 256 // ===== VERDICT ===== 257 var green: i64 = 1 258 if clean_ok != 1 { green = 0 } 259 if title_ok != 1 { green = 0 } 260 if semantic_ok != 1 { green = 0 } 261 if cite_wired_ok != 1 { green = 0 } 262 263 let lfd: i64 = sys_openat_append(WFG_LOG, 420) 264 wfg_w2(lfd, "WIKIFRONTEND clean_ok=" as *u8); wfg_n2(lfd, clean_ok) 265 wfg_w2(lfd, " title_ok=" as *u8); wfg_n2(lfd, title_ok) 266 wfg_w2(lfd, " semantic_ok=" as *u8); wfg_n2(lfd, semantic_ok) 267 wfg_w2(lfd, " cite_wired_ok=" as *u8); wfg_n2(lfd, cite_wired_ok) 268 if green == 1 { wfg_w2(lfd, " verdict=GREEN\n" as *u8) } else { wfg_w2(lfd, " verdict=RED\n" as *u8) } 269 if lfd >= 0 { sys_close(lfd) } 270 271 if green == 1 { sys_exit(0); return 0 } 272 sys_exit(1) 273 return 1 274}