nx_ws_hygiene_core.nx source
↩ module page · 141 lines · 8058 B
1// nx_ws_hygiene_core.nx -- importable CORE of the WORKSTREAM HYGIENE classifier (R-ORCH janitorial
2// enforcement, 07-15 operator: "stop having silly things like shell and tsv and non-api bullshit
3// when workstreams are started"). Turns rule 27 (api-first-no-shell) + no-TSV + no-plumbing from
4// DOCTRINE into a MECHANICAL verdict: classify a workstream operation line as SOVEREIGN or a
5// specific VIOLATION. The ONE sanctioned shell use (launch a sovereign build/ship ELF -- rule 27)
6// is exempted BY NAME and short-circuits GREEN; everything else that reaches for shell/TSV/tmp-
7// redirect/raw-plumbing to do workstream work is flagged so it can be reworked onto the API/MCP
8// rails. Pure classifier here (gate-locked); a scanner/linter CLI wraps it. Read-only.
9// license_tier: ORIGINAL
10import "nx_syscalls.nx"
11import "nx_crashresume_census_core.nx" // ccz_slen / ccz_cat_str / ccz_cat_num / ccz_read
12
13// verdict ids -- >=10 GREEN (sanctioned), 1..6 = VIOLATIONS, 0 = neutral (not a classified op)
14const HV_NONE: i64 = 0
15const HV_SHELL_CHAIN: i64 = 1 // `sh -c` / `bash -c` glue
16const HV_TMP_REDIRECT: i64 = 2 // `> /tmp/...` / `2>&1` (uncaptured, non-sovereign scratch)
17const HV_TSV: i64 = 3 // .tsv output (should be seg_store)
18const HV_WSL_PATH: i64 = 4 // /mnt/c path (MSYS-mangle-prone) used OUTSIDE the build lane
19const HV_PLUMBING: i64 = 5 // scp/curl/tar/ssh/rsync (should be mgmt API)
20const HV_SHELL_EXEC: i64 = 6 // running a non-build organ .elf via shell (should be tools/call)
21const HV_SHELL_UTIL: i64 = 7 // raw shell PLUMBING as a workstream op: rm -f/-rf (the ACTUAL sin; lock-reaping -> nx_lock_reap)
22const HV_TOKEN_BURN: i64 = 8 // Claude token-heavy one-off tool (WebFetch/WebSearch/Workflow/Agent/Task)
23const HV_ACCEPTED_VCS: i64 = 9 // git add/commit/push = operator-ACCEPTED "git or beyond" VCS -- NOT a violation (sovereign-git = end-state; the sin was the ps|grep|rm plumbing AROUND git, not git)
24const HV_SOV_BUILD: i64 = 10 // sanctioned build/ship lane (rule-27 exception)
25const HV_SOV_API: i64 = 11 // mcp__nishi__ / /api/ / nx_mgmt / tools/call
26
27// case-sensitive substring containment.
28func hv_has(hay: *u8, hn: i64, needle: *u8) -> i64 {
29 let nn: i64 = ccz_slen(needle)
30 if nn == 0 { return 0 }
31 var i: i64 = 0
32 while i + nn <= hn {
33 var k: i64 = 0
34 var ok: i64 = 1
35 while k < nn { if hay[i+k] != needle[k] { ok = 0; k = nn } k = k + 1 }
36 if ok == 1 { return 1 }
37 i = i + 1
38 }
39 return 0
40}
41
42// is this line the sanctioned build/ship launcher? (rule-27: shell may ONLY launch a sovereign ELF
43// for builds -- nx_sov_build_run / nx_ship / nx_content_ship). These legitimately use wsl + /mnt/c.
44func hv_is_sanctioned_launcher(line: *u8, n: i64) -> i64 {
45 if hv_has(line, n, "nx_sov_build_run" as *u8) == 1 { return 1 }
46 if hv_has(line, n, "nx_ship" as *u8) == 1 { return 1 }
47 if hv_has(line, n, "nx_content_ship" as *u8) == 1 { return 1 }
48 return 0
49}
50
51func hv_is_sovereign_api(line: *u8, n: i64) -> i64 {
52 if hv_has(line, n, "mcp__nishi__" as *u8) == 1 { return 1 }
53 if hv_has(line, n, "/api/" as *u8) == 1 { return 1 }
54 if hv_has(line, n, "nx_mgmt" as *u8) == 1 { return 1 }
55 if hv_has(line, n, "tools/call" as *u8) == 1 { return 1 }
56 return 0
57}
58
59// THE classifier. Sanctioned launcher wins first (GREEN); then the most-severe violation; then a
60// sovereign-API marker (GREEN); else neutral. Order of violation checks = severity/specificity.
61func hv_classify(line: *u8, n: i64) -> i64 {
62 if hv_is_sanctioned_launcher(line, n) == 1 { return HV_SOV_BUILD }
63
64 // running an organ .elf via shell that is NOT the sanctioned launcher = the headline anti-pattern
65 if hv_has(line, n, ".elf" as *u8) == 1 {
66 if hv_has(line, n, "wsl " as *u8) == 1 { return HV_SHELL_EXEC }
67 if hv_has(line, n, "sh -c" as *u8) == 1 { return HV_SHELL_EXEC }
68 if hv_has(line, n, "bash -c" as *u8) == 1 { return HV_SHELL_EXEC }
69 if hv_has(line, n, "./_offc/" as *u8) == 1 { return HV_SHELL_EXEC }
70 }
71 if hv_has(line, n, "sh -c" as *u8) == 1 { return HV_SHELL_CHAIN }
72 if hv_has(line, n, "bash -c" as *u8) == 1 { return HV_SHELL_CHAIN }
73 if hv_has(line, n, "2>&1" as *u8) == 1 { return HV_TMP_REDIRECT }
74 if hv_has(line, n, "> /tmp/" as *u8) == 1 { return HV_TMP_REDIRECT }
75 if hv_has(line, n, ">/tmp/" as *u8) == 1 { return HV_TMP_REDIRECT }
76 if hv_has(line, n, ".tsv" as *u8) == 1 { return HV_TSV }
77 if hv_has(line, n, "scp " as *u8) == 1 { return HV_PLUMBING }
78 if hv_has(line, n, "rsync " as *u8) == 1 { return HV_PLUMBING }
79 if hv_has(line, n, "curl " as *u8) == 1 { return HV_PLUMBING }
80 if hv_has(line, n, "tar " as *u8) == 1 { return HV_PLUMBING }
81 if hv_has(line, n, "ssh " as *u8) == 1 { return HV_PLUMBING }
82 // rm plumbing as a workstream op = the ACTUAL sin (ad-hoc lock-reaping etc. -> nx_lock_reap organ).
83 // CHECKED BEFORE git so a mixed pipeline `rm -f lock && git commit` still trips here (rm wins).
84 if hv_has(line, n, "rm -f" as *u8) == 1 { return HV_SHELL_UTIL }
85 if hv_has(line, n, "rm -rf" as *u8) == 1 { return HV_SHELL_UTIL }
86 // git itself = the operator's ACCEPTED "git or beyond" VCS -- NOT the sin (the sin was the ps|grep|rm
87 // plumbing AROUND it, now an organ). Classified distinctly: accepted interim, sovereign-git = end-state.
88 if hv_has(line, n, "git commit" as *u8) == 1 { return HV_ACCEPTED_VCS }
89 if hv_has(line, n, "git add" as *u8) == 1 { return HV_ACCEPTED_VCS }
90 if hv_has(line, n, "git push" as *u8) == 1 { return HV_ACCEPTED_VCS }
91 // /mnt/c reached here = NOT a sanctioned launcher and no other violation named it = bare WSL path use
92 if hv_has(line, n, "/mnt/c" as *u8) == 1 { return HV_WSL_PATH }
93
94 if hv_is_sovereign_api(line, n) == 1 { return HV_SOV_API }
95 return HV_NONE
96}
97
98// classify a CLAUDE TOOL NAME (from a transcript tool_use) on the token-budget axis: the expensive
99// one-off tools that should be redirected to grown Nishi organs. mcp__nishi__* = GREEN. Exact/prefix
100// match (not substring of prose -- the CLI feeds it a tool NAME, not a sentence).
101func hv_tool_verdict(name: *u8, n: i64) -> i64 {
102 // sovereign MCP tools are the GOOD path
103 let m: *u8 = "mcp__nishi__" as *u8
104 var k: i64 = 0
105 var pref: i64 = 1
106 while m[k] != (0 as u8) { if k >= n { pref = 0; k = 12 } else { if name[k] != m[k] { pref = 0; k = 12 } else { k = k + 1 } } }
107 if pref == 1 { return HV_SOV_API }
108 if streq_hv(name, "WebFetch" as *u8) == 1 { return HV_TOKEN_BURN }
109 if streq_hv(name, "WebSearch" as *u8) == 1 { return HV_TOKEN_BURN }
110 if streq_hv(name, "Workflow" as *u8) == 1 { return HV_TOKEN_BURN }
111 if streq_hv(name, "Agent" as *u8) == 1 { return HV_TOKEN_BURN }
112 if streq_hv(name, "Task" as *u8) == 1 { return HV_TOKEN_BURN }
113 return HV_NONE
114}
115
116func streq_hv(a: *u8, b: *u8) -> i64 {
117 var i: i64 = 0
118 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 }
119 if b[i] != (0 as u8) { return 0 }
120 return 1
121}
122
123func hv_is_violation(v: i64) -> i64 {
124 if v >= 1 { if v <= 8 { return 1 } }
125 return 0
126}
127
128func hv_name(v: i64) -> *u8 {
129 if v == HV_SHELL_CHAIN { return "SHELL-CHAIN(sh -c)" as *u8 }
130 if v == HV_TMP_REDIRECT { return "TMP-REDIRECT(/tmp,2>&1)" as *u8 }
131 if v == HV_TSV { return "TSV-FORMAT(use seg_store)" as *u8 }
132 if v == HV_WSL_PATH { return "WSL-PATH(/mnt/c mangle)" as *u8 }
133 if v == HV_PLUMBING { return "RAW-PLUMBING(use mgmt API)" as *u8 }
134 if v == HV_SHELL_EXEC { return "SHELL-EXEC-ORGAN(use tools/call)" as *u8 }
135 if v == HV_SHELL_UTIL { return "SHELL-UTIL(rm/ps plumbing -> nx_lock_reap/organ)" as *u8 }
136 if v == HV_TOKEN_BURN { return "TOKEN-BURN(WebFetch/Workflow/Agent -> Nishi organ)" as *u8 }
137 if v == HV_ACCEPTED_VCS { return "ACCEPTED-VCS(git; sovereign-git=end-state)" as *u8 }
138 if v == HV_SOV_BUILD { return "SOVEREIGN-BUILD-LANE" as *u8 }
139 if v == HV_SOV_API { return "SOVEREIGN-API/MCP" as *u8 }
140 return "NEUTRAL" as *u8
141}