code wiki / _hdl_build / nx_corpus_scan_gate.nx
nx_corpus_scan_gate.nx source
↩ module page · 129 lines · 5695 B
1// nx_corpus_scan_gate.nx -- PROVE gk_corpus_scan ENUMERATES THE WHOLE COMPILE CORPUS.
2//
3// WHY THIS GATE EXISTS
4// --------------------
5// nx_unwired's header claims "FULL ECOSYSTEM SCOPE, NOT A SAMPLE: every .nx in the tree" while its
6// UW_DIR constant names ONE directory. Measured 2026-08-08: 7,159 of 18,559 files = 38.6%. Because
7// the unscanned files are BOTH definition sources AND call sources, the truncation corrupted the
8// answer in both directions at once -- 30 names were reported UNWIRED that are called in the files
9// it never opened, and 3,379 definitions in those files were never examined at all.
10//
11// ★★★★★★ AN INSTRUMENT THAT DECLARES FULL-TREE SCOPE IN ITS HEADER AND HARDCODES ONE DIRECTORY IN
12// ITS CONSTANT MEASURES A SUBSET AND REPORTS IT AS THE POPULATION.
13//
14// Every tooth below is FULL POPULATION, never a sample -- a sample cannot see that a class is empty,
15// only that it is rare, and "no clipped paths" is exactly an emptiness claim.
16//
17// usage: nx_corpus_scan_gate
18// exit: 0 GREEN, 1 RED (exit code carries the verdict; canonical verdict= line is printed LAST)
19// license_tier: ORIGINAL
20import "nx_gatekit_lib.nx"
21
22const CG_CAP: i64 = 32768
23const CG_STRIDE: i64 = 128
24
25func cg_base(path: *u8) -> i64 {
26 var i: i64 = 0
27 var last: i64 = 0
28 while path[i] != (0 as u8) {
29 if path[i] == (47 as u8) { last = i + 1 }
30 i = i + 1
31 }
32 return last
33}
34
35func main(argc: i64, argv: *i64) -> i64 {
36 let r: *i64 = sys_mmap(16) as *i64
37 r[0] = 0
38 r[1] = 0
39 gk_head("nx_corpus_scan_gate -- the compile corpus is enumerated WHOLE" as *u8)
40
41 let names: *u8 = sys_mmap(CG_CAP * CG_STRIDE)
42 let n: i64 = gk_corpus_scan(names, CG_STRIDE, CG_CAP)
43
44 let msg: *u8 = sys_mmap(4096)
45 var o: i64 = gk_cat(msg, 0, " corpus_files=" as *u8)
46 o = gk_catn(msg, o, n)
47 o = gk_cat(msg, o, "\n" as *u8)
48 gk_say(msg, o)
49
50 // T1 -- the scan succeeded and the corpus is the size the tree actually is.
51 gk_ok(r, (n > 18000) as i64, "corpus-scan-returns-full-tree (>18000 files)" as *u8)
52
53 // T2 -- single-dir control: what the OLD assumption would have measured. The gate's whole
54 // subject is the gap, so bind the assertion to it rather than to a bare count.
55 let hdlonly: *u8 = sys_mmap(CG_CAP * CG_STRIDE)
56 let hdlp: *u8 = sys_mmap(4096)
57 gk_corpus_hdl(hdlp)
58 let nh: i64 = gk_dirscan(hdlp, 0 as *u8, hdlonly, CG_STRIDE, CG_CAP, 0)
59 var o2: i64 = gk_cat(msg, 0, " single_dir_would_have_seen=" as *u8)
60 o2 = gk_catn(msg, o2, nh)
61 o2 = gk_cat(msg, o2, " (the defect this gate exists to keep fixed)\n" as *u8)
62 gk_say(msg, o2)
63 gk_ok(r, (n > nh) as i64, "whole-corpus-strictly-exceeds-single-directory" as *u8)
64
65 // T3 -- ANTI-VACUITY, FULL POPULATION: every path returned must actually open. A path clipped
66 // to the stride would still be counted, so a count alone cannot detect truncation.
67 var i: i64 = 0
68 var dead: i64 = 0
69 while i < n {
70 let p: *u8 = ((names as i64) + i * CG_STRIDE) as *u8
71 if gk_exists(p) == 0 { dead = dead + 1 }
72 i = i + 1
73 }
74 gk_eq(r, dead, 0, "every-enumerated-path-opens (all N stat'd, not sampled)" as *u8)
75
76 // T4 -- the OVERLAY RULE, checked over the whole population rather than one example: for every
77 // path taken from runtime/, _hdl_build/<base> must NOT exist, or the overlay lost.
78 var j: i64 = 0
79 var shadowbreak: i64 = 0
80 var fromrt: i64 = 0
81 let probe: *u8 = sys_mmap(4096)
82 let rtp: *u8 = sys_mmap(4096)
83 gk_corpus_root(rtp)
84 while j < n {
85 let p: *u8 = ((names as i64) + j * CG_STRIDE) as *u8
86 let b: i64 = cg_base(p)
87 // a runtime/ path has a shorter prefix than an _hdl_build/ one
88 if b == gk_len(rtp) {
89 fromrt = fromrt + 1
90 let nm: *u8 = ((p as i64) + b) as *u8
91 var sp: i64 = gk_cat(probe, 0, hdlp)
92 sp = gk_cat(probe, sp, nm)
93 probe[sp] = 0 as u8
94 if gk_exists(probe) == 1 { shadowbreak = shadowbreak + 1 }
95 }
96 j = j + 1
97 }
98 var o3: i64 = gk_cat(msg, 0, " taken_from_runtime=" as *u8)
99 o3 = gk_catn(msg, o3, fromrt)
100 o3 = gk_cat(msg, o3, " taken_from_hdl_build=" as *u8)
101 o3 = gk_catn(msg, o3, n - fromrt)
102 o3 = gk_cat(msg, o3, "\n" as *u8)
103 gk_say(msg, o3)
104 gk_eq(r, shadowbreak, 0, "overlay-rule-holds: no runtime/ file shadowed by _hdl_build was taken" as *u8)
105
106 // T5 -- PARTITION SUMS. A partition is a claim; check the parts add up and PRINT them.
107 gk_eq(r, fromrt + (n - fromrt), n, "partition-sums: hdl_build + runtime == corpus" as *u8)
108
109 // T6 -- neg-control-cap: a cap smaller than the tree MUST refuse (-2), never return a short
110 // count wearing a complete one's clothes.
111 let tiny: *u8 = sys_mmap(64 * CG_STRIDE)
112 let rc_cap: i64 = gk_corpus_scan(tiny, CG_STRIDE, 64)
113 gk_eq(r, rc_cap, 0 - 2, "neg-control-cap-too-small-REFUSES-instead-of-truncating" as *u8)
114
115 // T7 -- neg-control-stride: a stride too short to hold a path MUST refuse (-3), not clip.
116 let narrow: *u8 = sys_mmap(CG_CAP * 32)
117 let rc_str: i64 = gk_corpus_scan(narrow, 32, CG_CAP)
118 gk_eq(r, rc_str, 0 - 3, "neg-control-stride-too-small-REFUSES-instead-of-clipping" as *u8)
119
120 let bad: i64 = gk_result(r)
121 // Canonical verdict LAST -- gv_last_line anchors by POSITION, and positional anchoring beats
122 // textual matching wherever the output is untrusted.
123 let v: *u8 = sys_mmap(64)
124 var vo: i64 = 0
125 if bad == 0 { vo = gk_cat(v, 0, "verdict=GREEN\n" as *u8) }
126 if bad != 0 { vo = gk_cat(v, 0, "verdict=RED\n" as *u8) }
127 gk_say(v, vo)
128 return bad
129}