Skip to content
Merged
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
19 changes: 19 additions & 0 deletions lib/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,25 @@ export class AccountManager {
const quotaKey = model ? `${family}:${model}` : family;
const healthTracker = getHealthTracker();
healthTracker.recordSuccess(account.index, quotaKey);
const hadCooldownMetadata =
account.coolingDownUntil !== undefined || account.cooldownReason !== undefined;
const hadAuthFailures = (account.consecutiveAuthFailures ?? 0) > 0;
const isCoolingDown = this.isAccountCoolingDown(account);
let healed = false;

if (!isCoolingDown && hadCooldownMetadata) {
this.clearAccountCooldown(account);
healed = true;
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.

if (!isCoolingDown && hadAuthFailures) {
this.clearAuthFailures(account);
healed = true;
}

if (healed) {
this.saveToDiskDebounced();
}
}

recordRateLimit(account: ManagedAccount, family: ModelFamily, model?: string | null): void {
Expand Down
105 changes: 105 additions & 0 deletions test/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,111 @@ describe("AccountManager", () => {
expect(score).toBe(100);
});

it("recordSuccess clears stale auth failure state and persists the healed account", async () => {
const { saveAccounts } = await import("../lib/storage.js");
const mockSaveAccounts = vi.mocked(saveAccounts);
mockSaveAccounts.mockClear();

const now = Date.now();
const stored = {
version: 3 as const,
activeIndex: 0,
accounts: [
{
refreshToken: "token-1",
addedAt: now,
lastUsed: now,
consecutiveAuthFailures: 2,
coolingDownUntil: now - 1000,
cooldownReason: "network-error" as const,
},
],
};

const manager = new AccountManager(undefined, stored);
const account = manager.getCurrentAccount()!;
account.consecutiveAuthFailures = 2;

manager.recordSuccess(account, "codex", "gpt-5.1");
await manager.flushPendingSave();

expect(account.consecutiveAuthFailures).toBe(0);
expect(account.coolingDownUntil).toBeUndefined();
expect(account.cooldownReason).toBeUndefined();
expect(mockSaveAccounts).toHaveBeenCalledTimes(1);
const persisted = mockSaveAccounts.mock.calls[0]?.[0];
expect(persisted?.accounts[0]?.consecutiveAuthFailures ?? 0).toBe(0);
expect(persisted?.accounts[0]?.coolingDownUntil).toBeUndefined();
expect(persisted?.accounts[0]?.cooldownReason).toBeUndefined();
});

it("recordSuccess clears stale cooldown metadata when only the reason remains", async () => {
const { saveAccounts } = await import("../lib/storage.js");
const mockSaveAccounts = vi.mocked(saveAccounts);
mockSaveAccounts.mockClear();

const now = Date.now();
const stored = {
version: 3 as const,
activeIndex: 0,
accounts: [
{
refreshToken: "token-1",
addedAt: now,
lastUsed: now,
cooldownReason: "network-error" as const,
},
],
};

const manager = new AccountManager(undefined, stored);
const account = manager.getCurrentAccount()!;

manager.recordSuccess(account, "codex", "gpt-5.1");
await manager.flushPendingSave();

expect(account.coolingDownUntil).toBeUndefined();
expect(account.cooldownReason).toBeUndefined();
expect(mockSaveAccounts).toHaveBeenCalledTimes(1);
const persisted = mockSaveAccounts.mock.calls[0]?.[0];
expect(persisted?.accounts[0]?.coolingDownUntil).toBeUndefined();
expect(persisted?.accounts[0]?.cooldownReason).toBeUndefined();
});

it("recordSuccess does not clear an active cooldown from a newer concurrent failure", async () => {
const { saveAccounts } = await import("../lib/storage.js");
const mockSaveAccounts = vi.mocked(saveAccounts);
mockSaveAccounts.mockClear();

const now = Date.now();
const stored = {
version: 3 as const,
activeIndex: 0,
accounts: [
{
refreshToken: "token-1",
addedAt: now,
lastUsed: now,
consecutiveAuthFailures: 2,
coolingDownUntil: now + 60_000,
cooldownReason: "auth-failure" as const,
},
],
};

const manager = new AccountManager(undefined, stored);
const account = manager.getCurrentAccount()!;
account.consecutiveAuthFailures = 2;

manager.recordSuccess(account, "codex", "gpt-5.1");
await manager.flushPendingSave();

expect(account.consecutiveAuthFailures).toBe(2);
expect(account.coolingDownUntil).toBe(now + 60_000);
expect(account.cooldownReason).toBe("auth-failure");
expect(mockSaveAccounts).not.toHaveBeenCalled();
});

it("recordRateLimit updates health and drains token bucket", () => {
const now = Date.now();
const stored = {
Expand Down