@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,515 +0,0 @@
1
- /**
2
- * Integration tests for external_directory tool_call enforcement.
3
- *
4
- * These tests exercise PermissionGateHandler.handleToolCall with the
5
- * external-directory gate, verifying the full descriptor→runner pipeline
6
- * while mocking only the PermissionSession boundary.
7
- *
8
- * Regression guard: importing the four external-directory message helpers
9
- * ensures the test file fails to load if any helper is removed.
10
- */
11
-
12
- import { describe, expect, it, vi } from "vitest";
13
-
14
- import { EXTENSION_TAG } from "#src/denial-messages";
15
- import { formatExternalDirectoryAskPrompt } from "#src/handlers/gates/external-directory-messages";
16
- import type { PermissionCheckResult } from "#src/types";
17
- import {
18
- ALL_PATH_BEARING_TOOLS,
19
- ALL_TOOLS,
20
- blockReviewEntries,
21
- EXT_DIR_CWD,
22
- EXTERNAL_PATH,
23
- findExtDirDecision,
24
- makeApprovingPrompter,
25
- makeDenyingPrompter,
26
- makeExtDirCheck,
27
- makeUnavailablePrompter,
28
- OPTIONAL_PATH_TOOLS,
29
- } from "#test/helpers/external-directory-fixtures";
30
- import {
31
- getDecisionEvents,
32
- makeCtx,
33
- makeHandler,
34
- makeToolCallEvent,
35
- } from "#test/helpers/handler-fixtures";
36
-
37
- // ── SDK stubs ──────────────────────────────────────────────────────────────
38
- vi.mock("@earendil-works/pi-coding-agent", async (importOriginal) => {
39
- const original =
40
- await importOriginal<typeof import("@earendil-works/pi-coding-agent")>();
41
- return { ...original };
42
- });
43
-
44
- // ── Regression guard: helper presence ──────────────────────────────────────
45
-
46
- describe("external_directory helper regression guard", () => {
47
- it("formatExternalDirectoryAskPrompt is a callable function", () => {
48
- expect(typeof formatExternalDirectoryAskPrompt).toBe("function");
49
- expect(
50
- formatExternalDirectoryAskPrompt(
51
- "read",
52
- "/outside/file",
53
- undefined,
54
- "/project",
55
- ),
56
- ).toContain("/outside/file");
57
- });
58
-
59
- it("EXTENSION_TAG is the expected value", () => {
60
- expect(EXTENSION_TAG).toBe("[pi-permission-system]");
61
- });
62
-
63
- // formatExternalDirectoryDenyReason, formatExternalDirectoryUserDeniedReason,
64
- // and formatExternalDirectoryHardStopHint have moved to denial-messages.ts.
65
- // Their behavior is tested in denial-messages.test.ts.
66
- });
67
-
68
- // ── Path scope: gate applicability ────────────────────────────────────────
69
-
70
- describe("external_directory path scope", () => {
71
- it("skips external_directory check when path is inside CWD", async () => {
72
- const { handler } = makeHandler({
73
- session: { checkPermission: makeExtDirCheck("deny") },
74
- tools: ALL_TOOLS,
75
- });
76
- const event = makeToolCallEvent("read", {
77
- input: { path: `${EXT_DIR_CWD}/src/index.ts` },
78
- });
79
- const result = await handler.handleToolCall(event, makeCtx());
80
- // Should not be blocked — the external_directory gate is skipped,
81
- // and the tool gate sees "allow" (default toolState in makeExtDirCheck)
82
- expect(result).toEqual({ action: "allow" });
83
- });
84
-
85
- it("fires external_directory check when path is outside CWD", async () => {
86
- const { handler } = makeHandler({
87
- session: { checkPermission: makeExtDirCheck("deny") },
88
- tools: ALL_TOOLS,
89
- });
90
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
91
- const result = await handler.handleToolCall(event, makeCtx());
92
- expect(result).toMatchObject({ action: "block" });
93
- });
94
-
95
- it("skips external_directory check for non-path-bearing tool (bash)", async () => {
96
- const { handler } = makeHandler({
97
- session: { checkPermission: makeExtDirCheck("deny", "allow") },
98
- tools: ALL_TOOLS,
99
- });
100
- const event = makeToolCallEvent("bash", {
101
- input: { command: `cat ${EXTERNAL_PATH}` },
102
- });
103
- // bash is not in PATH_BEARING_TOOLS, so the external_directory gate
104
- // for tool path does not fire (bash-external-directory gate is separate)
105
- const result = await handler.handleToolCall(event, makeCtx());
106
- // bash-external-directory gate MAY fire separately, but the tool-path
107
- // external_directory gate does NOT fire for bash
108
- // We verify the checkPermission was not called with "external_directory"
109
- // from the tool-path gate by checking the result is not blocked by it
110
- expect(result).toBeDefined();
111
- });
112
-
113
- it.each(
114
- ALL_PATH_BEARING_TOOLS,
115
- )("blocks %s with an out-of-cwd path when external_directory is deny", async (toolName) => {
116
- const { handler } = makeHandler({
117
- session: { checkPermission: makeExtDirCheck("deny") },
118
- tools: ALL_TOOLS,
119
- });
120
- const event = makeToolCallEvent(toolName, {
121
- input: { path: EXTERNAL_PATH },
122
- });
123
- const result = await handler.handleToolCall(event, makeCtx());
124
- expect(result).toMatchObject({ action: "block" });
125
- });
126
-
127
- it.each(
128
- OPTIONAL_PATH_TOOLS,
129
- )("skips external_directory check for %s when path is omitted", async (toolName) => {
130
- const { handler } = makeHandler({
131
- session: { checkPermission: makeExtDirCheck("deny") },
132
- tools: ALL_TOOLS,
133
- });
134
- // No path in input — external_directory gate should not fire
135
- const event = makeToolCallEvent(toolName);
136
- const result = await handler.handleToolCall(event, makeCtx());
137
- expect(result).toEqual({ action: "allow" });
138
- });
139
- });
140
-
141
- // ── Policy state matrix: allow and deny ────────────────────────────────────
142
-
143
- describe("external_directory policy state — allow", () => {
144
- it("falls through to tool gate when external_directory is allow", async () => {
145
- const { handler } = makeHandler({
146
- session: { checkPermission: makeExtDirCheck("allow") },
147
- tools: ALL_TOOLS,
148
- });
149
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
150
- const result = await handler.handleToolCall(event, makeCtx());
151
- expect(result).toEqual({ action: "allow" });
152
- });
153
-
154
- it("emits decision event with policy_allow on external_directory surface", async () => {
155
- const { handler, events } = makeHandler({
156
- session: { checkPermission: makeExtDirCheck("allow") },
157
- tools: ALL_TOOLS,
158
- });
159
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
160
- await handler.handleToolCall(event, makeCtx());
161
- expect(findExtDirDecision(events)).toMatchObject({
162
- surface: "external_directory",
163
- result: "allow",
164
- resolution: "policy_allow",
165
- });
166
- });
167
-
168
- it("does not write a block review-log entry when external_directory is allow", async () => {
169
- const { handler, logger } = makeHandler({
170
- session: { checkPermission: makeExtDirCheck("allow") },
171
- tools: ALL_TOOLS,
172
- });
173
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
174
- await handler.handleToolCall(event, makeCtx());
175
- expect(blockReviewEntries(logger)).toHaveLength(0);
176
- });
177
- });
178
-
179
- // #144: allow external reads, gate external writes
180
- describe("external_directory — allow external reads, gate external writes (#144)", () => {
181
- it("allows read of external path when external_directory and read are both allow", async () => {
182
- const { handler } = makeHandler({
183
- session: { checkPermission: makeExtDirCheck("allow", "allow") },
184
- tools: ALL_TOOLS,
185
- });
186
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
187
- const result = await handler.handleToolCall(event, makeCtx());
188
- expect(result).toEqual({ action: "allow" });
189
- });
190
-
191
- it("prompts for write to external path when external_directory allows but write is ask", async () => {
192
- const { handler, prompter } = makeHandler({
193
- session: { checkPermission: makeExtDirCheck("allow", "ask") },
194
- prompter: makeApprovingPrompter(),
195
- tools: ALL_TOOLS,
196
- });
197
- const event = makeToolCallEvent("write", {
198
- input: { path: EXTERNAL_PATH },
199
- });
200
- const result = await handler.handleToolCall(event, makeCtx());
201
- // external_directory passes; write gate prompts and user approves
202
- expect(result).toEqual({ action: "allow" });
203
- expect(prompter.prompt).toHaveBeenCalledOnce();
204
- });
205
-
206
- it("blocks write to external path when external_directory allows but write is deny", async () => {
207
- const { handler } = makeHandler({
208
- session: { checkPermission: makeExtDirCheck("allow", "deny") },
209
- tools: ALL_TOOLS,
210
- });
211
- const event = makeToolCallEvent("write", {
212
- input: { path: EXTERNAL_PATH },
213
- });
214
- const result = await handler.handleToolCall(event, makeCtx());
215
- expect(result).toMatchObject({ action: "block" });
216
- });
217
-
218
- it("emits separate decision events for external_directory and write surfaces", async () => {
219
- const { handler, events } = makeHandler({
220
- session: { checkPermission: makeExtDirCheck("allow", "deny") },
221
- tools: ALL_TOOLS,
222
- });
223
- const event = makeToolCallEvent("write", {
224
- input: { path: EXTERNAL_PATH },
225
- });
226
- await handler.handleToolCall(event, makeCtx());
227
- const decisions = getDecisionEvents(events);
228
- const writeDecision = decisions.find((d) => d.surface === "write");
229
- expect(findExtDirDecision(events)).toMatchObject({
230
- surface: "external_directory",
231
- result: "allow",
232
- resolution: "policy_allow",
233
- });
234
- expect(writeDecision).toMatchObject({
235
- surface: "write",
236
- result: "deny",
237
- resolution: "policy_deny",
238
- });
239
- });
240
- });
241
-
242
- describe("external_directory policy state — deny", () => {
243
- it("blocks with reason containing the external path", async () => {
244
- const { handler } = makeHandler({
245
- session: { checkPermission: makeExtDirCheck("deny") },
246
- tools: ALL_TOOLS,
247
- });
248
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
249
- const result = await handler.handleToolCall(event, makeCtx());
250
- expect(result).toMatchObject({ action: "block" });
251
- expect((result as { reason?: string }).reason).toContain(EXTERNAL_PATH);
252
- });
253
-
254
- it("block reason contains extension attribution", async () => {
255
- const { handler } = makeHandler({
256
- session: { checkPermission: makeExtDirCheck("deny") },
257
- tools: ALL_TOOLS,
258
- });
259
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
260
- const result = await handler.handleToolCall(event, makeCtx());
261
- expect((result as { reason?: string }).reason).toContain(
262
- "[pi-permission-system]",
263
- );
264
- expect((result as { reason?: string }).reason).not.toContain("Hard stop");
265
- });
266
-
267
- it("writes review-log entry with resolution policy_denied", async () => {
268
- const { handler, logger } = makeHandler({
269
- session: { checkPermission: makeExtDirCheck("deny") },
270
- tools: ALL_TOOLS,
271
- });
272
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
273
- await handler.handleToolCall(event, makeCtx());
274
- const entries = blockReviewEntries(logger);
275
- expect(entries.length).toBeGreaterThanOrEqual(1);
276
- expect(entries[0][1]).toMatchObject({
277
- resolution: "policy_denied",
278
- });
279
- });
280
-
281
- it("emits decision event with policy_deny on external_directory surface", async () => {
282
- const { handler, events } = makeHandler({
283
- session: { checkPermission: makeExtDirCheck("deny") },
284
- tools: ALL_TOOLS,
285
- });
286
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
287
- await handler.handleToolCall(event, makeCtx());
288
- expect(findExtDirDecision(events)).toMatchObject({
289
- surface: "external_directory",
290
- result: "deny",
291
- resolution: "policy_deny",
292
- });
293
- });
294
- });
295
-
296
- // ── Policy state matrix: ask ────────────────────────────────────────────────
297
-
298
- describe("external_directory policy state — ask", () => {
299
- it("does not block when user approves", async () => {
300
- const { handler } = makeHandler({
301
- session: { checkPermission: makeExtDirCheck("ask") },
302
- prompter: makeApprovingPrompter(),
303
- tools: ALL_TOOLS,
304
- });
305
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
306
- const result = await handler.handleToolCall(event, makeCtx());
307
- expect(result).toEqual({ action: "allow" });
308
- });
309
-
310
- it("emits user_approved decision when user approves", async () => {
311
- const { handler, events } = makeHandler({
312
- session: { checkPermission: makeExtDirCheck("ask") },
313
- prompter: makeApprovingPrompter(),
314
- tools: ALL_TOOLS,
315
- });
316
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
317
- await handler.handleToolCall(event, makeCtx());
318
- expect(findExtDirDecision(events)).toMatchObject({
319
- surface: "external_directory",
320
- result: "allow",
321
- resolution: "user_approved",
322
- });
323
- });
324
-
325
- it("blocks when user denies", async () => {
326
- const { handler } = makeHandler({
327
- session: { checkPermission: makeExtDirCheck("ask") },
328
- prompter: makeDenyingPrompter(),
329
- tools: ALL_TOOLS,
330
- });
331
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
332
- const result = await handler.handleToolCall(event, makeCtx());
333
- expect(result).toMatchObject({ action: "block" });
334
- });
335
-
336
- it("emits user_denied decision when user denies", async () => {
337
- const { handler, events } = makeHandler({
338
- session: { checkPermission: makeExtDirCheck("ask") },
339
- prompter: makeDenyingPrompter(),
340
- tools: ALL_TOOLS,
341
- });
342
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
343
- await handler.handleToolCall(event, makeCtx());
344
- expect(findExtDirDecision(events)).toMatchObject({
345
- surface: "external_directory",
346
- result: "deny",
347
- resolution: "user_denied",
348
- });
349
- });
350
-
351
- it("block reason includes denialReason when user provides one", async () => {
352
- const { handler } = makeHandler({
353
- session: { checkPermission: makeExtDirCheck("ask") },
354
- prompter: makeDenyingPrompter("not needed"),
355
- tools: ALL_TOOLS,
356
- });
357
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
358
- const result = await handler.handleToolCall(event, makeCtx());
359
- expect(result).toMatchObject({ action: "block" });
360
- expect((result as { reason?: string }).reason).toContain("not needed");
361
- });
362
-
363
- it("blocks with confirmation_unavailable when no UI is available", async () => {
364
- const { handler } = makeHandler({
365
- session: { checkPermission: makeExtDirCheck("ask") },
366
- prompter: makeUnavailablePrompter(),
367
- tools: ALL_TOOLS,
368
- });
369
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
370
- const result = await handler.handleToolCall(
371
- event,
372
- makeCtx({ hasUI: false }),
373
- );
374
- expect(result).toMatchObject({ action: "block" });
375
- expect((result as { reason?: string }).reason).toContain(
376
- "outside the working directory",
377
- );
378
- });
379
-
380
- it("writes review-log entry with confirmation_unavailable when no UI", async () => {
381
- const { handler, logger } = makeHandler({
382
- session: { checkPermission: makeExtDirCheck("ask") },
383
- prompter: makeUnavailablePrompter(),
384
- tools: ALL_TOOLS,
385
- });
386
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
387
- await handler.handleToolCall(event, makeCtx({ hasUI: false }));
388
- const entries = blockReviewEntries(logger);
389
- expect(entries.length).toBeGreaterThanOrEqual(1);
390
- expect(entries[0][1]).toMatchObject({
391
- resolution: "confirmation_unavailable",
392
- });
393
- });
394
-
395
- it("emits confirmation_unavailable decision when no UI", async () => {
396
- const { handler, events } = makeHandler({
397
- session: { checkPermission: makeExtDirCheck("ask") },
398
- prompter: makeUnavailablePrompter(),
399
- tools: ALL_TOOLS,
400
- });
401
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
402
- await handler.handleToolCall(event, makeCtx({ hasUI: false }));
403
- expect(findExtDirDecision(events)).toMatchObject({
404
- surface: "external_directory",
405
- result: "deny",
406
- resolution: "confirmation_unavailable",
407
- });
408
- });
409
- });
410
-
411
- // ── Per-agent override ─────────────────────────────────────────────────────
412
-
413
- describe("external_directory per-agent override", () => {
414
- it("honors per-agent override of external_directory policy", async () => {
415
- // checkPermission varies by agentName: allow for "special-agent", deny otherwise
416
- const agentAwareCheck = vi
417
- .fn()
418
- .mockImplementation(
419
- (
420
- surface: string,
421
- _input: unknown,
422
- agentName?: string,
423
- ): PermissionCheckResult => {
424
- if (surface === "external_directory") {
425
- const state =
426
- agentName === "special-agent" ? "allow" : ("deny" as const);
427
- return {
428
- state,
429
- toolName: surface,
430
- source: "tool",
431
- origin: agentName === "special-agent" ? "agent" : "global",
432
- };
433
- }
434
- return {
435
- state: "allow",
436
- toolName: surface,
437
- source: "tool",
438
- origin: "builtin",
439
- };
440
- },
441
- );
442
-
443
- // With agent override → allowed
444
- const { handler: handler1, events: events1 } = makeHandler({
445
- session: {
446
- checkPermission: agentAwareCheck,
447
- resolveAgentName: vi.fn().mockReturnValue("special-agent"),
448
- },
449
- tools: ALL_TOOLS,
450
- });
451
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
452
- const result1 = await handler1.handleToolCall(event, makeCtx());
453
- expect(result1).toEqual({ action: "allow" });
454
-
455
- expect(findExtDirDecision(events1)).toMatchObject({
456
- result: "allow",
457
- resolution: "policy_allow",
458
- agentName: "special-agent",
459
- });
460
-
461
- // Without agent override → denied
462
- const { handler: handler2 } = makeHandler({
463
- session: {
464
- checkPermission: agentAwareCheck,
465
- resolveAgentName: vi.fn().mockReturnValue(null),
466
- },
467
- tools: ALL_TOOLS,
468
- });
469
- const result2 = await handler2.handleToolCall(event, makeCtx());
470
- expect(result2).toMatchObject({ action: "block" });
471
- });
472
- });
473
-
474
- // ── Decision event surface and value ──────────────────────────────────────
475
-
476
- describe("external_directory decision event fields", () => {
477
- it("decision event value is the external path", async () => {
478
- const { handler, events } = makeHandler({
479
- session: { checkPermission: makeExtDirCheck("deny") },
480
- tools: ALL_TOOLS,
481
- });
482
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
483
- await handler.handleToolCall(event, makeCtx());
484
- const extDirDecision = findExtDirDecision(events);
485
- expect(extDirDecision).toBeDefined();
486
- expect(extDirDecision!.value).toBe(EXTERNAL_PATH);
487
- });
488
-
489
- it("decision event includes agentName when present", async () => {
490
- const { handler, events } = makeHandler({
491
- session: {
492
- checkPermission: makeExtDirCheck("allow"),
493
- resolveAgentName: vi.fn().mockReturnValue("my-agent"),
494
- },
495
- tools: ALL_TOOLS,
496
- });
497
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
498
- await handler.handleToolCall(event, makeCtx());
499
- expect(findExtDirDecision(events)).toMatchObject({
500
- agentName: "my-agent",
501
- });
502
- });
503
-
504
- it("decision event agentName is null when no agent", async () => {
505
- const { handler, events } = makeHandler({
506
- session: { checkPermission: makeExtDirCheck("allow") },
507
- tools: ALL_TOOLS,
508
- });
509
- const event = makeToolCallEvent("read", { input: { path: EXTERNAL_PATH } });
510
- await handler.handleToolCall(event, makeCtx());
511
- expect(findExtDirDecision(events)).toMatchObject({
512
- agentName: null,
513
- });
514
- });
515
- });
@@ -1,175 +0,0 @@
1
- /**
2
- * Integration tests verifying that sequential tool calls to the same
3
- * external path only prompt once — the session-approval recorded by the
4
- * first call covers the second.
5
- *
6
- * Uses real PermissionSession + PermissionResolver + SessionRules so the
7
- * stateful approval-tracking path is exercised end-to-end.
8
- */
9
-
10
- import { describe, expect, it, vi } from "vitest";
11
- import {
12
- makeApprovingPrompter,
13
- makeDeduplicatingHandler,
14
- makeDedupWiring,
15
- makeExtDirBashEvent,
16
- makeExtDirToolEvent,
17
- } from "#test/helpers/external-directory-fixtures";
18
- import { makeCtx } from "#test/helpers/handler-fixtures";
19
-
20
- // ── SDK stub ───────────────────────────────────────────────────────────────
21
- vi.mock("@earendil-works/pi-coding-agent", async (importOriginal) => {
22
- const original =
23
- await importOriginal<typeof import("@earendil-works/pi-coding-agent")>();
24
- return { ...original };
25
- });
26
-
27
- // ── tests ──────────────────────────────────────────────────────────────────
28
-
29
- describe("external-directory session dedup", () => {
30
- describe("path-bearing tools (read, write, edit)", () => {
31
- it("does not re-prompt for the same external path after session approval", async () => {
32
- const { handler, prompter } = makeDeduplicatingHandler();
33
- const ctx = makeCtx();
34
- const externalPath = "/outside/project/data.txt";
35
-
36
- // First call — should prompt
37
- const event1 = makeExtDirToolEvent("read", externalPath, "tc-1");
38
- const result1 = await handler.handleToolCall(event1, ctx);
39
- expect(result1).toEqual({ action: "allow" });
40
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
41
-
42
- // Second call — same path, should hit session rule, no prompt
43
- const event2 = makeExtDirToolEvent("read", externalPath, "tc-2");
44
- const result2 = await handler.handleToolCall(event2, ctx);
45
- expect(result2).toEqual({ action: "allow" });
46
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
47
- });
48
-
49
- it("does not re-prompt for a different file in the same external directory", async () => {
50
- const { handler, prompter } = makeDeduplicatingHandler();
51
- const ctx = makeCtx();
52
-
53
- // First call — prompt for /outside/project/a.txt
54
- const event1 = makeExtDirToolEvent(
55
- "read",
56
- "/outside/project/a.txt",
57
- "tc-1",
58
- );
59
- await handler.handleToolCall(event1, ctx);
60
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
61
-
62
- // Second call — /outside/project/b.txt is in the same directory
63
- const event2 = makeExtDirToolEvent(
64
- "read",
65
- "/outside/project/b.txt",
66
- "tc-2",
67
- );
68
- await handler.handleToolCall(event2, ctx);
69
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
70
- });
71
-
72
- it("does prompt for a file in a different external directory", async () => {
73
- const { handler, prompter } = makeDeduplicatingHandler();
74
- const ctx = makeCtx();
75
-
76
- // First call — /outside/alpha/file.txt
77
- const event1 = makeExtDirToolEvent(
78
- "read",
79
- "/outside/alpha/file.txt",
80
- "tc-1",
81
- );
82
- await handler.handleToolCall(event1, ctx);
83
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
84
-
85
- // Second call — /outside/beta/file.txt is a different directory
86
- const event2 = makeExtDirToolEvent(
87
- "read",
88
- "/outside/beta/file.txt",
89
- "tc-2",
90
- );
91
- await handler.handleToolCall(event2, ctx);
92
- expect(prompter.prompt).toHaveBeenCalledTimes(2);
93
- });
94
-
95
- it("re-prompts when user approved once (not for session)", async () => {
96
- const approveOnce = makeApprovingPrompter();
97
- const { handler, prompter } = makeDeduplicatingHandler(approveOnce);
98
- const ctx = makeCtx();
99
- const externalPath = "/outside/project/data.txt";
100
-
101
- // First call — prompt, approved once
102
- const event1 = makeExtDirToolEvent("read", externalPath, "tc-1");
103
- await handler.handleToolCall(event1, ctx);
104
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
105
-
106
- // Second call — no session rule recorded, should prompt again
107
- const event2 = makeExtDirToolEvent("read", externalPath, "tc-2");
108
- await handler.handleToolCall(event2, ctx);
109
- expect(prompter.prompt).toHaveBeenCalledTimes(2);
110
- });
111
- });
112
-
113
- describe("bash commands with external paths", () => {
114
- it("does not re-prompt for a bash command referencing the same external path after session approval", async () => {
115
- const { handler, prompter } = makeDeduplicatingHandler();
116
- const ctx = makeCtx();
117
-
118
- // First call — bash referencing /tmp/out.txt
119
- const event1 = makeExtDirBashEvent("echo hello > /tmp/out.txt", "tc-1");
120
- const result1 = await handler.handleToolCall(event1, ctx);
121
- expect(result1).toEqual({ action: "allow" });
122
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
123
-
124
- // Second call — different bash command, same external path
125
- const event2 = makeExtDirBashEvent("cat /tmp/out.txt", "tc-2");
126
- const result2 = await handler.handleToolCall(event2, ctx);
127
- expect(result2).toEqual({ action: "allow" });
128
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
129
- });
130
-
131
- it("does not re-prompt for read after bash already approved the same directory", async () => {
132
- const { handler, prompter } = makeDeduplicatingHandler();
133
- const ctx = makeCtx();
134
-
135
- // First call — bash writes to /tmp/out.txt
136
- const event1 = makeExtDirBashEvent("echo hello > /tmp/out.txt", "tc-1");
137
- await handler.handleToolCall(event1, ctx);
138
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
139
-
140
- // Second call — read from /tmp/out.txt (same directory, different tool)
141
- const event2 = makeExtDirToolEvent("read", "/tmp/out.txt", "tc-2");
142
- await handler.handleToolCall(event2, ctx);
143
- expect(prompter.prompt).toHaveBeenCalledTimes(1);
144
- });
145
- });
146
- });
147
-
148
- // ---------------------------------------------------------------------------
149
- // Moved from permission-system.test.ts catch-all (#342)
150
- // ---------------------------------------------------------------------------
151
-
152
- describe("session shutdown clears external-directory approvals", () => {
153
- it("re-prompts for the same path after session shutdown", async () => {
154
- const { handler, prompter, session } = makeDedupWiring();
155
-
156
- const externalPath = "/tmp/sibling/foo.ts";
157
- const ctx = makeCtx();
158
- const event = makeExtDirToolEvent("read", externalPath, "tc-1");
159
-
160
- // First access: prompt fires and records session approval.
161
- await handler.handleToolCall(event, ctx);
162
- expect(vi.mocked(prompter.prompt)).toHaveBeenCalledTimes(1);
163
-
164
- // Second access: covered by session approval — no re-prompt.
165
- await handler.handleToolCall({ ...event, toolCallId: "tc-2" }, ctx);
166
- expect(vi.mocked(prompter.prompt)).toHaveBeenCalledTimes(1);
167
-
168
- // Shutdown clears session approvals.
169
- session.shutdown();
170
-
171
- // Third access: session rules cleared — must re-prompt.
172
- await handler.handleToolCall({ ...event, toolCallId: "tc-3" }, ctx);
173
- expect(vi.mocked(prompter.prompt)).toHaveBeenCalledTimes(2);
174
- });
175
- });