From d377c1dae4f34084cb1a9650280b67591811c1b2 Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Fri, 13 Nov 2026 11:02:40 -0800 Subject: [PATCH] Preserve SSH account defaults, command paths, and setup retries Default new API-created SSH environments to the selected agent account. Keep absent authentication fields unchanged on reads and updates so older SSH environments retain their remote login. Launch SSH ACP with a non-login shell to retain the prepared remote PATH and private HOME. On a failed Codex host runtime install, remove only newly created links that still point to this install's staged skills. Preserve manual skills and native authentication so a corrected retry can start. Four regression cases failed first; all seven targeted cases now pass. The broader eight-suite run passes 529 cases. Server, adapter-utils, and Codex typechecks, SDK input checks, and secret/PII scans pass. Co-Authored-By: Paperclip --- .../src/acpx-engine/execute.test.ts | 13 +++++++++++- .../adapter-utils/src/acpx-engine/execute.ts | 2 +- .../src/server/execute.ssh-host-auth.test.ts | 21 +++++++++++++++++-- .../src/server/ssh-host-runtime.ts | 17 ++++++++++++++- .../src/__tests__/environment-routes.test.ts | 21 +++++++++++++++++++ server/src/routes/environments.ts | 1 + server/src/services/environment-config.ts | 2 ++ 7 files changed, 72 insertions(+), 5 deletions(-) diff --git a/packages/adapter-utils/src/acpx-engine/execute.test.ts b/packages/adapter-utils/src/acpx-engine/execute.test.ts index 459cb70ca0b..330837968f7 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.test.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.test.ts @@ -4589,7 +4589,7 @@ 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: "node ./fake-acp.js", stateDir, cwd: localCwd, + const configForTurn = (turn: number) => ({ agent: "custom", agentCommand: "fixture-acp-client", stateDir, cwd: localCwd, 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" }); @@ -4606,6 +4606,17 @@ describe("ACPX engine remote managed-home seam (PR 2: per-adapter home seed)", ( const env = typeof bridgeInput.env === "function" ? await bridgeInput.env() : bridgeInput.env; expect(env.CODEX_HOME).toBe(path.posix.join(runtimeRootDir, "home")); expect(env.HOME).toBe(runtimeRootDir); + const bin = path.join(root, "remote-user-bin"); + await fs.mkdir(bin); + await fs.writeFile(path.join(bin, "fixture-acp-client"), '#!/bin/sh\nprintf "%s" "$HOME"\n', { mode: 0o700 }); + const shell = path.join(root, "remote-profile-shell"); + await fs.writeFile(shell, '#!/bin/sh\nif [ "$1" = "-lc" ]; then PATH=/usr/bin:/bin; export PATH; fi\nexec /bin/sh -c "$2"\n', { mode: 0o700 }); + const { execFile } = await import("node:child_process"); + const { promisify } = await import("node:util"); + const launched = await promisify(execFile)(shell, bridgeInput.args!, { + cwd: root, env: { ...env, PATH: `${bin}:/usr/bin:/bin` }, + }); + expect(launched.stdout).toBe(runtimeRootDir); expect(restore).toHaveBeenCalledOnce(); expect(stopControl).toHaveBeenCalledOnce(); expect(stopProcess).toHaveBeenCalledOnce(); diff --git a/packages/adapter-utils/src/acpx-engine/execute.ts b/packages/adapter-utils/src/acpx-engine/execute.ts index 536d47ec09e..3c758d26ff8 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.ts @@ -2535,7 +2535,7 @@ async function buildRuntime(input: { runtimeRootDir, adapterKey: input.engine.adapterType, command: "sh", - args: ["-lc", `exec ${agentCommandShell}`], + args: [remoteTarget.transport === "ssh" ? "-c" : "-lc", `exec ${agentCommandShell}`], cwd: sessionCwd, env: launchEnv, timeoutSec, diff --git a/packages/adapters/codex-local/src/server/execute.ssh-host-auth.test.ts b/packages/adapters/codex-local/src/server/execute.ssh-host-auth.test.ts index cf43361fc00..855beb2d7f8 100644 --- a/packages/adapters/codex-local/src/server/execute.ssh-host-auth.test.ts +++ b/packages/adapters/codex-local/src/server/execute.ssh-host-auth.test.ts @@ -76,6 +76,7 @@ vi.mock("@paperclipai/adapter-utils/execution-target", async () => { }); import { execute } from "./execute.js"; +import { shellQuote } from "@paperclipai/adapter-utils/ssh"; import { buildProviderObservabilityLaunch } from "@paperclipai/adapter-utils/agent-observability"; describe("codex SSH host authentication", () => { @@ -90,7 +91,7 @@ describe("codex SSH host authentication", () => { } }); - it.each(["default", "configured", "in_place", "no_config", "observability", "unqualified_observability", "observability_profile", "observability_profile_missing", "observability_profile_symlink", "partial_upload", "partial_upload_in_place", "changed_config", "skill_conflict"])("delivers Codex tools and skills while preserving native SSH authentication (%s)", async mode => { + it.each(["default", "configured", "in_place", "no_config", "observability", "unqualified_observability", "observability_profile", "observability_profile_missing", "observability_profile_symlink", "partial_upload", "partial_upload_in_place", "changed_config", "skill_conflict", "failed_install_retry"])("delivers Codex tools and skills while preserving native SSH authentication (%s)", async mode => { const root = await mkdtemp(path.join(os.tmpdir(), "paperclip-codex-host-auth-")); cleanupDirs.push(root); const workspace = path.join(root, "workspace"), remote = path.join(root, "remote-workspace"), userHome = path.join(root, "remote-user"); @@ -114,6 +115,15 @@ describe("codex SSH host authentication", () => { } await writeFile(path.join(native, "skills", "manual", "SKILL.md"), "native skill"); await writeFile(path.join(skill, "SKILL.md"), "---\n---\nSelected review skill.\n"); + const bin = path.join(root, "fault-bin"), failCopy = path.join(root, "fail-config-copy"); + if (mode === "failed_install_retry") { + await mkdir(bin); + await writeFile(failCopy, "fail once"); + await writeFile(path.join(bin, "cp"), `#!/bin/sh +case "$1" in */config.toml) if [ -f ${shellQuote(failCopy)} ]; then rm -f ${shellQuote(failCopy)}; echo 'fixture config copy failed' >&2; exit 71; fi ;; esac +exec /bin/cp "$@" +`, { mode: 0o700 }); + } vi.stubEnv("CODEX_HOME", controller); vi.stubEnv("PAPERCLIP_HOME", path.join(root, "paperclip")); const originalRun = runSshCommand.getMockImplementation()!, originalSync = syncDirectoryToSsh.getMockImplementation()!, originalChild = runChildProcess.getMockImplementation()!; @@ -121,7 +131,7 @@ describe("codex SSH host authentication", () => { let calls = 0; try { runSshCommand.mockImplementation(async (_spec, command) => { - const remoteEnv: NodeJS.ProcessEnv = { ...process.env, HOME: userHome }; + const remoteEnv: NodeJS.ProcessEnv = { ...process.env, HOME: userHome, ...(mode === "failed_install_retry" ? { PATH: `${bin}:${process.env.PATH}` } : {}) }; delete remoteEnv.CODEX_HOME; return promisify(execFile)("/bin/sh", ["-c", command!], { env: remoteEnv, timeout: 5000 }); }); @@ -179,6 +189,13 @@ describe("codex SSH host authentication", () => { spec: { useHostAuth: true, host: "127.0.0.1", port: 2222, username: "fixture", remoteCwd: remote, remoteWorkspacePath: remote, privateKey: null, knownHosts: null, strictHostKeyChecking: true }, workspaceRealization: { mode: ((mode === "in_place" || mode === "partial_upload_in_place") ? "in_place" : "copy") as "in_place" | "copy", authoritativeRoot: remote, pathAliases: [], outboundRestorePaths: [] } }, onLog: async () => {}, }; + if (mode === "failed_install_retry") { + await expect(execute(ctx)).rejects.toThrow(); + expect(runChildProcess).not.toHaveBeenCalled(); + expect(await readdir(path.join(native, "skills"))).toEqual(["manual"]); + expect(await readFile(path.join(native, "auth.json"), "utf8")).toBe("fixture-native-login"); + expect(await readFile(path.join(native, "config.toml"), "utf8")).toContain('model = "native-model"'); + } if (mode === "changed_config" || mode === "skill_conflict" || mode.startsWith("partial_upload")) { await expect(execute(ctx)).rejects.toThrow(mode.startsWith("partial_upload") ? "fixture partial SSH upload" : "Cannot install remote Codex runtime configuration"); expect(runChildProcess).not.toHaveBeenCalled(); diff --git a/packages/adapters/codex-local/src/server/ssh-host-runtime.ts b/packages/adapters/codex-local/src/server/ssh-host-runtime.ts index cb388663411..f17f87a676d 100644 --- a/packages/adapters/codex-local/src/server/ssh-host-runtime.ts +++ b/packages/adapters/codex-local/src/server/ssh-host-runtime.ts @@ -35,7 +35,21 @@ native_codex_home=$1 asset=$2 mkdir -p "$native_codex_home" "$native_codex_home/skills" tmp= -trap 'if [ -n "$tmp" ]; then rm -f "$tmp" "$tmp.skills"; fi' EXIT +created_skills= +cleanup_install() { + install_status=$? + trap - EXIT + if [ "$install_status" -ne 0 ]; then + for name in $created_skills; do + if [ -L "$native_codex_home/skills/$name" ] && [ "$(readlink "$native_codex_home/skills/$name")" = "$asset/skills/$name" ]; then + rm -f -- "$native_codex_home/skills/$name" || install_status=1 + fi + done + fi + if [ -n "$tmp" ]; then rm -f "$tmp" "$tmp.skills" || install_status=1; fi + exit "$install_status" +} +trap cleanup_install EXIT trap 'exit 1' HUP INT TERM tmp=$(mktemp "$native_codex_home/.paperclip-runtime-config.XXXXXX") ${input.exists ? 'cmp -s "$asset/.paperclip-native-config-before" "$native_codex_home/config.toml"' : '[ ! -e "$native_codex_home/config.toml" ]'} || { echo 'Codex configuration changed; retry this task' >&2; exit 1; } @@ -61,6 +75,7 @@ fi for skill in "$asset/skills"/*; do [ -d "$skill" ] || continue name=\${skill##*/} + created_skills="$created_skills $name" ln -s "$skill" "$native_codex_home/skills/$name" printf '%s\\n' "$name" >> "$tmp.skills" done diff --git a/server/src/__tests__/environment-routes.test.ts b/server/src/__tests__/environment-routes.test.ts index 1cf1f17f089..832e4f90777 100644 --- a/server/src/__tests__/environment-routes.test.ts +++ b/server/src/__tests__/environment-routes.test.ts @@ -2111,6 +2111,27 @@ describe("environment routes", () => { ); }); + it.each([undefined, true, false])("defaults new SSH API environments to managed auth while preserving explicit %s", async (useHostAuth) => { + const environment = { ...createEnvironment(), driver: "ssh" as const, config: createSshConfig() }; + mockEnvironmentService.create.mockResolvedValue(environment); + const app = createApp(localBoardActor()); + const res = await request(app).post("/api/companies/company-1/environments").send({ + name: "SSH Fixture", driver: "ssh", config: { ...createSshConfig(), ...(useHostAuth === undefined ? {} : { useHostAuth }) }, + }); + expect(res.status).toBe(201); + expect(mockEnvironmentService.create.mock.calls[0]![0].config.useHostAuth).toBe(useHostAuth ?? false); + }); + + it("preserves absent authentication on an existing SSH API environment update", async () => { + const environment = { ...createEnvironment(), id: "env-ssh", driver: "ssh" as const, config: createSshConfig() }; + mockEnvironmentService.getById.mockResolvedValue(environment); + mockEnvironmentService.update.mockResolvedValue(environment); + const app = createApp(localBoardActor()); + const res = await request(app).patch("/api/environments/env-ssh").send({ config: { host: "changed.example.test" } }); + expect(res.status).toBe(200); + expect(mockEnvironmentService.update.mock.calls[0]![1].config).not.toHaveProperty("useHostAuth"); + }); + it("rejects invalid SSH config on create", async () => { const app = createApp(localBoardActor()); diff --git a/server/src/routes/environments.ts b/server/src/routes/environments.ts index 06bfe2d46a3..b498e26d6a1 100644 --- a/server/src/routes/environments.ts +++ b/server/src/routes/environments.ts @@ -1086,6 +1086,7 @@ export function environmentRoutes( ), config: await normalizeEnvironmentConfigForPersistence({ db, + operation: "create", companyId, environmentName: req.body.name, driver: req.body.driver, diff --git a/server/src/services/environment-config.ts b/server/src/services/environment-config.ts index 07746215a96..6876cc2a0c3 100644 --- a/server/src/services/environment-config.ts +++ b/server/src/services/environment-config.ts @@ -546,6 +546,7 @@ export function normalizeEnvironmentConfigForProbe(input: { export async function normalizeEnvironmentConfigForPersistence(input: { db: Db; + operation?: "create" | "update"; companyId: string; environmentName: string; driver: EnvironmentDriver; @@ -584,6 +585,7 @@ export async function normalizeEnvironmentConfigForPersistence(input: { } return { ...stored, + ...(input.operation === "create" ? { useHostAuth: stored.useHostAuth ?? false } : {}), privateKey: null, privateKeySecretRef: nextPrivateKeySecretRef, } satisfies SshEnvironmentConfig;