fix(server): retain stopped ephemeral workspaces after failed copy-back (#15790)

## Thinking Path

> - Paperclip manages work performed by agents.
> - A remote adapter must copy workspace changes back after execution.
> - Cancellation can stop and release an ephemeral sandbox before
copy-back fails.
> - The recovery recorder previously considered only active or already
held leases.
> - This change carries the original host lease into the recorder and
checks both stop receipts.
> - The exact stopped source receives a board repair obligation without
replaying work.

## Linked Issues or Issue Description

**What happened?**

A legacy remote run can report `workspaceRestoreFailure` after its
dedicated stop has released the source lease. The recorder misses that
source and does not create its file-repair action. Related prior work:
#15637 adds bounded transfer diagnostics; #15718 protects native
accepted finalization. This change addresses the separate legacy
recording order.

**Expected behavior**

Preserve the exact eligible source and record the existing board repair
obligation. Preserve cancellation, later task progress, and newer
execution ownership.

**Steps to reproduce**

1. Acquire an ephemeral remote lease for a legacy run.
2. Complete the dedicated stop-and-retain lifecycle and persist both
stop receipts.
3. Report a workspace copy-back failure from the original adapter
invocation.
4. Observe that the old recorder omits the released source. The
PostgreSQL regression reproduces this order.

**Paperclip version or commit**

de042e79ef.

**Deployment mode**

Server with PostgreSQL and a sandbox provider. The regression uses
disposable local PostgreSQL and synthetic provider receipts.

## What Changed

- Clone the realized host lease before adapter dispatch and carry it
into restore recording.
- Admit a released source only when its original and current policies
are ephemeral, its full ownership and workspace identity match, and its
dedicated retention receipt agrees with the independent
stopped-execution receipt.
- Reject reusable, expired, destroyed, rebound, competing, or
insufficiently proved sources. Preserve release state and receipts when
recording the existing no-replay repair action.
- Cover the actual stop-before-copy-back lifecycle, cleanup retries,
built-in dispatch, rollback, repeated recording, newer owners, and
ownership/receipt failures. Document the narrow scope.

## Verification

- `pnpm exec vitest run
server/src/__tests__/legacy-workspace-restore-recovery.test.ts
server/src/services/legacy-execution-recovery.test.ts`: 80 passed.
- Existing Grok confirmed-stop/copy-back boundary test: passed.
- The new PostgreSQL regression fails on the baseline because the
recovery marker is absent.
- Guard-removal controls fail when either the ephemeral-policy gate or
original-source identity checks are removed.
- `pnpm --filter @paperclipai/server typecheck`: passed.
- Additional competing-owner/lifecycle checks: 6 passed, including older
stopped and destroyed allocation owners.
- `pnpm -r typecheck` and `pnpm build`: passed.
- Independent review approved the final source. The exact ephemeral
regression fails on the baseline; policy/identity guard-removal controls
fail their intended assertions.
- Compatibility with #15789 was checked in a disposable combined
checkout: both commit orders yield the same tree. The earlier broad run
passed 136 cases; the final guard/SDK delta passed 9 cases.
- Greptile: 5/5 on the current head, with no review threads.
- Exact-head CI: 53 successful checks and 2 expected skips; no failures
or pending checks. This includes all test shards and Canary Dry Run.
- The duplicate local `pnpm test:run` aggregate was stopped after full
CI passed. It is not reported as a completed local suite.
- No live provider calls, historical data repair, run replay, or
deployment was performed.

## Risks

No schema or API change. A qualifying source remains held for manual
file repair and eventual removal, which can retain provider storage
longer. This fix does not recover historical files or establish that the
provider still holds the bytes. Reusable sources remain excluded from
this new path because provider resume can start before the database
handoff. Existing active-source and previously recorded recovery
behavior is unchanged.

## Model Used

OpenAI Codex, GPT-6 family, with reasoning, repository tools, and
independent agent review. The exact model revision and context-window
size are not exposed to this session.

## 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

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip authored and GitHub committed 2026-10-10 01:27:03 -07:00
1 parent caa7895f9a
commit 7e10dbe652
6 files changed
+391 -17

No files matched your search

@@ -1,12 +1,13 @@
import { randomUUID } from "node:crypto";
import { and, eq, sql } from "drizzle-orm";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { agents, companies, createDb, environmentLeases, environments, heartbeatRuns, issueRecoveryActions, issues } from "@paperclipai/db";
import { agents, companies, createDb, environmentLeases, environments, executionWorkspaces, heartbeatRuns, issueRecoveryActions, issues, projects } from "@paperclipai/db";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
import { recordLegacyWorkspaceRestoreFailure, legacyExecutionNeedsReconciliationWithEvidence, terminalizeLegacyExecution } from "../services/legacy-execution-recovery.js";
import { hasConversationContinuationPolicy, conversationRecoveryActionPredicate } from "../services/conversation-continuation.js";
import { preserveWorkspaceRestoreRecoveryMetadataSql } from "../services/legacy-workspace-restore-recovery.js";
import { settleStopOnlyCleanup } from "../services/sandbox-stop-and-retain.js";
import { hasConfirmedSandboxStopAndRetain, prepareSandboxStopAndRetain, settleStopOnlyCleanup } from "../services/sandbox-stop-and-retain.js";
import { environmentService } from "../services/environments.js";
import { hasRequiredWorkspaceRecovery } from "../services/workspace-restore-recovery-state.js";
const externalTestDatabaseUrl = process.env.PAPERCLIP_TEST_DATABASE_URL?.trim();
@@ -22,14 +23,20 @@ const support = externalTestDatabaseUrl ? { supported: true } : await getEmbedde
await db.insert(companies).values({ id: companyId, name: "Workspace recovery", issuePrefix: companyId.slice(0, 8) });
await db.insert(agents).values({ id: agentId, companyId, name: "Engineer", role: "engineer", adapterType: "codex_local" });
await db.insert(issues).values({ id: issueId, companyId, title: "Restore files", status: options.taskDone ? "done" : "in_progress", assigneeAgentId: agentId });
const [run] = await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, status: "running", runtimeMode: "legacy", contextSnapshot: { issueId } }).returning();
const [project] = await db.insert(projects).values({ companyId, name: "Workspace project" }).returning();
const [workspace] = await db.insert(executionWorkspaces).values({ companyId, projectId: project.id,
sourceIssueId: issueId, mode: "isolated", strategyType: "git_worktree", name: "Fixture workspace", cwd: "/work/fixture",
}).returning();
const [run] = await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, issueId, status: "running", runtimeMode: "legacy",
contextSnapshot: { issueId, executionWorkspaceId: workspace.id },
}).returning();
if (options.local) await db.insert(environments).values({ name: "Local", driver: "local" }).onConflictDoNothing();
const [environment] = options.local ? await db.select().from(environments).where(eq(environments.driver, "local"))
: await db.insert(environments).values({ name: `Isolated test sandbox ${runId}`, driver: "sandbox" }).returning();
const [lease] = await db.insert(environmentLeases).values({ companyId, issueId, heartbeatRunId: runId, environmentId: environment.id,
const [lease] = await db.insert(environmentLeases).values({ companyId, issueId, executionWorkspaceId: workspace.id, heartbeatRunId: runId, environmentId: environment.id,
status: "active", leasePolicy: options.reusable ? "reuse_by_environment" : "ephemeral",
provider: options.local ? "local" : "daytona", providerLeaseId: randomUUID(),
metadata: { driver: options.local ? "local" : "sandbox", pluginId: randomUUID(), sandboxProviderPlugin: !options.local },
metadata: { remoteCwd: "/work/fixture", workspaceRealization: { authoritativeRoot: "/work/fixture" }, driver: options.local ? "local" : "sandbox", pluginId: randomUUID(), sandboxProviderPlugin: !options.local },
}).returning();
return { companyId, runId, issueId, run, lease };
}
@@ -38,6 +45,22 @@ const support = externalTestDatabaseUrl ? { supported: true } : await getEmbedde
} };
const readLease = async (id: string) => (await db.select().from(environmentLeases).where(eq(environmentLeases.id, id)))[0];
async function stopAndRetain(f: Awaited<ReturnType<typeof seed>>, retry = false) {
let stopping = (await prepareSandboxStopAndRetain(db, f.lease))!;
expect(stopping.status).toBe("pending_cleanup");
if (retry) {
[stopping] = await db.update(environmentLeases).set({ metadata: {
...stopping.metadata, pendingCleanupAttemptId: randomUUID(),
} }).where(eq(environmentLeases.id, f.lease.id)).returning();
}
const stopped = (await settleStopOnlyCleanup(db, stopping, {
attemptId: String(stopping.metadata!.pendingCleanupAttemptId),
receipt: { providerLeaseId: f.lease.providerLeaseId, state: "stopped" },
}))!;
expect(stopped.status).toBe("released");
return stopped;
}
it.each([false, true])("persists terminal status, stop-only source and board action atomically (task done=%s)", async taskDone => {
const f = await seed({ taskDone, reusable: true });
expect(await legacyExecutionNeedsReconciliationWithEvidence(db, { ...f.run, status: "failed", ...patch })).toBe(true);
@@ -69,6 +92,239 @@ const support = externalTestDatabaseUrl ? { supported: true } : await getEmbedde
expect((await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId)))).toHaveLength(1);
});
it("records the exact stopped source when provider cancellation finishes before copy-back", async () => {
const f = await seed({ taskDone: true });
const stopping = await prepareSandboxStopAndRetain(db, f.lease);
expect(stopping).not.toBeNull();
const stopped = await settleStopOnlyCleanup(db, stopping!, {
attemptId: String(stopping!.metadata?.pendingCleanupAttemptId),
receipt: { providerLeaseId: f.lease.providerLeaseId, state: "stopped" },
});
expect(stopped?.status).toBe("released");
await db.update(heartbeatRuns).set({ status: "cancelled", finishedAt: new Date(),
resultJson: { executionCancellation: { state: "acknowledged" } },
}).where(eq(heartbeatRuns.id, f.runId));
// This is the lease captured by the host before invoking the adapter.
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.runId));
expect(run.status).toBe("cancelled");
expect(run.resultJson?.executionCancellation).toEqual({ state: "acknowledged" });
expect(run.resultJson?.workspaceRestoreRecovery).toMatchObject({ leaseIds: [f.lease.id] });
const retained = await readLease(f.lease.id);
expect(retained).toMatchObject({ status: "released", leasePolicy: "retain_on_failure", cleanupStatus: "success",
releasedAt: stopped!.releasedAt, metadata: { remoteExecutionTermination: stopped!.metadata!.remoteExecutionTermination,
sandboxStopAndRetainReceipt: stopped!.metadata!.sandboxStopAndRetainReceipt } });
const [action] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId));
expect(action).toMatchObject({ status: "resolved", outcome: "blocked", ownerType: "board",
evidence: { automaticRecovery: { replay: "blocked" }, workspaceRestoreRecovery: { leaseIds: [f.lease.id] } } });
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0].status).toBe("done");
// A lost commit response can cause the exact adapter receipt to be recorded twice.
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
const repeated = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId));
expect(repeated).toHaveLength(1);
expect(repeated[0]).toMatchObject({ id: action.id, status: "resolved", outcome: "blocked", attemptCount: 1,
evidence: { automaticRecovery: { replay: "blocked" }, workspaceRestoreRecovery: { leaseIds: [f.lease.id] } } });
expect(await readLease(f.lease.id)).toEqual(retained);
});
it.each(["retry", "builtin"])("keeps the original stop proof after %s dispatch", async scenario => {
const f = await seed();
if (scenario === "builtin") {
[f.lease] = await db.update(environmentLeases).set({ provider: "fake", metadata: {
driver: "sandbox", remoteCwd: "/work/fixture",
} }).where(eq(environmentLeases.id, f.lease.id)).returning();
}
const stopped = await stopAndRetain(f, scenario === "retry");
if (scenario === "retry") expect((stopped.metadata!.sandboxStopAndRetainReceipt as Record<string, unknown>).requestId)
.not.toBe(stopped.metadata!.pendingCleanupAttemptId);
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
expect(await readLease(f.lease.id)).toMatchObject({ leasePolicy: "retain_on_failure", status: "released",
metadata: { sandboxStopAndRetainReceipt: stopped.metadata!.sandboxStopAndRetainReceipt } });
});
it.each(["id", "companyId", "heartbeatRunId", "environmentId", "issueId", "executionWorkspaceId", "provider", "providerLeaseId", "acquiredAt", "pluginId", "remoteCwd", "workspaceRealization"])(
"rejects a stopped source with a different original %s", async field => {
const f = await seed();
const stopped = await stopAndRetain(f);
const source = { ...f.lease, metadata: { ...f.lease.metadata } };
if (["pluginId", "remoteCwd", "workspaceRealization"].includes(field)) source.metadata[field] = "different-original-value";
else if (field === "acquiredAt") source.acquiredAt = new Date(source.acquiredAt.getTime() - 1);
else Object.assign(source, { [field]: randomUUID() });
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, source);
expect(await readLease(f.lease.id)).toEqual(stopped);
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId))).toEqual([]);
},
);
it.each(["missing_dedicated", "missing_termination", "destroyed", "foreign_receipt", "wrong_plugin", "wrong_method", "empty_request", "invalid_time", "before_acquisition", "after_release", "unequal_confirmation", "pending_intent", "cleanup_in_flight", "expired_allocation", "missing_release_time", "retained_without_stop", "failed_cleanup"])(
"does not infer retention from %s", async scenario => {
const f = await seed();
const stopped = await stopAndRetain(f);
const metadata = structuredClone(stopped.metadata!);
const dedicated = metadata.sandboxStopAndRetainReceipt as Record<string, unknown>;
const termination = metadata.remoteExecutionTermination as Record<string, unknown>;
if (scenario === "missing_dedicated") delete metadata.sandboxStopAndRetainReceipt;
if (scenario === "missing_termination") delete metadata.remoteExecutionTermination;
if (scenario === "destroyed") termination.state = "destroyed";
if (scenario === "foreign_receipt") dedicated.runId = randomUUID();
if (scenario === "wrong_plugin") dedicated.pluginId = randomUUID();
if (scenario === "wrong_method") dedicated.method = "environmentReleaseLease";
if (scenario === "empty_request") dedicated.requestId = " ";
if (scenario === "invalid_time") dedicated.confirmedAt = termination.confirmedAt = "not-a-time";
if (scenario === "before_acquisition") dedicated.confirmedAt = termination.confirmedAt = new Date(stopped.acquiredAt.getTime() - 1).toISOString();
if (scenario === "after_release") dedicated.confirmedAt = termination.confirmedAt = new Date(stopped.releasedAt!.getTime() + 1).toISOString();
if (scenario === "unequal_confirmation") dedicated.confirmedAt = new Date(stopped.releasedAt!.getTime() - 1).toISOString();
if (scenario === "pending_intent") metadata.sandboxStopAndRetain = {};
if (scenario === "cleanup_in_flight") metadata.pendingCleanupInFlight = true;
const [invalid] = await db.update(environmentLeases).set({ metadata,
...(scenario === "retained_without_stop" ? { status: "retained" } : {}),
...(scenario === "expired_allocation" ? { expiresAt: new Date(stopped.acquiredAt.getTime() - 1) } : {}),
...(scenario === "missing_release_time" ? { releasedAt: null } : {}),
...(scenario === "failed_cleanup" ? { cleanupStatus: "failed" } : {}),
}).where(eq(environmentLeases.id, f.lease.id)).returning();
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
expect(await readLease(f.lease.id)).toEqual(invalid);
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId))).toEqual([]);
},
);
it("rejects invalid lease timestamps instead of treating receipt metadata as proof", async () => {
const f = await seed();
const stopped = await stopAndRetain(f);
expect(hasConfirmedSandboxStopAndRetain(stopped)).toBe(true);
for (const field of ["acquiredAt", "releasedAt", "expiresAt"] as const) {
expect(hasConfirmedSandboxStopAndRetain({ ...stopped, [field]: new Date(Number.NaN) })).toBe(false);
}
});
it.each(["missing_root", "empty_root", "invalid_realized_root", "run_workspace_mismatch"])(
"requires the original execution root after %s", async scenario => {
const f = await seed();
const metadata = { ...f.lease.metadata };
if (scenario === "missing_root") delete metadata.remoteCwd;
if (scenario === "empty_root") metadata.remoteCwd = " ";
if (scenario === "invalid_realized_root") metadata.workspaceRealization = { authoritativeRoot: " " };
[f.lease] = await db.update(environmentLeases).set({ metadata }).where(eq(environmentLeases.id, f.lease.id)).returning();
if (scenario === "run_workspace_mismatch") await db.update(heartbeatRuns).set({ contextSnapshot: {
...f.run.contextSnapshot, executionWorkspaceId: randomUUID(),
} }).where(eq(heartbeatRuns.id, f.runId));
const stopped = await stopAndRetain(f);
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
expect(await readLease(f.lease.id)).toEqual(stopped);
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId))).toEqual([]);
},
);
it("does not search arbitrary stopped leases when the original host snapshot is missing", async () => {
const f = await seed();
const stopped = await stopAndRetain(f);
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson);
expect(await readLease(f.lease.id)).toEqual(stopped);
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId))).toEqual([]);
});
it.each(["active", "stopped", "older_stopped", "older_destroyed"])("rejects a competing %s allocation owner across companies", async state => {
const f = await seed(), other = await seed();
const stopped = await stopAndRetain(f);
[other.lease] = await db.update(environmentLeases).set({ providerLeaseId: f.lease.providerLeaseId,
acquiredAt: new Date(f.lease.acquiredAt.getTime() + (state.startsWith("older_") ? -1_000 : 1)),
}).where(eq(environmentLeases.id, other.lease.id)).returning();
if (state !== "active") {
const stoppedOther = await stopAndRetain(other);
if (state === "older_destroyed") await db.update(environmentLeases).set({ metadata: {
...stoppedOther.metadata, remoteExecutionTermination: {
...(stoppedOther.metadata!.remoteExecutionTermination as Record<string, unknown>), state: "destroyed",
},
} }).where(eq(environmentLeases.id, other.lease.id));
}
const competing = await readLease(other.lease.id);
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
expect(await readLease(f.lease.id)).toEqual(stopped);
expect(await readLease(other.lease.id)).toEqual(competing);
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId))).toEqual([]);
});
it("does not claim a released reusable source from a stop receipt that may precede resume", async () => {
const f = await seed({ reusable: true });
const stopped = await stopAndRetain(f);
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
expect(await readLease(f.lease.id)).toEqual(stopped);
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId))).toEqual([]);
});
it.each(["original_reusable", "current_reusable", "expired", "root_rebound", "run_rebound"])(
"leaves a stopped source unchanged after %s", async scenario => {
const f = await seed({ reusable: scenario === "original_reusable" });
const other = await seed();
const stopped = await stopAndRetain(f);
const [changed] = await db.update(environmentLeases).set({
...(scenario === "original_reusable" ? { leasePolicy: "ephemeral" } : {}),
...(scenario === "current_reusable" ? { leasePolicy: "reuse_by_environment" } : {}),
...(scenario === "expired" ? { status: "expired" } : {}),
...(scenario === "root_rebound" ? { metadata: { ...stopped.metadata, remoteCwd: "/work/replacement" } } : {}),
...(scenario === "run_rebound" ? { heartbeatRunId: other.runId } : {}),
}).where(eq(environmentLeases.id, f.lease.id)).returning();
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
expect(await readLease(f.lease.id)).toEqual(changed);
expect(await readLease(other.lease.id)).toEqual(other.lease);
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId))).toEqual([]);
},
);
it("does not adopt the expired source when a normal reusable handoff wins first", async () => {
const f = await seed({ reusable: true });
await stopAndRetain(f);
const newerRunId = randomUUID();
await db.insert(heartbeatRuns).values({ id: newerRunId, companyId: f.companyId, agentId: f.run.agentId,
status: "running", runtimeMode: "legacy", contextSnapshot: { issueId: f.issueId } });
// The production handoff also checks the immutable task/agent scope.
[f.lease] = await db.update(environmentLeases).set({ metadata: {
...(await readLease(f.lease.id)).metadata, agentId: f.run.agentId,
} }).where(eq(environmentLeases.id, f.lease.id)).returning();
const newer = await environmentService(db).acquireLease({ companyId: f.companyId,
environmentId: f.lease.environmentId!, issueId: f.issueId, executionWorkspaceId: f.lease.executionWorkspaceId, heartbeatRunId: newerRunId,
leasePolicy: "reuse_by_environment", provider: f.lease.provider!, providerLeaseId: f.lease.providerLeaseId,
metadata: { ...f.lease.metadata, agentId: f.run.agentId }, replacesReusableLeaseId: f.lease.id,
});
const expired = await readLease(f.lease.id);
expect(expired.status).toBe("expired");
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
expect(await readLease(f.lease.id)).toEqual(expired);
expect(await readLease(newer.id)).toEqual(newer);
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId))).toEqual([]);
});
it("rolls back the stopped-source hold and action if recording cannot commit", async () => {
const f = await seed();
const stopped = await stopAndRetain(f);
await expect(db.transaction(async tx => {
await recordLegacyWorkspaceRestoreFailure(tx as unknown as typeof db, f.run, patch.resultJson, f.lease);
throw new Error("recording transaction failed");
})).rejects.toThrow("recording transaction failed");
expect(await readLease(f.lease.id)).toEqual(stopped);
expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.runId)))[0].resultJson).toBeNull();
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId))).toEqual([]);
});
it("preserves newer task execution while retaining only the original stopped source", async () => {
const f = await seed(), newerRunId = randomUUID();
await stopAndRetain(f);
await db.insert(heartbeatRuns).values({ id: newerRunId, companyId: f.companyId, agentId: f.run.agentId,
status: "running", runtimeMode: "legacy", contextSnapshot: { issueId: f.issueId } });
await db.update(issues).set({ executionRunId: newerRunId, checkoutRunId: newerRunId }).where(eq(issues.id, f.issueId));
const [newerLease] = await db.insert(environmentLeases).values({ companyId: f.companyId, issueId: f.issueId,
heartbeatRunId: newerRunId, environmentId: f.lease.environmentId, provider: "daytona", providerLeaseId: randomUUID(),
status: "active", leasePolicy: "ephemeral", metadata: { driver: "sandbox", pluginId: randomUUID() },
}).returning();
await recordLegacyWorkspaceRestoreFailure(db, f.run, patch.resultJson, f.lease);
expect(await readLease(f.lease.id)).toMatchObject({ leasePolicy: "retain_on_failure" });
expect(await readLease(newerLease.id)).toEqual(newerLease);
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0])
.toMatchObject({ status: "in_progress", executionRunId: newerRunId, checkoutRunId: newerRunId });
const [action] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, f.issueId));
expect(action.evidence.automaticRecovery).toMatchObject({ replay: "blocked" });
});
it("leaves local lock retry and successful remote cleanup policy unchanged", async () => {
const local = await seed({ local: true });
const failure = { ...patch, resultJson: { ...patch.resultJson, workspaceRestoreFailure: "restore_lock_timeout" } };