code wiki / _hdl_build / nx_capability_placement.nx
nx_capability_placement.nx source
↩ module page · 201 lines · 12986 B
1// nx_capability_placement.nx -- the "TRUE RACI PLACEMENT" governance instrument (operator 2026-07-16:
2// "make sure everything logically ... is getting true RACI aka what should live in workstreams, in agents,
3// in apis, in mcp, etc"). RACI answers WHO is accountable; this answers WHERE the capability should LIVE.
4// The architect (composition) + warden (policy bounds) own it. A standing rubric that gives every capability
5// a placement verdict + flags misplacements -> prevents future sprawl by construction (every new capability
6// gets a home BY RULE, not by accident).
7//
8// THE SURFACES (where a capability can live):
9// ORGAN -- internal library/primitive/gate/vocab: imported by other organs, NEVER directly exposed
10// API -- a served network surface (daemon w/ a port): machine-to-machine, HTTP
11// MCP -- an AGENT-callable tool: the crew/Claude invokes it directly (read=broad, write=cap, destr=ocap+confirm)
12// WORKSTREAM -- a multi-step, supervised/human-checkpointed BUILD or campaign (WMS work-unit), not a single call
13// AGENT -- a capability a crew ROLE owns + runs autonomously (nishi_crew dispatch); the role IS the agent
14// HUMAN -- a person's judgment (operator/sponsor); not automatable
15//
16// ★THE PURE RULE (cp_place), priority-ordered + adversarially GATED (un-gameable):
17// 0. no single Accountable role -> RACI-GAP (assign an owner BEFORE placing -- accountability first)
18// 1. internal (primitive/gate/vocab) -> ORGAN (and if currently MCP -> VIOLATION: over-exposed primitive)
19// 2. multi-step campaign / human-judg. -> WORKSTREAM
20// 3. role-autonomous -> AGENT (dispatch)
21// 4. served (daemon/port) -> API
22// 5. read-only, agent-relevant -> MCP (broad)
23// 6. write/mutating -> MCP + cap-gate
24// 7. destructive/irreversible -> MCP + ocap + confirm (never a bare API/MCP)
25// The order enforces the doctrine: accountability first, internal-never-exposed, destructive-always-guarded.
26//
27// nx_capability_placement gate -- adversarial self-test (default)
28// nx_capability_placement classify <capfile> -- placement verdict + action per crew capability row
29// license_tier: ORIGINAL module: nishi-core.architect.placement
30import "nx_syscalls.nx"
31const CP_MAGIC_262144: i64 = 262144
32const CP_MAGIC_262143: i64 = 262143
33
34// surfaces
35const CP_ORGAN: i64 = 1
36const CP_API: i64 = 2
37const CP_MCP: i64 = 3
38const CP_WORKSTREAM: i64 = 4
39const CP_AGENT: i64 = 5
40const CP_HUMAN: i64 = 6
41const CP_RACI_GAP: i64 = 0
42// io classes
43const CP_IO_NONE: i64 = 0
44const CP_IO_READ: i64 = 1
45const CP_IO_WRITE: i64 = 2
46const CP_IO_DESTRUCT:i64 = 3
47
48func cp_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
49func cp_pn_i(v: i64) -> i64 { let b: *u8 = sys_mmap(24); var m: i64 = v; var k: i64 = 0; let t: *u8 = sys_mmap(24); if m == 0 { t[0] = 48 as u8; k = 1 } while m > 0 { t[k] = (48+(m%10)) as u8; m = m/10; k = k+1 } var i: i64 = 0; while i < k { b[i] = t[k-1-i]; i = i+1 } sys_write(1, b, k); return 0 }
50func cp_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 }
51func cp_ends(s: *u8, suf: *u8) -> i64 {
52 var sl: i64 = 0; while s[sl] != (0 as u8) { sl = sl + 1 }
53 var fl: i64 = 0; while suf[fl] != (0 as u8) { fl = fl + 1 }
54 if fl > sl { return 0 }
55 var i: i64 = 0; while i < fl { if s[sl-fl+i] != suf[i] { return 0 } i = i + 1 }
56 return 1
57}
58func cp_contains(s: *u8, needle: *u8) -> i64 {
59 var sl: i64 = 0; while s[sl] != (0 as u8) { sl = sl + 1 }
60 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
61 if nl == 0 { return 1 } if nl > sl { return 0 }
62 var i: i64 = 0
63 while i + nl <= sl { var j: i64 = 0; var m: i64 = 1; while j < nl { if s[i+j] != needle[j] { m = 0; j = nl } else { j = j + 1 } } if m == 1 { return 1 } i = i + 1 }
64 return 0
65}
66func cp_sname(v: i64) -> *u8 {
67 if v == CP_ORGAN { return "ORGAN" as *u8 }
68 if v == CP_API { return "API" as *u8 }
69 if v == CP_MCP { return "MCP" as *u8 }
70 if v == CP_WORKSTREAM { return "WORKSTREAM" as *u8 }
71 if v == CP_AGENT { return "AGENT" as *u8 }
72 if v == CP_HUMAN { return "HUMAN" as *u8 }
73 return "RACI-GAP" as *u8
74}
75
76// ★THE PURE RULE (gated). is_human overrides. Order = the doctrine.
77func cp_place(has_a: i64, is_human: i64, is_internal: i64, is_multistep: i64, is_role_auto: i64, is_served: i64, io: i64) -> i64 {
78 if is_human == 1 { return CP_HUMAN }
79 if has_a != 1 { return CP_RACI_GAP } // accountability FIRST
80 if is_internal == 1 { return CP_ORGAN } // primitives/gates/vocab never exposed
81 if is_multistep == 1 { return CP_WORKSTREAM }
82 if is_role_auto == 1 { return CP_AGENT }
83 if is_served == 1 { return CP_API }
84 return CP_MCP // read/write/destruct all land on MCP (guard-level differs, below)
85}
86// the MCP guard level a given io demands (0 none/read=broad, 1 cap, 2 ocap+confirm) -- destructive is never bare
87func cp_guard(io: i64) -> i64 {
88 if io == CP_IO_DESTRUCT { return 2 }
89 if io == CP_IO_WRITE { return 1 }
90 return 0
91}
92
93// ---- adversarial gate: the doctrine must hold ----
94func cp_gate() -> i64 {
95 cp_p("=== nx_capability_placement GATE -- accountability-first, internal-never-exposed, destructive-guarded ===\n" as *u8)
96 var fail: i64 = 0
97 // T1 no Accountable -> RACI-GAP (before any placement)
98 if cp_place(0, 0, 0, 0, 0, 0, CP_IO_READ) == CP_RACI_GAP { cp_p(" [PASS] no Accountable -> RACI-GAP (assign owner first)\n" as *u8) } else { cp_p(" [FAIL] no-A\n" as *u8); fail = 1 }
99 // T2 ★internal primitive -> ORGAN even if someone wants it exposed (over-exposure is the violation)
100 if cp_place(1, 0, 1, 0, 0, 0, CP_IO_READ) == CP_ORGAN { cp_p(" [PASS] internal primitive -> ORGAN (never MCP -- over-exposing a primitive is the violation)\n" as *u8) } else { cp_p(" [FAIL] internal\n" as *u8); fail = 1 }
101 // T3 multi-step campaign -> WORKSTREAM, not a single MCP call
102 if cp_place(1, 0, 0, 1, 0, 0, CP_IO_WRITE) == CP_WORKSTREAM { cp_p(" [PASS] multi-step campaign -> WORKSTREAM (not a single call)\n" as *u8) } else { cp_p(" [FAIL] multistep\n" as *u8); fail = 1 }
103 // T4 role-autonomous -> AGENT (dispatch)
104 if cp_place(1, 0, 0, 0, 1, 0, CP_IO_READ) == CP_AGENT { cp_p(" [PASS] role-autonomous -> AGENT (nishi_crew dispatch)\n" as *u8) } else { cp_p(" [FAIL] agent\n" as *u8); fail = 1 }
105 // T5 served daemon -> API
106 if cp_place(1, 0, 0, 0, 0, 1, CP_IO_READ) == CP_API { cp_p(" [PASS] served daemon -> API\n" as *u8) } else { cp_p(" [FAIL] api\n" as *u8); fail = 1 }
107 // T6 read-only agent-relevant -> MCP (broad, guard=0)
108 if cp_place(1, 0, 0, 0, 0, 0, CP_IO_READ) == CP_MCP { if cp_guard(CP_IO_READ) == 0 { cp_p(" [PASS] read-only -> MCP broad (guard=0)\n" as *u8) } else { cp_p(" [FAIL] read-guard\n" as *u8); fail = 1 } } else { cp_p(" [FAIL] read-mcp\n" as *u8); fail = 1 }
109 // T7 ★destructive -> MCP but guard=2 (ocap+confirm), NEVER bare
110 if cp_place(1, 0, 0, 0, 0, 0, CP_IO_DESTRUCT) == CP_MCP { if cp_guard(CP_IO_DESTRUCT) == 2 { cp_p(" [PASS] destructive -> MCP + ocap + confirm (guard=2, never bare)\n" as *u8) } else { cp_p(" [FAIL] destr-guard not 2\n" as *u8); fail = 1 } } else { cp_p(" [FAIL] destr\n" as *u8); fail = 1 }
111 // T8 human -> HUMAN regardless
112 if cp_place(1, 1, 1, 1, 1, 1, CP_IO_DESTRUCT) == CP_HUMAN { cp_p(" [PASS] human judgment -> HUMAN (not automatable)\n" as *u8) } else { cp_p(" [FAIL] human\n" as *u8); fail = 1 }
113 if fail == 0 { cp_p("=== GATE GREEN: the placement doctrine holds -- every capability gets a home BY RULE ===\n" as *u8); sys_exit(0); return 0 }
114 cp_p("=== GATE RED ===\n" as *u8); sys_exit(1); return 1
115}
116
117// pipe-field n of line[ls..le) -> out
118func cp_field(buf: *u8, ls: i64, le: i64, n: i64, out: *u8, cap: i64) -> i64 {
119 var fs: i64 = ls; var seen: i64 = 0
120 while seen < n { if fs >= le { out[0] = 0 as u8; return 0 } if (buf[fs] as i64) == 124 { seen = seen + 1 } fs = fs + 1 }
121 var fe: i64 = fs; var g: i64 = 1
122 while g == 1 { if fe >= le { g = 0 } else { if (buf[fe] as i64) == 124 { g = 0 } else { fe = fe + 1 } } }
123 var o: i64 = 0; var k: i64 = fs
124 while k < fe { if (buf[k] as i64) != 13 { if o < cap - 1 { out[o] = buf[k]; o = o + 1 } } k = k + 1 }
125 out[o] = 0 as u8; return o
126}
127
128// infer capability properties from the row (organ name conventions + status), apply the rule, print verdict+action.
129// crew_capabilities.data columns: role | activity | organ | mcp_tool | status(mcp|dispatch|organ|concept|human)
130func cp_classify(path: *u8) -> i64 {
131 let fd: i64 = sys_openat_rd(path)
132 if fd < 0 { cp_p("classify: cannot read capability file\n" as *u8); return 1 }
133 let buf: *u8 = sys_mmap(CP_MAGIC_262144)
134 var n: i64 = 0; var r: i64 = 1
135 while r > 0 { r = sys_read(fd, (buf as i64 + n) as *u8, CP_MAGIC_262143 - n); if r > 0 { n = n + r } if n >= CP_MAGIC_262143 { r = 0 } }
136 sys_close(fd)
137 cp_p("=== TRUE RACI PLACEMENT -- where each crew capability should live (rule-derived) ===\n" as *u8)
138 let role: *u8 = sys_mmap(64); let act: *u8 = sys_mmap(64); let organ: *u8 = sys_mmap(96); let tool: *u8 = sys_mmap(64); let stat: *u8 = sys_mmap(32)
139 var gaps: i64 = 0; var exposeq: i64 = 0; var ok: i64 = 0; var viol: i64 = 0
140 var ls: i64 = 0; var i: i64 = 0
141 while i <= n {
142 var eol: i64 = 0; if i >= n { eol = 1 } else { if buf[i] == (10 as u8) { eol = 1 } }
143 if eol == 1 {
144 if i > ls { if buf[ls] != (35 as u8) {
145 cp_field(buf, ls, i, 0, role, 64)
146 cp_field(buf, ls, i, 1, act, 64)
147 cp_field(buf, ls, i, 2, organ, 96)
148 cp_field(buf, ls, i, 4, stat, 32)
149 if role[0] != (0 as u8) {
150 // property inference (honest heuristics)
151 let is_human: i64 = cp_streq(stat, "human" as *u8)
152 let has_a: i64 = 1 // rows in the ledger are RACI-owned by construction
153 var is_internal: i64 = 0
154 if cp_ends(organ, "_gate" as *u8) == 1 { is_internal = 1 }
155 if cp_ends(organ, "_test" as *u8) == 1 { is_internal = 1 }
156 var is_served: i64 = 0
157 if cp_contains(organ, "_daemon" as *u8) == 1 { is_served = 1 }
158 if cp_contains(organ, "_serve" as *u8) == 1 { is_served = 1 }
159 if cp_contains(organ, "hostctl" as *u8) == 1 { is_served = 1 }
160 // role-autonomous = the role dispatches this activity (status dispatch)
161 let is_role_auto: i64 = cp_streq(stat, "dispatch" as *u8)
162 let want: i64 = cp_place(has_a, is_human, is_internal, 0, is_role_auto, is_served, CP_IO_READ)
163 // current placement from status
164 cp_p(" " as *u8); cp_p(role); cp_p(" / " as *u8); cp_p(act)
165 cp_p(" now=" as *u8); cp_p(stat); cp_p(" -> should=" as *u8); cp_p(cp_sname(want))
166 // verdict
167 if cp_streq(stat, "concept" as *u8) == 1 { cp_p(" [BUILD-GAP: no organ -- build it]" as *u8); gaps = gaps + 1 }
168 else { if cp_streq(stat, "organ" as *u8) == 1 {
169 if want == CP_ORGAN { cp_p(" [OK: correctly internal]" as *u8); ok = ok + 1 }
170 else { cp_p(" [EXPOSE-GAP: built but not on " as *u8); cp_p(cp_sname(want)); cp_p("]" as *u8); exposeq = exposeq + 1 }
171 }
172 else { if cp_streq(stat, "mcp" as *u8) == 1 {
173 if is_internal == 1 { cp_p(" [VIOLATION: primitive over-exposed as MCP]" as *u8); viol = viol + 1 }
174 else { cp_p(" [OK: agent-callable]" as *u8); ok = ok + 1 }
175 }
176 else { cp_p(" [OK]" as *u8); ok = ok + 1 } } }
177 cp_p("\n" as *u8)
178 }
179 } }
180 ls = i + 1
181 }
182 i = i + 1
183 }
184 cp_p("--- placement summary: ok=" as *u8); cp_pn_i(ok)
185 cp_p(" build-gaps=" as *u8); cp_pn_i(gaps)
186 cp_p(" expose-gaps=" as *u8); cp_pn_i(exposeq)
187 cp_p(" VIOLATIONS=" as *u8); cp_pn_i(viol); cp_p("\n" as *u8)
188 if viol > 0 { return 1 }
189 return 0
190}
191
192func main(argc: i64, argv: *i64) -> i64 {
193 if argc < 2 { return cp_gate() }
194 if cp_streq(argv[1] as *u8, "gate" as *u8) == 1 { return cp_gate() }
195 if cp_streq(argv[1] as *u8, "classify" as *u8) == 1 {
196 if argc < 3 { cp_p("usage: nx_capability_placement classify <capfile>\n" as *u8); return 2 }
197 return cp_classify(argv[2] as *u8)
198 }
199 cp_p("usage: nx_capability_placement gate | classify <capfile>\n" as *u8)
200 return 2
201}