nx_dr_verify_cli.nx source
↩ module page · 79 lines · 4205 B
1// nx_dr_verify_cli.nx -- callable MCP/API surface for the citation-entailment verifier (DR-3).
2// Applies the drop-on-fail verification loop to a batch of per-claim entailment scores
3// (from the matcher/judge stage) and emits which claims SURVIVE into context memory,
4// which are DROPPED, and the forced belief-revision rate.
5// nx_dr_verify <threshold_permil> <entail_scores_csv>
6// e.g. nx_dr_verify 500 1000,666,0,800,300
7// -> {"survived":3,"dropped":2,"belief_revision_permil":400,"claims":[...]}
8// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
9import "nx_dr_verify.nx"
10import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
11import "nx_syscalls.nx"
12const K_MAGIC_1024: i64 = 1024
13
14func vf_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
15func vf_q() -> i64 { let b: *u8 = sys_mmap(1); b[0] = 34 as u8; sys_write(1, b, 1); return 0 }
16func vf_key(name: *u8) -> i64 { vf_q(); vf_puts(name); vf_q(); vf_puts(":" as *u8); return 0 }
17// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
18// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
19// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
20// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
21func vf_num(v: i64) -> i64 { nxi_out(v); return 0 }
22func vf_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
23func vf_atoi(a: *u8) -> i64 {
24 var v: i64 = 0; var i: i64 = 0
25 while a[i] != (0 as u8) {
26 let ch: i64 = a[i] as i64
27 if ch >= 48 { if ch <= 57 { v = v * 10 + (ch - 48) } }
28 i = i + 1
29 }
30 return v
31}
32func vf_parse_csv(s: *u8, out: *i64, maxn: i64) -> i64 {
33 var cnt: i64 = 0; var cur: i64 = 0; var have: i64 = 0; var i: i64 = 0
34 let n: i64 = vf_strlen(s)
35 while i < n {
36 let c: i64 = s[i] as i64
37 if c >= 48 { if c <= 57 { cur = cur * 10 + (c - 48); have = 1 } }
38 if c == 44 { if cnt < maxn { out[cnt] = cur; cnt = cnt + 1 } cur = 0; have = 0 }
39 i = i + 1
40 }
41 if have == 1 { if cnt < maxn { out[cnt] = cur; cnt = cnt + 1 } }
42 return cnt
43}
44
45func main(argc: i64, argv: *i64) -> i64 {
46 if argc < 3 { vf_puts("{" as *u8); vf_key("error" as *u8); vf_q(); vf_puts("usage: nx_dr_verify <threshold_permil> <entail_scores_csv>" as *u8); vf_q(); vf_puts("}\n" as *u8); sys_exit(2); return 2 }
47 let threshold: i64 = vf_atoi(argv[1] as *u8)
48 let sc: *i64 = sys_mmap(K_MAGIC_1024 * 8) as *i64
49 let n: i64 = vf_parse_csv(argv[2] as *u8, sc, K_MAGIC_1024)
50 if n < 1 { vf_puts("{" as *u8); vf_key("error" as *u8); vf_q(); vf_puts("need >=1 entailment score" as *u8); vf_q(); vf_puts("}\n" as *u8); sys_exit(1); return 1 }
51
52 let keep: *i64 = sys_mmap(K_MAGIC_1024 * 8) as *i64
53 let dropped: *i64 = sys_mmap(8) as *i64
54 let survived: i64 = dv_batch(sc, n, threshold, keep, dropped)
55 let rev: i64 = dv_revision_rate(dropped[0], n)
56
57 vf_puts("{" as *u8)
58 vf_key("tool" as *u8); vf_q(); vf_puts("nx_dr_verify" as *u8); vf_q(); vf_puts("," as *u8)
59 vf_key("n_claims" as *u8); vf_num(n); vf_puts("," as *u8)
60 vf_key("threshold_permil" as *u8); vf_num(threshold); vf_puts("," as *u8)
61 vf_key("survived" as *u8); vf_num(survived); vf_puts("," as *u8)
62 vf_key("dropped" as *u8); vf_num(dropped[0]); vf_puts("," as *u8)
63 vf_key("belief_revision_permil" as *u8); vf_num(rev); vf_puts("," as *u8)
64 vf_key("claims" as *u8); vf_puts("[" as *u8)
65 var r: i64 = 0
66 while r < n {
67 if r > 0 { vf_puts("," as *u8) }
68 vf_puts("{" as *u8)
69 vf_key("idx" as *u8); vf_num(r); vf_puts("," as *u8)
70 vf_key("entail" as *u8); vf_num(sc[r]); vf_puts("," as *u8)
71 vf_key("supported" as *u8); vf_num(keep[r])
72 vf_puts("}" as *u8)
73 r = r + 1
74 }
75 vf_puts("]," as *u8)
76 vf_key("note" as *u8); vf_q(); vf_puts("entailment=token-containment (cited+uncited back-tracking); drop-below-threshold; belief_revision=forced evidence consumption; sovereign integer" as *u8); vf_q()
77 vf_puts("}\n" as *u8)
78 sys_exit(0); return 0
79}