code wiki / _hdl_build / nx_research_miner.nx

nx_research_miner.nx source

↩ module page · 278 lines · 13179 B

1// nx_research_miner.nx -- THE NISHI MINER: flags RICH VEINS in the researched corpus = the high-value seams 2// worth mining DEEPER to push capability to S-class exceed. Scores every unique content (vein) in the content 3// registry by TWO grounded signals: (1) CROSS-TOPIC multiplicity -- content that many research topics need is a 4// rich shared seam (reuses the registry nx_research_grow builds); (2) INNOVATION/OPTIMIZATION signal density -- 5// occurrences of capability-pushing stems (optim/efficien/benchmark/novel/outperform/neural/lossless/...) in the 6// doc head, i.e. material about optimizing + innovating. richness = xtopic*XT_W + value. Ranks desc, writes 7// knowledge/index/rich_veins.log, and flags the top seams S-CLASS-EXCEED (where to mine next). 100% offline over 8// what we already fetched -- no network, deterministic, gateable. expect_exit: 0 license_tier: ORIGINAL 9import "nx_search_inverted_persist.nx" 10import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 11 12const RM_CAP: i64 = 65536 // max unique veins 13const RM_SCAN: i64 = 16384 // value-signal scan window (doc head) 14const RM_XT_WEIGHT: i64 = 100 // cross-topic weight (a shared seam is a strong vein signal) 15const RM_TOPK: i64 = 15 // how many top veins to flag S-CLASS-EXCEED 16 17func rm_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 19// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 20// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 21// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 22func rm_num(v: i64) -> i64 { nxi_out(v); return 0 } 23func rm_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 24func rm_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[off+i]=s[i];i=i+1} return off+i } 25func rm_catbuf(dst: *u8, off: i64, src: *u8, n: i64) -> i64 { var i: i64=0; while i<n { dst[off+i]=src[i]; i=i+1 } return off+n } 26func rm_catnum(dst: *u8, off: i64, v: i64) -> i64 { 27 if v == 0 { dst[off]=48 as u8; return off+1 } 28 let tmp: *u8=sys_mmap(28); var m: i64=v; var k: i64=0 29 while m>0 { tmp[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 30 var i: i64=0; while i<k { dst[off+i]=tmp[k-1-i]; i=i+1 } 31 return off+k 32} 33 34func rm_field_end(buf: *u8, i: i64, end: i64) -> i64 { 35 var p: i64=i; var stop: i64=0 36 while stop==0 { 37 if p >= end { stop=1 } 38 else { if buf[p]==(32 as u8) { stop=1 } else { if buf[p]==(10 as u8) { stop=1 } else { p=p+1 } } } 39 } 40 return p 41} 42func rm_parse_dec(buf: *u8, start: i64, fend: i64) -> i64 { 43 var v: i64=0; var p: i64=start 44 while p < fend { let c: i64=buf[p] as i64; if c>=48 { if c<=57 { v=v*10+(c-48) } } p=p+1 } 45 return v 46} 47func rm_streq_rng(buf: *u8, a_off: i64, a_len: i64, b_off: i64, b_len: i64) -> i64 { 48 if a_len != b_len { return 0 } 49 var i: i64=0 50 while i < a_len { if buf[a_off+i] != buf[b_off+i] { return 0 } i=i+1 } 51 return 1 52} 53 54// case-insensitive substring occurrences of `needle` in first `hlen` bytes of `hay` 55func rm_count(hay: *u8, hlen: i64, needle: *u8) -> i64 { 56 let nlen: i64 = rm_strlen(needle) 57 if nlen == 0 { return 0 } 58 var cnt: i64=0; var i: i64=0 59 while i + nlen <= hlen { 60 var j: i64=0; var ok: i64=1 61 while j < nlen { 62 var a: i64 = hay[i+j] as i64 63 var b: i64 = needle[j] as i64 64 if a >= 65 { if a <= 90 { a = a + 32 } } 65 if b >= 65 { if b <= 90 { b = b + 32 } } 66 if a != b { ok=0; j=nlen } else { j=j+1 } 67 } 68 if ok==1 { cnt=cnt+1 } 69 i=i+1 70 } 71 return cnt 72} 73 74// innovation/optimization signal density over the doc head (capability-pushing stems) 75func rm_value(buf: *u8, len: i64) -> i64 { 76 var n: i64 = len 77 if n > RM_SCAN { n = RM_SCAN } 78 var v: i64 = 0 79 v = v + rm_count(buf, n, "optim" as *u8) // optimize/optimization/optimal 80 v = v + rm_count(buf, n, "efficien" as *u8) // efficient/efficiency 81 v = v + rm_count(buf, n, "benchmark" as *u8) 82 v = v + rm_count(buf, n, "novel" as *u8) 83 v = v + rm_count(buf, n, "outperform" as *u8) 84 v = v + rm_count(buf, n, "breakthrough" as *u8) 85 v = v + rm_count(buf, n, "accelerat" as *u8) 86 v = v + rm_count(buf, n, "scalab" as *u8) 87 v = v + rm_count(buf, n, "lossless" as *u8) 88 v = v + rm_count(buf, n, "compress" as *u8) 89 v = v + rm_count(buf, n, "quantiz" as *u8) 90 v = v + rm_count(buf, n, "neural" as *u8) 91 v = v + rm_count(buf, n, "embedding" as *u8) 92 v = v + rm_count(buf, n, "gradient" as *u8) 93 v = v + rm_count(buf, n, "throughput" as *u8) 94 v = v + rm_count(buf, n, "latency" as *u8) 95 v = v + rm_count(buf, n, "parallel" as *u8) 96 v = v + rm_count(buf, n, "innovat" as *u8) // innovate/innovation 97 v = v + rm_count(buf, n, "state-of-the-art" as *u8) 98 v = v + rm_count(buf, n, "performance" as *u8) 99 return v 100} 101 102func main() -> i64 { 103 rm_puts("=== nx_research_miner: flag RICH VEINS (high-value seams) for S-class-exceed mining ===\n" as *u8) 104 let reg_path: *u8 = "knowledge/index/content_registry.txt" as *u8 105 let rbox: *i64 = sys_mmap(16) as *i64 106 let reg: *u8 = sys_read_file(reg_path, rbox) 107 if reg == 0 as *u8 { rm_puts(" NO registry yet (run nx_research_grow first)\n" as *u8); sys_exit(1); return 1 } 108 let rlen: i64 = rbox[0] 109 110 // parse registry lines: <fp> <topic> <path> 111 let lfp: *i64 = sys_mmap(8*RM_CAP) as *i64 112 let ltoff: *i64 = sys_mmap(8*RM_CAP) as *i64 113 let ltlen: *i64 = sys_mmap(8*RM_CAP) as *i64 114 let lpoff: *i64 = sys_mmap(8*RM_CAP) as *i64 115 let lplen: *i64 = sys_mmap(8*RM_CAP) as *i64 116 var nlines: i64 = 0 117 var i: i64 = 0 118 while i < rlen { 119 let f1: i64 = rm_field_end(reg, i, rlen) 120 let fp: i64 = rm_parse_dec(reg, i, f1) 121 var j: i64 = f1 122 if j < rlen { if reg[j]==(32 as u8) { j=j+1 } } 123 let f2: i64 = rm_field_end(reg, j, rlen) 124 let toff: i64 = j; let tl: i64 = f2 - j 125 var p: i64 = f2 126 if p < rlen { if reg[p]==(32 as u8) { p=p+1 } } 127 let f3: i64 = rm_field_end(reg, p, rlen) 128 let poff: i64 = p; let pl: i64 = f3 - p 129 var e: i64 = f3 130 var st: i64 = 0 131 while st==0 { if e>=rlen { st=1 } else { if reg[e]==(10 as u8) { st=1 } else { e=e+1 } } } 132 if e < rlen { e=e+1 } 133 if nlines < RM_CAP { lfp[nlines]=fp; ltoff[nlines]=toff; ltlen[nlines]=tl; lpoff[nlines]=poff; lplen[nlines]=pl; nlines=nlines+1 } 134 i = e 135 } 136 137 // unique veins: first occurrence of each fp = canonical; xtopic = distinct topics across its lines 138 let ufp: *i64 = sys_mmap(8*RM_CAP) as *i64 139 let ucpoff: *i64 = sys_mmap(8*RM_CAP) as *i64 // canonical path off/len 140 let ucplen: *i64 = sys_mmap(8*RM_CAP) as *i64 141 let uxt: *i64 = sys_mmap(8*RM_CAP) as *i64 // cross-topic count 142 let uval: *i64 = sys_mmap(8*RM_CAP) as *i64 // value-signal density 143 let usc: *i64 = sys_mmap(8*RM_CAP) as *i64 // richness score 144 var nveins: i64 = 0 145 var li: i64 = 0 146 while li < nlines { 147 // already counted? 148 var seen: i64 = 0 149 var u: i64 = 0 150 while u < nveins { if ufp[u]==lfp[li] { seen=1 } u=u+1 } 151 if seen == 0 { 152 // distinct topics for this fp 153 var xt: i64 = 0 154 var lj: i64 = 0 155 while lj < nlines { 156 if lfp[lj]==lfp[li] { 157 // distinct vs earlier same-fp lines 158 var dup: i64 = 0 159 var lk: i64 = 0 160 while lk < lj { 161 if lfp[lk]==lfp[li] { if rm_streq_rng(reg, ltoff[lk], ltlen[lk], ltoff[lj], ltlen[lj])==1 { dup=1 } } 162 lk=lk+1 163 } 164 if dup==0 { xt=xt+1 } 165 } 166 lj=lj+1 167 } 168 if nveins < RM_CAP { 169 ufp[nveins]=lfp[li]; ucpoff[nveins]=lpoff[li]; ucplen[nveins]=lplen[li]; uxt[nveins]=xt 170 nveins=nveins+1 171 } 172 } 173 li=li+1 174 } 175 176 // value-signal density per vein, INCREMENTAL: cache value by content fingerprint (vein_value_cache.txt) so 177 // only NEW docs are read+scanned each run. The per-run cost used to be reading ALL docs (the cycle's miner 178 // bottleneck at full scale); now a re-run with no new content does ZERO doc reads. Cache key = content fp 179 // (same content => same value), so it stays correct as the corpus grows. 180 let vc_fp: *i64 = sys_mmap(8*RM_CAP) as *i64 181 let vc_val: *i64 = sys_mmap(8*RM_CAP) as *i64 182 var nvc: i64 = 0 183 let vcbox: *i64 = sys_mmap(16) as *i64 184 let vcbuf: *u8 = sys_read_file("knowledge/index/vein_value_cache.txt" as *u8, vcbox) 185 if vcbuf != 0 as *u8 { 186 let vclen: i64 = vcbox[0] 187 var ci2: i64 = 0 188 while ci2 < vclen { 189 let f1: i64 = rm_field_end(vcbuf, ci2, vclen) 190 let fpv: i64 = rm_parse_dec(vcbuf, ci2, f1) 191 var j2: i64 = f1; if j2<vclen { if vcbuf[j2]==(32 as u8) { j2=j2+1 } } 192 let f2: i64 = rm_field_end(vcbuf, j2, vclen) 193 let vv: i64 = rm_parse_dec(vcbuf, j2, f2) 194 var e2: i64 = f2; var st2: i64=0 195 while st2==0 { if e2>=vclen { st2=1 } else { if vcbuf[e2]==(10 as u8) { st2=1 } else { e2=e2+1 } } } 196 if e2<vclen { e2=e2+1 } 197 if nvc < RM_CAP { vc_fp[nvc]=fpv; vc_val[nvc]=vv; nvc=nvc+1 } 198 ci2 = e2 199 } 200 } 201 let vc_fd: i64 = sys_openat_append("knowledge/index/vein_value_cache.txt" as *u8, 0x1a4) 202 let pbuf: *u8 = sys_mmap(RM_CAP) 203 let dbox: *i64 = sys_mmap(16) as *i64 204 var vi: i64 = 0 205 while vi < nveins { 206 var val: i64 = 0 - 1 207 var c: i64 = 0 208 while c < nvc { if vc_fp[c]==ufp[vi] { val = vc_val[c] } c=c+1 } // cached by content fp? 209 if val < 0 { 210 // miss: read canonical doc head, compute value, cache it (append + in-memory) 211 var po: i64 = rm_catbuf(pbuf, 0, ((reg as i64)+ucpoff[vi]) as *u8, ucplen[vi]); pbuf[po]=0 as u8 212 let buf: *u8 = sys_read_file(pbuf, dbox) 213 val = 0 214 if buf != 0 as *u8 { val = rm_value(buf, dbox[0]) } 215 if vc_fd >= 0 { 216 let cl: *u8 = sys_mmap(64); var cp2: i64 = 0 217 cp2 = rm_catnum(cl, cp2, ufp[vi]); cl[cp2]=32 as u8; cp2=cp2+1 218 cp2 = rm_catnum(cl, cp2, val); cl[cp2]=10 as u8; cp2=cp2+1 219 sys_write(vc_fd, cl, cp2) 220 } 221 if nvc < RM_CAP { vc_fp[nvc]=ufp[vi]; vc_val[nvc]=val; nvc=nvc+1 } 222 } 223 uval[vi] = val 224 usc[vi] = uxt[vi]*RM_XT_WEIGHT + val 225 vi=vi+1 226 } 227 if vc_fd >= 0 { sys_close(vc_fd) } 228 229 // selection-sort veins by score desc (carry fp/path/xt/val/score in lockstep) 230 var a: i64 = 0 231 while a < nveins { 232 var best: i64 = a 233 var b: i64 = a+1 234 while b < nveins { if usc[b] > usc[best] { best=b } b=b+1 } 235 if best != a { 236 let ts: i64=usc[a]; usc[a]=usc[best]; usc[best]=ts 237 let tx: i64=uxt[a]; uxt[a]=uxt[best]; uxt[best]=tx 238 let tv: i64=uval[a]; uval[a]=uval[best]; uval[best]=tv 239 let tf: i64=ufp[a]; ufp[a]=ufp[best]; ufp[best]=tf 240 let to: i64=ucpoff[a]; ucpoff[a]=ucpoff[best]; ucpoff[best]=to 241 let tl: i64=ucplen[a]; ucplen[a]=ucplen[best]; ucplen[best]=tl 242 } 243 a=a+1 244 } 245 246 // write rich_veins.log (all veins, desc), flag top-K S-CLASS-EXCEED 247 let vfd: i64 = sys_openat_wr("knowledge/index/rich_veins.log" as *u8, 0x1a4) 248 var flagged: i64 = 0 249 var r: i64 = 0 250 while r < nveins { 251 let line: *u8 = sys_mmap(RM_CAP+256); var lp: i64 = 0 252 if r < RM_TOPK { lp=rm_cat(line,lp,"S-CLASS-EXCEED-VEIN " as *u8); flagged=flagged+1 } else { lp=rm_cat(line,lp,"vein " as *u8) } 253 lp=rm_cat(line,lp,"score=" as *u8); lp=rm_catnum(line,lp,usc[r]) 254 lp=rm_cat(line,lp," xtopic=" as *u8); lp=rm_catnum(line,lp,uxt[r]) 255 lp=rm_cat(line,lp," value=" as *u8); lp=rm_catnum(line,lp,uval[r]) 256 lp=rm_cat(line,lp," path=" as *u8); lp=rm_catbuf(line,lp,((reg as i64)+ucpoff[r]) as *u8,ucplen[r]) 257 line[lp]=10 as u8; lp=lp+1 258 if vfd >= 0 { sys_write(vfd, line, lp) } 259 r=r+1 260 } 261 if vfd >= 0 { sys_close(vfd) } 262 263 rm_puts(" veins="); rm_num(nveins); rm_puts(" flagged_S-class="); rm_num(flagged) 264 rm_puts(" -> knowledge/index/rich_veins.log\n" as *u8) 265 rm_puts(" TOP VEINS (mine these deeper):\n" as *u8) 266 var t: i64 = 0 267 while t < 8 { 268 if t < nveins { 269 rm_puts(" #"); rm_num(t+1); rm_puts(" score="); rm_num(usc[t]) 270 rm_puts(" xtopic="); rm_num(uxt[t]); rm_puts(" value="); rm_num(uval[t]) 271 rm_puts(" "); sys_write(1, ((reg as i64)+ucpoff[t]) as *u8, ucplen[t]); rm_puts("\n" as *u8) 272 } 273 t=t+1 274 } 275 if nveins <= 0 { rm_puts(" NO veins\n" as *u8); sys_exit(1); return 1 } 276 rm_puts(" MINER-OK\n" as *u8) 277 sys_exit(0); return 0 278}