code wiki / _hdl_build / nx_doctor_import_gate.nx

nx_doctor_import_gate.nx source

↩ module page · 103 lines · 4669 B

1// nx_doctor_import_gate.nx -- THE LOCK for the import AUTO-FIX (CC-UNDEFFN OPEN->AUTO). REAL end-to-end 2// (rule 7, no mocks): writes a BAD source that calls a sys_* primitive with NO import, drives the 3// Engineer's REAL compile (capturing the diagnostic), then proves the Doctor's import-fix makes the 4// previously-failing source COMPILE. Self-cleaning (di_unlink) so it leaves no scratch in runtime/. 5// 6// G1 bad-fails : the un-imported source genuinely fails to compile (rc<0) 7// G2 extract : di_undef_fn pulls "sys_close" out of the captured "call to undefined function:" diag 8// G3 resolve : di_resolve("sys_close") -> "nx_syscalls.nx" 9// G4 heal-compiles: the healed candidate (import prepended) COMPILES exit-0 -- the auto-fix is REAL 10// G5 neg-control : di_resolve("frobnicate_made_up") -> 0 (no false fix for the unresolvable case) 11// GREEN only if G1..G5. Sources live in runtime/ (the import root, required for resolution); the .s/.err 12// go to WSL /tmp (organ-side). Evidence -> knowledge/status/doctor_import.log. license_tier: ORIGINAL 13import "nx_doctor_import.nx" 14import "nx_engineer_crash.nx" 15import "nx_syscalls.nx" 16 17const DIG_LOG: *u8 = "knowledge/status/doctor_import.log" 18const DIG_CC: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_cc_sovereign.elf" 19const DIG_BAD: *u8 = "runtime/_ki_autofix_bad.nx" 20const DIG_HEAL:*u8 = "runtime/_ki_autofix_healed.nx" 21 22func dig_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 } 23func dig_streq(a: *u8, b: *u8) -> i64 { 24 var i: i64 = 0 25 while a[i] != (0 as u8) { 26 if a[i] != b[i] { return 0 } 27 i = i + 1 28 } 29 if b[i] != (0 as u8) { return 0 } 30 return 1 31} 32func dig_row(fd: i64, name: *u8, pass: i64) -> i64 { 33 dig_w(fd, " "); dig_w(fd, name) 34 if pass == 1 { dig_w(fd, " PASS\n" as *u8) } else { dig_w(fd, " FAIL\n" as *u8) } 35 return 0 36} 37 38func main() -> i64 { 39 // ---- write the BAD source: calls sys_close (a sys_* primitive) with NO import ---- 40 let badsrc: *u8 = "func main() -> i64 {\n sys_close(0)\n return 0\n}\n" as *u8 41 let bf: i64 = sys_openat_wr(DIG_BAD, 0x1a4) 42 if bf >= 0 { sys_write(bf, badsrc, di_len(badsrc)); sys_close(bf) } 43 44 // ---- G1: compile BAD -> must FAIL, capturing the diagnostic to WSL /tmp ---- 45 let rc_bad: i64 = eng_compile_diag(DIG_CC, DIG_BAD, "/tmp/ki_af.s" as *u8, "/tmp/ki_af.err" as *u8) 46 var g1: i64 = 0 47 if rc_bad < 0 { g1 = 1 } 48 49 // ---- G2: extract the undefined function name from the captured diagnostic ---- 50 let name: *u8 = sys_mmap(128) 51 let nlen: i64 = di_undef_fn("/tmp/ki_af.err" as *u8, name) 52 var g2: i64 = 0 53 if nlen > 0 { if dig_streq(name, "sys_close" as *u8) == 1 { g2 = 1 } } 54 55 // ---- G3: resolve the name to its defining import ---- 56 let imp: *u8 = sys_mmap(128) 57 let ilen: i64 = di_resolve(name, imp) 58 var g3: i64 = 0 59 if ilen > 0 { if dig_streq(imp, "nx_syscalls.nx" as *u8) == 1 { g3 = 1 } } 60 61 // ---- G4: heal (prepend the import) and prove the candidate now COMPILES ---- 62 di_heal(DIG_BAD, DIG_HEAL, imp) 63 let rc_heal: i64 = eng_compile(DIG_CC, DIG_HEAL, "/tmp/ki_af2.s" as *u8) 64 var g4: i64 = 0 65 if rc_heal == 0 { g4 = 1 } 66 67 // ---- G5: neg-control -- an unresolvable name must NOT falsely resolve ---- 68 let imp2: *u8 = sys_mmap(128) 69 let il2: i64 = di_resolve("frobnicate_made_up" as *u8, imp2) 70 var g5: i64 = 0 71 if il2 == 0 { g5 = 1 } 72 73 // ---- self-clean: remove the scratch candidates from runtime/ (no sprawl) ---- 74 di_unlink(DIG_BAD) 75 di_unlink(DIG_HEAL) 76 77 var passes: i64 = 0 78 if g1 == 1 { passes = passes + 1 } 79 if g2 == 1 { passes = passes + 1 } 80 if g3 == 1 { passes = passes + 1 } 81 if g4 == 1 { passes = passes + 1 } 82 if g5 == 1 { passes = passes + 1 } 83 var ok: i64 = 0 84 if passes == 5 { ok = 1 } 85 86 dig_w(1, "DOCTOR-IMPORT gate (CC-UNDEFFN missing-import AUTO-FIX, end-to-end)\n" as *u8) 87 dig_row(1, "G1 bad-fails-compile " as *u8, g1) 88 dig_row(1, "G2 extract-undef-name " as *u8, g2) 89 dig_row(1, "G3 resolve-to-import " as *u8, g3) 90 dig_row(1, "G4 healed-compiles " as *u8, g4) 91 dig_row(1, "G5 neg-no-false-fix " as *u8, g5) 92 if ok == 1 { dig_w(1, "verdict=GREEN\n" as *u8) } else { dig_w(1, "verdict=RED\n" as *u8) } 93 94 let lf: i64 = sys_openat_append(DIG_LOG, 420) 95 if lf >= 0 { 96 dig_w(lf, "DOCTOR-IMPORT authored=organ " as *u8) 97 if ok == 1 { dig_w(lf, "G1..G5=PASS verdict=GREEN (missing-import auto-fix REAL)\n" as *u8) } else { dig_w(lf, "verdict=RED\n" as *u8) } 98 sys_close(lf) 99 } 100 101 if ok == 1 { return 0 } 102 return 1 103}