fix(SUB-003): stop ping-pong between exhausted subscriptions (#444)#445
Merged
Conversation
The auto-switch service was deleting rate-limit events from the old subscription immediately after each switch, which erased the signal that `is_subscription_rate_limited()` uses to filter candidates. The just-drained subscription then looked viable again on the next cycle, causing agents to ping-pong between two exhausted subscriptions (observed: 8+ switches over 90 minutes across 4 agents). Fix keeps the events in place so the 2h detection window stays truthful; events age out naturally via the existing cleanup job. Adds unit regression tests that reproduce the ping-pong against the db layer directly. Closes #444 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
_perform_auto_switch()deleted rate-limit events for the old subscription on every switch, erasing the signalis_subscription_rate_limited()reads. The drained sub then looked viable on the next cycle, causing agents to ping-pong between two exhausted subscriptions (observed: 8+ switches over 90 min across 4 agents).clear_rate_limit_eventscall. Events age out naturally via the existing 2h detection window + 24h cleanup job.Scope intentionally narrow — does not tackle the "proper fix" (parsing reset time from 429 body,
rate_limited_untilcolumn, exhaustion notification). That belongs in a follow-up.Changes
src/backend/services/subscription_auto_switch.py— deleteclear_rate_limit_eventscall, add comment explaining whytests/unit/test_subscription_auto_switch_pingpong.py— new, 4 testsdocs/memory/feature-flows/subscription-auto-switch.md— edge case Fix: Add missing Docker labels to system agent container #1 rewrittenTest Plan
pytest tests/unit/test_subscription_auto_switch_pingpong.py -vpytest tests/unit/test_subscription_bola.py tests/unit/test_backlog.pyauto_switch_subscriptions, exhaust two subs, confirm no switches occur after the first one (replaces the old 8+/90min loop)Closes #444
Generated with Claude Code