Repository navigation
Description
Summary
select_best_alternative_subscription (src/backend/db/subscriptions.py) returns the first candidate with no recent failure rows and never reads headroom. SUB-003 auto-switch therefore relocates agents onto a subscription that may be at 99% of its weekly window — the switch reports success, and the agent hits the wall again shortly afterwards. No human is in the loop, and nothing surfaces that the destination was a bad choice.
Context
Surfaced while reviewing how subscription headroom is consumed across the platform. The selector's candidate filter is has_recent_subscription_failures(...) and nothing else, so headroom never enters the decision.
That filter is correct and must stay first — a subscription whose token just failed is unusable no matter how much quota it has, which is what #444 established ("see #444 for what a forgetful candidate filter does" is in the code today). The gap is that among usable candidates there is no preference at all: first match wins.
Wrong in both directions:
- a subscription at 99% utilization with no failure rows is picked as if it were a good target;
- a subscription at 5% with one auth failure 90 minutes ago is skipped — correctly, but headroom played no part either way.
The two signals answer different questions and should compose rather than compete: recent failures answer "can this be used at all?", headroom answers "how long will it last?".
Acceptance Criteria
- Recent-failure filtering is unchanged and still runs first — a candidate with recent failures is never selected regardless of its headroom (bug(SUB-003): auto-switch ping-pongs between rate-limited subscriptions due to clear_rate_limit_events destroying detection signal #444 must not regress)
- Among candidates passing that filter, selection prefers the most headroom (lowest 7d utilization) from a fresh provider reading
- Never returns "no viable target" where it previously returned a candidate. If every survivor is saturated, the least-saturated one is still selected — leaving an agent on a definitely-limited subscription is strictly worse than moving it to a nearly-full one
- Missing, stale, or unreadable headroom never blocks a switch: candidates with no usable reading stay eligible and sort after those with one, degrading to today's first-match behaviour
- The selector adds no probes — it reads the existing snapshot only, never forcing a provider call on the auto-switch path
- Tests cover: ranking correctness, all-saturated fallback, no-headroom-data fallback, and that the failure filter still short-circuits ahead of any headroom logic
Technical Notes
- Selector:
src/backend/db/subscriptions.py::select_best_alternative_subscription - Consumes the three-state headroom classifier (
saturated/has_headroom/unassessable) being added tosrc/backend/services/subscription_headroom_service.py. Blocked on that landing — do not re-derive the classification here, or the two will drift _headroom_indicates_healthyin that module is not a substitute: it returnsFalsefor a stale snapshot, a rejected token, and a genuinely saturated subscription, so it cannot distinguish "no evidence" from "no headroom"- Related provider-signal fixes: bug: subscription reports "rate-limited" for auth failures — is_subscription_rate_limited ignores failure_kind #2352, bug: subscription pressure tile shows "rate-limited" for a rejected provider token (invalid_token hidden in tooltip) #2353, bug: subscription reports "rate-limited" for the provider's own warning tier — allowed_warning fails the _headroom_indicates_limited catch-all #2396
Activity
webmixgamer commentedon Aug 27, 2026
/claim
github-actions commentedon Aug 27, 2026
Claimed by @webmixgamer. Label
status-in-progressadded.Next steps:
feature/2409-<short-slug>Fixes #2409webmixgamer commentedon Aug 27, 2026
Plan clarification (from
/autoplan, operator-approved 2026-08-27) — three refinements to the acceptance criteria as written, recorded so scope is read against the agreed design rather than the original wording:agent_count) still spreads a storm within a band. Rationale: the feature/sub-003: retry triggering execution after successful auto-switch #792 retry re-issues the turn on the destination immediately, so a subscription at 7d 20% / 5h 98% fails within the minute — and a lexicographic 7d-then-5h key never reaches the 5h term (1-decimal utilization never ties on 7d).agent_count ASCkey sorts an unused dead-token subscription first (no agents ⇒ no failure rows).get_least_used_subscription(new-agent auto-assign, Auto-assign subscription to new agents (round-robin) #74) has the identical shape and gets the same ranking. Selection moves from the db layer intosubscription_auto_switch.py; the db layer only lists filtered survivors in a deterministic order.Unchanged and as specified: the recent-failure filter runs first, no probe is added to the switch path, and every failure path degrades to today's behaviour.
fix(subscriptions): bound the headroom sanitiser on the low side; say…
fix(subscriptions): auto-switch ranks alternatives by cached headroom…
Merge origin/dev into fix/2638-subscription-switch-on-turn
fix(subscriptions): a rate-limited turn completes on another subscrip…
fix(subscriptions): read the utilization header as a fraction, past 1…
Release: v0.9.5 (Abilityai#2872)