Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 45 additions & 21 deletions lib/accounts/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,6 @@ export class AccountManager {
model,
useRoundRobinCursor,
);

if (account && this.config.accountSelectionStrategy === "hybrid") {
await this.saveToDisk();
}

return account;
}

Expand All @@ -239,11 +234,11 @@ export class AccountManager {
const account = this.accounts[index];

if (this.isAccountAvailable(account, model, now)) {
this.activeIndex = index;
if (useRoundRobinCursor) {
this.roundRobinCursor = (index + 1) % this.accounts.length;
}
account.lastUsed = now;
await this.updateSelectedAccount(
account,
now,
useRoundRobinCursor ? (index + 1) % this.accounts.length : undefined,
);
return account;
}

Expand All @@ -252,11 +247,13 @@ export class AccountManager {

const fallback = this.getLeastRateLimitedAccount(model);
if (fallback) {
this.activeIndex = fallback.index;
if (useRoundRobinCursor) {
this.roundRobinCursor = (fallback.index + 1) % this.accounts.length;
}
fallback.lastUsed = now;
await this.updateSelectedAccount(
fallback,
now,
useRoundRobinCursor
? (fallback.index + 1) % this.accounts.length
: undefined,
);
}
return fallback;
}
Expand All @@ -277,9 +274,18 @@ export class AccountManager {
return;
}

this.activeIndex = this.normalizeIndex(this.activeIndex);
const normalizedActiveIndex = this.normalizeIndex(this.activeIndex);
const normalizedRoundRobinCursor = this.normalizeIndex(this.roundRobinCursor);
const normalizedStateChanged =
normalizedActiveIndex !== this.activeIndex ||
normalizedRoundRobinCursor !== this.roundRobinCursor;

this.roundRobinCursor = this.normalizeIndex(this.roundRobinCursor);
this.activeIndex = normalizedActiveIndex;
this.roundRobinCursor = normalizedRoundRobinCursor;

if (normalizedStateChanged) {
await this.saveToDisk();
}

if (this.config.pidOffsetEnabled && this.accounts.length > 1) {
const pidOffset = Math.abs(process.pid) % this.accounts.length;
Expand Down Expand Up @@ -311,6 +317,26 @@ export class AccountManager {
return true;
}

private async updateSelectedAccount(
account: ManagedAccount,
now: number,
nextRoundRobinCursor?: number,
): Promise<void> {
const activeIndexChanged = this.activeIndex !== account.index;
const roundRobinCursorChanged =
nextRoundRobinCursor !== undefined &&
this.roundRobinCursor !== nextRoundRobinCursor;

this.activeIndex = account.index;
if (nextRoundRobinCursor !== undefined) {
this.roundRobinCursor = nextRoundRobinCursor;
}
account.lastUsed = now;

if (activeIndexChanged || roundRobinCursorChanged) {
await this.saveToDisk();
}
}
private getLeastRateLimitedAccount(model?: string): ManagedAccount | null {
if (this.accounts.length === 0) return null;

Expand Down Expand Up @@ -490,8 +516,7 @@ export class AccountManager {
for (const account of this.accounts) {
if (excludeIndices.has(account.index)) continue;
if (this.isAccountAvailable(account, model, now)) {
this.activeIndex = account.index;
account.lastUsed = now;
await this.updateSelectedAccount(account, now);
return account;
}
}
Expand All @@ -517,8 +542,7 @@ export class AccountManager {
}

if (bestAccount) {
this.activeIndex = bestAccount.index;
bestAccount.lastUsed = now;
await this.updateSelectedAccount(bestAccount, now);
}

return bestAccount;
Expand Down
55 changes: 54 additions & 1 deletion test/account-manager-strategy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { mkdtempSync, statSync } from "node:fs";
import { mkdtempSync, readFileSync, statSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

Expand All @@ -19,6 +19,15 @@ async function createManager(
});
}

function readAccountsStorage(home: string) {
return JSON.parse(
readFileSync(join(home, ".config", "opencode", "openai-accounts.json"), "utf-8"),
) as {
activeAccountIndex: number;
roundRobinCursor?: number;
};
}

describe("AccountManager strategy selection", () => {
afterEach(() => {
process.env.HOME = originalHome;
Expand Down Expand Up @@ -61,6 +70,29 @@ describe("AccountManager strategy selection", () => {
expect(pick4?.index).toBe(0);
});

it("persists live round-robin selection state to disk", async () => {
const home = mkdtempSync(join(tmpdir(), "strategy-rr-persisted-active-"));
const manager = await createManager(home, "round-robin");
await manager.loadFromDisk();

await manager.addAccount("a@example.com", "rt-1");
await manager.addAccount("b@example.com", "rt-2");
await manager.addAccount("c@example.com", "rt-3");

expect((await manager.getNextAvailableAccount())?.index).toBe(0);
expect((await manager.getNextAvailableAccount())?.index).toBe(1);

const stored = readAccountsStorage(home);
expect(stored.activeAccountIndex).toBe(1);
expect(stored.roundRobinCursor).toBe(2);

const reloadedManager = await createManager(home, "round-robin");
await reloadedManager.loadFromDisk();

expect(reloadedManager.getActiveAccount()?.index).toBe(1);
expect((await reloadedManager.getNextAvailableAccount())?.index).toBe(2);
});

it("rotates initial account across sessions in hybrid mode", async () => {
const home = mkdtempSync(join(tmpdir(), "strategy-hybrid-"));

Expand Down Expand Up @@ -101,6 +133,27 @@ describe("AccountManager strategy selection", () => {
expect(third?.index).toBe(1);
});

it("persists active account after exclusion-based failover", async () => {
const home = mkdtempSync(join(tmpdir(), "strategy-sticky-persisted-failover-"));
const manager = await createManager(home, "sticky");
await manager.loadFromDisk();

await manager.addAccount("a@example.com", "rt-1");
await manager.addAccount("b@example.com", "rt-2");

expect((await manager.getNextAvailableAccount("gpt-5.2-codex"))?.index).toBe(0);
expect(
(await manager.getNextAvailableAccountExcluding(new Set([0]), "gpt-5.2-codex"))?.index,
).toBe(1);

const stored = readAccountsStorage(home);
expect(stored.activeAccountIndex).toBe(1);

const reloadedManager = await createManager(home, "sticky");
await reloadedManager.loadFromDisk();

expect(reloadedManager.getActiveAccount()?.index).toBe(1);
});
it("skips rate-limited accounts and keeps round-robin progression", async () => {
const home = mkdtempSync(join(tmpdir(), "strategy-rr-failover-"));
const manager = await createManager(home, "round-robin");
Expand Down