@guilz-dev/belay 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +268 -0
  3. package/agent-belay-logo.png +0 -0
  4. package/dist/adapters/claude/adapter.d.ts +7 -0
  5. package/dist/adapters/claude/adapter.js +114 -0
  6. package/dist/adapters/claude/hooks.d.ts +13 -0
  7. package/dist/adapters/claude/hooks.js +49 -0
  8. package/dist/adapters/claude/runtime-entry.d.ts +4 -0
  9. package/dist/adapters/claude/runtime-entry.js +260 -0
  10. package/dist/adapters/codex/adapter.d.ts +7 -0
  11. package/dist/adapters/codex/adapter.js +73 -0
  12. package/dist/adapters/codex/hooks.d.ts +21 -0
  13. package/dist/adapters/codex/hooks.js +78 -0
  14. package/dist/adapters/codex/runtime-entry.d.ts +4 -0
  15. package/dist/adapters/codex/runtime-entry.js +237 -0
  16. package/dist/adapters/cursor/adapter.d.ts +7 -0
  17. package/dist/adapters/cursor/adapter.js +29 -0
  18. package/dist/adapters/cursor/hooks.d.ts +2 -0
  19. package/dist/adapters/cursor/hooks.js +26 -0
  20. package/dist/adapters/cursor/runtime-entry.d.ts +4 -0
  21. package/dist/adapters/cursor/runtime-entry.js +143 -0
  22. package/dist/adapters/layouts/claude.d.ts +2 -0
  23. package/dist/adapters/layouts/claude.js +40 -0
  24. package/dist/adapters/layouts/codex.d.ts +2 -0
  25. package/dist/adapters/layouts/codex.js +43 -0
  26. package/dist/adapters/layouts/cursor.d.ts +2 -0
  27. package/dist/adapters/layouts/cursor.js +40 -0
  28. package/dist/adapters/layouts/index.d.ts +7 -0
  29. package/dist/adapters/layouts/index.js +23 -0
  30. package/dist/adapters/layouts/protected-paths.d.ts +3 -0
  31. package/dist/adapters/layouts/protected-paths.js +15 -0
  32. package/dist/adapters/layouts/scope.d.ts +19 -0
  33. package/dist/adapters/layouts/scope.js +86 -0
  34. package/dist/adapters/layouts/types.d.ts +14 -0
  35. package/dist/adapters/layouts/types.js +1 -0
  36. package/dist/adapters/registry.d.ts +4 -0
  37. package/dist/adapters/registry.js +14 -0
  38. package/dist/adapters/shared/gate-runtime.d.ts +51 -0
  39. package/dist/adapters/shared/gate-runtime.js +518 -0
  40. package/dist/adapters/shared/repo-root.d.ts +2 -0
  41. package/dist/adapters/shared/repo-root.js +17 -0
  42. package/dist/adapters/types.d.ts +19 -0
  43. package/dist/adapters/types.js +1 -0
  44. package/dist/branding.d.ts +3 -0
  45. package/dist/branding.js +3 -0
  46. package/dist/bundle/claude-runtime.mjs +5323 -0
  47. package/dist/bundle/codex-runtime.mjs +5310 -0
  48. package/dist/bundle/cursor-runtime.mjs +5208 -0
  49. package/dist/cleanup-orphans.d.ts +7 -0
  50. package/dist/cleanup-orphans.js +59 -0
  51. package/dist/cli.d.ts +2 -0
  52. package/dist/cli.js +631 -0
  53. package/dist/commands/approve.d.ts +14 -0
  54. package/dist/commands/approve.js +65 -0
  55. package/dist/commands/audit.d.ts +59 -0
  56. package/dist/commands/audit.js +132 -0
  57. package/dist/commands/classify-for-report.d.ts +9 -0
  58. package/dist/commands/classify-for-report.js +85 -0
  59. package/dist/commands/doctor.d.ts +3 -0
  60. package/dist/commands/doctor.js +366 -0
  61. package/dist/commands/dogfood.d.ts +5 -0
  62. package/dist/commands/dogfood.js +71 -0
  63. package/dist/commands/explain.d.ts +3 -0
  64. package/dist/commands/explain.js +133 -0
  65. package/dist/commands/health-snapshot.d.ts +2 -0
  66. package/dist/commands/health-snapshot.js +166 -0
  67. package/dist/commands/init-wizard.d.ts +16 -0
  68. package/dist/commands/init-wizard.js +50 -0
  69. package/dist/commands/metrics.d.ts +7 -0
  70. package/dist/commands/metrics.js +89 -0
  71. package/dist/commands/recover.d.ts +3 -0
  72. package/dist/commands/recover.js +105 -0
  73. package/dist/commands/report.d.ts +3 -0
  74. package/dist/commands/report.js +65 -0
  75. package/dist/commands/revoke.d.ts +5 -0
  76. package/dist/commands/revoke.js +22 -0
  77. package/dist/commands/simulate.d.ts +14 -0
  78. package/dist/commands/simulate.js +55 -0
  79. package/dist/commands/status.d.ts +5 -0
  80. package/dist/commands/status.js +107 -0
  81. package/dist/config-io.d.ts +23 -0
  82. package/dist/config-io.js +180 -0
  83. package/dist/conformance/guarantee-table.d.ts +14 -0
  84. package/dist/conformance/guarantee-table.js +95 -0
  85. package/dist/conformance/types.d.ts +6 -0
  86. package/dist/conformance/types.js +1 -0
  87. package/dist/core/approval-service.d.ts +26 -0
  88. package/dist/core/approval-service.js +41 -0
  89. package/dist/core/approval-token.d.ts +11 -0
  90. package/dist/core/approval-token.js +61 -0
  91. package/dist/core/approval.d.ts +19 -0
  92. package/dist/core/approval.js +58 -0
  93. package/dist/core/audit-analysis.d.ts +10 -0
  94. package/dist/core/audit-analysis.js +147 -0
  95. package/dist/core/audit-metrics.d.ts +51 -0
  96. package/dist/core/audit-metrics.js +155 -0
  97. package/dist/core/audit-query.d.ts +11 -0
  98. package/dist/core/audit-query.js +142 -0
  99. package/dist/core/audit-summary.d.ts +33 -0
  100. package/dist/core/audit-summary.js +111 -0
  101. package/dist/core/audit-types.d.ts +65 -0
  102. package/dist/core/audit-types.js +2 -0
  103. package/dist/core/capability/allowlist.d.ts +10 -0
  104. package/dist/core/capability/allowlist.js +53 -0
  105. package/dist/core/capability/broker.d.ts +17 -0
  106. package/dist/core/capability/broker.js +29 -0
  107. package/dist/core/capability/index.d.ts +5 -0
  108. package/dist/core/capability/index.js +4 -0
  109. package/dist/core/capability/paths.d.ts +1 -0
  110. package/dist/core/capability/paths.js +20 -0
  111. package/dist/core/capability/reasons.d.ts +2 -0
  112. package/dist/core/capability/reasons.js +4 -0
  113. package/dist/core/capability/types.d.ts +10 -0
  114. package/dist/core/capability/types.js +1 -0
  115. package/dist/core/capability-approval.d.ts +28 -0
  116. package/dist/core/capability-approval.js +43 -0
  117. package/dist/core/classify-subagent.d.ts +2 -0
  118. package/dist/core/classify-subagent.js +69 -0
  119. package/dist/core/classify-tool.d.ts +3 -0
  120. package/dist/core/classify-tool.js +311 -0
  121. package/dist/core/config-layers.d.ts +23 -0
  122. package/dist/core/config-layers.js +59 -0
  123. package/dist/core/config.d.ts +219 -0
  124. package/dist/core/config.js +720 -0
  125. package/dist/core/control-plane-isolation.d.ts +10 -0
  126. package/dist/core/control-plane-isolation.js +83 -0
  127. package/dist/core/custom-command-match.d.ts +2 -0
  128. package/dist/core/custom-command-match.js +8 -0
  129. package/dist/core/egress/allowlist.d.ts +7 -0
  130. package/dist/core/egress/allowlist.js +33 -0
  131. package/dist/core/egress/env.d.ts +3 -0
  132. package/dist/core/egress/env.js +17 -0
  133. package/dist/core/egress/fingerprint.d.ts +3 -0
  134. package/dist/core/egress/fingerprint.js +35 -0
  135. package/dist/core/egress/policy.d.ts +8 -0
  136. package/dist/core/egress/policy.js +47 -0
  137. package/dist/core/egress/proxy-server.d.ts +21 -0
  138. package/dist/core/egress/proxy-server.js +263 -0
  139. package/dist/core/egress/types.d.ts +25 -0
  140. package/dist/core/egress/types.js +1 -0
  141. package/dist/core/egress-approval.d.ts +48 -0
  142. package/dist/core/egress-approval.js +129 -0
  143. package/dist/core/fingerprint.d.ts +6 -0
  144. package/dist/core/fingerprint.js +24 -0
  145. package/dist/core/gate-contract.d.ts +48 -0
  146. package/dist/core/gate-contract.js +50 -0
  147. package/dist/core/gate-engine.d.ts +20 -0
  148. package/dist/core/gate-engine.js +172 -0
  149. package/dist/core/glob.d.ts +1 -0
  150. package/dist/core/glob.js +39 -0
  151. package/dist/core/index.d.ts +19 -0
  152. package/dist/core/index.js +15 -0
  153. package/dist/core/integrity.d.ts +15 -0
  154. package/dist/core/integrity.js +68 -0
  155. package/dist/core/judge-api-key.d.ts +4 -0
  156. package/dist/core/judge-api-key.js +11 -0
  157. package/dist/core/judge-config.d.ts +29 -0
  158. package/dist/core/judge-config.js +85 -0
  159. package/dist/core/judge-doctor.d.ts +7 -0
  160. package/dist/core/judge-doctor.js +124 -0
  161. package/dist/core/judgment.d.ts +6 -0
  162. package/dist/core/judgment.js +38 -0
  163. package/dist/core/notify.d.ts +13 -0
  164. package/dist/core/notify.js +44 -0
  165. package/dist/core/path-utils.d.ts +12 -0
  166. package/dist/core/path-utils.js +107 -0
  167. package/dist/core/reclassify.d.ts +15 -0
  168. package/dist/core/reclassify.js +82 -0
  169. package/dist/core/recover-advice.d.ts +30 -0
  170. package/dist/core/recover-advice.js +177 -0
  171. package/dist/core/recover-git-probe.d.ts +8 -0
  172. package/dist/core/recover-git-probe.js +50 -0
  173. package/dist/core/recover-select.d.ts +10 -0
  174. package/dist/core/recover-select.js +60 -0
  175. package/dist/core/scrub.d.ts +3 -0
  176. package/dist/core/scrub.js +87 -0
  177. package/dist/core/shell-substitution.d.ts +6 -0
  178. package/dist/core/shell-substitution.js +130 -0
  179. package/dist/core/shell-tokenizer.d.ts +5 -0
  180. package/dist/core/shell-tokenizer.js +129 -0
  181. package/dist/core/shell-unparseable.d.ts +4 -0
  182. package/dist/core/shell-unparseable.js +96 -0
  183. package/dist/core/transactional/diff-evaluator.d.ts +2 -0
  184. package/dist/core/transactional/diff-evaluator.js +84 -0
  185. package/dist/core/transactional/eligibility.d.ts +4 -0
  186. package/dist/core/transactional/eligibility.js +44 -0
  187. package/dist/core/transactional/git-worktree.d.ts +13 -0
  188. package/dist/core/transactional/git-worktree.js +189 -0
  189. package/dist/core/transactional/index.d.ts +5 -0
  190. package/dist/core/transactional/index.js +4 -0
  191. package/dist/core/transactional/reasons.d.ts +4 -0
  192. package/dist/core/transactional/reasons.js +8 -0
  193. package/dist/core/transactional/runner.d.ts +2 -0
  194. package/dist/core/transactional/runner.js +113 -0
  195. package/dist/core/transactional/types.d.ts +46 -0
  196. package/dist/core/transactional/types.js +1 -0
  197. package/dist/core/types.d.ts +90 -0
  198. package/dist/core/types.js +1 -0
  199. package/dist/core/v2/adapter.d.ts +14 -0
  200. package/dist/core/v2/adapter.js +118 -0
  201. package/dist/core/v2/containment.d.ts +19 -0
  202. package/dist/core/v2/containment.js +91 -0
  203. package/dist/core/v2/egress-classify.d.ts +7 -0
  204. package/dist/core/v2/egress-classify.js +216 -0
  205. package/dist/core/v2/fingerprint.d.ts +1 -0
  206. package/dist/core/v2/fingerprint.js +4 -0
  207. package/dist/core/v2/index.d.ts +12 -0
  208. package/dist/core/v2/index.js +10 -0
  209. package/dist/core/v2/judge-audit.d.ts +2 -0
  210. package/dist/core/v2/judge-audit.js +15 -0
  211. package/dist/core/v2/judge-factory.d.ts +25 -0
  212. package/dist/core/v2/judge-factory.js +75 -0
  213. package/dist/core/v2/judge-outbound.d.ts +12 -0
  214. package/dist/core/v2/judge-outbound.js +73 -0
  215. package/dist/core/v2/judge.d.ts +47 -0
  216. package/dist/core/v2/judge.js +264 -0
  217. package/dist/core/v2/launcher-resolve.d.ts +12 -0
  218. package/dist/core/v2/launcher-resolve.js +190 -0
  219. package/dist/core/v2/overrides.d.ts +7 -0
  220. package/dist/core/v2/overrides.js +37 -0
  221. package/dist/core/v2/parser.d.ts +21 -0
  222. package/dist/core/v2/parser.js +213 -0
  223. package/dist/core/v2/types.d.ts +67 -0
  224. package/dist/core/v2/types.js +1 -0
  225. package/dist/core/v2/verdict.d.ts +2 -0
  226. package/dist/core/v2/verdict.js +699 -0
  227. package/dist/corpus/evaluate.d.ts +24 -0
  228. package/dist/corpus/evaluate.js +69 -0
  229. package/dist/defaults.d.ts +18 -0
  230. package/dist/defaults.js +155 -0
  231. package/dist/egress-daemon.d.ts +1 -0
  232. package/dist/egress-daemon.js +52 -0
  233. package/dist/index.d.ts +17 -0
  234. package/dist/index.js +15 -0
  235. package/dist/installer/bootstrap.d.ts +5 -0
  236. package/dist/installer/bootstrap.js +61 -0
  237. package/dist/installer/runtime-artifacts.d.ts +3 -0
  238. package/dist/installer/runtime-artifacts.js +23 -0
  239. package/dist/installer/scope-config.d.ts +8 -0
  240. package/dist/installer/scope-config.js +25 -0
  241. package/dist/installer.d.ts +22 -0
  242. package/dist/installer.js +169 -0
  243. package/dist/node-resolution.d.ts +8 -0
  244. package/dist/node-resolution.js +237 -0
  245. package/dist/operational-insights.d.ts +19 -0
  246. package/dist/operational-insights.js +24 -0
  247. package/dist/presets.d.ts +4 -0
  248. package/dist/presets.js +95 -0
  249. package/dist/services/egress-service.d.ts +57 -0
  250. package/dist/services/egress-service.js +334 -0
  251. package/dist/services/sandbox-service.d.ts +38 -0
  252. package/dist/services/sandbox-service.js +95 -0
  253. package/dist/templates.d.ts +7 -0
  254. package/dist/templates.js +56 -0
  255. package/dist/types.d.ts +230 -0
  256. package/dist/types.js +1 -0
  257. package/dist/version.d.ts +1 -0
  258. package/dist/version.js +1 -0
  259. package/package.json +65 -0
  260. package/skills/belay/SKILL.md +52 -0
  261. package/skills/belay/belay-approve.md +7 -0
  262. package/skills/belay/belay-explain.md +11 -0
  263. package/skills/belay/belay-recover.md +13 -0
  264. package/skills/belay/belay-report.md +7 -0
  265. package/skills/belay/belay-status.md +9 -0
  266. package/skills/belay/belay-why.md +11 -0
