Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import type * as EffectAcpSchema from "effect-acp/compat";
8import * as NodePath from "node:path";
9
10import {
11 acpClientExecuteDisposition,
12 acpClientReadDisposition,
13 acpClientWriteDisposition,
14 acpMcpToolApprovalElicitationDisposition,
15 acpPermissionDisposition,
16 makeAcpClientPolicyGrants,
17 type AcpRuntimePolicy,
18} from "./AcpClientPolicy.ts";
19
20function permissionRequest(
21 kind: NonNullable<EffectAcpSchema.RequestPermissionRequest["toolCall"]["kind"]>,
22 locations?: ReadonlyArray<EffectAcpSchema.ToolCallLocation>,
23): EffectAcpSchema.RequestPermissionRequest {
24 return {
25 options: [],
26 sessionId: "permission-session",
27 toolCall: {
28 kind,
29 ...(locations === undefined ? {} : { locations }),
30 toolCallId: "permission-tool-call",
31 },
32 };
33}
34
35describe("acpPermissionDisposition", () => {
36 const cwd = NodePath.resolve(process.cwd(), "acp-permission-workspace");
37 const writableRoot = NodePath.resolve(process.cwd(), "acp-additional-writable-root");
38 const policy: AcpRuntimePolicy = {
39 runtimeMode: "full-access",
40 cwd,
41 approvalPolicy: "never",
42 sandboxPolicy: {
43 type: "workspaceWrite",
44 writableRoots: [writableRoot],
45 networkAccess: false,
46 },
47 };
48
49 it("auto-allows mutations only when every location is in cwd or an additional writable root", () => {
50 assert.equal(
51 acpPermissionDisposition(policy, permissionRequest("edit", [{ path: "src/index.ts" }])),
52 "allow",
53 );
54 assert.equal(
55 acpPermissionDisposition(
56 policy,
57 permissionRequest("delete", [{ path: NodePath.join(writableRoot, "generated.ts") }]),
58 ),
59 "allow",
60 );
61 assert.equal(
62 acpPermissionDisposition(
63 policy,
64 permissionRequest("move", [
65 { path: NodePath.join(cwd, "from.ts") },
66 { path: NodePath.join(writableRoot, "to.ts") },
67 ]),
68 ),
69 "allow",
70 );
71 });
72
73 it("denies missing or out-of-root mutation locations", () => {
74 const outside = NodePath.resolve(process.cwd(), "outside-acp-permission-workspace", "file.ts");
75 assert.equal(acpPermissionDisposition(policy, permissionRequest("edit")), "deny");
76 assert.equal(
77 acpPermissionDisposition(policy, permissionRequest("delete", [{ path: "../escape.ts" }])),
78 "deny",
79 );
80 assert.equal(
81 acpPermissionDisposition(
82 policy,
83 permissionRequest("move", [{ path: NodePath.join(cwd, "inside.ts") }, { path: outside }]),
84 ),
85 "deny",
86 );
87 });
88
89 it("keeps non-mutating workspace permissions and denials unchanged", () => {
90 assert.equal(acpPermissionDisposition(policy, permissionRequest("read")), "allow");
91 assert.equal(acpPermissionDisposition(policy, permissionRequest("execute")), "deny");
92 });
93
94 it.effect("denies mutations through workspace symlinks that escape the writable roots", () =>
95 Effect.gen(function* () {
96 const fileSystem = yield* FileSystem.FileSystem;
97 const path = yield* Path.Path;
98 const workspace = yield* fileSystem.makeTempDirectoryScoped({
99 prefix: "t3-acp-permission-workspace-",
100 });
101 const outside = yield* fileSystem.makeTempDirectoryScoped({
102 prefix: "t3-acp-permission-outside-",
103 });
104 const outsideFile = path.join(outside, "existing.ts");
105 yield* fileSystem.writeFileString(outsideFile, "outside");
106 yield* fileSystem.symlink(outsideFile, path.join(workspace, "linked-file.ts"));
107 yield* fileSystem.symlink(outside, path.join(workspace, "linked-directory"));
108
109 const realPolicy: AcpRuntimePolicy = {
110 runtimeMode: "full-access",
111 cwd: workspace,
112 approvalPolicy: "never",
113 sandboxPolicy: {
114 type: "workspaceWrite",
115 writableRoots: [],
116 networkAccess: false,
117 },
118 };
119
120 assert.equal(
121 acpPermissionDisposition(
122 realPolicy,
123 permissionRequest("edit", [{ path: "linked-file.ts" }]),
124 ),
125 "deny",
126 );
127 assert.equal(
128 acpPermissionDisposition(
129 realPolicy,
130 permissionRequest("edit", [{ path: "linked-directory/new-file.ts" }]),
131 ),
132 "deny",
133 "a missing leaf below an escaping directory symlink must not be auto-approved",
134 );
135 assert.equal(
136 acpPermissionDisposition(
137 realPolicy,
138 permissionRequest("edit", [{ path: "linked-directory/../escaped-file.ts" }]),
139 ),
140 "deny",
141 "physical symlink traversal must be resolved before parent segments",
142 );
143 }).pipe(Effect.provide(NodeServices.layer)),
144 );
145
146 it.effect("allows existing and new files beneath canonical writable roots", () =>
147 Effect.gen(function* () {
148 const fileSystem = yield* FileSystem.FileSystem;
149 const path = yield* Path.Path;
150 const workspace = yield* fileSystem.makeTempDirectoryScoped({
151 prefix: "t3-acp-permission-workspace-",
152 });
153 const workspaceLinkParent = yield* fileSystem.makeTempDirectoryScoped({
154 prefix: "t3-acp-permission-link-parent-",
155 });
156 const workspaceLink = path.join(workspaceLinkParent, "workspace-link");
157 yield* fileSystem.makeDirectory(path.join(workspace, "src"), { recursive: true });
158 yield* fileSystem.writeFileString(path.join(workspace, "src", "existing.ts"), "existing");
159 yield* fileSystem.symlink(workspace, workspaceLink);
160
161 const realPolicy: AcpRuntimePolicy = {
162 runtimeMode: "full-access",
163 cwd: workspaceLink,
164 approvalPolicy: "never",
165 sandboxPolicy: {
166 type: "workspaceWrite",
167 writableRoots: [],
168 networkAccess: false,
169 },
170 };
171
172 assert.equal(
173 acpPermissionDisposition(
174 realPolicy,
175 permissionRequest("edit", [{ path: "src/existing.ts" }]),
176 ),
177 "allow",
178 );
179 assert.equal(
180 acpPermissionDisposition(
181 realPolicy,
182 permissionRequest("edit", [{ path: "src/generated/new-file.ts" }]),
183 ),
184 "allow",
185 "non-existent descendants of a real in-root ancestor remain writable",
186 );
187 }).pipe(Effect.provide(NodeServices.layer)),
188 );
189});
190
191describe("acpMcpToolApprovalElicitationDisposition", () => {
192 it("applies runtime policy only to explicitly tagged MCP approval elicitations", () => {
193 const fullAccess: AcpRuntimePolicy = {
194 runtimeMode: "full-access",
195 cwd: process.cwd(),
196 };
197 const approvalRequired: AcpRuntimePolicy = {
198 runtimeMode: "approval-required",
199 cwd: process.cwd(),
200 };
201 const tagged = {
202 sessionId: "session-1",
203 message: "Approve this request?",
204 mode: "form",
205 requestedSchema: { type: "object", properties: {} },
206 _meta: { codex_approval_kind: "mcp_tool_call" },
207 } satisfies EffectAcpSchema.CreateElicitationRequest;
208
209 assert.equal(acpMcpToolApprovalElicitationDisposition(fullAccess, tagged), "allow");
210 assert.equal(acpMcpToolApprovalElicitationDisposition(approvalRequired, tagged), "ask");
211 assert.equal(
212 acpMcpToolApprovalElicitationDisposition(
213 { ...fullAccess, approvalPolicy: "on-request" },
214 tagged,
215 ),
216 "ask",
217 );
218 assert.equal(
219 acpMcpToolApprovalElicitationDisposition(
220 {
221 runtimeMode: "auto-accept-edits",
222 cwd: process.cwd(),
223 approvalPolicy: "never",
224 sandboxPolicy: {
225 type: "workspaceWrite",
226 writableRoots: [],
227 networkAccess: false,
228 },
229 },
230 tagged,
231 ),
232 "allow",
233 );
234 const { _meta: _tag, ...untagged } = tagged;
235 assert.equal(
236 acpMcpToolApprovalElicitationDisposition(
237 fullAccess,
238 untagged,
239 "mcp_tool_call_approval_exec-123",
240 ),
241 "allow",
242 );
243 assert.isUndefined(
244 acpMcpToolApprovalElicitationDisposition(fullAccess, {
245 ...tagged,
246 _meta: { codex_approval_kind: "ordinary_form" },
247 }),
248 );
249 assert.isUndefined(
250 acpMcpToolApprovalElicitationDisposition(fullAccess, {
251 sessionId: "session-1",
252 message: "Authenticate",
253 mode: "url",
254 elicitationId: "elicitation-1",
255 url: "https://example.com/login",
256 _meta: { codex_approval_kind: "mcp_tool_call" },
257 }),
258 );
259 });
260});
261
262describe("client-mediated dispositions", () => {
263 const cwd = NodePath.resolve(process.cwd(), "acp-client-policy-workspace");
264
265 it("asks in approval-required mode for reads, writes, and terminals", () => {
266 const policy: AcpRuntimePolicy = { runtimeMode: "approval-required", cwd };
267 assert.equal(acpClientReadDisposition(policy, NodePath.join(cwd, "file.ts")), "ask");
268 assert.equal(acpClientWriteDisposition(policy, NodePath.join(cwd, "file.ts")), "ask");
269 assert.equal(acpClientExecuteDisposition(policy), "ask");
270 });
271
272 it("allows in auto and full-access modes without an explicit sandbox", () => {
273 for (const runtimeMode of ["auto", "auto-accept-edits", "full-access"] as const) {
274 const policy: AcpRuntimePolicy = { runtimeMode, cwd };
275 assert.equal(acpClientReadDisposition(policy, NodePath.join(cwd, "file.ts")), "allow");
276 assert.equal(acpClientWriteDisposition(policy, NodePath.join(cwd, "file.ts")), "allow");
277 assert.equal(acpClientExecuteDisposition(policy), "allow");
278 }
279 });
280
281 it("allows reads but denies writes and terminals under an explicit read-only sandbox", () => {
282 const policy: AcpRuntimePolicy = {
283 runtimeMode: "full-access",
284 cwd,
285 approvalPolicy: "never",
286 sandboxPolicy: { type: "readOnly" },
287 };
288 assert.equal(acpClientReadDisposition(policy, NodePath.join(cwd, "file.ts")), "allow");
289 assert.equal(acpClientWriteDisposition(policy, NodePath.join(cwd, "file.ts")), "deny");
290 assert.equal(acpClientExecuteDisposition(policy), "deny");
291 });
292
293 it("allows reads, confines writes, and denies terminals under workspace-write", () => {
294 const policy: AcpRuntimePolicy = {
295 runtimeMode: "full-access",
296 cwd,
297 approvalPolicy: "never",
298 sandboxPolicy: { type: "workspaceWrite", writableRoots: [], networkAccess: false },
299 };
300 assert.equal(acpClientReadDisposition(policy, "/tmp/outside-workspace/file.ts"), "allow");
301 assert.equal(acpClientWriteDisposition(policy, NodePath.join(cwd, "src/file.ts")), "allow");
302 assert.equal(acpClientWriteDisposition(policy, "/tmp/outside-workspace/file.ts"), "deny");
303 assert.equal(acpClientExecuteDisposition(policy), "deny");
304 });
305});
306
307describe("makeAcpClientPolicyGrants", () => {
308 const cwd = NodePath.resolve(process.cwd(), "acp-grant-workspace");
309 const filePath = NodePath.join(cwd, "src", "granted.ts");
310
311 it("grants writes to approved locations for the approving turn only", () => {
312 const grants = makeAcpClientPolicyGrants();
313 grants.recordApproval({
314 kind: "file-change",
315 locations: [filePath],
316 cwd,
317 scope: "turn",
318 turnKey: "turn-1",
319 });
320 assert.isTrue(grants.allowsWrite({ path: filePath, cwd, turnKey: "turn-1" }));
321 assert.isFalse(
322 grants.allowsWrite({ path: NodePath.join(cwd, "src", "other.ts"), cwd, turnKey: "turn-1" }),
323 );
324 assert.isFalse(grants.allowsWrite({ path: filePath, cwd, turnKey: "turn-2" }));
325 assert.isFalse(grants.allowsWrite({ path: filePath, cwd, turnKey: null }));
326 });
327
328 it("keeps accept-for-session grants across turns", () => {
329 const grants = makeAcpClientPolicyGrants();
330 grants.recordApproval({
331 kind: "file-change",
332 locations: [filePath],
333 cwd,
334 scope: "session",
335 turnKey: "turn-1",
336 });
337 assert.isTrue(grants.allowsWrite({ path: filePath, cwd, turnKey: "turn-9" }));
338 assert.isTrue(grants.allowsWrite({ path: filePath, cwd, turnKey: null }));
339 });
340
341 it("treats a location-free file change as an unscoped write grant", () => {
342 const grants = makeAcpClientPolicyGrants();
343 grants.recordApproval({
344 kind: "file-change",
345 locations: [],
346 cwd,
347 scope: "turn",
348 turnKey: "turn-1",
349 });
350 assert.isTrue(
351 grants.allowsWrite({ path: NodePath.join(cwd, "anything.ts"), cwd, turnKey: "turn-1" }),
352 );
353 assert.isFalse(
354 grants.allowsWrite({ path: NodePath.join(cwd, "anything.ts"), cwd, turnKey: "turn-2" }),
355 );
356 });
357
358 it("grants reads only to approved locations and terminals only from commands", () => {
359 const grants = makeAcpClientPolicyGrants();
360 grants.recordApproval({
361 kind: "file-read",
362 locations: [filePath],
363 cwd,
364 scope: "turn",
365 turnKey: "turn-1",
366 });
367 assert.isTrue(grants.allowsRead({ path: filePath, cwd, turnKey: "turn-1" }));
368 assert.isFalse(
369 grants.allowsRead({ path: NodePath.join(cwd, "src", "other.ts"), cwd, turnKey: "turn-1" }),
370 );
371 assert.isFalse(grants.allowsRead({ path: filePath, cwd, turnKey: "turn-2" }));
372 assert.isFalse(grants.allowsExecute("turn-1"));
373 assert.isFalse(grants.allowsWrite({ path: filePath, cwd, turnKey: "turn-1" }));
374 grants.recordApproval({
375 kind: "command",
376 locations: [],
377 cwd,
378 scope: "turn",
379 turnKey: "turn-1",
380 });
381 assert.isTrue(grants.allowsExecute("turn-1"));
382 assert.isFalse(grants.allowsExecute("turn-2"));
383 assert.isFalse(grants.allowsExecute(null));
384 });
385
386 it("starting a later turn's grants drops the previous turn's grants", () => {
387 const grants = makeAcpClientPolicyGrants();
388 grants.recordApproval({
389 kind: "command",
390 locations: [],
391 cwd,
392 scope: "turn",
393 turnKey: "turn-1",
394 });
395 grants.recordApproval({
396 kind: "file-change",
397 locations: [filePath],
398 cwd,
399 scope: "turn",
400 turnKey: "turn-2",
401 });
402 assert.isFalse(grants.allowsExecute("turn-1"));
403 assert.isTrue(grants.allowsWrite({ path: filePath, cwd, turnKey: "turn-2" }));
404 });
405
406 it.effect("matches grants canonically so symlink escapes stay outside the grant", () =>
407 Effect.gen(function* () {
408 const fileSystem = yield* FileSystem.FileSystem;
409 const path = yield* Path.Path;
410 const workspace = yield* fileSystem.makeTempDirectoryScoped({
411 prefix: "t3-acp-grant-workspace-",
412 });
413 const outside = yield* fileSystem.makeTempDirectoryScoped({
414 prefix: "t3-acp-grant-outside-",
415 });
416 yield* fileSystem.makeDirectory(path.join(workspace, "approved"), { recursive: true });
417 yield* fileSystem.symlink(outside, path.join(workspace, "approved", "linked"));
418
419 const grants = makeAcpClientPolicyGrants();
420 grants.recordApproval({
421 kind: "file-change",
422 locations: [path.join(workspace, "approved")],
423 cwd: workspace,
424 scope: "turn",
425 turnKey: "turn-1",
426 });
427 assert.isTrue(
428 grants.allowsWrite({
429 path: path.join(workspace, "approved", "inside.ts"),
430 cwd: workspace,
431 turnKey: "turn-1",
432 }),
433 );
434 assert.isFalse(
435 grants.allowsWrite({
436 path: path.join(workspace, "approved", "linked", "escaped.ts"),
437 cwd: workspace,
438 turnKey: "turn-1",
439 }),
440 "a write through an escaping symlink resolves outside the granted root",
441 );
442 }).pipe(Effect.provide(NodeServices.layer)),
443 );
444});