@komarspn/pi-permission-system 16.0.2

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 (203) hide show
  1. package/CHANGELOG.md +2234 -0
  2. package/LICENSE +21 -0
  3. package/README.md +158 -0
  4. package/config/config.example.json +39 -0
  5. package/package.json +82 -0
  6. package/schemas/permissions.schema.json +158 -0
  7. package/src/active-agent.ts +72 -0
  8. package/src/async-cache.ts +21 -0
  9. package/src/bash-arity.ts +210 -0
  10. package/src/builtin-tool-input-formatters.ts +82 -0
  11. package/src/canonicalize-path.ts +30 -0
  12. package/src/common.ts +121 -0
  13. package/src/config-loader.ts +432 -0
  14. package/src/config-modal.ts +259 -0
  15. package/src/config-paths.ts +47 -0
  16. package/src/config-reporter.ts +34 -0
  17. package/src/config-store.ts +222 -0
  18. package/src/decision-audit.ts +75 -0
  19. package/src/decision-reporter.ts +41 -0
  20. package/src/denial-messages.ts +232 -0
  21. package/src/expand-home.ts +28 -0
  22. package/src/extension-config.ts +79 -0
  23. package/src/extension-paths.ts +66 -0
  24. package/src/forwarded-permissions/io.ts +404 -0
  25. package/src/forwarded-permissions/permission-forwarder.ts +580 -0
  26. package/src/forwarding-manager.ts +74 -0
  27. package/src/gate-prompter.ts +12 -0
  28. package/src/handlers/before-agent-start.ts +94 -0
  29. package/src/handlers/gates/bash-command.ts +75 -0
  30. package/src/handlers/gates/bash-external-directory.ts +127 -0
  31. package/src/handlers/gates/bash-path-extractor.ts +15 -0
  32. package/src/handlers/gates/bash-path.ts +152 -0
  33. package/src/handlers/gates/bash-program.ts +1143 -0
  34. package/src/handlers/gates/bash-token-classification.ts +105 -0
  35. package/src/handlers/gates/candidate-check.ts +32 -0
  36. package/src/handlers/gates/descriptor.ts +81 -0
  37. package/src/handlers/gates/external-directory-messages.ts +20 -0
  38. package/src/handlers/gates/external-directory.ts +133 -0
  39. package/src/handlers/gates/helpers.ts +76 -0
  40. package/src/handlers/gates/path.ts +91 -0
  41. package/src/handlers/gates/runner.ts +186 -0
  42. package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
  43. package/src/handlers/gates/skill-input.ts +46 -0
  44. package/src/handlers/gates/skill-read.ts +87 -0
  45. package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
  46. package/src/handlers/gates/tool.ts +102 -0
  47. package/src/handlers/gates/types.ts +13 -0
  48. package/src/handlers/index.ts +3 -0
  49. package/src/handlers/lifecycle.ts +95 -0
  50. package/src/handlers/permission-gate-handler.ts +190 -0
  51. package/src/handlers/tool-call-boundary.ts +91 -0
  52. package/src/index.ts +225 -0
  53. package/src/input-normalizer.ts +157 -0
  54. package/src/logging.ts +113 -0
  55. package/src/mcp-targets.ts +170 -0
  56. package/src/node-modules-discovery.ts +76 -0
  57. package/src/normalize.ts +43 -0
  58. package/src/path-utils.ts +355 -0
  59. package/src/pattern-suggest.ts +132 -0
  60. package/src/permission-dialog.ts +138 -0
  61. package/src/permission-event-rpc.ts +223 -0
  62. package/src/permission-events.ts +266 -0
  63. package/src/permission-forwarding.ts +188 -0
  64. package/src/permission-gate.ts +94 -0
  65. package/src/permission-manager.ts +392 -0
  66. package/src/permission-merge.ts +32 -0
  67. package/src/permission-prompter.ts +142 -0
  68. package/src/permission-prompts.ts +93 -0
  69. package/src/permission-resolver.ts +109 -0
  70. package/src/permission-session.ts +189 -0
  71. package/src/permission-ui-prompt.ts +127 -0
  72. package/src/permissions-service.ts +63 -0
  73. package/src/persistent-approval-recorder.ts +139 -0
  74. package/src/policy-loader.ts +350 -0
  75. package/src/prompting-gateway.ts +104 -0
  76. package/src/rule.ts +188 -0
  77. package/src/scope-merge.ts +72 -0
  78. package/src/service-lifecycle.ts +49 -0
  79. package/src/service.ts +163 -0
  80. package/src/session-approval-recorder.ts +6 -0
  81. package/src/session-approval.ts +43 -0
  82. package/src/session-logger.ts +91 -0
  83. package/src/session-rules.ts +79 -0
  84. package/src/skill-prompt-sanitizer.ts +292 -0
  85. package/src/status.ts +35 -0
  86. package/src/subagent-context.ts +104 -0
  87. package/src/subagent-lifecycle-events.ts +72 -0
  88. package/src/subagent-registry.ts +105 -0
  89. package/src/synthesize.ts +92 -0
  90. package/src/system-prompt-sanitizer.ts +274 -0
  91. package/src/tool-access-extractor-registry.ts +68 -0
  92. package/src/tool-input-formatter-registry.ts +67 -0
  93. package/src/tool-input-preview.ts +34 -0
  94. package/src/tool-input-prompt-formatters.ts +63 -0
  95. package/src/tool-preview-formatter.ts +207 -0
  96. package/src/tool-registry.ts +148 -0
  97. package/src/types.ts +64 -0
  98. package/src/wildcard-matcher.ts +120 -0
  99. package/src/yolo-mode.ts +30 -0
  100. package/test/active-agent.test.ts +155 -0
  101. package/test/async-cache.test.ts +48 -0
  102. package/test/bash-arity.test.ts +144 -0
  103. package/test/bash-external-directory.test.ts +956 -0
  104. package/test/builtin-tool-input-formatters.test.ts +109 -0
  105. package/test/canonicalize-path.test.ts +93 -0
  106. package/test/common.test.ts +287 -0
  107. package/test/composition-root.test.ts +603 -0
  108. package/test/config-loader.test.ts +740 -0
  109. package/test/config-modal.test.ts +320 -0
  110. package/test/config-paths.test.ts +83 -0
  111. package/test/config-pipeline.test.ts +90 -0
  112. package/test/config-reporter.test.ts +147 -0
  113. package/test/config-store.test.ts +466 -0
  114. package/test/decision-audit.test.ts +72 -0
  115. package/test/decision-reporter.test.ts +112 -0
  116. package/test/denial-messages.test.ts +656 -0
  117. package/test/detect-permissive-bash-fallback.test.ts +56 -0
  118. package/test/expand-home.test.ts +93 -0
  119. package/test/extension-config.test.ts +129 -0
  120. package/test/extension-paths.test.ts +108 -0
  121. package/test/forwarded-permissions/io.test.ts +251 -0
  122. package/test/forwarding-manager.test.ts +194 -0
  123. package/test/handlers/before-agent-start.test.ts +317 -0
  124. package/test/handlers/external-directory-integration.test.ts +623 -0
  125. package/test/handlers/external-directory-session-dedup.test.ts +430 -0
  126. package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
  127. package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
  128. package/test/handlers/gates/bash-command.test.ts +191 -0
  129. package/test/handlers/gates/bash-external-directory.test.ts +269 -0
  130. package/test/handlers/gates/bash-path.test.ts +337 -0
  131. package/test/handlers/gates/bash-program.test.ts +410 -0
  132. package/test/handlers/gates/bash-token-classification.test.ts +241 -0
  133. package/test/handlers/gates/candidate-check.test.ts +52 -0
  134. package/test/handlers/gates/external-directory-messages.test.ts +61 -0
  135. package/test/handlers/gates/external-directory.test.ts +259 -0
  136. package/test/handlers/gates/helpers.test.ts +177 -0
  137. package/test/handlers/gates/path.test.ts +294 -0
  138. package/test/handlers/gates/runner.test.ts +447 -0
  139. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
  140. package/test/handlers/gates/skill-input.test.ts +131 -0
  141. package/test/handlers/gates/skill-read.test.ts +158 -0
  142. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
  143. package/test/handlers/gates/tool.test.ts +223 -0
  144. package/test/handlers/input-events.test.ts +168 -0
  145. package/test/handlers/input.test.ts +199 -0
  146. package/test/handlers/lifecycle.test.ts +221 -0
  147. package/test/handlers/tool-call-boundary.test.ts +145 -0
  148. package/test/handlers/tool-call-events.test.ts +277 -0
  149. package/test/handlers/tool-call.test.ts +395 -0
  150. package/test/handlers/validate-requested-tool.test.ts +92 -0
  151. package/test/helpers/gate-fixtures.ts +323 -0
  152. package/test/helpers/handler-fixtures.ts +335 -0
  153. package/test/helpers/make-fake-pi.ts +100 -0
  154. package/test/helpers/manager-harness.ts +112 -0
  155. package/test/helpers/session-fixtures.ts +204 -0
  156. package/test/input-normalizer.test.ts +367 -0
  157. package/test/logging.test.ts +51 -0
  158. package/test/mcp-targets.test.ts +233 -0
  159. package/test/node-modules-discovery.test.ts +97 -0
  160. package/test/normalize.test.ts +247 -0
  161. package/test/path-utils.test.ts +650 -0
  162. package/test/pattern-suggest.test.ts +248 -0
  163. package/test/permission-dialog.test.ts +241 -0
  164. package/test/permission-event-rpc.test.ts +541 -0
  165. package/test/permission-events.test.ts +402 -0
  166. package/test/permission-forwarder.test.ts +369 -0
  167. package/test/permission-forwarding.test.ts +315 -0
  168. package/test/permission-gate.test.ts +305 -0
  169. package/test/permission-manager-unified.test.ts +3368 -0
  170. package/test/permission-merge.test.ts +61 -0
  171. package/test/permission-prompter.test.ts +518 -0
  172. package/test/permission-prompts.test.ts +363 -0
  173. package/test/permission-resolver.test.ts +265 -0
  174. package/test/permission-session.test.ts +363 -0
  175. package/test/permission-ui-prompt.test.ts +146 -0
  176. package/test/permissions-service.test.ts +177 -0
  177. package/test/persistent-approval-recorder.test.ts +133 -0
  178. package/test/pi-infrastructure-read.test.ts +369 -0
  179. package/test/policy-loader.test.ts +561 -0
  180. package/test/prompting-gateway.test.ts +230 -0
  181. package/test/rule.test.ts +604 -0
  182. package/test/scope-merge.test.ts +116 -0
  183. package/test/service-lifecycle.test.ts +163 -0
  184. package/test/service.test.ts +308 -0
  185. package/test/session-approval.test.ts +75 -0
  186. package/test/session-logger.test.ts +200 -0
  187. package/test/session-rules.test.ts +304 -0
  188. package/test/session-start.test.ts +112 -0
  189. package/test/skill-prompt-sanitizer.test.ts +374 -0
  190. package/test/status.test.ts +10 -0
  191. package/test/subagent-context.test.ts +326 -0
  192. package/test/subagent-lifecycle-events.test.ts +132 -0
  193. package/test/subagent-registry.test.ts +145 -0
  194. package/test/synthesize.test.ts +300 -0
  195. package/test/system-prompt-sanitizer.test.ts +382 -0
  196. package/test/tool-access-extractor-registry.test.ts +77 -0
  197. package/test/tool-input-formatter-registry.test.ts +75 -0
  198. package/test/tool-input-preview.test.ts +129 -0
  199. package/test/tool-input-prompt-formatters.test.ts +115 -0
  200. package/test/tool-preview-formatter.test.ts +458 -0
  201. package/test/tool-registry.test.ts +197 -0
  202. package/test/wildcard-matcher.test.ts +424 -0
  203. package/test/yolo-mode.test.ts +188 -0
