@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,85 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
-
3
- import {
4
- formatBashExternalDirectoryAskPrompt,
5
- formatExternalDirectoryAskPrompt,
6
- } from "#src/handlers/gates/external-directory-messages";
7
-
8
- // Denial message functions (formatExternalDirectoryDenyReason,
9
- // formatExternalDirectoryUserDeniedReason, formatExternalDirectoryHardStopHint,
10
- // formatBashExternalDirectoryDenyReason) have moved to denial-messages.ts.
11
- // Their behavior is tested in denial-messages.test.ts.
12
-
13
- describe("formatExternalDirectoryAskPrompt", () => {
14
- test("uses 'Current agent' when no agent name provided", () => {
15
- const result = formatExternalDirectoryAskPrompt(
16
- "read",
17
- "/etc/passwd",
18
- undefined,
19
- "/projects/my-app",
20
- );
21
- expect(result).toContain("Current agent");
22
- expect(result).toContain("read");
23
- expect(result).toContain("/etc/passwd");
24
- expect(result).toContain("/projects/my-app");
25
- });
26
-
27
- test("uses agent name when provided", () => {
28
- const result = formatExternalDirectoryAskPrompt(
29
- "write",
30
- "/tmp/out.txt",
31
- undefined,
32
- "/projects/my-app",
33
- "my-agent",
34
- );
35
- expect(result).toContain("Agent 'my-agent'");
36
- expect(result).toContain("write");
37
- expect(result).toContain("/tmp/out.txt");
38
- });
39
-
40
- test("discloses the resolved path when it differs from the typed path", () => {
41
- const result = formatExternalDirectoryAskPrompt(
42
- "read",
43
- "demo-symlink-passwd",
44
- "/etc/passwd",
45
- "/projects/my-app",
46
- );
47
- expect(result).toBe(
48
- "Current agent requested tool 'read' for path 'demo-symlink-passwd' (resolves to '/etc/passwd') outside working directory '/projects/my-app'. Allow this external directory access?",
49
- );
50
- });
51
-
52
- test("omits the disclosure when resolvedPath is undefined", () => {
53
- const result = formatExternalDirectoryAskPrompt(
54
- "read",
55
- "/etc/passwd",
56
- undefined,
57
- "/projects/my-app",
58
- );
59
- expect(result).not.toContain("resolves to");
60
- });
61
- });
62
-
63
- describe("formatBashExternalDirectoryAskPrompt", () => {
64
- test("includes command, paths, cwd, and agent name", () => {
65
- const result = formatBashExternalDirectoryAskPrompt(
66
- "cat /etc/passwd",
67
- [{ path: "/etc/passwd" }],
68
- "/projects/my-app",
69
- "my-agent",
70
- );
71
- expect(result).toContain("Agent 'my-agent'");
72
- expect(result).toContain("cat /etc/passwd");
73
- expect(result).toContain("/etc/passwd");
74
- expect(result).toContain("/projects/my-app");
75
- });
76
-
77
- test("uses 'Current agent' when no agent name provided", () => {
78
- const result = formatBashExternalDirectoryAskPrompt(
79
- "ls /tmp",
80
- [{ path: "/tmp" }],
81
- "/projects/my-app",
82
- );
83
- expect(result).toContain("Current agent");
84
- });
85
- });
@@ -1,134 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { AccessPath } from "#src/access-intent/access-path";
3
- import {
4
- resolveExternalDirectoryPolicy,
5
- selectUncoveredExternalPaths,
6
- } from "#src/handlers/gates/external-directory-policy";
7
- import type { PermissionCheckResult } from "#src/types";
8
-
9
- import { makeResolver } from "#test/helpers/gate-fixtures";
10
-
11
- const cwd = "/test/project";
12
-
13
- function makeCheckResult(
14
- state: "allow" | "deny" | "ask",
15
- overrides: Partial<PermissionCheckResult> = {},
16
- ): PermissionCheckResult {
17
- return {
18
- state,
19
- toolName: "external_directory",
20
- source: "special",
21
- origin: "builtin",
22
- ...overrides,
23
- };
24
- }
25
-
26
- describe("resolveExternalDirectoryPolicy", () => {
27
- it("resolves the path's match aliases on the external_directory surface (#418)", () => {
28
- const path = AccessPath.forPath("/outside/a.ts", {
29
- cwd,
30
- platform: "linux",
31
- });
32
- const resolver = makeResolver(makeCheckResult("ask"));
33
-
34
- const result = resolveExternalDirectoryPolicy(path, resolver, undefined);
35
-
36
- expect(resolver.resolve).toHaveBeenCalledWith({
37
- kind: "access-path",
38
- surface: "external_directory",
39
- path,
40
- agentName: undefined,
41
- });
42
- expect(result).toEqual(makeCheckResult("ask"));
43
- });
44
-
45
- it("threads the agent name through to the resolver", () => {
46
- const path = AccessPath.forPath("/outside/a.ts", {
47
- cwd,
48
- platform: "linux",
49
- });
50
- const resolver = makeResolver(makeCheckResult("allow"));
51
-
52
- resolveExternalDirectoryPolicy(path, resolver, "reviewer");
53
-
54
- expect(resolver.resolve).toHaveBeenCalledWith({
55
- kind: "access-path",
56
- surface: "external_directory",
57
- path,
58
- agentName: "reviewer",
59
- });
60
- });
61
- });
62
-
63
- describe("selectUncoveredExternalPaths", () => {
64
- it("returns no uncovered paths when every path resolves to allow", () => {
65
- const paths = [
66
- AccessPath.forPath("/outside/a.ts", { cwd, platform: "linux" }),
67
- AccessPath.forPath("/outside/b.ts", { cwd, platform: "linux" }),
68
- ];
69
- const resolver = makeResolver(makeCheckResult("allow"));
70
-
71
- const { uncovered, worstCheck } = selectUncoveredExternalPaths(
72
- paths,
73
- resolver,
74
- undefined,
75
- );
76
-
77
- expect(uncovered).toEqual([]);
78
- expect(worstCheck).toBeUndefined();
79
- });
80
-
81
- it("collects only paths whose resolved state is not allow", () => {
82
- const allowed = AccessPath.forPath("/outside/ok.ts", {
83
- cwd,
84
- platform: "linux",
85
- });
86
- const asked = AccessPath.forPath("/outside/ask.ts", {
87
- cwd,
88
- platform: "linux",
89
- });
90
- const resolver = makeResolver();
91
- resolver.resolve.mockImplementation((intent) => {
92
- const values =
93
- intent.kind === "access-path" ? intent.path.matchValues() : [];
94
- return values.includes("/outside/ok.ts")
95
- ? makeCheckResult("allow")
96
- : makeCheckResult("ask");
97
- });
98
-
99
- const { uncovered } = selectUncoveredExternalPaths(
100
- [allowed, asked],
101
- resolver,
102
- undefined,
103
- );
104
-
105
- expect(uncovered.map(({ path }) => path.value())).toEqual([asked.value()]);
106
- });
107
-
108
- it("returns the most restrictive uncovered check as worstCheck (deny > ask)", () => {
109
- const asked = AccessPath.forPath("/outside/ask.ts", {
110
- cwd,
111
- platform: "linux",
112
- });
113
- const denied = AccessPath.forPath("/outside/deny.ts", {
114
- cwd,
115
- platform: "linux",
116
- });
117
- const resolver = makeResolver();
118
- resolver.resolve.mockImplementation((intent) => {
119
- const values =
120
- intent.kind === "access-path" ? intent.path.matchValues() : [];
121
- return values.includes("/outside/deny.ts")
122
- ? makeCheckResult("deny")
123
- : makeCheckResult("ask");
124
- });
125
-
126
- const { worstCheck } = selectUncoveredExternalPaths(
127
- [asked, denied],
128
- resolver,
129
- undefined,
130
- );
131
-
132
- expect(worstCheck?.state).toBe("deny");
133
- });
134
- });
@@ -1,267 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import type {
4
- GateBypass,
5
- GateDescriptor,
6
- } from "#src/handlers/gates/descriptor";
7
- import { isGateBypass, isGateDescriptor } from "#src/handlers/gates/descriptor";
8
- import { describeExternalDirectoryGate } from "#src/handlers/gates/external-directory";
9
- import type { ToolCallContext } from "#src/handlers/gates/types";
10
- import { PathNormalizer } from "#src/path-normalizer";
11
- import type { ScopedPermissionResolver } from "#src/permission-resolver";
12
- import type { ToolAccessExtractorLookup } from "#src/tool-access-extractor-registry";
13
- import { makeResolver } from "#test/helpers/gate-fixtures";
14
- import { makeCheckResult } from "#test/helpers/handler-fixtures";
15
-
16
- // ── helpers ───────────────────────────��────────────────────────────��───────
17
-
18
- function makeTcc(overrides: Partial<ToolCallContext> = {}): ToolCallContext {
19
- return {
20
- toolName: "read",
21
- agentName: null,
22
- input: { path: "/outside/project/file.ts" },
23
- toolCallId: "tc-1",
24
- cwd: "/test/project",
25
- ...overrides,
26
- };
27
- }
28
-
29
- // Default resolver for descriptor-shape tests that do not assert the resolved
30
- // state: returns `ask` for the external_directory surface so a descriptor is
31
- // produced. Tests that assert the typed+resolved matching pass an explicit
32
- // resolver to `describeExternalDirectoryGate` directly.
33
- function gateUnderTest(
34
- tcc: ToolCallContext,
35
- infraDirs: string[],
36
- extractors?: ToolAccessExtractorLookup,
37
- resolver: ScopedPermissionResolver = makeResolver(
38
- makeCheckResult({ state: "ask", toolName: "external_directory" }),
39
- ),
40
- ) {
41
- return describeExternalDirectoryGate(
42
- tcc,
43
- infraDirs,
44
- resolver,
45
- new PathNormalizer(process.platform, tcc.cwd),
46
- extractors,
47
- );
48
- }
49
-
50
- // ── tests ────────────────────��────────────────────────────────────��────────
51
-
52
- describe("describeExternalDirectoryGate", () => {
53
- it("returns null when tool is not path-bearing", () => {
54
- const result = gateUnderTest(
55
- makeTcc({ toolName: "bash", input: { command: "ls" } }),
56
- ["/test/agent"],
57
- );
58
- expect(result).toBeNull();
59
- });
60
-
61
- it("returns null when path is inside CWD", () => {
62
- const result = gateUnderTest(
63
- makeTcc({ input: { path: "/test/project/src/index.ts" } }),
64
- ["/test/agent"],
65
- );
66
- expect(result).toBeNull();
67
- });
68
-
69
- // ── Pi infrastructure read bypass ─────────────────���────────────────────
70
-
71
- it("returns GateBypass for read targeting an infra dir", () => {
72
- const result = gateUnderTest(
73
- makeTcc({
74
- toolName: "read",
75
- input: { path: "/test/agent/git/some-package/SKILL.md" },
76
- }),
77
- ["/test/agent", "/test/agent/git"],
78
- );
79
- expect(result).not.toBeNull();
80
- expect(isGateBypass(result)).toBe(true);
81
- const bypass = result as GateBypass;
82
- expect(bypass.action).toBe("allow");
83
- expect(bypass.decision).toMatchObject({
84
- resolution: "infrastructure_auto_allowed",
85
- result: "allow",
86
- });
87
- expect(bypass.log).toMatchObject({
88
- event: "permission_request.infrastructure_auto_allowed",
89
- });
90
- });
91
-
92
- it("returns GateBypass respecting custom infraDirs", () => {
93
- const result = gateUnderTest(
94
- makeTcc({
95
- toolName: "read",
96
- input: { path: "/custom/infra/SKILL.md" },
97
- }),
98
- ["/custom/infra"],
99
- );
100
- expect(isGateBypass(result)).toBe(true);
101
- });
102
-
103
- it("does NOT bypass for write tools targeting infra dirs", () => {
104
- const result = gateUnderTest(
105
- makeTcc({
106
- toolName: "write",
107
- input: { path: "/test/agent/git/some-file.ts", content: "x" },
108
- }),
109
- ["/test/agent", "/test/agent/git"],
110
- );
111
- // Should be a GateDescriptor (needs permission check), not a bypass
112
- expect(result).not.toBeNull();
113
- expect(isGateDescriptor(result)).toBe(true);
114
- });
115
-
116
- // ── GateDescriptor for external paths ─────────────────────────────────��
117
-
118
- it("returns GateDescriptor with surface 'external_directory'", () => {
119
- const result = gateUnderTest(makeTcc(), ["/test/agent"]);
120
- expect(isGateDescriptor(result)).toBe(true);
121
- const desc = result as GateDescriptor;
122
- expect(desc.surface).toBe("external_directory");
123
- });
124
-
125
- it("decision value is the external path", () => {
126
- const result = gateUnderTest(
127
- makeTcc({ input: { path: "/outside/project/file.ts" } }),
128
- ["/test/agent"],
129
- ) as GateDescriptor;
130
- expect(result.decision.value).toBe("/outside/project/file.ts");
131
- expect(result.decision.surface).toBe("external_directory");
132
- });
133
-
134
- it("carries a precomputed preCheck and an empty input (matching is done by the gate)", () => {
135
- const result = gateUnderTest(
136
- makeTcc({ input: { path: "/outside/project/file.ts" } }),
137
- ["/test/agent"],
138
- ) as GateDescriptor;
139
- expect(result.input).toEqual({});
140
- expect(result.preCheck).toBeDefined();
141
- expect(result.preCheck?.state).toBe("ask");
142
- });
143
-
144
- it("resolves the typed and symlink-resolved aliases on the external_directory surface (#418)", () => {
145
- const resolver = makeResolver(
146
- makeCheckResult({ state: "ask", toolName: "external_directory" }),
147
- );
148
- gateUnderTest(
149
- makeTcc({ input: { path: "/outside/project/file.ts" } }),
150
- ["/test/agent"],
151
- undefined,
152
- resolver,
153
- );
154
- expect(resolver.resolve).toHaveBeenCalledWith(
155
- expect.objectContaining({
156
- kind: "access-path",
157
- surface: "external_directory",
158
- agentName: undefined,
159
- }),
160
- );
161
- const intent = resolver.resolve.mock.calls[0][0];
162
- expect(intent.kind).toBe("access-path");
163
- if (intent.kind === "access-path") {
164
- expect(intent.path.matchValues()).toEqual(["/outside/project/file.ts"]);
165
- }
166
- });
167
-
168
- it("sessionApproval uses deriveApprovalPattern", () => {
169
- const result = gateUnderTest(
170
- makeTcc({ input: { path: "/outside/project/file.ts" } }),
171
- ["/test/agent"],
172
- ) as GateDescriptor;
173
- expect(result.sessionApproval).toBeDefined();
174
- expect(result.sessionApproval?.surface).toBe("external_directory");
175
- expect(result.sessionApproval?.representativePattern).toBeDefined();
176
- });
177
-
178
- it("denialContext contains the external path and cwd", () => {
179
- const result = gateUnderTest(
180
- makeTcc({ input: { path: "/outside/project/file.ts" } }),
181
- ["/test/agent"],
182
- ) as GateDescriptor;
183
- expect(result.denialContext).toMatchObject({
184
- kind: "external_directory",
185
- toolName: "read",
186
- pathValue: "/outside/project/file.ts",
187
- cwd: "/test/project",
188
- });
189
- });
190
-
191
- it("promptDetails includes path and tool_call source", () => {
192
- const result = gateUnderTest(
193
- makeTcc({ toolName: "read", agentName: "agent-1", toolCallId: "tc-5" }),
194
- ["/test/agent"],
195
- ) as GateDescriptor;
196
- expect(result.promptDetails).toMatchObject({
197
- source: "tool_call",
198
- agentName: "agent-1",
199
- toolCallId: "tc-5",
200
- toolName: "read",
201
- path: "/outside/project/file.ts",
202
- });
203
- });
204
-
205
- it("logContext includes path and message", () => {
206
- const result = gateUnderTest(makeTcc(), ["/test/agent"]) as GateDescriptor;
207
- expect(result.logContext).toMatchObject({
208
- source: "tool_call",
209
- path: "/outside/project/file.ts",
210
- });
211
- expect(result.logContext.message).toBeDefined();
212
- });
213
- });
214
-
215
- // Extension and MCP tools are now external-directory gated (#352) ───────────
216
-
217
- describe("describeExternalDirectoryGate — extension and MCP tools (#352)", () => {
218
- it("gates an extension tool with an external input.path", () => {
219
- const result = gateUnderTest(
220
- makeTcc({
221
- toolName: "my-ext",
222
- input: { path: "/outside/project/file.ts" },
223
- }),
224
- ["/test/agent"],
225
- );
226
- expect(isGateDescriptor(result)).toBe(true);
227
- expect((result as GateDescriptor).surface).toBe("external_directory");
228
- });
229
-
230
- it("gates an MCP tool with an external arguments.path", () => {
231
- const result = gateUnderTest(
232
- makeTcc({
233
- toolName: "mcp",
234
- input: { arguments: { path: "/outside/project/file.ts" } },
235
- }),
236
- ["/test/agent"],
237
- );
238
- expect(isGateDescriptor(result)).toBe(true);
239
- });
240
-
241
- it("uses a registered extractor's external path for a custom-shaped tool", () => {
242
- const extractors = {
243
- get: (name: string) =>
244
- name === "ffgrep"
245
- ? (input: Record<string, unknown>) =>
246
- typeof input.target === "string" ? input.target : undefined
247
- : undefined,
248
- };
249
- const result = gateUnderTest(
250
- makeTcc({ toolName: "ffgrep", input: { target: "/outside/project/x" } }),
251
- ["/test/agent"],
252
- extractors,
253
- );
254
- expect(isGateDescriptor(result)).toBe(true);
255
- });
256
-
257
- it("returns null for an extension tool whose path is inside cwd", () => {
258
- const result = gateUnderTest(
259
- makeTcc({
260
- toolName: "my-ext",
261
- input: { path: "/test/project/src/x.ts" },
262
- }),
263
- ["/test/agent"],
264
- );
265
- expect(result).toBeNull();
266
- });
267
- });
@@ -1,165 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import {
4
- buildDecisionEvent,
5
- deriveDecisionValue,
6
- deriveResolution,
7
- } from "#src/handlers/gates/helpers";
8
- import type { PermissionCheckResult } from "#src/types";
9
-
10
- describe("deriveDecisionValue", () => {
11
- it("returns command for bash", () => {
12
- expect(deriveDecisionValue("bash", { command: "git status" })).toBe(
13
- "git status",
14
- );
15
- });
16
-
17
- it("falls back to toolName when bash has no command", () => {
18
- expect(deriveDecisionValue("bash", {})).toBe("bash");
19
- });
20
-
21
- it("returns target for mcp", () => {
22
- expect(deriveDecisionValue("mcp", { target: "exa:search" })).toBe(
23
- "exa:search",
24
- );
25
- });
26
-
27
- it("falls back to toolName when mcp has no target", () => {
28
- expect(deriveDecisionValue("mcp", {})).toBe("mcp");
29
- });
30
-
31
- it("returns toolName for non-path-bearing tools", () => {
32
- expect(deriveDecisionValue("my_extension_tool", {})).toBe(
33
- "my_extension_tool",
34
- );
35
- });
36
-
37
- it("returns path for path-bearing tools when path is provided", () => {
38
- expect(deriveDecisionValue("read", {}, "/project/src/main.ts")).toBe(
39
- "/project/src/main.ts",
40
- );
41
- expect(deriveDecisionValue("write", {}, "src/.env")).toBe("src/.env");
42
- });
43
-
44
- it("falls back to toolName for path-bearing tools when path is missing", () => {
45
- expect(deriveDecisionValue("read", {})).toBe("read");
46
- expect(deriveDecisionValue("write", {}, undefined)).toBe("write");
47
- });
48
- });
49
-
50
- describe("deriveResolution", () => {
51
- it("returns policy_allow for allow state", () => {
52
- expect(deriveResolution("allow", "allow", false, true)).toBe(
53
- "policy_allow",
54
- );
55
- });
56
-
57
- it("returns policy_deny for deny state", () => {
58
- expect(deriveResolution("deny", "block", false, true)).toBe("policy_deny");
59
- });
60
-
61
- it("returns user_approved for ask + allow without session", () => {
62
- expect(deriveResolution("ask", "allow", false, true)).toBe("user_approved");
63
- });
64
-
65
- it("returns user_approved_for_session for ask + allow with session", () => {
66
- expect(deriveResolution("ask", "allow", true, true)).toBe(
67
- "user_approved_for_session",
68
- );
69
- });
70
-
71
- it("returns auto_approved when autoApproved flag is set", () => {
72
- expect(deriveResolution("ask", "allow", false, true, true)).toBe(
73
- "auto_approved",
74
- );
75
- });
76
-
77
- it("returns user_denied for ask + block with canConfirm", () => {
78
- expect(deriveResolution("ask", "block", false, true)).toBe("user_denied");
79
- });
80
-
81
- it("returns confirmation_unavailable for ask + block without canConfirm", () => {
82
- expect(deriveResolution("ask", "block", false, false)).toBe(
83
- "confirmation_unavailable",
84
- );
85
- });
86
- });
87
-
88
- describe("buildDecisionEvent", () => {
89
- function makeCheck(
90
- overrides: Partial<PermissionCheckResult> = {},
91
- ): PermissionCheckResult {
92
- return {
93
- state: "allow",
94
- toolName: "read",
95
- source: "tool",
96
- origin: "builtin",
97
- matchedPattern: "*",
98
- ...overrides,
99
- };
100
- }
101
-
102
- it("builds a decision event with all fields populated", () => {
103
- const event = buildDecisionEvent(
104
- { surface: "read", value: "read" },
105
- makeCheck({ origin: "global", matchedPattern: "read" }),
106
- "test-agent",
107
- "allow",
108
- "policy_allow",
109
- );
110
- expect(event).toEqual({
111
- surface: "read",
112
- value: "read",
113
- result: "allow",
114
- resolution: "policy_allow",
115
- origin: "global",
116
- agentName: "test-agent",
117
- matchedPattern: "read",
118
- });
119
- });
120
-
121
- it("normalises undefined origin to null", () => {
122
- const event = buildDecisionEvent(
123
- { surface: "bash", value: "git status" },
124
- makeCheck({ origin: undefined }),
125
- null,
126
- "allow",
127
- "user_approved",
128
- );
129
- expect(event.origin).toBeNull();
130
- });
131
-
132
- it("normalises null agentName to null", () => {
133
- const event = buildDecisionEvent(
134
- { surface: "read", value: "read" },
135
- makeCheck(),
136
- null,
137
- "deny",
138
- "policy_deny",
139
- );
140
- expect(event.agentName).toBeNull();
141
- });
142
-
143
- it("normalises undefined matchedPattern to null", () => {
144
- const event = buildDecisionEvent(
145
- { surface: "read", value: "read" },
146
- makeCheck({ matchedPattern: undefined }),
147
- null,
148
- "deny",
149
- "policy_deny",
150
- );
151
- expect(event.matchedPattern).toBeNull();
152
- });
153
-
154
- it("passes result and resolution through", () => {
155
- const event = buildDecisionEvent(
156
- { surface: "bash", value: "rm -rf /" },
157
- makeCheck(),
158
- null,
159
- "deny",
160
- "user_denied",
161
- );
162
- expect(event.result).toBe("deny");
163
- expect(event.resolution).toBe("user_denied");
164
- });
165
- });