Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(rotation): persist cooldown when account has no resolvable accountId
The missing-accountId branch in the runtime rotation loop marked the
account cooling down (`markAccountCoolingDown`) but was the lone cooldown
branch that never called `saveToDiskDebounced()` — every other branch
(network-error, 429, server-error, 401 invalidation) persists its
mutation. Because `coolingDownUntil`/`cooldownReason` are serialized in
the V3 snapshot, a process restart inside the 30s cooldown window dropped
the cooldown and immediately re-selected the still-broken account.

Impact is low (no upstream request is issued and rotation proceeds
gracefully in-memory), but the inconsistency is a real durability gap.
Add the missing `saveToDiskDebounced()` so the branch matches the others
and the cooldown survives a restart.

Found via a codebase-wide bug-hunt sweep. Regression test asserts the
branch fires (account with no stored accountId + non-JWT access token)
and persists; it fails without the fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
  • Loading branch information
ndycode and claude committed Jun 14, 2026
commit 655c144ad45405046000e2f042e5b9755ac079d0
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
39 changes: 39 additions & 0 deletions test/runtime-rotation-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,45 @@ 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.
expect(calls).toHaveLength(0);
Comment thread
greptile-apps[bot] marked this conversation as resolved.
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