nx_cc_health.nx source
↩ module page · 79 lines · 4781 B
1// nx_cc_health.nx -- SELF-HEALING COMPILER HEALTH WITNESS (the IMMUNIZE step + a machine-target the autonomy
2// engine drives). Runs the generate->build->run pipeline on a KNOWN-GOOD codegen shape (1000 distinct functions,
3// via nx_shapeprobe) and exits 0 IFF nx_cc still compiles it AND the program runs correct (prints FALLTHRU_OK).
4// Exits 1 (RED) if nx_cc regresses below that scale -- the permanent regression witness the fixed 10-row corpus
5// lacked. Because it's a clean exec-with-verified-exit, nx_exec_engine can drive it autonomously = the self-
6// healing compiler loop (generate -> detect -> witness) as an AUTONOMOUS capability. Composes _offc organs;
7// no Claude-touch of nx_cc (shared compiler = max blast, Engineer-owned). license_tier: ORIGINAL expect_exit: 0
8import "nx_syscalls.nx"
9const H_MAGIC_1048576: i64 = 1048576
10
11const H_SHAPEPROBE: *u8 = "_offc/nx_shapeprobe.elf\x00"
12const H_BUILDRUN: *u8 = "_offc/nx_sov_build_run.elf\x00"
13const H_CAND: *u8 = "_offc/nx_shapeprobe_cand.elf\x00"
14const H_BUILDOUT: *u8 = "/tmp/cch_build\x00"
15const H_RUNOUT: *u8 = "/tmp/cch_run\x00"
16
17func h_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func h_putn(v: i64) -> i64 { let t: *u8=sys_mmap(24); var m: i64=v; var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let o: *u8=sys_mmap(24); var i: i64=0; while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
19
20// fork+exec path with argv; stdout+stderr -> outpath (or /dev/null if outpath is 0). returns WEXITSTATUS / 128+sig.
21func run_argv(path: *u8, argv: *i64, outpath: *u8) -> i64 {
22 let pid: i64 = sys_fork()
23 if pid == 0 {
24 var ofd: i64 = sys_openat_wr("/dev/null\x00" as *u8, 420)
25 if (outpath as i64) != 0 { ofd = sys_openat_wr(outpath, 420) }
26 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
27 let envp: *i64 = sys_mmap(16) as *i64; envp[0]="PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1]=0
28 sys_execve(path, argv, envp); sys_exit(127)
29 }
30 let st: *i64 = sys_mmap(16) as *i64; sys_wait4(pid, st, 0)
31 let sig: i64 = st[0] & 0x7f; if sig != 0 { return 128 + sig }
32 return (st[0] >> 8) & 0xff
33}
34
35// read file at path, return 1 if it contains the NUL-terminated needle, else 0.
36func file_has(path: *u8, needle: *u8) -> i64 {
37 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 }
38 let cap: i64 = H_MAGIC_1048576
39 let buf: *u8 = sys_mmap(cap)
40 var n: i64 = 0; var go: i64 = 1
41 while go == 1 { let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r; if n >= cap { go = 0 } } }
42 sys_close(fd)
43 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
44 var i: i64 = 0
45 while i + nl <= n {
46 var j: i64 = 0; while j < nl { if buf[i+j] != needle[j] { j = nl + 1 } else { j = j + 1 } }
47 if j == nl { return 1 }
48 i = i + 1
49 }
50 return 0
51}
52
53func main() -> i64 {
54 h_puts("=== nx_cc_health -- self-healing compiler witness: known-good shape MUST compile + run ===\n" as *u8)
55 // 1. emit a known-good candidate: 1000 distinct functions (well within the measured OK range; loud limit ~5000).
56 let a_emit: *i64 = sys_mmap(64) as *i64
57 a_emit[0]=H_SHAPEPROBE as i64; a_emit[1]="1000\x00" as *u8 as i64; a_emit[2]="2\x00" as *u8 as i64; a_emit[3]=0
58 run_argv(H_SHAPEPROBE, a_emit, 0)
59 // 2. build it (--install -> _offc/nx_shapeprobe_cand.elf); capture the build log.
60 let a_build: *i64 = sys_mmap(64) as *i64
61 a_build[0]=H_BUILDRUN as i64; a_build[1]="nx_shapeprobe_cand\x00" as *u8 as i64; a_build[2]="--install\x00" as *u8 as i64; a_build[3]=0
62 run_argv(H_BUILDRUN, a_build, H_BUILDOUT)
63 let built: i64 = file_has(H_BUILDOUT, "asm=\x00" as *u8)
64 let buildfail: i64 = file_has(H_BUILDOUT, "FAIL\x00" as *u8)
65 // 3. run the candidate, capture stdout; must print FALLTHRU_OK.
66 let a_run: *i64 = sys_mmap(64) as *i64
67 a_run[0]=H_CAND as i64; a_run[1]=0
68 let rrc: i64 = run_argv(H_CAND, a_run, H_RUNOUT)
69 let ran_ok: i64 = file_has(H_RUNOUT, "FALLTHRU_OK\x00" as *u8)
70
71 h_puts(" built(asm=)="); h_putn(built); h_puts(" buildfail="); h_putn(buildfail); h_puts(" cand-exit="); h_putn(rrc); h_puts(" FALLTHRU_OK="); h_putn(ran_ok); h_puts("\n" as *u8)
72
73 if built == 1 { if buildfail == 0 { if ran_ok == 1 {
74 h_puts("VERDICT GREEN -- nx_cc compiles + correctly runs a 1000-function program (compiler healthy at this scale). exit=0\n" as *u8)
75 sys_exit(0); return 0
76 } } }
77 h_puts("VERDICT RED -- nx_cc did NOT correctly compile+run the known-good shape (a compiler REGRESSION). This is the self-healing alarm; route to the Engineer + splice_bisect. exit=1\n" as *u8)
78 sys_exit(1); return 1
79}