Skip to content

The open-source app everyone uses to manage agents at work

refactor(adapter-utils): extract the shared workspace-restore teardown factory (#12196)

Nicky Leach821573ede850
diff --git a/packages/adapter-utils/src/workspace-restore-teardown.test.ts b/packages/adapter-utils/src/workspace-restore-teardown.test.ts
new file mode 100644
index 0000000000..664c4dcbff
--- /dev/null
+++ b/packages/adapter-utils/src/workspace-restore-teardown.test.ts
@@ -0,0 +1,78 @@
+import { describe, expect, it } from "vitest";
+
+import { createWorkspaceRestoreTeardown } from "./workspace-restore-teardown.js";
+
+// One row per adapter's two message strings, taken verbatim from
+// claude-local, codex-local, and gemini-local. The factory's contract must
+// hold identically for each pair.
+const ADAPTER_MESSAGE_PAIRS = [
+ {
+ adapter: "claude-local",
+ startMessage: "[paperclip] Restoring workspace changes from the sandbox.\n",
+ failurePrefix: "[paperclip] Claude ACP teardown workspace restore failed",
+ },
+ {
+ adapter: "codex-local",
+ startMessage: "[paperclip] Restoring workspace changes and Codex auth from the sandbox.\n",
+ failurePrefix: "[paperclip] Codex ACP teardown restore/copy-back failed",
+ },
+ {
+ adapter: "gemini-local",
+ startMessage: "[paperclip] Restoring workspace changes from the sandbox.\n",
+ failurePrefix: "[paperclip] Gemini ACP teardown workspace restore failed",
+ },
+];
+
+describe.each(ADAPTER_MESSAGE_PAIRS)("createWorkspaceRestoreTeardown ($adapter)", ({ startMessage, failurePrefix }) => {
+ it("logs the start message to stdout and returns ok on a clean restore", async () => {
+ const logLines: Array<{ stream: "stdout" | "stderr"; chunk: string }> = [];
+ const teardown = createWorkspaceRestoreTeardown({
+ stagedRuntime: { restoreWorkspace: async () => {} },
+ onLog: async (stream, chunk) => {
+ logLines.push({ stream, chunk });
+ },
+ startMessage,
+ failurePrefix,
+ });
+
+ const outcome = await teardown();
+
+ expect(outcome).toEqual({ ok: true });
+ expect(logLines).toEqual([{ stream: "stdout", chunk: startMessage }]);
+ });
+
+ it("classifies a thrown EACCES error and sanitizes the stderr line", async () => {
+ const logLines: Array<{ stream: "stdout" | "stderr"; chunk: string }> = [];
+ // A host filesystem path that must never reach the run log.
+ const secretHostPath = "/home/host-user/.secret-project/workspace";
+ const thrown: NodeJS.ErrnoException = new Error(`EACCES: permission denied, open '${secretHostPath}'`);
+ thrown.code = "EACCES";
+ const teardown = createWorkspaceRestoreTeardown({
+ stagedRuntime: {
+ restoreWorkspace: async () => {
+ throw thrown;
+ },
+ },
+ onLog: async (stream, chunk) => {
+ logLines.push({ stream, chunk });
+ },
+ startMessage,
+ failurePrefix,
+ });
+
+ const outcome = await teardown();
+
+ expect(outcome).toEqual({ ok: false, code: "restore_permission_denied" });
+ const stderrLines = logLines.filter((line) => line.stream === "stderr");
+ expect(stderrLines).toEqual([
+ {
+ stream: "stderr",
+ chunk: `${failurePrefix}: the restore could not write to the workspace (permission denied)\n`,
+ },
+ ]);
+ for (const line of logLines) {
+ expect(line.chunk).not.toContain(secretHostPath);
+ expect(line.chunk).not.toContain(thrown.message);
+ }
+ });
+});
diff --git a/packages/adapter-utils/src/workspace-restore-teardown.ts b/packages/adapter-utils/src/workspace-restore-teardown.ts
new file mode 100644
index 0000000000..7c5a298d91
--- /dev/null
+++ b/packages/adapter-utils/src/workspace-restore-teardown.ts
@@ -0,0 +1,51 @@
+import type { RuntimeProgressSink } from "./runtime-progress.js";
+import {
+ classifyWorkspaceRestoreFailure,
+ describeWorkspaceRestoreFailure,
+ type WorkspaceRestoreOutcome,
+} from "./workspace-restore-merge.js";
+
+/**
+ * The one staged-runtime capability the teardown factory needs: restore the
+ * sandbox workspace back onto the host.
+ */
+export interface WorkspaceRestoreTeardownRuntime {
+ restoreWorkspace(onProgress?: RuntimeProgressSink): Promise<void>;
+}
+
+/**
+ * Builds the shared ACP adapter teardown step. The Claude, Codex, and Gemini
+ * adapters run the same five steps at teardown and differ only in two log
+ * strings: the start message and the failure prefix. Give this factory the
+ * staged runtime, the `onLog` sink, and those two strings; it returns the
+ * teardown step.
+ *
+ * The returned step logs `startMessage` to `stdout`, restores the workspace,
+ * and on success returns `{ ok: true }`. On a caught error, it classifies the
+ * error into an allowlisted {@link WorkspaceRestoreOutcome} code, logs
+ * `failurePrefix` plus the allowlisted diagnostic to `stderr`, and returns
+ * `{ ok: false, code }`.
+ */
+export function createWorkspaceRestoreTeardown(input: {
+ stagedRuntime: WorkspaceRestoreTeardownRuntime;
+ onLog: (stream: "stdout" | "stderr", chunk: string) => Promise<void>;
+ startMessage: string;
+ failurePrefix: string;
+}): () => Promise<WorkspaceRestoreOutcome> {
+ const { stagedRuntime, onLog, startMessage, failurePrefix } = input;
+ return async () => {
+ try {
+ await onLog("stdout", startMessage);
+ await stagedRuntime.restoreWorkspace((line) => onLog("stdout", line));
+ return { ok: true };
+ } catch (err) {
+ // The run log is readable by any same-company actor, so it must never
+ // carry the caught error's own message: that message can hold a host
+ // filesystem path or a process id. Log only the fixed, allowlisted
+ // diagnostic for the classified code.
+ const code = classifyWorkspaceRestoreFailure(err);
+ await onLog("stderr", `${failurePrefix}: ${describeWorkspaceRestoreFailure(code)}\n`);
+ return { ok: false, code };
+ }
+ };
+}
diff --git a/packages/adapters/claude-local/src/server/acp.ts b/packages/adapters/claude-local/src/server/acp.ts
index d064cfae52..019e06baa6 100644
--- a/packages/adapters/claude-local/src/server/acp.ts
+++ b/packages/adapters/claude-local/src/server/acp.ts
@@ -48,10 +48,7 @@ import {
classifyThrownErrorClass,
logSandboxProbeDiagnostic,
} from "./probe-diagnostics.js";
-import {
- classifyWorkspaceRestoreFailure,
- describeWorkspaceRestoreFailure,
-} from "@paperclipai/adapter-utils/workspace-restore-merge";
+import { createWorkspaceRestoreTeardown } from "@paperclipai/adapter-utils/workspace-restore-teardown";
import { buildLocalAdapterTestProbeEnv } from "./probe-env.js";
import { detectClaudeLoginRequired, parseClaudeStreamJson } from "./parse.js";
import { buildClaudeProbePermissionArgs } from "./permissions.js";
@@ -219,24 +216,13 @@ async function prepareClaudeRemoteManagedHome(
// the host. A restore miss is logged and never fails the run.
const registerWorkspaceSyncBack = (
stagedRuntime: AcpxRemoteManagedHomeResult["stagedRuntime"],
- ): AcpxRemoteManagedHomeResult["teardown"] => async () => {
- try {
- await onLog("stdout", "[paperclip] Restoring workspace changes from the sandbox.\n");
- await stagedRuntime.restoreWorkspace((line) => onLog("stdout", line));
- return { ok: true };
- } catch (err) {
- // The run log is readable by any same-company actor, so it must never
- // carry the caught error's own message: that message can hold a host
- // filesystem path or a process id. Log only the fixed, allowlisted
- // diagnostic for the classified code.
- const code = classifyWorkspaceRestoreFailure(err);
- await onLog(
- "stderr",
- `[paperclip] Claude ACP teardown workspace restore failed: ${describeWorkspaceRestoreFailure(code)}\n`,
- );
- return { ok: false, code };
- }
- };
+ ): AcpxRemoteManagedHomeResult["teardown"] =>
+ createWorkspaceRestoreTeardown({
+ stagedRuntime,
+ onLog,
+ startMessage: "[paperclip] Restoring workspace changes from the sandbox.\n",
+ failurePrefix: "[paperclip] Claude ACP teardown workspace restore failed",
+ });
const envConfig = parseObject(input.config.env);
const explicitClaudeConfigDir =
typeof envConfig.CLAUDE_CONFIG_DIR === "string" && envConfig.CLAUDE_CONFIG_DIR.trim().length > 0
diff --git a/packages/adapters/codex-local/src/server/acp.test.ts b/packages/adapters/codex-local/src/server/acp.test.ts
index 6ce672ae98..d398e4e57d 100644
--- a/packages/adapters/codex-local/src/server/acp.test.ts
+++ b/packages/adapters/codex-local/src/server/acp.test.ts
@@ -1,9 +1,26 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import type { AdapterExecutionContext, AdapterInvocationMeta } from "@paperclipai/adapter-utils";
import { runChildProcess } from "@paperclipai/adapter-utils/server-utils";
+
+// Every test in this file needs a real teardown, so the mock below delegates
+// to the actual factory by default. Only the wiring test further down reads
+// the call arguments; it does not change this behavior.
+const mockCreateWorkspaceRestoreTeardown = vi.hoisted(() => vi.fn());
+
+vi.mock("@paperclipai/adapter-utils/workspace-restore-teardown", async (importOriginal) => {
+ const actual = await importOriginal<Record<string, unknown>>();
+ mockCreateWorkspaceRestoreTeardown.mockImplementation(
+ actual.createWorkspaceRestoreTeardown as (...args: unknown[]) => unknown,
+ );
+ return {
+ ...actual,
+ createWorkspaceRestoreTeardown: mockCreateWorkspaceRestoreTeardown,
+ };
+});
+
import {
buildCodexAcpConfig,
createCodexAcpExecutor,
@@ -1135,99 +1152,57 @@ describe("codex_local ACP lane", () => {
expect(hostAuth.tokens.refresh_token).toBe("ref-sandbox-newer");
});
- it("test_codex_acp_teardown_restore_failure_sanitizes_the_run_log", async () => {
- // Security regression for a workspace-restore write failure: the run log
- // is readable by any same-company actor, so the teardown must never write
- // the caught error's own message there — that message can carry the host
- // workspace path. Force a real EACCES by making the workspace read-only,
- // and name it with a sentinel marker so any leak is easy to spot.
- const runId = "run-restore-failure";
- const root = await makeTempRoot("paperclip-codex-acp-restore-failure-");
- const localCwd = path.join(root, "SENTINEL-HOST-PATH-marker", "worktree");
+ it("passes the Codex-specific teardown messages to the shared workspace-restore-teardown factory", async () => {
+ // Wiring test only: `createWorkspaceRestoreTeardown` owns the
+ // classify-and-redact contract, proven once for all three adapters by its
+ // own table-driven test in `packages/adapter-utils`. This test proves only
+ // that the Codex adapter passes its own two message strings to it.
+ // Clear the shared, hoisted mock first: earlier tests in this file also
+ // call through it, and a leftover call could hide a real wiring bug.
+ mockCreateWorkspaceRestoreTeardown.mockClear();
+ const root = await makeTempRoot("paperclip-codex-acp-teardown-wiring-");
+ const localCwd = path.join(root, "worktree");
const remoteCwd = path.join(root, "remote-workspace");
- const sourceHome = path.join(root, "codex-home");
- const sharedHostHome = path.join(root, "shared-codex-home");
await fs.mkdir(localCwd, { recursive: true });
await fs.mkdir(remoteCwd, { recursive: true });
- await fs.mkdir(sourceHome, { recursive: true });
- await fs.mkdir(sharedHostHome, { recursive: true });
- await fs.writeFile(path.join(localCwd, "hello.txt"), "hi", "utf8");
- process.env.CODEX_HOME = sharedHostHome;
- // The runtime writes a new file into the in-sandbox workspace during the
- // turn, so the teardown's restore has something to copy back — and a new
- // file is exactly what a read-only workspace directory rejects. The
- // workspace turns read-only only after the turn's own writes, so the
- // teardown restore that runs after the turn is the write this forces to
- // fail.
- const runtime = new FakeRuntime({});
- const startTurn = runtime.startTurn.bind(runtime);
- runtime.startTurn = (input) => {
- const turn = startTurn(input);
- const remoteWorkspaceCwd = input.handle.cwd ?? remoteCwd;
- return {
- ...turn,
- result: (async () => {
- await fs.writeFile(path.join(remoteWorkspaceCwd, "from-sandbox.txt"), "synced", "utf8");
- await fs.chmod(localCwd, 0o500);
- return await turn.result;
- })(),
- };
- };
-
- const stagedRuntimes = new Map();
const execute = createCodexAcpExecutor({
- createRuntime: (options: FakeRuntimeOptions) => {
- Object.assign(runtime.options, options);
- return runtime as never;
- },
- stagedRuntimes,
+ createRuntime: (options: FakeRuntimeOptions) => new FakeRuntime(options) as never,
+ stagedRuntimes: new Map(),
stagingLocks: new Map(),
});
+ const result = await execute(
+ buildContext(localCwd, {
+ config: {
+ engine: "acp",
+ cwd: localCwd,
+ agentCommand: "node ./fake-acp.js",
+ stateDir: path.join(root, "state"),
+ env: { CODEX_HOME: path.join(root, "codex-home") },
+ 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: "real-run-jwt",
+ }),
+ );
- const loggedLines: string[] = [];
- try {
- const result = await execute(
- buildContext(localCwd, {
- runId,
- config: {
- engine: "acp",
- cwd: localCwd,
- agentCommand: "node ./fake-acp.js",
- stateDir: path.join(root, "state"),
- env: { CODEX_HOME: sourceHome },
- 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: "real-run-jwt",
- onLog: async (_stream, chunk) => {
- loggedLines.push(chunk);
- },
- }),
- );
-
- // Fail-open: the restore miss never changes the run's exit code or
- // status, and it surfaces as one allowlisted code — never the raw error.
- expect(result.exitCode).toBe(0);
- expect(result.resultJson?.workspaceRestoreFailure).toBe("restore_permission_denied");
- const allLogs = loggedLines.join("");
- expect(allLogs).not.toContain("SENTINEL-HOST-PATH-marker");
- expect(allLogs).not.toContain(localCwd);
- expect(allLogs).not.toContain("EACCES");
- expect(allLogs).toContain("permission denied");
- } finally {
- await fs.chmod(localCwd, 0o700).catch(() => undefined);
- }
+ expect(result.exitCode).toBe(0);
+ expect(mockCreateWorkspaceRestoreTeardown).toHaveBeenCalledWith(
+ expect.objectContaining({
+ startMessage: "[paperclip] Restoring workspace changes and Codex auth from the sandbox.\n",
+ failurePrefix: "[paperclip] Codex ACP teardown restore/copy-back failed",
+ }),
+ );
});
it("falls back to the CLI lane for a runner-less sandbox even when the ACP command is set", async () => {
diff --git a/packages/adapters/codex-local/src/server/acp.ts b/packages/adapters/codex-local/src/server/acp.ts
index 9bd45142ee..650fb04096 100644
--- a/packages/adapters/codex-local/src/server/acp.ts
+++ b/packages/adapters/codex-local/src/server/acp.ts
@@ -35,10 +35,7 @@ import {
asString,
parseObject,
} from "@paperclipai/adapter-utils/server-utils";
-import {
- classifyWorkspaceRestoreFailure,
- describeWorkspaceRestoreFailure,
-} from "@paperclipai/adapter-utils/workspace-restore-merge";
+import { createWorkspaceRestoreTeardown } from "@paperclipai/adapter-utils/workspace-restore-teardown";
import { normalizeCodexModel } from "../index.js";
import { classifyCodexAuthRefreshFailure } from "./parse.js";
import { copyBackCodexAuth } from "./codex-auth-copyback.js";
@@ -240,32 +237,16 @@ async function prepareCodexRemoteManagedHome(
// without its staged home. Host staged-temp removal is deliberately NOT here
// — see `disposeStaged` — so caching this runtime for reuse never destroys
// resources the next resume needs.
- teardown: async () => {
- try {
- await onLog(
- "stdout",
- "[paperclip] Restoring workspace changes and Codex auth from the sandbox.\n",
- );
- await stagedRuntime.restoreWorkspace((line) => onLog("stdout", line));
- return { ok: true };
- } catch (err) {
- // Fail-soft: a teardown copy-back miss loses this rotation and surfaces
- // loudly as refresh_token_reused on the next host Codex use (re-auth
- // recovers) — never silent host-credential corruption, so it must not
- // mask the run result.
- //
- // The run log is readable by any same-company actor, so it must never
- // carry the caught error's own message: that message can hold a host
- // filesystem path or a process id. Log only the fixed, allowlisted
- // diagnostic for the classified code.
- const code = classifyWorkspaceRestoreFailure(err);
- await onLog(
- "stderr",
- `[paperclip] Codex ACP teardown restore/copy-back failed: ${describeWorkspaceRestoreFailure(code)}\n`,
- );
- return { ok: false, code };
- }
- },
+ // Fail-soft: a teardown copy-back miss loses this rotation and surfaces
+ // loudly as refresh_token_reused on the next host Codex use (re-auth
+ // recovers) — never silent host-credential corruption, so it must not
+ // mask the run result.
+ teardown: createWorkspaceRestoreTeardown({
+ stagedRuntime,
+ onLog,
+ startMessage: "[paperclip] Restoring workspace changes and Codex auth from the sandbox.\n",
+ failurePrefix: "[paperclip] Codex ACP teardown restore/copy-back failed",
+ }),
// One-time cleanup of the HOST staged home temp dir. Fired ONLY when the
// staged runtime is dropped (failed/cancelled/timed-out turn, incompatible
// re-stage, idle eviction) — never on a clean turn that keeps the runtime
diff --git a/packages/adapters/gemini-local/src/server/acp.test.ts b/packages/adapters/gemini-local/src/server/acp.test.ts
index 27ba27f786..53d369f3e9 100644
--- a/packages/adapters/gemini-local/src/server/acp.test.ts
+++ b/packages/adapters/gemini-local/src/server/acp.test.ts
@@ -1,9 +1,26 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import type { AdapterExecutionContext, AdapterInvocationMeta } from "@paperclipai/adapter-utils";
import { runChildProcess } from "@paperclipai/adapter-utils/server-utils";
+
+// Every test in this file needs a real teardown, so the mock below delegates
+// to the actual factory by default. Only the wiring test further down reads
+// the call arguments; it does not change this behavior.
+const mockCreateWorkspaceRestoreTeardown = vi.hoisted(() => vi.fn());
+
+vi.mock("@paperclipai/adapter-utils/workspace-restore-teardown", async (importOriginal) => {
+ const actual = await importOriginal<Record<string, unknown>>();
+ mockCreateWorkspaceRestoreTeardown.mockImplementation(
+ actual.createWorkspaceRestoreTeardown as (...args: unknown[]) => unknown,
+ );
+ return {
+ ...actual,
+ createWorkspaceRestoreTeardown: mockCreateWorkspaceRestoreTeardown,
+ };
+});
+
import {
buildGeminiAcpConfig,
createGeminiAcpExecutor,
@@ -613,88 +630,54 @@ describe("gemini_local ACP lane", () => {
await expect(fs.readFile(path.join(localCwd, "from-sandbox.txt"), "utf8")).resolves.toBe("synced");
});
- it("test_gemini_acp_teardown_restore_failure_sanitizes_the_run_log", async () => {
- // Security regression for a workspace-restore write failure: the run log
- // is readable by any same-company actor, so the teardown must never write
- // the caught error's own message there — that message can carry the host
- // workspace path. Force a real EACCES by making the workspace read-only,
- // and name it with a sentinel marker so any leak is easy to spot.
- const root = await makeTempRoot("paperclip-gemini-acp-restore-failure-");
- const localCwd = path.join(root, "SENTINEL-HOST-PATH-marker", "worktree");
+ it("passes the Gemini-specific teardown messages to the shared workspace-restore-teardown factory", async () => {
+ // Wiring test only: `createWorkspaceRestoreTeardown` owns the
+ // classify-and-redact contract, proven once for all three adapters by its
+ // own table-driven test in `packages/adapter-utils`. This test proves only
+ // that the Gemini adapter passes its own two message strings to it.
+ // Clear the shared, hoisted mock first: earlier tests in this file also
+ // call through it, and a leftover call could hide a real wiring bug.
+ mockCreateWorkspaceRestoreTeardown.mockClear();
+ const root = await makeTempRoot("paperclip-gemini-acp-teardown-wiring-");
+ const localCwd = path.join(root, "worktree");
const remoteCwd = path.join(root, "remote-workspace");
await fs.mkdir(localCwd, { recursive: true });
await fs.mkdir(remoteCwd, { recursive: true });
- await fs.writeFile(path.join(localCwd, "hello.txt"), "hi", "utf8");
-
- // The runtime writes a new file into the in-sandbox workspace during the
- // turn, so the teardown's restore has something to copy back — and a new
- // file is exactly what a read-only workspace directory rejects. The
- // workspace turns read-only only after the turn's own writes, so the
- // teardown restore that runs after the turn is the write this forces to
- // fail.
- const runtime = new FakeRuntime({});
- const startTurn = runtime.startTurn.bind(runtime);
- runtime.startTurn = (input) => {
- const turn = startTurn(input);
- const remoteWorkspaceCwd = input.handle.cwd ?? remoteCwd;
- return {
- ...turn,
- result: (async () => {
- await fs.writeFile(path.join(remoteWorkspaceCwd, "from-sandbox.txt"), "synced", "utf8");
- await fs.chmod(localCwd, 0o500);
- return await turn.result;
- })(),
- };
- };
const execute = createGeminiAcpExecutor({
- createRuntime: (options) => {
- Object.assign(runtime.options, options);
- return runtime as never;
- },
+ createRuntime: (options) => new FakeRuntime(options) as never,
});
+ const result = await execute(
+ buildContext(localCwd, {
+ config: {
+ engine: "acp",
+ cwd: localCwd,
+ agentCommand: "node ./fake-acp.js",
+ stateDir: path.join(root, "state"),
+ 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: "real-run-jwt",
+ }),
+ );
- const loggedLines: string[] = [];
- try {
- const result = await execute(
- buildContext(localCwd, {
- config: {
- engine: "acp",
- cwd: localCwd,
- agentCommand: "node ./fake-acp.js",
- stateDir: path.join(root, "state"),
- 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: "real-run-jwt",
- onLog: async (_stream, chunk) => {
- loggedLines.push(chunk);
- },
- }),
- );
-
- // Fail-open: the restore miss never changes the run's exit code or
- // status, and it surfaces as one allowlisted code — never the raw error.
- expect(result.exitCode).toBe(0);
- expect(result.resultJson?.workspaceRestoreFailure).toBe("restore_permission_denied");
- const allLogs = loggedLines.join("");
- expect(allLogs).not.toContain("SENTINEL-HOST-PATH-marker");
- expect(allLogs).not.toContain(localCwd);
- expect(allLogs).not.toContain("EACCES");
- expect(allLogs).toContain("permission denied");
- } finally {
- await fs.chmod(localCwd, 0o700).catch(() => undefined);
- }
+ expect(result.exitCode).toBe(0);
+ expect(mockCreateWorkspaceRestoreTeardown).toHaveBeenCalledWith(
+ expect.objectContaining({
+ startMessage: "[paperclip] Restoring workspace changes from the sandbox.\n",
+ failurePrefix: "[paperclip] Gemini ACP teardown workspace restore failed",
+ }),
+ );
});
it("does not persist an api-key auth selector from a host-only credential", async () => {
diff --git a/packages/adapters/gemini-local/src/server/acp.ts b/packages/adapters/gemini-local/src/server/acp.ts
index 95d8db4b69..97ea0f584b 100644
--- a/packages/adapters/gemini-local/src/server/acp.ts
+++ b/packages/adapters/gemini-local/src/server/acp.ts
@@ -31,10 +31,7 @@ import {
asString,
parseObject,
} from "@paperclipai/adapter-utils/server-utils";
-import {
- classifyWorkspaceRestoreFailure,
- describeWorkspaceRestoreFailure,
-} from "@paperclipai/adapter-utils/workspace-restore-merge";
+import { createWorkspaceRestoreTeardown } from "@paperclipai/adapter-utils/workspace-restore-teardown";
import { DEFAULT_GEMINI_LOCAL_MODEL } from "../index.js";
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
@@ -166,24 +163,13 @@ async function prepareGeminiRemoteManagedHome(
// the host. A restore miss is logged and never fails the run.
const registerWorkspaceSyncBack = (
stagedRuntime: AcpxRemoteManagedHomeResult["stagedRuntime"],
- ): AcpxRemoteManagedHomeResult["teardown"] => async () => {
- try {
- await onLog("stdout", "[paperclip] Restoring workspace changes from the sandbox.\n");
- await stagedRuntime.restoreWorkspace((line) => onLog("stdout", line));
- return { ok: true };
- } catch (err) {
- // The run log is readable by any same-company actor, so it must never
- // carry the caught error's own message: that message can hold a host
- // filesystem path or a process id. Log only the fixed, allowlisted
- // diagnostic for the classified code.
- const code = classifyWorkspaceRestoreFailure(err);
- await onLog(
- "stderr",
- `[paperclip] Gemini ACP teardown workspace restore failed: ${describeWorkspaceRestoreFailure(code)}\n`,
- );
- return { ok: false, code };
- }
- };
+ ): AcpxRemoteManagedHomeResult["teardown"] =>
+ createWorkspaceRestoreTeardown({
+ stagedRuntime,
+ onLog,
+ startMessage: "[paperclip] Restoring workspace changes from the sandbox.\n",
+ failurePrefix: "[paperclip] Gemini ACP teardown workspace restore failed",
+ });
const geminiSkillsHome = resolveGeminiSkillsHome(input.config);
const stagedRuntime = await input.stage(
geminiSkillsHome