From 84b75502c089111447b87f549da44adbde4c0901 Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Tue, 17 Nov 2026 13:18:26 -0800 Subject: [PATCH] fix(ssh): support ACP with durable Cloud workspaces (#22063) ## Thinking Path > - Paperclip coordinates agents that run on local and remote computers. > - SSH environments use the harness installed on a computer the operator controls. > - Cloud SSH tasks now use durable remote workspaces. > - Claude and Codex default to ACP, but ACP rejects those in-place SSH workspaces. > - The shared ACP engine already has SSH process bridges and managed account delivery. > - This pull request lets that engine use the remote task directory and stage runtime assets separately. > - The benefit is ACP execution with the selected Paperclip account and the existing SSH environment. ## Linked Issues or Issue Description **What happened?** A Claude or Codex agent using the default ACP engine cannot run in a volume-free Cloud SSH environment. The adapter rejects its in-place workspace before execution. Setting the engine to CLI works around that restriction. **Expected behavior** ACP runs directly in the authoritative remote task directory. Paperclip keeps using the selected account, permission settings, process bridges, and durable workspace owner. **Steps to reproduce** 1. Create an SSH environment with agent-managed authentication on a volume-free server. 2. Select a Paperclip AI account for a Claude or Codex agent and leave its engine at the default. 3. Assign a task to the agent. 4. Observe the ACP in-place workspace rejection. Related: #21957 adds durable Cloud SSH workspaces. #21248 adds agent-selected SSH accounts. A search for SSH ACP work found #21248 and no open duplicate. ROADMAP.md has no overlapping item. ## What Changed - Admit managed SSH ACP with an in-place workspace in both adapters. - Keep ACP's session directory and fingerprint bound to the remote task directory. - Stage private runtime assets with workspace copying disabled. The remote workspace owner still saves task files. - Retain the copy-mode SSH path and existing account, confinement, and command availability checks. No engine fallback or permission change is added. - Extend the existing lifecycle tests for in-place staging, session reuse, current credentials, account changes, and cleanup after bridge or turn failures. - Extend the real OpenSSH/Postgres heartbeat test to the default ACP engine. Its protocol fixture creates a file and verifies conversation history on a follow-up. - Update the SSH setup documentation. Workspace placement belongs to the shared ACP engine. The adapter changes remove their explicit SSH admission refusals. The server test exercises the existing heartbeat path; no server execution logic changes. Existing large test files retain their shared harnesses instead of adding a second lifecycle mock graph. ## Verification - 418 focused tests pass across 7 suites: 411 adapter/ACP lifecycle tests, all 4 real SSH workspace tests, and 3 real ACP host-spawn smoke tests. - Typechecks pass for adapter-utils, Claude, Codex, and the server. - `pnpm verify` returns 2 because this touches shared runtime code and deployment documentation. Required CI supplies the broader build, browser, and release coverage. - Tested head: `33b17a9a21c0a5929523702ebed9c8faa3b22955`. - `git diff --check` and the local secret/PII scan pass. Greptile reviewed the tested head at 5/5 with no actionable findings. Final-head [CI attempt 2](https://github.com/paperclipai/paperclip/actions/runs/44167483231/attempts/2) passes on `33b17a9a21c0a5929523702ebed9c8faa3b22955`: 58 checks pass and 6 are skipped, with no failures or pending checks. Both required gates pass. Four first-attempt jobs were cancelled after their runners received shutdown signals, confirmed in each job log; automatic recovery reran those jobs successfully. No source change was needed for that recovery. Greptile is 5/5 with no unresolved findings, and the branch has no merge conflicts. GitHub still requires code-owner review before merge. ```sh pnpm exec vitest run --maxWorkers=2 \ packages/adapter-utils/src/acpx-engine/execute.test.ts \ packages/adapters/claude-local/src/server/execute.remote.test.ts \ packages/adapters/codex-local/src/server/execute.remote.test.ts \ packages/adapters/claude-local/src/server/acp.test.ts \ packages/adapters/codex-local/src/server/acp.test.ts PAPERCLIP_ENABLE_DARWIN_SSH_ENV_LAB=1 pnpm exec vitest run --maxWorkers=2 \ server/src/services/ssh-remote-workspace.integration.test.ts pnpm exec vitest run packages/adapter-utils/src/acpx-engine/remote-spawn-smoke.test.ts pnpm --filter @paperclipai/adapter-utils typecheck pnpm --filter @paperclipai/adapter-claude-local typecheck pnpm --filter @paperclipai/adapter-codex-local typecheck pnpm --filter @paperclipai/server typecheck ``` The new admission and staging cases failed before the source fix. The first whole-heartbeat follow-up test also changed an agent setting between turns. That correctly reset the session. The fixture now keeps those settings unchanged and verifies the resumed conversation. Running both engine cases together also exposed duplicate company prefixes and environment names in the fixture; each case now uses its own names. The SSH qualification uses a real isolated OpenSSH server, real Postgres, real ACPX runtime and bridges, and a synthetic ACP provider with a selected test account. It does not call a live provider or run an agent on a user's computer. ## Risks The change affects the shared ACP staging path, restricted to SSH targets that declare in-place realization. Sandbox staging retains its prior behavior, including the existing Codex in-place refusal. Copy-mode SSH sessions retain their prior directory layout. Previously saved sessions keep their format; this adds no migration or session reset. ACP still requires its server command on the remote login PATH. Host authentication and local confinement remain CLI-only. Cold controller/provider reconnection and live Claude/Codex provider acceptance are not qualified by this test. No staging deployment or existing agent configuration is changed by this pull request. ## Model Used OpenAI GPT-6.1 Sol (`gpt-6.1-sol`) through the Codex harness, with xhigh reasoning effort and code tools. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge Co-authored-by: Paperclip --- doc/tailscale-environments.md | 14 +++-- .../src/acpx-engine/execute.test.ts | 19 ++++-- .../adapter-utils/src/acpx-engine/execute.ts | 7 ++- .../adapters/claude-local/src/server/acp.ts | 3 - .../src/server/execute.remote.test.ts | 7 ++- .../adapters/codex-local/src/server/acp.ts | 5 +- .../src/server/execute.remote.test.ts | 7 ++- .../ssh-remote-workspace.integration.test.ts | 59 ++++++++++++++----- 8 files changed, 82 insertions(+), 39 deletions(-) diff --git a/doc/tailscale-environments.md b/doc/tailscale-environments.md index f197a6bdb21..6d9c7f40e7f 100644 --- a/doc/tailscale-environments.md +++ b/doc/tailscale-environments.md @@ -64,12 +64,16 @@ and Tailscale connections. ## SSH on a volume-free Cloud stack Cloud SSH execution uses the same durable task workspace storage as Cloud -sandboxes. Claude Code and Codex use their existing SSH adapters with -`engine=cli`. ACP archive staging cannot use an in-place Cloud workspace. An -explicit engine choice preserves the agent's execution and permission settings. -The selected -SSH account supplies the installed harness; the agent's Paperclip AI account +sandboxes. Claude Code and Codex use their existing SSH adapters. With an +agent-selected Paperclip AI account, the default ACP engine runs directly in the +remote task directory and stages only private runtime assets. The CLI engine +also supports this directory. ACP requires its server command, such as +`claude-agent-acp`, on the SSH login PATH. Selecting SSH does not change the +agent's engine or permission settings. + +The SSH account supplies the installed harness; the agent's Paperclip AI account supplies authentication unless the remote authentication option is enabled. +Host authentication remains supported by the CLI engine. Each task receives a private directory below the configured SSH workspace path, inside `.paperclip-workspaces`. The task runs in that remote directory. Core diff --git a/packages/adapter-utils/src/acpx-engine/execute.test.ts b/packages/adapter-utils/src/acpx-engine/execute.test.ts index a54d179d72f..ea5bb05085c 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.test.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.test.ts @@ -4559,7 +4559,8 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( return { root, stateDir, localCwd, remoteCwd, executionTarget }; } - it.each(["turn", "bridge"])("stages the configured agent account for SSH, resumes it, and copies back before disposal after a %s failure", async failure => { + it.each(["turn", "bridge"].flatMap(failure => [false, true].map(inPlace => ({ failure, inPlace }))))( + "stages the configured SSH account and restores credentials after $failure failure (inPlace=$inPlace)", async ({ failure, inPlace }) => { const { root, stateDir, localCwd, remoteCwd } = await setupRemoteSandbox(); const homes = await Promise.all(["first", "second", "third"].map(async name => { const home = path.join(root, `selected-account-${name}`); @@ -4567,9 +4568,10 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( return home; })); const home = path.join(homes[0]!, "provider"); - const workspace = path.posix.join(remoteCwd, ".paperclip-runtime/runs/run-1/workspace"); - const runtimeRootDir = path.posix.join(workspace, ".paperclip-runtime/custom"); + const workspace = inPlace ? remoteCwd : path.posix.join(remoteCwd, ".paperclip-runtime/runs/run-1/workspace"); + const runtimeRootDir = path.join(root, "private-runtime"); const executionTarget = { kind: "remote", transport: "ssh", remoteCwd, spec: { useHostAuth: false, host: "ssh.example.test", port: 22, username: "agent", remoteCwd, remoteWorkspacePath: remoteCwd, privateKey: null, knownHosts: null, strictHostKeyChecking: true } }; + if (inPlace) Object.assign(executionTarget, { workspaceRealization: { mode: "in_place", authoritativeRoot: remoteCwd, pathAliases: [], outboundRestorePaths: [] } }); let copybackHome = ""; const restore = vi.fn(async () => { await fs.mkdir(copybackHome, { recursive: true }); @@ -4600,8 +4602,14 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( const result = await runExecutor(config, options); expect(seam).toHaveBeenCalledOnce(); expect(seam.mock.calls[0]![0].executionTarget).toMatchObject({ kind: "remote", transport: "ssh" }); + if (inPlace) { + expect(result.sessionInputs[0]?.cwd).toBe(remoteCwd); + expect(vi.mocked(prepareAdapterExecutionTargetRuntime).mock.calls[0]![0]).toMatchObject({ + syncWorkspace: false, workspaceRemoteDir: remoteCwd, privateRuntime: true, retainRuntime: true, + }); + } expect(prepareAdapterExecutionTargetRuntime).toHaveBeenCalledWith(expect.objectContaining({ assets: [{ key: "home", localDir: home }] })); - expect(result.sessionInputs[0]?.cwd).toMatch(new RegExp(`${remoteCwd}/\\.paperclip-runtime/runs/[a-f0-9]+/workspace$`)); + if (!inPlace) expect(result.sessionInputs[0]?.cwd).toMatch(new RegExp(`${remoteCwd}/\\.paperclip-runtime/runs/[a-f0-9]+/workspace$`)); const bridgeInput = vi.mocked(startAdapterExecutionTargetProcessSessionBridge).mock.calls[0]![0]; const env = typeof bridgeInput.env === "function" ? await bridgeInput.env() : bridgeInput.env; expect(env.CODEX_HOME).toBe(path.posix.join(runtimeRootDir, "home")); @@ -4654,7 +4662,8 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( else changed.env.USER_OPTION = "changed"; const reset = await runExecutor(changed, { ...options, runId: "run-4", expectedExitCode: 1, runtime: { sessionParams: resumed.result.sessionParams } }); - expect(reset.sessionInputs[0]?.cwd).not.toBe(result.sessionInputs[0]?.cwd); + if (inPlace) expect(reset.sessionInputs[0]?.cwd).toBe(remoteCwd); + else expect(reset.sessionInputs[0]?.cwd).not.toBe(result.sessionInputs[0]?.cwd); expect(reset.sessionInputs[0]?.resumeSessionId).not.toBe(result.result.sessionId); expect(seam).toHaveBeenCalledTimes(2); expect(restore).toHaveBeenCalledTimes(4); diff --git a/packages/adapter-utils/src/acpx-engine/execute.ts b/packages/adapter-utils/src/acpx-engine/execute.ts index 3c758d26ff8..389058e8252 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.ts @@ -1752,6 +1752,7 @@ async function stageAcpRemoteRuntime(input: { runId: string; target: AdapterExecutionTarget; privateRuntime?: boolean; + syncWorkspace?: boolean; adapterKey: string; workspaceLocalDir: string; // Pin the in-sandbox workspace dir so it provably equals the deterministic @@ -1774,11 +1775,12 @@ async function stageAcpRemoteRuntime(input: { }): Promise { await input.onLog( "stdout", - `[paperclip] Syncing workspace to ${describeAdapterExecutionTarget(input.target)}.\n`, + `[paperclip] Syncing ${input.syncWorkspace === false ? "runtime assets" : "workspace"} to ${describeAdapterExecutionTarget(input.target)}.\n`, ); return await prepareAdapterExecutionTargetRuntime({ privateRuntime: input.privateRuntime, retainRuntime: input.privateRuntime, + syncWorkspace: input.syncWorkspace, runId: input.runId, target: input.target, adapterKey: input.adapterKey, @@ -2350,7 +2352,7 @@ async function buildRuntime(input: { const sshWorkspaceRunId = useRemoteProcessSession && executionTarget?.kind === "remote" && executionTarget.transport === "ssh" ? shortHash({ companyId: agent.companyId, agentId: agent.id, taskKey, fingerprintIdentity }) : null; - if (sshWorkspaceRunId && executionTarget?.kind === "remote") { + if (sshWorkspaceRunId && executionTarget?.kind === "remote" && executionTarget.workspaceRealization?.mode !== "in_place") { sessionCwd = path.posix.join(executionTarget.remoteCwd, ".paperclip-runtime", "runs", sshWorkspaceRunId, "workspace"); fingerprintIdentity = { ...fingerprintIdentity, cwd: sessionCwd }; } @@ -2441,6 +2443,7 @@ async function buildRuntime(input: { const staged = await stageAcpRemoteRuntime({ runId: sshWorkspaceRunId ?? runId, privateRuntime: remoteTarget.transport === "ssh", + syncWorkspace: remoteTarget.transport === "ssh" ? remoteTarget.workspaceRealization?.mode !== "in_place" : undefined, target: remoteTarget, adapterKey: input.engine.adapterType, workspaceLocalDir: cwd, diff --git a/packages/adapters/claude-local/src/server/acp.ts b/packages/adapters/claude-local/src/server/acp.ts index cda1d2beec5..1a653c05f42 100644 --- a/packages/adapters/claude-local/src/server/acp.ts +++ b/packages/adapters/claude-local/src/server/acp.ts @@ -620,9 +620,6 @@ async function claudeAcpUnavailableReason( }); const managedSsh = target?.kind === "remote" && target.transport === "ssh" && target.spec.useHostAuth === false && Boolean(input.config.managedAiConnection); - if (managedSsh && target.workspaceRealization?.mode === "in_place") { - return "In-place SSH workspace realization requires the Claude CLI engine; ACP archive staging is not supported."; - } if (target?.kind === "remote" && !sandboxTargetHasProcessSessionBridge(target) && !managedSsh) { if (target.transport === "sandbox") { return "Claude ACP requires a bidirectional remote process target; this sandbox exposes only one-shot command execution."; diff --git a/packages/adapters/claude-local/src/server/execute.remote.test.ts b/packages/adapters/claude-local/src/server/execute.remote.test.ts index c97cd2ee831..c0f58fa873f 100644 --- a/packages/adapters/claude-local/src/server/execute.remote.test.ts +++ b/packages/adapters/claude-local/src/server/execute.remote.test.ts @@ -121,8 +121,10 @@ describe("claude remote execution", () => { }; } - it.each([undefined, "acp"])("admits managed SSH ACP through the public execution entry (engine=%s)", async engine => { + it.each([undefined, "acp"].flatMap(engine => (["copy", "in_place"] as const).map(mode => ({ engine, mode }))))( + "admits managed SSH ACP through the public execution entry (engine=$engine, workspace=$mode)", async ({ engine, mode }) => { const ctx = managedSshAcpContext(engine); + ctx.executionTarget.workspaceRealization.mode = mode; const actual = await vi.importActual("@paperclipai/adapter-utils/server-utils"); const bin = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-client-probe-")); cleanupDirs.push(bin); @@ -150,13 +152,12 @@ describe("claude remote execution", () => { } }); - it.each(["host auth", "legacy auth", "missing account", "in-place", "confinement", "missing SSH client"])("rejects unsupported SSH ACP at the public execution entry: %s", async reason => { + it.each(["host auth", "legacy auth", "missing account", "confinement", "missing SSH client"])("rejects unsupported SSH ACP at the public execution entry: %s", async reason => { const ctx = managedSshAcpContext("acp"); const originalResolve = ensureCommandResolvable.getMockImplementation()!; if (reason === "host auth") ctx.executionTarget.spec.useHostAuth = true; if (reason === "legacy auth") ctx.executionTarget.spec.useHostAuth = undefined; if (reason === "missing account") delete ctx.config.managedAiConnection; - if (reason === "in-place") ctx.executionTarget.workspaceRealization.mode = "in_place"; if (reason === "confinement") ctx.config.filesystemScope = "workspace"; const previousPath = process.env.PATH; if (reason === "missing SSH client") { diff --git a/packages/adapters/codex-local/src/server/acp.ts b/packages/adapters/codex-local/src/server/acp.ts index 939cd4e500c..70d4f463126 100644 --- a/packages/adapters/codex-local/src/server/acp.ts +++ b/packages/adapters/codex-local/src/server/acp.ts @@ -99,7 +99,7 @@ export async function resolveCodexExecutionEngineForRun( executionTarget: input.executionTarget, legacyRemoteExecution: input.executionTransport?.remoteExecution, }); - if (target?.workspaceRealization?.mode === "in_place") { + if (target?.workspaceRealization?.mode === "in_place" && !(target.kind === "remote" && target.transport === "ssh")) { return unavailable("In-place workspace realization requires the Codex CLI engine; ACP archive staging is not supported."); } const filesystemScope = parseLocalProcessFilesystemScope(input.config.filesystemScope); @@ -509,9 +509,6 @@ async function codexAcpUnavailableReason( }); const managedSsh = target?.kind === "remote" && target.transport === "ssh" && target.spec.useHostAuth === false && Boolean(input.config.managedAiConnection); - if (managedSsh && target.workspaceRealization?.mode === "in_place") { - return "In-place SSH workspace realization requires the Codex CLI engine; ACP archive staging is not supported."; - } if (target?.kind === "remote" && !sandboxTargetHasProcessSessionBridge(target) && !managedSsh) { if (target.transport === "sandbox") { return "Codex ACP requires a bidirectional remote process target; this sandbox exposes only one-shot command execution."; diff --git a/packages/adapters/codex-local/src/server/execute.remote.test.ts b/packages/adapters/codex-local/src/server/execute.remote.test.ts index 46a5e4e9ef8..a26ee8b9199 100644 --- a/packages/adapters/codex-local/src/server/execute.remote.test.ts +++ b/packages/adapters/codex-local/src/server/execute.remote.test.ts @@ -111,8 +111,10 @@ describe("codex remote execution", () => { }; } - it.each([undefined, "acp"])("admits managed SSH ACP through the public execution entry (engine=%s)", async engine => { + it.each([undefined, "acp"].flatMap(engine => (["copy", "in_place"] as const).map(mode => ({ engine, mode }))))( + "admits managed SSH ACP through the public execution entry (engine=$engine, workspace=$mode)", async ({ engine, mode }) => { const ctx = managedSshAcpContext(engine); + ctx.executionTarget.workspaceRealization.mode = mode; const actual = await vi.importActual("@paperclipai/adapter-utils/server-utils"); const bin = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-client-probe-")); cleanupDirs.push(bin); @@ -140,13 +142,12 @@ describe("codex remote execution", () => { } }); - it.each(["host auth", "legacy auth", "missing account", "in-place", "confinement", "missing SSH client"])("rejects unsupported SSH ACP at the public execution entry: %s", async reason => { + it.each(["host auth", "legacy auth", "missing account", "confinement", "missing SSH client"])("rejects unsupported SSH ACP at the public execution entry: %s", async reason => { const ctx = managedSshAcpContext("acp"); const originalResolve = ensureCommandResolvable.getMockImplementation()!; if (reason === "host auth") ctx.executionTarget.spec.useHostAuth = true; if (reason === "legacy auth") ctx.executionTarget.spec.useHostAuth = undefined; if (reason === "missing account") delete ctx.config.managedAiConnection; - if (reason === "in-place") ctx.executionTarget.workspaceRealization.mode = "in_place"; if (reason === "confinement") ctx.config.filesystemScope = "workspace"; const previousPath = process.env.PATH; if (reason === "missing SSH client") { diff --git a/server/src/services/ssh-remote-workspace.integration.test.ts b/server/src/services/ssh-remote-workspace.integration.test.ts index 3e820846f2c..4742fba6dc3 100644 --- a/server/src/services/ssh-remote-workspace.integration.test.ts +++ b/server/src/services/ssh-remote-workspace.integration.test.ts @@ -7,7 +7,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; import { agents, authUsers, companyMemberships, closeRegisteredClients, companies, createDb, environmentLeases, environments, executionWorkspaces, heartbeatRuns, issues } from "@paperclipai/db"; import type { AdapterSshExecutionTarget } from "@paperclipai/adapter-utils/execution-target"; -import { buildSshEnvLabFixtureConfig, getSshEnvLabSupport, startSshEnvLabFixture, +import { buildSshEnvLabFixtureConfig, getSshEnvLabSupport, shellQuote, startSshEnvLabFixture, stopSshEnvLabFixture, type SshEnvLabFixtureState } from "@paperclipai/adapter-utils/ssh"; import type { EnvironmentLease } from "@paperclipai/shared"; import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../__tests__/helpers/embedded-postgres.js"; @@ -195,49 +195,78 @@ describe.skipIf(!postgres.supported || !ssh.supported)("Cloud SSH durable worksp } finally { extract.mockRestore(); } }, 60_000); - it("runs an assigned Claude task through heartbeat with the configured Paperclip account", async () => { + it.each(["cli", "acp"])("runs an assigned Claude %s task through heartbeat with the configured Paperclip account", async engine => { vi.stubEnv("PAPERCLIP_EPHEMERAL_SERVER", "true"); vi.stubEnv("PAPERCLIP_STORAGE_PROVIDER", "s3"); vi.stubEnv("PAPERCLIP_AGENT_JWT_SECRET", "synthetic-cloud-ssh-jwt-secret"); vi.stubEnv("AWS_EC2_METADATA_DISABLED", "true"); - const storage = { ...createStorageService(createLocalDiskStorageProvider(path.join(root, "heartbeat-objects"))), provider: "s3" as const }; + const storage = { ...createStorageService(createLocalDiskStorageProvider(path.join(root, `heartbeat-${engine}-objects`))), provider: "s3" as const }; vi.spyOn(storageModule, "getStorageService").mockReturnValue(storage); vi.spyOn(runLogModule, "getRunLogStore").mockReturnValue(objectLogFixture()); const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(), userId = `ssh-user-${randomUUID()}`; await db.insert(authUsers).values({ id: userId, name: "SSH test user", email: `${userId}@example.test`, createdAt: new Date(), updatedAt: new Date() }); - await db.insert(companies).values({ id: companyId, name: "Cloud SSH task", issuePrefix: "HSSH", defaultResponsibleUserId: userId }); + await db.insert(companies).values({ id: companyId, name: "Cloud SSH task", issuePrefix: engine === "cli" ? "HCLI" : "HACP", defaultResponsibleUserId: userId }); await db.insert(companyMemberships).values({ companyId, principalId: userId, principalType: "user", status: "active", membershipRole: "member" }); const account = await aiConnectionService(db).save(companyId, userId, { provider: "anthropic", method: "api_key", ownership: "personal", name: "Selected test account", apiKey: "synthetic-selected-ssh-key", agentIds: [], allAgents: true }, "synthetic-selected-ssh-key"); const config = await buildSshEnvLabFixtureConfig(sshd!); - const command = path.join(root, "fake-claude"); + const command = path.join(root, `fake-claude-${engine}`); await writeFile(command, `#!${process.execPath} if (process.argv.includes('--version')) { console.log('2.1.0'); process.exit(0); } if (process.env.ANTHROPIC_API_KEY !== 'synthetic-selected-ssh-key') process.exit(91); const fs = require('fs'), path = require('path'); const history = path.join(process.env.CLAUDE_CONFIG_DIR, 'projects', 'fixture-history'); -const resumed = process.argv.includes('--resume'); -if (resumed && !fs.existsSync(history)) process.exit(92); +const runTask = (resumed) => { +if (resumed && !fs.existsSync(history)) throw Error('Missing provider conversation history'); fs.mkdirSync(path.dirname(history), {recursive:true}); fs.writeFileSync(history, 'provider conversation state'); fs.writeFileSync('continuation.json', JSON.stringify({resumed})); require('fs').writeFileSync('hello_world.txt', 'Hello World\\n'); -console.log(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'Created hello_world.txt',session_id:'12345678-1234-4abc-9def-123456789012',total_cost_usd:0,usage:{input_tokens:1,output_tokens:1}})); +}; +if (process.argv.includes('--acp-fixture')) { + const send = message => process.stdout.write(JSON.stringify(message) + '\\n'); + let resumed = false; + require('readline').createInterface({input:process.stdin}).on('line', line => { + const request = JSON.parse(line); + if (request.id === undefined) return; + let result = {}; + try { + if (request.method === 'initialize') result = {protocolVersion:1, agentCapabilities:{loadSession:true, sessionCapabilities:{close:{}}}}; + if (request.method === 'session/new') result = {sessionId:'12345678-1234-4abc-9def-123456789012'}; + if (request.method === 'session/load') resumed = true; + if (request.method === 'session/new' || request.method === 'session/load') { + if (request.params.cwd !== process.cwd()) throw Error('ACP working directory mismatch'); + } + if (request.method === 'session/prompt') { + runTask(resumed); + send({jsonrpc:'2.0',method:'session/update',params:{sessionId:request.params.sessionId, + update:{sessionUpdate:'agent_message_chunk',content:{type:'text',text:'Created hello_world.txt'}}}}); + result = {stopReason:'end_turn'}; + } + send({jsonrpc:'2.0',id:request.id,result}); + } catch (error) { send({jsonrpc:'2.0',id:request.id,error:{code:-32603,message:error.message}}); } + }); +} else { + runTask(process.argv.includes('--resume')); + console.log(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'Created hello_world.txt',session_id:'12345678-1234-4abc-9def-123456789012',total_cost_usd:0,usage:{input_tokens:1,output_tokens:1}})); +} `, { mode: 0o700 }); - const storedConfig = await normalizeEnvironmentConfigForPersistence({ db, companyId, environmentName: "Heartbeat SSH", + const storedConfig = await normalizeEnvironmentConfigForPersistence({ db, companyId, environmentName: `Heartbeat SSH ${engine}`, driver: "ssh", secretProvider: "local_encrypted", config: { ...config, useHostAuth: false } }); - const [environment] = await db.insert(environments).values({ driver: "ssh", name: "Heartbeat SSH", config: storedConfig }).returning(); + const [environment] = await db.insert(environments).values({ driver: "ssh", name: `Heartbeat SSH ${engine}`, config: storedConfig }).returning(); await secretService(db).replaceSecretRefsForInstanceTarget({ targetType: "environment", targetId: environment.id }, await collectEnvironmentSecretRefs({ db, environment: { id: environment.id, driver: "ssh", config: storedConfig } })); + const adapterConfig = { command, ...(engine === "cli" ? { engine: "cli" } : { agentCommand: `${shellQuote(command)} --acp-fixture`, acpWarmHandleIdleMs: 60_000 }), + model: "claude-sonnet-4-6", timeoutSec: 20 }; await db.insert(agents).values({ id: agentId, companyId, name: "Claude SSH task", adapterType: "claude_local", status: "idle", - defaultEnvironmentId: environment.id, adapterConfig: { command, engine: "cli", model: "claude-sonnet-4-6", timeoutSec: 20 }, + defaultEnvironmentId: environment.id, adapterConfig, runtimeConfig: { aiConnection: { provider: "anthropic", method: "api_key", mode: "delegated", connectionId: account.connectionId, grantId: account.grantId } } }); await db.insert(issues).values({ id: issueId, companyId, title: "Create hello_world.txt", status: "todo", assigneeAgentId: agentId, responsibleUserId: userId, issueNumber: 1 }); const adapter = getServerAdapter("claude_local"), execute = adapter.execute; expect(adapter.type).toBe("claude_local"); - vi.spyOn(adapter, "execute").mockImplementation(async input => { + const executing = vi.spyOn(adapter, "execute").mockImplementation(async input => { const result = await execute(input); - // The synthetic CLI stands in for the provider's final task update too. + // The synthetic provider stands in for the final task update too. if (result.exitCode === 0) await db.update(issues).set({ status: "in_review" }).where(eq(issues.id, issueId)); return result; }); @@ -266,9 +295,11 @@ console.log(JSON.stringify({type:'result',subtype:'success',is_error:false,resul retryReason: continued?.scheduledRetryReason, retryAt: continued?.scheduledRetryAt, errorCode: continued?.errorCode, error: continued?.error, result: continued?.resultJson, log: await heartbeat.readLog(followup!.id, { limitBytes: 12_000 }).catch(() => null) })); expect(continued).toMatchObject({ status: "succeeded" }); - expect(JSON.parse(await readFile(path.join(authority.remoteCwd, "continuation.json"), "utf8"))).toEqual({ resumed: true }); + const continuation = JSON.parse(await readFile(path.join(authority.remoteCwd, "continuation.json"), "utf8")); + expect(continuation).toEqual({ resumed: true }); } finally { await heartbeat.drainActiveRunExecutions(); + executing.mockRestore(); } }, 60_000); });