fix(native-runtime): stop failover backups from carrying throwaway provider state (#17618)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Native runner sessions on managed sandboxes keep a failover backup
of provider state, so a replacement sandbox can resume the session
> - Each backup copies the provider's runtime directory, with a
per-directory sha256 manifest, minus `excludeEntries`
> - Codex already excludes credentials and re-downloadable plugin
caches; OpenCode excluded nothing, and ACPX excluded only per-agent
credentials
> - OpenCode also leaked a temporary HOME on every launch, because
removing it failed on the read-only skill snapshot inside and the error
was swallowed
> - So every backup carried every earlier launch's HOME (with its npm
cache) plus re-installable plugin `node_modules`
> - This pull request removes those homes correctly, sweeps homes left
by earlier versions, and excludes the throwaway entries from OpenCode
and ACPX backups
> - The benefit is much smaller backups and live session directories,
while session history is still backed up and restored

## Linked Issues or Issue Description

Bug report described in-PR (bug report template).

**What happened?**
On one instance, current native failover backups totalled 24 GB across
about 300 sessions. For OpenCode sessions:
- `opencode/<session>/home-XXXXXX/`: 170 per-launch HOME directories
across 67 sessions, 9.3 GB. Each held `.npm` (about 111 MB of npm
cache), `.claude` (the skill snapshot) and `.local`.
- `opencode/<session>/config/opencode/node_modules`: 68 sessions, 4.3 GB
of OpenCode plugin installs.

One ACPX session also carried `acpx/<session>/data/pnpm/store` (229 MB
in the current backup, several GB in older copies).

Cause of the leaked homes: `startRuntime` in the OpenCode driver creates
a fresh `mkdtemp` HOME for each launch and seals the assigned skills
inside it (directories `0555`, files `0444`). `close()` and the
startup-failure paths removed it with `rm(..., { recursive: true, force:
true }).catch(() => undefined)`. That fails with `EACCES` on the sealed
directories, and the failure was ignored, so each launch left its HOME
behind.

**Expected behavior**
Per-launch homes are removed when the runtime closes, and backups hold
only state needed to resume a session. Caches that the provider
re-creates are left out.

**Steps to reproduce**
1. Run an OpenCode native session with at least one assigned skill on a
managed sandbox, over several turns or runs.
2. Inspect `<session>/` in the OpenCode runtime directory: one
`home-XXXXXX` per launch remains.
3. Inspect `failover-backups/current/opencode/<session>/`: the same
homes and `config/opencode/node_modules` are in the backup.

**Paperclip version or commit:** `master` at e335265568.
**Deployment mode:** any with native OpenCode/ACPX sessions on managed
sandboxes.
**Agent adapter(s) involved:** OpenCode (native), ACPX.

## What Changed

- OpenCode driver
(`packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.ts`):
- `removeIsolatedHome` unseals the skill snapshot with the existing
`releaseMaterializedNativeRuntimeSkills`, then removes the HOME. Close
and every startup-failure path use it. It only unseals when `.claude` is
a real directory, not a symlink.
- Per-launch homes are now created under `<session>/homes/home-XXXXXX`,
so one exact path can be excluded.
- When a session opens, `home-*` directories left at the session root by
earlier versions are removed. History is not in them: OpenCode keeps it
in `XDG_DATA_HOME` (`<session>/data/opencode/opencode.db`), and HOME
only holds caches, state locks and the skill copy.
- Exports `opencodeRuntimeSessionDirectoryName` (the existing
session-directory naming) and `OPENCODE_ISOLATED_HOMES_DIRECTORY` for
the server.
- `resolveNativeHarnessPersistenceProfile`
(`server/src/services/native-runtime/native-session-executor.ts`):
- OpenCode excludes `<session>/homes` and
`<session>/config/opencode/node_modules`.
- ACPX (every agent) excludes `acpx/<session>/home/.npm` and
`acpx/<session>/data/pnpm/store`, in addition to the existing per-agent
entries.
- `excludeEntries` are matched as exact relative paths (wildcards are
rejected), so the session directory is computed in the same way as the
existing ACPX entries.
- Exposes the two new exports through the server's runner vendor shim.

No code comments were added.

## Verification

