@agentic-sdlc-kit/cli 0.0.0-stage → 0.8.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 (240) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +32 -2
  3. package/dist/application/agent/execution-store.d.ts +55 -0
  4. package/dist/application/agent/execution-store.d.ts.map +1 -0
  5. package/dist/application/agent/execution-store.js +90 -0
  6. package/dist/application/agent/execution-store.js.map +1 -0
  7. package/dist/application/agent/gateway.d.ts +74 -0
  8. package/dist/application/agent/gateway.d.ts.map +1 -0
  9. package/dist/application/agent/gateway.js +580 -0
  10. package/dist/application/agent/gateway.js.map +1 -0
  11. package/dist/application/agent/index.d.ts +6 -0
  12. package/dist/application/agent/index.d.ts.map +1 -0
  13. package/dist/application/agent/index.js +18 -0
  14. package/dist/application/agent/index.js.map +1 -0
  15. package/dist/application/agent/orchestrator.d.ts +76 -0
  16. package/dist/application/agent/orchestrator.d.ts.map +1 -0
  17. package/dist/application/agent/orchestrator.js +422 -0
  18. package/dist/application/agent/orchestrator.js.map +1 -0
  19. package/dist/application/agent/providers/claude-code/claude-argv.d.ts +15 -0
  20. package/dist/application/agent/providers/claude-code/claude-argv.d.ts.map +1 -0
  21. package/dist/application/agent/providers/claude-code/claude-argv.js +98 -0
  22. package/dist/application/agent/providers/claude-code/claude-argv.js.map +1 -0
  23. package/dist/application/agent/providers/claude-code/index.d.ts +7 -0
  24. package/dist/application/agent/providers/claude-code/index.d.ts.map +1 -0
  25. package/dist/application/agent/providers/claude-code/index.js +13 -0
  26. package/dist/application/agent/providers/claude-code/index.js.map +1 -0
  27. package/dist/application/agent/providers/claude-code/mod-parity.d.ts +15 -0
  28. package/dist/application/agent/providers/claude-code/mod-parity.d.ts.map +1 -0
  29. package/dist/application/agent/providers/claude-code/mod-parity.js +41 -0
  30. package/dist/application/agent/providers/claude-code/mod-parity.js.map +1 -0
  31. package/dist/application/agent/providers/claude-code/permission-config.d.ts +59 -0
  32. package/dist/application/agent/providers/claude-code/permission-config.d.ts.map +1 -0
  33. package/dist/application/agent/providers/claude-code/permission-config.js +189 -0
  34. package/dist/application/agent/providers/claude-code/permission-config.js.map +1 -0
  35. package/dist/application/agent/providers/claude-code/provider.d.ts +38 -0
  36. package/dist/application/agent/providers/claude-code/provider.d.ts.map +1 -0
  37. package/dist/application/agent/providers/claude-code/provider.js +577 -0
  38. package/dist/application/agent/providers/claude-code/provider.js.map +1 -0
  39. package/dist/application/agent/providers/claude-code/resume-store.d.ts +25 -0
  40. package/dist/application/agent/providers/claude-code/resume-store.d.ts.map +1 -0
  41. package/dist/application/agent/providers/claude-code/resume-store.js +120 -0
  42. package/dist/application/agent/providers/claude-code/resume-store.js.map +1 -0
  43. package/dist/application/agent/providers/claude-code/stream-parser.d.ts +50 -0
  44. package/dist/application/agent/providers/claude-code/stream-parser.d.ts.map +1 -0
  45. package/dist/application/agent/providers/claude-code/stream-parser.js +129 -0
  46. package/dist/application/agent/providers/claude-code/stream-parser.js.map +1 -0
  47. package/dist/application/agent/test-fixture.d.ts +15 -0
  48. package/dist/application/agent/test-fixture.d.ts.map +1 -0
  49. package/dist/application/agent/test-fixture.js +93 -0
  50. package/dist/application/agent/test-fixture.js.map +1 -0
  51. package/dist/application/agent/workspace.d.ts +24 -0
  52. package/dist/application/agent/workspace.d.ts.map +1 -0
  53. package/dist/application/agent/workspace.js +139 -0
  54. package/dist/application/agent/workspace.js.map +1 -0
  55. package/dist/application/dispatcher.d.ts +13 -0
  56. package/dist/application/dispatcher.d.ts.map +1 -0
  57. package/dist/application/dispatcher.js +104 -0
  58. package/dist/application/dispatcher.js.map +1 -0
  59. package/dist/application/index.d.ts +5 -0
  60. package/dist/application/index.d.ts.map +1 -0
  61. package/dist/application/index.js +6 -0
  62. package/dist/application/index.js.map +1 -0
  63. package/dist/application/registry.d.ts +8 -0
  64. package/dist/application/registry.d.ts.map +1 -0
  65. package/dist/application/registry.js +17 -0
  66. package/dist/application/registry.js.map +1 -0
  67. package/dist/application/version.d.ts +2 -0
  68. package/dist/application/version.d.ts.map +1 -0
  69. package/dist/application/version.js +7 -0
  70. package/dist/application/version.js.map +1 -0
  71. package/dist/commands/doctor.d.ts +3 -0
  72. package/dist/commands/doctor.d.ts.map +1 -0
  73. package/dist/commands/doctor.js +68 -0
  74. package/dist/commands/doctor.js.map +1 -0
  75. package/dist/commands/elicit.d.ts +3 -0
  76. package/dist/commands/elicit.d.ts.map +1 -0
  77. package/dist/commands/elicit.js +163 -0
  78. package/dist/commands/elicit.js.map +1 -0
  79. package/dist/commands/evolve.d.ts +3 -0
  80. package/dist/commands/evolve.d.ts.map +1 -0
  81. package/dist/commands/evolve.js +165 -0
  82. package/dist/commands/evolve.js.map +1 -0
  83. package/dist/commands/feature.d.ts +13 -0
  84. package/dist/commands/feature.d.ts.map +1 -0
  85. package/dist/commands/feature.js +326 -0
  86. package/dist/commands/feature.js.map +1 -0
  87. package/dist/commands/fix.d.ts +3 -0
  88. package/dist/commands/fix.d.ts.map +1 -0
  89. package/dist/commands/fix.js +61 -0
  90. package/dist/commands/fix.js.map +1 -0
  91. package/dist/commands/help.d.ts +4 -0
  92. package/dist/commands/help.d.ts.map +1 -0
  93. package/dist/commands/help.js +32 -0
  94. package/dist/commands/help.js.map +1 -0
  95. package/dist/commands/index.d.ts +21 -0
  96. package/dist/commands/index.d.ts.map +1 -0
  97. package/dist/commands/index.js +22 -0
  98. package/dist/commands/index.js.map +1 -0
  99. package/dist/commands/init.d.ts +3 -0
  100. package/dist/commands/init.d.ts.map +1 -0
  101. package/dist/commands/init.js +90 -0
  102. package/dist/commands/init.js.map +1 -0
  103. package/dist/commands/inspect.d.ts +3 -0
  104. package/dist/commands/inspect.d.ts.map +1 -0
  105. package/dist/commands/inspect.js +64 -0
  106. package/dist/commands/inspect.js.map +1 -0
  107. package/dist/commands/install.d.ts +3 -0
  108. package/dist/commands/install.d.ts.map +1 -0
  109. package/dist/commands/install.js +329 -0
  110. package/dist/commands/install.js.map +1 -0
  111. package/dist/commands/journal.d.ts +3 -0
  112. package/dist/commands/journal.d.ts.map +1 -0
  113. package/dist/commands/journal.js +107 -0
  114. package/dist/commands/journal.js.map +1 -0
  115. package/dist/commands/memory.d.ts +3 -0
  116. package/dist/commands/memory.d.ts.map +1 -0
  117. package/dist/commands/memory.js +85 -0
  118. package/dist/commands/memory.js.map +1 -0
  119. package/dist/commands/new.d.ts +3 -0
  120. package/dist/commands/new.d.ts.map +1 -0
  121. package/dist/commands/new.js +68 -0
  122. package/dist/commands/new.js.map +1 -0
  123. package/dist/commands/next.d.ts +3 -0
  124. package/dist/commands/next.d.ts.map +1 -0
  125. package/dist/commands/next.js +107 -0
  126. package/dist/commands/next.js.map +1 -0
  127. package/dist/commands/optimize.d.ts +3 -0
  128. package/dist/commands/optimize.d.ts.map +1 -0
  129. package/dist/commands/optimize.js +28 -0
  130. package/dist/commands/optimize.js.map +1 -0
  131. package/dist/commands/plugin-linker.d.ts +34 -0
  132. package/dist/commands/plugin-linker.d.ts.map +1 -0
  133. package/dist/commands/plugin-linker.js +177 -0
  134. package/dist/commands/plugin-linker.js.map +1 -0
  135. package/dist/commands/refactor.d.ts +3 -0
  136. package/dist/commands/refactor.d.ts.map +1 -0
  137. package/dist/commands/refactor.js +32 -0
  138. package/dist/commands/refactor.js.map +1 -0
  139. package/dist/commands/types.d.ts +19 -0
  140. package/dist/commands/types.d.ts.map +1 -0
  141. package/dist/commands/types.js +7 -0
  142. package/dist/commands/types.js.map +1 -0
  143. package/dist/commands/validate-plan.d.ts +3 -0
  144. package/dist/commands/validate-plan.d.ts.map +1 -0
  145. package/dist/commands/validate-plan.js +62 -0
  146. package/dist/commands/validate-plan.js.map +1 -0
  147. package/dist/commands/validate-spec.d.ts +3 -0
  148. package/dist/commands/validate-spec.d.ts.map +1 -0
  149. package/dist/commands/validate-spec.js +62 -0
  150. package/dist/commands/validate-spec.js.map +1 -0
  151. package/dist/commands/verify.d.ts +3 -0
  152. package/dist/commands/verify.d.ts.map +1 -0
  153. package/dist/commands/verify.js +103 -0
  154. package/dist/commands/verify.js.map +1 -0
  155. package/dist/commands/version.d.ts +3 -0
  156. package/dist/commands/version.d.ts.map +1 -0
  157. package/dist/commands/version.js +12 -0
  158. package/dist/commands/version.js.map +1 -0
  159. package/dist/index.d.ts +3 -0
  160. package/dist/index.d.ts.map +1 -0
  161. package/dist/index.js +27 -0
  162. package/dist/index.js.map +1 -0
  163. package/dist/output/formatter.d.ts +2 -0
  164. package/dist/output/formatter.d.ts.map +1 -0
  165. package/dist/output/formatter.js +10 -0
  166. package/dist/output/formatter.js.map +1 -0
  167. package/dist/output/index.d.ts +2 -0
  168. package/dist/output/index.d.ts.map +1 -0
  169. package/dist/output/index.js +3 -0
  170. package/dist/output/index.js.map +1 -0
  171. package/package.json +37 -3
  172. package/src/application/agent/execution-store.ts +162 -0
  173. package/src/application/agent/gateway.test.ts +529 -0
  174. package/src/application/agent/gateway.ts +744 -0
  175. package/src/application/agent/index.ts +18 -0
  176. package/src/application/agent/orchestrator.test.ts +341 -0
  177. package/src/application/agent/orchestrator.ts +628 -0
  178. package/src/application/agent/providers/claude-code/claude-argv.ts +119 -0
  179. package/src/application/agent/providers/claude-code/claude-code-smoke.test.ts +139 -0
  180. package/src/application/agent/providers/claude-code/fixtures/fake-claude.mjs +125 -0
  181. package/src/application/agent/providers/claude-code/index.ts +28 -0
  182. package/src/application/agent/providers/claude-code/mod-parity.ts +50 -0
  183. package/src/application/agent/providers/claude-code/permission-config.test.ts +221 -0
  184. package/src/application/agent/providers/claude-code/permission-config.ts +242 -0
  185. package/src/application/agent/providers/claude-code/provider.test.ts +378 -0
  186. package/src/application/agent/providers/claude-code/provider.ts +717 -0
  187. package/src/application/agent/providers/claude-code/resume-store.test.ts +161 -0
  188. package/src/application/agent/providers/claude-code/resume-store.ts +157 -0
  189. package/src/application/agent/providers/claude-code/stream-parser.ts +166 -0
  190. package/src/application/agent/test-fixture.ts +131 -0
  191. package/src/application/agent/workspace.test.ts +104 -0
  192. package/src/application/agent/workspace.ts +156 -0
  193. package/src/application/dispatcher.test.ts +89 -0
  194. package/src/application/dispatcher.ts +98 -0
  195. package/src/application/index.ts +5 -0
  196. package/src/application/registry.ts +24 -0
  197. package/src/application/version.ts +7 -0
  198. package/src/commands/clse.test.ts +181 -0
  199. package/src/commands/doctor.test.ts +29 -0
  200. package/src/commands/doctor.ts +78 -0
  201. package/src/commands/elicit.test.ts +111 -0
  202. package/src/commands/elicit.ts +188 -0
  203. package/src/commands/evolve.ts +209 -0
  204. package/src/commands/feature.test.ts +323 -0
  205. package/src/commands/feature.ts +405 -0
  206. package/src/commands/fix.test.ts +36 -0
  207. package/src/commands/fix.ts +69 -0
  208. package/src/commands/help.test.ts +33 -0
  209. package/src/commands/help.ts +35 -0
  210. package/src/commands/index.ts +21 -0
  211. package/src/commands/init.test.ts +65 -0
  212. package/src/commands/init.ts +103 -0
  213. package/src/commands/inspect.test.ts +57 -0
  214. package/src/commands/inspect.ts +73 -0
  215. package/src/commands/install.test.ts +61 -0
  216. package/src/commands/install.ts +375 -0
  217. package/src/commands/journal.ts +118 -0
  218. package/src/commands/memory.ts +96 -0
  219. package/src/commands/new.test.ts +44 -0
  220. package/src/commands/new.ts +77 -0
  221. package/src/commands/next.test.ts +241 -0
  222. package/src/commands/next.ts +124 -0
  223. package/src/commands/optimize.ts +30 -0
  224. package/src/commands/plugin-linker.test.ts +139 -0
  225. package/src/commands/plugin-linker.ts +196 -0
  226. package/src/commands/refactor.ts +34 -0
  227. package/src/commands/stack-commands.test.ts +67 -0
  228. package/src/commands/tier-c-conformance.test.ts +194 -0
  229. package/src/commands/types.ts +26 -0
  230. package/src/commands/validate-plan.test.ts +107 -0
  231. package/src/commands/validate-plan.ts +67 -0
  232. package/src/commands/validate-spec.test.ts +59 -0
  233. package/src/commands/validate-spec.ts +68 -0
  234. package/src/commands/verify.test.ts +117 -0
  235. package/src/commands/verify.ts +112 -0
  236. package/src/commands/version.test.ts +11 -0
  237. package/src/commands/version.ts +14 -0
  238. package/src/index.ts +30 -0
  239. package/src/output/formatter.ts +10 -0
  240. package/src/output/index.ts +2 -0
