code wiki / (root) / nx_race_referee.nx

nx_race_referee.nx source

↩ module page · 340 lines · 12747 B

1// nx_race_referee.nx -- the GENERIC AUTONOMY-RACE REFEREE (race-1's third growth rung, 2// RACE_HARNESS). Everything assignment-specific is DATA in a manifest (rule 11) -- running a 3// NEW race needs no new referee code, only a manifest + an oracle stager: 4// 5// /tmp/race_manifest.txt lines: 6// lane <name> <elfpath> up to 4 lanes (lane 0 = the team by convention) 7// case <name> <inputfile> up to 8 staged input cases 8// row <case> <key> <want> up to 64 scoring rows (key matched in lane stdout, e.g. SNI=) 9// 10// For each lane x case: stage the case file to /tmp/race_input.bin (the lane contract), run the 11// lane ELF with stdout captured, parse each row's <key><int>, compare to <want>. Output: 12// RACE-H row= lines + per-lane permil + a lane0-vs-lane1 verdict -> stdout + 13// knowledge/status/race_harness.log (Archivist rule). Malformed manifest = LOUD exit 2, never 14// a silent partial race. RACI: Referee=SCORE -- mechanical, no self-grade, no lane authorship. 15// LAWS: struct-free, integer-only, flat ifs, no &&/||, <=6 args, no shell. license_tier: ORIGINAL 16import "nx_syscalls.nx" 17import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 18const K_MAGIC_8192: i64 = 8192 19 20func rh_puts(fd: i64, s: *u8) -> i64 { 21 var n: i64 = 0 22 while s[n] != (0 as u8) { n = n + 1 } 23 sys_write(fd, s, n) 24 return 0 25} 26 27// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 28// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 29// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 30// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 31func rh_putn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 } 32 33// token #idx within buf[ls,le), space-separated; writes start/end to se[0]/se[1]; 1=found 34func rh_tok(buf: *u8, ls: i64, le: i64, idx: i64, se: *i64) -> i64 { 35 var i: i64 = ls 36 var cur: i64 = 0 37 var found: i64 = 0 38 var scanning: i64 = 1 39 while scanning == 1 { 40 var skipping: i64 = 1 41 while skipping == 1 { 42 if i >= le { skipping = 0 } 43 if skipping == 1 { if buf[i] != (32 as u8) { skipping = 0 } } 44 if skipping == 1 { i = i + 1 } 45 } 46 if i >= le { scanning = 0 } 47 if scanning == 1 { 48 let st: i64 = i 49 var run: i64 = 1 50 while run == 1 { 51 if i >= le { run = 0 } 52 if run == 1 { if buf[i] == (32 as u8) { run = 0 } } 53 if run == 1 { i = i + 1 } 54 } 55 if cur == idx { se[0] = st; se[1] = i; found = 1; scanning = 0 } 56 cur = cur + 1 57 } 58 } 59 return found 60} 61 62// does buf[s,e) equal NUL-terminated lit? 63func rh_tok_is(buf: *u8, s: i64, e: i64, lit: *u8) -> i64 { 64 var n: i64 = 0 65 while lit[n] != (0 as u8) { n = n + 1 } 66 if e - s != n { return 0 } 67 var i: i64 = 0 68 while i < n { 69 if buf[s + i] != lit[i] { return 0 } 70 i = i + 1 71 } 72 return 1 73} 74 75// copy buf[s,e) into the string pool as a NUL-terminated string 76func rh_copy_tok(buf: *u8, s: i64, e: i64, pool: *u8, poolp: *i64) -> *u8 { 77 let dst: *u8 = pool + poolp[0] 78 var i: i64 = 0 79 while s + i < e { dst[i] = buf[s + i]; i = i + 1 } 80 dst[i] = 0 as u8 81 poolp[0] = poolp[0] + i + 1 82 return dst 83} 84 85// signed decimal in buf[s,e) 86func rh_int(buf: *u8, s: i64, e: i64) -> i64 { 87 var p: i64 = s 88 var neg: i64 = 0 89 if p < e { if buf[p] == (45 as u8) { neg = 1; p = p + 1 } } 90 var v: i64 = 0 91 while p < e { 92 let c: i64 = buf[p] & 0xff 93 var dig: i64 = 1 94 if c < 48 { dig = 0 } 95 if c > 57 { dig = 0 } 96 if dig == 0 { p = e } 97 if dig == 1 { v = v * 10 + (c - 48); p = p + 1 } 98 } 99 if neg == 1 { return 0 - v } 100 return v 101} 102 103// stage one case file as the lanes' fixed input path 104func rh_stage(src: *u8) -> i64 { 105 let lenp: *i64 = sys_mmap(16) as *i64 106 let b: *u8 = sys_read_file(src, lenp) 107 let n: i64 = lenp[0] 108 let fd: i64 = sys_openat_wr("/tmp/race_input.bin\x00" as *u8, 0x1a4) 109 if fd < 0 { return 0 - 1 } 110 if n > 0 { sys_write(fd, b, n) } 111 sys_close(fd) 112 return n 113} 114 115// run a lane ELF with stdout captured 116func rh_run(elf: *u8) -> i64 { 117 let pid: i64 = sys_fork() 118 if pid == 0 { 119 let ofd: i64 = sys_openat_wr("/tmp/race_lane_out.txt\x00" as *u8, 0x1a4) 120 if ofd >= 0 { sys_dup3(ofd, 1, 0) } 121 let argv: *i64 = sys_mmap(32) as *i64 122 argv[0] = elf as i64; argv[1] = 0 123 let envp: *i64 = sys_mmap(16) as *i64 124 envp[0] = 0 125 sys_execve(elf, argv, envp) 126 sys_exit(127) 127 } 128 let st: *i64 = sys_mmap(16) as *i64 129 sys_wait4(pid, st, 0) 130 return st[0] 131} 132 133// parse "<key><signed int>" from captured output; -999 = key missing 134func rh_parse(buf: *u8, blen: i64, key: *u8) -> i64 { 135 var klen: i64 = 0 136 while key[klen] != (0 as u8) { klen = klen + 1 } 137 var i: i64 = 0 138 var at: i64 = 0 - 1 139 while i + klen <= blen { 140 var j: i64 = 0 141 var ok: i64 = 1 142 while j < klen { 143 if buf[i + j] != key[j] { ok = 0; j = klen } 144 if ok == 1 { j = j + 1 } 145 } 146 if ok == 1 { at = i; i = blen } 147 i = i + 1 148 } 149 if at < 0 { return 0 - 999 } 150 var e: i64 = at + klen 151 var scanning: i64 = 1 152 if e < blen { if buf[e] == (45 as u8) { e = e + 1 } } 153 while scanning == 1 { 154 if e >= blen { scanning = 0 } 155 if scanning == 1 { 156 let c: i64 = buf[e] & 0xff 157 var dig: i64 = 1 158 if c < 48 { dig = 0 } 159 if c > 57 { dig = 0 } 160 if dig == 0 { scanning = 0 } 161 if dig == 1 { e = e + 1 } 162 } 163 } 164 return rh_int(buf, at + klen, e) 165} 166 167// one scored row, printed to stdout + log; 1 iff got==want 168func rh_row(logfd: i64, lane: *u8, casen: *u8, key: *u8, got: i64, want: i64) -> i64 { 169 var fdi: i64 = 0 170 while fdi < 2 { 171 var fd: i64 = 1 172 if fdi == 1 { fd = logfd } 173 if fd > 0 { 174 rh_puts(fd, "RACE-H row=\x00" as *u8) 175 rh_puts(fd, lane) 176 rh_puts(fd, ":\x00" as *u8) 177 rh_puts(fd, casen) 178 rh_puts(fd, ":\x00" as *u8) 179 rh_puts(fd, key) 180 rh_puts(fd, " got=\x00" as *u8) 181 rh_putn(fd, got) 182 rh_puts(fd, " want=\x00" as *u8) 183 rh_putn(fd, want) 184 if got == want { rh_puts(fd, " verdict=PASS\n\x00" as *u8) } 185 if got != want { rh_puts(fd, " verdict=FAIL\n\x00" as *u8) } 186 } 187 fdi = fdi + 1 188 } 189 if got == want { return 1 } 190 return 0 191} 192 193func main() -> i64 { 194 let lenp: *i64 = sys_mmap(16) as *i64 195 let mf: *u8 = sys_read_file("/tmp/race_manifest.txt\x00" as *u8, lenp) 196 let mn: i64 = lenp[0] 197 if mn <= 0 { rh_puts(1, "RACE-H MANIFEST MISSING /tmp/race_manifest.txt\n\x00" as *u8); return 2 } 198 199 let pool: *u8 = sys_mmap(K_MAGIC_8192) 200 let poolp: *i64 = sys_mmap(16) as *i64 201 poolp[0] = 0 202 let ln_names: *i64 = sys_mmap(64) as *i64 203 let ln_paths: *i64 = sys_mmap(64) as *i64 204 var n_lanes: i64 = 0 205 let cs_names: *i64 = sys_mmap(128) as *i64 206 let cs_files: *i64 = sys_mmap(128) as *i64 207 var n_cases: i64 = 0 208 let rw_case: *i64 = sys_mmap(8 * 64) as *i64 209 let rw_key: *i64 = sys_mmap(8 * 64) as *i64 210 let rw_want: *i64 = sys_mmap(8 * 64) as *i64 211 var n_rows: i64 = 0 212 213 let se: *i64 = sys_mmap(32) as *i64 214 var ls: i64 = 0 215 while ls < mn { 216 var le: i64 = ls 217 var scan: i64 = 1 218 while scan == 1 { 219 if le >= mn { scan = 0 } 220 if scan == 1 { if mf[le] == (10 as u8) { scan = 0 } } 221 if scan == 1 { le = le + 1 } 222 } 223 if rh_tok(mf, ls, le, 0, se) == 1 { 224 if rh_tok_is(mf, se[0], se[1], "lane\x00" as *u8) == 1 { 225 if n_lanes >= 4 { rh_puts(1, "RACE-H TOO MANY LANES\n\x00" as *u8); return 2 } 226 if rh_tok(mf, ls, le, 1, se) != 1 { return 2 } 227 ln_names[n_lanes] = rh_copy_tok(mf, se[0], se[1], pool, poolp) as i64 228 if rh_tok(mf, ls, le, 2, se) != 1 { return 2 } 229 ln_paths[n_lanes] = rh_copy_tok(mf, se[0], se[1], pool, poolp) as i64 230 n_lanes = n_lanes + 1 231 } else { if rh_tok_is(mf, se[0], se[1], "case\x00" as *u8) == 1 { 232 if n_cases >= 8 { rh_puts(1, "RACE-H TOO MANY CASES\n\x00" as *u8); return 2 } 233 if rh_tok(mf, ls, le, 1, se) != 1 { return 2 } 234 cs_names[n_cases] = rh_copy_tok(mf, se[0], se[1], pool, poolp) as i64 235 if rh_tok(mf, ls, le, 2, se) != 1 { return 2 } 236 cs_files[n_cases] = rh_copy_tok(mf, se[0], se[1], pool, poolp) as i64 237 n_cases = n_cases + 1 238 } else { if rh_tok_is(mf, se[0], se[1], "row\x00" as *u8) == 1 { 239 if n_rows >= 64 { rh_puts(1, "RACE-H TOO MANY ROWS\n\x00" as *u8); return 2 } 240 if rh_tok(mf, ls, le, 1, se) != 1 { return 2 } 241 var cidx: i64 = 0 - 1 242 var ci: i64 = 0 243 while ci < n_cases { 244 if rh_tok_is(mf, se[0], se[1], cs_names[ci] as *u8) == 1 { cidx = ci } 245 ci = ci + 1 246 } 247 if cidx < 0 { rh_puts(1, "RACE-H ROW NAMES UNKNOWN CASE\n\x00" as *u8); return 2 } 248 rw_case[n_rows] = cidx 249 if rh_tok(mf, ls, le, 2, se) != 1 { return 2 } 250 rw_key[n_rows] = rh_copy_tok(mf, se[0], se[1], pool, poolp) as i64 251 if rh_tok(mf, ls, le, 3, se) != 1 { return 2 } 252 rw_want[n_rows] = rh_int(mf, se[0], se[1]) 253 n_rows = n_rows + 1 254 } else { 255 rh_puts(1, "RACE-H UNKNOWN MANIFEST LINE KIND\n\x00" as *u8) 256 return 2 257 } } } 258 } 259 ls = le + 1 260 } 261 if n_lanes < 1 { rh_puts(1, "RACE-H NO LANES\n\x00" as *u8); return 2 } 262 if n_rows < 1 { rh_puts(1, "RACE-H NO ROWS\n\x00" as *u8); return 2 } 263 264 let logfd: i64 = sys_openat_append("knowledge/status/race_harness.log\x00" as *u8, 0x1a4) 265 var fdi: i64 = 0 266 while fdi < 2 { 267 var fd: i64 = 1 268 if fdi == 1 { fd = logfd } 269 if fd > 0 { 270 rh_puts(fd, "RACE-H epoch=\x00" as *u8) 271 rh_putn(fd, sys_now_realtime_sec()) 272 rh_puts(fd, " lanes=\x00" as *u8) 273 rh_putn(fd, n_lanes) 274 rh_puts(fd, " cases=\x00" as *u8) 275 rh_putn(fd, n_cases) 276 rh_puts(fd, " rows=\x00" as *u8) 277 rh_putn(fd, n_rows) 278 rh_puts(fd, "\n\x00" as *u8) 279 } 280 fdi = fdi + 1 281 } 282 283 let permils: *i64 = sys_mmap(64) as *i64 284 let olenp: *i64 = sys_mmap(16) as *i64 285 var li: i64 = 0 286 while li < n_lanes { 287 var passed: i64 = 0 288 var ci2: i64 = 0 289 while ci2 < n_cases { 290 rh_stage(cs_files[ci2] as *u8) 291 rh_run(ln_paths[li] as *u8) 292 let ob: *u8 = sys_read_file("/tmp/race_lane_out.txt\x00" as *u8, olenp) 293 let on: i64 = olenp[0] 294 var ri: i64 = 0 295 while ri < n_rows { 296 if rw_case[ri] == ci2 { 297 let got: i64 = rh_parse(ob, on, rw_key[ri] as *u8) 298 passed = passed + rh_row(logfd, ln_names[li] as *u8, cs_names[ci2] as *u8, rw_key[ri] as *u8, got, rw_want[ri]) 299 } 300 ri = ri + 1 301 } 302 ci2 = ci2 + 1 303 } 304 permils[li] = passed * 1000 / n_rows 305 fdi = 0 306 while fdi < 2 { 307 var fd: i64 = 1 308 if fdi == 1 { fd = logfd } 309 if fd > 0 { 310 rh_puts(fd, "RACE-H lane=\x00" as *u8) 311 rh_puts(fd, ln_names[li] as *u8) 312 rh_puts(fd, " permil=\x00" as *u8) 313 rh_putn(fd, permils[li]) 314 rh_puts(fd, "\n\x00" as *u8) 315 } 316 fdi = fdi + 1 317 } 318 li = li + 1 319 } 320 321 if n_lanes >= 2 { 322 fdi = 0 323 while fdi < 2 { 324 var fd: i64 = 1 325 if fdi == 1 { fd = logfd } 326 if fd > 0 { 327 rh_puts(fd, "RACE-H lane0=\x00" as *u8) 328 rh_puts(fd, ln_names[0] as *u8) 329 rh_puts(fd, " vs lane1=\x00" as *u8) 330 rh_puts(fd, ln_names[1] as *u8) 331 if permils[0] > permils[1] { rh_puts(fd, " verdict=LANE0-EXCEEDS\n\x00" as *u8) } 332 if permils[0] == permils[1] { rh_puts(fd, " verdict=LANE0-MATCHES\n\x00" as *u8) } 333 if permils[0] < permils[1] { rh_puts(fd, " verdict=LANE0-BEHIND\n\x00" as *u8) } 334 } 335 fdi = fdi + 1 336 } 337 } 338 if logfd > 0 { sys_close(logfd) } 339 return 0 340}