@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,363 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import {
3
- formatAskPrompt,
4
- formatMissingToolNameReason,
5
- formatSkillAskPrompt,
6
- formatSkillPathAskPrompt,
7
- formatUnknownToolReason,
8
- } from "#src/permission-prompts";
9
- import type { SkillPromptEntry } from "#src/skill-prompt-sanitizer";
10
- import type { ToolInputFormatterLookup } from "#src/tool-input-formatter-registry";
11
- import {
12
- TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
13
- TOOL_INPUT_PREVIEW_MAX_LENGTH,
14
- TOOL_TEXT_SUMMARY_MAX_LENGTH,
15
- } from "#src/tool-input-preview";
16
- import { ToolPreviewFormatter } from "#src/tool-preview-formatter";
17
- import type { PermissionCheckResult } from "#src/types";
18
-
19
- function makeFormatter(
20
- lookup?: ToolInputFormatterLookup,
21
- ): ToolPreviewFormatter {
22
- return new ToolPreviewFormatter(
23
- {
24
- toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
25
- toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
26
- toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
27
- },
28
- lookup,
29
- );
30
- }
31
-
32
- function makeMcpLookup(preview: string): ToolInputFormatterLookup {
33
- return { get: (name) => (name === "mcp" ? () => preview : undefined) };
34
- }
35
-
36
- function toolResult(
37
- toolName: string,
38
- overrides: Partial<PermissionCheckResult> = {},
39
- ): PermissionCheckResult {
40
- return {
41
- toolName,
42
- state: "ask",
43
- source: "tool",
44
- origin: "builtin",
45
- ...overrides,
46
- };
47
- }
48
-
49
- function mcpResult(
50
- target: string,
51
- overrides: Partial<PermissionCheckResult> = {},
52
- ): PermissionCheckResult {
53
- return {
54
- toolName: "mcp",
55
- target,
56
- state: "ask",
57
- source: "tool",
58
- origin: "builtin",
59
- ...overrides,
60
- };
61
- }
62
-
63
- function skillEntry(name: string): SkillPromptEntry {
64
- return {
65
- name,
66
- description: "A skill",
67
- location: `/skills/${name}/SKILL.md`,
68
- state: "ask",
69
- normalizedLocation: `/skills/${name}/SKILL.md`,
70
- normalizedBaseDir: `/skills/${name}`,
71
- };
72
- }
73
-
74
- describe("formatMissingToolNameReason", () => {
75
- test("mentions missing tool name and pi.getAllTools()", () => {
76
- const result = formatMissingToolNameReason();
77
- expect(result).toContain("no tool name");
78
- expect(result).toContain("pi.getAllTools()");
79
- });
80
- });
81
-
82
- describe("formatUnknownToolReason", () => {
83
- test("mentions the unknown tool name and lists available tools", () => {
84
- const result = formatUnknownToolReason("phantom", ["read", "write"]);
85
- expect(result).toContain("phantom");
86
- expect(result).toContain("read");
87
- expect(result).toContain("write");
88
- });
89
-
90
- test("includes MCP hint for non-mcp tool names", () => {
91
- const result = formatUnknownToolReason("my-server:tool", ["mcp"]);
92
- expect(result).toContain("mcp");
93
- });
94
-
95
- test("omits MCP hint when tool name is 'mcp'", () => {
96
- const result = formatUnknownToolReason("mcp", []);
97
- expect(result).not.toContain("call the registered 'mcp' tool");
98
- });
99
-
100
- test("shows 'none' when no tools are registered", () => {
101
- const result = formatUnknownToolReason("ghost", []);
102
- expect(result).toContain("none");
103
- });
104
-
105
- test("caps preview at 10 tools and appends ellipsis for longer lists", () => {
106
- const tools = Array.from({ length: 15 }, (_, i) => `tool${i}`);
107
- const result = formatUnknownToolReason("ghost", tools);
108
- expect(result).toContain("...");
109
- });
110
- });
111
-
112
- describe("formatAskPrompt", () => {
113
- test("uses 'Current agent' when no agent name given", () => {
114
- const result = formatAskPrompt(
115
- toolResult("read"),
116
- undefined,
117
- { path: "/src" },
118
- makeFormatter(),
119
- );
120
- expect(result).toContain("Current agent");
121
- });
122
-
123
- test("uses agent name when provided", () => {
124
- const result = formatAskPrompt(
125
- toolResult("read"),
126
- "my-agent",
127
- { path: "/src" },
128
- makeFormatter(),
129
- );
130
- expect(result).toContain("Agent 'my-agent'");
131
- });
132
-
133
- test("formats bash prompt with command and does not use formatter", () => {
134
- const result = formatAskPrompt(
135
- toolResult("bash", { command: "git status" }),
136
- undefined,
137
- undefined,
138
- makeFormatter(),
139
- );
140
- expect(result).toContain("git status");
141
- expect(result).toContain("Allow this command?");
142
- });
143
-
144
- test("formats bash prompt with matched pattern", () => {
145
- const result = formatAskPrompt(
146
- toolResult("bash", { command: "git push", matchedPattern: "git *" }),
147
- undefined,
148
- undefined,
149
- makeFormatter(),
150
- );
151
- expect(result).toContain("matched 'git *'");
152
- });
153
-
154
- test("appends full command when input contains a chain that differs from the sub-command", () => {
155
- const result = formatAskPrompt(
156
- toolResult("bash", { command: "rm -rf ." }),
157
- undefined,
158
- { command: 'echo "hello" && rm -rf .' },
159
- makeFormatter(),
160
- );
161
- expect(result).toBe(
162
- `Current agent requested bash command 'rm -rf .' (full command: 'echo "hello" && rm -rf .'). Allow this command?`,
163
- );
164
- });
165
-
166
- test("suppresses full-command suffix when input command matches the sub-command (no chain)", () => {
167
- const result = formatAskPrompt(
168
- toolResult("bash", { command: "git push" }),
169
- undefined,
170
- { command: "git push" },
171
- makeFormatter(),
172
- );
173
- expect(result).not.toContain("full command:");
174
- expect(result).toBe(
175
- "Current agent requested bash command 'git push'. Allow this command?",
176
- );
177
- });
178
-
179
- test("suppresses full-command suffix when input is undefined", () => {
180
- const result = formatAskPrompt(
181
- toolResult("bash", { command: "git push" }),
182
- undefined,
183
- undefined,
184
- makeFormatter(),
185
- );
186
- expect(result).not.toContain("full command:");
187
- });
188
-
189
- test("suppresses full-command suffix when input has no command field", () => {
190
- const result = formatAskPrompt(
191
- toolResult("bash", { command: "git push" }),
192
- undefined,
193
- { unrelated: "value" },
194
- makeFormatter(),
195
- );
196
- expect(result).not.toContain("full command:");
197
- });
198
-
199
- test("suppresses full-command suffix when input command is empty", () => {
200
- const result = formatAskPrompt(
201
- toolResult("bash", { command: "git push" }),
202
- undefined,
203
- { command: "" },
204
- makeFormatter(),
205
- );
206
- expect(result).not.toContain("full command:");
207
- });
208
-
209
- test("places full-command suffix after the qualifier and before the terminal sentence", () => {
210
- const result = formatAskPrompt(
211
- toolResult("bash", { command: "rm -rf foo", matchedPattern: "rm *" }),
212
- undefined,
213
- { command: "cd /tmp && rm -rf foo" },
214
- makeFormatter(),
215
- );
216
- expect(result).toBe(
217
- "Current agent requested bash command 'rm -rf foo' (matched 'rm *') (full command: 'cd /tmp && rm -rf foo'). Allow this command?",
218
- );
219
- });
220
-
221
- test("formats bash prompt with nested execution context", () => {
222
- const result = formatAskPrompt(
223
- toolResult("bash", {
224
- command: "rm -rf foo",
225
- matchedPattern: "rm *",
226
- commandContext: "command_substitution",
227
- }),
228
- undefined,
229
- undefined,
230
- makeFormatter(),
231
- );
232
- expect(result).toContain(
233
- "bash command 'rm -rf foo' (matched 'rm *', inside command substitution).",
234
- );
235
- });
236
-
237
- test("formats MCP prompt with target", () => {
238
- const result = formatAskPrompt(
239
- mcpResult("server:query"),
240
- undefined,
241
- undefined,
242
- makeFormatter(),
243
- );
244
- expect(result).toContain("server:query");
245
- expect(result).toContain("Allow this call?");
246
- });
247
-
248
- test("formats MCP prompt with matched pattern", () => {
249
- const result = formatAskPrompt(
250
- mcpResult("server:query", { matchedPattern: "server:*" }),
251
- undefined,
252
- undefined,
253
- makeFormatter(),
254
- );
255
- expect(result).toContain("matched 'server:*'");
256
- });
257
-
258
- test("appends MCP argument summary when the formatter has an mcp formatter registered", () => {
259
- const result = formatAskPrompt(
260
- mcpResult("exa:search"),
261
- undefined,
262
- { tool: "exa:search", arguments: { query: "typescript" } },
263
- makeFormatter(makeMcpLookup('with query: "typescript"')),
264
- );
265
- expect(result).toContain("exa:search");
266
- expect(result).toContain('with query: "typescript"');
267
- expect(result).toContain("Allow this call?");
268
- });
269
-
270
- test("MCP prompt is unchanged when the formatter returns undefined (no arguments)", () => {
271
- const noArgsLookup: ToolInputFormatterLookup = {
272
- get: (name) => (name === "mcp" ? () => undefined : undefined),
273
- };
274
- const result = formatAskPrompt(
275
- mcpResult("exa:search"),
276
- undefined,
277
- { tool: "exa:search" },
278
- makeFormatter(noArgsLookup),
279
- );
280
- expect(result).toContain("exa:search");
281
- expect(result).not.toMatch(/with /);
282
- expect(result).toContain("Allow this call?");
283
- });
284
-
285
- test("MCP prompt is unchanged when no formatter is provided", () => {
286
- const result = formatAskPrompt(mcpResult("exa:search"), undefined, {
287
- tool: "exa:search",
288
- arguments: { query: "test" },
289
- });
290
- expect(result).toContain("exa:search");
291
- expect(result).not.toMatch(/with /);
292
- expect(result).toContain("Allow this call?");
293
- });
294
-
295
- test("includes real input preview for non-bash non-mcp tools", () => {
296
- const result = formatAskPrompt(
297
- toolResult("read"),
298
- undefined,
299
- { path: "/src/foo.ts" },
300
- makeFormatter(),
301
- );
302
- expect(result).toContain("path '/src/foo.ts'");
303
- expect(result).toContain("Allow this call?");
304
- });
305
-
306
- test("omits input suffix when formatter returns empty string for input", () => {
307
- const result = formatAskPrompt(
308
- toolResult("task"),
309
- undefined,
310
- {},
311
- makeFormatter(),
312
- );
313
- expect(result).toContain("task");
314
- expect(result).not.toContain("undefined");
315
- });
316
-
317
- test("omits input suffix when no formatter provided", () => {
318
- const result = formatAskPrompt(toolResult("task"), undefined, {
319
- path: "/src",
320
- });
321
- expect(result).toContain("task");
322
- expect(result).not.toContain("undefined");
323
- expect(result).toContain("Allow this call?");
324
- });
325
- });
326
-
327
- describe("formatSkillAskPrompt", () => {
328
- test("includes skill name and agent name", () => {
329
- const result = formatSkillAskPrompt("librarian", "my-agent");
330
- expect(result).toContain("librarian");
331
- expect(result).toContain("Agent 'my-agent'");
332
- });
333
-
334
- test("uses 'Current agent' without agent name", () => {
335
- const result = formatSkillAskPrompt("librarian");
336
- expect(result).toContain("Current agent");
337
- expect(result).toContain("librarian");
338
- });
339
- });
340
-
341
- describe("formatSkillPathAskPrompt", () => {
342
- test("includes skill name, read path, and agent name", () => {
343
- const result = formatSkillPathAskPrompt(
344
- skillEntry("librarian"),
345
- "/skills/librarian/SKILL.md",
346
- "my-agent",
347
- );
348
- expect(result).toContain("librarian");
349
- expect(result).toContain("/skills/librarian/SKILL.md");
350
- expect(result).toContain("Agent 'my-agent'");
351
- });
352
-
353
- test("uses 'Current agent' without agent name", () => {
354
- const result = formatSkillPathAskPrompt(
355
- skillEntry("librarian"),
356
- "/skills/librarian/SKILL.md",
357
- );
358
- expect(result).toContain("Current agent");
359
- });
360
- });
361
-
362
- // formatSkillPathDenyReason has moved to denial-messages.ts.
363
- // Its behavior is tested in denial-messages.test.ts.
@@ -1,277 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
- import type { ResolvedAccessIntent } from "#src/access-intent/access-intent";
3
- import { AccessPath } from "#src/access-intent/access-path";
4
- import type { ScopedPermissionManager } from "#src/permission-manager";
5
- import { PermissionResolver } from "#src/permission-resolver";
6
- import type { Ruleset } from "#src/rule";
7
- import { SessionApproval } from "#src/session-approval";
8
- import { SessionRules } from "#src/session-rules";
9
- import type {
10
- PathRuleTokenMatcher,
11
- PermissionCheckResult,
12
- PermissionState,
13
- } from "#src/types";
14
-
15
- function makePermissionManager() {
16
- return {
17
- configureForCwd: vi.fn<(cwd: string | undefined | null) => void>(),
18
- check: vi
19
- .fn<
20
- (
21
- intent: ResolvedAccessIntent,
22
- sessionRules?: Ruleset,
23
- ) => PermissionCheckResult
24
- >()
25
- .mockReturnValue({
26
- state: "allow",
27
- toolName: "read",
28
- source: "tool",
29
- origin: "builtin",
30
- }),
31
- getToolPermission: vi
32
- .fn<(toolName: string, agentName?: string) => PermissionState>()
33
- .mockReturnValue("allow"),
34
- getConfigIssues: vi.fn((): string[] => []),
35
- getPromotablePathTokenMatcher: vi
36
- .fn<(agentName?: string) => PathRuleTokenMatcher>()
37
- .mockReturnValue(() => false),
38
- };
39
- }
40
-
41
- function makeResolver(
42
- pm?: ScopedPermissionManager,
43
- sessionRules?: Pick<SessionRules, "getRuleset">,
44
- ) {
45
- const permissionManager = pm ?? makePermissionManager();
46
- const rules = sessionRules ?? new SessionRules();
47
- return {
48
- resolver: new PermissionResolver(permissionManager, rules),
49
- permissionManager,
50
- };
51
- }
52
-
53
- beforeEach(() => {
54
- // no module-level vi.fn() stubs to reset
55
- });
56
-
57
- describe("PermissionResolver", () => {
58
- describe("resolve — tool intent", () => {
59
- it("forwards a tool intent with the empty session ruleset", () => {
60
- const { resolver, permissionManager } = makeResolver();
61
-
62
- resolver.resolve({
63
- kind: "tool",
64
- surface: "bash",
65
- input: { command: "ls" },
66
- agentName: "agent-x",
67
- });
68
-
69
- expect(permissionManager.check).toHaveBeenCalledWith(
70
- {
71
- kind: "tool",
72
- surface: "bash",
73
- input: { command: "ls" },
74
- agentName: "agent-x",
75
- },
76
- [],
77
- );
78
- });
79
-
80
- it("applies a recorded session approval on the next resolve", () => {
81
- const pm = makePermissionManager();
82
- const sessionRules = new SessionRules();
83
- const { resolver } = makeResolver(pm, sessionRules);
84
-
85
- sessionRules.recordSessionApproval(
86
- SessionApproval.single("bash", "git *"),
87
- );
88
- resolver.resolve({
89
- kind: "tool",
90
- surface: "bash",
91
- input: { command: "git status" },
92
- });
93
-
94
- const passedRules = vi.mocked(pm.check).mock.calls[0][1];
95
- expect(passedRules).toHaveLength(1);
96
- expect(passedRules?.[0]).toMatchObject({
97
- surface: "bash",
98
- pattern: "git *",
99
- action: "allow",
100
- });
101
- });
102
-
103
- it("returns the manager's check result", () => {
104
- const pm = makePermissionManager();
105
- vi.mocked(pm.check).mockReturnValue({
106
- state: "deny",
107
- toolName: "bash",
108
- source: "bash",
109
- origin: "global",
110
- matchedPattern: "rm *",
111
- });
112
- const { resolver } = makeResolver(pm);
113
-
114
- const result = resolver.resolve({
115
- kind: "tool",
116
- surface: "bash",
117
- input: { command: "rm -rf /" },
118
- });
119
-
120
- expect(result).toEqual({
121
- state: "deny",
122
- toolName: "bash",
123
- source: "bash",
124
- origin: "global",
125
- matchedPattern: "rm *",
126
- });
127
- });
128
- });
129
-
130
- describe("resolve — session ruleset threading", () => {
131
- it("applies a recorded session approval on the next call", () => {
132
- const pm = makePermissionManager();
133
- const sessionRules = new SessionRules();
134
- const { resolver } = makeResolver(pm, sessionRules);
135
-
136
- sessionRules.recordSessionApproval(
137
- SessionApproval.single("path", "src/*"),
138
- );
139
- resolver.resolve({
140
- kind: "access-path",
141
- surface: "path",
142
- path: AccessPath.forPath("src/a.ts", {
143
- cwd: "/proj",
144
- platform: "linux",
145
- }),
146
- });
147
-
148
- const passedRules = vi.mocked(pm.check).mock.calls[0][1];
149
- expect(passedRules).toHaveLength(1);
150
- expect(passedRules?.[0]).toMatchObject({
151
- surface: "path",
152
- pattern: "src/*",
153
- action: "allow",
154
- });
155
- });
156
- });
157
-
158
- describe("resolve — access-path intent", () => {
159
- it("unwraps the AccessPath via matchValues() into a path-values intent", () => {
160
- const { resolver, permissionManager } = makeResolver();
161
- const accessPath = AccessPath.forPath("/tmp/x", {
162
- cwd: "/workspace",
163
- platform: "linux",
164
- });
165
-
166
- resolver.resolve({
167
- kind: "access-path",
168
- surface: "external_directory",
169
- path: accessPath,
170
- agentName: "agent-x",
171
- });
172
-
173
- expect(permissionManager.check).toHaveBeenCalledWith(
174
- {
175
- kind: "path-values",
176
- surface: "external_directory",
177
- values: accessPath.matchValues(),
178
- agentName: "agent-x",
179
- },
180
- [],
181
- );
182
- });
183
-
184
- it("returns the manager's check result for an access-path intent", () => {
185
- const pm = makePermissionManager();
186
- vi.mocked(pm.check).mockReturnValue({
187
- state: "deny",
188
- toolName: "external_directory",
189
- source: "special",
190
- origin: "global",
191
- matchedPattern: "/tmp/*",
192
- });
193
- const { resolver } = makeResolver(pm);
194
- const accessPath = AccessPath.forPath("/tmp/x", {
195
- cwd: "/workspace",
196
- platform: "linux",
197
- });
198
-
199
- const result = resolver.resolve({
200
- kind: "access-path",
201
- surface: "external_directory",
202
- path: accessPath,
203
- });
204
-
205
- expect(result.state).toBe("deny");
206
- expect(result.matchedPattern).toBe("/tmp/*");
207
- });
208
- });
209
-
210
- describe("checkPermission (raw, off-interface)", () => {
211
- it("delegates to manager.check as a tool intent without session rules", () => {
212
- const { resolver, permissionManager } = makeResolver();
213
-
214
- resolver.checkPermission("bash", { command: "ls" }, "agent-1");
215
-
216
- expect(permissionManager.check).toHaveBeenCalledWith(
217
- {
218
- kind: "tool",
219
- surface: "bash",
220
- input: { command: "ls" },
221
- agentName: "agent-1",
222
- },
223
- undefined,
224
- );
225
- });
226
-
227
- it("passes optional sessionRules as the second arg to check", () => {
228
- const { resolver, permissionManager } = makeResolver();
229
- const extraRules: Ruleset = [
230
- { surface: "bash", pattern: "*", action: "allow", origin: "session" },
231
- ];
232
-
233
- resolver.checkPermission(
234
- "bash",
235
- { command: "ls" },
236
- undefined,
237
- extraRules,
238
- );
239
-
240
- expect(permissionManager.check).toHaveBeenCalledWith(
241
- {
242
- kind: "tool",
243
- surface: "bash",
244
- input: { command: "ls" },
245
- agentName: undefined,
246
- },
247
- extraRules,
248
- );
249
- });
250
- });
251
-
252
- describe("getToolPermission", () => {
253
- it("delegates to permissionManager.getToolPermission", () => {
254
- const pm = makePermissionManager();
255
- vi.mocked(pm.getToolPermission).mockReturnValue("deny");
256
- const { resolver } = makeResolver(pm);
257
-
258
- const result = resolver.getToolPermission("write", "my-agent");
259
-
260
- expect(pm.getToolPermission).toHaveBeenCalledWith("write", "my-agent");
261
- expect(result).toBe("deny");
262
- });
263
- });
264
-
265
- describe("getConfigIssues", () => {
266
- it("delegates to permissionManager.getConfigIssues", () => {
267
- const pm = makePermissionManager();
268
- vi.mocked(pm.getConfigIssues).mockReturnValue(["issue-1"]);
269
- const { resolver } = makeResolver(pm);
270
-
271
- const result = resolver.getConfigIssues("agent-1");
272
-
273
- expect(pm.getConfigIssues).toHaveBeenCalledWith("agent-1");
274
- expect(result).toEqual(["issue-1"]);
275
- });
276
- });
277
- });