Skip to content

Preempt to a higher-priority account when one is available - #29

Merged
MagicalTux merged 1 commit into
masterfrom
fix/priority-preemption
Jun 18, 2026
Merged

MagicalTux merged 1 commit into
masterfrom
fix/priority-preemption

Conversation

@MagicalTux

Copy link
Copy Markdown
Member

Follow-up refinement to the priority feature from #28.

Problem

priority was only consulted inside _selectNext, which runs only when a switch is forced (current account exhausted/throttled, or on requalify/session-reset). getActiveAccount returns the current account as long as it's available — without checking priority. So if rotation landed on a lower-priority account and your preferred (higher-priority) account recovered, the proxy wouldn't resume it until the current one ran low.

Change

getActiveAccount now preempts a healthy current account when a strictly higher-priority (lower value) account is available, while staying sticky within the same priority tier:

if (this._isAvailable(current)) {
  const betterExists = this.accounts.some(a =>
    this._isAvailable(a) && (a.priority || 0) < (current.priority || 0));
  return betterExists ? this._selectNext() : current;
}

Why this is safe

  • Common case unchanged: with all accounts at the default priority 0, no account is strictly higher than another, so betterExists is always false and behavior is identical to today — no extra switching, no thrash.
  • Sticky within a tier: equal-priority accounts never preempt each other, so the per-request weekly-reset heuristic doesn't cause churn.
  • Exhaustion + weekly heuristic intact: _isAvailable (session/weekly threshold) is still checked first, and _selectNext still applies the soonest-weekly-reset tiebreaker within a tier.

Tests

test/rotation-priority.test.js extended: preemption to a higher tier, stickiness within a tier, and the all-equal-priority no-op. 8/8 passing.

🤖 Generated with Claude Code

The `priority` field (added in #28) was only consulted when a switch was
forced (current account exhausted/throttled). A healthy lower-priority current
account was never preempted, so a recovered preferred account wouldn't be
resumed until the in-use one ran low.

getActiveAccount now switches to a strictly higher-priority (lower value)
available account even from a healthy current one, while staying sticky within
the same priority tier. The common case — all accounts at the default priority
0 — is unchanged and never thrashes, since no account is strictly higher than
another. Exhaustion-switching and the soonest-weekly-reset heuristic are intact.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@MagicalTux
MagicalTux merged commit 7b176a9 into master Jun 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant