Files
PaperClipAI/server
DottaandClaude Opus 5.5 50633695e7 fix(native-runtime): keep external chat waits when finalization retries after the run settles (#23049)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Native runs can end by waiting for the next external chat message,
for example a GitHub review agent that waits for the next push
> - The finalizer must prove that the run still owns the task before it
records that wait
> - When the first status commit loses a lock race, the finalizer
retries later. By then the heartbeat has settled the run and released
the task execution
> - The retry then reads "authorization lost", so the task stays
`in_progress` with no run and no registered wait
> - This pull request keeps the wait authority for a settled run whose
own saved result is still being applied, and gives tasks already in this
state one bounded continuation
> - The benefit is that these tasks never sit idle with no next step

## Linked Issues or Issue Description

**What happened?**
A GitHub review agent published a review and yielded with a
`response_wake` continuation. The run ended with
`finalizationReasonCode: external_chat_response_wait_authorization_lost`
and `finalizationFailureCount: 1`. The task stayed `in_progress` with no
run for more than 16 hours. Earlier runs on the same tasks ended with
`external_chat_response_waiting`. Two tasks showed this on staging.

**Expected behavior**
The run records `external_chat_response_waiting`, and the task waits for
the next GitHub event. If the wait truly cannot be registered while chat
access is still allowed, the task gets one continuation instead of an
idle `in_progress` state.

**Steps to reproduce**
1. Start a native external-chat turn that yields with `response_wake`.
2. Hold a row lock that the status committer needs (for example the chat
endpoint) until the finalizer's quick retries end with
`status_cas_exhausted`.
3. Settle the run as `succeeded` and release the task execution, as the
heartbeat does.
4. Let the scheduled finalization retry run. The decision is
`external_chat_response_wait_authorization_lost`.

**Paperclip version or commit**
`master` at 85ad16b47e.

## What Changed

- `resolveExternalChatResponseWaitAuthorizationInTransaction` accepts a
settled owner in addition to a live owner. The check is
`settledRunOwnsResponseWait` in the new
`external-chat-wait-authorization.ts`. A settled owner is a `running` or
`succeeded` run with no other execution or checkout run on the task and
no newer run of the task in any state, and either:
  - its own saved result is still being finalized, or
- its committed decision is
`external_chat_response_wait_authorization_lost`.
The assignee, agent status, inbound message links, and current principal
and destination policy are still checked as before.
- The newer-run check is one shared predicate, `hasNewerIssueRun` in the
new `native-run-ownership.ts`. `readPreservedNativeResultFinalization`
now uses it too, and it compares timestamps in SQL.
- `readPublishedGitHubReviewCompletionContinuationInTransaction` lets
the same authorization decide ownership for a `succeeded` run.
- `EXTERNAL_CHAT_RESPONSE_WAIT_AUTHORIZATION_LOST` moves to
`status-arbiter.ts`, which owns the reason.
- The stranded-task sweep (`continueUnregisteredExternalChatWait`) now
finds a settled external-chat turn whose wait was lost while chat access
is still allowed. It queues one continuation
(`issue.external_chat_wait_recovery`) with `retryOfRunId` set to that
turn. It skips the task when a wake is already queued. The sweep has
already checked the budget at that point. A wait lost because chat
access was revoked stays parked, as before.
- The continuation uses an issue-state guard. Under the issue lock, the
queue checks that the task is still `in_progress` for the same assignee,
and that no wake or run already continues the source run (new
`withoutSuccessorOfRunId`). Overlapping sweeps therefore queue at most
one continuation.
- `chat-attachment-reuse.ts`, `recovery/service.ts` and
`heartbeat/queue.ts` were already over 1,000 lines. The new ownership
check is in its own module. The sweep helper and the queue guard stay
next to the code they extend.

## Verification

- Red, then green: `keeps the response wait when a contended
finalization is retried after the {run settled, execution released, run
settled and execution released}` in
`server/src/services/native-runtime/external-chat-wait.integration.test.ts`.
Before the fix all three cases recorded
`external_chat_response_wait_authorization_lost`.
- Red, then green: `continues a settled task whose response wait was
lost while chat access is still allowed` queues exactly one continuation
over two sweeps. The `revoked` case queues none.
- Red, then green: `queues one continuation for a lost wait when
recovery passes overlap through the real queue`. Without the queue guard
it saved two continuation wakes.
- `re-derives a published review wait when its contended finalization is
retried after the run …`: a settled published-review turn records
`external_chat_response_waiting` in `in_review`; with a newer queued run
it records `external_chat_response_wait_authorization_lost`.
- `authorizes a settled run's lost wait only with …`: another execution
run, another checkout run, a terminal finalization, or a committed
decision with another reason revokes the wait.
- `npx vitest run
src/services/native-runtime/external-chat-wait.integration.test.ts
src/__tests__/issue-list-review-wait-authorization.integration.test.ts
src/__tests__/chat-channels/github-review-response-maintenance.integration.test.ts
src/services/native-runtime/status-arbiter.test.ts` (in `server/`): 389
passed.
- `npx tsc --noEmit -p server/tsconfig.json`.

## Risks

- A settled run can now authorize its wait when no newer run owns the
task. A newer run in any state, another execution or checkout run, or a
run that is not `running` or `succeeded` still revokes the wait.
- Tasks already in this state get one continuation run after deploy. The
sweep skips the task when the budget blocks new work, and it queues at
most one continuation per stranded turn.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), with tool use and code execution
in Claude Code.

## 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-26 14:00:22 +00:00
..