@gotgenes/pi-permission-system 18.1.1 → 18.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/CHANGELOG.md +16 -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/path-normalizer.ts +105 -2
  8. package/test/access-intent/access-path.test.ts +0 -277
  9. package/test/access-intent/bash/node-text.test.ts +0 -148
  10. package/test/access-intent/bash/parser.test.ts +0 -19
  11. package/test/access-intent/bash/program.test.ts +0 -673
  12. package/test/access-intent/bash/token-classification.test.ts +0 -363
  13. package/test/access-intent/bash/token-collection.test.ts +0 -300
  14. package/test/active-agent.test.ts +0 -155
  15. package/test/async-cache.test.ts +0 -48
  16. package/test/bash-arity.test.ts +0 -144
  17. package/test/bash-external-directory.test.ts +0 -1022
  18. package/test/builtin-tool-input-formatters.test.ts +0 -109
  19. package/test/canonicalize-path.test.ts +0 -119
  20. package/test/composition-root.test.ts +0 -698
  21. package/test/config-loader.test.ts +0 -740
  22. package/test/config-modal.test.ts +0 -320
  23. package/test/config-paths.test.ts +0 -83
  24. package/test/config-pipeline.test.ts +0 -90
  25. package/test/config-reporter.test.ts +0 -147
  26. package/test/config-store.test.ts +0 -466
  27. package/test/decision-audit.test.ts +0 -72
  28. package/test/decision-reporter.test.ts +0 -112
  29. package/test/denial-messages.test.ts +0 -714
  30. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  31. package/test/expand-home.test.ts +0 -93
  32. package/test/extension-config.test.ts +0 -129
  33. package/test/extension-paths.test.ts +0 -108
  34. package/test/forwarded-permissions/io.test.ts +0 -251
  35. package/test/forwarding-manager.test.ts +0 -200
  36. package/test/handlers/before-agent-start.test.ts +0 -314
  37. package/test/handlers/external-directory-integration.test.ts +0 -515
  38. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  39. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  40. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  41. package/test/handlers/gates/bash-command.test.ts +0 -257
  42. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  43. package/test/handlers/gates/bash-path.test.ts +0 -346
  44. package/test/handlers/gates/candidate-check.test.ts +0 -52
  45. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  46. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  47. package/test/handlers/gates/external-directory.test.ts +0 -267
  48. package/test/handlers/gates/helpers.test.ts +0 -165
  49. package/test/handlers/gates/path.test.ts +0 -369
  50. package/test/handlers/gates/runner.test.ts +0 -408
  51. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  52. package/test/handlers/gates/skill-input.test.ts +0 -128
  53. package/test/handlers/gates/skill-read.test.ts +0 -161
  54. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  55. package/test/handlers/gates/tool.test.ts +0 -244
  56. package/test/handlers/input-events.test.ts +0 -168
  57. package/test/handlers/input.test.ts +0 -199
  58. package/test/handlers/lifecycle.test.ts +0 -221
  59. package/test/handlers/tool-call-boundary.test.ts +0 -145
  60. package/test/handlers/tool-call-events.test.ts +0 -277
  61. package/test/handlers/tool-call.test.ts +0 -395
  62. package/test/handlers/validate-requested-tool.test.ts +0 -92
  63. package/test/helpers/external-directory-fixtures.ts +0 -269
  64. package/test/helpers/gate-fixtures.ts +0 -316
  65. package/test/helpers/handler-fixtures.ts +0 -335
  66. package/test/helpers/make-fake-pi.ts +0 -100
  67. package/test/helpers/manager-harness.ts +0 -112
  68. package/test/helpers/session-fixtures.ts +0 -199
  69. package/test/input-normalizer.test.ts +0 -325
  70. package/test/logging.test.ts +0 -51
  71. package/test/mcp-targets.test.ts +0 -233
  72. package/test/node-modules-discovery.test.ts +0 -97
  73. package/test/normalize.test.ts +0 -247
  74. package/test/path-containment.test.ts +0 -161
  75. package/test/path-normalization.test.ts +0 -233
  76. package/test/path-normalizer.test.ts +0 -196
  77. package/test/path-surfaces.test.ts +0 -55
  78. package/test/pattern-suggest.test.ts +0 -248
  79. package/test/permission-dialog.test.ts +0 -205
  80. package/test/permission-event-rpc.test.ts +0 -560
  81. package/test/permission-events.test.ts +0 -400
  82. package/test/permission-forwarder.test.ts +0 -370
  83. package/test/permission-forwarding.test.ts +0 -315
  84. package/test/permission-gate.test.ts +0 -269
  85. package/test/permission-manager-unified.test.ts +0 -3714
  86. package/test/permission-merge.test.ts +0 -61
  87. package/test/permission-prompter.test.ts +0 -518
  88. package/test/permission-prompts.test.ts +0 -363
  89. package/test/permission-resolver.test.ts +0 -277
  90. package/test/permission-session.test.ts +0 -404
  91. package/test/permission-ui-prompt.test.ts +0 -146
  92. package/test/permissions-service.test.ts +0 -192
  93. package/test/pi-infrastructure-read.test.ts +0 -432
  94. package/test/policy-loader.test.ts +0 -561
  95. package/test/prompting-gateway.test.ts +0 -231
  96. package/test/rule.test.ts +0 -650
  97. package/test/safe-system-paths.test.ts +0 -46
  98. package/test/scope-merge.test.ts +0 -116
  99. package/test/service-lifecycle.test.ts +0 -163
  100. package/test/service.test.ts +0 -261
  101. package/test/session-approval.test.ts +0 -75
  102. package/test/session-logger.test.ts +0 -200
  103. package/test/session-rules.test.ts +0 -321
  104. package/test/session-start.test.ts +0 -112
  105. package/test/skill-prompt-sanitizer.test.ts +0 -418
  106. package/test/status.test.ts +0 -10
  107. package/test/subagent-context.test.ts +0 -372
  108. package/test/subagent-lifecycle-events.test.ts +0 -132
  109. package/test/subagent-registry.test.ts +0 -145
  110. package/test/synthesize.test.ts +0 -302
  111. package/test/system-prompt-sanitizer.test.ts +0 -382
  112. package/test/tool-access-extractor-registry.test.ts +0 -77
  113. package/test/tool-input-formatter-registry.test.ts +0 -75
  114. package/test/tool-input-path.test.ts +0 -84
  115. package/test/tool-input-preview.test.ts +0 -129
  116. package/test/tool-input-prompt-formatters.test.ts +0 -115
  117. package/test/tool-preview-formatter.test.ts +0 -458
  118. package/test/tool-registry.test.ts +0 -197
  119. package/test/value-guards.test.ts +0 -193
  120. package/test/wildcard-matcher.test.ts +0 -424
  121. package/test/yaml-frontmatter.test.ts +0 -91
  122. 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
- });