@komarspn/pi-permission-system 16.0.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 (203) hide show
  1. package/CHANGELOG.md +2234 -0
  2. package/LICENSE +21 -0
  3. package/README.md +158 -0
  4. package/config/config.example.json +39 -0
  5. package/package.json +82 -0
  6. package/schemas/permissions.schema.json +158 -0
  7. package/src/active-agent.ts +72 -0
  8. package/src/async-cache.ts +21 -0
  9. package/src/bash-arity.ts +210 -0
  10. package/src/builtin-tool-input-formatters.ts +82 -0
  11. package/src/canonicalize-path.ts +30 -0
  12. package/src/common.ts +121 -0
  13. package/src/config-loader.ts +432 -0
  14. package/src/config-modal.ts +259 -0
  15. package/src/config-paths.ts +47 -0
  16. package/src/config-reporter.ts +34 -0
  17. package/src/config-store.ts +222 -0
  18. package/src/decision-audit.ts +75 -0
  19. package/src/decision-reporter.ts +41 -0
  20. package/src/denial-messages.ts +232 -0
  21. package/src/expand-home.ts +28 -0
  22. package/src/extension-config.ts +79 -0
  23. package/src/extension-paths.ts +66 -0
  24. package/src/forwarded-permissions/io.ts +404 -0
  25. package/src/forwarded-permissions/permission-forwarder.ts +580 -0
  26. package/src/forwarding-manager.ts +74 -0
  27. package/src/gate-prompter.ts +12 -0
  28. package/src/handlers/before-agent-start.ts +94 -0
  29. package/src/handlers/gates/bash-command.ts +75 -0
  30. package/src/handlers/gates/bash-external-directory.ts +127 -0
  31. package/src/handlers/gates/bash-path-extractor.ts +15 -0
  32. package/src/handlers/gates/bash-path.ts +152 -0
  33. package/src/handlers/gates/bash-program.ts +1143 -0
  34. package/src/handlers/gates/bash-token-classification.ts +105 -0
  35. package/src/handlers/gates/candidate-check.ts +32 -0
  36. package/src/handlers/gates/descriptor.ts +81 -0
  37. package/src/handlers/gates/external-directory-messages.ts +20 -0
  38. package/src/handlers/gates/external-directory.ts +133 -0
  39. package/src/handlers/gates/helpers.ts +76 -0
  40. package/src/handlers/gates/path.ts +91 -0
  41. package/src/handlers/gates/runner.ts +186 -0
  42. package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
  43. package/src/handlers/gates/skill-input.ts +46 -0
  44. package/src/handlers/gates/skill-read.ts +87 -0
  45. package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
  46. package/src/handlers/gates/tool.ts +102 -0
  47. package/src/handlers/gates/types.ts +13 -0
  48. package/src/handlers/index.ts +3 -0
  49. package/src/handlers/lifecycle.ts +95 -0
  50. package/src/handlers/permission-gate-handler.ts +190 -0
  51. package/src/handlers/tool-call-boundary.ts +91 -0
  52. package/src/index.ts +225 -0
  53. package/src/input-normalizer.ts +157 -0
  54. package/src/logging.ts +113 -0
  55. package/src/mcp-targets.ts +170 -0
  56. package/src/node-modules-discovery.ts +76 -0
  57. package/src/normalize.ts +43 -0
  58. package/src/path-utils.ts +355 -0
  59. package/src/pattern-suggest.ts +132 -0
  60. package/src/permission-dialog.ts +138 -0
  61. package/src/permission-event-rpc.ts +223 -0
  62. package/src/permission-events.ts +266 -0
  63. package/src/permission-forwarding.ts +188 -0
  64. package/src/permission-gate.ts +94 -0
  65. package/src/permission-manager.ts +392 -0
  66. package/src/permission-merge.ts +32 -0
  67. package/src/permission-prompter.ts +142 -0
  68. package/src/permission-prompts.ts +93 -0
  69. package/src/permission-resolver.ts +109 -0
  70. package/src/permission-session.ts +189 -0
  71. package/src/permission-ui-prompt.ts +127 -0
  72. package/src/permissions-service.ts +63 -0
  73. package/src/persistent-approval-recorder.ts +139 -0
  74. package/src/policy-loader.ts +350 -0
  75. package/src/prompting-gateway.ts +104 -0
  76. package/src/rule.ts +188 -0
  77. package/src/scope-merge.ts +72 -0
  78. package/src/service-lifecycle.ts +49 -0
  79. package/src/service.ts +163 -0
  80. package/src/session-approval-recorder.ts +6 -0
  81. package/src/session-approval.ts +43 -0
  82. package/src/session-logger.ts +91 -0
  83. package/src/session-rules.ts +79 -0
  84. package/src/skill-prompt-sanitizer.ts +292 -0
  85. package/src/status.ts +35 -0
  86. package/src/subagent-context.ts +104 -0
  87. package/src/subagent-lifecycle-events.ts +72 -0
  88. package/src/subagent-registry.ts +105 -0
  89. package/src/synthesize.ts +92 -0
  90. package/src/system-prompt-sanitizer.ts +274 -0
  91. package/src/tool-access-extractor-registry.ts +68 -0
  92. package/src/tool-input-formatter-registry.ts +67 -0
  93. package/src/tool-input-preview.ts +34 -0
  94. package/src/tool-input-prompt-formatters.ts +63 -0
  95. package/src/tool-preview-formatter.ts +207 -0
  96. package/src/tool-registry.ts +148 -0
  97. package/src/types.ts +64 -0
  98. package/src/wildcard-matcher.ts +120 -0
  99. package/src/yolo-mode.ts +30 -0
  100. package/test/active-agent.test.ts +155 -0
  101. package/test/async-cache.test.ts +48 -0
  102. package/test/bash-arity.test.ts +144 -0
  103. package/test/bash-external-directory.test.ts +956 -0
  104. package/test/builtin-tool-input-formatters.test.ts +109 -0
  105. package/test/canonicalize-path.test.ts +93 -0
  106. package/test/common.test.ts +287 -0
  107. package/test/composition-root.test.ts +603 -0
  108. package/test/config-loader.test.ts +740 -0
  109. package/test/config-modal.test.ts +320 -0
  110. package/test/config-paths.test.ts +83 -0
  111. package/test/config-pipeline.test.ts +90 -0
  112. package/test/config-reporter.test.ts +147 -0
  113. package/test/config-store.test.ts +466 -0
  114. package/test/decision-audit.test.ts +72 -0
  115. package/test/decision-reporter.test.ts +112 -0
  116. package/test/denial-messages.test.ts +656 -0
  117. package/test/detect-permissive-bash-fallback.test.ts +56 -0
  118. package/test/expand-home.test.ts +93 -0
  119. package/test/extension-config.test.ts +129 -0
  120. package/test/extension-paths.test.ts +108 -0
  121. package/test/forwarded-permissions/io.test.ts +251 -0
  122. package/test/forwarding-manager.test.ts +194 -0
  123. package/test/handlers/before-agent-start.test.ts +317 -0
  124. package/test/handlers/external-directory-integration.test.ts +623 -0
  125. package/test/handlers/external-directory-session-dedup.test.ts +430 -0
  126. package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
  127. package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
  128. package/test/handlers/gates/bash-command.test.ts +191 -0
  129. package/test/handlers/gates/bash-external-directory.test.ts +269 -0
  130. package/test/handlers/gates/bash-path.test.ts +337 -0
  131. package/test/handlers/gates/bash-program.test.ts +410 -0
  132. package/test/handlers/gates/bash-token-classification.test.ts +241 -0
  133. package/test/handlers/gates/candidate-check.test.ts +52 -0
  134. package/test/handlers/gates/external-directory-messages.test.ts +61 -0
  135. package/test/handlers/gates/external-directory.test.ts +259 -0
  136. package/test/handlers/gates/helpers.test.ts +177 -0
  137. package/test/handlers/gates/path.test.ts +294 -0
  138. package/test/handlers/gates/runner.test.ts +447 -0
  139. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
  140. package/test/handlers/gates/skill-input.test.ts +131 -0
  141. package/test/handlers/gates/skill-read.test.ts +158 -0
  142. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
  143. package/test/handlers/gates/tool.test.ts +223 -0
  144. package/test/handlers/input-events.test.ts +168 -0
  145. package/test/handlers/input.test.ts +199 -0
  146. package/test/handlers/lifecycle.test.ts +221 -0
  147. package/test/handlers/tool-call-boundary.test.ts +145 -0
  148. package/test/handlers/tool-call-events.test.ts +277 -0
  149. package/test/handlers/tool-call.test.ts +395 -0
  150. package/test/handlers/validate-requested-tool.test.ts +92 -0
  151. package/test/helpers/gate-fixtures.ts +323 -0
  152. package/test/helpers/handler-fixtures.ts +335 -0
  153. package/test/helpers/make-fake-pi.ts +100 -0
  154. package/test/helpers/manager-harness.ts +112 -0
  155. package/test/helpers/session-fixtures.ts +204 -0
  156. package/test/input-normalizer.test.ts +367 -0
  157. package/test/logging.test.ts +51 -0
  158. package/test/mcp-targets.test.ts +233 -0
  159. package/test/node-modules-discovery.test.ts +97 -0
  160. package/test/normalize.test.ts +247 -0
  161. package/test/path-utils.test.ts +650 -0
  162. package/test/pattern-suggest.test.ts +248 -0
  163. package/test/permission-dialog.test.ts +241 -0
  164. package/test/permission-event-rpc.test.ts +541 -0
  165. package/test/permission-events.test.ts +402 -0
  166. package/test/permission-forwarder.test.ts +369 -0
  167. package/test/permission-forwarding.test.ts +315 -0
  168. package/test/permission-gate.test.ts +305 -0
  169. package/test/permission-manager-unified.test.ts +3368 -0
  170. package/test/permission-merge.test.ts +61 -0
  171. package/test/permission-prompter.test.ts +518 -0
  172. package/test/permission-prompts.test.ts +363 -0
  173. package/test/permission-resolver.test.ts +265 -0
  174. package/test/permission-session.test.ts +363 -0
  175. package/test/permission-ui-prompt.test.ts +146 -0
  176. package/test/permissions-service.test.ts +177 -0
  177. package/test/persistent-approval-recorder.test.ts +133 -0
  178. package/test/pi-infrastructure-read.test.ts +369 -0
  179. package/test/policy-loader.test.ts +561 -0
  180. package/test/prompting-gateway.test.ts +230 -0
  181. package/test/rule.test.ts +604 -0
  182. package/test/scope-merge.test.ts +116 -0
  183. package/test/service-lifecycle.test.ts +163 -0
  184. package/test/service.test.ts +308 -0
  185. package/test/session-approval.test.ts +75 -0
  186. package/test/session-logger.test.ts +200 -0
  187. package/test/session-rules.test.ts +304 -0
  188. package/test/session-start.test.ts +112 -0
  189. package/test/skill-prompt-sanitizer.test.ts +374 -0
  190. package/test/status.test.ts +10 -0
  191. package/test/subagent-context.test.ts +326 -0
  192. package/test/subagent-lifecycle-events.test.ts +132 -0
  193. package/test/subagent-registry.test.ts +145 -0
  194. package/test/synthesize.test.ts +300 -0
  195. package/test/system-prompt-sanitizer.test.ts +382 -0
  196. package/test/tool-access-extractor-registry.test.ts +77 -0
  197. package/test/tool-input-formatter-registry.test.ts +75 -0
  198. package/test/tool-input-preview.test.ts +129 -0
  199. package/test/tool-input-prompt-formatters.test.ts +115 -0
  200. package/test/tool-preview-formatter.test.ts +458 -0
  201. package/test/tool-registry.test.ts +197 -0
  202. package/test/wildcard-matcher.test.ts +424 -0
  203. package/test/yolo-mode.test.ts +188 -0
