code wiki / (root) / nx_forge_judge.nx

nx_forge_judge.nx source

↩ module page · 174 lines · 7240 B

1// nx_forge_judge.nx -- FORGE-as-MCP: the DETERMINISTIC CHECKER exposed as a callable tool. 2// The core of the operator's asymmetric vision (2026-07-13): Claude Code (haiku/whatever) is the 3// Maker; our nishi coding model/organs are the domain kernel + CHECKER, engaged over MCP. This is 4// the checker: given a NishiLang organ NAME (source already at runtime/<name>.nx, written by the 5// caller e.g. via /api/srcwrite or the editor), it builds+runs through the SOVEREIGN LANE and 6// emits a compact JSON verdict Claude can act on -- byte-exact GREEN is a PROVABLE equality (our 7// exceed vs flaky-test verifiers). Optional argv[2] = expected-stdout-substring (byte check). 8// usage: nx_forge_judge eval-release <fixture-JSON-value> <response-JSON-value> (values, not paths); legacy: <organ> [expect-substring] 9// emits: {"name":..,"built":0/1,"run_exit":N,"verdict":"GREEN|COMPILE-FAIL|RUN-FAIL|OUTPUT-MISS", 10// "asm_bytes":N,"output":"<printable, truncated>"} 11// Sovereign (syscalls only), single fork of _offc/nx_sov_build_run.elf. license_tier: ORIGINAL 12// expect_exit: 0 (the TOOL succeeds even when the judged organ fails -- the verdict is the payload) 13import "nx_forge_evidence_t274.nx" 14import "nx_syscalls.nx" 15import "nx_lib_std.nx" 16const K_MAGIC_1048576: i64 = 1048576 17const K_MAGIC_1048575: i64 = 1048575 18const K_MAGIC_262144: i64 = 262144 19const K_MAGIC_3000: i64 = 3000 20 21func fj_contains(buf: *u8, n: i64, pat: *u8) -> i64 { 22 let pl: i64 = std_slen(pat) 23 if pl == 0 { return 1 } 24 if n < pl { return 0 } 25 var i: i64 = 0 26 let stop: i64 = n - pl 27 while i <= stop { 28 var m: i64 = 0 29 var hit: i64 = 1 30 while m < pl { 31 let bi: i64 = i + m 32 let a: i64 = buf[bi] as i64 33 let b: i64 = pat[m] as i64 34 if a != b { hit = 0; m = pl } 35 if a == b { m = m + 1 } 36 } 37 if hit == 1 { return 1 } 38 i = i + 1 39 } 40 return 0 41} 42 43// JSON-escape src[0,cap) into out (", \, control->space); returns len. Truncates at cap. 44func fj_jesc(out: *u8, off: i64, src: *u8, n: i64, cap: i64) -> i64 { 45 var o: i64 = off 46 var i: i64 = 0 47 while i < n { 48 if o >= cap - 2 { i = n } 49 if i < n { 50 let c: i64 = src[i] as i64 51 if c == 34 { out[o] = 92 as u8; o = o + 1; out[o] = 34 as u8; o = o + 1 } 52 if c == 92 { out[o] = 92 as u8; o = o + 1; out[o] = 92 as u8; o = o + 1 } 53 if c == 10 { out[o] = 92 as u8; o = o + 1; out[o] = 110 as u8; o = o + 1 } 54 if c == 9 { out[o] = 32 as u8; o = o + 1 } 55 if c == 13 { out[o] = 32 as u8; o = o + 1 } 56 if c != 34 && c != 92 && c != 10 && c != 9 && c != 13 { 57 if c < 32 { out[o] = 32 as u8; o = o + 1 } 58 if c >= 32 { out[o] = c as u8; o = o + 1 } 59 } 60 i = i + 1 61 } 62 } 63 return o 64} 65 66func main(argc: i64, argv: *i64) -> i64 { 67 if argc > 1 { if fje_eq(argv[1] as *u8,fje_len(argv[1] as *u8),"eval-release" as *u8)==1 { if argc != 4 { std_putln("{\"state\":\"usage\",\"reason\":\"eval-release requires two JSON argument values, not file paths\"}" as *u8);return 2 };return fje_cli(argv[2] as *u8,argv[3] as *u8) } } 68 if argc < 2 { 69 std_putln("{\"error\":\"usage: nx_forge_judge eval-release <fixture-JSON-value> <response-JSON-value> (values, not paths); legacy: <organ> [expect-substring]\"}" as *u8) 70 sys_exit(2) 71 return 2 72 } 73 let a1: i64 = argv[1] 74 let organ: *u8 = a1 as *u8 75 var expect: *u8 = "\x00" as *u8 76 var have_expect: i64 = 0 77 if argc >= 3 { 78 let a2: i64 = argv[2] 79 expect = a2 as *u8 80 have_expect = 1 81 } 82 // fork the sovereign lane, capture build+run output to /tmp 83 let pid: i64 = sys_fork() 84 if pid == 0 { 85 let ofd: i64 = sys_openat_wr("/tmp/nx_forge_judge.out" as *u8, 420) 86 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) } 87 let elf: *u8 = "_offc/nx_sov_build_run.elf" as *u8 88 let av: *i64 = sys_mmap(32) as *i64 89 av[0] = elf as i64 90 av[1] = organ as i64 91 av[2] = 0 92 let envp: *i64 = sys_mmap(16) as *i64 93 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64 94 envp[1] = 0 95 sys_execve(elf, av, envp) 96 sys_exit(127) 97 } 98 let st: *i64 = sys_mmap(16) as *i64 99 st[0] = 0 100 sys_wait4(pid, st, 0) 101 let sv: i64 = st[0] 102 let ec: i64 = (sv >> 8) & 255 103 let buf: *u8 = sys_mmap(K_MAGIC_1048576) as *u8 104 let fd: i64 = sys_openat_rd("/tmp/nx_forge_judge.out" as *u8) 105 var n: i64 = 0 106 if fd >= 0 { 107 var go: i64 = 1 108 while go == 1 { 109 let p: *u8 = buf + n 110 let room: i64 = K_MAGIC_1048575 - n 111 if room <= 0 { go = 0 } 112 if go == 1 { 113 let r: i64 = sys_read(fd, p, room) 114 if r <= 0 { go = 0 } 115 if r > 0 { n = n + r } 116 } 117 } 118 sys_close(fd) 119 } 120 // the lane prints "run-exit=0" + "SOVEREIGN(nx_cc" on a clean build+run; "COMPILE-FAIL" on build fail 121 let built: i64 = fj_contains(buf, n, "SOVEREIGN(nx_cc" as *u8) 122 let compfail: i64 = fj_contains(buf, n, "COMPILE-FAIL" as *u8) 123 let runok: i64 = fj_contains(buf, n, "run-exit=0" as *u8) 124 var verdict: *u8 = "RUN-FAIL" as *u8 125 var vok: i64 = 0 126 if compfail == 1 { verdict = "COMPILE-FAIL" as *u8 } 127 if compfail == 0 { 128 if built == 1 { 129 if runok == 1 { 130 if have_expect == 1 { 131 let hit: i64 = fj_contains(buf, n, expect) 132 if hit == 1 { verdict = "GREEN" as *u8; vok = 1 } 133 if hit == 0 { verdict = "OUTPUT-MISS" as *u8 } 134 } 135 if have_expect == 0 { verdict = "GREEN" as *u8; vok = 1 } 136 } 137 } 138 } 139 // emit JSON verdict 140 let out: *u8 = sys_mmap(K_MAGIC_262144) as *u8 141 var o: i64 = 0 142 o = std_scopy("{\"name\":\"" as *u8, out) 143 var oi: i64 = 0 144 while organ[oi] != (0 as u8) { out[o] = organ[oi]; o = o + 1; oi = oi + 1 } 145 let seg2: *u8 = "\",\"built\":" as *u8 146 var s2: i64 = 0 147 while seg2[s2] != (0 as u8) { out[o] = seg2[s2]; o = o + 1; s2 = s2 + 1 } 148 out[o] = (48 + built) as u8 149 o = o + 1 150 let seg3: *u8 = ",\"run_exit\":" as *u8 151 var s3: i64 = 0 152 while seg3[s3] != (0 as u8) { out[o] = seg3[s3]; o = o + 1; s3 = s3 + 1 } 153 let nb: *u8 = sys_mmap(32) as *u8 154 let nl: i64 = std_itoa(ec, nb) 155 var ni: i64 = 0 156 while ni < nl { out[o] = nb[ni]; o = o + 1; ni = ni + 1 } 157 let seg4: *u8 = ",\"verdict\":\"" as *u8 158 var s4: i64 = 0 159 while seg4[s4] != (0 as u8) { out[o] = seg4[s4]; o = o + 1; s4 = s4 + 1 } 160 var vi: i64 = 0 161 while verdict[vi] != (0 as u8) { out[o] = verdict[vi]; o = o + 1; vi = vi + 1 } 162 let seg5: *u8 = "\",\"output\":\"" as *u8 163 var s5: i64 = 0 164 while seg5[s5] != (0 as u8) { out[o] = seg5[s5]; o = o + 1; s5 = s5 + 1 } 165 var cap: i64 = n 166 if cap > K_MAGIC_3000 { cap = K_MAGIC_3000 } 167 o = fj_jesc(out, o, buf, cap, K_MAGIC_262144) 168 let seg6: *u8 = "\"}\n" as *u8 169 var s6: i64 = 0 170 while seg6[s6] != (0 as u8) { out[o] = seg6[s6]; o = o + 1; s6 = s6 + 1 } 171 sys_write(1, out, o) 172 sys_exit(0) 173 return 0 174}