- `pnpm exec vitest run src/drivers/opencode
src/drivers/runtime-context-materializer` (in
`packages/paperclip-runner/`): 108 passed. The 1 failure, `uses provider
structure rather than prose length to select a post-tool answer`, also
fails on unmodified `master`.
- The existing skill-tree test, with and without a startup retry, now
asserts that homes are created under `homes/` and that `homes/` is empty
after close.
- New test: a sealed legacy `home-*` at the session root is removed when
the session reopens, and `data/opencode/opencode.db` is kept.
- `pnpm exec vitest run src/services/native-runtime/
src/__tests__/native-harness-backup-expiration.test.ts` (in `server/`):
5080 passed. `plugin-runner-probe` could not run locally because of a
cargo manifest-path problem in this environment; it is unrelated to this
change.
- Profile tests cover the new OpenCode and ACPX exclusions, including a
session id that needs sanitizing.
- New checkpoint/restore round-trip tests for OpenCode and ACPX. They
use `syncRemoteRunnerDirectoryOut` for the backup and
`stageRemoteRunnerDirectory` for the replacement. Excluded caches are
absent from both the backup and the replacement, while history, config
and workspace files are restored and the live directory is unchanged.
- **Mutation checks**, each failing its test:
- removing the skill unsealing (homes leak again; 3 runner tests fail);
  - removing the legacy sweep (1 test fails);
  - emptying the OpenCode/ACPX exclusion lists (4 server tests fail).
- Checked by hand against the bundled OpenCode 1.18.34 binary, with the
driver's environment:
- launching with a fresh HOME and no `config/opencode/node_modules`
still lists earlier sessions;
  - OpenCode reinstalls the plugin dependencies on launch.
- `pnpm run typecheck` in `server/` and `packages/paperclip-runner/`:
pass.
- Not run: server build and boot smoke.

## Risks

- **Rollout:** existing backups still contain legacy `home-*`
directories. After a restore, the sweep removes them when the session
opens, and the next backup no longer carries them.
- **First launch after restore:** OpenCode reinstalls its plugin
dependencies and npm repopulates its cache. This needs network access to
the npm registry, the same as a brand-new session.
- **Crash leftovers:** a HOME left behind by a hard crash (the runner
killed before `close()`) stays under `homes/` until the session
directory is removed. It is still excluded from backups. Homes under
`homes/` are not swept on open, to avoid touching a HOME that a
concurrently running runtime might still use.
- **pnpm store:** dropping the pnpm store from ACPX backups means the
next `pnpm install` in a replacement sandbox downloads packages again.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), 1M context, through Claude Code
with tool use (shell, file edits, tests, and running the real OpenCode
binary to check what state it reads on resume).

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge

Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip authored and GitHub committed 2026-10-25 08:51:39 -07:00
1 parent 7ebf98eafd
commit 34409a92c4
5 files changed
+247 -25

No files matched your search

