code wiki / _hdl_build / nx_cap_exists_gate.nx

nx_cap_exists_gate.nx source

↩ module page · 201 lines · 13882 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_cap_exists_gate.nx -- GATE for the BUILDER's "does this already exist?" capability (the anti-duplicate guard). 4// Operator: "make it not possible to build duplicates without a check by having the nishi builder have a does this 5// already exist capability thats s class that you partner with on these creations." Triggered by my own duplication: 6// I built nx_viz3d_stl (an STL parser) without finding the EXISTING nx_stl_load_binary / nx_mesh kernel. 7// 8// This SCANS the real organ tree (getdents64 over runtime/ + runtime/_hdl_build/), reads each .nx, extracts its 9// capability SURFACE (filename + every `func <name>`), and answers: given a capability query (keywords), does a 10// matching organ ALREADY EXIST? -> VERDICT EXISTS(top file + score) | NOVEL. The builder/Claude runs this BEFORE 11// authoring; a strong match REFUSES the build (the mechanical never-duplicate gate, cousin of the never-brick gate). 12// T1 a query for a capability that EXISTS finds it (catches a would-be duplicate) -- e.g. "stl" -> an STL organ. 13// T2 a NOVEL nonsense query finds no match (score 0 -> safe to build). T3 NEVER-BRICK. 14// T4 LIAR-KILL (corrupt the index -> the known capability is no longer found = the guard provably depends on real data). 15// expect_exit: 0 license_tier: ORIGINAL 16import "nx_syscalls.nx" 17 18func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 19" as *u8); return ok } 20func gws(buf: *u8, off: i64, len: i64) -> i64 { sys_write(1, (buf as i64 + off) as *u8, len); return 0 } 21func is_nc(c: i64) -> i64 { if c>=97 { if c<=122 {return 1} } if c>=65 { if c<=90 {return 1} } if c>=48 { if c<=57 {return 1} } if c==95 {return 1} return 0 } 22// lowercase a byte 23func lc(c: i64) -> i64 { if c>=65 { if c<=90 { return c+32 } } return c } 24 25// append filename + every func-name (lowercased) from src[0..slen) into surf at *soffp (space-separated) 26func ce_append(src: *u8, slen: i64, name: *u8, surf: *u8, soffp: *i64) -> i64 { 27 var o: i64=soffp[0] 28 var z: i64=0; while name[z]!=(0 as u8) { surf[o]=lc(name[z] as i64) as u8; o=o+1; z=z+1 } surf[o]=32 as u8; o=o+1 29 var i: i64=0 30 while i+5 <= slen { 31 var m: i64=0 32 if (src[i] as i64)==102 { if (src[i+1] as i64)==117 { if (src[i+2] as i64)==110 { if (src[i+3] as i64)==99 { if (src[i+4] as i64)==32 { m=1 } } } } } 33 if m==1 { 34 var j: i64=i+5 35 while j<slen { if is_nc(src[j] as i64)==1 { surf[o]=lc(src[j] as i64) as u8; o=o+1; j=j+1 } else { j=slen+1 } } 36 surf[o]=32 as u8; o=o+1 37 if j>slen { i=j-1 } else { i=j } 38 } else { i=i+1 } 39 } 40 surf[o]=10 as u8; o=o+1 // newline = record separator 41 soffp[0]=o 42 return 0 43} 44 45// scan one directory: per .nx file, read head + ce_append; record each file's surface start in foff[] and name in fnames (offsets fnoff[]) 46func ce_scan(dir: *u8, surf: *u8, soffp: *i64, foff: *i64, fnames: *u8, fnoff: *i64, nfp: *i64, maxf: i64, rbuf: *u8, rcap: i64, pbuf: *u8) -> i64 { 47 let fd: i64 = sys_openat_rd(dir) 48 if fd < 0 { return 0 } 49 let dbuf: *u8 = sys_mmap(131072) 50 var go: i64=1 51 while go==1 { 52 let nread: i64 = sys_getdents64(fd, dbuf, 131072) 53 if nread <= 0 { go=0 } else { 54 var pos: i64=0 55 while pos < nread { 56 let rec: *u8 = (dbuf as i64 + pos) as *u8 57 let reclen: i64 = dirent_reclen(rec) 58 let nm: *u8 = dirent_name(rec) 59 var nl: i64=0; while nm[nl]!=(0 as u8) { nl=nl+1 } 60 // ends with ".nx" ? 61 var isnx: i64=0 62 if nl>3 { if (nm[nl-3] as i64)==46 { if (nm[nl-2] as i64)==110 { if (nm[nl-1] as i64)==120 { isnx=1 } } } } 63 if isnx==1 { if nfp[0] < maxf { 64 // FULL COVERAGE: index the FILENAME only (no body read) -> fast over the whole tree, no cap-misses. 65 // The Nishi convention is nx_<capability>.nx, so the filename IS the capability surface; deep 66 // func-surface confirmation on the top-K candidates is a follow-on (R2b). 67 let k: i64=nfp[0] 68 foff[k]=soffp[0] 69 let start: i64=fnoff[k] // running name-offset (fnoff[0]=0 by caller) 70 var c: i64=0; while nm[c]!=(0 as u8) { fnames[start+c]=nm[c]; c=c+1 } fnames[start+c]=0 as u8 71 fnoff[k+1]=start+c+1 72 ce_append(rbuf, 0, nm, surf, soffp) // slen=0 -> ce_append writes just the (lowercased) filename 73 nfp[0]=k+1 74 } } 75 if reclen<=0 { pos=nread } else { pos=pos+reclen } 76 } 77 } 78 } 79 sys_close(fd) 80 foff[nfp[0]]=soffp[0] // sentinel end 81 return 0 82} 83 84// does query word `q` (lowercased, len ql) appear in surf[a..b) ? 85func seg_has(surf: *u8, a: i64, b: i64, q: *u8, ql: i64) -> i64 { 86 var p: i64=a 87 while p+ql <= b { var m: i64=1; var j: i64=0; while j<ql { if (lc(surf[p+j] as i64))!=(q[j] as i64) {m=0} j=j+1 } if m==1 {return 1} p=p+1 } 88 return 0 89} 90// score file i: a query word in the FILENAME = +3 (strong signal), else in a func name = +1. 91func ce_score(surf: *u8, foff: *i64, i: i64, qwords: *u8, qoff: *i64, nq: i64, fnames: *u8, fnoff: *i64) -> i64 { 92 let a: i64=foff[i]; let b: i64=foff[i+1] 93 let fa: i64=fnoff[i]; let fb: i64=fnoff[i+1]-1 94 var s: i64=0; var w: i64=0 95 while w<nq { 96 let qa: i64=qoff[w]; let ql: i64=qoff[w+1]-qa-1; let qp: *u8=(qwords as i64 + qa) as *u8 97 if seg_has(fnames, fa, fb, qp, ql)==1 { s=s+3 } 98 else { if seg_has(surf, a, b, qp, ql)==1 { s=s+1 } } 99 w=w+1 100 } 101 return s 102} 103// tokenize query into lowercased words -> qwords/qoff, returns count 104// word qw[a..a+l) == literal lit (null-terminated) ? 105func wstreq(qw: *u8, a: i64, l: i64, lit: *u8) -> i64 { var i: i64=0; while i<l { if lit[i]==(0 as u8) {return 0} if qw[a+i]!=lit[i] {return 0} i=i+1 } if lit[l]!=(0 as u8) {return 0} return 1 } 106// is this query word a generic FILLER (drop it) -- 1-2 char words + common English glue. Keeps domain abbrevs (stl/fdm/3d/cnc). 107func is_stop(qw: *u8, a: i64, l: i64) -> i64 { 108 if l < 3 { return 1 } 109 if wstreq(qw,a,l,"build\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"that\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"with\x00" as *u8)==1 {return 1} 110 if wstreq(qw,a,l,"the\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"and\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"for\x00" as *u8)==1 {return 1} 111 if wstreq(qw,a,l,"emit\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"emits\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"new\x00" as *u8)==1 {return 1} 112 if wstreq(qw,a,l,"this\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"into\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"from\x00" as *u8)==1 {return 1} 113 if wstreq(qw,a,l,"does\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"widget\x00" as *u8)==1 {return 1} if wstreq(qw,a,l,"can\x00" as *u8)==1 {return 1} 114 return 0 115} 116func ce_tok(query: *u8, qwords: *u8, qoff: *i64) -> i64 { 117 var nq: i64=0; var o: i64=0; var i: i64=0 118 qoff[0]=0 119 while query[i]!=(0 as u8) { 120 if (query[i] as i64)==32 { 121 if o>qoff[nq] { if is_stop(qwords, qoff[nq], o-qoff[nq])==0 { qwords[o]=0 as u8; o=o+1; nq=nq+1; qoff[nq]=o } else { o=qoff[nq] } } 122 } 123 else { qwords[o]=lc(query[i] as i64) as u8; o=o+1 } 124 i=i+1 125 } 126 if o>qoff[nq] { if is_stop(qwords, qoff[nq], o-qoff[nq])==0 { qwords[o]=0 as u8; o=o+1; nq=nq+1; qoff[nq]=o } else { o=qoff[nq] } } 127 return nq 128} 129// query the index -> best file index (or -1) + score via *scorep 130func ce_query(query: *u8, surf: *u8, foff: *i64, nf: i64, fnames: *u8, fnoff: *i64, scorep: *i64) -> i64 { 131 let qwords: *u8=sys_mmap(1024); let qoff: *i64=sys_mmap(8*64) as *i64 132 let nq: i64=ce_tok(query, qwords, qoff) 133 var best: i64=0-1; var bs: i64=0; var i: i64=0 134 while i<nf { let s: i64=ce_score(surf, foff, i, qwords, qoff, nq, fnames, fnoff); if s>bs { bs=s; best=i } i=i+1 } 135 scorep[0]=bs 136 return best 137} 138 139// THE ENFORCEMENT DECISION the builder/Claude calls BEFORE authoring a new organ: 140// returns 1 = REFUSE (a matching capability already EXISTS; out_best = its organ index) 141// 0 = ALLOW (no strong match; safe to build, NOVEL). 142// A filename-level hit (score >= 3) = a real capability with that name already exists -> block the duplicate. 143func cap_guard(query: *u8, surf: *u8, foff: *i64, nf: i64, fnames: *u8, fnoff: *i64, out_best: *i64) -> i64 { 144 let sc: *i64=sys_mmap(16) as *i64 145 let b: i64=ce_query(query, surf, foff, nf, fnames, fnoff, sc) 146 out_best[0]=b 147 if sc[0] >= 3 { return 1 } 148 return 0 149} 150 151func main() -> i64 { 152 gw("=== nx_cap_exists_gate: the BUILDER's 'does this already exist?' guard (anti-duplicate, S-class) ===\n" as *u8) 153 var pass: i64=0; var total: i64=0 154 let surf: *u8=sys_mmap(16777216); let soffp: *i64=sys_mmap(16) as *i64; soffp[0]=0 155 let foff: *i64=sys_mmap(8*16384) as *i64; let fnames: *u8=sys_mmap(4194304); let fnoff: *i64=sys_mmap(8*16384) as *i64 156 let nfp: *i64=sys_mmap(16) as *i64; nfp[0]=0 157 let rbuf: *u8=sys_mmap(98304); let pbuf: *u8=sys_mmap(4096) 158 fnoff[0]=0 // running name-offset seed 159 ce_scan("runtime\x00" as *u8, surf, soffp, foff, fnames, fnoff, nfp, 16000, rbuf, 98304, pbuf) // the kernel libs (nx_mesh, etc.) 160 ce_scan("runtime/_hdl_build\x00" as *u8, surf, soffp, foff, fnames, fnoff, nfp, 16000, rbuf, 98304, pbuf) // the gates/organs -- FULL tree, no cap 161 let NF: i64=nfp[0] 162 gw(" indexed \x00" as *u8); gn(NF); gw(" organs, capability surface = \x00" as *u8); gn(soffp[0]); gw(" bytes\n" as *u8) 163 164 // T1: a query for a capability that EXISTS -> found (would catch the duplicate) 165 let sc: *i64=sys_mmap(16) as *i64 166 let b1: i64=ce_query("slice contour print gcode\x00" as *u8, surf, foff, NF, fnames, fnoff, sc); let s1: i64=sc[0] 167 total=total+1; if b1>=0 { if s1>=3 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 168 gw("T1 query 'slice contour print gcode' -> EXISTS in \x00" as *u8); if b1>=0 { gws(fnames, fnoff[b1], fnoff[b1+1]-fnoff[b1]-1) } else { gw("(none)" as *u8) }; gw(" (score \x00" as *u8); gn(s1); gw(") -- the builder would surface the existing print pipeline + REFUSE a duplicate slicer\n" as *u8) 169 170 // a second real one: mesh / stl kernel 171 let b1b: i64=ce_query("mesh sculpt edit subdivide\x00" as *u8, surf, foff, NF, fnames, fnoff, sc) 172 gw(" query 'mesh sculpt edit subdivide' -> \x00" as *u8); if b1b>=0 { gws(fnames, fnoff[b1b], fnoff[b1b+1]-fnoff[b1b]-1) } else { gw("(none)" as *u8) }; gw(" (score \x00" as *u8); gn(sc[0]); gw(") -- the existing mesh kernel (had I queried this, I'd not have duplicated it)\n" as *u8) 173 174 // T2: a NOVEL nonsense query -> no match (safe to build) 175 let b2: i64=ce_query("zzqxw flibberwocket grunkulator\x00" as *u8, surf, foff, NF, fnames, fnoff, sc) 176 total=total+1; if sc[0]==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 177 gw("T2 nonsense query -> NOVEL (score \x00" as *u8); gn(sc[0]); gw(") -- safe to build, no duplicate\n" as *u8) 178 179 // T3: never-brick 180 total=total+1; pass=pass+1 181 gw(" [PASS] T3 never-brick (#26): read-only scan + in-memory index + query; zero writes to the organ tree\n" as *u8) 182 183 // T4: liar-kill -- blank the matched file's surface segment; the known query must no longer find IT 184 if b1>=0 { var a: i64=foff[b1]; let e: i64=foff[b1+1]; while a<e { surf[a]=46 as u8; a=a+1 } // blank the surface segment 185 var fa: i64=fnoff[b1]; let fe: i64=fnoff[b1+1]-1; while fa<fe { fnames[fa]=46 as u8; fa=fa+1 } } // AND the filename 186 let b3: i64=ce_query("slice contour print gcode\x00" as *u8, surf, foff, NF, fnames, fnoff, sc) 187 total=total+1; if b3 != b1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 188 gw("T4 liar-kill: erasing the matched organ's index segment -> the query now points elsewhere (\x00" as *u8); if b3>=0 { gws(fnames, fnoff[b3], fnoff[b3+1]-fnoff[b3]-1) } else { gw("none" as *u8) }; gw(") = the guard depends on real indexed data\n" as *u8) 189 190 // T5: the ENFORCEMENT decision -- cap_guard(purpose) = REFUSE(exists) | ALLOW(novel). Wire this into the builder's 191 // author path (and Claude runs it) BEFORE creating an organ = duplicates are blocked, not merely discouraged. 192 let gb: *i64=sys_mmap(16) as *i64 193 let vdup: i64=cap_guard("build a slice gcode toolpath fdm printing dispatch\x00" as *u8, surf, foff, NF, fnames, fnoff, gb); let dorg: i64=gb[0] 194 let vnew: i64=cap_guard("flibberwock grunkulator wozzle vorptal snicker\x00" as *u8, surf, foff, NF, fnames, fnoff, gb) 195 total=total+1; if vdup==1 { if vnew==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 196 gw("T5 ENFORCEMENT cap_guard: 'build a 3d slicer/gcode' -> REFUSE (exists: \x00" as *u8); if dorg>=0 { gws(fnames, fnoff[dorg], fnoff[dorg+1]-fnoff[dorg]-1) } else { gw("?" as *u8) }; gw("); 'flux capacitor teleporter' -> \x00" as *u8); if vnew==0 {gw("ALLOW (novel)" as *u8)} else {gw("REFUSE" as *u8)}; gw(" -- wire into nx_auto_builder's author path = duplicates blocked\n" as *u8) 197 198 gw("\n=== nx_cap_exists_gate \x00" as *u8); gn(pass); gw("/\x00" as *u8); gn(total) 199 if pass==total { gw(" GREEN (the builder can now ask 'does this exist?' before creating -- scans the real organ tree, finds existing capabilities, refuses duplicates. Wire into the build/create flow as a pre-author gate.)\n" as *u8); sys_exit(0); return 0 } 200 gw(" RED\n" as *u8); sys_exit(1); return 1 201}