From 9e49c762e0e498f453cc5e901792356d40bb1534 Mon Sep 17 00:00:00 2001 From: Muhammed Mukhthar CM <56378562+mukhtharcm@users.noreply.github.com> Date: Tue, 6 Jan 2026 13:46:35 +0530 Subject: [PATCH] fix(auth): prioritize round-robin over lastGood for multi-account rotation (#281) * fix(auth): prioritize round-robin over lastGood for multi-account rotation When multiple OAuth accounts are configured, the round-robin rotation was not working because lastGood was always prioritized, defeating the sort by lastUsed. Changes: - Remove lastGood prioritization in resolveAuthProfileOrder - Always apply orderProfilesByMode (sorts by lastUsed, oldest first) - Only respect configuredOrder when explicitly set in config - preferredProfile still takes priority for explicit user choice Tested with 2 Google Antigravity accounts - verified alternating usage. Follow-up to PR #269. * style: fix formatting --- src/agents/auth-profiles.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/agents/auth-profiles.ts b/src/agents/auth-profiles.ts index 43308674c..1d96c94ac 100644 --- a/src/agents/auth-profiles.ts +++ b/src/agents/auth-profiles.ts @@ -433,19 +433,14 @@ export function resolveAuthProfileOrder(params: { .filter(([, profile]) => profile.provider === provider) .map(([profileId]) => profileId) : []; - const lastGood = store.lastGood?.[provider]; const baseOrder = configuredOrder ?? (explicitProfiles.length > 0 ? explicitProfiles : listProfilesForProvider(store, provider)); if (baseOrder.length === 0) return []; - const order = - configuredOrder && configuredOrder.length > 0 - ? baseOrder - : orderProfilesByMode(baseOrder, store); - const filtered = order.filter((profileId) => { + const filtered = baseOrder.filter((profileId) => { const cred = store.profiles[profileId]; return cred ? cred.provider === provider : true; }); @@ -453,21 +448,29 @@ export function resolveAuthProfileOrder(params: { for (const entry of filtered) { if (!deduped.includes(entry)) deduped.push(entry); } - if (preferredProfile && deduped.includes(preferredProfile)) { - const rest = deduped.filter((entry) => entry !== preferredProfile); - if (lastGood && rest.includes(lastGood)) { + + // If user specified explicit order in config, respect it exactly + if (configuredOrder && configuredOrder.length > 0) { + // Still put preferredProfile first if specified + if (preferredProfile && deduped.includes(preferredProfile)) { return [ preferredProfile, - lastGood, - ...rest.filter((entry) => entry !== lastGood), + ...deduped.filter((e) => e !== preferredProfile), ]; } - return [preferredProfile, ...rest]; + return deduped; } - if (lastGood && deduped.includes(lastGood)) { - return [lastGood, ...deduped.filter((entry) => entry !== lastGood)]; + + // Otherwise, use round-robin: sort by lastUsed (oldest first) + // preferredProfile goes first if specified (for explicit user choice) + // lastGood is NOT prioritized - that would defeat round-robin + const sorted = orderProfilesByMode(deduped, store); + + if (preferredProfile && sorted.includes(preferredProfile)) { + return [preferredProfile, ...sorted.filter((e) => e !== preferredProfile)]; } - return deduped; + + return sorted; } function orderProfilesByMode(