code wiki / _hdl_build / nx_ale_rsi_tick.nx

nx_ale_rsi_tick.nx source

↩ module page · 91 lines · 4956 B

1// nx_ale_rsi_tick.nx -- ALE-R5 autonomy CAPSTONE: ONE RSI TICK in a single pulse-ready no-arg 2// invocation. Composes the measure->ledger->file loop: (a) TRUNCATE the gap ledger (fresh per tick 3// -> no bloat across beats), (b) fork/exec _offc/nx_ale_flywheel.elf on the SEED manifest 4// (knowledge/specs/ale_suite.manifest) to MEASURE every task + ledger any GAPs, (c) fork/exec 5// _offc/nx_ale_gap_filer.elf to turn ledger gaps into NOVEL ALE-GAP-* queue rungs (idempotent, 6// lock-protected inside the filer). All-solved -> 0 gaps -> 0 rungs = a SAFE no-op every beat; a new 7// unsolved task -> gap -> rung -> the team builds the missing capability -> re-attempt closes. This 8// is the unit the pulse (or operator) invokes to make the RSI loop spin UNATTENDED. 9// no-arg => LIVE tick (filer writes the real queue). argv[1] => DRY (filer targets that scratch 10// queue; live queue untouched) -- for the gate/demo. 11// BAKED SELF-TEST FIRST: a DRY tick on an all-SOLVED control manifest + scratch queue must run both 12// children to exit 0 (the compose works, 0 gaps -> no ledger pollution); mismatch -> RED + exit. 13// license_tier: ORIGINAL 14// 15// module: nishi-core.ale.rsi_tick 16// depends: nishi-core.sys.syscalls 17// capability: ALE_RSI_TICK 18import "nx_syscalls.nx" 19 20func rt_w(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 } 21func rt_wn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(fd, "-" as *u8, 1) }; 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)) as u8; m = m / 10; k = k + 1 }; var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }; sys_write(fd, bb, k); return 0 } 22func rt_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 23func rt_unlink(path: *u8) -> i64 { return __syscall(263, AT_FDCWD, path, 0, 0, 0, 0) } 24 25func rt_write_file(path: *u8, s: *u8) -> i64 { 26 rt_unlink(path) 27 let fd: i64 = sys_openat_wr(path, 0x1a4) 28 if fd < 0 { return 0 } 29 sys_write(fd, s, rt_len(s)) 30 sys_close(fd) 31 return 1 32} 33 34// fork/exec elf with optional single arg (arg1==0 -> no arg); silenced child IO; return exit code. 35func rt_run(elf: *u8, arg1: i64) -> i64 { 36 let pid: i64 = sys_fork() 37 if pid == 0 { 38 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 39 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) } 40 let argv: *i64 = sys_mmap(32) as *i64 41 argv[0] = elf as i64 42 if arg1 == 0 { argv[1] = 0 } else { argv[1] = arg1; argv[2] = 0 } 43 let envp: *i64 = sys_mmap(16) as *i64 44 envp[0] = 0 45 sys_execve(elf, argv, envp) 46 sys_exit(127) 47 } 48 let st: *i64 = sys_mmap(16) as *i64 49 sys_wait4(pid, st, 0) 50 return (st[0] >> 8) & 0xff 51} 52 53// one tick: truncate ledger -> flywheel(manifest) -> filer(qarg). returns 0 iff both children exit 0. 54func rt_tick(manifest: i64, qarg: i64) -> i64 { 55 rt_unlink("knowledge/status/ale_gaps.tsv" as *u8) 56 let rcf: i64 = rt_run("_offc/nx_ale_flywheel.elf" as *u8, manifest) 57 let rcg: i64 = rt_run("_offc/nx_ale_gap_filer.elf" as *u8, qarg) 58 if rcf == 0 { if rcg == 0 { return 0 } } 59 return 1 60} 61 62func main(argc: i64, argv: *i64) -> i64 { 63 // SELF-TEST: a DRY tick on an all-SOLVED control manifest + scratch queue (0 gaps -> no 64 // pollution; both children must exit 0). 65 rt_write_file("/tmp/_rt_ctl.manifest" as *u8, "knowledge/specs/ale_examples/task_count.txt\n" as *u8) 66 rt_write_file("/tmp/_rt_ctl_q.tsv" as *u8, "# scratch queue head\n" as *u8) 67 let st_rc: i64 = rt_tick("/tmp/_rt_ctl.manifest" as *u8 as i64, "/tmp/_rt_ctl_q.tsv" as *u8 as i64) 68 var st_ok: i64 = 0 69 if st_rc == 0 { st_ok = 1 } 70 rt_w(1, "SELFTEST dry_tick_ok=" as *u8); rt_wn(1, st_ok) 71 if st_ok == 0 { rt_w(1, " verdict=RED (compose failed; live UNTOUCHED)\n" as *u8); sys_exit(7); return 7 } 72 rt_w(1, " verdict=GREEN\n" as *u8) 73 74 // REAL tick on the seed manifest. no-arg => filer LIVE; argv[1] => filer DRY (that scratch queue). 75 var qarg: i64 = 0 76 var live: i64 = 1 77 if argc >= 2 { qarg = argv[1]; live = 0 } 78 let rc: i64 = rt_tick("knowledge/specs/ale_suite.manifest" as *u8 as i64, qarg) 79 var ok: i64 = 0 80 if rc == 0 { ok = 1 } 81 rt_w(1, "RSITICK manifest=ale_suite tick_ok=" as *u8); rt_wn(1, ok) 82 if live == 1 { rt_w(1, " mode=LIVE" as *u8) } else { rt_w(1, " mode=DRYRUN" as *u8) } 83 if ok == 1 { rt_w(1, " verdict=GREEN\n" as *u8) } else { rt_w(1, " verdict=RED\n" as *u8) } 84 if live == 1 { 85 let lf: i64 = sys_openat_append("knowledge/status/ale_rsi_tick.log" as *u8, 0x1a4) 86 if lf >= 0 { rt_w(lf, "RSITICK tick_ok=" as *u8); rt_wn(lf, ok); rt_w(lf, " verdict=GREEN epoch=" as *u8); rt_wn(lf, sys_now_realtime_sec()); rt_w(lf, "\n" as *u8); sys_close(lf) } 87 } 88 if ok == 1 { sys_exit(0); return 0 } 89 sys_exit(1) 90 return 1 91}