Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 66 additions & 5 deletions open-sse/services/combo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +3306 to +3309

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If providerFailureThreshold is configured as a negative number (e.g., -1), Number(-1) is -1, which is truthy and bypasses the || 2 fallback. This will set the threshold to -1, causing immediate skips of all targets for that provider (since 0 >= -1 is true). We should safely parse and validate the threshold to ensure it is a positive integer >= 1.

    const parsedThreshold = Number((config as Record<string, unknown>).providerFailureThreshold);
    const providerFailureThreshold = Number.isInteger(parsedThreshold) && parsedThreshold > 0 ? parsedThreshold : 2;

if (setTry > 0) {
log.info("COMBO", `All targets failed — retrying set (${setTry}/${maxSetRetries})`);
await new Promise((resolve) => {
Expand Down Expand Up @@ -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;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using consecutiveProviderFailures.has(provider) followed by consecutiveProviderFailures.get(provider)! performs redundant Map lookups. We can simplify this by using nullish coalescing consecutiveProviderFailures.get(provider) ?? 0, which is cleaner and more efficient.

Suggested change
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;
}
if (provider && (consecutiveProviderFailures.get(provider) ?? 0) >= providerFailureThreshold) {
log.info("COMBO", "Skipping " + modelStr + " — provider " + provider + " hit consecutive failure threshold (#3200)");
if (i > 0) fallbackCount++;
return null;
}


// Pre-check: skip models where no credentials are available (excluded, rate-limited, or unavailable)
if (isModelAvailable) {
const available = await isModelAvailable(modelStr, targetForAttempt);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The break statement on line 3858 only breaks out of the inner retry loop of executeTarget, not the outer target loop of handleComboChat. As a result, executeTarget still returns null, and the combo continues to try other models, completely defeating the purpose of the 400 Bad Request guard ("stopping combo" and "skipping fallback to other targets to prevent infinite loop"). To correctly stop the combo and return the error response immediately, we should return { ok: false, response: result }. This also makes the consecutiveProviderFailures tracking inside this block unnecessary since the entire combo execution is aborted.

          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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to the parsing in handleComboChat, if providerFailureThreshold is configured as a negative number, it will bypass the fallback and cause immediate skips of all targets for that provider in round-robin mode. We should safely parse and validate the threshold to ensure it is a positive integer >= 1.

  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++) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down