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

clientTerminals.test.ts

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