code wiki / _hdl_build / nx_andelinwest_search_gate.nx

nx_andelinwest_search_gate.nx source

↩ module page · 117 lines · 7219 B

1// nx_andelinwest_search_gate.nx -- the REAL, MEASURED search gate for andelinwest.com. REPLACES the dishonest 2// nx_andelinwest_search demo (which hardcoded 6 strings and SELF-SCORED "REFEREE=EXCEED" -- exactly the 3// self-grading the operator's doctrine forbids). This gate (1) rebuilds the andelinwest index from the REAL 4// site content (knowledge/index/andelinwest_src.tsv, derived from web_assets/_sites/andelinwest.site) via the 5// reusable ingestor, then (2) forks the REAL /tmp/nx_onsite_search.sov.elf client and checks each EXAMPLE QUERY 6// from the site's own search box ("divorce, will, injury, contract...") lands the RIGHT practice page at rank #1. 7// The verdict is MEASURED from the live client's output, never asserted. GREEN iff 4/4. Durable log appends to 8// knowledge/status/andelinwest_search_gate.log. license_tier: ORIGINAL 9import "nx_syscalls.nx" 10 11func a_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 12func a_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 } 13func a_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 } 14func a_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 } 15func a_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 16 17func a_write_file(path: *u8, data: *u8) -> i64 { 18 let fd: i64=sys_openat_wr(path, 0x1a4) 19 if fd<0 { return 0-1 } 20 sys_write(fd, data, a_strlen(data)); sys_close(fd); return 0 21} 22func a_read_small(path: *u8, buf: *u8, cap: i64) -> i64 { 23 let fd: i64=sys_openat_rd(path) 24 if fd<0 { return 0 } 25 var total: i64=0; var nrd: i64=sys_read(fd, buf, cap) 26 while nrd>0 { total=total+nrd; if total>=cap { nrd=0 } else { nrd=sys_read(fd, ((buf as i64)+total) as *u8, cap-total) } } 27 sys_close(fd); return total 28} 29 30// fork a tool (path + up to 5 args), wait, return exit code 31func a_exec5(path: *u8, a1: *u8, a2: *u8, a3: *u8, a4: *u8, out_path: *u8) -> i64 { 32 let pid: i64=sys_fork() 33 if pid==0 { 34 if a_strlen(out_path)>0 { let o: i64=sys_openat_wr(out_path, 0x1a4); if o>=0 { sys_dup3(o,1,0) } } 35 let argv: *i64=sys_mmap(8*8) as *i64 36 argv[0]=path 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 37 let envp: *i64=sys_mmap(16) as *i64; envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0 38 sys_execve(path, argv, envp); sys_exit(127) 39 } 40 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0) 41 if (st[0]&0x7f)!=0 { return 128+(st[0]&0x7f) } 42 return (st[0]>>8)&0xff 43} 44// fork the client: <sites> <site> <term> 45func a_client(sites: *u8, site: *u8, term: *u8, out_path: *u8) -> i64 { 46 let pid: i64=sys_fork() 47 if pid==0 { 48 let o: i64=sys_openat_wr(out_path, 0x1a4); if o>=0 { sys_dup3(o,1,0) } 49 let argv: *i64=sys_mmap(8*6) as *i64 50 argv[0]="/tmp/nx_onsite_search.sov.elf" as *u8 as i64; argv[1]=sites as i64; argv[2]=site as i64; argv[3]=term as i64; argv[4]=0 51 let envp: *i64=sys_mmap(16) as *i64; envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0 52 sys_execve("/tmp/nx_onsite_search.sov.elf" as *u8, argv, envp); sys_exit(127) 53 } 54 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0) 55 return (st[0]>>8)&0xff 56} 57 58func a_find(hay: *u8, n: i64, needle: *u8) -> i64 { 59 let nl: i64=a_strlen(needle); if nl==0 { return 0-1 } 60 var i: i64=0 61 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 } 62 return 0-1 63} 64func a_rank_of(buf: *u8, n: i64, expected: *u8) -> i64 { 65 let at: i64=a_find(buf, n, expected); if at<0 { return 0 } 66 var ls: i64=at 67 while ls>0 { if buf[ls-1]==(10 as u8) { ls=0-ls } else { ls=ls-1 } } 68 if ls<0 { ls=0-ls } 69 let hash_at: i64=a_find(((buf as i64)+ls) as *u8, at-ls+1, "#" as *u8); if hash_at<0 { return 0 } 70 var p: i64=ls+hash_at+1; var rank: i64=0 71 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 } } 72 return rank 73} 74 75// one oracle row: query the live client, assert the expected practice URL ranks #1 76func a_row(label: *u8, term: *u8, expect: *u8, sites: *u8, outp: *u8, obuf: *u8) -> i64 { 77 a_client(sites, "aw" as *u8, term, outp) 78 let n: i64=a_read_small(outp, obuf, 65536) 79 let rk: i64=a_rank_of(obuf, n, expect) 80 a_puts(" q='" as *u8); a_puts(term); a_puts("' -> " as *u8); a_puts(expect); a_puts(" rank " as *u8); a_num(rk) 81 if rk==1 { a_puts(" PASS\n" as *u8); return 1 } 82 a_puts(" FAIL\n" as *u8); return 0 83} 84 85func main() -> i64 { 86 a_puts("=== ANDELINWEST SEARCH GATE (REAL content + reusable client; MEASURED, no self-score) ===\n" as *u8) 87 // (1) rebuild the index from the REAL site content via the reusable ingestor 88 let rc_idx: i64=a_exec5("/tmp/nx_onsite_index.sov.elf" as *u8, 89 "knowledge/index/andelinwest_src.tsv" as *u8, "/practice/" as *u8, 90 "knowledge/index/andelinwest.idx" as *u8, "knowledge/index/andelinwest.manifest" as *u8, 91 "/tmp/aw_idx_out.txt" as *u8) 92 if rc_idx!=0 { a_puts("GATE-FAIL: index build rc=" as *u8); a_num(rc_idx); a_puts("\n" as *u8); sys_exit(1); return 1 } 93 94 a_write_file("/tmp/aw_sites.tsv" as *u8, 95 "aw\tknowledge/index/andelinwest.idx\tknowledge/index/andelinwest.manifest\thttps://andelinwest.com\n" as *u8) 96 let sites: *u8="/tmp/aw_sites.tsv" as *u8 97 let outp: *u8="/tmp/aw_gate_out.txt" as *u8 98 let obuf: *u8=sys_mmap(65536) 99 100 // (2) the site's OWN search-box example queries -> the RIGHT practice page #1 101 var pass: i64=0 102 let rows: i64=4 103 pass=pass+a_row("divorce" as *u8, "divorce" as *u8, "/practice/family-law" as *u8, sites, outp, obuf) 104 pass=pass+a_row("will" as *u8, "will" as *u8, "/practice/estate-planning" as *u8, sites, outp, obuf) 105 pass=pass+a_row("contract" as *u8, "contract" as *u8, "/practice/business-law" as *u8, sites, outp, obuf) 106 pass=pass+a_row("injury" as *u8, "injury" as *u8, "/practice/personal-injury" as *u8, sites, outp, obuf) 107 108 a_puts("----\nANDELINWEST-SEARCH rows=" as *u8); a_num(rows); a_puts(" pass=" as *u8); a_num(pass); a_puts("\n" as *u8) 109 let lg: i64=sys_openat_append("knowledge/status/andelinwest_search_gate.log" as *u8, 0x1a4) 110 if lg>=0 { 111 a_w(lg, "ANDELINWEST-SEARCH rows=" as *u8); a_wn(lg, rows); a_w(lg, " pass=" as *u8); a_wn(lg, pass) 112 if pass==rows { a_w(lg, " verdict=GREEN\n" as *u8) } else { a_w(lg, " verdict=RED\n" as *u8) } 113 sys_close(lg) 114 } 115 if pass==rows { a_puts("ANDELINWEST-SEARCH GREEN (real content, live client, measured)\n" as *u8); sys_exit(0); return 0 } 116 a_puts("ANDELINWEST-SEARCH RED\n" as *u8); sys_exit(1); return 1 117}