code wiki / _hdl_build / nx_wirecensus.nx

nx_wirecensus.nx source

↩ module page · 283 lines · 15849 B

1// nx_wirecensus.nx -- THE NISHI KNIP, RUNG 1 (S4: registered but never called). 2// 3// nx_wirecensus [allowlist] [cap_consent.log] [actlog.jrnl] 4// defaults: tool_allowlist.conf cap_consent.log knowledge/status/actlog.jrnl 5// 6// WHY ONE ORGAN AND NOT FOUR (July-2026 SOTA, id=1785451391): Knip finds unused files, exports, 7// dependencies and devDependencies in ONE run over a whole monorepo -- ~300k weekly downloads, and 8// Vercel deleted ~300k lines with it. Our equivalent is FRAGMENTED across nx_adopt (functions), 9// nx_dupfunc (duplicate bodies), nx_srcdiverge (trees) and nx_toolreg_reconcile (registry): four 10// organs, four invocations, no single verdict -- and a census nobody runs in one look is a census 11// nobody runs. This organ is the consolidation point. Rung 1 is S4 because it is the class our 12// STATIC censuses structurally cannot see, and because the field's biggest win (16k lines, 35 pct of 13// an Express codebase) came from exactly this cross-reference: live evidence x declared surface. 14// 15// THE SIX CLASSES, so the shape is on the record even where unimplemented: 16// S1 defined, zero EXTERNAL callers ............ nx_adopt answers this today 17// S2 in SOURCE but in no shipped BINARY ........ open 18// S3 built+promoted but never REGISTERED ....... open 19// S4 registered but never AUTHORISED or CALLED . THIS ORGAN 20// S5 conf row present, no consumer re-read it .. nx_toolreg_reconcile now upserts on drift 21// S6 on the NAS but absent from the SSOT tree .. nx_srcdiverge_gate exists, stranded 22// 23// REFUSES RATHER THAN BLESSES: no allowlist, or zero registered tools, exits 2. "0 dark" computed 24// from an empty declared surface reads as a clean bill of health while guaranteeing nothing. 25// license_tier: ORIGINAL Read-only. No hw writes (Rule 26). expect_exit: 0 26import "nx_wirecensus_lib.nx" 27 28const WCO_OUT: i64 = 262144 29const WCO_LIST: i64 = 60 30 31func wco_cat(o: *u8, at: i64, s: *u8) -> i64 { 32 var a: i64 = at 33 var i: i64 = 0 34 while s[i] != (0 as u8) { o[a] = s[i]; a = a + 1; i = i + 1 } 35 return a 36} 37func wco_num(o: *u8, at: i64, v: i64) -> i64 { 38 if v == 0 { o[at] = 48 as u8; return at + 1 } 39 let t: *u8 = sys_mmap(32) 40 var n: i64 = 0 41 var x: i64 = v 42 while x > 0 { t[n] = ((x % 10) + 48) as u8; x = x / 10; n = n + 1 } 43 var a: i64 = at 44 while n > 0 { n = n - 1; o[a] = t[n]; a = a + 1 } 45 return a 46} 47 48func main(argc: i64, argv: *i64) -> i64 { 49 let o: *u8 = sys_mmap(WCO_OUT) 50 var b: i64 = 0 51 52 // EMIT MODE: propose organ_kind.conf rows that are DERIVABLE FROM EVIDENCE, never guessed. 53 // WHY THIS IS NEEDED: declaration coverage is 45 permil (35 of 762), and BOTH /api/promote and 54 // /api/deploy refuse an undeclared organ by design -- so 95 pct of promoted artifacts cannot be 55 // shipped through the sanctioned path without someone stopping to declare them first. That 56 // refusal blocked this session twice. 57 // ONLY THE SAFE DIRECTION IS EMITTED. organ_kind.conf's own header states the asymmetry: 58 // misfiling a DAEMON as a one-shot lets promote swap a live binary with no health probe (a 59 // rule-26 hazard); misfiling a one-shot as a daemon merely BLOCKS it. So: 60 // registered in tool_allowlist -> oneshot : it is fork-exec'd per call BY CONSTRUCTION 61 // name ends _gate/_test/_kat -> oracle : this is the SYSTEM'S OWN RULE, not a convention 62 // I invented -- /api/gate_run refuses any target 63 // whose name does not end in gate, test or kat 64 // DAEMONS ARE NEVER EMITTED. That class needs a human or a live-listener probe, and it is the 65 // only class where being wrong is dangerous. Emitting is read-only; appending stays deliberate. 66 if argc > 1 { 67 let v: *u8 = argv[1] as *u8 68 if v[0] == (101 as u8) { 69 let ab2: *u8 = sys_mmap(WC_BUF) 70 let an3: i64 = wc_read("tool_allowlist.conf" as *u8, ab2, WC_BUF) 71 if an3 <= 0 { 72 b = wco_cat(o, b, "EMIT REFUSED -- cannot read tool_allowlist.conf\n" as *u8) 73 sys_write(2, o, b); return 2 74 } 75 let rn2: *u8 = sys_mmap(WC_MAXT * WC_NAMEMAX) 76 let rl2: *i64 = sys_mmap(WC_MAXT * 8) as *i64 77 let tr2: *i64 = sys_mmap(16) as *i64 78 tr2[0] = 0 79 let rc2: i64 = wc_parse_registered(ab2, an3, rn2, rl2, tr2) 80 let kb2: *u8 = sys_mmap(WC_BUF) 81 let kn2: i64 = wc_read("knowledge/status/organ_kind.conf" as *u8, kb2, WC_BUF) 82 b = wco_cat(o, b, "# DERIVED organ_kind rows -- evidence-backed, SAFE DIRECTION ONLY.\n# oneshot = registered in tool_allowlist (fork-exec'd per call by construction)\n# oracle = name ends _gate/_test/_kat (the /api/gate_run bound, the system's own rule)\n# NO daemon rows are emitted: that is the one class where a wrong row is a rule-26 hazard.\n# Review, then append. Rows already present in organ_kind.conf are skipped.\n" as *u8) 83 var emitted: i64 = 0 84 var q: i64 = 0 85 while q < rc2 { 86 let p: *u8 = (rn2 as i64 + q * WC_NAMEMAX) as *u8 87 let l: i64 = rl2[q] 88 var already: i64 = 0 89 if kn2 > 0 { if wc_contains_name(kb2, kn2, p, l) == 1 { already = 1 } } 90 if already == 0 { 91 b = wco_cat(o, b, p) 92 if wc_looks_oracle(p, l) == 1 { b = wco_cat(o, b, " oracle\n" as *u8) } 93 else { b = wco_cat(o, b, " oneshot\n" as *u8) } 94 emitted = emitted + 1 95 } 96 q = q + 1 97 } 98 b = wco_cat(o, b, "# emitted=" as *u8); b = wco_num(o, b, emitted) 99 b = wco_cat(o, b, " of registered=" as *u8); b = wco_num(o, b, rc2) 100 b = wco_cat(o, b, " (skipped=already declared)\n" as *u8) 101 sys_write(1, o, b) 102 return 0 103 } 104 } 105 106 var apath: *u8 = "tool_allowlist.conf" as *u8 107 var cpath: *u8 = "cap_consent.log" as *u8 108 var lpath: *u8 = "knowledge/status/actlog.jrnl" as *u8 109 if argc > 1 { apath = argv[1] as *u8 } 110 if argc > 2 { cpath = argv[2] as *u8 } 111 if argc > 3 { lpath = argv[3] as *u8 } 112 113 let abuf: *u8 = sys_mmap(WC_BUF) 114 let an: i64 = wc_read(apath, abuf, WC_BUF) 115 if an <= 0 { 116 b = wco_cat(o, b, "WIRECENSUS REFUSED -- cannot read the tool allowlist: " as *u8) 117 b = wco_cat(o, b, apath) 118 b = wco_cat(o, b, "\n Without the DECLARED surface there is nothing to audit, and reporting\n '0 dark' from an empty expectation would read as a clean bill of health.\n" as *u8) 119 sys_write(2, o, b); return 2 120 } 121 122 let rn: *u8 = sys_mmap(WC_MAXT * WC_NAMEMAX) 123 let rl: *i64 = sys_mmap(WC_MAXT * 8) as *i64 124 let tr: *i64 = sys_mmap(16) as *i64 125 tr[0] = 0 126 let rc: i64 = wc_parse_registered(abuf, an, rn, rl, tr) 127 if rc == 0 { 128 b = wco_cat(o, b, "WIRECENSUS REFUSED -- the allowlist parsed to ZERO registered tools.\n" as *u8) 129 sys_write(2, o, b); return 2 130 } 131 132 // W1 STRONG (a cap is mandatory to invoke) | W2 WEAK-POSITIVE (voluntary logging) 133 let cbuf: *u8 = sys_mmap(WC_BUF) 134 // TAIL-ANCHORED (2026-08-07): cap_consent.log and actlog.jrnl are APPEND-ONLY, so when either 135 // outgrows WC_BUF the head-anchored read kept the oldest bytes and dropped the newest -- inflating 136 // DARK by hiding exactly the most recent evidence. actlog was measured at 1262 permil of the cap. 137 let cn: i64 = wc_read_tail(cpath, cbuf, WC_BUF) 138 let an2: *u8 = sys_mmap(WC_MAXT * WC_NAMEMAX) 139 let al2: *i64 = sys_mmap(WC_MAXT * 8) as *i64 140 var ac: i64 = 0 141 if cn > 0 { ac = wc_parse_authorised(cbuf, cn, an2, al2, tr) } 142 143 let lbuf: *u8 = sys_mmap(WC_BUF) 144 let ln: i64 = wc_read_tail(lpath, lbuf, WC_BUF) 145 let in2: *u8 = sys_mmap(WC_MAXT * WC_NAMEMAX) 146 let il2: *i64 = sys_mmap(WC_MAXT * 8) as *i64 147 var ic: i64 = 0 148 if ln > 0 { ic = wc_parse_invoked(lbuf, ln, in2, il2, tr) } 149 150 let dk: *i64 = sys_mmap(WC_MAXT * 8) as *i64 151 let d: i64 = wc_dark(rn, rl, rc, an2, al2, ac, in2, il2, ic, dk, WC_MAXT) 152 153 b = wco_cat(o, b, "=== NISHI WIRECENSUS -- S4: registered but never called ===\n registered=" as *u8) 154 b = wco_num(o, b, rc) 155 b = wco_cat(o, b, " authorised(W1 cap mints)=" as *u8); b = wco_num(o, b, ac) 156 b = wco_cat(o, b, " invoked(W2 actlog)=" as *u8); b = wco_num(o, b, ic) 157 b = wco_cat(o, b, "\n DARK=" as *u8); b = wco_num(o, b, d) 158 if rc > 0 { 159 b = wco_cat(o, b, " dark_permil=" as *u8) 160 b = wco_num(o, b, (d * 1000) / rc) 161 } 162 b = wco_cat(o, b, " table_truncated=" as *u8); b = wco_num(o, b, tr[0]) 163 if cn <= 0 { b = wco_cat(o, b, "\n ! W1 ABSENT: cap_consent.log unreadable -- DARK is inflated, treat as UNMEASURED" as *u8) } 164 if ln <= 0 { b = wco_cat(o, b, "\n ! W2 ABSENT: actlog unreadable -- tools proven only by invocation cannot be rescued" as *u8) } 165 b = wco_cat(o, b, "\n BOUND: DARK is an UPPER BOUND. Caps minted before this log existed, or minted with the\n root nx_cap_mint on the NAS instead of POST /api/cap/mint, never reach cap_consent.log; and\n seats log to actlog VOLUNTARILY. Quote the bound whenever you quote the number.\n" as *u8) 166 167 // ---- S3: promoted artifacts nobody registered ------------------------------------------------ 168 let en: *u8 = sys_mmap(WC_MAXT * WC_NAMEMAX) 169 let el: *i64 = sys_mmap(WC_MAXT * 8) as *i64 170 let ec: i64 = wc_scan_elfs("." as *u8, en, el, tr) 171 let gn: *u8 = sys_mmap(WC_MAXT * WC_NAMEMAX) 172 let gl: *i64 = sys_mmap(WC_MAXT * 8) as *i64 173 let gc: i64 = wc_parse_registered_elfs(abuf, an, gn, gl, tr) 174 let kbuf: *u8 = sys_mmap(WC_BUF) 175 let kn: i64 = wc_read("knowledge/status/organ_kind.conf" as *u8, kbuf, WC_BUF) 176 let nn: *u8 = sys_mmap(WC_MAXT * WC_NAMEMAX) 177 let nl: *i64 = sys_mmap(WC_MAXT * 8) as *i64 178 var nc: i64 = 0 179 if kn > 0 { nc = wc_parse_nontools(kbuf, kn, nn, nl, tr) } 180 let s3: *i64 = sys_mmap(WC_MAXT * 8) as *i64 181 let n3: i64 = wc_dark(en, el, ec, gn, gl, gc, nn, nl, nc, s3, WC_MAXT) 182 183 b = wco_cat(o, b, "\n=== S3: built + promoted but NEVER REGISTERED ===\n promoted_elfs=" as *u8) 184 b = wco_num(o, b, ec) 185 b = wco_cat(o, b, " registered_elfs=" as *u8); b = wco_num(o, b, gc) 186 b = wco_cat(o, b, " declared_nontool(daemon|lib)=" as *u8); b = wco_num(o, b, nc) 187 b = wco_cat(o, b, "\n UNEXPOSED=" as *u8); b = wco_num(o, b, n3) 188 if kn <= 0 { b = wco_cat(o, b, "\n ! organ_kind.conf unreadable -- daemons/libs CANNOT be excluded, UNEXPOSED is inflated" as *u8) } 189 b = wco_cat(o, b, "\n (a promoted artifact that is neither registered NOR declared daemon/lib is callable by\n nobody -- nx_srcdiverge_gate is the canonical instance: promoted, unregistered, and it is\n itself the detector for a stranding class)\n" as *u8) 190 // TRIAGE: a raw UNEXPOSED count mixes running daemons with real strays. Split it by CONVENTION 191 // (reporting only -- organ_kind.conf remains the sole authority for any promote decision) so the 192 // residue is the only thing a human has to look at. 193 // S7 evidence: an artifact that WRITES A LOG has run, even if the registry never exposed it. 194 let ln7: *u8 = sys_mmap(WC_MAXT * WC_NAMEMAX) 195 let ll7: *i64 = sys_mmap(WC_MAXT * 8) as *i64 196 // CORRECT witness first: tools named in column 1 of the plan- planes. Log stems are kept as a 197 // weak secondary because they cost nothing, but the plan planes are what actually joins. 198 var lc7: i64 = wc_scan_plan_tools("knowledge/store" as *u8, ln7, ll7, 0, tr) 199 let plan_tools: i64 = lc7 200 lc7 = wc_scan_logs("logs" as *u8, ln7, ll7, lc7, tr) 201 lc7 = wc_scan_logs("knowledge/status" as *u8, ln7, ll7, lc7, tr) 202 let lc7b: i64 = lc7 203 var looks_o: i64 = 0 204 var looks_d: i64 = 0 205 var s7run: i64 = 0 206 var unclass: i64 = 0 207 var j: i64 = 0 208 while j < n3 { 209 if j < WC_MAXT { 210 let p: *u8 = (en as i64 + s3[j] * WC_NAMEMAX) as *u8 211 let l: i64 = el[s3[j]] 212 if wc_looks_oracle(p, l) == 1 { looks_o = looks_o + 1 } else { 213 if wc_looks_daemon(p, l) == 1 { looks_d = looks_d + 1 } else { 214 // S7 RESCUE before condemning: a log with this stem proves it ran. 215 if wc_has(ln7, ll7, lc7b, p, l) == 1 { s7run = s7run + 1 } else { unclass = unclass + 1 } 216 } 217 } 218 } 219 j = j + 1 220 } 221 b = wco_cat(o, b, " TRIAGE (convention-inferred, REPORTING ONLY -- organ_kind.conf stays authoritative):\n oracle-shaped(_gate/_test/_kat)=" as *u8) 222 b = wco_num(o, b, looks_o) 223 b = wco_cat(o, b, " daemon-shaped(_daemon/_serve/_gw)=" as *u8); b = wco_num(o, b, looks_d) 224 b = wco_cat(o, b, "\n S7 plan/cron-INVOKED (named in a plan- plane, so a schedule CALLS it)=" as *u8); b = wco_num(o, b, s7run) 225 b = wco_cat(o, b, " [plan-named tools=" as *u8); b = wco_num(o, b, plan_tools) 226 b = wco_cat(o, b, " +log stems, total witnesses=" as *u8); b = wco_num(o, b, lc7b) 227 b = wco_cat(o, b, "]\n UNCLASSIFIED=" as *u8); b = wco_num(o, b, unclass) 228 b = wco_cat(o, b, " <-- the honest S3 residue; these are the rows worth a human declaration\n" as *u8) 229 b = wco_cat(o, b, " (S7 reads COLUMN 1 of the plan- planes -- the tool a scheduled plan actually calls, e.g.\n plan-surfsentinel- = 10x nx_https_get. An earlier attempt used LOG STEMS and rescued ZERO from\n 224 stems: logs are named after the JOB, never the ORGAN, so they can never join. Rescue is\n POSITIVE-ONLY, so a loose match shrinks the residue and can never retire a live organ.\n UNCLASSIFIED still means UNKNOWN -- a plan is not the only way to be called.)\n" as *u8) 230 if gc > 0 { 231 b = wco_cat(o, b, " declaration_coverage_permil=" as *u8) 232 b = wco_num(o, b, (nc * 1000) / ec) 233 b = wco_cat(o, b, " (organ_kind.conf declares " as *u8); b = wco_num(o, b, nc) 234 b = wco_cat(o, b, " of " as *u8); b = wco_num(o, b, ec) 235 b = wco_cat(o, b, " promoted artifacts -- BOTH /api/promote and /api/deploy consult it)\n" as *u8) 236 } 237 var s3shown: i64 = 0 238 j = 0 239 while j < n3 { 240 if s3shown < WCO_LIST { 241 if j < WC_MAXT { 242 let p2: *u8 = (en as i64 + s3[j] * WC_NAMEMAX) as *u8 243 let l2: i64 = el[s3[j]] 244 if wc_looks_oracle(p2, l2) == 0 { if wc_looks_daemon(p2, l2) == 0 { 245 if wc_has(ln7, ll7, lc7b, p2, l2) == 0 { 246 b = wco_cat(o, b, " UNCLASSIFIED " as *u8) 247 b = wco_cat(o, b, p2) 248 b = wco_cat(o, b, "\n" as *u8) 249 s3shown = s3shown + 1 250 } 251 } } 252 } 253 } 254 j = j + 1 255 } 256 if unclass > s3shown { 257 b = wco_cat(o, b, " ... " as *u8); b = wco_num(o, b, unclass - s3shown) 258 b = wco_cat(o, b, " more UNCLASSIFIED not shown\n" as *u8) 259 } 260 b = wco_cat(o, b, "\n=== S4 DARK LIST ===\n" as *u8) 261 262 var shown: i64 = 0 263 var i: i64 = 0 264 while i < d { 265 if shown < WCO_LIST { 266 if i < WC_MAXT { 267 b = wco_cat(o, b, " DARK " as *u8) 268 b = wco_cat(o, b, (rn as i64 + dk[i] * WC_NAMEMAX) as *u8) 269 b = wco_cat(o, b, "\n" as *u8) 270 shown = shown + 1 271 } 272 } 273 i = i + 1 274 } 275 if d > shown { 276 b = wco_cat(o, b, " ... " as *u8); b = wco_num(o, b, d - shown) 277 b = wco_cat(o, b, " more DARK not shown (list bounded to " as *u8); b = wco_num(o, b, WCO_LIST) 278 b = wco_cat(o, b, ")\n" as *u8) 279 } 280 if d == 0 { b = wco_cat(o, b, " GREEN -- every registered tool has been authorised or invoked at least once.\n" as *u8) } 281 sys_write(1, o, b) 282 return 0 283}