Skip to content

auto-switch picks an alternative subscription without checking headroom — it can move agents onto a nearly-exhausted one #2409

Closed

Description

@webmixgamer

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

Activity

  1. added
    priority-p1
    type-bug
    theme-monetization
    complexity-low
    status-ready
    on Aug 26, 2026
  2. removed
    status-ready
    on Aug 27, 2026
  3. webmixgamer commented on Aug 27, 2026

    @webmixgamer
    ContributorAuthor

    /claim

  4. added
    status-in-progress
    on Aug 27, 2026
  5. github-actions commented on Aug 27, 2026

    @github-actions

    Claimed by @webmixgamer. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/2409-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #2409
  6. webmixgamer commented on Aug 27, 2026

    @webmixgamer
    ContributorAuthor

    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:

    1. AC Feature/gemini runtime support #2 — ranking key. "Lowest 7d utilization" becomes "furthest from the nearest wall": the primary key is the fuller of the two provider windows (5h or 7d), banded to 10 points so load-balance (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).
    2. AC Feature/vector log retention #3 / fix: add missing logging_config.py to backend Dockerfile #4 — provider refusals are filtered, not ranked last. A fresh (≤30 min) reading in which the provider is refusing — probe 429, a blocking window status, or a rejected token — removes the candidate. Stale refusals and every other unusable reading still degrade to today's order, so "missing / stale / unreadable never blocks a switch" holds unchanged. The one case that now returns no target where it previously returned one is every survivor is currently refused by the provider: moving an agent there is a guaranteed failed turn plus a failure row, and with several such subscriptions the agent walks through all of them (bug(SUB-003): auto-switch ping-pongs between rate-limited subscriptions due to clear_rate_limit_events destroying detection signal #444's class as a walk). Note that today's agent_count ASC key sorts an unused dead-token subscription first (no agents ⇒ no failure rows).
    3. Scope. 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 into subscription_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.

  7. added
    status-in-dev
    and removed
    status-in-progress
    on Aug 28, 2026
  8. removed
    status-in-dev
    on Sep 17, 2026
  9. added a commit that references this issue on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-lowpriority-p1theme-monetizationtype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions