code wiki / _hdl_build / nx_research_grow_full.nx

nx_research_grow_full.nx source

↩ module page · 88 lines · 4591 B

1// nx_research_grow_full.nx -- ECOSYSTEM-NATIVE driver: grow the researcher index until it has caught up to the 2// whole fetched corpus, WITHOUT any shell loop (operator: no sh / no 3rd party -- the repetition itself is a Nishi 3// organ). It forks nx_research_grow as a fresh process each pass (so every pass is OOM-safe -- the toolchain has 4// no munmap, so one giant process would leak; many bounded passes do not), then reads the growth-ledger artifact 5// (loose coupling -- it consumes grow's output, never reaches into grow) to detect caught-up (last line new=0). 6// Stops at caught-up or GF_MAX passes. expect_exit: 0 license_tier: ORIGINAL 7import "nx_search_inverted_persist.nx" // brings syscalls (fork/execve/wait4/openat/read_file/write/mmap) 8import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 9 10const GF_MAX: i64 = 40 // safety cap on passes (40*50 = 2000 docs >> corpus) 11 12func gf_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 14// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 15// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 16// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 17func gf_num(v: i64) -> i64 { nxi_out(v); return 0 } 18 19// fork+exec one organ via the sovereign runner; child stdout -> /dev/null. Returns exit code. 20func gf_run(organ: *u8) -> i64 { 21 let pid: i64 = sys_fork() 22 if pid == 0 { 23 let devnull: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 24 if devnull >= 0 { sys_dup3(devnull, 1, 0) } 25 let argv: *i64 = sys_mmap(32) as *i64 26 argv[0] = "_offc/nx_sov_build_run.elf" as *u8 as i64 27 argv[1] = organ as i64 28 argv[2] = 0 29 let envp: *i64 = sys_mmap(16) as *i64 30 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64 31 envp[1] = 0 32 sys_execve("_offc/nx_sov_build_run.elf" as *u8, argv, envp) 33 sys_exit(127) 34 } 35 let st: *i64 = sys_mmap(16) as *i64 36 sys_wait4(pid, st, 0) 37 if (st[0] & 0x7f) != 0 { return 128 + (st[0] & 0x7f) } 38 return (st[0] >> 8) & 0xff 39} 40 41// read the growth ledger and return the LAST line's `unique=` value (or -1 if none). Loose coupling: consumes 42// the artifact, never touches grow's internals. We track UNIQUE (cumulative coverage) not the exit code, because 43// the runner exits 0 even when the inner organ ends abnormally -- the ledger is the truth. 44func gf_last_unique() -> i64 { 45 let box: *i64 = sys_mmap(16) as *i64 46 let buf: *u8 = sys_read_file("knowledge/index/research_growth.log" as *u8, box) 47 if buf == 0 as *u8 { return 0 - 1 } 48 let n: i64 = box[0] 49 var last: i64 = 0 - 1 50 var i: i64 = 0 51 while i + 7 <= n { 52 if buf[i]==(117 as u8) { if buf[i+1]==(110 as u8) { if buf[i+2]==(105 as u8) { if buf[i+3]==(113 as u8) { if buf[i+4]==(117 as u8) { if buf[i+5]==(101 as u8) { if buf[i+6]==(61 as u8) { 53 var p: i64 = i + 7 54 var v: i64 = 0 55 var stop: i64 = 0 56 while stop==0 { 57 if p >= n { stop=1 } 58 else { let c: i64 = buf[p] as i64; if c < 48 { stop=1 } else { if c > 57 { stop=1 } else { v=v*10+(c-48); p=p+1 } } } 59 } 60 last = v 61 } } } } } } } 62 i = i + 1 63 } 64 return last 65} 66 67func main() -> i64 { 68 gf_puts("=== nx_research_grow_full: grow until caught up (Nishi-native, no shell loop) ===\n" as *u8) 69 var pass: i64 = 0 70 var done: i64 = 0 71 var prev_u: i64 = gf_last_unique() 72 while done == 0 { 73 if pass >= GF_MAX { done = 1 } 74 else { 75 let e: i64 = gf_run("nx_research_grow" as *u8) 76 pass = pass + 1 77 let u: i64 = gf_last_unique() 78 gf_puts(" pass="); gf_num(pass); gf_puts(" runner_exit="); gf_num(e); gf_puts(" unique="); gf_num(u); gf_puts("\n" as *u8) 79 // robust termination: stop when COVERAGE stops increasing (caught up, OR grow stopped producing). 80 // Trust the ledger, not the exit code -- the runner exits 0 even if the inner organ ends abnormally. 81 if u <= prev_u { gf_puts(" NO PROGRESS (caught up or stalled) -- stopping; re-run to resume if more remains\n" as *u8); done = 1 } 82 prev_u = u 83 } 84 } 85 gf_puts("=== grow-full done: passes="); gf_num(pass); gf_puts(" ===\n" as *u8) 86 gf_puts(" GROW-FULL-OK (refresh the manifest with nx_search_register next)\n" as *u8) 87 sys_exit(0); return 0 88}