Files
PaperClipAI/server/src/services/legacy-source-unavailable-continuation.test.ts
T
DottaandClaude Opus 5.5 ca23330c69 fix(recovery): continue a task on its own when its saved files are permanently lost (#21541)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - When a run stops early, Paperclip recovers its saved files and then
continues the task automatically
> - Sometimes the sandbox is gone. Then saved-work recovery ends with
`source_unavailable` (use another strategy)
> - In that state the task showed Retry. Retry worked, because it
continues in a fresh workspace. But nothing continued the task
automatically
> - A task must never need a person's Retry for a system failure. The
target is 0%
> - This pull request lets the system take the same fresh-workspace step
that Retry takes, when the saved files are permanently lost
> - The benefit is that these tasks continue on their own, and the sweep
reports them correctly

## Linked Issues or Issue Description

Refs #21478

**What happened?**
A legacy run on a remote sandbox failed in setup. Its unsaved files were
held for saved-work recovery. The sandbox was gone, so the recovery
ended `source_unavailable` / `use_alternative_strategy`. The task then
showed Retry. For about 2 hours, the assigned-work sweep logged
`retry_offered` with outcome `not_started` every 15 minutes. A person
had to press Retry.

**Root cause**
The fresh-continuation fallback returned `person_owned` /
`retry_needed`. It does this when a later run of the task was withheld
at dispatch (here, a scheduled retry that the hold blocked) and the
source run does not continue automatically. The source was a person's
earlier Retry that failed with `setup_failed`, so it was not an
automatic recovery run and not a restart-class failure. Without the
withheld run, the same task already continued automatically.
Restore-hold ownership (`isOwnedReconciliationHold`) and the legacy
refusal in `inspectFallback` were not the blocker.

**Expected behavior**
When saved-work recovery proves that the files are permanently gone, the
system continues the task in a fresh workspace on its own. This is the
same path that Retry takes.

## What Changed

- `fresh-continuation-fallback.ts`: the eligibility rule
(`interruptionEligibility`, which the fallback and the backlog pass
share) also accepts a system-owned source run when a hold of that run
has saved-work recovery `exhausted` with a permanent unverifiable-source
failure (`UNVERIFIABLE_SOURCE_CODES` ×
`PERMANENT_SOURCE_RETRY_CLASSES`).
- `recovery/assigned-work-sweep.ts`: admission can settle the hold and
coalesce the sweep's wake into a pending fresh continuation. The sweep
now reports this as `repaired` / `queued` (`continuedFromSavedWork`).
Before, it reported `enqueue_refused` / `not_started`.
- `legacy-execution-recovery.ts`: the stranded sweep calls
`terminalizeLegacyExecution` for the same failed run on every pass. Its
restore-hold branch rewrote the hold every time, with a new
`automaticRecovery.recordedAt`, a new `updatedAt`, and
`continuationDelivery: "invalidated"`. The branch now returns early when
the hold already records the same no-replay decision for this run, or
when a fresh continuation took the hold over. Finding: a continuation
that was already recorded was not clobbered, because the existing
`executionReconciliation.runId` guard returned early. But a hold with a
`coalesced` delivery state would have been reset, and the churn kept
bumping `updatedAt`.
- New regression test `legacy-source-unavailable-continuation.test.ts`.
The existing `assigned-work-invariant` test now expects the corrected
sweep outcome.

## Verification

- Red/green: `legacy-source-unavailable-continuation.test.ts` uses
`failedRemotePreparation()` with a `destroyed` termination receipt,
`recoverRetainedWorkspace`, a withheld scheduled retry, and a source
that was a person's Retry. Then it runs
`assignedWorkSweep(...).checkIssue` with the real
`heartbeatService(db).wakeup`.
- Without the fix: outcome `enqueue_refused`, and the fallback returns
`person_owned` / `retry_needed`.
- With the fix: outcome `repaired`. A stranded pass keeps
`continuationDelivery: "pending"`. `deliverReconciledExecutions` starts
the successor run. The task no longer shows Retry. There is no
`not_started` entry.
- Red/green: the "revisits" test fails without the
`terminalizeLegacyExecution` guard, because the hold evidence changes.
It passes with the guard.
- Locally passing: `fresh-continuation-fallback`,
`fresh-continuation-move-forward-offer`,
`fresh-continuation-retry-delivery`, `legacy-execution-recovery`, both
`assigned-work-invariant` suites, `assigned-work-stalled-queued-run`,
`assigned-work-sweep-round-trips`, `saved-work-recovery-overdue`, and
the new test file (10 files, 225 tests). `tsc --noEmit` passes for
`server`.
- Not run locally: the full suite. CI covers it.

## Risks

- Behavior shift: a task whose source failure is system-owned now
continues automatically after its saved files are proven permanently
gone, even when an earlier wake was withheld. The new run does not
replay the old run. It starts in a fresh workspace, with the same notes
and quarantine that Retry uses. Person-owned failure codes still wait
for a person.
- The new hold guard can skip a rewrite. It skips only when the hold is
already resolved with the same no-replay decision and the same
restore-recovery data. A changed decision is still written; the test
covers this.
- No schema changes.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use
(shell, file edits, local Vitest against embedded Postgres).

## 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
- [ ] All Paperclip CI gates are green
- [ ] 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-15 05:22:00 -06:00

147 lines
9.5 KiB
TypeScript

import { and, eq, sql } from "drizzle-orm";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { activityLog, environmentLeases, heartbeatRuns, issueRecoveryActions, issues } from "@paperclipai/db";
import type { AdapterSandboxExecutionTarget } from "@paperclipai/adapter-utils/execution-target";
import { readSystemRecovery } from "./system-recovery-state.js";
import { recoverRetainedWorkspace } from "./workspace-recovery.js";
import { heartbeatService } from "./heartbeat.js";
import { deliverReconciledExecutions } from "./execution-recovery-resolution.js";
import { taskInterruptionPresentation } from "./task-interruption-presentation.js";
import { assignedWorkSweep, ASSIGNED_WORK_CONTINUED_ACTION } from "./recovery/assigned-work-sweep.js";
import { remoteTerminationReceipt } from "./remote-execution-termination.js";
import { legacyReconciliationRunFields, terminalizeLegacyExecution } from "./legacy-execution-recovery.js";
import { action, connectionString, db, failedRemotePreparation, run, useWorkspaceRecoveryFixture, withheldPreparationRetry } from "./__tests__/workspace-recovery-fixture.js";
const targets = vi.hoisted(() => new Map<string, AdapterSandboxExecutionTarget>());
vi.mock("./environment-execution-target.js", async importOriginal => {
const actual = await importOriginal<typeof import("./environment-execution-target.js")>();
return { ...actual, resolveEnvironmentExecutionTarget: async (input: Parameters<typeof actual.resolveEnvironmentExecutionTarget>[0]) =>
(input.leaseId ? targets.get(input.leaseId) : undefined) ?? actual.resolveEnvironmentExecutionTarget(input) };
});
/**
* Staging, 2026-11-13: a legacy run on Daytona failed in setup and
* its files were held for saved-work recovery. The sandbox was gone, so the
* recovery ended `source_unavailable` (use another strategy). The task then
* offered Retry, and Retry worked: it continued in a fresh workspace. But
* nothing continued it automatically. For 2 hours the assigned-work sweep
* logged `retry_offered` / `not_started` every 15 minutes, because the hold
* refused its wake. A person had to press Retry.
*/
describe("a legacy run whose saved files are gone", () => {
useWorkspaceRecoveryFixture(targets);
beforeEach(() => {
vi.stubEnv("DATABASE_URL", connectionString);
vi.stubEnv("PAPERCLIP_SECRETS_MASTER_KEY", "63".repeat(32));
});
afterEach(() => { vi.unstubAllEnvs(); });
async function sourceGone(withheldRetry = true) {
const f = await failedRemotePreparation();
// The staging run was itself a person's Retry, so it is not an automatic
// recovery run, and the failure (setup_failed) is not a restart.
const source = await run(f.runId);
const finishedAt = new Date(Date.now() - 45 * 60_000);
await db.update(heartbeatRuns).set({ finishedAt, startedAt: new Date(finishedAt.getTime() - 10 * 60_000),
createdAt: new Date(finishedAt.getTime() - 11 * 60_000),
contextSnapshot: { ...source.contextSnapshot, wakeReason: "task_retry" } }).where(eq(heartbeatRuns.id, f.runId));
await db.update(issues).set({ status: "in_progress", statusVersion: 3 }).where(eq(issues.id, f.issueId));
// The provider destroyed the sandbox: its files can never be read again.
const [lease] = await db.select().from(environmentLeases).where(eq(environmentLeases.id, f.lease.id));
await db.update(environmentLeases).set({ metadata: { ...lease.metadata,
remoteExecutionTermination: remoteTerminationReceipt(lease, { providerLeaseId: f.providerLeaseId, state: "destroyed" }) } })
.where(eq(environmentLeases.id, f.lease.id));
await recoverRetainedWorkspace(f.input);
expect(readSystemRecovery((await action(f.actionId)).evidence)).toMatchObject({ state: "exhausted",
failure: { code: "source_unavailable", retryClass: "use_alternative_strategy" } });
// The hold withheld the scheduled retry that came after it.
if (!withheldRetry) return f;
const withheld = await withheldPreparationRetry(f);
await db.update(heartbeatRuns).set({ createdAt: new Date(finishedAt.getTime() + 60_000),
finishedAt: new Date(finishedAt.getTime() + 2 * 60_000) }).where(eq(heartbeatRuns.id, withheld.id));
return f;
}
/** What the stranded-work sweep does on every pass for this failed run. */
async function strandedPass(runId: string) {
const [source] = await db.select(legacyReconciliationRunFields).from(heartbeatRuns).where(eq(heartbeatRuns.id, runId));
await terminalizeLegacyExecution({ db, run: source, status: source.status, fromStatuses: [source.status] });
}
it("continues the task in a fresh workspace without a person's Retry", async () => {
const f = await sourceGone();
const issue = (await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]!;
const heartbeat = heartbeatService(db);
const sweep = assignedWorkSweep(db, { enqueueWakeup: (agentId, opts) => heartbeat.wakeup(agentId, opts) });
const check = await sweep.checkIssue({ companyId: f.companyId, issueId: f.issueId });
// The sweep's wake becomes the fresh continuation (the same path Retry
// takes), instead of a wake the hold refuses.
expect(check).toMatchObject({ verdict: { reason: "retry_offered" }, outcome: "repaired" });
// The stranded sweep revisiting the failed run keeps the recorded continuation.
await strandedPass(f.runId);
expect((await action(f.actionId)).evidence).toMatchObject({ continuationDelivery: "pending",
freshContinuation: { sourceRunId: f.runId }, executionReconciliation: { runId: f.runId } });
// The continuation worker starts the recorded continuation.
await deliverReconciledExecutions(db, (agentId, opts) => heartbeat.wakeup(agentId, opts));
// A run continues the task, in a workspace that is not the lost sandbox.
await vi.waitFor(async () => {
const [successor] = await db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, f.companyId),
sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${f.issueId}`, sql`${heartbeatRuns.id} <> ${f.runId}`,
sql`${heartbeatRuns.errorCode} is distinct from 'execution_reconciliation_required'`));
expect(successor).toBeDefined();
}, { timeout: 10_000, interval: 200 });
// The task no longer asks a person to press Retry.
expect((await taskInterruptionPresentation(db, issue))?.canRetry ?? false).toBe(false);
const refused = await db.select().from(activityLog).where(and(eq(activityLog.entityId, f.issueId),
eq(activityLog.action, ASSIGNED_WORK_CONTINUED_ACTION), sql`${activityLog.details}->>'outcome' = 'not_started'`));
expect(refused).toHaveLength(0);
expect((await run(f.runId)).status).toBe("failed");
}, 90_000);
it("leaves the hold as it is when the stranded sweep revisits the failed run", async () => {
const f = await sourceGone();
const before = await action(f.actionId);
await strandedPass(f.runId);
await strandedPass(f.runId);
const after = await action(f.actionId);
expect(after.evidence).toEqual(before.evidence);
expect(after.updatedAt).toEqual(before.updatedAt);
// A changed decision is still recorded: a hold that lost it gets it back.
await db.update(issueRecoveryActions).set({ evidence: { ...before.evidence, automaticRecovery: undefined } })
.where(eq(issueRecoveryActions.id, f.actionId));
await strandedPass(f.runId);
expect((await action(f.actionId)).evidence).toMatchObject({ automaticRecovery: { runId: f.runId, replay: "blocked" } });
}, 90_000);
it("counts the continuation as started when its worker delivers it before the sweep looks", async () => {
// No withheld retry: the sweep's own wake reaches the fallback in admission.
const f = await sourceGone(false);
const heartbeat = heartbeatService(db);
// The continuation worker delivers the run between admission and the sweep's read.
const sweep = assignedWorkSweep(db, { enqueueWakeup: async (agentId, opts) => {
const run = await heartbeat.wakeup(agentId, opts);
await deliverReconciledExecutions(db, (id, wake) => heartbeat.wakeup(id, wake));
return run;
} });
expect(await sweep.checkIssue({ companyId: f.companyId, issueId: f.issueId })).toMatchObject({ outcome: "repaired" });
expect((await action(f.actionId)).evidence).toMatchObject({ continuationDelivery: "delivered" });
const refused = await db.select().from(activityLog).where(and(eq(activityLog.entityId, f.issueId),
eq(activityLog.action, ASSIGNED_WORK_CONTINUED_ACTION), sql`${activityLog.details}->>'outcome' = 'not_started'`));
expect(refused).toHaveLength(0);
}, 90_000);
it("does not report a continuation again once it is stuck waiting for delivery", async () => {
const f = await sourceGone(false);
const heartbeat = heartbeatService(db);
let clock = Date.now();
const sweep = assignedWorkSweep(db, { enqueueWakeup: (agentId, opts) => heartbeat.wakeup(agentId, opts), now: () => new Date(clock) });
expect(await sweep.checkIssue({ companyId: f.companyId, issueId: f.issueId })).toMatchObject({ outcome: "repaired" });
// Nothing delivers it. Once its pending bound passes, a new wake adds nothing.
clock += 2 * 60 * 60_000;
expect(await sweep.checkIssue({ companyId: f.companyId, issueId: f.issueId })).not.toMatchObject({ outcome: "repaired" });
const queued = await db.select().from(activityLog).where(and(eq(activityLog.entityId, f.issueId),
eq(activityLog.action, ASSIGNED_WORK_CONTINUED_ACTION), sql`${activityLog.details}->>'outcome' = 'queued'`));
expect(queued).toHaveLength(1);
}, 90_000);
});