code wiki / _hdl_build / nx_pub_plane_gate.nx

nx_pub_plane_gate.nx source

↩ module page · 285 lines · 18632 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(64) as *i64 // 3 i64 flags from sts_load_honest; 16B overruns 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&amp;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&amp;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 // ---- promote: the draft->live owner pass (2026-08-12). Adoption parks pages as DRAFT and the 194 // plane shipped no way to lift one; pl_promote is that verb. Both polarities: the listed draft 195 // ENTERS the sitemap, the unlisted stays out, a bogus list row is COUNTED not dropped, and a 196 // second run lifts nothing (idempotent). 197 let plst: *u8 = sys_mmap(PG_PATHCAP) 198 pl_join(plst, tmpdir, "promote.list" as *u8) 199 pg_fixfile(tmpdir, "promote.list" as *u8, "b.html\n# a comment row\nno-such-page.html\n" as *u8) 200 let pc1: *i64 = sys_mmap(64) as *i64 201 let pr1: i64 = pl_promote(prefix, plst, pc1) 202 gv_check("T28f promote lifts exactly the listed draft row" as *u8, (pr1 == 1) as i64, ctr) 203 gv_check("T28g promote COUNTS the unmatched list row, never drops it" as *u8, (pc1[2] == 1) as i64, ctr) 204 let nurl3: i64 = pl_sitemap(tmpdir, prefix, "https://x.test" as *u8, smp, flags) 205 gv_bite("T28h BITE promoted page ENTERS the sitemap / it was excluded while draft" as *u8, 206 pg_has(smp, "/b</loc>" as *u8), 207 (nurl3 == nurl) as i64, ctr) 208 let pc2: *i64 = sys_mmap(64) as *i64 209 let pr2: i64 = pl_promote(prefix, plst, pc2) 210 gv_bite("T28i BITE promote is IDEMPOTENT (2nd run lifts nothing / 1st did lift)" as *u8, 211 (pr2 == 0) as i64, 212 (pr1 == 0) as i64, ctr) 213 // ---- sectionize: mechanical taxonomy for adopted rows (2026-08-12). Promote two adopted pages 214 // live first so the llms grouping exposes the assigned sections, both depths. 215 pg_fixfile(tmpdir, "promote2.list" as *u8, "stray.html\ncode/nested.html\n" as *u8) 216 let plst2: *u8 = sys_mmap(PG_PATHCAP) 217 pl_join(plst2, tmpdir, "promote2.list" as *u8) 218 let pc4: *i64 = sys_mmap(64) as *i64 219 let pr4: i64 = pl_promote(prefix, plst2, pc4) 220 let sc1: *i64 = sys_mmap(64) as *i64 221 let sz1: i64 = pl_sectionize(prefix, sc1) 222 gv_check("T28j sectionize rewrites the Unsorted rows" as *u8, (sz1 > 0) as i64, ctr) 223 let nlk2: i64 = pl_llms(tmpdir, prefix, "https://x.test" as *u8, "Test Site" as *u8, lmp) 224 gv_check("T28k dir-derived section groups the nested page (## code)" as *u8, pg_has(lmp, "## code" as *u8), ctr) 225 gv_check("T28l top-level adopted page lands in ## Pages" as *u8, pg_has(lmp, "## Pages" as *u8), ctr) 226 let sc2: *i64 = sys_mmap(64) as *i64 227 let sz2: i64 = pl_sectionize(prefix, sc2) 228 gv_bite("T28m BITE sectionize is IDEMPOTENT (2nd run rewrites nothing / 1st did)" as *u8, 229 (sz2 == 0) as i64, 230 (sz1 == 0) as i64, ctr) 231 // ---- the read-modify-write safety tooth: adopt rewrites the WHOLE plane, so it must REFUSE a 232 // plane whose declared row count exceeds what it can actually reach. Craft the bad input by 233 // superseding q:n with a count no reader can satisfy, then prove the healthy plane still works. 234 let w: *i64 = ss_begin_cap(STS_WCAP) 235 ss_add(w, STS_KIND_LIVE, "q:n" as *u8, "9999" as *u8, 4) 236 ss_commit(prefix, w, ss_next_segid(prefix)) 237 let ac3: *i64 = sys_mmap(64) as *i64 238 let lossy: i64 = pl_adopt(tmpdir, prefix, ac3) 239 gv_bite("T29 BITE adopt REFUSES a lossy plane / accepts a healthy one" as *u8, 240 (lossy == 0 - 2) as i64, 241 (add2 == 0 - 2) as i64, ctr) 242 // promote must obey the SAME lossy-load law as adopt: never rewrite over a short read 243 let pc3: *i64 = sys_mmap(64) as *i64 244 let pr3: i64 = pl_promote(prefix, plst, pc3) 245 gv_bite("T29b BITE promote REFUSES the lossy plane / accepted the healthy one" as *u8, 246 (pr3 == 0 - 2) as i64, 247 (pr1 <= 0) as i64, ctr) 248 gv_check("T30 the refusal committed NOTHING (plane still readable)" as *u8, (pl_check(prefix, "a.html" as *u8) == 0) as i64, ctr) 249 // ---- depth bounding: REFUSE, never truncate. Its OWN docroot and its OWN registry plane, 250 // so the refusal cannot disturb the main fixture's teeth -- and "NOTHING COMMITTED" is 251 // provable: had adopt truncated instead of refused, the in-range top.html WOULD have been 252 // registered. (The deep plane is never seeded, so a regression that commits leaves a row 253 // behind and T31b stays red until it is cleaned -- a deliberate ratchet.) 254 let deepd: *u8 = sys_mmap(PG_PATHCAP) 255 var ddo: i64 = pl_cat(deepd, 0, tmpdir) 256 ddo = pl_cat(deepd, ddo, "-deep" as *u8) 257 deepd[ddo] = 0 as u8 258 sys_mkdir(deepd, PG_DIRMODE) 259 pg_fixfile(deepd, "top.html" as *u8, "<h1>within depth</h1>" as *u8) 260 let dchain: *u8 = sys_mmap(PG_PATHCAP) 261 var dco: i64 = pl_cat(dchain, 0, deepd) 262 dchain[dco] = 0 as u8 263 var lvl: i64 = 0 264 while lvl < PL_MAXDEPTH + 1 { 265 dco = pl_cat(dchain, dco, "/d" as *u8) 266 dchain[dco] = 0 as u8 267 sys_mkdir(dchain, PG_DIRMODE) 268 lvl = lvl + 1 269 } 270 pg_fixfile(dchain, "lost.html" as *u8, "<h1>beyond PL_MAXDEPTH -- truncation would drop me silently</h1>" as *u8) 271 let dpfx: *u8 = sys_mmap(PG_PATHCAP) 272 var dpo: i64 = pl_cat(dpfx, 0, tmpdir) 273 dpo = pl_cat(dpfx, dpo, "-deepstore-" as *u8) 274 dpfx[dpo] = 0 as u8 275 let ac4: *i64 = sys_mmap(64) as *i64 276 let deep_rc: i64 = pl_adopt(deepd, dpfx, ac4) 277 gv_bite("T31 BITE beyond-depth tree REFUSED LOUDLY (-3, cap named on stderr) / within-depth tree adopted" as *u8, 278 (deep_rc == 0 - 3) as i64, 279 (add1 < 0) as i64, ctr) 280 gv_check("T31b depth refusal committed NOTHING -- even in-range top.html stayed unregistered" as *u8, 281 (pl_check(dpfx, "top.html" as *u8) == 3) as i64, ctr) 282 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) 283 sys_exit(rc) 284 return rc 285}