Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AcpClientTerminals.test.ts

312 lines11.6 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Result from "effect/Result";
5import { ChildProcessSpawner } from "effect/process";
6​
7import {
8 makeAcpClientTerminals,
9 resolveEmbeddedTerminalContent,
10 type AcpClientTerminals,
11} from "./AcpClientTerminals.ts";
12​
13const withTerminals = <A, E>(use: (terminals: AcpClientTerminals) => Effect.Effect<A, E>) =>
14 Effect.gen(function* () {
15 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
16 const terminals = yield* makeAcpClientTerminals({
17 spawner,
18 defaultCwd: process.cwd(),
19 });
20 return yield* use(terminals).pipe(Effect.ensuring(terminals.disposeAll));
21 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
22​
23describe("AcpClientTerminals", () => {
24 it.effect("runs a command, buffers output, and reports the exit status", () =>
25 withTerminals((terminals) =>
26 Effect.gen(function* () {
27 const created = yield* terminals.create({
28 sessionId: "session",
29 command: process.execPath,
30 args: ["-e", "console.log('hello from acp'); process.exit(3);"],
31 });
32​
33 const exit = yield* terminals.waitForExit({
34 sessionId: "session",
35 terminalId: created.terminalId,
36 });
37 expect(exit.exitCode).toBe(3);
38​
39 const output = yield* terminals.output({
40 sessionId: "session",
41 terminalId: created.terminalId,
42 });
43 expect(output.output).toContain("hello from acp");
44 expect(output.truncated).toBe(false);
45 expect(output.exitStatus?.exitCode).toBe(3);
46 }),
47 ),
48 );
49​
50 it.effect("resolves terminal environment from the requesting ACP session", () =>
51 Effect.gen(function* () {
52 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
53 const terminals = yield* makeAcpClientTerminals({
54 spawner,
55 defaultCwd: process.cwd(),
56 environmentForSession: (sessionId) => ({
57 T3_TEST_ACP_SESSION_TOKEN: sessionId === "session-a" ? "token-a" : "token-b",
58 }),
59 });
60 yield* Effect.addFinalizer(() => terminals.disposeAll);
61​
62 for (const [sessionId, expected] of [
63 ["session-a", "token-a"],
64 ["session-b", "token-b"],
65 ] as const) {
66 const created = yield* terminals.create({
67 sessionId,
68 command: process.execPath,
69 args: ["-e", "process.stdout.write(process.env.T3_TEST_ACP_SESSION_TOKEN ?? '')"],
70 });
71 yield* terminals.waitForExit({ sessionId, terminalId: created.terminalId });
72 const output = yield* terminals.output({ sessionId, terminalId: created.terminalId });
73 expect(output.output).toBe(expected);
74 }
75 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
76 );
77​
78 it.effect("runs Devin shell source with arguments, pipes, and session fallback variables", () =>
79 Effect.gen(function* () {
80 const terminals = yield* makeAcpClientTerminals({
81 spawner: yield* ChildProcessSpawner.ChildProcessSpawner,
82 defaultCwd: process.cwd(),
83 shellCommands: true,
84 environmentForSession: () => ({ T3_ACP_MCP_NODE: "mailbox-probe" }),
85 });
86 yield* Effect.addFinalizer(() => terminals.disposeAll);
87 // Same command-only shape as the failed production commands and live capture.
88 const terminal = yield* terminals.create({
89 sessionId: "devin",
90 command: 'printf "%s\\n" "$T3_ACP_MCP_NODE" | tr a-z A-Z',
91 });
92 const exit = yield* terminals.waitForExit({ sessionId: "devin", ...terminal });
93 const output = yield* terminals.output({ sessionId: "devin", ...terminal });
94 expect(exit.exitCode).toBe(0);
95 expect(output.output.trim()).toBe("MAILBOX-PROBE");
96 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
97 );
98​
99 it.effect("truncates buffered output from the beginning at the byte limit", () =>
100 withTerminals((terminals) =>
101 Effect.gen(function* () {
102 const created = yield* terminals.create({
103 sessionId: "session",
104 command: process.execPath,
105 args: ["-e", "process.stdout.write('a'.repeat(64) + 'TAIL');"],
106 outputByteLimit: 16,
107 });
108 yield* terminals.waitForExit({ sessionId: "session", terminalId: created.terminalId });
109​
110 const output = yield* terminals.output({
111 sessionId: "session",
112 terminalId: created.terminalId,
113 });
114 expect(output.truncated).toBe(true);
115 expect(output.output.endsWith("TAIL")).toBe(true);
116 expect(output.output.length).toBeLessThanOrEqual(16);
117 }),
118 ),
119 );
120​
121 it.effect("kills long-running commands and rejects released terminal handles", () =>
122 withTerminals((terminals) =>
123 Effect.gen(function* () {
124 const created = yield* terminals.create({
125 sessionId: "session",
126 command: process.execPath,
127 args: ["-e", "setInterval(() => {}, 1000);"],
128 });
129 yield* terminals.kill({ sessionId: "session", terminalId: created.terminalId });
130 const exit = yield* terminals.waitForExit({
131 sessionId: "session",
132 terminalId: created.terminalId,
133 });
134 expect(exit.exitCode === 0 ? null : exit.exitCode).not.toBe(0);
135 expect(exit.signal).toBe("SIGTERM");
136​
137 yield* terminals.release({ sessionId: "session", terminalId: created.terminalId });
138 const rejected = yield* Effect.flip(
139 terminals.output({ sessionId: "session", terminalId: created.terminalId }),
140 );
141 expect(rejected.message).toContain("unknown terminal ID");
142​
143 // Embedded tool-call content still renders released terminals.
144 expect(terminals.readOutputSnapshot("session", created.terminalId)).toBeDefined();
145 }),
146 ),
147 );
148​
149 it.effect("reserves terminal capacity across concurrent creates", () =>
150 withTerminals((terminals) =>
151 Effect.gen(function* () {
152 const results = yield* Effect.all(
153 Array.from({ length: 17 }, () =>
154 terminals
155 .create({
156 sessionId: "session",
157 command: process.execPath,
158 args: ["-e", "setInterval(() => {}, 1000);"],
159 })
160 .pipe(Effect.result),
161 ),
162 { concurrency: "unbounded" },
163 );
164​
165 expect(results.filter(Result.isSuccess)).toHaveLength(16);
166 expect(results.filter(Result.isFailure)).toHaveLength(1);
167 }),
168 ),
169 );
170​
171 it.effect("bounds retained snapshots after terminals are released", () =>
172 withTerminals((terminals) =>
173 Effect.gen(function* () {
174 const terminalIds: string[] = [];
175 for (let index = 0; index < 33; index += 1) {
176 const created = yield* terminals.create({
177 sessionId: "session",
178 command: process.execPath,
179 args: ["-e", `process.stdout.write('${index}');`],
180 });
181 yield* terminals.release({ sessionId: "session", terminalId: created.terminalId });
182 terminalIds.push(created.terminalId);
183 }
184​
185 expect(terminals.readOutputSnapshot("session", terminalIds[0]!)).toBeUndefined();
186 expect(terminals.readOutputSnapshot("session", terminalIds.at(-1)!)).toBeDefined();
187 }),
188 ),
189 );
190​
191 it.effect("bounds unreleased terminal handles without invalidating existing ids", () =>
192 withTerminals((terminals) =>
193 Effect.gen(function* () {
194 const terminalIds: string[] = [];
195 for (let index = 0; index < 64; index += 1) {
196 const created = yield* terminals.create({
197 sessionId: "session",
198 command: process.execPath,
199 args: ["-e", `process.stdout.write('${index}');`],
200 });
201 yield* terminals.waitForExit({
202 sessionId: "session",
203 terminalId: created.terminalId,
204 });
205 terminalIds.push(created.terminalId);
206 }
207​
208 const rejected = yield* terminals
209 .create({
210 sessionId: "session",
211 command: process.execPath,
212 args: ["-e", ""],
213 })
214 .pipe(Effect.result);
215 expect(Result.isFailure(rejected)).toBe(true);
216 expect(terminals.readOutputSnapshot("session", terminalIds[0]!)).toBeDefined();
217 expect(terminals.readOutputSnapshot("session", terminalIds.at(-1)!)).toBeDefined();
218 }),
219 ),
220 );
221​
222 it.effect("bounds aggregate output retained across unreleased terminals", () =>
223 withTerminals((terminals) =>
224 Effect.gen(function* () {
225 const terminalIds: string[] = [];
226 for (let index = 0; index < 3; index += 1) {
227 const created = yield* terminals.create({
228 sessionId: "session",
229 command: process.execPath,
230 args: ["-e", "process.stdout.write('x'.repeat(8 * 1024 * 1024));"],
231 outputByteLimit: 8 * 1024 * 1024,
232 });
233 yield* terminals.waitForExit({
234 sessionId: "session",
235 terminalId: created.terminalId,
236 });
237 terminalIds.push(created.terminalId);
238 }
239​
240 const snapshots = terminalIds.map((terminalId) =>
241 terminals.readOutputSnapshot("session", terminalId)!,
242 );
243 const retainedBytes = snapshots.reduce(
244 (total, snapshot) => total + Buffer.byteLength(snapshot.output),
245 0,
246 );
247 expect(retainedBytes).toBeLessThanOrEqual(16 * 1024 * 1024);
248 expect(snapshots.some((snapshot) => snapshot.truncated)).toBe(true);
249 }),
250 ),
251 );
252​
253 it.effect("rejects terminal handles from a different ACP session", () =>
254 withTerminals((terminals) =>
255 Effect.gen(function* () {
256 const created = yield* terminals.create({
257 sessionId: "owner-session",
258 command: process.execPath,
259 args: ["-e", "process.stdout.write('owned');"],
260 });
261 const foreignRequest = {
262 sessionId: "foreign-session",
263 terminalId: created.terminalId,
264 };
265 const results = yield* Effect.all(
266 [
267 terminals.output(foreignRequest),
268 terminals.waitForExit(foreignRequest),
269 terminals.kill(foreignRequest),
270 terminals.release(foreignRequest),
271 ].map((effect) => effect.pipe(Effect.result)),
272 { concurrency: "unbounded" },
273 );
274​
275 expect(results.every(Result.isFailure)).toBe(true);
276 expect(terminals.readOutputSnapshot("foreign-session", created.terminalId)).toBeUndefined();
277 expect(terminals.readOutputSnapshot("owner-session", created.terminalId)).toBeDefined();
278 }),
279 ),
280 );
281});
282​
283describe("resolveEmbeddedTerminalContent", () => {
284 it("rewrites terminal content into text from the buffered snapshot", () => {
285 const notification = {
286 sessionId: "session",
287 update: {
288 sessionUpdate: "tool_call_update" as const,
289 toolCallId: "call-1",
290 content: [
291 { type: "content" as const, content: { type: "text" as const, text: "before" } },
292 { type: "terminal" as const, terminalId: "t3-term-1" },
293 { type: "terminal" as const, terminalId: "t3-term-unknown" },
294 ],
295 },
296 };
297​
298 const resolved = resolveEmbeddedTerminalContent(notification, (sessionId, terminalId) =>
299 sessionId === "session" && terminalId === "t3-term-1"
300 ? { output: "compiled 3 files", truncated: false, exitStatus: undefined }
301 : undefined,
302 );
303​
304 expect(resolved.update).toMatchObject({
305 content: [
306 { type: "content", content: { type: "text", text: "before" } },
307 { type: "content", content: { type: "text", text: "compiled 3 files" } },
308 { type: "content", content: { type: "text", text: "[terminal t3-term-unknown]" } },
309 ],
310 });
311 });
312});