nx_cc_guardian.nx source
↩ module page · 148 lines · 5879 B
1// nx_cc_guardian.nx -- SOVEREIGN AUTONOMOUS compiler-health guardian.
2//
3// A Nishi role-program (like nx_doctor), NOT a Claude actor. It owns its loop:
4// ENGINEER-measures -- compile a high-register-pressure CANARY with the active
5// compiler under a deadline (nx_run_timeout). known-good
6// does it in <1s; >10s == poisoned (loop/pathological).
7// DOCTOR-heals -- if active is poisoned AND the pinned known-good is
8// healthy, autonomously swap known-good -> active
9// (sovereign file copy, raw syscalls).
10// ENGINEER-verifies -- re-measure the active; confirm it is now healthy.
11// journals -- append the verdict (the regression-guard's honest trail).
12//
13// This is the "Claude moves away from being the hands" step: the ecosystem keeps
14// its own substrate (the compiler) sound, autonomously. The durable root-fix of
15// the homing slowdown remains a Builder improvement; this is the standing
16// self-heal that keeps every Nishi project building meanwhile.
17//
18// Composes runtime/nx_run_timeout.nx (the hang-detector). Sovereign: zero deps.
19// license_tier: ORIGINAL
20
21import "nx_run_timeout.nx"
22
23const G_ACTIVE: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_compile_x86_native.elf"
24const G_KNOWN_GOOD: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_cc_known_good.elf"
25const G_CANARY: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_slice_contour.nx"
26const G_JOURNAL: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_cc_guardian_journal.tsv"
27const G_CANARY_TIMEOUT_MS: i64 = 10000 // known-good <1s; >10s = poisoned
28const G_MODE_EXEC: i64 = 493 // 0o755
29const G_MODE_FILE: i64 = 420 // 0o644
30
31func g_puts(s: *u8) -> i64 {
32 var n: i64 = 0
33 while s[n] != 0 as u8 { n = n + 1 }
34 sys_write(1, s, n)
35 return 0
36}
37func g_putn(n: i64) -> i64 {
38 if n == 0 { sys_write(1, "0" as *u8, 1); return 0 }
39 var m: i64 = n
40 let d: *u8 = sys_mmap(24)
41 var k: i64 = 0
42 while m > 0 { d[k] = (0x30 + (m % 10)) as u8; m = m / 10; k = k + 1 }
43 var j: i64 = k - 1
44 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
45 return 0
46}
47
48// append one verdict line to the guardian journal (additive only).
49func g_journal(status: *u8) -> i64 {
50 let fd: i64 = sys_openat_append(G_JOURNAL, G_MODE_FILE)
51 if fd < 0 { return 0 - 1 }
52 var n: i64 = 0
53 while status[n] != 0 as u8 { n = n + 1 }
54 sys_write(fd, status, n)
55 sys_write(fd, "\n" as *u8, 1)
56 sys_close(fd)
57 return 0
58}
59
60// ENGINEER-measure: compile the canary with `compiler` under the deadline.
61// Returns 0 (healthy), NX_RT_TIMEOUT (poisoned: loop/pathological), or err code.
62func g_measure(compiler: *u8, elapsed_out: *i64) -> i64 {
63 let argv: *i64 = sys_mmap(32) as *i64
64 argv[0] = compiler as i64
65 argv[1] = G_CANARY as i64
66 argv[2] = 0
67 let envp: *i64 = sys_mmap(8) as *i64
68 envp[0] = 0
69 return nx_run_timeout(compiler, argv, envp, G_CANARY_TIMEOUT_MS, elapsed_out)
70}
71
72// write all `len` bytes (handles partial writes).
73func g_write_all(fd: i64, buf: *u8, len: i64) -> i64 {
74 var off: i64 = 0
75 while off < len {
76 let w: i64 = sys_write(fd, ((buf as i64) + off) as *u8, len - off)
77 if w <= 0 { return 0 - 1 }
78 off = off + w
79 }
80 return 0
81}
82
83// DOCTOR-heal: sovereign copy known-good -> active. Returns bytes written (>0)
84// on success, negative on failure.
85func g_heal() -> i64 {
86 let lenbox: *i64 = sys_mmap(16) as *i64
87 lenbox[0] = 0
88 let data: *u8 = sys_read_file(G_KNOWN_GOOD, lenbox)
89 if (data as i64) == 0 { return 0 - 1 }
90 let n: i64 = lenbox[0]
91 if n <= 0 { return 0 - 2 }
92 let fd: i64 = sys_openat_wr(G_ACTIVE, G_MODE_EXEC)
93 if fd < 0 { return 0 - 3 } // e.g. ETXTBSY if active is mid-execution
94 let wr: i64 = g_write_all(fd, data, n)
95 sys_close(fd)
96 if wr != 0 { return 0 - 4 }
97 return n
98}
99
100func main() -> i64 {
101 g_puts("NISHI COMPILER GUARDIAN -- autonomous Engineer-measure + Doctor-heal (sovereign)\n")
102 g_puts("================================================================================\n")
103 let elb: *i64 = sys_mmap(8) as *i64
104 elb[0] = 0
105
106 // 1. ENGINEER-measure the active compiler.
107 let ra: i64 = g_measure(G_ACTIVE, elb)
108 g_puts(" active : ")
109 if ra == 0 {
110 g_puts("[OK] healthy ("); g_putn(elb[0]); g_puts("ms) -- no action\n")
111 g_journal("HEALTHY" as *u8)
112 return 0
113 }
114 if ra == NX_RT_TIMEOUT { g_puts("[XX] POISONED -- loop/pathological (>"); g_putn(G_CANARY_TIMEOUT_MS); g_puts("ms)\n") }
115 else { g_puts("[XX] POISONED -- err("); g_putn(ra); g_puts(")\n") }
116
117 // 2. ENGINEER-measure the pinned known-good.
118 let rk: i64 = g_measure(G_KNOWN_GOOD, elb)
119 g_puts(" known-good : ")
120 if rk != 0 {
121 g_puts("[XX] ALSO poisoned -- cannot self-heal, ESCALATE to Builder\n")
122 g_journal("ESCALATE_BOTH_POISONED" as *u8)
123 return 2
124 }
125 g_puts("[OK] healthy ("); g_putn(elb[0]); g_puts("ms)\n")
126
127 // 3. DOCTOR-heal: swap known-good -> active.
128 g_puts(" heal : swapping known-good -> active ...\n")
129 let h: i64 = g_heal()
130 if h <= 0 {
131 g_puts(" heal : [XX] FAILED rc="); g_putn(0 - h); g_puts(" (active may be mid-execution)\n")
132 g_journal("HEAL_FAILED" as *u8)
133 return 3
134 }
135 g_puts(" heal : wrote "); g_putn(h); g_puts(" bytes\n")
136
137 // 4. ENGINEER-verify the heal took.
138 let rv: i64 = g_measure(G_ACTIVE, elb)
139 g_puts(" verify : ")
140 if rv == 0 {
141 g_puts("[OK] active now healthy ("); g_putn(elb[0]); g_puts("ms) -- PROJECTS UNBLOCKED\n")
142 g_journal("HEALED" as *u8)
143 return 0
144 }
145 g_puts("[XX] heal did not take (active still poisoned)\n")
146 g_journal("HEAL_DID_NOT_TAKE" as *u8)
147 return 4
148}