code wiki / _hdl_build / _wiki_engine_gate.nx
_wiki_engine_gate.nx source
↩ module page · 182 lines · 9529 B
1// _wiki_engine_gate.nx -- unit gate PROVING the INTERNAL sovereign wiki
2// engine SERVES auto-discovered pages, including SUBDIR charters reached
3// only by the new RECURSIVE walk.
4//
5// NO mocks, NO live socket: drives the REAL loader + REAL route handler
6// against the REAL wiki content dir (/mnt/c/Users/elder/nishi-silicon).
7// 1. fresh index builder -> nx_wcl_discover_tree (RECURSIVE walk) ->
8// assert pages >= 35 (31 top-level + subdir charters)
9// 2. resolve a KNOWN TOP-LEVEL slug (/wiki/nishi-wiki-charter) through
10// the real nx_wiki_doc_handle route path -> assert HTTP 200 + a
11// known body substring ("charter") -> toplevel_ok + render_ok
12// 3. resolve a KNOWN SUBDIR slug (/wiki/hdl-hacking, produced by the
13// recursion for hdl/HACKING.md) through nx_wiki_doc_handle ->
14// assert HTTP 200 -> subdir_ok (PROVES recursion serves)
15// 4. search "sovereign" -> >=1 hit (search_ok); bogus term -> 0 hits
16// (search_neg)
17// 5. emit ONE marker line (stdout + knowledge/status/wiki_engine_gate.log):
18// WIKIENGINE pages=<n> toplevel_ok=.. subdir_ok=.. render_ok=..
19// search_ok=.. search_neg=.. verdict=GREEN|RED
20//
21// Judged by the printed WIKIENGINE marker (verdict=GREEN only if EVERY
22// assertion holds), NOT by $? -- mirrors _wiki_discovery_gate.nx.
23//
24// LANDMINE (per the engineering rules + nx_wiki_index_builder DEBT note):
25// the native compiler can desync `let rc = <cross-fn call>` vs an
26// immediate `rc == const` compare. So EVERY assertion is judged on a
27// GROUND-TRUTH observable: the doc-store count delta (pages), the bytes
28// in the response buffer (served HTTP 200), and the query result's
29// n_rowids_filled (hits) -- never a bare returned status code.
30//
31// Import set: nx_syscalls + content_loader (discovery) + doc_handler
32// (the route path; transitively pulls index_builder->search_inverted +
33// doc_render). Each base module is imported exactly ONCE to avoid the
34// double-import nxasm rc6 trap.
35// license_tier: ORIGINAL
36import "nx_syscalls.nx"
37import "wiki/nx_wiki_content_loader.nx"
38import "wiki/nx_wiki_doc_handler.nx"
39
40// ----- tiny io helpers (write to a fd; mirror the _wiki_discovery_gate shape) -----
41func eg_fp(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 }
42func eg_fn(fd: i64, v: i64) -> i64 {
43 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(fd, "-" as *u8, 1) }
44 let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 }
45 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
46 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
47 sys_write(fd, bb, k); return 0
48}
49// write to BOTH stdout and the durable log fd (lfd<0 = stdout only)
50func eg_w2(lfd: i64, s: *u8) -> i64 { eg_fp(1, s); if lfd >= 0 { eg_fp(lfd, s) } return 0 }
51func eg_n2(lfd: i64, v: i64) -> i64 { eg_fn(1, v); if lfd >= 0 { eg_fn(lfd, v) } return 0 }
52
53// does NUL-terminated haystack contain counted-length needle? (substring)
54func eg_contains(hay: *u8, hay_n: i64, needle: *u8, needle_n: i64) -> i64 {
55 if needle_n <= 0 { return 0 }
56 if hay_n < needle_n { return 0 }
57 var i: i64 = 0
58 while i + needle_n <= hay_n {
59 var k: i64 = 0
60 var hit: i64 = 1
61 while k < needle_n { if hay[i + k] != needle[k] { hit = 0; k = needle_n } else { k = k + 1 } }
62 if hit == 1 { return 1 }
63 i = i + 1
64 }
65 return 0
66}
67
68const EG_K_MIN: i64 = 35 // assert: >= 31 top-level + subdir charters
69const EG_RESP_CAP: i64 = 1048576 // 1 MiB HTTP response buffer
70
71// Resolve ONE /wiki/<slug> through the REAL route handler nx_wiki_doc_handle.
72// Returns 1 iff the handler produced a non-empty response that begins with
73// "HTTP/1.1 200 OK" (GROUND TRUTH = bytes served), else 0. If `need_word`
74// is non-empty it must ALSO appear in the response (body substring check).
75func eg_serves(store: *NxWikiDocStore, url: *u8, url_n: i64,
76 need_word: *u8, need_word_n: i64) -> i64 {
77 let resp: *u8 = sys_mmap(EG_RESP_CAP)
78 let rn: *i64 = (sys_mmap(8)) as *i64
79 rn[0] = 0
80 // artifact_store=0 -> handler silently skips the pipeline banner (Cardinal 14)
81 nx_wiki_doc_handle(store, 0 as *NxArtifactStore, url, url_n, resp, EG_RESP_CAP, rn)
82 let n: i64 = rn[0]
83 if n <= 0 { return 0 }
84 let ok200: i64 = eg_contains(resp, n, "HTTP/1.1 200 OK" as *u8, 15)
85 if ok200 != 1 { return 0 }
86 if need_word_n > 0 {
87 let hasw: i64 = eg_contains(resp, n, need_word, need_word_n)
88 if hasw != 1 { return 0 }
89 }
90 return 1
91}
92
93func main() -> i64 {
94 eg_fp(1, "WIKIENGINE-GATE: start (RECURSIVE discover -> real route serve, top-level + subdir)\n" as *u8)
95
96 // ===== 1. RECURSIVE auto-discover over the REAL content tree =====
97 let doc_store: *NxWikiDocStore = (sys_mmap(256)) as *NxWikiDocStore
98 let store_rc: i64 = nx_wiki_doc_store_init(doc_store, 1000, 131072, 131072, 8388608)
99 if store_rc != NX_WIB_OK {
100 eg_fp(1, "WIKIENGINE pages=0 toplevel_ok=0 subdir_ok=0 render_ok=0 search_ok=0 search_neg=0 verdict=RED (store init failed)\n" as *u8)
101 sys_exit(1); return 1
102 }
103 let builder: *NxWikiIndexBuilder = (sys_mmap(64)) as *NxWikiIndexBuilder
104 let builder_rc: i64 = nx_wiki_index_builder_init(builder, doc_store, 1000)
105 if builder_rc != NX_WIB_OK {
106 eg_fp(1, "WIKIENGINE pages=0 toplevel_ok=0 subdir_ok=0 render_ok=0 search_ok=0 search_neg=0 verdict=RED (builder init failed)\n" as *u8)
107 sys_exit(1); return 1
108 }
109
110 // THE FEATURE UNDER TEST: recursively walk the dir + index every *.md.
111 let dir: *u8 = "/mnt/c/Users/elder/nishi-silicon/" as *u8
112 let pages_ret: i64 = nx_wcl_discover_tree(builder, dir)
113 eg_fp(1, " nx_wcl_discover_tree -> pages=" as *u8); eg_fn(1, pages_ret); eg_fp(1, "\n" as *u8)
114 // GROUND TRUTH = docs actually in the store (not the returned count)
115 let pages: i64 = nx_wiki_doc_store_count(doc_store)
116 eg_fp(1, " doc_store_count=" as *u8); eg_fn(1, pages); eg_fp(1, "\n" as *u8)
117
118 // finalize the inverted index (PASS 2) so search + handler work
119 let fin_rc: i64 = nx_wiki_index_builder_finalize(builder)
120 if fin_rc != NX_WIB_OK { eg_fp(1, " WARN finalize rc=" as *u8); eg_fn(1, fin_rc); eg_fp(1, "\n" as *u8) }
121
122 var pages_ok: i64 = 0
123 if pages >= EG_K_MIN { pages_ok = 1 }
124
125 // ===== 2. SERVE a KNOWN TOP-LEVEL slug through the route handler =====
126 // /wiki/nishi-wiki-charter (from NISHI_WIKI_CHARTER.md; slug unchanged
127 // by recursion since it is top-level). Assert HTTP 200 + "charter".
128 let toplevel_ok: i64 = eg_serves(doc_store,
129 "/wiki/nishi-wiki-charter" as *u8, 24, "charter" as *u8, 7)
130 eg_fp(1, " serve /wiki/nishi-wiki-charter (200+charter) -> " as *u8); eg_fn(1, toplevel_ok); eg_fp(1, "\n" as *u8)
131 // render_ok = the SAME serve proved rendered HTML (200 + body word)
132 let render_ok: i64 = toplevel_ok
133
134 // ===== 3. SERVE a KNOWN SUBDIR slug -- PROVES recursion serves =====
135 // hdl/HACKING.md -> path-aware slug "hdl-hacking" (only reachable via
136 // the recursive walk; the single-level walk never descended hdl/).
137 let subdir_ok: i64 = eg_serves(doc_store,
138 "/wiki/hdl-hacking" as *u8, 17, "" as *u8, 0)
139 eg_fp(1, " serve /wiki/hdl-hacking (200) -> " as *u8); eg_fn(1, subdir_ok); eg_fp(1, "\n" as *u8)
140
141 // ===== 4. SEARCH controls: positive term hits, bogus term misses =====
142 let idx: *NxInvIndex = nx_wiki_index_builder_get_index(builder)
143 var search_ok: i64 = 0
144 var search_neg: i64 = 0
145 if (idx as i64) != 0 {
146 let rowids: *i64 = (sys_mmap(8 * 256)) as *i64
147 let qr: *NxInvQueryResult = (sys_mmap(64)) as *NxInvQueryResult
148 // POSITIVE: "sovereign" appears across the charter corpus.
149 nx_inv_query_term(idx, "sovereign" as *u8, 9, rowids, 256, qr)
150 let hits_pos: i64 = qr.n_rowids_filled
151 if hits_pos >= 1 { search_ok = 1 }
152 eg_fp(1, " search 'sovereign' hits=" as *u8); eg_fn(1, hits_pos); eg_fp(1, "\n" as *u8)
153 // NEGATIVE: a bogus token must return zero hits (liar-kill).
154 let qr2: *NxInvQueryResult = (sys_mmap(64)) as *NxInvQueryResult
155 nx_inv_query_term(idx, "zzqxnonexistentterm" as *u8, 19, rowids, 256, qr2)
156 let hits_neg: i64 = qr2.n_rowids_filled
157 if hits_neg == 0 { search_neg = 1 }
158 eg_fp(1, " search 'zzqxnonexistentterm' hits=" as *u8); eg_fn(1, hits_neg); eg_fp(1, "\n" as *u8)
159 }
160
161 // ===== 5. VERDICT (all assertions) + marker line =====
162 var green: i64 = 1
163 if pages_ok != 1 { green = 0 }
164 if toplevel_ok != 1 { green = 0 }
165 if subdir_ok != 1 { green = 0 }
166 if render_ok != 1 { green = 0 }
167 if search_ok != 1 { green = 0 }
168 if search_neg != 1 { green = 0 }
169
170 let lfd: i64 = sys_openat_append("knowledge/status/wiki_engine_gate.log" as *u8, 420)
171 eg_w2(lfd, "WIKIENGINE pages=" as *u8); eg_n2(lfd, pages)
172 eg_w2(lfd, " toplevel_ok=" as *u8); eg_n2(lfd, toplevel_ok)
173 eg_w2(lfd, " subdir_ok=" as *u8); eg_n2(lfd, subdir_ok)
174 eg_w2(lfd, " render_ok=" as *u8); eg_n2(lfd, render_ok)
175 eg_w2(lfd, " search_ok=" as *u8); eg_n2(lfd, search_ok)
176 eg_w2(lfd, " search_neg=" as *u8); eg_n2(lfd, search_neg)
177 if green == 1 { eg_w2(lfd, " verdict=GREEN\n" as *u8) } else { eg_w2(lfd, " verdict=RED\n" as *u8) }
178 if lfd >= 0 { sys_close(lfd) }
179
180 if green == 1 { sys_exit(0); return 0 }
181 sys_exit(1); return 1
182}