nx_engine_dispatch_gate.nx source
↩ module page · 377 lines · 27691 B
1// nx_engine_dispatch_gate.nx -- the REFEREE for nx_engine_dispatch (ES3 + ES16 of /compare/engineshift, 2026-09-02).
2// Drives the DEPLOYED dispatcher over fixture tasks and a FIXTURE SEAT the gate serves from its own forked process
3// (one canned /gen reply per fork, pure NishiLang, no model load), and asserts the ladder: the declared judge decides,
4// a C task never reaches the external tier, an unclassed task is REFUSED by name (never defaulted), an unreachable
5// ladder is UNOBSERVABLE never GREEN, nothing below the seat passing is NEEDS-FRONTIER, --dry forks nothing and writes
6// nothing, a failed scoreboard push never changes a verdict, and ES16: a class row resolves clearance, engine order and
7// the token ceiling from data, a C row that lists external is refused at load, a missing row is refused by name, and a
8// class that admits nothing below the seat hands off without touching any engine. Every ledger row is counted against
9// its denominator.
10// nx_engine_dispatch_gate [subject-elf] default _build/nx_engine_dispatch.sov.elf (cwd nxc2)
11// Fixture seats bind 127.0.0.1 on four distinct ports; a busy port is a named PRECONDITION, never a RED.
12// license_tier: ORIGINAL. Writes only under /tmp/nx_engine_dispatch_gate/. No hw writes.
13import "nx_syscalls.nx"
14import "nx_gate_verdict.nx"
15import "nx_gatekit_lib.nx"
16import "nx_tool_run.nx"
17
18const MG_OUT: i64 = 65536
19const MG_I64: i64 = 8
20const MG_ARGV: i64 = 16
21const MG_TIMEOUT_MS: i64 = 30000
22const MG_REQ_CAP: i64 = 65536
23const MG_BODY_CAP: i64 = 4096
24const MG_READY_TRIES: i64 = 100 // readiness and reap are bounded: 100 x 20 ms = 2 s, then the fixture is declared absent
25const MG_READY_STEP_MS: i64 = 20
26const MG_PORT_A: i64 = 18632 // canned "The answer is 42"
27const MG_PORT_B: i64 = 18633 // canned "no idea"
28const MG_PORT_C: i64 = 18634 // canned "The answer is 42" for the tclass tooth (own port so a lingering child cannot alias)
29const MG_PORT_D: i64 = 18635 // canned "no idea" for the SKIPPED-by-flag tooth
30const MG_SO_REUSEADDR_CALL: i64 = 208 // RV64 setsockopt const form (raw 54 collides in the translator) -- as the seat itself
31const MG_LEDGER_ROWS: i64 = 16 // t42 1 + tkat 3 + t42-noext 2 + unreachable 1 + push-failed 1 + tcls 1 + tfront 3 + tnoloc 2 + mute 2
32const MG_ROOT: *u8 = "/tmp/nx_engine_dispatch_gate"
33const MG_STATE: *u8 = "/tmp/nx_engine_dispatch_gate/state"
34const MG_LEDGER: *u8 = "/tmp/nx_engine_dispatch_gate/state/engine_dispatch.jrnl"
35const MG_PROMPT: *u8 = "/tmp/nx_engine_dispatch_gate/prompt.txt"
36const MG_EXPECTED: *u8 = "/tmp/nx_engine_dispatch_gate/expected.txt"
37const MG_CAND_GOOD: *u8 = "/tmp/nx_engine_dispatch_gate/cand_good.txt"
38const MG_CAND_BAD: *u8 = "/tmp/nx_engine_dispatch_gate/cand_bad.txt"
39const MG_T42: *u8 = "/tmp/nx_engine_dispatch_gate/t42.conf"
40const MG_TKAT: *u8 = "/tmp/nx_engine_dispatch_gate/tkat.conf"
41const MG_TMISS: *u8 = "/tmp/nx_engine_dispatch_gate/tmiss.conf"
42const MG_TBAD: *u8 = "/tmp/nx_engine_dispatch_gate/tbad.conf"
43const MG_TNOCLASS: *u8 = "/tmp/nx_engine_dispatch_gate/tnoclass.conf"
44const MG_CLASSES: *u8 = "/tmp/nx_engine_dispatch_gate/classes.conf"
45const MG_TCLS: *u8 = "/tmp/nx_engine_dispatch_gate/tcls.conf"
46const MG_TFRONT: *u8 = "/tmp/nx_engine_dispatch_gate/tfront.conf"
47const MG_TNOLOC: *u8 = "/tmp/nx_engine_dispatch_gate/tnoloc.conf"
48const MG_TBADCLS: *u8 = "/tmp/nx_engine_dispatch_gate/tbadcls.conf"
49const MG_TMISSING: *u8 = "/tmp/nx_engine_dispatch_gate/tmissing.conf"
50
51func mg_run(elf: *u8, a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8, a6: *u8, a7: *u8, a8: *u8, a9: *u8, a10: *u8, a11: *u8, out: *u8, outlen: *i64) -> i64 {
52 let av: *i64 = sys_mmap(MG_ARGV * MG_I64) as *i64
53 av[0] = elf as i64; av[1] = a1 as i64; av[2] = a2 as i64; av[3] = a3 as i64; av[4] = a4 as i64; av[5] = a5 as i64
54 av[6] = a6 as i64; av[7] = a7 as i64; av[8] = a8 as i64; av[9] = a9 as i64; av[10] = a10 as i64; av[11] = a11 as i64
55 var k: i64 = 1
56 var z: i64 = 0
57 while k < 12 { if z == 0 { if gk_len(av[k] as *u8) == 0 { av[k] = 0; z = 1 } } else { av[k] = 0 } k = k + 1 }
58 av[12] = 0
59 let rc: i64 = tr_run_capture_to(elf, av, out, MG_OUT, outlen, MG_TIMEOUT_MS)
60 let n: i64 = outlen[0]
61 if n >= 0 { if n < MG_OUT { out[n] = 0 as u8 } }
62 sys_munmap(av as *u8, MG_ARGV * MG_I64)
63 return rc
64}
65
66// THE FIXTURE SEAT: bind 127.0.0.1:port, accept ONE connection, read the request to its Content-Length, answer with
67// a /gen-shaped JSON carrying `canned` as text, close, return. Runs in the forked child.
68func mg_serve_once(port: i64, canned: *u8) -> i64 {
69 let addr: *u8 = sys_mmap(16)
70 addr[0] = 2 as u8; addr[1] = 0 as u8
71 addr[2] = ((port >> 8) & 255) as u8
72 addr[3] = (port & 255) as u8
73 addr[4] = 127 as u8; addr[5] = 0 as u8; addr[6] = 0 as u8; addr[7] = 1 as u8
74 var az: i64 = 8
75 while az < 16 { addr[az] = 0 as u8; az = az + 1 }
76 let lfd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0)
77 if lfd < 0 { return 0 - 1 }
78 let so_one: *i64 = sys_mmap(8) as *i64
79 so_one[0] = 1
80 __syscall(MG_SO_REUSEADDR_CALL, lfd, 1, 2, so_one as i64, 4, 0)
81 if sys_bind(lfd, addr, 16) < 0 { sys_close(lfd); return 0 - 2 }
82 if sys_listen(lfd, 4) < 0 { sys_close(lfd); return 0 - 3 }
83 // accept until a POST /gen arrives: the parent's readiness probe is a bare connect-and-close, which reads as an
84 // empty request and is answered with nothing -- so readiness is MEASURED, never slept for
85 let req: *u8 = sys_mmap(MG_REQ_CAP)
86 var cfd: i64 = 0 - 1
87 var n: i64 = 0
88 var served: i64 = 0
89 while served == 0 {
90 cfd = sys_accept(lfd)
91 if cfd < 0 { sys_close(lfd); return 0 - 4 }
92 n = 0
93 var hdr_end: i64 = 0 - 1
94 var want: i64 = 0 - 1
95 var go: i64 = 1
96 while go == 1 {
97 if n >= MG_REQ_CAP - 1 { go = 0 } else {
98 let r: i64 = sys_read(cfd, ((req as i64) + n) as *u8, MG_REQ_CAP - 1 - n)
99 if r <= 0 { go = 0 } else {
100 n = n + r
101 if hdr_end < 0 {
102 var i: i64 = 0
103 while i + 3 < n {
104 if req[i] == (13 as u8) { if req[i + 1] == (10 as u8) { if req[i + 2] == (13 as u8) { if req[i + 3] == (10 as u8) { hdr_end = i + 4; i = n } } } }
105 i = i + 1
106 }
107 if hdr_end >= 0 {
108 let p: i64 = gk_out_pos(req, hdr_end, "Content-Length: " as *u8)
109 if p >= 0 {
110 var v: i64 = 0
111 var q: i64 = p + 16
112 var d: i64 = 1
113 while d == 1 { let c: i64 = req[q] as i64; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48); q = q + 1 } else { d = 0 } } else { d = 0 } }
114 want = hdr_end + v
115 } else { want = hdr_end }
116 }
117 }
118 if want >= 0 { if n >= want { go = 0 } }
119 }
120 }
121 }
122 if n > 0 { if gk_out_pos(req, n, "POST /gen" as *u8) >= 0 { served = 1 } }
123 if served == 0 { sys_close(cfd) }
124 }
125 let body: *u8 = sys_mmap(MG_BODY_CAP)
126 var bo: i64 = gk_cat(body, 0, "{\"ok\":1,\"mode\":\"i8\",\"prompt_tokens\":3,\"gen_tokens\":5,\"ms_total\":50,\"ms_per_token\":10,\"eos\":1,\"temp\":0,\"seed\":1,\"text\":\"" as *u8)
127 bo = gk_cat(body, bo, canned)
128 bo = gk_cat(body, bo, "\"}" as *u8)
129 let resp: *u8 = sys_mmap(MG_BODY_CAP)
130 var ro: i64 = gk_cat(resp, 0, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: " as *u8)
131 ro = gk_catn(resp, ro, bo)
132 ro = gk_cat(resp, ro, "\r\nConnection: close\r\n\r\n" as *u8)
133 var bi: i64 = 0
134 while bi < bo { resp[ro] = body[bi]; ro = ro + 1; bi = bi + 1 }
135 gk_write_all(cfd, resp, ro)
136 sys_close(cfd)
137 sys_close(lfd)
138 return 0
139}
140
141// readiness probe: a bare connect-and-close the fixture treats as an empty request; 1 when the port accepts
142func mg_port_ready(port: i64) -> i64 {
143 let addr: *u8 = sys_mmap(16)
144 addr[0] = 2 as u8; addr[1] = 0 as u8
145 addr[2] = ((port >> 8) & 255) as u8
146 addr[3] = (port & 255) as u8
147 addr[4] = 127 as u8; addr[5] = 0 as u8; addr[6] = 0 as u8; addr[7] = 1 as u8
148 var az: i64 = 8
149 while az < 16 { addr[az] = 0 as u8; az = az + 1 }
150 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0)
151 if fd < 0 { return 0 }
152 let cr: i64 = sys_connect(fd, addr, 16)
153 sys_close(fd)
154 if cr < 0 { return 0 }
155 return 1
156}
157
158// fork a fixture seat; returns the child pid once the port MEASURABLY accepts (bounded wait), or -1
159func mg_fork_seat(port: i64, canned: *u8) -> i64 {
160 let pid: i64 = sys_fork()
161 if pid == 0 { mg_serve_once(port, canned); sys_exit(0); return 0 }
162 var tries: i64 = 0
163 while tries < MG_READY_TRIES {
164 if mg_port_ready(port) == 1 { return pid }
165 sys_sleep_ms(MG_READY_STEP_MS)
166 tries = tries + 1
167 }
168 return 0 - 1
169}
170
171// a MUTE fixture seat (2026-09-05): accepts the connection, reads the POST /gen request, and closes WITHOUT writing a
172// byte -- the shape of a listener that is not a seat. Measured on the estate host: 127.0.0.1:8032 accepted and closed,
173// and the dispatcher called that ANSWERED-UNPARSEABLE http=0, a status word naming the parser for a socket defect.
174const MG_PORT_E: i64 = 18636 // mute: accepts and closes without a body (the NO-REPLY tooth)
175func mg_serve_mute(port: i64) -> i64 {
176 let addr: *u8 = sys_mmap(16)
177 addr[0] = 2 as u8; addr[1] = 0 as u8
178 addr[2] = ((port >> 8) & 255) as u8
179 addr[3] = (port & 255) as u8
180 addr[4] = 127 as u8; addr[5] = 0 as u8; addr[6] = 0 as u8; addr[7] = 1 as u8
181 var az: i64 = 8
182 while az < 16 { addr[az] = 0 as u8; az = az + 1 }
183 let lfd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0)
184 if lfd < 0 { return 0 - 1 }
185 let so_one: *i64 = sys_mmap(8) as *i64
186 so_one[0] = 1
187 __syscall(MG_SO_REUSEADDR_CALL, lfd, 1, 2, so_one as i64, 4, 0)
188 if sys_bind(lfd, addr, 16) < 0 { sys_close(lfd); return 0 - 2 }
189 if sys_listen(lfd, 4) < 0 { sys_close(lfd); return 0 - 3 }
190 // the parent's readiness probe is a bare connect-and-close (an empty read); serve until a POST /gen arrives,
191 // then close it unanswered -- that is the whole fixture
192 let req: *u8 = sys_mmap(MG_REQ_CAP)
193 var served: i64 = 0
194 while served == 0 {
195 let cfd: i64 = sys_accept(lfd)
196 if cfd < 0 { sys_close(lfd); return 0 - 4 }
197 let r: i64 = sys_read(cfd, req, MG_REQ_CAP - 1)
198 if r > 0 { if gk_out_pos(req, r, "POST /gen" as *u8) >= 0 { served = 1 } }
199 sys_close(cfd)
200 }
201 sys_close(lfd)
202 return 0
203}
204func mg_fork_mute(port: i64) -> i64 {
205 let pid: i64 = sys_fork()
206 if pid == 0 { mg_serve_mute(port); sys_exit(0); return 0 }
207 var tries: i64 = 0
208 while tries < MG_READY_TRIES {
209 if mg_port_ready(port) == 1 { return pid }
210 sys_sleep_ms(MG_READY_STEP_MS)
211 tries = tries + 1
212 }
213 return 0 - 1
214}
215
216// reap the fixture child: a served seat has exited; one still blocked in accept (the subject never connected) is
217// killed rather than waited on forever -- a gate that can hang is a gate nobody runs
218func mg_reap(pid: i64) -> i64 {
219 if pid <= 0 { return 0 }
220 let st: *i64 = sys_mmap(8) as *i64
221 var tries: i64 = 0
222 while tries < MG_READY_TRIES {
223 let w: i64 = sys_wait4(pid, st, WNOHANG)
224 if w == pid { return 0 }
225 sys_sleep_ms(MG_READY_STEP_MS)
226 tries = tries + 1
227 }
228 nx_kill(pid, 9)
229 sys_wait4(pid, st, 0)
230 return 0 - 1
231}
232
233func main(argc: i64, argv: *i64) -> i64 {
234 var elf: *u8 = "_build/nx_engine_dispatch.sov.elf" as *u8
235 if argc >= 2 { elf = argv[1] as *u8 }
236 gv_head("NX-ENGINE-DISPATCH-GATE: the dispatcher over fixture tasks, a fixture seat served from this process, and ES16 class rows" as *u8)
237 let ctr: *i64 = gv_ctr()
238 gv_need("subject elf present" as *u8, gk_exists(elf), ctr)
239
240 // ---- setup ----
241 gk_mkdir(MG_ROOT)
242 gk_mkdir(MG_STATE)
243 gk_rm(MG_LEDGER)
244 gk_write(MG_PROMPT, "What is the answer?\n" as *u8)
245 gk_write(MG_EXPECTED, "The answer is 42\n" as *u8)
246 gk_write(MG_CAND_GOOD, " The answer is 42\n\n" as *u8)
247 gk_write(MG_CAND_BAD, "no idea" as *u8)
248 gk_write(MG_T42, "# fixture task\nname|t42\nclass|U\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|contains|42\nmax_new|8\n" as *u8)
249 gk_write(MG_TKAT, "name|tkat\nclass|C\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|kat|/tmp/nx_engine_dispatch_gate/expected.txt\n" as *u8)
250 gk_write(MG_TMISS, "name|tmiss\nclass|U\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|kat|/nonexistent/expected.txt\n" as *u8)
251 gk_write(MG_TBAD, "name|tbad\nclass|U\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|foo|x\n" as *u8)
252 gk_write(MG_TNOCLASS, "name|tnc\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|contains|42\n" as *u8)
253 // ES16 fixtures: the class rows and the tasks that name them
254 gk_write(MG_CLASSES, "# fixture class rows\nclass|u-local-ext|U|local,external,frontier|8\nclass|frontier-only|C|frontier|24\nclass|u-ext-only|U|external,frontier|8\nclass|c-lists-ext|C|local,external,frontier|8\n" as *u8)
255 gk_write(MG_TCLS, "name|tcls\ntclass|u-local-ext\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|contains|42\nmax_new|64\n" as *u8)
256 gk_write(MG_TFRONT, "name|tfront\ntclass|frontier-only\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|contains|42\n" as *u8)
257 gk_write(MG_TNOLOC, "name|tnoloc\ntclass|u-ext-only\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|contains|42\n" as *u8)
258 gk_write(MG_TBADCLS, "name|tbadcls\ntclass|c-lists-ext\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|contains|42\n" as *u8)
259 gk_write(MG_TMISSING, "name|tmissing\ntclass|nonesuch\nprompt|/tmp/nx_engine_dispatch_gate/prompt.txt\njudge|contains|42\n" as *u8)
260 let out: *u8 = sys_mmap(MG_OUT)
261 let ol: *i64 = sys_mmap(MG_I64) as *i64
262
263 // ---- the judge alone ----
264 mg_run(elf, "judge" as *u8, MG_CAND_GOOD, MG_T42, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
265 gv_check("T1 contains: a candidate carrying the literal is GREEN" as *u8, gk_out_has(out, ol[0], "rule=contains verdict=GREEN" as *u8), ctr)
266 mg_run(elf, "judge" as *u8, MG_CAND_GOOD, MG_TKAT, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
267 gv_check("T2 kat: byte-exact after trimming outer whitespace is GREEN" as *u8, gk_out_has(out, ol[0], "rule=kat verdict=GREEN" as *u8), ctr)
268 mg_run(elf, "judge" as *u8, MG_CAND_BAD, MG_T42, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
269 gv_check("T3 contains: a candidate without the literal is RED" as *u8, gk_out_has(out, ol[0], "verdict=RED" as *u8), ctr)
270 mg_run(elf, "judge" as *u8, MG_CAND_BAD, MG_TKAT, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
271 gv_check("T4 kat: a different candidate is RED" as *u8, gk_out_has(out, ol[0], "rule=kat verdict=RED" as *u8), ctr)
272 mg_run(elf, "judge" as *u8, MG_CAND_GOOD, MG_TMISS, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
273 gv_check("neg-control-a-kat-whose-expected-file-is-missing-is-UNOBSERVABLE-never-RED-never-GREEN" as *u8, gk_out_has(out, ol[0], "verdict=UNOBSERVABLE" as *u8), ctr)
274 mg_run(elf, "judge" as *u8, MG_CAND_GOOD, MG_TBAD, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
275 gv_check("neg-control-an-unknown-judge-kind-is-refused-by-name" as *u8, gk_out_has(out, ol[0], "judge kind must be kat or contains" as *u8) * gk_out_has(out, ol[0], "verdict=USAGE" as *u8), ctr)
276
277 // ---- the ladder against the fixture seat ----
278 let pa: i64 = mg_fork_seat(MG_PORT_A, "The answer is 42" as *u8)
279 var pa_ok: i64 = 1
280 if pa < 0 { pa_ok = 0 }
281 gv_need("fixture seat A accepts on 127.0.0.1:18632" as *u8, pa_ok, ctr)
282 mg_run(elf, "run" as *u8, MG_T42, MG_STATE, "--local" as *u8, "127.0.0.1:18632" as *u8, "--no-external" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
283 mg_reap(pa)
284 gv_puts(out)
285 gv_check("T5 a U task the local seat answers correctly is GREEN with engine=local" as *u8, gk_out_has(out, ol[0], "ENGINE local status=ANSWERED gen_tokens=5 prompt_tokens=3 ms=50 judge=GREEN" as *u8) * gk_out_has(out, ol[0], "DISPATCH-DONE task=t42 engine=local reached=1 verdict=GREEN" as *u8), ctr)
286 gv_check("T6 the ledger carries the local GREEN row with its tokens" as *u8, gk_contains(MG_LEDGER, "\tt42\tlocal\tANSWERED\tGREEN\t5\t50\t" as *u8), ctr)
287 gv_check("T7 without an MCP client the shift row is declared NOT-PUSHED, never silently skipped" as *u8, gk_out_has(out, ol[0], "SHIFT-ROW local status=NOT-PUSHED (no --mcp)" as *u8), ctr)
288
289 let pb: i64 = mg_fork_seat(MG_PORT_B, "no idea" as *u8)
290 mg_run(elf, "run" as *u8, MG_TKAT, MG_STATE, "--local" as *u8, "127.0.0.1:18633" as *u8, "--mcp" as *u8, "/nonexistent/nx_mcp_call.elf" as *u8, "--base" as *u8, "https://example.invalid" as *u8, "" as *u8, "" as *u8, out, ol)
291 mg_reap(pb)
292 gv_puts(out)
293 gv_check("T8 a C task the local seat fails is handed to the frontier: NEEDS-FRONTIER" as *u8, gk_out_has(out, ol[0], "ENGINE local status=ANSWERED gen_tokens=5 prompt_tokens=3 ms=50 judge=RED" as *u8) * gk_out_has(out, ol[0], "verdict=NEEDS-FRONTIER" as *u8), ctr)
294 gv_check("neg-control-a-C-task-never-reaches-the-external-tier-even-with-a-client-configured (SKIPPED-class-C, no UNREACHABLE)" as *u8, gk_out_has(out, ol[0], "ENGINE external status=SKIPPED-class-C" as *u8) * (1 - gk_out_has(out, ol[0], "external status=UNREACHABLE" as *u8)), ctr)
295 gv_check("T9 the ledger carries local RED, external SKIPPED-class-C and frontier HANDED-TO-SEAT for tkat" as *u8, gk_contains(MG_LEDGER, "\ttkat\tlocal\tANSWERED\tRED\t" as *u8) * gk_contains(MG_LEDGER, "\ttkat\texternal\tSKIPPED-class-C\t" as *u8) * gk_contains(MG_LEDGER, "\ttkat\tfrontier\tHANDED-TO-SEAT\tPENDING\t" as *u8), ctr)
296
297 // ES16: an unclassed task is refused by name before any engine is touched -- it never defaults onto the seat
298 let nc_before: i64 = gk_count(MG_LEDGER, "\n" as *u8)
299 mg_run(elf, "run" as *u8, MG_TNOCLASS, MG_STATE, "--local" as *u8, "127.0.0.1:1" as *u8, "--mcp" as *u8, "/nonexistent/nx_mcp_call.elf" as *u8, "--base" as *u8, "https://example.invalid" as *u8, "" as *u8, "" as *u8, out, ol)
300 let nc_after: i64 = gk_count(MG_LEDGER, "\n" as *u8)
301 gv_check("neg-control-an-unclassed-task-is-REFUSED-by-name-never-defaulted (tnc has neither class nor tclass; USAGE, no engine, no ledger row)" as *u8, gk_out_has(out, ol[0], "refused=task has no class or tclass row" as *u8) * gk_out_has(out, ol[0], "verdict=USAGE" as *u8) * (1 - gk_out_has(out, ol[0], "ENGINE" as *u8)), ctr)
302 gv_check("T10 fixture-reached-the-condition: the refused task wrote no ledger row" as *u8, nc_before == nc_after, ctr)
303
304 let pd: i64 = mg_fork_seat(MG_PORT_D, "no idea" as *u8)
305 mg_run(elf, "run" as *u8, MG_T42, MG_STATE, "--local" as *u8, "127.0.0.1:18635" as *u8, "--no-external" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
306 mg_reap(pd)
307 gv_check("T11 a U task with the external tier disabled by flag reads SKIPPED-by-flag and NEEDS-FRONTIER" as *u8, gk_out_has(out, ol[0], "ENGINE external status=SKIPPED-by-flag" as *u8) * gk_out_has(out, ol[0], "verdict=NEEDS-FRONTIER" as *u8), ctr)
308
309 mg_run(elf, "run" as *u8, MG_T42, MG_STATE, "--local" as *u8, "127.0.0.1:1" as *u8, "--no-external" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
310 gv_check("T12 no engine reachable is UNOBSERVABLE, never GREEN and never NEEDS-FRONTIER" as *u8, gk_out_has(out, ol[0], "ENGINE local status=UNREACHABLE" as *u8) * gk_out_has(out, ol[0], "engine=none reached=0 verdict=UNOBSERVABLE" as *u8), ctr)
311
312 // a listener that is not a seat (2026-09-05): accepted, read the request, closed with no body -- NO-REPLY, never
313 // UNPARSEABLE (no bytes were parsed) and never UNREACHABLE (the connect succeeded); the seat is still handed the task
314 let pm: i64 = mg_fork_mute(MG_PORT_E)
315 var pm_ok: i64 = 1
316 if pm < 0 { pm_ok = 0 }
317 gv_need("fixture mute seat accepts on 127.0.0.1:18636" as *u8, pm_ok, ctr)
318 mg_run(elf, "run" as *u8, MG_T42, MG_STATE, "--local" as *u8, "127.0.0.1:18636" as *u8, "--no-external" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
319 mg_reap(pm)
320 gv_puts(out)
321 gv_check("T12b a socket that accepts and closes with no body reads NO-REPLY, writes its own ledger status, and hands off" as *u8, gk_out_has(out, ol[0], "ENGINE local status=NO-REPLY" as *u8) * gk_contains(MG_LEDGER, "\tt42\tlocal\tNO-REPLY\t" as *u8) * gk_out_has(out, ol[0], "verdict=NEEDS-FRONTIER" as *u8), ctr)
322 gv_check("neg-control-no-body-is-never-reported-as-UNPARSEABLE-or-UNREACHABLE" as *u8, (1 - gk_out_has(out, ol[0], "UNPARSEABLE" as *u8)) * (1 - gk_out_has(out, ol[0], "local status=UNREACHABLE" as *u8)), ctr)
323
324 let before: i64 = gk_count(MG_LEDGER, "\n" as *u8)
325 mg_run(elf, "run" as *u8, MG_T42, MG_STATE, "--local" as *u8, "127.0.0.1:1" as *u8, "--dry" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
326 let after: i64 = gk_count(MG_LEDGER, "\n" as *u8)
327 gv_check("T13 --dry names what it would try, forks nothing and writes no ledger row" as *u8, gk_out_has(out, ol[0], "ENGINE local status=WOULD-TRY" as *u8) * gk_out_has(out, ol[0], "ENGINE external status=WOULD-TRY" as *u8) * gk_out_has(out, ol[0], "verdict=DRY" as *u8), ctr)
328 gv_check("T14 fixture-reached-the-condition: the ledger row count is unchanged by --dry" as *u8, before == after, ctr)
329
330 let pe: i64 = mg_fork_seat(MG_PORT_A, "The answer is 42" as *u8)
331 mg_run(elf, "run" as *u8, MG_T42, MG_STATE, "--local" as *u8, "127.0.0.1:18632" as *u8, "--no-external" as *u8, "--mcp" as *u8, "/nonexistent/nx_mcp_call.elf" as *u8, "--base" as *u8, "https://example.invalid" as *u8, "" as *u8, out, ol)
332 mg_reap(pe)
333 gv_check("T15 a failed scoreboard push never changes a GREEN verdict, and says so" as *u8, gk_out_has(out, ol[0], "SHIFT-ROW local status=PUSH-FAILED" as *u8) * gk_out_has(out, ol[0], "verdict=GREEN" as *u8), ctr)
334
335 // ---- ES16: class rows as data ----
336 let pc: i64 = mg_fork_seat(MG_PORT_C, "The answer is 42" as *u8)
337 var pc_ok: i64 = 1
338 if pc < 0 { pc_ok = 0 }
339 gv_need("fixture seat C accepts on 127.0.0.1:18634" as *u8, pc_ok, ctr)
340 mg_run(elf, "run" as *u8, MG_TCLS, MG_STATE, "--local" as *u8, "127.0.0.1:18634" as *u8, "--classes" as *u8, MG_CLASSES, "--no-external" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
341 mg_reap(pc)
342 gv_puts(out)
343 gv_check("T16 tclass resolves clearance and engine order from the class row and the row's ceiling bounds the task's max_new (64 asked, 8 granted)" as *u8, gk_out_has(out, ol[0], "DISPATCH task=tcls class=U tclass=u-local-ext order=local,external,frontier judge=contains max_new=8" as *u8) * gk_out_has(out, ol[0], "DISPATCH-DONE task=tcls engine=local reached=1 verdict=GREEN" as *u8), ctr)
344
345 let fr_before: i64 = gk_count(MG_LEDGER, "\n" as *u8)
346 mg_run(elf, "run" as *u8, MG_TFRONT, MG_STATE, "--local" as *u8, "127.0.0.1:1" as *u8, "--classes" as *u8, MG_CLASSES, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
347 let fr_after: i64 = gk_count(MG_LEDGER, "\n" as *u8)
348 gv_puts(out)
349 gv_check("T17 a class that admits nothing below the seat hands off by name without touching any engine: both tiers SKIPPED-by-class-order, NEEDS-FRONTIER, reached=0" as *u8, gk_out_has(out, ol[0], "ENGINE local status=SKIPPED-by-class-order" as *u8) * gk_out_has(out, ol[0], "ENGINE external status=SKIPPED-by-class-order" as *u8) * gk_out_has(out, ol[0], "reached=0 skipped_by_class_order=2 verdict=NEEDS-FRONTIER" as *u8) * (1 - gk_out_has(out, ol[0], "UNREACHABLE" as *u8)), ctr)
350 gv_check("T18 fixture-reached-the-condition: the hand-off wrote local, external and frontier ledger rows (3)" as *u8, fr_after - fr_before == 3, ctr)
351
352 mg_run(elf, "run" as *u8, MG_TNOLOC, MG_STATE, "--local" as *u8, "127.0.0.1:1" as *u8, "--classes" as *u8, MG_CLASSES, "--no-external" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
353 gv_check("T19 a class row without local skips the local seat by class order (never UNREACHABLE) and still hands off" as *u8, gk_out_has(out, ol[0], "ENGINE local status=SKIPPED-by-class-order" as *u8) * (1 - gk_out_has(out, ol[0], "local status=UNREACHABLE" as *u8)) * gk_out_has(out, ol[0], "verdict=NEEDS-FRONTIER" as *u8), ctr)
354
355 let bc_before: i64 = gk_count(MG_LEDGER, "\n" as *u8)
356 mg_run(elf, "run" as *u8, MG_TBADCLS, MG_STATE, "--local" as *u8, "127.0.0.1:1" as *u8, "--classes" as *u8, MG_CLASSES, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
357 let bc_after: i64 = gk_count(MG_LEDGER, "\n" as *u8)
358 gv_check("neg-control-a-C-class-row-that-lists-external-is-refused-AT-LOAD-naming-the-rule (no engine, no ledger row)" as *u8, gk_out_has(out, ol[0], "refused=class row c-lists-ext is C and lists external" as *u8) * gk_out_has(out, ol[0], "verdict=USAGE" as *u8) * (1 - gk_out_has(out, ol[0], "ENGINE" as *u8)), ctr)
359 gv_check("T20 fixture-reached-the-condition: the refused class wrote no ledger row" as *u8, bc_before == bc_after, ctr)
360
361 mg_run(elf, "run" as *u8, MG_TMISSING, MG_STATE, "--local" as *u8, "127.0.0.1:1" as *u8, "--classes" as *u8, MG_CLASSES, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
362 gv_check("neg-control-a-tclass-with-no-row-is-refused-naming-the-class-and-the-conf" as *u8, gk_out_has(out, ol[0], "refused=no class row for nonesuch in /tmp/nx_engine_dispatch_gate/classes.conf" as *u8) * gk_out_has(out, ol[0], "verdict=USAGE" as *u8), ctr)
363
364 mg_run(elf, "run" as *u8, MG_TCLS, MG_STATE, "--local" as *u8, "127.0.0.1:1" as *u8, "--classes" as *u8, "/nonexistent/classes.conf" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
365 gv_check("neg-control-an-unreadable-classes-conf-is-refused-by-path-never-defaulted" as *u8, gk_out_has(out, ol[0], "refused=classes conf unreadable: /nonexistent/classes.conf" as *u8) * gk_out_has(out, ol[0], "verdict=USAGE" as *u8), ctr)
366
367 // ---- the ledger partition, bound to its denominator ----
368 let rows: i64 = gk_count(MG_LEDGER, "\n" as *u8)
369 gv_puts(" ledger rows=" as *u8); gv_num(rows); gv_puts(" (t42 GREEN 1 + tkat 3 + tnc 0 + t42-noext 2 + unreachable 1 + push-failed 1 + tcls 1 + tfront 3 + tnoloc 2 + mute 2 = 16)\n" as *u8)
370 gv_check("T21 the ledger holds exactly the rows the teeth above produced" as *u8, rows == MG_LEDGER_ROWS, ctr)
371 mg_run(elf, "bogus" as *u8, MG_T42, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
372 gv_check("T22 an unknown verb prints usage and no DISPATCH line" as *u8, gk_out_has(out, ol[0], "usage: nx_engine_dispatch" as *u8) * (1 - gk_out_has(out, ol[0], "DISPATCH" as *u8)), ctr)
373
374 let rc: i64 = gv_verdict("NX-ENGINE-DISPATCH-GATE" as *u8, ctr, "ES3 ed_dispatch + ES16 ec_route_class: the declared judge decides, the class row decides clearance, order and ceiling, C never leaves the estate, an unclassed task is refused by name, an unreachable ladder abstains, nothing-passed is handed to the seat by name, dry writes nothing, a failed push cannot change a verdict" as *u8)
375 sys_exit(rc)
376 return rc
377}