Retain SSH conversation guards through the Codex session codec

Keep normalized, credential-free execution identity and opaque conversation references when Codex CLI parameters are saved and reloaded. Exercise the real codec round trip in the two-turn SSH regression so it proves the server resume boundary.

Reproduced two failing codec round-trip cases before the fix. All 36 focused execution-target and Codex tests, plus both package typechecks, now pass.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip committed 2026-11-12 17:52:11 -08:00
1 parent 03e654e870
commit 1dacc94f79
4 files changed
+55 -1

No files matched your search

@@ -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: "" };
@@ -1385,6 +1385,23 @@ export function adapterExecutionTargetSessionIdentity(
};
}
export function normalizeAdapterExecutionTargetSessionIdentity(value: unknown): Record<string, unknown> | 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,
@@ -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);
@@ -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<string, unknown>): Record<string, unknown> {
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 } : {}),