Skip to content

Keep one pending retry per thread for accepted turn failures - #6842

Merged
ymichael merged 1 commit into
mainfrom
bb/triage-and-resolve-issue-6826-thr_7i4i8xy7rf
Nov 2, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/triage-and-resolve-issue-6826-thr_7i4i8xy7rf

Conversation

@ymichael

@ymichael ymichael commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

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. retryFailedTurn only refused a second retry of the same original request (hasQueuedRetryOfTurnRequest matches on retryOfTurnRequestId), 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.html

What changed

  • apps/server/src/services/threads/turn-retry.ts: a scheduled retry (sendAt set) 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 new sendAt (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", its turnRequestId, attempt and queuedMessageId).
  • packages/db/src/data/queued-thread-messages.ts: getFirstUnclaimedQueuedRetry finds that row with a targeted query.
  • Unchanged: input the provider never accepted keeps its own retry row and is replayed verbatim, so no message is dropped. An immediate retry (no sendAt) still dispatches now. A second retry of the same original is still refused with retry_already_queued.
  • Docs: the bb thread retry guide text, the provider-retry skill, and the provider-retry PLUGIN_OVERVIEW.md.
  • No wire or protocol change; server only.

Deviations from the issue's proposal:

  • Never-accepted inputs are not merged into the existing row as input groups. They still drain as separate turns, because a combined turn needs a policy for differing execution settings (model, reasoning, permission mode) and retry-chain ownership.
  • Optional item 2 (holding new messages behind a pending retry) and item 3 (per-thread counts in bb provider-retry status) are not done. With this change, a thread shows at most one pending retry for accepted input.

How you verified

  • New test 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 latest sendAt, and drain as one recovery turn. It fails on main (3 rows) and passes with the fix.
  • New test 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
  • Server tests for retries, the queue and dispatch (12 files, 181 tests); @bb/db, @bb/templates and provider-retry plugin tests; shipped-skill and plugin-catalog tests. All pass.
  • End to end on 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 3 client/turn/requested, 3 turn/input/accepted and 1 queued retry row. The thread's queue card showed a single "Retry failed turn · Rate limited · attempt 2".

Fixes #6826

AGENT GENERATED

🤖 Generated with Claude Code

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
ymichael merged commit b2833ee into main Nov 2, 2026
51 checks passed
@ymichael
ymichael deleted the bb/triage-and-resolve-issue-6826-thr_7i4i8xy7rf branch November 2, 2026 19:12
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

provider-retry: one pending retry per thread, not per failed request

1 participant