nx_crew_selfheal.nx source
↩ module page · 109 lines · 4862 B
1// nx_crew_selfheal.nx -- the SAFE-ACT loop: the crew doesn't just SEE a problem,
2// it FIXES it, bounded by the Warden. The full cycle, collaborating:
3// 1. DETECT (Engineer) -- measure the compiler-health car (nx_run_timeout)
4// 2. AUTHORIZE(Warden) -- warden_authorize() against the cardinals
5// 3. HEAL (Doctor) -- swap the pinned known-good over the poisoned binary
6// 4. VERIFY (Engineer) -- re-measure; the fix is real or it isn't
7//
8// This is the literal mechanism of a one-step autonomous fix -- and it's SAFE
9// because step 2 gates step 3. Proven on a SANDBOX copy of the compiler (no
10// collision with the live shared binary a concurrent session is improving);
11// pointing it at the live compiler is one const + the operator's call.
12//
13// Composes nx_run_timeout (measure) + nx_warden_lib (authorize). Sovereign.
14// license_tier: ORIGINAL
15
16import "nx_run_timeout.nx"
17import "nx_warden_lib.nx"
18
19const SH_KG: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_cc_known_good.elf"
20const SH_SANDBOX: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/_sandbox_cc.elf"
21const SH_CANARY: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_slice_contour.nx"
22const SH_TIMEOUT_MS: i64 = 8000
23const SH_MODE_EXEC: i64 = 493
24
25func sh_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } sys_write(1, s, n); return 0 }
26func sh_putn(n: i64) -> i64 {
27 if n == 0 { sys_write(1, "0" as *u8, 1); return 0 }
28 var m: i64 = n
29 let d: *u8 = sys_mmap(24); var k: i64 = 0
30 while m > 0 { d[k] = (0x30 + (m % 10)) as u8; m = m / 10; k = k + 1 }
31 var j: i64 = k - 1
32 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
33 return 0
34}
35
36func sh_measure(compiler: *u8, elapsed_out: *i64) -> i64 {
37 let argv: *i64 = sys_mmap(32) as *i64
38 argv[0] = compiler as i64; argv[1] = SH_CANARY as i64; argv[2] = 0
39 let envp: *i64 = sys_mmap(8) as *i64; envp[0] = 0
40 return nx_run_timeout(compiler, argv, envp, SH_TIMEOUT_MS, elapsed_out)
41}
42
43func sh_write_all(fd: i64, buf: *u8, len: i64) -> i64 {
44 var off: i64 = 0
45 while off < len {
46 let w: i64 = sys_write(fd, ((buf as i64) + off) as *u8, len - off)
47 if w <= 0 { return 0 - 1 }
48 off = off + w
49 }
50 return 0
51}
52
53// heal: sovereign copy `from` -> `to`. returns bytes written (>0) or negative.
54func sh_copy(from: *u8, to: *u8) -> i64 {
55 let lenbox: *i64 = sys_mmap(16) as *i64; lenbox[0] = 0
56 let data: *u8 = sys_read_file(from, lenbox)
57 if (data as i64) == 0 { return 0 - 1 }
58 let n: i64 = lenbox[0]
59 if n <= 0 { return 0 - 2 }
60 let fd: i64 = sys_openat_wr(to, SH_MODE_EXEC)
61 if fd < 0 { return 0 - 3 }
62 let wr: i64 = sh_write_all(fd, data, n)
63 sys_close(fd)
64 if wr != 0 { return 0 - 4 }
65 return n
66}
67
68func main() -> i64 {
69 sh_puts("NISHI CREW SELF-HEAL -- the safe-act loop (detect -> authorize -> heal -> verify)\n")
70 sh_puts("================================================================================\n")
71 sh_puts("target = sandbox copy of the compiler (no collision with the live shared binary)\n\n")
72 let elb: *i64 = sys_mmap(8) as *i64; elb[0] = 0
73
74 // 1. DETECT (Engineer)
75 let before: i64 = sh_measure(SH_SANDBOX, elb)
76 sh_puts(" 1. detect (Engineer): sandbox on canary -> ")
77 if before == NX_RT_TIMEOUT { sh_puts("POISON ") } else { if before == 0 { sh_puts("OK ") } else { sh_puts("err ") } }
78 sh_putn(elb[0]); sh_puts("ms\n")
79
80 // 2. AUTHORIZE (Warden)
81 sh_puts(" 2. warden (Warden) : HEAL_SWAP sandbox, backup=known-good exists -> ")
82 let auth: i64 = warden_authorize(W_HEAL_SWAP, SH_SANDBOX, 1)
83 if auth != W_ALLOW { sh_puts("DENY -- abort (autonomy refused by the cardinals)\n"); return 2 }
84 sh_puts("ALLOW\n")
85
86 // 3. HEAL (Doctor)
87 sh_puts(" 3. heal (Doctor) : swap known-good -> sandbox ... ")
88 let h: i64 = sh_copy(SH_KG, SH_SANDBOX)
89 if h <= 0 { sh_puts("FAILED rc="); sh_putn(0 - h); sh_puts("\n"); return 3 }
90 sh_puts("wrote "); sh_putn(h); sh_puts(" bytes\n")
91
92 // 4. VERIFY (Engineer)
93 elb[0] = 0
94 let after: i64 = sh_measure(SH_SANDBOX, elb)
95 sh_puts(" 4. verify (Engineer): sandbox on canary -> ")
96 if after == 0 { sh_puts("OK "); sh_putn(elb[0]); sh_puts("ms\n") } else { sh_puts("STILL NOT HEALTHY\n"); return 4 }
97
98 sh_puts("\n")
99 if before != 0 {
100 if after == 0 {
101 sh_puts(" AUTONOMOUS SAFE-FIX PROVEN 1:1: POISON -> [Warden ALLOW] -> heal -> OK.\n")
102 sh_puts(" Engineer detected, Warden bounded, Doctor healed, Engineer verified -- the crew fixed itself.\n")
103 sh_puts(" This is the <10s loop: a healthy compiler makes the compiler-health beat sub-second.\n")
104 return 0
105 }
106 }
107 sh_puts(" (sandbox was already healthy, or the heal did not flip the verdict -- see steps above.)\n")
108 return 0
109}