code wiki / _hdl_build / nx_beir_gate.nx

nx_beir_gate.nx source

↩ module page · 97 lines · 4533 B

1// nx_beir_gate.nx -- EXTERNAL-ANCHOR gate for the search domain: wraps the live nx_beir_eval 2// (BEIR/nfcorpus, the field's standard IR benchmark -- ground truth we did not author) and holds the 3// measured BM25 ndcg@10 against a conf-declared floor relative to the PUBLISHED baseline for the same 4// dataset. The eval emits honest JSON (incl rerank-arm UNAVAILABLE-is-not-zero); this gate adds the 5// verdict contract: bar in DATA (knowledge/beir_bars.conf, rule 11 -- absent conf = RED, a defaulted 6// bar is a wish), CROSS-MULTIPLIED comparison (ndcg*1000 >= floor*baseline -- the integer-truncation 7// silent-threshold-shift law, banked 2026-08-01), canonical gv_verdict tail + harness.jrnl frame. 8// license_tier: ORIGINAL expect_exit: 0 when the external anchor holds 9import "nx_syscalls.nx" 10import "nx_estate_path.nx" // ep_anchor: the CWD must not decide this organ's verdict 11import "nx_gate_verdict.nx" 12 13func bg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 14 15func bg_find(hay: *u8, n: i64, needle: *u8) -> i64 { 16 let nl: i64 = bg_slen(needle) 17 if nl == 0 { return 0 - 1 } 18 var i: i64 = 0 19 while i + nl <= n { 20 var j: i64 = 0 21 var hit: i64 = 1 22 while j < nl { if hay[i+j] != needle[j] { hit = 0; j = nl } else { j = j + 1 } } 23 if hit == 1 { return i } 24 i = i + 1 25 } 26 return 0 - 1 27} 28 29// parse the unsigned int immediately after `needle` in hay[0..n); -1 if needle absent or no digits 30func bg_int_after(hay: *u8, n: i64, needle: *u8) -> i64 { 31 let at: i64 = bg_find(hay, n, needle) 32 if at < 0 { return 0 - 1 } 33 var p: i64 = at + bg_slen(needle) 34 var v: i64 = 0 35 var got: i64 = 0 36 while p < n { 37 let c: i64 = hay[p] as i64 38 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48); got = 1; p = p + 1 } else { p = n } } else { p = n } 39 } 40 if got == 0 { return 0 - 1 } 41 return v 42} 43 44func bg_run_eval(out_path: *u8) -> i64 { 45 let pid: i64 = sys_fork() 46 if pid == 0 { 47 let out: i64 = sys_openat_wr(out_path, 0x1a4) 48 if out >= 0 { sys_dup3(out, 1, 0) } 49 let argv: *i64 = sys_mmap(16) as *i64 50 argv[0] = "nx_beir_eval.elf" as *u8 as i64 51 argv[1] = 0 52 let envp: *i64 = sys_mmap(16) as *i64 53 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64 54 envp[1] = 0 55 sys_execve("nx_beir_eval.elf" as *u8, argv, envp) 56 sys_exit(127) 57 } 58 let st: *i64 = sys_mmap(16) as *i64 59 sys_wait4(pid, st, 0) 60 if (st[0] & 0x7f) != 0 { return 128 + (st[0] & 0x7f) } 61 return (st[0] >> 8) & 0xff 62} 63 64func main() -> i64 { 65 // ANCHOR FIRST (2026-08-04, nx_cwdguard finding): this organ reads a RELATIVE 66 // knowledge/ path, so its answer depended on where it was launched. No-op when 67 // already at the estate root, so the cron/MCP context is unchanged. 68 ep_anchor() 69 gv_puts("=== NX-BEIR-GATE: search ranking vs the EXTERNAL BEIR anchor (published-baseline floor) ===\n" as *u8) 70 let ctr: *i64 = gv_ctr() 71 let box: *i64 = sys_mmap(16) as *i64 72 let conf: *u8 = sys_read_file("knowledge/beir_bars.conf" as *u8, box) 73 var floor: i64 = 0 - 1 74 if (conf as i64) != 0 { floor = bg_int_after(conf, box[0], "ndcg_floor_permil_of_baseline=" as *u8) } 75 if floor < 0 { gv_puts("BEIR-GATE RED: knowledge/beir_bars.conf absent or floor unreadable -- a defaulted bar is a wish\n" as *u8); return 4 } 76 let rc: i64 = bg_run_eval("/tmp/beir_gate_eval.out" as *u8) 77 let obox: *i64 = sys_mmap(16) as *i64 78 let ob: *u8 = sys_read_file("/tmp/beir_gate_eval.out" as *u8, obox) 79 var ndcg: i64 = 0 - 1 80 var base: i64 = 0 - 1 81 if (ob as i64) != 0 { 82 ndcg = bg_int_after(ob, obox[0], "\"ndcg_at_10_permil\":" as *u8) 83 base = bg_int_after(ob, obox[0], "\"published_bm25_baseline_permil\":" as *u8) 84 } 85 gv_puts(" eval rc=" as *u8); gv_num(rc) 86 gv_puts(" ndcg@10=" as *u8); gv_num(ndcg) 87 gv_puts(" published=" as *u8); gv_num(base) 88 gv_puts(" floor_permil=" as *u8); gv_num(floor) 89 gv_puts("\n" as *u8) 90 var t1: i64 = 0 91 if rc == 0 { if ndcg > 0 { if base > 0 { t1 = 1 } } } 92 gv_check("T1 external eval ran and both permil figures parsed from its JSON" as *u8, t1, ctr) 93 var t2: i64 = 0 94 if t1 == 1 { if ndcg * 1000 >= floor * base { t2 = 1 } } 95 gv_check("T2 measured ndcg within the conf floor of the PUBLISHED baseline (cross-multiplied)" as *u8, t2, ctr) 96 return gv_verdict("BEIR-GATE" as *u8, ctr, "integer BM25 holds against external BEIR ground truth we did not author" as *u8) 97}