diff --git a/doc/tailscale-environments.md b/doc/tailscale-environments.md index 6d9c7f40e7f..e966cf1b5ee 100644 --- a/doc/tailscale-environments.md +++ b/doc/tailscale-environments.md @@ -64,17 +64,44 @@ 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 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. +sandboxes. All built-in SSH-capable harnesses use their existing adapters: +Claude Code, Codex, Cursor, Gemini, Grok, Kimi, OpenCode, and Pi. The native +Paperclip Runner also retains its SSH execution path. Cloud API and gateway +adapters keep their provider-managed execution rather than selecting an SSH host. + +With a compatible agent-selected Paperclip AI account, ACP can run directly in +the remote task directory and stage only private runtime assets. Claude Code, +Codex, Gemini, and Kimi expose this ACP path. The CLI engines support the same +remote 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, +account compatibility, or permission settings. + +An in-place SSH workspace exists on the SSH computer. Neither engine creates +that workspace path on the controller. ACP keeps its local relay and state files +in the controller's state directory and leaves remote project settings in place. +Existing local Claude settings still contribute to the saved ACP conversation +identity for upgrade compatibility. SSH startup can read that file when present, +but does not create or rewrite it. 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. +Saved CLI sessions retain the SSH connection identity through persistence, so a +follow-up cannot attach to a different host. Disposable managed account-home +paths do not reset new ACP conversations. A configuration identity change is +reported as `acpx_session_identity_changed`; older Codex or Gemini ACP sessions +that used the former home identity can reset once at upgrade. Task files remain +in their durable SSH workspace. Older CLI sessions without a saved SSH identity +remain unresumable; Core does not guess their source host. + +Pi keeps its JSONL conversation file under `.paperclip/pi/sessions` in the +durable SSH task workspace. Its temporary credential home is removed after +each turn. A follow-up uses the saved session file only when the SSH identity +and the file's workspace header match. Existing readable session paths keep +working; files already removed by an older release cannot be recovered. + + Each task receives a private directory below the configured SSH workspace path, inside `.paperclip-workspaces`. The task runs in that remote directory. Core keeps the workspace binding and saved file versions in its database and object diff --git a/packages/adapter-utils/src/acpx-engine/execute.test.ts b/packages/adapter-utils/src/acpx-engine/execute.test.ts index ea5bb05085c..74f3d01a97b 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.test.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.test.ts @@ -1,4 +1,5 @@ import fs from "node:fs/promises"; +import { createHash } from "node:crypto"; import { summarizeStartupReuse } from "../startup-reuse.js"; import os from "node:os"; import path from "node:path"; @@ -29,6 +30,8 @@ vi.mock("@paperclipai/adapter-utils/execution-target", async (importActual) => { }); import { buildAcpxRunSummary, + buildSessionFingerprint, + buildSessionKey, createAcpxEngineExecutor, findAncestorBin, geminiVersionSupportsNativeAcpFlag, @@ -48,12 +51,13 @@ import { ACPX_STALLED_TOOL_TIMEOUT_MS, } from "./constants.js"; import { sessionCodec } from "./session-codec.js"; -import { runChildProcess } from "../server-utils.js"; +import { resolvePaperclipInstanceRootForAdapter, runChildProcess } from "../server-utils.js"; import { createPromptContextFixture } from "../test-fixtures/prompt-context.js"; import { setExpensiveWorkspaceGitExecutor } from "../git-workspace-sync.js"; import { createWorkspaceRestoreTeardown } from "../workspace-restore-teardown.js"; import { withWorkspaceRestoreDiagnostics, withWorkspaceRestoreStep } from "../workspace-restore-diagnostics.js"; import { resolveReferencedSourceIgnore } from "../sandbox-managed-runtime.js"; +import { managedAiSessionFingerprintConfig } from "../managed-ai-runtime-env.js"; import { getActiveStepContext, runWithRuntimeParent, @@ -2177,11 +2181,11 @@ describe("shared ACPX engine runtime behavior", () => { ); const changedEnv = await runExecutor( { ...baseConfig, env: { OPENROUTER_API_KEY: "value-2" } }, - { context: { taskId: "issue-1", wakeReason: "issue_assigned" } }, + { context: { taskId: "issue-1", wakeReason: "issue_assigned" }, runtime: { sessionParams: first.result.sessionParams } }, ); const sameEnvNewWake = await runExecutor( { ...baseConfig, env: { OPENROUTER_API_KEY: "value-1" } }, - { context: { taskId: "issue-1", wakeReason: "comment", wakeCommentId: "c-9" } }, + { context: { taskId: "issue-1", wakeReason: "comment", wakeCommentId: "c-9" }, runtime: { sessionParams: first.result.sessionParams } }, ); const fp = (r: { result: { sessionParams?: unknown } }) => @@ -2191,9 +2195,11 @@ describe("shared ACPX engine runtime behavior", () => { // the next launch sources the latest env. expect(fp(first)).toBeDefined(); expect(fp(changedEnv)).not.toBe(fp(first)); + expect(changedEnv.result.resultJson?.sessionReset).toMatchObject({ code: "acpx_session_identity_changed" }); // A new heartbeat with the same config env keeps the fingerprint stable, so // per-wake PAPERCLIP_* churn does not needlessly reset the session. expect(fp(sameEnvNewWake)).toBe(fp(first)); + expect(sameEnvNewWake.result.resultJson?.sessionReset).toBeUndefined(); }); it("keeps rotated run scratch paths out of session identity while retaining user temp overrides", async () => { @@ -4559,9 +4565,22 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( return { root, stateDir, localCwd, remoteCwd, executionTarget }; } - 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(); + it.each(["custom", "claude", "codex", "gemini"].flatMap(agent => ["turn", "bridge"].flatMap(failure => [ + { inPlace: false, legacySettings: false }, { inPlace: true, legacySettings: false }, + ...(agent === "claude" ? [{ inPlace: true, legacySettings: true }] : []), + ].map(workspace => ({ agent, failure, ...workspace })))))( + "stages the configured SSH account and restores credentials after $failure failure (agent=$agent, inPlace=$inPlace, legacySettings=$legacySettings)", async ({ agent, failure, inPlace, legacySettings }) => { + const { root, stateDir, localCwd, remoteCwd: copyRemoteCwd } = await setupRemoteSandbox(); + const remoteMount = path.join(root, "remote-filesystem"); + await fs.writeFile(remoteMount, "This path is not a directory on the controller."); + const remoteCwd = inPlace && !legacySettings ? path.join(remoteMount, "workspace") : copyRemoteCwd; + const settingsPath = path.join(remoteCwd, ".claude/settings.local.json"); + const legacyPermissions = { allow: ["Read(legacy-file)"], additionalDirectories: [path.join(root, "legacy-read-root")], defaultMode: "acceptEdits" }; + const settingsContents = JSON.stringify({ permissions: legacyPermissions, legacySetting: "preserved" }); + if (legacySettings) { + await fs.mkdir(path.dirname(settingsPath), { recursive: true }); + await fs.writeFile(settingsPath, settingsContents); + } const homes = await Promise.all(["first", "second", "third"].map(async name => { const home = path.join(root, `selected-account-${name}`); await fs.mkdir(path.join(home, "provider"), { recursive: true }); @@ -4591,7 +4610,8 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( vi.mocked(startAdapterExecutionTargetPaperclipBridge).mockResolvedValue({ env: { PAPERCLIP_API_URL: "http://127.0.0.1:3210", PAPERCLIP_API_KEY: "bridge-fixture" }, stop: stopControl }); vi.mocked(startAdapterExecutionTargetProcessSessionBridge).mockResolvedValue({ agentCommand: "/fixture/agent-proxy.cjs", stop: stopProcess }); try { - const configForTurn = (turn: number) => ({ agent: "custom", agentCommand: "fixture-acp-client", stateDir, cwd: localCwd, + const configForTurn = (turn: number) => ({ agent, agentCommand: "fixture-acp-client", stateDir, cwd: inPlace ? remoteCwd : localCwd, + paperclipSkillKeys: [], paperclipRuntimeSkills: [], managedAiConnection: { identity: `credential-revision-${turn}`, sessionIdentity: "selected-agent-account" }, env: { CODEX_HOME: path.join(homes[turn]!, "provider"), HOME: homes[turn]!, XDG_CONFIG_HOME: path.join(homes[turn]!, "config"), XDG_DATA_HOME: path.join(homes[turn]!, "data"), OPENAI_API_KEY: `fixture-token-${turn}`, USER_OPTION: "kept" }, mode: "persistent" }); @@ -4600,10 +4620,46 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( const options = { authToken: "run-fixture", executionTarget, prepareRemoteManagedHome: seam, stagedRuntimes, onStartTurn: () => { if (failTurn) throw new Error("fixture SSH turn failed"); } }; const result = await runExecutor(config, options); + let savedParams = result.result.sessionParams; + if (agent === "claude" && inPlace) { + const canonical = (value: unknown): unknown => Array.isArray(value) ? value.map(canonical) + : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, entry]) => [key, canonical(entry)])) : value; + const legacyHash = (value: unknown) => createHash("sha256").update(JSON.stringify(canonical(value))).digest("hex").slice(0, 16); + const instanceRoot = resolvePaperclipInstanceRootForAdapter({ homeDir: process.env.PAPERCLIP_HOME?.trim() || path.join(os.homedir(), ".paperclip"), instanceId: process.env.PAPERCLIP_INSTANCE_ID?.trim() || "default" }); + const { commandNotes: _notes, ...skillsIdentity } = savedParams!.skills as Record; + expect(skillsIdentity.selectedSkills).toEqual([]); + const legacyFingerprint = buildSessionFingerprint({ + acpxAgent: "claude", agentCommand: "fixture-acp-client", cwd: remoteCwd, mode: "persistent", + permissionMode: "approve-all", nonInteractivePermissions: "deny", requestedModel: "", requestedThinkingEffort: "", fastMode: false, + remoteExecutionIdentity: savedParams!.remoteExecution as Record, + additionalSourcesIdentity: [] as unknown as Record, skillsIdentity, skillPromptInstructions: "", + paperclipClaudeSettings: { + allow: [...(legacySettings ? legacyPermissions.allow : []), "Bash(curl:*)", "Bash(env:*)", "Bash(env)", `Bash(${remoteCwd}/scripts/paperclip-issue-update.sh:*)`, `Bash(${remoteCwd}/scripts/paperclip:*)`].sort(), + additionalDirectories: [...(legacySettings ? legacyPermissions.additionalDirectories : []), stateDir, path.join(instanceRoot, "companies/company-1")].sort(), + defaultMode: legacySettings ? "acceptEdits" : "default", + }, + mcpServers: [], secretManifestHash: legacyHash([]), + adapterEnvHash: legacyHash(managedAiSessionFingerprintConfig({ env: config.env, managedAiConnection: config.managedAiConnection }, config.env.HOME)), + }); + const legacyParams = sessionCodec.serialize({ ...savedParams, configFingerprint: legacyFingerprint, + sessionKey: buildSessionKey({ companyId: "company-1", agentId: "agent-1", taskKey: "default" }, legacyFingerprint) }); + expect(savedParams!.configFingerprint).toBe(legacyParams!.configFingerprint); + expect(savedParams!.sessionKey).toBe(legacyParams!.sessionKey); + savedParams = sessionCodec.deserialize(JSON.parse(JSON.stringify(legacyParams))); + if (legacySettings) await expect(fs.readFile(settingsPath, "utf8")).resolves.toBe(settingsContents); + } expect(seam).toHaveBeenCalledOnce(); expect(seam.mock.calls[0]![0].executionTarget).toMatchObject({ kind: "remote", transport: "ssh" }); if (inPlace) { expect(result.sessionInputs[0]?.cwd).toBe(remoteCwd); + const hostSpawnCwd = result.runtimeOptions[0]!.spawnCwd as string; + expect(hostSpawnCwd).not.toBe(remoteCwd); + const hostProcess = await runChildProcess("ssh-acp-host-cwd", process.execPath, ["-e", "process.stdout.write(process.cwd())"], { + cwd: hostSpawnCwd, env: {}, timeoutSec: 10, graceSec: 1, onLog: async () => {}, + }); + expect(hostProcess.exitCode).toBe(0); + expect(hostProcess.stdout).toBe(await fs.realpath(hostSpawnCwd)); + await expect(fs.readFile(remoteMount, "utf8")).resolves.toBe("This path is not a directory on the controller."); expect(vi.mocked(prepareAdapterExecutionTargetRuntime).mock.calls[0]![0]).toMatchObject({ syncWorkspace: false, workspaceRemoteDir: remoteCwd, privateRuntime: true, retainRuntime: true, }); @@ -4630,7 +4686,7 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( expect(stopProcess).toHaveBeenCalledOnce(); expect(dispose).not.toHaveBeenCalled(); await fs.rm(homes[0]!, { recursive: true, force: true }); - const resumed = await runExecutor(configForTurn(1), { ...options, runId: "run-2", runtime: { sessionParams: result.result.sessionParams } }); + const resumed = await runExecutor(configForTurn(1), { ...options, runId: "run-2", runtime: { sessionParams: savedParams } }); expect(resumed.sessionInputs[0]?.cwd).toBe(result.sessionInputs[0]?.cwd); expect(resumed.sessionInputs[0]?.resumeSessionId).toBe(result.result.sessionId); const resumeBridge = vi.mocked(startAdapterExecutionTargetProcessSessionBridge).mock.calls.at(-1)![0]; @@ -4669,6 +4725,27 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( expect(restore).toHaveBeenCalledTimes(4); expect(dispose).toHaveBeenCalledTimes(2); expect(stagedRuntimes.size).toBe(0); + if (inPlace && failure === "turn" && ["codex", "gemini"].includes(agent)) { + const canonical = (value: unknown): unknown => Array.isArray(value) ? value.map(canonical) + : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, entry]) => [key, canonical(entry)])) : value; + const legacyHash = (value: unknown) => createHash("sha256").update(JSON.stringify(canonical(value))).digest("hex").slice(0, 16); + const { commandNotes: _notes, ...legacySkills } = result.result.sessionParams!.skills as Record; + const legacyFingerprint = buildSessionFingerprint({ acpxAgent: agent, agentCommand: "fixture-acp-client", cwd: remoteCwd, + mode: "persistent", permissionMode: "approve-all", nonInteractivePermissions: "deny", requestedModel: "", requestedThinkingEffort: "", fastMode: false, + remoteExecutionIdentity: result.result.sessionParams!.remoteExecution as Record, + additionalSourcesIdentity: [] as unknown as Record, skillsIdentity: legacySkills, skillPromptInstructions: "", + paperclipClaudeSettings: null, mcpServers: [], secretManifestHash: legacyHash([]), + adapterEnvHash: legacyHash(managedAiSessionFingerprintConfig({ env: config.env, managedAiConnection: config.managedAiConnection }, config.env.HOME)) }); + expect(legacyFingerprint).not.toBe(result.result.sessionParams!.configFingerprint); + const stored = sessionCodec.serialize({ ...result.result.sessionParams, configFingerprint: legacyFingerprint, + sessionKey: buildSessionKey({ companyId: "company-1", agentId: "agent-1", taskKey: "default" }, legacyFingerprint) }); + failTurn = false; + const upgraded = await runExecutor(configForTurn(1), { ...options, runId: "run-upgrade", + runtime: { sessionParams: sessionCodec.deserialize(JSON.parse(JSON.stringify(stored))) } }); + expect(upgraded.sessionInputs[0]?.resumeSessionId).toBeUndefined(); + expect(upgraded.result.resultJson?.sessionReset).toMatchObject({ code: "acpx_session_identity_changed" }); + await expect(fs.readFile(remoteMount, "utf8")).resolves.toBe("This path is not a directory on the controller."); + } } finally { vi.mocked(prepareAdapterExecutionTargetRuntime).mockReset().mockImplementation(actual.prepareAdapterExecutionTargetRuntime); vi.mocked(startAdapterExecutionTargetPaperclipBridge).mockReset().mockImplementation(actual.startAdapterExecutionTargetPaperclipBridge); diff --git a/packages/adapter-utils/src/acpx-engine/execute.ts b/packages/adapter-utils/src/acpx-engine/execute.ts index 389058e8252..6f0dc54eb4c 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.ts @@ -30,6 +30,7 @@ import { adapterExecutionTargetEnablesSandboxDuplexBridge, formatAdapterExecutionTimeoutErrorMessage, formatAdapterExecutionTimeoutStartLogLine, + ensureAdapterExecutionTargetLocalWorkspace, prepareAdapterExecutionTargetRuntime, readAdapterExecutionTarget, resolveAdapterExecutionTargetTimeout, @@ -75,7 +76,6 @@ import { asString, buildInvocationEnvForLogs, buildPaperclipEnv, - ensureAbsoluteDirectory, ensurePathInEnv, ensurePaperclipSkillSymlink, isForbiddenConfigEnvKey, @@ -1663,11 +1663,12 @@ function uniqueSorted(values: Array): string[] { // denies every non-allowlisted tool and never reaches `canUseTool`), and we // widen the SDK's Read sandbox to include the Paperclip state dirs the agent // needs to talk to its own control plane. -async function writePaperclipClaudeSettings(input: { +async function preparePaperclipClaudeSettings(input: { cwd: string; stateDir: string; agentHome: string; companyId: string; + persist: boolean; }): Promise { const filePath = path.join(input.cwd, ".claude", "settings.local.json"); const instanceRoot = defaultPaperclipInstanceDir(); @@ -1723,7 +1724,7 @@ async function writePaperclipClaudeSettings(input: { defaultMode, }; const next: Record = { ...existing, permissions: nextPermissions }; - await writeFileAtomically({ + if (input.persist) await writeFileAtomically({ target: filePath, contents: `${JSON.stringify(next, null, 2)}\n`, mode: 0o600, @@ -1970,6 +1971,8 @@ async function buildRuntime(input: { executionTarget: input.ctx.executionTarget, legacyRemoteExecution: input.ctx.executionTransport?.remoteExecution, }); + const sshWorkspaceInPlace = executionTarget?.kind === "remote" && + executionTarget.transport === "ssh" && executionTarget.workspaceRealization?.mode === "in_place"; const remoteExecutionIdentity = adapterExecutionTargetSessionIdentity(executionTarget); const effectiveExecutionCwd = remoteExecutionIdentity && typeof remoteExecutionIdentity.remoteCwd === "string" @@ -1999,7 +2002,7 @@ async function buildRuntime(input: { // Step 1 — workspace.resolve: the workspace resolution/fallback chain closes // here on the awaited directory materialization. await measureStartupStep(input.ctx, nowMs, "workspace.resolve", () => - ensureAbsoluteDirectory(cwd, { createIfMissing: true }), + ensureAdapterExecutionTargetLocalWorkspace(executionTarget, cwd), stepMetrics, ); @@ -2035,6 +2038,7 @@ async function buildRuntime(input: { const timeoutSec = timeoutResolution.timeoutSec; const stateDir = path.resolve(asString(config.stateDir, "") || defaultStateDir(agent.companyId, agent.id)); await fs.mkdir(stateDir, { recursive: true }); + const hostSpawnCwd = sshWorkspaceInPlace ? stateDir : cwd; const envConfig = parseObject(config.env); const env: Record = { ...buildPaperclipEnv(agent, input.ctx.agentIdentity), PAPERCLIP_RUN_ID: runId }; @@ -2174,7 +2178,7 @@ async function buildRuntime(input: { skillsIdentity = preparedSkills.identity; skillCommandNotes.push(...preparedSkills.commandNotes); claudeSkillsBundleDir = preparedSkills.bundleDir; - paperclipClaudeSettings = await writePaperclipClaudeSettings({ + paperclipClaudeSettings = await preparePaperclipClaudeSettings({ cwd, stateDir, // A run's writable AGENT_HOME moves, but the existing permission root @@ -2183,8 +2187,9 @@ async function buildRuntime(input: { // still receives this run's actual AGENT_HOME. agentHome: asString(workspaceContext.agentHomeForPermissions, agentHome), companyId: agent.companyId, + persist: !sshWorkspaceInPlace, }); - skillCommandNotes.push( + if (!sshWorkspaceInPlace) skillCommandNotes.push( `Wrote Paperclip-managed Claude settings to ${paperclipClaudeSettings.filePath} (defaultMode=${paperclipClaudeSettings.defaultMode}${ paperclipClaudeSettings.overrodeDontAsk ? "; overrode user dontAsk" : "" }, +${paperclipClaudeSettings.additionalDirectories.length} read root(s), +${paperclipClaudeSettings.allow.length} allow rule(s)).`, @@ -2348,6 +2353,14 @@ async function buildRuntime(input: { : resolvedAdapterEnv, ), }; + if (useRemoteProcessSession && executionTarget?.kind === "remote" && executionTarget.transport === "ssh" && config.managedAiConnection && env.HOME) { + const managedHome = path.resolve(env.HOME); + const skills = Object.fromEntries(Object.entries(skillsIdentity).map(([key, value]) => [key, + typeof value === "string" && path.isAbsolute(value) && (value === managedHome || value.startsWith(managedHome + path.sep)) + ? path.join("", path.relative(managedHome, value)) : value, + ])); + fingerprintIdentity = { ...fingerprintIdentity, skillsIdentity: skills }; + } const taskKey = asString(input.ctx.runtime.taskKey, "") || wakeTaskId || workspaceId || "default"; const sshWorkspaceRunId = useRemoteProcessSession && executionTarget?.kind === "remote" && executionTarget.transport === "ssh" ? shortHash({ companyId: agent.companyId, agentId: agent.id, taskKey, fingerprintIdentity }) @@ -2433,7 +2446,7 @@ async function buildRuntime(input: { now: nowMs, idleMs: input.stagedIdleMs, sessionCwd, - spawnCwd: cwd, + spawnCwd: hostSpawnCwd, target: remoteTarget, env, managedHomeRunBinding: { env: { ...env }, onLog: input.ctx.onLog }, @@ -2742,11 +2755,7 @@ async function buildRuntime(input: { // → the HOST cwd (`sessionCwd` resolves both). Every cwd-keyed session site // reads `prepared.cwd`, so binding it once here keeps them consistent. cwd: sessionCwd, - // Only the remote process-session lane needs the host proxy's `spawn()` - // `chdir` redirected off the in-sandbox `sessionCwd` and onto the host - // `cwd` (which is where the workspace was staged FROM, so it is host-valid). - // Every other lane leaves it `undefined` → acpx falls back to `cwd`. - hostSpawnCwd: useRemoteProcessSession ? cwd : undefined, + hostSpawnCwd: useRemoteProcessSession ? hostSpawnCwd : undefined, workspaceId, workspaceRepoUrl, workspaceRepoRef, @@ -6005,7 +6014,15 @@ export function createAcpxEngineExecutor(deps: AcpxEngineExecutorOptions = {}) { }; }, }; - return await runAttempt(plan); + const result = await runAttempt(plan); + const previousSession = parseObject(ctx.runtime.sessionParams); + if (asString(previousSession.acpSessionId, "") && result.sessionParams?.configFingerprint + && previousSession.configFingerprint !== result.sessionParams.configFingerprint) { + return { ...result, resultJson: { ...result.resultJson, + sessionReset: { code: "acpx_session_identity_changed", reason: "The agent configuration or execution identity changed." }, + } }; + } + return result; } finally { // Record the in-memory evidence synchronously, but never let diagnostic // persistence hold the staging lease or delay the completed run's return. diff --git a/packages/adapter-utils/src/execution-target.test.ts b/packages/adapter-utils/src/execution-target.test.ts index 1449d11d904..6be93bdbd3a 100644 --- a/packages/adapter-utils/src/execution-target.test.ts +++ b/packages/adapter-utils/src/execution-target.test.ts @@ -1,5 +1,7 @@ +import os from "node:os"; +import path from "node:path"; import { randomUUID } from "node:crypto"; -import { access, readFile } from "node:fs/promises"; +import { access, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { afterEach, describe, expect, it, vi } from "vitest"; import * as ssh from "./ssh.js"; import * as serverUtils from "./server-utils.js"; @@ -11,6 +13,7 @@ import { normalizeAdapterExecutionTargetSessionIdentity, ensureAdapterExecutionTargetRuntimeCommandInstalled, ensureAdapterExecutionTargetDirectory, + ensureAdapterExecutionTargetLocalWorkspace, ensureAdapterExecutionTargetCommandResolvable, isAdapterExecutionTargetUnavailableError, resolveAdapterExecutionTargetCwd, @@ -485,3 +488,21 @@ describe("GitHub launcher lifecycle", () => { expect(runner.execute).toHaveBeenCalledTimes(1); }); }); + + +describe("controller workspace preparation", () => { + it.each(["local", "copy", "in_place"] as const)("keeps %s workspace access on its owning filesystem", async mode => { + const root = await mkdtemp(path.join(os.tmpdir(), "paperclip-remote-path-")); + try { + const mount = path.join(root, "remote-filesystem"); + await writeFile(mount, "Not a controller directory"); + const cwd = path.join(mount, "workspace"); + const target = mode === "local" ? null : { ...sshExecutionTarget(), remoteCwd: cwd, + workspaceRealization: { mode, authoritativeRoot: cwd, pathAliases: [], outboundRestorePaths: [] } }; + const prepare = ensureAdapterExecutionTargetLocalWorkspace(target, cwd); + if (mode === "in_place") await expect(prepare).resolves.toBeUndefined(); + else await expect(prepare).rejects.toMatchObject({ code: "ENOTDIR" }); + expect(await readFile(mount, "utf8")).toBe("Not a controller directory"); + } finally { await rm(root, { recursive: true, force: true }); } + }); +}); diff --git a/packages/adapter-utils/src/execution-target.ts b/packages/adapter-utils/src/execution-target.ts index 70b2f652c00..566d3fe4dd6 100644 --- a/packages/adapter-utils/src/execution-target.ts +++ b/packages/adapter-utils/src/execution-target.ts @@ -77,6 +77,7 @@ import { } from "./duplex-observability.js"; import { createSshCommandManagedRuntimeRunner, parseSshRemoteExecutionSpec, runSshCommand, shellQuote } from "./ssh.js"; import { + ensureAbsoluteDirectory, ensureCommandResolvable, resolveCommandForLogs, runChildProcess, @@ -1315,6 +1316,11 @@ export async function ensureAdapterExecutionTargetFile( ); } +export async function ensureAdapterExecutionTargetLocalWorkspace(target: AdapterExecutionTarget | null | undefined, cwd: string): Promise { + if (target?.kind === "remote" && target.workspaceRealization?.mode === "in_place") return; + await ensureAbsoluteDirectory(cwd, { createIfMissing: true }); +} + /** * Ensure a working directory exists (and is a directory) on the execution target. * @@ -1568,6 +1574,7 @@ export async function prepareAdapterExecutionTargetRuntime(input: { }; } + const syncWorkspace = input.syncWorkspace ?? target.workspaceRealization?.mode !== "in_place"; if (target.transport === "ssh") { const prepared = await prepareRemoteManagedRuntime({ spec: target.spec, @@ -1575,7 +1582,7 @@ export async function prepareAdapterExecutionTargetRuntime(input: { adapterKey: input.adapterKey, workspaceLocalDir: input.workspaceLocalDir, workspaceRemoteDir: input.workspaceRemoteDir, - syncWorkspace: input.syncWorkspace, + syncWorkspace, privateRuntime: input.privateRuntime, retainRuntime: input.retainRuntime, workspaceFileMode: input.workspaceFileMode, @@ -1615,7 +1622,7 @@ export async function prepareAdapterExecutionTargetRuntime(input: { workspaceLocalDir: input.workspaceLocalDir, workspaceRemoteDir: input.workspaceRemoteDir, runtimeRootDir: input.runtimeRootDir, - syncWorkspace: input.syncWorkspace, + syncWorkspace, workspaceInboundMode: input.workspaceInboundMode, workspaceSnapshotIsolation: input.workspaceSnapshotIsolation, workspaceGitIndexRefresh: input.workspaceGitIndexRefresh, 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 c0f58fa873f..767be33ad8c 100644 --- a/packages/adapters/claude-local/src/server/execute.remote.test.ts +++ b/packages/adapters/claude-local/src/server/execute.remote.test.ts @@ -269,12 +269,16 @@ describe("claude remote execution", () => { } }); - it("uses the agent's subscription in a private SSH home without reading the SSH user's Claude login", async () => { + it.each(["copy", "in_place"] as const)("uses the agent's subscription in a private SSH home without reading the SSH user's Claude login (workspace=%s)", async mode => { const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-claude-selected-ssh-")); cleanupDirs.push(rootDir); - const workspaceDir = path.join(rootDir, "workspace"); + const remoteMount = path.join(rootDir, "remote-filesystem"); + const workspaceDir = mode === "in_place" ? path.join(remoteMount, "workspace") : path.join(rootDir, "workspace"); const providerHome = path.join(rootDir, "selected-provider"); - await mkdir(workspaceDir);await mkdir(providerHome); + if (mode === "in_place") await writeFile(remoteMount, "The SSH workspace is not a local directory."); + else await mkdir(workspaceDir); + await mkdir(providerHome); + const remoteCwd = mode === "in_place" ? workspaceDir : "/remote/workspace"; const logs: string[] = []; await execute({ runId: "selected-subscription", agent: { id: "agent-1", companyId: "company-1", name: "Configured agent", adapterType: "claude_local", adapterConfig: {} }, @@ -282,16 +286,26 @@ describe("claude remote execution", () => { config: { engine: "cli", command: "claude", managedAiConnection: { provider: "anthropic", method: "subscription", identity: "selected-account" }, env: { HOME: rootDir, CLAUDE_CONFIG_DIR: providerHome, CLAUDE_CODE_OAUTH_TOKEN: "selected-agent-token", ANTHROPIC_API_KEY: "" } }, context: { paperclipWorkspace: { cwd: workspaceDir, source: "project_primary" } }, - executionTransport: { remoteExecution: { useHostAuth: false, host: "127.0.0.1", port: 2222, username: "fixture", remoteWorkspacePath: "/remote/workspace", remoteCwd: "/remote/workspace", privateKey: null, knownHosts: null, strictHostKeyChecking: false } }, + executionTarget: { kind: "remote", transport: "ssh", remoteCwd, + spec: { useHostAuth: false, host: "127.0.0.1", port: 2222, username: "fixture", remoteWorkspacePath: remoteCwd, remoteCwd, privateKey: null, knownHosts: null, strictHostKeyChecking: false }, + workspaceRealization: { mode, authoritativeRoot: remoteCwd, pathAliases: [], outboundRestorePaths: [] } }, onLog: async (_stream, chunk) => { logs.push(chunk); }, }); expect(syncDirectoryToSsh).toHaveBeenCalledWith(expect.objectContaining({ localDir: providerHome, remoteDir: "/tmp/paperclip-ssh-runtime.claude/config-seed" })); - const providerCall = runChildProcess.mock.calls.find(call => call[2].includes("--print")) as unknown as [string, string, string[], { env: Record }]; - expect(providerCall[3].env).toMatchObject({ CLAUDE_CODE_OAUTH_TOKEN: "selected-agent-token", CLAUDE_CONFIG_DIR: "/tmp/paperclip-ssh-runtime.claude/config", HOME: "/tmp/paperclip-ssh-runtime.claude", ANTHROPIC_API_KEY: "" }); + const providerCall = runChildProcess.mock.calls.find(call => call[2].includes("--print")) as unknown as [string, string, string[], { env: Record; cwd: string }]; + expect(providerCall[3].env).toMatchObject({ CLAUDE_CODE_OAUTH_TOKEN: "selected-agent-token", HOME: "/tmp/paperclip-ssh-runtime.claude", ANTHROPIC_API_KEY: "" }); + if (mode === "copy") expect(providerCall[3].env.CLAUDE_CONFIG_DIR).toBe("/tmp/paperclip-ssh-runtime.claude/config"); + else expect(path.posix.relative(remoteCwd, providerCall[3].env.CLAUDE_CONFIG_DIR!)).toMatch(/^\.paperclip-runtime\/claude\/conversations\/[a-f0-9]+\/[a-f0-9]+$/); const materialize = runSshCommand.mock.calls.find(call => call[1].includes("config-seed")); expect(materialize?.[1]).not.toContain(".claude/\u0024{file}"); expect(runSshCommand).toHaveBeenCalledWith(expect.anything(), "rm -rf -- '/tmp/paperclip-ssh-runtime.claude'", expect.anything()); expect(logs.join("")).not.toContain("selected-agent-token"); + if (mode === "in_place") { + expect(providerCall[3].cwd).toBe(remoteCwd); + expect(prepareWorkspaceForSshExecution).not.toHaveBeenCalled(); + expect(restoreWorkspaceFromSshExecution).not.toHaveBeenCalled(); + expect(await readFile(remoteMount, "utf8")).toBe("The SSH workspace is not a local directory."); + } }); it("removes the selected account's SSH home when Claude setup fails before launch", async () => { diff --git a/packages/adapters/claude-local/src/server/execute.ts b/packages/adapters/claude-local/src/server/execute.ts index 7ecb6c1dccc..67c1194edda 100644 --- a/packages/adapters/claude-local/src/server/execute.ts +++ b/packages/adapters/claude-local/src/server/execute.ts @@ -19,6 +19,7 @@ import { adapterExecutionTargetUsesPaperclipBridge, describeAdapterExecutionTarget, ensureAdapterExecutionTargetCommandResolvable, + ensureAdapterExecutionTargetLocalWorkspace, ensureAdapterExecutionTargetRuntimeCommandInstalled, prepareAdapterExecutionTargetRuntime, adapterExecutionTargetDuplexObservabilityRecorder, @@ -44,7 +45,6 @@ import { readPaperclipIssueWorkModeFromContext, joinPromptSections, buildInvocationEnvForLogs, - ensureAbsoluteDirectory, ensurePathInEnv, isForbiddenConfigEnvKey, isPaperclipRuntimeEnvKey, @@ -222,7 +222,7 @@ async function buildClaudeRuntimeConfig(input: ClaudeExecutionInput): Promise = { ...buildPaperclipEnv(agent, input.agentIdentity) }; diff --git a/packages/adapters/cursor-local/src/server/execute.ts b/packages/adapters/cursor-local/src/server/execute.ts index ad3430bf723..07736f782bf 100644 --- a/packages/adapters/cursor-local/src/server/execute.ts +++ b/packages/adapters/cursor-local/src/server/execute.ts @@ -16,6 +16,7 @@ import { adapterExecutionTargetUsesPaperclipBridge, describeAdapterExecutionTarget, ensureAdapterExecutionTargetCommandResolvable, + ensureAdapterExecutionTargetLocalWorkspace, ensureAdapterExecutionTargetRuntimeCommandInstalled, prepareAdapterExecutionTargetRuntime, adapterExecutionTargetDuplexObservabilityRecorder, @@ -36,7 +37,6 @@ import { buildPaperclipEnv, buildRuntimeToolsEnv, buildInvocationEnvForLogs, - ensureAbsoluteDirectory, ensurePaperclipSkillSymlink, ensurePathInEnv, refreshPaperclipWorkspaceEnvForExecution, @@ -228,7 +228,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise { ).resolves.toMatchObject({ engine: "acp", explicit: true, unavailableReason: expect.stringContaining("Node") }); }); - it("reports unavailable ACP for non-sandbox remote auto runs", async () => { + it("reports unavailable ACP for SSH runs without a managed account", async () => { setNodeVersion("v24.11.0"); await expect( resolveGeminiExecutionEngineForRun({ @@ -335,7 +335,7 @@ describe("gemini_local ACP lane", () => { ).resolves.toMatchObject({ engine: "acp", explicit: false, - unavailableReason: expect.stringContaining("sandbox remote targets only"), + unavailableReason: expect.stringContaining("agent-managed SSH account"), }); }); diff --git a/packages/adapters/gemini-local/src/server/acp.ts b/packages/adapters/gemini-local/src/server/acp.ts index d5a06a1bef2..7712c0088cd 100644 --- a/packages/adapters/gemini-local/src/server/acp.ts +++ b/packages/adapters/gemini-local/src/server/acp.ts @@ -329,7 +329,7 @@ async function commandIsResolvable( token, target, resolveAdapterExecutionTargetCwd(target, asString(input?.config.cwd, ""), process.cwd()), - process.env, + target.transport === "ssh" ? { PATH: process.env.PATH } : {}, ); return true; } catch { @@ -360,11 +360,13 @@ async function geminiAcpUnavailableReason( executionTarget: input.executionTarget, legacyRemoteExecution: input.executionTransport?.remoteExecution, }); - if (target?.kind === "remote" && !sandboxTargetHasProcessSessionBridge(target)) { + const managedSsh = target?.kind === "remote" && target.transport === "ssh" + && target.spec.useHostAuth === false && Boolean(input.config.managedAiConnection); + if (target?.kind === "remote" && !sandboxTargetHasProcessSessionBridge(target) && !managedSsh) { if (target.transport === "sandbox") { return "Gemini ACP requires a bidirectional remote process target; this sandbox exposes only one-shot command execution."; } - return "Gemini ACP supports sandbox remote targets only; this run targets a non-sandbox remote environment."; + return "Gemini ACP requires an agent-managed SSH account; host authentication is supported by the CLI engine."; } if (!nodeVersionMeetsGeminiAcpMinimum()) { return `Node ${process.version} (${process.execPath}) does not satisfy Gemini ACP's Node >=${MIN_ACP_NODE_VERSION} prerequisite.`; diff --git a/packages/adapters/gemini-local/src/server/engine-availability.test.ts b/packages/adapters/gemini-local/src/server/engine-availability.test.ts index 3440f874f44..bc9f89ab1c9 100644 --- a/packages/adapters/gemini-local/src/server/engine-availability.test.ts +++ b/packages/adapters/gemini-local/src/server/engine-availability.test.ts @@ -3,6 +3,12 @@ import { resolveGeminiExecutionEngineForRun } from "./acp.js"; import { execute } from "./execute.js"; import { testEnvironment } from "./test.js"; +const remoteCommandProbe = vi.hoisted(() => vi.fn(async (..._args: unknown[]) => undefined)); +vi.mock("@paperclipai/adapter-utils/execution-target", async importOriginal => ({ + ...await importOriginal(), + ensureAdapterExecutionTargetCommandResolvable: remoteCommandProbe, +})); + const originalVersion = process.version; afterEach(() => Object.defineProperty(process, "version", { value: originalVersion })); @@ -42,3 +48,24 @@ describe("gemini engine availability", () => { expect(result.unavailableReason).toContain("not available"); }); }); + + +describe("managed SSH ACP selection", () => { + afterEach(() => remoteCommandProbe.mockReset().mockResolvedValue(undefined)); + it.each(["account", "host auth", "missing account", "missing command"])("checks the selected %s setup", async reason => { + Object.defineProperty(process, "version", { value: "v24.11.0" }); + const config = { managedAiConnection: reason === "missing account" ? undefined : { identity: "fixture-account" } }; + const target = { kind: "remote" as const, transport: "ssh" as const, remoteCwd: "/remote/workspace", + workspaceRealization: { mode: "in_place" as const, authoritativeRoot: "/remote/workspace", pathAliases: [], outboundRestorePaths: [] }, + spec: { useHostAuth: reason === "host auth", host: "ssh.example.test", port: 22, username: "fixture", + remoteCwd: "/remote/workspace", remoteWorkspacePath: "/remote/workspace", privateKey: null, knownHosts: null, strictHostKeyChecking: true } }; + if (reason === "missing command") remoteCommandProbe.mockRejectedValueOnce(new Error("missing command")); + const result = await resolveGeminiExecutionEngineForRun({ config, executionTarget: target }); + expect(result.engine).toBe("acp"); + if (reason === "account") { + expect(result.unavailableReason).toBeUndefined(); + expect(remoteCommandProbe).toHaveBeenCalledOnce(); + expect(remoteCommandProbe.mock.calls[0]![3]).toEqual({ PATH: process.env.PATH }); + } else expect(result.unavailableReason).toBeTruthy(); + }); +}); diff --git a/packages/adapters/gemini-local/src/server/execute.ts b/packages/adapters/gemini-local/src/server/execute.ts index 3a72696aec5..0200585f185 100644 --- a/packages/adapters/gemini-local/src/server/execute.ts +++ b/packages/adapters/gemini-local/src/server/execute.ts @@ -17,6 +17,7 @@ import { adapterExecutionTargetUsesPaperclipBridge, describeAdapterExecutionTarget, ensureAdapterExecutionTargetCommandResolvable, + ensureAdapterExecutionTargetLocalWorkspace, ensureAdapterExecutionTargetRuntimeCommandInstalled, prepareAdapterExecutionTargetRuntime, adapterExecutionTargetDuplexObservabilityRecorder, @@ -37,7 +38,6 @@ import { buildPaperclipEnv, buildRuntimeToolsEnv, buildInvocationEnvForLogs, - ensureAbsoluteDirectory, ensurePaperclipSkillSymlink, joinPromptSections, ensurePathInEnv, @@ -261,7 +261,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise ({ +vi.mock("@paperclipai/adapter-utils/execution-target", async (importOriginal) => ({ + ...await importOriginal(), adapterExecutionTargetIsRemote: () => mocks.state.isRemote, adapterExecutionTargetRemoteCwd: (_target: unknown, cwd: string) => mocks.state.isRemote ? "/remote/workspace" : cwd, diff --git a/packages/adapters/grok-local/src/server/execute.ts b/packages/adapters/grok-local/src/server/execute.ts index fe750052de9..f6bab16ff10 100644 --- a/packages/adapters/grok-local/src/server/execute.ts +++ b/packages/adapters/grok-local/src/server/execute.ts @@ -3,9 +3,12 @@ import { withWorkspaceRestore } from "@paperclipai/adapter-utils/workspace-resto import { cancellableSandboxStartup } from "@paperclipai/adapter-utils/acpx-engine/startup-cancellation"; import fs from "node:fs/promises"; import path from "node:path"; +import os from "node:os"; +import { prepareGrokRemoteProjectAssets } from "./remote-project-assets.js"; import { fileURLToPath } from "node:url"; import type { AdapterExecutionContext, AdapterExecutionResult } from "@paperclipai/adapter-utils"; import { + type AdapterManagedRuntimeAsset, managedRemoteHomeEnvironment, adapterExecutionTargetIsRemote, adapterExecutionTargetRemoteCwd, @@ -13,6 +16,7 @@ import { adapterExecutionTargetSessionMatches, describeAdapterExecutionTarget, ensureAdapterExecutionTargetCommandResolvable, + ensureAdapterExecutionTargetLocalWorkspace, ensureAdapterExecutionTargetRuntimeCommandInstalled, overrideAdapterExecutionTargetRemoteCwd, prepareAdapterExecutionTargetRuntime, @@ -29,7 +33,6 @@ import { buildInvocationEnvForLogs, buildPaperclipEnv, buildRuntimeToolsEnv, - ensureAbsoluteDirectory, ensurePathInEnv, joinPromptSections, materializePaperclipSkillCopy, @@ -292,18 +295,21 @@ async function executeTurn(ctx: AdapterExecutionContext): Promise { if (projectAssetsDir) await fs.rm(projectAssetsDir, { recursive: true, force: true }); throw error; }); + let remoteProjectAssets: Awaited> | null = null; let restoreRemoteWorkspace: (() => Promise) | null = null; // Declared here (not inside the try) so the outer `finally` can remove it on // every exit path (teardown and setup failure alike), mirroring the Codex @@ -429,7 +435,9 @@ async function executeTurn(ctx: AdapterExecutionContext): Promise onLog("stdout", line), onRuntimeProgress: ctx.onRuntimeProgress, - assets: stagedGrokHomeDir + assets: projectAssetsDir || stagedGrokHomeDir ? [ + ...(projectAssetsDir ? [{ key: "project", localDir: projectAssetsDir, followSymlinks: false }] : []), + ...(stagedGrokHomeDir ? [ { key: "home", @@ -444,7 +452,7 @@ async function executeTurn(ctx: AdapterExecutionContext): Promise + restore: async ({ assetDir, readFile }: Parameters>[0]) => void (await copyBackGrokAuth({ readSandboxAuth: () => readFile(path.posix.join(assetDir, "auth.json")), hostHomeDir: hostGrokHome, @@ -453,10 +461,15 @@ async function executeTurn(ctx: AdapterExecutionContext): Promise undefined)), }, ] - : undefined, + : []), + ] : undefined, }); - restoreRemoteWorkspace = () => - preparedExecutionTargetRuntime.restoreWorkspace((line) => onLog("stdout", line)); + restoreRemoteWorkspace = async () => { + try { await remoteProjectAssets?.cleanup(); } + finally { await preparedExecutionTargetRuntime.restoreWorkspace((line) => onLog("stdout", line)); } + }; + if (projectAssetsDir) remoteProjectAssets = await prepareGrokRemoteProjectAssets({ runId, target: executionTarget!, + cwd: preparedExecutionTargetRuntime.workspaceRemoteDir ?? effectiveExecutionCwd, assets: preparedExecutionTargetRuntime.assetDirs.project! }); effectiveExecutionCwd = preparedExecutionTargetRuntime.workspaceRemoteDir ?? effectiveExecutionCwd; refreshPaperclipWorkspaceEnvForExecution({ env, @@ -564,7 +577,8 @@ async function executeTurn(ctx: AdapterExecutionContext): Promise { const fromExtraArgs = asStringArray(config.extraArgs); if (fromExtraArgs.length > 0) return fromExtraArgs; @@ -775,5 +789,6 @@ async function executeTurn(ctx: AdapterExecutionContext): Promise { + const root = await fs.mkdtemp(path.join(os.tmpdir(), "grok-project-assets-")); + try { + const cwd = path.join(root, "workspace"), assets = path.join(root, "assets"), outside = path.join(root, "outside"); + await fs.mkdir(cwd); await fs.mkdir(outside); + await fs.writeFile(path.join(outside, "keep.txt"), "outside"); + const stagedSkill = path.join(assets, ".claude/skills/selected"); + await fs.mkdir(stagedSkill, { recursive: true }); + await fs.writeFile(path.join(stagedSkill, "SKILL.md"), "selected skill"); + await fs.writeFile(path.join(assets, "Agents.md"), "agent instructions"); + if (scenario === "existing") { + await fs.mkdir(path.join(cwd, ".claude/skills/own"), { recursive: true }); + await fs.writeFile(path.join(cwd, ".claude/skills/own/keep.txt"), "user skill"); + } + if (scenario === "unsafe") await fs.symlink(outside, path.join(cwd, ".claude")); + const input = { runId: "fixture", target: { kind: "local" as const }, cwd, assets }; + if (scenario === "unsafe") { + await expect(prepareGrokRemoteProjectAssets(input)).rejects.toThrow("Could not prepare Grok SSH runtime assets"); + expect(await fs.readdir(outside)).toEqual(["keep.txt"]); + return; + } + const prepared = await prepareGrokRemoteProjectAssets(input); + expect(prepared.rulesFilePath).toBe(path.join(assets, "Agents.md")); + expect(await fs.readFile(path.join(cwd, ".claude/skills/selected/SKILL.md"), "utf8")).toBe("selected skill"); + if (scenario === "changed") { + await fs.rename(path.join(cwd, ".claude"), path.join(cwd, ".claude-original")); + await fs.symlink(outside, path.join(cwd, ".claude")); + await expect(prepared.cleanup()).rejects.toThrow("Could not prepare Grok SSH runtime assets"); + expect(await fs.readdir(outside)).toEqual(["keep.txt"]); + } else { + await fs.writeFile(path.join(cwd, ".claude/user.txt"), "user work"); + await prepared.cleanup(); + expect(await fs.readFile(path.join(cwd, ".claude/user.txt"), "utf8")).toBe("user work"); + await expect(fs.stat(path.join(cwd, ".claude/skills/selected"))).rejects.toMatchObject({ code: "ENOENT" }); + if (scenario === "existing") expect(await fs.readFile(path.join(cwd, ".claude/skills/own/keep.txt"), "utf8")).toBe("user skill"); + } + } finally { await fs.rm(root, { recursive: true, force: true }); } +}); diff --git a/packages/adapters/grok-local/src/server/remote-project-assets.ts b/packages/adapters/grok-local/src/server/remote-project-assets.ts new file mode 100644 index 00000000000..8e20f17c280 --- /dev/null +++ b/packages/adapters/grok-local/src/server/remote-project-assets.ts @@ -0,0 +1,61 @@ +import type { AdapterExecutionTarget } from "@paperclipai/adapter-utils/execution-target"; +import { runAdapterExecutionTargetShellCommand } from "@paperclipai/adapter-utils/execution-target"; +import { shellQuote } from "@paperclipai/adapter-utils/ssh"; + +const program = ` +const fs = require('fs'), path = require('path'); +const [operation, assets, cwd, encoded] = process.argv.slice(1); +const created = []; +const cleanup = entries => { for (const entry of [...entries].reverse()) { + const file = path.resolve(cwd, entry.path); + if (!file.startsWith(path.resolve(cwd) + path.sep) || !entry.path.startsWith('.claude')) throw Error('Invalid runtime asset path'); + try { + for (const relative of ['.claude', '.claude/skills']) { + const parent = path.join(cwd, relative); + if (file.startsWith(parent + path.sep) && fs.lstatSync(parent).isSymbolicLink()) throw Error('Unsafe runtime asset parent'); + } + if (entry.kind === 'skill') fs.rmSync(file, {recursive:true, force:true}); else fs.rmdirSync(file); + } + catch (error) { if (!['ENOENT', 'ENOTEMPTY'].includes(error.code)) throw error; } +} }; +if (operation === 'cleanup') { cleanup(JSON.parse(encoded)); process.exit(0); } +try { + const source = path.join(assets, '.claude', 'skills'); + if (fs.existsSync(source)) { + for (const relative of ['.claude', '.claude/skills']) { + const target = path.join(cwd, relative); + if (fs.existsSync(target)) { const st = fs.lstatSync(target); if (st.isSymbolicLink() || !st.isDirectory()) throw Error('Unsafe Grok skills directory'); } + else { fs.mkdirSync(target); created.push({path:relative,kind:'directory'}); } + } + for (const name of fs.readdirSync(source)) { + if (path.basename(name) !== name) throw Error('Invalid skill name'); + const relative = path.join('.claude', 'skills', name), target = path.join(cwd, relative); + if (fs.existsSync(target)) continue; + fs.mkdirSync(target); + created.push({path:relative,kind:'skill'}); + fs.cpSync(path.join(source,name),target,{recursive:true,force:false,errorOnExist:true}); + } + } + const rules = fs.existsSync(path.join(assets,'Agents.md')) ? path.join(assets,'Agents.md') + : !fs.existsSync(path.join(cwd,'Agents.md')) && fs.existsSync(path.join(cwd,'AGENTS.md')) ? path.join(cwd,'AGENTS.md') : null; + console.log(JSON.stringify({created,rules})); +} catch (error) { cleanup(created); throw error; } +`; + +export async function prepareGrokRemoteProjectAssets(input: { + runId: string; target: AdapterExecutionTarget; cwd: string; assets: string; +}): Promise<{ rulesFilePath: string | null; cleanup: () => Promise }> { + const execute = async (operation: string, entries: unknown[]) => { + const result = await runAdapterExecutionTargetShellCommand(input.runId, input.target, + ["node", "-e", program, operation, input.assets, input.cwd, JSON.stringify(entries)].map(shellQuote).join(" "), + { cwd: input.cwd, env: {}, timeoutSec: 15 }); + if (result.exitCode !== 0 || result.timedOut || result.signal) throw new Error("Could not prepare Grok SSH runtime assets"); + return result.stdout; + }; + const result = JSON.parse(await execute("stage", [])) as { created: Array<{ path: string; kind: string }>; rules: string | null }; + if (!Array.isArray(result.created) || result.created.some(entry => !entry || typeof entry !== "object" || typeof entry.path !== "string" || (entry.kind === "directory" ? ![".claude", ".claude/skills"].includes(entry.path) + : entry.kind !== "skill" || !/^\.claude\/skills\/[A-Za-z0-9][A-Za-z0-9._-]*$/.test(entry.path)))) throw new Error("Invalid Grok SSH runtime asset receipt"); + const allowedRules = [input.assets + "/Agents.md", input.cwd + "/AGENTS.md"]; + if (result.rules !== null && !allowedRules.includes(result.rules)) throw new Error("Invalid Grok SSH instructions path"); + return { rulesFilePath: result.rules, cleanup: async () => { await execute("cleanup", result.created); } }; +} diff --git a/packages/adapters/kimi-local/src/server/acp.ts b/packages/adapters/kimi-local/src/server/acp.ts index d1bbaa41d41..52c8ba4b30d 100644 --- a/packages/adapters/kimi-local/src/server/acp.ts +++ b/packages/adapters/kimi-local/src/server/acp.ts @@ -219,7 +219,7 @@ async function commandIsResolvable( token, target, resolveAdapterExecutionTargetCwd(target, asString(input?.config.cwd, ""), process.cwd()), - process.env, + target.transport === "ssh" ? { PATH: process.env.PATH } : {}, ); return true; } catch { @@ -250,11 +250,13 @@ async function kimiAcpUnavailableReason( executionTarget: input.executionTarget, legacyRemoteExecution: input.executionTransport?.remoteExecution, }); - if (target?.kind === "remote" && !sandboxTargetHasProcessSessionBridge(target)) { + const managedSsh = target?.kind === "remote" && target.transport === "ssh" + && target.spec.useHostAuth === false && Boolean(input.config.managedAiConnection); + if (target?.kind === "remote" && !sandboxTargetHasProcessSessionBridge(target) && !managedSsh) { if (target.transport === "sandbox") { return "Kimi ACP requires a bidirectional remote process target; this sandbox exposes only one-shot command execution."; } - return "Kimi ACP supports sandbox remote targets only; this run targets a non-sandbox remote environment."; + return "Kimi ACP requires an agent-managed SSH account; host authentication is supported by the CLI engine."; } if (!nodeVersionMeetsKimiAcpMinimum()) { return `Node ${process.version} (${process.execPath}) does not satisfy Kimi ACP's Node >=${MIN_ACP_NODE_VERSION} prerequisite.`; diff --git a/packages/adapters/kimi-local/src/server/engine-availability.test.ts b/packages/adapters/kimi-local/src/server/engine-availability.test.ts index 24685ed5c7d..e9fbbe645d8 100644 --- a/packages/adapters/kimi-local/src/server/engine-availability.test.ts +++ b/packages/adapters/kimi-local/src/server/engine-availability.test.ts @@ -3,6 +3,12 @@ import { resolveKimiExecutionEngineForRun } from "./acp.js"; import { execute } from "./execute.js"; import { testEnvironment } from "./test.js"; +const remoteCommandProbe = vi.hoisted(() => vi.fn(async (..._args: unknown[]) => undefined)); +vi.mock("@paperclipai/adapter-utils/execution-target", async importOriginal => ({ + ...await importOriginal(), + ensureAdapterExecutionTargetCommandResolvable: remoteCommandProbe, +})); + const originalVersion = process.version; afterEach(() => Object.defineProperty(process, "version", { value: originalVersion })); @@ -42,3 +48,24 @@ describe("kimi engine availability", () => { expect(result.unavailableReason).toContain("not available"); }); }); + + +describe("managed SSH ACP selection", () => { + afterEach(() => remoteCommandProbe.mockReset().mockResolvedValue(undefined)); + it.each(["account", "host auth", "missing account", "missing command"])("checks the selected %s setup", async reason => { + Object.defineProperty(process, "version", { value: "v24.11.0" }); + const config = { managedAiConnection: reason === "missing account" ? undefined : { identity: "fixture-account" } }; + const target = { kind: "remote" as const, transport: "ssh" as const, remoteCwd: "/remote/workspace", + workspaceRealization: { mode: "in_place" as const, authoritativeRoot: "/remote/workspace", pathAliases: [], outboundRestorePaths: [] }, + spec: { useHostAuth: reason === "host auth", host: "ssh.example.test", port: 22, username: "fixture", + remoteCwd: "/remote/workspace", remoteWorkspacePath: "/remote/workspace", privateKey: null, knownHosts: null, strictHostKeyChecking: true } }; + if (reason === "missing command") remoteCommandProbe.mockRejectedValueOnce(new Error("missing command")); + const result = await resolveKimiExecutionEngineForRun({ config, executionTarget: target }); + expect(result.engine).toBe("acp"); + if (reason === "account") { + expect(result.unavailableReason).toBeUndefined(); + expect(remoteCommandProbe).toHaveBeenCalledOnce(); + expect(remoteCommandProbe.mock.calls[0]![3]).toEqual({ PATH: process.env.PATH }); + } else expect(result.unavailableReason).toBeTruthy(); + }); +}); diff --git a/packages/adapters/kimi-local/src/server/execute.test.ts b/packages/adapters/kimi-local/src/server/execute.test.ts index 93ea4dc2b15..ec96fb00e49 100644 --- a/packages/adapters/kimi-local/src/server/execute.test.ts +++ b/packages/adapters/kimi-local/src/server/execute.test.ts @@ -14,7 +14,8 @@ const prepareRuntimeMock = vi.hoisted(() => vi.fn(async () => ({ const resolveCommandForLogsMock = vi.hoisted(() => vi.fn(async () => "kimi")); const runProcessMock = vi.hoisted(() => vi.fn()); -vi.mock("@paperclipai/adapter-utils/execution-target", () => ({ +vi.mock("@paperclipai/adapter-utils/execution-target", async (importOriginal) => ({ + ...await importOriginal(), adapterExecutionTargetIsRemote: () => false, adapterExecutionTargetRemoteCwd: (_target: unknown, cwd: string) => cwd, overrideAdapterExecutionTargetRemoteCwd: (target: unknown, _cwd: string) => target, diff --git a/packages/adapters/kimi-local/src/server/execute.ts b/packages/adapters/kimi-local/src/server/execute.ts index 207a2f05ef1..3cee96c084f 100644 --- a/packages/adapters/kimi-local/src/server/execute.ts +++ b/packages/adapters/kimi-local/src/server/execute.ts @@ -15,6 +15,7 @@ import { adapterExecutionTargetUsesPaperclipBridge, describeAdapterExecutionTarget, ensureAdapterExecutionTargetCommandResolvable, + ensureAdapterExecutionTargetLocalWorkspace, ensureAdapterExecutionTargetRuntimeCommandInstalled, prepareAdapterExecutionTargetRuntime, readAdapterExecutionTarget, @@ -30,7 +31,6 @@ import { buildPaperclipEnv, buildRuntimeToolsEnv, buildInvocationEnvForLogs, - ensureAbsoluteDirectory, joinPromptSections, ensurePathInEnv, refreshPaperclipWorkspaceEnvForExecution, @@ -239,7 +239,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise { expect(call?.[2]).toContain("deepseek/deepseek-chat-v3.1"); }); - it("resumes saved Pi sessions for remote SSH execution only when the identity matches", async () => { + it.each([false, true])("resumes saved Pi sessions when the SSH identity matches (inPlace=%s)", async inPlace => { const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-pi-remote-resume-")); cleanupDirs.push(rootDir); const workspaceDir = path.join(rootDir, "workspace"); - const managedRemoteWorkspace = "/remote/workspace/.paperclip-runtime/runs/run-ssh-resume/workspace"; + const managedRemoteWorkspace = inPlace ? workspaceDir : "/remote/workspace/.paperclip-runtime/runs/run-ssh-resume/workspace"; await mkdir(workspaceDir, { recursive: true }); runSshCommand.mockImplementation(async (...args: unknown[]) => { @@ -329,6 +329,12 @@ describe("pi remote execution", () => { await execute({ runId: "run-ssh-resume", + executionTarget: inPlace ? { + kind: "remote", transport: "ssh", remoteCwd: managedRemoteWorkspace, + workspaceRealization: { mode: "in_place", authoritativeRoot: managedRemoteWorkspace, pathAliases: [], outboundRestorePaths: [] }, + spec: { host: "127.0.0.1", port: 2222, username: "fixture", remoteWorkspacePath: "/remote/workspace", + remoteCwd: managedRemoteWorkspace, privateKey: "PRIVATE KEY", knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA", strictHostKeyChecking: true }, + } : undefined, agent: { id: "agent-1", companyId: "company-1", diff --git a/packages/adapters/pi-local/src/server/execute.ts b/packages/adapters/pi-local/src/server/execute.ts index 07fd74c4556..39be70e0bcf 100644 --- a/packages/adapters/pi-local/src/server/execute.ts +++ b/packages/adapters/pi-local/src/server/execute.ts @@ -16,6 +16,7 @@ import { adapterExecutionTargetUsesPaperclipBridge, describeAdapterExecutionTarget, ensureAdapterExecutionTargetCommandResolvable, + ensureAdapterExecutionTargetLocalWorkspace, ensureAdapterExecutionTargetFile, ensureAdapterExecutionTargetRuntimeCommandInstalled, prepareAdapterExecutionTargetRuntime, @@ -37,7 +38,6 @@ import { buildRuntimeToolsEnv, joinPromptSections, buildInvocationEnvForLogs, - ensureAbsoluteDirectory, ensurePaperclipSkillSymlink, ensurePathInEnv, refreshPaperclipWorkspaceEnvForExecution, @@ -159,9 +159,9 @@ function buildSessionPath(agentId: string, timestamp: string): string { return path.join(PAPERCLIP_SESSIONS_DIR, `${safeTimestamp}-${agentId}.jsonl`); } -function buildRemoteSessionPath(runtimeRootDir: string, agentId: string, timestamp: string): string { +function buildRemoteSessionPath(sessionRootDir: string, agentId: string, timestamp: string): string { const safeTimestamp = timestamp.replace(/[:.]/g, "-"); - return path.posix.join(runtimeRootDir, "sessions", `${safeTimestamp}-${agentId}.jsonl`); + return path.posix.join(sessionRootDir, "sessions", `${safeTimestamp}-${agentId}.jsonl`); } function normalizeExecutionCwd(candidate: string, remote: boolean): string { @@ -268,7 +268,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise assertEphemeralNativeRuntime(false)).not.toThrow(); }); -it.each(["claude_local", "codex_local"])("admits %s over SSH only with durable remote workspace authority", adapterType => { +it.each(["claude_local", "codex_local", "cursor", "gemini_local", "grok_local", "kimi_local", "opencode_local", "pi_local"])("admits %s over SSH only with durable remote workspace authority", adapterType => { vi.stubEnv("PAPERCLIP_EPHEMERAL_SERVER", "true"); vi.stubEnv("DATABASE_URL", config.databaseUrl); vi.stubEnv("PAPERCLIP_STORAGE_PROVIDER", "s3"); diff --git a/server/src/__tests__/ssh-adapter-session-codecs.test.ts b/server/src/__tests__/ssh-adapter-session-codecs.test.ts new file mode 100644 index 00000000000..11d2a4d9968 --- /dev/null +++ b/server/src/__tests__/ssh-adapter-session-codecs.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; +import { adapterExecutionTargetSessionIdentity, adapterExecutionTargetSessionMatches, type AdapterSshExecutionTarget } from "@paperclipai/adapter-utils/execution-target"; +import { getServerAdapter } from "../adapters/index.js"; + +describe("SSH adapter saved session identity", () => { + it.each(["claude_local", "codex_local", "cursor", "gemini_local", "grok_local", "kimi_local", "opencode_local", "pi_local"])("keeps %s connection guards through persistence", adapterType => { + const target: AdapterSshExecutionTarget = { kind: "remote", transport: "ssh", remoteCwd: "/remote/task", + spec: { host: "ssh.example.test", port: 22, username: "fixture", remoteCwd: "/remote/task", remoteWorkspacePath: "/remote", + useHostAuth: false, privateKey: null, knownHosts: null, strictHostKeyChecking: true } }; + const identity = adapterExecutionTargetSessionIdentity(target)!; + const codec = getServerAdapter(adapterType).sessionCodec!; + const stored = codec.serialize({ sessionId: "session-fixture", cwd: target.remoteCwd, + remoteExecution: { ...identity, privateKey: "credential-canary", tailscaleSocket: "/private/socket" } }); + const restored = codec.deserialize(JSON.parse(JSON.stringify(stored)))!; + expect(restored.remoteExecution).toEqual(identity); + expect(adapterExecutionTargetSessionMatches(restored.remoteExecution, target)).toBe(true); + expect(adapterExecutionTargetSessionMatches(restored.remoteExecution, { ...target, spec: { ...target.spec, host: "other.example.test" } })).toBe(false); + const legacy = codec.deserialize({ sessionId: "session-fixture", cwd: target.remoteCwd })!; + expect(legacy.remoteExecution).toBeUndefined(); + expect(adapterExecutionTargetSessionMatches(legacy.remoteExecution, target)).toBe(false); + }); +}); diff --git a/server/src/services/heartbeat/runtime-selection.test.ts b/server/src/services/heartbeat/runtime-selection.test.ts index e5a7e4ad989..dcb96533ada 100644 --- a/server/src/services/heartbeat/runtime-selection.test.ts +++ b/server/src/services/heartbeat/runtime-selection.test.ts @@ -942,7 +942,7 @@ describe.skipIf(!support.supported)("heartbeat runtime selection boundary", () = expect(prepareNativeWorkspaceSync).not.toHaveBeenCalled(); }); - it.each(["claude_local", "codex_local"])("keeps %s SSH runtime and its durable workspace receipt on a volume-free server", async adapterType => { + it.each(["claude_local", "codex_local", "cursor", "gemini_local", "grok_local", "kimi_local", "opencode_local", "pi_local"])("keeps %s SSH runtime and its durable workspace receipt on a volume-free server", async adapterType => { const { input, run, agent, issue } = await fixture(adapterType); vi.stubEnv("PAPERCLIP_EPHEMERAL_SERVER", "true"); const environmentId = randomUUID(), leaseId = randomUUID(), workspaceId = randomUUID(); diff --git a/server/src/services/heartbeat/workspace-preparation.test.ts b/server/src/services/heartbeat/workspace-preparation.test.ts index 220a8d06c89..f3966a264a6 100644 --- a/server/src/services/heartbeat/workspace-preparation.test.ts +++ b/server/src/services/heartbeat/workspace-preparation.test.ts @@ -590,7 +590,7 @@ describe.skipIf(!support.supported && !externalDatabaseUrl)("heartbeat workspace expect(await db.select().from(executionWorkspaces)).toHaveLength(0); }); - it.each(["claude_local", "codex_local"])("reserves remote storage for volume-free %s SSH tasks before acquiring the machine", async adapterType => { + it.each(["claude_local", "codex_local", "cursor", "gemini_local", "grok_local", "kimi_local", "opencode_local", "pi_local"])("reserves remote storage for volume-free %s SSH tasks before acquiring the machine", async adapterType => { const { input, issue, envOrchestrator } = await fixture(); configureRemotePersistence(); process.env.PAPERCLIP_EPHEMERAL_SERVER = "true"; diff --git a/server/src/services/remote-workspaces.ts b/server/src/services/remote-workspaces.ts index 23e9f3c6bbc..0c39bcc599f 100644 --- a/server/src/services/remote-workspaces.ts +++ b/server/src/services/remote-workspaces.ts @@ -14,7 +14,7 @@ import { companies, environmentLeases, executionWorkspaces, executionWorkspaceSnapshots, heartbeatRuns, issues, workspaceOperations, type Db, } from "@paperclipai/db"; import { - readRemoteWorkspaceAuthority, remoteWorkspaceSourceSchema, + adapterSupportsRemoteManagedEnvironments, readRemoteWorkspaceAuthority, remoteWorkspaceSourceSchema, type RemoteWorkspaceAuthority, type RemoteWorkspaceRunReference, type RemoteWorkspaceSource, type EnvironmentLease, } from "@paperclipai/shared"; @@ -95,7 +95,7 @@ function sameExportLocation(value: unknown, location: NonNullable { + const harnesses = [ + { adapterType: "claude_local", provider: "anthropic", authEnv: "ANTHROPIC_API_KEY", model: "claude-sonnet-4-6", engines: ["cli", "acp"] }, + { adapterType: "codex_local", provider: "openai", authEnv: "OPENAI_API_KEY", model: "gpt-5.4-mini", engines: ["cli", "acp"] }, + { adapterType: "gemini_local", provider: "google", authEnv: "GEMINI_API_KEY", model: "gemini-2.5-pro", engines: ["cli", "acp"] }, + { adapterType: "grok_local", provider: "xai", authEnv: "XAI_API_KEY", model: "grok-4", engines: ["cli"] }, + { adapterType: "opencode_local", provider: "openrouter", authEnv: "OPENROUTER_API_KEY", model: "openrouter/openai/gpt-4.1", engines: ["cli"] }, + { adapterType: "pi_local", provider: "openai", authEnv: "OPENAI_API_KEY", model: "openai/gpt-5.4-mini", engines: ["cli"] }, + { adapterType: "cursor", provider: null, authEnv: "PAPERCLIP_SSH_FIXTURE_AUTH", model: "auto", engines: ["cli"] }, + { adapterType: "kimi_local", provider: null, authEnv: "PAPERCLIP_SSH_FIXTURE_AUTH", model: "kimi-k2.5", engines: ["cli"] }, + ]; + const runs = harnesses.flatMap(harness => harness.engines.map(engine => ({ ...harness, engine, key: `${harness.adapterType}-${engine}` }))); + it.each(runs)("runs an assigned $key task through heartbeat over SSH", async ({ adapterType, provider, authEnv, model, engine, key }) => { 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-${engine}-objects`))), provider: "s3" as const }; + const storage = { ...createStorageService(createLocalDiskStorageProvider(path.join(root, `heartbeat-${key}-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: engine === "cli" ? "HCLI" : "HACP", defaultResponsibleUserId: userId }); + await db.insert(companies).values({ id: companyId, name: "Cloud SSH task", issuePrefix: `SSH${runs.findIndex(run => run.key === key)}`, 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 account = provider ? await aiConnectionService(db).save(companyId, userId, { provider: provider as AiProvider, method: "api_key", + ownership: "personal", name: "Selected test account", apiKey: "synthetic-selected-ssh-key", agentIds: [], allAgents: true }, "synthetic-selected-ssh-key") : null; const config = await buildSshEnvLabFixtureConfig(sshd!); - const command = path.join(root, `fake-claude-${engine}`); + const command = path.join(root, `fake-${key}`); 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); +if (process.argv.includes('models')) { console.log(${JSON.stringify(model)}); process.exit(0); } +if (process.env[${JSON.stringify(authEnv)}] !== '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 harness = ${JSON.stringify(adapterType)}; +const piSession = harness === 'pi_local' ? process.argv[process.argv.indexOf('--session') + 1] : null; +const history = harness === 'claude_local' ? path.join(process.env.CLAUDE_CONFIG_DIR, 'projects', 'fixture-history') + : piSession || path.join(process.cwd(), '.fixture-provider-history'); 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.mkdirSync(path.dirname(history), {recursive:true}); +if (piSession) { + const previous = resumed ? fs.readFileSync(piSession,'utf8') : ''; + if (resumed && JSON.parse(previous.split('\\n')[0]).cwd !== process.cwd()) throw Error('Invalid Pi session header'); + if (resumed && !previous.includes('first-turn-history')) throw Error('Missing Pi conversation messages'); + if (!resumed) fs.writeFileSync(piSession,JSON.stringify({type:'session',version:3,id:'fixture',cwd:process.cwd()})+'\\n'); + fs.appendFileSync(piSession,JSON.stringify({type:'message',message:{role:'assistant',content:resumed?'continued-history':'first-turn-history'}})+'\\n'); + fs.writeFileSync('pi-session-path.json',JSON.stringify({session:piSession,home:process.env.HOME})); +} else fs.writeFileSync(history, 'provider conversation state'); fs.writeFileSync('continuation.json', JSON.stringify({resumed})); require('fs').writeFileSync('hello_world.txt', 'Hello World\\n'); }; @@ -246,26 +270,63 @@ if (process.argv.includes('--acp-fixture')) { } 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}})); + const id = '12345678-1234-4abc-9def-123456789012'; + const resumed = ['--resume', 'resume', '-r'].some(arg => process.argv.includes(arg)) + || (harness === 'opencode_local' && process.argv.includes('--session')) + || Boolean(piSession && fs.existsSync(piSession) && fs.statSync(piSession).size > 0); + runTask(resumed); + const emit = value => console.log(JSON.stringify(value)); + if (harness === 'codex_local') { + emit({type:'thread.started',thread_id:id}); emit({type:'item.completed',item:{type:'agent_message',text:'Created hello_world.txt'}}); + emit({type:'turn.completed',usage:{input_tokens:1,output_tokens:1}}); + } else if (harness === 'gemini_local') { + emit({type:'system',subtype:'init',session_id:id}); emit({type:'message',role:'assistant',content:'Created hello_world.txt'}); + emit({type:'result',status:'success',session_id:id,stats:{input_tokens:1,output_tokens:1}}); + } else if (harness === 'kimi_local') { + emit({role:'assistant',content:'Created hello_world.txt'}); emit({role:'meta',type:'session.resume_hint',session_id:id}); + } else if (harness === 'opencode_local') { + emit({type:'text',sessionID:id,part:{text:'Created hello_world.txt'}}); + emit({type:'step_finish',sessionID:id,part:{cost:0,tokens:{input:1,output:1,cache:{read:0,write:0}}}}); + } else if (harness === 'pi_local') { + emit({type:'turn_end',message:{role:'assistant',content:'Created hello_world.txt',usage:{input:1,output:1}},toolResults:[]}); + } else if (harness === 'grok_local') { + emit({type:'message',data:'Created hello_world.txt'}); emit({type:'end',sessionId:id,stopReason:'EndTurn',usage:{input_tokens:1,output_tokens:1}}); + } else emit({type:'result',subtype:'success',is_error:false,result:'Created hello_world.txt',session_id:id,total_cost_usd:0,usage:{input_tokens:1,output_tokens:1}}); } `, { mode: 0o700 }); - 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 ${engine}`, config: storedConfig }).returning(); + const storedConfig = await normalizeEnvironmentConfigForPersistence({ db, companyId, environmentName: `Heartbeat SSH ${key}`, + driver: "ssh", secretProvider: "local_encrypted", config: { ...config, useHostAuth: !provider } }); + const [environment] = await db.insert(environments).values({ driver: "ssh", name: `Heartbeat SSH ${key}`, 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", + model, timeoutSec: 20, ...(!provider ? { env: { [authEnv]: "synthetic-selected-ssh-key" } } : {}) }; + await db.insert(agents).values({ id: agentId, companyId, name: `${adapterType} SSH task`, adapterType, status: "idle", defaultEnvironmentId: environment.id, adapterConfig, - runtimeConfig: { aiConnection: { provider: "anthropic", method: "api_key", mode: "delegated", connectionId: account.connectionId, grantId: account.grantId } } }); + runtimeConfig: account ? { aiConnection: { provider, 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"); + const adapter = getServerAdapter(adapterType), execute = adapter.execute; + expect(adapter.type).toBe(adapterType); const executing = vi.spyOn(adapter, "execute").mockImplementation(async input => { - const result = await execute(input); + const target = input.executionTarget; + expect(target).toMatchObject({ kind: "remote", transport: "ssh", workspaceRealization: { mode: "in_place" } }); + const remoteCwd = target!.kind === "remote" ? target!.remoteCwd : ""; + const localAccesses: string[] = []; + const guards = (["stat", "mkdir"] as const).map(method => { + const original = fs[method].bind(fs); + return vi.spyOn(fs, method).mockImplementation(((candidate: unknown, ...args: unknown[]) => { + if (typeof candidate === "string" && (candidate === remoteCwd || candidate.startsWith(remoteCwd + path.sep))) { + localAccesses.push(method); + return Promise.reject(Object.assign(new Error("SSH workspace is unavailable on the controller"), { code: "EACCES" })); + } + return (original as (...args: unknown[]) => unknown)(candidate, ...args); + }) as never); + }); + let result; + try { result = await execute(input); } + finally { for (const guard of guards) guard.mockRestore(); } + expect(localAccesses).toEqual([]); // 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; @@ -283,6 +344,11 @@ if (process.argv.includes('--acp-fixture')) { const authority = finished!.runnerProfileJson!.remoteWorkspace as { workspaceId: string; state: string; remoteCwd: string }; expect(authority.state).toBe("saved"); expect(await readFile(path.join(authority.remoteCwd, "hello_world.txt"), "utf8")).toBe("Hello World\n"); + const piState = adapterType === "pi_local" ? JSON.parse(await readFile(path.join(authority.remoteCwd, "pi-session-path.json"), "utf8")) : null; + if (piState) { + expect(await readFile(piState.session, "utf8")).toContain("first-turn-history"); + await expect(fs.stat(piState.home)).rejects.toMatchObject({ code: "ENOENT" }); + } const [workspace] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, authority.workspaceId)); expect(readRemoteWorkspaceAuthority(workspace.metadata)).toMatchObject({ writerRunId: null, dirty: false }); const comment = await issueService(db).addComment(issueId, "Confirm the file still exists.", { userId }); @@ -297,6 +363,13 @@ if (process.argv.includes('--acp-fixture')) { expect(continued).toMatchObject({ status: "succeeded" }); const continuation = JSON.parse(await readFile(path.join(authority.remoteCwd, "continuation.json"), "utf8")); expect(continuation).toEqual({ resumed: true }); + if (piState) { + const next = JSON.parse(await readFile(path.join(authority.remoteCwd, "pi-session-path.json"), "utf8")); + expect(next.session).toBe(piState.session); + expect(next.home).not.toBe(piState.home); + expect(await readFile(next.session, "utf8")).toContain("continued-history"); + await expect(fs.stat(next.home)).rejects.toMatchObject({ code: "ENOENT" }); + } } finally { await heartbeat.drainActiveRunExecutions(); executing.mockRestore();