code wiki / (root) / nx_crew_capability_gate.nx

nx_crew_capability_gate.nx source

↩ module page · 143 lines · 9919 B

1// nx_crew_capability_gate.nx -- does each RACI role OWN its accountable capability, and is it an MCP API? (operator 2026-07-10: 2// "each of these roles needs to own their raci capabilities and get their capabilities to be mcp apis"). Reads 3// knowledge/registry/crew_capabilities.data (role|activity|organ|mcp_tool|status) + knowledge/registry/mcp_tools_live.txt 4// (the LIVE /api/tools snapshot). LIAR-KILL: every row that CLAIMS status=mcp must name a tool that is ACTUALLY in the live 5// snapshot -- a role cannot claim an MCP API it does not have. Reports MCP-exposure coverage (mcp roles / non-human roles) and 6// lists the organ-only roles = the drive-to-100 work queue. NEG-CONTROL: a synthetic ghost tool must be detected as NOT live. 7// NOTE: no '#'/'!' in string literals (nx_cc lexer trap). license_tier: ORIGINAL expect_exit: 0 8import "nx_syscalls.nx" 9 10const CW_CAP: i64 = 262144 11const CW_MAXT: i64 = 128 12const CW_STR: i64 = 64 13 14func cw_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 15func cw_wn(v: i64) -> i64 { var m: i64=v; let t: *u8=sys_mmap(24); var k: i64=0; 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} let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0{o[w]=t[q];w=w+1;q=q-1} sys_write(1,o,w); return 0 } 16func cw_read(path: *u8, buf: *u8, cap: i64) -> i64 { 17 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 } 18 var off: i64 = 0; var go: i64 = 1 19 while go == 1 { if off >= cap { go = 0 } else { let r: i64 = sys_read(fd, buf + off, cap - off); if r <= 0 { go = 0 } else { off = off + r } } } 20 sys_close(fd); return off 21} 22func cw_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 } 23// pipe(124)-delimited field n of line[0..len) -> out (NUL-term). CR(13) skipped so CRLF files parse clean. 24func cw_fieldn(line: *u8, len: i64, n: i64, out: *u8, outcap: i64) -> i64 { 25 var i: i64=0; var f: i64=0; var w: i64=0 26 while i < len { 27 let c: i64 = line[i] as i64 28 if c == 124 { f = f + 1; i = i + 1 } else { if c != 13 { if f == n { if w < outcap-1 { out[w]=line[i]; w=w+1 } } } i = i + 1 } 29 } 30 out[w]=0 as u8; return 0 31} 32func cw_has(set: *u8, n: i64, name: *u8) -> i64 { var i: i64=0; while i<n { let off: i64=i*CW_STR; if cw_streq((set as i64 + off) as *u8, name)==1 {return 1} i=i+1 } return 0 } 33// load every non-comment / non-blank line of a file as a set entry (field 0). returns count. 34func cw_load_set(path: *u8, set: *u8, maxn: i64) -> i64 { 35 let buf: *u8 = sys_mmap(CW_CAP); let n: i64 = cw_read(path, buf, CW_CAP) 36 if n <= 0 { return 0 - 1 } 37 let f: *u8 = sys_mmap(CW_STR) 38 var cnt: i64 = 0; var ls: i64 = 0; var i: i64 = 0 39 while i <= n { 40 var nl: i64 = 0; if i>=n {nl=1} else { if buf[i]==(10 as u8) {nl=1} } 41 if nl == 1 { 42 let ll: i64 = i - ls 43 if ll > 0 { 44 let line: *u8 = ((buf as i64)+ls) as *u8 45 if line[0] != (35 as u8) { 46 cw_fieldn(line, ll, 0, f, CW_STR) 47 if f[0] != (0 as u8) { if cw_has(set, cnt, f)==0 { if cnt<maxn { let off: i64=cnt*CW_STR; var k: i64=0; while f[k]!=(0 as u8){ let d: *u8=(set as i64 + off + k) as *u8; d[0]=f[k]; k=k+1 } let z: *u8=(set as i64 + off + k) as *u8; z[0]=0 as u8; cnt=cnt+1 } } } 48 } 49 } 50 ls = i + 1 51 } 52 i = i + 1 53 } 54 return cnt 55} 56 57func cw_scopy(dst: *u8, off: i64, src: *u8) -> i64 { var i: i64=0; while src[i]!=(0 as u8){ let d: *u8=(dst as i64 + off + i) as *u8; d[0]=src[i]; i=i+1 } return off+i } 58func cw_contains(hay: *u8, needle: *u8) -> i64 { 59 var hn: i64=0; while hay[hn]!=(0 as u8) { hn=hn+1 } 60 var nn: i64=0; while needle[nn]!=(0 as u8) { nn=nn+1 } 61 if nn==0 { return 1 } 62 var i: i64=0 63 while i + nn <= hn { var j: i64=0; var mt: i64=1; while j < nn { if hay[i+j]!=needle[j] { mt=0; j=nn } else { j=j+1 } } if mt==1 { return 1 } i=i+1 } 64 return 0 65} 66// fork-exec _offc/nx_crew_serve.elf <a1> <a2>, CAPTURE stdout into out (NUL-term). returns bytes. (mirrors nx_ccheck) 67func cw_run(a1: *u8, a2: *u8, out: *u8, outcap: i64) -> i64 { 68 let path: *u8 = "_offc/nx_crew_serve.elf" as *u8 69 let av: *i64 = sys_mmap(4*8) as *i64; av[0]=path as i64; av[1]=a1 as i64; av[2]=a2 as i64; av[3]=0 70 let pb: *i64 = sys_mmap(16); sys_pipe2(pb, 0) 71 let rfd: i64 = pb[0] & 0xFFFFFFFF 72 let wfd: i64 = (pb[0] >> 32) & 0xFFFFFFFF 73 let pid: i64 = sys_fork() 74 if pid==0 { sys_dup3(wfd,1,0); sys_close(rfd); sys_close(wfd); sys_execve(path, av, 0 as *i64); sys_exit(127); return 0 } 75 sys_close(wfd) 76 var tot: i64=0; var go: i64=1 77 while go==1 { let room: i64=outcap-1-tot; if room<=0 { go=0 } else { let k: i64=sys_read(rfd, (out as i64 + tot) as *u8, room); if k<=0 { go=0 } else { tot=tot+k } } } 78 sys_close(rfd) 79 let st: *i64 = sys_mmap(8); sys_wait4(pid, st, 0) 80 let z: *u8=(out as i64 + tot) as *u8; z[0]=0 as u8 81 return tot 82} 83 84func main() -> i64 { 85 cw_w("=== nx_crew_capability_gate -- does every RACI role own its capability as an MCP API? ===\n" as *u8) 86 let tools: *u8 = sys_mmap(CW_MAXT*CW_STR) 87 let nt: i64 = cw_load_set("knowledge/registry/mcp_tools_live.txt" as *u8, tools, CW_MAXT) 88 if nt < 0 { cw_w("FAIL: mcp_tools_live.txt unreadable\n" as *u8); sys_exit(2); return 2 } 89 cw_w(" live MCP tools in snapshot=" as *u8); cw_wn(nt); cw_w("\n" as *u8) 90 let buf: *u8 = sys_mmap(CW_CAP); let n: i64 = cw_read("knowledge/registry/crew_capabilities.data" as *u8, buf, CW_CAP) 91 if n <= 0 { cw_w("FAIL: crew_capabilities.data unreadable\n" as *u8); sys_exit(2); return 2 } 92 let role: *u8 = sys_mmap(CW_STR); let tool: *u8 = sys_mmap(CW_STR); let stat: *u8 = sys_mmap(CW_STR); let act: *u8 = sys_mmap(CW_STR); let dout: *u8 = sys_mmap(16384) 93 var mcp: i64=0; var dispatch: i64=0; var organ: i64=0; var concept: i64=0; var human: i64=0; var liars: i64=0; var rows: i64=0 94 cw_w(" organ-only roles (own the capability, NO MCP API yet -- the drive-to-100 queue):\n " as *u8) 95 var ls: i64 = 0; var i: i64 = 0 96 while i <= n { 97 var nl: i64 = 0; if i>=n {nl=1} else { if buf[i]==(10 as u8) {nl=1} } 98 if nl == 1 { 99 let ll: i64 = i - ls 100 if ll > 0 { 101 let line: *u8 = ((buf as i64)+ls) as *u8 102 if line[0] != (35 as u8) { 103 cw_fieldn(line, ll, 0, role, CW_STR) 104 cw_fieldn(line, ll, 1, act, CW_STR) 105 cw_fieldn(line, ll, 3, tool, CW_STR) 106 cw_fieldn(line, ll, 4, stat, CW_STR) 107 if role[0] != (0 as u8) { 108 rows = rows + 1 109 if cw_streq(stat, "mcp" as *u8)==1 { 110 mcp = mcp + 1 111 // LIAR-KILL: the claimed tool must be in the live snapshot. 112 if cw_has(tools, nt, tool)==0 { liars = liars + 1; cw_w("\n LIAR: role " as *u8); cw_w(role); cw_w(" claims mcp via " as *u8); cw_w(tool); cw_w(" -- NOT in live registry\n " as *u8) } 113 } else { if cw_streq(stat, "dispatch" as *u8)==1 { 114 dispatch = dispatch + 1 115 // LIAR-KILL: the routing tool must be live AND actually RUN nishi_crew <role> <activity> -> require a CLEAN dispatch. 116 if cw_has(tools, nt, tool)==0 { liars = liars + 1; cw_w("\n LIAR: dispatch role " as *u8); cw_w(role); cw_w(" via " as *u8); cw_w(tool); cw_w(" -- routing tool NOT live\n " as *u8) } 117 cw_run(role, act, dout, 16384) 118 let ndl: *u8 = sys_mmap(32); var no: i64=cw_scopy(ndl,0,"exit_code" as *u8); ndl[no]=34 as u8; ndl[no+1]=58 as u8; ndl[no+2]=48 as u8; ndl[no+3]=0 as u8 119 if cw_contains(dout, ndl)==0 { liars = liars + 1; cw_w("\n LIAR: dispatch role " as *u8); cw_w(role); cw_w(" -- nishi_crew " as *u8); cw_w(role); cw_w(" " as *u8); cw_w(act); cw_w(" did NOT return a clean dispatch (exit_code 0)\n " as *u8) } 120 } else { if cw_streq(stat, "organ" as *u8)==1 { organ = organ + 1; cw_w(role); cw_w(" " as *u8) } else { if cw_streq(stat, "concept" as *u8)==1 { concept = concept + 1 } else { human = human + 1 } } } } 121 } 122 } 123 } 124 ls = i + 1 125 } 126 i = i + 1 127 } 128 cw_w("\n" as *u8) 129 let denom: i64 = rows - human 130 let served: i64 = mcp + dispatch 131 var permil: i64 = 0; if denom > 0 { permil = (served * 1000) / denom } 132 cw_w(" rows=" as *u8); cw_wn(rows); cw_w(" human=" as *u8); cw_wn(human); cw_w(" mcp=" as *u8); cw_wn(mcp); cw_w(" dispatch=" as *u8); cw_wn(dispatch); cw_w(" organ-only=" as *u8); cw_wn(organ); cw_w(" concept=" as *u8); cw_wn(concept); cw_w("\n" as *u8) 133 cw_w(" MCP-exposure coverage=" as *u8); cw_wn(permil); cw_w(" permil (" as *u8); cw_wn(served); cw_w("/" as *u8); cw_wn(denom); cw_w(" non-human roles callable over MCP = " as *u8); cw_wn(mcp); cw_w(" own-tool + " as *u8); cw_wn(dispatch); cw_w(" nishi_crew-dispatch [each dispatch actually re-run + verified])\n" as *u8) 134 // NEG-CONTROL: a ghost tool must be detected as NOT live (the liar-kill has teeth). 135 var negok: i64 = 0; if cw_has(tools, nt, "zzz_ghost_tool" as *u8)==0 { negok = 1 } 136 cw_w(" liar-claims=" as *u8); cw_wn(liars); cw_w(" neg-control(ghost tool absent)=" as *u8); cw_wn(negok); cw_w("\n" as *u8) 137 if liars == 0 { if negok == 1 { 138 cw_w("=== CAPABILITY LEDGER HONEST: every mcp claim resolves to a LIVE tool; detector has teeth. GREEN ===\n" as *u8) 139 sys_exit(0); return 0 140 } } 141 cw_w("=== CREW CAPABILITY GATE RED (a role claims an MCP API it does not have, or toothless detector) ===\n" as *u8) 142 sys_exit(1); return 1 143}