code wiki / _hdl_build / nx_overnight.nx

nx_overnight.nx source

↩ module page · 149 lines · 9548 B

1// nx_overnight.nx -- UNSUPERVISED overnight runner: the team mines a large search space in 2// chunks, beat after beat, banking what it finds and JOURNALING every beat to a log the 3// operator + Claude review in the morning. Resource-bounded (a parallel pool of K workers, 4// each a fork whose RAM is reclaimed on exit) so it runs all night without growing. Each beat 5// also EXEC-VERIFIES a sample, so any correctness regression is logged as an ISSUE to address. 6// The operator kicks it off (background it / loop the binary); Claude reads the journal next 7// day to critique + build the team up. license_tier: ORIGINAL 8 9import "nx_mulchain_deep.nx" // deepened + cost-bounded search (brings nx_mulchain transitively) 10import "nx_superopt_emit.nx" 11import "nx_engineer_crash.nx" 12import "nx_syscalls.nx" 13const K_MAGIC_8192: i64 = 8192 14const K_MAGIC_20000: i64 = 20000 15 16func ov_shared(size: i64) -> *u8 { let r: i64 = __syscall(SYS_MMAP, 0, size, 3, 0x21, 0 - 1, 0); return r as *u8 } 17 18// per-constant work = the COST-OPTIMAL decision (the team's resolution of its own GAP escalation: 19// "constants exceed imul cost -- author imul or deepen the search?" -> BOTH, composed). Deepen 20// the search to the cost-competitive cap (Levin's cost bound) and pick chain-vs-imul by cost, 21// ENGINEER-VERIFIED (mc_cost_select returns the op count ONLY if the chain provably computes c*x 22// AND is the cost-winner; else 0 = imul wins). cap=5 (throughput-aware), tie<=3 (latency line). 23func ov_work(c: i64) -> i64 { let op: *i64 = sys_mmap(8*20) as *i64; let a: *i64 = sys_mmap(8*20) as *i64; let b: *i64 = sys_mmap(8*20) as *i64; return mc_cost_select(c, 5, 3, op, a, b) } 24 25// bounded-parallel ingest of constants [lo,hi) into bank (bank[c-lo] = chain length). 26func ov_ingest(lo: i64, hi: i64, K: i64, bank: *u8) -> i64 { 27 var next: i64 = lo; var live: i64 = 0 28 let status: *i64 = sys_mmap(16) as *i64; var done: i64 = 0 29 while done == 0 { 30 var fill: i64 = 1 31 while fill == 1 { 32 if live >= K { fill = 0 } else { if next >= hi { fill = 0 } else { 33 let pid: i64 = sys_fork() 34 if pid == 0 { bank[next - lo] = ov_work(next) as u8; sys_exit(0) } 35 live = live + 1; next = next + 1 36 } } 37 } 38 if live > 0 { sys_wait4(0 - 1, status, 0); live = live - 1 } 39 if next >= hi { if live <= 0 { done = 1 } } 40 } 41 return 0 42} 43 44// EXEC-VERIFY one constant end to end (emit + run vs reference) -> 1 ok / 0 regression. 45func ov_verify(c: i64) -> i64 { 46 let op: *i64 = sys_mmap(8*12) as *i64; let a: *i64 = sys_mmap(8*12) as *i64; let b: *i64 = sys_mmap(8*12) as *i64 47 let dd: *i64 = sys_mmap(8*16) as *i64; let buf: *u8 = sys_mmap(K_MAGIC_8192); let icnt: *i64 = sys_mmap(8) as *i64 48 dd[0]=5; dd[1]=0-8; dd[2]=123; dd[3]=0-456; dd[4]=77; dd[5]=0-1; dd[6]=1000; dd[7]=0-1000 49 var ref: i64 = 0; var kk: i64 = 0; while kk < 8 { ref = ref + dd[kk]*c; kk = kk+1 }; ref = ref & 255 50 let L: i64 = mulchain_find(c, 8, op, a, b) 51 let blen: i64 = se_emit_full(op, a, b, L, dd, 8, buf, icnt) 52 let fd: i64 = sys_openat_wr("/tmp/ov.s" as *u8, 0x1a4); if fd >= 0 { sys_write(fd, buf, blen); sys_close(fd) } 53 if eng_link("/tmp/ov.s" as *u8, "/tmp/ov.elf" as *u8) == 0 { if eng_run("/tmp/ov.elf" as *u8, 0 as *u8) == ref { return 1 } } 54 return 0 55} 56 57func ov_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 58func ov_puts(s: *u8) -> i64 { sys_write(1, s, ov_slen(s)); return 0 } 59func ov_pn(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=48+(m%10); m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(1, bb, k); return 0 } 60 61// build a journal line, echo to stdout AND append to the persistent log for next-day review. 62func ov_jline(buf: *u8, s: *u8) -> i64 { var n: i64 = 0; var oi: i64 = 0; while buf[oi] != (0 as u8) { oi = oi + 1 } while s[n] != (0 as u8) { buf[oi] = s[n]; oi = oi + 1; n = n + 1 } buf[oi] = 0 as u8; return 0 } 63func ov_jnum(buf: *u8, v: i64) -> i64 { var oi: i64 = 0; while buf[oi] != (0 as u8) { oi = oi + 1 } let t: *u8 = sys_mmap(28); var m: i64=v; var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=48+(m%10); m=m/10; k=k+1}; var z: i64=0; while z<k {buf[oi]=t[k-1-z]; oi=oi+1; z=z+1} buf[oi]=0 as u8; return 0 } 64 65func ov_jrec(beat: i64, lo: i64, hi: i64, chainwin: i64, imulwin: i64, vok: i64) -> i64 { 66 let buf: *u8 = sys_mmap(256); buf[0] = 0 as u8 67 ov_jline(buf, " BEAT " as *u8); ov_jnum(buf, beat) 68 ov_jline(buf, " range[" as *u8); ov_jnum(buf, lo); ov_jline(buf, "," as *u8); ov_jnum(buf, hi); ov_jline(buf, ")" as *u8) 69 ov_jline(buf, " chain-win(cost<imul)=" as *u8); ov_jnum(buf, chainwin) 70 ov_jline(buf, " imul-win=" as *u8); ov_jnum(buf, imulwin) 71 if vok == 1 { ov_jline(buf, " verify=OK\n" as *u8) } else { ov_jline(buf, " verify=REGRESSION!\n" as *u8) } 72 let ln: i64 = ov_slen(buf) 73 sys_write(1, buf, ln) // live 74 let lf: i64 = sys_openat_append("/tmp/nishi_overnight.log" as *u8, 0x1a4) 75 if lf >= 0 { sys_write(lf, buf, ln); sys_close(lf) } // persistent journal 76 return 0 77} 78 79// ESCALATION: the autonomous team raises what it CANNOT resolve itself to a review queue, 80// for the operator (+ Claude) to look at the NEXT DAY. Confident cases it handles silently; 81// uncertainties it flags here with type + context + the decision it could not make alone. 82func ov_esc(typ: *u8, detail: *u8, a: i64, b: i64) -> i64 { 83 let buf: *u8 = sys_mmap(512); buf[0] = 0 as u8 84 ov_jline(buf, " [ESCALATE/" as *u8); ov_jline(buf, typ); ov_jline(buf, "] " as *u8); ov_jline(buf, detail) 85 ov_jline(buf, " (item=" as *u8); ov_jnum(buf, a); ov_jline(buf, " count=" as *u8); ov_jnum(buf, b); ov_jline(buf, ")\n" as *u8) 86 let ln: i64 = ov_slen(buf) 87 sys_write(1, buf, ln) 88 let ef: i64 = sys_openat_append("/tmp/nishi_escalations.log" as *u8, 0x1a4) 89 if ef >= 0 { sys_write(ef, buf, ln); sys_close(ef) } 90 return 0 91} 92 93func ov_main(r: *i64) -> i64 { 94 ov_puts("=== NISHI OVERNIGHT RUN: mine the multiply space in chunks, journal each beat, bounded ===\n" as *u8) 95 let BEATS: i64 = K_MAGIC_20000 // an all-night sweep of the multiply space (~constants 2..2.5M) 96 let CHUNK: i64 = 128 97 let K: i64 = 8 98 let bank: *u8 = ov_shared(CHUNK) 99 var allok: i64 = 1 100 var critical: i64 = 0 // a self-check failure -> the team halts itself for recompile-retry 101 var beat: i64 = 0 102 while beat < BEATS { 103 let lo: i64 = 2 + beat * CHUNK 104 let hi: i64 = lo + CHUNK 105 ov_ingest(lo, hi, K, bank) 106 // tally the COST-OPTIMAL decisions: chain-win (bank>=1 = a verified chain cheaper-or-tied 107 // vs imul) vs imul-win (bank==0). The old "expensive -> GAP escalation" is GONE: the team 108 // RESOLVED that question itself (deepen the search + cost-select), so each constant now has 109 // a settled cost decision, not an open dilemma. (Composes the Kolmogorov/Levin/Thurber work.) 110 var chainwin: i64 = 0; var imulwin: i64 = 0; var i: i64 = 0 111 while i < CHUNK { if bank[i] >= (1 as u8) { chainwin = chainwin + 1 } else { imulwin = imulwin + 1 } i = i + 1 } 112 let vok: i64 = ov_verify(lo + 7) // exec-verify a sample from the chunk 113 if vok == 0 { allok = 0 } 114 ov_jrec(beat, lo, hi, chainwin, imulwin, vok) 115 116 // AUTONOMOUS escalation -- the team now flags ONLY genuine CRITICAL failures it cannot 117 // resolve alone (a correctness regression). The cost GAP is no longer escalated: it is a 118 // settled, cost-selected decision per constant. Review = just real regressions. 119 if vok == 0 { ov_esc("CRITICAL" as *u8, "correctness REGRESSION: an emitted multiply ran WRONG -- needs a human look at codegen" as *u8, lo + 7, 0) } 120 // CANARY self-check (the Engineer, every beat): a KNOWN answer the team can recompute -- 121 // x*45 must be a verified 2-op chain. If it isn't, this build is broken (miscompiled); 122 // the team HALTS ITSELF so the supervisor recompile-retries + relaunches -- not fire-and-forget. 123 let cop: *i64 = sys_mmap(8*12) as *i64; let cca: *i64 = sys_mmap(8*12) as *i64; let ccb: *i64 = sys_mmap(8*12) as *i64 124 if mulchain_verified(45, 4, cop, cca, ccb) != 2 { 125 ov_esc("CRITICAL" as *u8, "self-check FAILED: the team could not recompute x*45 -- this build is broken; HALTING for recompile-retry + relaunch" as *u8, 45, beat) 126 critical = 1 127 beat = BEATS 128 } 129 beat = beat + 1 130 } 131 ov_puts("----------------------------------------------------------------\n" as *u8) 132 if critical == 1 { ov_puts(" HALTED by self-check -- the team caught a broken build and stopped itself (exit 2 -> supervisor recompile-retries).\n" as *u8) } 133 else { ov_puts(" autonomous run complete. journal=/tmp/nishi_overnight.log ESCALATIONS=/tmp/nishi_escalations.log\n" as *u8) } 134 ov_puts(" next-day review = ONLY the escalation queue (what the team could not resolve alone); the rest it handled itself.\n" as *u8) 135 r[0] = allok 136 r[1] = critical 137 return 1 138} 139 140func main() -> i64 { 141 let r: *i64 = sys_mmap(8 * 4) as *i64 142 r[1] = 0 143 ov_main(r) 144 var ec: i64 = 0 145 if r[0] != 1 { ec = 1 } 146 if r[1] == 1 { ec = 2 } // CRITICAL self-check failure -> tell the supervisor to recompile-retry 147 sys_exit(ec) 148 return ec 149}