diff --git a/doc/connections/AI-CONNECTIONS.md b/doc/connections/AI-CONNECTIONS.md index 34baa5f3fce..f66b56e710a 100644 --- a/doc/connections/AI-CONNECTIONS.md +++ b/doc/connections/AI-CONNECTIONS.md @@ -463,8 +463,8 @@ reader reports confirmed file absence as `ENOENT`, which needs no copyback. Connection and file-read failures still fail the probe. A disconnected target can prevent remote cleanup; the run reports the resulting failure rather than silently selecting another account. -Codex CLI runs in an in-place SSH workspace keep conversation files and native -SQLite indexes in a private remote home scoped to the company, agent, AI account, +Claude and Codex CLI runs in an in-place SSH workspace keep conversation files +(and Codex's native SQLite indexes) in a private remote home scoped to the company, agent, AI account, and conversation. The home path stays stable across turns and token refreshes. Each turn installs the current managed files, copies refreshed authentication back, and removes everything except conversation state, including on provider @@ -475,7 +475,11 @@ root also removes the retained conversation state. Older SSH host-auth sessions keep their existing remote home. A managed SSH session saved before conversation retention has no retained state reference and starts a fresh conversation instead of reusing an unavailable thread. This reset -is recorded as `codex_ssh_session_state_unavailable`; existing saved files remain. +is recorded as `claude_ssh_session_state_unavailable` or +`codex_ssh_session_state_unavailable`; existing saved files remain. Claude retains +only its `projects` transcript directory, removing credentials and configuration +after every turn. Saved CLI sessions preserve normalized remote identity and +account-scoped conversation references across server persistence. ACP SSH sessions use the existing remote process bridge and session reuse lifecycle. A compatible session can retain its private remote home; workspace @@ -484,7 +488,10 @@ removes the home on retirement. Workspace identity includes the company, agent, task and execution fingerprint. The fingerprint normalizes temporary managed-home paths and credential refreshes against the stable account session identity. Switching accounts or changing other adapter environment values starts a fresh -conversation. The launch still receives the current credentials. +conversation. The launch still receives the current credentials. When a Codex +ACP session reuses its staged remote home, credential copyback and logging bind +to the current run's local managed home before the remote environment is applied. +Refreshed credentials cannot recreate a retired run's local home. Older managed SSH ACP sessions used a different working-directory identity and start fresh after this change. Native runner session identities and sandbox staging are unchanged. diff --git a/packages/adapter-utils/src/acpx-engine/execute.test.ts b/packages/adapter-utils/src/acpx-engine/execute.test.ts index 8ac015aa8c0..459cb70ca0b 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.test.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.test.ts @@ -4570,13 +4570,18 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( const workspace = path.posix.join(remoteCwd, ".paperclip-runtime/runs/run-1/workspace"); const runtimeRootDir = path.posix.join(workspace, ".paperclip-runtime/custom"); 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 } }; - const restore = vi.fn(async () => {}), dispose = vi.fn(async () => {}); + let copybackHome = ""; + const restore = vi.fn(async () => { + await fs.mkdir(copybackHome, { recursive: true }); + await fs.writeFile(path.join(copybackHome, "copyback"), "fixture refreshed credential"); + }), dispose = vi.fn(async () => {}); const stopControl = vi.fn(async () => {}), stopProcess = vi.fn(async () => {}); const stagedRuntimes: NonNullable = new Map(); const seam = vi.fn(async (input: Parameters>[0]) => { - const stagedRuntime = await input.stage([{ key: "home", localDir: input.env.CODEX_HOME! }]); + copybackHome = input.env.CODEX_HOME!; + const stagedRuntime = await input.stage([{ key: "home", localDir: copybackHome }]); input.env.CODEX_HOME = stagedRuntime.assetDirs.home!; - return { stagedRuntime }; + return { stagedRuntime, rebindRun: async ({ env }: { env: Readonly> }) => { copybackHome = env.CODEX_HOME!; } }; }); const actual = await vi.importActual("@paperclipai/adapter-utils/execution-target"); vi.mocked(prepareAdapterExecutionTargetRuntime).mockResolvedValue({ target: executionTarget as never, workspaceRemoteDir: workspace, runtimeRootDir, @@ -4605,6 +4610,7 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( expect(stopControl).toHaveBeenCalledOnce(); 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 } }); expect(resumed.sessionInputs[0]?.cwd).toBe(result.sessionInputs[0]?.cwd); expect(resumed.sessionInputs[0]?.resumeSessionId).toBe(result.result.sessionId); @@ -4613,6 +4619,8 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( expect(resumedEnv.OPENAI_API_KEY).toBe("fixture-token-1"); expect(seam).toHaveBeenCalledOnce(); expect(restore).toHaveBeenCalledTimes(2); + await expect(fs.readFile(path.join(homes[1]!, "provider", "copyback"), "utf8")).resolves.toBe("fixture refreshed credential"); + await expect(fs.stat(homes[0]!)).rejects.toMatchObject({ code: "ENOENT" }); if (failure === "bridge") { vi.mocked(startAdapterExecutionTargetProcessSessionBridge).mockRejectedValueOnce(new Error("fixture SSH bridge unavailable")); await expect(runExecutor(configForTurn(2), { ...options, runId: "run-3", runtime: { sessionParams: resumed.result.sessionParams } })).rejects.toThrow("fixture SSH bridge unavailable"); @@ -4621,6 +4629,8 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( await runExecutor(configForTurn(2), { ...options, runId: "run-3", expectedExitCode: 1, runtime: { sessionParams: resumed.result.sessionParams } }); } expect(restore).toHaveBeenCalledTimes(3); + await expect(fs.readFile(path.join(homes[2]!, "provider", "copyback"), "utf8")).resolves.toBe("fixture refreshed credential"); + await expect(fs.stat(homes[0]!)).rejects.toMatchObject({ code: "ENOENT" }); expect(stopControl.mock.invocationCallOrder[2]).toBeLessThan(restore.mock.invocationCallOrder[2]!); expect(restore.mock.invocationCallOrder[2]).toBeLessThan(dispose.mock.invocationCallOrder[0]!); expect(dispose).toHaveBeenCalledOnce(); diff --git a/packages/adapter-utils/src/acpx-engine/execute.ts b/packages/adapter-utils/src/acpx-engine/execute.ts index 77b316a84cb..536d47ec09e 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.ts @@ -167,7 +167,7 @@ import { type ChildStderrState, type RuntimeCacheEntry, } from "./run-site-host.js"; -import { createSandboxRunSite, type SandboxRunSite } from "./run-site-sandbox.js"; +import { createSandboxRunSite, type SandboxRunSite, type ManagedHomeRunRebind } from "./run-site-sandbox.js"; import { createRuntimeSpanRunner, emitRunPhaseTiming, @@ -243,6 +243,7 @@ type AcpxRuntimeFactory = (options: PaperclipAcpRuntimeOptions) => AcpRuntime; * in-sandbox staged workspace + home, which this cache reuses. */ export interface StagedRuntimeCacheEntry { + rebindRun?: ManagedHomeRunRebind; observabilityCompatibilityKey?: string; stagedRuntime: PreparedAdapterExecutionTargetRuntime; /** @@ -355,6 +356,8 @@ export interface AcpxRemoteManagedHomeContext { } export interface AcpxRemoteManagedHomeResult { + /** Rebind a retained home's copyback destination before a compatible follow-up. */ + rebindRun?: ManagedHomeRunRebind; stagedRuntime: PreparedAdapterExecutionTargetRuntime; /** * Per-run copy-back, invoked once on every teardown/exit path (mirrors the CLI @@ -509,7 +512,7 @@ interface AcpxPreparedRuntime { // keeps the runtime warm. Null for local runs, the runner-less fallback, and // adapters with no disposable temp. remoteStagingDispose: (() => Promise) | null; - remoteStagingOwner?: Pick; + remoteStagingOwner?: Pick; // PR 3: for the remote runner-backed lane, the env keys the managed-home seam // mutated on this run (or the reused delta on a compatible resume), so the // executor can cache/refresh the staged-runtime entry after a clean turn. @@ -2409,7 +2412,7 @@ async function buildRuntime(input: { let stagedRuntime: PreparedAdapterExecutionTargetRuntime | null = null; let remoteManagedHomeTeardown: (() => Promise) | null = null; let remoteStagingDispose: (() => Promise) | null = null; - let remoteStagingOwner: Pick | undefined; + let remoteStagingOwner: Pick | undefined; let remoteStagingEnvDelta: Record | null = null; let sessionStagingLeaseRelease: (() => void) | null = null; // The sandbox run site owns the sandbox lane's staging, the per-session @@ -2431,6 +2434,7 @@ async function buildRuntime(input: { spawnCwd: cwd, target: remoteTarget, env, + managedHomeRunBinding: { env: { ...env }, onLog: input.ctx.onLog }, isCompatibleResume, observabilityCompatibilityKey: input.ctx.observability?.compatibilityKey, stage: async (assets) => { @@ -2469,6 +2473,7 @@ async function buildRuntime(input: { }) : { stagedRuntime: await stage([]) }; return { stagedRuntime: seeded.stagedRuntime, + rebindRun: seeded.rebindRun, teardown: seeded.teardown ?? (remoteTarget.transport === "ssh" ? createWorkspaceRestoreTeardown({ stagedRuntime: seeded.stagedRuntime, onLog: input.ctx.onLog, startMessage: "[paperclip] Restoring SSH workspace and managed credentials.\n", @@ -2568,9 +2573,9 @@ async function buildRuntime(input: { stagedRuntime = placedStaged?.stagedRuntime ?? null; remoteManagedHomeTeardown = placedStaged?.teardown ?? null; remoteStagingDispose = placedStaged?.dispose ?? null; + remoteStagingOwner = { teardown: remoteManagedHomeTeardown, dispose: remoteStagingDispose, rebindRun: placedStaged?.rebindRun }; if (remoteTarget.transport === "ssh") { - const owner = { teardown: remoteManagedHomeTeardown, dispose: remoteStagingDispose }; - remoteStagingOwner = owner; + const owner = remoteStagingOwner; let copyBackComplete = false; let disposeRequested = false; remoteManagedHomeTeardown = async () => { @@ -3791,6 +3796,7 @@ function saveStagedRuntimeAfterCleanTurn(input: { input.handles.set(prepared.sessionKey, { observabilityCompatibilityKey: input.observabilityCompatibilityKey, stagedRuntime: prepared.stagedRuntime, + rebindRun: prepared.remoteStagingOwner?.rebindRun, envDelta: prepared.remoteStagingEnvDelta, teardown: prepared.remoteStagingOwner?.teardown ?? prepared.remoteManagedHomeTeardown, dispose: prepared.remoteStagingOwner?.dispose ?? prepared.remoteStagingDispose, diff --git a/packages/adapter-utils/src/acpx-engine/run-site-sandbox.test.ts b/packages/adapter-utils/src/acpx-engine/run-site-sandbox.test.ts index bee3ab8ff68..3c14e6b7c24 100644 --- a/packages/adapter-utils/src/acpx-engine/run-site-sandbox.test.ts +++ b/packages/adapter-utils/src/acpx-engine/run-site-sandbox.test.ts @@ -11,6 +11,7 @@ import { describe, expect, it, vi } from "vitest"; import { createSandboxRunSite, type ManagedHomeSeamResult, + type ManagedHomeRunBinding, type SandboxRunSiteOptions, type StagedRuntimeStoreEntry, } from "./run-site-sandbox.js"; @@ -244,11 +245,15 @@ describe("sandbox run site", () => { it("test_sandbox_site_borrow_yields_files_and_runtime_is_still_created", async () => { const cachedRuntime = makeStagedRuntime("cached"); + let observedLocalHome: string | undefined; + const binding: ManagedHomeRunBinding = { env: { CODEX_HOME: "/current/local-home" }, onLog: async () => {} }; + const rebindRun = vi.fn(async (current: ManagedHomeRunBinding) => { observedLocalHome = current.env.CODEX_HOME; }); const stagedRuntimes = new Map([ [ "paperclip:c:a:t:fp", { stagedRuntime: cachedRuntime, + rebindRun, envDelta: { CODEX_HOME: "/remote/home" }, teardown: async () => ({ ok: true }), dispose: async () => {}, @@ -256,7 +261,7 @@ describe("sandbox run site", () => { }, ], ]); - const { site, env } = makeSite({ stagedRuntimes, isCompatibleResume: true }); + const { site, env } = makeSite({ stagedRuntimes, isCompatibleResume: true, managedHomeRunBinding: binding }); // A borrow reads the staged entry without removal, so an overlapping run of // the same session still reads it. @@ -272,12 +277,27 @@ describe("sandbox run site", () => { expect(site.staged?.reused).toBe(true); expect(site.staged?.stagedRuntime).toBe(cachedRuntime); expect(env.CODEX_HOME).toBe("/remote/home"); + expect(rebindRun).toHaveBeenCalledWith(binding); + expect(observedLocalHome).toBe("/current/local-home"); const candidate: SandboxReuseCandidate = site.reuseCandidate(makeReady(cachedRuntime)); expect(candidate).toEqual({ stagedRuntime: cachedRuntime }); expect("runtime" in candidate).toBe(false); expect("sessionHandle" in candidate).toBe(false); }); + it("releases the staging lease if rebinding a retained managed home fails", async () => { + const rebindRun = vi.fn(async () => { throw new Error("fixture binding failed"); }); + const entry = { stagedRuntime: makeStagedRuntime("cached"), envDelta: { CODEX_HOME: "/remote/home" }, + rebindRun, teardown: null, dispose: null, lastUsedAt: 500 }; + const stagedRuntimes = new Map([["s", entry]]); + const { site, stagingLocks, env } = makeSite({ stagedRuntimes, isCompatibleResume: true, + managedHomeRunBinding: { env: { CODEX_HOME: "/current/local-home" }, onLog: async () => {} } }); + await expect(site.placeWorkspace(makeContext("s"))).rejects.toThrow("fixture binding failed"); + expect(stagingLocks.size).toBe(0); + expect(env.CODEX_HOME).toBeUndefined(); + expect(stagedRuntimes.get("s")).toBe(entry); + }); + it("test_site_plan_supplies_session_cwd_spawn_cwd_and_tracing_before_the_fingerprint_build", () => { const { site } = makeSite(); // `plan()` is synchronous and reads no staged state, so it runs before the diff --git a/packages/adapter-utils/src/acpx-engine/run-site-sandbox.ts b/packages/adapter-utils/src/acpx-engine/run-site-sandbox.ts index fb286b189ef..c0dc775aa6b 100644 --- a/packages/adapter-utils/src/acpx-engine/run-site-sandbox.ts +++ b/packages/adapter-utils/src/acpx-engine/run-site-sandbox.ts @@ -21,6 +21,7 @@ // candidate never retain a run-scoped contribution or a bridge token. The staged // reuse payload carries the staged files only. +import type { AdapterExecutionContext } from "../types.js"; import type { AdapterExecutionTarget, AdapterExecutionTargetPaperclipBridgeHandle, @@ -43,6 +44,13 @@ import type { import { createSessionReuseStore } from "./session-reuse-store.js"; import type { WorkspaceRestoreOutcome } from "../workspace-restore-merge.js"; +export interface ManagedHomeRunBinding { + readonly env: Readonly>; + readonly onLog: AdapterExecutionContext["onLog"]; +} + +export type ManagedHomeRunRebind = (binding: ManagedHomeRunBinding) => void | Promise; + /** * One staged in-sandbox runtime the store keeps for the next compatible resume. * It is the value the staged-runtime map holds. The engine still owns this shape; @@ -51,6 +59,7 @@ import type { WorkspaceRestoreOutcome } from "../workspace-restore-merge.js"; * none — the run-start sweep is the only eviction path. */ export interface StagedRuntimeStoreEntry { + readonly rebindRun?: ManagedHomeRunRebind; observabilityCompatibilityKey?: string; stagedRuntime: PreparedAdapterExecutionTargetRuntime; envDelta: Record; @@ -64,6 +73,7 @@ export interface StagedRuntimeStoreEntry { * fields on the run and, after a clean turn, into the staged store. */ export interface StagedWorkspace { + readonly rebindRun?: ManagedHomeRunRebind; readonly stagedRuntime: PreparedAdapterExecutionTargetRuntime; readonly teardown: (() => Promise) | null; readonly dispose: (() => Promise) | null; @@ -80,6 +90,7 @@ export type StageWorkspace = (assets: AdapterManagedRuntimeAsset[]) => Promise

Promise) | null; readonly dispose: (() => Promise) | null; @@ -87,6 +98,7 @@ export interface ManagedHomeSeamResult { /** The construction options for the sandbox site. Every value is per run. */ export interface SandboxRunSiteOptions { + readonly managedHomeRunBinding?: ManagedHomeRunBinding; /** The run ledger. `executeAcpxEngine` creates it and passes it in (one ledger). */ readonly ledger: AcquiredRunResources; /** The staged-runtime map the store operates over. The engine owns it across runs. */ @@ -240,11 +252,16 @@ export function createSandboxRunSite(options: SandboxRunSiteOptions): SandboxRun // Reuse the already-staged in-sandbox workspace and managed home. // Re-apply the env keys the seam repointed and reuse the per-run // copy-back so the copy-back cadence stays exactly per run. + if (cached.rebindRun) { + if (!options.managedHomeRunBinding) throw new Error("Managed home requires the current run binding"); + await cached.rebindRun(options.managedHomeRunBinding); + } Object.assign(options.env, cached.envDelta); cached.lastUsedAt = options.now(); await options.onReuseLog(); return { stagedRuntime: cached.stagedRuntime, + rebindRun: cached.rebindRun, teardown: cached.teardown, dispose: cached.dispose, envDelta: cached.envDelta, @@ -286,6 +303,7 @@ export function createSandboxRunSite(options: SandboxRunSiteOptions): SandboxRun } return { stagedRuntime: seam.stagedRuntime, + rebindRun: seam.rebindRun, teardown: seam.teardown, dispose: seam.dispose, envDelta, diff --git a/packages/adapter-utils/src/ssh-conversation-state.ts b/packages/adapter-utils/src/ssh-conversation-state.ts new file mode 100644 index 00000000000..7a7d769dd99 --- /dev/null +++ b/packages/adapter-utils/src/ssh-conversation-state.ts @@ -0,0 +1,48 @@ +import { createHash } from "node:crypto"; +import path from "node:path"; +import { shellQuote } from "./ssh.js"; + +/** Retain provider-owned conversation files while scrubbing per-turn credentials. */ +export async function prepareSshConversationState(input: { + adapterKey: string; + preserve: readonly string[]; + remoteRoot: string; + companyId: string; + agentId: string; + accountIdentity: string; + executionIdentity: unknown; + runId: string; + savedScope: unknown; + savedConversationId: unknown; + stagedHome?: string; + run: (command: string) => Promise; +}) { + if (!/^[a-z0-9_-]+$/.test(input.adapterKey) || input.preserve.length === 0 + || input.preserve.some(pattern => !/^[A-Za-z0-9_.*-]+$/.test(pattern))) { + throw new Error("Invalid SSH conversation retention policy"); + } + const hash = (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex"); + const scope = hash([input.companyId, input.agentId, input.accountIdentity, input.executionIdentity]); + const savedId = input.savedConversationId; + const canResume = input.savedScope === scope && typeof savedId === "string" && /^[a-f0-9]{64}$/.test(savedId); + const conversationId = canResume ? savedId : hash(input.runId); + const home = path.posix.join(input.remoteRoot, ".paperclip-runtime", input.adapterKey, "conversations", scope, conversationId); + const prefix = `set -eu\numask 077\nhome=${shellQuote(home)}\n[ ! -L "$home" ]\n`; + const stateOnly = `for entry in "$home"/* "$home"/.[!.]* "$home"/..?*; do + [ -e "$entry" ] || [ -L "$entry" ] || continue + if [ ! -L "$entry" ]; then + case "\${entry##*/}" in + ${input.preserve.join("|")} ) continue ;; + esac + fi + rm -rf -- "$entry" +done`; + const cleanup = () => input.run(`${prefix}if [ -d "$home" ]; then\n${stateOnly}\nfi`); + try { + await input.run(`${prefix}mkdir -p "$home"\nchmod 700 "$home"\n${stateOnly}${input.stagedHome ? `\ncp -RP ${shellQuote(input.stagedHome + "/.")} "$home/"` : ""}`); + } catch (error) { + await cleanup(); + throw error; + } + return { home, scope, conversationId, canResume, cleanup }; +} 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 8f962153684..5ceb2b793bf 100644 --- a/packages/adapters/claude-local/src/server/execute.remote.test.ts +++ b/packages/adapters/claude-local/src/server/execute.remote.test.ts @@ -1,4 +1,6 @@ -import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { execFile } from "node:child_process"; +import { promisify } from "node:util"; +import { cp, mkdir, mkdtemp, readdir, readFile, rm, stat, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; @@ -33,7 +35,7 @@ const { resolveCommandForLogs: vi.fn(async () => "ssh://fixture@127.0.0.1:2222/remote/workspace :: claude"), prepareWorkspaceForSshExecution: vi.fn(async () => ({ gitBacked: false })), restoreWorkspaceFromSshExecution: vi.fn(async () => undefined), - syncDirectoryToSsh: vi.fn(async () => undefined), + syncDirectoryToSsh: vi.fn(async (_input?: { localDir: string; remoteDir: string }) => undefined), runSshCommand: vi.fn(async (_spec: unknown, command: string) => ({ stdout: command.includes("mktemp -d") ? "/tmp/paperclip-ssh-runtime.claude\n" : "", stderr: "" })), startAdapterExecutionTargetPaperclipBridge: vi.fn(async () => ({ env: { @@ -82,6 +84,7 @@ vi.mock("@paperclipai/adapter-utils/execution-target", async () => { import { createPromptContextFixture } from "@paperclipai/adapter-utils/test-fixtures/prompt-context"; import { execute } from "./execute.js"; +import { sessionCodec } from "./index.js"; import { resetClaudeCliCapabilitiesCacheForTests } from "./cli-capabilities.js"; describe("claude remote execution", () => { @@ -98,6 +101,98 @@ describe("claude remote execution", () => { } }); + it.each([false, true])("keeps managed SSH Claude conversations and scrubs credentials (follow-up fails=%s)", async fails => { + const root = await mkdtemp(path.join(os.tmpdir(), "paperclip-claude-ssh-conversation-")); + cleanupDirs.push(root); + const workspace = path.join(root, "workspace"); + await mkdir(workspace); + const homes = await Promise.all(["first", "next"].map(async name => { + const home = path.join(root, name); await mkdir(home); return home; + })); + const sessionId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"; + const originalRun = runChildProcess.getMockImplementation()!; + const originalSsh = runSshCommand.getMockImplementation()!; + const originalSync = syncDirectoryToSsh.getMockImplementation()!; + let firstHome = ""; + let calls = 0; + const privateRoots: string[] = []; + runSshCommand.mockImplementation(async (_spec, command) => { + const result = await promisify(execFile)("sh", ["-c", command]); + if (command.includes("mktemp -d")) privateRoots.push(result.stdout.trim()); + return result; + }); + syncDirectoryToSsh.mockImplementation(async input => { await cp(input!.localDir, input!.remoteDir, { recursive: true }); }); + runChildProcess.mockImplementation(async (...args: unknown[]) => { + const argv = args[2] as string[]; + if (!argv.includes("--print")) return originalRun(args[0] as string, args[1] as string, argv); + const options = args[3] as { env: Record; cwd: string }; + const home = options.env.CLAUDE_CONFIG_DIR!; + const transcript = path.join(home, "projects", "fixture-workspace", sessionId + ".jsonl"); + calls += 1; + expect(options.cwd).toBe(workspace); + if (calls === 1) { + firstHome = home; + await mkdir(path.dirname(transcript), { recursive: true }); + await writeFile(transcript, "fixture prior conversation"); + } else if (calls === 2) { + expect(home).toBe(firstHome); + expect(argv).toContain("--resume"); + expect(argv).toContain(sessionId); + expect(await readFile(transcript, "utf8")).toBe("fixture prior conversation"); + } else { + expect(home).not.toBe(firstHome); + expect(argv).not.toContain("--resume"); + await expect(stat(transcript)).rejects.toMatchObject({ code: "ENOENT" }); + } + await writeFile(path.join(home, ".credentials.json"), "fixture credential"); + await writeFile(path.join(home, "settings.json"), "fixture private config"); + if (calls === 2 && fails) throw new Error("fixture Claude follow-up failed"); + return { exitCode: 0, signal: null, timedOut: false, stdout: JSON.stringify({ type: "result", subtype: "success", session_id: sessionId, result: "hello" }), stderr: "", pid: 123, startedAt: new Date().toISOString() }; + }); + const context = { + agent: { id: "agent-1", companyId: "company-1", name: "Selected agent", adapterType: "claude_local", adapterConfig: {} }, + context: { paperclipWorkspace: { cwd: workspace, source: "project_primary" } }, + executionTarget: { kind: "remote" as const, transport: "ssh" as const, remoteCwd: workspace, + spec: { useHostAuth: false, host: "ssh.example.test", port: 22, username: "agent", remoteWorkspacePath: workspace, remoteCwd: workspace, privateKey: null, knownHosts: null, strictHostKeyChecking: true }, + workspaceRealization: { mode: "in_place" as const, authoritativeRoot: workspace, pathAliases: [], outboundRestorePaths: [] } }, + onLog: async () => {}, + }; + const config = (home: string) => ({ engine: "cli", command: "claude", managedAiConnection: { provider: "anthropic", method: "subscription", identity: "selected-account", sessionIdentity: "selected-account-session" }, + env: { HOME: home, CLAUDE_CONFIG_DIR: home, CLAUDE_CODE_OAUTH_TOKEN: "fixture-token", ANTHROPIC_API_KEY: "" } }); + try { + const first = await execute({ ...context, runId: "first", config: config(homes[0]!), runtime: { sessionId: null, sessionParams: null, sessionDisplayId: null, taskKey: "task" } }); + expect(first.sessionId).toBe(sessionId); + expect(await readdir(firstHome)).toEqual(["projects"]); + const saved = sessionCodec.deserialize(JSON.parse(JSON.stringify(sessionCodec.serialize(first.sessionParams!))))!; + await rm(homes[0]!, { recursive: true, force: true }); + const next = execute({ ...context, runId: "next", config: config(homes[1]!), runtime: { sessionId, sessionParams: saved, sessionDisplayId: sessionId, taskKey: "task" } }); + if (fails) await expect(next).rejects.toThrow("fixture Claude follow-up failed"); + else expect((await next).sessionId).toBe(sessionId); + expect(calls).toBe(2); + expect(await readdir(firstHome)).toEqual(["projects"]); + expect((await stat(firstHome)).mode & 0o077).toBe(0); + await expect(stat(homes[0]!)).rejects.toMatchObject({ code: "ENOENT" }); + if (!fails) { + const changed = config(homes[1]!); + changed.managedAiConnection.sessionIdentity = "another-account-session"; + for (const [runId, nextConfig, sessionParams] of [ + ["another-account", changed, saved], + ["old-params", config(homes[1]!), { sessionId, cwd: workspace, remoteExecution: saved.remoteExecution }], + ] as const) { + const result = await execute({ ...context, runId, config: nextConfig, runtime: { sessionId, sessionParams, sessionDisplayId: sessionId, taskKey: "task" } }); + expect(result.resultJson).toMatchObject({ sessionReset: { code: "claude_ssh_session_state_unavailable" } }); + expect(result.sessionParams?.sshClaudeConversationId).not.toBe(saved.sshClaudeConversationId); + } + expect(calls).toBe(4); + } + for (const dir of privateRoots) await expect(stat(dir)).rejects.toMatchObject({ code: "ENOENT" }); + } finally { + runChildProcess.mockReset().mockImplementation(originalRun); + runSshCommand.mockReset().mockImplementation(originalSsh); + syncDirectoryToSsh.mockReset().mockImplementation(originalSync); + } + }); + it("uses the agent's subscription in a private SSH home without reading the SSH user's Claude login", async () => { const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-claude-selected-ssh-")); cleanupDirs.push(rootDir); diff --git a/packages/adapters/claude-local/src/server/execute.ts b/packages/adapters/claude-local/src/server/execute.ts index 10fb5e99ef7..7ecb6c1dccc 100644 --- a/packages/adapters/claude-local/src/server/execute.ts +++ b/packages/adapters/claude-local/src/server/execute.ts @@ -2,6 +2,7 @@ import { isOpenRouterUrl, openRouterClaudeHeaders } from "@paperclipai/shared/op import { applyObservabilityEnvironment } from "@paperclipai/adapter-utils/agent-observability"; import { createProviderStoppedBoundary } from "@paperclipai/adapter-utils/provider-stopped-boundary"; import { createUsageCheckpointLog } from "@paperclipai/adapter-utils/usage-checkpoint"; +import { prepareSshConversationState } from "@paperclipai/adapter-utils/ssh-conversation-state"; import fs from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; @@ -26,6 +27,7 @@ import { resolveAdapterExecutionTargetTimeoutSec, resolveAdapterExecutionTargetCommandForLogs, runAdapterExecutionTargetProcess, + runAdapterExecutionTargetShellCommand, startAdapterExecutionTargetPaperclipBridge, } from "@paperclipai/adapter-utils/execution-target"; import { @@ -637,6 +639,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise onLog("stdout", line), @@ -667,6 +670,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise preparedExecutionTargetRuntime.restoreWorkspace((line) => onLog("stdout", line)) : null; let paperclipBridge: Awaited> = null; + let sshSessionState: Awaited> | null = null; try { if (preparedExecutionTargetRuntime?.workspaceRemoteDir) { effectiveExecutionCwd = preparedExecutionTargetRuntime.workspaceRemoteDir; @@ -712,9 +716,27 @@ export async function execute(ctx: AdapterExecutionContext): Promise { + const result = await runAdapterExecutionTargetShellCommand(runId, runtimeExecutionTarget, command, { cwd: effectiveExecutionCwd, env: {}, timeoutSec: 15 }); + if (result.exitCode !== 0 || result.timedOut) throw new Error("Cannot prepare or clean up SSH Claude conversation state"); + }, + }); + remoteClaudeConfigDir = sshSessionState.home; + } if (executionTarget?.kind === "remote" && executionTarget.transport === "ssh" && executionTarget.spec.useHostAuth === false && config.managedAiConnection && remoteClaudeRuntimeRoot) Object.assign(env, managedRemoteHomeEnvironment(remoteClaudeRuntimeRoot)); if (remoteClaudeConfigDir && remoteClaudeConfigSeedDir && remoteClaudeRuntimeRoot) { env.CLAUDE_CONFIG_DIR = remoteClaudeConfigDir; @@ -791,6 +813,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise 0 && isValidUuid && hasMatchingPromptBundle && @@ -1174,6 +1197,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise = { ...parsed, + ...(sshSessionState && runtimeSessionId && !sshSessionState.canResume + ? { sessionReset: { code: "claude_ssh_session_state_unavailable" } } : {}), ...(failed && clearSessionForMaxTurns ? { stopReason: "max_turns_exhausted" } : {}), ...(failed && poisonedPreviousMessageId ? { stopReason: "claude_poisoned_previous_message_id" } : {}), ...(claudeRefusal ? { stopReason: "refusal", errorFamily: "model_refusal" } : {}), @@ -1429,7 +1455,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise undefined); - await restoreRemoteWorkspace(); + try { + await sshSessionState?.cleanup(); + } finally { + if (restoreRemoteWorkspace) { + await onLog( + "stdout", + `[paperclip] Restoring workspace changes from ${describeAdapterExecutionTarget(executionTarget)}.\n`, + ).catch(() => undefined); + await restoreRemoteWorkspace(); + } } } } diff --git a/packages/adapters/claude-local/src/server/index.ts b/packages/adapters/claude-local/src/server/index.ts index bfba059d18e..5f962be00c6 100644 --- a/packages/adapters/claude-local/src/server/index.ts +++ b/packages/adapters/claude-local/src/server/index.ts @@ -65,11 +65,23 @@ export type { } from "./setup-token-runner.js"; import type { AdapterSessionCodec } from "@paperclipai/adapter-utils"; import { sessionCodec as acpxSessionCodec } from "@paperclipai/adapter-utils/acpx-engine/session-codec"; +import { normalizeAdapterExecutionTargetSessionIdentity } from "@paperclipai/adapter-utils/execution-target"; function readNonEmptyString(value: unknown): string | null { return typeof value === "string" && value.trim().length > 0 ? value.trim() : null; } +function remoteSessionMetadata(record: Record): Record { + const remoteExecution = normalizeAdapterExecutionTargetSessionIdentity(record.remoteExecution); + const conversationId = readNonEmptyString(record.sshClaudeConversationId); + const scope = readNonEmptyString(record.sshClaudeAccountScope); + return { + ...(remoteExecution ? { remoteExecution } : {}), + ...(conversationId && scope && /^[a-f0-9]{64}$/.test(conversationId) && /^[a-f0-9]{64}$/.test(scope) + ? { sshClaudeConversationId: conversationId, sshClaudeAccountScope: scope } : {}), + }; +} + export const sessionCodec: AdapterSessionCodec = { deserialize(raw: unknown) { if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return null; @@ -89,6 +101,7 @@ export const sessionCodec: AdapterSessionCodec = { const repoRef = readNonEmptyString(record.repoRef) ?? readNonEmptyString(record.repo_ref); return { sessionId, + ...remoteSessionMetadata(record), ...(cwd ? { cwd } : {}), ...(promptBundleKey ? { promptBundleKey } : {}), ...(mcpServerIdentity ? { mcpServerIdentity } : {}), @@ -114,6 +127,7 @@ export const sessionCodec: AdapterSessionCodec = { const repoRef = readNonEmptyString(params.repoRef) ?? readNonEmptyString(params.repo_ref); return { sessionId, + ...remoteSessionMetadata(params), ...(cwd ? { cwd } : {}), ...(promptBundleKey ? { promptBundleKey } : {}), ...(mcpServerIdentity ? { mcpServerIdentity } : {}), diff --git a/packages/adapters/codex-local/src/server/acp.test.ts b/packages/adapters/codex-local/src/server/acp.test.ts index 07a6fd24ccc..105ac5dbf40 100644 --- a/packages/adapters/codex-local/src/server/acp.test.ts +++ b/packages/adapters/codex-local/src/server/acp.test.ts @@ -1233,6 +1233,52 @@ describe("codex_local ACP lane", () => { await Promise.all(stagedDirs.map((dir) => fs.rm(dir, { recursive: true, force: true }))); }); + it("copies refreshed Codex auth to the current run home when a staged home is rebound", async () => { + const root = await makeTempRoot("paperclip-codex-acp-rebind-"); + const localCwd = path.join(root, "workspace"); + const remoteCwd = path.join(root, "remote"); + const firstHome = path.join(root, "first-account-home"); + const nextHome = path.join(root, "next-account-home"); + for (const dir of [localCwd, remoteCwd, firstHome, nextHome]) await fs.mkdir(dir); + const originalAuth = subscriptionAuthJson("acct-same", OLDER_REFRESH, "original"); + for (const home of [firstHome, nextHome]) await fs.writeFile(path.join(home, "auth.json"), originalAuth, { mode: 0o600 }); + process.env.OPENAI_API_KEY = ""; + const stagedRuntimes = new Map(); + const execute = createCodexAcpExecutor({ + createRuntime: options => new FakeRuntime(options) as never, + stagedRuntimes, stagingLocks: new Map(), + }); + const result = await execute(buildContext(localCwd, { + runId: `run-rebind-home-${randomUUID()}`, + config: { + engine: "acp", cwd: localCwd, agentCommand: "node ./fake-acp.js", stateDir: path.join(root, "state"), + managedAiConnection: { identity: "selected-account", sessionIdentity: "selected-account-session" }, + env: { CODEX_HOME: firstHome, OPENAI_API_KEY: "", CODEX_API_KEY: "" }, promptTemplate: "Do the assigned work.", + }, + context: { issueId: "issue-1", paperclipWorkspace: { cwd: localCwd, source: "project_workspace", workspaceId: "workspace-1" } }, + executionTarget: { kind: "remote", transport: "sandbox", providerKey: "fake-plugin", remoteCwd, runner: createLocalSandboxRunner() } as never, + authToken: "fixture-run-jwt", + })); + expect(result.exitCode).toBe(0); + expect(stagedRuntimes.size).toBe(1); + const entry = [...stagedRuntimes.values()][0]!; + try { + await fs.rm(firstHome, { recursive: true }); + const currentLogs: string[] = []; + await entry.rebindRun({ env: { CODEX_HOME: nextHome }, onLog: async (_stream: string, chunk: string) => { currentLogs.push(chunk); } }); + await fs.writeFile(path.join(entry.stagedRuntime.assetDirs.home, "auth.json"), subscriptionAuthJson("acct-same", NEWER_REFRESH, "refreshed")); + expect(await entry.teardown()).toMatchObject({ ok: true }); + const auth = JSON.parse(await fs.readFile(path.join(nextHome, "auth.json"), "utf8")); + expect(auth.tokens.refresh_token).toBe("ref-refreshed"); + expect(auth.last_refresh).toBe(NEWER_REFRESH); + expect((await fs.stat(path.join(nextHome, "auth.json"))).mode & 0o777).toBe(0o600); + await expect(fs.stat(firstHome)).rejects.toMatchObject({ code: "ENOENT" }); + expect(currentLogs.some(line => line.includes("Codex auth"))).toBe(true); + } finally { + await entry.dispose(); + } + }); + it("removes the host staged Codex home when a failed turn drops the staged runtime", async () => { // The complementary guardrail: when the staged runtime IS dropped (here, a // failed turn), the one-time `disposeStaged` fires and removes the host diff --git a/packages/adapters/codex-local/src/server/acp.ts b/packages/adapters/codex-local/src/server/acp.ts index 814694b1637..372b94976f4 100644 --- a/packages/adapters/codex-local/src/server/acp.ts +++ b/packages/adapters/codex-local/src/server/acp.ts @@ -183,7 +183,9 @@ async function prepareCodexRemoteManagedHome( ): Promise { const { env, runId, onLog } = input; // The host managed Codex home the engine seeded and set on env.CODEX_HOME. - const effectiveCodexHome = env.CODEX_HOME; + let effectiveCodexHome = env.CODEX_HOME; + let currentLog = onLog; + const copybackLog: typeof onLog = (stream, chunk) => currentLog(stream, chunk); if (!effectiveCodexHome) { // No managed home resolved (e.g. custom CODEX_HOME cleared) — stage the // workspace with no home asset, identical to the no-seam fallback. @@ -214,7 +216,7 @@ async function prepareCodexRemoteManagedHome( void (await copyBackCodexAuth({ readSandboxAuth: () => readFile(path.posix.join(assetDir, "auth.json")), hostAuthPath: path.join(input.config.managedAiConnection ? effectiveCodexHome : resolveSharedCodexHomeDir(process.env), "auth.json"), - log: (line) => onLog("stdout", `${line}\n`), + log: (line) => copybackLog("stdout", `${line}\n`), })), }, ]); @@ -229,6 +231,13 @@ async function prepareCodexRemoteManagedHome( return { stagedRuntime, + rebindRun: binding => { + if (input.config.managedAiConnection) { + if (!binding.env.CODEX_HOME) throw new Error("Managed Codex copyback requires the current account home"); + effectiveCodexHome = binding.env.CODEX_HOME; + } + currentLog = binding.onLog; + }, // Subscription copy-back fires on EVERY run's teardown (including a compatible // resume that reuses this staged runtime). It reads the sandbox auth.json / // workspace live and copies back to the host; it does NOT remove the staged @@ -242,7 +251,7 @@ async function prepareCodexRemoteManagedHome( // mask the run result. teardown: createWorkspaceRestoreTeardown({ stagedRuntime, - onLog, + onLog: copybackLog, startMessage: apiKeyAuth ? "[paperclip] Restoring workspace changes from the sandbox.\n" : "[paperclip] Restoring workspace changes and Codex auth from the sandbox.\n", diff --git a/packages/adapters/codex-local/src/server/ssh-session-state.ts b/packages/adapters/codex-local/src/server/ssh-session-state.ts index af949d70674..22e6b8f4fe0 100644 --- a/packages/adapters/codex-local/src/server/ssh-session-state.ts +++ b/packages/adapters/codex-local/src/server/ssh-session-state.ts @@ -1,16 +1,4 @@ -import { createHash } from "node:crypto"; -import path from "node:path"; -import { shellQuote } from "@paperclipai/adapter-utils/ssh"; - -const STATE_ONLY = `for entry in "$home"/* "$home"/.[!.]* "$home"/..?*; do - [ -e "$entry" ] || [ -L "$entry" ] || continue - if [ ! -L "$entry" ]; then - case "\${entry##*/}" in - sessions|archived_sessions|session_index.jsonl|history.jsonl|state_*.sqlite|state_*.sqlite-*|thread_history_*.sqlite|thread_history_*.sqlite-*) continue ;; - esac - fi - rm -rf -- "$entry" -done`; +import { prepareSshConversationState } from "@paperclipai/adapter-utils/ssh-conversation-state"; export async function prepareCodexSshSessionState(input: { remoteRoot: string; @@ -23,19 +11,11 @@ export async function prepareCodexSshSessionState(input: { stagedHome: string; run: (command: string) => Promise; }) { - const hash = (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex"); - const scope = hash([input.companyId, input.agentId, input.accountIdentity, input.executionIdentity]); - const savedId = input.saved.sshCodexConversationId; - const canResume = input.saved.sshCodexAccountScope === scope && typeof savedId === "string" && /^[a-f0-9]{64}$/.test(savedId); - const conversationId = canResume ? savedId : hash(input.runId); - const home = path.posix.join(input.remoteRoot, ".paperclip-runtime", "codex", "conversations", scope, conversationId); - const prefix = `set -eu\numask 077\nhome=${shellQuote(home)}\n[ ! -L "$home" ]\n`; - const cleanup = () => input.run(`${prefix}if [ -d "$home" ]; then\n${STATE_ONLY}\nfi`); - try { - await input.run(`${prefix}mkdir -p "$home"\nchmod 700 "$home"\n${STATE_ONLY}\ncp -RP ${shellQuote(input.stagedHome + "/.")} "$home/"`); - } catch (error) { - await cleanup(); - throw error; - } - return { home, scope, conversationId, canResume, cleanup }; + return prepareSshConversationState({ + ...input, + adapterKey: "codex", + preserve: ["sessions", "archived_sessions", "session_index.jsonl", "history.jsonl", "state_*.sqlite", "state_*.sqlite-*", "thread_history_*.sqlite", "thread_history_*.sqlite-*"], + savedScope: input.saved.sshCodexAccountScope, + savedConversationId: input.saved.sshCodexConversationId, + }); } diff --git a/server/src/__tests__/adapter-session-codecs.test.ts b/server/src/__tests__/adapter-session-codecs.test.ts index 622ffb5773f..f5df3906765 100644 --- a/server/src/__tests__/adapter-session-codecs.test.ts +++ b/server/src/__tests__/adapter-session-codecs.test.ts @@ -16,6 +16,23 @@ import { import { sessionCodec as acpxSessionCodec } from "@paperclipai/adapter-utils/acpx-engine/session-codec"; describe("adapter session codecs", () => { + it.each([claudeSessionCodec.serialize, claudeSessionCodec.deserialize])("keeps only normalized Claude remote identity and valid conversation references", transform => { + const params = { + sessionId: "11111111-1111-4111-8111-111111111111", + sshClaudeConversationId: "a".repeat(64), sshClaudeAccountScope: "b".repeat(64), + remoteExecution: { transport: "ssh", host: "fixture.example.test", port: 22, username: "agent", remoteCwd: "/work", + useHostAuth: false, privateKey: "fixture-private-key", tailscaleSocksProxySocketPath: "/fixture/socket" }, + }; + const output = transform(params)!; + expect(output).toMatchObject({ sessionId: params.sessionId, sshClaudeConversationId: params.sshClaudeConversationId, + sshClaudeAccountScope: params.sshClaudeAccountScope, remoteExecution: { transport: "ssh", host: "fixture.example.test", useHostAuth: false } }); + expect(JSON.stringify(output)).not.toContain("fixture-private-key"); + expect(JSON.stringify(output)).not.toContain("/fixture/socket"); + const invalid = transform({ ...params, sshClaudeConversationId: "../../untrusted" })!; + expect(invalid.sshClaudeConversationId).toBeUndefined(); + expect(invalid.sshClaudeAccountScope).toBeUndefined(); + }); + it("normalizes claude session params with cwd", () => { const parsed = claudeSessionCodec.deserialize({ session_id: "claude-session-1",