Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
chore(merge): resolve combo.ts conflict — preserve exhaustedProviders (#1731) + falloverBeforeRetry (#2417)
diegosouzapwa9cef6dbadfd
diff --cc open-sse/services/combo.tsindex aa3f5d5a83,6a55879568..6a9db8ce0e--- a/open-sse/services/combo.ts+++ b/open-sse/services/combo.ts@@@ -1992,235 -1956,289 +1994,313 @@@ export async function handleComboChat(return comboModelNotFoundResponse("Combo has no executable targets");}+ // #1731: Per-request in-memory set of providers whose quota is fully exhausted.- // When a target returns a quota-exhausted 429 (subscription/credits/daily),- // remaining targets from the same provider are skipped to avoid the- // 2–5 minute cascade through N same-provider targets.++ // When a target returns a quota-exhausted 429, remaining same-provider targets are skipped.+ const exhaustedProviders = new Set<string>();-- let lastError = null;- let earliestRetryAfter = null;- let lastStatus = null;- const startTime = Date.now();let globalAttempts = 0;- let fallbackCount = 0;- let recordedAttempts = 0;-- for (let i = 0; i < orderedTargets.length; i++) {- const target = orderedTargets[i];- const modelStr = target.modelStr;- const provider = target.provider;- const profile = await getRuntimeProviderProfile(provider);- // Pre-check: skip models where all accounts are in cooldown- if (isModelAvailable) {- const available = await isModelAvailable(modelStr, target);- if (!available) {- log.info("COMBO", `Skipping ${modelStr} (all accounts in cooldown)`);- if (i > 0) fallbackCount++;- continue;+ for (let setTry = 0; setTry <= maxSetRetries; setTry++) {+ if (setTry > 0) {+ log.info("COMBO", `All targets failed — retrying set (${setTry}/${maxSetRetries})`);+ await new Promise((resolve) => {+ const timer = setTimeout(resolve, setRetryDelayMs);+ signal?.addEventListener(+ "abort",+ () => {+ clearTimeout(timer);+ resolve(undefined);+ },+ { once: true }+ );+ });+ if (signal?.aborted) {+ log.info("COMBO", "Client disconnected during set retry delay — aborting");+ return errorResponse(499, "Client disconnected");}}- // #1731: Skip targets from a provider that already signaled full quota exhaustion- // this request.- if (provider && exhaustedProviders.has(provider)) {- log.info(- "COMBO",- `Skipping ${modelStr} — provider ${provider} marked exhausted this request (#1731)`- );- if (i > 0) fallbackCount++;- continue;- }+ let lastError = null;+ let earliestRetryAfter = null;+ let lastStatus = null;+ const startTime = Date.now();+ let fallbackCount = 0;+ let recordedAttempts = 0;- // Retry loop for transient errors- for (let retry = 0; retry <= maxRetries; retry++) {- // Fix #1681: Bail out immediately if the client has disconnected- if (signal?.aborted) {- log.info("COMBO", `Client disconnected — aborting combo loop before model ${modelStr}`);- return errorResponse(499, "Client disconnected");- }- globalAttempts++;- if (globalAttempts > MAX_GLOBAL_ATTEMPTS) {- log.warn(- "COMBO",- `Maximum combo attempts (${MAX_GLOBAL_ATTEMPTS}) exceeded across all targets and fallbacks. Terminating loop to prevent runaway background requests.`- );- return errorResponse(503, "Maximum combo retry limit reached");- }- if (retry > 0) {+ for (let i = 0; i < orderedTargets.length; i++) {+ const target = orderedTargets[i];+ const modelStr = target.modelStr;+ const provider = target.provider;+ const profile = await getRuntimeProviderProfile(provider);+++ // #1731: Skip targets from a provider that already signaled full quota exhaustion this request.++ if (provider && exhaustedProviders.has(provider)) {+ log.info(+ "COMBO",- `Retrying ${modelStr} in ${retryDelayMs}ms (attempt ${retry + 1}/${maxRetries + 1})`++ `Skipping ${modelStr} — provider ${provider} marked exhausted this request (#1731)`+ );- await new Promise((resolve) => {- const timer = setTimeout(resolve, retryDelayMs);- signal?.addEventListener(- "abort",- () => {- clearTimeout(timer);- resolve(undefined);- },- { once: true }- );- });++ if (i > 0) fallbackCount++;++ continue;++ }+++ // Pre-check: skip models where all accounts are in cooldown+ if (isModelAvailable) {+ const available = await isModelAvailable(modelStr, target);+ if (!available) {+ log.info("COMBO", `Skipping ${modelStr} (all accounts in cooldown)`);+ if (i > 0) fallbackCount++;+ continue;+ }+ }++ // Retry loop for transient errors+ for (let retry = 0; retry <= maxRetries; retry++) {+ // Fix #1681: Bail out immediately if the client has disconnectedif (signal?.aborted) {- log.info("COMBO", `Client disconnected during retry delay — aborting`);+ log.info("COMBO", `Client disconnected — aborting combo loop before model ${modelStr}`);return errorResponse(499, "Client disconnected");}- }+ globalAttempts++;+ if (globalAttempts > MAX_GLOBAL_ATTEMPTS) {+ log.warn(+ "COMBO",+ `Maximum combo attempts (${MAX_GLOBAL_ATTEMPTS}) exceeded across all targets and fallbacks. Terminating loop to prevent runaway background requests.`+ );+ return errorResponse(503, "Maximum combo retry limit reached");+ }+ if (retry > 0) {+ log.info(+ "COMBO",+ `Retrying ${modelStr} in ${retryDelayMs}ms (attempt ${retry + 1}/${maxRetries + 1})`+ );+ await new Promise((resolve) => {+ const timer = setTimeout(resolve, retryDelayMs);+ signal?.addEventListener(+ "abort",+ () => {+ clearTimeout(timer);+ resolve(undefined);+ },+ { once: true }+ );+ });+ if (signal?.aborted) {+ log.info("COMBO", `Client disconnected during retry delay — aborting`);+ return errorResponse(499, "Client disconnected");+ }+ }- log.info(- "COMBO",- `Trying model ${i + 1}/${orderedTargets.length}: ${modelStr}${retry > 0 ? ` (retry ${retry})` : ""}`- );+ log.info(+ "COMBO",+ `Trying model ${i + 1}/${orderedTargets.length}: ${modelStr}${retry > 0 ? ` (retry ${retry})` : ""}`+ );- const result = await handleSingleModelWrapped(body, modelStr, target);+ const result = await handleSingleModelWrapped(body, modelStr, {+ ...target,+ failoverBeforeRetry: config.failoverBeforeRetry,+ });- // Success — validate response quality before returning- if (result.ok) {- const quality = await validateResponseQuality(result, clientRequestedStream, log);- if (!quality.valid) {- log.warn(+ // Success — validate response quality before returning+ if (result.ok) {+ const quality = await validateResponseQuality(result, clientRequestedStream, log);+ if (!quality.valid) {+ log.warn(+ "COMBO",+ `Model ${modelStr} returned 200 but failed quality check: ${quality.reason}`+ );+ recordComboRequest(combo.name, modelStr, {+ success: false,+ latencyMs: Date.now() - startTime,+ fallbackCount,+ strategy,+ target: toRecordedTarget(target),+ });+ recordedAttempts++;+ // Fix #1707: Set terminal state so the fallback doesn't emit+ // misleading ALL_ACCOUNTS_INACTIVE when the real issue is quality.+ lastError = `Upstream response failed quality validation: ${quality.reason}`;+ if (!lastStatus) lastStatus = 502;+ if (i > 0) fallbackCount++;+ break; // move to next model+ }+ const latencyMs = Date.now() - startTime;+ log.info("COMBO",- `Model ${modelStr} returned 200 but failed quality check: ${quality.reason}`+ `Model ${modelStr} succeeded (${latencyMs}ms, ${fallbackCount} fallbacks)`);recordComboRequest(combo.name, modelStr, {- success: false,- latencyMs: Date.now() - startTime,+ success: true,+ latencyMs,fallbackCount,strategy,target: toRecordedTarget(target),});recordedAttempts++;- // Fix #1707: Set terminal state so the fallback doesn't emit- // misleading ALL_ACCOUNTS_INACTIVE when the real issue is quality.- lastError = `Upstream response failed quality validation: ${quality.reason}`;- if (!lastStatus) lastStatus = 502;- if (i > 0) fallbackCount++;- break; // move to next model- }- const latencyMs = Date.now() - startTime;- log.info(- "COMBO",- `Model ${modelStr} succeeded (${latencyMs}ms, ${fallbackCount} fallbacks)`- );- recordComboRequest(combo.name, modelStr, {- success: true,- latencyMs,- fallbackCount,- strategy,- target: toRecordedTarget(target),- });- recordedAttempts++;- // Context-relay intentionally splits responsibilities:- // combo.ts decides whether a successful turn should generate a handoff,- // while chat.ts injects the handoff after the real connectionId is resolved.- if (- strategy === "context-relay" &&- relayOptions?.sessionId &&- relayConfig &&- relayConfig.handoffProviders.includes(provider) &&- provider === "codex"- ) {- const connectionId = getSessionConnection(relayOptions.sessionId);- if (connectionId) {- const quotaInfo = await fetchCodexQuota(connectionId).catch(() => null);- if (quotaInfo) {- const resetCandidates = [quotaInfo.window5h?.resetAt, quotaInfo.window7d?.resetAt]- .filter((value): value is string => typeof value === "string" && value.length > 0)- .sort((a, b) => a.localeCompare(b));- const handoffSourceMessages =- Array.isArray(body?.messages) && body.messages.length > 0- ? body.messages- : Array.isArray(body?.input)- ? body.input- : [];-- maybeGenerateHandoff({- sessionId: relayOptions.sessionId,- comboName: combo.name,- connectionId,- percentUsed: quotaInfo.percentUsed,- messages: handoffSourceMessages,- model: modelStr,- expiresAt: resetCandidates[0] || null,- config: relayConfig,- handleSingleModel: handleSingleModelWrapped,- });+ // Context-relay intentionally splits responsibilities:+ // combo.ts decides whether a successful turn should generate a handoff,+ // while chat.ts injects the handoff after the real connectionId is resolved.+ if (+ strategy === "context-relay" &&+ relayOptions?.sessionId &&+ relayConfig &&+ relayConfig.handoffProviders.includes(provider) &&+ provider === "codex"+ ) {+ const connectionId = getSessionConnection(relayOptions.sessionId);+ if (connectionId) {+ const quotaInfo = await fetchCodexQuota(connectionId).catch(() => null);+ if (quotaInfo) {+ const resetCandidates = [quotaInfo.window5h?.resetAt, quotaInfo.window7d?.resetAt]+ .filter((value): value is string => typeof value === "string" && value.length > 0)+ .sort((a, b) => a.localeCompare(b));+ const handoffSourceMessages =+ Array.isArray(body?.messages) && body.messages.length > 0+ ? body.messages+ : Array.isArray(body?.input)+ ? body.input+ : [];++ maybeGenerateHandoff({+ sessionId: relayOptions.sessionId,+ comboName: combo.name,+ connectionId,+ percentUsed: quotaInfo.percentUsed,+ messages: handoffSourceMessages,+ model: modelStr,+ expiresAt: resetCandidates[0] || null,+ config: relayConfig,+ handleSingleModel: handleSingleModelWrapped,+ });+ }}}++ // Record last known good provider (LKGP) for this combo/model (#919)+ if (provider) {+ const connId = target.connectionId || undefined;+ void (async () => {+ try {+ const { setLKGP } = await import("../../src/lib/localDb");+ await Promise.all([+ setLKGP(combo.name, target.executionKey, provider, connId),+ setLKGP(combo.name, combo.id || combo.name, provider, connId),+ ]);+ } catch (err) {+ log.warn("COMBO", "Failed to record Last Known Good Provider. This is non-fatal.", {+ err,+ });+ }+ })();+ }++ return quality.clonedResponse ?? result;}- // Record last known good provider (LKGP) for this combo/model (#919)- if (provider) {- const connId = target.connectionId || undefined;- void (async () => {- try {- const { setLKGP } = await import("../../src/lib/localDb");- await Promise.all([- setLKGP(combo.name, target.executionKey, provider, connId),- setLKGP(combo.name, combo.id || combo.name, provider, connId),- ]);- } catch (err) {- log.warn("COMBO", "Failed to record Last Known Good Provider. This is non-fatal.", {- err,- });+ // Extract error info from response+ let errorText = result.statusText || "";+ let errorBody = null;+ let retryAfter = null;+ try {+ const cloned = result.clone();+ try {+ const text = await cloned.text();+ if (text) {+ errorText = text.substring(0, 500);+ errorBody = JSON.parse(text);+ errorText =+ errorBody?.error?.message || errorBody?.error || errorBody?.message || errorText;+ retryAfter = errorBody?.retryAfter || null;}- })();+ } catch {+ /* Clone parse failed */+ }+ } catch {+ /* Clone failed */}- return quality.clonedResponse ?? result;- }+ // Track earliest retryAfter+ if (+ retryAfter &&+ (!earliestRetryAfter || new Date(retryAfter) < new Date(earliestRetryAfter))+ ) {+ earliestRetryAfter = retryAfter;+ }- // Extract error info from response- let errorText = result.statusText || "";- let errorBody = null;- let retryAfter = null;- try {- const cloned = result.clone();- try {- const text = await cloned.text();- if (text) {- errorText = text.substring(0, 500);- errorBody = JSON.parse(text);- errorText =- errorBody?.error?.message || errorBody?.error || errorBody?.message || errorText;- retryAfter = errorBody?.retryAfter || null;+ // Normalize error text+ if (typeof errorText !== "string") {+ try {+ errorText = JSON.stringify(errorText);+ } catch {+ errorText = String(errorText);}- } catch {- /* Clone parse failed */}- } catch {- /* Clone failed */- }- // Track earliest retryAfter- if (- retryAfter &&- (!earliestRetryAfter || new Date(retryAfter) < new Date(earliestRetryAfter))- ) {- earliestRetryAfter = retryAfter;- }+ const isStreamReadinessFailure =+ (result.status === 502 || result.status === 504) &&+ isStreamReadinessFailureErrorBody(errorBody);- // Normalize error text- if (typeof errorText !== "string") {- try {- errorText = JSON.stringify(errorText);- } catch {- errorText = String(errorText);+ // Fix #1681: Status 499 means client disconnected — stop combo loop immediately.+ // There is no point trying fallback models when nobody is listening.+ if (result.status === 499) {+ log.info("COMBO", `Client disconnected (499) during ${modelStr} — stopping combo loop`);+ recordComboRequest(combo.name, modelStr, {+ success: false,+ latencyMs: Date.now() - startTime,+ fallbackCount,+ strategy,+ target: toRecordedTarget(target),+ });+ recordedAttempts++;+ return result;}- }- const isStreamReadinessFailure =- (result.status === 502 || result.status === 504) &&- isStreamReadinessFailureErrorBody(errorBody);+ // Combo fallback is target-level orchestration: a non-ok target response is+ // treated as local to that target and the combo continues to the next target.+ // Error classification is retained only for retry/cooldown pacing; it must+ // not decide whether fallback happens, including for generic 400 responses.- const { cooldownMs } = checkFallbackError(++ const fallbackResult = checkFallbackError(+ result.status,+ errorText,+ 0,+ null,+ provider,+ result.headers,+ profile+ );++ const { cooldownMs } = fallbackResult;+- // Fix #1681: Status 499 means client disconnected — stop combo loop immediately.- // There is no point trying fallback models when nobody is listening.- if (result.status === 499) {- log.info("COMBO", `Client disconnected (499) during ${modelStr} — stopping combo loop`);++ // #1731: If the entire provider quota is exhausted, mark it so subsequent++ // same-provider targets are skipped immediately.++ if (provider && isProviderExhaustedReason(fallbackResult)) {++ exhaustedProviders.add(provider);++ log.info(++ "COMBO",++ `Provider ${provider} quota exhausted — marking for skip on remaining targets (#1731)`++ );++ }++ // 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.+ const nextTarget = orderedTargets[i + 1];+ const sameProviderNext =+ typeof nextTarget?.provider === "string" && nextTarget.provider === provider;+ if (+ !isStreamReadinessFailure &&+ isProviderFailureCode(result.status) &&+ !sameProviderNext+ ) {+ recordProviderFailure(provider, log, target.connectionId, profile);+ }++ // Check if this is a transient error worth retrying on same model+ const isTransient =+ !isStreamReadinessFailure && [408, 429, 500, 502, 503, 504].includes(result.status);+ if (retry < maxRetries && isTransient) {+ continue; // Retry same model+ }++ // Done retrying this modelrecordComboRequest(combo.name, modelStr, {success: false,latencyMs: Date.now() - startTime,