code wiki / _hdl_build / nx_eco_graph_honesty.nx

nx_eco_graph_honesty.nx source

↩ module page · 309 lines · 14358 B

1// nx_eco_graph_honesty.nx -- THE HONESTY SYSTEM (operator 2026-07-16: "the graph or family tree or whatever 2// we call it is the ultimate honesty system"): a full-population mechanical audit of the ecosystem graph that 3// FLAGS, by name, what a polite dashboard hides: 4// [W] WEAK POINTS -- high blast-radius organs with ZERO validation (untested load-bearers, ranked) 5// [A] AMPLIFIERS -- small direct fan-in but huge transitive reach (hidden exponential ripple) 6// [C] COUPLING -- fan-out outliers (import too much; single-responsibility suspects) 7// [I] ISLANDS -- true floaters: not even undirected-connected to the god component 8// [R] RESEARCH SPACE-- orphan mass clustered by prefix (abandoned/unexplored wings, ranked) 9// Composes the eco_graph store; complements nx_eco_graph_arch (cycles/PC/fan-in/depth = the shape) -- 10// this is the per-organ WORKLIST layer. Also the drift-ratchet source: every count here is comparable run-over-run. 11// Usage: nx_eco_graph_honesty <store-prefix> [instfile] license_tier: ORIGINAL expect_exit:0 12import "nx_syscalls.nx" 13import "nx_eco_graph.nx" 14import "nx_gate_verdict.nx" 15 16func hw2(s: *u8) -> i64 { var n:i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 17func hn2(v: i64) -> i64 { let b:*u8=sys_mmap(24); var m:i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var j:i64=0; while j<k{b[j]=t[k-1-j];j=j+1} sys_write(1,b,k); return 0 } 18func hname(g: *EcoGraph, idx: i64) -> i64 { 19 let off: i64 = g.node_off[idx] 20 var n: i64 = 0 21 while g.arena[off+n] != (0 as u8) { n = n + 1 } 22 sys_write(1, ((g.arena as i64)+off) as *u8, n) 23 return 0 24} 25// validation-suffix check (_test/_gate/_bench/_demo/_probe/_smoke .nx) -- byte-wise on arena 26func histe(g: *EcoGraph, idx: i64) -> i64 { 27 let off: i64 = g.node_off[idx] 28 var ln: i64 = 0 29 while g.arena[off+ln] != (0 as u8) { ln = ln + 1 } 30 if ln > 8 { 31 let tl: i64 = off + ln - 8 32 if g.arena[tl]==(95 as u8) { if g.arena[tl+1]==(116 as u8) { if g.arena[tl+2]==(101 as u8) { if g.arena[tl+3]==(115 as u8) { if g.arena[tl+4]==(116 as u8) { return 1 } } } } } 33 if g.arena[tl]==(95 as u8) { if g.arena[tl+1]==(103 as u8) { if g.arena[tl+2]==(97 as u8) { if g.arena[tl+3]==(116 as u8) { if g.arena[tl+4]==(101 as u8) { return 1 } } } } } 34 if g.arena[tl]==(95 as u8) { if g.arena[tl+1]==(100 as u8) { if g.arena[tl+2]==(101 as u8) { if g.arena[tl+3]==(109 as u8) { if g.arena[tl+4]==(111 as u8) { return 1 } } } } } 35 } 36 if ln > 7 { 37 let t7: i64 = off + ln - 7 38 if g.arena[t7]==(95 as u8) { if g.arena[t7+1]==(107 as u8) { if g.arena[t7+2]==(97 as u8) { if g.arena[t7+3]==(116 as u8) { return 1 } } } } // _kat.nx (Known Answer Test) 39 } 40 if ln > 9 { 41 let t9: i64 = off + ln - 9 42 if g.arena[t9]==(95 as u8) { if g.arena[t9+1]==(98 as u8) { if g.arena[t9+2]==(101 as u8) { if g.arena[t9+3]==(110 as u8) { if g.arena[t9+4]==(99 as u8) { if g.arena[t9+5]==(104 as u8) { return 1 } } } } } } 43 if g.arena[t9]==(95 as u8) { if g.arena[t9+1]==(112 as u8) { if g.arena[t9+2]==(114 as u8) { if g.arena[t9+3]==(111 as u8) { if g.arena[t9+4]==(98 as u8) { if g.arena[t9+5]==(101 as u8) { return 1 } } } } } } 44 if g.arena[t9]==(95 as u8) { if g.arena[t9+1]==(115 as u8) { if g.arena[t9+2]==(109 as u8) { if g.arena[t9+3]==(111 as u8) { if g.arena[t9+4]==(107 as u8) { if g.arena[t9+5]==(101 as u8) { return 1 } } } } } } 45 } 46 return 0 47} 48// prefix key: bytes up to (and incl) the 2nd '_' (e.g. "nx_game_") capped 24; returns len 49func hprefix(g: *EcoGraph, idx: i64, out: *u8) -> i64 { 50 let off: i64 = g.node_off[idx] 51 var i: i64 = 0 52 var us: i64 = 0 53 while g.arena[off+i] != (0 as u8) { 54 if i >= 24 { out[i] = 0 as u8; return i } 55 out[i] = g.arena[off+i] 56 if g.arena[off+i] == (95 as u8) { us = us + 1; if us == 2 { out[i+1] = 0 as u8; return i+1 } } 57 i = i + 1 58 } 59 out[i] = 0 as u8 60 return i 61} 62 63// substring search: 1 if needle occurs in buf[0..len) 64func h_contains(buf: *u8, len: i64, needle: *u8, nlen: i64) -> i64 { 65 if nlen <= 0 { return 0 } 66 var i: i64 = 0 67 while i <= len - nlen { 68 var j: i64 = 0 69 var m: i64 = 1 70 while j < nlen { if buf[i+j] != needle[j] { m = 0; j = nlen } else { j = j + 1 } } 71 if m == 1 { return 1 } 72 i = i + 1 73 } 74 return 0 75} 76// build "runtime/<name>" (name = arena string incl .nx) into out; returns len 77func h_srcpath(g: *EcoGraph, idx: i64, pre: *u8, out: *u8) -> i64 { 78 var o: i64 = 0 79 var k: i64 = 0 80 while pre[k] != (0 as u8) { out[o] = pre[k]; o = o + 1; k = k + 1 } 81 let off: i64 = g.node_off[idx] 82 k = 0 83 while g.arena[off+k] != (0 as u8) { out[o] = g.arena[off+k]; o = o + 1; k = k + 1 } 84 out[o] = 0 as u8 85 return o 86} 87// 1 if the organ SELF-TESTS in its own main(): source has "func main" AND a gate marker ("PASS"). Reads 88// runtime/<name>, then runtime/_hdl_build/<name>. Distinguishes genuine-untested [W] from extract-debt [S]. 89func h_selftest(g: *EcoGraph, idx: i64) -> i64 { 90 let path: *u8 = sys_mmap(256) 91 let lp: *i64 = sys_mmap(16) as *i64 92 h_srcpath(g, idx, "runtime/\x00" as *u8, path) 93 lp[0] = 0 94 var buf: *u8 = sys_read_file(path, lp) 95 if lp[0] <= 0 { 96 h_srcpath(g, idx, "runtime/_hdl_build/\x00" as *u8, path) 97 lp[0] = 0 98 buf = sys_read_file(path, lp) 99 } 100 if lp[0] <= 0 { return 0 - 1 } // source not found -> unknown 101 if h_contains(buf, lp[0], "func main" as *u8, 9) == 0 { return 0 } 102 if h_contains(buf, lp[0], "PASS" as *u8, 4) == 1 { return 1 } 103 if h_contains(buf, lp[0], " GATE" as *u8, 5) == 1 { return 1 } 104 return 0 105} 106 107func main(argc: i64, argv: *i64) -> i64 { 108 var store: *u8 = "knowledge/store/ecograph_full" as *u8 109 if argc >= 2 { store = argv[1] as *u8 } 110 let g: *EcoGraph = eg_load(store) 111 if (g as i64) == 0 { hw2("ERROR store not found\n" as *u8); return 3 } 112 let n: i64 = g.node_count 113 hw2("=== NX-ECO-HONESTY: the ultimate honesty system -- full-population audit ===\n" as *u8) 114 115 // per-node basics: is_test, direct tests, direct real children 116 let iste: *u8 = sys_mmap(n+2) 117 var i: i64 = 0 118 while i < n { iste[i] = histe(g, i) as u8; i = i + 1 } 119 let tcnt: *i64 = sys_mmap((n+2)*8) as *i64 120 let rcnt: *i64 = sys_mmap((n+2)*8) as *i64 121 i = 0 122 while i < n { 123 var tc: i64 = 0 124 var rc: i64 = 0 125 var p: i64 = g.in_head[i] 126 while p < g.in_head[i+1] { 127 if iste[g.in_list[p]] == (1 as u8) { tc = tc + 1 } else { rc = rc + 1 } 128 p = p + 1 129 } 130 tcnt[i] = tc 131 rcnt[i] = rc 132 i = i + 1 133 } 134 // transitive reach (blast radius) per node -- stamp BFS on in-edges 135 let stamp: *i64 = sys_mmap((n+2)*8) as *i64 136 let stk: *i64 = sys_mmap((n+2)*8) as *i64 137 let reach: *i64 = sys_mmap((n+2)*8) as *i64 138 i = 0; while i < n { stamp[i] = 0 - 1; i = i + 1 } 139 var s: i64 = 0 140 while s < n { 141 var r: i64 = 0 142 var sp: i64 = 0 143 stk[0] = s 144 stamp[s] = s 145 while sp >= 0 { 146 let v: i64 = stk[sp] 147 sp = sp - 1 148 var p2: i64 = g.in_head[v] 149 while p2 < g.in_head[v+1] { 150 let w: i64 = g.in_list[p2] 151 if stamp[w] != s { stamp[w] = s; r = r + 1; sp = sp + 1; stk[sp] = w } 152 p2 = p2 + 1 153 } 154 } 155 reach[s] = r 156 s = s + 1 157 } 158 159 // Collect the top-40 untested-by-reach candidates (tcnt==0, not a test), then SPLIT by whether the 160 // organ self-tests in main() -- so [W] = GENUINE gaps and [S] = tests exist but in the wrong place. 161 // (The edge-count proxy alone false-positives self-testing organs; the source scan corrects it.) 162 let cand: *i64 = sys_mmap(48*8) as *i64 163 var ncand: i64 = 0 164 while ncand < 40 { 165 var best: i64 = 0 - 1 166 var bestr: i64 = 0 - 1 167 i = 0 168 while i < n { 169 if iste[i] == (0 as u8) { if tcnt[i] == 0 { if reach[i] > bestr { if reach[i] >= 0 { best = i; bestr = reach[i] } } } } 170 i = i + 1 171 } 172 if best < 0 { ncand = 40 } else { cand[ncand] = best; reach[best] = 0 - 1 - reach[best]; ncand = ncand + 1 } 173 } 174 i = 0; while i < n { if reach[i] < 0 { reach[i] = 0 - 1 - reach[i] } i = i + 1 } 175 // classify: st[k] = 1 self-tested, 0 genuine, -1 unknown 176 let stf: *i64 = sys_mmap(48*8) as *i64 177 var ci: i64 = 0 178 while ci < ncand { stf[ci] = h_selftest(g, cand[ci]); ci = ci + 1 } 179 180 hw2("\n[W] WEAK POINTS -- GENUINELY untested load-bearers (no gate, no in-file self-test):\n" as *u8) 181 var wshown: i64 = 0 182 ci = 0 183 while ci < ncand { 184 if stf[ci] == 0 { if wshown < 15 { 185 let idx: i64 = cand[ci] 186 hw2(" reach=" as *u8); hn2(reach[idx]); hw2(" " as *u8); hname(g, idx) 187 hw2(" (direct children=" as *u8); hn2(rcnt[idx]); hw2(")\n" as *u8) 188 wshown = wshown + 1 189 } } 190 ci = ci + 1 191 } 192 if wshown == 0 { hw2(" (none in the top 40 -- the real worklist is clear)\n" as *u8) } 193 194 hw2("\n[S] SELF-TESTED but no external gate node -- EXTRACT-DEBT (tests live in main(); move to a _gate/_kat):\n" as *u8) 195 var sshown: i64 = 0 196 ci = 0 197 while ci < ncand { 198 if stf[ci] == 1 { if sshown < 12 { 199 let idx2: i64 = cand[ci] 200 hw2(" reach=" as *u8); hn2(reach[idx2]); hw2(" " as *u8); hname(g, idx2); hw2("\n" as *u8) 201 sshown = sshown + 1 202 } } 203 ci = ci + 1 204 } 205 if sshown == 0 { hw2(" (none)\n" as *u8) } 206 207 // [A] AMPLIFIERS: direct fan-in <= 3 but transitive reach >= 200 (hidden exponential ripple) 208 hw2("\n[A] AMPLIFIERS -- tiny direct fan-in, huge transitive reach (hidden ripple):\n" as *u8) 209 var na: i64 = 0 210 i = 0 211 while i < n { 212 let din: i64 = g.in_head[i+1] - g.in_head[i] 213 if din > 0 { if din <= 3 { if reach[i] >= 200 { if na < 12 { 214 hw2(" direct=" as *u8); hn2(din); hw2(" reach=" as *u8); hn2(reach[i]); hw2(" " as *u8); hname(g, i); hw2("\n" as *u8) 215 } na = na + 1 } } } 216 i = i + 1 217 } 218 hw2(" total amplifiers=" as *u8); hn2(na); hw2("\n" as *u8) 219 220 // [C] COUPLING: fan-out outliers (imports too many parents; single-responsibility suspects) 221 hw2("\n[C] COUPLING -- fan-out outliers (organ imports too much):\n" as *u8) 222 var nc2: i64 = 0 223 i = 0 224 while i < n { 225 let dout: i64 = g.out_head[i+1] - g.out_head[i] 226 if dout >= 14 { if nc2 < 12 { 227 hw2(" imports=" as *u8); hn2(dout); hw2(" " as *u8); hname(g, i); hw2("\n" as *u8) 228 } nc2 = nc2 + 1 } 229 i = i + 1 230 } 231 hw2(" total organs importing >=14 parents=" as *u8); hn2(nc2); hw2("\n" as *u8) 232 233 // [I] ISLANDS: undirected BFS from the max-fan-in node (the god component); unvisited = floaters 234 var god: i64 = 0 235 i = 0 236 while i < n { if (g.in_head[i+1]-g.in_head[i]) > (g.in_head[god+1]-g.in_head[god]) { god = i } i = i + 1 } 237 let vis: *u8 = sys_mmap(n+2) 238 i = 0; while i < n { vis[i] = 0 as u8; i = i + 1 } 239 var sp4: i64 = 0 240 stk[0] = god 241 vis[god] = 1 as u8 242 while sp4 >= 0 { 243 let v4: i64 = stk[sp4] 244 sp4 = sp4 - 1 245 var p4: i64 = g.in_head[v4] 246 while p4 < g.in_head[v4+1] { let w4: i64 = g.in_list[p4]; if vis[w4] == (0 as u8) { vis[w4] = 1 as u8; sp4 = sp4 + 1; stk[sp4] = w4 } p4 = p4 + 1 } 247 p4 = g.out_head[v4] 248 while p4 < g.out_head[v4+1] { let w5: i64 = g.out_list[p4]; if vis[w5] == (0 as u8) { vis[w5] = 1 as u8; sp4 = sp4 + 1; stk[sp4] = w5 } p4 = p4 + 1 } 249 } 250 var nisl: i64 = 0 251 hw2("\n[I] ISLANDS -- not even undirected-connected to the god component (true floaters):\n" as *u8) 252 i = 0 253 while i < n { 254 if vis[i] == (0 as u8) { if nisl < 20 { hw2(" " as *u8); hname(g, i); hw2("\n" as *u8) } nisl = nisl + 1 } 255 i = i + 1 256 } 257 hw2(" total island organs=" as *u8); hn2(nisl); hw2("\n" as *u8) 258 259 // [R] RESEARCH SPACE: orphan-ish (no children at all, not a test) clustered by prefix, top 12 260 // small prefix table: up to 512 prefixes, linear probe by bytes 261 let pkey: *u8 = sys_mmap(512*26) 262 let pcnt2: *i64 = sys_mmap(514*8) as *i64 263 var np: i64 = 0 264 let tmp: *u8 = sys_mmap(32) 265 i = 0 266 while i < n { 267 let din2: i64 = g.in_head[i+1] - g.in_head[i] 268 if din2 == 0 { if iste[i] == (0 as u8) { 269 let pl: i64 = hprefix(g, i, tmp) 270 var f: i64 = 0 - 1 271 var k: i64 = 0 272 while k < np { 273 var eq: i64 = 1 274 var q: i64 = 0 275 while q <= pl { if pkey[k*26+q] != tmp[q] { eq = 0; q = pl } q = q + 1 } 276 if eq == 1 { f = k; k = np } 277 k = k + 1 278 } 279 if f < 0 { if np < 512 { var q2: i64 = 0; while q2 <= pl { pkey[np*26+q2] = tmp[q2]; q2 = q2 + 1 } pcnt2[np] = 1; np = np + 1 } } else { pcnt2[f] = pcnt2[f] + 1 } 280 } } 281 i = i + 1 282 } 283 hw2("\n[R] RESEARCH SPACE -- childless non-test organs clustered by prefix (abandoned/unexplored wings):\n" as *u8) 284 var rsh: i64 = 0 285 while rsh < 12 { 286 var bp: i64 = 0 - 1 287 var bpc: i64 = 0 288 var k3: i64 = 0 289 while k3 < np { if pcnt2[k3] > bpc { bpc = pcnt2[k3]; bp = k3 } k3 = k3 + 1 } 290 if bp < 0 { rsh = 12 } else { 291 hw2(" " as *u8) 292 var q3: i64 = 0 293 while pkey[bp*26+q3] != (0 as u8) { q3 = q3 + 1 } 294 sys_write(1, ((pkey as i64)+bp*26) as *u8, q3) 295 hw2("* floaters=" as *u8); hn2(bpc); hw2("\n" as *u8) 296 pcnt2[bp] = 0 - 1 297 rsh = rsh + 1 298 } 299 } 300 hw2("\nNX-ECO-HONESTY done -- ratchet these counts; worklists: [W]->hardening [I,R]->janitor/research [C]->refactor.\n" as *u8) 301 let ctr: *i64 = gv_ctr() 302 var wt: i64 = 0 303 if wshown == 0 { wt = 1 } 304 gv_check("T1 zero GENUINELY-untested load-bearers in the top-40 blast radius" as *u8, wt, ctr) 305 var st2: i64 = 0 306 if sshown == 0 { st2 = 1 } 307 gv_check("T2 zero self-tested-but-ungated load-bearers (extract-debt clear)" as *u8, st2, ctr) 308 return gv_verdict("ECO-HONESTY" as *u8, ctr, "load-bearing organs all externally gated; audit worklists ratcheted" as *u8) 309}