nx_forge_judge.nx source
↩ module page · 172 lines · 6744 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 <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_syscalls.nx"
14import "nx_lib_std.nx"
15const K_MAGIC_1048576: i64 = 1048576
16const K_MAGIC_1048575: i64 = 1048575
17const K_MAGIC_262144: i64 = 262144
18const K_MAGIC_3000: i64 = 3000
19
20func fj_contains(buf: *u8, n: i64, pat: *u8) -> i64 {
21 let pl: i64 = std_slen(pat)
22 if pl == 0 { return 1 }
23 if n < pl { return 0 }
24 var i: i64 = 0
25 let stop: i64 = n - pl
26 while i <= stop {
27 var m: i64 = 0
28 var hit: i64 = 1
29 while m < pl {
30 let bi: i64 = i + m
31 let a: i64 = buf[bi] as i64
32 let b: i64 = pat[m] as i64
33 if a != b { hit = 0; m = pl }
34 if a == b { m = m + 1 }
35 }
36 if hit == 1 { return 1 }
37 i = i + 1
38 }
39 return 0
40}
41
42// JSON-escape src[0,cap) into out (", \, control->space); returns len. Truncates at cap.
43func fj_jesc(out: *u8, off: i64, src: *u8, n: i64, cap: i64) -> i64 {
44 var o: i64 = off
45 var i: i64 = 0
46 while i < n {
47 if o >= cap - 2 { i = n }
48 if i < n {
49 let c: i64 = src[i] as i64
50 if c == 34 { out[o] = 92 as u8; o = o + 1; out[o] = 34 as u8; o = o + 1 }
51 if c == 92 { out[o] = 92 as u8; o = o + 1; out[o] = 92 as u8; o = o + 1 }
52 if c == 10 { out[o] = 92 as u8; o = o + 1; out[o] = 110 as u8; o = o + 1 }
53 if c == 9 { out[o] = 32 as u8; o = o + 1 }
54 if c == 13 { out[o] = 32 as u8; o = o + 1 }
55 if c != 34 && c != 92 && c != 10 && c != 9 && c != 13 {
56 if c < 32 { out[o] = 32 as u8; o = o + 1 }
57 if c >= 32 { out[o] = c as u8; o = o + 1 }
58 }
59 i = i + 1
60 }
61 }
62 return o
63}
64
65func main(argc: i64, argv: *i64) -> i64 {
66 if argc < 2 {
67 std_putln("{\"error\":\"usage: nx_forge_judge <organ> [expect-substring]\"}" as *u8)
68 sys_exit(2)
69 return 2
70 }
71 let a1: i64 = argv[1]
72 let organ: *u8 = a1 as *u8
73 var expect: *u8 = "\x00" as *u8
74 var have_expect: i64 = 0
75 if argc >= 3 {
76 let a2: i64 = argv[2]
77 expect = a2 as *u8
78 have_expect = 1
79 }
80 // fork the sovereign lane, capture build+run output to /tmp
81 let pid: i64 = sys_fork()
82 if pid == 0 {
83 let ofd: i64 = sys_openat_wr("/tmp/nx_forge_judge.out" as *u8, 420)
84 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
85 let elf: *u8 = "_offc/nx_sov_build_run.elf" as *u8
86 let av: *i64 = sys_mmap(32) as *i64
87 av[0] = elf as i64
88 av[1] = organ as i64
89 av[2] = 0
90 let envp: *i64 = sys_mmap(16) as *i64
91 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
92 envp[1] = 0
93 sys_execve(elf, av, envp)
94 sys_exit(127)
95 }
96 let st: *i64 = sys_mmap(16) as *i64
97 st[0] = 0
98 sys_wait4(pid, st, 0)
99 let sv: i64 = st[0]
100 let ec: i64 = (sv >> 8) & 255
101 let buf: *u8 = sys_mmap(K_MAGIC_1048576) as *u8
102 let fd: i64 = sys_openat_rd("/tmp/nx_forge_judge.out" as *u8)
103 var n: i64 = 0
104 if fd >= 0 {
105 var go: i64 = 1
106 while go == 1 {
107 let p: *u8 = buf + n
108 let room: i64 = K_MAGIC_1048575 - n
109 if room <= 0 { go = 0 }
110 if go == 1 {
111 let r: i64 = sys_read(fd, p, room)
112 if r <= 0 { go = 0 }
113 if r > 0 { n = n + r }
114 }
115 }
116 sys_close(fd)
117 }
118 // the lane prints "run-exit=0" + "SOVEREIGN(nx_cc" on a clean build+run; "COMPILE-FAIL" on build fail
119 let built: i64 = fj_contains(buf, n, "SOVEREIGN(nx_cc" as *u8)
120 let compfail: i64 = fj_contains(buf, n, "COMPILE-FAIL" as *u8)
121 let runok: i64 = fj_contains(buf, n, "run-exit=0" as *u8)
122 var verdict: *u8 = "RUN-FAIL" as *u8
123 var vok: i64 = 0
124 if compfail == 1 { verdict = "COMPILE-FAIL" as *u8 }
125 if compfail == 0 {
126 if built == 1 {
127 if runok == 1 {
128 if have_expect == 1 {
129 let hit: i64 = fj_contains(buf, n, expect)
130 if hit == 1 { verdict = "GREEN" as *u8; vok = 1 }
131 if hit == 0 { verdict = "OUTPUT-MISS" as *u8 }
132 }
133 if have_expect == 0 { verdict = "GREEN" as *u8; vok = 1 }
134 }
135 }
136 }
137 // emit JSON verdict
138 let out: *u8 = sys_mmap(K_MAGIC_262144) as *u8
139 var o: i64 = 0
140 o = std_scopy("{\"name\":\"" as *u8, out)
141 var oi: i64 = 0
142 while organ[oi] != (0 as u8) { out[o] = organ[oi]; o = o + 1; oi = oi + 1 }
143 let seg2: *u8 = "\",\"built\":" as *u8
144 var s2: i64 = 0
145 while seg2[s2] != (0 as u8) { out[o] = seg2[s2]; o = o + 1; s2 = s2 + 1 }
146 out[o] = (48 + built) as u8
147 o = o + 1
148 let seg3: *u8 = ",\"run_exit\":" as *u8
149 var s3: i64 = 0
150 while seg3[s3] != (0 as u8) { out[o] = seg3[s3]; o = o + 1; s3 = s3 + 1 }
151 let nb: *u8 = sys_mmap(32) as *u8
152 let nl: i64 = std_itoa(ec, nb)
153 var ni: i64 = 0
154 while ni < nl { out[o] = nb[ni]; o = o + 1; ni = ni + 1 }
155 let seg4: *u8 = ",\"verdict\":\"" as *u8
156 var s4: i64 = 0
157 while seg4[s4] != (0 as u8) { out[o] = seg4[s4]; o = o + 1; s4 = s4 + 1 }
158 var vi: i64 = 0
159 while verdict[vi] != (0 as u8) { out[o] = verdict[vi]; o = o + 1; vi = vi + 1 }
160 let seg5: *u8 = "\",\"output\":\"" as *u8
161 var s5: i64 = 0
162 while seg5[s5] != (0 as u8) { out[o] = seg5[s5]; o = o + 1; s5 = s5 + 1 }
163 var cap: i64 = n
164 if cap > K_MAGIC_3000 { cap = K_MAGIC_3000 }
165 o = fj_jesc(out, o, buf, cap, K_MAGIC_262144)
166 let seg6: *u8 = "\"}\n" as *u8
167 var s6: i64 = 0
168 while seg6[s6] != (0 as u8) { out[o] = seg6[s6]; o = o + 1; s6 = s6 + 1 }
169 sys_write(1, out, o)
170 sys_exit(0)
171 return 0
172}