Files
PaperClipAI/server/src/services/native-runtime/native-provider-artifact-capture.test.ts
T
DottaandClaude Opus 5.5 24cbea136f fix(native): export OpenCode launch artifacts through a confined workspace copy (#19743)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Native recovery saves the qualified OpenCode launch files so a cold
allocation can resume the same session (#19622)
> - Core reads those files from the sandbox with the provider's bulk
`syncOut`
> - The launch root is under the sandbox account home, not under the
workspace remote dir
> - Daytona, Kubernetes, and CreateOS confine every export source to the
workspace remote dir, so the export always fails
> - Every remote OpenCode run on a durable-storage instance now fails
40-90 s after start with `native_session_interrupted`
> - This pull request exports a confined scratch copy of the three
launch files from inside the workspace runtime dir
> - The benefit is that remote OpenCode runs work again, and the
provider confinement guard stays unchanged

## Linked Issues or Issue Description

Refs #19622

**What happened?**

After #19622, every remote OpenCode run on Daytona fails at provider
admission. The run error is `Daytona sync source path escapes the
workspace remote dir:
/home/daytona/.paperclip-runtime/paperclip-runner/provider-artifacts/<digest>`.
The error code is `native_session_interrupted`. The last successful run
of the affected agent was immediately before #19622 reached the
deployment.

**Expected behavior**

Core saves the qualified launch files and the run continues. The
provider confinement guard still refuses any export source outside the
workspace remote dir.

**Steps to reproduce**

1. Configure an OpenCode agent on a Daytona environment with durable
harness storage.
2. Start a run.
3. The run fails when Core captures the provider artifacts, before the
first turn.

## What Changed

- `captureRemoteNativeProviderArtifacts` now takes the workspace
`remoteCwd`.
- Before the export, a sandbox command copies the three launch files
into a new scratch directory at
`<remoteCwd>/.paperclip-runtime/paperclip-runner/provider-artifact-export/<uuid>/`.
The command refuses a symlink or a non-regular file. It uses `cp -P`, so
it never follows a link.
- `syncOut` reads from that scratch copy. A `finally` step removes the
scratch copy after success or failure. A failed removal is logged and
does not replace the transfer result.
- The launch root does not move. OpenCode still binds to the same
absolute paths. Core still verifies each downloaded byte against the
manifest SHA-256 values.
- `.paperclip-runtime` is already excluded from workspace sync, so the
scratch copy never enters the user workspace.
- The Daytona `assertConfinedSandboxPath` guard is not changed.

## Verification

- New regression
`server/src/services/native-runtime/native-provider-artifact-capture-confinement.test.ts`.
It runs the real sandbox commands on a local directory tree that has a
home-rooted launch root and a separate workspace. Its `syncOut` applies
the same workspace confinement rule as the Daytona provider.
- Before the fix: it fails with `Daytona sync source path escapes the
workspace remote dir:
.../home/daytona/.paperclip-runtime/paperclip-runner/provider-artifacts/dfe9e4b1d660/bin/nodeCommand`.
This is the staging error.
- After the fix: it passes. The published bytes match, every export
source is under the workspace runtime dir, and the scratch directory is
empty after the capture.
- Safety cases: a failed transfer removes the scratch copy and does not
publish. A launch file replaced by a symlink is refused before any
export, and nothing is published.
- `native-session-executor.test.ts`: the durable OpenCode admission case
now asserts that the capture receives `remoteCwd` and that every
`syncOut` source is under `/workspace/`.
- `pnpm vitest run` for the capture, confinement, remote OpenCode
artifact, and session root entries tests: 14 passed.
- `vitest run native-session-executor.test.ts -t "opencode|provider
artifact|durable"`: 62 passed.
- `pnpm --filter @paperclipai/server typecheck`: passed.
- Not run: a live Daytona run with this revision. The fix is not
deployed to staging.

## Risks

- Low risk. The capture now runs two short `sh` commands in the sandbox
and holds a second copy of the launch files during the transfer. The
files are bounded by the existing 512 MiB limit and are deleted
afterwards.
- The scratch copy uses `cp -P`, `mkdir`, and `rm -rf` from POSIX `sh`.
The Daytona image already uses these tools for the workspace sync.
- If the sandbox stops between the copy and the removal, a scratch
directory can remain under `.paperclip-runtime`. Workspace sync excludes
that directory.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, file edit, and read-only browser access to
staging).

## 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
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 00:04:21 -06:00

121 lines
3.7 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { stat, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { afterEach, expect, it, vi } from "vitest";
import type { CommandManagedRuntimeRunner } from "@paperclipai/adapter-utils/command-managed-runtime";
import type { NativeExecutionInput } from "../../vendor/paperclip-runner/index.js";
import type { NativeProviderArtifactSource } from "./native-provider-artifacts.js";
import { NativeExecutionContext } from "./native-execution-context.js";
import { captureRemoteNativeProviderArtifacts } from "./native-provider-artifact-capture.js";
afterEach(() => vi.useRealTimers());
it.each(["stop", "deadline"])(
"bounds %s while retaining the actual late transfer and scratch owner",
async (mode) => {
vi.useFakeTimers();
const context = new NativeExecutionContext(
{
binding: {
companyId: randomUUID(),
runId: randomUUID(),
agentId: randomUUID(),
},
} as NativeExecutionInput,
1_000,
);
let release!: () => void,
entered!: () => void,
root = "",
outcome = "pending";
const gate = new Promise<void>((done) => {
release = done;
}),
started = new Promise<void>((done) => {
entered = done;
});
const runner = {
// The confined export staging and its cleanup run as sandbox commands.
execute: vi.fn(async () => ({
exitCode: 0,
signal: null,
timedOut: false,
stdout: "",
stderr: "",
pid: null,
startedAt: null,
})),
syncOut: async (operations) => {
const destination = operations[0].files[0].targetPath;
root = dirname(destination);
await writeFile(destination, "partial qualified download");
entered();
await gate;
await writeFile(destination, "late qualified download finished");
return { operations: [] };
},
} as CommandManagedRuntimeRunner;
const manifest = {
payload: {
artifacts: Object.fromEntries(
["nodeCommand", "opencodeProxy", "opencodeExecutable"].map((name) => [
name,
{ path: name },
]),
),
},
} as NativeProviderArtifactSource["manifest"];
const publish = vi.fn(async () => "must not publish");
const result = captureRemoteNativeProviderArtifacts(
{
runner,
remoteCwd: "/workspace",
manifest,
context,
signal: context.signal,
launch: {
root: "/owned/qualified",
accountUid: 1000,
paths: {
nodeCommand: "/qualified/node",
opencodeProxy: "/qualified/proxy",
opencodeExecutable: "/qualified/opencode",
},
},
},
publish,
).then(
() => {
outcome = "published";
},
(error) => {
outcome = error.message;
},
);
try {
await started;
if (mode === "stop") context.abort(new Error("controlled Stop"));
await vi.advanceTimersByTimeAsync(mode === "deadline" ? 1_000 : 0);
expect(outcome).toBe(
mode === "stop"
? "controlled Stop"
: "native_execution_startup_expired",
);
expect(context.pending).toBe(true);
expect((await stat(root)).isDirectory()).toBe(true);
expect(publish).not.toHaveBeenCalled();
} finally {
release();
await result;
vi.useRealTimers();
await vi.waitFor(() => expect(context.pending).toBe(false), {
timeout: 1_000,
interval: 5,
});
context.finish();
}
expect(context.pending).toBe(false);
await expect(stat(root)).rejects.toMatchObject({ code: "ENOENT" });
expect(publish).not.toHaveBeenCalled();
},
);