-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-codex.test.ts
More file actions
195 lines (174 loc) · 5.74 KB
/
Copy pathruntime-codex.test.ts
File metadata and controls
195 lines (174 loc) · 5.74 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
186
187
188
189
190
191
192
193
194
195
import { spawnSync } from "node:child_process";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
AcpPermissionDeniedError,
AcpRuntime,
type AcpRuntimeSession,
type AcpRuntimeTurnEvent,
createCodexAcpAgent,
createStdioAcpConnectionFactory,
resolveRuntimeAgentFromRegistry,
} from "../index.js";
const tempDirs: string[] = [];
afterEach(async () => {
await Promise.all(
tempDirs.splice(0).map(async (path) => {
await rm(path, { force: true, recursive: true });
}),
);
});
function shouldSkipCodexContract(): boolean {
return process.env.ACP_RUNTIME_SKIP_CODEX_TEST === "1";
}
function isCommandInPath(command: string): boolean {
const which = process.platform === "win32" ? "where" : "which";
const result = spawnSync(which, [command], {
encoding: "utf8",
stdio: "pipe",
});
return result.status === 0;
}
function hasCodexCredentials(): boolean {
if (process.env.CODEX_API_KEY || process.env.OPENAI_API_KEY) {
return true;
}
const result = spawnSync("codex", ["login", "status"], {
encoding: "utf8",
stdio: "pipe",
});
const output = `${result.stdout ?? ""}\n${result.stderr ?? ""}`;
return result.status === 0 && /logged in/i.test(output);
}
async function resolveCodexContractAgent() {
if (shouldSkipCodexContract()) {
return {
skipReason: "ACP_RUNTIME_SKIP_CODEX_TEST=1",
} as const;
}
if (!hasCodexCredentials()) {
return {
skipReason:
"Codex auth unavailable; set CODEX_API_KEY / OPENAI_API_KEY or log in with `codex login`.",
} as const;
}
if (process.env.ACP_RUNTIME_RUN_CODEX_TEST === "1") {
return {
createSession(runtime: AcpRuntime, cwd: string) {
return runtime.sessions.start({
agent: createCodexAcpAgent({ via: "npx" }),
cwd,
});
},
} as const;
}
if (!isCommandInPath("codex-acp")) {
return {
skipReason:
"codex-acp is not installed locally; skipping registry-backed launch that would require package download or binary fetch. Set ACP_RUNTIME_RUN_CODEX_TEST=1 to force it.",
} as const;
}
try {
await resolveRuntimeAgentFromRegistry("codex-acp");
return {
createSession(runtime: AcpRuntime, cwd: string) {
return runtime.sessions.start({
agent: "codex-acp",
cwd,
});
},
} as const;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return {
skipReason: `failed to resolve codex-acp launch from ACP registry: ${message}`,
} as const;
}
}
describe("AcpRuntime x Codex ACP", () => {
it(
"creates a Codex ACP session over stdio",
async (context) => {
const resolution = await resolveCodexContractAgent();
if ("skipReason" in resolution) {
context.skip(resolution.skipReason);
}
const cwd = await mkdtemp(join(tmpdir(), "acp-runtime-codex-"));
tempDirs.push(cwd);
const trace: string[] = [];
const runtime = new AcpRuntime(
createStdioAcpConnectionFactory({
onAcpMessage(direction, message) {
trace.push(
`${direction}: ${JSON.stringify(message)}`,
);
},
stderr: "pipe",
}),
);
let stage = "create";
let session: AcpRuntimeSession | undefined;
try {
trace.push(`stage=${stage}`);
session = await resolution.createSession(runtime, cwd);
expect(session.metadata.id).toBeTruthy();
expect(session.capabilities.agent.prompt).toBe(true);
expect(
session.agent.listModes().some((mode) => mode.id === "read-only"),
).toBe(true);
const firstEvents: AcpRuntimeTurnEvent[] = [];
stage = "send";
trace.push(`stage=${stage}`);
const result = await session.turn.send("Reply with exactly the word READY.", {
onEvent(event) {
firstEvents.push(event);
trace.push(`event: ${JSON.stringify(event)}`);
},
});
expect(result.outputText).toContain("READY");
expect(firstEvents.some((event) => event.type === "started")).toBe(true);
expect(firstEvents.some((event) => event.type === "completed")).toBe(true);
stage = "set-read-only";
trace.push(`stage=${stage}`);
await session.agent.setMode("read-only");
expect(session.metadata.currentModeId).toBe("read-only");
const deniedEvents: AcpRuntimeTurnEvent[] = [];
stage = "permission-denied";
trace.push(`stage=${stage}`);
await expect(
session.turn.send(
"Write the exact text 'denied' into ./codex-readonly-denied.txt using tools, then report success.",
{
onEvent(event) {
deniedEvents.push(event);
trace.push(`event: ${JSON.stringify(event)}`);
},
},
),
).rejects.toBeInstanceOf(AcpPermissionDeniedError);
expect(
deniedEvents.some((event) => event.type === "permission_requested"),
).toBe(true);
expect(
deniedEvents.some(
(event) =>
event.type === "operation_updated" &&
event.operation.permission?.family === "permission_request_cancelled",
),
).toBe(true);
expect(session.status).toBe("ready");
} catch (error) {
console.error(`[codex-contract] failed during ${stage}`);
for (const line of trace.slice(-40)) {
console.error(`[codex-contract] ${line}`);
}
throw error;
} finally {
await session?.close().catch(() => undefined);
}
},
240_000,
);
});