@@ -0,0 +1,363 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import {
3
+ formatAskPrompt,
4
+ formatMissingToolNameReason,
5
+ formatSkillAskPrompt,
6
+ formatSkillPathAskPrompt,
7
+ formatUnknownToolReason,
8
+ } from "#src/permission-prompts";
9
+ import type { SkillPromptEntry } from "#src/skill-prompt-sanitizer";
10
+ import type { ToolInputFormatterLookup } from "#src/tool-input-formatter-registry";
11
+ import {
12
+ TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
13
+ TOOL_INPUT_PREVIEW_MAX_LENGTH,
14
+ TOOL_TEXT_SUMMARY_MAX_LENGTH,
15
+ } from "#src/tool-input-preview";
16
+ import { ToolPreviewFormatter } from "#src/tool-preview-formatter";
17
+ import type { PermissionCheckResult } from "#src/types";
18
+
19
+ function makeFormatter(
20
+ lookup?: ToolInputFormatterLookup,
21
+ ): ToolPreviewFormatter {
22
+ return new ToolPreviewFormatter(
23
+ {
24
+ toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
25
+ toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
26
+ toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
27
+ },
28
+ lookup,
29
+ );
30
+ }
31
+
32
+ function makeMcpLookup(preview: string): ToolInputFormatterLookup {
33
+ return { get: (name) => (name === "mcp" ? () => preview : undefined) };
34
+ }
35
+
36
+ function toolResult(
37
+ toolName: string,
38
+ overrides: Partial<PermissionCheckResult> = {},
39
+ ): PermissionCheckResult {
40
+ return {
41
+ toolName,
42
+ state: "ask",
43
+ source: "tool",
44
+ origin: "builtin",
45
+ ...overrides,
46
+ };
47
+ }
48
+
49
+ function mcpResult(
50
+ target: string,
51
+ overrides: Partial<PermissionCheckResult> = {},
52
+ ): PermissionCheckResult {
53
+ return {
54
+ toolName: "mcp",
55
+ target,
56
+ state: "ask",
57
+ source: "tool",
58
+ origin: "builtin",
59
+ ...overrides,
60
+ };
61
+ }
62
+
63
+ function skillEntry(name: string): SkillPromptEntry {
64
+ return {
65
+ name,
66
+ description: "A skill",
67
+ location: `/skills/${name}/SKILL.md`,
68
+ state: "ask",
69
+ normalizedLocation: `/skills/${name}/SKILL.md`,
70
+ normalizedBaseDir: `/skills/${name}`,
71
+ };
72
+ }
73
+
74
+ describe("formatMissingToolNameReason", () => {
75
+ test("mentions missing tool name and pi.getAllTools()", () => {
76
+ const result = formatMissingToolNameReason();
77
+ expect(result).toContain("no tool name");
78
+ expect(result).toContain("pi.getAllTools()");
79
+ });
80
+ });
81
+
82
+ describe("formatUnknownToolReason", () => {
83
+ test("mentions the unknown tool name and lists available tools", () => {
84
+ const result = formatUnknownToolReason("phantom", ["read", "write"]);
85
+ expect(result).toContain("phantom");
86
+ expect(result).toContain("read");
87
+ expect(result).toContain("write");
88
+ });
89
+
90
+ test("includes MCP hint for non-mcp tool names", () => {
91
+ const result = formatUnknownToolReason("my-server:tool", ["mcp"]);
92
+ expect(result).toContain("mcp");
93
+ });
94
+
95
+ test("omits MCP hint when tool name is 'mcp'", () => {
96
+ const result = formatUnknownToolReason("mcp", []);
97
+ expect(result).not.toContain("call the registered 'mcp' tool");
98
+ });
99
+
100
+ test("shows 'none' when no tools are registered", () => {
101
+ const result = formatUnknownToolReason("ghost", []);
102
+ expect(result).toContain("none");
103
+ });
104
+
105
+ test("caps preview at 10 tools and appends ellipsis for longer lists", () => {
106
+ const tools = Array.from({ length: 15 }, (_, i) => `tool${i}`);
107
+ const result = formatUnknownToolReason("ghost", tools);
108
+ expect(result).toContain("...");
109
+ });
110
+ });
111
+
112
+ describe("formatAskPrompt", () => {
113
+ test("uses 'Current agent' when no agent name given", () => {
114
+ const result = formatAskPrompt(
115
+ toolResult("read"),
116
+ undefined,
117
+ { path: "/src" },
118
+ makeFormatter(),
119
+ );
120
+ expect(result).toContain("Current agent");
121
+ });
122
+
123
+ test("uses agent name when provided", () => {
124
+ const result = formatAskPrompt(
125
+ toolResult("read"),
126
+ "my-agent",
127
+ { path: "/src" },
128
+ makeFormatter(),
129
+ );
130
+ expect(result).toContain("Agent 'my-agent'");
131
+ });
132
+
133
+ test("formats bash prompt with command and does not use formatter", () => {
134
+ const result = formatAskPrompt(
135
+ toolResult("bash", { command: "git status" }),
136
+ undefined,
137
+ undefined,
138
+ makeFormatter(),
139
+ );
140
+ expect(result).toContain("git status");
141
+ expect(result).toContain("Allow this command?");
142
+ });
143
+
144
+ test("formats bash prompt with matched pattern", () => {
145
+ const result = formatAskPrompt(
146
+ toolResult("bash", { command: "git push", matchedPattern: "git *" }),
147
+ undefined,
148
+ undefined,
149
+ makeFormatter(),
150
+ );
151
+ expect(result).toContain("matched 'git *'");
152
+ });
153
+
154
+ test("appends full command when input contains a chain that differs from the sub-command", () => {
155
+ const result = formatAskPrompt(
156
+ toolResult("bash", { command: "rm -rf ." }),
157
+ undefined,
158
+ { command: 'echo "hello" && rm -rf .' },
159
+ makeFormatter(),
160
+ );
161
+ expect(result).toBe(
162
+ `Current agent requested bash command 'rm -rf .' (full command: 'echo "hello" && rm -rf .'). Allow this command?`,
163
+ );
164
+ });
165
+
166
+ test("suppresses full-command suffix when input command matches the sub-command (no chain)", () => {
167
+ const result = formatAskPrompt(
168
+ toolResult("bash", { command: "git push" }),
169
+ undefined,
170
+ { command: "git push" },
171
+ makeFormatter(),
172
+ );
173
+ expect(result).not.toContain("full command:");
174
+ expect(result).toBe(
175
+ "Current agent requested bash command 'git push'. Allow this command?",
176
+ );
177
+ });
178
+
179
+ test("suppresses full-command suffix when input is undefined", () => {
180
+ const result = formatAskPrompt(
181
+ toolResult("bash", { command: "git push" }),
182
+ undefined,
183
+ undefined,
184
+ makeFormatter(),
185
+ );
186
+ expect(result).not.toContain("full command:");
187
+ });
188
+
189
+ test("suppresses full-command suffix when input has no command field", () => {
190
+ const result = formatAskPrompt(
191
+ toolResult("bash", { command: "git push" }),
192
+ undefined,
193
+ { unrelated: "value" },
194
+ makeFormatter(),
195
+ );
196
+ expect(result).not.toContain("full command:");
197
+ });
198
+
199
+ test("suppresses full-command suffix when input command is empty", () => {
200
+ const result = formatAskPrompt(
201
+ toolResult("bash", { command: "git push" }),
202
+ undefined,
203
+ { command: "" },
204
+ makeFormatter(),
205
+ );
206
+ expect(result).not.toContain("full command:");
207
+ });
208
+
209
+ test("places full-command suffix after the qualifier and before the terminal sentence", () => {
210
+ const result = formatAskPrompt(
211
+ toolResult("bash", { command: "rm -rf foo", matchedPattern: "rm *" }),
212
+ undefined,
213
+ { command: "cd /tmp && rm -rf foo" },
214
+ makeFormatter(),
215
+ );
216
+ expect(result).toBe(
217
+ "Current agent requested bash command 'rm -rf foo' (matched 'rm *') (full command: 'cd /tmp && rm -rf foo'). Allow this command?",
218
+ );
219
+ });
220
+
221
+ test("formats bash prompt with nested execution context", () => {
222
+ const result = formatAskPrompt(
223
+ toolResult("bash", {
224
+ command: "rm -rf foo",
225
+ matchedPattern: "rm *",
226
+ commandContext: "command_substitution",
227
+ }),
228
+ undefined,
229
+ undefined,
230
+ makeFormatter(),
231
+ );
232
+ expect(result).toContain(
233
+ "bash command 'rm -rf foo' (matched 'rm *', inside command substitution).",
234
+ );
235
+ });
236
+
237
+ test("formats MCP prompt with target", () => {
238
+ const result = formatAskPrompt(
239
+ mcpResult("server:query"),
240
+ undefined,
241
+ undefined,
242
+ makeFormatter(),
243
+ );
244
+ expect(result).toContain("server:query");
245
+ expect(result).toContain("Allow this call?");
246
+ });
247
+
248
+ test("formats MCP prompt with matched pattern", () => {
249
+ const result = formatAskPrompt(
250
+ mcpResult("server:query", { matchedPattern: "server:*" }),
251
+ undefined,
252
+ undefined,
253
+ makeFormatter(),
254
+ );
255
+ expect(result).toContain("matched 'server:*'");
256
+ });
257
+
258
+ test("appends MCP argument summary when the formatter has an mcp formatter registered", () => {
259
+ const result = formatAskPrompt(
260
+ mcpResult("exa:search"),
261
+ undefined,
262
+ { tool: "exa:search", arguments: { query: "typescript" } },
263
+ makeFormatter(makeMcpLookup('with query: "typescript"')),
264
+ );
265
+ expect(result).toContain("exa:search");
266
+ expect(result).toContain('with query: "typescript"');
267
+ expect(result).toContain("Allow this call?");
268
+ });
269
+
270
+ test("MCP prompt is unchanged when the formatter returns undefined (no arguments)", () => {
271
+ const noArgsLookup: ToolInputFormatterLookup = {
272
+ get: (name) => (name === "mcp" ? () => undefined : undefined),
273
+ };
274
+ const result = formatAskPrompt(
275
+ mcpResult("exa:search"),
276
+ undefined,
277
+ { tool: "exa:search" },
278
+ makeFormatter(noArgsLookup),
279
+ );
280
+ expect(result).toContain("exa:search");
281
+ expect(result).not.toMatch(/with /);
282
+ expect(result).toContain("Allow this call?");
283
+ });
284
+
285
+ test("MCP prompt is unchanged when no formatter is provided", () => {
286
+ const result = formatAskPrompt(mcpResult("exa:search"), undefined, {
287
+ tool: "exa:search",
288
+ arguments: { query: "test" },
289
+ });
290
+ expect(result).toContain("exa:search");
291
+ expect(result).not.toMatch(/with /);
292
+ expect(result).toContain("Allow this call?");
293
+ });
294
+
295
+ test("includes real input preview for non-bash non-mcp tools", () => {
296
+ const result = formatAskPrompt(
297
+ toolResult("read"),
298
+ undefined,
299
+ { path: "/src/foo.ts" },
300
+ makeFormatter(),
301
+ );
302
+ expect(result).toContain("path '/src/foo.ts'");
303
+ expect(result).toContain("Allow this call?");
304
+ });
305
+
306
+ test("omits input suffix when formatter returns empty string for input", () => {
307
+ const result = formatAskPrompt(
308
+ toolResult("task"),
309
+ undefined,
310
+ {},
311
+ makeFormatter(),
312
+ );
313
+ expect(result).toContain("task");
314
+ expect(result).not.toContain("undefined");
315
+ });
316
+
317
+ test("omits input suffix when no formatter provided", () => {
318
+ const result = formatAskPrompt(toolResult("task"), undefined, {
319
+ path: "/src",
320
+ });
321
+ expect(result).toContain("task");
322
+ expect(result).not.toContain("undefined");
323
+ expect(result).toContain("Allow this call?");
324
+ });
325
+ });
326
+
327
+ describe("formatSkillAskPrompt", () => {
328
+ test("includes skill name and agent name", () => {
329
+ const result = formatSkillAskPrompt("librarian", "my-agent");
330
+ expect(result).toContain("librarian");
331
+ expect(result).toContain("Agent 'my-agent'");
332
+ });
333
+
334
+ test("uses 'Current agent' without agent name", () => {
335
+ const result = formatSkillAskPrompt("librarian");
336
+ expect(result).toContain("Current agent");
337
+ expect(result).toContain("librarian");
338
+ });
339
+ });
340
+
341
+ describe("formatSkillPathAskPrompt", () => {
342
+ test("includes skill name, read path, and agent name", () => {
343
+ const result = formatSkillPathAskPrompt(
344
+ skillEntry("librarian"),
345
+ "/skills/librarian/SKILL.md",
346
+ "my-agent",
347
+ );
348
+ expect(result).toContain("librarian");
349
+ expect(result).toContain("/skills/librarian/SKILL.md");
350
+ expect(result).toContain("Agent 'my-agent'");
351
+ });
352
+
353
+ test("uses 'Current agent' without agent name", () => {
354
+ const result = formatSkillPathAskPrompt(
355
+ skillEntry("librarian"),
356
+ "/skills/librarian/SKILL.md",
357
+ );
358
+ expect(result).toContain("Current agent");
359
+ });
360
+ });
361
+
362
+ // formatSkillPathDenyReason has moved to denial-messages.ts.
363
+ // Its behavior is tested in denial-messages.test.ts.
@@ -0,0 +1,265 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import type { ScopedPermissionManager } from "#src/permission-manager";
3
+ import { PermissionResolver } from "#src/permission-resolver";
4
+ import type { Ruleset } from "#src/rule";
5
+ import { SessionApproval } from "#src/session-approval";
6
+ import { SessionRules } from "#src/session-rules";
7
+ import type { PermissionCheckResult, PermissionState } from "#src/types";
8
+
9
+ function makePermissionManager() {
10
+ return {
11
+ configureForCwd: vi.fn<(cwd: string | undefined | null) => void>(),
12
+ checkPermission: vi
13
+ .fn<
14
+ (
15
+ toolName: string,
16
+ input: unknown,
17
+ agentName?: string,
18
+ sessionRules?: Ruleset,
19
+ ) => PermissionCheckResult
20
+ >()
21
+ .mockReturnValue({
22
+ state: "allow",
23
+ toolName: "read",
24
+ source: "tool",
25
+ origin: "builtin",
26
+ }),
27
+ checkPathPolicy: vi
28
+ .fn<
29
+ (
30
+ values: readonly string[],
31
+ agentName?: string,
32
+ sessionRules?: Ruleset,
33
+ ) => PermissionCheckResult
34
+ >()
35
+ .mockReturnValue({
36
+ state: "allow",
37
+ toolName: "path",
38
+ source: "special",
39
+ origin: "builtin",
40
+ }),
41
+ getToolPermission: vi
42
+ .fn<(toolName: string, agentName?: string) => PermissionState>()
43
+ .mockReturnValue("allow"),
44
+ getConfigIssues: vi.fn((): string[] => []),
45
+ };
46
+ }
47
+
48
+ function makeResolver(
49
+ pm?: ScopedPermissionManager,
50
+ sessionRules?: Pick<SessionRules, "getRuleset">,
51
+ ) {
52
+ const permissionManager = pm ?? makePermissionManager();
53
+ const rules = sessionRules ?? new SessionRules();
54
+ return {
55
+ resolver: new PermissionResolver(permissionManager, rules),
56
+ permissionManager,
57
+ };
58
+ }
59
+
60
+ beforeEach(() => {
61
+ // no module-level vi.fn() stubs to reset
62
+ });
63
+
64
+ describe("PermissionResolver", () => {
65
+ describe("resolve", () => {
66
+ it("forwards surface, input, and agentName, applying the empty session ruleset", () => {
67
+ const { resolver, permissionManager } = makeResolver();
68
+
69
+ resolver.resolve("bash", { command: "ls" }, "agent-x");
70
+
71
+ expect(permissionManager.checkPermission).toHaveBeenCalledWith(
72
+ "bash",
73
+ { command: "ls" },
74
+ "agent-x",
75
+ [],
76
+ );
77
+ });
78
+
79
+ it("defaults agentName to undefined when omitted", () => {
80
+ const { resolver, permissionManager } = makeResolver();
81
+
82
+ resolver.resolve("read", { path: ".env" });
83
+
84
+ expect(permissionManager.checkPermission).toHaveBeenCalledWith(
85
+ "read",
86
+ { path: ".env" },
87
+ undefined,
88
+ [],
89
+ );
90
+ });
91
+
92
+ it("applies a recorded session approval on the next resolve", () => {
93
+ const pm = makePermissionManager();
94
+ const sessionRules = new SessionRules();
95
+ const { resolver } = makeResolver(pm, sessionRules);
96
+
97
+ // Record an approval directly into the shared SessionRules instance.
98
+ sessionRules.recordSessionApproval(
99
+ SessionApproval.single("bash", "git *"),
100
+ );
101
+ resolver.resolve("bash", { command: "git status" });
102
+
103
+ const passedRules = vi.mocked(pm.checkPermission).mock.calls[0][3];
104
+ expect(passedRules).toHaveLength(1);
105
+ expect(passedRules?.[0]).toMatchObject({
106
+ surface: "bash",
107
+ pattern: "git *",
108
+ action: "allow",
109
+ });
110
+ });
111
+
112
+ it("returns the PermissionManager's check result", () => {
113
+ const pm = makePermissionManager();
114
+ vi.mocked(pm.checkPermission).mockReturnValue({
115
+ state: "deny",
116
+ toolName: "bash",
117
+ source: "bash",
118
+ origin: "global",
119
+ matchedPattern: "rm *",
120
+ });
121
+ const { resolver } = makeResolver(pm);
122
+
123
+ const result = resolver.resolve("bash", { command: "rm -rf /" });
124
+
125
+ expect(result).toEqual({
126
+ state: "deny",
127
+ toolName: "bash",
128
+ source: "bash",
129
+ origin: "global",
130
+ matchedPattern: "rm *",
131
+ });
132
+ });
133
+ });
134
+
135
+ describe("resolvePathPolicy", () => {
136
+ it("forwards values and agentName with the current session ruleset", () => {
137
+ const { resolver, permissionManager } = makeResolver();
138
+
139
+ resolver.resolvePathPolicy(["/proj/src/a.ts", "src/a.ts"], "agent-x");
140
+
141
+ expect(permissionManager.checkPathPolicy).toHaveBeenCalledWith(
142
+ ["/proj/src/a.ts", "src/a.ts"],
143
+ "agent-x",
144
+ [],
145
+ "path",
146
+ );
147
+ });
148
+
149
+ it("forwards an explicit surface to checkPathPolicy", () => {
150
+ const { resolver, permissionManager } = makeResolver();
151
+
152
+ resolver.resolvePathPolicy(["/tmp/x"], "agent-x", "external_directory");
153
+
154
+ expect(permissionManager.checkPathPolicy).toHaveBeenCalledWith(
155
+ ["/tmp/x"],
156
+ "agent-x",
157
+ [],
158
+ "external_directory",
159
+ );
160
+ });
161
+
162
+ it("applies a recorded session approval on the next call", () => {
163
+ const pm = makePermissionManager();
164
+ const sessionRules = new SessionRules();
165
+ const { resolver } = makeResolver(pm, sessionRules);
166
+
167
+ sessionRules.recordSessionApproval(
168
+ SessionApproval.single("path", "src/*"),
169
+ );
170
+ resolver.resolvePathPolicy(["src/a.ts"]);
171
+
172
+ const passedRules = vi.mocked(pm.checkPathPolicy).mock.calls[0][2];
173
+ expect(passedRules).toHaveLength(1);
174
+ expect(passedRules?.[0]).toMatchObject({
175
+ surface: "path",
176
+ pattern: "src/*",
177
+ action: "allow",
178
+ });
179
+ });
180
+
181
+ it("returns the PermissionManager's check result", () => {
182
+ const pm = makePermissionManager();
183
+ vi.mocked(pm.checkPathPolicy).mockReturnValue({
184
+ state: "deny",
185
+ toolName: "path",
186
+ source: "special",
187
+ origin: "global",
188
+ matchedPattern: "src/*",
189
+ });
190
+ const { resolver } = makeResolver(pm);
191
+
192
+ const result = resolver.resolvePathPolicy(["src/a.ts"]);
193
+
194
+ expect(result).toEqual({
195
+ state: "deny",
196
+ toolName: "path",
197
+ source: "special",
198
+ origin: "global",
199
+ matchedPattern: "src/*",
200
+ });
201
+ });
202
+ });
203
+
204
+ describe("checkPermission", () => {
205
+ it("delegates to permissionManager.checkPermission with the given args", () => {
206
+ const { resolver, permissionManager } = makeResolver();
207
+
208
+ resolver.checkPermission("bash", { command: "ls" }, "agent-1");
209
+
210
+ expect(permissionManager.checkPermission).toHaveBeenCalledWith(
211
+ "bash",
212
+ { command: "ls" },
213
+ "agent-1",
214
+ undefined,
215
+ );
216
+ });
217
+
218
+ it("passes optional sessionRules through when supplied", () => {
219
+ const { resolver, permissionManager } = makeResolver();
220
+ const extraRules: Ruleset = [
221
+ { surface: "bash", pattern: "*", action: "allow", origin: "session" },
222
+ ];
223
+
224
+ resolver.checkPermission(
225
+ "bash",
226
+ { command: "ls" },
227
+ undefined,
228
+ extraRules,
229
+ );
230
+
231
+ expect(permissionManager.checkPermission).toHaveBeenCalledWith(
232
+ "bash",
233
+ { command: "ls" },
234
+ undefined,
235
+ extraRules,
236
+ );
237
+ });
238
+ });
239
+
240
+ describe("getToolPermission", () => {
241
+ it("delegates to permissionManager.getToolPermission", () => {
242
+ const pm = makePermissionManager();
243
+ vi.mocked(pm.getToolPermission).mockReturnValue("deny");
244
+ const { resolver } = makeResolver(pm);
245
+
246
+ const result = resolver.getToolPermission("write", "my-agent");
247
+
248
+ expect(pm.getToolPermission).toHaveBeenCalledWith("write", "my-agent");
249
+ expect(result).toBe("deny");
250
+ });
251
+ });
252
+
253
+ describe("getConfigIssues", () => {
254
+ it("delegates to permissionManager.getConfigIssues", () => {
255
+ const pm = makePermissionManager();
256
+ vi.mocked(pm.getConfigIssues).mockReturnValue(["issue-1"]);
257
+ const { resolver } = makeResolver(pm);
258
+
259
+ const result = resolver.getConfigIssues("agent-1");
260
+
261
+ expect(pm.getConfigIssues).toHaveBeenCalledWith("agent-1");
262
+ expect(result).toEqual(["issue-1"]);
263
+ });
264
+ });
265
+ });