Repository navigation
feat(resilience): useUpstream429BreakerHints toggle (#2100 follow-up to #2116) #2133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -162,3 +162,63 @@ export function retryAfterFromResponse(response: { | |
| }): number | null { | ||
| return parseRetryAfter(getHeader(response.headers, "retry-after")); | ||
| } | ||
|
|
||
| /** | ||
| * Adapter that takes an error thrown by an HTTP client (fetch wrapper, axios, | ||
| * upstream SDK, etc.) and produces a {@link FailureKind} suitable for the | ||
| * `classifyError` option of the circuit breaker. | ||
| * | ||
| * Recognises the common error shapes: | ||
| * - `err.status` + `err.headers` + `err.body` (low-level fetch wrapper) | ||
| * - `err.response.status` + `err.response.headers` + `err.response.data` (axios-style) | ||
| * - `err.message` (last-resort body for keyword scan) | ||
| * | ||
| * Returns `undefined` when the error doesn't carry enough information to | ||
| * classify, so the breaker can decide what to do without a kind tag. | ||
| * | ||
| * Companion to issue #2100 follow-up. | ||
| */ | ||
| export function classify429FromError(err: unknown): FailureKind | undefined { | ||
| if (err === null || typeof err !== "object") return undefined; | ||
| const e = err as Record<string, unknown>; | ||
|
|
||
| let status: number | undefined; | ||
| let headers: Record<string, string> | undefined; | ||
| let body: unknown; | ||
|
|
||
| if (typeof e.status === "number") { | ||
| status = e.status; | ||
| } | ||
| if (typeof e.statusCode === "number" && status === undefined) { | ||
| status = e.statusCode; | ||
| } | ||
|
|
||
| if (e.response && typeof e.response === "object") { | ||
| const resp = e.response as Record<string, unknown>; | ||
| if (typeof resp.status === "number" && status === undefined) { | ||
| status = resp.status; | ||
| } | ||
| if (resp.headers && typeof resp.headers === "object") { | ||
| headers = resp.headers as Record<string, string>; | ||
| } | ||
|
Comment on lines
+222
to
+224
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blindly casting if (resp.headers && typeof resp.headers === "object") {
headers = (typeof (resp.headers as any).entries === "function")
? Object.fromEntries((resp.headers as any).entries())
: resp.headers as Record<string, string>;
} |
||
| if (resp.data !== undefined) { | ||
| body = resp.data; | ||
| } else if (typeof resp.body !== "undefined") { | ||
| body = resp.body; | ||
| } | ||
| } | ||
|
|
||
| if (headers === undefined && e.headers && typeof e.headers === "object") { | ||
| headers = e.headers as Record<string, string>; | ||
| } | ||
|
Comment on lines
+232
to
+234
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the if (headers === undefined && e.headers && typeof e.headers === "object") {
headers = (typeof (e.headers as any).entries === "function")
? Object.fromEntries((e.headers as any).entries())
: e.headers as Record<string, string>;
} |
||
| if (body === undefined) { | ||
| if (typeof e.body !== "undefined") { | ||
| body = e.body; | ||
| } else if (typeof e.message === "string") { | ||
| body = e.message; | ||
| } | ||
| } | ||
|
|
||
| if (typeof status !== "number") return undefined; | ||
| return classify429({ status, headers, body }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /** | ||
| * Per-provider default policy for upstream 429 hint trust. | ||
| * | ||
| * @see Issue #2100 follow-up — surface a user-overridable per-profile toggle | ||
| * that decides whether the circuit breaker uses upstream 429 body / Retry-After | ||
| * hints (`classify429`, `cooldownByKind`) to differentiate rate-limit from | ||
| * quota-exhausted failure cooldowns. | ||
| * | ||
| * This helper returns the **default** answer for a given provider. The actual | ||
| * runtime decision is the user override (if any) OR this default. See | ||
| * `accountFallback.ts` / `chat.ts` / `chatHelpers.ts` for the resolution | ||
| * call sites: | ||
| * | ||
| * ```ts | ||
| * const userValue = providerProfile.useUpstream429BreakerHints; // boolean | undefined | ||
| * const useHints = userValue !== undefined | ||
| * ? userValue | ||
| * : defaultUseUpstream429BreakerHints(provider); | ||
| * ``` | ||
| * | ||
| * Default policy: direct cloud providers default `true` because their 429 | ||
| * bodies and `Retry-After` headers are authoritative. Reverse-proxy / | ||
| * self-hosted / CLI-backed providers default `false` because forwarded 429 | ||
| * metadata is often unreliable or fabricated by the proxy. | ||
| * | ||
| * @module shared/utils/providerHints | ||
| */ | ||
|
|
||
| import { | ||
| UPSTREAM_PROXY_PROVIDERS, | ||
| SELF_HOSTED_CHAT_PROVIDER_IDS, | ||
| isLocalProvider, | ||
| isClaudeCodeCompatibleProvider, | ||
| } from "../constants/providers"; | ||
|
|
||
| /** | ||
| * Conservative per-provider default for `useUpstream429BreakerHints`. | ||
| * | ||
| * Returns `false` for any provider whose 429 metadata may be forwarded by | ||
| * an intermediary (proxy, self-hosted runtime, CLI wrapper). Returns `true` | ||
| * for direct cloud providers where the upstream response is authoritative. | ||
| */ | ||
| export function defaultUseUpstream429BreakerHints(providerId: string): boolean { | ||
| if (Object.prototype.hasOwnProperty.call(UPSTREAM_PROXY_PROVIDERS, providerId)) { | ||
| return false; | ||
| } | ||
| if (isLocalProvider(providerId)) { | ||
| return false; | ||
| } | ||
| if (SELF_HOSTED_CHAT_PROVIDER_IDS.has(providerId)) { | ||
| return false; | ||
| } | ||
| if (isClaudeCodeCompatibleProvider(providerId)) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the effective `useHints` decision: the user override wins if set, | ||
| * otherwise fall back to the per-provider default. | ||
| * | ||
| * `undefined` means "not user-set" and triggers the default lookup. | ||
| */ | ||
| export function resolveUseUpstream429BreakerHints( | ||
| providerId: string, | ||
| userValue: boolean | undefined, | ||
| ): boolean { | ||
| if (userValue !== undefined) { | ||
| return userValue; | ||
| } | ||
| return defaultUseUpstream429BreakerHints(providerId); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
useUpstream429BreakerHintsfield is missing from theProviderProfiletype definition (line 32) and is not being populated in thebuildProviderProfilehelper (line 180). Consequently,resolvedProfile.useUpstream429BreakerHintswill always beundefinedhere, causing the circuit breaker to ignore user-configured overrides and always fall back to the per-provider default. Please ensure both the type and the builder are updated to include this field.