code wiki / _hdl_build / nx_hr_sheriff.nx
nx_hr_sheriff.nx source
↩ module page · 110 lines · 6475 B
1// nx_hr_sheriff.nx -- NISHI HR, the SHERIFF: behavioral compliance monitor for the AUTONOMOUS Nishi team. Partner
2// to the PM (the PM owns the RACI/plan; the sheriff audits that the team's ACTIONS follow the PROCEDURES). Watches
3// a team-action log and flags violations of the standing procedures:
4// R1 GATE-BEFORE-DEPLOY : a release-class action (deploy/publish) MUST have a GREEN gate first.
5// R2 PUBLISH-VIA-PUBLISHER: a release-class action MUST go through the coordinated publisher (the lease) -- the
6// "workstreams stop crashing everything" rule; a raw push is a violation.
7// R3 IN-YOUR-LANE (RACI) : the actor's role must be Responsible or Accountable for the area it's acting on
8// (cross-checked against the PM's RACI). Acting outside your lane is a violation.
9// Behavioral, not punitive-by-default: it COUNTS per-actor violations so a repeat offender (a pattern) is flagged,
10// not just a one-off. Composes the RACI (nx_hr_team) + nx_hr helpers. license_tier: ORIGINAL
11import "nx_hr.nx" // hr_read / hr_line_end / hr_field_end / hr_eqrange
12import "nx_syscalls.nx"
13const SH_MAGIC_262144: i64 = 262144
14
15const SH_OK: i64 = 0
16const SH_NO_GATE: i64 = 1 // deploy/publish without a green gate
17const SH_NO_PUBLISHER: i64 = 2 // release not routed through the publisher (raw push)
18const SH_RACI_UNAUTH: i64 = 3 // actor's role is not R/A for the area (out of lane)
19
20const SH_R: i64 = 82 // 'R'
21const SH_A: i64 = 65 // 'A'
22
23func sh_eq(s: *u8, slen: i64, lit: *u8, litlen: i64) -> i64 { if slen != litlen { return 0 } var k: i64=0; while k<slen { if s[k]!=lit[k] { return 0 } k=k+1 } return 1 }
24func sh_atoi(buf: *u8, s: i64, e: i64) -> i64 { var v: i64=0; var i: i64=s; while i<e { let c: i64=buf[i] as i64; if c>=48 { if c<=57 { v=v*10+(c-48); i=i+1 } else {i=e} } else {i=e} } return v }
25// release-class action? (deploy or publish)
26func sh_is_release(action: *u8, alen: i64) -> i64 { if sh_eq(action, alen, "deploy" as *u8, 6)==1 { return 1 } if sh_eq(action, alen, "publish" as *u8, 7)==1 { return 1 } return 0 }
27
28// is the actor's role R or A for `area` in the RACI? (in-your-lane authorization)
29func sh_raci_authorized(raci_path: *u8, area: *u8, arealen: i64, actor: *u8, actorlen: i64) -> i64 {
30 let buf: *u8 = sys_mmap(SH_MAGIC_262144); let n: i64 = hr_read(raci_path, buf, SH_MAGIC_262144)
31 var i: i64 = 0
32 while i < n {
33 let le: i64 = hr_line_end(buf, i, n)
34 let f0e: i64 = hr_field_end(buf, i, le)
35 if hr_eqrange(buf, i, f0e, area, arealen) == 1 {
36 let f1s: i64 = f0e+1; let f1e: i64 = hr_field_end(buf, f1s, le)
37 if hr_eqrange(buf, f1s, f1e, actor, actorlen) == 1 {
38 let f2s: i64 = f1e+1
39 if f2s < le { let lc: i64 = buf[f2s] as i64; if lc == SH_R { return 1 } if lc == SH_A { return 1 } }
40 }
41 }
42 i = le + 1
43 }
44 return 0
45}
46
47// THE CHECK for one action. returns SH_OK or the first violation code.
48func sheriff_check_event(actor: *u8, actorlen: i64, action: *u8, actionlen: i64, area: *u8, arealen: i64, gate_green: i64, via_publisher: i64, raci_path: *u8) -> i64 {
49 if sh_is_release(action, actionlen) == 1 {
50 if via_publisher != 1 { return SH_NO_PUBLISHER }
51 if gate_green != 1 { return SH_NO_GATE }
52 }
53 if sh_raci_authorized(raci_path, area, arealen, actor, actorlen) == 0 { return SH_RACI_UNAUTH }
54 return SH_OK
55}
56
57// SWEEP the action log (`actor<TAB>action<TAB>area<TAB>gate<TAB>pub`); write a per-event report; return violation count.
58func sheriff_sweep(log_path: *u8, raci_path: *u8, report_path: *u8) -> i64 {
59 let buf: *u8 = sys_mmap(SH_MAGIC_262144); let n: i64 = hr_read(log_path, buf, SH_MAGIC_262144)
60 let rfd: i64 = sys_openat_wr(report_path, 0x1a4)
61 var viol: i64 = 0; var i: i64 = 0
62 while i < n {
63 let le: i64 = hr_line_end(buf, i, n)
64 let f0e: i64 = hr_field_end(buf, i, le)
65 if f0e > i {
66 let f1s: i64 = f0e+1; let f1e: i64 = hr_field_end(buf, f1s, le)
67 let f2s: i64 = f1e+1; let f2e: i64 = hr_field_end(buf, f2s, le)
68 let f3s: i64 = f2e+1; let f3e: i64 = hr_field_end(buf, f3s, le)
69 let f4s: i64 = f3e+1; let f4e: i64 = hr_field_end(buf, f4s, le)
70 let actor: *u8 = ((buf as i64)+i) as *u8
71 let action: *u8 = ((buf as i64)+f1s) as *u8
72 let area: *u8 = ((buf as i64)+f2s) as *u8
73 let gate: i64 = sh_atoi(buf, f3s, f3e)
74 let pub: i64 = sh_atoi(buf, f4s, f4e)
75 let v: i64 = sheriff_check_event(actor, f0e-i, action, f1e-f1s, area, f2e-f2s, gate, pub, raci_path)
76 if rfd >= 0 {
77 sys_write(rfd, actor, f0e-i); sys_write(rfd, "\t" as *u8, 1)
78 if v == SH_OK { sys_write(rfd, "OK\n" as *u8, 3) } else { if v == SH_NO_GATE { sys_write(rfd, "VIOLATION:NO_GATE\n" as *u8, 18) } else { if v == SH_NO_PUBLISHER { sys_write(rfd, "VIOLATION:NO_PUBLISHER\n" as *u8, 23) } else { sys_write(rfd, "VIOLATION:RACI_UNAUTH\n" as *u8, 22) } } }
79 }
80 if v != SH_OK { viol = viol + 1 }
81 }
82 i = le + 1
83 }
84 if rfd >= 0 { sys_close(rfd) }
85 return viol
86}
87
88// behavioral: how many violations has a specific actor committed (repeat-offender pattern)?
89func sheriff_actor_violations(log_path: *u8, raci_path: *u8, actor_q: *u8, actor_qlen: i64) -> i64 {
90 let buf: *u8 = sys_mmap(SH_MAGIC_262144); let n: i64 = hr_read(log_path, buf, SH_MAGIC_262144)
91 var viol: i64 = 0; var i: i64 = 0
92 while i < n {
93 let le: i64 = hr_line_end(buf, i, n)
94 let f0e: i64 = hr_field_end(buf, i, le)
95 if f0e > i {
96 if hr_eqrange(buf, i, f0e, actor_q, actor_qlen) == 1 {
97 let f1s: i64 = f0e+1; let f1e: i64 = hr_field_end(buf, f1s, le)
98 let f2s: i64 = f1e+1; let f2e: i64 = hr_field_end(buf, f2s, le)
99 let f3s: i64 = f2e+1; let f3e: i64 = hr_field_end(buf, f3s, le)
100 let f4s: i64 = f3e+1; let f4e: i64 = hr_field_end(buf, f4s, le)
101 let action: *u8 = ((buf as i64)+f1s) as *u8
102 let area: *u8 = ((buf as i64)+f2s) as *u8
103 let v: i64 = sheriff_check_event(actor_q, actor_qlen, action, f1e-f1s, area, f2e-f2s, sh_atoi(buf,f3s,f3e), sh_atoi(buf,f4s,f4e), raci_path)
104 if v != SH_OK { viol = viol + 1 }
105 }
106 }
107 i = le + 1
108 }
109 return viol
110}