From f357ed033a8b4ec6564e453a1f64378e186462d3 Mon Sep 17 00:00:00 2001 From: ZeyuFu Date: Sun, 17 May 2026 10:28:05 -0400 Subject: [PATCH] fix(runtime-fallback): classify more provider quota error names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #3937. Adds three small classification gaps to `classifyErrorType` so that quota-exhaustion errors from a wider range of providers trigger configured fallback chains instead of looping retry attempts: - Normalize error names by stripping `_` and `-` so snake_case / SCREAMING_SNAKE_CASE provider names (`insufficient_quota`, `RESOURCE_EXHAUSTED`, `rate_limit_exceeded`) match the existing alphanumeric `.includes()` checks. - Add `resourceexhausted` to the quota error-name allow-list to cover Google Generative AI's gRPC code 8 / `ResourceExhausted` surface. - Add `/resource.?exhausted/i` to the quota message-pattern list so the same error surface is caught when the provider only sets a generic error name but puts the signal in the message. Three new regression tests in `quota-error-classifier.regression.test.ts` cover: - Google `RESOURCE_EXHAUSTED` (gRPC error name + quota-shaped message) - Google `ResourceExhausted` message form without HTTP status - OpenAI snake_case `insufficient_quota` error name No existing tests were touched; the underscore normalization preserves all existing `.includes()` matches by rewriting the one underscore-bearing literal (`ai_loadapikeyerror` → `ailoadapikeyerror`) so previously matched names still resolve. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../runtime-fallback/error-classifier.ts | 9 +++- .../quota-error-classifier.regression.test.ts | 48 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/hooks/runtime-fallback/error-classifier.ts b/src/hooks/runtime-fallback/error-classifier.ts index 33b17ccf3..1f18b93ad 100644 --- a/src/hooks/runtime-fallback/error-classifier.ts +++ b/src/hooks/runtime-fallback/error-classifier.ts @@ -106,10 +106,13 @@ function isLocalizedQuotaExhaustionMessage(message: string): boolean { export function classifyErrorType(error: unknown): string | undefined { const message = getErrorMessage(error) - const errorName = extractErrorName(error)?.toLowerCase() + // Normalize by stripping underscores and dashes so snake_case / kebab-case + // provider error names (e.g. "insufficient_quota", "RESOURCE_EXHAUSTED") + // match the existing alphanumeric .includes() checks below. + const errorName = extractErrorName(error)?.toLowerCase().replace(/[_-]/g, "") if ( - errorName?.includes("ai_loadapikeyerror") || + errorName?.includes("ailoadapikeyerror") || errorName?.includes("loadapi") || (/api.?key.?is.?missing/i.test(message) && /environment variable/i.test(message)) ) { @@ -132,6 +135,7 @@ export function classifyErrorType(error: unknown): string | undefined { errorName?.includes("quotaexceeded") || errorName?.includes("insufficientquota") || errorName?.includes("billingerror") || + errorName?.includes("resourceexhausted") || /quota.?exceeded/i.test(message) || /exceeded.*quota/i.test(message) || /usage\s*quota/i.test(message) || @@ -139,6 +143,7 @@ export function classifyErrorType(error: unknown): string | undefined { /insufficient.?(?:quota|balance|funds?)/i.test(message) || /billing.?(?:hard.?)?limit/i.test(message) || /exhausted\s+your\s+capacity/i.test(message) || + /resource.?exhausted/i.test(message) || /out\s+of\s+credits?/i.test(message) || /payment.?required/i.test(message) || /usage\s+limit/i.test(message) || diff --git a/src/hooks/runtime-fallback/quota-error-classifier.regression.test.ts b/src/hooks/runtime-fallback/quota-error-classifier.regression.test.ts index f55c632cf..1a26bfb89 100644 --- a/src/hooks/runtime-fallback/quota-error-classifier.regression.test.ts +++ b/src/hooks/runtime-fallback/quota-error-classifier.regression.test.ts @@ -89,4 +89,52 @@ describe("runtime-fallback quota error regressions", () => { expect(errorType).toBe("quota_exceeded") expect(retryable).toBe(true) }) + + test("classifies Google RESOURCE_EXHAUSTED (gRPC code 8) as quota_exceeded", () => { + //#given + const error = { + name: "RESOURCE_EXHAUSTED", + message: "Quota exceeded for quota metric 'Generate Content' and limit 'Generate Content quota per minute'.", + } + + //#when + const errorType = classifyErrorType(error) + const retryable = isRetryableError(error, [429, 500, 502, 503, 504]) + + //#then + expect(errorType).toBe("quota_exceeded") + expect(retryable).toBe(true) + }) + + test("classifies Google ResourceExhausted message without HTTP status as quota_exceeded", () => { + //#given + const error = { + name: "GoogleGenerativeAIError", + message: "Resource exhausted: Please try again later.", + } + + //#when + const errorType = classifyErrorType(error) + const retryable = isRetryableError(error, [429, 500, 502, 503, 504]) + + //#then + expect(errorType).toBe("quota_exceeded") + expect(retryable).toBe(true) + }) + + test("classifies snake_case OpenAI insufficient_quota error name as quota_exceeded", () => { + //#given + const error = { + name: "insufficient_quota", + message: "You exceeded your current quota, please check your plan and billing details.", + } + + //#when + const errorType = classifyErrorType(error) + const retryable = isRetryableError(error, [429, 500, 502, 503, 504]) + + //#then + expect(errorType).toBe("quota_exceeded") + expect(retryable).toBe(true) + }) })