Skip to content

Subscription auto-switch: trigger on any subscription/auth failure and enable by default #442

Closed

Description

@vybe

Summary

Make subscription auto-switch (SUB-003) more aggressive by default: trigger on a single call failure (not 2 consecutive), broaden the trigger surface from rate-limit (429) only to any subscription/auth error, and flip the system setting default from off to on.

Context

Today's behavior in services/subscription_auto_switch.py + subscription-auto-switch.md:

  • Opt-in via auto_switch_subscriptions setting, default "false"
  • Triggers only on HTTP 429 from the agent's chat endpoint
  • Requires 2+ consecutive rate-limit events on the same (agent, subscription) before switching

In practice agents fail for reasons beyond 429 — expired OAuth tokens, revoked credentials, account credit-balance issues, 401/403 auth errors — and the current design leaves them stuck on a broken subscription until a human notices. The scheduler already classifies these as [AUTH_ERROR] using a well-defined indicator list (src/scheduler/service.py:677-717); the auto-switch service should hook into the same signal.

We also want auto-switch to be the default: a single failure should rotate to a healthy subscription if one is available, so agents self-heal without configuration.

Acceptance Criteria

  • Threshold drops from 2 consecutive failures to 1: a single qualifying failure is enough to trigger a switch (given other preconditions hold)
  • Trigger surface broadens beyond 429: any failure matching the auth-indicator list (credit balance, unauthorized, authentication, credentials, forbidden, 401, 403, oauth, token expired, not authenticated) routes into handle_rate_limit_error (rename if appropriate — e.g. handle_subscription_failure)
  • auto_switch_subscriptions system setting default flips from "false" to "true"
  • Existing guards still hold: agent must have a subscription (not API key), alternative must exist, subscriptions with events in last 2h are excluded from candidate selection (prevents thrashing)
  • Activity log + notification text reflect the actual trigger reason (rate limit vs auth failure) instead of hardcoded "consecutive rate-limit errors"
  • All three 429 interception points are updated to also forward non-429 auth failures: task_execution_service.py:532, chat.py:431, and the background task handler
  • Feature flow docs/memory/feature-flows/subscription-auto-switch.md updated: preconditions, flow diagram, selection strategy, setting default
  • Requirements doc docs/requirements/SUB-003-subscription-auto-switch.md updated to match
  • Smoke test added/updated in tests/test_subscription_auto_switch.py covering the single-failure and auth-error paths

Technical Notes

  • The auth-indicator list already lives in src/scheduler/service.py — extract into a shared helper (e.g. services/subscription_auto_switch.py::is_subscription_failure(error_message)) and reuse from both sites to keep a single source of truth
  • Be careful at task_execution_service.py:532 and chat.py:431: auth failures typically surface as 500/502 with the indicator text in the body, not as a specific status code — the branch currently gated on agent_status_code == 429 needs widening (e.g. agent_status_code == 429 or is_subscription_failure(error_msg))
  • The "2-consecutive" guard in subscription_auto_switch.py:52 is what prevented flip-flopping; with threshold=1 the only remaining safeguard is select_best_alternative_subscription excluding subs with events in the last 2h. Confirm that window is sufficient, or consider a per-agent cooldown on auto-switches themselves
  • Flipping the default to on is a behavior change for existing installs — mention in release notes / CHANGELOG if one exists
  • Related: subscription-credential-health.md describes the auth-error classification the scheduler already does; this issue extends the same signal into the auto-switch pipeline

Activity

  1. added
    priority-p2
    type-feature
    status-in-progress
    and removed
    status-in-progress
    on Apr 21, 2026
  2. vybe commented on Apr 21, 2026

    @vybe
    ContributorAuthor

    Deferred pending Tier 2.5 simplification (#428)

    Picked this up via /sprint on 2026-04-21, ran plan review, and decided to defer. Capturing the reasoning + a scope reframe so it's not lost when we come back.

    Why defer

    Per docs/planning/ORCHESTRATION_RELIABILITY_2026-04.md, current focus is Tier 2.5 — Simplification: #306 just landed (PRs #438, #439), now in a 2-week soak window before #428 (CAPACITY-CONSOLIDATE) opens. Both interception points this issue touches — task_execution_service.py:532 and chat.py:431 — sit inside the surface #428 is meant to collapse. The plan's additive-first principle calls out this exact pattern:

    "Each new Sprint C primitive welds itself into the pyramid; adding … on top first would make consolidation strictly harder."

    This issue is P2, no active incident, and operators can manually switch. Papercut, not fire. Right time to land it is when #428 opens those files anyway.

    Scope reframe for when we pick it back up

    Detection for auth failures is already solved by #285 (shipped PR #322): TaskExecutionService has a stderr scanner that classifies auth failures and sets TaskExecutionErrorCode.AUTH on the result (task_execution_service.py:542-544 for 503s + the stderr pattern matcher). The right implementation is to consume that signal, not add a parallel substring matcher at the HTTP layer.

    Revised plan:

    1. Widen the two interception points to trigger on `error_code == TaskExecutionErrorCode.AUTH` OR `agent_status_code == 429` — no new detection logic
    2. Threshold 2 → 1 (one-line tuning)
    3. Activity-log + notification text reflects the actual trigger reason
    4. Default flip via new-install migration seed only — leave the hardcoded `default="false"` fallback alone so existing-unset installs aren't silently flipped
    5. Add a per-agent switch cooldown guard (e.g. 15 min) so a real dead subscription + concurrent in-flight requests can't double-fire during the async container restart window
    6. Drop the scheduler-side dedup work (src/scheduler/service.py:778-784 and :1031-1037) — those lists are log classification only, and the scheduler consolidates later
    7. Drop the rename — handle_rate_limit_error can stay until refactor: consolidate ExecutionQueue + SlotService + BacklogService into CapacityManager #428 is restructuring the file

    Findings from plan review worth preserving

    Why not the substring matcher as originally specified:

    • False positives: "401", "oauth", "forbidden" appear in agent tool output (e.g. "got 403 from GitHub", /oauth/authorize URLs in help text)
    • Thrashing risk: threshold=1 + broad text match + async restart + no cooldown → one buggy agent can trigger N restarts across N subscriptions

    Why a per-agent cooldown even with perfect detection:

    • Restart is async; concurrent in-flight requests during the restart window can double-fire and flag the healthy backup subscription as rate-limited for 2h

    Deferred as a separate follow-up (not blocking this issue):

    Action

  3. added
    complexity-low
    theme-reliability
    on Jun 3, 2026
  4. added
    status-incubating
    on Jun 14, 2026
  5. vybe commented on Jun 15, 2026

    @vybe
    ContributorAuthor

    Grooming triage: verified shipped — src/backend/services/subscription_auto_switch.py implements 429 + auth-class triggers, default-on (opt-out), #441 threshold. Closing as done.

  6. removed
    status-incubating
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p2theme-reliabilitytype-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions