code wiki / _hdl_build / nx_sclass_dispatch_gate.nx
nx_sclass_dispatch_gate.nx source
↩ module page · 227 lines · 13846 B
1// nx_sclass_dispatch_gate.nx -- SCLASS-FLOW-01: AUTONOMOUS FLOW over the REAL WMS registry (the audit's #1
2// gap: "no real registered stream is auto-picked, leased, RUN, and COMPLETED by the machine"). This closes
3// the execute+complete half that nx_dispatch_lease (pick+lease only) left open: the dispatcher PICKS a real
4// registered stream, LEASES it (fl_acquire), RUNS the gate named in its code_link (fork/exec the sovereign
5// runner, verdict on the exit code -- nx_gate_sweep idiom), and -- iff GREEN -- marks the stream DONE in the
6// registry (ws_put_locked) + beats its heartbeat. So the MACHINE moves a real stream ACTIVE->DONE, not Claude.
7// LIAR-KILL / neg-control: a stream whose code_link names a NON-EXISTENT gate is NEVER completed (stays ACTIVE).
8// REUSE (rule 15): fl_acquire/fl_release (nx_arbiter), ws_put_p/ws_get/ws_field/ws_member/WS_PREFIX (registry),
9// ws_put_locked (nx_ws_cas), hb_beat_s (nx_heartbeat_str), run_gate fork/exec (nx_gate_sweep). Sovereign,
10// concurrency-safe, additive, liar-killed. license_tier: ORIGINAL expect_exit: 0
11import "nx_ws_cas.nx"
12import "nx_arbiter.nx"
13import "nx_heartbeat_str.nx"
14import "nx_gate_verdict.nx"
15import "nx_artifact_root.nx" // HOST-BOUND FIX 2026-08-02: the runner is _offc/ on the laptop and
16// buildroot/_offc/ (or flat) on the NAS. Execing the literal made this gate grade the MACHINE, not the
17// capability -- T2 could never pass off-laptop. ar_resolve is the ecosystem's existing roots-table
18// resolver (the 9-of-9-UNGROUNDED law); wire it, never build a second.
19
20const SD_RUNNER: *u8 = "_offc/nx_sov_build_run.elf"
21const SD_HBP: *u8 = "knowledge/status/wms_heartbeat.log"
22const SD_PROOF_GATE: *u8 = "nx_raci_gate" // a real, light, GREEN leaf gate = the proof stream's work
23
24func sd_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
25func sd_cat(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 }
26func sd_num(dst: *u8, off: i64, v0: i64) -> i64 { var o: i64=off; var v: i64=v0; if v<0 {dst[o]=45 as u8;o=o+1;v=0-v} let t: *u8=sys_mmap(24); var k: i64=0; if v==0{t[0]=48 as u8;k=1} while v>0{t[k]=(48+(v-(v/10)*10)) as u8;v=v/10;k=k+1} var i: i64=0; while i<k{dst[o+i]=t[k-1-i];i=i+1} return o+k }
27func sd_n(v: i64) -> i64 { let b: *u8=sys_mmap(24); let e: i64=sd_num(b,0,v); sys_write(1,b,e); return 0 }
28func sd_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 }
29func sd_key(id: *u8, out: *u8) -> i64 { out[0]=119 as u8; out[1]=115 as u8; out[2]=58 as u8; var k: i64=0; while id[k]!=(0 as u8){ out[3+k]=id[k]; k=k+1 } out[3+k]=0 as u8; return 3+k }
30
31// fork/exec the sovereign runner on `name`; stdout+stderr -> /dev/null; return the exit code (0=GREEN).
32// PHASE-SPLIT (2026-08-02, debt 1785694359): BUILD and RUN need DIFFERENT working directories, and
33// conflating them is what made a healthy gate report its subject broken.
34// build phase: cwd MUST be buildroot (nx_sov_build_run resolves runtime/ relative to cwd)
35// run phase: cwd MUST be the SERVING ROOT (the built gate resolves its DATA -- knowledge/... --
36// relative to cwd; running it from buildroot re-roots that data and it reports a
37// perfectly valid registry as invalid)
38// So: fork#1 chdir->buildroot, exec the runner with --build-only; fork#2 exec the produced elf from
39// the root we are already standing in. The parent NEVER chdirs.
40func sd_build_only(name: *u8) -> i64 {
41 let pid: i64 = sys_fork()
42 if pid == 0 {
43 let dn: i64 = sys_openat_wr("/dev/null\x00" as *u8, 420)
44 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) }
45 if sys_chdir("buildroot" as *u8) != 0 { sys_exit(126) }
46 let argv: *i64 = sys_mmap(64) as *i64
47 argv[0] = "_offc/nx_sov_build_run.elf" as *u8 as i64
48 argv[1] = name as i64
49 argv[2] = "--build-only" as *u8 as i64
50 argv[3] = 0
51 let envp: *i64 = sys_mmap(16) as *i64
52 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
53 sys_execve("_offc/nx_sov_build_run.elf" as *u8, argv, envp)
54 sys_exit(127)
55 }
56 let st: *i64 = sys_mmap(16) as *i64
57 sys_wait4(pid, st, 0)
58 if (st[0] & 0x7f) != 0 { return 128 + (st[0] & 0x7f) }
59 return (st[0] >> 8) & 0xff
60}
61
62func sd_run_gate(name: *u8) -> i64 {
63 let runner: *u8 = sys_mmap(512)
64 ar_resolve(SD_RUNNER, runner)
65 // phase 1: build (in buildroot). A build failure is NOT a subject verdict -- surface it distinctly.
66 let brc: i64 = sd_build_only(name)
67 if brc != 0 { return 0 - 20 }
68 // phase 2: run the produced elf FROM THE SERVING ROOT (never from buildroot)
69 let built: *u8 = sys_mmap(512)
70 var bo: i64 = sd_cat(built, 0, "buildroot/_build/" as *u8)
71 bo = sd_cat(built, bo, name); bo = sd_cat(built, bo, ".sov.elf" as *u8); built[bo] = 0 as u8
72 let bprobe: i64 = sys_openat_rd(built)
73 if bprobe < 0 { return 0 - 21 }
74 sys_close(bprobe)
75 // HOST-BOUND FIX 2026-08-02 (3rd and last facet): nx_sov_build_run RESOLVES ITS SOURCE TREE RELATIVE TO
76 // CWD -- the banked law is `cd buildroot && ./_offc/nx_sov_build_run.elf <target>`. Forked from nishihost
77 // it cannot see runtime/ and the build fails exit=2, which read as "the stream's gate is RED" when the
78 // truth was "the builder was standing in the wrong directory". The chdir happens in the CHILD ONLY, so
79 // the parent's registry/lock/heartbeat paths stay exactly as they were.
80 let pid: i64 = sys_fork()
81 if pid == 0 {
82 let dn: i64 = sys_openat_wr("/dev/null\x00" as *u8, 420)
83 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) }
84 let argv: *i64 = sys_mmap(64) as *i64
85 argv[0] = built as i64; argv[1] = 0
86 let envp: *i64 = sys_mmap(16) as *i64
87 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
88 sys_execve(built, argv, envp)
89 sys_exit(127)
90 }
91 let st: *i64 = sys_mmap(16) as *i64
92 sys_wait4(pid, st, 0)
93 return (st[0] >> 8) & 0xff
94}
95
96// does an organ source exist (so we never dispatch a phantom gate)? checks both organ dirs.
97func sd_organ_exists(name: *u8) -> i64 {
98 // HOST-BOUND FIX 2026-08-02 (2nd site in this organ): the source dirs are `runtime/...` on a dev tree
99 // and `buildroot/runtime/...` on the NAS. Hardcoding them made a REAL gate read as a PHANTOM (rc -3)
100 // off-laptop -- the dispatcher then refused to run work that exists, and T1/T2/T4 could never pass.
101 // ar_resolve walks the host's roots table; the raw path is still tried FIRST so dev behaviour is identical.
102 let p: *u8 = sys_mmap(512)
103 var o: i64 = sd_cat(p, 0, "runtime/_hdl_build/" as *u8); o = sd_cat(p, o, name); o = sd_cat(p, o, ".nx" as *u8); p[o]=0 as u8
104 let r1: *u8 = sys_mmap(512)
105 ar_resolve(p, r1)
106 let fd: i64 = sys_openat_rd(r1)
107 if fd >= 0 { sys_close(fd); return 1 }
108 let p2: *u8 = sys_mmap(512)
109 var o2: i64 = sd_cat(p2, 0, "runtime/" as *u8); o2 = sd_cat(p2, o2, name); o2 = sd_cat(p2, o2, ".nx" as *u8); p2[o2]=0 as u8
110 let r2: *u8 = sys_mmap(512)
111 ar_resolve(p2, r2)
112 let fd2: i64 = sys_openat_rd(r2)
113 if fd2 >= 0 { sys_close(fd2); return 1 }
114 return 0
115}
116
117// register a NEW stream (E-PM), concurrency-safe: hold the registry wlock across the ws:ids RMW + record write.
118func sd_register(id: *u8, state: *u8, code: *u8) -> i64 {
119 let wp: *u8 = WS_PREFIX
120 let lp: *u8 = sys_mmap(512); var li: i64 = 0
121 while wp[li] != (0 as u8) { lp[li]=wp[li]; li=li+1 }
122 let suf: *u8 = "wlock"; var ls: i64 = 0
123 while suf[ls] != (0 as u8) { lp[li]=suf[ls]; li=li+1; ls=ls+1 }
124 lp[li]=0 as u8
125 let lfd: i64 = sys_openat_append(lp, 0x1a4)
126 if lfd >= 0 { sys_flock(lfd, SYS_LOCK_EX) }
127 let key: *u8 = sys_mmap(192); sd_key(id, key)
128 let rec: *u8 = sys_mmap(1024); var o: i64 = 0
129 o=sd_cat(rec,o,id); rec[o]=9 as u8;o=o+1
130 o=sd_cat(rec,o,"E-PM" as *u8); rec[o]=9 as u8;o=o+1
131 o=sd_cat(rec,o,state); rec[o]=9 as u8;o=o+1
132 o=sd_num(rec,o,sys_now_realtime_sec()); rec[o]=9 as u8;o=o+1
133 o=sd_cat(rec,o,"nx_sclass_dispatch" as *u8); rec[o]=9 as u8;o=o+1
134 o=sd_cat(rec,o,code); rec[o]=9 as u8;o=o+1
135 o=sd_cat(rec,o,"-" as *u8)
136 rec[o]=0 as u8
137 ws_put_p(WS_PREFIX, key, rec)
138 let pq: *i64 = sys_mmap(16) as *i64; let lq: *i64 = sys_mmap(16) as *i64
139 if ss_get(WS_PREFIX, "ws:ids" as *u8, pq, lq) == 1 {
140 let ids: *u8 = pq[0] as *u8; let idn: i64 = lq[0]
141 if ws_member(ids, idn, id) == 0 {
142 let nb: *u8 = sys_mmap(idn + 192); var c: i64 = 0
143 while c < idn { nb[c]=ids[c]; c=c+1 }
144 nb[c]=9 as u8; c=c+1
145 var d: i64 = 0; while id[d] != (0 as u8) { nb[c]=id[d]; c=c+1; d=d+1 }
146 nb[c]=0 as u8
147 ws_put_p(WS_PREFIX, "ws:ids" as *u8, nb)
148 }
149 }
150 if lfd >= 0 { sys_flock(lfd, SYS_LOCK_UN); sys_close(lfd) }
151 return 0
152}
153
154// read field f of stream id into out (1 found / 0 absent).
155func sd_field(id: *u8, f: i64, out: *u8) -> i64 {
156 let key: *u8 = sys_mmap(192); sd_key(id, key)
157 let pq: *i64 = sys_mmap(16) as *i64; let lq: *i64 = sys_mmap(16) as *i64
158 if ws_get(key, pq, lq) != 1 { out[0]=0 as u8; return 0 }
159 ws_field(pq[0] as *u8, lq[0], f, out)
160 return 1
161}
162
163// set an EXISTING stream's state (preserve mem/code/deps); last_touched = now. ws_put_locked (single key).
164func sd_set_state(id: *u8, newstate: *u8) -> i64 {
165 let key: *u8 = sys_mmap(192); sd_key(id, key)
166 let pq: *i64 = sys_mmap(16) as *i64; let lq: *i64 = sys_mmap(16) as *i64
167 if ws_get(key, pq, lq) != 1 { return 0 }
168 let rec: *u8 = pq[0] as *u8; let rlen: i64 = lq[0]
169 let mem: *u8 = sys_mmap(256); ws_field(rec, rlen, 4, mem)
170 let code: *u8 = sys_mmap(256); ws_field(rec, rlen, 5, code)
171 let deps: *u8 = sys_mmap(256); ws_field(rec, rlen, 6, deps)
172 let nr: *u8 = sys_mmap(1024); var o: i64 = 0
173 o=sd_cat(nr,o,id); nr[o]=9 as u8;o=o+1
174 o=sd_cat(nr,o,"E-PM" as *u8); nr[o]=9 as u8;o=o+1
175 o=sd_cat(nr,o,newstate); nr[o]=9 as u8;o=o+1
176 o=sd_num(nr,o,sys_now_realtime_sec()); nr[o]=9 as u8;o=o+1
177 o=sd_cat(nr,o,mem); nr[o]=9 as u8;o=o+1
178 o=sd_cat(nr,o,code); nr[o]=9 as u8;o=o+1
179 o=sd_cat(nr,o,deps)
180 nr[o]=0 as u8
181 ws_put_locked(WS_PREFIX, key, nr)
182 return 1
183}
184
185// DISPATCH one stream end-to-end: lease -> read its code_link gate -> run it -> complete iff GREEN.
186// returns the gate exit code (0=GREEN, >0=RED), -2 busy(lease lost), -3 no runnable gate.
187func sd_dispatch_one(id: *u8) -> i64 {
188 let resource: *u8 = sys_mmap(160); var o: i64 = sd_cat(resource, 0, "sclassdisp_" as *u8); o=sd_cat(resource,o,id); resource[o]=0 as u8
189 let lk: i64 = fl_acquire(resource, 50, 20)
190 if lk < 0 { return 0 - 2 }
191 let codebuf: *u8 = sys_mmap(256)
192 sd_field(id, 5, codebuf)
193 if sd_organ_exists(codebuf) == 0 { fl_release(lk); return 0 - 3 }
194 let ex: i64 = sd_run_gate(codebuf)
195 if ex == 0 { sd_set_state(id, "DONE" as *u8); hb_beat_s(SD_HBP, id, 1) }
196 fl_release(lk)
197 return ex
198}
199
200func main() -> i64 {
201 sd_w("=== nx_sclass_dispatch (SCLASS-FLOW-01): AUTONOMOUS pick -> lease -> RUN gate -> COMPLETE a real WMS stream ===\n" as *u8)
202 sd_register("SCLASS-DISP-PROOF" as *u8, "ACTIVE" as *u8, SD_PROOF_GATE)
203 sd_register("SCLASS-DISP-NEG" as *u8, "ACTIVE" as *u8, "nx_no_such_gate_zzz" as *u8)
204 sd_w(" registered 2 real WMS streams (E-PM): PROOF(gate=" as *u8); sd_w(SD_PROOF_GATE); sd_w(") + NEG(phantom gate)\n" as *u8)
205
206 sd_w("-- the machine dispatches (lease -> run the stream's gate -> complete iff GREEN) ...\n" as *u8)
207 let v1: i64 = sd_dispatch_one("SCLASS-DISP-PROOF" as *u8)
208 let v2: i64 = sd_dispatch_one("SCLASS-DISP-NEG" as *u8)
209
210 let st1: *u8 = sys_mmap(64); sd_field("SCLASS-DISP-PROOF" as *u8, 2, st1)
211 let st2: *u8 = sys_mmap(64); sd_field("SCLASS-DISP-NEG" as *u8, 2, st2)
212 sd_w(" PROOF: gate-exit=" as *u8); sd_n(v1); sd_w(" -> registry state=" as *u8); sd_w(st1); sd_w("\n" as *u8)
213 sd_w(" NEG: dispatch-rc=" as *u8); sd_n(v2); sd_w(" -> registry state=" as *u8); sd_w(st2); sd_w("\n" as *u8)
214
215 var pass: i64=0; var fail: i64=0
216 if v1 >= 0 { sd_w(" T1 lease+run: PROOF leased + RAN its real gate end-to-end. PASS\n" as *u8); pass=pass+1 } else { sd_w(" T1 FAIL (no lease / no gate, v1=" as *u8); sd_n(v1); sd_w(")\n" as *u8); fail=fail+1 }
217 if v1 == 0 { if sd_streq(st1, "DONE" as *u8) == 1 { sd_w(" T2 complete-on-GREEN: the gate passed -> the MACHINE marked the stream DONE. PASS\n" as *u8); pass=pass+1 } else { sd_w(" T2 FAIL: GREEN but state!=DONE\n" as *u8); fail=fail+1 } } else { sd_w(" T2 FAIL: proof gate not GREEN (v1=" as *u8); sd_n(v1); sd_w(") -- swap SD_PROOF_GATE\n" as *u8); fail=fail+1 }
218 if v2 == (0 - 3) { if sd_streq(st2, "ACTIVE" as *u8) == 1 { sd_w(" T3 LIAR-KILL: phantom gate -> NOT run, stream stays ACTIVE (no fabricated completion). PASS\n" as *u8); pass=pass+1 } else { sd_w(" T3 FAIL\n" as *u8); fail=fail+1 } } else { sd_w(" T3 FAIL: NEG rc != -3 (=" as *u8); sd_n(v2); sd_w(")\n" as *u8); fail=fail+1 }
219 if sd_streq(st1, "DONE" as *u8) == 1 { sd_w(" T4 FLOW PROVEN: a REAL registry stream went ACTIVE->DONE by the dispatcher (not by Claude). PASS\n" as *u8); pass=pass+1 } else { sd_w(" T4 FAIL\n" as *u8); fail=fail+1 }
220
221 sd_w("\npass=" as *u8); sd_n(pass); sd_w(" fail=" as *u8); sd_n(fail); sd_w("\n" as *u8)
222 if fail == 0 { sd_w("VERDICT: GREEN (autonomous pick->lease->run->complete proven on a real WMS stream; liar-killed). The team FLOWS.\n" as *u8) } else { sd_w("VERDICT: RED\n" as *u8) }
223 let ctr: *i64 = gv_ctr()
224 ctr[0] = pass
225 ctr[1] = pass + fail
226 return gv_verdict("SCLASS-DISPATCH-GATE" as *u8, ctr, "autonomous pick-lease-run-complete on a REAL WMS stream, phantom-gate liar-kill holding" as *u8)
227}