@gotgenes/pi-permission-system 18.1.1 → 18.2.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 (133) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/docs/configuration.md +11 -0
  3. package/package.json +1 -2
  4. package/src/access-intent/access-path.ts +29 -4
  5. package/src/access-intent/bash/bash-path-resolver.ts +39 -18
  6. package/src/access-intent/bash/msys-bash-tokens.ts +64 -0
  7. package/src/extension-config.ts +7 -0
  8. package/src/forwarded-permissions/permission-forwarder.ts +6 -3
  9. package/src/handlers/gates/helpers.ts +1 -1
  10. package/src/handlers/gates/runner.ts +22 -0
  11. package/src/index.ts +10 -6
  12. package/src/path-normalizer.ts +105 -2
  13. package/src/permission-manager.ts +21 -1
  14. package/src/permission-prompter.ts +10 -16
  15. package/src/prompting-gateway.ts +13 -17
  16. package/src/rule.ts +21 -1
  17. package/src/status.ts +1 -1
  18. package/src/yolo-mode.ts +0 -30
  19. package/test/access-intent/access-path.test.ts +0 -277
  20. package/test/access-intent/bash/node-text.test.ts +0 -148
  21. package/test/access-intent/bash/parser.test.ts +0 -19
  22. package/test/access-intent/bash/program.test.ts +0 -673
  23. package/test/access-intent/bash/token-classification.test.ts +0 -363
  24. package/test/access-intent/bash/token-collection.test.ts +0 -300
  25. package/test/active-agent.test.ts +0 -155
  26. package/test/async-cache.test.ts +0 -48
  27. package/test/bash-arity.test.ts +0 -144
  28. package/test/bash-external-directory.test.ts +0 -1022
  29. package/test/builtin-tool-input-formatters.test.ts +0 -109
  30. package/test/canonicalize-path.test.ts +0 -119
  31. package/test/composition-root.test.ts +0 -698
  32. package/test/config-loader.test.ts +0 -740
  33. package/test/config-modal.test.ts +0 -320
  34. package/test/config-paths.test.ts +0 -83
  35. package/test/config-pipeline.test.ts +0 -90
  36. package/test/config-reporter.test.ts +0 -147
  37. package/test/config-store.test.ts +0 -466
  38. package/test/decision-audit.test.ts +0 -72
  39. package/test/decision-reporter.test.ts +0 -112
  40. package/test/denial-messages.test.ts +0 -714
  41. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  42. package/test/expand-home.test.ts +0 -93
  43. package/test/extension-config.test.ts +0 -129
  44. package/test/extension-paths.test.ts +0 -108
  45. package/test/forwarded-permissions/io.test.ts +0 -251
  46. package/test/forwarding-manager.test.ts +0 -200
  47. package/test/handlers/before-agent-start.test.ts +0 -314
  48. package/test/handlers/external-directory-integration.test.ts +0 -515
  49. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  50. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  51. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  52. package/test/handlers/gates/bash-command.test.ts +0 -257
  53. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  54. package/test/handlers/gates/bash-path.test.ts +0 -346
  55. package/test/handlers/gates/candidate-check.test.ts +0 -52
  56. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  57. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  58. package/test/handlers/gates/external-directory.test.ts +0 -267
  59. package/test/handlers/gates/helpers.test.ts +0 -165
  60. package/test/handlers/gates/path.test.ts +0 -369
  61. package/test/handlers/gates/runner.test.ts +0 -408
  62. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  63. package/test/handlers/gates/skill-input.test.ts +0 -128
  64. package/test/handlers/gates/skill-read.test.ts +0 -161
  65. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  66. package/test/handlers/gates/tool.test.ts +0 -244
  67. package/test/handlers/input-events.test.ts +0 -168
  68. package/test/handlers/input.test.ts +0 -199
  69. package/test/handlers/lifecycle.test.ts +0 -221
  70. package/test/handlers/tool-call-boundary.test.ts +0 -145
  71. package/test/handlers/tool-call-events.test.ts +0 -277
  72. package/test/handlers/tool-call.test.ts +0 -395
  73. package/test/handlers/validate-requested-tool.test.ts +0 -92
  74. package/test/helpers/external-directory-fixtures.ts +0 -269
  75. package/test/helpers/gate-fixtures.ts +0 -316
  76. package/test/helpers/handler-fixtures.ts +0 -335
  77. package/test/helpers/make-fake-pi.ts +0 -100
  78. package/test/helpers/manager-harness.ts +0 -112
  79. package/test/helpers/session-fixtures.ts +0 -199
  80. package/test/input-normalizer.test.ts +0 -325
  81. package/test/logging.test.ts +0 -51
  82. package/test/mcp-targets.test.ts +0 -233
  83. package/test/node-modules-discovery.test.ts +0 -97
  84. package/test/normalize.test.ts +0 -247
  85. package/test/path-containment.test.ts +0 -161
  86. package/test/path-normalization.test.ts +0 -233
  87. package/test/path-normalizer.test.ts +0 -196
  88. package/test/path-surfaces.test.ts +0 -55
  89. package/test/pattern-suggest.test.ts +0 -248
  90. package/test/permission-dialog.test.ts +0 -205
  91. package/test/permission-event-rpc.test.ts +0 -560
  92. package/test/permission-events.test.ts +0 -400
  93. package/test/permission-forwarder.test.ts +0 -370
  94. package/test/permission-forwarding.test.ts +0 -315
  95. package/test/permission-gate.test.ts +0 -269
  96. package/test/permission-manager-unified.test.ts +0 -3714
  97. package/test/permission-merge.test.ts +0 -61
  98. package/test/permission-prompter.test.ts +0 -518
  99. package/test/permission-prompts.test.ts +0 -363
  100. package/test/permission-resolver.test.ts +0 -277
  101. package/test/permission-session.test.ts +0 -404
  102. package/test/permission-ui-prompt.test.ts +0 -146
  103. package/test/permissions-service.test.ts +0 -192
  104. package/test/pi-infrastructure-read.test.ts +0 -432
  105. package/test/policy-loader.test.ts +0 -561
  106. package/test/prompting-gateway.test.ts +0 -231
  107. package/test/rule.test.ts +0 -650
  108. package/test/safe-system-paths.test.ts +0 -46
  109. package/test/scope-merge.test.ts +0 -116
  110. package/test/service-lifecycle.test.ts +0 -163
  111. package/test/service.test.ts +0 -261
  112. package/test/session-approval.test.ts +0 -75
  113. package/test/session-logger.test.ts +0 -200
  114. package/test/session-rules.test.ts +0 -321
  115. package/test/session-start.test.ts +0 -112
  116. package/test/skill-prompt-sanitizer.test.ts +0 -418
  117. package/test/status.test.ts +0 -10
  118. package/test/subagent-context.test.ts +0 -372
  119. package/test/subagent-lifecycle-events.test.ts +0 -132
  120. package/test/subagent-registry.test.ts +0 -145
  121. package/test/synthesize.test.ts +0 -302
  122. package/test/system-prompt-sanitizer.test.ts +0 -382
  123. package/test/tool-access-extractor-registry.test.ts +0 -77
  124. package/test/tool-input-formatter-registry.test.ts +0 -75
  125. package/test/tool-input-path.test.ts +0 -84
  126. package/test/tool-input-preview.test.ts +0 -129
  127. package/test/tool-input-prompt-formatters.test.ts +0 -115
  128. package/test/tool-preview-formatter.test.ts +0 -458
  129. package/test/tool-registry.test.ts +0 -197
  130. package/test/value-guards.test.ts +0 -193
  131. package/test/wildcard-matcher.test.ts +0 -424
  132. package/test/yaml-frontmatter.test.ts +0 -91
  133. package/test/yolo-mode.test.ts +0 -188
