Skip to content

Avoid Account Pooler usage 429 bursts and keep current cached quotas visible - #8556

Open
salemsayed wants to merge 1 commit into
get-bb:mainfrom
salemsayed:fix/account-pool-usage-rate-limit
Open

salemsayed wants to merge 1 commit into
get-bb:mainfrom
salemsayed:fix/account-pool-usage-rate-limit

Conversation

@salemsayed

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

In a multi-account OAuth pool, three behaviors in plugins/account-pool combined to trigger /api/oauth/usage HTTP 429s and hide valid cached quotas in Provider usage (#8555):

  1. forward() called refreshExhaustedUsage() whenever select() returned null or an account past switchThreshold, polling every past-threshold/exhausted account every 30 seconds in parallel (Promise.all) even when its blocking resetAt was hours or days in the future, despite isRefusedWindow/isWindowPastThreshold already auto-clearing at resetAt.
  2. registerUsageSource() returned { status: "error" } whenever hub.usageCheckFailure(account.id) was set, even on passive refresh: false reads when account-pool already had current un-reset limitWindows persisted in SQLite. When provider-usage had a cold in-memory cache (for example after a server restart whose startup burst 429'd), it had no cached reading to fall back to and showed only the rate-limit message with no bars, plan badge, or reset times.
  3. refreshUsage() fired Promise.all across all enabled OAuth accounts simultaneously on hub startup and every 5 minutes; 429 responses without Retry-After fell back to 1 second with no exponential backoff; and refreshAccountUsage() stamped lastUsageRefreshAt before the fetch, locking out passive retries for 5 minutes even after retryAt expired.

What changed

  • plugins/account-pool/src/quota.ts: Added hasUnknownPastThresholdReset() (true only when an account is past threshold or usage-restricted with no known future resetAt) and parseRetryAfterMilliseconds() (returning null when Retry-After is absent/unparseable, with retryAfterMilliseconds() keeping its 1s fallback for existing callers).
  • plugins/account-pool/src/provider-adapter.ts, claude-adapter.ts, codex-adapter.ts: Narrowed UsageRefreshResult to { kind: "done" } | { kind: "rate-limited"; retryAfterMs: number | null } | { kind: "failed" }.
  • plugins/account-pool/src/hub.ts:
    • refreshExhaustedUsage() now only rechecks accounts matching hasUnknownPastThresholdReset() and returns boolean, so forward() skips the extra selection loop when nothing was refreshed. Known future resets rely on the 5-minute background loop and manual refresh, and auto-clear at resetAt.
    • Replaced Promise.all bursts across same-provider accounts with refreshAccountsSequentiallyByProvider(), propagating the service abort signal from start().
    • Separated lastUsageRefreshAt (updated only on success) from usageFailures ({ kind, retryAt, attempts }) with exponential backoff from 30s to 5m (taking Math.max(retryAfterMs, backoffMs) on 429). Passive checks retry as soon as retryAt passes instead of waiting 5 minutes; non-429 failures back off on passive checks while remaining immediately retryable on forced refresh (minIntervalMs === 0).
  • plugins/account-pool/src/usage-source.ts, server.ts: Wired now into registerUsageSource(). Passive refresh: false reads return { status: "ok", windows } when account.error === null, observedAt !== null, and at least one window is still current (resetAt === null || resetAt > now). Explicit refresh: true reads, unobserved accounts, and accounts whose cached windows have all reset still return status: "error" (preserving Show stale and rate-limited usage honestly in the usage panel #8455 and provider-usage's "Showing last update." behavior).
  • plugins/account-pool/skills/account-pool/references/accounts-and-routing.md: Documented per-provider serialized refreshes and that the 30-second recheck applies when the blocking reset time is unknown.
  • plugins/account-pool/src/server.test.ts: Updated the 30s exhausted-recheck and paid-pin recovery tests to match the reset-time policy, and added 4 tests covering future-reset skip + auto-recovery, passive vs forced/post-reset usage errors, exponential 429/500 backoff + passive retry after retryAt, and serialized per-provider startup refreshes.

No wire, host-daemon protocol, CLI, or Plugin SDK changes.

How you verified

  • Added 4 targeted tests in plugins/account-pool/src/server.test.ts:
    • skips 30-second exhausted-usage rechecks while a blocking reset time is in the future and recovers at reset
    • returns current cached usage on passive reads during a 429, but surfaces the error on forced refresh or after reset
    • backs off 429 usage checks exponentially without Retry-After and retries passively once backoff expires
    • serializes multi-account usage refreshes per provider on hub startup
  • pnpm exec turbo run typecheck lint test --filter=bb-plugin-account-pool --filter=bb-plugin-bb--provider-usage passes (13/13 tasks; 410/410 tests in bb-plugin-account-pool, 0 lint errors).
  • pnpm exec turbo run lint:type-aware --filter=bb-plugin-account-pool passes (0 warnings, 0 errors).
  • pnpm exec oxfmt --check on all 8 modified .ts files passes.

Fixes #8555

AGENT GENERATED

This branch has not been deployed

No deployments
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: exhausted-pool usage polling bursts trigger /api/oauth/usage 429s and hide current cached quotas

1 participant