code wiki / _hdl_build / nx_docstage_gate.nx
nx_docstage_gate.nx source
↩ module page · 236 lines · 14645 B
1// nx_docstage_gate.nx -- THE REFEREE for nx_docstage (DM8, devmgmt rung).
2// END-TO-END: forks the DEPLOYED nx_docstage elf, so it grades the artifact callers run.
3//
4// The accept rule, verbatim from devmgmt.plan: "the stage reports DOCUMENTED or UNDOCUMENTED per ship
5// with ZERO false DOCUMENTED, measured against the symbols a session actually shipped."
6//
7// ZERO FALSE DOCUMENTED IS THE ONLY BAR THAT MATTERS HERE, so the fixture is built around the one way
8// this check goes wrong: a SUBSTRING match. `nx_dora_page` contains `nx_dora`, and a scanner that does
9// not test token boundaries reports the shipped symbol as written-down because a DIFFERENT symbol is
10// mentioned somewhere. That failure is invisible -- it produces a green, plausible, wrong answer -- so
11// the fixture carries all four boundary traps at once (`nx_dora_page`, `xnx_dora`, `_nx_dora`,
12// `nx_dorax`) with NO real token, and demands UNDOCUMENTED. The positive control sits in the same
13// gate: the same symbol, as a real token, must come back DOCUMENTED. A detector that answers
14// "documented" for everything fails the first; one that answers "undocumented" for everything fails
15// the second. Neither alone proves discrimination.
16//
17// The third state is tested too, because "I could not look" and "it is not written down" demand
18// opposite actions and only one of them is ever safe to act on.
19// Fixtures under /tmp/nx_docstage_gate/, rewritten O_TRUNC each run (idempotent).
20// license_tier: ORIGINAL Read-only outside /tmp. No hw writes (Rule 26).
21import "nx_syscalls.nx"
22import "nx_sovjson_lib.nx"
23import "nx_gate_verdict.nx"
24import "nx_tool_run.nx"
25
26const DG_MODE_644: i64 = 420
27const DG_MODE_755: i64 = 493
28const DG_PATH: i64 = 512
29const DG_FIX: i64 = 65536
30const DG_OUT: i64 = 1048576
31const DG_EXIT_SKIP: i64 = 3
32const DG_RC_DOC: i64 = 0
33const DG_RC_UNDOC: i64 = 1
34const DG_RC_UNMEASURED: i64 = 3
35const DG_RC_USAGE: i64 = 2
36const DG_MANY: i64 = 30 // token occurrences planted to exceed the printed-list bound
37
38func dg_wr(path: *u8, buf: *u8, n: i64) -> i64 {
39 let fd: i64 = sys_openat_wr(path, DG_MODE_644)
40 if fd < 0 { return 0 - 1 }
41 sys_write(fd, buf, n)
42 sys_close(fd)
43 return n
44}
45func dg_run(subj: *u8, a1: *u8, a2: *u8, out: *u8, cl: *i64) -> i64 {
46 let av: *i64 = sys_mmap(64) as *i64
47 av[0] = subj as i64
48 av[1] = a1 as i64
49 av[2] = a2 as i64
50 av[3] = 0
51 return tr_run_capture(subj, av, out, DG_OUT - 8, cl)
52}
53
54func main(argc: i64, argv: *i64) -> i64 {
55 let ctr: *i64 = gv_ctr()
56 gv_head("nx_docstage_gate -- a substring is not a symbol, and 'I could not look' is not 'not written down'" as *u8)
57
58 let subj: *u8 = sys_mmap(DG_PATH)
59 var have: i64 = 0
60 if argc >= 2 { var z: i64 = sj_cat(subj, 0, argv[1] as *u8); subj[z] = 0 as u8; have = 1 }
61 if have == 0 { if sj_allow_path("nx_docstage" as *u8, subj) == 1 { have = 1 } }
62 if have == 0 { var z2: i64 = sj_cat(subj, 0, "./nx_docstage.elf" as *u8); subj[z2] = 0 as u8; have = 1 }
63
64 let root: *u8 = "/tmp/nx_docstage_gate" as *u8
65 let ddocs: *u8 = "/tmp/nx_docstage_gate/docs" as *u8
66 let dsub: *u8 = "/tmp/nx_docstage_gate/docs/sub" as *u8
67 let dtraps: *u8 = "/tmp/nx_docstage_gate/traps" as *u8
68 let dmany: *u8 = "/tmp/nx_docstage_gate/many" as *u8
69 let fa: *u8 = "/tmp/nx_docstage_gate/docs/a.matrix" as *u8
70 let fb: *u8 = "/tmp/nx_docstage_gate/docs/sub/b.plan" as *u8
71 let fc: *u8 = "/tmp/nx_docstage_gate/docs/c.txt" as *u8
72 let ft: *u8 = "/tmp/nx_docstage_gate/traps/only_traps.matrix" as *u8
73 let fm: *u8 = "/tmp/nx_docstage_gate/many/m.txt" as *u8
74 let cgood: *u8 = "/tmp/nx_docstage_gate/roots.conf" as *u8
75 let ctraps: *u8 = "/tmp/nx_docstage_gate/traps.conf" as *u8
76 let cbad: *u8 = "/tmp/nx_docstage_gate/badroot.conf" as *u8
77 let cmany: *u8 = "/tmp/nx_docstage_gate/many.conf" as *u8
78 let cabsent: *u8 = "/tmp/nx_docstage_gate/there-is-no-conf-here.conf" as *u8
79 let dbig: *u8 = "/tmp/nx_docstage_gate/big" as *u8
80 let dbig2: *u8 = "/tmp/nx_docstage_gate/big2" as *u8
81 let fbig: *u8 = "/tmp/nx_docstage_gate/big/over_budget.txt" as *u8
82 let fbig2: *u8 = "/tmp/nx_docstage_gate/big2/over_budget.txt" as *u8
83 let ftiny: *u8 = "/tmp/nx_docstage_gate/big2/under_budget.txt" as *u8
84 let cbig: *u8 = "/tmp/nx_docstage_gate/big.conf" as *u8
85 let cbig2: *u8 = "/tmp/nx_docstage_gate/big2.conf" as *u8
86 let bsym: *u8 = "zz_big" as *u8
87 let sym: *u8 = "nx_dora" as *u8
88 let msym: *u8 = "zz_many" as *u8
89 let empty: *u8 = "" as *u8
90 sys_mkdir(root, DG_MODE_755)
91 sys_mkdir(ddocs, DG_MODE_755)
92 sys_mkdir(dsub, DG_MODE_755)
93 sys_mkdir(dtraps, DG_MODE_755)
94 sys_mkdir(dmany, DG_MODE_755)
95
96 let b: *u8 = sys_mmap(DG_FIX)
97 // the substring trap, alone in its file: a longer symbol that CONTAINS the queried one
98 var o: i64 = sj_cat(b, 0, "row|organ|nx_dora_page|0|1|the page emitter, a DIFFERENT symbol\n" as *u8)
99 let w1: i64 = dg_wr(fa, b, o)
100 // the ONLY real token in the whole good-root tree, and it sits in a SUBDIRECTORY
101 o = sj_cat(b, 0, "rung|D1|DORA on ship receipts|nx_dora|Organ|1|-\n" as *u8)
102 let w2: i64 = dg_wr(fb, b, o)
103 // the three boundary traps that are not the substring trap
104 o = sj_cat(b, 0, "xnx_dora and _nx_dora and nx_dorax are all NOT the symbol\n" as *u8)
105 let w3: i64 = dg_wr(fc, b, o)
106 // a root that contains EVERY trap and no real token
107 o = sj_cat(b, 0, "row|organ|nx_dora_page|0|1\nxnx_dora _nx_dora nx_dorax nx_dora_page\n" as *u8)
108 let w4: i64 = dg_wr(ft, b, o)
109 // more real tokens than the printed list can hold, so the prefix declaration has to fire
110 o = 0
111 var mi: i64 = 0
112 while mi < DG_MANY { o = sj_cat(b, o, "zz_many\n" as *u8); mi = mi + 1 }
113 let w5: i64 = dg_wr(fm, b, o)
114 // confs: comments and blank lines must be ignored, not read as roots
115 o = sj_cat(b, 0, "# doc roots for the gate fixture\n\n \n/tmp/nx_docstage_gate/docs\n" as *u8)
116 let w6: i64 = dg_wr(cgood, b, o)
117 o = sj_cat(b, 0, "/tmp/nx_docstage_gate/traps\n" as *u8)
118 let w7: i64 = dg_wr(ctraps, b, o)
119 o = sj_cat(b, 0, "/tmp/nx_docstage_gate/this-root-does-not-exist\n" as *u8)
120 let w8: i64 = dg_wr(cbad, b, o)
121 o = sj_cat(b, 0, "/tmp/nx_docstage_gate/many\n" as *u8)
122 let w9: i64 = dg_wr(cmany, b, o)
123 // the work-budget fixture: a file OVER the declared budget that DOES carry a real token, so a
124 // scanner that ignores the budget finds it and a scanner that honours it must say it could not look
125 sys_mkdir(dbig, DG_MODE_755)
126 sys_mkdir(dbig2, DG_MODE_755)
127 o = sj_cat(b, 0, "zz_big is a real token in a file that exceeds the declared budget\n" as *u8)
128 var pad: i64 = 0
129 while pad < DG_MANY { o = sj_cat(b, o, "padpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpadpad\n" as *u8); pad = pad + 1 }
130 let wA: i64 = dg_wr(fbig, b, o)
131 let wB: i64 = dg_wr(fbig2, b, o)
132 o = sj_cat(b, 0, "zz_big also appears here, under budget\n" as *u8)
133 let wC: i64 = dg_wr(ftiny, b, o)
134 o = sj_cat(b, 0, "# a corpus of prose needs a different budget from a corpus of media\nmax_file_bytes=1024\n/tmp/nx_docstage_gate/big\n" as *u8)
135 let wD: i64 = dg_wr(cbig, b, o)
136 o = sj_cat(b, 0, "max_file_bytes=1024\n/tmp/nx_docstage_gate/big2\n" as *u8)
137 let wE: i64 = dg_wr(cbig2, b, o)
138 sys_unlinkat(cabsent)
139
140 var fixok: i64 = 0
141 if w1 > 0 { if w2 > 0 { if w3 > 0 { if w4 > 0 { if w5 > 0 { if w6 > 0 { if w7 > 0 { if w8 > 0 { if w9 > 0 { if wA > 0 { if wB > 0 { if wC > 0 { if wD > 0 { if wE > 0 { fixok = 1 } } } } } } } } } } } } } }
142 gv_check("fixture-written-all-nine (assert the fixture reached the condition before the outcome)" as *u8, fixok, ctr)
143
144 let out: *u8 = sys_mmap(DG_OUT)
145 let cl: *i64 = sys_mmap(16) as *i64
146
147 // ---- positive control: the symbol IS a real token, in a subdirectory ----
148 let rc1: i64 = dg_run(subj, sym, cgood, out, cl)
149 let n1: i64 = cl[0]
150 if n1 <= 0 {
151 gv_puts("SKIP subject produced no output -- build/deploy nx_docstage, then re-run: " as *u8)
152 gv_puts(subj)
153 gv_puts("\n" as *u8)
154 sys_exit(DG_EXIT_SKIP)
155 return DG_EXIT_SKIP
156 }
157 gv_check("subject-examined-a-nonzero-population (a zero-file scan cannot judge documentation)" as *u8, tr_contains(out, n1, "files_scanned=3" as *u8), ctr)
158 gv_check("pos-control-a-real-token-reports-DOCUMENTED" as *u8, tr_contains(out, n1, "verdict=DOCUMENTED hits=1 files_with_hits=1" as *u8), ctr)
159 gv_check("pos-control-carries-exit-code-0-so-a-caller-can-branch" as *u8, 1 - rc1, ctr)
160 gv_check("the-hit-is-NAMED-with-its-file-and-line-not-just-counted" as *u8, tr_contains(out, n1, "DOC-HIT /tmp/nx_docstage_gate/docs/sub/b.plan line=1" as *u8), ctr)
161 gv_check("recursion-reached-the-subdirectory-that-holds-the-only-real-token" as *u8, tr_contains(out, n1, "docs/sub/b.plan" as *u8), ctr)
162 gv_check("the-three-non-substring-boundary-traps-in-the-same-tree-are-NOT-counted" as *u8, tr_contains(out, n1, "hits=1 " as *u8), ctr)
163 gv_check("coverage-is-declared-complete-on-a-fully-walked-tree" as *u8, tr_contains(out, n1, "coverage_complete=1 match=exact-token" as *u8), ctr)
164 // EVERY assertion about a run lives BEFORE the next run: `out` is one reused buffer, so a tooth
165 // written further down silently grades a LATER run's bytes under an earlier run's name. This gate
166 // failed exactly that way on its first execution -- the tooth was correct and the subject was
167 // correct, and the RED was mine.
168 gv_check("conf-comments-and-blank-lines-are-not-read-as-roots" as *u8, tr_contains(out, n1, "roots_declared=1 roots_walked=1 roots_source=conf" as *u8), ctr)
169
170 // ---- THE BAR: a root of nothing but look-alikes must NOT report DOCUMENTED ----
171 let rc2: i64 = dg_run(subj, sym, ctraps, out, cl)
172 let n2: i64 = cl[0]
173 gv_check("ACCEPT-zero-false-DOCUMENTED-a-root-of-only-look-alikes-reports-UNDOCUMENTED" as *u8, tr_contains(out, n2, "verdict=UNDOCUMENTED hits=0" as *u8), ctr)
174 var nfp: i64 = 1
175 if tr_contains(out, n2, "DOC-HIT" as *u8) == 1 { nfp = 0 }
176 gv_check("neg-control-no-hit-row-is-emitted-for-any-look-alike" as *u8, nfp, ctr)
177 var rcok2: i64 = 0
178 if rc2 == DG_RC_UNDOC { rcok2 = 1 }
179 gv_check("UNDOCUMENTED-carries-its-own-exit-code-1-distinct-from-cannot-look" as *u8, rcok2, ctr)
180 gv_check("UNDOCUMENTED-is-only-claimed-with-complete-coverage-behind-it" as *u8, tr_contains(out, n2, "verdict=UNDOCUMENTED hits=0 files_with_hits=0 files_scanned=1 coverage_complete=1" as *u8), ctr)
181
182 // ---- the third state: cannot look is not an acquittal and not a conviction ----
183 let rc3: i64 = dg_run(subj, sym, cbad, out, cl)
184 let n3: i64 = cl[0]
185 gv_check("neg-control-an-unwalkable-root-reports-UNMEASURED-never-UNDOCUMENTED" as *u8, tr_contains(out, n3, "verdict=UNMEASURED" as *u8), ctr)
186 var rcok3: i64 = 0
187 if rc3 == DG_RC_UNMEASURED { rcok3 = 1 }
188 gv_check("UNMEASURED-carries-exit-3-so-a-caller-cannot-mistake-it-for-a-finding" as *u8, rcok3, ctr)
189 gv_check("the-unwalkable-root-is-COUNTED-roots_declared-1-roots_walked-0" as *u8, tr_contains(out, n3, "roots_declared=1 roots_walked=0" as *u8), ctr)
190
191 // ---- a capped LIST beside an uncapped COUNT must say so ----
192 let rc4: i64 = dg_run(subj, msym, cmany, out, cl)
193 let n4: i64 = cl[0]
194 gv_check("the-full-count-is-reported-even-though-the-printed-list-is-bounded" as *u8, tr_contains(out, n4, "verdict=DOCUMENTED hits=30" as *u8), ctr)
195 gv_check("and-the-bounded-list-DECLARES-itself-a-prefix-of-that-count" as *u8, tr_contains(out, n4, "PREFIX of its own count: shown=24 of hits=30" as *u8), ctr)
196
197 // ---- config hierarchy: the code default is the lowest rung and it announces itself ----
198 let rc5: i64 = dg_run(subj, sym, cabsent, out, cl)
199 gv_check("an-absent-conf-falls-back-to-the-builtin-default-and-SAYS-SO" as *u8, tr_contains(out, cl[0], "roots_source=builtin-default" as *u8), ctr)
200
201 // ---- the work budget: an unbounded scan of a real doc root NEVER RETURNED (measured on
202 // knowledge/library, 7,627+ entries incl. mirrored video, zero bytes out, indistinguishable from a
203 // crash). A bound is only legitimate if it ANNOUNCES, and if it costs the run its completeness. ----
204 let rcA: i64 = dg_run(subj, bsym, cbig, out, cl)
205 let nA: i64 = cl[0]
206 gv_check("an-over-budget-file-is-SKIPPED-and-COUNTED-not-silently-read" as *u8, tr_contains(out, nA, "files_skipped_oversize=1" as *u8), ctr)
207 gv_check("the-budget-and-WHERE-IT-CAME-FROM-are-printed-in-every-report" as *u8, tr_contains(out, nA, "max_file_bytes=1024 budget_source=conf" as *u8), ctr)
208 gv_check("a-skipped-file-costs-the-run-its-completeness-claim" as *u8, tr_contains(out, nA, "coverage_complete=0" as *u8), ctr)
209 gv_check("ACCEPT-partial-coverage-with-no-witness-is-UNMEASURED-never-UNDOCUMENTED" as *u8, tr_contains(out, nA, "verdict=UNMEASURED hits=0" as *u8), ctr)
210 var rcokA: i64 = 0
211 if rcA == DG_RC_UNMEASURED { rcokA = 1 }
212 gv_check("and-it-exits-3-so-a-caller-cannot-read-a-skipped-corpus-as-a-finding" as *u8, rcokA, ctr)
213 let rcB: i64 = dg_run(subj, bsym, cbig2, out, cl)
214 let nB: i64 = cl[0]
215 gv_check("ONE-WITNESS-IS-ENOUGH-a-hit-under-budget-yields-DOCUMENTED-despite-partial-coverage" as *u8, tr_contains(out, nB, "verdict=DOCUMENTED hits=1" as *u8), ctr)
216 gv_check("and-that-DOCUMENTED-still-publishes-coverage_complete-0-rather-than-hiding-it" as *u8, tr_contains(out, nB, "files_skipped_oversize=1" as *u8), ctr)
217 var rcokB: i64 = 0
218 if rcB == DG_RC_DOC { rcokB = 1 }
219 gv_check("presence-and-absence-do-not-need-the-same-evidence-and-the-exit-code-says-so" as *u8, rcokB, ctr)
220
221 // ---- an empty needle matches every boundary: it must be refused, not answered ----
222 let rc6: i64 = dg_run(subj, empty, cgood, out, cl)
223 var rcok6: i64 = 0
224 if rc6 == DG_RC_USAGE { if tr_contains(out, cl[0], "REFUSED empty symbol" as *u8) == 1 { rcok6 = 1 } }
225 gv_check("neg-control-an-empty-symbol-is-REFUSED-not-reported-DOCUMENTED" as *u8, rcok6, ctr)
226
227 // ---- idempotence ----
228 let rc7: i64 = dg_run(subj, sym, cgood, out, cl)
229 var idem: i64 = 0
230 if rc7 == DG_RC_DOC { if tr_contains(out, cl[0], "verdict=DOCUMENTED hits=1 files_with_hits=1" as *u8) == 1 { idem = 1 } }
231 gv_check("idempotent-a-second-run-on-the-same-fixture-grades-identically" as *u8, idem, ctr)
232
233 let v: i64 = gv_verdict("DOCSTAGE-GATE" as *u8, ctr, "documentation is judged on exact tokens, absence is claimed only with complete coverage, and cannot-look is its own third state" as *u8)
234 sys_exit(v)
235 return v
236}