-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathacp-e2e-shell-approval.test.ts
More file actions
185 lines (158 loc) · 7.19 KB
/
Copy pathacp-e2e-shell-approval.test.ts
File metadata and controls
185 lines (158 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import fs from "node:fs";
import path from "node:path";
import {afterEach, beforeEach, expect, it, onTestFinished, vi} from "vitest";
import {AgentMode} from "../../../AgentMode";
import {ApprovalOptionId} from "../../../ApprovalOptionId";
import {
createAuthenticatedFixture,
createPermissionResponder,
createPermissionResponse,
describeE2E,
expectEndTurn,
expectNoPermissionRequests,
expectPermissionRequests,
generateFileNameForTest,
type SpawnedAgentFixture,
} from "./acp-e2e-test-utils";
const FIRST_FILE_NAME = "approval-first.txt";
const SECOND_FILE_NAME = "approval-second.txt";
const COMMAND = `if [ -e ${FIRST_FILE_NAME} ]; then touch ${SECOND_FILE_NAME}; else touch ${FIRST_FILE_NAME}; fi`;
describeE2E("E2E shell approval tests", () => {
let fixture: SpawnedAgentFixture;
let sessionId: string;
beforeEach(async () => {
fixture = await createAuthenticatedFixture(AgentMode.ReadOnly);
sessionId = (await fixture.createSession()).sessionId;
});
afterEach(async () => {
await fixture.dispose();
});
async function promptShellCommandTwice(): Promise<void> {
for (const text of [
`Use your shell tool to run exactly \`${COMMAND}\`.`,
`Use your shell tool to run exactly the same command again: \`${COMMAND}\`.`,
]) {
expectEndTurn(await fixture.connection.prompt({
sessionId,
prompt: [{type: "text", text}],
}));
}
}
it("prompts for every command when allow_once is selected", async () => {
const responses = [ApprovalOptionId.AllowOnce, ApprovalOptionId.RejectOnce];
fixture.setPermissionResponder((request) => createPermissionResponse(
request.toolCall.kind === "execute"
? responses.shift() ?? ApprovalOptionId.RejectOnce
: null
));
await promptShellCommandTwice();
expect(fs.existsSync(path.join(fixture.workspaceDir, FIRST_FILE_NAME))).toBe(true);
expect(fs.existsSync(path.join(fixture.workspaceDir, SECOND_FILE_NAME))).toBe(false);
expectPermissionRequests(fixture, sessionId, {execute: 2, edit: 0});
});
it("skips subsequent approvals when allow_always is selected", async () => {
fixture.setPermissionResponder(createPermissionResponder("execute", ApprovalOptionId.AllowAlways));
await promptShellCommandTwice();
expect(fs.existsSync(path.join(fixture.workspaceDir, FIRST_FILE_NAME))).toBe(true);
expect(fs.existsSync(path.join(fixture.workspaceDir, SECOND_FILE_NAME))).toBe(true);
expectPermissionRequests(fixture, sessionId, {execute: 1, edit: 0});
});
it("prompts for every command when reject_once is selected", async () => {
fixture.setPermissionResponder(createPermissionResponder("execute", ApprovalOptionId.RejectOnce));
await promptShellCommandTwice();
expect(fs.existsSync(path.join(fixture.workspaceDir, FIRST_FILE_NAME))).toBe(false);
expect(fs.existsSync(path.join(fixture.workspaceDir, SECOND_FILE_NAME))).toBe(false);
expectPermissionRequests(fixture, sessionId, {execute: 2, edit: 0});
});
});
describeE2E("E2E Agent mode shell permission tests", () => {
let fixture: SpawnedAgentFixture;
beforeEach(async () => {
fixture = await createAuthenticatedFixture(AgentMode.Agent);
});
afterEach(async () => {
await fixture.dispose();
});
it("runs a workspace command without prompting for permission", async () => {
const sessionId = await writeToFile(fixture, path.join(fixture.workspaceDir, generateFileNameForTest()));
expectNoPermissionRequests(fixture, sessionId);
});
it("requests permission for a command that writes outside the workspace", async () => {
const dir = createDirOutsideWorkspace(fixture);
fixture.setPermissionResponder(createPermissionResponder("execute", ApprovalOptionId.AllowOnce));
const sessionId = await writeToFile(fixture, path.join(dir, generateFileNameForTest()));
expectPermissionRequests(fixture, sessionId, {execute: 1, edit: 0});
});
});
describeE2E("E2E Agent with full access shell permission tests", () => {
let fixture: SpawnedAgentFixture;
beforeEach(async () => {
fixture = await createAuthenticatedFixture(AgentMode.AgentFullAccess);
});
afterEach(async () => {
await fixture.dispose();
});
it("runs a command outside workspace without prompting for permission", async () => {
const dir = createDirOutsideWorkspace(fixture);
const sessionId = await writeToFile(fixture, path.join(dir, generateFileNameForTest()));
expectNoPermissionRequests(fixture, sessionId);
});
});
describeE2E("E2E shell cancellation tests", () => {
let fixture: SpawnedAgentFixture | null = null;
afterEach(async () => {
await fixture?.dispose();
fixture = null;
});
function isProcessRunning(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
it("cancels a running shell command", async () => {
fixture = await createAuthenticatedFixture(AgentMode.AgentFullAccess);
const sessionId = (await fixture.createSession()).sessionId;
const pidFilePath = path.join(fixture.workspaceDir, "cancel-command.pid");
const command = `/bin/sh -c 'echo $$ > "${pidFilePath}"; exec sleep 100'`;
const promptResponse = fixture.connection.prompt({
sessionId,
prompt: [{type: "text", text: `Use your shell tool to run exactly \`${command}\`.`}],
});
const pid = await vi.waitFor(() => {
const content = fs.existsSync(pidFilePath) ? fs.readFileSync(pidFilePath, "utf8").trim() : "";
const parsed = Number.parseInt(content, 10);
expect(parsed).toBeGreaterThan(0);
return parsed;
}, {timeout: 10_000});
expect(isProcessRunning(pid)).toBe(true);
await fixture.connection.cancel({sessionId});
expect((await promptResponse).stopReason).toBe("cancelled");
await vi.waitFor(() => {
expect(isProcessRunning(pid)).toBe(false);
}, {timeout: 5_000});
});
});
async function writeToFile(fixture: SpawnedAgentFixture, filePath: string): Promise<string> {
const content = "hello from e2e";
const command = `printf '${content}' > '${filePath}'`;
const sessionId = (await fixture.createSession()).sessionId;
const response = await fixture.connection.prompt({
sessionId,
prompt: [{
type: "text",
text: `Use your shell tool to run exactly \`${command}\`. Do not modify files any other way.`,
}],
});
expectEndTurn(response);
expect(fs.readFileSync(filePath, "utf8").trim()).toBe(content);
return sessionId;
}
function createDirOutsideWorkspace(fixture: SpawnedAgentFixture): string {
const outsideWorkspaceDir = path.join(path.dirname(fixture.workspaceDir), "outside-workspace");
onTestFinished(() => fs.rmSync(outsideWorkspaceDir, {recursive: true, force: true}));
fs.mkdirSync(outsideWorkspaceDir);
return outsideWorkspaceDir;
}