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 acpChangedPathsOfToolCall,
12 acpClientExecuteDisposition,
13 acpMcpToolApprovalElicitationDisposition,
14 acpPermissionDisposition,
15 makeAcpClientPolicyGrants,
16 mergeAcpChangedPaths,
17 type AcpRuntimePolicy,
18} from "./clientPolicy.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 // A multi-folder thread's other folders are writable roots too.
72 const tracked: AcpRuntimePolicy = {
73 ...policy,
74 sandboxPolicy: { type: "workspaceWrite", networkAccess: false },
75 workspace: { extraRoots: [writableRoot] },
76 };
77 const edit = (path: string) =>
78 acpPermissionDisposition(tracked, permissionRequest("edit", [{ path }]));
79 assert.equal(edit(NodePath.join(writableRoot, "generated.ts")), "allow");
80 assert.equal(edit(NodePath.resolve(process.cwd(), "untracked-folder", "file.ts")), "deny");
81 });
82
83 it("denies missing or out-of-root mutation locations", () => {
84 const outside = NodePath.resolve(process.cwd(), "outside-acp-permission-workspace", "file.ts");
85 assert.equal(acpPermissionDisposition(policy, permissionRequest("edit")), "deny");
86 assert.equal(
87 acpPermissionDisposition(policy, permissionRequest("delete", [{ path: "../escape.ts" }])),
88 "deny",
89 );
90 assert.equal(
91 acpPermissionDisposition(
92 policy,
93 permissionRequest("move", [{ path: NodePath.join(cwd, "inside.ts") }, { path: outside }]),
94 ),
95 "deny",
96 );
97 });
98
99 it("keeps non-mutating workspace permissions and denials unchanged", () => {
100 assert.equal(acpPermissionDisposition(policy, permissionRequest("read")), "allow");
101 assert.equal(acpPermissionDisposition(policy, permissionRequest("execute")), "deny");
102 });
103
104 it("auto-accept-edits approves file changes inside the thread's folders and asks for the rest", () => {
105 const autoAcceptEdits: AcpRuntimePolicy = { runtimeMode: "auto-accept-edits", cwd };
106 const outside = NodePath.resolve(process.cwd(), "outside-acp-permission-workspace", "file.ts");
107 for (const kind of ["edit", "delete", "move"] as const) {
108 assert.equal(
109 acpPermissionDisposition(autoAcceptEdits, permissionRequest(kind, [{ path: "src/a.ts" }])),
110 "allow",
111 );
112 assert.equal(
113 acpPermissionDisposition(autoAcceptEdits, permissionRequest(kind, [{ path: outside }])),
114 "ask",
115 );
116 assert.equal(
117 acpPermissionDisposition(
118 autoAcceptEdits,
119 permissionRequest(kind, [{ path: NodePath.join(cwd, "inside.ts") }, { path: outside }]),
120 ),
121 "ask",
122 );
123 assert.equal(
124 acpPermissionDisposition(autoAcceptEdits, permissionRequest(kind, [{ path: "../up.ts" }])),
125 "ask",
126 );
127 // Without a location there is no folder to check, so the user decides.
128 assert.equal(acpPermissionDisposition(autoAcceptEdits, permissionRequest(kind)), "ask");
129 }
130 // A multi-folder thread's other folders count as its own.
131 assert.equal(
132 acpPermissionDisposition(
133 { ...autoAcceptEdits, workspace: { extraRoots: [writableRoot] } },
134 permissionRequest("edit", [{ path: NodePath.join(writableRoot, "generated.ts") }]),
135 ),
136 "allow",
137 );
138 for (const kind of ["execute", "fetch", "other"] as const) {
139 assert.equal(acpPermissionDisposition(autoAcceptEdits, permissionRequest(kind)), "ask");
140 }
141 assert.equal(acpPermissionDisposition(autoAcceptEdits, permissionRequest("read")), "allow");
142 assert.equal(
143 acpPermissionDisposition(
144 { ...autoAcceptEdits, approvalPolicy: "on-request" },
145 permissionRequest("edit", [{ path: "src/a.ts" }]),
146 ),
147 "ask",
148 );
149 // Full access and Auto are not narrowed.
150 for (const runtimeMode of ["full-access", "auto"] as const) {
151 assert.equal(
152 acpPermissionDisposition(
153 { runtimeMode, cwd },
154 permissionRequest("edit", [{ path: outside }]),
155 ),
156 "allow",
157 );
158 }
159 });
160
161 it.effect("auto-accept-edits asks for edits that leave the folder through a link", () =>
162 Effect.gen(function* () {
163 const fileSystem = yield* FileSystem.FileSystem;
164 const path = yield* Path.Path;
165 const workspace = yield* fileSystem.makeTempDirectoryScoped({
166 prefix: "t3-acp-permission-workspace-",
167 });
168 const outside = yield* fileSystem.makeTempDirectoryScoped({
169 prefix: "t3-acp-permission-outside-",
170 });
171 yield* fileSystem.symlink(outside, path.join(workspace, "linked-directory"));
172 yield* fileSystem.makeDirectory(path.join(workspace, "src"));
173 const autoAcceptEdits: AcpRuntimePolicy = {
174 runtimeMode: "auto-accept-edits",
175 cwd: workspace,
176 };
177 const edit = (target: string) =>
178 acpPermissionDisposition(autoAcceptEdits, permissionRequest("edit", [{ path: target }]));
179
180 assert.equal(edit("src/new-file.ts"), "allow");
181 assert.equal(edit("linked-directory/new-file.ts"), "ask");
182 assert.equal(edit(path.join(outside, "new-file.ts")), "ask");
183 }).pipe(Effect.provide(NodeServices.layer)),
184 );
185
186 it.effect("checks a file change's path exactly as the agent sent it", () =>
187 Effect.gen(function* () {
188 const fileSystem = yield* FileSystem.FileSystem;
189 const path = yield* Path.Path;
190 const workspace = yield* fileSystem.makeTempDirectoryScoped({
191 prefix: "t3-acp-permission-workspace-",
192 });
193 const outside = yield* fileSystem.makeTempDirectoryScoped({
194 prefix: "t3-acp-permission-outside-",
195 });
196 // Two different files on POSIX: `target` stays in the folder, `target ` leads out of it.
197 yield* fileSystem.writeFileString(path.join(workspace, "target"), "");
198 yield* fileSystem.symlink(path.join(outside, "file"), path.join(workspace, "target "));
199 yield* fileSystem.symlink(outside, path.join(workspace, " linked"));
200 yield* fileSystem.makeDirectory(path.join(workspace, "linked"));
201 const disposition = (sandboxPolicy: AcpRuntimePolicy["sandboxPolicy"], target: string) =>
202 acpPermissionDisposition(
203 {
204 runtimeMode: sandboxPolicy === undefined ? "auto-accept-edits" : "full-access",
205 cwd: workspace,
206 ...(sandboxPolicy === undefined ? {} : { approvalPolicy: "never", sandboxPolicy }),
207 },
208 permissionRequest("edit", [{ path: target }]),
209 );
210 for (const [sandboxPolicy, refused] of [
211 [undefined, "ask"],
212 [{ type: "workspaceWrite", writableRoots: [] }, "deny"],
213 ] as const) {
214 assert.equal(disposition(sandboxPolicy, path.join(workspace, "target")), "allow");
215 assert.equal(disposition(sandboxPolicy, "target"), "allow");
216 assert.equal(disposition(sandboxPolicy, "linked/new.ts"), "allow");
217 if (NodePath.sep !== "/") continue;
218 assert.equal(disposition(sandboxPolicy, path.join(workspace, "target ")), refused);
219 assert.equal(disposition(sandboxPolicy, "target "), refused);
220 assert.equal(disposition(sandboxPolicy, " linked/new.ts"), refused);
221 }
222 }).pipe(Effect.provide(NodeServices.layer)),
223 );
224
225 it("asks about a file change whose path may mean another file to the agent", () => {
226 const autoAcceptEdits: AcpRuntimePolicy = { runtimeMode: "auto-accept-edits", cwd };
227 const edit = (path: unknown) =>
228 acpPermissionDisposition(
229 autoAcceptEdits,
230 permissionRequest("edit", [{ path }] as ReadonlyArray<EffectAcpSchema.ToolCallLocation>),
231 );
232 assert.equal(edit("src/a.ts"), "allow");
233 assert.equal(edit("./src/../src/a.ts"), "allow");
234 for (const path of [
235 "",
236 " ",
237 "\n",
238 7,
239 null,
240 undefined,
241 ["src/a.ts"],
242 "~",
243 "~/notes.txt",
244 "~other/notes.txt",
245 `file://${cwd}/src/a.ts`,
246 "file:src/a.ts",
247 "vscode-remote://host/src/a.ts",
248 "src/a.ts\0",
249 `${cwd}/src/a.ts\0.png`,
250 "src/../../up.ts",
251 `${cwd}/../up.ts`,
252 ]) {
253 assert.equal(edit(path), "ask", JSON.stringify(path));
254 }
255 if (NodePath.sep === "/") {
256 for (const path of ["C:\\work\\a.ts", "C:a.ts", "\\\\host\\share\\a.ts", "src\\..\\a.ts"]) {
257 assert.equal(edit(path), "ask", path);
258 }
259 }
260 });
261
262 it("reads a request's diffs and raw input as well as its locations", () => {
263 const autoAcceptEdits: AcpRuntimePolicy = { runtimeMode: "auto-accept-edits", cwd };
264 const outside = NodePath.resolve(process.cwd(), "outside-acp-permission-workspace", "file.ts");
265 const edit = (toolCall: Record<string, unknown>) =>
266 acpPermissionDisposition(autoAcceptEdits, {
267 options: [],
268 sessionId: "permission-session",
269 toolCall: { kind: "edit", toolCallId: "permission-tool-call", ...toolCall },
270 } as EffectAcpSchema.RequestPermissionRequest);
271 const inside = [{ path: "src/a.ts" }];
272 assert.equal(edit({ locations: inside, rawInput: { path: "src/a.ts" } }), "allow");
273 assert.equal(edit({ content: [{ type: "diff", path: "src/a.ts", newText: "" }] }), "allow");
274 assert.equal(edit({ rawInput: { file_path: "src/a.ts", command: "write" } }), "allow");
275 for (const toolCall of [
276 { locations: inside, content: [{ type: "diff", path: outside, newText: "" }] },
277 { locations: inside, rawInput: { path: outside } },
278 // The same key under another spelling still counts.
279 { locations: inside, rawInput: { path: "src/a.ts", filePath: outside } },
280 { locations: inside, rawInput: { path: "src/a.ts", file_path: outside } },
281 { locations: inside, rawInput: { path: 7 } },
282 { locations: inside, rawInput: { path: " " } },
283 { locations: inside, content: [{ type: "diff", newText: "" }] },
284 { locations: inside, content: [{ type: "diff", changes: {} }] },
285 { locations: inside, content: [{ type: "_t3_unknown", originalType: "_patch", raw: {} }] },
286 { locations: inside, content: "diff" },
287 { locations: { path: "src/a.ts" } },
288 // A diff that carries a path beside its changes changes both.
289 {
290 content: [
291 { type: "diff", path: outside, changes: [{ operation: "modify", path: "src/a.ts" }] },
292 ],
293 },
294 { content: [{ type: "diff", changes: [{ operation: "_swap", path: "src/a.ts" }] }] },
295 {
296 content: [
297 { type: "diff", changes: [{ operation: "move", oldPath: outside, path: "src/a.ts" }] },
298 ],
299 },
300 { content: [{ type: "diff", changes: [{ operation: "move", path: "src/a.ts" }] }] },
301 // No path at all.
302 {},
303 { locations: [] },
304 { rawInput: "src/a.ts" },
305 ]) {
306 assert.equal(edit(toolCall), "ask", JSON.stringify(toolCall));
307 }
308 });
309
310 it("does not approve a file change that names very many paths", () => {
311 const autoAcceptEdits: AcpRuntimePolicy = { runtimeMode: "auto-accept-edits", cwd };
312 const edit = (count: number) =>
313 acpPermissionDisposition(
314 autoAcceptEdits,
315 permissionRequest(
316 "edit",
317 Array.from({ length: count }, (_, index) => ({ path: `src/${index}.ts` })),
318 ),
319 );
320 assert.equal(edit(256), "allow");
321 assert.equal(edit(257), "ask");
322 // The same path named again is one path.
323 assert.equal(
324 acpPermissionDisposition(
325 autoAcceptEdits,
326 permissionRequest(
327 "edit",
328 Array.from({ length: 1_000 }, () => ({ path: "src/a.ts" })),
329 ),
330 ),
331 "allow",
332 );
333 });
334
335 it("auto-allows read-kind permission requests under on-request approval", () => {
336 for (const runtimePolicy of [
337 { ...policy, approvalPolicy: "on-request" },
338 { ...policy, approvalPolicy: "on-request", sandboxPolicy: { type: "readOnly" } },
339 { runtimeMode: "approval-required", cwd },
340 ] satisfies ReadonlyArray<AcpRuntimePolicy>) {
341 for (const kind of ["read", "search", "think"] as const) {
342 assert.equal(acpPermissionDisposition(runtimePolicy, permissionRequest(kind)), "allow");
343 }
344 for (const kind of ["edit", "delete", "move", "execute", "fetch", "other"] as const) {
345 assert.equal(acpPermissionDisposition(runtimePolicy, permissionRequest(kind)), "ask");
346 }
347 }
348 });
349
350 it.effect("denies mutations through workspace symlinks that escape the writable roots", () =>
351 Effect.gen(function* () {
352 const fileSystem = yield* FileSystem.FileSystem;
353 const path = yield* Path.Path;
354 const workspace = yield* fileSystem.makeTempDirectoryScoped({
355 prefix: "t3-acp-permission-workspace-",
356 });
357 const outside = yield* fileSystem.makeTempDirectoryScoped({
358 prefix: "t3-acp-permission-outside-",
359 });
360 const outsideFile = path.join(outside, "existing.ts");
361 yield* fileSystem.writeFileString(outsideFile, "outside");
362 yield* fileSystem.symlink(outsideFile, path.join(workspace, "linked-file.ts"));
363 yield* fileSystem.symlink(outside, path.join(workspace, "linked-directory"));
364
365 const realPolicy: AcpRuntimePolicy = {
366 runtimeMode: "full-access",
367 cwd: workspace,
368 approvalPolicy: "never",
369 sandboxPolicy: {
370 type: "workspaceWrite",
371 writableRoots: [],
372 networkAccess: false,
373 },
374 };
375
376 assert.equal(
377 acpPermissionDisposition(
378 realPolicy,
379 permissionRequest("edit", [{ path: "linked-file.ts" }]),
380 ),
381 "deny",
382 );
383 assert.equal(
384 acpPermissionDisposition(
385 realPolicy,
386 permissionRequest("edit", [{ path: "linked-directory/new-file.ts" }]),
387 ),
388 "deny",
389 "a missing leaf below an escaping directory symlink must not be auto-approved",
390 );
391 assert.equal(
392 acpPermissionDisposition(
393 realPolicy,
394 permissionRequest("edit", [{ path: "linked-directory/../escaped-file.ts" }]),
395 ),
396 "deny",
397 "physical symlink traversal must be resolved before parent segments",
398 );
399 }).pipe(Effect.provide(NodeServices.layer)),
400 );
401
402 it.effect("allows existing and new files beneath canonical writable roots", () =>
403 Effect.gen(function* () {
404 const fileSystem = yield* FileSystem.FileSystem;
405 const path = yield* Path.Path;
406 const workspace = yield* fileSystem.makeTempDirectoryScoped({
407 prefix: "t3-acp-permission-workspace-",
408 });
409 const workspaceLinkParent = yield* fileSystem.makeTempDirectoryScoped({
410 prefix: "t3-acp-permission-link-parent-",
411 });
412 const workspaceLink = path.join(workspaceLinkParent, "workspace-link");
413 yield* fileSystem.makeDirectory(path.join(workspace, "src"), { recursive: true });
414 yield* fileSystem.writeFileString(path.join(workspace, "src", "existing.ts"), "existing");
415 yield* fileSystem.symlink(workspace, workspaceLink);
416
417 const realPolicy: AcpRuntimePolicy = {
418 runtimeMode: "full-access",
419 cwd: workspaceLink,
420 approvalPolicy: "never",
421 sandboxPolicy: {
422 type: "workspaceWrite",
423 writableRoots: [],
424 networkAccess: false,
425 },
426 };
427
428 assert.equal(
429 acpPermissionDisposition(
430 realPolicy,
431 permissionRequest("edit", [{ path: "src/existing.ts" }]),
432 ),
433 "allow",
434 );
435 assert.equal(
436 acpPermissionDisposition(
437 realPolicy,
438 permissionRequest("edit", [{ path: "src/generated/new-file.ts" }]),
439 ),
440 "allow",
441 "non-existent descendants of a real in-root ancestor remain writable",
442 );
443 }).pipe(Effect.provide(NodeServices.layer)),
444 );
445});
446
447describe("acpChangedPathsOfToolCall", () => {
448 it("names every path of the locations, diffs and raw input once, as sent", () => {
449 assert.deepEqual(
450 acpChangedPathsOfToolCall({
451 locations: [{ path: "/work/a.ts", line: 3 }, { path: " /work/a.ts" }],
452 content: [
453 { type: "content", content: { type: "text", text: "/elsewhere/ignored.ts" } },
454 { type: "terminal", terminalId: "terminal-1" },
455 { type: "diff", path: "/work/b.ts ", newText: "b" },
456 {
457 type: "diff",
458 changes: [
459 { operation: "add", path: "/work/c.ts" },
460 { operation: "delete", path: "/work/d.ts" },
461 { operation: "modify", path: "/work/a.ts" },
462 { operation: "move", oldPath: "/work/e.ts", path: "/work/f.ts" },
463 // A copy only reads its source.
464 { operation: "copy", oldPath: "/elsewhere/g.ts", path: "/work/g.ts" },
465 ],
466 },
467 ],
468 rawInput: { path: "/work/a.ts", filePath: "/work/h.ts", file_path: null, other: 7 },
469 }),
470 [
471 "/work/a.ts",
472 " /work/a.ts",
473 "/work/b.ts ",
474 "/work/c.ts",
475 "/work/d.ts",
476 "/work/e.ts",
477 "/work/f.ts",
478 "/work/g.ts",
479 "/work/h.ts",
480 ],
481 );
482 assert.deepEqual(acpChangedPathsOfToolCall({}), []);
483 assert.deepEqual(
484 acpChangedPathsOfToolCall({ locations: null, content: null, rawInput: null }),
485 [],
486 );
487 });
488
489 it("merges two readings into every path of both, and unknown stays unknown", () => {
490 assert.deepEqual(mergeAcpChangedPaths(["/a", "/b"], ["/b", "/c"]), ["/a", "/b", "/c"]);
491 assert.deepEqual(mergeAcpChangedPaths(undefined, ["/a"]), ["/a"]);
492 assert.deepEqual(mergeAcpChangedPaths(["/a"], undefined), ["/a"]);
493 assert.deepEqual(mergeAcpChangedPaths(undefined, undefined), []);
494 assert.equal(mergeAcpChangedPaths("unknown", ["/a"]), "unknown");
495 assert.equal(mergeAcpChangedPaths(["/a"], "unknown"), "unknown");
496 assert.equal(mergeAcpChangedPaths("unknown", undefined), "unknown");
497 });
498});
499
500describe("acpMcpToolApprovalElicitationDisposition", () => {
501 it("applies runtime policy only to explicitly tagged MCP approval elicitations", () => {
502 const fullAccess: AcpRuntimePolicy = {
503 runtimeMode: "full-access",
504 cwd: process.cwd(),
505 };
506 const approvalRequired: AcpRuntimePolicy = {
507 runtimeMode: "approval-required",
508 cwd: process.cwd(),
509 };
510 const tagged = {
511 sessionId: "session-1",
512 message: "Approve this request?",
513 mode: "form",
514 requestedSchema: { type: "object", properties: {} },
515 _meta: { codex_approval_kind: "mcp_tool_call" },
516 } satisfies EffectAcpSchema.CreateElicitationRequest;
517
518 assert.equal(acpMcpToolApprovalElicitationDisposition(fullAccess, tagged), "allow");
519 assert.equal(acpMcpToolApprovalElicitationDisposition(approvalRequired, tagged), "ask");
520 assert.equal(
521 acpMcpToolApprovalElicitationDisposition(
522 { ...fullAccess, approvalPolicy: "on-request" },
523 tagged,
524 ),
525 "ask",
526 );
527 assert.equal(
528 acpMcpToolApprovalElicitationDisposition(
529 {
530 runtimeMode: "auto-accept-edits",
531 cwd: process.cwd(),
532 approvalPolicy: "never",
533 sandboxPolicy: {
534 type: "workspaceWrite",
535 writableRoots: [],
536 networkAccess: false,
537 },
538 },
539 tagged,
540 ),
541 "allow",
542 );
543 const { _meta: _tag, ...untagged } = tagged;
544 assert.equal(
545 acpMcpToolApprovalElicitationDisposition(
546 fullAccess,
547 untagged,
548 "mcp_tool_call_approval_exec-123",
549 ),
550 "allow",
551 );
552 assert.isUndefined(
553 acpMcpToolApprovalElicitationDisposition(fullAccess, {
554 ...tagged,
555 _meta: { codex_approval_kind: "ordinary_form" },
556 }),
557 );
558 assert.isUndefined(
559 acpMcpToolApprovalElicitationDisposition(fullAccess, {
560 sessionId: "session-1",
561 message: "Authenticate",
562 mode: "url",
563 elicitationId: "elicitation-1",
564 url: "https://example.com/login",
565 _meta: { codex_approval_kind: "mcp_tool_call" },
566 }),
567 );
568 });
569});
570
571describe("client terminal disposition", () => {
572 const cwd = NodePath.resolve(process.cwd(), "acp-client-policy-workspace");
573
574 it("asks in approval-required and auto-accept-edits, allows in auto and full access", () => {
575 assert.equal(acpClientExecuteDisposition({ runtimeMode: "approval-required", cwd }), "ask");
576 assert.equal(acpClientExecuteDisposition({ runtimeMode: "auto-accept-edits", cwd }), "ask");
577 assert.equal(acpClientExecuteDisposition({ runtimeMode: "auto", cwd }), "allow");
578 assert.equal(acpClientExecuteDisposition({ runtimeMode: "full-access", cwd }), "allow");
579 });
580
581 it("denies terminals under explicit read-only and workspace-write sandboxes", () => {
582 for (const sandboxPolicy of [
583 { type: "readOnly" },
584 { type: "workspaceWrite", writableRoots: [], networkAccess: false },
585 ]) {
586 assert.equal(
587 acpClientExecuteDisposition({
588 runtimeMode: "full-access",
589 cwd,
590 approvalPolicy: "never",
591 sandboxPolicy,
592 }),
593 "deny",
594 );
595 }
596 });
597});
598
599describe("makeAcpClientPolicyGrants", () => {
600 it("grants terminals only from execute approvals, never from any other approved kind", () => {
601 const grants = makeAcpClientPolicyGrants();
602 for (const toolKind of ["read", "edit", "switch_mode", "other", "unknown", "browse"]) {
603 grants.recordApproval({ toolKind, scope: "session", turnKey: "turn-1" });
604 }
605 assert.isFalse(grants.allowsExecute("turn-1"));
606 grants.recordApproval({ toolKind: "execute", scope: "turn", turnKey: "turn-1" });
607 assert.isTrue(grants.allowsExecute("turn-1"));
608 assert.isFalse(grants.allowsExecute("turn-2"));
609 assert.isFalse(grants.allowsExecute(null));
610 });
611
612 it("keeps accept-for-session command grants across turns", () => {
613 const grants = makeAcpClientPolicyGrants();
614 grants.recordApproval({ toolKind: "execute", scope: "session", turnKey: "turn-1" });
615 assert.isTrue(grants.allowsExecute("turn-9"));
616 assert.isTrue(grants.allowsExecute(null));
617 });
618
619 it("drops a turn's command grant when a later turn approves one", () => {
620 const grants = makeAcpClientPolicyGrants();
621 grants.recordApproval({ toolKind: "execute", scope: "turn", turnKey: "turn-1" });
622 grants.recordApproval({ toolKind: "execute", scope: "turn", turnKey: "turn-2" });
623 assert.isFalse(grants.allowsExecute("turn-1"));
624 assert.isTrue(grants.allowsExecute("turn-2"));
625 });
626});