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); });