code wiki / _hdl_build / nx_researcher_fuse_gate.nx

nx_researcher_fuse_gate.nx source

↩ module page · 55 lines · 4361 B

1// nx_researcher_fuse_gate.nx -- GATE: RRF FUSION of the researcher's INTERNAL + EXTERNAL ranking arms (the core of 2// "internal and external sources so we exceed deep-research"). The internal arm (BM25 over our 511-doc banked corpus, 3// proven live via nx_search_cli) ranks sources by OUR-corpus relevance; the external arm ranks by external authority. 4// nx_rrf (Cormack-Clarke-Buettcher 2009, parameter-free k=60, integer-exact Q16.16) fuses them by RANK POSITION: 5// score = sum 1/(k+rank). A source must rank well in BOTH arms to top the fused brief -- so internal-relevance AND 6// external-authority must AGREE, neutralizing single-source bias (the same property that neutralizes SEO). 7// T1 the fused top is the source ranked well in BOTH arms (not the one strong in only one). 8// T2 both-agreement beats single-source strength (score(both) > score(internal-only) and > score(external-only)). 9// T3 DETERMINISM: identical fusion -> byte-identical scores. 10// T4 EXTERNAL ARM LOAD-BEARING: internal-only fusion picks a DIFFERENT top -> adding external sources changes the answer. 11// expect_exit: 0 license_tier: ORIGINAL 12import "nx_rrf.nx" 13 14func rw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 15func rn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);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{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 16 17func main() -> i64 { 18 rw("=== nx_researcher_fuse: RRF fusion of INTERNAL + EXTERNAL ranking arms (the researcher core) ===\n" as *u8) 19 var pass: i64=0; var total: i64=0 20 let N: i64=5 21 // item ids: 0=internal-strong-only, 1=BOTH-good, 2=external-strong-only, 3,4=weak 22 let io: *i64=sys_mmap(8*8) as *i64; let eo: *i64=sys_mmap(8*8) as *i64 23 io[0]=0; io[1]=1; io[2]=3; io[3]=2; io[4]=4 // INTERNAL ranking (BM25 over our corpus) 24 eo[0]=2; eo[1]=1; eo[2]=4; eo[3]=0; eo[4]=3 // EXTERNAL ranking (authority) 25 let sc: *i64=sys_mmap(8*8) as *i64; var i: i64=0; while i<N { sc[i]=0; i=i+1 } 26 nx_rrf_add(io, N, sc) 27 nx_rrf_add(eo, N, sc) 28 let tp: i64=nx_rrf_argmax(sc, N) 29 30 // ---- T1: fused top = the source ranked well in BOTH arms ---- 31 total=total+1; if tp==1 { pass=pass+1; rw(" [PASS] " as *u8) } else { rw(" [FAIL] " as *u8) } 32 rw("T1 FUSION: internal+external RRF top=item" as *u8); rn(tp); rw(" (expected 1 -- ranked well in BOTH arms, beats strong-in-one)\n" as *u8) 33 34 // ---- T2: both-agreement beats single-source strength ---- 35 total=total+1; if sc[1]>sc[0] { if sc[1]>sc[2] { pass=pass+1; rw(" [PASS] " as *u8) } else { rw(" [FAIL] " as *u8) } } else { rw(" [FAIL] " as *u8) } 36 rw("T2 BOTH-AGREEMENT WINS: score(both-good) > score(internal-only) AND > score(external-only) -- neutralizes single-source bias\n" as *u8) 37 38 // ---- T3: determinism ---- 39 let sc2: *i64=sys_mmap(8*8) as *i64; i=0; while i<N { sc2[i]=0; i=i+1 } 40 nx_rrf_add(io, N, sc2); nx_rrf_add(eo, N, sc2) 41 var d: i64=0; i=0; while i<N { if sc[i]!=sc2[i] { d=d+1 } i=i+1 } 42 total=total+1; if d==0 { pass=pass+1; rw(" [PASS] " as *u8) } else { rw(" [FAIL] " as *u8) } 43 rw("T3 DETERMINISM: identical fusion -> byte-identical scores (diffs=" as *u8); rn(d); rw("; deep-research/LLM is stochastic)\n" as *u8) 44 45 // ---- T4: external arm load-bearing -- internal-only fusion picks a DIFFERENT top ---- 46 let sci: *i64=sys_mmap(8*8) as *i64; i=0; while i<N { sci[i]=0; i=i+1 } 47 nx_rrf_add(io, N, sci) 48 let tpi: i64=nx_rrf_argmax(sci, N) 49 total=total+1; if tpi==0 { if tp==1 { pass=pass+1; rw(" [PASS] " as *u8) } else { rw(" [FAIL] " as *u8) } } else { rw(" [FAIL] " as *u8) } 50 rw("T4 EXTERNAL ARM LOAD-BEARING: internal-only top=item" as *u8); rn(tpi); rw(" but FUSED top=item1 -- adding external sources CHANGES the answer (fusion is not vacuous)\n" as *u8) 51 52 rw("FUSE-GATE verdict=" as *u8) 53 if pass==total { rw("GREEN passes=" as *u8); rn(pass); rw("/" as *u8); rn(total); rw(" (internal+external fused via nx_rrf; both-agreement wins; external arm load-bearing; deterministic)\n" as *u8); sys_exit(0); return 0 } 54 rw("RED passes=" as *u8); rn(pass); rw("/" as *u8); rn(total); rw("\n" as *u8); sys_exit(1); return 1 55}