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