feat: provider fallback chain with primary-leave cooldown#413
Open
feat: provider fallback chain with primary-leave cooldown#413
Conversation
Adds a resilience layer modeled on hermes-agent's `_try_activate_fallback`
pattern: an ordered fallback chain swaps in a backup provider on auth
(401/403) or rate-limit (429/quota) failures, while a 60s cooldown clock
is set only when leaving the primary — chain-internal switches don't
re-arm it. Subsequent requests skip a cooled-down primary and go
straight to the first healthy fallback.
- New `src/gateway/provider-fallback.ts` with `ProviderFallbackController`,
module-level cooldown map, error classifier, and `callWithProviderFallback`
wrapper.
- Wraps both tool-chat and streaming tool-chat handlers in
`openai-compatible.ts`. Streaming retries refuse mid-stream switches
to avoid duplicated text deltas.
- Configured via `HYBRIDAI_FALLBACK_CHAIN` env var (JSON array of
`{model, baseUrl?, keyEnv?, chatbotId?, agentId?}` entries).
- 11 new unit tests covering chain parsing, error classification,
primary-leave cooldown semantics, key-env override, primary-cooldown
skip, and exhausted-chain re-throw.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a provider fallback controller for the OpenAI-compatible gateway path to improve resiliency by retrying requests against a configured fallback chain when auth or rate-limit errors occur, including a “primary leave” cooldown mechanism and streaming-safe behavior.
Changes:
- Introduces
ProviderFallbackController+ helpers to parseHYBRIDAI_FALLBACK_CHAIN, classify provider errors, and apply a primary-provider cooldown. - Wraps OpenAI-compatible tool-chat handlers (non-streaming + streaming) with
callWithProviderFallback. - Adds a new unit test suite validating parsing, classification, chain advancement, cooldown semantics, and keyEnv behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/gateway/provider-fallback.ts |
Implements fallback chain parsing, error classification, cooldown tracking, and the fallback wrapper/controller. |
src/gateway/openai-compatible.ts |
Applies the fallback wrapper to tool-chat request paths (including streaming). |
tests/provider-fallback.test.ts |
Adds unit coverage for fallback chain behavior and cooldown semantics. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+26
to
+28
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return !!value && typeof value === 'object' && !Array.isArray(value); | ||
| } |
Comment on lines
+212
to
+225
| if ( | ||
| params.chain.length > 0 && | ||
| isProviderCooledDown(params.primaryRuntime.provider) | ||
| ) { | ||
| const activation = await controller.tryActivate( | ||
| 'rate_limit', | ||
| params.primaryRuntime.provider, | ||
| ); | ||
| if (activation) { | ||
| runtime = activation.runtime; | ||
| model = activation.model; | ||
| params.onFallback?.(activation, 'rate_limit'); | ||
| } | ||
| } |
Comment on lines
+708
to
+738
| const result = await callWithProviderFallback({ | ||
| primaryRuntime: runtime, | ||
| primaryModel: prepared.model, | ||
| chain: loadFallbackChainFromEnv(), | ||
| invoke: async (activeRuntime, activeModel) => { | ||
| if (streamStarted) { | ||
| throw new Error( | ||
| 'Stream already started; cannot retry provider fallback mid-stream.', | ||
| ); | ||
| } | ||
| return callOpenAICompatibleModelStream({ | ||
| runtime: activeRuntime, | ||
| model: activeModel, | ||
| messages, | ||
| tools: input.tools, | ||
| toolChoice: input.toolChoice, | ||
| onTextDelta: (delta) => { | ||
| if (!delta) return; | ||
| streamStarted = true; | ||
| if (!isResponseWritable(res)) return; | ||
| writeOpenAICompatibleStreamChunk( | ||
| res, | ||
| buildOpenAICompatibleStreamTextChunk({ | ||
| completionId, | ||
| created, | ||
| model: prepared.responseModel, | ||
| content: delta, | ||
| }), | ||
| ); | ||
| }, | ||
| }); |
- Reuse the shared `isRecord` helper from `src/utils/type-guards.ts`
instead of redeclaring it locally.
- Stop re-arming the primary cooldown on the cooled-down skip path:
`tryActivate` now accepts `{ markCooldown }`, and the initial skip in
`callWithProviderFallback` passes `false`. Without this, steady traffic
while the primary was cooling down would push its deadline forward on
every request and the primary would never recover. Covered by a new
test that fires three back-to-back requests against a cooled-down
primary and asserts the original 5 s deadline is honored.
- Add an optional `shouldFallback(err, reason)` callback to
`callWithProviderFallback`. The streaming tool-chat handler passes
`() => !streamStarted`, so a mid-stream provider failure now
re-throws the original 401/429 error instead of being masked by a
generic "Stream already started" placeholder. Covered by tests for
both the suppress and allow paths.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a resilience layer in front of the OpenAI-compatible gateway path so a single auth or rate-limit failure no longer breaks a chat. Inspired by the
_try_activate_fallbackpattern inhermes-agent/run_agent.py:streamStartedand refuses mid-stream switches to avoid duplicated text deltas.Configuration is via a new env var,
HYBRIDAI_FALLBACK_CHAIN, holding a JSON array of entries:Each entry supports
model(required), optionalbaseUrl,keyEnv(env var to read the API key from),chatbotId, andagentId.Changes
src/gateway/provider-fallback.ts(new):ProviderFallbackController, module-level cooldown map,classifyProviderError,loadFallbackChainFromEnv, and thecallWithProviderFallbackwrapper.src/gateway/openai-compatible.ts: wraps the two tool-chat handlers (non-streaming + streaming) incallWithProviderFallback.tests/provider-fallback.test.ts(new): 11 unit tests covering chain parsing, error classification, chain advancement with skip-on-resolve-failure, primary-leave cooldown semantics, key-env override, primary-cooldown short-circuit, and exhausted-chain re-throw.Verification
npx tsc --noEmit— clean.npx vitest run tests/provider-fallback.test.ts— 11/11 passing.main: identical pass count, no regressions introduced (the pre-existingproviders.factory.test.tsandgateway-http-server.test.tsfailures predate this branch).Test plan
HYBRIDAI_FALLBACK_CHAINto a chain that starts with a deliberately-bad API key for the primary; confirm the request still completes via fallback.🤖 Generated with Claude Code