Repository navigation
Keep one pending retry per thread for accepted turn failures - #6842
Merged
Merged
Conversation
When a thread hit a subscription limit, every later message started a new turn, failed, and got its own retry row, because retry admission only deduplicated on the original request id. After the reset each row drained as its own "Please continue." turn. A failure whose input the provider already accepted now joins the thread's first unclaimed retry row instead of queuing another nudge: that row moves to the new sendAt (the freshest reset estimate) and the response names it. Input the provider never accepted still gets its own retry row, so no message is dropped. Immediate retries (no sendAt) are unchanged. Fixes #6826 Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
added a commit
that referenced
this pull request
Nov 2, 2026
Nothing re-checked a queued retry after it was written. A newer message did
not affect it, so a scheduled "Please continue." or a hidden re-send of the
old input landed after the user had moved on. `bb thread wait` also failed at
once on a failed thread even when its retry was about to send.
- Appending a new turn request supersedes the retries of other turns, in the
same transaction: a retry whose chain the provider accepted is deleted; one
whose input was never accepted waits on the new `superseded` kind ("Not
sent") and is only sent by the user.
- A later accepted failure no longer joins a Not sent retry (the
`getFirstUnclaimedQueuedRetry` join from #6842 skips them), so it still gets
a retry that sends by itself.
- SDK `threads.wait` (and `bb thread wait`) keeps waiting on a thread in error
that has a retry which will send by itself, and names it on timeout.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
added a commit
that referenced
this pull request
Nov 2, 2026
## Human comments ## What was wrong Nothing re-checks a queued retry after it is created. `retryFailedTurn` queues a retry row whose content is either an agent-only "Please continue." (when the provider accepted the failed input) or a hidden re-send of the failed turn's input ([turn-retry.ts#L108-L135](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/apps/server/src/services/threads/turn-retry.ts#L108-L135), queued at [turn-retry.ts#L210-L228](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/apps/server/src/services/threads/turn-retry.ts#L210-L228)). Appending a new turn request does not look at the queue ([thread-events.ts#L548-L568](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/apps/server/src/services/threads/thread-events.ts#L548-L568)). The only code that deletes a queued retry is the message-edit path ([thread-edit-message.ts#L686-L691](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/apps/server/src/services/threads/thread-edit-message.ts#L686-L691) → [queued-thread-messages.ts#L1000](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/packages/db/src/data/queued-thread-messages.ts#L1000)). So when the user moves on and sends a new message, the old retry still fires later, either continuing a turn the user abandoned or re-sending input on top of the newer message. Separately, SDK `threads.wait` treats `error` as unreachable for `idle` and throws as soon as it sees it ([threads.ts#L1725](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/packages/sdk/src/areas/threads.ts#L1725)), so `bb thread wait` exits 4 even when a retry is queued to recover the thread by itself. This is the first step of the queue plan: the queue holds only unsent work, the event log records everything after send, and the retry row is the one "owed work" row. Here the retry row learns that it is owed only until something newer overtakes it. ## What changed - **Overtaken retries** (`supersedeQueuedRetriesInTransaction` in `packages/db/src/data/queued-thread-messages.ts`, called from `appendPreparedClientTurnRequestedEventWithNotificationInTransaction` in `apps/server/src/services/threads/thread-events.ts`): appending a turn request supersedes the queued retries of *other* turns in the same transaction. - If the provider accepted any input in the retry's chain (`turn/input/accepted` for the original request or any earlier retry of it), the retry row is deleted. - Otherwise the row waits on a new `waitingOn` kind, `superseded` (`packages/domain/src/queued-message.ts`). Automatic dispatch never claims it, and it doesn't block rows queued behind it. The user sends it with Send now or deletes it. - The append's notification includes `queue-changed` when a retry was superseded. `parent-system-messages.ts` now uses the append's own notification fields to deliver that. - **With #6842's retry join** (`getFirstUnclaimedQueuedRetry`): a scheduled retry of a later accepted failure joins the thread's waiting retry. That join now skips Not sent rows. Otherwise the later failure would attach to a row that never sends by itself and get no automatic retry. #6842's two tests now expect the combined behaviour: the earlier retry is dropped or Not sent when the next message overtakes it, and one retry still waits per thread. - **`wait` on a recovering thread** (`packages/sdk/src/areas/threads.ts`): when the thread is in a status that can't reach the target, `threads.wait` lists the thread's queue. If a retry will still send by itself (not `superseded`, not `failed_exhausted`), it keeps waiting. On timeout, the error names the retry and when it is scheduled. - Rendering of the new kind: app queue row (`apps/app/src/lib/queued-message-wait.ts`), CLI `Waiting on` column, and the server's send-now description. - Guides: `bb-guide-thread-lifecycle` (overtaken retries), `bb-guide-thread-messaging` (`superseded` kind), `bb-guide-thread-inspection` (`thread wait` exit codes). No daemon wire change, so no `HOST_DAEMON_PROTOCOL_VERSION` bump. Plugin SDK surface unchanged. ### Visible behaviour changes - A retry that a newer message overtook before the provider accepted its input now shows in the queue as **"Not sent · a newer message was sent after it"** (alert icon). The CLI shows `not sent: a newer message was sent after it`. It stays there until the user sends or deletes it. A retry that would only have continued an accepted turn disappears instead. - `bb thread wait` / `threads.wait` no longer exits 4 (unreachable) on a thread in error while a retry is pending that will send by itself. It keeps waiting. If it times out (exit 2), the message says `It failed and a retry is scheduled for <time>; wait again to follow it.` ## How you verified New tests. Each one that fails against main's source was confirmed by checking out main's non-test files and running them: - `apps/server/test/threads/turn-failed-retry.test.ts` › "a queued retry": - "is kept as Not sent when a newer message overtakes input the provider never accepted" (also sends it with Send now and checks the re-sent input): fails on main, passes here. - "is dropped when a newer message overtakes a turn the provider accepted": fails on main, passes here. - "does not hold back messages once it is Not sent": fails on main, passes here. - "gives an accepted failure its own retry when the thread's only retry is Not sent": fails without the join fix (the failure joins the Not sent row), passes here. - `apps/cli/src/__tests__/command-output/thread-wait.test.ts`: - "keeps waiting on a failed thread whose retry will send": fails on main, passes here. - "names the pending retry when it times out": fails on main, passes here. - "fails fast when the failed thread's retry is not sent" (`superseded` + `failed_exhausted` rows): passes on both. It guards the new check so these rows still exit 4. Commands: - `pnpm exec turbo run typecheck lint --filter='...[origin/main]'`: 153/153 tasks pass. - `pnpm exec turbo run test` for `@bb/server` (3824 passed), `@bb/db` (652), `@bb/domain`, `@bb/sdk`, `@bb/cli` (888), `@bb/templates` (47), after rebasing onto `c2365296a4`: all pass. - `apps/app` queued-message tests (6 files, 94 tests): pass. - `node .github/workflows/check-plugin-sdk-version.mjs`: "Plugin SDK surface unchanged." 🤖 Generated with [Claude Code](https://claude.com/claude-code) > AGENT GENERATED Co-authored-by: Claude Opus 5.5 <[email protected]>
ymichael
added a commit
that referenced
this pull request
Nov 4, 2026
Review follow-up. Retries going first is needed for resume: a message steered (the default send) while the daemon is down waits as host-offline, and the reconnect's host-connected wake sends it before the resume, which then drops the resume. Queued (thread-busy) rows already wait on an error thread, so they were never the problem. The earlier version refused automatic claims behind a live retry. That stranded exactly those host-offline rows: the reconnect wake is their only wake, so they stayed host-offline after the resume finished. Now the hold is a core wait in the dispatch checkpoint: a drained, non-retry row on a thread with a live retry is re-queued as thread-busy and sends when the retry's turn ends. Inline sends and Send now are not held, so they still overtake a pending retry. Both halves are behind continueThreadsAfterRestarts: retry rows are moved to the front of the queue only with it on, and the hold only applies with it on. With it off, retry order and holding are exactly as on main, and the two #6842 tests are back to main's version. The live-retry check is SQL built on #6727's notSupersededQueuedThreadMessage instead of a JSON parse. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
When a Claude or Codex thread hit a subscription limit, every later message to that thread started a turn, failed, and got its own retry row.
retryFailedTurnonly refused a second retry of the same original request (hasQueuedRetryOfTurnRequestmatches onretryOfTurnRequestId), and each later message is a new request. After the reset the rows drained one by one, each as its own "Please continue." turn: 28 for one lead in the issue. SlopCop reproduced this: https://get-bb.github.io/reports/issues/6826.htmlWhat changed
apps/server/src/services/threads/turn-retry.ts: a scheduled retry (sendAtset) of a failed turn whose input the provider already accepted now joins the thread's first unclaimed retry row instead of queuing another nudge. That row moves to the newsendAt(the freshest estimate of the reset; this also lets an account pool that reports a free account pull it earlier). The response names that row (delivery: "queued", itsturnRequestId,attemptandqueuedMessageId).packages/db/src/data/queued-thread-messages.ts:getFirstUnclaimedQueuedRetryfinds that row with a targeted query.sendAt) still dispatches now. A second retry of the same original is still refused withretry_already_queued.bb thread retryguide text, the provider-retry skill, and the provider-retryPLUGIN_OVERVIEW.md.Deviations from the issue's proposal:
bb provider-retry status) are not done. With this change, a thread shows at most one pending retry for accepted input.How you verified
joins later accepted failures to the thread's waiting retry: three accepted rate-limit failures, with user messages in between, leave one retry row, moved to the latestsendAt, and drain as one recovery turn. It fails on main (3 rows) and passes with the fix.keeps a separate retry for every input the provider never accepted: never-accepted inputs keep their own rows with their original text.pnpm exec turbo run typecheck lint --filter=@bb/server --filter=@bb/db --filter=@bb/templates@bb/db,@bb/templatesand provider-retry plugin tests; shipped-skill and plugin-catalog tests. All pass.pnpm start:worktree: I pointed Claude Code at a local fake Anthropic API that returns a 5-hour subscription-limit 429, using a fake OAuth token. I sent three messages to a Claude Code thread. Each turn was accepted and failed with "You've hit your 5-hour limit". provider-retry scheduled one retry; the next two failures joined the same row (qmsg_q9zkbr6es3), each moving its time. The database showed 3client/turn/requested, 3turn/input/acceptedand 1 queued retry row. The thread's queue card showed a single "Retry failed turn · Rate limited · attempt 2".Fixes #6826
🤖 Generated with Claude Code