@gotgenes/pi-permission-system 6.0.1 → 7.0.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.
@@ -2,6 +2,10 @@ import type { SkillPromptEntry } from "./skill-prompt-sanitizer";
2
2
  import { formatToolInputForPrompt } from "./tool-input-preview";
3
3
  import type { PermissionCheckResult } from "./types";
4
4
 
5
+ // NOTE: formatDenyReason, formatUserDeniedReason, and
6
+ // formatPermissionHardStopHint have been moved to denial-messages.ts.
7
+ // This module retains only pre-check messages and user-facing ask prompts.
8
+
5
9
  export function formatMissingToolNameReason(): string {
6
10
  return "Tool call was blocked because no tool name was provided. Use a registered tool name from pi.getAllTools().";
7
11
  }
@@ -23,58 +27,6 @@ export function formatUnknownToolReason(
23
27
  return `Tool '${toolName}' is not registered in this runtime and was blocked before permission checks.${mcpHint} Registered tools: ${availableList}.`;
24
28
  }
25
29
 
26
- export function formatPermissionHardStopHint(
27
- result: PermissionCheckResult,
28
- ): string {
29
- if ((result.source === "mcp" || result.toolName === "mcp") && result.target) {
30
- return "Hard stop: this MCP permission denial is policy-enforced. Do not retry this target, do not run discovery/investigation to bypass it, and report the block to the user.";
31
- }
32
-
33
- return "Hard stop: this permission denial is policy-enforced. Do not retry or investigate bypasses; report the block to the user.";
34
- }
35
-
36
- export function formatDenyReason(
37
- result: PermissionCheckResult,
38
- agentName?: string,
39
- ): string {
40
- const parts: string[] = [];
41
-
42
- if (agentName) {
43
- parts.push(`Agent '${agentName}'`);
44
- }
45
-
46
- if ((result.source === "mcp" || result.toolName === "mcp") && result.target) {
47
- parts.push(`is not permitted to run MCP target '${result.target}'`);
48
- } else {
49
- parts.push(`is not permitted to run '${result.toolName}'`);
50
- }
51
-
52
- if (result.command) {
53
- parts.push(`command '${result.command}'`);
54
- }
55
-
56
- if (result.matchedPattern) {
57
- parts.push(`(matched '${result.matchedPattern}')`);
58
- }
59
-
60
- return `${parts.join(" ")}. ${formatPermissionHardStopHint(result)}`;
61
- }
62
-
63
- export function formatUserDeniedReason(
64
- result: PermissionCheckResult,
65
- denialReason?: string,
66
- ): string {
67
- const base =
68
- (result.source === "mcp" || result.toolName === "mcp") && result.target
69
- ? `User denied MCP target '${result.target}'.`
70
- : result.toolName === "bash" && result.command
71
- ? `User denied bash command '${result.command}'.`
72
- : `User denied tool '${result.toolName}'.`;
73
- const reasonSuffix = denialReason ? ` Reason: ${denialReason}.` : "";
74
-
75
- return `${base}${reasonSuffix} ${formatPermissionHardStopHint(result)}`;
76
- }
77
-
78
30
  export function formatAskPrompt(
79
31
  result: PermissionCheckResult,
80
32
  agentName?: string,
@@ -121,11 +73,4 @@ export function formatSkillPathAskPrompt(
121
73
  return `${subject} requested access to skill '${skill.name}' via '${readPath}'. Allow this read?`;
122
74
  }
123
75
 
124
- export function formatSkillPathDenyReason(
125
- skill: SkillPromptEntry,
126
- readPath: string,
127
- agentName?: string,
128
- ): string {
129
- const subject = agentName ? `Agent '${agentName}'` : "Current agent";
130
- return `${subject} is not permitted to access skill '${skill.name}' via '${readPath}'.`;
131
- }
76
+ // formatSkillPathDenyReason has been moved to denial-messages.ts.
@@ -9,14 +9,12 @@ vi.mock("node:os", () => {
9
9
  };
10
10
  });
11
11
 
12
+ import { formatDenyReason } from "../src/denial-messages";
12
13
  import {
13
14
  extractExternalPathsFromBashCommand,
14
15
  extractTokensForPathRules,
15
16
  } from "../src/handlers/gates/bash-path-extractor";
