Skip to content

Keep Account Pooler accounts past the switch threshold usable until the provider refuses them - #6849

Merged
ymichael merged 5 commits into
mainfrom
bb/6169-account-pooler-when-every-account-is-past-s-thr_cxiiebtxyd
Nov 2, 2026
Merged

ymichael merged 5 commits into
mainfrom
bb/6169-account-pooler-when-every-account-is-past-s-thr_cxiiebtxyd

Conversation

@ymichael

@ymichael ymichael commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

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 Recheck exhausted Account Pool usage before refusing a request #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 (Account Pooler: last-resort accounts, per-account switch thresholds, and an opt-in order by earliest weekly reset #4468), overage-claim detection (Account Pooler bills Fable to a Team account's extra usage while a pooled Max account includes Fable #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

AGENT GENERATED

ymichael and others added 5 commits November 2, 2026 12:11
… know

The quota store reads `SELECT *` into a strict row schema, so a column added
by a later migration made every quota read throw on an older build. Running
an older build against the same plugin database, for example after a
downgrade, therefore broke the pool instead of just losing the newest fields.
The row schema now ignores unknown columns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he provider refuses them

The pool treated every window at or above switchThreshold as exhausted. Once
all accounts crossed it, the pool answered with its own 429 while the provider
still accepted them; its Retry-After, "available at" time, and
provider-retry.v1.availability waited for windows that were only past the
threshold, so Provider Retry parked threads for hours; and the settings page
and `bb pool status` called those working accounts "Exhausted".

A window now counts as refused when its status is "rejected", or, when it has
no status, when utilization reaches 1. Selection still prefers accounts below
the threshold; when none is ready it falls back to accounts the provider still
accepts, then to refused accounts with extra usage or Codex credits. The wait
time, retry availability, and the exhausted status count only refused windows.
Pins, affinity, the sticky cursor, family detours, and the usage recheck are
unchanged whenever an account below the threshold is ready.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Accounts past the switch threshold that the provider still accepts and refused
accounts with extra usage or Codex credits are one fallback group again, picked
in the pool's usual order, as main picks among its extra-usage fallbacks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…reshold

A pinned conversation moved to another account as soon as its own crossed
switchThreshold and an account below it was ready. It now stays on its account
until the provider refuses it; a conversation inheriting its parent's pin
follows the same rule. New conversations still go to accounts below the
threshold first, and conversations pinned to a refused account running on
extra usage or Codex credits still return when an account below the threshold
is ready.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With every account past the switch threshold, the pool could keep routing new
and pinned conversations to a refused account running on extra usage or Codex
credits while another account still had subscription quota. Accounts past the
threshold that the provider still accepts now come before those paid accounts,
and conversations pinned to a paid account move to one when it is ready.

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

After a server restart, the GitHub PR Status plugin restores each
environment's PR from its database and serves it right away. It also
starts a background re-check right away, often before the host daemon
has reconnected. That re-check returned `unavailable` ("machine isn't
available"), and `store.load` replaced the environment's known PR link
with that result. The sidebar row badge disappeared and stayed gone
until something re-read that row, such as opening the thread or
refocusing the page. Observed on prod: the server restarted at 13:04:14
and the daemon reconnected at 13:04:20. Afterwards the sidebar showed
only #6849 (the thread that was opened). Six other inbox threads with
open PRs (#6760, #6755, #6736, #6728, #6727, #6734) showed no badge,
although their links and snapshots were still in the plugin database.

## What changed

- `plugins/github-pr-status/store.ts`: when a re-check of a known PR URL
comes back `unavailable`, the environment's link is no longer replaced,
so the last snapshot keeps being served. The snapshot's `fetchedAt` is
still bumped, so the regular poll retries it. Discovery reads (no known
URL) behave as before.
- Behaviour change: `bb pull-request show` for an environment whose
machine is offline now prints the last PR bb saw, or `unavailable` when
it has none. Actions (`ready`/`draft`/`merge`) still refuse with the
machine message. The skill doc and README are updated.

## How you verified

- New store test, "keeps serving a restored PR when its machine is
unreachable after a restart": it restores a persisted PR, makes the
first re-check return `unavailable`, and checks that the PR is still
served, nothing is published, and the next successful check publishes
the update. It fails without the fix (`expected { outcome: 'unavailable'
} to deeply equal { outcome: 'available' }`) and passes with it.
- `pnpm exec turbo run test typecheck lint
--filter=bb-plugin-bb--github-pr-status`: 183/183 tests pass, typecheck
and lint clean.

> AGENT GENERATED

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

Co-authored-by: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit efde6ff into main Nov 2, 2026
52 checks passed
@ymichael
ymichael deleted the bb/6169-account-pooler-when-every-account-is-past-s-thr_cxiiebtxyd branch November 2, 2026 21:55
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.

Account Pooler: when every account is past switchThreshold, the pool returns 429 with a far reset although upstream still allows requests

1 participant