Skip to content
Closed
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
2 changes: 1 addition & 1 deletion desktop/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "opencodex-desktop"
version = "2.65.0-vadymhimself.17"
version = "2.65.0-vadymhimself.18"
description = "OpenCodex desktop shell"
authors = ["OpenCodex contributors"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion desktop/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "OpenCodex",
"version": "2.65.0-vadymhimself.17",
"version": "2.65.0-vadymhimself.18",
"identifier": "com.opencodex.desktop",
"build": {
"frontendDist": "../ui",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitkyc08/opencodex",
"version": "2.65.0-vadymhimself.17",
"version": "2.65.0-vadymhimself.18",
"description": "Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code",
"type": "module",
"main": "./bin/package-main.mjs",
Expand Down
111 changes: 74 additions & 37 deletions src/oauth/anthropic-routing.ts

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/server/claude-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import { captureOAuthAccountSelection } from "../oauth/store";
import {
anthropicSessionKeyFromParts,
getAnthropicPoolAccessSnapshot,
anthropicModelFamily,
getAnthropicPoolRetryAfterSeconds,
isAnthropicAccountPoolEnabled,
promoteAnthropicActiveAccount,
Expand Down Expand Up @@ -1769,10 +1770,10 @@ export async function handleClaudeCountTokens(
: null,
promptCacheKeyIsSharedCohort: translated.cacheKeySource === "system",
});
const selection = resolveAnthropicAccountForSession(sessionKey, config);
const selection = resolveAnthropicAccountForSession(sessionKey, config, Date.now(), route.modelId);
if (!selection.accountId) {
if (selection.reason === "all-cooled") {
const retryAfter = getAnthropicPoolRetryAfterSeconds();
const retryAfter = getAnthropicPoolRetryAfterSeconds(Date.now(), anthropicModelFamily(route.modelId));
const response = anthropicErrorResponse(
429,
"All Anthropic OAuth accounts are temporarily rate-limited",
Expand Down
1 change: 1 addition & 0 deletions src/server/responses/adapter-continuation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ export function createAdapterContinuations(
anthropicSessionKey,
Date.now(),
response.headers,
route.modelId,
);
if (nextAccountId) {
try { void response.body?.cancel().catch(() => {}); } catch { /* already closed */ }
Expand Down
1 change: 1 addition & 0 deletions src/server/responses/adapter-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ export async function prepareAdapterExchange(
anthropicSessionKey,
Date.now(),
upstreamResponse.headers,
route.modelId,
);
if (!nextAccountId) break;
try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ }
Expand Down
5 changes: 3 additions & 2 deletions src/server/responses/request-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
formatAnthropicProviderForLog,
anthropicSessionKeyFromParts,
resolveAnthropicAccountForSession,
anthropicModelFamily,
getAnthropicPoolRetryAfterSeconds,
hasAnthropicFailoverQuorum,
} from "../../oauth/anthropic-routing";
Expand Down Expand Up @@ -534,10 +535,10 @@ export async function prepareResponsesTransport(
} else genericFailoverAccountId = admitted.accountId;
if (hasPassiveAccountQuota(route.providerName)) passiveQuotaWriterGeneration = captureConfigGeneration();
} else if (route.providerName === "anthropic" && isAnthropicAccountPoolEnabled(config)) {
const selection = resolveAnthropicAccountForSession(anthropicSessionKey, config);
const selection = resolveAnthropicAccountForSession(anthropicSessionKey, config, Date.now(), route.modelId);
if (!selection.accountId) {
if (selection.reason === "all-cooled") {
const retryAfterSec = getAnthropicPoolRetryAfterSeconds();
const retryAfterSec = getAnthropicPoolRetryAfterSeconds(Date.now(), anthropicModelFamily(route.modelId));
return formatErrorResponse(
429,
"rate_limit_error",
Expand Down
1 change: 1 addition & 0 deletions src/server/responses/sidecar-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export async function executeResponsesSidecars(
anthropicSessionKey,
Date.now(),
responseHeaders,
route.modelId,
);
if (!nextAccountId) {
hop.permit?.release();
Expand Down
48 changes: 48 additions & 0 deletions tests/adapters/anthropic/anthropic-ratelimit-headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
anthropicModelFamily,
clearAnthropicAccountCooldown,
clearAnthropicAccountPoolState,
forgetAnthropicFailoverQuorum,
getAnthropicAccountHealthSnapshot,
getAnthropicPoolRetryAfterSeconds,
getEligibleAnthropicAccounts,
rotateAnthropicAccountOn429,
resetAnthropicRoutingForManualSelection,
resolveAnthropicAccountForSession,
Expand Down Expand Up @@ -103,6 +106,51 @@ function drainedFiveHour(resetEpochSeconds: number): Headers {
});
}

describe("a model-scoped weekly 429 cools only that model family", () => {
/** A seven_day_opus refusal: the account's Opus allowance is spent, its other models are not. */
const opusWeekly = (resetEpochSeconds: number) => new Headers({
"anthropic-ratelimit-unified-status": "rejected",
"anthropic-ratelimit-unified-representative-claim": "seven_day_opus",
"anthropic-ratelimit-unified-reset": String(resetEpochSeconds),
"anthropic-ratelimit-unified-5h-status": "allowed",
"anthropic-ratelimit-unified-7d-status": "allowed",
});

test("Opus cools per account, Sonnet keeps the account, and every 429 logs its limit headers", async () => {
const start = Date.now();
const ids = await seed(2);
const reset = Math.floor(start / 1000) + 3 * 86_400;
const warn = spyOn(console, "warn").mockImplementation(() => {});
try {
expect(rotateAnthropicAccountOn429(poolEnabled(), ids[0]!, null, "s1", start, opusWeekly(reset), "claude-opus-5")).toBe(ids[1]!);
expect(warn.mock.calls.map(c => String(c[0])).find(l => l.startsWith("[anthropic-pool] 429 on "))).toEndWith(
" model=claude-opus-5 retry-after=- 5h-status=allowed 7d-status=allowed representative-claim=seven_day_opus"
+ ` reset=${reset} status=rejected cooldown=${3 * 86_400}s until ${new Date(reset * 1000).toISOString()}`
+ " (reset-derived) scope=opus");
} finally { warn.mockRestore(); }
expect(getAnthropicAccountHealthSnapshot(ids[0]!, start)).toBeNull();
expect(getEligibleAnthropicAccounts(start, "opus")).toEqual([ids[1]!]);
expect(getEligibleAnthropicAccounts(start, "sonnet")).toEqual(ids);

// Both accounts out of Opus: an Opus request is refused with the family's reset, a Sonnet one still routes.
rotateAnthropicAccountOn429(poolEnabled(), ids[1]!, null, "s2", start, opusWeekly(reset), "claude-opus-5");
expect(resolveAnthropicAccountForSession("s3", poolEnabled(), start, "claude-opus-5").reason).toBe("all-cooled");
expect(getAnthropicPoolRetryAfterSeconds(start, anthropicModelFamily("claude-opus-5"))).toBe(3 * 86_400);
expect(resolveAnthropicAccountForSession("s4", poolEnabled(), start, "claude-sonnet-5-5").accountId).not.toBeNull();
clearAnthropicAccountCooldown(ids[0]!);
expect(getEligibleAnthropicAccounts(start, "opus")).toEqual([ids[0]!]);
});

test("a five-hour claim on an Opus request still cools the whole account", async () => {
const start = Date.now();
const ids = await seed(2);
rotateAnthropicAccountOn429(poolEnabled(), ids[0]!, "120", null, start,
new Headers({ "anthropic-ratelimit-unified-representative-claim": "five_hour" }), "claude-opus-5");
expect(getAnthropicAccountHealthSnapshot(ids[0]!, start)?.cooldownUntil).toBe(start + 120_000);
expect(getEligibleAnthropicAccounts(start, "sonnet")).toEqual([ids[1]!]);
});
});

describe("Anthropic cooldown honours the stated window", () => {
test("a multi-hour Retry-After is not truncated to the guessed-backoff ceiling", async () => {
const start = Date.now();
Expand Down
2 changes: 1 addition & 1 deletion tests/server/agentradar-account-pin-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ test("an AgentRadar account pin serves exactly its own stored account (pin.check
codexAccounts: [1, 2].map(n => ({ id: `acct${n}`, email: `c${n}@example.invalid`, isMain: false, chatgptAccountId: `cga${n}` })),
});
const rotations = () => ocx.log.split("[anthropic-pool] credential denial on").length - 1;
const failovers429 = () => ocx.log.split("[anthropic-pool] 429 on").length - 1;
const failovers429 = () => ocx.log.split("; failing over to").length - 1;
try {
// Claude: unpinned control serves the active account; a pin serves exactly its own, both ways, and names it.
let r = await msg(ocx);
Expand Down
Loading