16
- import {
17
- formatBashExternalDirectoryAskPrompt,
18
- formatBashExternalDirectoryDenyReason,
19
- } from "../src/handlers/gates/external-directory-messages";
17
+ import { formatBashExternalDirectoryAskPrompt } from "../src/handlers/gates/external-directory-messages";
20
18
 
21
19
  afterEach(() => {
22
20
  vi.restoreAllMocks();
@@ -859,35 +857,19 @@ describe("formatBashExternalDirectoryAskPrompt", () => {
859
857
  });
860
858
  });
861
859
 
862
- describe("formatBashExternalDirectoryDenyReason", () => {
863
- test("includes command, external paths, and CWD", () => {
864
- const result = formatBashExternalDirectoryDenyReason(
865
- "cat /etc/hosts",
866
- ["/etc/hosts"],
867
- "/projects/my-app",
868
- );
860
+ describe("bash external-directory denial messages (centralized)", () => {
861
+ test("denial message includes command, paths, and extension tag", () => {
862
+ const result = formatDenyReason({
863
+ kind: "bash_external_directory",
864
+ command: "cat /etc/hosts",
865
+ externalPaths: ["/etc/hosts"],
866
+ cwd: "/projects/my-app",
867
+ });
869
868
  expect(result).toContain("cat /etc/hosts");
870
869
  expect(result).toContain("/etc/hosts");
871
870
  expect(result).toContain("/projects/my-app");
872
- });
873
-
874
- test("includes hard stop hint", () => {
875
- const result = formatBashExternalDirectoryDenyReason(
876
- "cat /etc/hosts",
877
- ["/etc/hosts"],
878
- "/projects/my-app",
879
- );
880
- expect(result).toContain("Hard stop");
881
- });
882
-
883
- test("includes agent name when provided", () => {
884
- const result = formatBashExternalDirectoryDenyReason(
885
- "cat /etc/hosts",
886
- ["/etc/hosts"],
887
- "/projects/my-app",
888
- "my-agent",
889
- );
890
- expect(result).toContain("my-agent");
871
+ expect(result).toContain("[pi-permission-system]");
872
+ expect(result).not.toContain("Hard stop");
891
873
  });
892
874
  });
893
875
 
@@ -0,0 +1,517 @@
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("MCP source with target on non-mcp toolName", () => {
102
+ expect(
103
+ formatDenyReason(
104
+ toolCtx(
105
+ toolCheck("anything", { source: "mcp", target: "server:tool" }),
106
+ ),
107
+ ),
108
+ ).toBe(
109
+ "[pi-permission-system] is not permitted to run MCP target 'server:tool'.",
110
+ );
111
+ });
112
+ });
113
+
114
+ describe("path context", () => {
115
+ test("without agent", () => {
116
+ expect(
117
+ formatDenyReason({
118
+ kind: "path",
119
+ toolName: "read",
120
+ pathValue: "/etc/passwd",
121
+ }),
122
+ ).toBe(
123
+ "[pi-permission-system] Current agent is not permitted to access path '/etc/passwd' via tool 'read'.",
124
+ );
125
+ });
126
+
127
+ test("with agent", () => {
128
+ expect(
129
+ formatDenyReason({
130
+ kind: "path",
131
+ toolName: "read",
132
+ pathValue: "/etc/passwd",
133
+ agentName: "sec-agent",
134
+ }),
135
+ ).toBe(
136
+ "[pi-permission-system] Agent 'sec-agent' is not permitted to access path '/etc/passwd' via tool 'read'.",
137
+ );
138
+ });
139
+ });
140
+
141
+ describe("external_directory context", () => {
142
+ test("without agent", () => {
143
+ expect(
144
+ formatDenyReason({
145
+ kind: "external_directory",
146
+ toolName: "read",
147
+ pathValue: "/etc/passwd",
148
+ cwd: "/project",
149
+ }),
150
+ ).toBe(
151
+ "[pi-permission-system] Current agent is not permitted to run tool 'read' for path '/etc/passwd' outside working directory '/project'.",
152
+ );
153
+ });
154
+
155
+ test("with agent", () => {
156
+ expect(
157
+ formatDenyReason({
158
+ kind: "external_directory",
159
+ toolName: "read",
160
+ pathValue: "/etc/passwd",
161
+ cwd: "/project",
162
+ agentName: "sec-agent",
163
+ }),
164
+ ).toBe(
165
+ "[pi-permission-system] Agent 'sec-agent' is not permitted to run tool 'read' for path '/etc/passwd' outside working directory '/project'.",
166
+ );
167
+ });
168
+ });
169
+
170
+ describe("bash_external_directory context", () => {
171
+ test("single path without agent", () => {
172
+ expect(
173
+ formatDenyReason({
174
+ kind: "bash_external_directory",
175
+ command: "cat /etc/hosts",
176
+ externalPaths: ["/etc/hosts"],
177
+ cwd: "/project",
178
+ }),
179
+ ).toBe(
180
+ "[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.",
181
+ );
182
+ });
183
+
184
+ test("multiple paths with agent", () => {
185
+ expect(
186
+ formatDenyReason({
187
+ kind: "bash_external_directory",
188
+ command: "cp /etc/hosts /tmp/out",
189
+ externalPaths: ["/etc/hosts", "/tmp/out"],
190
+ cwd: "/project",
191
+ agentName: "my-agent",
192
+ }),
193
+ ).toBe(
194
+ "[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.",
195
+ );
196
+ });
197
+ });
198
+
199
+ describe("bash_path context", () => {
200
+ test("without agent", () => {
201
+ expect(
202
+ formatDenyReason({
203
+ kind: "bash_path",
204
+ command: "cat /etc/passwd",
205
+ pathValue: "/etc/passwd",
206
+ }),
207
+ ).toBe(
208
+ "[pi-permission-system] Current agent is not permitted to access path '/etc/passwd' via tool 'bash'.",
209
+ );
210
+ });
211
+
212
+ test("with agent", () => {
213
+ expect(
214
+ formatDenyReason({
215
+ kind: "bash_path",
216
+ command: "cat /etc/passwd",
217
+ pathValue: "/etc/passwd",
218
+ agentName: "my-agent",
219
+ }),
220
+ ).toBe(
221
+ "[pi-permission-system] Agent 'my-agent' is not permitted to access path '/etc/passwd' via tool 'bash'.",
222
+ );
223
+ });
224
+ });
225
+
226
+ describe("skill_read context", () => {
227
+ test("without agent", () => {
228
+ expect(
229
+ formatDenyReason({
230
+ kind: "skill_read",
231
+ skillName: "librarian",
232
+ readPath: "/skills/librarian/SKILL.md",
233
+ }),
234
+ ).toBe(
235
+ "[pi-permission-system] Current agent is not permitted to access skill 'librarian' via '/skills/librarian/SKILL.md'.",
236
+ );
237
+ });
238
+
239
+ test("with agent", () => {
240
+ expect(
241
+ formatDenyReason({
242
+ kind: "skill_read",
243
+ skillName: "librarian",
244
+ readPath: "/skills/librarian/SKILL.md",
245
+ agentName: "my-agent",
246
+ }),
247
+ ).toBe(
248
+ "[pi-permission-system] Agent 'my-agent' is not permitted to access skill 'librarian' via '/skills/librarian/SKILL.md'.",
249
+ );
250
+ });
251
+ });
252
+ });
253
+
254
+ // ── formatUnavailableReason ────────────────────────────────────────────────
255
+
256
+ describe("formatUnavailableReason", () => {
257
+ test("generic tool", () => {
258
+ expect(formatUnavailableReason(toolCtx(toolCheck("write")))).toBe(
259
+ "[pi-permission-system] Using tool 'write' requires approval, but no interactive UI is available.",
260
+ );
261
+ });
262
+
263
+ test("bash with command", () => {
264
+ expect(
265
+ formatUnavailableReason(
266
+ toolCtx(toolCheck("bash", { command: "git push" })),
267
+ ),
268
+ ).toBe(
269
+ "[pi-permission-system] Running bash command 'git push' requires approval, but no interactive UI is available.",
270
+ );
271
+ });
272
+
273
+ test("mcp", () => {
274
+ expect(formatUnavailableReason(toolCtx(mcpCheck("server:tool")))).toBe(
275
+ "[pi-permission-system] Using tool 'mcp' requires approval, but no interactive UI is available.",
276
+ );
277
+ });
278
+
279
+ test("path", () => {
280
+ expect(
281
+ formatUnavailableReason({
282
+ kind: "path",
283
+ toolName: "read",
284
+ pathValue: "/etc/passwd",
285
+ }),
286
+ ).toBe(
287
+ "[pi-permission-system] Accessing '/etc/passwd' requires approval, but no interactive UI is available.",
288
+ );
289
+ });
290
+
291
+ test("external_directory", () => {
292
+ expect(
293
+ formatUnavailableReason({
294
+ kind: "external_directory",
295
+ toolName: "read",
296
+ pathValue: "/etc/passwd",
297
+ cwd: "/project",
298
+ }),
299
+ ).toBe(
300
+ "[pi-permission-system] Accessing '/etc/passwd' outside the working directory requires approval, but no interactive UI is available.",
301
+ );
302
+ });
303
+
304
+ test("bash_external_directory", () => {
305
+ expect(
306
+ formatUnavailableReason({
307
+ kind: "bash_external_directory",
308
+ command: "cat /etc/hosts",
309
+ externalPaths: ["/etc/hosts"],
310
+ cwd: "/project",
311
+ }),
312
+ ).toBe(
313
+ "[pi-permission-system] Bash command 'cat /etc/hosts' references path(s) outside the working directory and requires approval, but no interactive UI is available.",
314
+ );
315
+ });
316
+
317
+ test("bash_path", () => {
318
+ expect(
319
+ formatUnavailableReason({
320
+ kind: "bash_path",
321
+ command: "cat /etc/passwd",
322
+ pathValue: "/etc/passwd",
323
+ }),
324
+ ).toBe(
325
+ "[pi-permission-system] Bash command 'cat /etc/passwd' accesses path '/etc/passwd' which requires approval, but no interactive UI is available.",
326
+ );
327
+ });
328
+
329
+ test("skill_read", () => {
330
+ expect(
331
+ formatUnavailableReason({
332
+ kind: "skill_read",
333
+ skillName: "librarian",
334
+ readPath: "/skills/librarian/SKILL.md",
335
+ }),
336
+ ).toBe(
337
+ "[pi-permission-system] Accessing skill 'librarian' requires approval, but no interactive UI is available.",
338
+ );
339
+ });
340
+ });
341
+
342
+ // ── formatUserDeniedReason ─────────────────────────────────────────────────
343
+
344
+ describe("formatUserDeniedReason", () => {
345
+ describe("tool context", () => {
346
+ test("generic tool without reason", () => {
347
+ expect(formatUserDeniedReason(toolCtx(toolCheck("write")))).toBe(
348
+ "[pi-permission-system] User denied tool 'write'.",
349
+ );
350
+ });
351
+
352
+ test("generic tool with reason", () => {
353
+ expect(
354
+ formatUserDeniedReason(toolCtx(toolCheck("write")), "too risky"),
355
+ ).toBe(
356
+ "[pi-permission-system] User denied tool 'write'. Reason: too risky.",
357
+ );
358
+ });
359
+
360
+ test("bash with command", () => {
361
+ expect(
362
+ formatUserDeniedReason(
363
+ toolCtx(toolCheck("bash", { command: "ls -la" })),
364
+ ),
365
+ ).toBe("[pi-permission-system] User denied bash command 'ls -la'.");
366
+ });
367
+
368
+ test("MCP target", () => {
369
+ expect(formatUserDeniedReason(toolCtx(mcpCheck("server:query")))).toBe(
370
+ "[pi-permission-system] User denied MCP target 'server:query'.",
371
+ );
372
+ });
373
+ });
374
+
375
+ describe("path context", () => {
376
+ test("without reason", () => {
377
+ expect(
378
+ formatUserDeniedReason({
379
+ kind: "path",
380
+ toolName: "read",
381
+ pathValue: "/etc/passwd",
382
+ }),
383
+ ).toBe(
384
+ "[pi-permission-system] User denied access to path '/etc/passwd'.",
385
+ );
386
+ });
387
+
388
+ test("with reason", () => {
389
+ expect(
390
+ formatUserDeniedReason(
391
+ { kind: "path", toolName: "read", pathValue: "/etc/passwd" },
392
+ "sensitive",
393
+ ),
394
+ ).toBe(
395
+ "[pi-permission-system] User denied access to path '/etc/passwd'. Reason: sensitive.",
396
+ );
397
+ });
398
+ });
399
+
400
+ describe("external_directory context", () => {
401
+ test("without reason", () => {
402
+ expect(
403
+ formatUserDeniedReason({
404
+ kind: "external_directory",
405
+ toolName: "edit",
406
+ pathValue: "/etc/hosts",
407
+ cwd: "/project",
408
+ }),
409
+ ).toBe(
410
+ "[pi-permission-system] User denied external directory access for tool 'edit' path '/etc/hosts'.",
411
+ );
412
+ });
413
+
414
+ test("with reason", () => {
415
+ expect(
416
+ formatUserDeniedReason(
417
+ {
418
+ kind: "external_directory",
419
+ toolName: "edit",
420
+ pathValue: "/etc/hosts",
421
+ cwd: "/project",
422
+ },
423
+ "too risky",
424
+ ),
425
+ ).toBe(
426
+ "[pi-permission-system] User denied external directory access for tool 'edit' path '/etc/hosts'. Reason: too risky.",
427
+ );
428
+ });
429
+ });
430
+
431
+ describe("bash_external_directory context", () => {
432
+ test("without reason", () => {
433
+ expect(
434
+ formatUserDeniedReason({
435
+ kind: "bash_external_directory",
436
+ command: "rm /etc/hosts",
437
+ externalPaths: ["/etc/hosts"],
438
+ cwd: "/project",
439
+ }),
440
+ ).toBe(
441
+ "[pi-permission-system] User denied external directory access for bash command 'rm /etc/hosts'.",
442
+ );
443
+ });
444
+
445
+ test("with reason", () => {
446
+ expect(
447
+ formatUserDeniedReason(
448
+ {
449
+ kind: "bash_external_directory",
450
+ command: "rm /etc/hosts",
451
+ externalPaths: ["/etc/hosts"],
452
+ cwd: "/project",
453
+ },
454
+ "dangerous",
455
+ ),
456
+ ).toBe(
457
+ "[pi-permission-system] User denied external directory access for bash command 'rm /etc/hosts'. Reason: dangerous.",
458
+ );
459
+ });
460
+ });
461
+
462
+ describe("bash_path context", () => {
463
+ test("without reason", () => {
464
+ expect(
465
+ formatUserDeniedReason({
466
+ kind: "bash_path",
467
+ command: "cat /etc/passwd",
468
+ pathValue: "/etc/passwd",
469
+ }),
470
+ ).toBe(
471
+ "[pi-permission-system] User denied path access for bash command 'cat /etc/passwd' (path '/etc/passwd').",
472
+ );
473
+ });
474
+
475
+ test("with reason", () => {
476
+ expect(
477
+ formatUserDeniedReason(
478
+ {
479
+ kind: "bash_path",
480
+ command: "cat /etc/passwd",
481
+ pathValue: "/etc/passwd",
482
+ },
483
+ "sensitive",
484
+ ),
485
+ ).toBe(
486
+ "[pi-permission-system] User denied path access for bash command 'cat /etc/passwd' (path '/etc/passwd'). Reason: sensitive.",
487
+ );
488
+ });
489
+ });
490
+
491
+ describe("skill_read context", () => {
492
+ test("without reason", () => {
493
+ expect(
494
+ formatUserDeniedReason({
495
+ kind: "skill_read",
496
+ skillName: "librarian",
497
+ readPath: "/skills/librarian/SKILL.md",
498
+ }),
499
+ ).toBe("[pi-permission-system] User denied access to skill 'librarian'.");
500
+ });
501
+
502
+ test("with reason", () => {
503
+ expect(
504
+ formatUserDeniedReason(
505
+ {
506
+ kind: "skill_read",
507
+ skillName: "librarian",
508
+ readPath: "/skills/librarian/SKILL.md",
509
+ },
510
+ "not needed",
511
+ ),
512
+ ).toBe(
513
+ "[pi-permission-system] User denied access to skill 'librarian'. Reason: not needed.",
514
+ );
515
+ });
516
+ });
517
+ });