Skip to content

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

fix: enforce terminal outcomes during recovered sandbox cleanup (#14767)

Dottae2908fff5c4e
diff --git a/doc/DEVELOPING.md b/doc/DEVELOPING.md
index 40b568a6b6..7fdd364f4a 100644
--- a/doc/DEVELOPING.md
+++ b/doc/DEVELOPING.md
@@ -1246,6 +1246,9 @@ Only successful turns retain a live warm provider session. A structured failed
or cancelled result must retire that session and collect its managed files before
heartbeat stops the reusable sandbox. A later retry can resume the same sandbox
without inheriting the stopped provider transport.
+The shared lease-release boundary rechecks the durable run status for recovery
+and ordinary teardown. Successful workspace copy-back alone must not keep a
+failed turn's sandbox running.
Run the credential-free real-process restart suite with:
diff --git a/server/src/__tests__/heartbeat-run-terminalize-before-release.test.ts b/server/src/__tests__/heartbeat-run-terminalize-before-release.test.ts
index 3d0ab72eec..62555a2737 100644
--- a/server/src/__tests__/heartbeat-run-terminalize-before-release.test.ts
+++ b/server/src/__tests__/heartbeat-run-terminalize-before-release.test.ts
@@ -301,6 +301,43 @@ describeEmbeddedPostgres("heartbeat teardown terminalizes the run before releasi
expect(releaseRunLeases).not.toHaveBeenCalled();
});
+ it.each(["failed", "cancelled", "timed_out", "interrupted", "succeeded"])(
+ "uses the durable %s outcome when recovered workspace cleanup requests warm retention",
+ async (status) => {
+ const { companyId, agentId, issueId, runId } = await seed({
+ issueStatus: status === "succeeded" ? "done" : "blocked",
+ runStatus: status,
+ });
+ await db.update(heartbeatRuns).set({
+ runtimeMode: "native",
+ nativeIssueId: issueId,
+ nativePhase: "committed",
+ finishedAt: new Date(),
+ }).where(eq(heartbeatRuns.id, runId));
+ const releaseRunLeases = vi.fn(async () => []);
+ const heartbeat = heartbeatService(db, {
+ environmentRuntime: { releaseRunLeases } as unknown as HeartbeatEnvironmentRuntime,
+ });
+
+ // Successful workspace copy-back does not make a failed provider turn
+ // successful. Recovery can reach this boundary without the run's finally.
+ await heartbeat.releaseEnvironmentLeasesForRun({
+ runId,
+ companyId,
+ agentId,
+ status: "succeeded",
+ providerResourceDisposition: "keep_running",
+ });
+
+ expect(releaseRunLeases).toHaveBeenCalledWith(
+ runId,
+ leaseReleaseStatusForRunStatus(status),
+ expect.any(Function),
+ status === "succeeded" ? "keep_running" : "stop_and_retain",
+ );
+ },
+ );
+
it.each(["daytona", "local"])("destroy after failed checkpoint requires a terminal remote owner: %s", async provider => {
const { companyId, agentId, runId } = await seed({ issueStatus: "blocked", runStatus: "running" });
await db.update(heartbeatRuns).set({ status: "cancelled", finishedAt: new Date() }).where(eq(heartbeatRuns.id, runId));
diff --git a/server/src/services/heartbeat.ts b/server/src/services/heartbeat.ts
index 0b77ae299c..388f1583fa 100644
--- a/server/src/services/heartbeat.ts
+++ b/server/src/services/heartbeat.ts
@@ -10173,7 +10173,14 @@ export function heartbeatService(
}) {
const leaseOwnerRun = await getRun(input.runId);
if (leaseOwnerRun && isNativeRunnerOwnershipHeld(leaseOwnerRun)) return;
- if (input.providerResourceDisposition === "destroy") {
+ // Recovery can finish workspace copy-back outside the executor's finally.
+ // Successful copy-back does not earn warm retention for a failed turn.
+ const status = leaseOwnerRun?.status ?? input.status;
+ const providerResourceDisposition = providerResourceDispositionForTerminalRun(
+ input.providerResourceDisposition,
+ status,
+ );
+ if (providerResourceDisposition === "destroy") {
const closeResult = await (
options.closeWarmNativeSessionsForRun ??
(async ({ runId, reason }) => {
@@ -10241,9 +10248,9 @@ export function heartbeatService(
heartbeatRunId: input.runId,
companyId: input.companyId,
agentId: input.agentId,
- status: leaseReleaseStatusForRunStatus(input.status),
+ status: leaseReleaseStatusForRunStatus(status),
failureReason: input.failureReason ?? undefined,
- providerResourceDisposition: input.providerResourceDisposition,
+ providerResourceDisposition,
nativeLifecycleTelemetry: input.nativeLifecycleTelemetry,
})
.catch((err) => {