Files
PaperClipAI/doc
Devin FoleyandPaperclip 6c7b136b16 fix(native): keep startup recovery running when one run's claim fails (#18931)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - At startup, the server runs native restart recovery before it
reports ready. Recovery claims each interrupted or retryable native run
in its own transaction.
> - On one instance, one run's claim hit the routine execution unique
index. The error escaped restart recovery, startup recovery failed
closed, health stayed "still starting", and two deploys rolled back.
> - #18916 fixed that exact conflict: it defers the run when its claim
hits `issues_open_routine_execution_uq`. Any other error in one run's
claim still aborts the whole pass, and so does a failed per-run audit
write.
> - This pull request handles every per-run claim failure the way #18916
handles the routine conflict, and keeps a failed audit write from
stopping committed claims.
> - The benefit is that one bad row cannot keep the server from becoming
ready, and no run is claimed or changed when its claim fails.

## Linked Issues or Issue Description

Refs #18916. This change generalizes its catch: #18916 keeps the
routine-conflict disposition (`routine_execution_busy`) exactly as it
is, and this change adds a separate reason for any other per-run
failure. It does not change how the routine conflict is detected or
deferred. Refs #18879, whose lock reclaim exposed the startup failure.
Refs #18877 (open), which edits other parts of the same files. This
change touches only the claim's error path and the audit loop, so a
second merge needs at most a small rebase.

**What happened?**

Two deploys never became healthy. About 25 seconds after the listener
bound, the boot log showed `startup native runner restart recovery
failed closed` and `startup heartbeat recovery failed`, both from a
failed `update "issues" set "execution_run_id" = ...` that violated
`issues_open_routine_execution_uq`. #18916 now defers that violation.
Any other error in one run's claim (another constraint, a trigger, a
timeout) still escapes `claimNativeRestartRecoveries` and fails startup
the same way. #18916's own test asserts this: "Other database failures
still fail the recovery pass."

**Expected behavior**

One run's recovery failure is logged and recorded on that run, and the
rest of startup recovery continues. The server becomes ready. Only a
failure of the candidate scan itself fails the pass.

**Steps to reproduce**

1. Create a native run that is due for restart recovery, and make its
claim fail (the new test uses a trigger on `native_run_finalizations`).
2. Add a healthy run that is due for restart recovery.
3. Run restart recovery for both. On `master` the batch throws and the
healthy run is not claimed.

**Paperclip version or commit**

`master` at `4a83ba4050f`.

**Deployment mode**

Any server that runs startup native restart recovery.

## What Changed

- `claimNativeRestartRecoveries`: #18916's `.catch` on each candidate's
claim transaction no longer rethrows other errors. The whole claim has
rolled back, so the run's state is unchanged and nothing is claimed. The
run gets `awaiting_evidence` with the same 30-second retry deadline. The
reason stays `routine_execution_busy` for the routine slot conflict, and
is the new `restart_recovery_failed` for any other error, which is
logged at error level. The batch continues.
- `recoverNativeRunsAfterRestart`: records a `restart_recovery_failed`
deferral as a warning transition on the run ("Native restart recovery
failed for this run and will retry"). A failed `getRun` or transition
write is logged and no longer stops the dispatch of claims that already
committed.
- `doc/execution-semantics.md`: replaces "Other database failures still
fail the recovery pass" with the per-run rule.

## Verification

- Failing first: `native-restart-recovery-isolation.test.ts` (new,
PostgreSQL). It seeds the reported shape (two open tasks of one routine:
the older with a due scheduled resume and a cleared lock, the newer
holding the lock), a run whose claim fails through a trigger, and a
healthy run. On `master` the batch threw `injected recovery claim
failure`. Now the routine run is deferred with `routine_execution_busy`
and the failing run with `restart_recovery_failed`, and both keep their
coordinator, attempt and locks. The healthy run is claimed.
`recoverNativeRunsAfterRestart` completes and records an info transition
for the routine run and a warning transition for the failing run.
- Two existing tests expected the pass to reject on one run's failure:
#18916's unrelated-constraint test, and failed-workspace retention's
rollback test. They now expect the per-run deferral. Their rollback
checks (no claim, unchanged coordinator, lease, lock and recovery
actions) are unchanged, and the unrelated failure still does not become
a routine wait.
- `cd server && npx vitest run` over restart recovery (isolation,
integration, diskless, preparation), controller recovery, heartbeat
recovery, scheduled resume lock, failed-workspace recovery, startup
cleanup, native resumption, live recovery work, startup feedback export,
recovery deadlines, runtime selection, stop accounting, warm transition
restart, workspace reconstruction, remote workspace migration and
heartbeat process recovery: all pass except one.
`heartbeat-process-recovery` "redacts opaque environment-bound
credentials from Sentry diagnostics: result" fails the same way on
unmodified `master` on this host (a local environment value is
redacted).
- Review round 1: two `heartbeat/recovery.test.ts` cases make `getRun`
and then `appendRunEvent` fail for a deferred run while a committed
claim is in the same batch. Recovery completes and dispatches the claim
exactly once; the `appendRunEvent` case also checks the warning
transition. Both fail when the catches are removed. In one of four full
runs of that file, the unrelated orphan-cleanup case "reconstructs
missing native preservation during orphan cleanup without replay:
reuse_by_environment" failed; it passed alone and in the other three
full runs.
- After the rebase onto `4a83ba4050f`: the isolation and
failed-workspace recovery tests and `pnpm --filter @paperclipai/server
typecheck` pass.
- Not verified: a deploy to a real instance.

## Risks

- A persistent per-run error now retries every 30 seconds and logs an
error each time, instead of stopping startup. The run stays unclaimed,
so ownership does not change.
- If the database fails for every candidate's claim, startup recovery
now completes with every run deferred, instead of failing closed. The
candidate scan still fails closed, and the deferred runs retry on their
deadlines.
- No schema migration.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) through Claude Code, with tool use
(shell, file edits, test runs). The runtime does not expose the context
window or reasoning configuration.

## 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
- [ ] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-03 21:05:27 -08:00
..
…