Repository navigation
Reset quota view on every dispatch, not just before a wait-and-retry - #32
Merged
Merged
Conversation
Live bug, reproduced from a real user's daemon.log: a single-account session hit an unbroken chain of 14+ handoffs in a row, each one "quota breach on claude" immediately after the previous dispatch had just started running, with no other account to fail over to. Root cause: ClaudeQuotaAdapter's Layer 2 fallback (declared cap, used when no real anthropic-ratelimit-* header has been observed yet) logs tokensLogged for the entire lifetime of the orchestrator session, and only resets on an explicit account switch (Reset()'s own doc comment). With one account, that switch never happens, so tokensLogged just keeps accumulating across every handoff. Once the cumulative total crosses the fallback's effective cap (declaredCap/1.5, the default works out to ~26,666 tokens, trivial for a long session), every future dispatch reports an instant breach regardless of that account's real remaining quota, forever. doHandoff never called resetQuotaView(); the wait-and-retry path already did before its own retry, but a same- or cross-provider handoff got no such reset. Fixed by calling resetQuotaView() at the end of doHandoff, after the new provider is set active, mirroring the existing wait-and-retry behaviour. A fresh dispatch now gets a fresh Layer-2 baseline: a real proxy header observed during that dispatch supersedes it immediately if quota genuinely remains, and if it does not, the account breaches again honestly instead of on a stale accumulated total inherited from every prior handoff this session. Tests: internal/quota/claude_test.go (new file, package had none) proves the mechanism directly — cumulative RecordRequest calls breach Layer 2 and stay breached without an explicit Reset, and Reset() returns both the declared-cap fallback and a stale observed header to a fresh, non-breached baseline. doHandoff itself is too heavy to unit test cheaply (needs adapters, graph store, FSM machine); this is the same proportionate-testing call made elsewhere in this session, e.g. worktree.go's capture/apply tests rather than a full orchestrator.New() integration test. Full daemon suite green (145 tests, 22 packages), go vet clean.
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.
Summary
Live bug, reproduced directly from a real user's daemon.log: a single-account session hit an unbroken chain of 14+ handoffs in a row, each one "quota breach on claude" immediately after the previous dispatch had just started, with no other account to fail over to.
Root cause
ClaudeQuotaAdapter's Layer 2 fallback (declared cap, used when no realanthropic-ratelimit-*header has been observed yet) accumulatestokensLoggedfor the entire lifetime of the orchestrator session, and only resets on an explicit account switch. With one account, that switch never happens, so the counter just keeps growing across every handoff. Once cumulative usage crosses the fallback's effective cap (declaredCap/1.5, ~26,666 tokens by default, trivial for a long session), every future dispatch reports an instant breach regardless of that account's real remaining quota, forever.doHandoffnever calledresetQuotaView(); the wait-and-retry path already did before its own retry, but a handoff got no such reset.Fix
Call
resetQuotaView()at the end ofdoHandoff, after the new provider is set active, mirroring the existing wait-and-retry behaviour. A fresh dispatch now gets a fresh Layer-2 baseline: a real proxy header observed during that dispatch supersedes it immediately if quota genuinely remains; if it does not, the account breaches again honestly instead of on a stale accumulated total inherited from every prior handoff this session.Tests
internal/quota/claude_test.go(new, package had no tests) proves the mechanism directly: cumulativeRecordRequestcalls breach Layer 2 and stay breached without an explicitReset, andReset()returns both the declared-cap fallback and a stale observed header to a fresh, non-breached baseline.doHandoffitself is too heavy to unit test cheaply (needs adapters, graph store, FSM machine) — same proportionate-testing call made elsewhere this session.Full daemon suite green (145 tests, 22 packages),
go vetclean.Test plan