code wiki / _hdl_build / nx_semantic_gap_gate.nx

nx_semantic_gap_gate.nx source

↩ module page · 115 lines · 8933 B

1// nx_semantic_gap_gate.nx -- HONEST measurement of the SEMANTIC GAP (answers the operator's doubt "are we 2// really exceed doing semantic work?" with a number, not an opinion). Our search is LEXICAL (BM25/BM25F = 3// "literal matches of the query words", cited sem_search.raw). This gate runs NATURAL-LANGUAGE queries -- the 4// way a real person types -- through the LIVE nx_onsite_search client over the real andelinwest corpus, and 5// counts how many land the semantically-correct practice page at rank #1. A LOW score is the POINT: it proves, 6// measured, that lexical search does NOT understand meaning -> we are NOT exceed on semantics. The fix is the 7// PARTNERSHIP (BM25 retrieve + LLM rerank = RAG, cited sem_rag.raw), which is unbuilt here. license_tier: ORIGINAL 8import "nx_syscalls.nx" 9 10func s_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func s_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" 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(1,bb,k); return 0 } 12func s_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 } 13func s_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; 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 } 14func s_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 15 16func s_write_file(path: *u8, data: *u8) -> i64 { let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0-1 } sys_write(fd, data, s_strlen(data)); sys_close(fd); return 0 } 17func s_read_small(path: *u8, buf: *u8, cap: i64) -> i64 { 18 let fd: i64=sys_openat_rd(path); if fd<0 { return 0 } 19 var total: i64=0; var nrd: i64=sys_read(fd, buf, cap) 20 while nrd>0 { total=total+nrd; if total>=cap { nrd=0 } else { nrd=sys_read(fd, ((buf as i64)+total) as *u8, cap-total) } } 21 sys_close(fd); return total 22} 23// fork the indexer (5 args) 24func s_index(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> i64 { 25 let pid: i64=sys_fork() 26 if pid==0 { 27 let o: i64=sys_openat_wr("/tmp/sg_idx.txt" as *u8, 0x1a4); if o>=0 { sys_dup3(o,1,0) } 28 let argv: *i64=sys_mmap(8*8) as *i64 29 argv[0]="/tmp/nx_onsite_index.sov.elf" as *u8 as i64; argv[1]=a1 as i64; argv[2]=a2 as i64; argv[3]=a3 as i64; argv[4]=a4 as i64; argv[5]=0 30 let envp: *i64=sys_mmap(16) as *i64; envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0 31 sys_execve("/tmp/nx_onsite_index.sov.elf" as *u8, argv, envp); sys_exit(127) 32 } 33 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0); return (st[0]>>8)&0xff 34} 35// fork the client with a multi-word query (terms = *i64 of *u8, nt up to 6) 36func s_client(sites: *u8, site: *u8, terms: *i64, nt: i64, out_path: *u8) -> i64 { 37 let pid: i64=sys_fork() 38 if pid==0 { 39 let o: i64=sys_openat_wr(out_path, 0x1a4); if o>=0 { sys_dup3(o,1,0) } 40 let argv: *i64=sys_mmap(8*16) as *i64 41 argv[0]="/tmp/nx_onsite_search.sov.elf" as *u8 as i64; argv[1]=sites as i64; argv[2]=site as i64 42 var i: i64=0; while i<nt { argv[3+i]=terms[i]; i=i+1 } argv[3+nt]=0 43 let envp: *i64=sys_mmap(16) as *i64; envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0 44 sys_execve("/tmp/nx_onsite_search.sov.elf" as *u8, argv, envp); sys_exit(127) 45 } 46 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0); return (st[0]>>8)&0xff 47} 48func s_find(hay: *u8, n: i64, needle: *u8) -> i64 { 49 let nl: i64=s_strlen(needle); if nl==0 { return 0-1 } 50 var i: i64=0 51 while i+nl<=n { var j: i64=0; var hit: i64=1; while j<nl { if hay[i+j]!=needle[j] { hit=0; j=nl } else { j=j+1 } } if hit==1 { return i } i=i+1 } 52 return 0-1 53} 54// is `expected` the rank #1 result? (find it, scan to line start, parse #N==1) 55func s_top_is(buf: *u8, n: i64, expected: *u8) -> i64 { 56 let at: i64=s_find(buf, n, expected); if at<0 { return 0 } 57 var ls: i64=at 58 while ls>0 { if buf[ls-1]==(10 as u8) { ls=0-ls } else { ls=ls-1 } } 59 if ls<0 { ls=0-ls } 60 let h: i64=s_find(((buf as i64)+ls) as *u8, at-ls+1, "#" as *u8); if h<0 { return 0 } 61 var p: i64=ls+h+1; var rank: i64=0 62 while p<n { let c: i64=buf[p] as i64; if c>=48 { if c<=57 { rank=rank*10+(c-48); p=p+1 } else { p=n } } else { p=n } } 63 if rank==1 { return 1 } 64 return 0 65} 66 67// one NL query row: print BM25's matches count + whether it got the right practice #1. 68func s_row(sites: *u8, label: *u8, terms: *i64, nt: i64, expect: *u8, obuf: *u8) -> i64 { 69 s_client(sites, "aw" as *u8, terms, nt, "/tmp/sg_out.txt" as *u8) 70 let n: i64=s_read_small("/tmp/sg_out.txt" as *u8, obuf, 65536) 71 var matches: i64=0 72 let mp: i64=s_find(obuf, n, "matches=" as *u8) 73 if mp>=0 { var p: i64=mp+8; while p<n { let c: i64=obuf[p] as i64; if c>=48 { if c<=57 { matches=matches*10+(c-48); p=p+1 } else { p=n } } else { p=n } } } 74 let ok: i64=s_top_is(obuf, n, expect) 75 s_puts(" \"" as *u8); s_puts(label); s_puts("\"\n BM25 matches=" as *u8); s_num(matches) 76 s_puts(" correct-practice-#1? " as *u8) 77 if ok==1 { s_puts("YES\n" as *u8) } else { s_puts("NO (lexical miss)\n" as *u8) } 78 return ok 79} 80 81func main() -> i64 { 82 s_puts("=== NATURAL-LANGUAGE PROBE (BM25 + sovereign concept-expansion; honest reach AND boundary) ===\n" as *u8) 83 let rc: i64=s_index("knowledge/index/andelinwest_src.tsv" as *u8, "/practice/" as *u8, 84 "knowledge/index/andelinwest.idx" as *u8, "knowledge/index/andelinwest.manifest" as *u8) 85 if rc!=0 { s_puts("PROBE-FAIL: index rc=" as *u8); s_num(rc); s_puts(" (rebuild /tmp/nx_onsite_index.sov.elf?)\n" as *u8); sys_exit(1); return 1 } 86 s_write_file("/tmp/aw_sites.tsv" as *u8, "aw\tknowledge/index/andelinwest.idx\tknowledge/index/andelinwest.manifest\thttps://andelinwest.com\n" as *u8) 87 let sites: *u8="/tmp/aw_sites.tsv" as *u8 88 let obuf: *u8=sys_mmap(65536) 89 90 // 4 natural-language queries a real person would type (NO exact practice keywords) 91 let q1: *i64=sys_mmap(8*6) as *i64; q1[0]="who" as *u8 as i64; q1[1]="keeps" as *u8 as i64; q1[2]="children" as *u8 as i64; q1[3]="split" as *u8 as i64 92 let q2: *i64=sys_mmap(8*6) as *i64; q2[0]="leave" as *u8 as i64; q2[1]="everything" as *u8 as i64; q2[2]="kids" as *u8 as i64; q2[3]="pass" as *u8 as i64; q2[4]="away" as *u8 as i64 93 let q3: *i64=sys_mmap(8*6) as *i64; q3[0]="rear" as *u8 as i64; q3[1]="ended" as *u8 as i64; q3[2]="drunk" as *u8 as i64; q3[3]="driver" as *u8 as i64 94 let q4: *i64=sys_mmap(8*6) as *i64; q4[0]="incorporate" as *u8 as i64; q4[1]="startup" as *u8 as i64 95 96 // curated-concept queries (their concepts ARE in the per-site thesaurus) 97 var curated: i64=0 98 curated=curated + s_row(sites, "who keeps the children if we split" as *u8, q1, 4, "/practice/family-law" as *u8, obuf) 99 curated=curated + s_row(sites, "leave everything to my kids when i pass away" as *u8, q2, 5, "/practice/estate-planning" as *u8, obuf) 100 curated=curated + s_row(sites, "rear ended by a drunk driver" as *u8, q3, 4, "/practice/personal-injury" as *u8, obuf) 101 curated=curated + s_row(sites, "incorporate my startup" as *u8, q4, 2, "/practice/business-law" as *u8, obuf) 102 // BOUNDARY query: a paraphrase whose concepts are NOT in the thesaurus -> honestly still misses (proves 103 // concept-expansion is bounded, NOT general semantics). 104 let q5: *i64=sys_mmap(8*6) as *i64; q5[0]="house" as *u8 as i64; q5[1]="gone" as *u8 as i64; q5[2]="happens" as *u8 as i64 105 let boundary: i64=s_row(sites, "what happens to my house after im gone" as *u8, q5, 3, "/practice/estate-planning" as *u8, obuf) 106 107 s_puts("----\nCONCEPT-EXPANSION reach : curated-concept queries = " as *u8); s_num(curated); s_puts("/4 correct (was 0/4 pure-lexical)\n" as *u8) 108 s_puts("CONCEPT-EXPANSION boundary: novel paraphrase (no thesaurus entry) correct? " as *u8); s_num(boundary); s_puts(" (0 = still misses)\n" as *u8) 109 s_puts("HONEST: sovereign concept-expansion is REAL measurable growth (0/4 -> " as *u8); s_num(curated); s_puts("/4 on curated concepts), but it is\n" as *u8) 110 s_puts(" a curated thesaurus, NOT neural understanding -- a paraphrase outside it still misses. GENERAL\n" as *u8) 111 s_puts(" semantics needs the PARTNERSHIP (BM25 retrieve + LLM rerank = RAG). We are NOT yet 'exceed' there.\n" as *u8) 112 let lg: i64=sys_openat_append("knowledge/status/semantic_gap.log" as *u8, 0x1a4) 113 if lg>=0 { s_w(lg, "NLQ-PROBE concept_expansion_curated=" as *u8); s_wn(lg, curated); s_w(lg, "/4 boundary_novel_paraphrase=" as *u8); s_wn(lg, boundary); s_w(lg, " (pure_lexical_was 0/4)\n" as *u8); sys_close(lg) } 114 sys_exit(0); return 0 115}