code wiki / (root) / nx_conductor_heal.nx

nx_conductor_heal.nx source

↩ module page · 74 lines · 3583 B

1// nx_conductor_heal.nx -- the Nishi CONDUCTOR, v3: the come-alive tick that 2// does NOT just verify -- it AUTO-FIXES, bounded by the Warden. 3// 4// Each beat runs the full SAFE-ACT loop (the proven nx_crew_selfheal pattern, 5// now IN the autonomic tick): Engineer DETECTs compiler-health (run the sandbox 6// compiler on a canary, hang-safe) -> if unhealthy, Warden AUTHORIZEs the heal 7// (W_HEAL_SWAP: allowed only because a known-good backup exists AND the target 8// is a regenerable /_offc/ build artifact) -> Doctor HEALs (sovereign-copy the 9// pinned known-good over the poisoned binary) -> Engineer VERIFIes (re-measure). 10// 11// Reuses nx_crew_selfheal's sh_measure/sh_copy/sh_puts (its main is stripped on 12// import) -- ONE heal mechanism, no duplication. The Warden gates step 3 with 13// step 2, so the autonomy is safe by construction (#13 additive-only: it swaps a 14// known-good over a build artifact, never deletes, never overwrites source). 15// 16// Proven by gate_nx_conductor_heal.sh, which SEEDS a poisoned sandbox so the 17// heal demonstrably fires: detect UNHEALTHY -> Warden ALLOW -> heal -> verify OK. 18// license_tier: ORIGINAL 19 20import "nx_crew_selfheal.nx" 21 22const CH_BEATS: i64 = 2 23const CH_CADENCE_MS: i64 = 300 24 25func main() -> i64 { 26 sh_puts("NISHI CONDUCTOR v3 -- come-alive tick: VERIFY + AUTO-FIX (detect -> authorize -> heal -> verify, in the loop)\n") 27 sh_puts("==========================================================================================================\n") 28 var beat: i64 = 0 29 var heals: i64 = 0 30 var final_ok: i64 = 0 31 while beat < CH_BEATS { 32 sh_puts("beat "); sh_putn(beat); sh_puts(": detect compiler-health -> ") 33 let elb: *i64 = sys_mmap(8) as *i64 34 elb[0] = 0 35 let before: i64 = sh_measure(SH_SANDBOX, elb) 36 if before == 0 { 37 sh_puts("OK "); sh_putn(elb[0]); sh_puts("ms (healthy, no action)\n") 38 final_ok = 1 39 } 40 if before != 0 { 41 sh_puts("UNHEALTHY (rc="); sh_putn(before); sh_puts(") -> warden ") 42 let auth: i64 = warden_authorize(W_HEAL_SWAP, SH_SANDBOX, 1) 43 if auth != W_ALLOW { 44 sh_puts("DENY (autonomy refused by the cardinals)\n") 45 final_ok = 0 46 } 47 if auth == W_ALLOW { 48 sh_puts("ALLOW -> heal ") 49 let h: i64 = sh_copy(SH_KG, SH_SANDBOX) 50 if h <= 0 { sh_puts("COPY-FAILED\n"); final_ok = 0 } 51 if h > 0 { 52 sh_puts("("); sh_putn(h); sh_puts("B) -> verify ") 53 elb[0] = 0 54 let after: i64 = sh_measure(SH_SANDBOX, elb) 55 if after == 0 { 56 sh_puts("OK "); sh_putn(elb[0]); sh_puts("ms -> HEALED\n") 57 heals = heals + 1 58 final_ok = 1 59 } 60 if after != 0 { sh_puts("STILL UNHEALTHY\n"); final_ok = 0 } 61 } 62 } 63 } 64 sys_sleep_ms(CH_CADENCE_MS) 65 beat = beat + 1 66 } 67 sh_puts("\nHEARTBEAT: come-alive tick done. heals="); sh_putn(heals) 68 sh_puts(" final-compiler-health=") 69 if final_ok == 1 { sh_puts("OK\n") } else { sh_puts("UNHEALTHY\n") } 70 sh_puts(" Engineer detected -> Warden bounded -> Doctor healed -> Engineer verified: the loop FIXED itself, in the tick.\n") 71 if heals < 1 { sys_exit(1); return 1 } // the heal must have actually fired 72 if final_ok != 1 { sys_exit(2); return 2 } // and the compiler must end healthy 73 sys_exit(0); return 0 74}