Repository navigation
Recheck exhausted Account Pool usage before refusing a request - #4215
Merged
ymichael merged 3 commits intoSep 23, 2026
Merged
Conversation
When every account looked exhausted, the hub refused requests using usage data that could be up to five minutes old, so a plan upgrade or early reset kept threads blocked until the idle refresh ran. Before refusing, the hub now refreshes the enabled OAuth accounts it considers exhausted, at most once per 30 seconds per account, and retries selection once. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude Code sends two requests per turn. When both found no eligible account, the second skipped the recheck because the first had just stamped the 30 second throttle, and was refused even though the first request's refresh showed a recovered account. Join an in-flight refresh before the throttle applies, and always retry selection once after the recheck. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
deleted the
bb/investigate-account-pooler-issue-thr_3y6tvr5by6
branch
September 23, 2026 21:38
3 tasks done
This was referenced Oct 30, 2026
ymichael
added a commit
that referenced
this pull request
Nov 2, 2026
…he provider refuses them (#6849) ## Human comments ## What was wrong `quota.ts` had a single window predicate, `activeWindow()`, that treated a window as blocking when its status was `rejected` **or** its utilization was at or above `switchThreshold`. Every caller used it: selection in `hub.ts`, the pool's own 429 (`Retry-After` and the "expected to be available at" text), `provider-retry.v1.availability`, and `accountStatus()`. So the threshold acted as a hard cutoff. Once every account crossed 98%, the pool returned its own 429 even though the provider still accepted those accounts. The wait time came from the latest past-threshold window, often a 7-day window, so Provider Retry parked threads for days. Settings and `bb pool status` also called those working accounts "Exhausted" (#6169). Separately, `quotaRowSchema` was `.strict()`, so after a downgrade, any column added by a newer build made every quota read throw. ## What changed - **`src/quota.ts`**: Window logic is split into two predicates. A window is *refused* when its status is `rejected`. When the window has no status, it is refused once utilization reaches 1. A window is *past threshold* when it is refused or its utilization is at or above `switchThreshold`. `isQuotaExhausted(quota, family | null, now)` means refused (or usage-restricted). `isPastThreshold(quota, family | null, threshold, now)` keeps the old threshold meaning. `blockingResetAt` and `accountStatus` no longer take a threshold and count only refused windows. `isSharedQuotaExhausted` is gone; callers use `family = null` instead. - **`src/hub.ts` `select()`**: New conversations work as on main, except that the "below threshold if any is ready" filter now uses `isPastThreshold`. Behind it, accounts past the threshold that the provider has not refused come next, then refused accounts with extra usage or credits (main's only fallback). Within each group the pool keeps its usual order. The cursor and round-robin order are unchanged. - **Pins (behaviour change requested in review):** A pinned conversation now stays on its account past the threshold until the provider refuses it, even when an account below the threshold is ready. On main it moved. A conversation inheriting its parent's pin follows the same rule. Conversations pinned to a refused account that runs on extra usage or credits move to an account the provider accepts as soon as one is ready. On main that move happened only for accounts below the threshold, since main never used the others. Family detours are unchanged. - **Retry timing:** The #4215 usage recheck still runs before serving from a past-threshold account or refusing (`refreshExhaustedUsage` now selects accounts with `isPastThreshold`). `accountAvailability()` (429 text, `Retry-After`, and `provider-retry.v1.availability`) counts only refused windows. - **`app.tsx` / `src/cli.ts`**: "Exhausted" now means only that the provider refused the account. Its reset label uses refused windows. The threshold description, the quota detail line ("other accounts preferred from N%"), and CLI help now describe the threshold as a preference. - **Docs**: `skills/account-pool/references/accounts-and-routing.md`, `PLUGIN_OVERVIEW.md` (3,993 of 4,000 characters), and `docs/configuration.md` (`switchThreshold`). - **`src/store.ts`**: Cherry-picked 6bd890b865: `quotaRowSchema` no longer uses `.strict()`. I dropped that commit's `family_overage_until_json` column because main does not have it. - This is a plugin-local change with no SDK or host-daemon protocol changes. The ranked-tier rewrite (b4ad1a2b36) is deliberately not ported. That leaves out API-key ordering (#4468), overage-claim detection (#3615), pin promotion off paid accounts, the `tier` field, and the settings banner. **Behaviour choice beyond the issue (conservative):** When no account below the threshold is ready, accounts the provider still accepts come before refused accounts on extra usage or Codex credits. Without this, the sticky cursor or a pin could keep spending extra usage while another account still had subscription quota. Covered by `prefers an account past the threshold that the provider accepts over extra usage`. **Note:** As on main, the usage recheck (at most once per 30 s per account) runs whenever selection lands on an account past the threshold. That now includes the new fallback and pinned conversations staying on a past-threshold account, not only extra usage. ## How you verified New tests. Each fails on main's `quota.ts`/`hub.ts`/`app.tsx` and passes with the fix: - `sends new conversations to accounts past the switch threshold behind those below it until the provider refuses them`: two accounts at 99% and 100% `allowed` keep serving (main: `expected 429 to be 200`). A refused account fails over to the past-threshold one, and a new below-threshold account still wins. - `waits only for windows the provider refused before retrying`: one account is refused through a status-less 5h window at utilization 1, the other through a `rejected` 5h window. Both also have 7d windows at 0.99 `allowed`. `Retry-After` is 3600, the message shows that time, and `provider-retry.v1.availability` reports `retryAt = now + 1h` (main: `'432000'`, i.e. 5 days). - `keeps a conversation on its account past the switch threshold until the provider refuses it`: a pinned session stays on its 99% account while a new session goes to the account below the threshold. Once the provider refuses the pinned account, the session moves. This fails on main and on this PR's first commit, which kept main's pin rule. - `prefers an account past the threshold that the provider accepts over extra usage`: a new conversation and a conversation pinned to the paid account both go to the 99% account the provider still accepts. Without the ordering, both stay on the paid account. - `reports an account past the switch threshold as exhausted only once the provider refuses it`: `bb pool status --json` (main: `expected 'exhausted' to be 'ready'`). - `app.test.tsx` `dates an exhausted account by its refused window, not one past the threshold`: the settings label shows the refused 5h reset, not the 7d reset. - `store.test.ts` (cherry-picked): quota reads and writes succeed with an unknown extra column. `pnpm exec turbo run typecheck lint test --filter=bb-plugin-account-pool`: 10/10 tasks pass, 372 tests. The 2 lint warnings were already on main. Fixes #6169 🤖 Generated with [Claude Code](https://claude.com/claude-code) > AGENT GENERATED --------- 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
The hub decides eligibility from cached quota. While an account is idle, that cache only refreshes every five minutes, so when every account looked exhausted the hub returned 429 from data that could be minutes old. After a plan upgrade or an early window reset, threads stayed blocked until the idle refresh happened to run (or someone ran
bb pool account refresh <id>).What changed
plugins/account-pool/src/hub.ts: when selection finds no eligible account,forwardcalls a newrefreshExhaustedUsagebefore refusing. It refreshes the candidate accounts that are enabled, OAuth, not already attempted, error-free, and exhausted for the request's model family, then retries selection once per request. Each account is refreshed at most once per 30 seconds (EXHAUSTED_USAGE_REFRESH_INTERVAL_MS), so a client's retry storm doesn't hammer the usage endpoint. The wait is abortable with the request.refreshAccountUsagenow takes a minimum interval instead offorce: boolean(force =0, idle loop = five minutes) and joins an in-flight refresh before applying that interval. Claude Code sends two requests per turn, so concurrent refused requests share one recheck instead of the second being refused by the throttle the first just set.PLUGIN_OVERVIEW.mdand the skill'saccounts-and-routing.mddescribe the recheck and its 30-second limit.There are no wire, CLI, or protocol changes.
For context on the throttle: I captured real client traffic against a local always-refusing server. Claude Code 2.1.280 retries a 429 about 7 times over ~37s with byte-identical requests (
x-stainless-retry-countstays0). Codex 0.155.1 fails the turn on the first 429. So the per-account 30s limit allows at most a couple of usage checks per Claude Code retry storm.How you verified
refreshes exhausted usage before refusing a request, at most every 30 seconds per accountinserver.test.ts: fails without thehub.tschange (expected 429 to be 200) and passes with it. It covers the 30s throttle, repeated refusals inside the window, and recovery once usage drops.lets concurrent requests share an exhausted-usage recheck: holds the usage response until two concurrent requests are waiting; fails without the join ([200, 429]) and passes with it (5/5 repeated runs).pnpm exec turbo run typecheck lint test --filter=bb-plugin-account-pool: 7/7 tasks, 313 tests pass.pnpm start:worktreewith real Claude Code threads and two OAuth accounts backed by a fake Anthropic (mitmproxy standing in for the OAuth token, profile, usage and Messages endpoints): baseline turn, failover from an exhausted account, and a turn 32s after the fake started reporting a recovered account while the pool still cached both as exhausted. That turn rechecked both accounts once and was served by the recovered one. The concurrency bug above was found in this pass.Retry-Afteruntil the earliest reset, and an already-running Claude Code session waits that out before sending its next request. So the recheck helps new threads and freshly loaded runtimes; a live session only benefits once it sends again.🤖 Generated with Claude Code