nx_dr_run_cli.nx source
↩ module page · 81 lines · 4076 B
1// nx_dr_run_cli.nx -- callable MCP/API surface for the real-text judge stage (DR-0 rung-2).
2// Given a fetched document and a list of groundtruth-insight strings, computes which insights
3// the document actually supports (token-containment >= threshold) and the DRBench insight-recall.
4// nx_dr_run <threshold_permil> <document_text> <insight_1> [insight_2 ...]
5// e.g. nx_dr_run 500 "the co-scientist system uses a tournament of ideas" "tournament of ideas" "liver fibrosis"
6// -> {"document_tokens":8,"n_insights":2,"covered":1,"insight_recall_permil":500,"insights":[...]}
7// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
8import "nx_dr_run.nx"
9import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
10import "nx_dr_verify.nx"
11import "nx_dr_drbench.nx"
12import "nx_syscalls.nx"
13const K_MAGIC_8192: i64 = 8192
14const K_MAGIC_2048: i64 = 2048
15
16func rn_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
17func rn_q() -> i64 { let b: *u8 = sys_mmap(1); b[0] = 34 as u8; sys_write(1, b, 1); return 0 }
18func rn_key(name: *u8) -> i64 { rn_q(); rn_puts(name); rn_q(); rn_puts(":" as *u8); return 0 }
19// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
20// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
21// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
22// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
23func rn_num(v: i64) -> i64 { nxi_out(v); return 0 }
24func rn_atoi(a: *u8) -> i64 {
25 var v: i64 = 0; var i: i64 = 0
26 while a[i] != (0 as u8) {
27 let ch: i64 = a[i] as i64
28 if ch >= 48 { if ch <= 57 { v = v * 10 + (ch - 48) } }
29 i = i + 1
30 }
31 return v
32}
33
34func main(argc: i64, argv: *i64) -> i64 {
35 if argc < 4 { rn_puts("{" as *u8); rn_key("error" as *u8); rn_q(); rn_puts("usage: nx_dr_run <threshold_permil> <document> <insight>..." as *u8); rn_q(); rn_puts("}\n" as *u8); sys_exit(2); return 2 }
36 let threshold: i64 = rn_atoi(argv[1] as *u8)
37 let ds: *u8 = argv[2] as *u8
38 let doc: *i64 = sys_mmap(K_MAGIC_8192 * 8) as *i64
39 let nd: i64 = drr_tokenize(ds, drr_strlen(ds), doc, K_MAGIC_8192)
40
41 let ni_total: i64 = argc - 3
42 let cov: *i64 = sys_mmap(ni_total * 8) as *i64
43 let ent: *i64 = sys_mmap(ni_total * 8) as *i64
44 var k: i64 = 0
45 while k < ni_total {
46 let is: *u8 = argv[3 + k] as *u8
47 let ins: *i64 = sys_mmap(K_MAGIC_2048 * 8) as *i64
48 let ni: i64 = drr_tokenize(is, drr_strlen(is), ins, K_MAGIC_2048)
49 let e: i64 = dv_entail(ins, ni, doc, nd)
50 ent[k] = e
51 if e >= threshold { cov[k] = 1 } else { cov[k] = 0 }
52 k = k + 1
53 }
54 let recall: i64 = dr_frac(cov, ni_total)
55 var covered: i64 = 0
56 var j: i64 = 0
57 while j < ni_total { covered = covered + cov[j]; j = j + 1 }
58
59 rn_puts("{" as *u8)
60 rn_key("tool" as *u8); rn_q(); rn_puts("nx_dr_run" as *u8); rn_q(); rn_puts("," as *u8)
61 rn_key("document_tokens" as *u8); rn_num(nd); rn_puts("," as *u8)
62 rn_key("n_insights" as *u8); rn_num(ni_total); rn_puts("," as *u8)
63 rn_key("threshold_permil" as *u8); rn_num(threshold); rn_puts("," as *u8)
64 rn_key("covered" as *u8); rn_num(covered); rn_puts("," as *u8)
65 rn_key("insight_recall_permil" as *u8); rn_num(recall); rn_puts("," as *u8)
66 rn_key("insights" as *u8); rn_puts("[" as *u8)
67 var r: i64 = 0
68 while r < ni_total {
69 if r > 0 { rn_puts("," as *u8) }
70 rn_puts("{" as *u8)
71 rn_key("idx" as *u8); rn_num(r); rn_puts("," as *u8)
72 rn_key("entail_permil" as *u8); rn_num(ent[r]); rn_puts("," as *u8)
73 rn_key("covered" as *u8); rn_num(cov[r])
74 rn_puts("}" as *u8)
75 r = r + 1
76 }
77 rn_puts("]," as *u8)
78 rn_key("note" as *u8); rn_q(); rn_puts("real-text lexical judge (djb2 token-containment); insight covered iff entailment>=threshold; sovereign integer" as *u8); rn_q()
79 rn_puts("}\n" as *u8)
80 sys_exit(0); return 0
81}