diff --git a/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.claude-login-text.test.ts b/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.claude-login-text.test.ts new file mode 100644 index 00000000000..5ad29ba2763 --- /dev/null +++ b/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.claude-login-text.test.ts @@ -0,0 +1,107 @@ +import { spawn } from "node:child_process"; +import { mkdir, mkdtemp, rm } from "node:fs/promises"; +import { createServer } from "node:http"; +import { createRequire } from "node:module"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +import type { AcpRuntimeEvent } from "acpx/runtime"; +import { expect, it, vi } from "vitest"; + +import { openCodexAcpxRuntime } from "./acpx-runtime-adapter.js"; +import type { VerifiedAcpxCommandLease } from "./installation-integrity.js"; +import { resolveQualifiedAcpxProfile } from "./qualified-profiles.js"; +import type { AcpxRuntimePortOpenOptions } from "./runtime-host.js"; + +const QUOTED_LOGIN_TEXT = 'The probe exited 1: "Not logged in ยท Please run /login". I posted its output.'; + +async function runTurn() { + const root = await mkdtemp(join(tmpdir(), "claude-login-text-")); + const home = join(root, "home"), cwd = join(root, "workspace"), config = join(home, ".claude"); + await Promise.all([mkdir(config, { recursive: true }), mkdir(cwd)]); + let sequence = 0; + const api = createServer(async (request, response) => { + let raw = ""; + for await (const chunk of request) raw += chunk; + if (request.method === "GET") { + response.writeHead(200, { "content-type": "application/json" }); + response.end(JSON.stringify({ data: [], has_more: false })); + return; + } + if (request.url?.includes("count_tokens")) { + response.writeHead(200, { "content-type": "application/json" }); + response.end(JSON.stringify({ input_tokens: 1 })); + return; + } + const body = JSON.parse(raw || "{}") as { model?: string; stream?: boolean }; + const model = body.model ?? "claude-sonnet-5"; + if (body.stream !== true) { + response.writeHead(200, { "content-type": "application/json" }); + response.end(JSON.stringify({ id: "msg_fixture", type: "message", role: "assistant", model, + content: [{ type: "text", text: "OK" }], stop_reason: "end_turn", usage: { input_tokens: 1, output_tokens: 1 } })); + return; + } + const frames: Array> = [ + { type: "message_start", message: { id: `msg_${++sequence}`, type: "message", role: "assistant", model, content: [], + stop_reason: null, stop_sequence: null, usage: { input_tokens: 7, output_tokens: 0 } } }, + { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }, + { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: QUOTED_LOGIN_TEXT } }, + { type: "content_block_stop", index: 0 }, + { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } }, + { type: "message_stop" }, + ]; + response.writeHead(200, { "content-type": "text/event-stream" }); + for (const frame of frames) response.write(`event: ${frame.type}\ndata: ${JSON.stringify(frame)}\n\n`); + response.end(); + }); + await new Promise(resolve => api.listen(0, "127.0.0.1", resolve)); + const address = api.address() as { port: number }; + + const entry = createRequire(import.meta.url).resolve("@agentclientprotocol/claude-agent-acp/dist/index.js"); + const command: VerifiedAcpxCommandLease = { + spawn: (args, input) => spawn(process.execPath, [entry, ...(args ?? [])], { ...input, detached: true, stdio: ["pipe", "pipe", "pipe"] }), + close: async () => undefined, + }; + const options: AcpxRuntimePortOpenOptions = { + command, + profile: resolveQualifiedAcpxProfile("claude", "claude-sonnet-5"), + cwd, + stateDirectory: join(root, "state"), + providerSessionKey: "claude-login-text", + permissionMode: "approve-all", + permissionPolicy: { autoApprove: ["read", "write", "execute", "other"], defaultAction: "approve" }, + launchEnvironment: { HOME: home, CLAUDE_CONFIG_DIR: config, PATH: "/usr/bin:/bin", TMPDIR: root, + ANTHROPIC_API_KEY: "local-fixture-only", ANTHROPIC_BASE_URL: `http://127.0.0.1:${address.port}`, + CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1", DISABLE_TELEMETRY: "1", DISABLE_ERROR_REPORTING: "1", ENABLE_TOOL_SEARCH: "false" }, + credentialFenceFds: [42, 43] as const, + activateCredentialFenceOwner: async () => undefined, + systemInstructions: "Report the probe output.", + mcpServers: [], + retainFailedAdmissionCleanup: vi.fn(), + } as AcpxRuntimePortOpenOptions; + let port: Awaited> | undefined; + try { + port = await openCodexAcpxRuntime(options, { + awaitProviderOwnership: async () => undefined, + awaitProviderExit: child => new Promise(resolve => child.once("exit", () => resolve())), + }); + const turn = port.startTurn({ text: "Run the probe and report its output.", requestId: "turn-1", + onPermissionRequest: async () => ({ outcome: "allow_once" }) }); + const events: AcpRuntimeEvent[] = []; + for await (const event of turn.events) events.push(event); + return { result: await turn.result, events }; + } finally { + try { await port?.close({ reason: "sign-in hint verified" }); } + finally { + api.closeAllConnections(); + await new Promise(resolve => api.close(() => resolve())); + await rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 }); + } + } +} + +it("completes a turn whose successful reply quotes the CLI's sign-in hint through the real ACP stream", async () => { + const { result, events } = await runTurn(); + expect(result).toMatchObject({ status: "completed", stopReason: "end_turn" }); + expect(JSON.stringify(events)).toContain("Please run /login"); +}, 60_000); diff --git a/patches/@agentclientprotocol__claude-agent-acp@0.73.0.patch b/patches/@agentclientprotocol__claude-agent-acp@0.73.0.patch index df6f4c77e00..b7e7589c717 100644 --- a/patches/@agentclientprotocol__claude-agent-acp@0.73.0.patch +++ b/patches/@agentclientprotocol__claude-agent-acp@0.73.0.patch @@ -1,5 +1,5 @@ diff --git a/dist/acp-agent.js b/dist/acp-agent.js -index 0214a50..73f0c16 100644 +index 0214a50..a68121d 100644 --- a/dist/acp-agent.js +++ b/dist/acp-agent.js @@ -2490,11 +2490,88 @@ export class ClaudeAcpAgent { @@ -117,7 +117,18 @@ index 0214a50..73f0c16 100644 await subagents.finishTask(message.task_id, message.patch.status, sendUpdate); session.liveBackgroundTasks.delete(message.task_id); } -@@ -2922,9 +3002,19 @@ export class ClaudeAcpAgent { +@@ -2794,7 +2874,9 @@ export class ClaudeAcpAgent { + session.pendingGoalUpdate = undefined; + const goalCommandFailed = message.is_error || + message.stop_reason === "refusal" || +- ("result" in message && message.result.includes("Please run /login")); ++ ("result" in message && ++ message.is_error && ++ message.result.includes("Please run /login")); + if (goalCommandFailed) { + await this.publishGoal(params.sessionId, pendingGoalUpdate.previous ?? null); + } +@@ -2922,9 +3004,19 @@ export class ClaudeAcpAgent { amount: message.total_cost_usd, currency: "USD", }, @@ -140,7 +151,16 @@ index 0214a50..73f0c16 100644 }, }); } -@@ -4506,6 +4596,48 @@ export class ClaudeAcpAgent { +@@ -3081,7 +3173,7 @@ export class ClaudeAcpAgent { + } + switch (message.subtype) { + case "success": { +- if (message.result.includes("Please run /login")) { ++ if (message.is_error && message.result.includes("Please run /login")) { + await failActiveWithSessionFailure("auth_required", RequestError.authRequired(), message.result); + break; + } +@@ -4506,6 +4598,48 @@ export class ClaudeAcpAgent { * `$/cancel_request`, and our local abort race settles even if the client * ignores it. A `cancelled` outcome, request rejection, and local abort all * surface the same "Tool use aborted" the callers already expect. */ @@ -189,7 +209,7 @@ index 0214a50..73f0c16 100644 async requestPermissionFromClient(params, toolName, signal, parentToolUseId, ownerSessionId = params.sessionId) { if (signal.aborted) throw new Error("Tool use aborted"); -@@ -5311,14 +5443,24 @@ export class ClaudeAcpAgent { +@@ -5311,14 +5445,24 @@ export class ClaudeAcpAgent { settingSources: ["user", "project", "local"], ...(thinking !== undefined && { thinking }), ...userProvidedOptions, @@ -215,7 +235,7 @@ index 0214a50..73f0c16 100644 ...mcpServers, ...(fileChangeAuditSupport ? { [FILE_CHANGE_AUDIT_SERVER_NAME]: fileChangeAuditSupport.mcpServer } -@@ -5329,6 +5471,16 @@ export class ClaudeAcpAgent { +@@ -5329,6 +5473,16 @@ export class ClaudeAcpAgent { allowDangerouslySkipPermissions: ALLOW_BYPASS, permissionMode: initialPermissionMode, canUseTool: this.canUseTool(sessionId),