Skip to content

Commit 93f62f5

Browse files
committed
fix: skip cached-blocked fallback retries
1 parent 3f22577 commit 93f62f5

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,19 @@ while (attempted.size < Math.max(1, accountCount)) {
21302130
refreshToken: fallbackAccount.refreshToken,
21312131
index: fallbackAccount.index,
21322132
});
2133+
const fallbackEntitlementBlock = entitlementCache.isBlocked(
2134+
fallbackEntitlementAccountKey,
2135+
model ?? modelFamily,
2136+
);
2137+
if (fallbackEntitlementBlock.blocked) {
2138+
runtimeMetrics.accountRotations++;
2139+
runtimeMetrics.lastError =
2140+
`Entitlement cached block for account ${fallbackAccount.index + 1}`;
2141+
logWarn(
2142+
`Skipping account ${fallbackAccount.index + 1} due to cached entitlement block (${formatWaitTime(fallbackEntitlementBlock.waitMs)} remaining).`,
2143+
);
2144+
continue;
2145+
}
21332146

21342147
if (!accountManager.consumeToken(fallbackAccount, modelFamily, model)) {
21352148
continue;

test/index-retry.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ vi.mock("../lib/accounts.js", async () => {
120120
source,
121121
storedEmail,
122122
accessToken,
123+
idToken,
123124
}: {
124125
storedAccountId?: string;
125126
source?: string;
126127
storedEmail?: string;
127128
accessToken?: string;
129+
idToken?: string;
128130
}) => {
129131
const tokenUtilsModule = tokenUtils as typeof import("../lib/auth/token-utils.js");
130132
const tokenAccountId = accessToken ? "account-1" : undefined;
131133
const tokenEmail = tokenUtilsModule.sanitizeEmail(
132-
tokenUtilsModule.extractAccountEmail(accessToken, undefined),
134+
tokenUtilsModule.extractAccountEmail(accessToken, idToken),
133135
);
134136
const sanitizedStoredEmail = tokenUtilsModule.sanitizeEmail(storedEmail);
135137
return {

test/index.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,19 +1237,22 @@ describe("OpenAIOAuthPlugin fetch handler", () => {
12371237
});
12381238

12391239
it("uses the refreshed token email when checking entitlement blocks", async () => {
1240-
mockStorage.accounts = [
1240+
const { AccountManager } = await import("../lib/accounts.js");
1241+
const manager = buildRoutingManager([
12411242
{
1243+
index: 0,
12421244
accountId: "acc-1",
12431245
email: "stale@example.com",
12441246
refreshToken: "refresh-1",
12451247
},
1246-
];
1248+
]);
1249+
vi.spyOn(AccountManager, "loadFromDisk").mockResolvedValueOnce(manager as never);
12471250
extractAccountEmailMock.mockReturnValueOnce("fresh@example.com");
12481251
const entitlementModule = await import("../lib/entitlement-cache.js");
12491252
const isBlockedSpy = vi
12501253
.spyOn(entitlementModule.EntitlementCache.prototype, "isBlocked")
12511254
.mockImplementation(() => {
1252-
expect(mockStorage.accounts[0]?.email).toBe("stale@example.com");
1255+
expect(manager.getAccountsSnapshot()[0]?.email).toBe("stale@example.com");
12531256
return { blocked: false, waitMs: 0 };
12541257
});
12551258
globalThis.fetch = vi.fn().mockResolvedValue(
@@ -1267,6 +1270,7 @@ describe("OpenAIOAuthPlugin fetch handler", () => {
12671270
"account:acc-1::email:fresh@example.com",
12681271
"gpt-5.1",
12691272
);
1273+
expect(manager.getAccountsSnapshot()[0]?.email).toBe("fresh@example.com");
12701274
});
12711275

12721276
it.each([

0 commit comments

Comments
 (0)