nx_crew_roster_gate.nx source
↩ module page · 120 lines · 8916 B
1// nx_crew_roster_gate.nx -- THE CREW-COMPLETENESS GUARD (operator 2026-07-10: "are you sure you have them all").
2// The canonical roster (knowledge/registry/crew_roster.tsv) and the machine RACI (knowledge/registry/nishi_raci.tsv)
3// MUST name the EXACT SAME SET of roles -- so a crew member can never again be RACI'd-but-off-roster or
4// on-roster-but-un-RACI'd (the drift that lost Analyst/Examiner/Auditor/Critic/Scribe/Arbiter until the operator
5// caught it). Reads both, extracts role = field 0 of every non-comment line, computes the symmetric difference,
6// and FAILS if either direction is non-empty. NEG-CONTROL with teeth: injects a synthetic role into each set and
7// confirms the detector's missing-count rises by exactly 1. NOTE: no '#'/'!' in string literals (nx_cc lexer trap).
8// license_tier: ORIGINAL expect_exit: 0
9import "nx_syscalls.nx"
10
11const CR_CAP: i64 = 262144
12const CR_MAXR: i64 = 256
13const CR_STR: i64 = 48
14
15func cr_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
16func cr_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 }
17func cr_read(path: *u8, buf: *u8, cap: i64) -> i64 {
18 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 }
19 var off: i64 = 0; var go: i64 = 1
20 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 } } }
21 sys_close(fd); return off
22}
23func cr_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 }
24// field 0 of line[0..len): copy up to first TAB (9) or end -> out (NUL-term).
25func cr_field0(line: *u8, len: i64, out: *u8, outcap: i64) -> i64 {
26 var i: i64=0; var w: i64=0
27 while i < len { let c: i64 = line[i] as i64; if c==9 { i=len } else { if w<outcap-1 { out[w]=line[i]; w=w+1 } i=i+1 } }
28 out[w]=0 as u8; return 0
29}
30func cr_has(set: *u8, n: i64, name: *u8) -> i64 { var i: i64=0; while i<n { let off: i64=i*CR_STR; if cr_streq((set as i64 + off) as *u8, name)==1 {return 1} i=i+1 } return 0 }
31// parse role = field0 of every non-comment / non-blank line into set (dedup). returns count.
32func cr_parse(path: *u8, set: *u8, maxr: i64) -> i64 {
33 let buf: *u8 = sys_mmap(CR_CAP); let n: i64 = cr_read(path, buf, CR_CAP)
34 if n <= 0 { return 0 - 1 }
35 let f: *u8 = sys_mmap(CR_STR)
36 var cnt: i64 = 0; var ls: i64 = 0; var i: i64 = 0
37 while i <= n {
38 var nl: i64 = 0; if i>=n {nl=1} else { if buf[i]==(10 as u8) {nl=1} }
39 if nl == 1 {
40 let ll: i64 = i - ls
41 if ll > 0 {
42 let line: *u8 = ((buf as i64)+ls) as *u8
43 if line[0] != (35 as u8) {
44 cr_field0(line, ll, f, CR_STR)
45 if f[0] != (0 as u8) {
46 if cr_has(set, cnt, f)==0 { if cnt<maxr { let off: i64=cnt*CR_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 } }
47 }
48 }
49 }
50 ls = i + 1
51 }
52 i = i + 1
53 }
54 return cnt
55}
56// count (and print) members of A[0..na) absent from B[0..nb).
57func cr_count_missing(A: *u8, na: i64, B: *u8, nb: i64, label: *u8, verbose: i64) -> i64 {
58 var cnt: i64 = 0; var i: i64 = 0
59 while i < na { let off: i64=i*CR_STR; let nm: *u8=(A as i64 + off) as *u8
60 if cr_has(B, nb, nm)==0 { cnt=cnt+1; if verbose==1 { cr_w(" MISSING " as *u8); cr_w(label); cr_w(": " as *u8); cr_w(nm); cr_w("\n" as *u8) } }
61 i=i+1 }
62 return cnt
63}
64func cr_setadd_ghost(set: *u8, n: i64, name: *u8) -> i64 { let off: i64=n*CR_STR; var k: i64=0; while name[k]!=(0 as u8){ let d: *u8=(set as i64 + off + k) as *u8; d[0]=name[k]; k=k+1 } let z: *u8=(set as i64 + off + k) as *u8; z[0]=0 as u8; return n+1 }
65func cr_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 }
66// does runtime/nx_<noun>.nx OR runtime/_hdl_build/nx_<noun>.nx exist? (openat probe, no getdents)
67func cr_organ_exists(noun: *u8, pathbuf: *u8) -> i64 {
68 var o: i64 = cr_scopy(pathbuf, 0, "runtime/nx_" as *u8); o = cr_scopy(pathbuf, o, noun); o = cr_scopy(pathbuf, o, ".nx" as *u8); let z1: *u8=(pathbuf as i64 + o) as *u8; z1[0]=0 as u8
69 let fd1: i64 = sys_openat_rd(pathbuf); if fd1 >= 0 { sys_close(fd1); return 1 }
70 var p: i64 = cr_scopy(pathbuf, 0, "runtime/_hdl_build/nx_" as *u8); p = cr_scopy(pathbuf, p, noun); p = cr_scopy(pathbuf, p, ".nx" as *u8); let z2: *u8=(pathbuf as i64 + p) as *u8; z2[0]=0 as u8
71 let fd2: i64 = sys_openat_rd(pathbuf); if fd2 >= 0 { sys_close(fd2); return 1 }
72 return 0
73}
74// DISK->ROSTER advisory: for each role-noun candidate with an nx_<noun>.nx organ on disk NOT in the roster, flag it
75// (this is what would have caught HR/Analyst/etc.). Advisory, not a hard fail (a noun may name a non-role organ).
76func cr_disk_scan(rset: *u8, rn: i64) -> i64 {
77 let cand: *u8 = sys_mmap(4096)
78 // Role-noun candidates. Excluded name-collisions (organ exists but is NOT a crew role): dispatcher=nx_dispatcher.nx is a
79 // hardware-aware op-routing COMPUTE brick (conductor compute arc, matmul-on-GPU), the Conductor owns the `dispatch` activity.
80 cr_scopy(cand, 0, "hr|analyst|examiner|auditor|critic|scribe|arbiter|conductor|host_operator|recruiter|accountant|treasurer|marketer|salesperson|support|concierge|moderator|registrar|steward|recycler|governor|sentinel|merchant|assistant|product|architect|builder|security|finance|legal|modelwright|archivist|librarian|genealogist|racing|referee|warden|teacher|coach|publisher|supervisor|engineer|doctor|researcher|pm|ux|analyst2\0" as *u8)
81 let tok: *u8 = sys_mmap(CR_STR)
82 let pathbuf: *u8 = sys_mmap(512)
83 var flags: i64 = 0; var i: i64 = 0; var w: i64 = 0
84 while cand[i] != (0 as u8) {
85 if cand[i] == (124 as u8) { tok[w]=0 as u8; if w>0 { if cr_organ_exists(tok, pathbuf)==1 { if cr_has(rset, rn, tok)==0 { cr_w(" ADVISORY: organ nx_" as *u8); cr_w(tok); cr_w(".nx exists but role NOT in roster\n" as *u8); flags=flags+1 } } } w=0 } else { if w<CR_STR-1 { tok[w]=cand[i]; w=w+1 } }
86 i = i + 1
87 }
88 tok[w]=0 as u8; if w>0 { if cr_organ_exists(tok, pathbuf)==1 { if cr_has(rset, rn, tok)==0 { cr_w(" ADVISORY: organ nx_" as *u8); cr_w(tok); cr_w(".nx exists but role NOT in roster\n" as *u8); flags=flags+1 } } }
89 return flags
90}
91
92func main() -> i64 {
93 cr_w("=== nx_crew_roster_gate -- roster <-> RACI role-set bijection (the crew-completeness guard) ===\n" as *u8)
94 let rset: *u8 = sys_mmap(CR_MAXR*CR_STR)
95 let aset: *u8 = sys_mmap(CR_MAXR*CR_STR)
96 let rn: i64 = cr_parse("knowledge/registry/crew_roster.tsv" as *u8, rset, CR_MAXR)
97 let an: i64 = cr_parse("knowledge/registry/nishi_raci.tsv" as *u8, aset, CR_MAXR)
98 if rn < 0 { cr_w("FAIL: crew_roster.tsv unreadable\n" as *u8); sys_exit(2); return 2 }
99 if an < 0 { cr_w("FAIL: nishi_raci.tsv unreadable\n" as *u8); sys_exit(2); return 2 }
100 cr_w(" roster roles=" as *u8); cr_wn(rn); cr_w(" raci roles=" as *u8); cr_wn(an); cr_w("\n" as *u8)
101 let missA: i64 = cr_count_missing(rset, rn, aset, an, "from RACI (on roster, not RACI'd)" as *u8, 1)
102 let missB: i64 = cr_count_missing(aset, an, rset, rn, "from roster (RACI'd, off roster)" as *u8, 1)
103 // NEG-CONTROL (teeth): inject a synthetic role into each set; the missing-count must rise by exactly 1.
104 let rn2: i64 = cr_setadd_ghost(rset, rn, "zzz_ghost_r" as *u8)
105 let teethA: i64 = cr_count_missing(rset, rn2, aset, an, "teethA" as *u8, 0)
106 let an2: i64 = cr_setadd_ghost(aset, an, "zzz_ghost_a" as *u8)
107 let teethB: i64 = cr_count_missing(aset, an2, rset, rn, "teethB" as *u8, 0)
108 var negok: i64 = 0
109 if teethA == missA + 1 { if teethB == missB + 1 { negok = 1 } }
110 cr_w(" symmetric-diff: on-roster-not-RACI=" as *u8); cr_wn(missA); cr_w(" RACI-not-on-roster=" as *u8); cr_wn(missB); cr_w("\n" as *u8)
111 cr_w(" neg-control (synthetic role detected both directions)=" as *u8); cr_wn(negok); cr_w("\n" as *u8)
112 let dflags: i64 = cr_disk_scan(rset, rn)
113 cr_w(" disk->roster scan: role-noun organs on disk NOT in roster=" as *u8); cr_wn(dflags); cr_w(" (advisory)\n" as *u8)
114 if missA == 0 { if missB == 0 { if negok == 1 {
115 cr_w("=== CREW COMPLETE: roster and RACI name the SAME " as *u8); cr_wn(rn); cr_w(" roles; detector has teeth. GREEN ===\n" as *u8)
116 sys_exit(0); return 0
117 } } }
118 cr_w("=== CREW ROSTER GATE RED (drift or toothless detector) ===\n" as *u8)
119 sys_exit(1); return 1
120}