Repository navigation
fix(combo): prevent infinite same-provider fallback loop (#3200) #3145
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
d1a74c2
6ad60d4
4aa1b27
63b0f80
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3298,6 +3298,15 @@ export async function handleComboChat({ | |||||||||||||||||||||
| // Reset each retry so providers excluded in a previous attempt get another chance. | ||||||||||||||||||||||
| const exhaustedProviders = new Set<string>(); | ||||||||||||||||||||||
| const transientRateLimitedProviders = new Set<string>(); | ||||||||||||||||||||||
| // #3200: Track consecutive failures per provider across targets to break infinite | ||||||||||||||||||||||
| // loops where a single failing provider exhausts all its targets before the combo | ||||||||||||||||||||||
| // moves to a different provider. Once a provider fails >= threshold times in a row, | ||||||||||||||||||||||
| // all remaining targets from that provider are skipped for this set iteration. | ||||||||||||||||||||||
| const consecutiveProviderFailures = new Map<string, number>(); | ||||||||||||||||||||||
| const providerFailureThreshold = | ||||||||||||||||||||||
| (config as Record<string, unknown>).providerFailureThreshold | ||||||||||||||||||||||
| ? Number((config as Record<string, unknown>).providerFailureThreshold) || 2 | ||||||||||||||||||||||
| : 2; | ||||||||||||||||||||||
| if (setTry > 0) { | ||||||||||||||||||||||
| log.info("COMBO", `All targets failed — retrying set (${setTry}/${maxSetRetries})`); | ||||||||||||||||||||||
| await new Promise((resolve) => { | ||||||||||||||||||||||
|
|
@@ -3360,6 +3369,12 @@ export async function handleComboChat({ | |||||||||||||||||||||
| return null; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (provider && consecutiveProviderFailures.has(provider) && consecutiveProviderFailures.get(provider)! >= providerFailureThreshold) { | ||||||||||||||||||||||
| log.info("COMBO", `Skipping ${modelStr} — provider ${provider} hit consecutive failure threshold (#3200)`); | ||||||||||||||||||||||
| if (i > 0) fallbackCount++; | ||||||||||||||||||||||
| return null; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
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. Using
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Pre-check: skip models where no credentials are available (excluded, rate-limited, or unavailable) | ||||||||||||||||||||||
| if (isModelAvailable) { | ||||||||||||||||||||||
| const available = await isModelAvailable(modelStr, targetForAttempt); | ||||||||||||||||||||||
|
|
@@ -3537,6 +3552,10 @@ export async function handleComboChat({ | |||||||||||||||||||||
| }); | ||||||||||||||||||||||
| return null; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| // #3200: Reset consecutive failure counter for this provider on success. | ||||||||||||||||||||||
| if (provider && provider !== "unknown") { | ||||||||||||||||||||||
| consecutiveProviderFailures.delete(provider); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| const latencyMs = Date.now() - startTime; | ||||||||||||||||||||||
| emit("combo.target.succeeded", { | ||||||||||||||||||||||
| comboName: combo.name, | ||||||||||||||||||||||
|
|
@@ -3827,22 +3846,32 @@ export async function handleComboChat({ | |||||||||||||||||||||
| if (!lastStatus) lastStatus = result.status; | ||||||||||||||||||||||
| if (i > 0) fallbackCount++; | ||||||||||||||||||||||
| log.warn("COMBO", `Model ${modelStr} failed with body-specific error, stopping combo`); | ||||||||||||||||||||||
| break; // Break out of the target loop to avoid trying other models | ||||||||||||||||||||||
| if (provider && provider !== "unknown") { | ||||||||||||||||||||||
| const prev = consecutiveProviderFailures.get(provider) ?? 0; | ||||||||||||||||||||||
| const next = prev + 1; | ||||||||||||||||||||||
| consecutiveProviderFailures.set(provider, next); | ||||||||||||||||||||||
| if (next >= providerFailureThreshold && !exhaustedProviders.has(provider)) { | ||||||||||||||||||||||
| exhaustedProviders.add(provider); | ||||||||||||||||||||||
| log.info("COMBO", `Provider ${provider} consecutive failure threshold reached (${next}/${providerFailureThreshold}) — skipping remaining targets (#3200)`); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| break; | ||||||||||||||||||||||
|
Comment on lines
+3859
to
+3868
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. The return { ok: false, response: result }; |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Trigger shared provider circuit breaker for 5xx errors and connection failures. | ||||||||||||||||||||||
| // If the next target in the combo is on the same provider, don't mark the provider | ||||||||||||||||||||||
| // as failed — different models on the same provider may still succeed. | ||||||||||||||||||||||
| // G-02: when fallbackResult.skipProviderBreaker is set (embedded service supervisor | ||||||||||||||||||||||
| // outage signalled via X-Omni-Fallback-Hint: connection_cooldown) apply connection | ||||||||||||||||||||||
| // cooldown only — do NOT trip the whole-provider breaker. | ||||||||||||||||||||||
| const nextTarget = orderedTargets[i + 1]; | ||||||||||||||||||||||
| const sameProviderNext = | ||||||||||||||||||||||
| typeof nextTarget?.provider === "string" && nextTarget.provider === provider; | ||||||||||||||||||||||
| const providerHitThreshold = | ||||||||||||||||||||||
| provider && | ||||||||||||||||||||||
| provider !== "unknown" && | ||||||||||||||||||||||
| (consecutiveProviderFailures.get(provider) ?? 0) >= providerFailureThreshold; | ||||||||||||||||||||||
| if ( | ||||||||||||||||||||||
| !isStreamReadinessFailure && | ||||||||||||||||||||||
| isProviderFailureCode(result.status) && | ||||||||||||||||||||||
| !sameProviderNext && | ||||||||||||||||||||||
| (!sameProviderNext || providerHitThreshold) && | ||||||||||||||||||||||
| !fallbackResult.skipProviderBreaker | ||||||||||||||||||||||
| ) { | ||||||||||||||||||||||
| recordProviderFailure(provider, log, target.connectionId, profile); | ||||||||||||||||||||||
|
|
@@ -3872,6 +3901,16 @@ export async function handleComboChat({ | |||||||||||||||||||||
| if (i > 0) fallbackCount++; | ||||||||||||||||||||||
| log.warn("COMBO", `Model ${modelStr} failed, trying next`, { status: result.status }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (provider && provider !== "unknown") { | ||||||||||||||||||||||
| const prev = consecutiveProviderFailures.get(provider) ?? 0; | ||||||||||||||||||||||
| const next = prev + 1; | ||||||||||||||||||||||
| consecutiveProviderFailures.set(provider, next); | ||||||||||||||||||||||
| if (next >= providerFailureThreshold && !exhaustedProviders.has(provider)) { | ||||||||||||||||||||||
| exhaustedProviders.add(provider); | ||||||||||||||||||||||
| log.info("COMBO", `Provider ${provider} consecutive failure threshold reached (${next}/${providerFailureThreshold}) — skipping remaining targets (#3200)`); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const fallbackWaitMs = | ||||||||||||||||||||||
| fallbackDelayMs > 0 && cooldownMs > 0 && cooldownMs <= MAX_FALLBACK_WAIT_MS | ||||||||||||||||||||||
| ? Math.min(cooldownMs, fallbackDelayMs) | ||||||||||||||||||||||
|
|
@@ -4086,6 +4125,11 @@ async function handleRoundRobinCombo({ | |||||||||||||||||||||
| // provider are skipped to avoid the cascade through N same-provider targets. | ||||||||||||||||||||||
| const exhaustedProviders = new Set<string>(); | ||||||||||||||||||||||
| const transientRateLimitedProviders = new Set<string>(); | ||||||||||||||||||||||
| const consecutiveProviderFailures = new Map<string, number>(); | ||||||||||||||||||||||
| const providerFailureThreshold = | ||||||||||||||||||||||
| (config as Record<string, unknown>).providerFailureThreshold | ||||||||||||||||||||||
| ? Number((config as Record<string, unknown>).providerFailureThreshold) || 2 | ||||||||||||||||||||||
| : 2; | ||||||||||||||||||||||
|
Comment on lines
+4139
to
+4142
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 parsing in const parsedThreshold = Number((config as Record<string, unknown>).providerFailureThreshold);
const providerFailureThreshold = Number.isInteger(parsedThreshold) && parsedThreshold > 0 ? parsedThreshold : 2; |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Try each model starting from the round-robin target | ||||||||||||||||||||||
| for (let offset = 0; offset < modelCount; offset++) { | ||||||||||||||||||||||
|
|
@@ -4122,6 +4166,13 @@ async function handleRoundRobinCombo({ | |||||||||||||||||||||
| continue; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const rrConsecutiveFails = consecutiveProviderFailures.get(provider) ?? 0; | ||||||||||||||||||||||
| if (provider && rrConsecutiveFails >= providerFailureThreshold) { | ||||||||||||||||||||||
| log.info("COMBO-RR", `Skipping ${modelStr} — provider ${provider} consecutive failure threshold (${rrConsecutiveFails}/${providerFailureThreshold}) (#3200)`); | ||||||||||||||||||||||
| if (offset > 0) fallbackCount++; | ||||||||||||||||||||||
| continue; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Acquire semaphore slot (may wait in queue) | ||||||||||||||||||||||
| let release: () => void; | ||||||||||||||||||||||
| try { | ||||||||||||||||||||||
|
|
@@ -4398,6 +4449,16 @@ async function handleRoundRobinCombo({ | |||||||||||||||||||||
| if (offset > 0) fallbackCount++; | ||||||||||||||||||||||
| log.warn("COMBO-RR", `${modelStr} failed, trying next model`, { status: result.status }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (provider && provider !== "unknown") { | ||||||||||||||||||||||
| const prev = consecutiveProviderFailures.get(provider) ?? 0; | ||||||||||||||||||||||
| const next = prev + 1; | ||||||||||||||||||||||
| consecutiveProviderFailures.set(provider, next); | ||||||||||||||||||||||
| if (next >= providerFailureThreshold && !exhaustedProviders.has(provider)) { | ||||||||||||||||||||||
| exhaustedProviders.add(provider); | ||||||||||||||||||||||
| log.info("COMBO-RR", `Provider ${provider} consecutive failure threshold reached (${next}/${providerFailureThreshold}) — skipping remaining targets (#3200)`); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const fallbackWaitMs = | ||||||||||||||||||||||
| fallbackDelayMs > 0 && cooldownMs > 0 && cooldownMs <= MAX_FALLBACK_WAIT_MS | ||||||||||||||||||||||
| ? Math.min(cooldownMs, fallbackDelayMs) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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.
If
providerFailureThresholdis configured as a negative number (e.g.,-1),Number(-1)is-1, which is truthy and bypasses the|| 2fallback. This will set the threshold to-1, causing immediate skips of all targets for that provider (since0 >= -1istrue). We should safely parse and validate the threshold to ensure it is a positive integer >= 1.