nx_gate_runner.nx source
↩ module page · 294 lines · 18505 B
1// nx_gate_runner.nx -- SOVEREIGN gate runner (NishiLang, no .sh, no grep).
2//
3// The operator's cardinal: tooling surfaces are sovereign NishiLang, never shell
4// glue. The GATES are already NishiLang -- each gate-test .nx self-asserts and
5// its EXIT CODE is the verdict (0 = PROVEN). The .sh wrappers (compile + run +
6// grep) were the non-sovereign part. This replaces them: it compiles each gate
7// .nx + runs it + reads the exit code, all in NishiLang (raw syscalls). It is
8// what nx_engineer/nx_conductor should dispatch instead of bashing a .sh.
9//
10// Build oracles (as/ld) are fork+exec'd here as tolerated Wheeler oracles (being
11// replaced by nxasm/nxld); the ORCHESTRATION + VERDICT are fully sovereign.
12// Composes nx_run_timeout (hang-safe final run). license_tier: ORIGINAL
13
14import "nx_run_timeout.nx"
15import "nx_gate_discover.nx"
16
17const GR_CC: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_cc_known_good.elf"
18const GR_AS: *u8 = "/usr/bin/as"
19const GR_LD: *u8 = "/usr/bin/ld"
20const GR_S: *u8 = "/tmp/nx_gr.s"
21const GR_O: *u8 = "/tmp/nx_gr.o"
22const GR_ELF: *u8 = "/tmp/nx_gr.elf"
23const GR_RUN_TIMEOUT_MS: i64 = 60000
24const GR_MODE: i64 = 420
25
26// ===== SELF-REGISTRATION wiring (additive on top of the static floor) =========
27//
28// The runner's gate list is the UNION of (a) the hand-proven STATIC set -- the
29// unconditional FLOOR -- and (b) verified-discovered additives from _hdl_build/.
30//
31// STATIC_N is the keystone guarantee: the runner exits 0 IFF it proves at least
32// this many gates. The static set is copied into the union FIRST, so there is no
33// code path on which the runner runs fewer than STATIC_N gates. A discovered gate
34// can only ADD (after passing gr_gate); a stale/failing one is QUARANTINED.
35const STATIC_N: i64 = 37 // 25 base + 12 sovereign QUIC transport gates (R1-R7 + R8a FEC + R8b relay)
36// The directory the self-registration organ scans for *_test.nx gates.
37const GR_HDL_DIR: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build"
38// Union capacity: the static floor + the discover organ's bounded universe.
39// Bounded, no unbounded growth (NX_GD_MAX_GATES is the discover-side cap).
40const GR_UNION_CAP: i64 = STATIC_N + NX_GD_MAX_GATES // 25 + 256
41
42// rc-class boundaries for QUARANTINE forensics. gr_gate returns negative stage
43// codes for a BUILD-oracle failure (cc=-1 / as=-2 / ld=-3) -- which could be the
44// documented active-compiler miscompile class -- vs a positive nonzero exit for a
45// gate that RAN and refuted (benign for a known timing race), vs a hang.
46// Quarantine never lowers the green count, but it must LOG the class loudly so a
47// genuinely-new failing gate is a signal to fix, never silently swallowed.
48const GR_QT_BUILD_HI: i64 = 0 - 1 // -1, -2, -3 are build-stage failures (rc <= -1, >= -3)
49const GR_QT_BUILD_LO: i64 = 0 - 3
50
51func gr_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } sys_write(1, s, n); return 0 }
52func gr_putn(n: i64) -> i64 {
53 if n == 0 { sys_write(1, "0" as *u8, 1); return 0 }
54 var m: i64 = n; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
55 let d: *u8 = sys_mmap(24); var k: i64 = 0
56 while m > 0 { d[k] = (0x30 + (m % 10)) as u8; m = m / 10; k = k + 1 }
57 var j: i64 = k - 1
58 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
59 return 0
60}
61
62// fork+exec `path` with argv/envp; child stdout -> out_path if non-null else
63// /dev/null, stderr -> /dev/null. Returns the child exit code (blocking wait).
64func gr_run(path: *u8, argv: *i64, envp: *i64, out_path: *u8) -> i64 {
65 let pid: i64 = sys_fork()
66 if pid == 0 {
67 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0)
68 if (out_path as i64) != 0 {
69 let ofd: i64 = sys_openat_wr(out_path, GR_MODE)
70 if ofd >= 0 { sys_dup3(ofd, 1, 0) }
71 }
72 if (out_path as i64) == 0 { if dn >= 0 { sys_dup3(dn, 1, 0) } }
73 if dn >= 0 { sys_dup3(dn, 2, 0) }
74 sys_execve(path, argv, envp)
75 sys_exit(127)
76 }
77 let st: *i64 = sys_mmap(16) as *i64
78 sys_wait4(pid, st, 0)
79 return wait_exit_code(st[0])
80}
81
82// compile + assemble + link a gate .nx, then run it hang-safe; return the gate's
83// exit code (0 = PROVEN), or a negative stage code on a build-oracle failure.
84const GR_BUILD_RETRIES: i64 = 3
85func gr_gate(testnx: *u8) -> i64 {
86 let envp: *i64 = sys_mmap(8) as *i64; envp[0] = 0
87 let a1: *i64 = sys_mmap(32) as *i64
88 a1[0] = GR_CC as i64; a1[1] = testnx as i64; a1[2] = 0
89 let a2: *i64 = sys_mmap(40) as *i64
90 a2[0] = GR_AS as i64; a2[1] = GR_S as i64; a2[2] = ("-o" as *u8) as i64; a2[3] = GR_O as i64; a2[4] = 0
91 let a3: *i64 = sys_mmap(40) as *i64
92 a3[0] = GR_LD as i64; a3[1] = ("-o" as *u8) as i64; a3[2] = GR_ELF as i64; a3[3] = GR_O as i64; a3[4] = 0
93 // Build (cc -> as -> ld) with RETRY. Under the self-registering runner's heavy fork
94 // load (verify-before-enroll + main run = ~46 compiles), the as/ld build oracles
95 // transiently fail (rc -2/-3) on random gates -- an ORACLE hiccup, not a gate
96 // failure (each gate is deterministic when built alone). Retry the build up to
97 // GR_BUILD_RETRIES before declaring a build-stage failure; the gate's RUN exit code
98 // remains the sole verdict. A genuine compile error fails all retries -> returns the stage code.
99 var br: i64 = 0
100 var attempt: i64 = 0
101 while attempt < GR_BUILD_RETRIES {
102 br = 0
103 if gr_run(GR_CC, a1, envp, GR_S) != 0 { br = 0 - 1 }
104 if br == 0 { if gr_run(GR_AS, a2, envp, 0 as *u8) != 0 { br = 0 - 2 } }
105 if br == 0 { if gr_run(GR_LD, a3, envp, 0 as *u8) != 0 { br = 0 - 3 } }
106 if br == 0 { attempt = GR_BUILD_RETRIES } else { attempt = attempt + 1 }
107 }
108 if br != 0 { return br }
109 // run the gate hang-safe; its exit code IS the verdict
110 let a4: *i64 = sys_mmap(16) as *i64
111 a4[0] = GR_ELF as i64; a4[1] = 0
112 let ms: *i64 = sys_mmap(8) as *i64
113 return nx_run_timeout(GR_ELF, a4, envp, GR_RUN_TIMEOUT_MS, ms)
114}
115
116// Bare filename of an absolute path: scan to the last '/', return the byte after
117// it. If there is no '/', the whole string is the basename. Used to DEDUP a
118// discovered gate (bare name from the organ) against the static set (absolute
119// paths) -- dedup is load-bearing: the static set and _hdl_build/ overlap, so a
120// missing/wrong basename would silently double-run ~17 gates.
121func gr_basename(path: *u8) -> *u8 {
122 var i: i64 = 0
123 var last: i64 = 0 - 1
124 while path[i] != 0 as u8 {
125 if path[i] == 0x2F as u8 { last = i } // '/'
126 i = i + 1
127 }
128 return (path as i64 + last + 1) as *u8
129}
130
131// Is `name` (a bare filename) already the basename of one of the static gates?
132// Reuses gd_streq from nx_gate_discover.nx (NUL-terminated byte equality).
133func gr_static_has(sgates: *i64, sn: i64, name: *u8) -> i64 {
134 var i: i64 = 0
135 while i < sn {
136 let bn: *u8 = gr_basename(sgates[i] as *u8)
137 if gd_streq(bn, name) == 1 { return 1 }
138 i = i + 1
139 }
140 return 0
141}
142
143func main() -> i64 {
144 gr_puts("NISHI GATE RUNNER -- sovereign NishiLang (no .sh): compile + run each gate .nx, exit-code = verdict\n")
145 gr_puts("===================================================================================================\n")
146 let N: i64 = 37
147 let names: *i64 = sys_mmap(N * 8) as *i64
148 let gates: *i64 = sys_mmap(N * 8) as *i64
149 names[0] = ("eqsat-generator " as *u8) as i64
150 gates[0] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_eqsat_test.nx" as *u8) as i64
151 names[1] = ("nxgate-verifier " as *u8) as i64
152 gates[1] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_nxgate_sim_test.nx" as *u8) as i64
153 names[2] = ("alu-netlist-verifier " as *u8) as i64
154 gates[2] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_alu_netlist_test.nx" as *u8) as i64
155 names[3] = ("divider (meet) " as *u8) as i64
156 gates[3] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_alu_divider_test.nx" as *u8) as i64
157 names[4] = ("divider EXCEED (100pct) " as *u8) as i64
158 gates[4] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_alu_divider_exhaustive_test.nx" as *u8) as i64
159 names[5] = ("genealogist gap-scan " as *u8) as i64
160 gates[5] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_genealogist.nx" as *u8) as i64
161 names[6] = ("divider FLOOR (63b rand)" as *u8) as i64
162 gates[6] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_alu_divider_floor_test.nx" as *u8) as i64
163 names[7] = ("divider radix-4 (q+q) " as *u8) as i64
164 gates[7] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_alu_divider_r4_test.nx" as *u8) as i64
165 names[8] = ("divider PROOF (induction)" as *u8) as i64
166 gates[8] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_alu_divider_proof_test.nx" as *u8) as i64
167 names[9] = ("law-of-diminishing-returns" as *u8) as i64
168 gates[9] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_dimret.nx" as *u8) as i64
169 names[10] = ("multiplier 64x64->128 (3-leg triangulated)" as *u8) as i64
170 gates[10] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_mul_wide_test.nx" as *u8) as i64
171 names[11] = ("rigor/reproducibility axis" as *u8) as i64
172 gates[11] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_rigor_score.nx" as *u8) as i64
173 names[12] = ("divider NEWTON frontier (log-W, 3-leg)" as *u8) as i64
174 gates[12] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_alu_divider_newton.nx" as *u8) as i64
175 names[13] = ("recip-synth divider W16 (GATE-NET, 3-leg)" as *u8) as i64
176 gates[13] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_recip_synth_test.nx" as *u8) as i64
177 names[14] = ("triangulate harness (>=K/N + catch-test)" as *u8) as i64
178 gates[14] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_triangulate_test.nx" as *u8) as i64
179 names[15] = ("honest critical-path latency metric" as *u8) as i64
180 gates[15] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_latency_metric_test.nx" as *u8) as i64
181 names[16] = ("AUTHOR leg: superopt (mul->shl, 3-leg + neg-control)" as *u8) as i64
182 gates[16] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_superopt_test.nx" as *u8) as i64
183 names[17] = ("VERIFY leg: rule-soundness (6 rules all-W, 2-witness)" as *u8) as i64
184 gates[17] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_rule_soundness_test.nx" as *u8) as i64
185 names[18] = ("VERIFY leg: membership-as-PROOF (mul x 8==shl x 3, 3 negs)" as *u8) as i64
186 gates[18] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_eqsat_membership_proof_test.nx" as *u8) as i64
187 names[19] = ("SELF-BUILD: 3-tier policy + rollback byte-identical" as *u8) as i64
188 gates[19] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_self_build_test.nx" as *u8) as i64
189 names[20] = ("SELF-REGISTER: gate-discover (nx_dir, plausibility floor)" as *u8) as i64
190 gates[20] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_gate_discover_test.nx" as *u8) as i64
191 names[21] = ("SELF-LEARN: win-ledger (HOLDS/REFUTES, falsifiable assumptions)" as *u8) as i64
192 gates[21] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_win_ledger_test.nx" as *u8) as i64
193 names[22] = ("e-graph MEET: congruence closure (a==b=>f(a)==f(b), cited sound)" as *u8) as i64
194 gates[22] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_eqsat_congruence_test.nx" as *u8) as i64
195 names[23] = ("e-graph MEET: rule-DSL e-matcher (7 rules as DATA byte-equiv + new (xor x x)==0 proven+certified)" as *u8) as i64
196 gates[23] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_eqsat_dsl_parity_test.nx" as *u8) as i64
197 names[24] = ("e-graph MEET: e-class analysis const-fold (sound via gate-sim, certified)" as *u8) as i64
198 gates[24] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_eqsat_constfold_test.nx" as *u8) as i64
199 // ===== sovereign QUIC datagram transport (R1-R6b), RFC-conformant, added to the verified FLOOR =====
200 names[25] = ("QUIC wire: varints + DATAGRAM frame (RFC 9000 sec16 / 9221)" as *u8) as i64
201 gates[25] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_wire_test.nx" as *u8) as i64
202 names[26] = ("QUIC packet numbers (RFC 9000 A.2/A.3)" as *u8) as i64
203 gates[26] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_pkt_test.nx" as *u8) as i64
204 names[27] = ("QUIC long/short headers (RFC 9000 17.2-3 / 9001 A.2)" as *u8) as i64
205 gates[27] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_hdr_test.nx" as *u8) as i64
206 names[28] = ("QUIC core frames CRYPTO/ACK/CLOSE (RFC 9000 sec19)" as *u8) as i64
207 gates[28] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_frame_test.nx" as *u8) as i64
208 names[29] = ("QUIC Initial key schedule (RFC 9001 A.1 byte-exact)" as *u8) as i64
209 gates[29] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_keys_test.nx" as *u8) as i64
210 // (QUIC AEAD gate is correct -- 5/5 under nx_cc -- but nx_cc_known_good lacks the __aes128_enc_block
211 // AES-NI intrinsic that nx_aes128_gcm uses; it joins the floor once the known-good compiler gets it.)
212 names[30] = ("QUIC TLS1.3 key schedule early/derived/handshake (RFC 8448 byte-exact)" as *u8) as i64
213 gates[30] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_tls_schedule_test.nx" as *u8) as i64
214 names[31] = ("QUIC transport params + datagram negotiation (RFC 9000/9221)" as *u8) as i64
215 gates[31] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_transport_params_test.nx" as *u8) as i64
216 names[32] = ("QUIC TLS1.3 handshake framing + CRYPTO reassembly (RFC 8446 sec4)" as *u8) as i64
217 gates[32] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_hs_test.nx" as *u8) as i64
218 names[33] = ("QUIC handshake state machine + Finished-MAC (RFC 8446 App A)" as *u8) as i64
219 gates[33] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_handshake_sm_test.nx" as *u8) as i64
220 names[34] = ("QUIC RTT + NewReno CC + loss detection (RFC 9002)" as *u8) as i64
221 gates[34] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_recovery_test.nx" as *u8) as i64
222 names[35] = ("QUIC FEC over DATAGRAM frames -- zero-retransmit loss recovery (resilience exceed)" as *u8) as i64
223 gates[35] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_fec_test.nx" as *u8) as i64
224 names[36] = ("QUIC relay datagram fan-out (server-side wire-in core)" as *u8) as i64
225 gates[36] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/_hdl_build/nx_quic_relay_test.nx" as *u8) as i64
226
227 // ===== Build the UNION gate list: static FLOOR (always) + verified additives.
228 // The static set is copied FIRST so the union can never run fewer than N.
229 let unames: *i64 = sys_mmap(GR_UNION_CAP * 8) as *i64
230 let ugates: *i64 = sys_mmap(GR_UNION_CAP * 8) as *i64
231 var total: i64 = 0
232 while total < N { unames[total] = names[total]; ugates[total] = gates[total]; total = total + 1 }
233 // total == N (== STATIC_N) here. The floor is now structural.
234
235 // ===== SELF-REGISTRATION: discover _hdl_build/, VERIFY-BEFORE-ENROLL each new
236 // gate, with a STATIC FALLBACK if discovery is not a trustworthy source.
237 let gl: *NxGateList = sys_mmap(NX_GATE_LIST_BYTES) as *NxGateList
238 let v: i64 = nx_gate_discover(GR_HDL_DIR, gl, 1) // enforce the plausibility floor
239 // TRUST GATE: a discovered set is an additive source IFF its verdict is OK.
240 // NX_GD_OK already proves count >= NX_GD_PLAUSIBLE_FLOOR (the organ returns
241 // NX_GD_IMPLAUSIBLE below it), so a single-directory scan never has to match
242 // the runner's multi-directory STATIC_N -- which would make this loop dead
243 // code (a single _hdl_build/ scan is always < 25). Any non-OK verdict
244 // (EMPTY/IMPLAUSIBLE/SCAN_FAILED/TRUNCATED) -> STATIC FALLBACK.
245 var do_discovery: i64 = 0
246 if v == NX_GD_OK { do_discovery = 1 }
247 if do_discovery == 0 {
248 gr_puts(" [DISCOVER] verdict="); gr_putn(v); gr_puts(" count="); gr_putn(gl.count)
249 gr_puts(" not OK -> STATIC FALLBACK (running proven "); gr_putn(N); gr_puts(" unchanged)\n")
250 }
251 if do_discovery == 1 {
252 gr_puts(" [DISCOVER] verdict=OK count="); gr_putn(gl.count); gr_puts(" -> verify-before-enroll additives\n")
253 var d: i64 = 0
254 while d < gl.count {
255 let dpath: *u8 = gl.paths[d] as *u8
256 let dname: *u8 = gl.names[d] as *u8
257 // DEDUP: a discovered gate already in the static set must not double-run.
258 if gr_static_has(gates, N, dname) == 1 {
259 d = d + 1
260 } else {
261 // VERIFY: the FULL gr_gate (cc(pinned)->as->ld->run hang-safe).
262 // ENROLL iff rc==0; otherwise QUARANTINE (logged, NOT fatal, NOT counted).
263 let rc: i64 = gr_gate(dpath)
264 if rc == 0 {
265 ugates[total] = dpath as i64; unames[total] = dname as i64; total = total + 1
266 gr_puts(" [ENROLL] "); gr_puts(dname); gr_puts(" (discovered + PROVEN -> added)\n")
267 } else {
268 gr_puts(" [QUARANTINE")
269 if rc <= GR_QT_BUILD_HI { if rc >= GR_QT_BUILD_LO { gr_puts("-BUILD") } }
270 gr_puts("] "); gr_puts(dname); gr_puts(" rc="); gr_putn(rc)
271 gr_puts(" (discovered but did not PROVEN -- skipped, NOT counted)\n")
272 }
273 d = d + 1
274 }
275 }
276 }
277
278 // ===== RUN the union: static floor (always) + enrolled additives (0..M). =====
279 var proven: i64 = 0
280 var i: i64 = 0
281 while i < total {
282 let rc: i64 = gr_gate(ugates[i] as *u8)
283 gr_puts(" "); gr_puts(unames[i] as *u8); gr_puts(" ")
284 if rc == 0 { gr_puts("[PROVEN]"); proven = proven + 1 } else { gr_puts("[FAIL rc="); gr_putn(rc); gr_puts("]") }
285 gr_puts("\n")
286 i = i + 1
287 }
288 gr_puts("VERDICT: "); gr_putn(proven); gr_puts("/"); gr_putn(total)
289 gr_puts(" PROVEN (floor="); gr_putn(STATIC_N); gr_puts("; sovereign run -- NishiLang, no shell, no grep)\n")
290 // KEYSTONE: exit 0 IFF we proved at least the static floor. NOT proven==total,
291 // so an enrolled-then-later-broken additive can never RED the proven floor.
292 if proven < STATIC_N { sys_exit(1); return 1 }
293 sys_exit(0); return 0
294}