code wiki / (root) / nx_engineer.nx

nx_engineer.nx source

↩ module page · 156 lines · 8376 B

1// nx_engineer.nx -- the Nishi engineer's clear-view cockpit (sovereign, 2// bits-up). REASON TO EXIST: one-step, sub-10-second fixes. The builder is 3// the new-&-improve expert; the ENGINEER is the measure-&-fix expert -- it 4// guards what's built against silent REGRESSION, MIS-MEASUREMENT, and 5// THRASHING, keeps the qualitative+quantitative ecosystem always winning, and 6// either drives a fast fix or recommends a new capability back to the builder. 7// 8// It is the measured-ground-truth leg of the triangulation (human=algebra, 9// AI=statistics, engineer=what RUNS+PASSES) and keeps both honest. 10// 11// How: runs the racing-crew gates LIVE (the proofs), TIMES each, and DIFFS 12// each verdict against the last known-good journal -> REGRESSED / FIXED / 13// stable. A regression is the alarm; the timing is the fix-speed clock 14// (target: every fix loop one step, < 10 s). Honest by construction: 15// PROVEN only if the gate JUST ran + passed -- never a claim file. 16// 17// See docs/NISHI_TRIANGULATION_ENGINEERING.md. 18// license_tier: ORIGINAL 19 20import "nx_syscalls.nx" 21import "nx_run_timeout.nx" 22const NX_MAGIC_10000: i64 = 10000 23 24const NCARS: i64 = 7 25const NX_ENG_GATE_TIMEOUT_MS: i64 = 12000 // per-gate hang bound (nx_run_timeout); a slow/hung gate is NOT-PROVEN, never fatal 26const JOURNAL: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_engineer_journal.tsv" 27 28func e_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } sys_write(1, s, n); return 0 } 29func e_putn(n: i64) -> i64 { 30 if n == 0 { sys_write(1, "0" as *u8, 1); return 0 } 31 var m: i64 = n 32 let d: *u8 = sys_mmap(24); var k: i64 = 0 33 while m > 0 { d[k] = (0x30 + (m % 10)) as u8; m = m / 10; k = k + 1 } 34 // write-once: buffer HOISTED (was a per-char sys_mmap(1) inside the loop = mmap-in-loop leak-by-construction) 35 let o: *u8 = sys_mmap(24); var i: i64 = 0 36 while i < k { o[i] = d[k - 1 - i]; i = i + 1 } 37 sys_write(1, o, k) 38 return 0 39} 40 41// run a car's SOVEREIGN gate to its honest verdict (exit 0 == measured + passed) -- NO /bin/bash. Each car maps 42// to a sovereign gate ELF (+ up to 2 args): nx_gate_run <organ> <marker> for the single-organ cars, and the 43// nx_seng_rdtsc_gate / nx_seng_linkqual_gate / nx_sovereign_deps_gate wrappers for the multi-organ ones. 44// Migrated 2026-07-06 off the bench/gate_*.sh scripts (each proven verdict-equivalent to its .sh oracle; the 45// rdtsc car became buildable once nxasm gained x86_rdtsc). Still bounded by nx_run_timeout so a hung gate is 46// NOT-PROVEN, never stalling the cockpit. 47func run_gate(elf: *u8, a1: *u8, a2: *u8) -> i64 { 48 let argv: *i64 = sys_mmap(32) as *i64 49 argv[0] = elf as i64; var ai: i64 = 1 50 if (a1 as i64) != 0 { argv[ai] = a1 as i64; ai = ai + 1 } 51 if (a2 as i64) != 0 { argv[ai] = a2 as i64; ai = ai + 1 } 52 argv[ai] = 0 53 let envp: *i64 = sys_mmap(16) as *i64; envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 54 let ms: *i64 = sys_mmap(8) as *i64 55 return nx_run_timeout(elf, argv, envp, NX_ENG_GATE_TIMEOUT_MS, ms) 56} 57 58// previous verdicts (one per line, fixed car order). -1 = no prior record. 59func read_journal(prev: *i64, n: i64) -> i64 { 60 var i: i64 = 0 61 while i < n { prev[i] = 0 - 1; i = i + 1 } 62 let lenbox: *i64 = sys_mmap(16) as *i64 63 let data: *u8 = sys_read_file(JOURNAL, lenbox) 64 if (data as i64) == 0 { return 0 } 65 let total: i64 = lenbox[0] 66 var p: i64 = 0; var car: i64 = 0 67 while p < total { 68 if car >= n { break } 69 // verdict = first char of the line: '1' proven, '0' not. 70 let c: i64 = data[p] as i64 71 if c == 0x31 { prev[car] = 1 } else { if c == 0x30 { prev[car] = 0 } } 72 while p < total { if data[p] == 10 as u8 { break } p = p + 1 } 73 p = p + 1; car = car + 1 74 } 75 return 0 76} 77 78func write_journal(cur: *i64, n: i64) -> i64 { 79 let buf: *u8 = sys_mmap(256); var b: i64 = 0 80 var i: i64 = 0 81 while i < n { 82 if cur[i] == 1 { buf[b] = 0x31 as u8 } else { buf[b] = 0x30 as u8 } 83 b = b + 1; buf[b] = 10 as u8; b = b + 1 84 i = i + 1 85 } 86 let fd: i64 = sys_openat_wr(JOURNAL, 420) 87 if fd >= 0 { sys_write(fd, buf, b); sys_close(fd) } 88 return 0 89} 90 91func main() -> i64 { 92 e_puts("NISHI ENGINEER -- measure-&-fix cockpit (reason to exist: 1-step, <10s fixes)\n") 93 e_puts("============================================================================\n") 94 e_puts("Guards what the builder built vs REGRESSION / MIS-MEASURE / THRASH.\n") 95 e_puts("Law: hardcore proof, 1:1. PROVEN only if the gate just ran + passed.\n\n") 96 97 let prev: *i64 = sys_mmap(NCARS * 8) as *i64 98 let cur: *i64 = sys_mmap(NCARS * 8) as *i64 99 read_journal(prev, NCARS) 100 101 // car name[i] + SOVEREIGN gate spec {elf + up to 2 args}, fixed order (must match the journal). 102 let names: *i64 = sys_mmap(NCARS * 8) as *i64 103 let gelf: *i64 = sys_mmap(NCARS * 8) as *i64 104 let garg1: *i64 = sys_mmap(NCARS * 8) as *i64 105 let garg2: *i64 = sys_mmap(NCARS * 8) as *i64 106 names[0] = ("doctor (local+remote self-heal)" as *u8) as i64 107 gelf[0] = ("_offc/nx_gate_run.elf" as *u8) as i64; garg1[0] = ("nx_doctor" as *u8) as i64; garg2[0] = ("NISHI DOCTOR" as *u8) as i64 108 names[1] = ("rdtsc (cycle measurement) " as *u8) as i64 109 gelf[1] = ("_offc/nx_seng_rdtsc_gate.elf" as *u8) as i64; garg1[1] = 0; garg2[1] = 0 110 names[2] = ("sovereign-deps (0 external deps, static)" as *u8) as i64 111 gelf[2] = ("_offc/nx_sovereign_deps_gate.elf" as *u8) as i64; garg1[2] = 0; garg2[2] = 0 112 names[3] = ("linkqual (RT metrics substrate) " as *u8) as i64 113 gelf[3] = ("_offc/nx_seng_linkqual_gate.elf" as *u8) as i64; garg1[3] = 0; garg2[3] = 0 114 names[4] = ("eqsat-generator(invention GENERATOR) " as *u8) as i64 115 gelf[4] = ("_offc/nx_gate_run.elf" as *u8) as i64; garg1[4] = ("nx_eqsat_test" as *u8) as i64; garg2[4] = ("2 2 4 3 11 1 3 3 " as *u8) as i64 116 names[5] = ("nxgate-verifier(invention VERIFIER) " as *u8) as i64 117 gelf[5] = ("_offc/nx_gate_run.elf" as *u8) as i64; garg1[5] = ("nx_nxgate_sim_test" as *u8) as i64; garg2[5] = ("8 16 8 48 0 16 16 3 " as *u8) as i64 118 names[6] = ("alu-netlist (verifier on real ALU) " as *u8) as i64 119 gelf[6] = ("_offc/nx_gate_run.elf" as *u8) as i64; garg1[6] = ("nx_alu_netlist_test" as *u8) as i64; garg2[6] = ("24 24 16 16 24 5 " as *u8) as i64 120 121 var proven: i64 = 0 122 var regressed: i64 = 0 123 var fixed: i64 = 0 124 var slow: i64 = 0 125 let t_all0: i64 = sys_now_us() 126 var i: i64 = 0 127 while i < NCARS { 128 let t0: i64 = sys_now_us() 129 let rc: i64 = run_gate(gelf[i] as *u8, garg1[i] as *u8, garg2[i] as *u8) 130 let ms: i64 = (sys_now_us() - t0) / 1000 131 var v: i64 = 0 132 if rc == 0 { v = 1 } 133 cur[i] = v 134 e_puts(" "); e_puts(names[i] as *u8); e_puts(" ") 135 if v == 1 { e_puts("[PROVEN] "); proven = proven + 1 } else { e_puts("[NOT-PROVEN]") } 136 e_puts(" "); e_putn(ms); e_puts("ms") 137 // regression / fix diff vs last known-good 138 if prev[i] == 1 { if v == 0 { e_puts(" <== REGRESSED! hammer NOW (1-step fix)"); regressed = regressed + 1 } } 139 if prev[i] == 0 { if v == 1 { e_puts(" <== FIXED (was failing)"); fixed = fixed + 1 } } 140 if ms > NX_MAGIC_10000 { e_puts(" [slow feedback >10s]"); slow = slow + 1 } 141 e_puts("\n") 142 i = i + 1 143 } 144 let all_ms: i64 = (sys_now_us() - t_all0) / 1000 145 write_journal(cur, NCARS) 146 147 e_puts("\n CLEAR VIEW: "); e_putn(proven); e_puts("/"); e_putn(NCARS); e_puts(" PROVEN. ") 148 e_putn(regressed); e_puts(" regressed, "); e_putn(fixed); e_puts(" fixed since last run.\n") 149 if regressed > 0 { e_puts(" !! REGRESSION is the top hammer target -- the builder broke a proven car.\n") } 150 e_puts(" fix-speed clock: total loop "); e_putn(all_ms); e_puts("ms") 151 if slow > 0 { e_puts(" ("); e_putn(slow); e_puts(" gate(s) >10s -- speed the feedback so fixes stay 1-step)") } 152 e_puts("\n Honest by construction: PROVEN only if the gate JUST passed. Verdicts journaled\n") 153 e_puts(" to diff next run -- the regression guard. NOT-PROVEN = fix here or recommend to builder.\n") 154 if regressed > 0 { return 1 } 155 return 0 156}