code wiki / (root) / nx_engine_dispatch.nx

nx_engine_dispatch.nx source

↩ module page · 768 lines · 43023 B

1// nx_engine_dispatch.nx -- ES3 of /compare/engineshift: THE DISPATCHER. ONE verb sends a task to the cheapest 2// engine whose terms permit its class, stops at the first engine whose output the task's declared judge passes, 3// returns the verdict with the engine named, and appends a shift-ledger row so the engine-shift scoreboard's 4// local column can move (ES0 nx_seat shift; ES1 nx_seat_meter_beat writes the Claude column, this organ writes 5// the local one). MEASURED 2026-09-02 before building: three routers existed and none was wired 6// (nx_llm_provider 0 production importers, llm_routes.conf 0 active rows, nx_clawf_route registered-dark), and 7// the local column had read ZERO since 2026-07-20 because nothing ever wrote it. 8// 9// THE LADDER (the operator's order: sovereign first, rent the frontier seat only for what only it can do): 10// 1 LOCAL the sovereign no-float seat (nx_nofloat_serve, POST /gen on --local, default 127.0.0.1:8032); 11// any class -- the bytes never leave the machine 12// 2 EXTERNAL a free external worker through the estate's warden (nx_extllm_call over the sovereign MCP client); 13// class U ONLY -- a C task is SKIPPED BY NAME here and the warden re-checks at the door (extllm XL2) 14// 3 FRONTIER the rented seat. NOT an engine this organ can call: it exits NEEDS-FRONTIER with the task summary 15// and the seat that reads it decides. Escalation is on a RED judge, never on a budget. 16// THE JUDGE is declared in the task, never chosen here: `judge|kat|<expected-file>` (byte-exact after trimming 17// outer whitespace) or `judge|contains|<literal>`. An engine's answer that the judge cannot read is UNOBSERVABLE, 18// never GREEN and never RED. 19// ES16 (operator 2026-09-02, "minimise token use autonomously without my engagement"): WHICH engines a task may 20// try, its clearance and its token ceiling are DATA -- knowledge/engine_classes.conf rows 21// `class|<name>|<U or C>|<engine order>|<max_new ceiling>`, named by the task's `tclass|<name>` row. A task with 22// neither tclass nor class is REFUSED by name; it never defaults its way onto the frontier seat. A C row that 23// lists external is refused at load, before any task runs. 24// 25// nx_engine_dispatch run <task.conf> <state-dir> [--local a.b.c.d:port] [--no-external] [--dry] 26// [--mcp <nx_mcp_call elf> --base <url> --cap <seat cap> --extcap <extllm cap>] 27// [--keyfile <path outside the estate>] [--ledger <estate egress ledger>] 28// [--classes <engine_classes.conf>] 29// nx_engine_dispatch judge <candidate-file> <task.conf> 30// task.conf rows (pipe-separated, # comments): 31// name|<id> tclass|<class row name> or class|U or C prompt|<path> 32// judge|kat|<expected-file> or judge|contains|<literal> 33// max_new|N (default 24; the class row's ceiling and the seat's 96 cap both bound it) subsystem|<name for the egress ledger> 34// Ledger: <state-dir>/engine_dispatch.jrnl, one row per engine attempt: epoch TAB task TAB engine TAB status TAB 35// verdict TAB gen_tokens TAB ms TAB note. Shift row on a LOCAL GREEN when --mcp is given: 36// nx_seat meter maker-local-dispatch 1 <gen_tokens> <prompt_tokens> 0 dispatch-<task>-engine=local-src=nx_engine_dispatch 37// Exit: 0 GREEN (an engine passed) | 2 usage | 3 UNOBSERVABLE (no engine could be reached) | 38// 4 NEEDS-FRONTIER (every engine below the seat was reached or excluded by its class row and none passed). verdict= is the last token. 39// license_tier: ORIGINAL. Writes only the ledger under <state-dir>. No hw writes (Rule 26). 40import "nx_syscalls.nx" 41import "nx_memplane_lib.nx" 42import "nx_tool_run.nx" 43import "nx_http_client.nx" 44import "nx_http_response_parse.nx" 45 46const ED_EXIT_GREEN: i64 = 0 47const ED_EXIT_USAGE: i64 = 2 48const ED_EXIT_UNOBS: i64 = 3 49const ED_EXIT_FRONTIER: i64 = 4 50const ED_CONF_CAP: i64 = 65536 51const ED_PATH: i64 = 4096 52const ED_PROMPT_CAP: i64 = 16384 // the seat's own request window is 16 KB 53const ED_BODY_CAP: i64 = 65536 54const ED_REQ_CAP: i64 = 131072 55const ED_RESP_CAP: i64 = 1048576 56const ED_R_NOREPLY: i64 = 0 - 3 // ed_local_gen: the socket ACCEPTED, the request was WRITTEN, and zero bytes came back -- a listener that is not a seat. Measured 2026-09-05 on the NAS: 127.0.0.1:8032 accepted and closed, and the old code printed ANSWERED-UNPARSEABLE http=0, a status word that named the wrong stage and sent the reader at the parser 57const ED_TEXT_CAP: i64 = 262144 58const ED_OUT: i64 = 65536 59const ED_ROW: i64 = 1024 60const ED_FRAG_CAP: i64 = 4096 61const ED_ARGV_MAX: i64 = 16 62const ED_I64: i64 = 8 63const ED_MAX_NEW_DEFAULT: i64 = 24 64const ED_MAX_NEW_SEAT_CAP: i64 = 96 // NSV_MAXNEW in nx_nofloat_serve_core 65const ED_LOCAL_TIMEOUT_MS: i64 = 180000 // 96 tokens at the NAS CPU's 1 tok/s is 96 s; 180 s covers a loaded box 66const ED_FORK_TIMEOUT_MS: i64 = 300000 // an external call outran the edge window once at 282 s 67const ED_DEFAULT_A: i64 = 127 68const ED_DEFAULT_B: i64 = 0 69const ED_DEFAULT_C: i64 = 0 70const ED_DEFAULT_D: i64 = 1 71const ED_DEFAULT_PORT: i64 = 8032 // nx_nofloat_serve NSD_PORT 72const ED_NL: i64 = 10 73const ED_QUOTE: i64 = 34 74const ED_BSLASH: i64 = 92 75const ED_PIPE: i64 = 124 76const ED_HASH: i64 = 35 77const ED_SPACE: i64 = 32 78const ED_TAB: i64 = 9 79const ED_CR: i64 = 13 80const ED_HTTP_OK: i64 = 200 81const ED_MODE_RW: i64 = 420 82const ED_CLASS_U: i64 = 0 83const ED_CLASS_C: i64 = 1 84const ED_CLASS_UNSET: i64 = 2 // ES16: neither tclass nor class declared -- a refusal, never a default 85const ED_JUDGE_NONE: i64 = 0 86const ED_JUDGE_KAT: i64 = 1 87const ED_JUDGE_CONTAINS: i64 = 2 88const ED_V_GREEN: i64 = 0 89const ED_V_RED: i64 = 1 90const ED_V_UNOBS: i64 = 3 91const ED_ENGINE_LOCAL: i64 = 1 92const ED_ENGINE_EXTERNAL: i64 = 2 93const ED_ENGINE_FRONTIER: i64 = 3 94// task table slots (offsets into a *u8 arena of ED_PATH cells) 95const ED_T_NAME: i64 = 0 96const ED_T_CLASS: i64 = 1 97const ED_T_PROMPT: i64 = 2 98const ED_T_JUDGE_ARG: i64 = 3 99const ED_T_SUBSYS: i64 = 4 100const ED_T_TCLASS: i64 = 5 101const ED_T_CELLS: i64 = 6 102// ES16 class rows: field count and the engine-order slots 103const ED_CF_FIELDS: i64 = 5 104const ED_ORD_LOCAL: i64 = 0 105const ED_ORD_EXTERNAL: i64 = 1 106const ED_ORD_CELLS: i64 = 2 107 108// ---- tiny helpers ----------------------------------------------------------------------------------------- 109func ed_cell(base: *u8, k: i64) -> *u8 { return ((base as i64) + k * ED_PATH) as *u8 } 110func ed_fld(base: *u8, k: i64) -> *u8 { return ((base as i64) + k * ED_ROW) as *u8 } 111func ed_find(buf: *u8, s: i64, e: i64, lit: *u8) -> i64 { 112 let ll: i64 = mp_len(lit) 113 if ll == 0 { return 0 - 1 } 114 var i: i64 = s 115 while i + ll <= e { 116 var j: i64 = 0 117 var same: i64 = 1 118 var scan: i64 = 1 119 while scan == 1 { if j >= ll { scan = 0 } else { if buf[i + j] != lit[j] { same = 0; scan = 0 } else { j = j + 1 } } } 120 if same == 1 { return i } 121 i = i + 1 122 } 123 return 0 - 1 124} 125func ed_atoi(s: *u8) -> i64 { 126 var v: i64 = 0 127 var i: i64 = 0 128 while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } } i = i + 1 } 129 return v 130} 131func ed_is_ws(c: i64) -> i64 { if c == ED_SPACE { return 1 } if c == ED_NL { return 1 } if c == ED_CR { return 1 } if c == ED_TAB { return 1 } return 0 } 132// copy buf[s..e) trimmed of outer whitespace into dst (NUL-terminated); returns the trimmed length 133func ed_trim_into(buf: *u8, s: i64, e: i64, dst: *u8, cap: i64) -> i64 { 134 var a: i64 = s 135 var b: i64 = e 136 var go: i64 = 1 137 while go == 1 { if a >= b { go = 0 } else { if ed_is_ws(buf[a] as i64) == 1 { a = a + 1 } else { go = 0 } } } 138 go = 1 139 while go == 1 { if b <= a { go = 0 } else { if ed_is_ws(buf[b - 1] as i64) == 1 { b = b - 1 } else { go = 0 } } } 140 var n: i64 = b - a 141 if n > cap - 1 { n = cap - 1 } 142 var i: i64 = 0 143 while i < n { dst[i] = buf[a + i]; i = i + 1 } 144 dst[n] = 0 as u8 145 return n 146} 147func ed_read(path: *u8, lenp: *i64) -> *u8 { return sys_read_file(path, lenp) } 148 149// ---- ES16: engine class rows -------------------------------------------------------------------------------- 150// class|<name>|<U or C>|<engine order: comma list of local,external,frontier>|<max_new ceiling> 151// The row decides clearance, which engines may be tried and how many tokens; the task only names its class. 152// A C row that lists external is refused at load: a C task never reaches a third-party sink, and a conf that 153// says otherwise is wrong before any task runs. Every refusal names the row and the rule. 154func ec_route_class(path: *u8, name: *u8, cls: *i64, mx: *i64, ord: *i64, why: *u8) -> i64 { 155 let lp: *i64 = sys_mmap(ED_I64) as *i64 156 let buf: *u8 = ed_read(path, lp) 157 if (buf as i64) == 0 { var w0: i64 = mp_cat(why, 0, "classes conf unreadable: " as *u8); mp_cat(why, w0, path); return 0 - 1 } 158 let n: i64 = lp[0] 159 let f: *u8 = sys_mmap(ED_CF_FIELDS * ED_ROW) 160 var found: i64 = 0 161 var i: i64 = 0 162 while i < n { 163 var e: i64 = i 164 var scan: i64 = 1 165 while scan == 1 { if e >= n { scan = 0 } else { if (buf[e] as i64) == ED_NL { scan = 0 } else { e = e + 1 } } } 166 if found == 0 { if e > i { if (buf[i] as i64) != ED_HASH { 167 var fi: i64 = 0 168 var s: i64 = i 169 var p: i64 = i 170 var go: i64 = 1 171 while go == 1 { 172 var cut: i64 = 0 173 if p >= e { cut = 1 } else { if (buf[p] as i64) == ED_PIPE { cut = 1 } } 174 if cut == 1 { 175 if fi < ED_CF_FIELDS { ed_trim_into(buf, s, p, ed_fld(f, fi), ED_ROW); fi = fi + 1 } 176 s = p + 1 177 if p >= e { go = 0 } 178 } 179 p = p + 1 180 } 181 if fi >= ED_CF_FIELDS { if mp_streq(ed_fld(f, 0), "class" as *u8) == 1 { if mp_streq(ed_fld(f, 1), name) == 1 { 182 found = 1 183 let cl: *u8 = ed_fld(f, 2) 184 if mp_streq(cl, "U" as *u8) == 1 { cls[0] = ED_CLASS_U } else { if mp_streq(cl, "C" as *u8) == 1 { cls[0] = ED_CLASS_C } else { 185 var w1: i64 = mp_cat(why, 0, "class row " as *u8); w1 = mp_cat(why, w1, name); mp_cat(why, w1, " clearance must be U or C" as *u8); return 0 - 1 } } 186 let eng: *u8 = ed_fld(f, 3) 187 let el: i64 = mp_len(eng) 188 ord[ED_ORD_LOCAL] = 0 189 ord[ED_ORD_EXTERNAL] = 0 190 if ed_find(eng, 0, el, "local" as *u8) >= 0 { ord[ED_ORD_LOCAL] = 1 } 191 if ed_find(eng, 0, el, "external" as *u8) >= 0 { ord[ED_ORD_EXTERNAL] = 1 } 192 if cls[0] == ED_CLASS_C { if ord[ED_ORD_EXTERNAL] == 1 { 193 var w2: i64 = mp_cat(why, 0, "class row " as *u8); w2 = mp_cat(why, w2, name); mp_cat(why, w2, " is C and lists external (a C task never reaches a third-party sink; refused at load)" as *u8); return 0 - 1 } } 194 mx[0] = ed_atoi(ed_fld(f, 4)) 195 } } } 196 } } } 197 i = e + 1 198 } 199 if found == 0 { var w3: i64 = mp_cat(why, 0, "no class row for " as *u8); w3 = mp_cat(why, w3, name); w3 = mp_cat(why, w3, " in " as *u8); mp_cat(why, w3, path); return 0 - 1 } 200 return 0 201} 202 203// ---- task conf -------------------------------------------------------------------------------------------- 204// fills cells NAME CLASS PROMPT JUDGE_ARG SUBSYS TCLASS; returns judge kind, class in cls[0], max_new in mx[0], the 205// engine order in ord[ED_ORD_LOCAL..ED_ORD_EXTERNAL] (1 = may be tried); -1 on a bad conf. ES16: a task names its 206// class with tclass|<name>, resolved from the classes conf; a task with neither tclass nor class is REFUSED by name. 207func ed_load_task(path: *u8, cells: *u8, cls: *i64, mx: *i64, ord: *i64, classes: *u8, why: *u8) -> i64 { 208 let lp: *i64 = sys_mmap(ED_I64) as *i64 209 let buf: *u8 = ed_read(path, lp) 210 let n: i64 = lp[0] 211 if (buf as i64) == 0 { mp_cat(why, 0, "task conf unreadable" as *u8); return 0 - 1 } 212 var judge: i64 = ED_JUDGE_NONE 213 cls[0] = ED_CLASS_UNSET 214 mx[0] = 0 // 0 = the task did not say; the class row's ceiling or the default applies 215 ord[ED_ORD_LOCAL] = 1 216 ord[ED_ORD_EXTERNAL] = 1 217 var k: i64 = 0 218 while k < ED_T_CELLS { let ck: *u8 = ed_cell(cells, k); ck[0] = 0 as u8; k = k + 1 } 219 var i: i64 = 0 220 while i < n { 221 var e: i64 = i 222 var scan: i64 = 1 223 while scan == 1 { if e >= n { scan = 0 } else { if (buf[e] as i64) == ED_NL { scan = 0 } else { e = e + 1 } } } 224 if e > i { if (buf[i] as i64) != ED_HASH { 225 // key|value[|value2] 226 var p: i64 = i 227 var s2: i64 = 1 228 while s2 == 1 { if p >= e { s2 = 0 } else { if (buf[p] as i64) == ED_PIPE { s2 = 0 } else { p = p + 1 } } } 229 if p < e { 230 let key: *u8 = sys_mmap(ED_ROW) 231 ed_trim_into(buf, i, p, key, ED_ROW) 232 var v2: i64 = p + 1 233 var s3: i64 = 1 234 while s3 == 1 { if v2 >= e { s3 = 0 } else { if (buf[v2] as i64) == ED_PIPE { s3 = 0 } else { v2 = v2 + 1 } } } 235 let val: *u8 = sys_mmap(ED_PATH) 236 ed_trim_into(buf, p + 1, v2, val, ED_PATH) 237 if mp_streq(key, "name" as *u8) == 1 { mp_cat(ed_cell(cells, ED_T_NAME), 0, val) } 238 if mp_streq(key, "prompt" as *u8) == 1 { mp_cat(ed_cell(cells, ED_T_PROMPT), 0, val) } 239 if mp_streq(key, "subsystem" as *u8) == 1 { mp_cat(ed_cell(cells, ED_T_SUBSYS), 0, val) } 240 if mp_streq(key, "tclass" as *u8) == 1 { mp_cat(ed_cell(cells, ED_T_TCLASS), 0, val) } 241 if mp_streq(key, "max_new" as *u8) == 1 { mx[0] = ed_atoi(val) } 242 if mp_streq(key, "class" as *u8) == 1 { 243 if mp_streq(val, "U" as *u8) == 1 { cls[0] = ED_CLASS_U } else { if mp_streq(val, "C" as *u8) == 1 { cls[0] = ED_CLASS_C } else { mp_cat(why, 0, "class must be U or C" as *u8); return 0 - 1 } } 244 } 245 if mp_streq(key, "judge" as *u8) == 1 { 246 if v2 >= e { mp_cat(why, 0, "judge row needs kind and argument" as *u8); return 0 - 1 } 247 ed_trim_into(buf, v2 + 1, e, ed_cell(cells, ED_T_JUDGE_ARG), ED_PATH) 248 if mp_streq(val, "kat" as *u8) == 1 { judge = ED_JUDGE_KAT } else { if mp_streq(val, "contains" as *u8) == 1 { judge = ED_JUDGE_CONTAINS } else { mp_cat(why, 0, "judge kind must be kat or contains" as *u8); return 0 - 1 } } 249 } 250 sys_munmap(key, ED_ROW) 251 sys_munmap(val, ED_PATH) 252 } 253 } } 254 i = e + 1 255 } 256 let cname: *u8 = ed_cell(cells, ED_T_NAME) 257 let cprompt: *u8 = ed_cell(cells, ED_T_PROMPT) 258 let csub: *u8 = ed_cell(cells, ED_T_SUBSYS) 259 if cname[0] == (0 as u8) { mp_cat(why, 0, "task has no name row" as *u8); return 0 - 1 } 260 if cprompt[0] == (0 as u8) { mp_cat(why, 0, "task has no prompt row" as *u8); return 0 - 1 } 261 if judge == ED_JUDGE_NONE { mp_cat(why, 0, "task has no judge row" as *u8); return 0 - 1 } 262 let ctc: *u8 = ed_cell(cells, ED_T_TCLASS) 263 if ctc[0] != (0 as u8) { 264 let cmx: *i64 = sys_mmap(ED_I64) as *i64 265 cmx[0] = 0 266 if ec_route_class(classes, ctc, cls, cmx, ord, why) < 0 { return 0 - 1 } 267 // the class row's max_new is a CEILING over the task's own request 268 if cmx[0] > 0 { if mx[0] < 1 { mx[0] = cmx[0] } else { if mx[0] > cmx[0] { mx[0] = cmx[0] } } } 269 } else { 270 if cls[0] == ED_CLASS_UNSET { mp_cat(why, 0, "task has no class or tclass row (ES16: an unclassed task is refused by name, never defaulted onto the frontier seat)" as *u8); return 0 - 1 } 271 } 272 if mx[0] < 1 { mx[0] = ED_MAX_NEW_DEFAULT } 273 if mx[0] > ED_MAX_NEW_SEAT_CAP { mx[0] = ED_MAX_NEW_SEAT_CAP } 274 if csub[0] == (0 as u8) { mp_cat(csub, 0, "dispatch" as *u8) } 275 return judge 276} 277 278// ---- THE JUDGE: declared in the task, applied to a candidate text. GREEN / RED / UNOBSERVABLE ---- 279func ed_judge_contains(cand: *u8, cn: i64, lit: *u8) -> i64 { 280 if ed_find(cand, 0, cn, lit) >= 0 { return ED_V_GREEN } 281 return ED_V_RED 282} 283func ed_judge_kat(cand: *u8, cn: i64, expected_path: *u8) -> i64 { 284 let lp: *i64 = sys_mmap(ED_I64) as *i64 285 let ex: *u8 = ed_read(expected_path, lp) 286 if (ex as i64) == 0 { return ED_V_UNOBS } 287 let a: *u8 = sys_mmap(ED_TEXT_CAP) 288 let b: *u8 = sys_mmap(ED_TEXT_CAP) 289 let an: i64 = ed_trim_into(cand, 0, cn, a, ED_TEXT_CAP) 290 let bn: i64 = ed_trim_into(ex, 0, lp[0], b, ED_TEXT_CAP) 291 var v: i64 = ED_V_GREEN 292 if an != bn { v = ED_V_RED } else { var i: i64 = 0; while i < an { if a[i] != b[i] { v = ED_V_RED } i = i + 1 } } 293 sys_munmap(a, ED_TEXT_CAP) 294 sys_munmap(b, ED_TEXT_CAP) 295 return v 296} 297func ed_judge(kind: i64, arg: *u8, cand: *u8, cn: i64) -> i64 { 298 if kind == ED_JUDGE_KAT { return ed_judge_kat(cand, cn, arg) } 299 if kind == ED_JUDGE_CONTAINS { return ed_judge_contains(cand, cn, arg) } 300 return ED_V_UNOBS 301} 302func ed_vname(v: i64) -> *u8 { 303 if v == ED_V_GREEN { return "GREEN" as *u8 } 304 if v == ED_V_RED { return "RED" as *u8 } 305 return "UNOBSERVABLE" as *u8 306} 307 308// ---- JSON: escape a prompt into a body string, unescape a "text" value out of the seat's reply ---- 309func ed_json_escape(src: *u8, n: i64, dst: *u8, off: i64, cap: i64) -> i64 { 310 var o: i64 = off 311 var i: i64 = 0 312 while i < n { 313 if o >= cap - 8 { return o } 314 let c: i64 = src[i] as i64 315 if c == ED_QUOTE { dst[o] = ED_BSLASH as u8; dst[o + 1] = ED_QUOTE as u8; o = o + 2 } else { 316 if c == ED_BSLASH { dst[o] = ED_BSLASH as u8; dst[o + 1] = ED_BSLASH as u8; o = o + 2 } else { 317 if c == ED_NL { dst[o] = ED_BSLASH as u8; dst[o + 1] = 110 as u8; o = o + 2 } else { 318 if c == ED_CR { dst[o] = ED_BSLASH as u8; dst[o + 1] = 114 as u8; o = o + 2 } else { 319 if c == ED_TAB { dst[o] = ED_BSLASH as u8; dst[o + 1] = 116 as u8; o = o + 2 } else { 320 if c < 32 { dst[o] = ED_SPACE as u8; o = o + 1 } else { dst[o] = c as u8; o = o + 1 } } } } } } 321 i = i + 1 322 } 323 return o 324} 325func ed_hexval(c: i64) -> i64 { 326 if c >= 48 { if c <= 57 { return c - 48 } } 327 if c >= 97 { if c <= 102 { return c - 87 } } 328 if c >= 65 { if c <= 70 { return c - 55 } } 329 return 0 - 1 330} 331// find "<key>":" in json[s..e) and unescape its string value into dst; returns value length or -1 332func ed_json_str(json: *u8, s: i64, e: i64, key: *u8, dst: *u8, cap: i64) -> i64 { 333 let k: *u8 = sys_mmap(ED_ROW) 334 var ko: i64 = mp_cat(k, 0, "\"" as *u8) 335 ko = mp_cat(k, ko, key) 336 ko = mp_cat(k, ko, "\":\"" as *u8) 337 k[ko] = 0 as u8 338 let p: i64 = ed_find(json, s, e, k) 339 sys_munmap(k, ED_ROW) 340 if p < 0 { return 0 - 1 } 341 var i: i64 = p + ko 342 var o: i64 = 0 343 var go: i64 = 1 344 while go == 1 { 345 if i >= e { go = 0 } else { if o >= cap - 2 { go = 0 } else { 346 let c: i64 = json[i] as i64 347 if c == ED_QUOTE { go = 0 } else { 348 if c == ED_BSLASH { 349 if i + 1 < e { 350 let d: i64 = json[i + 1] as i64 351 if d == 110 { dst[o] = ED_NL as u8; o = o + 1; i = i + 2 } else { 352 if d == 116 { dst[o] = ED_TAB as u8; o = o + 1; i = i + 2 } else { 353 if d == 114 { dst[o] = ED_CR as u8; o = o + 1; i = i + 2 } else { 354 if d == 117 { 355 // \uXXXX: decode when it fits a byte, else a question mark -- never dropped silently 356 var v: i64 = 0 357 var ok: i64 = 1 358 var h: i64 = 0 359 while h < 4 { if i + 2 + h < e { let hv: i64 = ed_hexval(json[i + 2 + h] as i64); if hv < 0 { ok = 0 } else { v = v * 16 + hv } } else { ok = 0 } h = h + 1 } 360 if ok == 1 { if v < 128 { dst[o] = v as u8 } else { dst[o] = 63 as u8 } o = o + 1; i = i + 6 } else { dst[o] = 63 as u8; o = o + 1; i = i + 2 } 361 } else { dst[o] = d as u8; o = o + 1; i = i + 2 } } } } 362 } else { i = i + 1 } 363 } else { dst[o] = c as u8; o = o + 1; i = i + 1 } 364 } 365 } } 366 } 367 dst[o] = 0 as u8 368 return o 369} 370func ed_json_int(json: *u8, s: i64, e: i64, key: *u8) -> i64 { 371 let k: *u8 = sys_mmap(ED_ROW) 372 var ko: i64 = mp_cat(k, 0, "\"" as *u8) 373 ko = mp_cat(k, ko, key) 374 ko = mp_cat(k, ko, "\":" as *u8) 375 k[ko] = 0 as u8 376 let p: i64 = ed_find(json, s, e, k) 377 sys_munmap(k, ED_ROW) 378 if p < 0 { return 0 - 1 } 379 var i: i64 = p + ko 380 var v: i64 = 0 381 var digits: i64 = 0 382 var go: i64 = 1 383 while go == 1 { if i >= e { go = 0 } else { let c: i64 = json[i] as i64; if c < 48 { go = 0 } else { if c > 57 { go = 0 } else { v = v * 10 + (c - 48); digits = digits + 1; i = i + 1 } } } } 384 if digits == 0 { return 0 - 1 } 385 return v 386} 387 388// ---- ENGINE 1: the sovereign no-float seat over loopback HTTP. Fills text/gen/prompt tokens/ms; returns 389// 0 answered, -1 unreachable, -2 answered but not 200 or unparseable ---- 390func ed_local_gen(a: i64, b: i64, c: i64, d: i64, port: i64, prompt: *u8, pn: i64, max_new: i64, text: *u8, meta: *i64) -> i64 { 391 let body: *u8 = sys_mmap(ED_BODY_CAP) 392 var o: i64 = mp_cat(body, 0, "{\"prompt\":\"" as *u8) 393 o = ed_json_escape(prompt, pn, body, o, ED_BODY_CAP) 394 o = mp_cat(body, o, "\",\"max_new\":" as *u8) 395 o = mp_catn(body, o, max_new) 396 o = mp_cat(body, o, ",\"mode\":\"i8\"}" as *u8) 397 let req: *u8 = sys_mmap(ED_REQ_CAP) 398 let rn: i64 = nx_http_client_build_request_post("/gen" as *u8, 4, "127.0.0.1" as *u8, 9, "application/json" as *u8, 16, body, o, req) 399 let addr: *u8 = sys_mmap(16) 400 nx_http_client_sockaddr_ipv4(addr, a, b, c, d, port) 401 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0) 402 if fd < 0 { return 0 - 1 } 403 if sys_connect(fd, addr, 16) < 0 { sys_close(fd); return 0 - 1 } 404 if sys_write(fd, req, rn) != rn { sys_close(fd); return 0 - 1 } 405 let resp: *u8 = sys_mmap(ED_RESP_CAP) 406 var got: i64 = 0 407 var keep: i64 = 1 408 while keep == 1 { 409 if got >= ED_RESP_CAP - 1 { keep = 0 } else { 410 let r: i64 = sys_read(fd, ((resp as i64) + got) as *u8, ED_RESP_CAP - 1 - got) 411 if r <= 0 { keep = 0 } else { got = got + r } 412 } 413 } 414 sys_close(fd) 415 if got <= 0 { return ED_R_NOREPLY } 416 let pr: *i64 = nx_http_resp_alloc() 417 let pv: i64 = nx_http_response_parse(resp, got, pr) 418 if pv != NX_HTTP_RESP_OK { return 0 - 2 } 419 if pr[1] != ED_HTTP_OK { meta[3] = pr[1]; return 0 - 2 } 420 let bo: i64 = pr[6] 421 var bl: i64 = pr[7] 422 if bl < 0 { bl = got - bo } 423 let tn: i64 = ed_json_str(resp, bo, bo + bl, "text" as *u8, text, ED_TEXT_CAP) 424 if tn < 0 { return 0 - 2 } 425 meta[0] = ed_json_int(resp, bo, bo + bl, "gen_tokens" as *u8) 426 meta[1] = ed_json_int(resp, bo, bo + bl, "prompt_tokens" as *u8) 427 meta[2] = ed_json_int(resp, bo, bo + bl, "ms_total" as *u8) 428 meta[3] = ED_HTTP_OK 429 return 0 430} 431 432// ---- ENGINE 2: the external worker through the warden, via the sovereign MCP client. The class check here is 433// the dispatcher's own rule; the warden repeats it at the door. Returns 0 answered, -1 unreachable, -2 refused ---- 434func ed_external_gen(mcp: *u8, base: *u8, extcap: *u8, prompt_path: *u8, keyfile: *u8, subsys: *u8, ledger: *u8, text: *u8) -> i64 { 435 let frag: *u8 = sys_mmap(ED_FRAG_CAP) 436 var o: i64 = mp_cat(frag, 0, "\"argv\":[\"call\",\"openrouter\",\"stealth/ox-alpha\",\"stealth/ox-alpha\",\"" as *u8) 437 o = mp_cat(frag, o, prompt_path) 438 o = mp_cat(frag, o, "\",\"" as *u8) 439 o = mp_cat(frag, o, keyfile) 440 o = mp_cat(frag, o, "\",\"" as *u8) 441 o = mp_cat(frag, o, subsys) 442 o = mp_cat(frag, o, "\",\"" as *u8) 443 o = mp_cat(frag, o, ledger) 444 o = mp_cat(frag, o, "\"]" as *u8) 445 frag[o] = 0 as u8 446 let av: *i64 = sys_mmap(ED_ARGV_MAX * ED_I64) as *i64 447 av[0] = mcp as i64; av[1] = base as i64; av[2] = "nx_extllm_call" as i64; av[3] = extcap as i64; av[4] = frag as i64; av[5] = 0 448 let resp: *u8 = sys_mmap(ED_RESP_CAP) 449 let rl: *i64 = sys_mmap(ED_I64) as *i64 450 let rc: i64 = tr_run_capture_to(mcp, av, resp, ED_RESP_CAP, rl, ED_FORK_TIMEOUT_MS) 451 var n: i64 = rl[0] 452 if n < 0 { n = 0 } 453 if n >= ED_RESP_CAP { n = ED_RESP_CAP - 1 } 454 resp[n] = 0 as u8 455 if rc < 0 { return 0 - 1 } 456 if n == 0 { return 0 - 1 } 457 if ed_find(resp, 0, n, "PROVIDER-REFUSED" as *u8) >= 0 { ed_json_str(resp, 0, n, "text" as *u8, text, ED_TEXT_CAP); return 0 - 2 } 458 if ed_find(resp, 0, n, "XL3-FAIL" as *u8) >= 0 { return 0 - 2 } 459 if ed_find(resp, 0, n, "REFUSE" as *u8) >= 0 { return 0 - 2 } 460 // the MCP reply wraps the provider body as the tool text; one unescape reaches the provider JSON, whose 461 // choices[0].message.content is the answer -- find the content key after that unescape 462 let one: *u8 = sys_mmap(ED_TEXT_CAP) 463 let on: i64 = ed_json_str(resp, 0, n, "text" as *u8, one, ED_TEXT_CAP) 464 if on < 0 { return 0 - 2 } 465 let tn: i64 = ed_json_str(one, 0, on, "content" as *u8, text, ED_TEXT_CAP) 466 if tn < 0 { return 0 - 2 } 467 return 0 468} 469 470// ---- ledger + shift row ---------------------------------------------------------------------------------- 471func ed_ledger_row(path: *u8, now: i64, task: *u8, engine: *u8, status: *u8, verdict: *u8, gen: i64, ms: i64, note: *u8) -> i64 { 472 let ln: *u8 = sys_mmap(ED_ROW) 473 var o: i64 = mp_catn(ln, 0, now) 474 ln[o] = ED_TAB as u8; o = o + 1 475 o = mp_cat(ln, o, task); ln[o] = ED_TAB as u8; o = o + 1 476 o = mp_cat(ln, o, engine); ln[o] = ED_TAB as u8; o = o + 1 477 o = mp_cat(ln, o, status); ln[o] = ED_TAB as u8; o = o + 1 478 o = mp_cat(ln, o, verdict); ln[o] = ED_TAB as u8; o = o + 1 479 o = mp_catn(ln, o, gen); ln[o] = ED_TAB as u8; o = o + 1 480 o = mp_catn(ln, o, ms); ln[o] = ED_TAB as u8; o = o + 1 481 var j: i64 = 0 482 while note[j] != (0 as u8) { var c: i64 = note[j] as i64; if c < 32 { c = 32 } if o < ED_ROW - 2 { ln[o] = c as u8; o = o + 1 } j = j + 1 } 483 ln[o] = ED_NL as u8; o = o + 1 484 let fd: i64 = sys_openat_append(path, ED_MODE_RW) 485 if fd < 0 { sys_munmap(ln, ED_ROW); return 0 - 1 } 486 let w: i64 = sys_write(fd, ln, o) 487 sys_close(fd) 488 sys_munmap(ln, ED_ROW) 489 if w != o { return 0 - 1 } 490 return 0 491} 492// the LOCAL column of the engine-shift scoreboard: one meter row per local GREEN, seat class maker-local-dispatch 493func ed_shift_row(mcp: *u8, base: *u8, cap: *u8, task: *u8, gen: i64, ptok: i64, resp: *u8) -> i64 { 494 let frag: *u8 = sys_mmap(ED_FRAG_CAP) 495 var o: i64 = mp_cat(frag, 0, "\"argv\":[\"meter\",\"maker-local-dispatch\",\"1\",\"" as *u8) 496 o = mp_catn(frag, o, gen) 497 o = mp_cat(frag, o, "\",\"" as *u8) 498 o = mp_catn(frag, o, ptok) 499 o = mp_cat(frag, o, "\",\"0\",\"dispatch-" as *u8) 500 o = mp_cat(frag, o, task) 501 o = mp_cat(frag, o, "-engine=local-judge=GREEN-src=nx_engine_dispatch\"]" as *u8) 502 frag[o] = 0 as u8 503 let av: *i64 = sys_mmap(ED_ARGV_MAX * ED_I64) as *i64 504 av[0] = mcp as i64; av[1] = base as i64; av[2] = "nx_seat" as i64; av[3] = cap as i64; av[4] = frag as i64; av[5] = 0 505 let rl: *i64 = sys_mmap(ED_I64) as *i64 506 let rc: i64 = tr_run_capture_to(mcp, av, resp, ED_RESP_CAP, rl, ED_FORK_TIMEOUT_MS) 507 var n: i64 = rl[0] 508 if n < 0 { n = 0 } 509 if n >= ED_RESP_CAP { n = ED_RESP_CAP - 1 } 510 resp[n] = 0 as u8 511 if rc < 0 { return 0 - 1 } 512 if ed_find(resp, 0, n, "METERED row appended" as *u8) >= 0 { return 0 } 513 return 0 - 2 514} 515 516// ---- THE DISPATCH: the ES3 contract ed_dispatch. Tries the ladder for one task; returns the exit code 517// and prints one line per engine attempt plus the DISPATCH summary with verdict= last ---- 518func ed_dispatch(taskpath: *u8, statedir: *u8, la: i64, lb: i64, lc: i64, ld: i64, lport: i64, no_external: i64, dry: i64, 519 mcp: *u8, base: *u8, cap: *u8, extcap: *u8, keyfile: *u8, egress_ledger: *u8, classes: *u8, ob: *u8, op: *i64) -> i64 { 520 var o: i64 = op[0] 521 let cells: *u8 = sys_mmap(ED_T_CELLS * ED_PATH) 522 let cls: *i64 = sys_mmap(ED_I64) as *i64 523 let mx: *i64 = sys_mmap(ED_I64) as *i64 524 let ord: *i64 = sys_mmap(ED_ORD_CELLS * ED_I64) as *i64 525 let why: *u8 = sys_mmap(ED_ROW) 526 why[0] = 0 as u8 527 let judge: i64 = ed_load_task(taskpath, cells, cls, mx, ord, classes, why) 528 if judge < 0 { 529 o = mp_cat(ob, o, "DISPATCH task=" as *u8); o = mp_cat(ob, o, taskpath) 530 o = mp_cat(ob, o, " refused=" as *u8); o = mp_cat(ob, o, why) 531 o = mp_cat(ob, o, " verdict=USAGE\n" as *u8) 532 op[0] = o 533 return ED_EXIT_USAGE 534 } 535 let task: *u8 = ed_cell(cells, ED_T_NAME) 536 let jarg: *u8 = ed_cell(cells, ED_T_JUDGE_ARG) 537 let lp: *i64 = sys_mmap(ED_I64) as *i64 538 let prompt: *u8 = ed_read(ed_cell(cells, ED_T_PROMPT), lp) 539 if (prompt as i64) == 0 { 540 o = mp_cat(ob, o, "DISPATCH task=" as *u8); o = mp_cat(ob, o, task) 541 o = mp_cat(ob, o, " refused=prompt-unreadable verdict=USAGE\n" as *u8) 542 op[0] = o 543 return ED_EXIT_USAGE 544 } 545 var pn: i64 = lp[0] 546 if pn > ED_PROMPT_CAP { pn = ED_PROMPT_CAP } 547 let now: i64 = sys_now_realtime_sec() 548 let ledger: *u8 = sys_mmap(ED_PATH) 549 mp_join(ledger, statedir, "engine_dispatch.jrnl" as *u8) 550 let text: *u8 = sys_mmap(ED_TEXT_CAP) 551 let meta: *i64 = sys_mmap(4 * ED_I64) as *i64 552 let resp: *u8 = sys_mmap(ED_RESP_CAP) 553 var reached: i64 = 0 554 var passed: i64 = 0 555 var engine_won: i64 = 0 556 var skipped_order: i64 = 0 557 o = mp_cat(ob, o, "DISPATCH task=" as *u8); o = mp_cat(ob, o, task) 558 if cls[0] == ED_CLASS_U { o = mp_cat(ob, o, " class=U" as *u8) } else { o = mp_cat(ob, o, " class=C" as *u8) } 559 let ctc0: *u8 = ed_cell(cells, ED_T_TCLASS) 560 if ctc0[0] != (0 as u8) { o = mp_cat(ob, o, " tclass=" as *u8); o = mp_cat(ob, o, ctc0) } 561 o = mp_cat(ob, o, " order=" as *u8) 562 if ord[ED_ORD_LOCAL] == 1 { o = mp_cat(ob, o, "local," as *u8) } 563 if ord[ED_ORD_EXTERNAL] == 1 { o = mp_cat(ob, o, "external," as *u8) } 564 o = mp_cat(ob, o, "frontier" as *u8) 565 if judge == ED_JUDGE_KAT { o = mp_cat(ob, o, " judge=kat" as *u8) } else { o = mp_cat(ob, o, " judge=contains" as *u8) } 566 o = mp_cat(ob, o, " max_new=" as *u8); o = mp_catn(ob, o, mx[0]) 567 o = mp_cat(ob, o, "\n" as *u8) 568 // ---- 1 LOCAL ---- 569 if ord[ED_ORD_LOCAL] == 0 { 570 o = mp_cat(ob, o, "ENGINE local status=SKIPPED-by-class-order\n" as *u8) 571 skipped_order = skipped_order + 1 572 if dry == 0 { ed_ledger_row(ledger, now, task, "local" as *u8, "SKIPPED-by-class-order" as *u8, "UNOBSERVABLE" as *u8, 0, 0, "the class row does not list local" as *u8) } 573 } else { if dry == 1 { o = mp_cat(ob, o, "ENGINE local status=WOULD-TRY\n" as *u8) } else { 574 meta[0] = 0; meta[1] = 0; meta[2] = 0; meta[3] = 0 575 let lr: i64 = ed_local_gen(la, lb, lc, ld, lport, prompt, pn, mx[0], text, meta) 576 if lr == 0 - 1 { 577 o = mp_cat(ob, o, "ENGINE local status=UNREACHABLE\n" as *u8) 578 ed_ledger_row(ledger, now, task, "local" as *u8, "UNREACHABLE" as *u8, "UNOBSERVABLE" as *u8, 0, 0, "seat did not answer" as *u8) 579 } else { if lr == ED_R_NOREPLY { 580 reached = reached + 1 581 o = mp_cat(ob, o, "ENGINE local status=NO-REPLY (the socket accepted and the request was written; zero bytes came back -- a listener that is not a seat)\n" as *u8) 582 ed_ledger_row(ledger, now, task, "local" as *u8, "NO-REPLY" as *u8, "UNOBSERVABLE" as *u8, 0, 0, "socket accepted and closed with no body" as *u8) 583 } else { if lr == 0 - 2 { 584 reached = reached + 1 585 o = mp_cat(ob, o, "ENGINE local status=ANSWERED-UNPARSEABLE http=" as *u8); o = mp_catn(ob, o, meta[3]); o = mp_cat(ob, o, "\n" as *u8) 586 ed_ledger_row(ledger, now, task, "local" as *u8, "UNPARSEABLE" as *u8, "UNOBSERVABLE" as *u8, 0, 0, "reply not a /gen JSON" as *u8) 587 } else { 588 reached = reached + 1 589 let v: i64 = ed_judge(judge, jarg, text, mp_len(text)) 590 o = mp_cat(ob, o, "ENGINE local status=ANSWERED gen_tokens=" as *u8); o = mp_catn(ob, o, meta[0]) 591 o = mp_cat(ob, o, " prompt_tokens=" as *u8); o = mp_catn(ob, o, meta[1]) 592 o = mp_cat(ob, o, " ms=" as *u8); o = mp_catn(ob, o, meta[2]) 593 o = mp_cat(ob, o, " judge=" as *u8); o = mp_cat(ob, o, ed_vname(v)) 594 o = mp_cat(ob, o, "\n" as *u8) 595 ed_ledger_row(ledger, now, task, "local" as *u8, "ANSWERED" as *u8, ed_vname(v), meta[0], meta[2], text) 596 if v == ED_V_GREEN { 597 passed = 1 598 engine_won = ED_ENGINE_LOCAL 599 if mp_len(mcp) > 0 { 600 let sr: i64 = ed_shift_row(mcp, base, cap, task, meta[0], meta[1], resp) 601 if sr == 0 { o = mp_cat(ob, o, "SHIFT-ROW local status=METERED\n" as *u8) } else { o = mp_cat(ob, o, "SHIFT-ROW local status=PUSH-FAILED (the verdict stands; the scoreboard row is owed)\n" as *u8) } 602 } else { o = mp_cat(ob, o, "SHIFT-ROW local status=NOT-PUSHED (no --mcp)\n" as *u8) } 603 } 604 } } } 605 } } 606 // ---- 2 EXTERNAL ---- 607 if passed == 0 { 608 if ord[ED_ORD_EXTERNAL] == 0 { 609 o = mp_cat(ob, o, "ENGINE external status=SKIPPED-by-class-order\n" as *u8) 610 skipped_order = skipped_order + 1 611 if dry == 0 { ed_ledger_row(ledger, now, task, "external" as *u8, "SKIPPED-by-class-order" as *u8, "UNOBSERVABLE" as *u8, 0, 0, "the class row does not list external" as *u8) } 612 } else { if cls[0] == ED_CLASS_C { 613 o = mp_cat(ob, o, "ENGINE external status=SKIPPED-class-C (a C task never reaches a third-party sink)\n" as *u8) 614 if dry == 0 { ed_ledger_row(ledger, now, task, "external" as *u8, "SKIPPED-class-C" as *u8, "UNOBSERVABLE" as *u8, 0, 0, "class C never leaves the estate" as *u8) } 615 } else { if no_external == 1 { 616 o = mp_cat(ob, o, "ENGINE external status=SKIPPED-by-flag\n" as *u8) 617 } else { if dry == 1 { o = mp_cat(ob, o, "ENGINE external status=WOULD-TRY\n" as *u8) } else { 618 if mp_len(mcp) == 0 { o = mp_cat(ob, o, "ENGINE external status=SKIPPED-no-mcp-client\n" as *u8) } else { 619 // the local answer must never be filed under the external row (MEASURED live: a warden REFUSE left 620 // the previous engine's text in the buffer and the ledger attributed it to the external tier) 621 text[0] = 0 as u8 622 let er: i64 = ed_external_gen(mcp, base, extcap, ed_cell(cells, ED_T_PROMPT), keyfile, ed_cell(cells, ED_T_SUBSYS), egress_ledger, text) 623 if er == 0 - 1 { 624 o = mp_cat(ob, o, "ENGINE external status=UNREACHABLE\n" as *u8) 625 ed_ledger_row(ledger, now, task, "external" as *u8, "UNREACHABLE" as *u8, "UNOBSERVABLE" as *u8, 0, 0, "mcp client or warden did not answer" as *u8) 626 } else { if er == 0 - 2 { 627 reached = reached + 1 628 o = mp_cat(ob, o, "ENGINE external status=REFUSED (warden or provider; the receipt names which)\n" as *u8) 629 ed_ledger_row(ledger, now, task, "external" as *u8, "REFUSED" as *u8, "UNOBSERVABLE" as *u8, 0, 0, text) 630 } else { 631 reached = reached + 1 632 let v2: i64 = ed_judge(judge, jarg, text, mp_len(text)) 633 o = mp_cat(ob, o, "ENGINE external status=ANSWERED judge=" as *u8); o = mp_cat(ob, o, ed_vname(v2)); o = mp_cat(ob, o, "\n" as *u8) 634 ed_ledger_row(ledger, now, task, "external" as *u8, "ANSWERED" as *u8, ed_vname(v2), 0, 0, text) 635 if v2 == ED_V_GREEN { passed = 1; engine_won = ED_ENGINE_EXTERNAL } 636 } } 637 } 638 } } } } 639 } 640 // ---- 3 FRONTIER: never called, always named ---- 641 var rc: i64 = ED_EXIT_GREEN 642 if passed == 1 { 643 o = mp_cat(ob, o, "DISPATCH-DONE task=" as *u8); o = mp_cat(ob, o, task) 644 if engine_won == ED_ENGINE_LOCAL { o = mp_cat(ob, o, " engine=local" as *u8) } else { o = mp_cat(ob, o, " engine=external" as *u8) } 645 o = mp_cat(ob, o, " reached=" as *u8); o = mp_catn(ob, o, reached) 646 o = mp_cat(ob, o, " verdict=GREEN\n" as *u8) 647 } else { if dry == 1 { 648 o = mp_cat(ob, o, "DISPATCH-DONE task=" as *u8); o = mp_cat(ob, o, task); o = mp_cat(ob, o, " dry=1 reached=0 verdict=DRY\n" as *u8) 649 } else { if reached + skipped_order == 0 { 650 o = mp_cat(ob, o, "DISPATCH-DONE task=" as *u8); o = mp_cat(ob, o, task); o = mp_cat(ob, o, " engine=none reached=0 verdict=UNOBSERVABLE\n" as *u8) 651 rc = ED_EXIT_UNOBS 652 } else { 653 // reached engines all failed the judge, or the class row admits nothing below the seat: the seat decides 654 ed_ledger_row(ledger, now, task, "frontier" as *u8, "HANDED-TO-SEAT" as *u8, "PENDING" as *u8, 0, 0, "every engine below the seat failed the judge or is excluded by the class row" as *u8) 655 o = mp_cat(ob, o, "ENGINE frontier status=HANDED-TO-SEAT (not an engine this organ calls)\n" as *u8) 656 o = mp_cat(ob, o, "DISPATCH-DONE task=" as *u8); o = mp_cat(ob, o, task) 657 o = mp_cat(ob, o, " engine=none reached=" as *u8); o = mp_catn(ob, o, reached) 658 o = mp_cat(ob, o, " skipped_by_class_order=" as *u8); o = mp_catn(ob, o, skipped_order) 659 o = mp_cat(ob, o, " verdict=NEEDS-FRONTIER\n" as *u8) 660 rc = ED_EXIT_FRONTIER 661 } } } 662 op[0] = o 663 return rc 664} 665 666func ed_usage() -> i64 { 667 let m: *u8 = "usage: nx_engine_dispatch run <task.conf> <state-dir> [--local a.b.c.d:port] [--no-external] [--dry] [--mcp <elf> --base <url> --cap <file> --extcap <file>] [--keyfile <path>] [--ledger <estate-path>] [--classes <engine_classes.conf>] | judge <candidate-file> <task.conf>\n" as *u8 668 mp_write_all(2, m, mp_len(m)) 669 return ED_EXIT_USAGE 670} 671 672// parse a.b.c.d:port into out[0..4]; 1 ok 673func ed_parse_hostport(s: *u8, out: *i64) -> i64 { 674 var i: i64 = 0 675 var f: i64 = 0 676 var v: i64 = 0 677 var digits: i64 = 0 678 // MEASURED by the gate on the first run: the colon (58) sorts ABOVE the digit range, so a branch that treated 679 // every byte >= 48 as a digit refused `127.0.0.1:18632` as usage while the default port path passed live 680 while f < 5 { 681 let c: i64 = s[i] as i64 682 var isdig: i64 = 0 683 if c >= 48 { if c <= 57 { isdig = 1 } } 684 if isdig == 1 { v = v * 10 + (c - 48); digits = digits + 1; i = i + 1 } else { 685 if digits == 0 { return 0 } 686 out[f] = v; f = f + 1; v = 0; digits = 0 687 if f < 5 { 688 var want: i64 = 46 689 if f == 4 { want = 58 } 690 if c != want { return 0 } 691 i = i + 1 692 } 693 } 694 } 695 return 1 696} 697 698func main(argc: i64, argv: *i64) -> i64 { 699 if argc < 3 { sys_exit(ed_usage()); return ED_EXIT_USAGE } 700 let verb: *u8 = argv[1] as *u8 701 let ob: *u8 = sys_mmap(ED_OUT) 702 let op: *i64 = sys_mmap(ED_I64) as *i64 703 op[0] = 0 704 var classes: *u8 = "knowledge/engine_classes.conf" as *u8 705 if mp_streq(verb, "judge" as *u8) == 1 { 706 if argc < 4 { sys_exit(ed_usage()); return ED_EXIT_USAGE } 707 let cells: *u8 = sys_mmap(ED_T_CELLS * ED_PATH) 708 let cls: *i64 = sys_mmap(ED_I64) as *i64 709 let mx: *i64 = sys_mmap(ED_I64) as *i64 710 let ordj: *i64 = sys_mmap(ED_ORD_CELLS * ED_I64) as *i64 711 let why: *u8 = sys_mmap(ED_ROW) 712 why[0] = 0 as u8 713 let judge: i64 = ed_load_task(argv[3] as *u8, cells, cls, mx, ordj, classes, why) 714 if judge < 0 { var o0: i64 = mp_cat(ob, 0, "JUDGE refused=" as *u8); o0 = mp_cat(ob, o0, why); o0 = mp_cat(ob, o0, " verdict=USAGE\n" as *u8); mp_write_all(1, ob, o0); sys_exit(ED_EXIT_USAGE); return ED_EXIT_USAGE } 715 let lp: *i64 = sys_mmap(ED_I64) as *i64 716 let cand: *u8 = ed_read(argv[2] as *u8, lp) 717 var v: i64 = ED_V_UNOBS 718 if (cand as i64) != 0 { v = ed_judge(judge, ed_cell(cells, ED_T_JUDGE_ARG), cand, lp[0]) } 719 var o1: i64 = mp_cat(ob, 0, "JUDGE task=" as *u8); o1 = mp_cat(ob, o1, ed_cell(cells, ED_T_NAME)) 720 if judge == ED_JUDGE_KAT { o1 = mp_cat(ob, o1, " rule=kat" as *u8) } else { o1 = mp_cat(ob, o1, " rule=contains" as *u8) } 721 o1 = mp_cat(ob, o1, " verdict=" as *u8); o1 = mp_cat(ob, o1, ed_vname(v)); o1 = mp_cat(ob, o1, "\n" as *u8) 722 mp_write_all(1, ob, o1) 723 var jrc: i64 = ED_EXIT_GREEN 724 if v == ED_V_RED { jrc = 1 } 725 if v == ED_V_UNOBS { jrc = ED_EXIT_UNOBS } 726 sys_exit(jrc) 727 return jrc 728 } 729 if mp_streq(verb, "run" as *u8) == 0 { sys_exit(ed_usage()); return ED_EXIT_USAGE } 730 if argc < 4 { sys_exit(ed_usage()); return ED_EXIT_USAGE } 731 let taskpath: *u8 = argv[2] as *u8 732 let statedir: *u8 = argv[3] as *u8 733 var la: i64 = ED_DEFAULT_A 734 var lb: i64 = ED_DEFAULT_B 735 var lc: i64 = ED_DEFAULT_C 736 var ld: i64 = ED_DEFAULT_D 737 var lport: i64 = ED_DEFAULT_PORT 738 var no_external: i64 = 0 739 var dry: i64 = 0 740 var mcp: *u8 = "" as *u8 741 var base: *u8 = "" as *u8 742 var cap: *u8 = "" as *u8 743 var extcap: *u8 = "" as *u8 744 var keyfile: *u8 = "/tmp/nx_extllm_bearer.txt" as *u8 745 var egress: *u8 = "knowledge/status/extllm_egress.ledger" as *u8 746 var ai: i64 = 4 747 let hp: *i64 = sys_mmap(5 * ED_I64) as *i64 748 while ai < argc { 749 let a: *u8 = argv[ai] as *u8 750 var used: i64 = 0 751 if mp_streq(a, "--no-external" as *u8) == 1 { no_external = 1; used = 1 } 752 if mp_streq(a, "--dry" as *u8) == 1 { dry = 1; used = 1 } 753 if mp_streq(a, "--local" as *u8) == 1 { if ai + 1 < argc { if ed_parse_hostport(argv[ai + 1] as *u8, hp) == 1 { la = hp[0]; lb = hp[1]; lc = hp[2]; ld = hp[3]; lport = hp[4] } else { sys_exit(ed_usage()); return ED_EXIT_USAGE } ai = ai + 1 } used = 1 } 754 if mp_streq(a, "--mcp" as *u8) == 1 { if ai + 1 < argc { mcp = argv[ai + 1] as *u8; ai = ai + 1 } used = 1 } 755 if mp_streq(a, "--base" as *u8) == 1 { if ai + 1 < argc { base = argv[ai + 1] as *u8; ai = ai + 1 } used = 1 } 756 if mp_streq(a, "--cap" as *u8) == 1 { if ai + 1 < argc { cap = argv[ai + 1] as *u8; ai = ai + 1 } used = 1 } 757 if mp_streq(a, "--extcap" as *u8) == 1 { if ai + 1 < argc { extcap = argv[ai + 1] as *u8; ai = ai + 1 } used = 1 } 758 if mp_streq(a, "--keyfile" as *u8) == 1 { if ai + 1 < argc { keyfile = argv[ai + 1] as *u8; ai = ai + 1 } used = 1 } 759 if mp_streq(a, "--ledger" as *u8) == 1 { if ai + 1 < argc { egress = argv[ai + 1] as *u8; ai = ai + 1 } used = 1 } 760 if mp_streq(a, "--classes" as *u8) == 1 { if ai + 1 < argc { classes = argv[ai + 1] as *u8; ai = ai + 1 } used = 1 } 761 if used == 0 { sys_exit(ed_usage()); return ED_EXIT_USAGE } 762 ai = ai + 1 763 } 764 let rc: i64 = ed_dispatch(taskpath, statedir, la, lb, lc, ld, lport, no_external, dry, mcp, base, cap, extcap, keyfile, egress, classes, ob, op) 765 mp_write_all(1, ob, op[0]) 766 sys_exit(rc) 767 return rc 768}