@@ -0,0 +1,221 @@
1
+ // packages/cli/src/application/agent/providers/claude-code/permission-config.test.ts
2
+ //
3
+ // ADR-0020 §3 POLICY PARITY TESTS (release blockers on failure).
4
+ //
5
+ // The two enforcement points — Kit's capability grant check and the
6
+ // generated Claude Code runtime representation — must produce EQUIVALENT
7
+ // decisions for identical policy inputs: a capability the snapshot did not
8
+ // grant yields NO allowing tool rule (and an explicit deny where the tool
9
+ // is unconditionally dangerous to leave ambiguous), and every Bash rule in
10
+ // the generated allowlist maps back to exactly the capability that produced
11
+ // it (single table, both directions — parity by construction, pinned here).
12
+
13
+ import { describe, expect, it } from 'vitest';
14
+ import { AGENT_CAPABILITIES, type AgentCapability } from '@agentic-sdlc-kit/contracts';
15
+ import {
16
+ BASH_RULE_CAPABILITIES,
17
+ bashRulesForCapability,
18
+ deriveClaudeCodePermissionConfig,
19
+ inferCapabilityFromBashCommand,
20
+ inferCapabilityFromToolUse,
21
+ } from './permission-config.js';
22
+ import { buildClaudeArgv } from './claude-argv.js';
23
+
24
+ const ALL_TOOLS = [
25
+ 'Read', 'Grep', 'Glob', 'LS',
26
+ 'Write', 'Edit', 'NotebookEdit',
27
+ 'WebFetch', 'WebSearch',
28
+ 'Task',
29
+ ];
30
+
31
+ function configFor(
32
+ capabilities: readonly AgentCapability[],
33
+ overrides: Partial<{ writablePaths: string[]; networkAllowed: boolean }> = {},
34
+ ) {
35
+ return deriveClaudeCodePermissionConfig(
36
+ {
37
+ capabilities,
38
+ writablePaths: overrides.writablePaths ?? ['/ws/run-001'],
39
+ networkAllowed: overrides.networkAllowed ?? false,
40
+ },
41
+ { maxToolCalls: 25 },
42
+ );
43
+ }
44
+
45
+ describe('deriveClaudeCodePermissionConfig (ADR-0020 §2 generated representation)', () => {
46
+ it('grants repository.read -> observation tools only', () => {
47
+ const cfg = configFor(['repository.read']);
48
+ expect(cfg.allowedTools).toEqual(expect.arrayContaining(['Read', 'Grep', 'Glob', 'LS']));
49
+ expect(cfg.allowedTools).not.toContain('Write');
50
+ expect(cfg.allowedTools).not.toContain('Edit');
51
+ });
52
+
53
+ it('scopes repository.write file tools to the granted writablePaths', () => {
54
+ const cfg = configFor(['repository.read', 'repository.write'], {
55
+ writablePaths: ['/ws/run-001', '/ws/shared'],
56
+ });
57
+ expect(cfg.allowedTools).toContain('Write(/ws/run-001/**)');
58
+ expect(cfg.allowedTools).toContain('Edit(/ws/run-001/**)');
59
+ expect(cfg.allowedTools).toContain('Write(/ws/shared/**)');
60
+ // NO unscoped file mutation: an absolute-scope rule per path only.
61
+ expect(cfg.allowedTools).not.toContain('Write');
62
+ expect(cfg.allowedTools).not.toContain('Edit');
63
+ });
64
+
65
+ it('denies network tools when the capability is not granted (fail closed)', () => {
66
+ const cfg = configFor(['repository.read']);
67
+ expect(cfg.disallowedTools).toEqual(expect.arrayContaining(['WebFetch', 'WebSearch']));
68
+ });
69
+
70
+ it('denies network tools even when granted if networkAllowed is false', () => {
71
+ const cfg = configFor(['network.access'], { networkAllowed: false });
72
+ expect(cfg.allowedTools).not.toContain('WebFetch');
73
+ expect(cfg.disallowedTools).toEqual(expect.arrayContaining(['WebFetch', 'WebSearch']));
74
+ });
75
+
76
+ it('allows network tools only when granted AND networkAllowed', () => {
77
+ const cfg = configFor(['network.access'], { networkAllowed: true });
78
+ expect(cfg.allowedTools).toEqual(expect.arrayContaining(['WebFetch', 'WebSearch']));
79
+ });
80
+
81
+ it('always denies the Task subagent tool (one execution = ONE bounded agent)', () => {
82
+ for (const capability of AGENT_CAPABILITIES) {
83
+ expect(configFor([capability]).disallowedTools).toContain('Task');
84
+ }
85
+ });
86
+
87
+ it('never generates bypassPermissions (ADR-0019 §6.5)', () => {
88
+ for (const capability of AGENT_CAPABILITIES) {
89
+ expect(configFor([capability]).permissionMode).not.toBe('bypassPermissions');
90
+ }
91
+ });
92
+
93
+ it('derives maxTurns from the budget tool-call bound', () => {
94
+ expect(configFor(['repository.read']).maxTurns).toBe(25);
95
+ expect(
96
+ deriveClaudeCodePermissionConfig(
97
+ { capabilities: ['repository.read'], writablePaths: ['/ws'], networkAllowed: false },
98
+ { maxToolCalls: 0 },
99
+ ).maxTurns,
100
+ ).toBeUndefined();
101
+ });
102
+ });
103
+
104
+ describe('parity: grant <-> runtime representation equivalence (ADR-0020 §3)', () => {
105
+ it('every capability denied by the grant is absent from the allowlist', () => {
106
+ for (const granted of AGENT_CAPABILITIES) {
107
+ const cfg = configFor([granted]);
108
+ for (const denied of AGENT_CAPABILITIES) {
109
+ if (denied === granted) continue;
110
+ const rules = rulesOf(cfg.allowedTools, denied);
111
+ expect(rules, `capability ${denied} leaked into allowlist when only ${granted} was granted`).toEqual([]);
112
+ }
113
+ }
114
+ });
115
+
116
+ it('every Bash rule in the allowlist maps back to the capability that produced it', () => {
117
+ for (const capability of BASH_RULE_CAPABILITIES) {
118
+ const cfg = configFor([capability]);
119
+ const rules = cfg.allowedTools.filter((r) => r.startsWith('Bash('));
120
+ expect(rules.length).toBeGreaterThan(0);
121
+ for (const rule of rules) {
122
+ const prefix = rule.slice('Bash('.length, -':*)'.length);
123
+ expect(inferCapabilityFromBashCommand(prefix)).toBe(capability);
124
+ }
125
+ }
126
+ });
127
+
128
+ it('round-trip: every table rule infers back to its own capability', () => {
129
+ for (const capability of BASH_RULE_CAPABILITIES) {
130
+ for (const rule of bashRulesForCapability(capability)) {
131
+ expect(inferCapabilityFromBashCommand(rule.prefix)).toBe(capability);
132
+ }
133
+ }
134
+ });
135
+
136
+ it('more specific prefixes win: tsc --noEmit infers typecheck, not build', () => {
137
+ expect(inferCapabilityFromBashCommand('tsc --noEmit --strict')).toBe('process.typecheck');
138
+ expect(inferCapabilityFromBashCommand('tsc --build')).toBe('process.build');
139
+ });
140
+
141
+ it('unmapped Bash commands infer undefined (never a guessed capability)', () => {
142
+ expect(inferCapabilityFromBashCommand('rm -rf /')).toBeUndefined();
143
+ expect(inferCapabilityFromBashCommand('curl https://evil.example')).toBeUndefined();
144
+ });
145
+
146
+ it('tool-use inference mirrors the derivation table', () => {
147
+ expect(inferCapabilityFromToolUse('Read', {})).toBe('repository.read');
148
+ expect(inferCapabilityFromToolUse('Grep', {})).toBe('repository.read');
149
+ expect(inferCapabilityFromToolUse('Edit', { file_path: '/x' })).toBe('repository.write');
150
+ expect(inferCapabilityFromToolUse('Write', { file_path: '/x' })).toBe('repository.write');
151
+ expect(inferCapabilityFromToolUse('WebFetch', { url: 'https://x' })).toBe('network.access');
152
+ expect(inferCapabilityFromToolUse('Bash', { command: 'git status' })).toBe('git.read');
153
+ expect(inferCapabilityFromToolUse('Bash', { command: 'pnpm test' })).toBe('process.test');
154
+ expect(inferCapabilityFromToolUse('SomeNewTool', {})).toBeUndefined();
155
+ });
156
+ });
157
+
158
+ describe('argv carries the generated representation, never a bypass', () => {
159
+ it('includes derived flags and excludes permission bypasses', () => {
160
+ const { args } = buildClaudeArgv(fakeRequest(['repository.read', 'repository.write', 'process.test']));
161
+ const joined = args.join(' ');
162
+ expect(joined).toContain('-p');
163
+ expect(joined).toContain('--output-format stream-json');
164
+ expect(joined).toContain('--verbose');
165
+ expect(joined).toContain('--permission-mode default');
166
+ expect(joined).toContain('--allowedTools');
167
+ expect(joined).toContain('Write(/ws/run-001/**)');
168
+ expect(joined).toContain('Bash(pnpm test:*)');
169
+ expect(joined).toContain('--max-turns 40');
170
+ expect(joined).not.toContain('bypassPermissions');
171
+ expect(joined).not.toContain('--dangerously-skip-permissions');
172
+ expect(joined).toContain('--disallowedTools');
173
+ expect(joined).toContain('Task');
174
+ });
175
+
176
+ it('external.approval grants no tool (the approval policy governs it)', () => {
177
+ const cfg = configFor(['external.approval']);
178
+ const toolRules = cfg.allowedTools.filter((r) => ALL_TOOLS.some((t) => r === t || r.startsWith(`${t}(`)));
179
+ expect(toolRules).toEqual([]);
180
+ });
181
+ });
182
+
183
+ function rulesOf(allowedTools: readonly string[], capability: AgentCapability): string[] {
184
+ const prefixes = bashRulesForCapability(capability).map((r) => `Bash(${r.prefix}:*)`);
185
+ return allowedTools.filter((r) => prefixes.includes(r));
186
+ }
187
+
188
+ function fakeRequest(capabilities: AgentCapability[]) {
189
+ return {
190
+ schemaVersion: 2 as const,
191
+ executionId: 'exec-RUN-001-TASK-001-red-1',
192
+ workflowRunId: 'RUN-001',
193
+ taskId: 'TASK-001',
194
+ providerId: 'claude-code',
195
+ taskKind: 'test_authoring' as const,
196
+ policyHash: 'sha256:test-policy',
197
+ workspace: {
198
+ id: 'ws-run-001',
199
+ kind: 'git-worktree' as const,
200
+ rootPath: '/ws/run-001',
201
+ branch: 'kit/RUN-001',
202
+ baseCommitSha: 'a'.repeat(40),
203
+ createdAt: new Date().toISOString(),
204
+ },
205
+ instructions: 'Write the failing test.',
206
+ context: { relevantFiles: [], constraints: [] },
207
+ permissions: {
208
+ capabilities,
209
+ writablePaths: ['/ws/run-001'],
210
+ networkAllowed: false,
211
+ },
212
+ budget: {
213
+ maxDurationMs: 60_000,
214
+ maxToolCalls: 40,
215
+ },
216
+ approvalPolicy: {
217
+ requireApprovalFor: ['external.approval'],
218
+ allowAutonomousExecution: true,
219
+ },
220
+ };
221
+ }
@@ -0,0 +1,242 @@
1
+ // packages/cli/src/application/agent/providers/claude-code/permission-config.ts
2
+ //
3
+ // The GENERATED runtime policy representation for Claude Code (ADR-0020 §2).
4
+ //
5
+ // ADR-0020 §2: runtime enforcement consumes a representation GENERATED from
6
+ // the canonical ExecutionPolicy snapshot — never a hand-maintained second
7
+ // policy. This module is that generator for the claude-code provider: it
8
+ // derives Claude Code's session permission configuration (tool allowlist,
9
+ // denylist, permission mode, turn bound) from the same snapshot whose hash
10
+ // every event must echo.
11
+ //
12
+ // Parity (ADR-0020 §3) is pinned by permission-config.test.ts: a capability
13
+ // the snapshot did not grant produces NO allowing tool rule (and for network
14
+ // tools an explicit deny), and every Bash prefix rule maps back to exactly
15
+ // the capability that produced it (single mapping table, used for both
16
+ // derivation and event inference — parity by construction).
17
+ //
18
+ // Hard rules enforced here (ADR-0019 §6.5, ADR-0020 §6/§7):
19
+ // - `bypassPermissions` is not representable: the mode type excludes it
20
+ // and the argv builder asserts it is never passed.
21
+ // - File-mutating tools are scoped to the granted writablePaths only.
22
+ // - `networkAllowed: false` denies WebFetch/WebSearch even when the
23
+ // `network.access` capability was granted (fail closed).
24
+ // - The subagent tool (`Task`) is always denied: one execution is ONE
25
+ // bounded agent, not an unbounded subagent tree.
26
+
27
+ import { AGENT_CAPABILITIES, type AgentCapability } from '@agentic-sdlc-kit/contracts';
28
+
29
+ /** Claude Code permission modes we are willing to generate. */
30
+ export type ClaudeCodePermissionMode = 'default' | 'acceptEdits' | 'plan';
31
+
32
+ /**
33
+ * The generated representation of the canonical policy for one execution.
34
+ * Purely derived: same snapshot in, same config out, no hidden state.
35
+ */
36
+ export interface ClaudeCodePermissionConfig {
37
+ /** Rules pre-approving exactly what the policy granted (Claude Code rule syntax). */
38
+ readonly allowedTools: readonly string[];
39
+ /** Tools explicitly denied regardless of the allowlist. */
40
+ readonly disallowedTools: readonly string[];
41
+ /** Never `bypassPermissions` (ADR-0019 §6.5). */
42
+ readonly permissionMode: ClaudeCodePermissionMode;
43
+ /** Turn bound derived from budget.maxToolCalls (defense in depth; the gateway enforces the real count). */
44
+ readonly maxTurns: number | undefined;
45
+ }
46
+
47
+ /**
48
+ * One Bash prefix rule. The SAME table drives both directions:
49
+ * - derivation: capability granted -> `Bash(prefix:*)` rule emitted;
50
+ * - inference: observed tool call -> capability that authorized it.
51
+ */
52
+ interface BashRule {
53
+ /** Command prefix (compared against the start of the Bash command). */
54
+ readonly prefix: string;
55
+ /** Number of command tokens the rule matches (e.g. 3 for `npm run test`). */
56
+ readonly matchTokens: number;
57
+ readonly capability: AgentCapability;
58
+ }
59
+
60
+ const BASH_RULES: readonly BashRule[] = [
61
+ // git.read
62
+ { prefix: 'git log', matchTokens: 2, capability: 'git.read' },
63
+ { prefix: 'git diff', matchTokens: 2, capability: 'git.read' },
64
+ { prefix: 'git status', matchTokens: 2, capability: 'git.read' },
65
+ { prefix: 'git show', matchTokens: 2, capability: 'git.read' },
66
+ { prefix: 'git blame', matchTokens: 2, capability: 'git.read' },
67
+ // git.write
68
+ { prefix: 'git add', matchTokens: 2, capability: 'git.write' },
69
+ { prefix: 'git commit', matchTokens: 2, capability: 'git.write' },
70
+ { prefix: 'git checkout', matchTokens: 2, capability: 'git.write' },
71
+ { prefix: 'git switch', matchTokens: 2, capability: 'git.write' },
72
+ { prefix: 'git branch', matchTokens: 2, capability: 'git.write' },
73
+ { prefix: 'git restore', matchTokens: 2, capability: 'git.write' },
74
+ { prefix: 'git stash', matchTokens: 2, capability: 'git.write' },
75
+ // process.test
76
+ { prefix: 'npm test', matchTokens: 2, capability: 'process.test' },
77
+ { prefix: 'npm run test', matchTokens: 3, capability: 'process.test' },
78
+ { prefix: 'pnpm test', matchTokens: 2, capability: 'process.test' },
79
+ { prefix: 'pnpm run test', matchTokens: 3, capability: 'process.test' },
80
+ { prefix: 'pnpm vitest', matchTokens: 2, capability: 'process.test' },
81
+ { prefix: 'npx vitest', matchTokens: 2, capability: 'process.test' },
82
+ { prefix: 'vitest', matchTokens: 1, capability: 'process.test' },
83
+ { prefix: 'node --test', matchTokens: 2, capability: 'process.test' },
84
+ // process.build
85
+ { prefix: 'npm run build', matchTokens: 3, capability: 'process.build' },
86
+ { prefix: 'pnpm build', matchTokens: 2, capability: 'process.build' },
87
+ { prefix: 'pnpm run build', matchTokens: 3, capability: 'process.build' },
88
+ { prefix: 'npx tsc', matchTokens: 2, capability: 'process.build' },
89
+ { prefix: 'tsc', matchTokens: 1, capability: 'process.build' },
90
+ // process.lint
91
+ { prefix: 'npm run lint', matchTokens: 3, capability: 'process.lint' },
92
+ { prefix: 'pnpm lint', matchTokens: 2, capability: 'process.lint' },
93
+ { prefix: 'pnpm run lint', matchTokens: 3, capability: 'process.lint' },
94
+ { prefix: 'npx eslint', matchTokens: 2, capability: 'process.lint' },
95
+ { prefix: 'eslint', matchTokens: 1, capability: 'process.lint' },
96
+ // process.typecheck
97
+ { prefix: 'npm run typecheck', matchTokens: 3, capability: 'process.typecheck' },
98
+ { prefix: 'pnpm typecheck', matchTokens: 2, capability: 'process.typecheck' },
99
+ { prefix: 'pnpm run typecheck', matchTokens: 3, capability: 'process.typecheck' },
100
+ { prefix: 'tsc --noEmit', matchTokens: 2, capability: 'process.typecheck' },
101
+ ];
102
+
103
+ /** Capabilities that map to non-Bash Claude Code tools. */
104
+ const READ_TOOLS: readonly string[] = ['Read', 'Grep', 'Glob', 'LS'];
105
+ const WRITE_TOOLS: readonly string[] = ['Write', 'Edit', 'NotebookEdit'];
106
+ const NETWORK_TOOLS: readonly string[] = ['WebFetch', 'WebSearch'];
107
+
108
+ /** Always denied: subagents would multiply an execution beyond its bounds. */
109
+ const ALWAYS_DENIED: readonly string[] = ['Task'];
110
+
111
+ function has(list: readonly AgentCapability[], value: AgentCapability): boolean {
112
+ return list.includes(value);
113
+ }
114
+
115
+ /**
116
+ * Derive the Claude Code permission configuration from the canonical
117
+ * snapshot fields (the same fields the gateway freezes into the request).
118
+ *
119
+ * NOTE: takes the REQUEST's permission fields, which the gateway has already
120
+ * validated equal the canonical snapshot and whose policyHash the events
121
+ * must echo — so this derivation is traceable to the exact policy that
122
+ * authorized the execution.
123
+ */
124
+ export function deriveClaudeCodePermissionConfig(
125
+ permissions: {
126
+ capabilities: readonly AgentCapability[];
127
+ writablePaths: readonly string[];
128
+ networkAllowed: boolean;
129
+ },
130
+ budget: { maxToolCalls: number },
131
+ ): ClaudeCodePermissionConfig {
132
+ const granted = permissions.capabilities;
133
+ const allowed: string[] = [];
134
+ const disallowed: string[] = [...ALWAYS_DENIED];
135
+
136
+ // repository.read -> observation tools
137
+ if (has(granted, 'repository.read')) allowed.push(...READ_TOOLS);
138
+
139
+ // repository.write -> file mutation tools, SCOPED to writablePaths
140
+ if (has(granted, 'repository.write')) {
141
+ for (const tool of WRITE_TOOLS) {
142
+ for (const target of permissions.writablePaths) {
143
+ allowed.push(`${tool}(${target}/**)`);
144
+ }
145
+ }
146
+ }
147
+
148
+ // git.* / process.* -> Bash prefix rules from the single mapping table
149
+ for (const rule of BASH_RULES) {
150
+ if (has(granted, rule.capability)) allowed.push(`Bash(${rule.prefix}:*)`);
151
+ }
152
+
153
+ // network.access -> web tools; networkAllowed=false denies them (fail closed)
154
+ if (has(granted, 'network.access')) {
155
+ if (permissions.networkAllowed) allowed.push(...NETWORK_TOOLS);
156
+ else disallowed.push(...NETWORK_TOOLS);
157
+ } else {
158
+ disallowed.push(...NETWORK_TOOLS);
159
+ }
160
+
161
+ // external.approval is not a tool: the approval POLICY governs it (the
162
+ // provider blocks autonomously when it requires human approval).
163
+
164
+ return {
165
+ allowedTools: dedupe(allowed),
166
+ disallowedTools: dedupe(disallowed),
167
+ // 'default': headless sessions cannot prompt, so anything outside the
168
+ // allowlist is denied by Claude Code itself — which is exactly the
169
+ // fail-closed behavior the policy wants. Never bypassPermissions.
170
+ permissionMode: 'default',
171
+ maxTurns: budget.maxToolCalls > 0 ? budget.maxToolCalls : undefined,
172
+ };
173
+ }
174
+
175
+ function dedupe(list: readonly string[]): string[] {
176
+ return [...new Set(list)];
177
+ }
178
+
179
+ /**
180
+ * Infer the capability an observed tool call exercises — the reverse
181
+ * direction of the derivation, from the SAME mapping table. Used for
182
+ * `tool.requested` events (observability only; an event never authorizes
183
+ * anything).
184
+ *
185
+ * Unmapped tools return undefined: the provider skips the tool.requested
186
+ * event rather than asserting a capability it cannot honestly name.
187
+ */
188
+ export function inferCapabilityFromToolUse(
189
+ toolName: string,
190
+ input: unknown,
191
+ ): AgentCapability | undefined {
192
+ if (READ_TOOLS.includes(toolName)) return 'repository.read';
193
+ if (WRITE_TOOLS.includes(toolName)) return 'repository.write';
194
+ if (NETWORK_TOOLS.includes(toolName)) return 'network.access';
195
+ if (toolName === 'Bash') return inferCapabilityFromBashCommand(bashCommandOf(input));
196
+ return undefined;
197
+ }
198
+
199
+ function bashCommandOf(input: unknown): string {
200
+ if (
201
+ input &&
202
+ typeof input === 'object' &&
203
+ typeof (input as { command?: unknown }).command === 'string'
204
+ ) {
205
+ return (input as { command: string }).command;
206
+ }
207
+ return '';
208
+ }
209
+
210
+ /**
211
+ * Match a Bash command against the rule table. Longer (more specific)
212
+ * prefixes win, so `tsc --noEmit` resolves to process.typecheck, not the
213
+ * 1-token `tsc` build rule.
214
+ */
215
+ export function inferCapabilityFromBashCommand(command: string): AgentCapability | undefined {
216
+ const trimmed = command.trim();
217
+ let best: { rule: BashRule; score: number } | undefined;
218
+ for (const rule of BASH_RULES) {
219
+ if (trimmed.startsWith(rule.prefix)) {
220
+ const score = rule.matchTokens * 100 - rule.prefix.length;
221
+ if (!best || score > best.score) best = { rule, score };
222
+ }
223
+ }
224
+ return best?.rule.capability;
225
+ }
226
+
227
+ /** Exported for parity tests: the full rule table for one capability. */
228
+ export function bashRulesForCapability(
229
+ capability: AgentCapability,
230
+ ): readonly { prefix: string; capability: AgentCapability }[] {
231
+ return BASH_RULES.filter((r) => r.capability === capability);
232
+ }
233
+
234
+ /** Exported for parity tests: all capabilities with Bash rules. */
235
+ export const BASH_RULE_CAPABILITIES: readonly AgentCapability[] = [
236
+ ...new Set(BASH_RULES.map((r) => r.capability)),
237
+ ];
238
+
239
+ /** Capabilities the claude-code provider declares it can exercise. */
240
+ export const CLAUDE_CODE_SUPPORTED_CAPABILITIES: readonly AgentCapability[] = [
241
+ ...AGENT_CAPABILITIES,
242
+ ];