@@ -6,6 +6,7 @@ import { accessSync, constants, readFileSync, realpathSync, statSync } from "nod
import { createRequire } from "node:module";
import {
chmod,
lstat,
mkdir,
mkdtemp,
readFile,
@@ -85,7 +86,10 @@ import {
type OpenCodeMcpBridge,
} from "./mcp-bridge.js";
import { nativeMcpLaunchBinding } from "../native-mcp.js";
import { materializeNativeRuntimeSkills } from "../runtime-context-materializer.js";
import {
materializeNativeRuntimeSkills,
releaseMaterializedNativeRuntimeSkills,
} from "../runtime-context-materializer.js";
export const OPENCODE_SERVER_DRIVER_KIND = "opencode_server" as const;
export const QUALIFIED_OPENCODE_VERSION = "1.18.34" as const;
@@ -369,6 +373,7 @@ export class OpenCodeServerDriver implements HarnessDriver {
);
await mkdir(root, { recursive: true, mode: 0o700 });
await writeFile(join(root, "workspace"), `${cwd}\n`, { mode: 0o600 });
await removeLegacyIsolatedHomes(root);
const trace = await createProviderTraceFileSink({
path: this.#options.environment?.PAPERCLIP_PROVIDER_TRACE_PATH,
provider: "opencode",
@@ -2281,7 +2286,9 @@ async function startRuntime(input: {
// HOME contains a read-only skill snapshot, whose destination must be fresh.
// Keep OpenCode's XDG data stable for provider-session recovery while giving
// every launch (including retries and recovery) a new isolated HOME.
const isolatedHome = await mkdtemp(join(input.root, "home-"));
const homesRoot = join(input.root, OPENCODE_ISOLATED_HOMES_DIRECTORY);
await mkdir(homesRoot, { recursive: true, mode: 0o700 });
const isolatedHome = await mkdtemp(join(homesRoot, "home-"));
await chmod(isolatedHome, 0o700);
try {
await materializeNativeRuntimeSkills(
@@ -2289,18 +2296,14 @@ async function startRuntime(input: {
join(isolatedHome, ".claude", "skills"),
);
} catch (error) {
await rm(isolatedHome, { recursive: true, force: true }).catch(
() => undefined,
);
await removeIsolatedHome(isolatedHome).catch(() => undefined);
throw error;
}
const bridge = await startOpenCodeMcpBridge({
tools: input.options.dynamicTools,
handler: input.dispatch,
}).catch(async (error) => {
await rm(isolatedHome, { recursive: true, force: true }).catch(
() => undefined,
);
await removeIsolatedHome(isolatedHome).catch(() => undefined);
throw error;
});
const assignedMcp = nativeMcpLaunchBinding(
@@ -2335,7 +2338,7 @@ async function startRuntime(input: {
const providerProxy = modelProvider === "paperclip" && input.options.environment?.PAPERCLIP_AI_PROVIDER_URL
? await startOpenCodeProviderProxy(input.options.environment.PAPERCLIP_AI_PROVIDER_URL, input.options.environment.PAPERCLIP_AI_PROVIDER_KEY ?? "", providerModelId, input.options.environment).catch(async error => {
await bridge.close().catch(() => {});
await rm(isolatedHome, { recursive: true, force: true }).catch(() => {});
await removeIsolatedHome(isolatedHome).catch(() => {});
throw error;
})
: null;
@@ -2568,9 +2571,7 @@ async function startRuntime(input: {
await rm(join(configHome, "opencode", "opencode.json"), {
force: true,
}).catch(() => undefined);
await rm(isolatedHome, { recursive: true, force: true }).catch(
() => undefined,
);
await removeIsolatedHome(isolatedHome).catch(() => undefined);
if (closeInput.finalizeTrace !== false) {
await input.trace?.finish({ reason: closeInput.reason ?? null });
}
@@ -2585,9 +2586,7 @@ async function startRuntime(input: {
await rm(join(configHome, "opencode", "opencode.json"), {
force: true,
}).catch(() => undefined);
await rm(isolatedHome, { recursive: true, force: true }).catch(
() => undefined,
);
await removeIsolatedHome(isolatedHome).catch(() => undefined);
throw error;
}
}
@@ -2962,8 +2961,9 @@ function sanitizedEnvironmentKeys(): string[] {
];
}
function sessionRoot(
runtimeDirectory: string,
export const OPENCODE_ISOLATED_HOMES_DIRECTORY = "homes";
export function opencodeRuntimeSessionDirectoryName(
normalizedSessionId: string,
): string {
const safe = normalizedSessionId
@@ -2971,7 +2971,35 @@ function sessionRoot(
.slice(0, 120);
if (!safe || safe === "." || safe === "..")
throw new Error("Invalid normalized OpenCode session id");
return join(resolve(runtimeDirectory), safe);
return safe;
}
function sessionRoot(
runtimeDirectory: string,
normalizedSessionId: string,
): string {
return join(
resolve(runtimeDirectory),
opencodeRuntimeSessionDirectoryName(normalizedSessionId),
);
}
async function removeIsolatedHome(home: string): Promise<void> {
const skillsParent = await lstat(join(home, ".claude")).catch(() => null);
if (skillsParent?.isDirectory()) {
await releaseMaterializedNativeRuntimeSkills(
join(home, ".claude", "skills"),
).catch(() => undefined);
}
await rm(home, { recursive: true, force: true });
}
async function removeLegacyIsolatedHomes(root: string): Promise<void> {
const entries = await readdir(root, { withFileTypes: true }).catch(() => []);
for (const entry of entries) {
if (!entry.isDirectory() || !entry.name.startsWith("home-")) continue;
await removeIsolatedHome(join(root, entry.name)).catch(() => undefined);
}
}
function validateWorkspace(value: string): string {