diff --git a/packages/adapter-utils/src/execution-target.test.ts b/packages/adapter-utils/src/execution-target.test.ts index 64556f7bf34..28f239e0f9d 100644 --- a/packages/adapter-utils/src/execution-target.test.ts +++ b/packages/adapter-utils/src/execution-target.test.ts @@ -7,6 +7,8 @@ import { cleanupGitHubOperationLaunchers, prepareGitHubOperationLaunchers, adapterExecutionTargetUsesManagedHome, + adapterExecutionTargetSessionIdentity, + normalizeAdapterExecutionTargetSessionIdentity, ensureAdapterExecutionTargetRuntimeCommandInstalled, ensureAdapterExecutionTargetDirectory, ensureAdapterExecutionTargetCommandResolvable, @@ -35,6 +37,23 @@ function sshExecutionTarget(): AdapterSshExecutionTarget { }; } +describe("persisted execution target identities", () => { + it("retains SSH session guards without persisting connection credentials or socket paths", () => { + const target = sshExecutionTarget(); + target.spec.useHostAuth = false; + const identity = adapterExecutionTargetSessionIdentity(target)!; + expect(normalizeAdapterExecutionTargetSessionIdentity({ ...identity, privateKey: "credential canary", tailscaleSocket: "/private/socket", unexpected: { secret: "canary" } })).toEqual(identity); + expect(normalizeAdapterExecutionTargetSessionIdentity({ ...identity, port: "22" })).toBeNull(); + expect(normalizeAdapterExecutionTargetSessionIdentity({ ...identity, host: {} })).toBeNull(); + }); + it("retains sandbox lease guards and rejects unknown transports", () => { + const target = { kind: "remote" as const, transport: "sandbox" as const, remoteCwd: "/workspace", providerKey: "fixture", environmentId: "environment", leaseId: "lease" }; + const identity = adapterExecutionTargetSessionIdentity(target)!; + expect(normalizeAdapterExecutionTargetSessionIdentity({ ...identity, credential: "canary" })).toEqual(identity); + expect(normalizeAdapterExecutionTargetSessionIdentity({ ...identity, transport: "unknown" })).toBeNull(); + }); +}); + describe("execution target validation failures", () => { afterEach(() => vi.restoreAllMocks()); const result = { exitCode: 0, signal: null, timedOut: false, stdout: "", stderr: "", pid: null, startedAt: "" }; diff --git a/packages/adapter-utils/src/execution-target.ts b/packages/adapter-utils/src/execution-target.ts index d3a02464899..14c7c5924ad 100644 --- a/packages/adapter-utils/src/execution-target.ts +++ b/packages/adapter-utils/src/execution-target.ts @@ -1385,6 +1385,23 @@ export function adapterExecutionTargetSessionIdentity( }; } +export function normalizeAdapterExecutionTargetSessionIdentity(value: unknown): Record | null { + const saved = parseObject(value); + if (saved.transport === "ssh") { + if (!readStringMeta(saved, "host") || !readStringMeta(saved, "username") || !readStringMeta(saved, "remoteCwd") + || typeof saved.port !== "number" || !Number.isInteger(saved.port) || saved.port < 1 || saved.port > 65535 + || (saved.useHostAuth !== undefined && typeof saved.useHostAuth !== "boolean")) return null; + const fields = Object.fromEntries(Object.entries(saved).filter(([key, entry]) => typeof entry === "string" + || (key === "port" && typeof entry === "number") || (key === "useHostAuth" && typeof entry === "boolean"))); + return buildRemoteExecutionSessionIdentity(fields as unknown as SshRemoteExecutionSpec); + } + if (saved.transport === "sandbox" && readStringMeta(saved, "remoteCwd")) { + return adapterExecutionTargetSessionIdentity({ kind: "remote", transport: "sandbox", remoteCwd: readStringMeta(saved, "remoteCwd")!, + providerKey: readStringMeta(saved, "providerKey"), environmentId: readStringMeta(saved, "environmentId"), leaseId: readStringMeta(saved, "leaseId") }); + } + return null; +} + export function adapterExecutionTargetSessionMatches( saved: unknown, target: AdapterExecutionTarget | null | undefined, diff --git a/packages/adapters/codex-local/src/server/execute.remote.test.ts b/packages/adapters/codex-local/src/server/execute.remote.test.ts index 4329b22afc5..6e27ee86b80 100644 --- a/packages/adapters/codex-local/src/server/execute.remote.test.ts +++ b/packages/adapters/codex-local/src/server/execute.remote.test.ts @@ -77,6 +77,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"; describe("codex remote execution", () => { const cleanupDirs: string[] = []; @@ -156,13 +157,16 @@ describe("codex remote execution", () => { try { const first = await execute({ ...ctx, runId: "first", runtime: { sessionId: null, sessionParams: null, sessionDisplayId: null, taskKey: "task-one" } }); expect(first.sessionId).toBe("ssh-thread"); + const saved = sessionCodec.deserialize(JSON.parse(JSON.stringify(sessionCodec.serialize(first.sessionParams!))))!; + expect(saved).toMatchObject({ remoteExecution: first.sessionParams!.remoteExecution, + sshCodexConversationId: first.sessionParams!.sshCodexConversationId, sshCodexAccountScope: first.sessionParams!.sshCodexAccountScope }); expect(await readFile(path.join(home, "auth.json"), "utf8")).toBe(refreshed); await writeFile(path.join(secondHome, "auth.json"), refreshed); expect(await readdir(firstRemoteHome!)).toEqual(expect.arrayContaining(["sessions", "state_5.sqlite"])); expect(await readdir(firstRemoteHome!)).not.toContain("auth.json"); const second = execute({ ...ctx, runId: "second", config: { ...ctx.config, managedAiConnection: { ...ctx.config.managedAiConnection, identity: "generation-two" }, env: { CODEX_HOME: secondHome, OPENAI_API_KEY: "" } }, - runtime: { sessionId: first.sessionId!, sessionParams: first.sessionParams!, sessionDisplayId: first.sessionId!, taskKey: "task-one" } }); + runtime: { sessionId: first.sessionId!, sessionParams: saved, sessionDisplayId: first.sessionId!, taskKey: "task-one" } }); if (fails) await expect(second).rejects.toThrow("fixture provider failed"); else expect((await second).sessionId).toBe("ssh-thread"); expect(calls).toBe(2); diff --git a/packages/adapters/codex-local/src/server/index.ts b/packages/adapters/codex-local/src/server/index.ts index bc4c9ef0f8d..809418aa9c0 100644 --- a/packages/adapters/codex-local/src/server/index.ts +++ b/packages/adapters/codex-local/src/server/index.ts @@ -65,11 +65,23 @@ export { } from "./quota.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.sshCodexConversationId); + const scope = readNonEmptyString(record.sshCodexAccountScope); + return { + ...(remoteExecution ? { remoteExecution } : {}), + ...(conversationId && scope && /^[a-f0-9]{64}$/.test(conversationId) && /^[a-f0-9]{64}$/.test(scope) + ? { sshCodexConversationId: conversationId, sshCodexAccountScope: scope } : {}), + }; +} + export const sessionCodec: AdapterSessionCodec = { deserialize(raw: unknown) { if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return null; @@ -85,6 +97,7 @@ export const sessionCodec: AdapterSessionCodec = { const repoRef = readNonEmptyString(record.repoRef) ?? readNonEmptyString(record.repo_ref); return { sessionId, + ...remoteSessionMetadata(record), ...(cwd ? { cwd } : {}), ...(workspaceId ? { workspaceId } : {}), ...(repoUrl ? { repoUrl } : {}), @@ -104,6 +117,7 @@ export const sessionCodec: AdapterSessionCodec = { const repoRef = readNonEmptyString(params.repoRef) ?? readNonEmptyString(params.repo_ref); return { sessionId, + ...remoteSessionMetadata(params), ...(cwd ? { cwd } : {}), ...(workspaceId ? { workspaceId } : {}), ...(repoUrl ? { repoUrl } : {}),