Files
PaperClipAI/server/src/services/environment-run-orchestrator.ts
T
DottaandClaude Opus 5.5 a665ed6a1b fix(recovery): wait out a sandbox provider outage with no Retry (#22139)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A task that runs in a sandbox environment first gets a lease from
the environment provider (for example Daytona)
> - When the provider refuses the lease with a transient error
(`environment_provider_unavailable`), Paperclip retries twice, after 30
s and 60 s
> - After those two retries the task rests on "Retry when ready". A
provider outage longer than about 90 s therefore needs a person to click
Retry
> - Recovery must just work, and the target for runs that need Retry is
0%. The fix must not multiply automatic runs into a crash loop
> - This pull request adds a provider-outage wait after the fast
retries. The task stays system-owned, one waiter per environment checks
the provider on a capped backoff, and the other waiters continue by
themselves once the provider gives out a lease again. A person gets
Retry only after a 30-minute ceiling
> - The benefit is that a provider outage of up to 30 minutes delays
work instead of stranding it, without extra load on the provider

## Linked Issues or Issue Description

Refs #22087 (it classifies Daytona connection errors as transient, so
they reach this path).

**What happened?**
In a restart-chaos wave on 2026-11-17, the Daytona API host names did
not resolve from 19:40:28Z to 19:52:17Z (182 `ENOTFOUND` errors for the
API and proxy hosts). Tasks that started in that window failed with
`environment_provider_unavailable`. The two bounded retries (30 s, then
60 s) ran inside the outage, so these tasks rested on "Retry when ready"
and needed a person to continue them.

**Expected behavior**
Paperclip continues the task by itself when the provider is reachable
again. The task shows that it waits for the provider. It shows no Retry.
Only a long outage hands the task to a person, and then Retry works.

**Steps to reproduce**
1. Make lease acquisition fail with a transient provider error (HTTP 502
or a connection errno) for longer than about 90 s.
2. Assign a task to an agent that uses that environment.
3. After the second automatic retry fails, the task is blocked with a
Retry button, although the provider becomes available again later.

## What Changed

- `server/src/services/environment-provider-wait.ts` (new): the
`environment_provider_wait` retry lane. The delays are 1 min, 2 min,
then 5 min. The lane stops at 30 minutes after the wait started, with at
most 12 attempts. It also builds the task text "Waiting for
"<environment name>" to come back. Paperclip continues this task
automatically when it does."
- `scheduleBoundedRetryForRun` (`heartbeat/retries.ts`): when a run
fails with `environment_provider_unavailable` after the fast retries are
spent, it schedules the next attempt in the wait lane. The retry context
records the environment and the time the wait started. The time is kept
across attempts. The lane is uncharged
(`execution-recovery-attempt.ts`), so it does not spend the failure
budget. Other lanes drop the wait context.
- The environment orchestrator puts the environment id on
`EnvironmentProviderUnavailableError`, and the setup-failure path saves
it in `resultJson.environmentProvider`.
- Run dispatch (`run-dispatch/adapters/environment-provider-wait.ts`,
new): a due wait stays parked while an earlier waiter for the same
environment is still in line or still acquiring its lease, and no lease
was acquired in that environment since this waiter last failed. Parked
waiters check again every 30 s. This uses database reads only, and it
adds no work to the run start path. When a lease is acquired, the parked
waiters are promoted in arrival order. The automatic-run brake then
meters their starts as before.
- The probe must be able to start. A queued earlier waiter blocks later
waiters for only 60 s after promotion, and a running one only while it
is still acquiring its lease (at most 10 minutes). If the probe's agent
is busy, the next runnable waiter becomes the probe. A waiter past the
30-minute ceiling is never parked, so it makes its last attempt and the
exhaustion path runs.
- The notify-and-update mechanics of parking are in one helper,
`parkScheduledRetryInTx`
(`run-dispatch/adapters/scheduled-retry-park.ts`). The workspace-busy
wait and the provider wait use it and only decide whether to park and
until when.
- UI: `IssueScheduledRetryCard` shows nothing for
`environment_provider_wait` (as for `workspace_busy`). `IssueProperties`
shows "Sandbox: Waiting for the provider" instead of the scheduled-retry
picker. `IssueMonitorBanner` shows a steady "Waiting for the sandbox
provider" with no countdown and no Check now.
- Task presentation: a task in the wait lane is "working", system-owned,
with no Retry, and shows the wait text. After the ceiling the existing
path applies: the task is blocked with the system-owned notice and a
Retry that works. The notice and the provider-failure text now say that
Paperclip retried for 30 minutes.
- The per-task transient cap, the automatic-run brake and the bounded
fast retries are unchanged.

## Verification

- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/heartbeat-process-recovery.test.ts -t "transient sandbox
provider refusal"` (embedded Postgres):
- New: "resumes ten tasks by itself when a five-minute provider outage
ends, one probe at a time and within the automatic-run brake". Ten tasks
in one company share one environment. The provider refuses leases for 5
minutes of simulated time (the test shifts all stored timestamps). All
ten tasks complete with no Retry. No task is blocked and no task offers
Retry while it waits. At most one waiter per tick is refused by the
provider. While the brake is open, at most one automatic start happens
per tick. All ten finish within 5 minutes after the outage ends. Red on
master: tasks become `blocked` after the fast retries. Green with this
change.
- New: "lets one waiter per environment probe the provider and releases
the others once a lease is acquired". The second waiter stays parked
while the first one is queued or running without a lease. It is promoted
after a lease is acquired. Red without the park step, green with it.
- Updated: "waits for the provider without a Retry, and offers a working
Retry only after the 30-minute ceiling". After the fast retries the task
is working with the wait text and no Retry. The backoff grows from 1 to
2 minutes. After the ceiling the task is blocked, Retry is offered, and
Retry runs the task.
- Updated: "rests a provider deadline that never clears on a working
Retry, never on a board recovery action". It now reaches the ceiling
first.
- New: "lets the next waiter probe when the first waiter cannot start
because its agent is busy" (red before, green after).
- New: "hands a parked task to a person with a working Retry at the
30-minute ceiling even while the leader stays queued" (red before, green
after).
- `src/services/environment-provider-wait.test.ts`: backoff, ceiling and
text.
- UI: `IssueScheduledRetryCard.test.tsx` ("offers no Retry while the
task waits for the sandbox provider to come back") and
`IssueMonitorBanner.test.tsx` ("shows a steady provider wait with no
countdown"). `pnpm check:token-gates` is clean.
- Also run locally: `heartbeat/retries.test.ts`,
`heartbeat/run-completion.test.ts` (all 163 tests pass; the file-level
`afterAll` teardown timed out on this loaded machine),
`task-interruption-query.test.ts`, `stranded-notice.test.ts`,
`execution-recovery-attempt.test.ts`,
`environment-provider-unavailable-error.test.ts`,
`task-interruption-presentation.test.ts`, the automatic-run brake tests,
and `src/modules/run-dispatch`.
- Server `tsc --noEmit` passes.

## Risks

- **Automatic-run volume:** while a provider is down, each environment
has one waiter that calls the provider on the backoff (1, 2, then 5
minutes). The other waiters only read the database every 30 s. Before
this change the volume was zero after the second retry, because the task
waited for a person. The new volume per environment is at most about 8
provider calls in 30 minutes. It does not grow with the number of tasks.
The automatic-run brake still opens after 5 consecutive provider
refusals, and it limits the probe to one start per minute.
- After the outage, waiting tasks start again as the brake allows (a
probe, then a release ramp of 2, 4, 8 and more starts per minute). They
do not all start at once.
- A run that was in the old fast lane when this deploys continues in the
new lane when its second retry fails. A run that is already blocked
keeps its Retry.
- No migration. The new fields are optional JSON in `resultJson` and
`contextSnapshot`.

## Model Used

- Claude Opus 5.5 (provider Anthropic, model ID `claude-opus-5-5`), with
tool use and code execution 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-18 01:24:13 -06:00

943 lines
42 KiB
TypeScript

import { RetainedSandboxAbsentError } from "./retained-sandbox-absent-error.js";
import { freshWorkspaceSchema } from "@paperclipai/plugin-sdk";
import { prepareRemoteWorkspace, readRemoteWorkspaceAuthority, remoteWorkspaceRepositoryPath, supportsSshRemoteWorkspace } from "./remote-workspaces.js";
/**
* Centralized environment run orchestrator.
*
* Owns the full environment lifecycle for a heartbeat run:
* 1. Resolve selected environment
* 2. Validate environment is active and allowed
* 3. Acquire or resume lease
* 4. Realize workspace in the environment
* 5. Resolve execution target for the adapter
* 6. Release / retain / fail lease according to policy
* 7. Record activity and operator-visible status
*
* Heartbeat callers delegate to this service instead of inlining
* environment resolution, lease management, workspace realization,
* and transport logic.
*/
import { environmentLeases, type Db } from "@paperclipai/db";
import { and, eq } from "drizzle-orm";
import { remoteExecutionHasReleased } from "./remote-execution-termination.js";
import { remoteRecoveryLeaseMismatch, type RemoteRecoveryLeaseMismatchField } from "./native-runtime/remote-recovery-lease-mismatch.js";
import { EnvironmentProviderUnavailableError, readTransientLeaseAcquisitionFailure } from "./environment-provider-unavailable-error.js";
import { injectLeaseAcquisitionFault } from "./environment-lease-fault-injection.js";
import { findReusableSandboxResumeError } from "./reusable-sandbox-resume-error.js";
import { findPluginWorkerUnavailableError } from "./plugin-worker-unavailable-error.js";
import type {
Environment,
EnvironmentLease,
EnvironmentLeasePolicy,
EnvironmentLeaseStatus,
ExecutionWorkspace,
ExecutionWorkspaceConfig,
IssueExecutionWorkspaceSettings,
} from "@paperclipai/shared";
import {
resolveRunnerEnvironmentForRun,
resolveRunnerReconnectGraceMsForRun,
runnerLifecycleDefaultNeedsLeaseProbe,
resolveWarmRunnerIdleTimeoutMsForRun,
} from "./runner-environment-lifecycle.js";
import { assertEnvironmentCompanyBinding, environmentService, toEnvironmentLease } from "./environments.js";
import {
environmentRuntimeService,
buildEnvironmentLeaseContext,
type EnvironmentRuntimeLeaseRecord,
type EnvironmentRuntimeService,
type ProviderResourceDisposition,
} from "./environment-runtime.js";
import { REUSABLE_SANDBOX_RESUME_BUDGET_MS, retryTransientResumeCall } from "./reusable-sandbox-resume-replacement.js";
import { ENVIRONMENT_DRIVER_TRAITS } from "./environment-driver-traits.js";
import type { PluginTaskDescription } from "./environment-task-runtime.js";
import { unlaunchedPortableTaskLease } from "./plugin-task-ownership.js";
import {
resolveEnvironmentExecutionTarget,
resolveEnvironmentExecutionTransport,
} from "./environment-execution-target.js";
import {
adapterExecutionTargetToRemoteSpec,
type AdapterExecutionTarget,
type AdapterRemoteExecutionSpec,
type AdapterWorkspaceRealization,
type SandboxLeaseAcquisition,
} from "@paperclipai/adapter-utils/execution-target";
import type { DuplexObservabilityRecorder } from "@paperclipai/adapter-utils/duplex-observability";
import { buildWorkspaceRealizationRequest } from "./workspace-realization.js";
import { executionWorkspaceService } from "./execution-workspaces.js";
import { logActivity } from "./activity-log.js";
import { logger } from "../middleware/logger.js";
import { parseObject } from "../adapters/utils.js";
import type { RealizedExecutionWorkspace } from "./workspace-runtime.js";
import type { PluginWorkerManager } from "./plugin-worker-manager.js";
// ---------------------------------------------------------------------------
// Error types
// ---------------------------------------------------------------------------
export type EnvironmentErrorCode =
| "environment_not_found"
| "environment_inactive"
| "unsupported_environment"
| "unsupported_adapter_environment"
| "probe_failed"
| "lease_acquire_failed"
| "workspace_realization_failed"
| "transport_resolution_failed"
| "lease_release_failed"
| "lease_cleanup_failed";
export class EnvironmentRunError extends Error {
code: EnvironmentErrorCode;
environmentId?: string;
driver?: string;
provider?: string;
cause?: unknown;
constructor(
code: EnvironmentErrorCode,
message: string,
details?: {
environmentId?: string;
driver?: string;
provider?: string;
cause?: unknown;
},
) {
super(message);
this.name = "EnvironmentRunError";
this.code = code;
this.environmentId = details?.environmentId;
this.driver = details?.driver;
this.provider = details?.provider;
this.cause = details?.cause;
}
}
// ---------------------------------------------------------------------------
// Orchestration result types
// ---------------------------------------------------------------------------
export interface EnvironmentAcquisitionResult {
environment: Environment;
lease: EnvironmentLease;
leaseContext: ReturnType<typeof buildEnvironmentLeaseContext>;
executionTransport: Record<string, unknown> | null;
pluginTaskDescription?: PluginTaskDescription;
sandboxLeaseAcquisition?: SandboxLeaseAcquisition;
}
export interface EnvironmentRealizationResult {
lease: EnvironmentLease;
workspaceRealization: Record<string, unknown>;
executionTarget: AdapterExecutionTarget | null;
remoteExecution: AdapterRemoteExecutionSpec | null;
persistedExecutionWorkspace: ExecutionWorkspace | null;
}
export interface EnvironmentReleaseResult {
released: EnvironmentRuntimeLeaseRecord[];
errors: Array<{ leaseId: string; error: unknown }>;
}
function firstNonEmptyLine(text: string | null | undefined): string | null {
if (!text) return null;
for (const rawLine of text.split(/\r?\n/)) {
const line = rawLine.trim();
if (line) return line;
}
return null;
}
function formatProvisionFailureDetail(result: {
exitCode: number | null;
signal?: string | null;
timedOut: boolean;
stdout: string;
stderr: string;
}): string {
if (result.timedOut) {
return "provision command timed out";
}
const signal = typeof result.signal === "string" && result.signal.trim().length > 0
? ` (signal ${result.signal.trim()})`
: "";
const detail = firstNonEmptyLine(result.stderr) ?? firstNonEmptyLine(result.stdout);
const status = `exit code ${result.exitCode ?? "null"}${signal}`;
return detail ? `${status}: ${detail}` : status;
}
// ---------------------------------------------------------------------------
// Service factory
// ---------------------------------------------------------------------------
export function environmentRunOrchestrator(
db: Db,
options: {
pluginWorkerManager?: PluginWorkerManager;
environmentRuntime?: EnvironmentRuntimeService;
reattachResumeRetryBudgetMs?: number;
} = {},
) {
const reattachResumeRetryBudgetMs = options.reattachResumeRetryBudgetMs ?? REUSABLE_SANDBOX_RESUME_BUDGET_MS;
const environmentsSvc = environmentService(db);
const executionWorkspacesSvc = executionWorkspaceService(db);
const environmentRuntime = options.environmentRuntime ?? environmentRuntimeService(db, {
pluginWorkerManager: options.pluginWorkerManager,
});
/**
* Resolve the selected environment for a run. The caller passes the concrete
* selected environment id plus the built-in local fallback id used to lazily
* ensure the local environment row exists.
*/
async function resolveEnvironment(input: {
companyId: string;
selectedEnvironmentId: string;
localEnvironmentId: string;
}): Promise<Environment> {
const environmentId = input.selectedEnvironmentId || input.localEnvironmentId;
const environment =
environmentId === input.localEnvironmentId
? await environmentsSvc.ensureLocalEnvironment(input.companyId)
: await environmentsSvc.getById(environmentId);
if (!environment) {
throw new EnvironmentRunError("environment_not_found", `Environment "${environmentId}" not found.`, {
environmentId,
});
}
if (environment.status !== "active") {
throw new EnvironmentRunError("environment_inactive", `Environment "${environment.name}" is not active (status: ${environment.status}).`, {
environmentId: environment.id,
driver: environment.driver,
});
}
await assertEnvironmentCompanyBinding(db, environment.id, input.companyId);
return environment;
}
/**
* Acquire an environment lease for a heartbeat run.
* Wraps the runtime driver's acquire call with standardized error handling.
*/
async function acquireLease(input: {
companyId: string;
environment: Environment;
configuredEnvironment?: Environment;
issueId: string | null;
agentId: string;
heartbeatRunId: string;
persistedExecutionWorkspace: Pick<ExecutionWorkspace, "id" | "mode"> | null;
executionWorkspaceSettings: IssueExecutionWorkspaceSettings | null;
runnerTaskProjectIds?: readonly string[];
adapterType: string | null;
warmRunnerIdleTimeoutMs?: number;
runnerReconnectGraceMs?: number;
}): Promise<EnvironmentRuntimeLeaseRecord> {
try {
// Restart-chaos EP1 only; disarmed (always in production) this is one null check.
injectLeaseAcquisitionFault({ companyId: input.companyId, environmentId: input.environment.id,
heartbeatRunId: input.heartbeatRunId, issueId: input.issueId });
return await environmentRuntime.acquireRunLease({ ...input, assertCompanyBinding: true });
} catch (err) {
// Lease acquisition precedes any provider work, so a failure without a
// definite non-transient status (HTTP 5xx/429, errno, acquisition
// deadline, unfinished creation, database error) is retried by the setup
// path with its bounded bootstrap budget. Saved-sandbox resume and
// stopped-worker refusals keep their own, more specific classifications.
const transient = findReusableSandboxResumeError(err) || findPluginWorkerUnavailableError(err)
? null : readTransientLeaseAcquisitionFailure(err, { id: input.heartbeatRunId, companyId: input.companyId });
throw new EnvironmentRunError(
"lease_acquire_failed",
`Failed to acquire lease for environment "${input.environment.name}" (${input.environment.driver}): ${err instanceof Error ? err.message : String(err)}`,
{
environmentId: input.environment.id,
driver: input.environment.driver,
cause: transient
? new EnvironmentProviderUnavailableError({ environmentId: input.environment.id, environmentName: input.environment.name,
driver: input.environment.driver, evidence: transient, cause: err })
: err,
},
);
}
}
/**
* Resolve the execution transport for an adapter based on the acquired lease.
*/
async function resolveTransport(input: {
companyId: string;
adapterType: string;
environment: Environment;
leaseMetadata: Record<string, unknown> | null;
}): Promise<Record<string, unknown> | null> {
try {
return await resolveEnvironmentExecutionTransport({
db,
companyId: input.companyId,
adapterType: input.adapterType,
environment: input.environment,
leaseMetadata: input.leaseMetadata,
});
} catch (err) {
throw new EnvironmentRunError(
"transport_resolution_failed",
`Failed to resolve execution transport for "${input.environment.name}": ${err instanceof Error ? err.message : String(err)}`,
{
environmentId: input.environment.id,
driver: input.environment.driver,
cause: err,
},
);
}
}
/**
* Full acquisition flow: resolve environment, acquire lease, resolve transport.
* This is the primary entry point for heartbeat run setup.
*/
async function acquireForRun(input: {
companyId: string;
selectedEnvironmentId: string;
localEnvironmentId: string;
adapterType: string;
adapterConfig?: Record<string, unknown>;
admittedLifecycleMode?: "warm" | "per_turn";
managedAiCredentialIdentity?: string;
issueId: string | null;
heartbeatRunId: string;
agentId: string;
persistedExecutionWorkspace: Pick<ExecutionWorkspace, "id" | "mode"> | null;
executionWorkspaceSettings: IssueExecutionWorkspaceSettings | null;
runnerTaskProjectIds?: readonly string[];
runnerTaskResumeSessionId?: string | null;
/** Existing live runner recovery must use its original active lease, including ephemeral leases. */
reattachRemoteLease?: { kind?: "sandbox"; leaseId: string; providerLeaseId: string; remoteCwd: string }
| { kind: "plugin_task"; leaseId: string; preparation?: true };
/** A new controller must stop the previous plugin task before allocating a replacement. */
recoverPluginTask?: boolean;
replacementWorkspaceCreation?: string;
}): Promise<EnvironmentAcquisitionResult> {
// Step 1: Resolve environment
const selectedEnvironment = await resolveEnvironment({
companyId: input.companyId,
selectedEnvironmentId: input.selectedEnvironmentId,
localEnvironmentId: input.localEnvironmentId,
});
// The remote-sandbox warm default needs a provider that keeps reusable
// leases. Probe it only for a run the default can apply to; every other
// run resolves without it, exactly as before.
const reusableLeaseSupported = runnerLifecycleDefaultNeedsLeaseProbe(
selectedEnvironment, input.adapterType, input.adapterConfig, input.admittedLifecycleMode, input.managedAiCredentialIdentity,
) && await Promise.resolve(environmentRuntime.providerSupportsReusableLeases?.(selectedEnvironment)).then(
(supported) => supported === true, () => false);
const lifecycleDefaultContext = { reusableLeaseSupported, managedAiCredentialIdentity: input.managedAiCredentialIdentity };
const environment = resolveRunnerEnvironmentForRun(
selectedEnvironment, input.adapterType, input.adapterConfig, input.admittedLifecycleMode, input.managedAiCredentialIdentity,
lifecycleDefaultContext,
);
if (input.recoverPluginTask && environment.driver === "plugin" && input.adapterType === "paperclip_runner") {
const prior = await db.select().from(environmentLeases).where(and(
eq(environmentLeases.companyId, input.companyId), eq(environmentLeases.heartbeatRunId, input.heartbeatRunId),
));
if (prior.some(lease => lease.metadata?.runnerTaskLease !== true || lease.environmentId !== environment.id ||
lease.issueId !== input.issueId || lease.executionWorkspaceId !== (input.persistedExecutionWorkspace?.id ?? null) ||
lease.metadata?.agentId !== input.agentId)) throw new Error("native_plugin_recovery_lease_mismatch");
const errors: unknown[] = [];
await environmentRuntime.releaseRunLeases(input.heartbeatRunId, "released", (_id, error) => { errors.push(error); });
// Same-run task resume requires every prior lease released with its receipt;
// a stop recorded before a held release is not enough here.
if (errors.length || (prior.length && !await remoteExecutionHasReleased(db, input.companyId, input.heartbeatRunId))) {
throw new Error("native_plugin_recovery_termination_unconfirmed", { cause: errors[0] });
}
}
// Step 2: Acquire lease
const warmRunnerIdleTimeoutMs = resolveWarmRunnerIdleTimeoutMsForRun(
selectedEnvironment, input.adapterType, input.adapterConfig, input.admittedLifecycleMode, lifecycleDefaultContext,
);
const runnerReconnectGraceMs = input.adapterType
? resolveRunnerReconnectGraceMsForRun(selectedEnvironment, input.adapterType)
: undefined;
const acquisitionInput = {
companyId: input.companyId,
environment,
configuredEnvironment: selectedEnvironment,
issueId: input.issueId,
agentId: input.agentId,
heartbeatRunId: input.heartbeatRunId,
persistedExecutionWorkspace: input.persistedExecutionWorkspace,
executionWorkspaceSettings: input.executionWorkspaceSettings,
runnerTaskProjectIds: input.runnerTaskProjectIds,
runnerTaskResumeSessionId: input.runnerTaskResumeSessionId,
replacementWorkspaceCreation: input.replacementWorkspaceCreation,
adapterType: input.adapterType ?? null,
warmRunnerIdleTimeoutMs,
runnerReconnectGraceMs,
};
let leaseRecord: EnvironmentRuntimeLeaseRecord;
let sandboxLeaseAcquisition: SandboxLeaseAcquisition | undefined;
const expected = input.reattachRemoteLease;
if (expected) {
// Reattachment keeps the original lease, including ephemeral leases.
// Never reacquire: acquisition can replace a non-reusable sandbox.
const replayPreparation = expected.kind === "plugin_task" && expected.preparation;
const lease = replayPreparation
? await db.select().from(environmentLeases).where(and(eq(environmentLeases.id, expected.leaseId),
eq(environmentLeases.companyId, input.companyId), unlaunchedPortableTaskLease()))
.then(([row]) => row ? toEnvironmentLease(row) : null)
: await environmentsSvc.getLeaseById(expected.leaseId);
const mismatch: RemoteRecoveryLeaseMismatchField | null = !lease ? "lease"
: lease.companyId !== input.companyId || lease.environmentId !== environment.id ||
lease.heartbeatRunId !== input.heartbeatRunId || lease.issueId !== input.issueId ||
lease.executionWorkspaceId !== (input.persistedExecutionWorkspace?.id ?? null) ||
lease.metadata?.agentId !== input.agentId ? "lease_scope"
: lease.status !== "active" || lease.releasedAt !== null || lease.cleanupStatus !== null ? "lease_status"
: lease.expiresAt !== null && new Date(lease.expiresAt).getTime() <= Date.now() ? "lease_expired"
: expected.kind === "plugin_task"
? environment.driver !== "plugin" ? "environment_driver"
: input.adapterType !== "paperclip_runner" ? "adapter_type"
: replayPreparation ? environment.config.pluginKey !== lease.metadata?.pluginKey ||
environment.config.driverKey !== lease.metadata?.driverKey ? "plugin_task" : null
: lease.metadata?.runnerTaskLease !== true || lease.metadata?.runnerTaskDispatched !== true
|| lease.metadata?.runnerTaskExecutionStopped === true ? "plugin_task" : null
: environment.driver !== "sandbox" ? "environment_driver"
: lease.provider !== environment.config.provider ? "provider"
: lease.providerLeaseId !== expected.providerLeaseId ? "provider_lease_id"
: lease.metadata?.remoteCwd !== expected.remoteCwd ? "remote_cwd" : null;
if (mismatch || !lease) {
throw remoteRecoveryLeaseMismatch("environment_acquire", mismatch ?? "lease");
}
if (expected.kind !== "plugin_task") {
// The provider may have archived this retained sandbox while its
// controller was offline. Ready the exact lease under its lifecycle
// budget before short preparation commands (such as project-auth
// checks) can run. This API cannot acquire a replacement.
const resumed = await retryTransientResumeCall({ budgetMs: reattachResumeRetryBudgetMs, scope: "known_host",
call: timeoutMs => environmentRuntime.resumeRunLease({
environment, lease, requestedCwd: expected.remoteCwd, ...(timeoutMs !== undefined ? { timeoutMs } : {}),
...(warmRunnerIdleTimeoutMs !== undefined ? { warmRunnerIdleTimeoutMs } : {}),
...(runnerReconnectGraceMs !== undefined ? { runnerReconnectGraceMs } : {}),
}) }).catch((error: unknown) => {
// Confirmed provider absence is a named lease mismatch; other
// failures (for example a transient refusal) keep their own error.
if (error instanceof RetainedSandboxAbsentError) throw remoteRecoveryLeaseMismatch("environment_resume", "lease_expired");
throw error;
});
if (!resumed) throw remoteRecoveryLeaseMismatch("environment_resume", "lease");
if (resumed.providerLeaseId !== expected.providerLeaseId) {
throw remoteRecoveryLeaseMismatch("environment_resume", "provider_lease_id");
}
if (resumed.metadata?.expired === true) throw remoteRecoveryLeaseMismatch("environment_resume", "lease_expired");
// Resume confirms allocation readiness. Its optional metadata may
// describe the allocation root; keep the validated lease's workspace.
sandboxLeaseAcquisition = { outcome: "resumed", providerLeaseId: expected.providerLeaseId };
}
leaseRecord = { environment, lease, leaseContext: buildEnvironmentLeaseContext(input) };
} else {
leaseRecord = await acquireLease(acquisitionInput);
}
// Step 3: Log lease acquisition activity
await logActivity(db, {
companyId: input.companyId,
actorType: "agent",
actorId: input.agentId,
agentId: input.agentId,
runId: input.heartbeatRunId,
action: "environment.lease_acquired",
entityType: "environment_lease",
entityId: leaseRecord.lease.id,
issueId: input.issueId,
details: {
environmentId: environment.id,
driver: environment.driver,
leasePolicy: leaseRecord.lease.leasePolicy,
provider: leaseRecord.lease.provider,
executionWorkspaceId: leaseRecord.leaseContext.executionWorkspaceId,
issueId: input.issueId,
networkEgress: input.executionWorkspaceSettings?.networkEgress ?? null,
},
});
// Step 4: Resolve execution transport
const executionTransport = environment.driver === "plugin" && input.adapterType === "paperclip_runner" ? null : await resolveTransport({
companyId: input.companyId,
adapterType: input.adapterType,
environment,
leaseMetadata: leaseRecord.lease.metadata,
});
return {
environment,
lease: leaseRecord.lease,
leaseContext: leaseRecord.leaseContext,
executionTransport,
...(sandboxLeaseAcquisition ? { sandboxLeaseAcquisition } : {}),
};
}
async function confirmWorkspaceTaskPlacement(acquisition: EnvironmentAcquisitionResult, input: {
companyId: string;
environmentId: string;
workspaceId: string;
signal?: AbortSignal;
}) {
const { environment, lease } = acquisition;
const driverKey = environment.config?.driverKey;
if (environment.id !== input.environmentId || typeof driverKey !== "string" || !lease.providerLeaseId) {
throw new Error("Workspace Launcher placement changed");
}
const description = await environmentRuntime.task({
companyId: input.companyId, leaseId: lease.id, operation: { kind: "describe" }, ownershipSignal: input.signal,
});
const current = await environmentsSvc.getLeaseById(lease.id);
if (description.kind !== "description" || !current || current.providerLeaseId !== description.taskId ||
current.status !== "active" || current.environmentId !== environment.id ||
current.executionWorkspaceId !== input.workspaceId) {
throw new Error("Workspace Launcher lease is no longer active");
}
return {
acquisition: { ...acquisition, lease: current, pluginTaskDescription: description },
taskLease: { driverKey, environmentId: environment.id, providerLeaseId: description.taskId },
};
}
/**
* Realize workspace in the environment and resolve the execution target.
*
* After lease acquisition, this method:
* 1. Builds a workspace realization request
* 2. Calls the environment runtime driver to realize the workspace
* 3. Persists realization metadata on the lease and execution workspace
* 4. Resolves the adapter execution target (local/ssh/sandbox)
*
* Returns the updated lease, realization metadata, and the execution
* target spec that the adapter needs to run.
*/
async function realizeForRun(input: {
environment: Environment;
lease: EnvironmentLease;
sandboxLeaseAcquisition?: SandboxLeaseAcquisition;
adapterType: string;
companyId: string;
issueId: string | null;
heartbeatRunId: string;
executionWorkspace: RealizedExecutionWorkspace;
effectiveExecutionWorkspaceMode: string | null;
persistedExecutionWorkspace: ExecutionWorkspace | null;
runnerTaskProjectIds?: readonly string[];
signal?: AbortSignal;
assertOwned?: () => Promise<void>;
/**
* The host duplex observability recorder for this run. The orchestrator threads
* it to `resolveEnvironmentExecutionTarget`, which stamps it on the sandbox
* target. Absent keeps the safe no-op default in the bridge.
*/
duplexObservabilityRecorder?: DuplexObservabilityRecorder | null;
}): Promise<EnvironmentRealizationResult> {
const {
environment,
adapterType,
companyId,
issueId,
heartbeatRunId,
executionWorkspace,
effectiveExecutionWorkspaceMode,
} = input;
let { lease, persistedExecutionWorkspace } = input;
const withAcquisitionReceipt = (target: AdapterExecutionTarget | null): AdapterExecutionTarget | null => {
if (!input.sandboxLeaseAcquisition || target?.kind !== "remote" || target.transport !== "sandbox") return target;
if (lease.providerLeaseId !== input.sandboxLeaseAcquisition.providerLeaseId) {
throw remoteRecoveryLeaseMismatch("environment_resume", "provider_lease_id");
}
return { ...target, sandboxLeaseAcquisition: input.sandboxLeaseAcquisition };
};
const remoteAuthority = readRemoteWorkspaceAuthority(persistedExecutionWorkspace?.metadata);
if (remoteAuthority && persistedExecutionWorkspace) {
if (!(adapterType === "paperclip_runner" && environment.driver === "sandbox") && !supportsSshRemoteWorkspace(adapterType, environment.driver)) {
throw new EnvironmentRunError("unsupported_adapter_environment", "Remote workspace storage requires a supported remote environment");
}
const target = withAcquisitionReceipt(await resolveEnvironmentExecutionTarget({
db, companyId, adapterType, environment, leaseId: lease.id, leaseMetadata: lease.metadata,
lease, environmentRuntime, duplexObservabilityRecorder: input.duplexObservabilityRecorder ?? null,
}));
if (!target || target.kind !== "remote" || (target.transport === "sandbox" && (!target.runner || target.runnerTask))) {
throw new EnvironmentRunError("transport_resolution_failed", "Remote workspace storage requires a remote file transport");
}
const prepared = await prepareRemoteWorkspace({
db, workspace: persistedExecutionWorkspace, runId: heartbeatRunId, lease, target,
source: remoteAuthority.source,
assertOwned: input.assertOwned,
provisionCommand: persistedExecutionWorkspace.config?.provisionCommand,
runtimeProvisionCommand: persistedExecutionWorkspace.config?.runtimeProvisionCommand,
});
lease = prepared.lease;
const workspaceRealization = {
version: 1, mode: "in_place", authoritativeRoot: prepared.remoteRoot,
pathAliases: [], outboundRestorePaths: [], provider: lease.provider,
environmentId: environment.id, leaseId: lease.id, providerLeaseId: lease.providerLeaseId,
additional: (remoteAuthority.source.repositories ?? []).filter(repository => repository.readOnly)
.map(repository => ({ path: remoteWorkspaceRepositoryPath(prepared.remoteRoot, repository),
projectId: repository.projectId ?? null, projectWorkspaceId: repository.projectWorkspaceId ?? null,
repoUrl: repository.repoUrl, repoRef: repository.ref })),
};
const updatedLease = await environmentsSvc.updateLeaseMetadata(lease.id, {
...lease.metadata, remoteCwd: prepared.remoteRoot, workspaceRealization,
});
if (!updatedLease) throw new Error("Remote workspace lease disappeared during preparation");
lease = updatedLease;
persistedExecutionWorkspace = await executionWorkspacesSvc.getStoredById(companyId, prepared.workspace.id);
if (!persistedExecutionWorkspace) throw new Error("Remote workspace disappeared during preparation");
input.executionWorkspace.cwd = prepared.remoteRoot;
const executionTarget: AdapterExecutionTarget = {
...target, remoteCwd: prepared.remoteRoot,
...(target.transport === "ssh" ? { spec: { ...target.spec, remoteCwd: prepared.remoteRoot } } : {}),
workspaceRealization: { mode: "in_place", authoritativeRoot: prepared.remoteRoot,
pathAliases: [], outboundRestorePaths: [] },
};
return { lease, workspaceRealization, executionTarget,
remoteExecution: adapterExecutionTargetToRemoteSpec(executionTarget), persistedExecutionWorkspace };
}
if (executionWorkspace.providerBinding) {
const binding = executionWorkspace.providerBinding;
if (environment.driver !== "plugin" || adapterType !== "paperclip_runner" || lease.metadata?.pluginId !== binding.pluginId || !await environmentRuntime.supportsTasks({ lease })) {
throw new EnvironmentRunError("workspace_realization_failed", "The execution plugin cannot access this managed workspace.", { environmentId: environment.id });
}
const updated = await environmentsSvc.updateLeaseMetadata(lease.id, { ...lease.metadata, workspaceProvider: binding });
if (!updated) throw new Error("Workspace execution lease disappeared");
lease = updated;
}
const freshWorkspace = freshWorkspaceSchema.safeParse(persistedExecutionWorkspace?.metadata?.workspaceProviderFresh ??
persistedExecutionWorkspace?.metadata?.runnerTaskFreshWorkspace);
if (environment.driver === "plugin" && freshWorkspace.success) {
const updated = await environmentsSvc.updateLeaseMetadata(lease.id, { ...lease.metadata, freshWorkspace: freshWorkspace.data });
if (!updated) throw new Error("Workspace execution lease disappeared");
lease = updated;
}
// Step 1: Build workspace realization request
const workspaceRealizationRequest = buildWorkspaceRealizationRequest({
adapterType,
companyId,
environmentId: environment.id,
executionWorkspaceId: persistedExecutionWorkspace?.id ?? null,
issueId,
heartbeatRunId,
requestedMode: persistedExecutionWorkspace?.mode ?? effectiveExecutionWorkspaceMode,
workspace: executionWorkspace,
workspaceConfig: persistedExecutionWorkspace?.config ?? null,
});
// Step 2: Realize workspace in the environment via the runtime driver
let workspaceRealization: Record<string, unknown> = {};
let realizedWorkspaceCwd: string | null = null;
const pluginTaskWorkspace = environment.driver === "plugin" && adapterType === "paperclip_runner";
if (pluginTaskWorkspace && !await environmentRuntime.supportsTasks({ lease })) {
throw new EnvironmentRunError("unsupported_adapter_environment", "Plugin environment does not provide native Runner task execution");
}
if (ENVIRONMENT_DRIVER_TRAITS[environment.driver].realizesWorkspace) {
try {
const remoteCwd =
typeof lease.metadata?.remoteCwd === "string" && lease.metadata.remoteCwd.trim().length > 0
? lease.metadata.remoteCwd
: undefined;
const workspaceRealizationResult = await environmentRuntime.realizeWorkspace({
environment,
lease,
workspace: {
localPath: executionWorkspace.cwd,
remotePath: remoteCwd,
mode: persistedExecutionWorkspace?.mode ?? effectiveExecutionWorkspaceMode ?? undefined,
metadata: {
workspaceRealizationRequest,
},
},
});
realizedWorkspaceCwd =
typeof workspaceRealizationResult.cwd === "string" && workspaceRealizationResult.cwd.trim().length > 0
? workspaceRealizationResult.cwd.trim()
: null;
workspaceRealization = parseObject(workspaceRealizationResult.metadata?.workspaceRealization);
} catch (err) {
throw new EnvironmentRunError(
"workspace_realization_failed",
`Failed to realize workspace for environment "${environment.name}" (${environment.driver}): ${err instanceof Error ? err.message : String(err)}`,
{
environmentId: environment.id,
driver: environment.driver,
cause: err,
},
);
}
}
const provisionCommand = workspaceRealizationRequest.runtimeOverlay.provisionCommand?.trim() ?? "";
const realizedCwd =
realizedWorkspaceCwd ??
(typeof lease.metadata?.remoteCwd === "string" && lease.metadata.remoteCwd.trim().length > 0
? lease.metadata.remoteCwd.trim()
: executionWorkspace.cwd);
// The host `provisionCommand` runs on the host worktree during the
// `workspace_provision` step, before the run reaches the environment.
// A `sandbox`-driver environment does not receive the repo tree here. The
// sandbox driver `realizeWorkspace` step only creates the remote folder.
// The adapter uploads the provisioned tree later, in its `stage.sync` step.
// So the host command must not run inside the still-empty sandbox; it fails
// there (exit 127). Skip the step for `sandbox`, and keep the existing skip
// for `local`. Keep the step for `ssh`, which runs the command on the
// remote host that shares the workspace path.
const driverSkipsHostProvision =
environment.driver === "local" || environment.driver === "sandbox" || pluginTaskWorkspace;
if (provisionCommand && environment.driver === "sandbox") {
logger.info(
{
environmentId: environment.id,
driver: environment.driver,
issueId,
heartbeatRunId,
},
"Skip host provisionCommand for sandbox-driver environment; the adapter stage.sync step delivers the provisioned tree",
);
}
if (provisionCommand && !executionWorkspace.providerBinding && !driverSkipsHostProvision) {
try {
const provisionResult = await environmentRuntime.execute({
environment,
lease,
command: "bash",
args: ["-lc", provisionCommand],
cwd: realizedCwd,
env: {
SHELL: "/bin/bash",
},
timeoutMs: 300_000,
// The provision command runs before the run opens its trace root, so it
// carries no run parent. A sandbox provider that opens a persistent
// session on the first command must not open the session here, or the
// session-setup span loses its parent and the span backend drops it.
// Bypass the session for this command; the session opens on the first
// in-run command instead, whose setup span parents to the run trace.
bypassSession: true,
});
if (provisionResult.exitCode !== 0 || provisionResult.timedOut) {
throw new Error(formatProvisionFailureDetail(provisionResult));
}
} catch (err) {
throw new EnvironmentRunError(
"workspace_realization_failed",
`Failed to provision workspace for environment "${environment.name}" (${environment.driver}): ${err instanceof Error ? err.message : String(err)}`,
{
environmentId: environment.id,
driver: environment.driver,
cause: err,
},
);
}
}
// Step 3: Persist realization metadata on lease and execution workspace
if (Object.keys(workspaceRealization).length > 0) {
const nextLeaseMetadata = {
...(lease.metadata ?? {}),
workspaceRealization,
};
const updatedLease = await environmentsSvc.updateLeaseMetadata(lease.id, nextLeaseMetadata);
if (updatedLease) {
lease = updatedLease;
}
if (persistedExecutionWorkspace) {
const updatedEw = await executionWorkspacesSvc.update(persistedExecutionWorkspace.id, {
metadata: {
...(persistedExecutionWorkspace.metadata ?? {}),
workspaceRealizationRequest,
workspaceRealization,
},
});
if (updatedEw) {
persistedExecutionWorkspace = updatedEw;
}
}
}
// Step 4: Resolve execution target for the adapter
let executionTarget: AdapterExecutionTarget | null;
try {
executionTarget = withAcquisitionReceipt(await resolveEnvironmentExecutionTarget({
db,
companyId,
adapterType,
environment,
leaseId: lease.id,
leaseMetadata: (lease.metadata as Record<string, unknown> | null) ?? null,
lease,
environmentRuntime,
signal: input.signal,
duplexObservabilityRecorder: input.duplexObservabilityRecorder ?? null,
}));
// The native plugin task bridge owns guest paths. Never fall back to host execution.
if (executionWorkspace.providerBinding && (executionTarget?.kind !== "remote" ||
!("runnerTask" in executionTarget) || typeof executionTarget.runnerTask !== "function")) {
throw new Error("Managed workspace execution requires the native plugin task bridge");
}
const realizationMode = pluginTaskWorkspace || workspaceRealization.mode === "in_place" ? "in_place" : "copy";
const authoritativeRoot =
typeof workspaceRealization.authoritativeRoot === "string" && workspaceRealization.authoritativeRoot.trim().length > 0
? workspaceRealization.authoritativeRoot.trim()
: realizedCwd;
const workspaceTargetMetadata: AdapterWorkspaceRealization = {
mode: realizationMode,
authoritativeRoot,
pathAliases: Array.isArray(workspaceRealization.pathAliases)
? workspaceRealization.pathAliases.filter(
(entry): entry is { path: string; target: string } =>
typeof entry === "object" && entry !== null &&
typeof (entry as { path?: unknown }).path === "string" &&
typeof (entry as { target?: unknown }).target === "string",
)
: [],
outboundRestorePaths: Array.isArray(workspaceRealization.outboundRestorePaths)
? workspaceRealization.outboundRestorePaths.filter((entry): entry is string => typeof entry === "string")
: [],
};
if (executionTarget) {
executionTarget = {
...executionTarget,
...(executionTarget.kind === "remote" && executionTarget.transport === "sandbox" && executionTarget.runnerTask
? { runnerTaskProjectIds: [...new Set([executionWorkspace.projectId,
...input.runnerTaskProjectIds ?? [],
...(executionWorkspace.additionalWorkspaces ?? []).map(workspace => workspace.projectId)]
.filter((id): id is string => Boolean(id)))] } : {}),
...(executionTarget.kind === "remote" && realizationMode === "in_place"
? { remoteCwd: authoritativeRoot }
: {}),
workspaceRealization: workspaceTargetMetadata,
} as AdapterExecutionTarget;
}
} catch (err) {
throw new EnvironmentRunError(
"transport_resolution_failed",
`Failed to resolve execution target for "${environment.name}": ${err instanceof Error ? err.message : String(err)}`,
{
environmentId: environment.id,
driver: environment.driver,
cause: err,
},
);
}
return {
lease,
workspaceRealization,
executionTarget,
remoteExecution: adapterExecutionTargetToRemoteSpec(executionTarget),
persistedExecutionWorkspace,
};
}
/**
* Release all active leases for a heartbeat run.
* Tracks cleanup status per lease. Errors during individual lease release
* are captured but do not prevent other leases from being released.
* The original run failure (if any) is never hidden by cleanup errors.
*/
async function releaseForRun(input: {
heartbeatRunId: string;
companyId: string;
agentId: string;
status?: Extract<EnvironmentLeaseStatus, "released" | "expired" | "failed">;
failureReason?: string;
/** Explicit Stop during adapter startup; never used for ordinary cleanup. */
cancelActiveWork?: boolean;
respectCleanupDeadlines?: boolean;
/** Explicit paperclip_runner resource lifecycle. Omitted for legacy adapters. */
providerResourceDisposition?: ProviderResourceDisposition;
nativeLifecycleTelemetry?: {
provider: string;
harness: string;
lifecycleMode: "per_turn" | "warm";
sandboxResource:
| "keep_running"
| "stop_and_reuse"
| "destroy_after_turn";
};
}): Promise<EnvironmentReleaseResult> {
const status = input.status ?? "released";
const result: EnvironmentReleaseResult = { released: [], errors: [] };
let releasedLeases: EnvironmentRuntimeLeaseRecord[];
try {
const releaseArgs: Parameters<typeof environmentRuntime.releaseRunLeases> = [
input.heartbeatRunId,
status,
(leaseId, error) => result.errors.push({ leaseId, error }),
input.providerResourceDisposition,
];
if (input.cancelActiveWork) releaseArgs[4] = true;
if (input.respectCleanupDeadlines) releaseArgs[5] = { respectCleanupDeadlines: true };
releasedLeases = await environmentRuntime.releaseRunLeases(...releaseArgs);
} catch (err) {
result.errors.push({ leaseId: "*", error: err });
return result;
}
for (const released of releasedLeases) {
try {
await logActivity(db, {
companyId: input.companyId,
actorType: "agent",
actorId: input.agentId,
agentId: input.agentId,
runId: input.heartbeatRunId,
action: "environment.lease_released",
entityType: "environment_lease",
entityId: released.lease.id,
issueId: released.lease.issueId,
details: {
environmentId: released.lease.environmentId,
driver: released.environment.driver,
leasePolicy: released.lease.leasePolicy,
provider: released.lease.provider,
executionWorkspaceId: released.lease.executionWorkspaceId,
issueId: released.lease.issueId,
status: released.lease.status,
cleanupStatus: released.lease.cleanupStatus,
failureReason: input.failureReason ?? released.lease.failureReason,
providerResourceDisposition:
input.providerResourceDisposition ?? "legacy_default",
},
});
} catch {
// Activity logging failure should not block lease release
}
result.released.push(released);
}
return result;
}
return {
resolveEnvironment,
acquireLease,
resolveTransport,
acquireForRun,
confirmWorkspaceTaskPlacement,
realizeForRun,
releaseForRun,
// Expose the underlying runtime for cases that need direct driver access
runtime: environmentRuntime,
};
}
export type EnvironmentRunOrchestrator = ReturnType<typeof environmentRunOrchestrator>;