code wiki / _hdl_build / nx_pub_plane_gate.nx
nx_pub_plane_gate.nx source
↩ module page · 241 lines · 15701 B
1// nx_pub_plane_gate.nx -- the VERIFIER for the publishing plane (nx_pub_lib / nx_pub_plane).
2//
3// Proves the two properties the operator actually asked for:
4// 1. every discovery artifact (sitemap.xml, robots.txt, llms.txt) is DERIVED from the per-site
5// registry -- so what a crawler sees and what the team believes are the same object, and no
6// page can be "put somewhere" and quietly become site truth;
7// 2. the publish GATEWAY is FAIL-CLOSED -- an unregistered path is refused, and an unreadable
8// registry refuses everything rather than waving it through;
9// 3. adoption walks the WHOLE docroot TREE (debt 1785614931: the one-level walk skipped every
10// subdirectory, so a nested page could never be registered and the gateway refused code/
11// pages forever) -- nested pages enter under their FULL relpath, idempotently, and a
12// beyond-depth tree REFUSES LOUDLY rather than truncating.
13//
14// NON-VACUITY IS STRUCTURAL HERE, not a promise: the exclusion teeth are gv_bite cells, which count
15// only if they FIRE on the crafted-bad input AND stay SILENT on the crafted-good one. A sitemap
16// filter that excluded everything, or nothing, fails both bites.
17// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
18import "nx_pub_lib.nx"
19import "nx_gate_verdict.nx"
20import "nx_syscalls.nx"
21
22const PG_PATHCAP: i64 = 1024
23const PG_BUF: i64 = 65536
24const PG_SPAN: i64 = 16
25const PG_DIRMODE: i64 = 493
26const PG_ARG_A: i64 = 1
27
28func pg_fixfile(dir: *u8, name: *u8, body: *u8) -> i64 {
29 let p: *u8 = sys_mmap(PG_PATHCAP)
30 pl_join(p, dir, name)
31 let rc: i64 = pl_wfile(p, body, pl_vlen(body))
32 sys_munmap(p, PG_PATHCAP)
33 return rc
34}
35func pg_has(path: *u8, needle: *u8) -> i64 {
36 let b: *u8 = sys_mmap(PG_BUF)
37 let n: i64 = pl_rfile(path, b, PG_BUF)
38 if n <= 0 { sys_munmap(b, PG_BUF); return 0 }
39 let r: i64 = pl_find(b, n, needle)
40 sys_munmap(b, PG_BUF)
41 return r
42}
43
44func main(argc: i64, argv: *i64) -> i64 {
45 var tmpdir: *u8 = "knowledge/pubgate" as *u8
46 if argc > PG_ARG_A { tmpdir = argv[PG_ARG_A] as *u8 }
47 sys_mkdir(tmpdir, PG_DIRMODE)
48 // SETUP MUST FAIL LOUD. The first run of this gate reported 5/24 with a plausible-looking
49 // transcript because its scratch directory did not exist: every fixture write silently failed
50 // and the exclusion teeth then "passed" on an empty sitemap. A gate that cannot build its
51 // fixtures has proved NOTHING, so it must refuse to emit checks at all rather than grade a void.
52 if pg_fixfile(tmpdir, "_setup.probe" as *u8, "ok" as *u8) != 0 {
53 pl_werr("PUB-GATE-SETUP-FAIL scratch dir not writable: " as *u8)
54 pl_werr(tmpdir)
55 pl_werr(" -- refusing to grade a void\n" as *u8)
56 sys_exit(1)
57 return 1
58 }
59 let ctr: *i64 = gv_ctr()
60 gv_head("nx_pub_plane gate -- registry-DERIVED discovery artifacts, canonical URLs, and a fail-closed publish gateway" as *u8)
61 // fixtures on disk: present pages, one deliberately ABSENT (ghost), one with an XML metacharacter
62 pg_fixfile(tmpdir, "index.html" as *u8, "<h1>home</h1>" as *u8)
63 pg_fixfile(tmpdir, "a.html" as *u8, "<h1>alpha</h1>" as *u8)
64 pg_fixfile(tmpdir, "b.html" as *u8, "<h1>bee</h1>" as *u8)
65 pg_fixfile(tmpdir, "old.html" as *u8, "<h1>retired</h1>" as *u8)
66 pg_fixfile(tmpdir, "style.css" as *u8, "body{}" as *u8)
67 pg_fixfile(tmpdir, "old.html.prev" as *u8, "<h1>a rollback artifact</h1>" as *u8)
68 // DELIBERATELY ABSENT from the registry below: this is the orphan case -- a page someone dropped
69 // into the docroot. 54 of these were measured live on nishifamily.com. Adoption must register it
70 // WITHOUT publishing it.
71 pg_fixfile(tmpdir, "stray.html" as *u8, "<h1>someone just dropped this here</h1>" as *u8)
72 pg_fixfile(tmpdir, "r&d.html" as *u8, "<h1>r and d</h1>" as *u8)
73 let subd: *u8 = sys_mmap(PG_PATHCAP)
74 pl_join(subd, tmpdir, "contact" as *u8)
75 sys_mkdir(subd, PG_DIRMODE)
76 pg_fixfile(subd, "index.html" as *u8, "<h1>contact</h1>" as *u8)
77 // NESTED, UNREGISTERED pages -- the 2026-08-01 defect (debt 1785614931): pre-fix, pl_adopt
78 // walked ONE level, so these could never enter the registry and the fail-closed gateway
79 // refused them FOREVER while they were live and serving. Adoption must find BOTH depths.
80 let codd: *u8 = sys_mmap(PG_PATHCAP)
81 pl_join(codd, tmpdir, "code" as *u8)
82 sys_mkdir(codd, PG_DIRMODE)
83 pg_fixfile(codd, "nested.html" as *u8, "<h1>a page in a subdirectory</h1>" as *u8)
84 let codd2: *u8 = sys_mmap(PG_PATHCAP)
85 pl_join(codd2, codd, "deep" as *u8)
86 sys_mkdir(codd2, PG_DIRMODE)
87 pg_fixfile(codd2, "leaf.html" as *u8, "<h1>two directory levels down</h1>" as *u8)
88 // the registry: one row per status in the vocabulary, so every exclusion rule is exercised
89 let reg: *u8 = sys_mmap(PG_BUF)
90 var o: i64 = 0
91 o = pl_cat(reg, o, "1\tHome\tops\tlive\tFamily\tindex.html\tthe front door\n" as *u8)
92 o = pl_cat(reg, o, "2\tAlpha\tops\tlive\tFamily\ta.html\tfirst page\n" as *u8)
93 o = pl_cat(reg, o, "3\tGhost\tops\tlive\tFamily\tghost.html\tregistered but never written\n" as *u8)
94 o = pl_cat(reg, o, "4\tBee\tops\tdraft\tWork\tb.html\tnot published yet\n" as *u8)
95 o = pl_cat(reg, o, "5\tStyle\tops\tasset\tWork\tstyle.css\tserved, never indexed\n" as *u8)
96 o = pl_cat(reg, o, "6\tOld\tops\twithdrawn\tWork\told.html\tretired page\n" as *u8)
97 o = pl_cat(reg, o, "7\tAmp\tops\tlive\tWork\tr&d.html\tXML metacharacter in the path\n" as *u8)
98 o = pl_cat(reg, o, "8\tContact\tops\tlive\tWork\tcontact/index.html\ta DIRECTORY index, not a page named index\n" as *u8)
99 let prefix: *u8 = sys_mmap(PG_PATHCAP)
100 // the registry lives OUTSIDE the docroot -- as in production (knowledge/store/, not sites/).
101 // Putting it inside would make the adopt-idempotence tooth lie: adopt would keep discovering
102 // the store files its own previous run had just written.
103 var po: i64 = pl_cat(prefix, 0, tmpdir)
104 po = pl_cat(prefix, po, "-store-" as *u8)
105 prefix[po] = 0 as u8
106 let seeded: i64 = sts_seed(prefix, reg, o)
107 gv_check("T0 registry seeded (8 rows)" as *u8, (seeded == 8) as i64, ctr)
108 // ---- sitemap ----
109 let smp: *u8 = sys_mmap(PG_PATHCAP)
110 pl_join(smp, tmpdir, "sitemap.xml" as *u8)
111 let flags: *i64 = sys_mmap(PG_SPAN) as *i64
112 let nurl: i64 = pl_sitemap(tmpdir, prefix, "https://x.test" as *u8, smp, flags)
113 gv_check("T1 sitemap emits exactly the 4 live+present rows" as *u8, (nurl == 4) as i64, ctr)
114 gv_check("T1b nested index -> the DIRECTORY URL, never /contact/index" as *u8, pg_has(smp, "<loc>https://x.test/contact/</loc>" as *u8), ctr)
115 gv_check("T1c no /contact/index URL was emitted" as *u8, (1 - pg_has(smp, "/contact/index<" as *u8)) as i64, ctr)
116 gv_check("T2 index.html -> the bare canonical root URL" as *u8, pg_has(smp, "<loc>https://x.test/</loc>" as *u8), ctr)
117 gv_check("T3 a.html -> the clean extensionless URL" as *u8, pg_has(smp, "<loc>https://x.test/a</loc>" as *u8), ctr)
118 gv_check("T4 & in a path is XML-ESCAPED, not dropped or dotted" as *u8, pg_has(smp, "<loc>https://x.test/r&d</loc>" as *u8), ctr)
119 gv_check("T5 lastmod is a real machine date, not self-reported" as *u8, pg_has(smp, "<lastmod>20" as *u8), ctr)
120 gv_check("T6 draft page EXCLUDED" as *u8, (1 - pg_has(smp, "/b</loc>" as *u8)) as i64, ctr)
121 gv_check("T7 asset EXCLUDED (served, never indexed)" as *u8, (1 - pg_has(smp, "style.css" as *u8)) as i64, ctr)
122 gv_check("T8 honest load reports declared==loaded" as *u8, (flags[0] == flags[1]) as i64, ctr)
123 // both-polarity teeth: the exclusions must fire on the bad case AND stay silent on the good one
124 gv_bite("T9 BITE absent-file excluded / present-file included" as *u8,
125 (1 - pg_has(smp, "ghost" as *u8)) as i64,
126 (1 - pg_has(smp, "/a</loc>" as *u8)) as i64, ctr)
127 gv_bite("T10 BITE withdrawn excluded / live included" as *u8,
128 (1 - pg_has(smp, "/old</loc>" as *u8)) as i64,
129 (1 - pg_has(smp, "/r&d</loc>" as *u8)) as i64, ctr)
130 // ---- robots ----
131 let rbp: *u8 = sys_mmap(PG_PATHCAP)
132 pl_join(rbp, tmpdir, "robots.txt" as *u8)
133 let dis: *u8 = "private/,preview/" as *u8
134 let nd: i64 = pl_robots("https://x.test" as *u8, dis, 0, pl_vlen(dis), rbp)
135 gv_check("T11 robots emits one Disallow per configured prefix" as *u8, (nd == 2) as i64, ctr)
136 gv_check("T12 robots Disallow: /private/" as *u8, pg_has(rbp, "Disallow: /private/" as *u8), ctr)
137 gv_check("T13 robots points at the sitemap" as *u8, pg_has(rbp, "Sitemap: https://x.test/sitemap.xml" as *u8), ctr)
138 // ---- llms.txt ----
139 let lmp: *u8 = sys_mmap(PG_PATHCAP)
140 pl_join(lmp, tmpdir, "llms.txt" as *u8)
141 let nlk: i64 = pl_llms(tmpdir, prefix, "https://x.test" as *u8, "Test Site" as *u8, lmp)
142 gv_check("T14 llms.txt links exactly the live+present set" as *u8, (nlk == 4) as i64, ctr)
143 gv_check("T15 llms.txt carries the H1 title" as *u8, pg_has(lmp, "# Test Site" as *u8), ctr)
144 gv_check("T16 llms.txt groups by registry section" as *u8, pg_has(lmp, "## Family" as *u8), ctr)
145 gv_check("T17 llms.txt link carries title + note" as *u8, pg_has(lmp, "- [Alpha](https://x.test/a): first page" as *u8), ctr)
146 // ---- the gateway ----
147 let g_live: i64 = pl_check(prefix, "a.html" as *u8)
148 let g_asset: i64 = pl_check(prefix, "style.css" as *u8)
149 let g_draft: i64 = pl_check(prefix, "b.html" as *u8)
150 let g_withdrawn: i64 = pl_check(prefix, "old.html" as *u8)
151 let g_unreg: i64 = pl_check(prefix, "whatever-someone-dropped.html" as *u8)
152 gv_check("T18 gateway ADMITS a live registered page" as *u8, (g_live == 0) as i64, ctr)
153 gv_check("T19 gateway ADMITS an asset" as *u8, (g_asset == 0) as i64, ctr)
154 gv_check("T20 gateway ADMITS a draft (publishable, just not indexable)" as *u8, (g_draft == 0) as i64, ctr)
155 gv_check("T21 gateway REFUSES a withdrawn page" as *u8, (g_withdrawn == 3) as i64, ctr)
156 gv_bite("T22 BITE gateway refuses UNREGISTERED / admits registered" as *u8,
157 (g_unreg == 3) as i64,
158 (g_live != 0) as i64, ctr)
159 // fail-CLOSED: a registry that cannot be read must refuse everything, never wave it through
160 let nop: *u8 = sys_mmap(PG_PATHCAP)
161 var xo: i64 = pl_cat(nop, 0, tmpdir)
162 xo = pl_cat(nop, xo, "/plgate-no-such-plane-" as *u8)
163 nop[xo] = 0 as u8
164 gv_check("T23 unreadable registry FAILS CLOSED (refuses)" as *u8, (pl_check(nop, "a.html" as *u8) == 3) as i64, ctr)
165 // ---- adopt: the migration step. It must be IDEMPOTENT, or re-running it duplicates the site.
166 let ac1: *i64 = sys_mmap(64) as *i64
167 let ac2: *i64 = sys_mmap(64) as *i64
168 let add1: i64 = pl_adopt(tmpdir, prefix, ac1)
169 let add2: i64 = pl_adopt(tmpdir, prefix, ac2)
170 gv_check("T24 adopt registers the unregistered docroot entries" as *u8, (add1 > 0) as i64, ctr)
171 gv_check("T25 adopt lands pages as DRAFT (registered, never auto-indexed)" as *u8, (ac1[0] > 0) as i64, ctr)
172 gv_check("T26 adopt classifies backup/temp artifacts as debris" as *u8, (ac1[2] > 0) as i64, ctr)
173 // THE property that makes adoption safe: it changes what the registry KNOWS, never what the
174 // world SEES. Re-emitting after adoption must produce the same public URL set.
175 let nurl2: i64 = pl_sitemap(tmpdir, prefix, "https://x.test" as *u8, smp, flags)
176 gv_check("T25b adoption is publicly INERT: sitemap URL count unchanged" as *u8, (nurl2 == nurl) as i64, ctr)
177 gv_bite("T27 BITE adopt is IDEMPOTENT (2nd run adds nothing / 1st run did add)" as *u8,
178 (add2 == 0) as i64,
179 (add1 == 0) as i64, ctr)
180 gv_check("T28 an adopted path now passes the gateway" as *u8, (pl_check(prefix, "sitemap.xml" as *u8) == 0) as i64, ctr)
181 // ---- RECURSIVE adoption (the fix for debt 1785614931): full relpaths, gateway-proven.
182 // pl_check here IS the gateway: the driver's `check` verb prints and exits with exactly
183 // this function's verdict, so ==0 is the same fact as `nx_pub_plane check` exiting 0.
184 gv_check("T28b adopted NESTED page admits at its FULL relpath (code/nested.html)" as *u8,
185 (pl_check(prefix, "code/nested.html" as *u8) == 0) as i64, ctr)
186 gv_check("T28c adoption reaches depth 2 (code/deep/leaf.html admits)" as *u8,
187 (pl_check(prefix, "code/deep/leaf.html" as *u8) == 0) as i64, ctr)
188 gv_check("T28d nested page registered by PATH, not basename (bare nested.html still refused)" as *u8,
189 (pl_check(prefix, "nested.html" as *u8) == 3) as i64, ctr)
190 gv_bite("T28e BITE gateway still refuses an UNREGISTERED nested path / admits the ADOPTED one" as *u8,
191 (pl_check(prefix, "code/never-dropped-here.html" as *u8) == 3) as i64,
192 (pl_check(prefix, "code/nested.html" as *u8) != 0) as i64, ctr)
193 // ---- the read-modify-write safety tooth: adopt rewrites the WHOLE plane, so it must REFUSE a
194 // plane whose declared row count exceeds what it can actually reach. Craft the bad input by
195 // superseding q:n with a count no reader can satisfy, then prove the healthy plane still works.
196 let w: *i64 = ss_begin_cap(STS_WCAP)
197 ss_add(w, STS_KIND_LIVE, "q:n" as *u8, "9999" as *u8, 4)
198 ss_commit(prefix, w, ss_next_segid(prefix))
199 let ac3: *i64 = sys_mmap(64) as *i64
200 let lossy: i64 = pl_adopt(tmpdir, prefix, ac3)
201 gv_bite("T29 BITE adopt REFUSES a lossy plane / accepts a healthy one" as *u8,
202 (lossy == 0 - 2) as i64,
203 (add2 == 0 - 2) as i64, ctr)
204 gv_check("T30 the refusal committed NOTHING (plane still readable)" as *u8, (pl_check(prefix, "a.html" as *u8) == 0) as i64, ctr)
205 // ---- depth bounding: REFUSE, never truncate. Its OWN docroot and its OWN registry plane,
206 // so the refusal cannot disturb the main fixture's teeth -- and "NOTHING COMMITTED" is
207 // provable: had adopt truncated instead of refused, the in-range top.html WOULD have been
208 // registered. (The deep plane is never seeded, so a regression that commits leaves a row
209 // behind and T31b stays red until it is cleaned -- a deliberate ratchet.)
210 let deepd: *u8 = sys_mmap(PG_PATHCAP)
211 var ddo: i64 = pl_cat(deepd, 0, tmpdir)
212 ddo = pl_cat(deepd, ddo, "-deep" as *u8)
213 deepd[ddo] = 0 as u8
214 sys_mkdir(deepd, PG_DIRMODE)
215 pg_fixfile(deepd, "top.html" as *u8, "<h1>within depth</h1>" as *u8)
216 let dchain: *u8 = sys_mmap(PG_PATHCAP)
217 var dco: i64 = pl_cat(dchain, 0, deepd)
218 dchain[dco] = 0 as u8
219 var lvl: i64 = 0
220 while lvl < PL_MAXDEPTH + 1 {
221 dco = pl_cat(dchain, dco, "/d" as *u8)
222 dchain[dco] = 0 as u8
223 sys_mkdir(dchain, PG_DIRMODE)
224 lvl = lvl + 1
225 }
226 pg_fixfile(dchain, "lost.html" as *u8, "<h1>beyond PL_MAXDEPTH -- truncation would drop me silently</h1>" as *u8)
227 let dpfx: *u8 = sys_mmap(PG_PATHCAP)
228 var dpo: i64 = pl_cat(dpfx, 0, tmpdir)
229 dpo = pl_cat(dpfx, dpo, "-deepstore-" as *u8)
230 dpfx[dpo] = 0 as u8
231 let ac4: *i64 = sys_mmap(64) as *i64
232 let deep_rc: i64 = pl_adopt(deepd, dpfx, ac4)
233 gv_bite("T31 BITE beyond-depth tree REFUSED LOUDLY (-3, cap named on stderr) / within-depth tree adopted" as *u8,
234 (deep_rc == 0 - 3) as i64,
235 (add1 < 0) as i64, ctr)
236 gv_check("T31b depth refusal committed NOTHING -- even in-range top.html stayed unregistered" as *u8,
237 (pl_check(dpfx, "top.html" as *u8) == 3) as i64, ctr)
238 let rc: i64 = gv_verdict("PUB-PLANE-GATE" as *u8, ctr, "every discovery artifact is derived from the registry, the gateway is fail-closed, and adoption walks the whole tree -- bounded, refusing over truncating" as *u8)
239 sys_exit(rc)
240 return rc
241}