@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,418 +0,0 @@
1
- import { resolve } from "node:path";
2
- import { afterEach, describe, expect, test, vi } from "vitest";
3
- import { PathNormalizer } from "#src/path-normalizer";
4
- import type { ScopedPermissionManager } from "#src/permission-manager";
5
- import {
6
- findSkillPathMatch,
7
- parseAllSkillPromptSections,
8
- resolveSkillPromptEntries,
9
- type SkillPermissionChecker,
10
- } from "#src/skill-prompt-sanitizer";
11
- import type { PermissionCheckResult } from "#src/types";
12
- import { createManager } from "#test/helpers/manager-harness";
13
-
14
- /**
15
- * Adapt a real `PermissionManager` to the raw `SkillPermissionChecker`
16
- * contract, mirroring how `PermissionResolver.checkPermission` delegates to
17
- * `manager.check` with a tool intent (#478).
18
- */
19
- function asChecker(manager: ScopedPermissionManager): SkillPermissionChecker {
20
- return {
21
- checkPermission: (surface, input, agentName) =>
22
- manager.check({ kind: "tool", surface, input, agentName }),
23
- };
24
- }
25
-
26
- afterEach(() => {
27
- vi.restoreAllMocks();
28
- });
29
-
30
- // ── Helpers ────────────────────────────────────────────────────────────────
31
-
32
- const CWD = "/projects/my-app";
33
-
34
- // `findSkillPathMatch` only uses the normalizer's platform (it compares two
35
- // already-absolute paths via `isWithinDirectory`), so this CWD-baked instance
36
- // serves every call regardless of the entries' cwd.
37
- const normalizer = new PathNormalizer("linux", CWD);
38
-
39
- function makeManager(
40
- defaultState: "allow" | "deny" | "ask" = "allow",
41
- overrides: Record<string, "allow" | "deny" | "ask"> = {},
42
- ): SkillPermissionChecker {
43
- return {
44
- checkPermission: vi.fn(
45
- (_surface: string, input: unknown): PermissionCheckResult => {
46
- const name = (input as { name?: string }).name ?? "";
47
- const state = overrides[name] ?? defaultState;
48
- return { toolName: "skill", state, source: "tool", origin: "builtin" };
49
- },
50
- ),
51
- };
52
- }
53
-
54
- function skillBlock(
55
- name: string,
56
- location = `/skills/${name}/SKILL.md`,
57
- ): string {
58
- return [
59
- " <skill>",
60
- ` <name>${name}</name>`,
61
- ` <description>Description of ${name}</description>`,
62
- ` <location>${location}</location>`,
63
- " </skill>",
64
- ].join("\n");
65
- }
66
-
67
- function availableSkillsSection(...names: string[]): string {
68
- return [
69
- "<available_skills>",
70
- ...names.map((n) => skillBlock(n)),
71
- "</available_skills>",
72
- ].join("\n");
73
- }
74
-
75
- // ── resolveSkillPromptEntries ───────────────────────────────────────────────
76
-
77
- describe("resolveSkillPromptEntries", () => {
78
- test("returns unchanged prompt and empty entries when no skills section present", () => {
79
- const input = "You are a helpful assistant.";
80
- const manager = makeManager("allow");
81
- const result = resolveSkillPromptEntries(input, manager, null, normalizer);
82
- expect(result.prompt).toBe(input);
83
- expect(result.entries).toEqual([]);
84
- expect(manager.checkPermission).not.toHaveBeenCalled();
85
- });
86
-
87
- test("keeps all skills when all are allowed", () => {
88
- const input = availableSkillsSection("librarian", "ask-user");
89
- const manager = makeManager("allow");
90
- const result = resolveSkillPromptEntries(input, manager, null, normalizer);
91
- expect(result.prompt).toContain("librarian");
92
- expect(result.prompt).toContain("ask-user");
93
- expect(result.entries).toHaveLength(2);
94
- });
95
-
96
- test("removes denied skill from section", () => {
97
- const input = availableSkillsSection("librarian", "dangerous");
98
- const manager = makeManager("allow", { dangerous: "deny" });
99
- const result = resolveSkillPromptEntries(input, manager, null, normalizer);
100
- expect(result.prompt).toContain("librarian");
101
- expect(result.prompt).not.toContain("dangerous");
102
- // denied skill is excluded from returned entries
103
- expect(result.entries.map((e) => e.name)).not.toContain("dangerous");
104
- });
105
-
106
- test("removes entire section when all skills are denied", () => {
107
- const input = `Intro\n${availableSkillsSection("dangerous")}\nOutro`;
108
- const manager = makeManager("deny");
109
- const result = resolveSkillPromptEntries(input, manager, null, normalizer);
110
- expect(result.prompt).not.toContain("<available_skills>");
111
- expect(result.prompt).toContain("Intro");
112
- expect(result.prompt).toContain("Outro");
113
- expect(result.entries).toHaveLength(0);
114
- });
115
-
116
- test("keeps ask-state skills in section and entries", () => {
117
- const input = availableSkillsSection("librarian");
118
- const manager = makeManager("ask");
119
- const result = resolveSkillPromptEntries(input, manager, null, normalizer);
120
- expect(result.prompt).toContain("librarian");
121
- expect(result.entries).toHaveLength(1);
122
- expect(result.entries[0].state).toBe("ask");
123
- });
124
-
125
- test("delegates permission check to permissionManager for each skill", () => {
126
- const input = availableSkillsSection("alpha", "beta");
127
- const manager = makeManager("allow");
128
- resolveSkillPromptEntries(input, manager, null, normalizer);
129
- expect(manager.checkPermission).toHaveBeenCalledWith(
130
- "skill",
131
- { name: "alpha" },
132
- undefined,
133
- );
134
- expect(manager.checkPermission).toHaveBeenCalledWith(
135
- "skill",
136
- { name: "beta" },
137
- undefined,
138
- );
139
- });
140
-
141
- test("passes agentName to permissionManager", () => {
142
- const input = availableSkillsSection("librarian");
143
- const manager = makeManager("allow");
144
- resolveSkillPromptEntries(input, manager, "my-agent", normalizer);
145
- expect(manager.checkPermission).toHaveBeenCalledWith(
146
- "skill",
147
- { name: "librarian" },
148
- "my-agent",
149
- );
150
- });
151
-
152
- test("caches permission result: checkPermission called once per unique skill name", () => {
153
- // Same skill appears in two separate sections.
154
- const input = [
155
- availableSkillsSection("librarian"),
156
- availableSkillsSection("librarian"),
157
- ].join("\n");
158
- const manager = makeManager("allow");
159
- resolveSkillPromptEntries(input, manager, null, normalizer);
160
- // Should only be called once despite appearing twice.
161
- expect(manager.checkPermission).toHaveBeenCalledTimes(1);
162
- });
163
-
164
- test("resolves entry normalizedLocation relative to cwd", () => {
165
- const location = "/skills/librarian/SKILL.md";
166
- const input = availableSkillsSection("librarian");
167
- const manager = makeManager("allow");
168
- const result = resolveSkillPromptEntries(input, manager, null, normalizer);
169
- expect(result.entries[0].normalizedLocation).toBe(location);
170
- expect(result.entries[0].normalizedBaseDir).toBe("/skills/librarian");
171
- });
172
-
173
- test("handles multi-section prompt: processes each section independently", () => {
174
- const section1 = availableSkillsSection("alpha");
175
- const section2 = availableSkillsSection("beta");
176
- const input = `${section1}\n${section2}`;
177
- const manager = makeManager("allow", { beta: "deny" });
178
- const result = resolveSkillPromptEntries(input, manager, null, normalizer);
179
- expect(result.entries.map((e) => e.name)).toContain("alpha");
180
- expect(result.entries.map((e) => e.name)).not.toContain("beta");
181
- });
182
- });
183
-
184
- // ── findSkillPathMatch ──────────────────────────────────────────────────────
185
-
186
- describe("findSkillPathMatch", () => {
187
- const entries = [
188
- {
189
- name: "librarian",
190
- description: "desc",
191
- location: "/skills/librarian/SKILL.md",
192
- state: "allow" as const,
193
- normalizedLocation: "/skills/librarian/SKILL.md",
194
- normalizedBaseDir: "/skills/librarian",
195
- },
196
- {
197
- name: "ask-user",
198
- description: "desc",
199
- location: "/skills/ask-user/SKILL.md",
200
- state: "allow" as const,
201
- normalizedLocation: "/skills/ask-user/SKILL.md",
202
- normalizedBaseDir: "/skills/ask-user",
203
- },
204
- ];
205
-
206
- test("returns null for empty normalized path", () => {
207
- expect(findSkillPathMatch("", entries, normalizer)).toBeNull();
208
- });
209
-
210
- test("returns null for empty entries array", () => {
211
- expect(
212
- findSkillPathMatch("/skills/librarian/SKILL.md", [], normalizer),
213
- ).toBeNull();
214
- });
215
-
216
- test("matches exact location path", () => {
217
- const match = findSkillPathMatch(
218
- "/skills/librarian/SKILL.md",
219
- entries,
220
- normalizer,
221
- );
222
- expect(match?.name).toBe("librarian");
223
- });
224
-
225
- test("matches path within skill base directory", () => {
226
- const match = findSkillPathMatch(
227
- "/skills/librarian/extra/helper.md",
228
- entries,
229
- normalizer,
230
- );
231
- expect(match?.name).toBe("librarian");
232
- });
233
-
234
- test("returns null for path not within any skill directory", () => {
235
- const match = findSkillPathMatch(
236
- "/other/path/file.md",
237
- entries,
238
- normalizer,
239
- );
240
- expect(match).toBeNull();
241
- });
242
-
243
- test("returns null for sibling path that shares a prefix", () => {
244
- // "/skills/librarian-extra" should not match "/skills/librarian"
245
- const match = findSkillPathMatch(
246
- "/skills/librarian-extra/SKILL.md",
247
- entries,
248
- normalizer,
249
- );
250
- expect(match).toBeNull();
251
- });
252
-
253
- test("prefers longer matching base directory (most specific skill wins)", () => {
254
- const nestedEntries = [
255
- {
256
- name: "parent",
257
- description: "desc",
258
- location: "/skills/parent/SKILL.md",
259
- state: "allow" as const,
260
- normalizedLocation: "/skills/parent/SKILL.md",
261
- normalizedBaseDir: "/skills/parent",
262
- },
263
- {
264
- name: "child",
265
- description: "desc",
266
- location: "/skills/parent/child/SKILL.md",
267
- state: "allow" as const,
268
- normalizedLocation: "/skills/parent/child/SKILL.md",
269
- normalizedBaseDir: "/skills/parent/child",
270
- },
271
- ];
272
- const match = findSkillPathMatch(
273
- "/skills/parent/child/helper.md",
274
- nestedEntries,
275
- normalizer,
276
- );
277
- expect(match?.name).toBe("child");
278
- });
279
- });
280
-
281
- // ---------------------------------------------------------------------------
282
- // Moved from permission-system.test.ts catch-all (#342)
283
- // ---------------------------------------------------------------------------
284
-
285
- test("parseAllSkillPromptSections finds every available_skills block", () => {
286
- const prompt = [
287
- "Some preamble",
288
- "<available_skills>",
289
- " <skill>",
290
- " <name>skill-one</name>",
291
- " <description>First skill</description>",
292
- " <location>/path/to/one</location>",
293
- " </skill>",
294
- "</available_skills>",
295
- "Some content between",
296
- "<available_skills>",
297
- " <skill>",
298
- " <name>skill-two</name>",
299
- " <description>Second skill</description>",
300
- " <location>/path/to/two</location>",
301
- " </skill>",
302
- "</available_skills>",
303
- "Footer",
304
- ].join("\n");
305
-
306
- const sections = parseAllSkillPromptSections(prompt);
307
-
308
- expect(sections.length).toBe(2);
309
- expect(sections[0].entries[0]?.name).toBe("skill-one");
310
- expect(sections[1].entries[0]?.name).toBe("skill-two");
311
- });
312
-
313
- test("REGRESSION: resolveSkillPromptEntries sanitizes every available_skills block", () => {
314
- const { manager, cleanup } = createManager({
315
- permission: {
316
- "*": "ask",
317
- skill: { "denied-skill": "deny" },
318
- },
319
- });
320
-
321
- try {
322
- const prompt = [
323
- "System prompt start",
324
- "<available_skills>",
325
- " <skill>",
326
- " <name>visible-skill</name>",
327
- " <description>Allowed skill</description>",
328
- " <location>/skills/visible/index.ts</location>",
329
- " </skill>",
330
- " <skill>",
331
- " <name>denied-skill</name>",
332
- " <description>Denied in first block</description>",
333
- " <location>/skills/blocked/one.ts</location>",
334
- " </skill>",
335
- "</available_skills>",
336
- "Agent identity section",
337
- "<available_skills>",
338
- " <skill>",
339
- " <name>denied-skill</name>",
340
- " <description>Denied in second block</description>",
341
- " <location>/skills/blocked/two.ts</location>",
342
- " </skill>",
343
- "</available_skills>",
344
- "System prompt end",
345
- ].join("\n");
346
-
347
- const result = resolveSkillPromptEntries(
348
- prompt,
349
- asChecker(manager),
350
- null,
351
- new PathNormalizer("linux", "/cwd"),
352
- );
353
-
354
- expect(result.prompt).not.toContain("denied-skill");
355
- expect(result.prompt).toContain("visible-skill");
356
- expect((result.prompt.match(/<available_skills>/g) ?? []).length).toBe(1);
357
- expect(result.entries.map((entry) => entry.name)).toEqual([
358
- "visible-skill",
359
- ]);
360
- } finally {
361
- cleanup();
362
- }
363
- });
364
-
365
- test("REGRESSION: resolveSkillPromptEntries keeps only visible skills available for path matching", () => {
366
- const { manager, cleanup } = createManager({
367
- permission: {
368
- "*": "ask",
369
- skill: { "blocked-skill": "deny" },
370
- },
371
- });
372
-
373
- try {
374
- const prompt = [
375
- "System prompt start",
376
- "<available_skills>",
377
- " <skill>",
378
- " <name>blocked-skill</name>",
379
- " <description>Blocked skill</description>",
380
- " <location>@./skills/blocked/entry.ts</location>",
381
- " </skill>",
382
- "</available_skills>",
383
- "Middle section",
384
- "<available_skills>",
385
- " <skill>",
386
- " <name>visible-skill</name>",
387
- " <description>Visible skill</description>",
388
- " <location>@./skills/visible/entry.ts</location>",
389
- " </skill>",
390
- "</available_skills>",
391
- "System prompt end",
392
- ].join("\n");
393
-
394
- const result = resolveSkillPromptEntries(
395
- prompt,
396
- asChecker(manager),
397
- null,
398
- new PathNormalizer("linux", "/cwd"),
399
- );
400
- const visiblePath = resolve("/cwd", "./skills/visible/file.ts");
401
- const blockedPath = resolve("/cwd", "./skills/blocked/file.ts");
402
- const matchedVisibleSkill = findSkillPathMatch(
403
- process.platform === "win32" ? visiblePath.toLowerCase() : visiblePath,
404
- result.entries,
405
- normalizer,
406
- );
407
- const matchedBlockedSkill = findSkillPathMatch(
408
- process.platform === "win32" ? blockedPath.toLowerCase() : blockedPath,
409
- result.entries,
410
- normalizer,
411
- );
412
-
413
- expect(matchedVisibleSkill?.name).toBe("visible-skill");
414
- expect(matchedBlockedSkill).toBe(null);
415
- } finally {
416
- cleanup();
417
- }
418
- });
@@ -1,10 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
3
- import { getPermissionSystemStatus } from "#src/status";
4
-
5
- test("Permission-system status is only exposed when yolo mode is enabled", () => {
6
- expect(getPermissionSystemStatus(DEFAULT_EXTENSION_CONFIG)).toBe(undefined);
7
- expect(
8
- getPermissionSystemStatus({ ...DEFAULT_EXTENSION_CONFIG, yoloMode: true }),
9
- ).toBe("yolo");
10
- });