@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.
- package/CHANGELOG.md +2234 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/config/config.example.json +39 -0
- package/package.json +82 -0
- package/schemas/permissions.schema.json +158 -0
- package/src/active-agent.ts +72 -0
- package/src/async-cache.ts +21 -0
- package/src/bash-arity.ts +210 -0
- package/src/builtin-tool-input-formatters.ts +82 -0
- package/src/canonicalize-path.ts +30 -0
- package/src/common.ts +121 -0
- package/src/config-loader.ts +432 -0
- package/src/config-modal.ts +259 -0
- package/src/config-paths.ts +47 -0
- package/src/config-reporter.ts +34 -0
- package/src/config-store.ts +222 -0
- package/src/decision-audit.ts +75 -0
- package/src/decision-reporter.ts +41 -0
- package/src/denial-messages.ts +232 -0
- package/src/expand-home.ts +28 -0
- package/src/extension-config.ts +79 -0
- package/src/extension-paths.ts +66 -0
- package/src/forwarded-permissions/io.ts +404 -0
- package/src/forwarded-permissions/permission-forwarder.ts +580 -0
- package/src/forwarding-manager.ts +74 -0
- package/src/gate-prompter.ts +12 -0
- package/src/handlers/before-agent-start.ts +94 -0
- package/src/handlers/gates/bash-command.ts +75 -0
- package/src/handlers/gates/bash-external-directory.ts +127 -0
- package/src/handlers/gates/bash-path-extractor.ts +15 -0
- package/src/handlers/gates/bash-path.ts +152 -0
- package/src/handlers/gates/bash-program.ts +1143 -0
- package/src/handlers/gates/bash-token-classification.ts +105 -0
- package/src/handlers/gates/candidate-check.ts +32 -0
- package/src/handlers/gates/descriptor.ts +81 -0
- package/src/handlers/gates/external-directory-messages.ts +20 -0
- package/src/handlers/gates/external-directory.ts +133 -0
- package/src/handlers/gates/helpers.ts +76 -0
- package/src/handlers/gates/path.ts +91 -0
- package/src/handlers/gates/runner.ts +186 -0
- package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
- package/src/handlers/gates/skill-input.ts +46 -0
- package/src/handlers/gates/skill-read.ts +87 -0
- package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
- package/src/handlers/gates/tool.ts +102 -0
- package/src/handlers/gates/types.ts +13 -0
- package/src/handlers/index.ts +3 -0
- package/src/handlers/lifecycle.ts +95 -0
- package/src/handlers/permission-gate-handler.ts +190 -0
- package/src/handlers/tool-call-boundary.ts +91 -0
- package/src/index.ts +225 -0
- package/src/input-normalizer.ts +157 -0
- package/src/logging.ts +113 -0
- package/src/mcp-targets.ts +170 -0
- package/src/node-modules-discovery.ts +76 -0
- package/src/normalize.ts +43 -0
- package/src/path-utils.ts +355 -0
- package/src/pattern-suggest.ts +132 -0
- package/src/permission-dialog.ts +138 -0
- package/src/permission-event-rpc.ts +223 -0
- package/src/permission-events.ts +266 -0
- package/src/permission-forwarding.ts +188 -0
- package/src/permission-gate.ts +94 -0
- package/src/permission-manager.ts +392 -0
- package/src/permission-merge.ts +32 -0
- package/src/permission-prompter.ts +142 -0
- package/src/permission-prompts.ts +93 -0
- package/src/permission-resolver.ts +109 -0
- package/src/permission-session.ts +189 -0
- package/src/permission-ui-prompt.ts +127 -0
- package/src/permissions-service.ts +63 -0
- package/src/persistent-approval-recorder.ts +139 -0
- package/src/policy-loader.ts +350 -0
- package/src/prompting-gateway.ts +104 -0
- package/src/rule.ts +188 -0
- package/src/scope-merge.ts +72 -0
- package/src/service-lifecycle.ts +49 -0
- package/src/service.ts +163 -0
- package/src/session-approval-recorder.ts +6 -0
- package/src/session-approval.ts +43 -0
- package/src/session-logger.ts +91 -0
- package/src/session-rules.ts +79 -0
- package/src/skill-prompt-sanitizer.ts +292 -0
- package/src/status.ts +35 -0
- package/src/subagent-context.ts +104 -0
- package/src/subagent-lifecycle-events.ts +72 -0
- package/src/subagent-registry.ts +105 -0
- package/src/synthesize.ts +92 -0
- package/src/system-prompt-sanitizer.ts +274 -0
- package/src/tool-access-extractor-registry.ts +68 -0
- package/src/tool-input-formatter-registry.ts +67 -0
- package/src/tool-input-preview.ts +34 -0
- package/src/tool-input-prompt-formatters.ts +63 -0
- package/src/tool-preview-formatter.ts +207 -0
- package/src/tool-registry.ts +148 -0
- package/src/types.ts +64 -0
- package/src/wildcard-matcher.ts +120 -0
- package/src/yolo-mode.ts +30 -0
- package/test/active-agent.test.ts +155 -0
- package/test/async-cache.test.ts +48 -0
- package/test/bash-arity.test.ts +144 -0
- package/test/bash-external-directory.test.ts +956 -0
- package/test/builtin-tool-input-formatters.test.ts +109 -0
- package/test/canonicalize-path.test.ts +93 -0
- package/test/common.test.ts +287 -0
- package/test/composition-root.test.ts +603 -0
- package/test/config-loader.test.ts +740 -0
- package/test/config-modal.test.ts +320 -0
- package/test/config-paths.test.ts +83 -0
- package/test/config-pipeline.test.ts +90 -0
- package/test/config-reporter.test.ts +147 -0
- package/test/config-store.test.ts +466 -0
- package/test/decision-audit.test.ts +72 -0
- package/test/decision-reporter.test.ts +112 -0
- package/test/denial-messages.test.ts +656 -0
- package/test/detect-permissive-bash-fallback.test.ts +56 -0
- package/test/expand-home.test.ts +93 -0
- package/test/extension-config.test.ts +129 -0
- package/test/extension-paths.test.ts +108 -0
- package/test/forwarded-permissions/io.test.ts +251 -0
- package/test/forwarding-manager.test.ts +194 -0
- package/test/handlers/before-agent-start.test.ts +317 -0
- package/test/handlers/external-directory-integration.test.ts +623 -0
- package/test/handlers/external-directory-session-dedup.test.ts +430 -0
- package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
- package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
- package/test/handlers/gates/bash-command.test.ts +191 -0
- package/test/handlers/gates/bash-external-directory.test.ts +269 -0
- package/test/handlers/gates/bash-path.test.ts +337 -0
- package/test/handlers/gates/bash-program.test.ts +410 -0
- package/test/handlers/gates/bash-token-classification.test.ts +241 -0
- package/test/handlers/gates/candidate-check.test.ts +52 -0
- package/test/handlers/gates/external-directory-messages.test.ts +61 -0
- package/test/handlers/gates/external-directory.test.ts +259 -0
- package/test/handlers/gates/helpers.test.ts +177 -0
- package/test/handlers/gates/path.test.ts +294 -0
- package/test/handlers/gates/runner.test.ts +447 -0
- package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
- package/test/handlers/gates/skill-input.test.ts +131 -0
- package/test/handlers/gates/skill-read.test.ts +158 -0
- package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
- package/test/handlers/gates/tool.test.ts +223 -0
- package/test/handlers/input-events.test.ts +168 -0
- package/test/handlers/input.test.ts +199 -0
- package/test/handlers/lifecycle.test.ts +221 -0
- package/test/handlers/tool-call-boundary.test.ts +145 -0
- package/test/handlers/tool-call-events.test.ts +277 -0
- package/test/handlers/tool-call.test.ts +395 -0
- package/test/handlers/validate-requested-tool.test.ts +92 -0
- package/test/helpers/gate-fixtures.ts +323 -0
- package/test/helpers/handler-fixtures.ts +335 -0
- package/test/helpers/make-fake-pi.ts +100 -0
- package/test/helpers/manager-harness.ts +112 -0
- package/test/helpers/session-fixtures.ts +204 -0
- package/test/input-normalizer.test.ts +367 -0
- package/test/logging.test.ts +51 -0
- package/test/mcp-targets.test.ts +233 -0
- package/test/node-modules-discovery.test.ts +97 -0
- package/test/normalize.test.ts +247 -0
- package/test/path-utils.test.ts +650 -0
- package/test/pattern-suggest.test.ts +248 -0
- package/test/permission-dialog.test.ts +241 -0
- package/test/permission-event-rpc.test.ts +541 -0
- package/test/permission-events.test.ts +402 -0
- package/test/permission-forwarder.test.ts +369 -0
- package/test/permission-forwarding.test.ts +315 -0
- package/test/permission-gate.test.ts +305 -0
- package/test/permission-manager-unified.test.ts +3368 -0
- package/test/permission-merge.test.ts +61 -0
- package/test/permission-prompter.test.ts +518 -0
- package/test/permission-prompts.test.ts +363 -0
- package/test/permission-resolver.test.ts +265 -0
- package/test/permission-session.test.ts +363 -0
- package/test/permission-ui-prompt.test.ts +146 -0
- package/test/permissions-service.test.ts +177 -0
- package/test/persistent-approval-recorder.test.ts +133 -0
- package/test/pi-infrastructure-read.test.ts +369 -0
- package/test/policy-loader.test.ts +561 -0
- package/test/prompting-gateway.test.ts +230 -0
- package/test/rule.test.ts +604 -0
- package/test/scope-merge.test.ts +116 -0
- package/test/service-lifecycle.test.ts +163 -0
- package/test/service.test.ts +308 -0
- package/test/session-approval.test.ts +75 -0
- package/test/session-logger.test.ts +200 -0
- package/test/session-rules.test.ts +304 -0
- package/test/session-start.test.ts +112 -0
- package/test/skill-prompt-sanitizer.test.ts +374 -0
- package/test/status.test.ts +10 -0
- package/test/subagent-context.test.ts +326 -0
- package/test/subagent-lifecycle-events.test.ts +132 -0
- package/test/subagent-registry.test.ts +145 -0
- package/test/synthesize.test.ts +300 -0
- package/test/system-prompt-sanitizer.test.ts +382 -0
- package/test/tool-access-extractor-registry.test.ts +77 -0
- package/test/tool-input-formatter-registry.test.ts +75 -0
- package/test/tool-input-preview.test.ts +129 -0
- package/test/tool-input-prompt-formatters.test.ts +115 -0
- package/test/tool-preview-formatter.test.ts +458 -0
- package/test/tool-registry.test.ts +197 -0
- package/test/wildcard-matcher.test.ts +424 -0
- package/test/yolo-mode.test.ts +188 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { SessionLifecycleHandler } from "#src/handlers/lifecycle";
|
|
4
|
+
import type { ServiceLifecycle } from "#src/service-lifecycle";
|
|
5
|
+
|
|
6
|
+
import { makeCtx } from "#test/helpers/handler-fixtures";
|
|
7
|
+
import {
|
|
8
|
+
makeLogger,
|
|
9
|
+
makeRealResolver,
|
|
10
|
+
makeRealSession,
|
|
11
|
+
} from "#test/helpers/session-fixtures";
|
|
12
|
+
|
|
13
|
+
// ── status stub ────────────────────────────────────────────────────────────
|
|
14
|
+
vi.mock("../../src/status", () => ({
|
|
15
|
+
PERMISSION_SYSTEM_STATUS_KEY: "permission-system",
|
|
16
|
+
syncPermissionSystemStatus: vi.fn(),
|
|
17
|
+
getPermissionSystemStatus: vi.fn(),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
// ── helpers ────────────────────────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
function makeSetup(opts?: { configIssues?: string[] }) {
|
|
23
|
+
const { session, permissionManager, sessionRules, forwarding, configStore } =
|
|
24
|
+
makeRealSession();
|
|
25
|
+
const { resolver } = makeRealResolver(permissionManager, sessionRules);
|
|
26
|
+
if (opts?.configIssues) {
|
|
27
|
+
vi.mocked(permissionManager.getConfigIssues).mockReturnValue(
|
|
28
|
+
opts.configIssues,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
const serviceLifecycle: ServiceLifecycle = {
|
|
32
|
+
activate: vi.fn<ServiceLifecycle["activate"]>(),
|
|
33
|
+
teardown: vi.fn<ServiceLifecycle["teardown"]>(),
|
|
34
|
+
};
|
|
35
|
+
// Use a session-independent logger so assertions verify direct injection,
|
|
36
|
+
// not reach-through to session.logger.
|
|
37
|
+
const logger = makeLogger();
|
|
38
|
+
const audit = { writeSummary: vi.fn<(logger: unknown) => void>() };
|
|
39
|
+
const handler = new SessionLifecycleHandler(
|
|
40
|
+
session,
|
|
41
|
+
resolver,
|
|
42
|
+
serviceLifecycle,
|
|
43
|
+
logger,
|
|
44
|
+
audit,
|
|
45
|
+
);
|
|
46
|
+
return {
|
|
47
|
+
handler,
|
|
48
|
+
session,
|
|
49
|
+
resolver,
|
|
50
|
+
permissionManager,
|
|
51
|
+
logger,
|
|
52
|
+
forwarding,
|
|
53
|
+
configStore,
|
|
54
|
+
serviceLifecycle,
|
|
55
|
+
audit,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ── handleSessionStart ─────────────────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
describe("handleSessionStart", () => {
|
|
62
|
+
it("refreshes config with ctx", async () => {
|
|
63
|
+
const ctx = makeCtx();
|
|
64
|
+
const { handler, configStore } = makeSetup();
|
|
65
|
+
await handler.handleSessionStart({ reason: "startup" }, ctx);
|
|
66
|
+
expect(configStore.refresh).toHaveBeenCalledWith(ctx);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("calls resetForNewSession with ctx", async () => {
|
|
70
|
+
const ctx = makeCtx();
|
|
71
|
+
const { handler, session } = makeSetup();
|
|
72
|
+
const spy = vi.spyOn(session, "resetForNewSession");
|
|
73
|
+
await handler.handleSessionStart({ reason: "startup" }, ctx);
|
|
74
|
+
expect(spy).toHaveBeenCalledWith(ctx);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("logs resolved config paths", async () => {
|
|
78
|
+
const { handler, configStore } = makeSetup();
|
|
79
|
+
await handler.handleSessionStart({ reason: "startup" }, makeCtx());
|
|
80
|
+
expect(configStore.logResolvedPaths).toHaveBeenCalledOnce();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("resolves agent name from ctx", async () => {
|
|
84
|
+
const ctx = makeCtx();
|
|
85
|
+
const { handler, session } = makeSetup();
|
|
86
|
+
const spy = vi.spyOn(session, "resolveAgentName");
|
|
87
|
+
await handler.handleSessionStart({ reason: "startup" }, ctx);
|
|
88
|
+
expect(spy).toHaveBeenCalledWith(ctx);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("notifies each policy issue", async () => {
|
|
92
|
+
const { handler, logger } = makeSetup({
|
|
93
|
+
configIssues: ["issue A", "issue B"],
|
|
94
|
+
});
|
|
95
|
+
await handler.handleSessionStart({ reason: "startup" }, makeCtx());
|
|
96
|
+
expect(logger.warn).toHaveBeenCalledWith("issue A");
|
|
97
|
+
expect(logger.warn).toHaveBeenCalledWith("issue B");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("does not warn when there are no policy issues", async () => {
|
|
101
|
+
const { handler, logger } = makeSetup();
|
|
102
|
+
await handler.handleSessionStart({ reason: "startup" }, makeCtx());
|
|
103
|
+
expect(logger.warn).not.toHaveBeenCalled();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("writes lifecycle.reload debug log when reason is reload", async () => {
|
|
107
|
+
const ctx = makeCtx({ cwd: "/proj" });
|
|
108
|
+
const { handler, logger } = makeSetup();
|
|
109
|
+
await handler.handleSessionStart({ reason: "reload" }, ctx);
|
|
110
|
+
expect(logger.debug).toHaveBeenCalledWith("lifecycle.reload", {
|
|
111
|
+
triggeredBy: "session_start",
|
|
112
|
+
reason: "reload",
|
|
113
|
+
cwd: "/proj",
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("does not write lifecycle.reload debug log for non-reload reasons", async () => {
|
|
118
|
+
const { handler, logger } = makeSetup();
|
|
119
|
+
await handler.handleSessionStart({ reason: "startup" }, makeCtx());
|
|
120
|
+
expect(logger.debug).not.toHaveBeenCalled();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("activates the service for the session with ctx", async () => {
|
|
124
|
+
const ctx = makeCtx();
|
|
125
|
+
const { handler, serviceLifecycle } = makeSetup();
|
|
126
|
+
await handler.handleSessionStart({ reason: "startup" }, ctx);
|
|
127
|
+
expect(serviceLifecycle.activate).toHaveBeenCalledWith(ctx);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("calls refreshConfig before resetForNewSession", async () => {
|
|
131
|
+
const callOrder: string[] = [];
|
|
132
|
+
const { handler, session, configStore } = makeSetup();
|
|
133
|
+
vi.spyOn(configStore, "refresh").mockImplementation(() => {
|
|
134
|
+
callOrder.push("refreshConfig");
|
|
135
|
+
});
|
|
136
|
+
vi.spyOn(session, "resetForNewSession").mockImplementation(() => {
|
|
137
|
+
callOrder.push("resetForNewSession");
|
|
138
|
+
});
|
|
139
|
+
await handler.handleSessionStart({ reason: "startup" }, makeCtx());
|
|
140
|
+
expect(callOrder).toEqual(["refreshConfig", "resetForNewSession"]);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// ── handleResourcesDiscover ────────────────────────────────────────────────
|
|
145
|
+
|
|
146
|
+
describe("handleResourcesDiscover", () => {
|
|
147
|
+
it("does nothing when reason is not reload", async () => {
|
|
148
|
+
const { handler, session } = makeSetup();
|
|
149
|
+
const spy = vi.spyOn(session, "reload");
|
|
150
|
+
await handler.handleResourcesDiscover({ reason: "startup" });
|
|
151
|
+
expect(spy).not.toHaveBeenCalled();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("calls reload on the session on reload", async () => {
|
|
155
|
+
const { handler, session } = makeSetup();
|
|
156
|
+
const spy = vi.spyOn(session, "reload");
|
|
157
|
+
await handler.handleResourcesDiscover({ reason: "reload" });
|
|
158
|
+
expect(spy).toHaveBeenCalledOnce();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("writes lifecycle.reload debug log on reload", async () => {
|
|
162
|
+
const ctx = makeCtx({ cwd: "/proj" });
|
|
163
|
+
const { handler, session, logger } = makeSetup();
|
|
164
|
+
session.activate(ctx);
|
|
165
|
+
await handler.handleResourcesDiscover({ reason: "reload" });
|
|
166
|
+
expect(logger.debug).toHaveBeenCalledWith("lifecycle.reload", {
|
|
167
|
+
triggeredBy: "resources_discover",
|
|
168
|
+
reason: "reload",
|
|
169
|
+
cwd: "/proj",
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("logs cwd as null when runtimeContext is null on reload", async () => {
|
|
174
|
+
const { handler, logger } = makeSetup();
|
|
175
|
+
await handler.handleResourcesDiscover({ reason: "reload" });
|
|
176
|
+
expect(logger.debug).toHaveBeenCalledWith("lifecycle.reload", {
|
|
177
|
+
triggeredBy: "resources_discover",
|
|
178
|
+
reason: "reload",
|
|
179
|
+
cwd: null,
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// ── handleSessionShutdown ──────────────────────────────────────────────────
|
|
185
|
+
|
|
186
|
+
describe("handleSessionShutdown", () => {
|
|
187
|
+
it("clears UI status when runtime context is present", async () => {
|
|
188
|
+
const ctx = makeCtx();
|
|
189
|
+
const { handler, session } = makeSetup();
|
|
190
|
+
session.activate(ctx);
|
|
191
|
+
await handler.handleSessionShutdown();
|
|
192
|
+
expect(ctx.ui.setStatus).toHaveBeenCalledWith(
|
|
193
|
+
"permission-system",
|
|
194
|
+
undefined,
|
|
195
|
+
);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("does not throw when runtime context is null", async () => {
|
|
199
|
+
const { handler } = makeSetup();
|
|
200
|
+
await expect(handler.handleSessionShutdown()).resolves.not.toThrow();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("calls shutdown on the session", async () => {
|
|
204
|
+
const { handler, session } = makeSetup();
|
|
205
|
+
const spy = vi.spyOn(session, "shutdown");
|
|
206
|
+
await handler.handleSessionShutdown();
|
|
207
|
+
expect(spy).toHaveBeenCalledOnce();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("calls serviceLifecycle.teardown", async () => {
|
|
211
|
+
const { handler, serviceLifecycle } = makeSetup();
|
|
212
|
+
await handler.handleSessionShutdown();
|
|
213
|
+
expect(serviceLifecycle.teardown).toHaveBeenCalledOnce();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("writes the decision-audit summary to the logger", async () => {
|
|
217
|
+
const { handler, audit, logger } = makeSetup();
|
|
218
|
+
await handler.handleSessionShutdown();
|
|
219
|
+
expect(audit.writeSummary).toHaveBeenCalledWith(logger);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fail-closed boundary is the only tool_call handler the SDK sees.
|
|
3
|
+
*
|
|
4
|
+
* The SDK's emitToolCall (@earendil-works/pi-coding-agent dist/core/extensions/
|
|
5
|
+
* runner.js) awaits the registered handler with NO try/catch — unlike
|
|
6
|
+
* emitUserBash directly below it, which catches and continues. So a thrown
|
|
7
|
+
* gate would otherwise yield no block and the command would run ungated with
|
|
8
|
+
* no trace. This boundary must absorb the throw and fail closed.
|
|
9
|
+
*/
|
|
10
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import { describe, expect, it, vi } from "vitest";
|
|
12
|
+
import type { GateOutcome } from "#src/handlers/gates/types";
|
|
13
|
+
import { createFailClosedToolCall } from "#src/handlers/tool-call-boundary";
|
|
14
|
+
|
|
15
|
+
import { makeReporter } from "#test/helpers/gate-fixtures";
|
|
16
|
+
import { makeCtx, makeToolCallEvent } from "#test/helpers/handler-fixtures";
|
|
17
|
+
|
|
18
|
+
function makeAudit() {
|
|
19
|
+
return {
|
|
20
|
+
recordDecision: vi.fn<(action: "allow" | "block") => void>(),
|
|
21
|
+
recordError: vi.fn<() => void>(),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function makeTracer() {
|
|
26
|
+
return {
|
|
27
|
+
debug: vi.fn<(event: string, details?: Record<string, unknown>) => void>(),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function gateReturning(outcome: GateOutcome) {
|
|
32
|
+
return vi
|
|
33
|
+
.fn<(event: unknown, ctx: ExtensionContext) => Promise<GateOutcome>>()
|
|
34
|
+
.mockResolvedValue(outcome);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe("createFailClosedToolCall", () => {
|
|
38
|
+
it("translates an allow outcome to the empty SDK shape", async () => {
|
|
39
|
+
const audit = makeAudit();
|
|
40
|
+
const reporter = makeReporter();
|
|
41
|
+
const boundary = createFailClosedToolCall(
|
|
42
|
+
gateReturning({ action: "allow" }),
|
|
43
|
+
reporter,
|
|
44
|
+
audit,
|
|
45
|
+
makeTracer(),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const result = await boundary(makeToolCallEvent("read"), makeCtx());
|
|
49
|
+
|
|
50
|
+
expect(result).toEqual({});
|
|
51
|
+
expect(audit.recordDecision).toHaveBeenCalledWith("allow");
|
|
52
|
+
expect(audit.recordError).not.toHaveBeenCalled();
|
|
53
|
+
expect(reporter.writeReviewLog).not.toHaveBeenCalled();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("translates a block outcome to the SDK block shape with the reason", async () => {
|
|
57
|
+
const audit = makeAudit();
|
|
58
|
+
const reporter = makeReporter();
|
|
59
|
+
const boundary = createFailClosedToolCall(
|
|
60
|
+
gateReturning({ action: "block", reason: "denied by policy" }),
|
|
61
|
+
reporter,
|
|
62
|
+
audit,
|
|
63
|
+
makeTracer(),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const result = await boundary(makeToolCallEvent("read"), makeCtx());
|
|
67
|
+
|
|
68
|
+
expect(result).toEqual({ block: true, reason: "denied by policy" });
|
|
69
|
+
expect(audit.recordDecision).toHaveBeenCalledWith("block");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("writes a per-call decision trace with the tool name and action", async () => {
|
|
73
|
+
const tracer = makeTracer();
|
|
74
|
+
const boundary = createFailClosedToolCall(
|
|
75
|
+
gateReturning({ action: "allow" }),
|
|
76
|
+
makeReporter(),
|
|
77
|
+
makeAudit(),
|
|
78
|
+
tracer,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
await boundary(makeToolCallEvent("bash"), makeCtx());
|
|
82
|
+
|
|
83
|
+
expect(tracer.debug).toHaveBeenCalledWith(
|
|
84
|
+
"permission.decision",
|
|
85
|
+
expect.objectContaining({ toolName: "bash", action: "allow" }),
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("blocks fail-closed when the gate throws, recording an error and a review-log entry", async () => {
|
|
90
|
+
const audit = makeAudit();
|
|
91
|
+
const reporter = makeReporter();
|
|
92
|
+
const gate = vi
|
|
93
|
+
.fn<(event: unknown, ctx: ExtensionContext) => Promise<GateOutcome>>()
|
|
94
|
+
.mockRejectedValue(new Error("parser init failed"));
|
|
95
|
+
const boundary = createFailClosedToolCall(
|
|
96
|
+
gate,
|
|
97
|
+
reporter,
|
|
98
|
+
audit,
|
|
99
|
+
makeTracer(),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const event = makeToolCallEvent("bash", {
|
|
103
|
+
input: { command: "cd /repo && git push" },
|
|
104
|
+
});
|
|
105
|
+
const result = await boundary(event, makeCtx());
|
|
106
|
+
|
|
107
|
+
expect((result as { block?: true }).block).toBe(true);
|
|
108
|
+
expect(audit.recordError).toHaveBeenCalledTimes(1);
|
|
109
|
+
expect(audit.recordDecision).not.toHaveBeenCalled();
|
|
110
|
+
expect(reporter.writeReviewLog).toHaveBeenCalledWith(
|
|
111
|
+
"permission_request.blocked",
|
|
112
|
+
expect.objectContaining({
|
|
113
|
+
toolName: "bash",
|
|
114
|
+
command: "cd /repo && git push",
|
|
115
|
+
resolution: "gate_error",
|
|
116
|
+
error: "parser init failed",
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("does not throw when the event is malformed and the gate throws", async () => {
|
|
122
|
+
const audit = makeAudit();
|
|
123
|
+
const reporter = makeReporter();
|
|
124
|
+
const gate = vi
|
|
125
|
+
.fn<(event: unknown, ctx: ExtensionContext) => Promise<GateOutcome>>()
|
|
126
|
+
.mockRejectedValue("non-error rejection");
|
|
127
|
+
const boundary = createFailClosedToolCall(
|
|
128
|
+
gate,
|
|
129
|
+
reporter,
|
|
130
|
+
audit,
|
|
131
|
+
makeTracer(),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const result = await boundary(undefined, makeCtx());
|
|
135
|
+
|
|
136
|
+
expect((result as { block?: true }).block).toBe(true);
|
|
137
|
+
expect(reporter.writeReviewLog).toHaveBeenCalledWith(
|
|
138
|
+
"permission_request.blocked",
|
|
139
|
+
expect.objectContaining({
|
|
140
|
+
resolution: "gate_error",
|
|
141
|
+
error: "non-error rejection",
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests that handleToolCall emits permissions:decision events at every
|
|
3
|
+
* gate resolution and fast-path site.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, it, vi } from "vitest";
|
|
6
|
+
|
|
7
|
+
import type { GatePrompter } from "#src/gate-prompter";
|
|
8
|
+
import {
|
|
9
|
+
getDecisionEvents,
|
|
10
|
+
makeCheckResult,
|
|
11
|
+
makeCtx,
|
|
12
|
+
makeHandler,
|
|
13
|
+
makeToolCallEvent,
|
|
14
|
+
} from "#test/helpers/handler-fixtures";
|
|
15
|
+
|
|
16
|
+
// ── policy_allow path ──────────────────────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
describe("handleToolCall decision events — policy_allow", () => {
|
|
19
|
+
it("emits allow with policy_allow when checkPermission returns allow", async () => {
|
|
20
|
+
const { handler, events } = makeHandler({
|
|
21
|
+
session: {
|
|
22
|
+
checkPermission: vi.fn().mockReturnValue(
|
|
23
|
+
makeCheckResult({
|
|
24
|
+
state: "allow",
|
|
25
|
+
origin: "global",
|
|
26
|
+
matchedPattern: "*",
|
|
27
|
+
}),
|
|
28
|
+
),
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
await handler.handleToolCall(makeToolCallEvent("read"), makeCtx());
|
|
33
|
+
|
|
34
|
+
const decisions = getDecisionEvents(events);
|
|
35
|
+
expect(decisions).toHaveLength(1);
|
|
36
|
+
expect(decisions[0]).toMatchObject({
|
|
37
|
+
surface: "read",
|
|
38
|
+
result: "allow",
|
|
39
|
+
resolution: "policy_allow",
|
|
40
|
+
origin: "global",
|
|
41
|
+
matchedPattern: "*",
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// ── policy_deny path ───────────────────────────────────────────────────────
|
|
47
|
+
|
|
48
|
+
describe("handleToolCall decision events — policy_deny", () => {
|
|
49
|
+
it("emits deny with policy_deny when checkPermission returns deny", async () => {
|
|
50
|
+
const { handler, events } = makeHandler({
|
|
51
|
+
session: {
|
|
52
|
+
checkPermission: vi.fn().mockReturnValue(
|
|
53
|
+
makeCheckResult({
|
|
54
|
+
state: "deny",
|
|
55
|
+
origin: "project",
|
|
56
|
+
matchedPattern: "read",
|
|
57
|
+
}),
|
|
58
|
+
),
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
await handler.handleToolCall(makeToolCallEvent("read"), makeCtx());
|
|
63
|
+
|
|
64
|
+
const decisions = getDecisionEvents(events);
|
|
65
|
+
expect(decisions).toHaveLength(1);
|
|
66
|
+
expect(decisions[0]).toMatchObject({
|
|
67
|
+
surface: "read",
|
|
68
|
+
result: "deny",
|
|
69
|
+
resolution: "policy_deny",
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// ── session_approved fast path ─────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
describe("handleToolCall decision events — session_approved", () => {
|
|
77
|
+
it("emits allow with session_approved when checkPermission returns source:session", async () => {
|
|
78
|
+
const { handler, events } = makeHandler({
|
|
79
|
+
session: {
|
|
80
|
+
checkPermission: vi.fn().mockReturnValue(
|
|
81
|
+
makeCheckResult({
|
|
82
|
+
state: "allow",
|
|
83
|
+
source: "session",
|
|
84
|
+
matchedPattern: "git *",
|
|
85
|
+
}),
|
|
86
|
+
),
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
await handler.handleToolCall(
|
|
91
|
+
makeToolCallEvent("bash", { input: { command: "git status" } }),
|
|
92
|
+
makeCtx(),
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const decisions = getDecisionEvents(events);
|
|
96
|
+
expect(decisions).toHaveLength(1);
|
|
97
|
+
expect(decisions[0]).toMatchObject({
|
|
98
|
+
surface: "bash",
|
|
99
|
+
result: "allow",
|
|
100
|
+
resolution: "session_approved",
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// ── user_approved path ─────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
describe("handleToolCall decision events — user_approved", () => {
|
|
108
|
+
it("emits allow with user_approved when state=ask and user approves once", async () => {
|
|
109
|
+
const { handler, events } = makeHandler({
|
|
110
|
+
session: {
|
|
111
|
+
checkPermission: vi
|
|
112
|
+
.fn()
|
|
113
|
+
.mockReturnValue(makeCheckResult({ state: "ask" })),
|
|
114
|
+
},
|
|
115
|
+
prompter: {
|
|
116
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
117
|
+
prompt: vi
|
|
118
|
+
.fn<GatePrompter["prompt"]>()
|
|
119
|
+
.mockResolvedValue({ approved: true, state: "approved" }),
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
await handler.handleToolCall(makeToolCallEvent("read"), makeCtx());
|
|
124
|
+
|
|
125
|
+
const decisions = getDecisionEvents(events);
|
|
126
|
+
expect(decisions).toHaveLength(1);
|
|
127
|
+
expect(decisions[0]).toMatchObject({
|
|
128
|
+
result: "allow",
|
|
129
|
+
resolution: "user_approved",
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("emits allow with user_approved_for_session when user approves for session", async () => {
|
|
134
|
+
const { handler, events } = makeHandler({
|
|
135
|
+
session: {
|
|
136
|
+
checkPermission: vi
|
|
137
|
+
.fn()
|
|
138
|
+
.mockReturnValue(makeCheckResult({ state: "ask" })),
|
|
139
|
+
},
|
|
140
|
+
prompter: {
|
|
141
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
142
|
+
prompt: vi.fn<GatePrompter["prompt"]>().mockResolvedValue({
|
|
143
|
+
approved: true,
|
|
144
|
+
state: "approved_for_session",
|
|
145
|
+
}),
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
await handler.handleToolCall(makeToolCallEvent("read"), makeCtx());
|
|
150
|
+
|
|
151
|
+
const decisions = getDecisionEvents(events);
|
|
152
|
+
expect(decisions).toHaveLength(1);
|
|
153
|
+
expect(decisions[0]).toMatchObject({
|
|
154
|
+
result: "allow",
|
|
155
|
+
resolution: "user_approved_for_session",
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// ── user_denied path ───────────────────────────────────────────────────────
|
|
161
|
+
|
|
162
|
+
describe("handleToolCall decision events — user_denied", () => {
|
|
163
|
+
it("emits deny with user_denied when state=ask and user denies", async () => {
|
|
164
|
+
const { handler, events } = makeHandler({
|
|
165
|
+
session: {
|
|
166
|
+
checkPermission: vi
|
|
167
|
+
.fn()
|
|
168
|
+
.mockReturnValue(makeCheckResult({ state: "ask" })),
|
|
169
|
+
},
|
|
170
|
+
prompter: {
|
|
171
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
172
|
+
prompt: vi
|
|
173
|
+
.fn<GatePrompter["prompt"]>()
|
|
174
|
+
.mockResolvedValue({ approved: false, state: "denied" }),
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
await handler.handleToolCall(makeToolCallEvent("read"), makeCtx());
|
|
179
|
+
|
|
180
|
+
const decisions = getDecisionEvents(events);
|
|
181
|
+
expect(decisions).toHaveLength(1);
|
|
182
|
+
expect(decisions[0]).toMatchObject({
|
|
183
|
+
result: "deny",
|
|
184
|
+
resolution: "user_denied",
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// ── confirmation_unavailable path ──────────────────────────────────────────
|
|
190
|
+
|
|
191
|
+
describe("handleToolCall decision events — confirmation_unavailable", () => {
|
|
192
|
+
it("emits deny with confirmation_unavailable when state=ask but no UI", async () => {
|
|
193
|
+
const { handler, events } = makeHandler({
|
|
194
|
+
session: {
|
|
195
|
+
checkPermission: vi
|
|
196
|
+
.fn()
|
|
197
|
+
.mockReturnValue(makeCheckResult({ state: "ask" })),
|
|
198
|
+
},
|
|
199
|
+
prompter: {
|
|
200
|
+
canConfirm: vi.fn().mockReturnValue(false),
|
|
201
|
+
prompt: vi.fn<GatePrompter["prompt"]>(),
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
await handler.handleToolCall(
|
|
206
|
+
makeToolCallEvent("read"),
|
|
207
|
+
makeCtx({ hasUI: false }),
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
const decisions = getDecisionEvents(events);
|
|
211
|
+
expect(decisions).toHaveLength(1);
|
|
212
|
+
expect(decisions[0]).toMatchObject({
|
|
213
|
+
result: "deny",
|
|
214
|
+
resolution: "confirmation_unavailable",
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// ── infrastructure_auto_allowed path ──────────────────────────────────────
|
|
220
|
+
|
|
221
|
+
describe("handleToolCall decision events — infrastructure_auto_allowed", () => {
|
|
222
|
+
it("emits allow with infrastructure_auto_allowed for Pi infra reads", async () => {
|
|
223
|
+
const infraDir = "/test/agent";
|
|
224
|
+
const { handler, events } = makeHandler({
|
|
225
|
+
session: {
|
|
226
|
+
checkPermission: vi.fn().mockReturnValue(makeCheckResult()),
|
|
227
|
+
getInfrastructureReadDirs: vi.fn().mockReturnValue([infraDir]),
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
const event = makeToolCallEvent("read", {
|
|
232
|
+
input: { path: `${infraDir}/some-file.json` },
|
|
233
|
+
});
|
|
234
|
+
await handler.handleToolCall(event, makeCtx());
|
|
235
|
+
|
|
236
|
+
const decisions = getDecisionEvents(events);
|
|
237
|
+
const infraEvents = decisions.filter(
|
|
238
|
+
(e) => e.resolution === "infrastructure_auto_allowed",
|
|
239
|
+
);
|
|
240
|
+
expect(infraEvents).toHaveLength(1);
|
|
241
|
+
expect(infraEvents[0]).toMatchObject({
|
|
242
|
+
result: "allow",
|
|
243
|
+
resolution: "infrastructure_auto_allowed",
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// ── auto_approved path (yolo mode) ───────────────────────────────────
|
|
249
|
+
|
|
250
|
+
describe("handleToolCall decision events — auto_approved", () => {
|
|
251
|
+
it("emits allow with auto_approved when prompt returns autoApproved:true", async () => {
|
|
252
|
+
const { handler, events } = makeHandler({
|
|
253
|
+
session: {
|
|
254
|
+
checkPermission: vi
|
|
255
|
+
.fn()
|
|
256
|
+
.mockReturnValue(makeCheckResult({ state: "ask" })),
|
|
257
|
+
},
|
|
258
|
+
prompter: {
|
|
259
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
260
|
+
prompt: vi.fn<GatePrompter["prompt"]>().mockResolvedValue({
|
|
261
|
+
approved: true,
|
|
262
|
+
state: "approved",
|
|
263
|
+
autoApproved: true,
|
|
264
|
+
}),
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
await handler.handleToolCall(makeToolCallEvent("read"), makeCtx());
|
|
269
|
+
|
|
270
|
+
const decisions = getDecisionEvents(events);
|
|
271
|
+
expect(decisions).toHaveLength(1);
|
|
272
|
+
expect(decisions[0]).toMatchObject({
|
|
273
|
+
result: "allow",
|
|
274
|
+
resolution: "auto_approved",
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
});
|