code wiki / _hdl_build / nx_orchestrate_before_status_t49.nx
nx_orchestrate_before_status_t49.nx source
↩ module page · 174 lines · 8808 B
1// nx_orchestrate.nx -- ORCHESTRATION rung 3: the CONDUCTOR / the "call the API, it calls the team, the team
2// runs the workstream" engine (operator 2026-07-10: "standard mcp apis so that we call the apis that call the
3// teams that run the workstreams ... true orchestration like an orchestra ... key for autonomous capabilities").
4//
5// Given an ACTIVITY (workstream), nx_orchestrate:
6// (1) RESOLVES its RACI from the SSOT knowledge/registry/nishi_raci.tsv (role<TAB>activity<TAB>letters) --
7// the ONE Accountable role, the Responsible role(s), Consulted, Informed. NEVER fabricates: an activity
8// with no Accountable row -> DENY (returns -1), so the orchestra can't engage a role that doesn't own it.
9// (2) BINDS the runnable workstream ORGAN from knowledge/orchestration/workstreams.conf (activity<TAB>organ).
10// (3) RECORDS the engagement durably on the seg-store (orchestration- prefix) -- who was engaged, what ran.
11// (4) DISPATCHES: forks _offc/nx_sov_build_run.elf <organ> and wait4s it = the team actually runs the
12// workstream (only with the run flag; resolve/plan is side-effect-free).
13// This is what makes RACI-driven team orchestration STANDARD instead of needing the operator's explicit callout.
14// Exposed as the nishi_orchestrate MCP tool (fork-exec backend, the nishi_compare precedent). license_tier: ORIGINAL
15import "nx_syscalls.nx"
16import "nx_raci_sov.nx" // raci_read_all -- RACI from the SOVEREIGN store (NO TSV; flip 2026-07-16)
17import "nx_seg_store.nx"
18
19// OR_RACI retired 2026-07-16: RACI reads from the sovereign store (raci_read_all)
20const OR_WS: *u8 = "knowledge/orchestration/workstreams.conf"
21const OR_STORE:*u8 = "knowledge/store/orchestration-"
22
23func or_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
24func 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 }
25func or_w(s: *u8) -> i64 { sys_write(1, s, or_slen(s)); return 0 }
26func 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 }
27func or_read(path: *u8, buf: *u8, cap: i64) -> i64 {
28 let fd: i64 = sys_openat_rd(path); if fd<0 { return 0 }
29 var n: i64=0; var r: i64=sys_read(fd, buf, cap-1)
30 while r>0 { n=n+r; if n>=cap-1 { r=0 } else { r=sys_read(fd, buf+n, cap-1-n) } }
31 sys_close(fd); buf[n]=0 as u8; return n
32}
33// field col (0-based, TAB-delimited) of line [ls,le) into dst (NUL-term); returns len.
34func or_field(buf: *u8, ls: i64, le: i64, col: i64, dst: *u8) -> i64 {
35 var c: i64=0; var p: i64=ls; var k: i64=0
36 while p<le {
37 if buf[p]==(9 as u8) { if c==col { dst[k]=0 as u8; return k } c=c+1; if c==col { k=0 } }
38 else { if c==col { if buf[p]!=(13 as u8) { dst[k]=buf[p]; k=k+1 } } }
39 p=p+1
40 }
41 dst[k]=0 as u8; return k
42}
43func 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 }
44
45// Resolve every role for ACTIVITY whose RACI letters include L (A/R/C/I) -> comma-joined into out. returns count.
46func orch_roles(activity: *u8, L: u8, out: *u8) -> i64 {
47 let buf: *u8 = sys_mmap(131072); let n: i64 = raci_read_all(buf, 131072)
48 let role: *u8 = sys_mmap(64); let act: *u8 = sys_mmap(64); let let3: *u8 = sys_mmap(16)
49 var o: i64 = 0; var cnt: i64 = 0
50 var ls: i64=0; var i: i64=0
51 while i<=n {
52 var eol: i64=0
53 if i==n { eol=1 } else { if buf[i]==(10 as u8) { eol=1 } }
54 if eol==1 {
55 if i>ls { if buf[ls]!=(35 as u8) {
56 or_field(buf, ls, i, 0, role)
57 or_field(buf, ls, i, 1, act)
58 or_field(buf, ls, i, 2, let3)
59 if or_eq(act, activity)==1 { if or_has_letter(let3, L)==1 {
60 if cnt>0 { out[o]=44 as u8; o=o+1 } // ','
61 var j: i64=0; while role[j]!=(0 as u8){ out[o]=role[j]; o=o+1; j=j+1 }
62 cnt=cnt+1
63 } }
64 } }
65 ls=i+1
66 }
67 i=i+1
68 }
69 out[o]=0 as u8
70 return cnt
71}
72// the ONE Accountable role for activity into out; returns 1 found / 0 none (DENY: never fabricate).
73func orch_accountable(activity: *u8, out: *u8) -> i64 {
74 let tmp: *u8 = sys_mmap(256)
75 let c: i64 = orch_roles(activity, 65 as u8, tmp) // 'A'
76 if c < 1 { out[0]=0 as u8; return 0 }
77 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
78 return 1
79}
80// runnable organ bound to activity from workstreams.conf into out; returns 1 found / 0 none.
81func orch_organ(activity: *u8, out: *u8) -> i64 {
82 let buf: *u8 = sys_mmap(16384); let n: i64 = or_read(OR_WS, buf, 16384)
83 let act: *u8 = sys_mmap(64)
84 var ls: i64=0; var i: i64=0
85 while i<=n {
86 var eol: i64=0
87 if i==n { eol=1 } else { if buf[i]==(10 as u8) { eol=1 } }
88 if eol==1 {
89 if i>ls { if buf[ls]!=(35 as u8) {
90 or_field(buf, ls, i, 0, act)
91 if or_eq(act, activity)==1 { or_field(buf, ls, i, 1, out); return 1 }
92 } }
93 ls=i+1
94 }
95 i=i+1
96 }
97 out[0]=0 as u8; return 0
98}
99// record the engagement durably on the seg-store.
100func orch_record(activity: *u8, acct: *u8, organ: *u8, verdict: *u8) -> i64 {
101 let key: *u8 = sys_mmap(128); var ko: i64 = or_cat(key, 0, "orch:engage:" as *u8); or_cat(key, ko, activity)
102 let val: *u8 = sys_mmap(512); var o: i64 = 0
103 o = or_cat(val, o, "activity="); o = or_cat(val, o, activity)
104 o = or_cat(val, o, "|accountable="); o = or_cat(val, o, acct)
105 o = or_cat(val, o, "|organ="); o = or_cat(val, o, organ)
106 o = or_cat(val, o, "|verdict="); o = or_cat(val, o, verdict)
107 val[o]=0 as u8
108 let w: *i64 = ss_begin()
109 if ss_add(w, 1, key, val, o) != 0 { return 0-1 }
110 if ss_commit(OR_STORE, w, sys_now_us()) != 0 { return 0-2 }
111 return 0
112}
113// DISPATCH: fork+exec `_offc/nx_sov_build_run.elf <organ>` and wait; returns child exit code, or -1 on fork fail.
114func orch_dispatch(organ: *u8) -> i64 {
115 let pid: i64 = sys_fork()
116 if pid < 0 { return 0-1 }
117 if pid == 0 {
118 let argv: *i64 = sys_mmap(64) as *i64
119 argv[0] = "_offc/nx_sov_build_run.elf" as *u8 as i64
120 argv[1] = organ as i64
121 argv[2] = 0
122 let envp: *i64 = sys_mmap(16) as *i64; envp[0] = 0
123 sys_execve("_offc/nx_sov_build_run.elf" as *u8, argv, envp)
124 sys_exit(127)
125 }
126 let st: *i64 = sys_mmap(16) as *i64
127 sys_wait4(pid, st, 0)
128 let raw: i64 = st[0]
129 return (raw / 256) & 0xff // WEXITSTATUS
130}
131
132// ORCHESTRATE one activity. do_run: 0 = resolve+plan+record only; 1 = also dispatch the workstream.
133// returns 1 GREEN (engaged; if run, workstream exit 0) / 0 workstream-nonzero / -1 DENY (no accountable role).
134func orch_engage(activity: *u8, do_run: i64) -> i64 {
135 let acct: *u8 = sys_mmap(128)
136 if orch_accountable(activity, acct) == 0 {
137 or_w("ORCHESTRATE deny activity="); or_w(activity); or_w(" reason=no-accountable-role (not in the RACI SSOT)\n" as *u8)
138 return 0-1
139 }
140 let resp: *u8 = sys_mmap(256); orch_roles(activity, 82 as u8, resp) // 'R'
141 let cons: *u8 = sys_mmap(256); orch_roles(activity, 67 as u8, cons) // 'C'
142 let inf: *u8 = sys_mmap(256); orch_roles(activity, 73 as u8, inf) // 'I'
143 let organ: *u8 = sys_mmap(128); let hasorgan: i64 = orch_organ(activity, organ)
144 or_w("ORCHESTRATE activity="); or_w(activity)
145 or_w("\n Accountable="); or_w(acct)
146 or_w(" Responsible="); or_w(resp)
147 or_w("\n Consulted="); or_w(cons)
148 or_w(" Informed="); or_w(inf)
149 or_w("\n workstream-organ="); if hasorgan==1 { or_w(organ) } else { or_w("(none bound)" as *u8) }
150 or_w("\n" as *u8)
151 var verdict: *u8 = "PLANNED" as *u8
152 var rc: i64 = 1
153 if do_run == 1 { if hasorgan == 1 {
154 or_w(" DISPATCHING -> the "); or_w(acct); or_w(" team runs "); or_w(organ); or_w("...\n" as *u8)
155 let ex: i64 = orch_dispatch(organ)
156 if ex == 0 { verdict = "RAN-GREEN" as *u8 } else { verdict = "RAN-NONZERO" as *u8; rc = 0 }
157 } }
158 orch_record(activity, acct, organ, verdict)
159 or_w(" verdict="); or_w(verdict); or_w(" (engagement recorded on the seg-store)\n" as *u8)
160 return rc
161}
162
163func main(argc: i64, argv: *i64) -> i64 {
164 if argc < 2 {
165 or_w("usage: nx_orchestrate <activity> [run] (activity from the RACI store: research organize curate verify redteam ...)\n" as *u8)
166 return 1
167 }
168 let activity: *u8 = argv[1] as *u8
169 var do_run: i64 = 0
170 if argc >= 3 { if or_eq(argv[2] as *u8, "run" as *u8)==1 { do_run=1 } }
171 let r: i64 = orch_engage(activity, do_run)
172 if r < 0 { return 1 }
173 return 0
174}