@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,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
- });