The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off -- The scenario supplies a local ACP agent with a gated list. |
| 2 | import * as NodeHttp from "node:http"; |
| 3 | import * as NodePath from "node:path"; |
| 4 | |
| 5 | import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts"; |
| 6 | import { expect } from "e2e"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | |
| 9 | import { T3_HOME } from "../e2e.config.ts"; |
| 10 | import { rpcClient, runPromise, test, WEB } from "./t3.ts"; |
| 11 | |
| 12 | const SESSION_TITLE = "Native draft to delete"; |
| 13 | const ACP = ` |
| 14 | import { createInterface } from "node:readline"; |
| 15 | |
| 16 | const [origin] = process.argv.slice(2); |
| 17 | const reply = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n"); |
| 18 | const handle = async ({ method, params }) => { |
| 19 | if (method === "initialize") { |
| 20 | return { |
| 21 | protocolVersion: 1, |
| 22 | agentInfo: { name: "session-scenario", version: "1.0.0" }, |
| 23 | agentCapabilities: { sessionCapabilities: { list: {}, delete: {} } }, |
| 24 | authMethods: [], |
| 25 | }; |
| 26 | } |
| 27 | if (method === "session/new") return { sessionId: "health-probe" }; |
| 28 | if (method === "session/list") { |
| 29 | const response = await fetch(origin + "/list"); |
| 30 | const { present, title } = await response.json(); |
| 31 | return { sessions: present ? [{ sessionId: "native-draft", cwd: params.cwd, title }] : [] }; |
| 32 | } |
| 33 | if (method === "session/delete") { |
| 34 | if (params.sessionId === "native-draft") await fetch(origin + "/delete"); |
| 35 | return {}; |
| 36 | } |
| 37 | return undefined; |
| 38 | }; |
| 39 | // A gated session/list must not block the session/delete that arrives while it waits. |
| 40 | createInterface({ input: process.stdin }).on("line", (line) => { |
| 41 | const request = JSON.parse(line); |
| 42 | if (request.id === undefined) return; |
| 43 | void handle(request).then((result) => |
| 44 | reply( |
| 45 | result === undefined |
| 46 | ? { id: request.id, error: { code: -32601, message: "Method not found" } } |
| 47 | : { id: request.id, result }, |
| 48 | ), |
| 49 | ); |
| 50 | }); |
| 51 | `; |
| 52 | |
| 53 | test( |
| 54 | "Refreshing native ACP sessions does not bring back a session deleted during the refresh", |
| 55 | WEB, |
| 56 | async ({ t3, app, screen }) => { |
| 57 | const instanceId = ProviderInstanceId.make("scenario-session-agent"); |
| 58 | const snapshotTaken = Promise.withResolvers<void>(); |
| 59 | const releaseSnapshot = Promise.withResolvers<void>(); |
| 60 | let present = true; |
| 61 | let holdNextList = false; |
| 62 | const state = NodeHttp.createServer((request, response) => { |
| 63 | if (request.url === "/list") { |
| 64 | const snapshot = JSON.stringify({ present, title: SESSION_TITLE }); |
| 65 | const reply = () => |
| 66 | response.writeHead(200, { "Content-Type": "application/json" }).end(snapshot); |
| 67 | if (holdNextList) { |
| 68 | holdNextList = false; |
| 69 | snapshotTaken.resolve(); |
| 70 | void releaseSnapshot.promise.then(reply); |
| 71 | } else { |
| 72 | reply(); |
| 73 | } |
| 74 | } else if (request.url === "/delete") { |
| 75 | present = false; |
| 76 | response.writeHead(204).end(); |
| 77 | } else { |
| 78 | response.writeHead(404).end(); |
| 79 | } |
| 80 | }); |
| 81 | |
| 82 | try { |
| 83 | await new Promise<void>((resolve) => state.listen(0, "127.0.0.1", resolve)); |
| 84 | const address = state.address(); |
| 85 | if (address === null || typeof address === "string") throw new Error("No fixture port"); |
| 86 | const project = await t3.project("demo", { "session-agent.mjs": ACP }); |
| 87 | await runPromise( |
| 88 | Effect.gen(function* () { |
| 89 | const client = yield* rpcClient(T3_HOME, app.baseUrl ?? ""); |
| 90 | yield* client["server.updateSettings"]({ |
| 91 | patch: {}, |
| 92 | providerInstanceMutation: { |
| 93 | operation: "upsert", |
| 94 | instanceId, |
| 95 | instance: { |
| 96 | driver: ProviderDriverKind.make("acpRegistry"), |
| 97 | displayName: "Session scenario agent", |
| 98 | enabled: true, |
| 99 | config: { |
| 100 | source: "local", |
| 101 | commandPath: process.execPath, |
| 102 | commandArgs: [ |
| 103 | NodePath.join(project, "session-agent.mjs"), |
| 104 | `http://127.0.0.1:${address.port}`, |
| 105 | ], |
| 106 | }, |
| 107 | }, |
| 108 | }, |
| 109 | }); |
| 110 | }).pipe(Effect.scoped), |
| 111 | ); |
| 112 | await t3.open(`/settings/providers?instanceId=${instanceId}`); |
| 113 | await screen.getByRole("button", "List sessions", { exact: true }).tap({ timeout: 60_000 }); |
| 114 | const entry = screen.getByText(SESSION_TITLE, { exact: true }); |
| 115 | await entry.waitFor(); |
| 116 | |
| 117 | holdNextList = true; |
| 118 | await screen.getByRole("button", "Refresh", { exact: true }).tap(); |
| 119 | await snapshotTaken.promise; |
| 120 | const remove = screen.getByRole("button", "Delete", { exact: true }); |
| 121 | const loading = screen.getByRole("button", "Loading", { exact: true }); |
| 122 | if (!(await remove.isVisible()) || !(await remove.isEnabled())) { |
| 123 | releaseSnapshot.resolve(); |
| 124 | await loading.waitFor({ state: "hidden" }); |
| 125 | } |
| 126 | await remove.tap(); |
| 127 | const confirm = screen.getByRole("alertdialog"); |
| 128 | await confirm.getByRole("button", "Confirm", { exact: true }).tap(); |
| 129 | await entry.waitFor({ state: "hidden" }); |
| 130 | expect(await loading.isVisible(), "The refresh is still pending after deletion").toBe(true); |
| 131 | |
| 132 | releaseSnapshot.resolve(); |
| 133 | await loading.waitFor({ state: "hidden" }); |
| 134 | await app.screenshot("after-deletion-and-refresh"); |
| 135 | expect(await entry.isVisible(), "The deleted native session stays off the list").toBe(false); |
| 136 | } finally { |
| 137 | releaseSnapshot.resolve(); |
| 138 | state.close(); |
| 139 | state.closeAllConnections(); |
| 140 | } |
| 141 | }, |
| 142 | ); |