The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off -- The scenario stages a local ACP executable. |
| 2 | import * as NodeFSP from "node:fs/promises"; |
| 3 | import * as NodePath from "node:path"; |
| 4 | |
| 5 | import { expect } from "e2e"; |
| 6 | |
| 7 | import { T3_HOME } from "../e2e.config.ts"; |
| 8 | import { makeState, runPromise, test, WEB } from "./t3.ts"; |
| 9 | |
| 10 | const DRAFT = "Draft summary the agent discarded"; |
| 11 | const REPLY = "Compaction did not finish."; |
| 12 | const ACP = ` |
| 13 | import { createInterface } from "node:readline"; |
| 14 | |
| 15 | const sessionId = "compaction-scenario"; |
| 16 | const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n"); |
| 17 | const respond = (id, result) => send({ id, result }); |
| 18 | const update = (update) => send({ method: "session/update", params: { sessionId, update } }); |
| 19 | for await (const line of createInterface({ input: process.stdin })) { |
| 20 | const { id, method } = JSON.parse(line); |
| 21 | if (id === undefined) continue; |
| 22 | if (method === "initialize") { |
| 23 | respond(id, { protocolVersion: 2, info: { name: "compaction-agent", version: "1.0.0" } }); |
| 24 | } else if (method === "session/new") { |
| 25 | respond(id, { sessionId }); |
| 26 | } else if (method === "session/prompt") { |
| 27 | update({ sessionUpdate: "compaction_update", compactionId: "compact-1", status: "in_progress" }); |
| 28 | update({ |
| 29 | sessionUpdate: "compaction_summary_chunk", |
| 30 | compactionId: "compact-1", |
| 31 | content: { type: "text", text: ${JSON.stringify(DRAFT)} }, |
| 32 | }); |
| 33 | // The agent discards its draft when the compaction fails. |
| 34 | update({ sessionUpdate: "compaction_update", compactionId: "compact-1", status: "failed", summary: null }); |
| 35 | update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: ${JSON.stringify(REPLY)} } }); |
| 36 | update({ sessionUpdate: "state_update", state: "idle", stopReason: "end_turn" }); |
| 37 | respond(id, {}); |
| 38 | } else { |
| 39 | send({ id, error: { code: -32601, message: "Method not found" } }); |
| 40 | } |
| 41 | } |
| 42 | `; |
| 43 | |
| 44 | test( |
| 45 | "A failed ACP compaction does not show the draft summary its agent cleared", |
| 46 | WEB, |
| 47 | async ({ t3, app, screen }) => { |
| 48 | const project = await t3.project("demo"); |
| 49 | const script = NodePath.join(project, "compaction-acp.mjs"); |
| 50 | const settingsPath = NodePath.join(T3_HOME, "userdata/settings.json"); |
| 51 | const originalSettings = await NodeFSP.readFile(settingsPath, "utf8"); |
| 52 | const settings = JSON.parse(originalSettings); |
| 53 | await NodeFSP.writeFile(script, ACP); |
| 54 | settings.providerInstances["compaction-agent"] = { |
| 55 | driver: "acpRegistry", |
| 56 | displayName: "Compaction Agent", |
| 57 | enabled: true, |
| 58 | config: { source: "local", commandPath: process.execPath, commandArgs: [script] }, |
| 59 | }; |
| 60 | try { |
| 61 | await NodeFSP.writeFile(settingsPath, JSON.stringify(settings)); |
| 62 | const { cli } = makeState(T3_HOME); |
| 63 | // The watcher reloads settings asynchronously. Retry only before a thread was created. |
| 64 | let threadId: string | undefined; |
| 65 | for (let attempt = 0; attempt < 8 && threadId === undefined; attempt++) { |
| 66 | const started = await runPromise( |
| 67 | cli( |
| 68 | [ |
| 69 | "thread", |
| 70 | "start", |
| 71 | project, |
| 72 | "Compact the context.", |
| 73 | "--title", |
| 74 | "Failed compaction", |
| 75 | "--provider", |
| 76 | "compaction-agent", |
| 77 | "--model", |
| 78 | "default", |
| 79 | "--wait", |
| 80 | "--json", |
| 81 | ], |
| 82 | [0, 1], |
| 83 | ), |
| 84 | ); |
| 85 | if (!started.stdout.includes("threadId")) continue; |
| 86 | const result = JSON.parse(started.stdout) as { |
| 87 | threadId: string; |
| 88 | status: string; |
| 89 | reply: string | null; |
| 90 | }; |
| 91 | if (result.status !== "completed" || !result.reply?.includes(REPLY)) { |
| 92 | throw new Error(`Compaction turn did not complete: ${started.stdout} ${started.stderr}`); |
| 93 | } |
| 94 | threadId = result.threadId; |
| 95 | } |
| 96 | if (threadId === undefined) throw new Error("Could not start the compaction turn."); |
| 97 | |
| 98 | await t3.open({ threadId }); |
| 99 | await screen.getByText(REPLY, { exact: true }).waitFor({ timeout: 60_000 }); |
| 100 | const separator = screen.getByRole("separator", /^Context compaction failed/); |
| 101 | await separator.waitFor({ timeout: 15_000 }); |
| 102 | try { |
| 103 | await expect(separator).toHaveText("Context compaction failed"); |
| 104 | } finally { |
| 105 | await app.screenshot("failed-compaction-row"); |
| 106 | } |
| 107 | } finally { |
| 108 | await NodeFSP.writeFile(settingsPath, originalSettings); |
| 109 | } |
| 110 | }, |
| 111 | ); |