Files
PaperClipAI/server
7b446b4504 fix(recovery): report an unrunnable recovery continuation once instead of every sweep (#21288)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Execution recovery delivers a recorded continuation to the task's
agent through a sweep that runs about every 15 seconds.
> - The sweep keeps a continuation `pending` until a wake for it is
admitted. A skipped wake leaves it pending, and the next pass tries
again.
> - The workspace preflight refuses a wake for an isolated git-worktree
task that has no project and no reusable workspace
(`workspace_worktree_requires_project`). Each refusal blocks the task
again, writes an activity row and posts a system comment.
> - Thus one refused continuation produced a new "blocked before
dispatch" comment about every 15 seconds, without end. A person's chat
collected hundreds of them in a few hours.
> - This pull request closes the continuation that the preflight
refuses, in the same transaction as the refusal.
> - The benefit is that the task is reported once and then waits for a
person, instead of a tight retry loop that floods its thread.

## Linked Issues or Issue Description

Refs #21250 (merged; fixes one way to reach this state: admission
refused a fresh isolated continuation of a projectless task. This PR
bounds the loop for any continuation that the preflight refuses.)

**What happened?**
A conversation task had `executionWorkspaceSettings.mode =
isolated_workspace`, no project, and a bound adapter-managed workspace.
A run failed at bootstrap. The fresh-continuation fallback could not
prove that the old writer had stopped, so it chose
`fresh_isolated_workspace`, cleared the task's `executionWorkspaceId`,
and recorded a continuation with `continuationDelivery: "pending"`. The
recovery wake sets `forceFreshSession`, so the preflight found no prior
session workspace. With no project and no binding, the preflight refused
the wake as `workspace_worktree_requires_project`. The continuation
stayed `pending`, so `deliverReconciledExecutions` woke it again on
every pass. Each pass posted the same system comment and activity row
(about every 15 s, for hours).

**Expected behavior**
A continuation that cannot start is reported once. The task stays
blocked with one clear comment until a person changes its workspace
settings. The sweep does not retry it in a tight loop.

**Steps to reproduce**
1. Enable isolated workspaces. Give a task `mode: isolated_workspace`,
`git_worktree` strategy, no project and no execution workspace.
2. Record a resolved recovery action for it with `continuationDelivery:
"pending"` and a valid `executionReconciliation`.
3. Run `deliverReconciledExecutions` three times.
4. See three "blocked before dispatch" comments and three
`issue.workspace_preflight_blocked` rows. The continuation stays
`pending`.

## What Changed

- `server/src/services/heartbeat/queue.ts`: when the workspace preflight
refuses a validated execution-reconciliation wake
(`reconciledRecoveryActionId`), it now sets the action's
`continuationDelivery` to `invalidated` with
`continuationInvalidatedReason: "workspace_worktree_requires_project"`,
and settles its coalesced holds in the same transaction as the block,
the comment, and the activity row. The sweep only reads `pending`
continuations, so it stops. (`queue.ts` already exceeded 1,000 lines;
this adds 10 lines inside the existing preflight refusal transaction
rather than splitting the transactional refusal path.)
- `server/src/__tests__/issue-recovery-actions.test.ts`: new regression
`"reports a reconciled continuation with an unrunnable workspace once
instead of retrying every sweep"` verifying across 3
`deliverReconciledExecutions` sweeps that only 1 comment, 1
`issue.workspace_preflight_blocked` activity row, and 1 skipped wakeup
request are recorded, the task is blocked, the primary continuation is
`invalidated` with `workspace_worktree_requires_project`, and a
coalesced sibling hold is also settled to `invalidated`.

## Verification

- Red, on unfixed `origin/master` (`2d06567795`): `cd server && npx
vitest run src/__tests__/issue-recovery-actions.test.ts -t "unrunnable
workspace once"` failed with `AssertionError: expected [ …(3) ] to have
a length of 1 but got 3` (three sweeps, three duplicate comments).
- Green, on rebased head (`7b2c4cdbda`):
- `cd server && npx vitest run
src/__tests__/issue-recovery-actions.test.ts -t "unrunnable workspace
once"`: 1 passed (120 skipped).
- `cd server && npx vitest run
src/__tests__/issue-recovery-actions.test.ts
src/__tests__/completed-turn-cleanup-hold-settle.test.ts`: 2 files, 131
passed (121 + 10).
  - `pnpm --filter @paperclipai/server typecheck`: passed.
- `node scripts/check-test-budget.mjs --files
server/src/__tests__/issue-recovery-actions.test.ts`: passed (110
nightly suites validated, 1 test file checked).
- `pnpm verify --explain` selected 1,944/2,301 Vitest files because
`queue.ts` is a shared server hub; focused recovery suites passed
locally and PR CI supplies full typecheck, build, and sharded suite
coverage.

## Risks

- Low risk. The change only runs on the preflight's refusal path, and
only for a validated execution-reconciliation wake.
- After this change, the refused continuation does not run by itself
when a person later fixes the workspace settings. The person's next
message or Retry starts the task through normal admission. This is the
same contract as the existing `in_review` invalidation.
- No migration. No schema change. No new query on the normal admission
path.

## Model Used

- Original PR: Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used
through Claude Code with tool use.
- Rebase onto `master`, conflict resolution, strengthened regression
test, and verification: Claude Opus 4.6 (`claude-opus-4-6`), Anthropic,
used through Antigravity with tool use (shell, file edits, git/gh).

## 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: lucktastic <13757243+lucktastic@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 15:31:34 -06:00
..