@@ -0,0 +1,699 @@
1
+ import path from 'node:path';
2
+ import { relativeWithinRepo } from '../path-utils.js';
3
+ import { extractRedirectTargets, tokenizeShell } from '../shell-tokenizer.js';
4
+ import { analyzePathTargets, cwdRelative, resolveTrustedPath } from './containment.js';
5
+ import { classifyEgressTool } from './egress-classify.js';
6
+ import { verdictFingerprint } from './fingerprint.js';
7
+ import { prescanInterpreterCode, tier1RequiresAsk } from './judge.js';
8
+ import { isRoutineLauncher, resolveLauncherRecipe } from './launcher-resolve.js';
9
+ import { allowFromCustomOverride, askFromCustomExternal, customAllowMatch, customExternalMatch, } from './overrides.js';
10
+ import { extractRecursiveScript, isBareInterpreter, isVariableIndirectHead, parseSegment, peelTransparentWrappers, redactCommand, segmentOpacity, splitTopLevelSegments, substitutionInners, } from './parser.js';
11
+ const DEFAULT_MAX_DEPTH = 8;
12
+ const TIER0_EXTERNAL_KEYS = new Set([
13
+ 'git push',
14
+ 'docker push',
15
+ 'docker run',
16
+ 'npm publish',
17
+ 'pnpm publish',
18
+ 'terraform apply',
19
+ 'firebase',
20
+ 'fly',
21
+ 'supabase',
22
+ 'scp',
23
+ 'ssh',
24
+ 'rsync',
25
+ ]);
26
+ const TIER0_EXTERNAL_HEADS = new Set([
27
+ 'dropdb',
28
+ 'createdb',
29
+ 'psql',
30
+ 'mysql',
31
+ 'mongosh',
32
+ 'redis-cli',
33
+ ]);
34
+ const READ_ONLY_KEYS = new Set([
35
+ 'cat',
36
+ 'cd',
37
+ 'echo',
38
+ 'git diff',
39
+ 'git log',
40
+ 'git rev-parse',
41
+ 'git show',
42
+ 'git status',
43
+ 'head',
44
+ 'ls',
45
+ 'pwd',
46
+ 'rg',
47
+ 'sort',
48
+ 'tail',
49
+ 'wc',
50
+ 'which',
51
+ 'find',
52
+ ]);
53
+ const PURE_READ_ONLY_KEYS = new Set([
54
+ 'echo',
55
+ 'git diff',
56
+ 'git log',
57
+ 'git rev-parse',
58
+ 'git show',
59
+ 'git status',
60
+ 'pwd',
61
+ 'which',
62
+ ]);
63
+ const LOCAL_MUTATION_KEYS = new Set([
64
+ 'chmod',
65
+ 'cp',
66
+ 'git add',
67
+ 'git clean',
68
+ 'git commit',
69
+ 'git mv',
70
+ 'git reset',
71
+ 'mkdir',
72
+ 'mv',
73
+ 'rm',
74
+ 'sed',
75
+ 'tee',
76
+ 'touch',
77
+ 'truncate',
78
+ ]);
79
+ /** Routine local build/test runners resolved from launcher recipes. */
80
+ const LOCAL_ROUTINE_HEADS = new Set([
81
+ 'tsc',
82
+ 'vitest',
83
+ 'vite',
84
+ 'webpack',
85
+ 'esbuild',
86
+ 'rollup',
87
+ 'jest',
88
+ 'mocha',
89
+ 'cargo',
90
+ 'go',
91
+ 'make',
92
+ 'cmake',
93
+ ]);
94
+ const FIND_DANGEROUS_FLAGS = new Set(['-delete', '-exec', '-execdir', '-ok', '-okdir']);
95
+ function isFindDangerous(tokens) {
96
+ return tokens.some((token) => FIND_DANGEROUS_FLAGS.has(token) || token.startsWith('-exec') || token.startsWith('-ok'));
97
+ }
98
+ function worsePermission(left, right) {
99
+ return left === 'ask' || right === 'ask' ? 'ask' : 'allow';
100
+ }
101
+ async function evaluateSubstitutions(command, context, depth) {
102
+ const inners = substitutionInners(command);
103
+ if (inners.length === 0) {
104
+ return null;
105
+ }
106
+ if (context.unknownLocalEffect === 'deny') {
107
+ return askVerdict({
108
+ location: 'unknown',
109
+ opacity: 'recursive',
110
+ effect: 'unknown',
111
+ confidence: 'deterministic',
112
+ reason: 'command_substitution',
113
+ signals: ['command_substitution'],
114
+ });
115
+ }
116
+ let worst = null;
117
+ for (const inner of inners) {
118
+ const innerVerdict = await evaluateSegment(inner, context, depth + 1);
119
+ if (innerVerdict.permission === 'ask') {
120
+ return askVerdict({
121
+ ...innerVerdict,
122
+ opacity: 'recursive',
123
+ reason: 'command_substitution',
124
+ signals: [...innerVerdict.signals, 'command_substitution'],
125
+ });
126
+ }
127
+ worst = worst ? combineInternal(worst, innerVerdict) : innerVerdict;
128
+ }
129
+ if (!worst) {
130
+ return null;
131
+ }
132
+ return {
133
+ ...worst,
134
+ permission: 'allow',
135
+ opacity: 'recursive',
136
+ reason: 'command_substitution',
137
+ signals: [...worst.signals, 'command_substitution'],
138
+ };
139
+ }
140
+ function mergeLocation(left, right) {
141
+ if (left === right) {
142
+ return left;
143
+ }
144
+ if (left === 'unknown' || right === 'unknown') {
145
+ return 'unknown';
146
+ }
147
+ if (left === 'mixed' || right === 'mixed') {
148
+ return 'mixed';
149
+ }
150
+ return 'mixed';
151
+ }
152
+ function combineInternal(left, right) {
153
+ return {
154
+ permission: worsePermission(left.permission, right.permission),
155
+ location: mergeLocation(left.location, right.location),
156
+ opacity: left.opacity === 'unparseable' || right.opacity === 'unparseable'
157
+ ? 'unparseable'
158
+ : left.opacity === 'opaque' || right.opacity === 'opaque'
159
+ ? 'opaque'
160
+ : left.opacity === 'recursive' || right.opacity === 'recursive'
161
+ ? 'recursive'
162
+ : 'transparent',
163
+ effect: left.effect === 'remote_mutation' || right.effect === 'remote_mutation'
164
+ ? 'remote_mutation'
165
+ : left.effect === 'unknown' || right.effect === 'unknown'
166
+ ? 'unknown'
167
+ : left.effect === 'local_mutation' || right.effect === 'local_mutation'
168
+ ? 'local_mutation'
169
+ : 'read_only',
170
+ confidence: left.confidence === 'deterministic' || right.confidence === 'deterministic'
171
+ ? 'deterministic'
172
+ : left.confidence,
173
+ reason: worsePermission(left.permission, right.permission) === 'ask'
174
+ ? right.permission === 'ask'
175
+ ? right.reason
176
+ : left.reason
177
+ : right.reason,
178
+ signals: [...new Set([...left.signals, ...right.signals])],
179
+ judgeTrace: right.judgeTrace ?? left.judgeTrace,
180
+ };
181
+ }
182
+ function askVerdict(params) {
183
+ return { ...params, permission: 'ask' };
184
+ }
185
+ function allowVerdict(params) {
186
+ return { ...params, permission: 'allow' };
187
+ }
188
+ function withJudgeTrace(verdict, judgeTrace) {
189
+ if (!judgeTrace) {
190
+ return verdict;
191
+ }
192
+ return { ...verdict, judgeTrace };
193
+ }
194
+ function extractPathArgs(tokens) {
195
+ const redirects = extractRedirectTargets(tokens);
196
+ const args = [...redirects];
197
+ for (let index = 1; index < tokens.length; index += 1) {
198
+ const token = tokens[index];
199
+ if (!token || token.startsWith('-') || token === '>' || token === '>>' || token === '<') {
200
+ continue;
201
+ }
202
+ if (redirects.includes(token)) {
203
+ continue;
204
+ }
205
+ args.push(token);
206
+ }
207
+ return args;
208
+ }
209
+ function isVariableOrOpaquePathToken(token) {
210
+ return token.includes('$') || token.includes('`');
211
+ }
212
+ function isPureReadOnlySegment(segment) {
213
+ return PURE_READ_ONLY_KEYS.has(segment.key) || PURE_READ_ONLY_KEYS.has(segment.head);
214
+ }
215
+ function updateChainState(command, state) {
216
+ const segment = parseSegment(command);
217
+ if (segment.head !== 'cd') {
218
+ return state;
219
+ }
220
+ if (!state.trustedCwd) {
221
+ return state;
222
+ }
223
+ const target = segment.tokens[1] ?? '~';
224
+ if (!target || target === '-' || isVariableOrOpaquePathToken(target)) {
225
+ return { ...state, trustedCwd: false };
226
+ }
227
+ const resolved = resolveTrustedPath(target, state.cwd, state.trustedCwd);
228
+ if (!resolved) {
229
+ return { ...state, trustedCwd: false };
230
+ }
231
+ return {
232
+ cwd: resolved,
233
+ trustedCwd: true,
234
+ };
235
+ }
236
+ function tier0ExternalMatch(key, head, tokens) {
237
+ if (TIER0_EXTERNAL_KEYS.has(key)) {
238
+ return true;
239
+ }
240
+ if (TIER0_EXTERNAL_HEADS.has(head)) {
241
+ return true;
242
+ }
243
+ if (head === 'npm' && tokens[1] === 'publish') {
244
+ return true;
245
+ }
246
+ if (head === 'docker' &&
247
+ (tokens[1] === 'push' ||
248
+ tokens.some((t) => t === '--push' || t.startsWith('--output=type=registry')))) {
249
+ return true;
250
+ }
251
+ if (head === 'git' && tokens[1] === 'push') {
252
+ return true;
253
+ }
254
+ if (head === 'terraform' && tokens[1] === 'apply') {
255
+ return true;
256
+ }
257
+ return false;
258
+ }
259
+ function tier0HighStakesRm(tokens, context) {
260
+ const head = tokens[0] ?? '';
261
+ if (head !== 'rm') {
262
+ return null;
263
+ }
264
+ const targets = extractPathArgs(tokens);
265
+ const analysis = analyzePathTargets({
266
+ targets,
267
+ cwd: context.cwd,
268
+ repoRoot: context.repoRoot,
269
+ trustedCwd: context.trustedCwd,
270
+ sensitivePaths: context.sensitivePaths,
271
+ protectedArtifactRoots: context.protectedArtifactRoots,
272
+ });
273
+ if (!context.trustedCwd || !context.cwd) {
274
+ return askVerdict({
275
+ location: 'unknown',
276
+ opacity: 'transparent',
277
+ effect: 'unknown',
278
+ confidence: 'deterministic',
279
+ reason: 'missing_trusted_cwd',
280
+ signals: ['missing_trusted_cwd', ...analysis.signals],
281
+ });
282
+ }
283
+ if (analysis.isHighStakes) {
284
+ return askVerdict({
285
+ location: analysis.location,
286
+ opacity: 'transparent',
287
+ effect: 'local_mutation',
288
+ confidence: 'deterministic',
289
+ reason: 'high_stakes_path',
290
+ signals: ['high_stakes_path', ...analysis.signals],
291
+ });
292
+ }
293
+ for (const target of targets) {
294
+ if (target === '~' || target.startsWith('~/') || target.startsWith('/')) {
295
+ const resolved = path.resolve(target === '~' || target.startsWith('~/') ? (process.env.HOME ?? '/') : context.cwd, target);
296
+ const relative = relativeWithinRepo(context.repoRoot, resolved);
297
+ if (relative === null) {
298
+ return askVerdict({
299
+ location: 'repo_outside',
300
+ opacity: 'transparent',
301
+ effect: 'local_mutation',
302
+ confidence: 'deterministic',
303
+ reason: 'repo_outside_mutation',
304
+ signals: ['repo_outside_mutation'],
305
+ });
306
+ }
307
+ }
308
+ }
309
+ return null;
310
+ }
311
+ async function evaluateSegment(command, context, depth) {
312
+ const maxDepth = context.maxRecursionDepth ?? DEFAULT_MAX_DEPTH;
313
+ if (depth > maxDepth) {
314
+ return askVerdict({
315
+ location: 'unknown',
316
+ opacity: 'opaque',
317
+ effect: 'unknown',
318
+ confidence: 'deterministic',
319
+ reason: 'recursion_depth_exceeded',
320
+ signals: ['recursion_depth_exceeded'],
321
+ });
322
+ }
323
+ const opacity = segmentOpacity(command);
324
+ if (opacity === 'unparseable') {
325
+ if (context.unparseableShell === 'deny') {
326
+ return askVerdict({
327
+ location: 'unknown',
328
+ opacity: 'unparseable',
329
+ effect: 'unknown',
330
+ confidence: 'deterministic',
331
+ reason: 'unparseable_shell',
332
+ signals: ['unparseable_shell'],
333
+ });
334
+ }
335
+ return allowVerdict({
336
+ location: 'unknown',
337
+ opacity: 'unparseable',
338
+ effect: 'unknown',
339
+ confidence: 'deterministic',
340
+ reason: 'unparseable_shell',
341
+ signals: ['unparseable_shell'],
342
+ });
343
+ }
344
+ const substitutionResult = await evaluateSubstitutions(command, context, depth);
345
+ if (substitutionResult) {
346
+ return substitutionResult;
347
+ }
348
+ const tokens = tokenizeShell(command);
349
+ const { tokens: peeled, xargsStdinOpaque } = peelTransparentWrappers(tokens);
350
+ if (xargsStdinOpaque || isBareInterpreter(tokens)) {
351
+ return askVerdict({
352
+ location: 'unknown',
353
+ opacity: 'opaque',
354
+ effect: 'unknown',
355
+ confidence: 'deterministic',
356
+ reason: 'opaque_execution',
357
+ signals: ['opaque_execution'],
358
+ });
359
+ }
360
+ const segment = parseSegment(command);
361
+ const allowOverride = customAllowMatch(command, segment, context);
362
+ const externalOverride = customExternalMatch(command, segment, context);
363
+ if (allowOverride && externalOverride) {
364
+ return allowFromCustomOverride(opacity);
365
+ }
366
+ if (externalOverride) {
367
+ return askFromCustomExternal(opacity);
368
+ }
369
+ if (allowOverride && isRoutineLauncher(peeled)) {
370
+ return allowFromCustomOverride(opacity);
371
+ }
372
+ if (isVariableIndirectHead(segment.head)) {
373
+ return askVerdict({
374
+ location: 'unknown',
375
+ opacity: 'opaque',
376
+ effect: 'unknown',
377
+ confidence: 'deterministic',
378
+ reason: 'variable_indirect',
379
+ signals: ['variable_indirect'],
380
+ });
381
+ }
382
+ const recursiveScript = extractRecursiveScript(peeled);
383
+ if (recursiveScript) {
384
+ const prescan = prescanInterpreterCode(recursiveScript);
385
+ if (prescan && tier1RequiresAsk(prescan)) {
386
+ return askVerdict({
387
+ location: 'unknown',
388
+ opacity: 'recursive',
389
+ effect: 'unknown',
390
+ confidence: 'deterministic',
391
+ reason: 'interpreter_secret_prescan',
392
+ signals: ['interpreter_secret_prescan'],
393
+ });
394
+ }
395
+ const innerVerdict = await evaluateSegment(recursiveScript, context, depth + 1);
396
+ const wrapReason = segment.head === 'eval'
397
+ ? 'dynamic_shell_evaluation'
398
+ : ['bash', 'sh', 'zsh', 'dash', 'fish'].includes(segment.head)
399
+ ? 'shell_interpreter_script'
400
+ : innerVerdict.reason;
401
+ return {
402
+ ...innerVerdict,
403
+ opacity: 'recursive',
404
+ reason: wrapReason,
405
+ signals: [...innerVerdict.signals, 'recursive_wrapper'],
406
+ };
407
+ }
408
+ if (isRoutineLauncher(peeled)) {
409
+ const resolution = resolveLauncherRecipe({
410
+ tokens: peeled,
411
+ cwd: context.cwd,
412
+ repoRoot: context.repoRoot,
413
+ depth,
414
+ });
415
+ if (!resolution) {
416
+ return askVerdict({
417
+ location: 'unknown',
418
+ opacity: 'opaque',
419
+ effect: 'unknown',
420
+ confidence: 'deterministic',
421
+ reason: 'launcher_unresolved',
422
+ signals: ['launcher_unresolved'],
423
+ });
424
+ }
425
+ if (resolution.opaque || resolution.recipes.length === 0) {
426
+ return askVerdict({
427
+ location: 'unknown',
428
+ opacity: 'opaque',
429
+ effect: 'unknown',
430
+ confidence: 'deterministic',
431
+ reason: resolution.reason,
432
+ signals: [resolution.reason],
433
+ });
434
+ }
435
+ let innerVerdict = null;
436
+ for (const recipe of resolution.recipes) {
437
+ const evaluated = await evaluateSegment(recipe, context, depth + 1);
438
+ innerVerdict = innerVerdict ? combineInternal(innerVerdict, evaluated) : evaluated;
439
+ }
440
+ if (!innerVerdict) {
441
+ return askVerdict({
442
+ location: 'unknown',
443
+ opacity: 'opaque',
444
+ effect: 'unknown',
445
+ confidence: 'deterministic',
446
+ reason: resolution.reason,
447
+ signals: [resolution.reason],
448
+ });
449
+ }
450
+ return {
451
+ ...innerVerdict,
452
+ opacity: 'recursive',
453
+ signals: [...innerVerdict.signals, resolution.reason],
454
+ };
455
+ }
456
+ const egressClass = classifyEgressTool(segment.head, peeled);
457
+ if (egressClass === 'destructive') {
458
+ return askVerdict({
459
+ location: 'external',
460
+ opacity: 'transparent',
461
+ effect: 'remote_mutation',
462
+ confidence: 'deterministic',
463
+ reason: 'tier0_external',
464
+ signals: ['tier0_external', segment.head],
465
+ });
466
+ }
467
+ if (egressClass === 'read') {
468
+ return allowVerdict({
469
+ location: 'external',
470
+ opacity: 'transparent',
471
+ effect: 'read_only',
472
+ confidence: 'deterministic',
473
+ reason: 'egress_read',
474
+ signals: ['egress_read', segment.head],
475
+ });
476
+ }
477
+ if (tier0ExternalMatch(segment.key, segment.head, peeled)) {
478
+ return askVerdict({
479
+ location: 'external',
480
+ opacity: 'transparent',
481
+ effect: 'remote_mutation',
482
+ confidence: 'deterministic',
483
+ reason: 'tier0_external',
484
+ signals: ['tier0_external', segment.key],
485
+ });
486
+ }
487
+ const rmVerdict = tier0HighStakesRm(peeled, context);
488
+ if (rmVerdict) {
489
+ return rmVerdict;
490
+ }
491
+ let effect = 'unknown';
492
+ if (READ_ONLY_KEYS.has(segment.key) || READ_ONLY_KEYS.has(segment.head)) {
493
+ effect = 'read_only';
494
+ }
495
+ else if (LOCAL_MUTATION_KEYS.has(segment.key) || LOCAL_MUTATION_KEYS.has(segment.head)) {
496
+ effect = 'local_mutation';
497
+ }
498
+ else if (LOCAL_ROUTINE_HEADS.has(segment.head)) {
499
+ effect = 'local_mutation';
500
+ }
501
+ const pathArgs = extractPathArgs(peeled);
502
+ const pathAnalysis = analyzePathTargets({
503
+ targets: pathArgs,
504
+ cwd: context.cwd,
505
+ repoRoot: context.repoRoot,
506
+ trustedCwd: context.trustedCwd,
507
+ sensitivePaths: context.sensitivePaths,
508
+ protectedArtifactRoots: context.protectedArtifactRoots,
509
+ });
510
+ if (!context.trustedCwd || !context.cwd) {
511
+ if (opacity === 'opaque' || effect === 'unknown' || effect === 'local_mutation') {
512
+ return askVerdict({
513
+ location: 'unknown',
514
+ opacity,
515
+ effect: effect === 'read_only' ? 'unknown' : effect,
516
+ confidence: 'deterministic',
517
+ reason: 'missing_trusted_cwd',
518
+ signals: ['missing_trusted_cwd'],
519
+ });
520
+ }
521
+ if (effect === 'read_only' && !isPureReadOnlySegment(segment)) {
522
+ return askVerdict({
523
+ location: 'unknown',
524
+ opacity,
525
+ effect: 'read_only',
526
+ confidence: 'deterministic',
527
+ reason: 'missing_trusted_cwd',
528
+ signals: ['missing_trusted_cwd'],
529
+ });
530
+ }
531
+ }
532
+ if (pathAnalysis.isHighStakes) {
533
+ return askVerdict({
534
+ location: pathAnalysis.location,
535
+ opacity: 'transparent',
536
+ effect: 'local_mutation',
537
+ confidence: 'deterministic',
538
+ reason: 'high_stakes_path',
539
+ signals: pathAnalysis.signals,
540
+ });
541
+ }
542
+ if (segment.head === 'find' && isFindDangerous(peeled)) {
543
+ return askVerdict({
544
+ location: pathAnalysis.location === 'unknown' ? 'repo_local' : pathAnalysis.location,
545
+ opacity: 'transparent',
546
+ effect: 'local_mutation',
547
+ confidence: 'deterministic',
548
+ reason: 'find_dangerous_action',
549
+ signals: ['find_dangerous_action'],
550
+ });
551
+ }
552
+ if (pathAnalysis.location === 'repo_outside' || pathAnalysis.location === 'mixed') {
553
+ const outsideEffect = effect === 'read_only' ? 'read_only' : effect === 'unknown' ? 'local_mutation' : effect;
554
+ return askVerdict({
555
+ location: pathAnalysis.location,
556
+ opacity: 'transparent',
557
+ effect: outsideEffect,
558
+ confidence: 'deterministic',
559
+ reason: 'repo_outside_mutation',
560
+ signals: ['repo_outside_mutation', ...pathAnalysis.signals],
561
+ });
562
+ }
563
+ if (pathAnalysis.location === 'unknown' &&
564
+ pathArgs.length > 0 &&
565
+ LOCAL_MUTATION_KEYS.has(segment.head)) {
566
+ return askVerdict({
567
+ location: 'unknown',
568
+ opacity: 'transparent',
569
+ effect: 'unknown',
570
+ confidence: 'deterministic',
571
+ reason: 'unknown_location_mutation',
572
+ signals: ['unknown_location_mutation'],
573
+ });
574
+ }
575
+ const needsTier1 = effect === 'unknown' || TIER0_EXTERNAL_HEADS.has(segment.head) || egressClass === 'ambiguous';
576
+ let tier1Trace;
577
+ if (needsTier1) {
578
+ const tier1Text = recursiveScript ?? command;
579
+ const tier1 = await context.judge.evaluate({
580
+ text: tier1Text,
581
+ context: { cwd: context.cwd, repoRoot: context.repoRoot },
582
+ innerCode: recursiveScript ?? undefined,
583
+ });
584
+ tier1Trace = context.judge.lastTrace;
585
+ if (tier1RequiresAsk(tier1)) {
586
+ return askVerdict({
587
+ location: pathAnalysis.location === 'unknown' ? 'unknown' : 'repo_local',
588
+ opacity,
589
+ effect: tier1.external_change ? 'remote_mutation' : effect,
590
+ confidence: 'llm',
591
+ reason: 'tier1_catastrophic',
592
+ signals: ['tier1_catastrophic', tier1.reason],
593
+ judgeTrace: tier1Trace,
594
+ });
595
+ }
596
+ }
597
+ if (pathAnalysis.location === 'repo_local' &&
598
+ (effect === 'read_only' || effect === 'local_mutation') &&
599
+ opacity !== 'opaque') {
600
+ return withJudgeTrace(allowVerdict({
601
+ location: 'repo_local',
602
+ opacity,
603
+ effect,
604
+ confidence: 'assumed_repo_local',
605
+ reason: effect === 'read_only' ? 'read_only' : 'repo_local_mutation',
606
+ signals: effect === 'read_only' ? ['read_only'] : ['repo_local_mutation'],
607
+ }), tier1Trace);
608
+ }
609
+ if (effect === 'read_only') {
610
+ const readOnlyLocation = context.trustedCwd && context.cwd
611
+ ? pathAnalysis.location === 'unknown'
612
+ ? 'repo_local'
613
+ : pathAnalysis.location
614
+ : 'unknown';
615
+ return withJudgeTrace(allowVerdict({
616
+ location: readOnlyLocation,
617
+ opacity,
618
+ effect: 'read_only',
619
+ confidence: context.trustedCwd && context.cwd ? 'assumed_repo_local' : 'deterministic',
620
+ reason: 'read_only',
621
+ signals: ['read_only'],
622
+ }), tier1Trace);
623
+ }
624
+ if (allowOverride) {
625
+ return withJudgeTrace(allowFromCustomOverride(opacity), tier1Trace);
626
+ }
627
+ if (context.unknownLocalEffect === 'allow_flagged') {
628
+ return withJudgeTrace(allowVerdict({
629
+ location: pathAnalysis.location === 'unknown' ? 'repo_local' : pathAnalysis.location,
630
+ opacity,
631
+ effect: 'unknown',
632
+ confidence: 'assumed_repo_local',
633
+ reason: 'unknown_local_effect',
634
+ signals: ['unknown_local_effect'],
635
+ }), tier1Trace);
636
+ }
637
+ return withJudgeTrace(askVerdict({
638
+ location: pathAnalysis.location,
639
+ opacity,
640
+ effect,
641
+ confidence: 'deterministic',
642
+ reason: 'unknown_local_effect',
643
+ signals: ['unknown_local_effect'],
644
+ }), tier1Trace);
645
+ }
646
+ function toVerdictResult(internal, command, context, fingerprintCwd = context.cwd) {
647
+ const commandRedacted = redactCommand(command);
648
+ const relative = cwdRelative(context.repoRoot, fingerprintCwd);
649
+ return {
650
+ permission: internal.permission,
651
+ location: internal.location,
652
+ opacity: internal.opacity,
653
+ effect: internal.effect,
654
+ confidence: internal.confidence,
655
+ reason: internal.reason,
656
+ commandRedacted,
657
+ fingerprint: verdictFingerprint(relative, commandRedacted),
658
+ signals: internal.signals,
659
+ judgeTrace: internal.judgeTrace,
660
+ };
661
+ }
662
+ export async function verdict(command, context) {
663
+ const trimmed = command.trim();
664
+ if (!trimmed) {
665
+ return toVerdictResult(allowVerdict({
666
+ location: 'repo_local',
667
+ opacity: 'transparent',
668
+ effect: 'read_only',
669
+ confidence: 'deterministic',
670
+ reason: 'empty_command',
671
+ signals: ['empty_command'],
672
+ }), trimmed, context);
673
+ }
674
+ const segments = splitTopLevelSegments(trimmed);
675
+ let combined = null;
676
+ let chainState = {
677
+ cwd: context.cwd,
678
+ trustedCwd: context.trustedCwd,
679
+ };
680
+ for (const segment of segments) {
681
+ const segmentContext = {
682
+ ...context,
683
+ cwd: chainState.cwd,
684
+ trustedCwd: chainState.trustedCwd,
685
+ };
686
+ const segmentVerdict = await evaluateSegment(segment, segmentContext, 0);
687
+ combined = combined ? combineInternal(combined, segmentVerdict) : segmentVerdict;
688
+ chainState = updateChainState(segment, chainState);
689
+ }
690
+ return toVerdictResult(combined ??
691
+ askVerdict({
692
+ location: 'unknown',
693
+ opacity: 'unparseable',
694
+ effect: 'unknown',
695
+ confidence: 'deterministic',
696
+ reason: 'empty_segments',
697
+ signals: ['empty_segments'],
698
+ }), trimmed, context, chainState.cwd);
699
+ }