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
6 changes: 6 additions & 0 deletions lib/runtime-rotation-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,12 @@ async function handleRequestInner(
DEFAULT_AUTH_FAILURE_COOLDOWN_MS,
"auth-failure",
);
// Persist the cooldown like every other cooldown branch in this loop
// (network-error, 429, server-error, 401). `coolingDownUntil`/
// `cooldownReason` are serialized in the V3 snapshot, so without this
// a restart inside the cooldown window loses it and immediately
// re-selects the still-broken account.
accountManager.saveToDiskDebounced();
exhaustionReason = "auth-failure";
transientAttempts += 1;
transientExhaustionReason = "auth-failure";
Expand Down
41 changes: 41 additions & 0 deletions test/runtime-rotation-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,47 @@ describe("runtime rotation proxy", () => {
expect(saveToDiskDebouncedSpy).toHaveBeenCalled();
});

it("persists the cooldown when an account has no resolvable accountId", async () => {
const now = Date.now();
// Account with no stored accountId and a non-JWT access token, so
// resolveAccountId returns null and the missing-accountId cooldown branch
// fires. The cooldown must be persisted like every other cooldown branch
// so a restart inside the window honors it (regression: this branch used
// to mutate cooldown state without scheduling a disk write).
const storage: AccountStorageV3 = {
version: 3,
activeIndex: 0,
activeIndexByFamily: { codex: 0 },
accounts: [
{
email: "no-id@example.com",
refreshToken: "refresh-1",
accessToken: "plain-access-not-a-jwt",
expiresAt: now + 3_600_000,
addedAt: now - 60_000,
lastUsed: now - 60_000,
enabled: true,
},
],
};
const accountManager = new AccountManager(undefined, storage);
expect(accountManager.getAccountByIndex(0)?.accountId).toBeUndefined();
const saveToDiskDebouncedSpy = vi.spyOn(accountManager, "saveToDiskDebounced");
const { calls, fetchImpl } = createRecordingFetch(() => textEventStream());
const proxy = await startProxy({ accountManager, fetchImpl });

const response = await postResponses(proxy, { model: "gpt-5-codex" });
await response.text();

// No upstream request is issued — the account is cooled down before send,
// exhausting the single-account pool.
expect(response.status).toBe(HTTP_STATUS.SERVICE_UNAVAILABLE);
expect(calls).toHaveLength(0);
Comment thread
greptile-apps[bot] marked this conversation as resolved.
expect(accountManager.getAccountByIndex(0)?.cooldownReason).toBe("auth-failure");
expect(accountManager.getAccountByIndex(0)?.coolingDownUntil).toBeGreaterThan(now);
expect(saveToDiskDebouncedSpy).toHaveBeenCalled();
});

it("deduplicates concurrent expired-token refresh and persistence", async () => {
const now = Date.now();
const storage = createStorage(now, 1);
Expand Down