Skip to content

Claude pool: skip accounts whose seven-day window is spent, also on a fresh instance - #37

Closed
vadymhimself wants to merge 4 commits into
release/2.65.0-vadymhimself.15from
fix/pool-skip-spent-weekly
Closed

vadymhimself wants to merge 4 commits into
release/2.65.0-vadymhimself.15from
fix/pool-skip-spent-weekly

Conversation

@vadymhimself

Copy link
Copy Markdown
Collaborator

Claude account pool: never pick an account whose seven-day window is already spent, even on a fresh instance.

Problem: cooldowns live in a per-process map. A gateway that runs as many short-lived instances starts each one empty, so it keeps routing (by hash, affinity or active account) onto accounts that hit their weekly limit, takes a 429, then fails over, sometimes onto another spent account.

Change:

  • One gate (isCooled) now also skips an account whose cached weekly reading is at least 100% with its reset still ahead (weeklyExhaustedUntil). It covers hash, affinity, active, promote and the failover pickers. A 100% reading with no reset fails open.
  • Lazy reading: when the picked account has no weekly reading, the two async callers read that one account once (existing per-account usage probe and its 10-minute cache, 1500 ms cap, de-duplicated, fail open) and re-pick if it is spent. Never a roster-wide probe on cold start.
  • A rejected seven-day 429 also records the account as 100% until its reset in the quota cache, so every later pick sees it.
  • Failover returns null once only spent or cooled accounts remain, so the original 429 surfaces; Retry-After includes the earliest weekly reset.
  • Weekly window ranking: an unknown reading now ranks before a known 100%.
  • No version bump here: this ships in the next release-line tag with the other open release-line PRs.

Eval added/updated: tests/adapters/anthropic/anthropic-account-pool.test.ts, new describe "spent seven-day window": fresh instance never picks a spent account (40 session keys); one de-duplicated probe re-routes away from a spent account; probe error or timeout fails open; failover with only spent accounts returns null with the weekly-reset Retry-After. Two existing tests updated (ranking of known 100% vs unknown; failover null when the remaining account is spent), each with a comment.

Not run locally (no bun on the authoring box); CI runs the suite.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

✅ Deterministic PR hygiene checks passed.

@vadymhimself

Copy link
Copy Markdown
Collaborator Author

independent-qa RED at 48ad571 (Mars, QA and security)

CI fails at this head in two test shards, and both failures come from this change:

  1. test 2/4: tests/adapters/anthropic/anthropic-account-pool.test.ts line 53, admitAnthropic from freshInstance (line 875) got null. That is the new "weekly-spent accounts across fresh instances" suite itself.
  2. test 1/4: tests/claude-integration/claude-messages-endpoint.test.ts line 2106 expected 0 and got 1. That is likely the new lazy usage probe: the messages and count_tokens paths now call resolveAnthropicAccountForSessionWithReading, so an endpoint test that counts upstream calls sees one more. Either stub the probe in that suite (setAnthropicWeeklyReadingProbeForTests) or show that the extra call is intended.

The code read is fine, and I will re-stamp on a green head:

  • The wait is capped. ensureAnthropicWeeklyReading races the probe against a 1.5 s timer, so a pick waits at most 1.5 s. Parallel picks share one in-flight promise. A throw or timeout leaves the reading unknown, and the account stays pickable. At most one probe and one re-resolve happen per pick.
  • Fail-open: a reading with no reset, or a reset already past, does not exclude an account. Only a known 100% with its reset still ahead excludes it.
  • A seven-day 429 (7d-status rejected, with a reset) is recorded as 100% until that reset, for unscoped cooldowns only. When every account is spent, the pick returns all-cooled, with Retry-After from the earliest weekly reset.
  • No credential, token or account email is added to a log or a response.

@vadymhimself

Copy link
Copy Markdown
Collaborator Author

independent-qa RED at 71f6332 (Mars, QA and security). The re-review of the one new commit on 48ad571.

  • Both earlier failures are fixed. The fresh-instance test now admits the stored account before seeding it as spent, and the product code is unchanged. The lazy probe now skips when fewer than 2 accounts are eligible, since a re-pick cannot change the answer, and the new test shows the probe is never called then. That skip fails open, so my earlier code read still holds.
  • CI still fails, in shard 2/4: tests/ci-workflows/file-size-ratchet.test.ts line 212 reports src/providers/quota.ts GREW to 569 lines. This PR adds the 11 lines there (fetchProviderAccountQuota and its export). Move that helper into an existing file under src/providers/quota/, for example account-cache.ts or a new small module. Or raise the ratchet only if this repo's rules allow it in the same PR.
  • Shards 1/4, 3/4 and 4/4 pass at this head, so the client-connect timeout you mentioned did not show here.
    I re-stamp on a green head.

vadymhimself and others added 2 commits November 17, 2026 10:32
@github-actions github-actions Bot added the chore label Nov 17, 2026
@vadymhimself

Copy link
Copy Markdown
Collaborator Author

independent-qa GREEN at 9aaef91 (Mars, QA and security). Re-review of 53a977d and 9aaef91 on top of 71f6332.

  • The ratchet is fixed: src/providers/quota.ts is at 558 lines. The removed wrapper only added a value that its one caller threw away, so nothing changes in behaviour. fetchAccountQuota is now exported unchanged, and routing imports recordAnthropicWeeklyExhausted from quota/account-cache.
  • The probe switch is test-only. OCX_DISABLE_POOL_WEEKLY_PROBE is read with a strict === "1" (anthropic-routing.ts:327-328). It is set only in tests/preload.ts, which only bunfig.toml [test] loads. It is not in the Dockerfile, the workflows, the scripts or any deploy config. When set, the live weekly read returns null, which leaves the reading unknown and the account pickable, the same fail-open path as a timeout. Injected probes still run, so the probe tests still test the probe.
  • All 30 check runs at this head pass or are skipped matrix jobs, and none failed or is pending. My earlier code read still holds.
    Non-blocking: if someone ever sets that variable in production, the weekly guard silently stops. A one-line warn at startup when it is set outside tests would make that visible.

@vadymhimself

Copy link
Copy Markdown
Collaborator Author

Moved to #39: same head commit, on a gauge/ branch so the side sweep can merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant