code wiki / _hdl_build / nx_adopt.nx
nx_adopt.nx source
↩ module page · 408 lines · 22529 B
1// nx_adopt.nx -- THE ADOPTION CENSUS. Which capability primitives has nobody adopted? (operator priority)
2//
3// LAW: THE BOTTLENECK IS NOT BUILDING PRIMITIVES, IT IS ADOPTING THEM. Three proofs on 2026-07-30:
4// cpu-feature-detect existed with a KAT, registered nowhere (seq1359) - ss_close: 133 openers, 1 closer
5// (seq1347) - nx_hw_cpu_count: CORRECT (sched_getaffinity, cgroup-safe) with ZERO external callers in
6// 19,823 files while nx_torrent_get hardcodes MAXP=32 (seq1410). A build-only 'done' is the cause.
7// ==> A ZERO-CALLER CAPABILITY PRIMITIVE IS A RED DEFECT, NOT A COMPLETED FEATURE.
8//
9// SCALE: the naive census greps the corpus once per primitive = O(primitives x corpus). This walks the
10// corpus TWICE total -- pass A hashes every definition, pass B counts every identifier against it with
11// O(1) lookups -- so cost is O(corpus) regardless of how many primitives exist.
12//
13// SELF-REFERENCES DO NOT COUNT AS ADOPTION. nx_hw_cpu_count showed FIVE grep matches and looked alive;
14// four were its own file. Definition-file hits are tallied SEPARATELY from external ones -- that is the
15// entire difference between 'used' and 'looks used'.
16// argv: <defs-dir> [callers-dir]
17// exit 0 = ran, 2 = usage/unreadable. license_tier: ORIGINAL Read-only. No hw writes (Rule 26).
18import "nx_adopt_lib.nx"
19import "nx_proc_ctl.nx"
20import "nx_resmon_lib.nx"
21
22const AD_CAP: i64 = 65536
23const AD_TBL_BYTES: i64 = 524288
24const AD_NAMEBUF: i64 = 2097152
25const AD_FILECAP: i64 = 2097152
26const AD_DIRBUF: i64 = 262144
27const AD_MAXREPORT: i64 = 30
28const AD_PATHBUF: i64 = 1024
29const AD_PAREN: i64 = 40
30const AD_SLASH: i64 = 47
31const AD_NL: i64 = 10
32const AD_SP: i64 = 32
33const AD_F: i64 = 102
34const AD_U: i64 = 117
35const AD_N: i64 = 110
36const AD_C: i64 = 99
37// Spare fd used to PARK the caller's real stdout while the long report is written to argv[3].
38// 9 is above the 0/1/2 standard trio and below any fd this organ opens, so it cannot collide.
39const AD_SAVEFD: i64 = 9
40// Second spare fd, same trick, used to park stdout while the machine-readable stamp is written.
41const AD_STAMPFD: i64 = 10
42// The ADOPTION axis stamp. A .stamp and NOT a .log ON PURPOSE: nx_gatereg_derive registers ANY anchored
43// *.log in knowledge/status as a gate, and this is a CENSUS, not a verdict -- registering it would
44// manufacture a false RED in nx_gate_rollup (whose pass vocabulary is GREEN|PASS|VALID). Filed as its own
45// defect; this organ declines to feed it.
46const AD_STAMP: *u8 = "knowledge/status/adopt_census.stamp" as *u8
47
48// PROOF-FILE CLASSIFIER (2026-07-30). A caller that is a GATE is not adoption -- it is the receipt for
49// the build. This census scored external>0 as ADOPTED, which made it blind to the class that caused six
50// separate defects in one session: capability BUILT, capability GATED, capability never called in
51// production. ls_signup_allowed scored adopted (two gates call it) while the live register route gated on
52// a single global boolean and public registration sat open to the internet.
53// BOUNDED substring search. The first cut kept scanning after a mismatch and read nm[i+k] PAST the
54// NUL terminator -- an out-of-bounds read that happened to compile and would usually 'work'. Stop at the
55// first mismatch AND at the end of nm, so the scan can never leave the string.
56func ad_has_sub(nm: *u8, sub: *u8) -> i64 {
57 var i: i64 = 0
58 while nm[i] != (0 as u8) {
59 var k: i64 = 0
60 var ok: i64 = 1
61 var go: i64 = 1
62 while go == 1 {
63 if sub[k] == (0 as u8) { go = 0 } else {
64 if nm[i+k] == (0 as u8) { ok = 0; go = 0 } else {
65 if nm[i+k] != sub[k] { ok = 0; go = 0 } else { k = k + 1 }
66 }
67 }
68 }
69 if ok == 1 { return 1 }
70 i = i + 1
71 }
72 return 0
73}
74func ad_is_prooffile(nm: *u8) -> i64 {
75 if ad_has_sub(nm, "_gate" as *u8) == 1 { return 1 }
76 if ad_has_sub(nm, "_test" as *u8) == 1 { return 1 }
77 if ad_has_sub(nm, "_probe" as *u8) == 1 { return 1 }
78 if ad_has_sub(nm, "_kat" as *u8) == 1 { return 1 }
79 if ad_has_sub(nm, "_smoke" as *u8) == 1 { return 1 }
80 return 0
81}
82func main(argc: i64, argv: *i64) -> i64 {
83 if argc < 2 { rm_puts("usage: nx_adopt <defs-dir> [callers-dir]\n" as *u8); return 2 }
84 let dirs: *i64 = sys_mmap(32) as *i64
85 dirs[0] = argv[1]
86 var ndirs: i64 = 1
87 if argc >= 3 { dirs[1] = argv[2]; ndirs = 2 }
88 // Optional argv[3] = report path. WHY: a census whose findings only reach stdout is capped at the
89 // 200-char plan snippet, so the DARK LIST -- the entire point -- is unreadable. Same lesson as
90 // nx_procchurn's journal: an instrument that cannot deliver its findings has not delivered them.
91 // DELIVERY FIX 2026-07-30. The comment directly above states the law -- 'an instrument that cannot
92 // deliver its findings has not delivered them' -- and then this line dup3'd the report file OVER fd 1
93 // and never restored it, so EVERY caller received an empty response. It is not conditional in practice:
94 // the allowlist row pins argv[3]=knowledge/status/adopt_census.txt, so argc is ALWAYS >=4 over MCP and
95 // the census returned nothing to every seat that ever called it -- indistinguishable from a broken tool,
96 // which is why nx_adopt sits in the S4 DARK list while working perfectly.
97 // NOW: the long DEBT/PROVEN_UNWIRED rows still go to the file (they belong there -- they are unbounded),
98 // and the caller's stdout is PARKED on AD_SAVEFD and restored before the summary, so the answer arrives
99 // where it was asked for. Fail-safe: if the park fails, ad_savefd stays -1 and behaviour is exactly as before.
100 var ad_savefd: i64 = 0 - 1
101 if argc >= 4 {
102 let ofd: i64 = sys_openat_wr(argv[3] as *u8, 420)
103 if ofd >= 0 {
104 if sys_dup3(1, AD_SAVEFD, 0) >= 0 { ad_savefd = AD_SAVEFD }
105 sys_dup3(ofd, 1, 0)
106 }
107 }
108
109 let h_hash: *i64 = sys_mmap(AD_TBL_BYTES) as *i64
110 let h_noff: *i64 = sys_mmap(AD_TBL_BYTES) as *i64
111 let h_nlen: *i64 = sys_mmap(AD_TBL_BYTES) as *i64
112 let h_fh: *i64 = sys_mmap(AD_TBL_BYTES) as *i64
113 let h_ext: *i64 = sys_mmap(AD_TBL_BYTES) as *i64
114 let h_self: *i64 = sys_mmap(AD_TBL_BYTES) as *i64
115 // callers that are GATES/TESTS, counted SEPARATELY from h_ext (which stays the TOTAL external count so
116 // the existing DEBT verdict is byte-for-byte unchanged). production callers = h_ext - h_proof.
117 let h_proof: *i64 = sys_mmap(AD_TBL_BYTES) as *i64
118 let arena: *u8 = sys_mmap(AD_NAMEBUF)
119 let src: *u8 = sys_mmap(AD_FILECAP)
120 let dbuf: *u8 = sys_mmap(AD_DIRBUF)
121 let p: *u8 = sys_mmap(AD_PATHBUF)
122 var ab: i64 = 0
123 var ndef: i64 = 0
124 var nfiles: i64 = 0
125 var truncated: i64 = 0
126
127 let d1: *u8 = dirs[0] as *u8
128 let fdA: i64 = sys_openat_rd(d1)
129 if fdA < 0 { rm_puts("NX-ADOPT defs-dir unreadable\n" as *u8); return 2 }
130 var goA: i64 = 1
131 while goA == 1 {
132 let dn: i64 = sys_getdents64(fdA, dbuf, AD_DIRBUF)
133 if dn <= 0 { goA = 0 } else {
134 var off: i64 = 0
135 while off < dn {
136 let rec: *u8 = ((dbuf as i64) + off) as *u8
137 let reclen: i64 = dirent_reclen(rec)
138 if reclen <= 0 { off = dn } else {
139 let nm: *u8 = dirent_name(rec)
140 var take: i64 = 0
141 if dirent_type(rec) != 4 { if ad_ends_nx(nm) == 1 { take = 1 } }
142 if take == 1 {
143 var po: i64 = 0
144 var di: i64 = 0
145 while d1[di] != (0 as u8) { p[po] = d1[di]; po = po + 1; di = di + 1 }
146 p[po] = AD_SLASH as u8; po = po + 1
147 let nb0: i64 = po
148 var mi: i64 = 0
149 while nm[mi] != (0 as u8) { p[po] = nm[mi]; po = po + 1; mi = mi + 1 }
150 p[po] = 0 as u8
151 let fh: i64 = ad_fnv(p, nb0, po)
152 let fd: i64 = sys_openat_rd(p)
153 if fd >= 0 {
154 var slen: i64 = 0
155 var r: i64 = sys_read(fd, src, AD_FILECAP - 1)
156 while r > 0 {
157 slen = slen + r
158 if slen >= AD_FILECAP - 1 { r = 0; truncated = truncated + 1 } else { r = sys_read(fd, ((src as i64) + slen) as *u8, AD_FILECAP - 1 - slen) }
159 }
160 sys_close(fd)
161 nfiles = nfiles + 1
162 var i: i64 = 0
163 while i + 6 < slen {
164 var bol: i64 = 0
165 if i == 0 { bol = 1 }
166 if i > 0 { if src[i-1] == (AD_NL as u8) { bol = 1 } }
167 var isfunc: i64 = 0
168 if bol == 1 {
169 if src[i] == (AD_F as u8) { if src[i+1] == (AD_U as u8) { if src[i+2] == (AD_N as u8) { if src[i+3] == (AD_C as u8) { if src[i+4] == (AD_SP as u8) { isfunc = 1 } } } } }
170 }
171 if isfunc == 1 {
172 let s: i64 = i + 5
173 var e: i64 = s
174 var run: i64 = 1
175 while run == 1 {
176 if e >= slen { run = 0 } else {
177 if ad_isid(src[e] as i64) == 1 { e = e + 1 } else { run = 0 }
178 }
179 }
180 if e > s {
181 let hh: i64 = ad_fnv(src, s, e)
182 var slot: i64 = ad_slot(hh, AD_CAP)
183 var placed: i64 = 0
184 var probes: i64 = 0
185 while placed == 0 {
186 if probes > AD_CAP { placed = 1 } else {
187 if h_hash[slot] == 0 {
188 if ab + (e - s) < AD_NAMEBUF {
189 h_hash[slot] = hh
190 h_noff[slot] = ab
191 h_nlen[slot] = e - s
192 var k: i64 = 0
193 while k < e - s { arena[ab + k] = src[s + k]; k = k + 1 }
194 ab = ab + (e - s)
195 h_fh[slot] = fh
196 h_ext[slot] = 0
197 h_self[slot] = 0
198 ndef = ndef + 1
199 }
200 placed = 1
201 } else {
202 if h_hash[slot] == hh { placed = 1 } else { slot = ad_next(slot, AD_CAP); probes = probes + 1 }
203 }
204 }
205 }
206 }
207 i = e
208 } else { i = i + 1 }
209 }
210 }
211 }
212 off = off + reclen
213 }
214 }
215 }
216 }
217 sys_close(fdA)
218
219 var dix: i64 = 0
220 var scanned: i64 = 0
221 while dix < ndirs {
222 let dcur: *u8 = dirs[dix] as *u8
223 let fdB: i64 = sys_openat_rd(dcur)
224 if fdB >= 0 {
225 var goB: i64 = 1
226 while goB == 1 {
227 let dn: i64 = sys_getdents64(fdB, dbuf, AD_DIRBUF)
228 if dn <= 0 { goB = 0 } else {
229 var off: i64 = 0
230 while off < dn {
231 let rec: *u8 = ((dbuf as i64) + off) as *u8
232 let reclen: i64 = dirent_reclen(rec)
233 if reclen <= 0 { off = dn } else {
234 let nm: *u8 = dirent_name(rec)
235 var take: i64 = 0
236 if dirent_type(rec) != 4 { if ad_ends_nx(nm) == 1 { take = 1 } }
237 if take == 1 {
238 var po: i64 = 0
239 var di: i64 = 0
240 while dcur[di] != (0 as u8) { p[po] = dcur[di]; po = po + 1; di = di + 1 }
241 p[po] = AD_SLASH as u8; po = po + 1
242 let nb0: i64 = po
243 var mi: i64 = 0
244 while nm[mi] != (0 as u8) { p[po] = nm[mi]; po = po + 1; mi = mi + 1 }
245 p[po] = 0 as u8
246 let fh: i64 = ad_fnv(p, nb0, po)
247 let fd: i64 = sys_openat_rd(p)
248 if fd >= 0 {
249 var slen: i64 = 0
250 var r: i64 = sys_read(fd, src, AD_FILECAP - 1)
251 while r > 0 {
252 slen = slen + r
253 if slen >= AD_FILECAP - 1 { r = 0 } else { r = sys_read(fd, ((src as i64) + slen) as *u8, AD_FILECAP - 1 - slen) }
254 }
255 sys_close(fd)
256 scanned = scanned + 1
257 // is THIS caller file a proof file? computed once per file, not per identifier.
258 let isproof: i64 = ad_is_prooffile(nm)
259 var i: i64 = 0
260 while i < slen {
261 var isstart: i64 = 0
262 if ad_id_start(src[i] as i64) == 1 {
263 if i == 0 { isstart = 1 }
264 if i > 0 { if ad_isid(src[i-1] as i64) == 0 { isstart = 1 } }
265 }
266 if isstart == 1 {
267 var e: i64 = i
268 var run: i64 = 1
269 while run == 1 {
270 if e >= slen { run = 0 } else {
271 if ad_isid(src[e] as i64) == 1 { e = e + 1 } else { run = 0 }
272 }
273 }
274 var iscall: i64 = 0
275 if e < slen { if src[e] == (AD_PAREN as u8) { iscall = 1 } }
276 if iscall == 1 {
277 let hh: i64 = ad_fnv(src, i, e)
278 var slot: i64 = ad_slot(hh, AD_CAP)
279 var done: i64 = 0
280 var probes: i64 = 0
281 while done == 0 {
282 if probes > AD_CAP { done = 1 } else {
283 if h_hash[slot] == 0 { done = 1 } else {
284 var hit: i64 = 0
285 if h_hash[slot] == hh {
286 if ad_name_eq(arena, h_noff[slot], h_nlen[slot], src, i, e) == 1 { hit = 1 }
287 }
288 if hit == 1 {
289 if h_fh[slot] == fh { h_self[slot] = h_self[slot] + 1 } else {
290 h_ext[slot] = h_ext[slot] + 1
291 if isproof == 1 { h_proof[slot] = h_proof[slot] + 1 }
292 }
293 done = 1
294 } else { slot = ad_next(slot, AD_CAP); probes = probes + 1 }
295 }
296 }
297 }
298 }
299 i = e
300 } else { i = i + 1 }
301 }
302 }
303 }
304 off = off + reclen
305 }
306 }
307 }
308 }
309 sys_close(fdB)
310 }
311 dix = dix + 1
312 }
313
314 var dark: i64 = 0
315 var debt: i64 = 0
316 var shown: i64 = 0
317 rm_puts("=== nx_adopt -- ADOPTION CENSUS: primitives nobody calls (seq1410) ===\n" as *u8)
318 var sl: i64 = 0
319 while sl < AD_CAP {
320 if h_hash[sl] != 0 {
321 if ad_is_adopted(h_ext[sl]) == 0 {
322 dark = dark + 1
323 // Only PUBLIC-surface names are printed: those are the actionable set (seq1436). A dark
324 // module-local helper is CORRECT DESIGN, and listing it would drown the real debt.
325 if ad_is_debt(h_ext[sl], ad_is_public(arena, h_noff[sl], h_nlen[sl])) == 1 {
326 debt = debt + 1
327 if shown < AD_MAXREPORT {
328 rm_puts(" DEBT " as *u8)
329 var k: i64 = 0
330 while k < h_nlen[sl] { sys_write(1, ((arena as i64) + h_noff[sl] + k) as *u8, 1); k = k + 1 }
331 rm_puts(" self_refs=" as *u8); rm_num(h_self[sl])
332 rm_puts(" external=0\n" as *u8)
333 shown = shown + 1
334 }
335 }
336 }
337 }
338 sl = sl + 1
339 }
340 // ---- PROVEN_UNWIRED: gates call it, production does not -------------------------------------
341 // STRICTLY WORSE THAN AN UNCALLED PRIMITIVE: someone paid to BUILD it AND to PROVE it, and it
342 // protects nothing. Invisible to the DEBT verdict above because external>0 (the gate IS external).
343 // A GATE PROVES A CAPABILITY WORKS; ONLY A PRODUCTION CALLER PROVES IT RUNS.
344 var unwired: i64 = 0
345 var ushown: i64 = 0
346 rm_puts("--- PROVEN_UNWIRED: gate-proven, ZERO production callers ---\n" as *u8)
347 var ul: i64 = 0
348 while ul < AD_CAP {
349 if h_hash[ul] != 0 {
350 if h_proof[ul] > 0 {
351 if h_ext[ul] - h_proof[ul] == 0 {
352 if ad_is_public(arena, h_noff[ul], h_nlen[ul]) == 1 {
353 unwired = unwired + 1
354 if ushown < AD_MAXREPORT {
355 rm_puts(" UNWIRED " as *u8)
356 var uk: i64 = 0
357 while uk < h_nlen[ul] { sys_write(1, ((arena as i64) + h_noff[ul] + uk) as *u8, 1); uk = uk + 1 }
358 rm_puts(" proof_callers=" as *u8); rm_num(h_proof[ul])
359 rm_puts(" production_callers=0 self_refs=" as *u8); rm_num(h_self[ul])
360 rm_puts("\n" as *u8)
361 ushown = ushown + 1
362 }
363 }
364 }
365 }
366 }
367 ul = ul + 1
368 }
369 // Restore the caller's stdout so the SUMMARY is delivered to whoever asked, not buried in the report.
370 if ad_savefd >= 0 { sys_dup3(ad_savefd, 1, 0) }
371 rm_puts("NX-ADOPT defs=" as *u8); rm_num(ndef)
372 rm_puts(" def_files=" as *u8); rm_num(nfiles)
373 rm_puts(" scanned_files=" as *u8); rm_num(scanned)
374 rm_puts(" DARK_total=" as *u8); rm_num(dark)
375 rm_puts(" DEBT_public=" as *u8); rm_num(debt)
376 rm_puts(" shown=" as *u8); rm_num(shown)
377 rm_puts(" PROVEN_UNWIRED=" as *u8); rm_num(unwired)
378 rm_puts(" oversize_files=" as *u8); rm_num(truncated)
379 rm_puts("\n" as *u8)
380 rm_puts("COVERAGE: FLAT dirs only (no recursion); a name defined twice counts once; DARK = ZERO\n" as *u8)
381 rm_puts("external callers -- self-references excluded BY DESIGN (that is the nx_hw_cpu_count trap).\n" as *u8)
382 // THE ADOPTION AXIS, DERIVED HERE so no consumer has to re-run a 17k-file scan to learn one number.
383 // adoption_permil = defs that SOMEONE ELSE calls / all defs. It is the live source for the ADOPTION
384 // axis of nx_capaxes, and by construction it can only LOWER a capability headline, never raise it.
385 var adopt_permil: i64 = 0
386 if ndef > 0 { adopt_permil = (ndef - dark) * 1000 / ndef }
387 rm_puts("ADOPTION_PERMIL=" as *u8); rm_num(adopt_permil)
388 rm_puts(" = externally-called defs / total defs -- the ADOPTION axis for nx_capaxes.\n" as *u8)
389 let sfd: i64 = sys_openat_wr(AD_STAMP, 420)
390 if sfd >= 0 {
391 var back: i64 = 0 - 1
392 if sys_dup3(1, AD_STAMPFD, 0) >= 0 { back = AD_STAMPFD }
393 sys_dup3(sfd, 1, 0)
394 rm_puts("NX-ADOPT-STAMP defs=" as *u8); rm_num(ndef)
395 rm_puts(" dark=" as *u8); rm_num(dark)
396 rm_puts(" debt_public=" as *u8); rm_num(debt)
397 rm_puts(" proven_unwired=" as *u8); rm_num(unwired)
398 rm_puts(" adoption_permil=" as *u8); rm_num(adopt_permil)
399 rm_puts("\n" as *u8)
400 if back >= 0 { sys_dup3(back, 1, 0) }
401 }
402 if ad_savefd >= 0 {
403 rm_puts("REPORT: the full DEBT + PROVEN_UNWIRED rows went to argv[3] -- read it with nx_fs.\n" as *u8)
404 rm_puts(" PROVEN_UNWIRED is the one that matters: gate-proven capability with ZERO production\n" as *u8)
405 rm_puts(" callers -- BUILT and GATED and never called, which scores as done and behaves as absent.\n" as *u8)
406 }
407 return 0
408}