code wiki / _hdl_build / nx_pipeline_run.nx
nx_pipeline_run.nx source
↩ module page · 206 lines · 9778 B
1// nx_pipeline_run.nx -- R2: THE CHAIN. The orchestrator that auto-walks a pipeline work-item
2// through discover->research->spec->build via the spine, closing the census gap (nx_pipeline_run
3// ABSENT). Per the operator's chosen path: a DETERMINISTIC orchestrator + self-gate first (proves
4// the walk logic + the halt-on-failure invariant), with the LIVE organ exec as a marked seam (R3).
5//
6// INVARIANT (orchestrator level, on top of the spine's no-skip): if a stage executor FAILS, the
7// chain HALTS at that stage -- the item never advances, so failure can never be laundered into
8// completion. The self-gate's neg-control forces a stage failure and proves the item is stuck.
9//
10// CLI: nx_pipeline_run SEED <id> <gap> -- create a live work-item (under PR_PREFIX)
11// nx_pipeline_run RUN <id> -- auto-walk a live work-item to EXCEEDED (stub exec)
12// nx_pipeline_run -- no-arg: run the deterministic SELF-GATE + verdict
13// Sovereign; imports the spine. license_tier: ORIGINAL
14import "nx_pipeline_spine.nx"
15import "nx_workstream_store.nx"
16import "nx_seg_store.nx"
17import "nx_framed_append.nx"
18import "nx_syscalls.nx"
19
20const PR_PREFIX: *u8 = "knowledge/store/pl-" // production live work-items
21const PR_GATE_PREFIX: *u8 = "knowledge/store/plrun-" // self-gate scratch (cleaned before run)
22const PR_LOG: *u8 = "knowledge/status/pipeline_run.log"
23const PR_RUNNER: *u8 = "_offc/nx_sov_build_run.elf"
24
25func pr_(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
26func prn(v: i64) -> i64 {
27 let bb: *u8 = sys_mmap(28); var m: i64 = v
28 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
29 let t: *u8 = sys_mmap(28); var k: i64 = 0
30 if m == 0 { t[0] = 48 as u8; k = 1 }
31 while m > 0 { t[k] = ((48 + (m % 10)) as u8); m = m / 10; k = k + 1 }
32 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
33 sys_write(1, bb, k); return 0
34}
35func pr_eq(a: *u8, b: *u8) -> i64 {
36 var k: i64 = 0
37 while a[k] != (0 as u8) { if a[k] != b[k] { return 0 } k = k + 1 }
38 if b[k] != (0 as u8) { return 0 }
39 return 1
40}
41
42// data-driven stage->organ map: the organ that performs the cur->cur+1 transition (the LIVE path).
43func pr_organ_for(cur: i64) -> *u8 {
44 if cur == PL_DISCOVERED { return "nx_research_fetch" as *u8 }
45 if cur == PL_RESEARCHED { return "nx_pattern_emit16" as *u8 }
46 if cur == PL_SPECED { return "nx_auto_builder" as *u8 }
47 if cur == PL_BUILT { return "nx_capability_audit" as *u8 }
48 return "-" as *u8
49}
50
51// DETERMINISTIC stub executor: synthesize the cur->cur+1 artifact. fail_at>0 simulates that stage's
52// organ FAILING (-1) so the self-gate can prove the chain halts. (LIVE seam: R3 fork/exec
53// pr_organ_for(cur) and key success on its exit code.)
54func pr_exec_stub(cur: i64, id: *u8, fail_at: i64, art_out: *u8) -> i64 {
55 if cur == fail_at { return 0 - 1 }
56 var o: i64 = 0
57 o = fa_cat(art_out, o, "stub:" as *u8)
58 o = fa_cat(art_out, o, pr_organ_for(cur))
59 art_out[o] = 58 as u8; o = o + 1 // ':'
60 o = fa_cat(art_out, o, id)
61 art_out[o] = 0 as u8
62 return o
63}
64
65// AUTO-WALK from the item's current stage to EXCEEDED, halting on any executor failure or spine
66// refusal. returns the final stage reached. THIS is the chain the census found absent.
67func pr_run(prefix: *u8, id: *u8, fail_at: i64) -> i64 {
68 var cur: i64 = pl_stage_p(prefix, id)
69 var guard: i64 = 0
70 while cur >= PL_DISCOVERED {
71 if cur >= PL_EXCEEDED { return cur }
72 let art: *u8 = sys_mmap(512)
73 let rc: i64 = pr_exec_stub(cur, id, fail_at, art)
74 if rc < 0 { return cur } // stage executor FAILED -> HALT (no fabricated progress)
75 let ns: i64 = pl_advance_p(prefix, id, cur, art)
76 if ns < 0 { return cur } // spine refused -> HALT
77 cur = ns
78 guard = guard + 1
79 if guard > 8 { return cur } // bounded (JPL rule 2)
80 }
81 return cur
82}
83
84// --- R3: the LIVE executor. fork/exec the REAL stage organ (reuses the verified nx_sclass_dispatch
85// sd_run_gate idiom, rule 15); stdout/stderr -> /dev/null; exit code 0 = the organ ran GREEN. ---
86func pr_run_organ(organ: *u8) -> i64 {
87 let pid: i64 = sys_fork()
88 if pid == 0 {
89 let dn: i64 = sys_openat_wr("/dev/null\x00" as *u8, 420)
90 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) }
91 let argv: *i64 = sys_mmap(64) as *i64
92 argv[0] = PR_RUNNER as i64; argv[1] = organ as i64; argv[2] = 0
93 let envp: *i64 = sys_mmap(16) as *i64
94 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
95 sys_execve(PR_RUNNER, argv, envp)
96 sys_exit(127)
97 }
98 let st: *i64 = sys_mmap(16) as *i64
99 sys_wait4(pid, st, 0)
100 return (st[0] >> 8) & 0xff
101}
102
103// LIVE stage executor: actually RUN the stage's organ; success = exit 0; artifact records organ+exit.
104func pr_exec_live(cur: i64, id: *u8, art_out: *u8) -> i64 {
105 let organ: *u8 = pr_organ_for(cur)
106 let ex: i64 = pr_run_organ(organ)
107 var o: i64 = 0
108 o = fa_cat(art_out, o, "live:" as *u8); o = fa_cat(art_out, o, organ)
109 o = fa_cat(art_out, o, ":exit" as *u8); o = fa_catn(art_out, o, ex)
110 art_out[o] = 0 as u8
111 if ex == 0 { return 0 }
112 return 0 - 1
113}
114
115// LIVE auto-walk: like pr_run but invokes the REAL organs. Halts honestly where a real organ fails
116// or needs inputs not yet wired = the R3 per-stage I/O boundary, SURFACED not faked.
117func pr_run_live(prefix: *u8, id: *u8) -> i64 {
118 var cur: i64 = pl_stage_p(prefix, id)
119 var guard: i64 = 0
120 while cur >= PL_DISCOVERED {
121 if cur >= PL_EXCEEDED { return cur }
122 let art: *u8 = sys_mmap(512)
123 let rc: i64 = pr_exec_live(cur, id, art)
124 if rc < 0 { return cur }
125 let ns: i64 = pl_advance_p(prefix, id, cur, art)
126 if ns < 0 { return cur }
127 cur = ns
128 guard = guard + 1
129 if guard > 8 { return cur }
130 }
131 return cur
132}
133
134func main(argc: i64, argv: *i64) -> i64 {
135 if argc >= 4 {
136 if pr_eq(argv[1] as *u8, "SEED" as *u8) == 1 {
137 let s: i64 = pl_seed_p(PR_PREFIX, argv[2] as *u8, argv[3] as *u8)
138 pr_("WS-PIPELINE SEED id=" as *u8); pr_(argv[2] as *u8); pr_(" stage=" as *u8); prn(s); pr_("\n" as *u8)
139 sys_exit(0); return 0
140 }
141 }
142 if argc >= 3 {
143 if pr_eq(argv[1] as *u8, "RUN" as *u8) == 1 {
144 let f: i64 = pr_run(PR_PREFIX, argv[2] as *u8, 0)
145 pr_("WS-PIPELINE RUN id=" as *u8); pr_(argv[2] as *u8); pr_(" final_stage=" as *u8); prn(f); pr_("\n" as *u8)
146 if f >= PL_EXCEEDED { sys_exit(0); return 0 }
147 sys_exit(0); return 0
148 }
149 if pr_eq(argv[1] as *u8, "RUNLIVE" as *u8) == 1 {
150 pl_seed_p(PR_PREFIX, argv[2] as *u8, "live demo gap" as *u8)
151 let f: i64 = pr_run_live(PR_PREFIX, argv[2] as *u8)
152 let af: *u8 = sys_mmap(512); pl_field_p(PR_PREFIX, argv[2] as *u8, 3, af)
153 pr_("WS-PIPELINE RUNLIVE id=" as *u8); pr_(argv[2] as *u8)
154 pr_(" final_stage=" as *u8); prn(f); pr_(" last_artifact=" as *u8); pr_(af); pr_("\n" as *u8)
155 sys_exit(0); return 0
156 }
157 }
158 if argc >= 2 {
159 if pr_eq(argv[1] as *u8, "LIVEPROBE" as *u8) == 1 {
160 // prove the LIVE executor is REAL: fork/exec a known-GREEN light organ and read its
161 // actual exit code (0). This is the seam being live, not stub -- without the heavy /
162 // networked full chain.
163 let ex: i64 = pr_run_organ("nx_pipeline_census" as *u8)
164 pr_("WS-PIPELINE LIVEPROBE organ=nx_pipeline_census exit=" as *u8); prn(ex)
165 if ex == 0 { pr_(" -> live-exec WIRED (real organ ran GREEN via fork/exec)\n" as *u8); sys_exit(0); return 0 }
166 pr_(" -> probe non-zero\n" as *u8); sys_exit(1); return 1
167 }
168 }
169
170 // no-arg: deterministic SELF-GATE.
171 // T1 full walk DISCOVERED..EXCEEDED.
172 pl_seed_p(PR_GATE_PREFIX, "PR-A" as *u8, "demo gap" as *u8)
173 let fa: i64 = pr_run(PR_GATE_PREFIX, "PR-A" as *u8, 0)
174 var pass1: i64 = 0
175 if fa == PL_EXCEEDED { pass1 = 1 }
176 // T2 NEG (halt-on-failure): force the SPECED->BUILT step to fail -> item must stick at SPECED.
177 pl_seed_p(PR_GATE_PREFIX, "PR-B" as *u8, "fail gap" as *u8)
178 let fb: i64 = pr_run(PR_GATE_PREFIX, "PR-B" as *u8, PL_SPECED)
179 let sb: i64 = pl_stage_p(PR_GATE_PREFIX, "PR-B" as *u8)
180 var pass2: i64 = 0
181 if fb == PL_SPECED { if sb == PL_SPECED { pass2 = 1 } }
182 // T3 HONEST: walking a never-seeded item does nothing (stage 0), never fabricates a walk.
183 let fc: i64 = pr_run(PR_GATE_PREFIX, "PR-NEVER" as *u8, 0)
184 var pass3: i64 = 0
185 if fc == 0 { pass3 = 1 }
186
187 var green: i64 = 0
188 if pass1 == 1 { if pass2 == 1 { if pass3 == 1 { green = 1 } } }
189
190 pr_("PIPELINE-RUN-GATE walkA=" as *u8); prn(fa)
191 pr_(" failB_final=" as *u8); prn(fb); pr_(" B_stage=" as *u8); prn(sb)
192 pr_(" unseeded=" as *u8); prn(fc)
193 pr_(" T1=" as *u8); prn(pass1); pr_(" T2=" as *u8); prn(pass2); pr_(" T3=" as *u8); prn(pass3)
194
195 let buf: *u8 = sys_mmap(512)
196 var o: i64 = 0
197 o = fa_cat(buf, o, "PIPELINE-RUN-GATE ts=\x00" as *u8); o = fa_catn(buf, o, sys_now_realtime_sec())
198 o = fa_cat(buf, o, " T1=\x00" as *u8); o = fa_catn(buf, o, pass1)
199 o = fa_cat(buf, o, " T2=\x00" as *u8); o = fa_catn(buf, o, pass2)
200 o = fa_cat(buf, o, " T3=\x00" as *u8); o = fa_catn(buf, o, pass3)
201 if green == 1 { o = fa_cat(buf, o, " verdict=GREEN\x00" as *u8) } else { o = fa_cat(buf, o, " verdict=RED\x00" as *u8) }
202 fa_appendz(PR_LOG, buf, 512)
203
204 if green == 1 { pr_(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
205 pr_(" verdict=RED\n" as *u8); sys_exit(1); return 1
206}