@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,400 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-deprecated -- tests the deprecated RPC channel implementation */
2
- import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3
- import { tmpdir } from "node:os";
4
- import { dirname, join } from "node:path";
5
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
-
7
- import { getGlobalConfigPath } from "#src/config-paths";
8
- import piPermissionSystemExtension from "#src/index";
9
- import type {
10
- PermissionDecisionEvent,
11
- PermissionsCheckReplyData,
12
- PermissionsCheckRequest,
13
- PermissionsPromptReplyData,
14
- PermissionsPromptRequest,
15
- PermissionsReadyEvent,
16
- PermissionsRpcReply,
17
- PermissionUiPromptEvent,
18
- } from "#src/permission-events";
19
- import {
20
- emitDecisionEvent,
21
- emitReadyEvent,
22
- emitUiPromptEvent,
23
- PERMISSIONS_DECISION_CHANNEL,
24
- PERMISSIONS_PROTOCOL_VERSION,
25
- PERMISSIONS_READY_CHANNEL,
26
- PERMISSIONS_RPC_CHECK_CHANNEL,
27
- PERMISSIONS_RPC_PROMPT_CHANNEL,
28
- PERMISSIONS_UI_PROMPT_CHANNEL,
29
- } from "#src/permission-events";
30
-
31
- // ── Minimal EventBus stub ──────────────────────────────────────────────────
32
-
33
- function makeEventBus() {
34
- return {
35
- emit: vi.fn(),
36
- on: vi.fn().mockReturnValue(() => undefined),
37
- };
38
- }
39
-
40
- // ── Constants ──────────────────────────────────────────────────────────────
41
-
42
- describe("constants", () => {
43
- it("PERMISSIONS_PROTOCOL_VERSION is 1", () => {
44
- expect(PERMISSIONS_PROTOCOL_VERSION).toBe(1);
45
- });
46
-
47
- it("channel names have the correct values", () => {
48
- expect(PERMISSIONS_READY_CHANNEL).toBe("permissions:ready");
49
- expect(PERMISSIONS_UI_PROMPT_CHANNEL).toBe("permissions:ui_prompt");
50
- expect(PERMISSIONS_DECISION_CHANNEL).toBe("permissions:decision");
51
- expect(PERMISSIONS_RPC_CHECK_CHANNEL).toBe("permissions:rpc:check");
52
- expect(PERMISSIONS_RPC_PROMPT_CHANNEL).toBe("permissions:rpc:prompt");
53
- });
54
- });
55
-
56
- // ── emitReadyEvent ─────────────────────────────────────────────────────────
57
-
58
- describe("emitReadyEvent", () => {
59
- it("emits an empty payload on the permissions:ready channel", () => {
60
- const bus = makeEventBus();
61
- emitReadyEvent(bus);
62
- expect(bus.emit).toHaveBeenCalledOnce();
63
- expect(bus.emit).toHaveBeenCalledWith("permissions:ready", {});
64
- });
65
-
66
- it("carries no protocolVersion (version lives in the RPC envelope)", () => {
67
- const bus = makeEventBus();
68
- emitReadyEvent(bus);
69
- const payload = bus.emit.mock.calls[0][1] as PermissionsReadyEvent;
70
- expect(payload).not.toHaveProperty("protocolVersion");
71
- });
72
-
73
- it("swallows event bus errors because broadcasts are best-effort", () => {
74
- const bus = {
75
- emit: vi.fn(() => {
76
- throw new Error("listener failed");
77
- }),
78
- on: vi.fn().mockReturnValue(() => undefined),
79
- };
80
-
81
- expect(() => emitReadyEvent(bus)).not.toThrow();
82
- });
83
- });
84
-
85
- // ── emitUiPromptEvent ──────────────────────────────────────────────────────
86
-
87
- describe("emitUiPromptEvent", () => {
88
- function makeUiPromptEvent(
89
- overrides: Partial<PermissionUiPromptEvent> = {},
90
- ): PermissionUiPromptEvent {
91
- return {
92
- requestId: "req-123",
93
- source: "tool_call",
94
- surface: "bash",
95
- value: "git status",
96
- agentName: "Explore",
97
- message: "Allow git status?",
98
- forwarding: null,
99
- ...overrides,
100
- };
101
- }
102
-
103
- it("emits on the permissions:ui_prompt channel", () => {
104
- const bus = makeEventBus();
105
- emitUiPromptEvent(bus, makeUiPromptEvent());
106
- expect(bus.emit).toHaveBeenCalledOnce();
107
- expect(bus.emit.mock.calls[0][0]).toBe("permissions:ui_prompt");
108
- });
109
-
110
- it("forwards the full payload unchanged", () => {
111
- const bus = makeEventBus();
112
- const event = makeUiPromptEvent({
113
- forwarding: { requesterAgentName: "Worker", requesterSessionId: "child" },
114
- });
115
- emitUiPromptEvent(bus, event);
116
- expect(bus.emit.mock.calls[0][1]).toEqual(event);
117
- });
118
-
119
- it("swallows event bus errors because UI prompt broadcasts are observational", () => {
120
- const bus = {
121
- emit: vi.fn(() => {
122
- throw new Error("listener failed");
123
- }),
124
- on: vi.fn().mockReturnValue(() => undefined),
125
- };
126
-
127
- expect(() => emitUiPromptEvent(bus, makeUiPromptEvent())).not.toThrow();
128
- });
129
- });
130
-
131
- // ── emitDecisionEvent ──────────────────────────────────────────────────────
132
-
133
- describe("emitDecisionEvent", () => {
134
- function makeDecisionEvent(
135
- overrides: Partial<PermissionDecisionEvent> = {},
136
- ): PermissionDecisionEvent {
137
- return {
138
- surface: "bash",
139
- value: "git status",
140
- result: "allow",
141
- resolution: "policy_allow",
142
- origin: "global",
143
- agentName: null,
144
- matchedPattern: "*",
145
- ...overrides,
146
- };
147
- }
148
-
149
- it("emits on the permissions:decision channel", () => {
150
- const bus = makeEventBus();
151
- emitDecisionEvent(bus, makeDecisionEvent());
152
- expect(bus.emit).toHaveBeenCalledOnce();
153
- expect(bus.emit.mock.calls[0][0]).toBe("permissions:decision");
154
- });
155
-
156
- it("forwards the full payload unchanged", () => {
157
- const bus = makeEventBus();
158
- const event = makeDecisionEvent({
159
- surface: "mcp",
160
- value: "exa:search",
161
- result: "deny",
162
- resolution: "policy_deny",
163
- origin: "project",
164
- agentName: "Worker",
165
- matchedPattern: "exa:*",
166
- });
167
- emitDecisionEvent(bus, event);
168
- expect(bus.emit.mock.calls[0][1]).toEqual(event);
169
- });
170
-
171
- it("accepts all defined resolution values", () => {
172
- const resolutions: PermissionDecisionEvent["resolution"][] = [
173
- "policy_allow",
174
- "policy_deny",
175
- "session_approved",
176
- "infrastructure_auto_allowed",
177
- "user_approved",
178
- "user_approved_for_session",
179
- "user_denied",
180
- "auto_approved",
181
- "confirmation_unavailable",
182
- ];
183
- const bus = makeEventBus();
184
- for (const resolution of resolutions) {
185
- emitDecisionEvent(bus, makeDecisionEvent({ resolution }));
186
- }
187
- expect(bus.emit).toHaveBeenCalledTimes(resolutions.length);
188
- });
189
-
190
- it("accepts null for optional fields", () => {
191
- const bus = makeEventBus();
192
- emitDecisionEvent(
193
- bus,
194
- makeDecisionEvent({
195
- origin: null,
196
- agentName: null,
197
- matchedPattern: null,
198
- }),
199
- );
200
- const payload = bus.emit.mock.calls[0][1] as PermissionDecisionEvent;
201
- expect(payload.origin).toBeNull();
202
- expect(payload.agentName).toBeNull();
203
- expect(payload.matchedPattern).toBeNull();
204
- });
205
-
206
- it("swallows event bus errors because broadcasts are best-effort", () => {
207
- const bus = {
208
- emit: vi.fn(() => {
209
- throw new Error("listener failed");
210
- }),
211
- on: vi.fn().mockReturnValue(() => undefined),
212
- };
213
-
214
- expect(() => emitDecisionEvent(bus, makeDecisionEvent())).not.toThrow();
215
- });
216
- });
217
-
218
- // ── Type-shape compile-time checks (runtime assertions on literal values) ──
219
-
220
- describe("type shapes (PermissionsRpcReply)", () => {
221
- it("success reply has success=true and protocolVersion", () => {
222
- const reply: PermissionsRpcReply<{ result: "allow" }> = {
223
- success: true,
224
- protocolVersion: PERMISSIONS_PROTOCOL_VERSION,
225
- data: { result: "allow" },
226
- };
227
- expect(reply.success).toBe(true);
228
- expect(reply.protocolVersion).toBe(1);
229
- });
230
-
231
- it("error reply has success=false and error string", () => {
232
- const reply: PermissionsRpcReply = {
233
- success: false,
234
- protocolVersion: PERMISSIONS_PROTOCOL_VERSION,
235
- error: "no_ui",
236
- };
237
- expect(reply.success).toBe(false);
238
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- narrowing on discriminated union
239
- if (!reply.success) {
240
- expect(reply.error).toBe("no_ui");
241
- }
242
- });
243
- });
244
-
245
- describe("type shapes (PermissionsCheckRequest)", () => {
246
- it("minimal request requires requestId and surface", () => {
247
- const req: PermissionsCheckRequest = {
248
- requestId: "abc-123",
249
- surface: "bash",
250
- };
251
- expect(req.requestId).toBe("abc-123");
252
- expect(req.surface).toBe("bash");
253
- });
254
-
255
- it("optional fields are accepted", () => {
256
- const req: PermissionsCheckRequest = {
257
- requestId: "abc-123",
258
- surface: "bash",
259
- value: "git status",
260
- agentName: "Worker",
261
- };
262
- expect(req.value).toBe("git status");
263
- expect(req.agentName).toBe("Worker");
264
- });
265
- });
266
-
267
- describe("type shapes (PermissionsCheckReplyData)", () => {
268
- it("has result, matchedPattern, origin", () => {
269
- const data: PermissionsCheckReplyData = {
270
- result: "ask",
271
- matchedPattern: null,
272
- origin: "builtin",
273
- };
274
- expect(data.result).toBe("ask");
275
- });
276
- });
277
-
278
- describe("type shapes (PermissionsPromptRequest)", () => {
279
- it("minimal request requires requestId, surface, value, message", () => {
280
- const req: PermissionsPromptRequest = {
281
- requestId: "def-456",
282
- surface: "bash",
283
- value: "rm -rf /tmp",
284
- message: "Allow rm -rf /tmp?",
285
- };
286
- expect(req.requestId).toBe("def-456");
287
- });
288
-
289
- it("optional agentName and sessionLabel are accepted", () => {
290
- const req: PermissionsPromptRequest = {
291
- requestId: "def-456",
292
- surface: "bash",
293
- value: "rm -rf /tmp",
294
- message: "Allow rm -rf /tmp?",
295
- agentName: "Explore",
296
- sessionLabel: "Allow rm *",
297
- };
298
- expect(req.agentName).toBe("Explore");
299
- expect(req.sessionLabel).toBe("Allow rm *");
300
- });
301
- });
302
-
303
- describe("type shapes (PermissionsPromptReplyData)", () => {
304
- it("approved reply has approved=true and state", () => {
305
- const data: PermissionsPromptReplyData = {
306
- approved: true,
307
- state: "approved_for_session",
308
- };
309
- expect(data.approved).toBe(true);
310
- expect(data.state).toBe("approved_for_session");
311
- });
312
-
313
- it("denied reply may include denialReason", () => {
314
- const data: PermissionsPromptReplyData = {
315
- approved: false,
316
- state: "denied_with_reason",
317
- denialReason: "Too risky",
318
- };
319
- expect(data.denialReason).toBe("Too risky");
320
- });
321
- });
322
-
323
- // ── piPermissionSystemExtension emits permissions:ready ────────────────────
324
-
325
- describe("piPermissionSystemExtension ready event wiring", () => {
326
- let baseDir: string;
327
- let originalAgentDir: string | undefined;
328
-
329
- beforeEach(() => {
330
- baseDir = mkdtempSync(join(tmpdir(), "pi-perm-events-test-"));
331
- originalAgentDir = process.env.PI_CODING_AGENT_DIR;
332
- const globalConfigPath = getGlobalConfigPath(baseDir);
333
- mkdirSync(dirname(globalConfigPath), { recursive: true });
334
- mkdirSync(join(baseDir, "agents"), { recursive: true });
335
- writeFileSync(
336
- globalConfigPath,
337
- `${JSON.stringify({ permission: { "*": "ask" } })}\n`,
338
- "utf8",
339
- );
340
- process.env.PI_CODING_AGENT_DIR = baseDir;
341
- });
342
-
343
- afterEach(() => {
344
- if (originalAgentDir === undefined) {
345
- delete process.env.PI_CODING_AGENT_DIR;
346
- } else {
347
- process.env.PI_CODING_AGENT_DIR = originalAgentDir;
348
- }
349
- rmSync(baseDir, { recursive: true, force: true });
350
- });
351
-
352
- it("emits permissions:ready at session_start", async () => {
353
- const emitSpy = vi.fn();
354
- const handlers = new Map<
355
- string,
356
- (event: unknown, ctx: unknown) => unknown
357
- >();
358
- piPermissionSystemExtension({
359
- on: vi.fn(
360
- (event: string, handler: (e: unknown, c: unknown) => unknown) => {
361
- handlers.set(event, handler);
362
- },
363
- ),
364
- registerCommand: vi.fn(),
365
- getAllTools: vi.fn().mockReturnValue([]),
366
- getActiveTools: vi.fn().mockReturnValue([]),
367
- setActiveTools: vi.fn(),
368
- registerProvider: vi.fn(),
369
- events: { emit: emitSpy, on: vi.fn().mockReturnValue(() => undefined) },
370
- } as never);
371
-
372
- // ready is not emitted at load — only after session_start publishes.
373
- expect(
374
- emitSpy.mock.calls.filter(([c]) => c === PERMISSIONS_READY_CHANNEL),
375
- ).toHaveLength(0);
376
-
377
- const ctx = {
378
- cwd: baseDir,
379
- hasUI: false,
380
- sessionManager: {
381
- getEntries: (): unknown[] => [],
382
- getSessionId: (): string => "top-session",
383
- getSessionDir: (): string => baseDir,
384
- },
385
- ui: {
386
- notify: (): void => {},
387
- setStatus: (): void => {},
388
- select: async (): Promise<string | undefined> => undefined,
389
- input: async (): Promise<string | undefined> => undefined,
390
- },
391
- };
392
- await handlers.get("session_start")?.({ reason: "start" }, ctx);
393
-
394
- const readyCalls = emitSpy.mock.calls.filter(
395
- ([channel]) => channel === PERMISSIONS_READY_CHANNEL,
396
- );
397
- expect(readyCalls).toHaveLength(1);
398
- expect(readyCalls[0][1]).toEqual({});
399
- });
400
- });