code wiki / _hdl_build / nx_dep_audit.nx

nx_dep_audit.nx source

↩ module page · 264 lines · 11661 B

1// nx_dep_audit.nx -- the ENGINEER+DOCTOR dangling-dep probe (operator 2026-06-13: "build the nishi 2// engineer to one step less than ten seconds [and] have the capabilities to address the errors"). 3// 4// A DANGLING DEP is a `deps` token (field 6) that names NO real queue row. The Engineer FINDS, the 5// Doctor ROOT-CAUSES: one <10s pass classifies each so the finding is actionable, not noise: 6// STATUS-IN-DEPS a lifecycle word (TODO/DONE/...) -- a SIL-umbrella VARIANT whose columns are 7// (parent/program, status) where normal rows are (status, deps); an_core mis-reads 8// it. NOT a missing foundation (e.g. GENEALOGIST-SIL: field5=NOORPHAN-SIL is the 9// real parent, field6=TODO is the status). 10// CROSS-REGISTRY a real row in callouts.tsv (CALLOUT-*) or cred_requests.tsv (CR-*) -- a valid 11// cross-registry dependency, not an error. 12// ORGAN-IN-DEPS an organ name (nx_*) where a ROW-ID belongs -- a category error. 13// LIKELY-RENAME a real queue row shares a strong prefix -> the dep was renamed/typo'd (suggest it). 14// MISSING no match anywhere -- a genuinely absent foundation, file for the owner. 15// TRUE-DANGLING = ORGAN + RENAME + MISSING (STATUS-IN-DEPS = schema variant; CROSS-REGISTRY = valid). 16// It SURFACES + DIAGNOSES + SUGGESTS; it never guess-edits deps (re-point/normalise needs intent). 17// 18// Composes nx_assign_core (DRY: the queue loader + an_streq). SELF-VALIDATING: baked pos (real-row 19// dep -> 0) + neg (ghost dep -> 1). Sub-second single pass. Wired into the pulse (tp_tail_c) as the 20// deps-coherence gate, beside the two no-floating gates (capability_ladder, root_trace). license_tier: ORIGINAL 21import "nx_assign_core.nx" 22const DA_MAGIC_262144: i64 = 262144 23const DA_MAGIC_262143: i64 = 262143 24const DA_MAGIC_4000: i64 = 4000 25 26const DA_QUEUE: *u8 = "knowledge/registry/assignment_queue.tsv" 27const DA_CALLOUTS: *u8 = "knowledge/registry/callouts.tsv" 28const DA_CREDS: *u8 = "knowledge/registry/cred_requests.tsv" 29const DA_LOG: *u8 = "knowledge/status/dep_audit.log" 30const DA_XCAP: i64 = 48 31 32func da_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 33func da_wn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8 = sys_mmap(28); 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 i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(fd, bb, k); return 0 } 34 35func da_has_dot(s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { if s[i] == (46 as u8) { return 1 } i = i + 1 } return 0 } 36 37func da_status_str(b: i64) -> *u8 { 38 if b == 68 { return "DONE" as *u8 } 39 if b == 84 { return "TODO" as *u8 } 40 if b == 78 { return "NOVEL" as *u8 } 41 if b == 87 { return "WIP" as *u8 } 42 if b == 71 { return "GEN" as *u8 } 43 if b == 82 { return "RED" as *u8 } 44 return "?" as *u8 45} 46 47func da_is_status_word(t: *u8) -> i64 { 48 if an_streq(t, "TODO" as *u8) == 1 { return 1 } 49 if an_streq(t, "DONE" as *u8) == 1 { return 1 } 50 if an_streq(t, "NOVEL" as *u8) == 1 { return 1 } 51 if an_streq(t, "WIP" as *u8) == 1 { return 1 } 52 if an_streq(t, "GEN" as *u8) == 1 { return 1 } 53 if an_streq(t, "RED" as *u8) == 1 { return 1 } 54 if an_streq(t, "KILLED" as *u8) == 1 { return 1 } 55 if an_streq(t, "VIEW" as *u8) == 1 { return 1 } 56 if an_streq(t, "SIL-META" as *u8) == 1 { return 1 } 57 return 0 58} 59 60// load field0 (id) of every non-comment row of a TSV into arena; returns count. 61func da_load_ids(path: *u8, arena: i64, idcap: i64, maxc: i64) -> i64 { 62 let buf: *u8 = sys_mmap(DA_MAGIC_262144) 63 let fd: i64 = sys_openat_rd(path) 64 if fd < 0 { return 0 } 65 var n: i64 = 0 66 var r: i64 = sys_read(fd, buf, DA_MAGIC_262143) 67 while r > 0 { n = n + r; r = sys_read(fd, buf + n, DA_MAGIC_262143 - n) } 68 sys_close(fd) 69 var cnt: i64 = 0 70 var i: i64 = 0 71 while i < n { 72 if buf[i] == (35 as u8) { 73 var sk: i64 = 1 74 while sk == 1 { if i >= n { sk = 0 } else { if buf[i] == (10 as u8) { sk = 0 } i = i + 1 } } 75 } else { 76 var fe: i64 = i 77 var scan: i64 = 1 78 while scan == 1 { if fe >= n { scan = 0 } else { if buf[fe] == (9 as u8) { scan = 0 } else { if buf[fe] == (10 as u8) { scan = 0 } else { fe = fe + 1 } } } } 79 let flen: i64 = fe - i 80 if flen > 0 { if flen < idcap { if cnt < maxc { 81 let dst: *u8 = (arena + cnt * idcap) as *u8 82 var k: i64 = 0 83 while k < flen { dst[k] = buf[i + k]; k = k + 1 } 84 dst[flen] = 0 as u8 85 cnt = cnt + 1 86 } } } 87 var le: i64 = fe 88 var s2: i64 = 1 89 while s2 == 1 { if le >= n { s2 = 0 } else { if buf[le] == (10 as u8) { s2 = 0 } else { le = le + 1 } } } 90 i = le + 1 91 } 92 } 93 return cnt 94} 95 96func da_in_set(arena: i64, cnt: i64, idcap: i64, name: *u8) -> i64 { 97 var i: i64 = 0 98 while i < cnt { if an_streq((arena + i * idcap) as *u8, name) == 1 { return 1 } i = i + 1 } 99 return 0 100} 101 102func da_pfx(a: *u8, b: *u8) -> i64 { 103 var i: i64 = 0 104 while a[i] != (0 as u8) { 105 if b[i] == (0 as u8) { return i } 106 if a[i] != b[i] { return i } 107 i = i + 1 108 } 109 return i 110} 111func da_best(cx: *i64, dep: *u8, selfr: i64) -> i64 { 112 var best: i64 = 0 - 1 113 var bestlen: i64 = 0 114 var j: i64 = 0 115 while j < cx[0] { 116 if j != selfr { 117 let p: i64 = da_pfx(dep, an_id_at(cx, j)) 118 if p > bestlen { bestlen = p; best = j } 119 } 120 j = j + 1 121 } 122 if bestlen >= 4 { return best } 123 return 0 - 1 124} 125 126// classify a NON-cross-registry dangling token: 0=STATUS-IN-DEPS 1=ORGAN 2=RENAME 3=MISSING 127func da_class(cx: *i64, tok: *u8, r: i64) -> i64 { 128 if da_is_status_word(tok) == 1 { return 0 } 129 if tok[0] == (110 as u8) { if tok[1] == (120 as u8) { if tok[2] == (95 as u8) { return 1 } } } // "nx_" 130 if da_best(cx, tok, r) >= 0 { return 2 } 131 return 3 132} 133func da_class_str(c: i64) -> *u8 { 134 if c == 0 { return "STATUS-IN-DEPS(sil-variant)" as *u8 } 135 if c == 1 { return "ORGAN-IN-DEPS" as *u8 } 136 if c == 2 { return "LIKELY-RENAME" as *u8 } 137 if c == 4 { return "CROSS-REGISTRY(callout/cred)" as *u8 } 138 return "MISSING" as *u8 139} 140 141func da_finding_to(fd: i64, cx: *i64, r: i64, tok: *u8, cls: i64) -> i64 { 142 let st: *i64 = cx[4] as *i64 143 let best: i64 = da_best(cx, tok, r) 144 da_w(fd, "DEPAUDIT row=" as *u8); da_w(fd, an_id_at(cx, r)) 145 da_w(fd, " field5=" as *u8); da_w(fd, da_status_str(st[r])) 146 da_w(fd, " dangling-dep=" as *u8); da_w(fd, tok) 147 da_w(fd, " class=" as *u8); da_w(fd, da_class_str(cls)) 148 da_w(fd, " suggest=" as *u8) 149 if best >= 0 { if cls == 2 { da_w(fd, an_id_at(cx, best)) } else { da_w(fd, "NONE" as *u8) } } else { da_w(fd, "NONE" as *u8) } 150 da_w(fd, "\n" as *u8) 151 return 0 152} 153 154// count + classify + (emit) the dangling deps of row r; counts[cls]++ per dangling (5 classes). 155func da_row_danglings(cx: *i64, r: i64, lfd: i64, emit: i64, counts: *i64, cross: i64, ncross: i64) -> i64 { 156 let dd: *u8 = an_deps_at(cx, r) 157 if dd[0] == (45 as u8) { if dd[1] == (0 as u8) { return 0 } } // "-" 158 let tok: *u8 = sys_mmap(64) 159 var i: i64 = 0 160 var k: i64 = 0 161 var cnt: i64 = 0 162 var go: i64 = 1 163 while go == 1 { 164 let ch: i64 = dd[i] as i64 165 var fin: i64 = 0 166 if ch == 0 { fin = 1 } 167 if ch == 44 { fin = 1 } 168 if fin == 1 { 169 if k > 0 { 170 tok[k] = 0 as u8 171 var skip: i64 = 0 172 if tok[0] == (45 as u8) { if tok[1] == (0 as u8) { skip = 1 } } 173 if da_has_dot(tok) == 1 { skip = 1 } 174 if skip == 0 { 175 if an_find(cx, tok) < 0 { 176 cnt = cnt + 1 177 var cls: i64 = 4 178 if da_in_set(cross, ncross, DA_XCAP, tok) == 0 { cls = da_class(cx, tok, r) } 179 counts[cls] = counts[cls] + 1 180 if emit == 1 { 181 da_finding_to(1, cx, r, tok, cls) 182 if lfd >= 0 { da_finding_to(lfd, cx, r, tok, cls) } 183 } 184 } 185 } 186 k = 0 187 } 188 if ch == 0 { go = 0 } 189 } else { if k < 63 { tok[k] = ch as u8; k = k + 1 } } 190 i = i + 1 191 } 192 return cnt 193} 194 195// synthetic 1-dep test (no cross set): row "B" deps=<dep> + real row "A". returns B's dangling count. 196func da_test(dep: *u8) -> i64 { 197 let t: *i64 = an_newcx() 198 t[0] = 2 199 let a0: *u8 = an_id_at(t, 0); a0[0] = 65 as u8; a0[1] = 0 as u8 200 let d0: *u8 = an_deps_at(t, 0); d0[0] = 45 as u8; d0[1] = 0 as u8 201 let a1: *u8 = an_id_at(t, 1); a1[0] = 66 as u8; a1[1] = 0 as u8 202 let d1: *u8 = an_deps_at(t, 1) 203 var i: i64 = 0 204 while dep[i] != (0 as u8) { d1[i] = dep[i]; i = i + 1 } 205 d1[i] = 0 as u8 206 let scratch: *i64 = sys_mmap(8 * 8) as *i64 207 return da_row_danglings(t, 1, 0 - 1, 0, scratch, 0, 0) 208} 209 210func da_gate(fd: i64, cpos: i64, cneg: i64, rows: i64, counts: *i64, ok: i64) -> i64 { 211 let total: i64 = counts[0] + counts[1] + counts[2] + counts[3] + counts[4] 212 let truedang: i64 = counts[1] + counts[2] + counts[3] 213 da_w(fd, "DEPAUDIT-GATE authored=organ control_pos=" as *u8); da_wn(fd, cpos) 214 da_w(fd, " control_neg=" as *u8); da_wn(fd, cneg) 215 da_w(fd, " rows=" as *u8); da_wn(fd, rows) 216 da_w(fd, " dangling_total=" as *u8); da_wn(fd, total) 217 da_w(fd, " sil_variant=" as *u8); da_wn(fd, counts[0]) 218 da_w(fd, " cross_registry=" as *u8); da_wn(fd, counts[4]) 219 da_w(fd, " organ_in_deps=" as *u8); da_wn(fd, counts[1]) 220 da_w(fd, " likely_rename=" as *u8); da_wn(fd, counts[2]) 221 da_w(fd, " missing=" as *u8); da_wn(fd, counts[3]) 222 da_w(fd, " TRUE_DANGLING=" as *u8); da_wn(fd, truedang) 223 if truedang == 0 { da_w(fd, " deps_coherent=YES" as *u8) } else { da_w(fd, " deps_coherent=NO" as *u8) } 224 if ok == 1 { da_w(fd, " verdict=GREEN\n" as *u8) } else { da_w(fd, " verdict=RED\n" as *u8) } 225 return 0 226} 227 228func main() -> i64 { 229 let cpos: i64 = da_test("A" as *u8) 230 let cneg: i64 = da_test("GHOST" as *u8) 231 var ok: i64 = 1 232 if cpos != 0 { ok = 0 } 233 if cneg != 1 { ok = 0 } 234 let zc: *i64 = sys_mmap(8 * 8) as *i64 235 if ok == 0 { 236 da_gate(1, cpos, cneg, 0, zc, 0) 237 let lf0: i64 = sys_openat_append(DA_LOG, 420) 238 if lf0 >= 0 { da_gate(lf0, cpos, cneg, 0, zc, 0); sys_close(lf0) } 239 sys_exit(1); return 1 240 } 241 242 let cx: *i64 = an_newcx() 243 let n: i64 = an_load(DA_QUEUE, cx) 244 if n <= 0 { da_w(1, "DEPAUDIT-GATE verdict=RED reason=queue-missing\n" as *u8); sys_exit(1); return 1 } 245 246 // cross-registry id set: callouts + cred_requests (valid non-queue dep targets) 247 let cross: i64 = sys_mmap(DA_MAGIC_4000 * DA_XCAP) as i64 248 var ncross: i64 = da_load_ids(DA_CALLOUTS, cross, DA_XCAP, DA_MAGIC_4000) 249 let n2: i64 = da_load_ids(DA_CREDS, (cross + ncross * DA_XCAP), DA_XCAP, DA_MAGIC_4000 - ncross) 250 ncross = ncross + n2 251 252 let counts: *i64 = sys_mmap(8 * 8) as *i64 253 var ci: i64 = 0 254 while ci < 5 { counts[ci] = 0; ci = ci + 1 } 255 let lf: i64 = sys_openat_append(DA_LOG, 420) 256 var r: i64 = 0 257 while r < n { 258 da_row_danglings(cx, r, lf, 1, counts, cross, ncross) 259 r = r + 1 260 } 261 da_gate(1, cpos, cneg, n, counts, 1) 262 if lf >= 0 { da_gate(lf, cpos, cneg, n, counts, 1); sys_close(lf) } 263 sys_exit(0); return 0 264}