Skip to content

Commit 2d8d9f4

Browse files
dodge1218Test
authored andcommitted
Fix quota exhaustion circuit breaker review findings
1 parent f845b58 commit 2d8d9f4

7 files changed

Lines changed: 111 additions & 7 deletions

File tree

‎src/agents/auth-profiles/state-observation.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ export function logAuthProfileFailureStateChange(params: {
6969
provider: params.provider,
7070
reason: params.reason,
7171
}),
72-
).catch((e) => {
73-
observationLog.error("failed to dispatch provider_tripped hook", { error: e?.message });
72+
).catch((error: unknown) => {
73+
observationLog.error("failed to dispatch provider_tripped hook", {
74+
error: error instanceof Error ? error.message : String(error),
75+
});
7476
});
7577
}
7678
}

‎src/agents/auth-profiles/usage.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,24 @@ describe("resolveProfilesUnavailableReason", () => {
298298
).toBe("billing");
299299
});
300300

301+
it("returns quota_exhausted for active quota disabled profiles", () => {
302+
const now = Date.now();
303+
const store = makeStore({
304+
"anthropic:default": {
305+
disabledUntil: now + 60_000,
306+
disabledReason: "quota_exhausted",
307+
},
308+
});
309+
310+
expect(
311+
resolveProfilesUnavailableReason({
312+
store,
313+
profileIds: ["anthropic:default"],
314+
now,
315+
}),
316+
).toBe("quota_exhausted");
317+
});
318+
301319
it("returns auth_permanent for active permanent auth disables", () => {
302320
const now = Date.now();
303321
const store = makeStore({

‎src/agents/auth-profiles/usage.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const FAILURE_REASON_PRIORITY: AuthProfileFailureReason[] = [
6464
"auth_permanent",
6565
"auth",
6666
"billing",
67+
"quota_exhausted",
6768
"format",
6869
"model_not_found",
6970
"overloaded",
@@ -418,7 +419,10 @@ type ResolvedAuthCooldownConfig = {
418419
failureWindowMs: number;
419420
};
420421

421-
type DisabledFailureReason = Extract<AuthProfileFailureReason, "billing" | "auth_permanent" | "quota_exhausted">;
422+
type DisabledFailureReason = Extract<
423+
AuthProfileFailureReason,
424+
"billing" | "auth_permanent" | "quota_exhausted"
425+
>;
422426

423427
type DisabledFailureBackoffPolicy = {
424428
baseMs: (cfg: ResolvedAuthCooldownConfig) => number;
@@ -619,7 +623,11 @@ function computeNextProfileUsageStats(params: {
619623
};
620624

621625
const disabledFailureReason =
622-
params.reason === "billing" || params.reason === "auth_permanent" || params.reason === "quota_exhausted" ? params.reason : null;
626+
params.reason === "billing" ||
627+
params.reason === "auth_permanent" ||
628+
params.reason === "quota_exhausted"
629+
? params.reason
630+
: null;
623631

624632
if (disabledFailureReason) {
625633
const disableCount = failureCounts[disabledFailureReason] ?? 1;

‎src/agents/embedded-agent-helpers/errors.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,9 @@ function classifyFailoverClassificationFromHttpStatus(
706706
return toReasonClassification(classify402Message(message));
707707
}
708708
if (status === 429) {
709+
if (messageReason === "quota_exhausted") {
710+
return toReasonClassification("quota_exhausted");
711+
}
709712
if (messageReason === "billing" && !isAmbiguousGeneric429BalanceMessage(message ?? "")) {
710713
return toReasonClassification("billing");
711714
}

‎src/agents/failover-error.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,12 @@ describe("failover-error", () => {
694694
message: "LLM error: monthly limit reached",
695695
}),
696696
).toBe("quota_exhausted");
697+
expect(
698+
resolveFailoverReasonFromError({
699+
status: 429,
700+
message: "429 monthly limit reached",
701+
}),
702+
).toBe("quota_exhausted");
697703
});
698704

699705
it("treats Chinese provider network/server errors as timeout for failover", () => {

‎src/agents/model-fallback.probe.test.ts‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ describe("runWithModelFallback – probe logic", () => {
240240
});
241241

242242
function resolveOpenAiCooldownDecision(params: {
243-
reason: "rate_limit" | "overloaded" | "timeout" | "auth" | "billing";
243+
reason: "rate_limit" | "overloaded" | "timeout" | "auth" | "billing" | "quota_exhausted";
244244
soonest: number | null;
245245
isPrimary?: boolean;
246246
hasFallbackCandidates?: boolean;
@@ -283,7 +283,9 @@ describe("runWithModelFallback – probe logic", () => {
283283
});
284284
}
285285

286-
async function expectPrimarySkippedAfterLongCooldown(reason: "billing") {
286+
async function expectPrimarySkippedAfterLongCooldown(
287+
reason: "billing" | "quota_exhausted" | "rate_limit",
288+
) {
287289
const cfg = makeCfg();
288290
const expiresIn30Min = NOW + 30 * 60 * 1000;
289291
mockedGetSoonestCooldownExpiry.mockReturnValue(expiresIn30Min);
@@ -747,4 +749,56 @@ describe("runWithModelFallback – probe logic", () => {
747749
"billing",
748750
);
749751
});
752+
753+
it("does not use billing's 30-second single-provider probe for quota exhaustion", async () => {
754+
const cfg = makeCfg({
755+
agents: {
756+
defaults: {
757+
model: {
758+
primary: "openai/gpt-4.1-mini",
759+
fallbacks: [],
760+
},
761+
},
762+
},
763+
} as Partial<OpenClawConfig>);
764+
765+
const expiresIn30Min = NOW + 30 * 60 * 1000;
766+
mockedGetSoonestCooldownExpiry.mockReturnValue(expiresIn30Min);
767+
mockedResolveProfilesUnavailableReason.mockReturnValue("quota_exhausted");
768+
769+
const run = vi.fn().mockResolvedValue("quota-probe");
770+
771+
await expect(
772+
runWithModelFallback({
773+
cfg,
774+
provider: "openai",
775+
model: "gpt-4.1-mini",
776+
fallbacksOverride: [],
777+
run,
778+
}),
779+
).rejects.toMatchObject({
780+
name: "FallbackSummaryError",
781+
attempts: [
782+
expect.objectContaining({
783+
provider: "openai",
784+
model: "gpt-4.1-mini",
785+
reason: "quota_exhausted",
786+
}),
787+
],
788+
});
789+
expect(run).not.toHaveBeenCalled();
790+
});
791+
792+
it("probes quota-exhausted primary only near disabled-window expiry", async () => {
793+
const cfg = makeCfg();
794+
const expiresIn1Min = NOW + 60 * 1000;
795+
mockedGetSoonestCooldownExpiry.mockReturnValue(expiresIn1Min);
796+
mockedResolveProfilesUnavailableReason.mockReturnValue("quota_exhausted");
797+
798+
const run = vi.fn().mockResolvedValue("quota-probe-ok");
799+
800+
const result = await runPrimaryCandidate(cfg, run);
801+
802+
expectPrimaryProbeSuccess(result, run, "quota-probe-ok");
803+
});
750804
});

‎src/agents/model-fallback.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ function resolveCooldownDecision(params: {
11661166
// 402 might have been misclassified. Probe single-provider setups on the
11671167
// standard throttle so they can recover without a restart; when fallbacks
11681168
// exist, only probe near cooldown expiry so the fallback chain stays preferred.
1169-
if (inferredReason === "billing" || inferredReason === "quota_exhausted") {
1169+
if (inferredReason === "billing") {
11701170
const shouldProbeSingleProviderBilling =
11711171
params.isPrimary &&
11721172
!params.hasFallbackCandidates &&
@@ -1181,6 +1181,19 @@ function resolveCooldownDecision(params: {
11811181
};
11821182
}
11831183

1184+
// Quota exhaustion is calendar/plan-window based, so avoid billing's
1185+
// 30-second single-provider probe loop. Only probe near disabled-window expiry.
1186+
if (inferredReason === "quota_exhausted") {
1187+
if (params.isPrimary && shouldProbe) {
1188+
return { type: "attempt", reason: inferredReason, markProbe: true };
1189+
}
1190+
return {
1191+
type: "skip",
1192+
reason: inferredReason,
1193+
error: `Provider ${params.candidate.provider} has ${inferredReason} issue (skipping all models)`,
1194+
};
1195+
}
1196+
11841197
const shouldAttemptDespiteCooldown =
11851198
(params.isPrimary && (!params.requestedModel || shouldProbe)) ||
11861199
(!params.isPrimary && shouldUseTransientCooldownProbeSlot(inferredReason));

0 commit comments

Comments
 (0)