code wiki / _hdl_build / nx_team_solve.nx

nx_team_solve.nx source

↩ module page · 227 lines · 10265 B

1// nx_team_solve.nx -- the MULTI-MEMBER gap solver: how the whole Nishi TEAM (not just the LLM) 2// grows capabilities. When the practice loop (flywheel) hits a GAP (a task scoring <1000), the team 3// solves it by routing to the right MEMBER, cheapest/most-automatic first: 4// TIER-1 MECHANISTIC-SEARCH (this organ, automatic, NO LLM): search the team's EXISTING capability 5// vocabulary (the run_organ transforms, read DATA-DRIVEN from ale_exec.spec) for one that already 6// solves the gap -- the team often HAS the capability and just needs to FIND it. Scores 1000 -> 7// SOLVED, no LLM, no new build. 8// TIER-2 EVOLUTIONARY (nx_evolve / GP -- a heavier search member): for gaps whose solution is a 9// COMPOSITION/parameterization beyond a single existing transform -- routed (ROUTE=EVO). 10// TIER-3 LLM (one team member, last resort): NOVEL capabilities needing reasoning/design -- routed 11// (ROUTE=LLM); the gap_filer already files an ALE-GAP rung the LLM member (or a future organ) 12// picks up. The LLM is NOT the default -- it is what the automatic members escalate to. 13// So the team is self-sufficient for everything its existing + searchable capabilities cover, and 14// only reaches for the LLM on the genuinely-novel frontier. Reuses the harness (DRY). 15// args mode: nx_team_solve <task> -> try to solve; print TEAMSOLVE solved_by / route. 16// no-arg: SELF-GATE -- a gap solvable by an EXISTING transform (wrong/blank instruction) is SOLVED 17// by tier-1 search; a gap needing a NEW capability ESCALATEs -> TEAMSOLVE-GATE verdict=GREEN. 18// Landmines: nested ifs (no &&/||), <=6 args/func, no empty-string literal. license_tier: ORIGINAL 19// 20// module: nishi-core.team.solve 21// depends: nishi-core.sys.syscalls 22// capability: MULTI_MEMBER_GAP_SOLVE 23import "nx_syscalls.nx" 24const TS_MAGIC_131072: i64 = 131072 25const TS_MAGIC_262144: i64 = 262144 26 27const TS_MAXV: i64 = 32 28 29func ts_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 30func ts_fp(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 31func ts_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 32func ts_unlink(path: *u8) -> i64 { return __syscall(263, AT_FDCWD, path, 0, 0, 0, 0) } 33 34func ts_read(path: *u8, buf: *u8, cap: i64) -> i64 { 35 let fd: i64 = sys_openat_rd(path) 36 if fd < 0 { return 0 } 37 var n: i64 = 0 38 var go: i64 = 1 39 while go == 1 { 40 let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - 1 - n) 41 if r <= 0 { go = 0 } else { n = n + r } 42 if n >= cap - 1 { go = 0 } 43 } 44 sys_close(fd) 45 return n 46} 47 48func ts_score_of(path: *u8) -> i64 { 49 let sb: *u8 = sys_mmap(64) 50 let n: i64 = ts_read(path, sb, 64) 51 if n <= 0 { return 0 - 1 } 52 var v: i64 = 0 53 var i: i64 = 0 54 while i < n { if sb[i] >= (48 as u8) { if sb[i] <= (57 as u8) { v = v * 10 + (sb[i] - 48) } } i = i + 1 } 55 return v 56} 57 58func ts_write_buf(path: *u8, buf: *u8, n: i64) -> i64 { 59 ts_unlink(path) 60 let fd: i64 = sys_openat_wr(path, 0x1a4) 61 if fd < 0 { return 0 } 62 if n > 0 { sys_write(fd, buf, n) } 63 sys_close(fd) 64 return 1 65} 66 67func ts_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64 = off; var i: i64 = 0; while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } return o } 68 69func ts_at(buf: *u8, ls: i64, le: i64, pat: *u8, pl: i64) -> i64 { 70 if ls + pl > le { return 0 } 71 var k: i64 = 0 72 while k < pl { if buf[ls+k] != pat[k] { return 0 } k = k + 1 } 73 return 1 74} 75 76// DATA-DRIVEN: read the run_organ transform vocabulary tokens from ale_exec.spec ("vocab|<tok>|<id>|.."). 77// fills toks[] with NUL-terminated token pointers; returns count. (So adding a transform auto-extends 78// the team's search space -- no hardcoded vocab.) 79func ts_vocab(toks: *i64) -> i64 { 80 let buf: *u8 = sys_mmap(TS_MAGIC_131072) 81 let n: i64 = ts_read("knowledge/specs/ale_exec.spec" as *u8, buf, TS_MAGIC_131072) 82 var cnt: i64 = 0 83 var ls: i64 = 0 84 var i: i64 = 0 85 while i <= n { 86 var eol: i64 = 0 87 if i == n { eol = 1 } else { if buf[i] == (10 as u8) { eol = 1 } } 88 if eol == 1 { 89 if ts_at(buf, ls, i, "vocab|" as *u8, 6) == 1 { 90 if cnt < TS_MAXV { 91 let tok: *u8 = sys_mmap(64) 92 var k: i64 = 0 93 var p: i64 = ls + 6 94 var g: i64 = 1 95 while g == 1 { 96 if p >= i { g = 0 } else { if buf[p] == (124 as u8) { g = 0 } else { if k < 63 { tok[k] = buf[p]; k = k + 1 } p = p + 1 } } 97 } 98 tok[k] = 0 as u8 99 if k > 0 { toks[cnt] = tok as i64; cnt = cnt + 1 } 100 } 101 } 102 ls = i + 1 103 } 104 i = i + 1 105 } 106 return cnt 107} 108 109// write a VARIANT of the gap task with task|instruction| swapped to lead with <transform>. 110func ts_variant(tbuf: *u8, tn: i64, transform: *u8, outpath: *u8) -> i64 { 111 let ob: *u8 = sys_mmap(TS_MAGIC_262144) 112 var o: i64 = 0 113 var ls: i64 = 0 114 var i: i64 = 0 115 while i <= tn { 116 var eol: i64 = 0 117 if i == tn { eol = 1 } else { if tbuf[i] == (10 as u8) { eol = 1 } } 118 if eol == 1 { 119 if ts_at(tbuf, ls, i, "task|instruction|" as *u8, 16) == 1 { 120 o = ts_cat(ob, o, "task|instruction|" as *u8) 121 o = ts_cat(ob, o, transform) 122 o = ts_cat(ob, o, " team-solve search variant" as *u8) 123 ob[o] = 10 as u8; o = o + 1 124 } else { 125 var p: i64 = ls 126 while p < i { ob[o] = tbuf[p]; o = o + 1; p = p + 1 } 127 if i < tn { ob[o] = 10 as u8; o = o + 1 } 128 } 129 ls = i + 1 130 } 131 i = i + 1 132 } 133 ts_write_buf(outpath, ob, o) 134 return o 135} 136 137// run the harness on a variant task; return milli-score (-1 on fail). 138func ts_run(variantpath: *u8) -> i64 { 139 let sc: *u8 = "/tmp/_ts_score" as *u8 140 ts_unlink(sc) 141 let pid: i64 = sys_fork() 142 if pid == 0 { 143 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 144 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) } 145 let argv: *i64 = sys_mmap(48) as *i64 146 argv[0] = "_offc/nx_ale_harness.elf" as *u8 as i64 147 argv[1] = variantpath as i64 148 argv[2] = "/tmp/_ts_sb" as *u8 as i64 149 argv[3] = "/tmp/_ts_out" as *u8 as i64 150 argv[4] = sc as i64 151 argv[5] = 0 152 let envp: *i64 = sys_mmap(16) as *i64 153 envp[0] = 0 154 sys_execve("_offc/nx_ale_harness.elf" as *u8, argv, envp) 155 sys_exit(127) 156 } 157 let st: *i64 = sys_mmap(16) as *i64 158 sys_wait4(pid, st, 0) 159 return ts_score_of(sc) 160} 161 162// TIER-1 mechanistic search: try every existing transform on the gap task; return the SOLVING token 163// (NUL-term in *solved) and 1, or 0 if none of the team's existing capabilities solve it. 164func ts_search(taskpath: *u8, solved: *u8) -> i64 { 165 sys_mkdir("/tmp/_ts_sb" as *u8, 0x1ed); sys_mkdir("/tmp/_ts_out" as *u8, 0x1ed) 166 let tbuf: *u8 = sys_mmap(TS_MAGIC_262144) 167 let tn: i64 = ts_read(taskpath, tbuf, TS_MAGIC_262144) 168 if tn <= 0 { return 0 } 169 let toks: *i64 = sys_mmap(8 * TS_MAXV) as *i64 170 let vn: i64 = ts_vocab(toks) 171 var i: i64 = 0 172 while i < vn { 173 let tk: *u8 = toks[i] as *u8 174 ts_variant(tbuf, tn, tk, "/tmp/_ts_variant.txt" as *u8) 175 let score: i64 = ts_run("/tmp/_ts_variant.txt" as *u8) 176 if score == 1000 { 177 var c: i64 = 0 178 while tk[c] != (0 as u8) { solved[c] = tk[c]; c = c + 1 } 179 solved[c] = 0 as u8 180 return 1 181 } 182 i = i + 1 183 } 184 solved[0] = 0 as u8 185 return 0 186} 187 188// solve a gap by the team: tier-1 search; on success print SOLVED+member; else route to EVO/LLM. 189func ts_solve(taskpath: *u8) -> i64 { 190 let solved: *u8 = sys_mmap(128) 191 let hit: i64 = ts_search(taskpath, solved) 192 if hit == 1 { 193 ts_p("TEAMSOLVE task=" as *u8); ts_p(taskpath); ts_p(" member=MECHANISTIC-SEARCH solved_by=" as *u8); ts_p(solved); ts_p(" route=SOLVED\n" as *u8) 194 return 1 195 } 196 ts_p("TEAMSOLVE task=" as *u8); ts_p(taskpath); ts_p(" member=none-existing route=ESCALATE next=EVO-then-LLM (novel capability)\n" as *u8) 197 return 0 198} 199 200func main(argc: i64, argv: *i64) -> i64 { 201 if argc >= 2 { 202 ts_solve(argv[1] as *u8) 203 sys_exit(0); return 0 204 } 205 ts_p("=== team-solve gate (MULTI-MEMBER: search existing capabilities before the LLM) ===\n" as *u8) 206 // POS: a gap solvable by an EXISTING transform (its instruction is unknown) -> tier-1 search SOLVES it. 207 let s1: *u8 = sys_mmap(128) 208 let pos: i64 = ts_search("knowledge/specs/ale_examples/task_searchme.txt" as *u8, s1) 209 // NEG: a gap needing a NEW capability (product, no transform) -> tier-1 search ESCALATEs (returns 0). 210 let s2: *u8 = sys_mmap(128) 211 let neg: i64 = ts_search("knowledge/specs/ale_examples/task_product.txt" as *u8, s2) 212 ts_p(" pos_search_solved=" as *u8); if pos == 1 { ts_p("1 by=" as *u8); ts_p(s1) } else { ts_p("0" as *u8) } 213 ts_p(" | neg_escalated=" as *u8); if neg == 0 { ts_p("1" as *u8) } else { ts_p("0" as *u8) } 214 ts_p("\n" as *u8) 215 var ok: i64 = 0 216 if pos == 1 { if neg == 0 { ok = 1 } } 217 let lfd: i64 = sys_openat_append("knowledge/status/team_solve.log" as *u8, 0x1a4) 218 if ok == 1 { 219 ts_p("TEAMSOLVE-GATE verdict=GREEN search_solves_existing=1 escalates_novel=1 llm_is_last_resort=1 rung=TEAM-SOLVE\n" as *u8) 220 if lfd >= 0 { ts_fp(lfd, "TEAMSOLVE-GATE verdict=GREEN solved_by=" as *u8); ts_fp(lfd, s1); ts_fp(lfd, " escalates_novel=1 epoch=" as *u8); var e: i64 = sys_now_realtime_sec(); let eb: *u8 = sys_mmap(28); var m: i64 = e; var k: i64 = 0; if m == 0 { eb[0] = 48; k = 1 }; while m > 0 { eb[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }; var z: i64 = 0; while z < k { sys_write(lfd, (eb as i64 + (k - 1 - z)) as *u8, 1); z = z + 1 }; ts_fp(lfd, "\n" as *u8); sys_close(lfd) } 221 sys_exit(0); return 0 222 } 223 ts_p("TEAMSOLVE-GATE verdict=RED\n" as *u8) 224 if lfd >= 0 { ts_fp(lfd, "TEAMSOLVE-GATE verdict=RED\n" as *u8); sys_close(lfd) } 225 sys_exit(1) 226 return 1 227}