Skip to content

Fix provider retries for exhausted account pools - #5283

Merged
ymichael merged 1 commit into
mainfrom
bb/retry-plugin-conflict-thr_x92t8cc5k3
Oct 9, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/retry-plugin-conflict-thr_x92t8cc5k3

Conversation

@ymichael

@ymichael ymichael commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Account Pooler could reject an exhausted pool with HTTP 429 while Provider Retry received no structured subscription-window snapshot. The pool knew when an account would become usable, but the retry policy silently declined the failed turn. Live verification also found that Codex wraps exhausted native retries as too-many-failed-attempts while retaining HTTP status 429, which the category-only check missed.

What changed

  • Account Pooler exposes the discoverable provider-retry.v1.availability RPC for a failed thread/request. It verifies the recorded route and latest request, uses that request's model, and returns ready, blocked with a recovery timestamp, unavailable with a reason, or not-routed.
  • Pool recovery uses the earliest usable account after all applicable blocking windows and temporary holds clear. The HTTP rejection and RPC share this calculation; parent pools provide the same information through an authenticated endpoint.
  • Provider Retry consults pool availability before provider-local quota snapshots, recognizes structured HTTP 429 even when wrapped by Codex, and retains the existing durable queue, attempt cap, maximum wait, buffer, and jitter.
  • Added persisted last-decision diagnostics through decision.get and bb provider-retry explain [thread-id] [--json], with CLI guide and skill updates. Diagnostics describe historical decisions; status remains the current queue view.

No server/daemon wire changes or protocol version bump. No backfill of previously failed turns. The availability contract assumes one routing owner per thread; the first applicable source wins.

How you verified

  • pnpm exec turbo run test typecheck lint --filter=bb-plugin-account-pool --filter=bb-plugin-provider-retry: 356 Account Pooler tests and 20 Provider Retry tests passed. After the live Codex discovery, reran Provider Retry's tests/typecheck/lint: all 21 tests passed. Account Pooler lint has two existing React warnings and no errors.
  • pnpm exec turbo run test --filter=@bb/server -- test/threads/turn-failed-retry.test.ts: all 13 tests passed, including a real database integration that queues a durable retry without a provider quota event. Confirmed the regression fails against the old retry handler because no row is queued.
  • pnpm exec turbo run typecheck lint --filter=@bb/server passed. git diff --check passed.
  • Started this checkout with pnpm start:worktree and ran real Claude Code and Codex clients through controlled HTTP 429 injection. Claude queued a retry without a provider quota event, and the queue survived a server restart; successful Claude completion remains unverified because the real account was exhausted.
  • The first live Codex run reproduced the wrapped-429 gap. Added a regression that fails before the fix and passes after it, rebuilt the worktree server, and reran with a fresh thread. Native retries exhausted, Provider Retry queued attempt 2, and the scheduled attempt dispatched without manual intervention. The actual upstream provider returned HTTP 200 and CODEX_DURABLE_RETRY_OK; the thread became idle and its retry queue emptied. Verified the queue card and completed response in the browser, including after reload.

AGENT GENERATED

@ymichael
ymichael merged commit 93ba274 into main Oct 9, 2026
88 of 89 checks passed
@ymichael
ymichael deleted the bb/retry-plugin-conflict-thr_x92t8cc5k3 branch October 9, 2026 21:02
stephendolan added a commit to stephendolan/bb that referenced this pull request Oct 13, 2026
Incorporate upstream get-bb#5130, get-bb#5226, get-bb#5283 and settings/clipboard fixes. Keep retry availability scoped to durable conversation assignments, including parent pools and spend-control recovery. Build the managed package against BB 0.46.0 SDK 0.6.37.
ymichael added a commit that referenced this pull request Oct 20, 2026
## Human comments

## What was wrong

Provider retry only scheduled retries for two kinds of failure:
structured overloads (529) and rate limits. Every other failure was
declined as `not-retryable`. When the Account Pooler, or the provider
itself, briefly cannot be reached, Claude Code records the failure as
`{category: "internal", httpStatusCode: 502}` and Codex records
`{category: "stream-disconnected", httpStatusCode: 502}`. Neither
matched, so the thread stayed in `error` until someone sent a new
message. The 429 half of #4839 was already fixed by #5283; this PR
covers the 502/503 half. Nothing in the history shows 5xx failures being
excluded on purpose: #1027 scoped the plugin to subscription limits, and
#2801 added only `overloaded`.

## What changed

- `plugins/provider-retry/src/retry-policy.ts`: a failure counts as
"provider unavailable" when its HTTP status is 500, 502, 503 or 504, or
its category is `internal`, `connection-failed` or
`stream-disconnected`. Such failures are retried for every thread,
pooled or not, with exponential backoff that starts at 1 minute: 1–2,
2–4, 4–8 and 8–16 minutes with jitter. They share the existing 5-attempt
cap, and the queue card shows "Provider unavailable". Overload timing is
unchanged; the backoff helper now takes a base delay. Rate-limit
failures keep their existing handling. `unauthorized` (401) and other
permanent failures are still declined.
- The longer base delay is deliberate. Providers already retry 5xx
errors internally for a few minutes, so the 5-second overload backoff
would use up the attempt cap during an outage like the 30-minute one
reported in #4839.
- Docs: the provider-retry overview, skill and package description, plus
the `bb guide plugins` and `bb guide providers` templates.
- No server/daemon wire change, CLI change or protocol bump.

## How you verified

- **Production evidence (from a local bb.db):**
- Pooler 502s on 2026-09-23 were recorded with exactly the `errorInfo`
shapes above.
  - Each affected thread sat idle about 20 minutes, until a manual send.
- The 429 wave on 2026-10-20 was retried automatically, confirming #5283
works.
- **Regression tests (`plugins/provider-retry/server.test.ts`):**
- Policy cases built from the recorded shapes: Claude Code pooler 502,
Codex stream-disconnected 502, Codex too-many-failed-attempts 503, and a
connection failure.
  - One plugin-level case that queues a "Provider unavailable" retry.
- All 5 new tests fail against the previous policy and pass with this
change.
- The existing decline test used a 500 as its example of a permanent
failure; it now uses a 401.
- **Commands:**
- `pnpm exec turbo run test typecheck lint
--filter=bb-plugin-provider-retry --filter=@bb/templates`: 26 and 46
tests pass.
- `pnpm exec turbo run test --filter=@bb/server --
test/threads/turn-failed-retry.test.ts
test/services/plugins/plugin-authoring-docs.test.ts
test/services/plugins/builtin-plugins.test.ts`: 60 tests pass.
- `pnpm exec turbo run test typecheck --filter=@bb/plugin-registry
--filter=@bb/bundled-plugins`: pass.
  - `oxfmt --check` is clean.
- **Live test (`pnpm start:worktree`):**
- Claude Code was routed through a local proxy that returns the pooler's
exact "502 Account Pooler could not reach Anthropic." response.
- The follow-up turn failed and a "Provider unavailable · attempt 2"
retry was queued about 90 s later.
- Attempt 2 failed while the outage was still on, and attempt 3 was
queued about 4 minutes later.
- After the outage ended, attempt 3 ran on its own and the agent
replied.
  - A 401 from the same proxy was recorded as `Skipped: not-retryable`.

Fixes #4839

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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.

1 participant