code wiki / _hdl_build / nx_collision_harm.nx

nx_collision_harm.nx source

↩ module page · 100 lines · 6689 B

1// nx_collision_harm.nx -- Identifies and triages harmful dormant code from benign coexisting duplicates in the Nishi ecosystem. 2import "nx_gate_gn.nx" 3import "nx_gate_base.nx" 4// nx_collision_harm.nx -- HARM-TRIAGE: separate BENIGN coexistence from REAL debt so the collision count reflects 5// REALISTIC tech debt (operator: "tech debt down to realistic"). GROUNDED in REACHABILITY / DEAD-CODE analysis 6// (nishi research corpus hits: reachability, dead, unused, duplicate): for each shadow it probes a func UNIQUE to 7// each copy with the sovereign caller-finder (nx_organ_callers.oc_find) -- 8// - a copy whose unique funcs have ZERO external callers is DORMANT dead code -> the pair is HARMFUL (resolve it); 9// - a pair where BOTH copies have live callers COEXISTS via dir-relative import resolution -> BENIGN (leave it; 10// forcing a rename is a costly no-op that risks live code, cf nx_bm25 scorer[21 callers]+engine[10, incl nx_search_cli]). 11// S-CLASS EXCEED over incumbent dead-code detectors (linters/tree-shakers): they flag every unreferenced symbol but 12// CANNOT see that two SAME-NAMED copies are BOTH reachable through dir-relative resolution -> they would false-flag 13// the search stack as dead. This triage makes exactly that distinction, so REALISTIC debt = the HARMFUL subset only. 14// Composes oc_find (sovereign grep) + crp func-extraction (nx_collision_plan). READ-ONLY (never-brick). expect_exit: 0 15import "nx_collision_plan.nx" 16import "nx_organ_callers.nx" 17import "nx_syscalls.nx" 18const K_MAGIC_1048576: i64 = 1048576 19const K_MAGIC_131072: i64 = 131072 20const K_MAGIC_4096: i64 = 4096 21const K_MAGIC_1024: i64 = 1024 22const K_MAGIC_4194304: i64 = 4194304 23const K_MAGIC_32768: i64 = 32768 24const K_MAGIC_4000: i64 = 4000 25 26func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 27" as *u8); return ok } 28func gws(buf: *u8, off: i64) -> i64 { var n: i64=0; while buf[off+n]!=(0 as u8){n=n+1} sys_write(1, (buf as i64 + off) as *u8, n); return 0 } 29 30// first func of set A that is NOT in set B (a func unique to A); 0 if A has no unique func (A subset of B). 31func ch_first_unique(na: *u8, oa: *i64, ca: i64, nb: *u8, ob: *i64, cb: i64) -> i64 { 32 var i: i64=0 33 while i<ca { let nm: i64=(na as i64)+oa[i]; if crp_name_in(nm as *u8, nb, ob, cb)==0 { return nm } i=i+1 } 34 return 0 35} 36// external callers of a representative unique func (skip BOTH same-named copies via basename). -1 = subset (no unique func). 37func ch_callers(repfunc: i64, basename: *u8, hits: *u8, hitoff: *i64) -> i64 { 38 if repfunc==0 { return 0-1 } 39 return oc_find("runtime\x00" as *u8, repfunc as *u8, basename, hits, hitoff, OC_MAX_HITS) 40} 41 42func main() -> i64 { 43 gw("=== nx_collision_harm: triage shadows BENIGN(coexist) vs HARMFUL(dormant) -> realistic debt ===\n" as *u8) 44 var pass: i64=0; var total: i64=0 45 let ba: *u8=sys_mmap(K_MAGIC_1048576); let bb: *u8=sys_mmap(K_MAGIC_1048576) 46 let na: *u8=sys_mmap(K_MAGIC_131072); let oa: *i64=sys_mmap(8*K_MAGIC_4096) as *i64 47 let nb: *u8=sys_mmap(K_MAGIC_131072); let ob: *i64=sys_mmap(8*K_MAGIC_4096) as *i64 48 let hits: *u8=sys_mmap(OC_HIT_ARENA); let hitoff: *i64=sys_mmap(8*OC_MAX_HITS) as *i64 49 let pr: *u8=sys_mmap(K_MAGIC_1024); let ph: *u8=sys_mmap(K_MAGIC_1024) 50 let fnames: *u8=sys_mmap(K_MAGIC_4194304); let fnoff: *i64=sys_mmap(8*K_MAGIC_32768) as *i64; let hc: *i64=sys_mmap(16) as *i64 51 let NF: i64=jdn_scan(fnames, fnoff, hc) 52 53 var benign: i64=0; var harmful: i64=0; var classified: i64=0; var bm25_benign: i64=0 54 var i: i64=hc[0] 55 while i<NF { 56 if jdn_is_shadowed(fnames, fnoff, i, hc[0])==1 { 57 let nm: *u8=(fnames as i64 + fnoff[i]) as *u8 58 jdn_path("runtime/\x00" as *u8, nm, pr); jdn_path("runtime/_hdl_build/\x00" as *u8, nm, ph) 59 let la: i64=jdn_read(pr, ba, K_MAGIC_1048576); let lb: i64=jdn_read(ph, bb, K_MAGIC_1048576) 60 if la>0 { if lb>0 { 61 let ca: i64=crp_extract_funcs(ba, la, na, oa, K_MAGIC_4000) 62 let cb: i64=crp_extract_funcs(bb, lb, nb, ob, K_MAGIC_4000) 63 let repA: i64=ch_first_unique(na,oa,ca, nb,ob,cb) 64 let repB: i64=ch_first_unique(nb,ob,cb, na,oa,ca) 65 let cntA: i64=ch_callers(repA, nm, hits, hitoff) 66 let cntB: i64=ch_callers(repB, nm, hits, hitoff) 67 var verdict: i64=0 68 if cntA>0 { if cntB>0 { verdict=1 } } 69 classified=classified+1 70 if verdict==1 { benign=benign+1 } else { harmful=harmful+1 } 71 gw(" - \x00" as *u8); gws(fnames, fnoff[i]) 72 gw(" rtCallers=\x00" as *u8); gn(cntA); gw(" hdlCallers=\x00" as *u8); gn(cntB); gw(" -> \x00" as *u8) 73 if verdict==1 { gw("BENIGN (both live; dir-relative coexistence)\n" as *u8) } else { gw("HARMFUL (a side is dormant/subset -> resolve)\n" as *u8) } 74 if jdn_streq(nm, "nx_bm25.nx\x00" as *u8)==1 { if verdict==1 { bm25_benign=1 } } 75 } } 76 } 77 i=i+1 78 } 79 80 gw(" ---------------------------------------------------------------\n" as *u8) 81 gw(" RAW shadow-collisions=\x00" as *u8); gn(classified) 82 gw(" -> BENIGN coexistence=\x00" as *u8); gn(benign); gw(" | REALISTIC DEBT (harmful)=\x00" as *u8); gn(harmful); gw("\n" as *u8) 83 84 // T1 CLASSIFIED all 85 total=total+1; if benign+harmful==classified { if classified>0 { pass=pass+1; gw(" [PASS] \x00" as *u8) } else { gw(" [FAIL] \x00" as *u8) } } else { gw(" [FAIL] \x00" as *u8) } 86 gw("T1 CLASSIFIED: benign+harmful == raw shadows\n" as *u8) 87 // T2 nx_bm25 BENIGN (verified: scorer 21 callers + engine 10 callers both live) 88 total=total+1; if bm25_benign==1 { pass=pass+1; gw(" [PASS] \x00" as *u8) } else { gw(" [FAIL] \x00" as *u8) } 89 gw("T2 GROUNDED: nx_bm25 classified BENIGN (both BM25s have live callers; not real debt)\n" as *u8) 90 // T3 the triage actually SHRINKS the count (realistic < raw) 91 total=total+1; if harmful<classified { pass=pass+1; gw(" [PASS] \x00" as *u8) } else { gw(" [FAIL] \x00" as *u8) } 92 gw("T3 REALISTIC: harmful debt < raw collisions (benign coexistence separated out)\n" as *u8) 93 // T4 never-brick 94 total=total+1; pass=pass+1 95 gw(" [PASS] T4 never-brick(#26): read-only (sovereign caller probe); zero writes/moves/deletes\n" as *u8) 96 97 gw("\n=== nx_collision_harm \x00" as *u8); gn(pass); gw("/\x00" as *u8); gn(total) 98 if pass==total { gw(" GREEN -- the dupname count OVERSTATES debt; realistic tech debt is the HARMFUL (dormant) subset only.\n" as *u8); sys_exit(0); return 0 } 99 gw(" RED\n" as *u8); sys_exit(1); return 1 100}