@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,268 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import type { AccessIntent } from "#src/access-intent/access-intent";
3
- import { BashProgram } from "#src/access-intent/bash/program";
4
- import { describeBashExternalDirectoryGate } from "#src/handlers/gates/bash-external-directory";
5
- import type {
6
- GateBypass,
7
- GateDescriptor,
8
- GateResult,
9
- } from "#src/handlers/gates/descriptor";
10
- import { isGateBypass, isGateDescriptor } from "#src/handlers/gates/descriptor";
11
- import type { ToolCallContext } from "#src/handlers/gates/types";
12
- import { PathNormalizer } from "#src/path-normalizer";
13
- import type { ScopedPermissionResolver } from "#src/permission-resolver";
14
- import type { PermissionCheckResult } from "#src/types";
15
- import { getNonEmptyString, toRecord } from "#src/value-guards";
16
-
17
- import { makeResolver } from "#test/helpers/gate-fixtures";
18
-
19
- // ── helpers ────────────────────────────────────────────────────────────────
20
-
21
- function makeTcc(overrides: Partial<ToolCallContext> = {}): ToolCallContext {
22
- return {
23
- toolName: "bash",
24
- agentName: null,
25
- input: { command: "cat /outside/project/file.ts" },
26
- toolCallId: "tc-1",
27
- cwd: "/test/project",
28
- ...overrides,
29
- };
30
- }
31
-
32
- function makeCheckResult(
33
- state: "allow" | "deny" | "ask",
34
- overrides: Partial<PermissionCheckResult> = {},
35
- ): PermissionCheckResult {
36
- return {
37
- state,
38
- toolName: "external_directory",
39
- source: "special",
40
- origin: "builtin",
41
- ...overrides,
42
- };
43
- }
44
-
45
- /** Extract the policy match values a resolve(intent) call carries. */
46
- function intentValues(intent: AccessIntent): readonly string[] {
47
- if (intent.kind === "access-path") return intent.path.matchValues();
48
- return [];
49
- }
50
-
51
- /**
52
- * Mirror the handler's parse-once derivation: parse the bash command into a
53
- * shared `BashProgram` and inject it, exactly as `permission-gate-handler.ts`
54
- * does, so the gate is exercised through the production wiring.
55
- */
56
- async function describeGate(
57
- tcc: ToolCallContext,
58
- resolver: ScopedPermissionResolver,
59
- ): Promise<GateResult> {
60
- const command = getNonEmptyString(toRecord(tcc.input).command);
61
- const bashProgram =
62
- tcc.toolName === "bash" && command
63
- ? await BashProgram.parse(
64
- command,
65
- new PathNormalizer(process.platform, tcc.cwd),
66
- )
67
- : null;
68
- return describeBashExternalDirectoryGate(tcc, bashProgram, resolver);
69
- }
70
-
71
- // ── tests ──────────────────────────────────────────────────────────────────
72
-
73
- describe("describeBashExternalDirectoryGate", () => {
74
- it("returns null when tool is not bash", async () => {
75
- const result = await describeGate(
76
- makeTcc({ toolName: "read" }),
77
- makeResolver(makeCheckResult("ask")),
78
- );
79
- expect(result).toBeNull();
80
- });
81
-
82
- it("returns null when command has no external paths", async () => {
83
- const result = await describeGate(
84
- makeTcc({ input: { command: "ls -la" } }),
85
- makeResolver(makeCheckResult("ask")),
86
- );
87
- expect(result).toBeNull();
88
- });
89
-
90
- it("resolves each external path on the external_directory surface via an access-path intent (#418)", async () => {
91
- const resolver = makeResolver(makeCheckResult("ask"));
92
- await describeGate(
93
- makeTcc({ input: { command: "cat /outside/a.ts" } }),
94
- resolver,
95
- );
96
- const intent = resolver.resolve.mock.calls[0][0];
97
- expect(intent).toMatchObject({
98
- kind: "access-path",
99
- surface: "external_directory",
100
- agentName: undefined,
101
- });
102
- expect(intentValues(intent)).toEqual(["/outside/a.ts"]);
103
- });
104
-
105
- it("returns GateBypass when all external paths are session-covered", async () => {
106
- const resolver = makeResolver(
107
- makeCheckResult("allow", { source: "session" }),
108
- );
109
- const result = await describeGate(makeTcc(), resolver);
110
- expect(result).not.toBeNull();
111
- expect(isGateBypass(result)).toBe(true);
112
- const bypass = result as GateBypass;
113
- expect(bypass.action).toBe("allow");
114
- expect(bypass.log).toMatchObject({
115
- event: "permission_request.session_approved",
116
- details: expect.objectContaining({ resolution: "session_approved" }),
117
- });
118
- });
119
-
120
- it("returns GateDescriptor with multi-pattern sessionApproval for uncovered paths", async () => {
121
- const result = await describeGate(
122
- makeTcc({ input: { command: "diff /outside/a.ts /outside/b.ts" } }),
123
- makeResolver(makeCheckResult("ask")),
124
- );
125
- expect(isGateDescriptor(result)).toBe(true);
126
- const desc = result as GateDescriptor;
127
- expect(desc.sessionApproval).toBeDefined();
128
- if (!desc.sessionApproval) return;
129
- expect(desc.sessionApproval.patterns.length).toBeGreaterThan(0);
130
- });
131
-
132
- it("returns GateBypass when all external paths are config-level allowed", async () => {
133
- // Config-level allow (source: "special") should suppress the prompt,
134
- // not just session-level allow. This was the bug: source !== "session"
135
- // kept config-allowed paths in the uncovered set.
136
- const resolver = makeResolver();
137
- resolver.resolve.mockImplementation((intent) =>
138
- intentValues(intent).length > 0
139
- ? makeCheckResult("allow", { source: "special" })
140
- : makeCheckResult("ask"),
141
- );
142
- const result = await describeGate(makeTcc(), resolver);
143
- expect(result).not.toBeNull();
144
- expect(isGateBypass(result)).toBe(true);
145
- });
146
-
147
- it("uses worst-check state from uncovered paths for preCheck (config deny > catch-all ask)", async () => {
148
- // The path-less extCheck used to always return the "*" catch-all (ask),
149
- // silently downgrading a config-level deny to ask. After the fix, the
150
- // descriptor's preCheck is derived from the actual path check result.
151
- const resolver = makeResolver();
152
- resolver.resolve.mockImplementation((intent) =>
153
- intentValues(intent).length > 0
154
- ? makeCheckResult("deny", { source: "special" })
155
- : makeCheckResult("ask"),
156
- );
157
- const result = await describeGate(makeTcc(), resolver);
158
- expect(isGateDescriptor(result)).toBe(true);
159
- const desc = result as GateDescriptor;
160
- expect(desc.preCheck?.state).toBe("deny");
161
- });
162
-
163
- it("descriptor surface is 'external_directory'", async () => {
164
- const result = await describeGate(
165
- makeTcc(),
166
- makeResolver(makeCheckResult("ask")),
167
- );
168
- const desc = result as GateDescriptor;
169
- expect(desc.surface).toBe("external_directory");
170
- });
171
-
172
- it("descriptor decision surface is 'external_directory'", async () => {
173
- const result = await describeGate(
174
- makeTcc(),
175
- makeResolver(makeCheckResult("ask")),
176
- );
177
- const desc = result as GateDescriptor;
178
- expect(desc.decision.surface).toBe("external_directory");
179
- });
180
-
181
- it("denialContext contains the command and external paths", async () => {
182
- const result = await describeGate(
183
- makeTcc({ input: { command: "cat /outside/file.ts" } }),
184
- makeResolver(makeCheckResult("ask")),
185
- );
186
- const desc = result as GateDescriptor;
187
- expect(desc.denialContext).toMatchObject({
188
- kind: "bash_external_directory",
189
- command: "cat /outside/file.ts",
190
- cwd: "/test/project",
191
- });
192
- });
193
-
194
- it("promptDetails includes command and tool_call source", async () => {
195
- const result = await describeGate(
196
- makeTcc({ agentName: "agent-1", toolCallId: "tc-5" }),
197
- makeResolver(makeCheckResult("ask")),
198
- );
199
- const desc = result as GateDescriptor;
200
- expect(desc.promptDetails).toMatchObject({
201
- source: "tool_call",
202
- agentName: "agent-1",
203
- toolCallId: "tc-5",
204
- toolName: "bash",
205
- command: "cat /outside/project/file.ts",
206
- });
207
- });
208
-
209
- it("config-allowed path is excluded; remaining ask path produces a descriptor", async () => {
210
- // One path config-allowed, one config-ask → descriptor with only the ask path.
211
- const resolver = makeResolver();
212
- resolver.resolve.mockImplementation((intent) =>
213
- intentValues(intent).includes("/outside/a.ts")
214
- ? makeCheckResult("allow", { source: "special" })
215
- : makeCheckResult("ask"),
216
- );
217
- const result = await describeGate(
218
- makeTcc({ input: { command: "diff /outside/a.ts /outside/b.ts" } }),
219
- resolver,
220
- );
221
- expect(isGateDescriptor(result)).toBe(true);
222
- const desc = result as GateDescriptor;
223
- expect(desc.sessionApproval).toBeDefined();
224
- if (!desc.sessionApproval) return;
225
- expect(desc.sessionApproval.patterns.length).toBe(1);
226
- expect(desc.preCheck?.state).toBe("ask");
227
- });
228
-
229
- it("config-denied path makes worstCheck deny even when another path is ask", async () => {
230
- // One path config-denied, one config-ask → descriptor with preCheck.state === "deny".
231
- const resolver = makeResolver();
232
- resolver.resolve.mockImplementation((intent) =>
233
- intentValues(intent).includes("/outside/a.ts")
234
- ? makeCheckResult("deny", { source: "special" })
235
- : makeCheckResult("ask"),
236
- );
237
- const result = await describeGate(
238
- makeTcc({ input: { command: "diff /outside/a.ts /outside/b.ts" } }),
239
- resolver,
240
- );
241
- expect(isGateDescriptor(result)).toBe(true);
242
- const desc = result as GateDescriptor;
243
- expect(desc.preCheck?.state).toBe("deny");
244
- // Both paths are uncovered (neither is allow), so both patterns are included.
245
- expect(desc.sessionApproval).toBeDefined();
246
- if (!desc.sessionApproval) return;
247
- expect(desc.sessionApproval.patterns.length).toBe(2);
248
- });
249
-
250
- it("only includes uncovered paths when some are session-covered", async () => {
251
- const resolver = makeResolver();
252
- resolver.resolve.mockImplementation((intent) =>
253
- intentValues(intent).includes("/outside/a.ts")
254
- ? makeCheckResult("allow", { source: "session" })
255
- : makeCheckResult("ask"),
256
- );
257
- const result = await describeGate(
258
- makeTcc({ input: { command: "diff /outside/a.ts /outside/b.ts" } }),
259
- resolver,
260
- );
261
- expect(isGateDescriptor(result)).toBe(true);
262
- const desc = result as GateDescriptor;
263
- // Should have patterns only for the uncovered path
264
- expect(desc.sessionApproval).toBeDefined();
265
- if (!desc.sessionApproval) return;
266
- expect(desc.sessionApproval.patterns.length).toBe(1);
267
- });
268
- });
@@ -1,346 +0,0 @@
1
- import { afterEach, describe, expect, it, vi } from "vitest";
2
-
3
- // Mock node:os so tilde-expansion is deterministic across platforms.
4
- vi.mock("node:os", () => {
5
- const homedir = vi.fn(() => "/mock/home");
6
- return {
7
- homedir,
8
- default: { homedir },
9
- };
10
- });
11
-
12
- import { AccessPath } from "#src/access-intent/access-path";
13
- import { BashProgram } from "#src/access-intent/bash/program";
14
- import { describeBashPathGate } from "#src/handlers/gates/bash-path";
15
- import type {
16
- GateBypass,
17
- GateDescriptor,
18
- GateResult,
19
- } from "#src/handlers/gates/descriptor";
20
- import { isGateBypass, isGateDescriptor } from "#src/handlers/gates/descriptor";
21
- import type { ToolCallContext } from "#src/handlers/gates/types";
22
- import { PathNormalizer } from "#src/path-normalizer";
23
- import type { ScopedPermissionResolver } from "#src/permission-resolver";
24
- import { getNonEmptyString, toRecord } from "#src/value-guards";
25
-
26
- import {
27
- makeGateCheckResult as makeCheckResult,
28
- makePathDispatchResolver,
29
- makeResolver,
30
- makeTcc,
31
- } from "#test/helpers/gate-fixtures";
32
-
33
- afterEach(() => {
34
- vi.restoreAllMocks();
35
- });
36
-
37
- /**
38
- * Mirror the handler's parse-once derivation: parse the bash command into a
39
- * shared `BashProgram` and inject it, exactly as `permission-gate-handler.ts`
40
- * does, so the gate is exercised through the production wiring.
41
- */
42
- async function describeGate(
43
- tcc: ToolCallContext,
44
- resolver: ScopedPermissionResolver,
45
- ): Promise<GateResult> {
46
- const command = getNonEmptyString(toRecord(tcc.input).command);
47
- const bashProgram =
48
- tcc.toolName === "bash" && command
49
- ? await BashProgram.parse(
50
- command,
51
- new PathNormalizer(process.platform, tcc.cwd),
52
- )
53
- : null;
54
- return describeBashPathGate(tcc, bashProgram, resolver);
55
- }
56
-
57
- // ── tests ──────────────────────────────────────────────────────────────────
58
-
59
- describe("describeBashPathGate", () => {
60
- it("returns null for non-bash tools", async () => {
61
- const result = await describeGate(
62
- makeTcc({ toolName: "read", input: { path: ".env" } }),
63
- makeResolver(),
64
- );
65
- expect(result).toBeNull();
66
- });
67
-
68
- it("returns null when no tokens are extracted", async () => {
69
- const result = await describeGate(
70
- makeTcc({ input: { command: "echo hello" } }),
71
- makeResolver(),
72
- );
73
- expect(result).toBeNull();
74
- });
75
-
76
- it("returns null when all tokens evaluate to allow", async () => {
77
- const result = await describeGate(
78
- makeTcc(),
79
- makeResolver(makeCheckResult({ state: "allow" })),
80
- );
81
- expect(result).toBeNull();
82
- });
83
-
84
- it("returns GateDescriptor when a token evaluates to deny", async () => {
85
- const result = await describeGate(
86
- makeTcc(),
87
- makeResolver(makeCheckResult({ state: "deny", matchedPattern: "*.env" })),
88
- );
89
- expect(result).not.toBeNull();
90
- expect(isGateDescriptor(result)).toBe(true);
91
- const desc = result as GateDescriptor;
92
- expect(desc.surface).toBe("path");
93
- expect(desc.preCheck?.state).toBe("deny");
94
- });
95
-
96
- it("returns GateDescriptor when a token evaluates to ask", async () => {
97
- const result = await describeGate(
98
- makeTcc(),
99
- makeResolver(makeCheckResult({ state: "ask", matchedPattern: "*" })),
100
- );
101
- expect(result).not.toBeNull();
102
- expect(isGateDescriptor(result)).toBe(true);
103
- const desc = result as GateDescriptor;
104
- expect(desc.preCheck?.state).toBe("ask");
105
- });
106
-
107
- it("descriptor includes triggering token in prompt message", async () => {
108
- const result = (await describeGate(
109
- makeTcc(),
110
- makeResolver(makeCheckResult({ state: "deny", matchedPattern: "*.env" })),
111
- )) as GateDescriptor;
112
- expect(result.denialContext).toMatchObject({
113
- kind: "bash_path",
114
- command: "cat .env",
115
- pathValue: ".env",
116
- });
117
- expect(result.promptDetails.message).toContain(".env");
118
- });
119
-
120
- it("descriptor decision uses surface 'path'", async () => {
121
- const result = (await describeGate(
122
- makeTcc(),
123
- makeResolver(makeCheckResult({ state: "deny", matchedPattern: "*.env" })),
124
- )) as GateDescriptor;
125
- expect(result.decision.surface).toBe("path");
126
- });
127
-
128
- it("returns GateBypass when session rule covers the path", async () => {
129
- const result = await describeGate(
130
- makeTcc(),
131
- makeResolver(makeCheckResult({ state: "allow", source: "session" })),
132
- );
133
- expect(result).not.toBeNull();
134
- expect(isGateBypass(result)).toBe(true);
135
- expect((result as GateBypass).action).toBe("allow");
136
- });
137
-
138
- it("returns null when command is missing", async () => {
139
- const result = await describeGate(makeTcc({ input: {} }), makeResolver());
140
- expect(result).toBeNull();
141
- });
142
-
143
- it("evaluates most restrictive across multiple tokens", async () => {
144
- const resolver = makePathDispatchResolver(
145
- { "src/foo.ts": makeCheckResult({ state: "allow" }) },
146
- makeCheckResult({ state: "deny", matchedPattern: "*.env" }),
147
- );
148
- const result = await describeGate(
149
- makeTcc({ input: { command: "cat src/foo.ts .env" } }),
150
- resolver,
151
- );
152
- expect(result).not.toBeNull();
153
- expect(isGateDescriptor(result)).toBe(true);
154
- expect((result as GateDescriptor).preCheck?.state).toBe("deny");
155
- });
156
-
157
- it("deny wins in multi-token: cp .env README.md", async () => {
158
- const resolver = makePathDispatchResolver(
159
- { ".env": makeCheckResult({ state: "deny", matchedPattern: "*.env" }) },
160
- makeCheckResult({ state: "allow" }),
161
- );
162
- const result = await describeGate(
163
- makeTcc({ input: { command: "cp .env README.md" } }),
164
- resolver,
165
- );
166
- expect(result).not.toBeNull();
167
- expect(isGateDescriptor(result)).toBe(true);
168
- const desc = result as GateDescriptor;
169
- expect(desc.preCheck?.state).toBe("deny");
170
- expect(desc.decision.value).toBe(".env");
171
- });
172
-
173
- it("extracts redirect target: echo test > .env triggers deny", async () => {
174
- const resolver = makePathDispatchResolver(
175
- { ".env": makeCheckResult({ state: "deny", matchedPattern: "*.env" }) },
176
- makeCheckResult({ state: "allow" }),
177
- );
178
- const result = await describeGate(
179
- makeTcc({ input: { command: "echo test > .env" } }),
180
- resolver,
181
- );
182
- expect(result).not.toBeNull();
183
- expect(isGateDescriptor(result)).toBe(true);
184
- expect((result as GateDescriptor).preCheck?.state).toBe("deny");
185
- });
186
-
187
- it("returns null when all tokens match only the universal default", async () => {
188
- const result = await describeGate(
189
- makeTcc(),
190
- makeResolver(
191
- makeCheckResult({
192
- state: "ask",
193
- matchedPattern: undefined,
194
- source: "special",
195
- origin: "builtin",
196
- }),
197
- ),
198
- );
199
- expect(result).toBeNull();
200
- });
201
-
202
- it("ignores tokens matching universal default but fires for explicit rule matches", async () => {
203
- const resolver = makePathDispatchResolver(
204
- { ".env": makeCheckResult({ state: "deny", matchedPattern: "*.env" }) },
205
- // Other tokens match only the universal default (no matchedPattern)
206
- makeCheckResult({
207
- state: "ask",
208
- matchedPattern: undefined,
209
- source: "special",
210
- origin: "builtin",
211
- }),
212
- );
213
- const result = await describeGate(
214
- makeTcc({ input: { command: "cat src/foo.ts .env" } }),
215
- resolver,
216
- );
217
- expect(result).not.toBeNull();
218
- expect(isGateDescriptor(result)).toBe(true);
219
- const desc = result as GateDescriptor;
220
- expect(desc.preCheck?.state).toBe("deny");
221
- expect(desc.decision.value).toBe(".env");
222
- });
223
-
224
- it("resolves cd-aware policy values while keeping the raw prompt token", async () => {
225
- const resolver = makeResolver(
226
- makeCheckResult({ state: "deny", matchedPattern: "*" }),
227
- );
228
- const result = (await describeGate(
229
- makeTcc({
230
- input: { command: "cd nested && cat src/file.txt" },
231
- cwd: "/test/project",
232
- }),
233
- resolver,
234
- )) as GateDescriptor;
235
-
236
- expect(resolver.resolve).toHaveBeenCalledWith({
237
- kind: "access-path",
238
- surface: "path",
239
- path: AccessPath.forPath("src/file.txt", {
240
- cwd: "/test/project",
241
- resolveBase: "/test/project/nested",
242
- platform: "linux",
243
- }),
244
- agentName: undefined,
245
- });
246
- // The raw token drives the prompt, denial context, and session approval.
247
- expect(result.denialContext).toMatchObject({ pathValue: "src/file.txt" });
248
- expect(result.decision.value).toBe("src/file.txt");
249
- });
250
-
251
- it("does not resolve relative policy values through an unknown cd", async () => {
252
- const resolver = makeResolver(
253
- makeCheckResult({ state: "deny", matchedPattern: "*" }),
254
- );
255
- await describeGate(
256
- makeTcc({
257
- input: { command: 'cd "$DIR" && cat src/foo.ts' },
258
- cwd: "/test/project",
259
- }),
260
- resolver,
261
- );
262
-
263
- expect(resolver.resolve).toHaveBeenCalledWith({
264
- kind: "access-path",
265
- surface: "path",
266
- path: AccessPath.forLiteral("src/foo.ts"),
267
- agentName: undefined,
268
- });
269
- });
270
-
271
- it("binds a current-directory token's session approval to the cwd subtree", async () => {
272
- const resolver = makeResolver(
273
- makeCheckResult({ state: "ask", matchedPattern: "*" }),
274
- );
275
- const result = (await describeGate(
276
- makeTcc({
277
- input: { command: "cat .env" },
278
- cwd: "/test/project",
279
- }),
280
- resolver,
281
- )) as GateDescriptor;
282
-
283
- expect(result.decision.value).toBe(".env");
284
- expect(result.sessionApproval?.surface).toBe("path");
285
- expect(result.sessionApproval?.representativePattern).toBe(
286
- "/test/project/*",
287
- );
288
- });
289
- });
290
-
291
- // Home-relative path characterization (#350) ──────────────────────────────
292
- //
293
- // The parser extracts ~/... tokens from bash commands; the resolver receives
294
- // the raw token and normalizeInput handles expansion. These tests verify the
295
- // gate correctly dispatches ~/... tokens through the deny/ask path.
296
-
297
- describe("describeBashPathGate — home-relative paths", () => {
298
- it("extracts ~/... token and builds descriptor on deny", async () => {
299
- // node:os is mocked: homedir() returns "/mock/home".
300
- // cat ~/.ssh/config → token "~/.ssh/config" extracted.
301
- const resolver = makePathDispatchResolver(
302
- {
303
- "/mock/home/.ssh/config": makeCheckResult({
304
- state: "deny",
305
- matchedPattern: "~/.ssh/*",
306
- }),
307
- },
308
- makeCheckResult({ state: "allow" }),
309
- );
310
- const result = (await describeGate(
311
- makeTcc({ input: { command: "cat ~/.ssh/config" } }),
312
- resolver,
313
- )) as GateDescriptor;
314
-
315
- expect(isGateDescriptor(result)).toBe(true);
316
- expect(result.preCheck?.state).toBe("deny");
317
- expect(result.denialContext).toMatchObject({
318
- kind: "bash_path",
319
- command: "cat ~/.ssh/config",
320
- pathValue: "~/.ssh/config",
321
- });
322
- });
323
-
324
- it("extracts $HOME/... token and builds descriptor on deny", async () => {
325
- const resolver = makePathDispatchResolver(
326
- {
327
- "/mock/home/.ssh/config": makeCheckResult({
328
- state: "deny",
329
- matchedPattern: "$HOME/.ssh/*",
330
- }),
331
- },
332
- makeCheckResult({ state: "allow" }),
333
- );
334
- const result = (await describeGate(
335
- makeTcc({ input: { command: "cat $HOME/.ssh/config" } }),
336
- resolver,
337
- )) as GateDescriptor;
338
-
339
- expect(isGateDescriptor(result)).toBe(true);
340
- expect(result.preCheck?.state).toBe("deny");
341
- expect(result.denialContext).toMatchObject({
342
- kind: "bash_path",
343
- pathValue: "$HOME/.ssh/config",
344
- });
345
- });
346
- });
@@ -1,52 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { pickMostRestrictive } from "#src/handlers/gates/candidate-check";
4
-
5
- import { makeGateCheckResult } from "#test/helpers/gate-fixtures";
6
-
7
- describe("pickMostRestrictive", () => {
8
- it("returns undefined for an empty list", () => {
9
- expect(pickMostRestrictive([])).toBeUndefined();
10
- });
11
-
12
- it("returns the single result for a one-element list", () => {
13
- const only = makeGateCheckResult({ state: "allow" });
14
- expect(pickMostRestrictive([only])).toBe(only);
15
- });
16
-
17
- it("prefers deny over ask and allow regardless of position", () => {
18
- const allow = makeGateCheckResult({ state: "allow", matchedPattern: "a" });
19
- const ask = makeGateCheckResult({ state: "ask", matchedPattern: "b" });
20
- const deny = makeGateCheckResult({ state: "deny", matchedPattern: "c" });
21
- expect(pickMostRestrictive([allow, ask, deny])).toBe(deny);
22
- expect(pickMostRestrictive([deny, ask, allow])).toBe(deny);
23
- });
24
-
25
- it("prefers ask over allow when no deny is present", () => {
26
- const allow = makeGateCheckResult({ state: "allow" });
27
- const ask = makeGateCheckResult({ state: "ask" });
28
- expect(pickMostRestrictive([allow, ask])).toBe(ask);
29
- });
30
-
31
- it("keeps the first deny on ties", () => {
32
- const deny1 = makeGateCheckResult({
33
- state: "deny",
34
- matchedPattern: "first",
35
- });
36
- const deny2 = makeGateCheckResult({
37
- state: "deny",
38
- matchedPattern: "second",
39
- });
40
- expect(pickMostRestrictive([deny1, deny2])).toBe(deny1);
41
- });
42
-
43
- it("keeps the first ask on ties when no deny is present", () => {
44
- const allow = makeGateCheckResult({ state: "allow" });
45
- const ask1 = makeGateCheckResult({ state: "ask", matchedPattern: "first" });
46
- const ask2 = makeGateCheckResult({
47
- state: "ask",
48
- matchedPattern: "second",
49
- });
50
- expect(pickMostRestrictive([allow, ask1, ask2])).toBe(ask1);
51
- });
52
- });