code wiki / _hdl_build / nx_wiki_structure_regen_gate.nx
nx_wiki_structure_regen_gate.nx source
↩ module page · 316 lines · 18099 B
1// nx_wiki_structure_regen_gate.nx -- TEETH for IMS A4b (the re-publish-on-pulse engine).
2//
3// HERMETIC + apply_flag=0 throughout: the gate exercises the regen DECISION CORE (reg_decide +
4// reg_guard_and_apply) directly against in-memory NxWikiDocStore fixtures + a synthetic RegRegistry, with
5// do_push_flag/apply_flag = 0 EVERYWHERE -- it never touches web_assets, the NAS, the vault, or SSH. Each
6// clean assertion is paired with a PLANTED-fault assertion that forces a specific verdict at a known page:
7//
8// T1 CLEAN registry (every non-root page cross-linked, no dead links) -> EVERY emitter page CLEAN +
9// would_publish=YES, 0 orphans, and the manual-only page is RC_MANUAL_ONLY (counted, not published).
10// T2 PLANTED ORPHAN via a DROPPED LINK: page "hub" used to link "leaf" (baseline) but its re-emitted
11// body DROPS that link, leaving "leaf" with 0 inbound -> regen marks the OFFENDING page ("hub")
12// RC_REFUSE_ORPHAN + would_publish=NO. (proves it REFUSES to create an orphan, and attributes it to
13// the change that caused it.) [TEETH]
14// T3 PLANTED GUARD-FAIL: a page whose would-be body has NO freshness stamp -> pg_decide returns
15// PG_REJECT_NO_FRESHNESS -> that page is skipped (would_publish=NO) with the EXACT reason. [TEETH]
16// T4 PUSH SAFETY: across the WHOLE run, apply_flag=0 -> NO page has pushed==1 (push_invoked never set).
17// [TEETH]
18//
19// GREEN iff T1..T4 with EXACT verdicts/identities. exit 0 GREEN / 1 RED. Logs to
20// knowledge/status/wiki_structure_regen_gate.log. Sovereign: nx_wiki_structure_regen (which composes A1
21// monitor + A2 guard + A4a versioned-publish + index_builder) + nx_syscalls. license_tier: ORIGINAL
22import "nx_wiki_structure_regen.nx"
23import "nx_syscalls.nx"
24
25// ---- dual-sink print (stdout + log) ----
26func gp(logfd: i64, s: *u8) -> i64 {
27 var n: i64 = 0
28 while s[n] != (0 as u8) { n = n + 1 }
29 sys_write(1, s, n)
30 if logfd > 0 { sys_write(logfd, s, n) }
31 return 0
32}
33func gn(logfd: i64, v: i64) -> i64 {
34 var m: i64 = v
35 if m < 0 { gp(logfd, "-" as *u8); m = 0 - m }
36 let t: *u8 = sys_mmap(28)
37 var k: i64 = 0
38 if m == 0 { t[0] = 48 as u8; k = 1 }
39 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
40 let bb: *u8 = sys_mmap(28)
41 var i: i64 = 0
42 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
43 sys_write(1, bb, k)
44 if logfd > 0 { sys_write(logfd, bb, k) }
45 return 0
46}
47func grow(logfd: i64, name: *u8, ok: i64) -> i64 {
48 if ok == 1 { gp(logfd, " PASS " as *u8) }
49 if ok != 1 { gp(logfd, " FAIL " as *u8) }
50 gp(logfd, name)
51 gp(logfd, "\n" as *u8)
52 return ok
53}
54func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
55
56// add a page (url == /wiki/<slug>.html ; title == url for brevity) to the store; returns rowid.
57func add_page(st: *NxWikiDocStore, url: *u8, body: *u8) -> i64 {
58 return nx_wiki_doc_store_add(st, url, slen(url), url, slen(url), body, slen(body))
59}
60
61// build a synthetic RegRegistry from parallel literal arrays. emitter "-" => manual-only.
62func mk_reg(reg: *RegRegistry, slugs: *i64, emits: *i64, files: *i64, roots: *i64, n: i64) -> i64 {
63 reg.n = n
64 reg.slug_p = sys_mmap(REG_MAX_PAGES * 8) as *i64
65 reg.emit_p = sys_mmap(REG_MAX_PAGES * 8) as *i64
66 reg.file_p = sys_mmap(REG_MAX_PAGES * 8) as *i64
67 reg.is_root = sys_mmap(REG_MAX_PAGES * 8) as *i64
68 reg.has_emit = sys_mmap(REG_MAX_PAGES * 8) as *i64
69 var i: i64 = 0
70 while i < n {
71 if i >= REG_MAX_PAGES { i = n }
72 if i < n {
73 reg.slug_p[i] = slugs[i]
74 reg.emit_p[i] = emits[i]
75 reg.file_p[i] = files[i]
76 reg.is_root[i] = roots[i]
77 var he: i64 = 1
78 if reg_eq(emits[i] as *u8, "-" as *u8) == 1 { he = 0 }
79 reg.has_emit[i] = he
80 }
81 i = i + 1
82 }
83 return 0
84}
85
86// allocate + zero/seed a RegPlan whose rows mirror the registry. emitter pages start RC_CLEAN (provisional,
87// as the live regen_ex seeds after a successful re-emit); manual-only pages start RC_MANUAL_ONLY.
88func mk_plan(plan: *RegPlan, reg: *RegRegistry) -> i64 {
89 plan.n = reg.n
90 plan.slug_p = sys_mmap(REG_MAX_PAGES * 8) as *i64
91 plan.emitted = sys_mmap(REG_MAX_PAGES * 8) as *i64
92 plan.guard = sys_mmap(REG_MAX_PAGES * 8) as *i64
93 plan.orphan_chk = sys_mmap(REG_MAX_PAGES * 8) as *i64
94 plan.would_pub = sys_mmap(REG_MAX_PAGES * 8) as *i64
95 plan.pushed = sys_mmap(REG_MAX_PAGES * 8) as *i64
96 var i: i64 = 0
97 while i < reg.n {
98 if i >= REG_MAX_PAGES { i = reg.n }
99 if i < reg.n {
100 plan.slug_p[i] = reg.slug_p[i]
101 plan.emitted[i] = 100
102 plan.guard[i] = 0
103 plan.would_pub[i] = WP_NO
104 plan.pushed[i] = 0
105 if reg.has_emit[i] == 1 { plan.orphan_chk[i] = RC_CLEAN }
106 else { plan.orphan_chk[i] = RC_MANUAL_ONLY }
107 }
108 i = i + 1
109 }
110 return 0
111}
112
113// find a plan row index by slug; -1 if absent.
114func plan_row(plan: *RegPlan, slug: *u8) -> i64 {
115 var i: i64 = 0
116 while i < plan.n {
117 if i >= REG_MAX_PAGES { i = plan.n }
118 if i < plan.n {
119 if reg_eq(plan.slug_p[i] as *u8, slug) == 1 { return i }
120 }
121 i = i + 1
122 }
123 return 0 - 1
124}
125
126// a hermetic /tmp archive store prefix so pg_decide's cite resolution + any vpub stay off the live store.
127// (the fixtures carry NO [[cite:]] tokens, so cite resolution is a no-op; this just guarantees isolation.)
128const GATE_PREFIX: *u8 = "/tmp/nx_regen_gate_store-"
129
130func main() -> i64 {
131 let logfd: i64 = sys_openat_append("knowledge/status/wiki_structure_regen_gate.log" as *u8, 0x1a4)
132 gp(logfd, "WIKI-STRUCTURE-REGEN GATE (A4b) re-publish-on-pulse: hermetic apply_flag=0 + planted-fault TEETH\n" as *u8)
133
134 var pass: i64 = 0
135
136 // shared registry shape used by T1/T3/T4: start(root,emit), charter(emit), leaf(emit), board(manual).
137 // (slug, emitter, file, is_root)
138 let slugs: *i64 = sys_mmap(8 * 8) as *i64
139 let emits: *i64 = sys_mmap(8 * 8) as *i64
140 let files: *i64 = sys_mmap(8 * 8) as *i64
141 let roots: *i64 = sys_mmap(8 * 8) as *i64
142 slugs[0] = "start" as *u8 as i64; emits[0] = "em_start" as *u8 as i64; files[0] = "start.html" as *u8 as i64; roots[0] = 1
143 slugs[1] = "charter" as *u8 as i64; emits[1] = "em_charter" as *u8 as i64; files[1] = "charter.html" as *u8 as i64; roots[1] = 0
144 slugs[2] = "leaf" as *u8 as i64; emits[2] = "em_leaf" as *u8 as i64; files[2] = "leaf.html" as *u8 as i64; roots[2] = 0
145 slugs[3] = "board" as *u8 as i64; emits[3] = "-" as *u8 as i64; files[3] = "-" as *u8 as i64; roots[3] = 0
146
147 // ===================================================================
148 // T1: CLEAN. would-be bodies all freshness-stamped + cross-linked so no orphan, no dead link.
149 // start(root) -> charter, leaf, board charter -> leaf, start leaf -> start board(manual) -> start
150 // (start is root => exempt from orphan; charter/leaf/board all have inbound.) NO dead links.
151 // ===================================================================
152 let s1: *NxWikiDocStore = sys_mmap(512) as *NxWikiDocStore
153 nx_wiki_doc_store_init(s1, 16, 4096, 4096, 262144)
154 add_page(s1, "/wiki/start.html" as *u8, "epoch=1 Start <a href=\"/wiki/charter.html\">c</a> <a href=\"/wiki/leaf.html\">l</a> <a href=\"/wiki/board.html\">b</a>" as *u8)
155 add_page(s1, "/wiki/charter.html" as *u8, "epoch=1 Charter <a href=\"/wiki/leaf.html\">l</a> <a href=\"/wiki/start.html\">s</a>" as *u8)
156 add_page(s1, "/wiki/leaf.html" as *u8, "epoch=1 Leaf <a href=\"/wiki/start.html\">s</a>" as *u8)
157 add_page(s1, "/wiki/board.html" as *u8, "epoch=1 Board (manual) <a href=\"/wiki/start.html\">s</a>" as *u8)
158
159 let r1: *RegRegistry = sys_mmap(128) as *RegRegistry
160 mk_reg(r1, slugs, emits, files, roots, 4)
161 let p1: *RegPlan = sys_mmap(128) as *RegPlan
162 mk_plan(p1, r1)
163 // no baselines needed for T1 (no drops). base arrays all zero.
164 let b1p: *i64 = sys_mmap(REG_MAX_PAGES * 8) as *i64
165 let b1l: *i64 = sys_mmap(REG_MAX_PAGES * 8) as *i64
166 // corpus = the 4 managed slugs
167 let cs: *i64 = sys_mmap(8 * 8) as *i64
168 let cl: *i64 = sys_mmap(8 * 8) as *i64
169 cs[0] = slugs[0]; cl[0] = slen(slugs[0] as *u8)
170 cs[1] = slugs[1]; cl[1] = slen(slugs[1] as *u8)
171 cs[2] = slugs[2]; cl[2] = slen(slugs[2] as *u8)
172 cs[3] = slugs[3]; cl[3] = slen(slugs[3] as *u8)
173
174 reg_decide(s1, r1, b1p, b1l, p1)
175 reg_guard_and_apply(r1, p1, s1, cs, cl, 4, GATE_PREFIX, 0, 777)
176
177 let i_start: i64 = plan_row(p1, "start" as *u8)
178 let i_chart: i64 = plan_row(p1, "charter" as *u8)
179 let i_leaf: i64 = plan_row(p1, "leaf" as *u8)
180 let i_board: i64 = plan_row(p1, "board" as *u8)
181 gp(logfd, "T1 start[chk=" as *u8); gp(logfd, rc_name(p1.orphan_chk[i_start])); gp(logfd, ",wp=" as *u8); gn(logfd, p1.would_pub[i_start])
182 gp(logfd, "] charter[chk=" as *u8); gp(logfd, rc_name(p1.orphan_chk[i_chart])); gp(logfd, ",g=" as *u8); gp(logfd, guard_name(p1.guard[i_chart])); gp(logfd, ",wp=" as *u8); gn(logfd, p1.would_pub[i_chart])
183 gp(logfd, "] leaf[chk=" as *u8); gp(logfd, rc_name(p1.orphan_chk[i_leaf])); gp(logfd, ",wp=" as *u8); gn(logfd, p1.would_pub[i_leaf])
184 gp(logfd, "] board[chk=" as *u8); gp(logfd, rc_name(p1.orphan_chk[i_board])); gp(logfd, ",wp=" as *u8); gn(logfd, p1.would_pub[i_board]); gp(logfd, "]\n" as *u8)
185 var t1: i64 = 1
186 if p1.orphan_chk[i_start] != RC_CLEAN { t1 = 0 }
187 if p1.would_pub[i_start] != WP_YES { t1 = 0 }
188 if p1.orphan_chk[i_chart] != RC_CLEAN { t1 = 0 }
189 if p1.would_pub[i_chart] != WP_YES { t1 = 0 }
190 if p1.orphan_chk[i_leaf] != RC_CLEAN { t1 = 0 }
191 if p1.would_pub[i_leaf] != WP_YES { t1 = 0 }
192 if p1.orphan_chk[i_board] != RC_MANUAL_ONLY { t1 = 0 }
193 if p1.would_pub[i_board] != WP_NO { t1 = 0 }
194 pass = pass + grow(logfd, "T1 clean -> emitter pages CLEAN+publish, manual page MANUAL_ONLY+no-publish\x00" as *u8, t1)
195
196 // ===================================================================
197 // T2: PLANTED ORPHAN via DROPPED LINK. Registry: start(root), hub(emit), leaf(emit).
198 // BASELINE: hub linked leaf (so leaf had inbound). WOULD-BE: hub's re-emit DROPS the leaf link.
199 // start(root) -> hub only. => leaf now has 0 inbound -> ORPHAN; the OFFENDING page is hub.
200 // Expect: hub -> RC_REFUSE_ORPHAN + wp=NO (and leaf, being the orphan, also wp=NO).
201 // ===================================================================
202 let slugs2: *i64 = sys_mmap(8 * 8) as *i64
203 let emits2: *i64 = sys_mmap(8 * 8) as *i64
204 let files2: *i64 = sys_mmap(8 * 8) as *i64
205 let roots2: *i64 = sys_mmap(8 * 8) as *i64
206 slugs2[0] = "start" as *u8 as i64; emits2[0] = "em_start" as *u8 as i64; files2[0] = "start.html" as *u8 as i64; roots2[0] = 1
207 slugs2[1] = "hub" as *u8 as i64; emits2[1] = "em_hub" as *u8 as i64; files2[1] = "hub.html" as *u8 as i64; roots2[1] = 0
208 slugs2[2] = "leaf" as *u8 as i64; emits2[2] = "em_leaf" as *u8 as i64; files2[2] = "leaf.html" as *u8 as i64; roots2[2] = 0
209
210 let s2: *NxWikiDocStore = sys_mmap(512) as *NxWikiDocStore
211 nx_wiki_doc_store_init(s2, 16, 4096, 4096, 262144)
212 // WOULD-BE store: hub NO LONGER links leaf.
213 add_page(s2, "/wiki/start.html" as *u8, "epoch=1 Start <a href=\"/wiki/hub.html\">h</a>" as *u8)
214 add_page(s2, "/wiki/hub.html" as *u8, "epoch=1 Hub now links only <a href=\"/wiki/start.html\">s</a>" as *u8)
215 add_page(s2, "/wiki/leaf.html" as *u8, "epoch=1 Leaf links <a href=\"/wiki/start.html\">s</a>" as *u8)
216
217 let r2: *RegRegistry = sys_mmap(128) as *RegRegistry
218 mk_reg(r2, slugs2, emits2, files2, roots2, 3)
219 let p2: *RegPlan = sys_mmap(128) as *RegPlan
220 mk_plan(p2, r2)
221 // BASELINE bodies: hub USED TO link leaf. (only hub's baseline matters for the drop-diff.)
222 let b2p: *i64 = sys_mmap(REG_MAX_PAGES * 8) as *i64
223 let b2l: *i64 = sys_mmap(REG_MAX_PAGES * 8) as *i64
224 let i2_hub_pre: i64 = 1 // hub is registry row 1
225 let hub_baseline: *u8 = "epoch=1 Hub used to link <a href=\"/wiki/leaf.html\">l</a> and <a href=\"/wiki/start.html\">s</a>" as *u8
226 b2p[i2_hub_pre] = hub_baseline as i64
227 b2l[i2_hub_pre] = slen(hub_baseline)
228
229 let cs2: *i64 = sys_mmap(8 * 8) as *i64
230 let cl2: *i64 = sys_mmap(8 * 8) as *i64
231 cs2[0] = slugs2[0]; cl2[0] = slen(slugs2[0] as *u8)
232 cs2[1] = slugs2[1]; cl2[1] = slen(slugs2[1] as *u8)
233 cs2[2] = slugs2[2]; cl2[2] = slen(slugs2[2] as *u8)
234
235 reg_decide(s2, r2, b2p, b2l, p2)
236 reg_guard_and_apply(r2, p2, s2, cs2, cl2, 3, GATE_PREFIX, 0, 777)
237
238 let i2_hub: i64 = plan_row(p2, "hub" as *u8)
239 let i2_leaf: i64 = plan_row(p2, "leaf" as *u8)
240 gp(logfd, "T2 hub[chk=" as *u8); gp(logfd, rc_name(p2.orphan_chk[i2_hub])); gp(logfd, ",wp=" as *u8); gn(logfd, p2.would_pub[i2_hub])
241 gp(logfd, "] leaf[chk=" as *u8); gp(logfd, rc_name(p2.orphan_chk[i2_leaf])); gp(logfd, ",wp=" as *u8); gn(logfd, p2.would_pub[i2_leaf]); gp(logfd, "]\n" as *u8)
242 var t2: i64 = 1
243 if p2.orphan_chk[i2_hub] != RC_REFUSE_ORPHAN { t2 = 0 } // the OFFENDING change is flagged
244 if p2.would_pub[i2_hub] != WP_NO { t2 = 0 }
245 if p2.would_pub[i2_leaf] != WP_NO { t2 = 0 } // the orphaned page is also held back
246 pass = pass + grow(logfd, "T2 dropped-link orphan -> offending page REFUSE_ORPHAN + no-publish (TEETH)\x00" as *u8, t2)
247
248 // ===================================================================
249 // T3: PLANTED GUARD-FAIL. Registry: start(root), charter(emit), leaf(emit).
250 // charter's would-be body has NO "epoch=" stamp -> pg_decide REJECT_NO_FRESHNESS -> skipped.
251 // (graph is otherwise clean so the orphan check passes; the GUARD is what refuses charter.)
252 // ===================================================================
253 let s3: *NxWikiDocStore = sys_mmap(512) as *NxWikiDocStore
254 nx_wiki_doc_store_init(s3, 16, 4096, 4096, 262144)
255 add_page(s3, "/wiki/start.html" as *u8, "epoch=1 Start <a href=\"/wiki/charter.html\">c</a> <a href=\"/wiki/leaf.html\">l</a>" as *u8)
256 add_page(s3, "/wiki/charter.html" as *u8, "NO FRESHNESS STAMP HERE Charter <a href=\"/wiki/leaf.html\">l</a> <a href=\"/wiki/start.html\">s</a>" as *u8)
257 add_page(s3, "/wiki/leaf.html" as *u8, "epoch=1 Leaf <a href=\"/wiki/start.html\">s</a>" as *u8)
258
259 let slugs3: *i64 = sys_mmap(8 * 8) as *i64
260 let emits3: *i64 = sys_mmap(8 * 8) as *i64
261 let files3: *i64 = sys_mmap(8 * 8) as *i64
262 let roots3: *i64 = sys_mmap(8 * 8) as *i64
263 slugs3[0] = "start" as *u8 as i64; emits3[0] = "em_start" as *u8 as i64; files3[0] = "start.html" as *u8 as i64; roots3[0] = 1
264 slugs3[1] = "charter" as *u8 as i64; emits3[1] = "em_charter" as *u8 as i64; files3[1] = "charter.html" as *u8 as i64; roots3[1] = 0
265 slugs3[2] = "leaf" as *u8 as i64; emits3[2] = "em_leaf" as *u8 as i64; files3[2] = "leaf.html" as *u8 as i64; roots3[2] = 0
266
267 let r3: *RegRegistry = sys_mmap(128) as *RegRegistry
268 mk_reg(r3, slugs3, emits3, files3, roots3, 3)
269 let p3: *RegPlan = sys_mmap(128) as *RegPlan
270 mk_plan(p3, r3)
271 let b3p: *i64 = sys_mmap(REG_MAX_PAGES * 8) as *i64
272 let b3l: *i64 = sys_mmap(REG_MAX_PAGES * 8) as *i64
273 let cs3: *i64 = sys_mmap(8 * 8) as *i64
274 let cl3: *i64 = sys_mmap(8 * 8) as *i64
275 cs3[0] = slugs3[0]; cl3[0] = slen(slugs3[0] as *u8)
276 cs3[1] = slugs3[1]; cl3[1] = slen(slugs3[1] as *u8)
277 cs3[2] = slugs3[2]; cl3[2] = slen(slugs3[2] as *u8)
278
279 reg_decide(s3, r3, b3p, b3l, p3)
280 reg_guard_and_apply(r3, p3, s3, cs3, cl3, 3, GATE_PREFIX, 0, 777)
281
282 let i3_chart: i64 = plan_row(p3, "charter" as *u8)
283 let i3_leaf: i64 = plan_row(p3, "leaf" as *u8)
284 gp(logfd, "T3 charter[chk=" as *u8); gp(logfd, rc_name(p3.orphan_chk[i3_chart])); gp(logfd, ",guard=" as *u8); gp(logfd, guard_name(p3.guard[i3_chart])); gp(logfd, ",wp=" as *u8); gn(logfd, p3.would_pub[i3_chart])
285 gp(logfd, "] leaf[guard=" as *u8); gp(logfd, guard_name(p3.guard[i3_leaf])); gp(logfd, ",wp=" as *u8); gn(logfd, p3.would_pub[i3_leaf]); gp(logfd, "]\n" as *u8)
286 var t3: i64 = 1
287 if p3.guard[i3_chart] != PG_REJECT_NO_FRESHNESS { t3 = 0 } // EXACT reason
288 if p3.would_pub[i3_chart] != WP_NO { t3 = 0 } // skipped
289 if p3.guard[i3_leaf] != PG_ALLOW { t3 = 0 } // leaf still fine (only charter fails)
290 if p3.would_pub[i3_leaf] != WP_YES { t3 = 0 }
291 pass = pass + grow(logfd, "T3 missing-freshness -> REJECT_NO_FRESHNESS + skipped, others unaffected (TEETH)\x00" as *u8, t3)
292
293 // ===================================================================
294 // T4: PUSH SAFETY. apply_flag=0 in EVERY call above -> no plan row across T1/T2/T3 has pushed==1.
295 // ===================================================================
296 var any_pushed: i64 = 0
297 var pi: i64 = 0
298 while pi < p1.n { if pi < REG_MAX_PAGES { if p1.pushed[pi] == 1 { any_pushed = 1 } } pi = pi + 1 }
299 pi = 0
300 while pi < p2.n { if pi < REG_MAX_PAGES { if p2.pushed[pi] == 1 { any_pushed = 1 } } pi = pi + 1 }
301 pi = 0
302 while pi < p3.n { if pi < REG_MAX_PAGES { if p3.pushed[pi] == 1 { any_pushed = 1 } } pi = pi + 1 }
303 gp(logfd, "T4 any_pushed_across_run=" as *u8); gn(logfd, any_pushed); gp(logfd, " (apply_flag=0 throughout)\n" as *u8)
304 var t4: i64 = 0
305 if any_pushed == 0 { t4 = 1 }
306 pass = pass + grow(logfd, "T4 apply_flag=0 -> push_invoked NEVER set (push_invoked=0) (TEETH)\x00" as *u8, t4)
307
308 // ===================================================================
309 if pass == 4 {
310 gp(logfd, "WIKI-STRUCTURE-REGEN GATE GREEN 4/4 (refuses to create orphans; guard-skips; dry-run never pushes)\n" as *u8)
311 sys_exit(0)
312 }
313 gp(logfd, "WIKI-STRUCTURE-REGEN GATE RED pass=" as *u8); gn(logfd, pass); gp(logfd, "/4\n" as *u8)
314 sys_exit(1)
315 return 1
316}