nx_orchestrate_raci.nx source
↩ module page · 191 lines · 9898 B
1// nx_orchestrate_raci.nx -- ORCHESTRATION rung 3: the CONDUCTOR / the "call the API, it calls the team,
2// the team runs the workstream" engine (operator 2026-07-10: "standard mcp apis so that we call the apis
3// that call the teams that run the workstreams ... true orchestration like an orchestra ... key for
4// autonomous capabilities").
5//
6// ---- seq1789: WHY THIS FILE WAS RENAMED (restored capability, do not undo) --------------------------
7// This organ was `runtime/nx_orchestrate.nx`. A deduplication pass matched it against a COMPLETELY
8// DIFFERENT organ that happened to share the filename -- `runtime/_hdl_build/nx_orchestrate.nx`, the
9// wait-for-opening DEPLOY QUEUE POLLER -- and retired this one to `.dupe-reconciled`. They are not
10// duplicates: one resolves RACI and engages a team, the other polls a deploy queue. Nothing in their
11// CONTENT is shared; only the name was.
12// What that cost, measured 2026-07-30: nx_orchestrate_gate could no longer compile (expand_imports
13// failed), so it kept running its STALE binary -- which HANGS after two checks. That hang propagated:
14// nx_swcompare_evidence forks its domain gates with no deadline, so `librarian` never returned at all and
15// the failure surfaced as an MCP transport error, blaming the network for a retired source file.
16// LAW: A DEDUPLICATOR THAT MATCHES ON NAME RETIRES CAPABILITY, NOT DUPLICATION. Sameness is a property
17// of CONTENT; a filename is a label someone chose twice. Same family as the substring/role confusion in
18// md_promote_deny -- identity inferred from a name instead of read from the thing itself.
19// The name is now distinct from the poller's, so the collision cannot recur rather than being remembered.
20//
21// Given an ACTIVITY (workstream), this organ:
22// (1) RESOLVES its RACI from the sovereign RACI store -- the ONE Accountable role, the Responsible
23// role(s), Consulted, Informed. NEVER fabricates: an activity with no Accountable row -> DENY
24// (returns -1), so the orchestra cannot engage a role that does not own the work.
25// (2) BINDS the runnable workstream ORGAN from knowledge/orchestration/workstreams.conf (activity<TAB>organ).
26// (3) RECORDS the engagement durably on the seg-store (orchestration- prefix) -- who was engaged, what ran.
27// (4) DISPATCHES: forks _offc/nx_sov_build_run.elf <organ> and wait4s it = the team actually runs the
28// workstream (only with the run flag; resolve/plan is side-effect-free).
29// license_tier: ORIGINAL
30import "nx_syscalls.nx"
31import "nx_raci_sov.nx"
32import "nx_seg_store.nx"
33const OR_MAGIC_131072: i64 = 131072
34const OR_MAGIC_16384: i64 = 16384
35
36// OR_RACI retired 2026-07-16: RACI reads from the sovereign store (raci_read_all)
37const OR_WS: *u8 = "knowledge/orchestration/workstreams.conf"
38const OR_STORE:*u8 = "knowledge/store/orchestration-"
39
40func or_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
41func or_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){d[o]=s[i];o=o+1;i=i+1} d[o]=0 as u8; return o }
42func or_w(s: *u8) -> i64 { sys_write(1, s, or_slen(s)); return 0 }
43func or_eq(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 }
44func or_read(path: *u8, buf: *u8, cap: i64) -> i64 {
45 let fd: i64 = sys_openat_rd(path); if fd<0 { return 0 }
46 var n: i64=0; var r: i64=sys_read(fd, buf, cap-1)
47 while r>0 { n=n+r; if n>=cap-1 { r=0 } else { r=sys_read(fd, buf+n, cap-1-n) } }
48 sys_close(fd); buf[n]=0 as u8; return n
49}
50// field col (0-based, TAB-delimited) of line [ls,le) into dst (NUL-term); returns len.
51func or_field(buf: *u8, ls: i64, le: i64, col: i64, dst: *u8) -> i64 {
52 var c: i64=0; var p: i64=ls; var k: i64=0
53 while p<le {
54 if buf[p]==(9 as u8) { if c==col { dst[k]=0 as u8; return k } c=c+1; if c==col { k=0 } }
55 else { if c==col { if buf[p]!=(13 as u8) { dst[k]=buf[p]; k=k+1 } } }
56 p=p+1
57 }
58 dst[k]=0 as u8; return k
59}
60func or_has_letter(letters: *u8, L: u8) -> i64 { var i: i64=0; while letters[i]!=(0 as u8){ if letters[i]==L { return 1 } i=i+1 } return 0 }
61
62// Resolve every role for ACTIVITY whose RACI letters include L (A/R/C/I) -> comma-joined into out. returns count.
63func orch_roles(activity: *u8, L: u8, out: *u8) -> i64 {
64 let buf: *u8 = sys_mmap(OR_MAGIC_131072); let n: i64 = raci_read_all(buf, OR_MAGIC_131072)
65 let role: *u8 = sys_mmap(64); let act: *u8 = sys_mmap(64); let let3: *u8 = sys_mmap(16)
66 var o: i64 = 0; var cnt: i64 = 0
67 var ls: i64=0; var i: i64=0
68 while i<=n {
69 var eol: i64=0
70 if i==n { eol=1 } else { if buf[i]==(10 as u8) { eol=1 } }
71 if eol==1 {
72 if i>ls { if buf[ls]!=(35 as u8) {
73 or_field(buf, ls, i, 0, role)
74 or_field(buf, ls, i, 1, act)
75 or_field(buf, ls, i, 2, let3)
76 if or_eq(act, activity)==1 { if or_has_letter(let3, L)==1 {
77 if cnt>0 { out[o]=44 as u8; o=o+1 }
78 var j: i64=0; while role[j]!=(0 as u8){ out[o]=role[j]; o=o+1; j=j+1 }
79 cnt=cnt+1
80 } }
81 } }
82 ls=i+1
83 }
84 i=i+1
85 }
86 out[o]=0 as u8
87 return cnt
88}
89// the ONE Accountable role for activity into out; returns 1 found / 0 none (DENY: never fabricate).
90func orch_accountable(activity: *u8, out: *u8) -> i64 {
91 let tmp: *u8 = sys_mmap(256)
92 let c: i64 = orch_roles(activity, 65 as u8, tmp)
93 if c < 1 { out[0]=0 as u8; return 0 }
94 var i: i64=0; while tmp[i]!=(0 as u8){ if tmp[i]==(44 as u8) { out[i]=0 as u8; return 1 } out[i]=tmp[i]; i=i+1 } out[i]=0 as u8
95 return 1
96}
97// runnable organ bound to activity from workstreams.conf into out; returns 1 found / 0 none.
98func orch_organ(activity: *u8, out: *u8) -> i64 {
99 let buf: *u8 = sys_mmap(OR_MAGIC_16384); let n: i64 = or_read(OR_WS, buf, OR_MAGIC_16384)
100 let act: *u8 = sys_mmap(64)
101 var ls: i64=0; var i: i64=0
102 while i<=n {
103 var eol: i64=0
104 if i==n { eol=1 } else { if buf[i]==(10 as u8) { eol=1 } }
105 if eol==1 {
106 if i>ls { if buf[ls]!=(35 as u8) {
107 or_field(buf, ls, i, 0, act)
108 if or_eq(act, activity)==1 { or_field(buf, ls, i, 1, out); return 1 }
109 } }
110 ls=i+1
111 }
112 i=i+1
113 }
114 out[0]=0 as u8; return 0
115}
116// record the engagement durably on the seg-store.
117func orch_record(activity: *u8, acct: *u8, organ: *u8, verdict: *u8) -> i64 {
118 let key: *u8 = sys_mmap(128); var ko: i64 = or_cat(key, 0, "orch:engage:" as *u8); or_cat(key, ko, activity)
119 let val: *u8 = sys_mmap(512); var o: i64 = 0
120 o = or_cat(val, o, "activity="); o = or_cat(val, o, activity)
121 o = or_cat(val, o, "|accountable="); o = or_cat(val, o, acct)
122 o = or_cat(val, o, "|organ="); o = or_cat(val, o, organ)
123 o = or_cat(val, o, "|verdict="); o = or_cat(val, o, verdict)
124 val[o]=0 as u8
125 let w: *i64 = ss_begin()
126 if ss_add(w, 1, key, val, o) != 0 { return 0-1 }
127 if ss_commit(OR_STORE, w, sys_now_us()) != 0 { return 0-2 }
128 return 0
129}
130// DISPATCH: fork+exec `_offc/nx_sov_build_run.elf <organ>` and wait; returns child exit code, or -1 on fork fail.
131func orch_dispatch(organ: *u8) -> i64 {
132 let pid: i64 = sys_fork()
133 if pid < 0 { return 0-1 }
134 if pid == 0 {
135 let argv: *i64 = sys_mmap(64) as *i64
136 argv[0] = "_offc/nx_sov_build_run.elf" as *u8 as i64
137 argv[1] = organ as i64
138 argv[2] = 0
139 let envp: *i64 = sys_mmap(16) as *i64; envp[0] = 0
140 sys_execve("_offc/nx_sov_build_run.elf" as *u8, argv, envp)
141 sys_exit(127)
142 }
143 let st: *i64 = sys_mmap(16) as *i64
144 sys_wait4(pid, st, 0)
145 let raw: i64 = st[0]
146 return (raw / 256) & 0xff
147}
148
149// ORCHESTRATE one activity. do_run: 0 = resolve+plan+record only; 1 = also dispatch the workstream.
150// returns 1 GREEN (engaged; if run, workstream exit 0) / 0 workstream-nonzero / -1 DENY (no accountable role).
151func orch_engage(activity: *u8, do_run: i64) -> i64 {
152 let acct: *u8 = sys_mmap(128)
153 if orch_accountable(activity, acct) == 0 {
154 or_w("ORCHESTRATE deny activity="); or_w(activity); or_w(" reason=no-accountable-role (not in the RACI SSOT)\n" as *u8)
155 return 0-1
156 }
157 let resp: *u8 = sys_mmap(256); orch_roles(activity, 82 as u8, resp)
158 let cons: *u8 = sys_mmap(256); orch_roles(activity, 67 as u8, cons)
159 let inf: *u8 = sys_mmap(256); orch_roles(activity, 73 as u8, inf)
160 let organ: *u8 = sys_mmap(128); let hasorgan: i64 = orch_organ(activity, organ)
161 or_w("ORCHESTRATE activity="); or_w(activity)
162 or_w("\n Accountable="); or_w(acct)
163 or_w(" Responsible="); or_w(resp)
164 or_w("\n Consulted="); or_w(cons)
165 or_w(" Informed="); or_w(inf)
166 or_w("\n workstream-organ="); if hasorgan==1 { or_w(organ) } else { or_w("(none bound)" as *u8) }
167 or_w("\n" as *u8)
168 var verdict: *u8 = "PLANNED" as *u8
169 var rc: i64 = 1
170 if do_run == 1 { if hasorgan == 1 {
171 or_w(" DISPATCHING -> the "); or_w(acct); or_w(" team runs "); or_w(organ); or_w("...\n" as *u8)
172 let ex: i64 = orch_dispatch(organ)
173 if ex == 0 { verdict = "RAN-GREEN" as *u8 } else { verdict = "RAN-NONZERO" as *u8; rc = 0 }
174 } }
175 orch_record(activity, acct, organ, verdict)
176 or_w(" verdict="); or_w(verdict); or_w(" (engagement recorded on the seg-store)\n" as *u8)
177 return rc
178}
179
180func main(argc: i64, argv: *i64) -> i64 {
181 if argc < 2 {
182 or_w("usage: nx_orchestrate_raci <activity> [run] (activity from the RACI store: research organize curate verify redteam ...)\n" as *u8)
183 return 1
184 }
185 let activity: *u8 = argv[1] as *u8
186 var do_run: i64 = 0
187 if argc >= 3 { if or_eq(argv[2] as *u8, "run" as *u8)==1 { do_run=1 } }
188 let r: i64 = orch_engage(activity, do_run)
189 if r < 0 { return 1 }
190 return 0
191}