code wiki / _hdl_build / nx_sclass_flow04_gate.nx
nx_sclass_flow04_gate.nx source
↩ module page · 130 lines · 6904 B
1// nx_sclass_flow04_gate.nx -- the EVIDENCE GATE for SCLASS-FLOW-04 ("de-flake the control plane:
2// dispatch/journal/boot-revive intermittent RED -> 3x deterministic GREEN each"). PM-routed to the engineer.
3//
4// ROOT CAUSE (found by measurement, not assumption): the three control-plane gates tried to be hermetic by
5// namespacing their /tmp scratch with getpid -- but __syscall(39) is BROKEN on this backend (returns the
6// CONSTANT -25 for every process). So every concurrent run shared the same scratch files (/tmp/m6_ledger_-25.log,
7// /tmp/rj_good_.jrnl, /tmp/dlg_<ms>_-25.tsv) and corrupted each other => intermittent RED under any parallel sweep.
8// Sequentially they pass (each truncates at start); concurrently they collided. FIX: a g_uniq() that claims a
9// process-unique token ATOMICALLY via O_EXCL (/tmp/nxuq_<n>), now namespacing ALL scratch in all three gates.
10//
11// THIS GATE proves the de-flake on the REAL built ELFs:
12// PHASE 1 (the literal criterion): build each gate once, run each 3x CONSECUTIVELY -> 9/9 must be GREEN.
13// PHASE 2 (the actual flake trigger): run each gate's ELF F4_CONC times CONCURRENTLY -> 0 fails (the proof
14// the fix holds; before the fix this RED-ed 5..8 of 8).
15// LIAR-KILL: a bogus ELF path must yield non-zero -> the harness genuinely detects RED, never rubber-stamps.
16// GREEN iff phase1==9 AND every gate's concurrent-fails==0 AND the liar-kill fired. expect_exit: 0
17// license_tier: ORIGINAL
18import "nx_syscalls.nx"
19
20const F4_RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8
21const F4_CONC: i64 = 8 // concurrent instances per gate (the original flake showed at 4..8)
22
23func fw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
24func fn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-\x00" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); 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} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
25func fcat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i];o=o+1;i=i+1} return o }
26
27// "/tmp/<name>.sov.elf" -> out (name is NUL-terminated, no .nx)
28func f4_elf_path(name: *u8, out: *u8) -> i64 {
29 var o: i64 = fcat(out, 0, "/tmp/\x00" as *u8)
30 o = fcat(out, o, name)
31 o = fcat(out, o, ".sov.elf\x00" as *u8)
32 out[o] = 0 as u8
33 return o
34}
35
36// fork+exec a path with argv0=path (and optional arg1), stdout/stderr -> /dev/null; child only. returns child pid.
37func f4_spawn(path: *u8, arg1: *u8) -> i64 {
38 let pid: i64 = sys_fork()
39 if pid == 0 {
40 let dn: i64 = sys_openat_wr("/dev/null\x00" as *u8, 420)
41 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) }
42 let argv: *i64 = sys_mmap(32) as *i64
43 argv[0] = path as i64
44 if arg1 == (0 as *u8) { argv[1] = 0 } else { argv[1] = arg1 as i64; argv[2] = 0 }
45 let envp: *i64 = sys_mmap(16) as *i64
46 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0
47 sys_execve(path, argv, envp)
48 sys_exit(127)
49 }
50 return pid
51}
52
53func f4_wait(pid: i64) -> i64 {
54 let st: *i64 = sys_mmap(16) as *i64
55 sys_wait4(pid, st, 0)
56 return (st[0] >> 8) & 0xff
57}
58
59// run ONE thing to completion, return its exit code
60func f4_run(path: *u8, arg1: *u8) -> i64 { return f4_wait(f4_spawn(path, arg1)) }
61
62func main() -> i64 {
63 fw("=== nx_sclass_flow04_gate: de-flake control plane (dispatch/journal/boot-revive) -> deterministic GREEN ===\n" as *u8)
64
65 let g0: *u8 = "nx_dispatch_lease_gate\x00" as *u8
66 let g1: *u8 = "nx_research_journal_gate\x00" as *u8
67 let g2: *u8 = "nx_boot_revive_m6_gate\x00" as *u8
68 let gates: *i64 = sys_mmap(32) as *i64
69 gates[0] = g0 as i64; gates[1] = g1 as i64; gates[2] = g2 as i64
70
71 // build each gate ONCE via the runner (leaves /tmp/<name>.sov.elf reflecting current source)
72 fw("-- building the 3 control-plane gates (sovereign nx_cc->nxasm) --\n" as *u8)
73 var gi: i64 = 0
74 while gi < 3 {
75 let bx: i64 = f4_run(F4_RUNNER, gates[gi] as *u8)
76 fw(" built "); fw(gates[gi] as *u8); fw(" runner-exit="); fn(bx); fw("\n" as *u8)
77 gi = gi + 1
78 }
79
80 // PHASE 1 -- 3x CONSECUTIVE GREEN each (the literal FLOW-04 criterion)
81 fw("\n-- PHASE 1: 3x consecutive GREEN each (the criterion) --\n" as *u8)
82 var p1_green: i64 = 0
83 gi = 0
84 while gi < 3 {
85 let ep: *u8 = sys_mmap(128); f4_elf_path(gates[gi] as *u8, ep)
86 var r: i64 = 0
87 var ok: i64 = 0
88 while r < 3 {
89 let ec: i64 = f4_run(ep, 0 as *u8)
90 if ec == 0 { ok = ok + 1; p1_green = p1_green + 1 }
91 r = r + 1
92 }
93 fw(" "); fw(gates[gi] as *u8); fw(" seq3 GREEN="); fn(ok); fw("/3\n" as *u8)
94 gi = gi + 1
95 }
96
97 // PHASE 2 -- F4_CONC CONCURRENT each (the actual flake trigger); 0 fails proves the de-flake
98 fw("\n-- PHASE 2: "); fn(F4_CONC); fw("x concurrent each (the de-flake; was 5..8 fails before the g_uniq fix) --\n" as *u8)
99 var p2_totalfail: i64 = 0
100 gi = 0
101 while gi < 3 {
102 let ep2: *u8 = sys_mmap(128); f4_elf_path(gates[gi] as *u8, ep2)
103 let pids: *i64 = sys_mmap(8 * (F4_CONC + 4)) as *i64
104 var j: i64 = 0
105 while j < F4_CONC { pids[j] = f4_spawn(ep2, 0 as *u8); j = j + 1 }
106 var fails: i64 = 0
107 j = 0
108 while j < F4_CONC { if f4_wait(pids[j]) != 0 { fails = fails + 1 } j = j + 1 }
109 p2_totalfail = p2_totalfail + fails
110 fw(" "); fw(gates[gi] as *u8); fw(" concurrent"); fn(F4_CONC); fw(" fails="); fn(fails); fw("\n" as *u8)
111 gi = gi + 1
112 }
113
114 // LIAR-KILL -- a bogus ELF must yield non-zero (the harness detects RED, not a rubber stamp)
115 let liar: i64 = f4_run("/tmp/nx_flow04_bogus_does_not_exist.sov.elf\x00" as *u8, 0 as *u8)
116 fw("\n-- LIAR-KILL: bogus ELF exit="); fn(liar); fw(" (must be non-zero)\n" as *u8)
117
118 // VERDICT
119 var pass: i64 = 0; var total: i64 = 0
120 total=total+1; if p1_green == 9 { pass=pass+1; fw(" [PASS] " as *u8) } else { fw(" [FAIL] " as *u8) }
121 fw("T1 phase1 3x-consecutive-GREEN each = "); fn(p1_green); fw("/9\n" as *u8)
122 total=total+1; if p2_totalfail == 0 { pass=pass+1; fw(" [PASS] " as *u8) } else { fw(" [FAIL] " as *u8) }
123 fw("T2 phase2 concurrent corruption fails = "); fn(p2_totalfail); fw(" (0 = de-flaked)\n" as *u8)
124 total=total+1; if liar != 0 { pass=pass+1; fw(" [PASS] " as *u8) } else { fw(" [FAIL] " as *u8) }
125 fw("T3 liar-kill: bogus gate detected as non-zero\n" as *u8)
126
127 fw("\n=== nx_sclass_flow04_gate "); fn(pass); fw("/"); fn(total)
128 if pass == total { fw(" GREEN (control plane de-flaked: deterministic sequential + concurrent; root cause = broken getpid, fixed with O_EXCL g_uniq)\n" as *u8); sys_exit(0); return 0 }
129 fw(" RED\n" as *u8); sys_exit(1); return 1
130}