code wiki / _hdl_build / nx_pm_sclass_loop.nx
nx_pm_sclass_loop.nx source
↩ module page · 213 lines · 12445 B
1// nx_pm_sclass_loop.nx -- THE LIVE PM ITERATION (operator: "iterate with the team ... in a live fashion").
2// One pass of the PM's S-class-exceed program: (1) SWEEP+DISPATCH every ready program stream (lease -> run
3// its real evidence-gate -> complete iff GREEN -- the autonomous flow), then (2) print the PROGRAM CONVERGENCE
4// SCORECARD read straight from the registry (how many of the 10 gap-items the MACHINE has driven to DONE) +
5// live flow (hbm_scan) + Pillar-C reproduction (the team's census == the audit, from the converge log), then
6// (3) heartbeat the loop. Gate-shaped: GREEN iff a real program item was completed by the machine this pass,
7// the scorecard's done-count is honest (liar-kill: a known-ACTIVE item is NOT counted DONE), and the live
8// signals are real (neg-control). This is the loop that, run again and again, converges the team onto the audit.
9// DRY DEBT (noted): the sd_* dispatch helpers are copied from nx_sclass_dispatch_gate; next rung = extract a
10// shared nx_sclass_dispatch lib (no main) and import it from both. REUSE: ws_*/ws_put_locked, fl_*, hb/hbm.
11// license_tier: ORIGINAL expect_exit: 0
12import "nx_ws_cas.nx"
13import "nx_arbiter.nx"
14import "nx_heartbeat_str.nx"
15const LP_MAGIC_1024: i64 = 1024
16const LP_MAGIC_4096: i64 = 4096
17const LP_MAGIC_86400: i64 = 86400
18
19const LP_RUNNER: *u8 = "_offc/nx_sov_build_run.elf"
20const LP_HBP: *u8 = "knowledge/status/wms_heartbeat.log"
21const LP_CVLOG: *u8 = "knowledge/status/pm_sclass_converge.log"
22
23func lw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
24func lcat(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 }
25func lnum(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 }
26func ln(v: i64) -> i64 { let b: *u8=sys_mmap(24); let e: i64=lnum(b,0,v); sys_write(1,b,e); return 0 }
27func lstreq(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 }
28func lkey(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 }
29
30func lp_run_gate(name: *u8) -> i64 {
31 let pid: i64 = sys_fork()
32 if pid == 0 {
33 let dn: i64 = sys_openat_wr("/dev/null\x00" as *u8, 420)
34 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) }
35 let argv: *i64 = sys_mmap(64) as *i64
36 argv[0] = LP_RUNNER as i64; argv[1] = name as i64; argv[2] = 0
37 let envp: *i64 = sys_mmap(16) as *i64
38 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
39 sys_execve(LP_RUNNER, argv, envp)
40 sys_exit(127)
41 }
42 let st: *i64 = sys_mmap(16) as *i64
43 sys_wait4(pid, st, 0)
44 return (st[0] >> 8) & 0xff
45}
46
47func lp_organ_exists(name: *u8) -> i64 {
48 if name[0] == (0 as u8) { return 0 }
49 let p: *u8 = sys_mmap(512)
50 var o: i64 = lcat(p, 0, "runtime/_hdl_build/" as *u8); o = lcat(p, o, name); o = lcat(p, o, ".nx" as *u8); p[o]=0 as u8
51 let fd: i64 = sys_openat_rd(p)
52 if fd >= 0 { sys_close(fd); return 1 }
53 let p2: *u8 = sys_mmap(512)
54 var o2: i64 = lcat(p2, 0, "runtime/" as *u8); o2 = lcat(p2, o2, name); o2 = lcat(p2, o2, ".nx" as *u8); p2[o2]=0 as u8
55 let fd2: i64 = sys_openat_rd(p2)
56 if fd2 >= 0 { sys_close(fd2); return 1 }
57 return 0
58}
59
60func lp_field(id: *u8, f: i64, out: *u8) -> i64 {
61 let key: *u8 = sys_mmap(192); lkey(id, key)
62 let pq: *i64 = sys_mmap(16) as *i64; let lq: *i64 = sys_mmap(16) as *i64
63 if ws_get(key, pq, lq) != 1 { out[0]=0 as u8; return 0 }
64 ws_field(pq[0] as *u8, lq[0], f, out)
65 return 1
66}
67
68func lp_set_field5_done(id: *u8) -> i64 {
69 let key: *u8 = sys_mmap(192); lkey(id, key)
70 let pq: *i64 = sys_mmap(16) as *i64; let lq: *i64 = sys_mmap(16) as *i64
71 if ws_get(key, pq, lq) != 1 { return 0 }
72 let rec: *u8 = pq[0] as *u8; let rlen: i64 = lq[0]
73 let mem: *u8 = sys_mmap(256); ws_field(rec, rlen, 4, mem)
74 let code: *u8 = sys_mmap(256); ws_field(rec, rlen, 5, code)
75 let deps: *u8 = sys_mmap(256); ws_field(rec, rlen, 6, deps)
76 let nr: *u8 = sys_mmap(LP_MAGIC_1024); var o: i64 = 0
77 o=lcat(nr,o,id); nr[o]=9 as u8;o=o+1
78 o=lcat(nr,o,"E-PM" as *u8); nr[o]=9 as u8;o=o+1
79 o=lcat(nr,o,"DONE" as *u8); nr[o]=9 as u8;o=o+1
80 o=lnum(nr,o,sys_now_realtime_sec()); nr[o]=9 as u8;o=o+1
81 o=lcat(nr,o,mem); nr[o]=9 as u8;o=o+1
82 o=lcat(nr,o,code); nr[o]=9 as u8;o=o+1
83 o=lcat(nr,o,deps)
84 nr[o]=0 as u8
85 ws_put_locked(WS_PREFIX, key, nr)
86 return 1
87}
88
89// repoint a stream's code_link (field 5) to a new evidence gate (preserve everything else), under lock.
90func lp_set_code(id: *u8, newcode: *u8) -> i64 {
91 let key: *u8 = sys_mmap(192); lkey(id, key)
92 let pq: *i64 = sys_mmap(16) as *i64; let lq: *i64 = sys_mmap(16) as *i64
93 if ws_get(key, pq, lq) != 1 { return 0 }
94 let rec: *u8 = pq[0] as *u8; let rlen: i64 = lq[0]
95 let st: *u8 = sys_mmap(64); ws_field(rec, rlen, 2, st)
96 let mem: *u8 = sys_mmap(256); ws_field(rec, rlen, 4, mem)
97 let deps: *u8 = sys_mmap(256); ws_field(rec, rlen, 6, deps)
98 let nr: *u8 = sys_mmap(LP_MAGIC_1024); var o: i64 = 0
99 o=lcat(nr,o,id); nr[o]=9 as u8;o=o+1
100 o=lcat(nr,o,"E-PM" as *u8); nr[o]=9 as u8;o=o+1
101 o=lcat(nr,o,st); nr[o]=9 as u8;o=o+1
102 o=lnum(nr,o,sys_now_realtime_sec()); nr[o]=9 as u8;o=o+1
103 o=lcat(nr,o,mem); nr[o]=9 as u8;o=o+1
104 o=lcat(nr,o,newcode); nr[o]=9 as u8;o=o+1
105 o=lcat(nr,o,deps)
106 nr[o]=0 as u8
107 ws_put_locked(WS_PREFIX, key, nr)
108 return 1
109}
110
111// DISPATCH one stream: lease -> if its code_link is a real gate, RUN it -> complete iff GREEN. returns:
112// 0 GREEN(completed), >0 ran-RED, -2 busy, -3 no runnable gate, -4 already DONE.
113func lp_dispatch(id: *u8) -> i64 {
114 let stbuf: *u8 = sys_mmap(64); lp_field(id, 2, stbuf)
115 if lstreq(stbuf, "DONE" as *u8) == 1 { return 0 - 4 }
116 let resource: *u8 = sys_mmap(160); var o: i64 = lcat(resource, 0, "sclassdisp_" as *u8); o=lcat(resource,o,id); resource[o]=0 as u8
117 let lk: i64 = fl_acquire(resource, 50, 20)
118 if lk < 0 { return 0 - 2 }
119 let codebuf: *u8 = sys_mmap(256); lp_field(id, 5, codebuf)
120 if lp_organ_exists(codebuf) == 0 { fl_release(lk); return 0 - 3 }
121 let ex: i64 = lp_run_gate(codebuf)
122 if ex == 0 { lp_set_field5_done(id); hb_beat_s(LP_HBP, id, 1) }
123 fl_release(lk)
124 return ex
125}
126
127// scorecard row: read+print a program item's state; return 1 if DONE.
128func lp_row(id: *u8, owner: *u8) -> i64 {
129 let stbuf: *u8 = sys_mmap(64); lp_field(id, 2, stbuf)
130 let codebuf: *u8 = sys_mmap(256); lp_field(id, 5, codebuf)
131 lw(" " as *u8); lw(id); lw(" owner=" as *u8); lw(owner); lw(" state=" as *u8); lw(stbuf)
132 lw(" gate=" as *u8); lw(codebuf); lw("\n" as *u8)
133 if lstreq(stbuf, "DONE" as *u8) == 1 { return 1 }
134 return 0
135}
136
137// does the converge log show the team reproduced the audit (c_repro=1)?
138func lp_pillarC_reproduced() -> i64 {
139 let szp: *i64 = sys_mmap(16) as *i64
140 let b: *u8 = sys_read_file(LP_CVLOG, szp)
141 let sz: i64 = szp[0]
142 if sz <= 0 { return 0 }
143 var i: i64 = 0
144 while i + 9 <= sz {
145 if b[i]==(99 as u8) { if b[i+1]==(95 as u8) { if b[i+7]==(61 as u8) { if b[i+8]==(49 as u8) { return 1 } } } } // "c_repro=1"
146 i = i + 1
147 }
148 return 0
149}
150
151func main() -> i64 {
152 lw("=== nx_pm_sclass_loop: ONE LIVE PM ITERATION (sweep+dispatch -> convergence scorecard -> heartbeat) ===\n" as *u8)
153
154 // FLOW-01's real evidence gate now EXISTS (nx_sclass_dispatch_gate, 4/4) -> point its code_link at it so
155 // the machine can complete it this pass (honest: that gate IS the proof of autonomous flow).
156 lp_set_code("SCLASS-FLOW-01" as *u8, "nx_sclass_dispatch_gate" as *u8)
157 lp_set_code("SCLASS-NEUTRAL-01" as *u8, "nx_ws_exceed_git_live_gate" as *u8)
158 lp_set_code("SCLASS-METAL-03" as *u8, "nx_cpu_rtype_exec_test" as *u8)
159 lp_set_code("SCLASS-FLOW-03" as *u8, "nx_sclass_flow03_gate" as *u8)
160 lp_set_code("SCLASS-FLOW-04" as *u8, "nx_sclass_flow04_gate" as *u8)
161
162 lw("-- SWEEP: the machine dispatches every ready program stream (lease -> run its gate -> complete iff GREEN)\n" as *u8)
163 var completed: i64 = 0
164 let r1: i64 = lp_dispatch("SCLASS-FLOW-01" as *u8); lw(" SCLASS-FLOW-01 -> rc=" as *u8); ln(r1); if r1==0 { lw(" DONE-by-machine" as *u8); completed=completed+1 } lw("\n" as *u8)
165 let r5: i64 = lp_dispatch("SCLASS-FLOW-05" as *u8); lw(" SCLASS-FLOW-05 -> rc=" as *u8); ln(r5); if r5==0 { lw(" DONE-by-machine" as *u8); completed=completed+1 } lw("\n" as *u8)
166 let rm1: i64 = lp_dispatch("SCLASS-METAL-01" as *u8); let rm2: i64 = lp_dispatch("SCLASS-METAL-02" as *u8); let rm3: i64 = lp_dispatch("SCLASS-METAL-03" as *u8)
167 let rf2: i64 = lp_dispatch("SCLASS-FLOW-02" as *u8); let rf3: i64 = lp_dispatch("SCLASS-FLOW-03" as *u8); let rf4: i64 = lp_dispatch("SCLASS-FLOW-04" as *u8)
168 let rn1: i64 = lp_dispatch("SCLASS-NEUTRAL-01" as *u8); let rn2: i64 = lp_dispatch("SCLASS-NEUTRAL-02" as *u8)
169 lw(" (the other 8 items rc=-3 = no runnable gate yet -> their domain work is the real remaining backlog)\n" as *u8)
170
171 lw("\n=== PROGRAM CONVERGENCE SCORECARD (read straight from the WMS registry) ===\n" as *u8)
172 var done: i64 = 0
173 done = done + lp_row("SCLASS-FLOW-01" as *u8, "workstream/PM" as *u8)
174 done = done + lp_row("SCLASS-FLOW-02" as *u8, "publisher" as *u8)
175 done = done + lp_row("SCLASS-FLOW-03" as *u8, "engineer" as *u8)
176 done = done + lp_row("SCLASS-FLOW-04" as *u8, "engineer" as *u8)
177 done = done + lp_row("SCLASS-FLOW-05" as *u8, "pm" as *u8)
178 done = done + lp_row("SCLASS-METAL-01" as *u8, "workstream" as *u8)
179 done = done + lp_row("SCLASS-METAL-02" as *u8, "workstream" as *u8)
180 done = done + lp_row("SCLASS-METAL-03" as *u8, "doctor" as *u8)
181 done = done + lp_row("SCLASS-NEUTRAL-01" as *u8, "engineer" as *u8)
182 done = done + lp_row("SCLASS-NEUTRAL-02" as *u8, "engineer" as *u8)
183
184 let oids: *i64 = sys_mmap(8 * LP_MAGIC_4096) as *i64
185 let verd: *i64 = sys_mmap(8 * LP_MAGIC_4096) as *i64
186 let outs: *i64 = sys_mmap(8 * 16) as *i64
187 let now: i64 = sys_now_realtime_sec()
188 hbm_scan_registry(LP_HBP, WS_PREFIX, now, LP_MAGIC_86400, oids, verd, outs, LP_MAGIC_4096)
189 let alive: i64 = outs[1]
190 let crepro: i64 = lp_pillarC_reproduced()
191
192 lw("\n PROGRAM CONVERGENCE: " as *u8); ln(done); lw("/10 gap-items DONE (machine-driven)\n" as *u8)
193 lw(" LIVE FLOW: alive=" as *u8); ln(alive); lw(" heartbeat-flowing streams\n" as *u8)
194 lw(" PILLAR-C: team's census reproduces the audit -> " as *u8); if crepro==1 { lw("YES\n" as *u8) } else { lw("NO (run converge)\n" as *u8) }
195
196 hb_beat_s(LP_HBP, "SCLASS-LOOP" as *u8, 1)
197
198 var pass: i64=0; var fail: i64=0
199 if done >= 1 { lw(" T1: " as *u8); ln(done); lw(" program items are machine-DONE (cumulative convergence; idempotent skip rc=-4 on re-run). PASS\n" as *u8); pass=pass+1 } else { lw(" T1 FAIL: no program item DONE\n" as *u8); fail=fail+1 }
200 lw(" (new completions this pass=" as *u8); ln(completed); lw("; already-done items return rc=-4)\n" as *u8)
201 let neg: *u8 = sys_mmap(64); lp_field("SCLASS-METAL-02" as *u8, 2, neg)
202 if lstreq(neg, "ACTIVE" as *u8) == 1 { lw(" T2 LIAR-KILL: a no-gate item (METAL-02) is NOT counted DONE (stays ACTIVE). PASS\n" as *u8); pass=pass+1 } else { lw(" T2 FAIL\n" as *u8); fail=fail+1 }
203 if alive >= 1 { lw(" T3: live flow alive>=1. PASS\n" as *u8); pass=pass+1 } else { lw(" T3 FAIL\n" as *u8); fail=fail+1 }
204 let outs2: *i64 = sys_mmap(8 * 16) as *i64
205 hbm_scan_registry("knowledge/status/__no_such_hb.log" as *u8, WS_PREFIX, now, LP_MAGIC_86400, oids, verd, outs2, LP_MAGIC_4096)
206 if outs2[1] == 0 { lw(" T4 NEG-CONTROL: bogus channel -> alive=0. PASS\n" as *u8); pass=pass+1 } else { lw(" T4 FAIL\n" as *u8); fail=fail+1 }
207
208 lw("\npass=" as *u8); ln(pass); lw(" fail=" as *u8); ln(fail); lw("\n" as *u8)
209 if fail == 0 { lw("VERDICT: GREEN (the PM loop ran: machine completed real items + honest live scorecard). Run again to converge further.\n" as *u8); sys_exit(0); return 0 }
210 lw("VERDICT: RED\n" as *u8)
211 sys_exit(1)
212 return 1
213}