Files
PaperClipAI/server/src/__tests__/environment-runtime-reusable-resume-replacement.test.ts
T
DottaandClaude Opus 5.5 4afcbfc0c7 fix(native): retry transient provider refusals while reattaching a handed-off run (#22118)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A release swap hands live native runs from the outgoing server to
the incoming server. The incoming server reattaches each run to its
original sandbox.
> - Reattachment first resumes the exact sandbox lease through the
sandbox provider plugin.
> - In a release-swap wave, one reattach resume got an HTTP 502 from the
provider gateway. The host did not retry it. The run failed with the
generic `setup_failed` code after 13 minutes of agent work.
> - The task then stayed blocked with a Retry banner for about 10
minutes, until the fresh-continuation fallback reached its bound and
started a new run.
> - This pull request retries a transient provider refusal during
reattachment, with a bounded budget. It also reads the provider status
from the plugin RPC error envelope, so the host sees the 502.
> - The benefit is that a brief gateway failure during a swap is
invisible to the user. The same run continues and no Retry banner shows.

## Linked Issues or Issue Description

**What happened?**
During a graceful release swap, the incoming server reattached a live
native run to its retained sandbox. The `environmentResumeLease` plugin
call failed with an HTML "502 Bad Gateway" body 280 ms after the
reattach started. The other 18 handed-off runs in the same swap
reattached in about 8 seconds. The host treated the 502 as an unknown
error, so the run failed with `setup_failed`. The task stayed `blocked`
with `canRetry: true` until the fresh-continuation fallback created a
new run 10 minutes later.

**Expected behavior**
A transient provider refusal (HTTP 408/425/429/5xx, a connection errno,
or a plugin RPC deadline) during reattachment is retried within a
bounded budget. The same run continues hands-off. A definite refusal
(for example HTTP 409) or a confirmed missing sandbox still fails at
once with its own named reason.

**Steps to reproduce**
1. Start a remote native run on a reusable sandbox.
2. Send SIGTERM to the server so it hands the run to the next server.
3. Make the first `environmentResumeLease` call on the incoming server
fail with HTTP 502.
4. See that the run fails with `setup_failed` and the task shows Retry.

**Paperclip version or commit**
`master` at ca34aca777 (release-swap wave) and ea154ce55e (this branch
base).

Root causes:
- Why setup failed: `environmentRunOrchestrator.acquireForRun` called
`resumeRunLease` once for a reattachment. Acquisition already retries
transient resume refusals, but reattachment did not. Also,
`transientSandboxResumeFailure` read `status` and `statusCode` only from
the thrown error. A plugin refusal arrives as `JsonRpcCallError`, which
carries the provider status only in the validated
`paperclip/environment-resume-error/v1` data envelope. So a plugin 502
was not transient even on the acquisition path.
- Why recovery took 10 minutes: `setup_failed` on a native run with
provider work is not a restart-class code, so the same-run reattach lane
does not take it. Failed-workspace preservation recorded a restore hold.
The fresh-continuation fallback waits for that hold up to
`FRESH_CONTINUATION_PENDING_BOUND_MS` (10 minutes) when it sees no
progress. The continuation started at 23:23:40, which is 10 minutes and
2 seconds after the run finished at 23:13:38.
- Why a Retry banner showed: no fresh-continuation check row existed yet
and no system recovery was active, so the presentation fell through to
`failed` with `canRetry: true`. The banner did not stay after the task
was done. The issue rests at `done` with `canRetry: false`. The grader
reported 10482 s because it closed the banner at its final read hours
later. Its last sample that saw the banner was 272 s after the first.

## What Changed

- `environment-run-orchestrator.ts`: the exact-lease resume during
reattachment goes through `retryTransientResumeCall`. The first call
keeps the provider's configured start deadline. A first call that used
up that deadline (plugin RPC TIMEOUT) is not repeated. After a transient
refusal, a 90 s budget (`REUSABLE_SANDBOX_RESUME_BUDGET_MS`) bounds each
later wait and passes the remaining budget as `timeoutMs`. A confirmed
missing sandbox and a definite refusal are not retried.
- `packages/plugins/sdk/src/environment-resume-error.ts`: owns one retry
policy for provider failures: `isRetryableEnvironmentHttpStatus` (408,
425, 429, 500, 502-504 and gateway/edge 520-527, 529, 598, 599; never
501 or 505) and `isRetryableEnvironmentErrorCode(code, scope)`.
ABORT_ERR is never retried. ENOTFOUND is retried only in the
`known_host` scope (an exact reattach to a host this run already
reached, the DNS-outage case). Lease acquisition keeps its canonical
semantics in the `first_acquisition` scope.
- `environment-provider-unavailable-error.ts`: lease acquisition uses
the SDK policy instead of its own sets.
- `reusable-sandbox-resume-replacement.ts`: owns the resume retry helper
and `transientSandboxResumeFailure(error, scope)`, which reads only
structured evidence (status, envelope errno, plugin RPC deadline,
sandbox state transition) and a few transport phrases. The acquisition
resume loop uses the same classifier and backoff helpers, and
`resumeFailureTimedOut` (label or envelope ETIMEDOUT) for its idle-start
extension.
- `environment-runtime.ts`: `EnvironmentDriverResumeInput.timeoutMs`
caps the sandbox plugin driver's resume RPC. Other drivers keep their
own deadline.
- Cloudflare bridge client: a bridge request deadline throws
`CloudflareBridgeTimeoutError` with code `ETIMEDOUT`. Daytona:
`SandboxLivenessTimeoutError` carries code `ETIMEDOUT`. Both reach the
host as a structured errno in the resume envelope.
- `tests/restart-chaos/staging-mix.ts`: `bannerShownMs` counts a visible
Retry banner at most 2 minutes past the last sample that saw it. Before,
it counted the whole unsampled gap until the final read.

Previous-release story: this changes no persisted state, launch
configuration, or pack identity. A run saved by the previous release
reattaches the same way. Only the in-process retry around the resume
call is new.

## Verification

- `pnpm verify` (selected Vitest files), and focused runs of the four
changed server test files and the two restart-chaos test files.
- Red/green: `server/src/__tests__/environment-run-orchestrator.test.ts`
- "retries a provider gateway refusal during reattachment and keeps the
original allocation" fails on master (1 call, rejects with the 502) and
passes with the fix.
- "treats a provider HTTP %i refusal during reattachment as transient"
(500, 502, 520, 599).
- "gives the first reattach resume the provider deadline and bounds
every retry by the budget".
- "does not repeat a reattach resume that used up the provider's start
deadline", "does not retry a definite provider refusal during
reattachment", and "preserves a retained sandbox startup failure without
acquiring a replacement" (default budget, one call).
- `server/src/services/reusable-sandbox-resume-retry.test.ts`: a table
over every classifier branch, and the helper's deadline rules.
- `server/src/__tests__/environment-runtime.test.ts`: "caps the exact
resume call at the caller's remaining budget".
- `server/src/services/environment-provider-unavailable-error.test.ts`:
scope table and the extended status lists. Orchestrator reattach cases:
ENOTFOUND retried, ABORT_ERR and 501 not.
-
`server/src/__tests__/environment-runtime-reusable-resume-replacement.test.ts`:
"applies the idle-start extension to a provider liveness deadline named
only by its errno" (red/green).
- Cloudflare `bridge-client.test.ts` and Daytona `plugin.test.ts`: the
deadline errors carry ETIMEDOUT in the resume envelope.
- Red/green integration:
`server/src/__tests__/heartbeat-native-remote-failure-retention.test.ts`
"keeps reattaching through a gateway refusal during a graceful hand-off
and continues the same run". The run's controller is lost, restart
recovery claims it to reattach, and the first resume fails with the 502
envelope. On master the run fails before the runner step (1 resume
call). With the fix the resume is retried, the same run reaches the
runner step at controller generation 2, no replacement allocation is
acquired, and no second run is created.
- `tests/restart-chaos/staging-mix.test.ts`: banner tests for a late
hide sample and for a banner still open at report time.

## Risks

- The acquisition resume loop no longer retries a refusal whose only
evidence is "timeout" or "timed out" text. Cloudflare and Daytona now
name their own deadlines ETIMEDOUT, and the plugin RPC deadline is
structured, so those stay retried. A provider that reports a timeout
only as text fails at once.
- Lease acquisition now also retries gateway statuses 520-527, 529, 598
and 599.
- A reattach resume can now take the provider deadline plus up to 90 s
when it gets transient refusals. Each retry is the same exact-lease
lifecycle resume. It cannot acquire or replace a sandbox, so a retry
cannot create a second executor.
- The aggregate recovery brake is unchanged. This change adds no new run
starts and no work before `turn.start`.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic. Agentic coding with
tool use (shell, file edits, test runs) in Claude Code.

## 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-17 22:11:04 -06:00

542 lines
33 KiB
TypeScript

import { JsonRpcCallError, environmentResumeErrorData } from "@paperclipai/plugin-sdk";
import { randomUUID } from "node:crypto";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { eq } from "drizzle-orm";
import {
agents,
companies,
createDb,
environmentLeases,
environments,
executionWorkspaceSnapshots,
executionWorkspaces,
heartbeatRuns,
plugins,
projects,
} from "@paperclipai/db";
import type { Environment } from "@paperclipai/shared";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js";
import { environmentRuntimeService } from "../services/environment-runtime.ts";
import { environmentService } from "../services/environments.ts";
import type { PluginWorkerManager } from "../services/plugin-worker-manager.ts";
// Durable harness-backup verification has its own tests. Here it is a boundary:
// a lease-bound stamp verifies, a missing stamp (an older release's lease) does not.
vi.mock("../services/native-runtime/native-harness-backup-storage.js", () => ({
verifyRecoverableNativeHarnessBackup: vi.fn(async (input: { stamp: unknown; providerLeaseId: string }) =>
Boolean(input.stamp && (input.stamp as { authorizedProviderLeaseId?: unknown }).authorizedProviderLeaseId === input.providerLeaseId)),
}));
const externalTestDatabaseUrl = resolveExternalTestDatabaseUrl("PAPERCLIP_ENVIRONMENT_RUNTIME_TEST_DATABASE_URL");
const embeddedPostgresSupport = externalTestDatabaseUrl ? { supported: true } : await getEmbeddedPostgresTestSupport();
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
// The staging failure: Daytona kept the idle sandbox in a state transition past
// its five-minute start deadline, and the worker reported it with no status.
const STAGING_REFUSAL = "Daytona sandbox 0f6c1e52-3d1a-4b7e-9c2a-5e8f7a1b2c3d is still stopping at its start deadline";
describeEmbeddedPostgres("reusable sandbox resume failure replacement", () => {
let stopDb: (() => Promise<void>) | null = null;
let db!: ReturnType<typeof createDb>;
let connectionString: string;
beforeAll(async () => {
if (externalTestDatabaseUrl) connectionString = externalTestDatabaseUrl;
else {
const started = await startEmbeddedPostgresTestDatabase("environment-runtime-resume-replacement");
stopDb = started.cleanup;
connectionString = started.connectionString;
}
});
beforeEach(() => { db = createDb(connectionString); });
afterEach(async () => {
vi.restoreAllMocks();
await db.delete(executionWorkspaceSnapshots);
await db.delete(environmentLeases);
await db.delete(heartbeatRuns);
await db.delete(agents);
await db.delete(environments);
await db.delete(executionWorkspaces);
await db.delete(plugins);
await db.delete(projects);
await db.delete(companies);
await db.$client.end({ timeout: 1 });
});
afterAll(async () => { await stopDb?.(); });
type Shape = {
leaseStatus?: "retained" | "released";
previousRunStatus?: "succeeded" | "running";
dirty?: boolean;
snapshot?: "committed" | "pending" | null;
backupStamp?: boolean;
};
async function seed(shape: Shape = {}) {
const companyId = randomUUID();
const agentId = randomUUID();
const environmentId = randomUUID();
const pluginId = randomUUID();
const previousRunId = randomUUID();
const runId = randomUUID();
const projectId = randomUUID();
const executionWorkspaceId = randomUUID();
const providerLeaseId = "0f6c1e52-3d1a-4b7e-9c2a-5e8f7a1b2c3d";
// The staging environment's provider settings, including its 300s start deadline.
const providerConfig = { provider: "fake-plugin", image: "fake:test", timeoutMs: 300_000, reuseLease: true };
await db.insert(companies).values({ id: companyId, name: "Acme", status: "active" });
await db.insert(agents).values({ id: agentId, companyId, name: "Coder", role: "engineer", status: "active",
adapterType: "paperclip_runner", adapterConfig: {}, runtimeConfig: {}, permissions: {} });
await db.insert(environments).values({ id: environmentId, name: "Paperclip Sandbox", driver: "sandbox", status: "active",
config: providerConfig });
await db.insert(plugins).values({
id: pluginId, pluginKey: "acme.reusable-sandbox-provider", packageName: "@acme/reusable-sandbox-provider",
version: "1.0.0", apiVersion: 1, categories: ["automation"], status: "ready", installOrder: 1, updatedAt: new Date(),
manifestJson: {
id: "acme.reusable-sandbox-provider", apiVersion: 1, version: "1.0.0", displayName: "Reusable Sandbox Provider",
description: "Test provider", author: "Paperclip", categories: ["automation"],
capabilities: ["environment.drivers.register"], entrypoints: { worker: "dist/worker.js" },
environmentDrivers: [{ driverKey: "fake-plugin", kind: "sandbox_provider", displayName: "Fake Plugin",
supportsReusableLeases: true,
configSchema: { type: "object", properties: { image: { type: "string" }, timeoutMs: { type: "number" }, reuseLease: { type: "boolean" } } } }],
},
} as never);
await db.insert(projects).values({ id: projectId, companyId, name: "Project", status: "active" });
await db.insert(executionWorkspaces).values({ id: executionWorkspaceId, companyId, projectId, mode: "isolated_workspace",
strategyType: "project_primary", name: "Task workspace", status: "active", providerType: "cloud_sandbox",
cwd: `/workspaces/${executionWorkspaceId}` });
await db.insert(heartbeatRuns).values([
{ id: previousRunId, companyId, agentId, invocationSource: "assignment", status: "running" },
{ id: runId, companyId, agentId, invocationSource: "assignment", status: "running" },
]);
const environment: Environment = { id: environmentId, name: "Paperclip Sandbox", description: null, driver: "sandbox",
status: "active", config: providerConfig, envVars: {}, metadata: null, createdAt: new Date(), updatedAt: new Date() };
// The previous run creates the sandbox through the real acquisition path,
// so the preserved lease carries exactly the identity production records.
const priming = environmentRuntimeService(db, { pluginWorkerManager: {
isRunning: () => true,
call: async (_pluginId: string, method: string) => {
if (method !== "environmentAcquireLease") throw new Error(`Unexpected plugin method: ${method}`);
return { providerLeaseId, metadata: { ...providerConfig, sandboxId: providerLeaseId, remoteCwd: "/home/daytona/paperclip-workspace" } };
},
getWorker: () => ({ supportedMethods: ["environmentResumeLease", "environmentReleaseLease", "environmentDestroyLease"] }),
} as unknown as PluginWorkerManager });
const created = await priming.acquireRunLease({ companyId, environment, issueId: null, agentId, adapterType: "paperclip_runner",
heartbeatRunId: previousRunId, persistedExecutionWorkspace: { id: executionWorkspaceId, mode: "isolated_workspace" } });
// The run then ended after a clean final save and left its lease-bound
// harness backup stamp. A lease saved by an older release has no
// acquisition outcome and may predate the backup stamp.
const { sandboxLeaseAcquisition: _outcome, ...metadata } = created.lease.metadata ?? {};
const lease = (await environmentService(db).updateLeaseMetadata(created.lease.id, {
...(shape.leaseStatus === "released" ? metadata : created.lease.metadata),
remoteWorkspaceId: executionWorkspaceId,
...(shape.backupStamp === false ? {} : { nativeHarnessBackup: {
schema: "paperclip.native-harness-backup-stamp.v2", sourceProviderLeaseId: providerLeaseId,
authorizedProviderLeaseId: providerLeaseId,
} }),
}))!;
await db.update(environmentLeases).set({ status: shape.leaseStatus ?? "retained" }).where(eq(environmentLeases.id, lease.id));
await db.update(heartbeatRuns).set({ status: shape.previousRunStatus ?? "succeeded" }).where(eq(heartbeatRuns.id, previousRunId));
const snapshotId = randomUUID();
if (shape.snapshot !== null) {
await db.insert(executionWorkspaceSnapshots).values({ id: snapshotId, companyId, executionWorkspaceId,
sourceOperationId: randomUUID(), sourceRunId: previousRunId, generation: 1, epoch: 1, status: shape.snapshot ?? "committed" });
}
// This run already holds the workspace writer slot (reserved before the
// environment is acquired); the saved version is the previous run's save.
await db.update(executionWorkspaces).set({ metadata: { remoteWorkspace: {
schema: "paperclip.remote-workspace.v1", epoch: 2, snapshotId: shape.snapshot === null ? null : snapshotId,
source: { kind: "empty" }, writerRunId: runId, dirty: shape.dirty ?? false,
location: { environmentId, leaseId: lease.id, providerLeaseId, remoteCwd: "/home/daytona/paperclip-workspace/.paperclip-workspaces/w" },
} } }).where(eq(executionWorkspaces.id, executionWorkspaceId));
return { companyId, agentId, environment, pluginId, runId, executionWorkspaceId, lease, providerLeaseId };
}
function worker(seeded: Awaited<ReturnType<typeof seed>>, resume: (attempt: number, timeoutMs: number) => Promise<unknown>,
options: { acquireFails?: boolean } = {}) {
let attempts = 0;
const call = vi.fn(async (_pluginId: string, method: string, _params: unknown, timeoutMs?: number) => {
if (method === "environmentResumeLease") return await resume(++attempts, timeoutMs ?? 0);
if (method === "environmentAcquireLease") {
if (options.acquireFails) throw new JsonRpcCallError({ code: -32000, message: "create refused" });
return { providerLeaseId: "replacement-sandbox", metadata: { provider: "fake-plugin", image: "fake:test",
timeoutMs: 300_000, reuseLease: true, remoteCwd: "/home/daytona/paperclip-workspace" } };
}
throw new Error(`Unexpected plugin method: ${method}`);
});
const manager = { isRunning: vi.fn((id: string) => id === seeded.pluginId), call,
getWorker: vi.fn(() => ({ supportedMethods: ["environmentResumeLease", "environmentReleaseLease", "environmentDestroyLease"] })),
} as unknown as PluginWorkerManager;
return { call, runtime: environmentRuntimeService(db, { pluginWorkerManager: manager }) };
}
function acquire(seeded: Awaited<ReturnType<typeof seed>>, runtime: ReturnType<typeof environmentRuntimeService>,
runId = seeded.runId) {
return runtime.acquireRunLease({ companyId: seeded.companyId, environment: seeded.environment, issueId: null,
agentId: seeded.agentId, adapterType: "paperclip_runner", heartbeatRunId: runId,
persistedExecutionWorkspace: { id: seeded.executionWorkspaceId, mode: "isolated_workspace" } });
}
// Each refused resume consumes 40s of the run's clock, so the in-run retry
// budget is exercised without real waiting.
function stepClock() {
let clock = Date.now();
vi.spyOn(Date, "now").mockImplementation(() => clock);
return () => { clock += 40_000; };
}
it("replaces a clean, saved sandbox that keeps refusing to resume, within a bounded budget (staging shape)", async () => {
const seeded = await seed();
const step = stepClock();
const { call, runtime } = worker(seeded, async () => {
step();
throw new JsonRpcCallError({ code: -32000, message: STAGING_REFUSAL });
});
const acquired = await acquire(seeded, runtime);
expect(acquired.lease.providerLeaseId).toBe("replacement-sandbox");
expect(acquired.lease.metadata?.sandboxLeaseAcquisition).toEqual({ outcome: "replacement", reason: "resume_failed" });
const resumes = call.mock.calls.filter(([, method]) => method === "environmentResumeLease");
// Retried inside the run, then stopped well short of the five-minute deadline.
expect(resumes.length).toBeGreaterThan(1);
for (const [, , , timeoutMs] of resumes) expect(timeoutMs).toBeLessThanOrEqual(90_000);
// The old sandbox is handed to the cleanup sweep; no synchronous destroy
// delays the first turn.
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentDestroyLease", expect.anything(), expect.anything());
await expect(environmentService(db).getLeaseById(seeded.lease.id)).resolves.toMatchObject({
status: "pending_cleanup", cleanupStatus: "failed", failureReason: "resume_failed",
});
});
it("keeps the preserved lease when the replacement cannot be created, so the next attempt replaces it", async () => {
const seeded = await seed();
const refuse = async () => { throw new JsonRpcCallError({ code: -32000, message: STAGING_REFUSAL }); };
const step = stepClock();
const failing = worker(seeded, async () => { step(); return await refuse(); }, { acquireFails: true });
await expect(acquire(seeded, failing.runtime)).rejects.toThrow();
await expect(environmentService(db).getLeaseById(seeded.lease.id)).resolves.toMatchObject({
status: "retained", providerLeaseId: seeded.providerLeaseId,
});
// The bounded retry runs as a new run that reserves the same clean workspace.
const retryRunId = randomUUID();
await db.insert(heartbeatRuns).values({ id: retryRunId, companyId: seeded.companyId, agentId: seeded.agentId,
invocationSource: "assignment", status: "running" });
await db.update(heartbeatRuns).set({ status: "failed" }).where(eq(heartbeatRuns.id, seeded.runId));
const [workspace] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, seeded.executionWorkspaceId));
await db.update(executionWorkspaces).set({ metadata: { ...workspace!.metadata,
remoteWorkspace: { ...(workspace!.metadata!.remoteWorkspace as Record<string, unknown>), writerRunId: retryRunId } } })
.where(eq(executionWorkspaces.id, seeded.executionWorkspaceId));
const retry = worker(seeded, async () => { step(); return await refuse(); });
const acquired = await acquire(seeded, retry.runtime, retryRunId);
expect(acquired.lease.metadata?.sandboxLeaseAcquisition).toEqual({ outcome: "replacement", reason: "resume_failed" });
// The replacement carries the verified harness backup, so the session restores
// without waiting for the old sandbox to be deleted.
expect(acquired.lease.metadata?.nativeHarnessBackup).toMatchObject({ authorizedProviderLeaseId: seeded.providerLeaseId });
await expect(environmentService(db).getLeaseById(seeded.lease.id)).resolves.toMatchObject({ status: "pending_cleanup" });
});
it("replaces a released lease saved by an older release in the same way", async () => {
const seeded = await seed({ leaseStatus: "released" });
const step = stepClock();
const { runtime } = worker(seeded, async () => {
step();
throw new JsonRpcCallError({ code: -32000, message: "Sandbox state change in progress",
data: environmentResumeErrorData(Object.assign(new Error("private"), { statusCode: 409 })) });
});
const acquired = await acquire(seeded, runtime);
expect(acquired.lease.metadata?.sandboxLeaseAcquisition).toEqual({ outcome: "replacement", reason: "resume_failed" });
});
// Staging: a run could not retire its predecessor's warm
// Runner on this sandbox, did no provider work, and asked for a fresh one.
async function markReplaceBeforeReuse(seeded: Awaited<ReturnType<typeof seed>>) {
await environmentService(db).updateLeaseMetadata(seeded.lease.id, { ...seeded.lease.metadata,
replaceBeforeReuse: { schema: "paperclip.replace-before-reuse.v1", runId: randomUUID(), predecessorRunId: randomUUID(),
recordedAt: new Date().toISOString() } });
}
it("replaces a resumable sandbox whose previous Runner could not be retired, without resuming it", async () => {
const seeded = await seed();
await markReplaceBeforeReuse(seeded);
const { call, runtime } = worker(seeded, async () => ({ providerLeaseId: seeded.providerLeaseId,
metadata: { remoteCwd: "/home/daytona/paperclip-workspace" } }));
const acquired = await acquire(seeded, runtime);
expect(acquired.lease.providerLeaseId).toBe("replacement-sandbox");
expect(acquired.lease.metadata?.sandboxLeaseAcquisition).toEqual({ outcome: "replacement", reason: "resume_failed",
cause: "native_predecessor_retirement_unproven" });
// The new sandbox restores the saved session from the verified backup.
expect(acquired.lease.metadata?.nativeHarnessBackup).toMatchObject({ authorizedProviderLeaseId: seeded.providerLeaseId });
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentResumeLease", expect.anything(), expect.anything());
// The old sandbox, with the Runner on it, is kept for the cleanup sweep.
await expect(environmentService(db).getLeaseById(seeded.lease.id)).resolves.toMatchObject({ status: "pending_cleanup" });
});
it.each([
["unsaved work on the sandbox", { dirty: true }],
["no verified harness backup", { backupStamp: false }],
] as Array<[string, Shape]>)("resumes a sandbox marked for replacement as before when it holds %s", async (_label, shape) => {
const seeded = await seed(shape);
await markReplaceBeforeReuse(seeded);
const { runtime } = worker(seeded, async () => ({ providerLeaseId: seeded.providerLeaseId,
metadata: { remoteCwd: "/home/daytona/paperclip-workspace" } }));
const acquired = await acquire(seeded, runtime);
expect(acquired.lease.providerLeaseId).toBe(seeded.providerLeaseId);
expect(acquired.lease.metadata?.sandboxLeaseAcquisition).toEqual({ outcome: "resumed" });
});
it("retries a sandbox state change inside the run and resumes the same sandbox", async () => {
const seeded = await seed();
const { call, runtime } = worker(seeded, async (attempt) => {
if (attempt === 1) throw new JsonRpcCallError({ code: -32000, message: "Sandbox state change in progress" });
return { providerLeaseId: seeded.providerLeaseId, metadata: { remoteCwd: "/home/daytona/paperclip-workspace" } };
});
const acquired = await acquire(seeded, runtime);
expect(acquired.lease.providerLeaseId).toBe(seeded.providerLeaseId);
expect(acquired.lease.metadata?.sandboxLeaseAcquisition).toEqual({ outcome: "resumed" });
expect(call.mock.calls.filter(([, method]) => method === "environmentResumeLease")).toHaveLength(2);
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentAcquireLease", expect.anything(), expect.anything());
});
it.each([
["unsaved work on the sandbox", { dirty: true }, undefined],
["no saved workspace version", { snapshot: null }, undefined],
["a saved version that never committed", { snapshot: "pending" }, undefined],
["a live owner", { previousRunStatus: "running" }, undefined],
["no verified harness backup (older lease)", { backupStamp: false }, undefined],
["a provider access refusal", {}, 403],
] as Array<[string, Shape, number | undefined]>)("keeps the sandbox and fails strictly with %s", async (_label, shape, status) => {
const seeded = await seed(shape);
const step = stepClock();
const { call, runtime } = worker(seeded, async () => {
step();
throw new JsonRpcCallError({ code: -32000, message: STAGING_REFUSAL,
...(status ? { data: environmentResumeErrorData(Object.assign(new Error("private"), { statusCode: status })) } : {}) });
});
await expect(acquire(seeded, runtime)).rejects.toThrow(
/could not be resumed \(cause=sandbox_state_transition( status=403)?\); the lease was preserved and no replacement was created\.$/,
);
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentAcquireLease", expect.anything(), expect.anything());
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentDestroyLease", expect.anything(), expect.anything());
await expect(environmentService(db).getLeaseById(seeded.lease.id)).resolves.toMatchObject({
status: shape.leaseStatus ?? "retained", providerLeaseId: seeded.providerLeaseId,
});
});
function rpcTimeoutClock() {
let clock = Date.now();
const startedAt = clock;
vi.spyOn(Date, "now").mockImplementation(() => clock);
return {
elapsed: () => clock - startedAt,
timeOut: (timeoutMs: number) => {
clock += timeoutMs;
return new JsonRpcCallError({ code: -32003,
message: `RPC call "environmentResumeLease" timed out after ${timeoutMs}ms` });
},
};
}
it("keeps waiting for an idle sandbox whose resume timed out and resumes it in the same run (staging shape)", async () => {
const seeded = await seed({ leaseStatus: "released", dirty: true });
const clock = rpcTimeoutClock();
const { call, runtime } = worker(seeded, async (attempt, timeoutMs) => {
if (attempt === 1) throw clock.timeOut(timeoutMs);
return { providerLeaseId: seeded.providerLeaseId, metadata: { remoteCwd: "/home/daytona/paperclip-workspace" } };
});
const acquired = await acquire(seeded, runtime);
expect(acquired.lease.providerLeaseId).toBe(seeded.providerLeaseId);
expect(acquired.lease.metadata?.sandboxLeaseAcquisition).toEqual({ outcome: "resumed" });
const resumes = call.mock.calls.filter(([, method]) => method === "environmentResumeLease");
expect(resumes).toHaveLength(2);
for (const [, , , timeoutMs] of resumes) expect(timeoutMs).toBeLessThanOrEqual(90_000);
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentAcquireLease", expect.anything(), expect.anything());
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentDestroyLease", expect.anything(), expect.anything());
});
it("applies the idle-start extension to a provider liveness deadline named only by its errno", async () => {
const seeded = await seed({ leaseStatus: "released", dirty: true });
let clock = Date.now();
vi.spyOn(Date, "now").mockImplementation(() => clock);
const { call, runtime } = worker(seeded, async (attempt, timeoutMs) => {
if (attempt === 1) {
clock += timeoutMs;
throw new JsonRpcCallError({ code: -32000,
message: `Daytona sandbox liveness call "sandbox.refreshData" did not respond within ${timeoutMs} ms; the sandbox connection is unresponsive.`,
data: environmentResumeErrorData(Object.assign(new Error("private"), { code: "ETIMEDOUT" })) });
}
return { providerLeaseId: seeded.providerLeaseId, metadata: { remoteCwd: "/home/daytona/paperclip-workspace" } };
});
const acquired = await acquire(seeded, runtime);
expect(acquired.lease.providerLeaseId).toBe(seeded.providerLeaseId);
expect(call.mock.calls.filter(([, method]) => method === "environmentResumeLease")).toHaveLength(2);
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentAcquireLease", expect.anything(), expect.anything());
});
it("waits out the in-flight resume when a retry finds the stopped sandbox still settling", async () => {
const seeded = await seed({ leaseStatus: "released", dirty: true });
const clock = rpcTimeoutClock();
const { call, runtime } = worker(seeded, async (attempt, timeoutMs) => {
if (attempt === 1) throw clock.timeOut(timeoutMs);
if (attempt === 2) throw new JsonRpcCallError({ code: -32000,
message: "The stopped Daytona sandbox is still settling cancelled work. Retry shortly." });
return { providerLeaseId: seeded.providerLeaseId, metadata: { remoteCwd: "/home/daytona/paperclip-workspace" } };
});
const acquired = await acquire(seeded, runtime);
expect(acquired.lease.providerLeaseId).toBe(seeded.providerLeaseId);
expect(acquired.lease.metadata?.sandboxLeaseAcquisition).toEqual({ outcome: "resumed" });
expect(call.mock.calls.filter(([, method]) => method === "environmentResumeLease")).toHaveLength(3);
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentAcquireLease", expect.anything(), expect.anything());
});
it("stops waiting at the provider start deadline and keeps the sandbox when every resume times out", async () => {
const seeded = await seed({ leaseStatus: "released", dirty: true });
const clock = rpcTimeoutClock();
const { call, runtime } = worker(seeded, async (_attempt, timeoutMs) => { throw clock.timeOut(timeoutMs); });
await expect(acquire(seeded, runtime)).rejects.toThrow(
/could not be resumed \(cause=transport_timeout\); the lease was preserved and no replacement was created\.$/,
);
expect(call.mock.calls.filter(([, method]) => method === "environmentResumeLease").length).toBeGreaterThan(1);
expect(clock.elapsed()).toBeLessThanOrEqual(330_000);
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentAcquireLease", expect.anything(), expect.anything());
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentDestroyLease", expect.anything(), expect.anything());
await expect(environmentService(db).getLeaseById(seeded.lease.id)).resolves.toMatchObject({
status: "released", providerLeaseId: seeded.providerLeaseId,
});
});
it("continues on a new sandbox and keeps the old one when an idle sandbox never starts and has no harness backup (staging shape)", async () => {
const seeded = await seed({ leaseStatus: "released", backupStamp: false });
const clock = rpcTimeoutClock();
const { call, runtime } = worker(seeded, async (_attempt, timeoutMs) => { throw clock.timeOut(timeoutMs); });
const acquired = await acquire(seeded, runtime);
expect(acquired.lease.providerLeaseId).toBe("replacement-sandbox");
expect(acquired.lease.metadata?.sandboxLeaseAcquisition).toEqual({ outcome: "replacement", reason: "resume_failed" });
expect(acquired.lease.metadata?.nativeHarnessBackup).toBeUndefined();
expect(call.mock.calls.filter(([, method]) => method === "environmentResumeLease").length).toBeGreaterThan(1);
expect(clock.elapsed()).toBeLessThanOrEqual(330_000);
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentDestroyLease", expect.anything(), expect.anything());
const kept = await environmentService(db).getLeaseById(seeded.lease.id);
expect(kept).toMatchObject({ status: "released", providerLeaseId: seeded.providerLeaseId, cleanupStatus: null });
expect(kept?.metadata?.reusableSandboxKeptAfterReplacement).toMatchObject({
runId: seeded.runId, replacementLeaseId: acquired.lease.id,
});
const nextRunId = randomUUID();
await db.insert(heartbeatRuns).values({ id: nextRunId, companyId: seeded.companyId, agentId: seeded.agentId,
invocationSource: "assignment", status: "running" });
await environmentService(db).releaseLease(acquired.lease.id, "released");
await db.update(heartbeatRuns).set({ status: "succeeded" }).where(eq(heartbeatRuns.id, seeded.runId));
const next = worker(seeded, async () => ({ providerLeaseId: "replacement-sandbox",
metadata: { remoteCwd: "/home/daytona/paperclip-workspace" } }));
const resumed = await acquire(seeded, next.runtime, nextRunId);
expect(resumed.lease.providerLeaseId).toBe("replacement-sandbox");
const nextResumes = next.call.mock.calls.filter(([, method]) => method === "environmentResumeLease");
expect(nextResumes).toHaveLength(1);
expect(nextResumes[0]![2]).toMatchObject({ providerLeaseId: "replacement-sandbox" });
await expect(environmentService(db).getLeaseById(seeded.lease.id)).resolves.toMatchObject({
status: "released", providerLeaseId: seeded.providerLeaseId,
});
});
it("never destroys a sandbox kept after replacement through the run or workspace teardown paths", async () => {
const seeded = await seed({ leaseStatus: "released", backupStamp: false });
const clock = rpcTimeoutClock();
const { call, runtime } = worker(seeded, async (_attempt, timeoutMs) => { throw clock.timeOut(timeoutMs); });
const acquired = await acquire(seeded, runtime);
await environmentService(db).releaseLease(acquired.lease.id, "released");
await db.update(heartbeatRuns).set({ status: "succeeded" }).where(eq(heartbeatRuns.id, seeded.runId));
const kept = (await environmentService(db).getLeaseById(seeded.lease.id))!;
await expect(runtime.destroyRunLease({ environment: seeded.environment, lease: kept })).resolves.toBeNull();
const destroyed = await runtime.destroyReusableSandboxLeases({ companyId: seeded.companyId,
executionWorkspaceId: seeded.executionWorkspaceId });
expect(destroyed.map(record => record.lease.id)).toEqual([acquired.lease.id]);
expect(call.mock.calls.filter(([, method, params]) => method === "environmentDestroyLease"
&& (params as { providerLeaseId?: string }).providerLeaseId === seeded.providerLeaseId)).toHaveLength(0);
await expect(environmentService(db).getLeaseById(seeded.lease.id)).resolves.toMatchObject({
status: "released", providerLeaseId: seeded.providerLeaseId,
});
});
it("destroys a sandbox kept after replacement when an operator approves the environment delete", async () => {
const seeded = await seed({ leaseStatus: "released", backupStamp: false });
const clock = rpcTimeoutClock();
const { runtime } = worker(seeded, async (_attempt, timeoutMs) => { throw clock.timeOut(timeoutMs); });
const acquired = await acquire(seeded, runtime);
await environmentService(db).releaseLease(acquired.lease.id, "released");
await db.update(heartbeatRuns).set({ status: "succeeded" }).where(eq(heartbeatRuns.id, seeded.runId));
const teardown = worker(seeded, async () => { throw new Error("no resume during delete"); });
teardown.call.mockImplementation(async (_pluginId: string, method: string, params: unknown) => {
if (method === "environmentDestroyLease") return { providerLeaseId: (params as { providerLeaseId: string }).providerLeaseId };
throw new Error(`Unexpected plugin method: ${method}`);
});
const result = await teardown.runtime.destroyReusableSandboxLeasesForEnvironment({
environmentId: seeded.environment.id, consentedBy: "operator" });
expect(result).toMatchObject({ destroyed: 2, failed: 0 });
expect(teardown.call.mock.calls.filter(([, method, params]) => method === "environmentDestroyLease"
&& (params as { providerLeaseId?: string }).providerLeaseId === seeded.providerLeaseId)).toHaveLength(1);
const deleted = await environmentService(db).getLeaseById(seeded.lease.id);
expect(deleted?.status).not.toBe("pending_cleanup");
expect(deleted?.metadata?.reusableSandboxKeptAfterReplacement).toBeUndefined();
});
it("keeps an idle sandbox with no harness backup when some resume calls failed for another reason", async () => {
const seeded = await seed({ leaseStatus: "released", backupStamp: false });
const clock = rpcTimeoutClock();
const { call, runtime } = worker(seeded, async (attempt, timeoutMs) => {
if (attempt === 1) throw clock.timeOut(timeoutMs);
throw Object.assign(clock.timeOut(timeoutMs), { message: "connection reset by peer (ECONNRESET)" });
});
await expect(acquire(seeded, runtime)).rejects.toThrow(/the lease was preserved and no replacement was created\.$/);
expect(call.mock.calls.filter(([, method]) => method === "environmentResumeLease").length).toBeGreaterThan(1);
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentAcquireLease", expect.anything(), expect.anything());
const kept = await environmentService(db).getLeaseById(seeded.lease.id);
expect(kept?.metadata?.reusableSandboxKeptAfterReplacement).toBeUndefined();
});
it.each([
["unsaved work on the sandbox", { dirty: true }],
["no saved workspace version", { snapshot: null }],
] as Array<[string, Shape]>)("keeps an idle sandbox with no harness backup that never starts and fails strictly with %s", async (_label, shape) => {
const seeded = await seed({ leaseStatus: "released", backupStamp: false, ...shape });
const clock = rpcTimeoutClock();
const { call, runtime } = worker(seeded, async (_attempt, timeoutMs) => { throw clock.timeOut(timeoutMs); });
await expect(acquire(seeded, runtime)).rejects.toThrow(
/could not be resumed \(cause=transport_timeout\); the lease was preserved and no replacement was created\.$/,
);
expect(call).not.toHaveBeenCalledWith(seeded.pluginId, "environmentAcquireLease", expect.anything(), expect.anything());
const kept = await environmentService(db).getLeaseById(seeded.lease.id);
expect(kept).toMatchObject({ status: "released", providerLeaseId: seeded.providerLeaseId });
expect(kept?.metadata?.reusableSandboxKeptAfterReplacement).toBeUndefined();
});
it("does not extend the wait for a sandbox that a live run still holds", async () => {
const seeded = await seed({ previousRunStatus: "running", dirty: true });
const clock = rpcTimeoutClock();
const { call, runtime } = worker(seeded, async (_attempt, timeoutMs) => { throw clock.timeOut(timeoutMs); });
await expect(acquire(seeded, runtime)).rejects.toThrow(/could not be resumed \(cause=transport_timeout\)/);
expect(call.mock.calls.filter(([, method]) => method === "environmentResumeLease")).toHaveLength(1);
expect(clock.elapsed()).toBeLessThanOrEqual(90_000);
});
});