code wiki / _hdl_build / nx_beir_gate.nx
nx_beir_gate.nx source
↩ module page · 102 lines · 4966 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(24) as *i64
50 argv[0] = "nx_beir_eval.elf" as *u8 as i64
51 // bm25only (2026-09-14): this gate holds the BM25 anchor and reads only the BM25 figure. Since the R0
52 // cross-encoder arm landed, a BARE run scores 50 candidates x 323 queries through the sovereign BERT --
53 // a twenty-hour job -- so the arms are skipped here; the arms are measured by their own runs, never by
54 // the roster beat that runs this gate daily.
55 argv[1] = "bm25only" as *u8 as i64
56 argv[2] = 0
57 let envp: *i64 = sys_mmap(16) as *i64
58 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
59 envp[1] = 0
60 sys_execve("nx_beir_eval.elf" as *u8, argv, envp)
61 sys_exit(127)
62 }
63 let st: *i64 = sys_mmap(16) as *i64
64 sys_wait4(pid, st, 0)
65 if (st[0] & 0x7f) != 0 { return 128 + (st[0] & 0x7f) }
66 return (st[0] >> 8) & 0xff
67}
68
69func main() -> i64 {
70 // ANCHOR FIRST (2026-08-04, nx_cwdguard finding): this organ reads a RELATIVE
71 // knowledge/ path, so its answer depended on where it was launched. No-op when
72 // already at the estate root, so the cron/MCP context is unchanged.
73 ep_anchor()
74 gv_puts("=== NX-BEIR-GATE: search ranking vs the EXTERNAL BEIR anchor (published-baseline floor) ===\n" as *u8)
75 let ctr: *i64 = gv_ctr()
76 let box: *i64 = sys_mmap(16) as *i64
77 let conf: *u8 = sys_read_file("knowledge/beir_bars.conf" as *u8, box)
78 var floor: i64 = 0 - 1
79 if (conf as i64) != 0 { floor = bg_int_after(conf, box[0], "ndcg_floor_permil_of_baseline=" as *u8) }
80 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 }
81 let rc: i64 = bg_run_eval("/tmp/beir_gate_eval.out" as *u8)
82 let obox: *i64 = sys_mmap(16) as *i64
83 let ob: *u8 = sys_read_file("/tmp/beir_gate_eval.out" as *u8, obox)
84 var ndcg: i64 = 0 - 1
85 var base: i64 = 0 - 1
86 if (ob as i64) != 0 {
87 ndcg = bg_int_after(ob, obox[0], "\"ndcg_at_10_permil\":" as *u8)
88 base = bg_int_after(ob, obox[0], "\"published_bm25_baseline_permil\":" as *u8)
89 }
90 gv_puts(" eval rc=" as *u8); gv_num(rc)
91 gv_puts(" ndcg@10=" as *u8); gv_num(ndcg)
92 gv_puts(" published=" as *u8); gv_num(base)
93 gv_puts(" floor_permil=" as *u8); gv_num(floor)
94 gv_puts("\n" as *u8)
95 var t1: i64 = 0
96 if rc == 0 { if ndcg > 0 { if base > 0 { t1 = 1 } } }
97 gv_check("T1 external eval ran and both permil figures parsed from its JSON" as *u8, t1, ctr)
98 var t2: i64 = 0
99 if t1 == 1 { if ndcg * 1000 >= floor * base { t2 = 1 } }
100 gv_check("T2 measured ndcg within the conf floor of the PUBLISHED baseline (cross-multiplied)" as *u8, t2, ctr)
101 return gv_verdict("BEIR-GATE" as *u8, ctr, "integer BM25 holds against external BEIR ground truth we did not author" as *u8)
102}