fix(ai): fall back to default gateway when swarm Bifrost is unreachable#4571
Open
Evanfeenstra wants to merge 1 commit into
Open
fix(ai): fall back to default gateway when swarm Bifrost is unreachable#4571Evanfeenstra wants to merge 1 commit into
Evanfeenstra wants to merge 1 commit into
Conversation
The LLM inference call routes through one gateway — the primary workspace's swarm Bifrost proxy. When that swarm is unreachable (expired TLS cert, connection refused, timeout) the whole chat turn died with CERT_HAS_EXPIRED, even when the default gateway was healthy. Pre-flight the swarm gateway before committing to it: any resolved HTTP response means the connection is fine (keep the swarm route); a rejection means drop the entire Bifrost bundle (baseUrl + VK + macaroon) and fall back to the plain default gateway — the same path a non-Bifrost workspace takes. We probe rather than catch a mid-stream streamText error because by then the SSE response is already returned and the stream can't restart. Applied to both the main canvas stream (runCanvasAgent) and the follow-up questions enrichment. Verified against swarm38: a healthy gateway returns 200 at root, so no false fallbacks.
|
Test environment is now live. View it at: https://hive-preview-2.sphinx.chat Database expires at: Jun 28, 2026, 5:41 AM UTC |
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.
Problem
When a workspace's swarm has an expired TLS cert (or is otherwise unreachable), the entire chat turn dies with
CERT_HAS_EXPIRED:The LLM inference call routes through one gateway — the primary workspace's swarm Bifrost proxy (
bifrost.baseUrl). If that swarm is down, there's no fallback, so the turn fails even when the default gateway is perfectly healthy. (The concept pre-fetch already skips dead swarms; the LLM call didn't.)Fix
Pre-flight the swarm Bifrost gateway before committing to it:
CERT_HAS_EXPIRED,ECONNREFUSED, timeout,fetch failed): drop the entire Bifrost bundle (baseUrl + VK + macaroon) and fall back to the plain default gateway — the same path a non-Bifrost workspace already uses (default key, no override).We probe instead of catching a mid-stream
streamTexterror because by the time that surfaces, the 200 + SSE response is already returned to the client and the stream can't be restarted.This is not disabling cert verification — it routes around the broken swarm. The swarm cert still needs renewing; this just stops it from taking down chat.
Changes
src/lib/ai/provider.ts— sharedisGatewayReachable(baseUrl)probe (3s timeout).src/lib/ai/runCanvasAgent.ts— main canvas stream falls back to the default gateway when the swarm gateway is unreachable (covers/api/ask/quickand/api/ask/sync). Adds abifrostFellBackflag to the streamText log.src/services/canvas-turn-enrichments.ts— follow-up-questionsgenerateObject(which failed the same way) gets the same fallback.Verification
Probed the real endpoint
https://swarm38.sphinx.chat:8181:fetchto root resolves HTTP 200 →isGatewayReachable()returnstrue→ no false fallbacks on a healthy gateway; no need for a special/healthpath.fetchrejects → probe returnsfalse→ fall back to default gateway.Tradeoffs
BIFROST_ENABLEDallow-list). ~50–200ms on a healthy swarm; fails fast and rescues the turn on a dead one.