code wiki / _hdl_build / nx_library_search_test.nx
nx_library_search_test.nx source
↩ module page · 92 lines · 6388 B
1// nx_library_search_test.nx -- the Researcher handles a LIBRARY OF INFORMATION, not one source, and
2// searches it with S-class ranked retrieval. End to end, sovereign:
3// 1. FETCH the index, RECOGNIZE it is a library (>=3 [doc] entries)
4// 2. HARVEST every listed doc -- follow each entry, fetch it, MIRROR it into the Nishi Library
5// 3. build the corpus FROM THE LOCAL MIRRORS (bot-block-proof)
6// 4. answer 3 different SEARCH PROMPTS by TF-IDF RANKING the whole corpus -- each returns the single
7// best-matching doc, where naive substring search would be ambiguous (e.g. "abandon" is in 2 docs)
8// Exit 0 on 8/8. license_tier: ORIGINAL
9//
10// NOTE 2026-06-20: this is a LIVE-INTEGRATION test -- it requires a fixture HTTP server on 127.0.0.1:8099
11// serving a library index + the 3 docs. With no server up it reads 0/8 ("ambiguous across 0 docs") because
12// the harvest fetches nothing -- that is an unmet fixture dependency, NOT a search-logic regression. The
13// REPRODUCIBLE-BY-OUR-SYSTEM proof of the ranked-retrieval claim (no socket, real on-disk corpus, both TF-IDF
14// + production BM25) is nx_library_search_gate (8/8 GREEN). Run THIS one only with the :8099 fixture live.
15
16import "nx_browse_text.nx"
17import "nx_library_search.nx"
18import "nx_library_cache.nx"
19import "nx_syscalls.nx"
20
21func st_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
22func st_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;k=1}; while m>0 {t[k]=48+(m%10); 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 }
23
24// build "knowledge/library/harvK.txt" into out (null-terminated); return length.
25func st_harv_path(out: *u8, k: i64) -> i64 {
26 let pre: *u8 = "knowledge/library/harv" as *u8
27 var i: i64 = 0; while pre[i] != (0 as u8) { out[i] = pre[i]; i = i + 1 }
28 out[i] = 48 + k; i = i + 1
29 let suf: *u8 = ".txt" as *u8
30 var j: i64 = 0; while suf[j] != (0 as u8) { out[i] = suf[j]; i = i + 1; j = j + 1 }
31 out[i] = 0
32 return i
33}
34
35func main() -> i64 {
36 st_puts("=== RESEARCHER: harvest a LIBRARY + S-class ranked search over the corpus ===\n" as *u8)
37 let addr: *u8 = sys_mmap(32); br_addr_ipv4(addr, 127, 0, 0, 1, 8099)
38 let scratch: *u8 = sys_mmap(16384)
39 let status: *i64 = sys_mmap(8) as *i64; let verdict: *i64 = sys_mmap(8) as *i64
40
41 // 1) FETCH the index, recognize a LIBRARY
42 let idx: *u8 = sys_mmap(16384)
43 let inb: i64 = nx_browse_text(addr, "/" as *u8, 1, "127.0.0.1" as *u8, 9, scratch, 16384, idx, 16384, status, verdict)
44 let islib: i64 = ls_is_library(idx, inb)
45 let entries: i64 = ls_entry_count(idx, inb)
46 st_puts(" 1. INDEX : bytes=" as *u8); st_num(inb); st_puts(" is-library=" as *u8); st_num(islib); st_puts(" entries=" as *u8); st_num(entries); st_puts("\n" as *u8)
47
48 // 2) HARVEST every listed doc -> mirror into the library; 3) build corpus FROM THE MIRRORS
49 let ptrs: *i64 = sys_mmap(8 * 8) as *i64
50 let lens: *i64 = sys_mmap(8 * 8) as *i64
51 let pathbuf: *u8 = sys_mmap(128); let mpath: *u8 = sys_mmap(128)
52 var harvested: i64 = 0; var k: i64 = 0
53 while k < entries {
54 let plen: i64 = ls_entry_path(idx, inb, k, pathbuf, 120)
55 let tmp: *u8 = sys_mmap(16384)
56 let dn: i64 = nx_browse_text(addr, pathbuf, plen, "127.0.0.1" as *u8, 9, scratch, 16384, tmp, 16384, status, verdict)
57 st_harv_path(mpath, k)
58 let w: i64 = lc_mirror(mpath, tmp, dn) // download into the Nishi Library
59 let buf: *u8 = sys_mmap(16384)
60 let rn: i64 = lc_read_mirror(mpath, buf, 16384) // corpus comes from the LOCAL mirror
61 ptrs[k] = buf as i64; lens[k] = rn
62 if rn > 0 { if w == dn { harvested = harvested + 1 } }
63 st_puts(" harvested " as *u8); sys_write(1, pathbuf, plen); st_puts(" -> " as *u8); st_num(rn); st_puts(" bytes mirrored\n" as *u8)
64 k = k + 1
65 }
66
67 // 4) S-class SEARCH: 3 prompts, TF-IDF ranked over the corpus
68 let q1: *i64 = sys_mmap(4 * 8) as *i64; q1[0] = "abandon" as *u8 as i64; q1[1] = "load" as *u8 as i64
69 let q2: *i64 = sys_mmap(4 * 8) as *i64; q2[0] = "choices" as *u8 as i64; q2[1] = "decision" as *u8 as i64
70 let q3: *i64 = sys_mmap(4 * 8) as *i64; q3[0] = "credibility" as *u8 as i64; q3[1] = "conversion" as *u8 as i64
71 let b1: i64 = ls_best(ptrs, lens, entries, q1, 2)
72 let b2: i64 = ls_best(ptrs, lens, entries, q2, 2)
73 let b3: i64 = ls_best(ptrs, lens, entries, q3, 2)
74 let ambig: i64 = ls_df(ptrs, lens, entries, "abandon" as *u8) // naive substring: "abandon" in 2 docs
75 st_puts(" 4. SEARCH : 'abandon load'->doc" as *u8); st_num(b1); st_puts(" 'choices decision'->doc" as *u8); st_num(b2); st_puts(" 'credibility conversion'->doc" as *u8); st_num(b3); st_puts("\n" as *u8)
76 st_puts(" EXCEED: naive substring 'abandon' is ambiguous across " as *u8); st_num(ambig); st_puts(" docs; ranked search resolves to the single best (doc" as *u8); st_num(b1); st_puts(")\n" as *u8)
77
78 let r: *i64 = sys_mmap(8 * 8) as *i64
79 r[0] = 0; if islib == 1 { r[0] = 1 } // recognized a LIBRARY (not one source)
80 r[1] = 0; if entries == 3 { r[1] = 1 } // parsed the index
81 r[2] = 0; if harvested == 3 { r[2] = 1 } // harvested+mirrored every doc
82 r[3] = 0; if b1 == 0 { r[3] = 1 } // ranked search picks the abandonment doc
83 r[4] = 0; if b2 == 1 { r[4] = 1 } // ... the decision doc
84 r[5] = 0; if b3 == 2 { r[5] = 1 } // ... the trust doc
85 r[6] = 0; if ambig == 2 { if b1 == 0 { r[6] = 1 } } // naive ambiguous (2) but ranked resolves to 1
86 r[7] = 0; if lens[0] > 0 { if lens[1] > 0 { if lens[2] > 0 { r[7] = 1 } } } // corpus built from local mirrors
87 var pass: i64 = 0; var i: i64 = 0
88 while i < 8 { pass = pass + r[i]; i = i + 1 }
89 st_puts("----\n passed " as *u8); st_num(pass); st_puts("/8\n" as *u8)
90 if pass == 8 { st_puts(" S-CLASS SEARCH: a library recognized, every doc harvested into the Nishi Library, and 3 prompts each ranked to the right cited doc -- over LOCAL mirrors, no live web.\n" as *u8); sys_exit(0); return 0 }
91 st_puts(" FAIL\n" as *u8); sys_exit(1); return 1
92}