@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,131 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { describeSkillInputGate } from "#src/handlers/gates/skill-input";
4
+ import { makeCheckResult } from "#test/helpers/handler-fixtures";
5
+
6
+ // ── helpers ────────────────────────────────────────────────────────────────
7
+
8
+ function makeSkillCheck(state: "allow" | "deny" | "ask") {
9
+ return makeCheckResult({
10
+ state,
11
+ toolName: "skill",
12
+ source: "skill",
13
+ origin: "global",
14
+ matchedPattern: "*",
15
+ });
16
+ }
17
+
18
+ // ── describeSkillInputGate ─────────────────────────────────────────────────
19
+
20
+ describe("describeSkillInputGate", () => {
21
+ it("sets surface to 'skill'", () => {
22
+ const descriptor = describeSkillInputGate(
23
+ "librarian",
24
+ null,
25
+ makeSkillCheck("allow"),
26
+ );
27
+ expect(descriptor.surface).toBe("skill");
28
+ });
29
+
30
+ it("sets input.name to the skill name", () => {
31
+ const descriptor = describeSkillInputGate(
32
+ "librarian",
33
+ null,
34
+ makeSkillCheck("allow"),
35
+ );
36
+ expect(descriptor.input).toEqual({ name: "librarian" });
37
+ });
38
+
39
+ it("passes preCheck through verbatim", () => {
40
+ const check = makeSkillCheck("deny");
41
+ const descriptor = describeSkillInputGate("librarian", null, check);
42
+ expect(descriptor.preCheck).toBe(check);
43
+ });
44
+
45
+ it("sets denialContext with kind skill_input and skill name", () => {
46
+ const descriptor = describeSkillInputGate(
47
+ "librarian",
48
+ null,
49
+ makeSkillCheck("allow"),
50
+ );
51
+ expect(descriptor.denialContext).toEqual({
52
+ kind: "skill_input",
53
+ skillName: "librarian",
54
+ agentName: undefined,
55
+ });
56
+ });
57
+
58
+ it("includes agentName in denialContext when provided", () => {
59
+ const descriptor = describeSkillInputGate(
60
+ "librarian",
61
+ "code-agent",
62
+ makeSkillCheck("allow"),
63
+ );
64
+ expect(descriptor.denialContext).toEqual({
65
+ kind: "skill_input",
66
+ skillName: "librarian",
67
+ agentName: "code-agent",
68
+ });
69
+ });
70
+
71
+ it("sets promptDetails source to 'skill_input' with skill name and agent", () => {
72
+ const descriptor = describeSkillInputGate(
73
+ "librarian",
74
+ "code-agent",
75
+ makeSkillCheck("ask"),
76
+ );
77
+ expect(descriptor.promptDetails).toMatchObject({
78
+ source: "skill_input",
79
+ agentName: "code-agent",
80
+ skillName: "librarian",
81
+ });
82
+ });
83
+
84
+ it("includes a non-empty message in promptDetails", () => {
85
+ const descriptor = describeSkillInputGate(
86
+ "librarian",
87
+ null,
88
+ makeSkillCheck("ask"),
89
+ );
90
+ expect(typeof descriptor.promptDetails.message).toBe("string");
91
+ expect(descriptor.promptDetails.message.length).toBeGreaterThan(0);
92
+ });
93
+
94
+ it("sets logContext source to 'skill_input' with skill name and agent", () => {
95
+ const descriptor = describeSkillInputGate(
96
+ "librarian",
97
+ "code-agent",
98
+ makeSkillCheck("allow"),
99
+ );
100
+ expect(descriptor.logContext).toMatchObject({
101
+ source: "skill_input",
102
+ skillName: "librarian",
103
+ agentName: "code-agent",
104
+ });
105
+ });
106
+
107
+ it("sets decision surface to 'skill' and value to the skill name", () => {
108
+ const descriptor = describeSkillInputGate(
109
+ "my-skill",
110
+ null,
111
+ makeSkillCheck("allow"),
112
+ );
113
+ expect(descriptor.decision).toEqual({
114
+ surface: "skill",
115
+ value: "my-skill",
116
+ });
117
+ });
118
+
119
+ it("does not set preResolved and sets skill sessionApproval", () => {
120
+ const descriptor = describeSkillInputGate(
121
+ "librarian",
122
+ null,
123
+ makeSkillCheck("allow"),
124
+ );
125
+ expect(descriptor.preResolved).toBeUndefined();
126
+ expect(descriptor.sessionApproval?.toGateApproval()).toEqual({
127
+ surface: "skill",
128
+ pattern: "librarian",
129
+ });
130
+ });
131
+ });
@@ -0,0 +1,158 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { describeSkillReadGate } from "#src/handlers/gates/skill-read";
3
+ import type { ToolCallContext } from "#src/handlers/gates/types";
4
+ import type { SkillPromptEntry } from "#src/skill-prompt-sanitizer";
5
+
6
+ // ── SDK stubs ──────────────────────────────────────────────────────────────
7
+ vi.mock("@earendil-works/pi-coding-agent", async (importOriginal) => {
8
+ const original =
9
+ await importOriginal<typeof import("@earendil-works/pi-coding-agent")>();
10
+ return { ...original };
11
+ });
12
+
13
+ // ── helpers ────────────────────────────────────────────────────────────────
14
+
15
+ function makeSkillEntry(
16
+ overrides: Partial<SkillPromptEntry> = {},
17
+ ): SkillPromptEntry {
18
+ return {
19
+ name: "librarian",
20
+ description: "Research skills",
21
+ location: "/skills/librarian/SKILL.md",
22
+ state: "ask",
23
+ normalizedLocation: "/skills/librarian/SKILL.md",
24
+ normalizedBaseDir: "/skills/librarian",
25
+ ...overrides,
26
+ };
27
+ }
28
+
29
+ function makeTcc(overrides: Partial<ToolCallContext> = {}): ToolCallContext {
30
+ return {
31
+ toolName: "read",
32
+ agentName: null,
33
+ input: { path: "/skills/librarian/SKILL.md" },
34
+ toolCallId: "tc-1",
35
+ cwd: "/test/project",
36
+ ...overrides,
37
+ };
38
+ }
39
+
40
+ // ── tests ──────────────────────────────────────────────────────────────────
41
+
42
+ describe("describeSkillReadGate", () => {
43
+ it("returns null when tool is not read", () => {
44
+ const result = describeSkillReadGate(makeTcc({ toolName: "write" }), () => [
45
+ makeSkillEntry(),
46
+ ]);
47
+ expect(result).toBeNull();
48
+ });
49
+
50
+ it("returns null when no active skill entries", () => {
51
+ const result = describeSkillReadGate(makeTcc(), () => []);
52
+ expect(result).toBeNull();
53
+ });
54
+
55
+ it("returns null when read path does not match any skill", () => {
56
+ const result = describeSkillReadGate(
57
+ makeTcc({ input: { path: "/test/project/src/index.ts" } }),
58
+ () => [makeSkillEntry()],
59
+ );
60
+ expect(result).toBeNull();
61
+ });
62
+
63
+ it("returns null when input has no path", () => {
64
+ const result = describeSkillReadGate(makeTcc({ input: {} }), () => [
65
+ makeSkillEntry(),
66
+ ]);
67
+ expect(result).toBeNull();
68
+ });
69
+
70
+ it("returns GateDescriptor with preResolved.state matching skill entry state (ask)", () => {
71
+ const result = describeSkillReadGate(makeTcc(), () => [
72
+ makeSkillEntry({ state: "ask" }),
73
+ ]);
74
+ expect(result).not.toBeNull();
75
+ const desc = result!;
76
+ expect(desc.preResolved).toEqual({ state: "ask" });
77
+ });
78
+
79
+ it("returns GateDescriptor with preResolved.state matching skill entry state (allow)", () => {
80
+ const result = describeSkillReadGate(makeTcc(), () => [
81
+ makeSkillEntry({ state: "allow" }),
82
+ ]);
83
+ expect(result).not.toBeNull();
84
+ const desc = result!;
85
+ expect(desc.preResolved).toEqual({ state: "allow" });
86
+ });
87
+
88
+ it("returns GateDescriptor with preResolved.state matching skill entry state (deny)", () => {
89
+ const result = describeSkillReadGate(makeTcc(), () => [
90
+ makeSkillEntry({ state: "deny" }),
91
+ ]);
92
+ expect(result).not.toBeNull();
93
+ const desc = result!;
94
+ expect(desc.preResolved).toEqual({ state: "deny" });
95
+ });
96
+
97
+ it("decision surface is 'skill' and decision value is the skill name", () => {
98
+ const result = describeSkillReadGate(makeTcc(), () => [
99
+ makeSkillEntry({ name: "my-skill" }),
100
+ ])!;
101
+ expect(result.decision.surface).toBe("skill");
102
+ expect(result.decision.value).toBe("my-skill");
103
+ });
104
+
105
+ it("denialContext contains the skill name and read path", () => {
106
+ const result = describeSkillReadGate(makeTcc(), () => [
107
+ makeSkillEntry({ name: "librarian" }),
108
+ ])!;
109
+ expect(result.denialContext).toEqual({
110
+ kind: "skill_read",
111
+ skillName: "librarian",
112
+ readPath: "/skills/librarian/SKILL.md",
113
+ agentName: undefined,
114
+ });
115
+ });
116
+
117
+ it("promptDetails includes skill_read source and skillName", () => {
118
+ const result = describeSkillReadGate(
119
+ makeTcc({ agentName: "test-agent", toolCallId: "tc-42" }),
120
+ () => [makeSkillEntry({ name: "my-skill" })],
121
+ )!;
122
+ expect(result.promptDetails).toMatchObject({
123
+ source: "skill_read",
124
+ agentName: "test-agent",
125
+ toolCallId: "tc-42",
126
+ toolName: "read",
127
+ skillName: "my-skill",
128
+ });
129
+ expect(result.promptDetails.message).toBeDefined();
130
+ });
131
+
132
+ it("logContext includes skill_read source and skillName", () => {
133
+ const result = describeSkillReadGate(
134
+ makeTcc({ agentName: "agent-1" }),
135
+ () => [makeSkillEntry({ name: "librarian" })],
136
+ )!;
137
+ expect(result.logContext).toMatchObject({
138
+ source: "skill_read",
139
+ skillName: "librarian",
140
+ agentName: "agent-1",
141
+ });
142
+ });
143
+
144
+ it("surface is 'skill' on the descriptor", () => {
145
+ const result = describeSkillReadGate(makeTcc(), () => [makeSkillEntry()])!;
146
+ expect(result.surface).toBe("skill");
147
+ });
148
+
149
+ it("sets skill sessionApproval", () => {
150
+ const result = describeSkillReadGate(makeTcc(), () => [
151
+ makeSkillEntry({ name: "librarian" }),
152
+ ])!;
153
+ expect(result.sessionApproval?.toGateApproval()).toEqual({
154
+ surface: "skill",
155
+ pattern: "librarian",
156
+ });
157
+ });
158
+ });
@@ -0,0 +1,252 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ import { ToolCallGatePipeline } from "#src/handlers/gates/tool-call-gate-pipeline";
4
+
5
+ import {
6
+ makeGateInputs,
7
+ makeGateRunner,
8
+ makeResolver,
9
+ makeTcc,
10
+ } from "#test/helpers/gate-fixtures";
11
+ import { makeCheckResult } from "#test/helpers/handler-fixtures";
12
+
13
+ // ── BashProgram.parse mock ─────────────────────────────────────────────────
14
+
15
+ const { mockBashProgramParse } = vi.hoisted(() => ({
16
+ mockBashProgramParse: vi.fn(),
17
+ }));
18
+
19
+ vi.mock("#src/handlers/gates/bash-program", () => ({
20
+ BashProgram: { parse: mockBashProgramParse },
21
+ }));
22
+
23
+ function makeMockBashProgram() {
24
+ return {
25
+ commands: vi.fn<() => []>(() => []),
26
+ pathRuleCandidates: vi.fn<() => []>(() => []),
27
+ externalPaths: vi.fn<() => []>(() => []),
28
+ };
29
+ }
30
+
31
+ // ── ToolCallGatePipeline ───────────────────────────────────────────────────
32
+
33
+ describe("ToolCallGatePipeline", () => {
34
+ beforeEach(() => {
35
+ mockBashProgramParse.mockReset();
36
+ mockBashProgramParse.mockResolvedValue(makeMockBashProgram());
37
+ });
38
+
39
+ // ── non-bash tools ───────────────────────────────────────────────────────
40
+
41
+ describe("evaluate — non-bash tool", () => {
42
+ it("returns allow when all gates pass", async () => {
43
+ const resolver = makeResolver(makeCheckResult());
44
+ const inputs = makeGateInputs();
45
+ const { runner } = makeGateRunner();
46
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
47
+
48
+ const result = await pipeline.evaluate(
49
+ makeTcc({ toolName: "read", input: {} }),
50
+ runner,
51
+ );
52
+
53
+ expect(result).toEqual({ action: "allow" });
54
+ });
55
+
56
+ it("returns block when the tool gate denies", async () => {
57
+ const resolver = makeResolver(
58
+ makeCheckResult({ state: "deny", matchedPattern: "*" }),
59
+ );
60
+ const inputs = makeGateInputs();
61
+ const { runner } = makeGateRunner();
62
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
63
+
64
+ const result = await pipeline.evaluate(
65
+ makeTcc({ toolName: "read", input: {} }),
66
+ runner,
67
+ );
68
+
69
+ expect(result).toMatchObject({ action: "block" });
70
+ });
71
+
72
+ it("short-circuits after the first blocking gate without evaluating later ones", async () => {
73
+ const resolver = makeResolver(makeCheckResult());
74
+ const inputs = makeGateInputs();
75
+ const { runner } = makeGateRunner();
76
+ const runSpy = vi
77
+ .spyOn(runner, "run")
78
+ .mockResolvedValue({ action: "block", reason: "first gate blocked" });
79
+
80
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
81
+ const result = await pipeline.evaluate(
82
+ makeTcc({ toolName: "read", input: {} }),
83
+ runner,
84
+ );
85
+
86
+ expect(result).toEqual({ action: "block", reason: "first gate blocked" });
87
+ // Pipeline looped to the first gate, got block, and stopped — not all 6 gates.
88
+ expect(runSpy).toHaveBeenCalledTimes(1);
89
+ });
90
+
91
+ it("calls getToolPreviewLimits() during evaluate", async () => {
92
+ const getToolPreviewLimits = vi.fn(() => ({
93
+ toolInputPreviewMaxLength: 500,
94
+ toolTextSummaryMaxLength: 100,
95
+ toolInputLogPreviewMaxLength: 200,
96
+ }));
97
+ const resolver = makeResolver(makeCheckResult());
98
+ const inputs = makeGateInputs({ getToolPreviewLimits });
99
+ const { runner } = makeGateRunner();
100
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
101
+
102
+ await pipeline.evaluate(makeTcc({ toolName: "read", input: {} }), runner);
103
+
104
+ expect(getToolPreviewLimits).toHaveBeenCalled();
105
+ });
106
+
107
+ it("calls getInfrastructureReadDirs() during evaluate", async () => {
108
+ const getInfrastructureReadDirs = vi.fn<() => string[]>(() => []);
109
+ const resolver = makeResolver(makeCheckResult());
110
+ const inputs = makeGateInputs({ getInfrastructureReadDirs });
111
+ const { runner } = makeGateRunner();
112
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
113
+
114
+ await pipeline.evaluate(makeTcc({ toolName: "read", input: {} }), runner);
115
+
116
+ expect(getInfrastructureReadDirs).toHaveBeenCalled();
117
+ });
118
+
119
+ it("calls getActiveSkillEntries() during evaluate", async () => {
120
+ const getActiveSkillEntries = vi.fn<() => []>(() => []);
121
+ const resolver = makeResolver(makeCheckResult());
122
+ const inputs = makeGateInputs({ getActiveSkillEntries });
123
+ const { runner } = makeGateRunner();
124
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
125
+
126
+ await pipeline.evaluate(makeTcc({ toolName: "read", input: {} }), runner);
127
+
128
+ expect(getActiveSkillEntries).toHaveBeenCalled();
129
+ });
130
+
131
+ it("does not call BashProgram.parse for non-bash tools", async () => {
132
+ const resolver = makeResolver(makeCheckResult());
133
+ const inputs = makeGateInputs();
134
+ const { runner } = makeGateRunner();
135
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
136
+
137
+ await pipeline.evaluate(makeTcc({ toolName: "read", input: {} }), runner);
138
+
139
+ expect(mockBashProgramParse).not.toHaveBeenCalled();
140
+ });
141
+ });
142
+
143
+ // ── bash tool ────────────────────────────────────────────────────────────
144
+
145
+ describe("evaluate — bash tool", () => {
146
+ it("returns allow when the bash command is permitted", async () => {
147
+ const resolver = makeResolver(makeCheckResult());
148
+ const inputs = makeGateInputs();
149
+ const { runner } = makeGateRunner();
150
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
151
+
152
+ const result = await pipeline.evaluate(
153
+ makeTcc({ toolName: "bash", input: { command: "echo hello" } }),
154
+ runner,
155
+ );
156
+
157
+ expect(result).toEqual({ action: "allow" });
158
+ });
159
+
160
+ it("parses BashProgram exactly once per evaluate for bash tools with a command", async () => {
161
+ const resolver = makeResolver(makeCheckResult());
162
+ const inputs = makeGateInputs();
163
+ const { runner } = makeGateRunner();
164
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
165
+
166
+ await pipeline.evaluate(
167
+ makeTcc({ toolName: "bash", input: { command: "echo hello" } }),
168
+ runner,
169
+ );
170
+
171
+ expect(mockBashProgramParse).toHaveBeenCalledTimes(1);
172
+ expect(mockBashProgramParse).toHaveBeenCalledWith("echo hello");
173
+ });
174
+
175
+ it("does not parse BashProgram when the bash command is empty", async () => {
176
+ const resolver = makeResolver(makeCheckResult());
177
+ const inputs = makeGateInputs();
178
+ const { runner } = makeGateRunner();
179
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
180
+
181
+ await pipeline.evaluate(
182
+ makeTcc({ toolName: "bash", input: { command: "" } }),
183
+ runner,
184
+ );
185
+
186
+ expect(mockBashProgramParse).not.toHaveBeenCalled();
187
+ });
188
+ });
189
+
190
+ // ── customExtractors threading (#352) ────────────────────────────────────
191
+
192
+ describe("evaluate — customExtractors threading (#352)", () => {
193
+ // Deny only the cross-cutting `path` surface; allow everything else, so a
194
+ // block can only come from the path gate seeing the extracted path.
195
+ function pathDenyingResolver() {
196
+ const resolver = makeResolver();
197
+ resolver.resolve.mockImplementation((surface) =>
198
+ surface === "path"
199
+ ? makeCheckResult({ state: "deny", matchedPattern: "*" })
200
+ : makeCheckResult(),
201
+ );
202
+ return resolver;
203
+ }
204
+
205
+ const extractors = {
206
+ get: (name: string) =>
207
+ name === "ffgrep"
208
+ ? (input: Record<string, unknown>) =>
209
+ typeof input.target === "string" ? input.target : undefined
210
+ : undefined,
211
+ };
212
+
213
+ it("forwards extractors so a custom-shaped tool is path-gated", async () => {
214
+ const resolver = pathDenyingResolver();
215
+ const inputs = makeGateInputs();
216
+ const { runner } = makeGateRunner();
217
+ const pipeline = new ToolCallGatePipeline(
218
+ resolver,
219
+ inputs,
220
+ undefined,
221
+ extractors,
222
+ );
223
+
224
+ const result = await pipeline.evaluate(
225
+ makeTcc({
226
+ toolName: "ffgrep",
227
+ input: { target: "/test/project/secret.env" },
228
+ }),
229
+ runner,
230
+ );
231
+
232
+ expect(result).toMatchObject({ action: "block" });
233
+ });
234
+
235
+ it("without extractors the custom-shaped tool is not path-gated", async () => {
236
+ const resolver = pathDenyingResolver();
237
+ const inputs = makeGateInputs();
238
+ const { runner } = makeGateRunner();
239
+ const pipeline = new ToolCallGatePipeline(resolver, inputs);
240
+
241
+ const result = await pipeline.evaluate(
242
+ makeTcc({
243
+ toolName: "ffgrep",
244
+ input: { target: "/test/project/secret.env" },
245
+ }),
246
+ runner,
247
+ );
248
+
249
+ expect(result).toEqual({ action: "allow" });
250
+ });
251
+ });
252
+ });