@@ -0,0 +1,191 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { resolveBashCommandCheck } from "#src/handlers/gates/bash-command";
4
+ import type { PermissionCheckResult } from "#src/types";
5
+
6
+ import { makeResolver } from "#test/helpers/gate-fixtures";
7
+ import { makeCheckResult } from "#test/helpers/handler-fixtures";
8
+
9
+ /** Build a bash-surface check result for a single command unit. */
10
+ function bashResult(
11
+ state: PermissionCheckResult["state"],
12
+ command: string,
13
+ matchedPattern?: string,
14
+ ): PermissionCheckResult {
15
+ return makeCheckResult({ state, source: "bash", command, matchedPattern });
16
+ }
17
+
18
+ describe("resolveBashCommandCheck", () => {
19
+ it("passes a single command straight through", () => {
20
+ const resolver = makeResolver(
21
+ bashResult("allow", "npm install pkg", "npm *"),
22
+ );
23
+
24
+ const result = resolveBashCommandCheck(
25
+ "npm install pkg",
26
+ [{ text: "npm install pkg" }],
27
+ undefined,
28
+ resolver,
29
+ );
30
+
31
+ expect(result.state).toBe("allow");
32
+ expect(resolver.resolve).toHaveBeenCalledTimes(1);
33
+ expect(resolver.resolve).toHaveBeenCalledWith(
34
+ "bash",
35
+ { command: "npm install pkg" },
36
+ undefined,
37
+ );
38
+ });
39
+
40
+ it("denies the chain when any sub-command is denied, reporting that command's pattern", () => {
41
+ const resolver = makeResolver();
42
+ resolver.resolve.mockImplementation((_surface, input) => {
43
+ const command = (input as { command: string }).command;
44
+ return command.startsWith("npm")
45
+ ? bashResult("deny", command, "npm *")
46
+ : bashResult("allow", command, "cd *");
47
+ });
48
+
49
+ const result = resolveBashCommandCheck(
50
+ "cd /p && npm install pkg",
51
+ [{ text: "cd /p" }, { text: "npm install pkg" }],
52
+ undefined,
53
+ resolver,
54
+ );
55
+
56
+ expect(result.state).toBe("deny");
57
+ expect(result.matchedPattern).toBe("npm *");
58
+ expect(result.command).toBe("npm install pkg");
59
+ });
60
+
61
+ it("asks when a sub-command asks and none denies", () => {
62
+ const resolver = makeResolver();
63
+ resolver.resolve.mockImplementation((_surface, input) => {
64
+ const command = (input as { command: string }).command;
65
+ return command.startsWith("git")
66
+ ? bashResult("ask", command, "git *")
67
+ : bashResult("allow", command, "cd *");
68
+ });
69
+
70
+ const result = resolveBashCommandCheck(
71
+ "cd /p && git push",
72
+ [{ text: "cd /p" }, { text: "git push" }],
73
+ undefined,
74
+ resolver,
75
+ );
76
+
77
+ expect(result.state).toBe("ask");
78
+ expect(result.matchedPattern).toBe("git *");
79
+ expect(result.command).toBe("git push");
80
+ });
81
+
82
+ it("returns the first allow result when every sub-command is allowed", () => {
83
+ const resolver = makeResolver();
84
+ resolver.resolve.mockImplementation((_surface, input) => {
85
+ const command = (input as { command: string }).command;
86
+ return bashResult("allow", command, `${command} *`);
87
+ });
88
+
89
+ const result = resolveBashCommandCheck(
90
+ "a && b",
91
+ [{ text: "a" }, { text: "b" }],
92
+ undefined,
93
+ resolver,
94
+ );
95
+
96
+ expect(result.state).toBe("allow");
97
+ expect(result.matchedPattern).toBe("a *");
98
+ });
99
+
100
+ it("falls back to the whole command for a comment-only line (genuinely nothing to gate)", () => {
101
+ const resolver = makeResolver(bashResult("allow", "# just a comment", "*"));
102
+
103
+ const result = resolveBashCommandCheck(
104
+ "# just a comment",
105
+ [],
106
+ undefined,
107
+ resolver,
108
+ );
109
+
110
+ expect(result.state).toBe("allow");
111
+ expect(resolver.resolve).toHaveBeenCalledTimes(1);
112
+ expect(resolver.resolve).toHaveBeenCalledWith(
113
+ "bash",
114
+ { command: "# just a comment" },
115
+ undefined,
116
+ );
117
+ });
118
+
119
+ it("falls back to the whole command for an empty/whitespace-only command", () => {
120
+ const resolver = makeResolver(bashResult("allow", " ", "*"));
121
+
122
+ const result = resolveBashCommandCheck(" ", [], undefined, resolver);
123
+
124
+ expect(result.state).toBe("allow");
125
+ expect(resolver.resolve).toHaveBeenCalledTimes(1);
126
+ });
127
+
128
+ it("fails closed to ask when a non-empty command parses to zero command units", () => {
129
+ const resolver = makeResolver(bashResult("allow", "( rm x )", "*"));
130
+
131
+ const result = resolveBashCommandCheck("( rm x )", [], undefined, resolver);
132
+
133
+ // A permissive top-level '*' must NOT silently allow an unparseable command.
134
+ expect(result.state).toBe("ask");
135
+ expect(result.matchedPattern).toBe("<unparseable-bash-command>");
136
+ expect(result.command).toBe("( rm x )");
137
+ expect(result.commandContext).toBeUndefined();
138
+ // The synthetic ask is returned without consulting the resolver.
139
+ expect(resolver.resolve).not.toHaveBeenCalled();
140
+ });
141
+
142
+ it("forwards the agent name to each sub-command check", () => {
143
+ const resolver = makeResolver(bashResult("allow", "npm i"));
144
+
145
+ resolveBashCommandCheck("npm i", [{ text: "npm i" }], "agent-x", resolver);
146
+
147
+ expect(resolver.resolve).toHaveBeenCalledWith(
148
+ "bash",
149
+ { command: "npm i" },
150
+ "agent-x",
151
+ );
152
+ });
153
+
154
+ it("tags the winning result with the offending command's execution context", () => {
155
+ const resolver = makeResolver();
156
+ resolver.resolve.mockImplementation((_surface, input) => {
157
+ const command = (input as { command: string }).command;
158
+ return command.startsWith("rm")
159
+ ? bashResult("deny", command, "rm *")
160
+ : bashResult("allow", command, "echo *");
161
+ });
162
+
163
+ const result = resolveBashCommandCheck(
164
+ "echo $(rm -rf foo)",
165
+ [
166
+ { text: "echo $(rm -rf foo)" },
167
+ { text: "rm -rf foo", context: "command_substitution" },
168
+ ],
169
+ undefined,
170
+ resolver,
171
+ );
172
+
173
+ expect(result.state).toBe("deny");
174
+ expect(result.command).toBe("rm -rf foo");
175
+ expect(result.commandContext).toBe("command_substitution");
176
+ });
177
+
178
+ it("leaves commandContext unset when the winning command is top-level", () => {
179
+ const resolver = makeResolver(bashResult("deny", "rm -rf foo", "rm *"));
180
+
181
+ const result = resolveBashCommandCheck(
182
+ "rm -rf foo",
183
+ [{ text: "rm -rf foo" }],
184
+ undefined,
185
+ resolver,
186
+ );
187
+
188
+ expect(result.state).toBe("deny");
189
+ expect(result.commandContext).toBeUndefined();
190
+ });
191
+ });
@@ -0,0 +1,269 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { getNonEmptyString, toRecord } from "#src/common";
3
+ import { describeBashExternalDirectoryGate } from "#src/handlers/gates/bash-external-directory";
4
+ import { BashProgram } from "#src/handlers/gates/bash-program";
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 type { ScopedPermissionResolver } from "#src/permission-resolver";
13
+ import type { PermissionCheckResult } from "#src/types";
14
+
15
+ import { makeResolver } from "#test/helpers/gate-fixtures";
16
+
17
+ // ── helpers ────────────────────────────────────────────────────────────────
18
+
19
+ function makeTcc(overrides: Partial<ToolCallContext> = {}): ToolCallContext {
20
+ return {
21
+ toolName: "bash",
22
+ agentName: null,
23
+ input: { command: "cat /outside/project/file.ts" },
24
+ toolCallId: "tc-1",
25
+ cwd: "/test/project",
26
+ ...overrides,
27
+ };
28
+ }
29
+
30
+ function makeCheckResult(
31
+ state: "allow" | "deny" | "ask",
32
+ overrides: Partial<PermissionCheckResult> = {},
33
+ ): PermissionCheckResult {
34
+ return {
35
+ state,
36
+ toolName: "external_directory",
37
+ source: "special",
38
+ origin: "builtin",
39
+ ...overrides,
40
+ };
41
+ }
42
+
43
+ /**
44
+ * Mirror the handler's parse-once derivation: parse the bash command into a
45
+ * shared `BashProgram` and inject it, exactly as `permission-gate-handler.ts`
46
+ * does, so the gate is exercised through the production wiring.
47
+ */
48
+ async function describeGate(
49
+ tcc: ToolCallContext,
50
+ resolver: ScopedPermissionResolver,
51
+ ): Promise<GateResult> {
52
+ const command = getNonEmptyString(toRecord(tcc.input).command);
53
+ const bashProgram =
54
+ tcc.toolName === "bash" && command
55
+ ? await BashProgram.parse(command)
56
+ : null;
57
+ return describeBashExternalDirectoryGate(tcc, bashProgram, resolver);
58
+ }
59
+
60
+ // ── tests ──────────────────────────────────────────────────────────────────
61
+
62
+ describe("describeBashExternalDirectoryGate", () => {
63
+ it("returns null when tool is not bash", async () => {
64
+ const result = await describeGate(
65
+ makeTcc({ toolName: "read" }),
66
+ makeResolver(makeCheckResult("ask")),
67
+ );
68
+ expect(result).toBeNull();
69
+ });
70
+
71
+ it("returns null when no CWD", async () => {
72
+ const result = await describeGate(
73
+ makeTcc({ cwd: undefined }),
74
+ makeResolver(makeCheckResult("ask")),
75
+ );
76
+ expect(result).toBeNull();
77
+ });
78
+
79
+ it("returns null when command has no external paths", async () => {
80
+ const result = await describeGate(
81
+ makeTcc({ input: { command: "ls -la" } }),
82
+ makeResolver(makeCheckResult("ask")),
83
+ );
84
+ expect(result).toBeNull();
85
+ });
86
+
87
+ it("resolves each external path on the external_directory surface via resolvePathPolicy (#418)", async () => {
88
+ const resolver = makeResolver(makeCheckResult("ask"));
89
+ await describeGate(
90
+ makeTcc({ input: { command: "cat /outside/a.ts" } }),
91
+ resolver,
92
+ );
93
+ expect(resolver.resolvePathPolicy).toHaveBeenCalledWith(
94
+ ["/outside/a.ts"],
95
+ undefined,
96
+ "external_directory",
97
+ );
98
+ expect(resolver.resolve).not.toHaveBeenCalled();
99
+ });
100
+
101
+ it("returns GateBypass when all external paths are session-covered", async () => {
102
+ const resolver = makeResolver(
103
+ makeCheckResult("allow", { source: "session" }),
104
+ );
105
+ const result = await describeGate(makeTcc(), resolver);
106
+ expect(result).not.toBeNull();
107
+ expect(isGateBypass(result)).toBe(true);
108
+ const bypass = result as GateBypass;
109
+ expect(bypass.action).toBe("allow");
110
+ expect(bypass.log).toMatchObject({
111
+ event: "permission_request.session_approved",
112
+ details: expect.objectContaining({ resolution: "session_approved" }),
113
+ });
114
+ });
115
+
116
+ it("returns GateDescriptor with multi-pattern sessionApproval for uncovered paths", async () => {
117
+ const result = await describeGate(
118
+ makeTcc({ input: { command: "diff /outside/a.ts /outside/b.ts" } }),
119
+ makeResolver(makeCheckResult("ask")),
120
+ );
121
+ expect(isGateDescriptor(result)).toBe(true);
122
+ const desc = result as GateDescriptor;
123
+ expect(desc.sessionApproval).toBeDefined();
124
+ if (!desc.sessionApproval) return;
125
+ expect(desc.sessionApproval.patterns.length).toBeGreaterThan(0);
126
+ });
127
+
128
+ it("returns GateBypass when all external paths are config-level allowed", async () => {
129
+ // Config-level allow (source: "special") should suppress the prompt,
130
+ // not just session-level allow. This was the bug: source !== "session"
131
+ // kept config-allowed paths in the uncovered set.
132
+ const resolver = makeResolver();
133
+ resolver.resolvePathPolicy.mockImplementation(
134
+ (values: readonly string[]) =>
135
+ values.length > 0
136
+ ? makeCheckResult("allow", { source: "special" })
137
+ : makeCheckResult("ask"),
138
+ );
139
+ const result = await describeGate(makeTcc(), resolver);
140
+ expect(result).not.toBeNull();
141
+ expect(isGateBypass(result)).toBe(true);
142
+ });
143
+
144
+ it("uses worst-check state from uncovered paths for preCheck (config deny > catch-all ask)", async () => {
145
+ // The path-less extCheck used to always return the "*" catch-all (ask),
146
+ // silently downgrading a config-level deny to ask. After the fix, the
147
+ // descriptor's preCheck is derived from the actual path check result.
148
+ const resolver = makeResolver();
149
+ resolver.resolvePathPolicy.mockImplementation(
150
+ (values: readonly string[]) =>
151
+ values.length > 0
152
+ ? makeCheckResult("deny", { source: "special" })
153
+ : makeCheckResult("ask"),
154
+ );
155
+ const result = await describeGate(makeTcc(), resolver);
156
+ expect(isGateDescriptor(result)).toBe(true);
157
+ const desc = result as GateDescriptor;
158
+ expect(desc.preCheck?.state).toBe("deny");
159
+ });
160
+
161
+ it("descriptor surface is 'external_directory'", async () => {
162
+ const result = await describeGate(
163
+ makeTcc(),
164
+ makeResolver(makeCheckResult("ask")),
165
+ );
166
+ const desc = result as GateDescriptor;
167
+ expect(desc.surface).toBe("external_directory");
168
+ });
169
+
170
+ it("descriptor decision surface is 'external_directory'", async () => {
171
+ const result = await describeGate(
172
+ makeTcc(),
173
+ makeResolver(makeCheckResult("ask")),
174
+ );
175
+ const desc = result as GateDescriptor;
176
+ expect(desc.decision.surface).toBe("external_directory");
177
+ });
178
+
179
+ it("denialContext contains the command and external paths", async () => {
180
+ const result = await describeGate(
181
+ makeTcc({ input: { command: "cat /outside/file.ts" } }),
182
+ makeResolver(makeCheckResult("ask")),
183
+ );
184
+ const desc = result as GateDescriptor;
185
+ expect(desc.denialContext).toMatchObject({
186
+ kind: "bash_external_directory",
187
+ command: "cat /outside/file.ts",
188
+ cwd: "/test/project",
189
+ });
190
+ });
191
+
192
+ it("promptDetails includes command and tool_call source", async () => {
193
+ const result = await describeGate(
194
+ makeTcc({ agentName: "agent-1", toolCallId: "tc-5" }),
195
+ makeResolver(makeCheckResult("ask")),
196
+ );
197
+ const desc = result as GateDescriptor;
198
+ expect(desc.promptDetails).toMatchObject({
199
+ source: "tool_call",
200
+ agentName: "agent-1",
201
+ toolCallId: "tc-5",
202
+ toolName: "bash",
203
+ command: "cat /outside/project/file.ts",
204
+ });
205
+ });
206
+
207
+ it("config-allowed path is excluded; remaining ask path produces a descriptor", async () => {
208
+ // One path config-allowed, one config-ask → descriptor with only the ask path.
209
+ const resolver = makeResolver();
210
+ resolver.resolvePathPolicy.mockImplementation(
211
+ (values: readonly string[]) =>
212
+ values.includes("/outside/a.ts")
213
+ ? makeCheckResult("allow", { source: "special" })
214
+ : makeCheckResult("ask"),
215
+ );
216
+ const result = await describeGate(
217
+ makeTcc({ input: { command: "diff /outside/a.ts /outside/b.ts" } }),
218
+ resolver,
219
+ );
220
+ expect(isGateDescriptor(result)).toBe(true);
221
+ const desc = result as GateDescriptor;
222
+ expect(desc.sessionApproval).toBeDefined();
223
+ if (!desc.sessionApproval) return;
224
+ expect(desc.sessionApproval.patterns.length).toBe(1);
225
+ expect(desc.preCheck?.state).toBe("ask");
226
+ });
227
+
228
+ it("config-denied path makes worstCheck deny even when another path is ask", async () => {
229
+ // One path config-denied, one config-ask → descriptor with preCheck.state === "deny".
230
+ const resolver = makeResolver();
231
+ resolver.resolvePathPolicy.mockImplementation(
232
+ (values: readonly string[]) =>
233
+ values.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.resolvePathPolicy.mockImplementation(
253
+ (values: readonly string[]) =>
254
+ values.includes("/outside/a.ts")
255
+ ? makeCheckResult("allow", { source: "session" })
256
+ : makeCheckResult("ask"),
257
+ );
258
+ const result = await describeGate(
259
+ makeTcc({ input: { command: "diff /outside/a.ts /outside/b.ts" } }),
260
+ resolver,
261
+ );
262
+ expect(isGateDescriptor(result)).toBe(true);
263
+ const desc = result as GateDescriptor;
264
+ // Should have patterns only for the uncovered path
265
+ expect(desc.sessionApproval).toBeDefined();
266
+ if (!desc.sessionApproval) return;
267
+ expect(desc.sessionApproval.patterns.length).toBe(1);
268
+ });
269
+ });