@@ -1,714 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
-
3
- import {
4
- type DenialContext,
5
- EXTENSION_TAG,
6
- formatDenyReason,
7
- formatUnavailableReason,
8
- formatUserDeniedReason,
9
- } from "#src/denial-messages";
10
- import type { PermissionCheckResult } from "#src/types";
11
-
12
- // ── Helpers ────────────────────────────────────────────────────────────────
13
-
14
- function toolCheck(
15
- toolName: string,
16
- overrides: Partial<PermissionCheckResult> = {},
17
- ): PermissionCheckResult {
18
- return {
19
- toolName,
20
- state: "deny",
21
- source: "tool",
22
- origin: "builtin",
23
- ...overrides,
24
- };
25
- }
26
-
27
- function mcpCheck(
28
- target: string,
29
- overrides: Partial<PermissionCheckResult> = {},
30
- ): PermissionCheckResult {
31
- return {
32
- toolName: "mcp",
33
- target,
34
- state: "deny",
35
- source: "mcp",
36
- origin: "builtin",
37
- ...overrides,
38
- };
39
- }
40
-
41
- function toolCtx(
42
- check: PermissionCheckResult,
43
- agentName?: string,
44
- ): Extract<DenialContext, { kind: "tool" }> {
45
- return { kind: "tool", check, agentName };
46
- }
47
-
48
- // ── EXTENSION_TAG ──────────────────────────────────────────────────────────
49
-
50
- describe("EXTENSION_TAG", () => {
51
- test("is [pi-permission-system]", () => {
52
- expect(EXTENSION_TAG).toBe("[pi-permission-system]");
53
- });
54
- });
55
-
56
- // ── formatDenyReason ───────────────────────────────────────────────────────
57
-
58
- describe("formatDenyReason", () => {
59
- describe("tool context", () => {
60
- test("generic tool without agent", () => {
61
- expect(formatDenyReason(toolCtx(toolCheck("write")))).toBe(
62
- "[pi-permission-system] is not permitted to run 'write'.",
63
- );
64
- });
65
-
66
- test("generic tool with agent", () => {
67
- expect(formatDenyReason(toolCtx(toolCheck("write"), "my-agent"))).toBe(
68
- "[pi-permission-system] Agent 'my-agent' is not permitted to run 'write'.",
69
- );
70
- });
71
-
72
- test("MCP target", () => {
73
- expect(formatDenyReason(toolCtx(mcpCheck("server:do-thing")))).toBe(
74
- "[pi-permission-system] is not permitted to run MCP target 'server:do-thing'.",
75
- );
76
- });
77
-
78
- test("bash with command", () => {
79
- expect(
80
- formatDenyReason(toolCtx(toolCheck("bash", { command: "rm -rf /" }))),
81
- ).toBe(
82
- "[pi-permission-system] is not permitted to run 'bash' command 'rm -rf /'.",
83
- );
84
- });
85
-
86
- test("bash with command and matched pattern", () => {
87
- expect(
88
- formatDenyReason(
89
- toolCtx(
90
- toolCheck("bash", {
91
- command: "rm -rf /",
92
- matchedPattern: "rm *",
93
- }),
94
- ),
95
- ),
96
- ).toBe(
97
- "[pi-permission-system] is not permitted to run 'bash' command 'rm -rf /' (matched 'rm *').",
98
- );
99
- });
100
-
101
- test("bash with nested execution context", () => {
102
- expect(
103
- formatDenyReason(
104
- toolCtx(
105
- toolCheck("bash", {
106
- command: "rm -rf foo",
107
- matchedPattern: "rm *",
108
- commandContext: "command_substitution",
109
- }),
110
- ),
111
- ),
112
- ).toBe(
113
- "[pi-permission-system] is not permitted to run 'bash' command 'rm -rf foo' (matched 'rm *', inside command substitution).",
114
- );
115
- });
116
-
117
- test("bash with a custom reason appended after the period", () => {
118
- expect(
119
- formatDenyReason(
120
- toolCtx(
121
- toolCheck("bash", {
122
- command: "npm install",
123
- matchedPattern: "npm *",
124
- reason: "Use pnpm instead",
125
- }),
126
- ),
127
- ),
128
- ).toBe(
129
- "[pi-permission-system] is not permitted to run 'bash' command 'npm install' (matched 'npm *'). Reason: Use pnpm instead.",
130
- );
131
- });
132
-
133
- test("custom reason with no matched pattern", () => {
134
- expect(
135
- formatDenyReason(
136
- toolCtx(
137
- toolCheck("write", {
138
- reason: "Write access is disabled for security",
139
- }),
140
- ),
141
- ),
142
- ).toBe(
143
- "[pi-permission-system] is not permitted to run 'write'. Reason: Write access is disabled for security.",
144
- );
145
- });
146
-
147
- test("custom reason is included alongside the agent name", () => {
148
- expect(
149
- formatDenyReason(
150
- toolCtx(
151
- toolCheck("bash", {
152
- command: "yarn build",
153
- matchedPattern: "yarn *",
154
- reason: "Use pnpm instead",
155
- }),
156
- "dev-agent",
157
- ),
158
- ),
159
- ).toBe(
160
- "[pi-permission-system] Agent 'dev-agent' is not permitted to run 'bash' command 'yarn build' (matched 'yarn *'). Reason: Use pnpm instead.",
161
- );
162
- });
163
-
164
- test("custom reason on an MCP target", () => {
165
- expect(
166
- formatDenyReason(
167
- toolCtx(
168
- mcpCheck("server:deploy", {
169
- reason: "Deploy requires approval from a senior engineer",
170
- }),
171
- ),
172
- ),
173
- ).toBe(
174
- "[pi-permission-system] is not permitted to run MCP target 'server:deploy'. Reason: Deploy requires approval from a senior engineer.",
175
- );
176
- });
177
-
178
- test("MCP source with target on non-mcp toolName", () => {
179
- expect(
180
- formatDenyReason(
181
- toolCtx(
182
- toolCheck("anything", { source: "mcp", target: "server:tool" }),
183
- ),
184
- ),
185
- ).toBe(
186
- "[pi-permission-system] is not permitted to run MCP target 'server:tool'.",
187
- );
188
- });
189
- });
190
-
191
- describe("path context", () => {
192
- test("without agent", () => {
193
- expect(
194
- formatDenyReason({
195
- kind: "path",
196
- toolName: "read",
197
- pathValue: "/etc/passwd",
198
- }),
199
- ).toBe(
200
- "[pi-permission-system] Current agent is not permitted to access path '/etc/passwd' via tool 'read'.",
201
- );
202
- });
203
-
204
- test("with agent", () => {
205
- expect(
206
- formatDenyReason({
207
- kind: "path",
208
- toolName: "read",
209
- pathValue: "/etc/passwd",
210
- agentName: "sec-agent",
211
- }),
212
- ).toBe(
213
- "[pi-permission-system] Agent 'sec-agent' is not permitted to access path '/etc/passwd' via tool 'read'.",
214
- );
215
- });
216
- });
217
-
218
- describe("external_directory context", () => {
219
- test("without agent", () => {
220
- expect(
221
- formatDenyReason({
222
- kind: "external_directory",
223
- toolName: "read",
224
- pathValue: "/etc/passwd",
225
- cwd: "/project",
226
- }),
227
- ).toBe(
228
- "[pi-permission-system] Current agent is not permitted to run tool 'read' for path '/etc/passwd' outside working directory '/project'.",
229
- );
230
- });
231
-
232
- test("with agent", () => {
233
- expect(
234
- formatDenyReason({
235
- kind: "external_directory",
236
- toolName: "read",
237
- pathValue: "/etc/passwd",
238
- cwd: "/project",
239
- agentName: "sec-agent",
240
- }),
241
- ).toBe(
242
- "[pi-permission-system] Agent 'sec-agent' is not permitted to run tool 'read' for path '/etc/passwd' outside working directory '/project'.",
243
- );
244
- });
245
-
246
- test("discloses the resolved path when it differs from the typed path", () => {
247
- expect(
248
- formatDenyReason({
249
- kind: "external_directory",
250
- toolName: "read",
251
- pathValue: "demo-symlink-passwd",
252
- resolvedPath: "/etc/passwd",
253
- cwd: "/project",
254
- }),
255
- ).toBe(
256
- "[pi-permission-system] Current agent is not permitted to run tool 'read' for path 'demo-symlink-passwd' (resolves to '/etc/passwd') outside working directory '/project'.",
257
- );
258
- });
259
- });
260
-
261
- describe("bash_external_directory context", () => {
262
- test("single path without agent", () => {
263
- expect(
264
- formatDenyReason({
265
- kind: "bash_external_directory",
266
- command: "cat /etc/hosts",
267
- externalPaths: [{ path: "/etc/hosts" }],
268
- cwd: "/project",
269
- }),
270
- ).toBe(
271
- "[pi-permission-system] Current agent is not permitted to run bash command 'cat /etc/hosts' which references path(s) outside working directory '/project': /etc/hosts.",
272
- );
273
- });
274
-
275
- test("multiple paths with agent", () => {
276
- expect(
277
- formatDenyReason({
278
- kind: "bash_external_directory",
279
- command: "cp /etc/hosts /tmp/out",
280
- externalPaths: [{ path: "/etc/hosts" }, { path: "/tmp/out" }],
281
- cwd: "/project",
282
- agentName: "my-agent",
283
- }),
284
- ).toBe(
285
- "[pi-permission-system] Agent 'my-agent' is not permitted to run bash command 'cp /etc/hosts /tmp/out' which references path(s) outside working directory '/project': /etc/hosts, /tmp/out.",
286
- );
287
- });
288
-
289
- test("discloses resolved targets per path when they differ", () => {
290
- expect(
291
- formatDenyReason({
292
- kind: "bash_external_directory",
293
- command: "cat demo-symlink-passwd /etc/hosts",
294
- externalPaths: [
295
- { path: "demo-symlink-passwd", resolvedPath: "/etc/passwd" },
296
- { path: "/etc/hosts" },
297
- ],
298
- cwd: "/project",
299
- }),
300
- ).toBe(
301
- "[pi-permission-system] Current agent is not permitted to run bash command 'cat demo-symlink-passwd /etc/hosts' which references path(s) outside working directory '/project': demo-symlink-passwd (resolves to '/etc/passwd'), /etc/hosts.",
302
- );
303
- });
304
- });
305
-
306
- describe("bash_path context", () => {
307
- test("without agent", () => {
308
- expect(
309
- formatDenyReason({
310
- kind: "bash_path",
311
- command: "cat /etc/passwd",
312
- pathValue: "/etc/passwd",
313
- }),
314
- ).toBe(
315
- "[pi-permission-system] Current agent is not permitted to access path '/etc/passwd' via tool 'bash'.",
316
- );
317
- });
318
-
319
- test("with agent", () => {
320
- expect(
321
- formatDenyReason({
322
- kind: "bash_path",
323
- command: "cat /etc/passwd",
324
- pathValue: "/etc/passwd",
325
- agentName: "my-agent",
326
- }),
327
- ).toBe(
328
- "[pi-permission-system] Agent 'my-agent' is not permitted to access path '/etc/passwd' via tool 'bash'.",
329
- );
330
- });
331
- });
332
-
333
- describe("skill_read context", () => {
334
- test("without agent", () => {
335
- expect(
336
- formatDenyReason({
337
- kind: "skill_read",
338
- skillName: "librarian",
339
- readPath: "/skills/librarian/SKILL.md",
340
- }),
341
- ).toBe(
342
- "[pi-permission-system] Current agent is not permitted to access skill 'librarian' via '/skills/librarian/SKILL.md'.",
343
- );
344
- });
345
-
346
- test("with agent", () => {
347
- expect(
348
- formatDenyReason({
349
- kind: "skill_read",
350
- skillName: "librarian",
351
- readPath: "/skills/librarian/SKILL.md",
352
- agentName: "my-agent",
353
- }),
354
- ).toBe(
355
- "[pi-permission-system] Agent 'my-agent' is not permitted to access skill 'librarian' via '/skills/librarian/SKILL.md'.",
356
- );
357
- });
358
- });
359
-
360
- describe("skill_input context", () => {
361
- test("without agent", () => {
362
- expect(
363
- formatDenyReason({
364
- kind: "skill_input",
365
- skillName: "librarian",
366
- }),
367
- ).toBe(
368
- "[pi-permission-system] Current agent is not permitted to access skill 'librarian'.",
369
- );
370
- });
371
-
372
- test("with agent", () => {
373
- expect(
374
- formatDenyReason({
375
- kind: "skill_input",
376
- skillName: "librarian",
377
- agentName: "my-agent",
378
- }),
379
- ).toBe(
380
- "[pi-permission-system] Agent 'my-agent' is not permitted to access skill 'librarian'.",
381
- );
382
- });
383
- });
384
- });
385
-
386
- // ── formatUnavailableReason ────────────────────────────────────────────────
387
-
388
- describe("formatUnavailableReason", () => {
389
- test("generic tool", () => {
390
- expect(formatUnavailableReason(toolCtx(toolCheck("write")))).toBe(
391
- "[pi-permission-system] Using tool 'write' requires approval, but no interactive UI is available.",
392
- );
393
- });
394
-
395
- test("bash with command", () => {
396
- expect(
397
- formatUnavailableReason(
398
- toolCtx(toolCheck("bash", { command: "git push" })),
399
- ),
400
- ).toBe(
401
- "[pi-permission-system] Running bash command 'git push' requires approval, but no interactive UI is available.",
402
- );
403
- });
404
-
405
- test("mcp", () => {
406
- expect(formatUnavailableReason(toolCtx(mcpCheck("server:tool")))).toBe(
407
- "[pi-permission-system] Using tool 'mcp' requires approval, but no interactive UI is available.",
408
- );
409
- });
410
-
411
- test("path", () => {
412
- expect(
413
- formatUnavailableReason({
414
- kind: "path",
415
- toolName: "read",
416
- pathValue: "/etc/passwd",
417
- }),
418
- ).toBe(
419
- "[pi-permission-system] Accessing '/etc/passwd' requires approval, but no interactive UI is available.",
420
- );
421
- });
422
-
423
- test("external_directory", () => {
424
- expect(
425
- formatUnavailableReason({
426
- kind: "external_directory",
427
- toolName: "read",
428
- pathValue: "/etc/passwd",
429
- cwd: "/project",
430
- }),
431
- ).toBe(
432
- "[pi-permission-system] Accessing '/etc/passwd' outside the working directory requires approval, but no interactive UI is available.",
433
- );
434
- });
435
-
436
- test("external_directory discloses the resolved path when it differs from the typed path", () => {
437
- expect(
438
- formatUnavailableReason({
439
- kind: "external_directory",
440
- toolName: "read",
441
- pathValue: "demo-symlink-passwd",
442
- resolvedPath: "/etc/passwd",
443
- cwd: "/project",
444
- }),
445
- ).toBe(
446
- "[pi-permission-system] Accessing 'demo-symlink-passwd' (resolves to '/etc/passwd') outside the working directory requires approval, but no interactive UI is available.",
447
- );
448
- });
449
-
450
- test("bash_external_directory", () => {
451
- expect(
452
- formatUnavailableReason({
453
- kind: "bash_external_directory",
454
- command: "cat /etc/hosts",
455
- externalPaths: [{ path: "/etc/hosts" }],
456
- cwd: "/project",
457
- }),
458
- ).toBe(
459
- "[pi-permission-system] Bash command 'cat /etc/hosts' references path(s) outside the working directory and requires approval, but no interactive UI is available.",
460
- );
461
- });
462
-
463
- test("bash_path", () => {
464
- expect(
465
- formatUnavailableReason({
466
- kind: "bash_path",
467
- command: "cat /etc/passwd",
468
- pathValue: "/etc/passwd",
469
- }),
470
- ).toBe(
471
- "[pi-permission-system] Bash command 'cat /etc/passwd' accesses path '/etc/passwd' which requires approval, but no interactive UI is available.",
472
- );
473
- });
474
-
475
- test("skill_read", () => {
476
- expect(
477
- formatUnavailableReason({
478
- kind: "skill_read",
479
- skillName: "librarian",
480
- readPath: "/skills/librarian/SKILL.md",
481
- }),
482
- ).toBe(
483
- "[pi-permission-system] Accessing skill 'librarian' requires approval, but no interactive UI is available.",
484
- );
485
- });
486
-
487
- test("skill_input", () => {
488
- expect(
489
- formatUnavailableReason({
490
- kind: "skill_input",
491
- skillName: "librarian",
492
- }),
493
- ).toBe(
494
- "[pi-permission-system] Accessing skill 'librarian' requires approval, but no interactive UI is available.",
495
- );
496
- });
497
- });
498
-
499
- // ── formatUserDeniedReason ─────────────────────────────────────────────────
500
-
501
- describe("formatUserDeniedReason", () => {
502
- describe("tool context", () => {
503
- test("generic tool without reason", () => {
504
- expect(formatUserDeniedReason(toolCtx(toolCheck("write")))).toBe(
505
- "[pi-permission-system] User denied tool 'write'.",
506
- );
507
- });
508
-
509
- test("generic tool with reason", () => {
510
- expect(
511
- formatUserDeniedReason(toolCtx(toolCheck("write")), "too risky"),
512
- ).toBe(
513
- "[pi-permission-system] User denied tool 'write'. Reason: too risky.",
514
- );
515
- });
516
-
517
- test("bash with command", () => {
518
- expect(
519
- formatUserDeniedReason(
520
- toolCtx(toolCheck("bash", { command: "ls -la" })),
521
- ),
522
- ).toBe("[pi-permission-system] User denied bash command 'ls -la'.");
523
- });
524
-
525
- test("MCP target", () => {
526
- expect(formatUserDeniedReason(toolCtx(mcpCheck("server:query")))).toBe(
527
- "[pi-permission-system] User denied MCP target 'server:query'.",
528
- );
529
- });
530
- });
531
-
532
- describe("path context", () => {
533
- test("without reason", () => {
534
- expect(
535
- formatUserDeniedReason({
536
- kind: "path",
537
- toolName: "read",
538
- pathValue: "/etc/passwd",
539
- }),
540
- ).toBe(
541
- "[pi-permission-system] User denied access to path '/etc/passwd'.",
542
- );
543
- });
544
-
545
- test("with reason", () => {
546
- expect(
547
- formatUserDeniedReason(
548
- { kind: "path", toolName: "read", pathValue: "/etc/passwd" },
549
- "sensitive",
550
- ),
551
- ).toBe(
552
- "[pi-permission-system] User denied access to path '/etc/passwd'. Reason: sensitive.",
553
- );
554
- });
555
- });
556
-
557
- describe("external_directory context", () => {
558
- test("without reason", () => {
559
- expect(
560
- formatUserDeniedReason({
561
- kind: "external_directory",
562
- toolName: "edit",
563
- pathValue: "/etc/hosts",
564
- cwd: "/project",
565
- }),
566
- ).toBe(
567
- "[pi-permission-system] User denied external directory access for tool 'edit' path '/etc/hosts'.",
568
- );
569
- });
570
-
571
- test("with reason", () => {
572
- expect(
573
- formatUserDeniedReason(
574
- {
575
- kind: "external_directory",
576
- toolName: "edit",
577
- pathValue: "/etc/hosts",
578
- cwd: "/project",
579
- },
580
- "too risky",
581
- ),
582
- ).toBe(
583
- "[pi-permission-system] User denied external directory access for tool 'edit' path '/etc/hosts'. Reason: too risky.",
584
- );
585
- });
586
-
587
- test("discloses the resolved path when it differs from the typed path", () => {
588
- expect(
589
- formatUserDeniedReason({
590
- kind: "external_directory",
591
- toolName: "edit",
592
- pathValue: "demo-symlink-hosts",
593
- resolvedPath: "/etc/hosts",
594
- cwd: "/project",
595
- }),
596
- ).toBe(
597
- "[pi-permission-system] User denied external directory access for tool 'edit' path 'demo-symlink-hosts' (resolves to '/etc/hosts').",
598
- );
599
- });
600
- });
601
-
602
- describe("bash_external_directory context", () => {
603
- test("without reason", () => {
604
- expect(
605
- formatUserDeniedReason({
606
- kind: "bash_external_directory",
607
- command: "rm /etc/hosts",
608
- externalPaths: [{ path: "/etc/hosts" }],
609
- cwd: "/project",
610
- }),
611
- ).toBe(
612
- "[pi-permission-system] User denied external directory access for bash command 'rm /etc/hosts'.",
613
- );
614
- });
615
-
616
- test("with reason", () => {
617
- expect(
618
- formatUserDeniedReason(
619
- {
620
- kind: "bash_external_directory",
621
- command: "rm /etc/hosts",
622
- externalPaths: [{ path: "/etc/hosts" }],
623
- cwd: "/project",
624
- },
625
- "dangerous",
626
- ),
627
- ).toBe(
628
- "[pi-permission-system] User denied external directory access for bash command 'rm /etc/hosts'. Reason: dangerous.",
629
- );
630
- });
631
- });
632
-
633
- describe("bash_path context", () => {
634
- test("without reason", () => {
635
- expect(
636
- formatUserDeniedReason({
637
- kind: "bash_path",
638
- command: "cat /etc/passwd",
639
- pathValue: "/etc/passwd",
640
- }),
641
- ).toBe(
642
- "[pi-permission-system] User denied path access for bash command 'cat /etc/passwd' (path '/etc/passwd').",
643
- );
644
- });
645
-
646
- test("with reason", () => {
647
- expect(
648
- formatUserDeniedReason(
649
- {
650
- kind: "bash_path",
651
- command: "cat /etc/passwd",
652
- pathValue: "/etc/passwd",
653
- },
654
- "sensitive",
655
- ),
656
- ).toBe(
657
- "[pi-permission-system] User denied path access for bash command 'cat /etc/passwd' (path '/etc/passwd'). Reason: sensitive.",
658
- );
659
- });
660
- });
661
-
662
- describe("skill_read context", () => {
663
- test("without reason", () => {
664
- expect(
665
- formatUserDeniedReason({
666
- kind: "skill_read",
667
- skillName: "librarian",
668
- readPath: "/skills/librarian/SKILL.md",
669
- }),
670
- ).toBe("[pi-permission-system] User denied access to skill 'librarian'.");
671
- });
672
-
673
- test("with reason", () => {
674
- expect(
675
- formatUserDeniedReason(
676
- {
677
- kind: "skill_read",
678
- skillName: "librarian",
679
- readPath: "/skills/librarian/SKILL.md",
680
- },
681
- "not needed",
682
- ),
683
- ).toBe(
684
- "[pi-permission-system] User denied access to skill 'librarian'. Reason: not needed.",
685
- );
686
- });
687
- });
688
-
689
- describe("skill_input context", () => {
690
- test("without agent and without reason", () => {
691
- expect(
692
- formatUserDeniedReason({
693
- kind: "skill_input",
694
- skillName: "librarian",
695
- }),
696
- ).toBe("[pi-permission-system] User denied access to skill 'librarian'.");
697
- });
698
-
699
- test("with agent and with reason", () => {
700
- expect(
701
- formatUserDeniedReason(
702
- {
703
- kind: "skill_input",
704
- skillName: "librarian",
705
- agentName: "code-agent",
706
- },
707
- "not permitted",
708
- ),
709
- ).toBe(
710
- "[pi-permission-system] User denied access to skill 'librarian'. Reason: not permitted.",
711
- );
712
- });
713
- });
714
- });