code wiki / _hdl_build / nx_cap_exists.nx
nx_cap_exists.nx source
↩ module page · 132 lines · 7394 B
1// nx_cap_exists.nx -- LIB: the Nishi builder's "does this already exist?" anti-duplicate guard (importable).
2// Operator: "make it not possible to build duplicates without a check ... a does this already exist capability."
3// The builder (and Claude) call this BEFORE authoring an organ: `cap_index(ctx)` once builds an index of the whole
4// organ tree (every `.nx` filename = the nx_<capability> surface, 12k+ organs, fast: getdents only, no body reads),
5// then `cap_guard(query) -> 1 REFUSE (a matching capability EXISTS) | 0 ALLOW (novel)`. Filename-weighted, stopword-
6// filtered so only DOMAIN words score. NEVER-BRICK (#26): read-only scan + in-memory index, zero writes to the tree.
7// API: alloc the buffers, `cap_index(...)` -> NF, then `cap_query(q,...)/cap_guard(q,...)`. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9const K_MAGIC_131072: i64 = 131072
10const K_MAGIC_16000: i64 = 16000
11const K_MAGIC_1024: i64 = 1024
12
13func ce_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 }
14func ce_lc(c: i64) -> i64 { if c>=65 { if c<=90 { return c+32 } } return c }
15
16// append filename + every func-name (lowercased) from src[0..slen) into surf at *soffp. slen=0 -> filename only.
17func ce_append(src: *u8, slen: i64, name: *u8, surf: *u8, soffp: *i64) -> i64 {
18 var o: i64=soffp[0]
19 var z: i64=0; while name[z]!=(0 as u8) { surf[o]=ce_lc(name[z] as i64) as u8; o=o+1; z=z+1 } surf[o]=32 as u8; o=o+1
20 var i: i64=0
21 while i+5 <= slen {
22 var m: i64=0
23 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 } } } } }
24 if m==1 {
25 var j: i64=i+5
26 while j<slen { if ce_is_nc(src[j] as i64)==1 { surf[o]=ce_lc(src[j] as i64) as u8; o=o+1; j=j+1 } else { j=slen+1 } }
27 surf[o]=32 as u8; o=o+1
28 if j>slen { i=j-1 } else { i=j }
29 } else { i=i+1 }
30 }
31 surf[o]=10 as u8; o=o+1
32 soffp[0]=o
33 return 0
34}
35
36// scan one directory: per .nx file index its filename (no body read) -> surf + fnames + foff/fnoff.
37func ce_scan(dir: *u8, surf: *u8, soffp: *i64, foff: *i64, fnames: *u8, fnoff: *i64, nfp: *i64, maxf: i64, rbuf: *u8) -> i64 {
38 let fd: i64 = sys_openat_rd(dir)
39 if fd < 0 { return 0 }
40 let dbuf: *u8 = sys_mmap(K_MAGIC_131072)
41 var go: i64=1
42 while go==1 {
43 let nread: i64 = sys_getdents64(fd, dbuf, K_MAGIC_131072)
44 if nread <= 0 { go=0 } else {
45 var pos: i64=0
46 while pos < nread {
47 let rec: *u8 = (dbuf as i64 + pos) as *u8
48 let reclen: i64 = dirent_reclen(rec)
49 let nm: *u8 = dirent_name(rec)
50 var nl: i64=0; while nm[nl]!=(0 as u8) { nl=nl+1 }
51 var isnx: i64=0
52 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 } } } }
53 if isnx==1 { if nfp[0] < maxf {
54 let k: i64=nfp[0]
55 foff[k]=soffp[0]
56 let start: i64=fnoff[k]
57 var c: i64=0; while nm[c]!=(0 as u8) { fnames[start+c]=nm[c]; c=c+1 } fnames[start+c]=0 as u8
58 fnoff[k+1]=start+c+1
59 ce_append(rbuf, 0, nm, surf, soffp)
60 nfp[0]=k+1
61 } }
62 if reclen<=0 { pos=nread } else { pos=pos+reclen }
63 }
64 }
65 }
66 sys_close(fd)
67 foff[nfp[0]]=soffp[0]
68 return 0
69}
70
71// BUILD the standard index over the organ tree (runtime/ + runtime/_hdl_build/). Returns NF (organ count).
72func cap_index(surf: *u8, soffp: *i64, foff: *i64, fnames: *u8, fnoff: *i64, rbuf: *u8) -> i64 {
73 let nfp: *i64=sys_mmap(16) as *i64; nfp[0]=0
74 soffp[0]=0; fnoff[0]=0
75 ce_scan("runtime\x00" as *u8, surf, soffp, foff, fnames, fnoff, nfp, K_MAGIC_16000, rbuf)
76 ce_scan("runtime/_hdl_build\x00" as *u8, surf, soffp, foff, fnames, fnoff, nfp, K_MAGIC_16000, rbuf)
77 return nfp[0]
78}
79
80func ce_seg_has(surf: *u8, a: i64, b: i64, q: *u8, ql: i64) -> i64 {
81 var p: i64=a
82 while p+ql <= b { var m: i64=1; var j: i64=0; while j<ql { if (ce_lc(surf[p+j] as i64))!=(q[j] as i64) {m=0} j=j+1 } if m==1 {return 1} p=p+1 }
83 return 0
84}
85func ce_score(surf: *u8, foff: *i64, i: i64, qwords: *u8, qoff: *i64, nq: i64, fnames: *u8, fnoff: *i64) -> i64 {
86 let a: i64=foff[i]; let b: i64=foff[i+1]; let fa: i64=fnoff[i]; let fb: i64=fnoff[i+1]-1
87 var s: i64=0; var w: i64=0
88 while w<nq {
89 let qa: i64=qoff[w]; let ql: i64=qoff[w+1]-qa-1; let qp: *u8=(qwords as i64 + qa) as *u8
90 if ce_seg_has(fnames, fa, fb, qp, ql)==1 { s=s+3 } else { if ce_seg_has(surf, a, b, qp, ql)==1 { s=s+1 } }
91 w=w+1
92 }
93 return s
94}
95func ce_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 }
96func ce_is_stop(qw: *u8, a: i64, l: i64) -> i64 {
97 if l < 3 { return 1 }
98 if ce_wstreq(qw,a,l,"build\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"that\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"with\x00" as *u8)==1 {return 1}
99 if ce_wstreq(qw,a,l,"the\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"and\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"for\x00" as *u8)==1 {return 1}
100 if ce_wstreq(qw,a,l,"emit\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"emits\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"new\x00" as *u8)==1 {return 1}
101 if ce_wstreq(qw,a,l,"this\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"into\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"from\x00" as *u8)==1 {return 1}
102 if ce_wstreq(qw,a,l,"does\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"widget\x00" as *u8)==1 {return 1} if ce_wstreq(qw,a,l,"can\x00" as *u8)==1 {return 1}
103 return 0
104}
105func ce_tok(query: *u8, qwords: *u8, qoff: *i64) -> i64 {
106 var nq: i64=0; var o: i64=0; var i: i64=0
107 qoff[0]=0
108 while query[i]!=(0 as u8) {
109 if (query[i] as i64)==32 { if o>qoff[nq] { if ce_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] } } }
110 else { qwords[o]=ce_lc(query[i] as i64) as u8; o=o+1 }
111 i=i+1
112 }
113 if o>qoff[nq] { if ce_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] } }
114 return nq
115}
116// best-matching organ index for a query (or -1); best score via scorep.
117func cap_query(query: *u8, surf: *u8, foff: *i64, nf: i64, fnames: *u8, fnoff: *i64, scorep: *i64) -> i64 {
118 let qwords: *u8=sys_mmap(K_MAGIC_1024); let qoff: *i64=sys_mmap(8*64) as *i64
119 let nq: i64=ce_tok(query, qwords, qoff)
120 var best: i64=0-1; var bs: i64=0; var i: i64=0
121 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 }
122 scorep[0]=bs
123 return best
124}
125// THE ENFORCEMENT DECISION: 1 = REFUSE (capability EXISTS; out_best = its organ idx) | 0 = ALLOW (novel).
126func cap_guard(query: *u8, surf: *u8, foff: *i64, nf: i64, fnames: *u8, fnoff: *i64, out_best: *i64) -> i64 {
127 let sc: *i64=sys_mmap(16) as *i64
128 let b: i64=cap_query(query, surf, foff, nf, fnames, fnoff, sc)
129 out_best[0]=b
130 if sc[0] >= 3 { return 1 }
131 return 0
132}