code wiki / _hdl_build / nx_library_search_gate.nx

nx_library_search_gate.nx source

↩ module page · 88 lines · 5904 B

1// nx_library_search_gate.nx -- REPRODUCIBLE proof of S-class ranked retrieval over the REAL on-disk Nishi 2// library (knowledge/library/*.txt), with NO live server (the old nx_library_search_test needs an orphaned 3// :8099 fixture -> 0/8 when it is down = not reproducible by our system). This gate reads five real ingested 4// docs straight off disk, builds the corpus, and asserts both rankers (ls_best TF-IDF + ls_best_bm25, the 5// production BM25) pick the right doc for distinctive query terms, and resolve an AMBIGUOUS term (in >=2 docs) 6// to one deterministic best -- the honest exceed over naive substring scan, which can only say present/absent 7// and cannot rank or pick. Term doc-frequencies were verified against the live corpus before wiring. Fully 8// sovereign + reproducible: reads local files, our own ranker, no socket. GREEN iff 8/8. license_tier: ORIGINAL 9import "nx_library_search.nx" // ls_best (TF-IDF), ls_best_bm25 (production BM25), ls_df 10import "nx_syscalls.nx" 11 12func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-");m=0-m} let t:*u8=sys_mmap(24); 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; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1}; sys_write(1,o,k); return 0 } 14func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 } 15 16// read a whole file into buf (cap); return length (0 if missing -> graceful skip). 17func gh_read(path: *u8, buf: *u8, cap: i64) -> i64 { 18 let fd: i64 = sys_openat_rd(path) 19 if fd < 0 { return 0 } 20 var total: i64 = 0; var go: i64 = 1 21 while go == 1 { 22 let n: i64 = sys_read(fd, buf + total, cap - total) 23 if n <= 0 { go = 0 } else { total = total + n; if total >= cap { go = 0 } } 24 } 25 sys_close(fd) 26 return total 27} 28 29func main() -> i64 { 30 let pass: *i64 = sys_mmap(8) as *i64; pass[0] = 0 31 g_w("=== NX-LIBRARY-SEARCH GATE (ranked retrieval over the REAL on-disk Nishi library) ===\n") 32 33 let ND: i64 = 5 34 let ptrs: *i64 = sys_mmap(8 * 8) as *i64 35 let lens: *i64 = sys_mmap(8 * 8) as *i64 36 // five real ingested docs (df of the query terms verified against the live corpus first) 37 let p0: *u8 = "knowledge/library/alphatensor_readme.txt" 38 let p1: *u8 = "knowledge/library/automl_wiki.txt" 39 let p2: *u8 = "knowledge/library/evolutionary_computation_wiki.txt" 40 let p3: *u8 = "knowledge/library/chipfab-research-grounded-2026-06-09.txt" 41 let p4: *u8 = "knowledge/library/electronics-fab-deep-research-2026-06-09.txt" 42 let b0: *u8 = sys_mmap(65536); lens[0] = gh_read(p0, b0, 65535); ptrs[0] = b0 as i64 43 let b1: *u8 = sys_mmap(65536); lens[1] = gh_read(p1, b1, 65535); ptrs[1] = b1 as i64 44 let b2: *u8 = sys_mmap(65536); lens[2] = gh_read(p2, b2, 65535); ptrs[2] = b2 as i64 45 let b3: *u8 = sys_mmap(65536); lens[3] = gh_read(p3, b3, 65535); ptrs[3] = b3 as i64 46 let b4: *u8 = sys_mmap(65536); lens[4] = gh_read(p4, b4, 65535); ptrs[4] = b4 as i64 47 48 g_w(" corpus: ND="); g_n(ND); g_w(" docs lens=["); 49 var i: i64 = 0; while i < ND { g_n(lens[i]); if i < ND-1 { g_w(",") } i = i + 1 } g_w("]\n") 50 51 // doc-frequencies over THIS corpus (the distinctiveness IDF/BM25 exploit) 52 let df_tensor: i64 = ls_df(ptrs, lens, ND, "tensor" as *u8) 53 let df_litho: i64 = ls_df(ptrs, lens, ND, "lithography" as *u8) 54 g_w(" df(tensor)="); g_n(df_tensor); g_w(" df(lithography)="); g_n(df_litho); g_w("\n") 55 56 // queries (single distinctive term each; qterms[t] = needle *u8 as i64) 57 let qt: *i64 = sys_mmap(16) as *i64; qt[0] = "tensor" as *u8 as i64 58 let qa: *i64 = sys_mmap(16) as *i64; qa[0] = "automl" as *u8 as i64 59 let qh: *i64 = sys_mmap(16) as *i64; qh[0] = "hyperparameter" as *u8 as i64 60 let ql: *i64 = sys_mmap(16) as *i64; ql[0] = "lithography" as *u8 as i64 61 62 let b_tensor: i64 = ls_best(ptrs, lens, ND, qt, 1) 63 let b_automl: i64 = ls_best(ptrs, lens, ND, qa, 1) 64 let b_hyper: i64 = ls_best(ptrs, lens, ND, qh, 1) 65 let b_litho: i64 = ls_best(ptrs, lens, ND, ql, 1) 66 let m_tensor: i64 = ls_best_bm25(ptrs, lens, ND, qt, 1) 67 let m_automl: i64 = ls_best_bm25(ptrs, lens, ND, qa, 1) 68 69 g_w(" TF-IDF: tensor->doc"); g_n(b_tensor); g_w(" automl->doc"); g_n(b_automl); g_w(" hyperparameter->doc"); g_n(b_hyper); g_w(" lithography->doc"); g_n(b_litho); g_w("\n") 70 g_w(" BM25: tensor->doc"); g_n(m_tensor); g_w(" automl->doc"); g_n(m_automl); g_w("\n") 71 72 var all_loaded: i64 = 1 73 i = 0; while i < ND { if lens[i] <= 0 { all_loaded = 0 } i = i + 1 } 74 g_row("corpus: all 5 real library docs loaded from disk" as *u8, all_loaded, pass) 75 g_row("df: 'tensor' distinctive (==1)" as *u8, (df_tensor == 1) as i64, pass) 76 g_row("df: 'lithography' ambiguous across 2 docs (substring can't pick)" as *u8, (df_litho == 2) as i64, pass) 77 g_row("TF-IDF: 'tensor' ranks the AlphaTensor doc (doc0)" as *u8, (b_tensor == 0) as i64, pass) 78 g_row("TF-IDF: 'automl' AND 'hyperparameter' both rank the AutoML doc (doc1)" as *u8, ((b_automl == 1) & (b_hyper == 1)) as i64, pass) 79 g_row("BM25 (production ranker) agrees: tensor->0, automl->1" as *u8, ((m_tensor == 0) & (m_automl == 1)) as i64, pass) 80 var litho_resolved: i64 = 0 81 if b_litho == 3 { litho_resolved = 1 } if b_litho == 4 { litho_resolved = 1 } 82 g_row("EXCEED: ambiguous 'lithography' resolves to ONE deterministic best (doc3|doc4)" as *u8, litho_resolved, pass) 83 g_row("LIAR-KILL: a distinctive term does NOT collide (tensor doc != automl doc)" as *u8, (b_tensor != b_automl) as i64, pass) 84 85 g_w("LIBRARY-SEARCH-GATE rows=8 pass="); g_n(pass[0]) 86 if pass[0] == 8 { g_w(" verdict=GREEN\n"); sys_exit(0); return 0 } 87 g_w(" verdict=RED\n"); sys_exit(1); return 1 88}