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
9 changes: 9 additions & 0 deletions src/codex/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,15 @@ export function tryAcquireCodexQuotaScopeProbeLease(
return probeLeaseId;
}

/** Side-effect-free check for a confirmed model-specific quota probe. */
export function canAcquireCodexQuotaScopeProbeLease(
accountId: string,
scope: CodexQuotaScope,
now = Date.now(),
): boolean {
return canAcquireQuotaProbeLease(scopedHealthFor(accountId, scope), now);
}

/**
* Hand a probe lease back without recording an upstream outcome. Used by paths
* that take a lease and then fail before any request reaches upstream.
Expand Down
16 changes: 10 additions & 6 deletions src/codex/subagent-model-fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import { CODEX_HOME, getCodexHome } from "./paths";
import { CODEX_UNKNOWN_USAGE_SCORE, getAccountQuota } from "./quota";
import {
canAcquireCodexQuotaProbeLease,
canAcquireCodexQuotaScopeProbeLease,
codexQuotaScopeForModel,
computeCodexUsageScore,
getCodexQuotaHealthSnapshot,
getPoolAccountPlan,
isCodexAccountInCooldown,
} from "./routing";
import {
isCodexAccountUsable,
Expand Down Expand Up @@ -233,11 +233,15 @@ export function isSubagentModelUnavailable(
// advances instead of selecting a candidate that exact auth will reject.
const quotaScope = codexQuotaScopeForModel(route.modelId);
if (getCodexQuotaHealthSnapshot(resolvedAccountId, quotaScope, now) !== null) return true;
} else if (
isCodexAccountInCooldown(resolvedAccountId, now)
&& !canAcquireCodexQuotaProbeLease(resolvedAccountId, now)
) {
return true;
} else {
const quotaScope = codexQuotaScopeForModel(route.modelId);
const cooldown = getCodexQuotaHealthSnapshot(resolvedAccountId, quotaScope, now);
if (cooldown !== null) {
const probeAvailable = cooldown.quotaScope
? canAcquireCodexQuotaScopeProbeLease(resolvedAccountId, cooldown.quotaScope, now)
: canAcquireCodexQuotaProbeLease(resolvedAccountId, now);
if (!probeAvailable) return true;
}
}
return isNativeModelQuotaExhausted(model, config, accountId, now);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/subagent-model-fallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,23 @@ describe("subagent model fallback chain", () => {
});
});

test("pool fallback skips a reset-derived cooldown in the model's quota scope", () => {
const now = 1_800_000_000_000;
updateAccountQuota("pool-a", 10, undefined, 20);
const config = cfg({ subagentModelFallback: ["kimi/k3"] });
recordCodexUpstreamOutcome(config, "pool-a", 429, {
modelId: "gpt-5.6-sol",
now,
resetAt: Math.floor((now + 60 * 60_000) / 1_000),
});

expect(selectAvailableSubagentModel("gpt-5.6-sol", config, [], "pool-a", now + 1)).toEqual({
model: "kimi/k3",
rewritten: true,
skipped: ["gpt-5.6-sol"],
});
});

test("account selector fallbacks still reject invalid or disabled native models", () => {
resetSubagentModelFallbackStateForTests();
updateAccountQuota("pool-a", 95, undefined, 20);
Expand Down
Loading