mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-05 23:02:10 +03:00
* feat(combo): universal cooldown-aware retry & auto-strategy combo-ref guard
Two changes:
1. Universal cooldown-aware retry (combo.ts):
- Remove strategy==="quota-share" gate from comboCooldownWaitEnabled
- Enables all 18 combo strategies (priority, weighted, round-robin, etc.)
to wait out a short transient cooldown and retry the full set
- Non-quota-share strategies use shouldWaitForComboCooldown directly
with earliestRetryAfter and reason="rate_limit" (no per-model lockout)
- quota-share retains its existing per-target model lockout logic
2. Auto-strategy combo-ref guard (autoStrategy.ts):
- expandAutoComboCandidatePool now detects kind==="combo-ref" entries
- When present, returns eligibleTargets without expanding to ALL providers
- Fixes scenario where an "auto" combo with combo-ref delegates to a
sub-combo but pulls in every model from every active provider
* feat(combo): global comboTimeoutMs + aggregated error diagnostics
Adds two features to improve combo resilience and debuggability:
1. Global combo timeout (comboTimeoutMs)
- Configurable per-combo via DEFAULT_COMBO_CONFIG (default 0 = disabled)
- After each target completes, checks if total elapsed time exceeds limit
- When exceeded, stops trying further targets and returns 504 immediately
- Backward-compatible: 0 preserves legacy unlimited-iteration behavior
2. Aggregated error diagnostics
- comboErrors array accumulates per-model failure details (model, status, error)
- On combo timeout or all-models-exhausted, returns a message listing the
first (up to 5) model-level errors with their HTTP status codes
- Enables operators to see WHICH models failed and WHY without digging
through individual server logs
* test(combo): cover universal cooldown-aware retry, comboTimeoutMs, and combo-ref guard
Adds/updates automated coverage for this PR's production changes (PR Test
Policy requires tests alongside src/open-sse/electron/bin changes):
- Update the "preserves the first failure status" expectation in
combo-routing-engine.test.ts: the aggregated per-model error-diagnostics
suffix is new intended output, not a regression.
- Add two new tests for the global comboTimeoutMs feature: the combo stops
dispatching further targets and returns 504/COMBO_TIMEOUT with aggregated
diagnostics once the ceiling trips, and comboTimeoutMs=0 (default) never
trips it.
- Rewrite the non-quota-share (priority) cooldown-wait scenario in
combo-quota-share-cooldown-wait-timing.test.ts: comboCooldownWait is no
longer gated on strategy === "quota-share", so a priority combo now waits
out a short 429 and re-dispatches too (via shouldWaitForComboCooldown with
reason "rate_limit"), instead of propagating immediately as before. Also
adds the disabled-flag counterpart for parity with the quota-share suite.
- Add coverage for the #COMBO-REF guard in expandAutoComboCandidatePool:
a combo whose models array contains a kind:"combo-ref" entry must not be
expanded to every model of every active provider.
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
* test(combo): keep the new comboTimeoutMs tests free of no-explicit-any
The two new tests initially copied the neighbouring tests' `any`-typed
handleSingleModel params / json() casts. Those neighbours are pre-existing
violations frozen in config/quality/eslint-suppressions.json at a count of 261
for this file, so the 7 new occurrences pushed it to 268 and broke `npm run
lint` (no-explicit-any is an error in tests/ since #6218; new violations must
be fixed, not re-frozen).
Type the new tests properly instead: `unknown`/`string` params, a
ComboErrorPayload interface for the parsed body, and drop the unused
`relayOptions: null as any` (the sibling combo cooldown suites already omit
it). Back to exactly 261 — the suppression file is untouched.
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
* fix(combo): gate the universal cooldown retry on the REAL lock reason, not a hardcoded "rate_limit"
The universal cooldown-aware retry kept the quota-share path on
resolveComboCooldownWaitDecision but gave every OTHER strategy a shortcut that
hardcoded `reason: "rate_limit"` and fed shouldWaitForComboCooldown the
earliestRetryAfter directly.
comboCooldownRetry.ts documents TWO deliberate barriers ("SECURITY —
quota_exhausted must be excluded"): (1) the reason allow-list, and (2) the
maxWaitMs ceiling, explicitly called the SECOND barrier. Hardcoding the reason
removed barrier 1 for 17 of the 18 strategies and left only the ceiling — which
does NOT cover a quota_exhausted lock whose wait lands under maxWaitMs. In that
case the combo waits, redispatches against a model that is locked until the
quota resets, and burns the retry budget for nothing.
The shortcut's premise ("non-quota-share combos have no per-connection model
lockout tracking") is also false: recordModelLockoutFailure in the target loop
is not gated on quota-share, so every strategy records model lockouts and the
real reason is always available.
Fix: one decision path for every strategy, always through
resolveComboCooldownWaitDecision, so the reason always crosses the allow-list.
The lock lookup is now keyed on each TARGET's own model (via a new third
`target` arg on lookupLock) — quota-share combos are single-model/multi-account
so this is identical to the previous orderedTargets[0] behavior, but
heterogeneous combos (priority, weighted, round-robin, …) carry a different
model per target and would otherwise miss every lock but the first.
Regression guard (tests/unit/serial/combo-quota-share-cooldown-wait-timing.test.ts):
a priority combo where modelLockout.errorCodes=[403] leaves the 403's
quota_exhausted lock as the only one in play while a 429 crystallizes the
status, and the resulting wait is short enough that the ceiling lets it through
— so only the allow-list can stop it. Verified failing-then-passing: with the
hardcoded reason the combo makes 6 dispatches (wait+redispatch x2); with the
real reason it makes 2 and propagates the 429.
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
* chore(quality): rebaseline file-size for PR #7301 own growth (combo +91, combo-routing-engine test +68)
---------
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
259 lines
9.8 KiB
TypeScript
259 lines
9.8 KiB
TypeScript
/**
|
|
* comboCooldownRetry.ts — pure decision helper for the quota-share combo
|
|
* cooldown-aware retry (Variante A).
|
|
*
|
|
* Problem it solves
|
|
* -----------------
|
|
* In a single-connection quota-share combo (`qtSd/…`), when the upstream
|
|
* returns a SHORT transient 429 (reset in a few seconds), the combo loop
|
|
* treats the only target as locked-by-resilience, skips it, and crystallizes a
|
|
* 429 `model_cooldown` to the client immediately — even though waiting a couple
|
|
* of seconds would let the same connection succeed. This helper decides whether
|
|
* `handleComboChat` should WAIT for that short cooldown and re-dispatch instead
|
|
* of propagating the 429.
|
|
*
|
|
* Why a pure helper
|
|
* -----------------
|
|
* The actual wait (`waitForCooldownAwareRetry`) and the lock lookup
|
|
* (`getModelLockoutInfo`) live in `handleComboChat`; only the *gating policy*
|
|
* lives here. Keeping the policy pure (no I/O, no clock) means every branch is
|
|
* unit-testable deterministically and the orchestration in combo.ts stays thin.
|
|
*
|
|
* SECURITY — `quota_exhausted` must be excluded
|
|
* ---------------------------------------------
|
|
* `isRetryableModelLockoutReason` (src/sse/services/auth.ts:533) considers
|
|
* `quota_exhausted` RETRYABLE — its NON_RETRYABLE set only holds
|
|
* `not_found`/`not_found_local`. But `recordModelLockoutFailure`
|
|
* (accountFallback.ts) locks a quota-exhausted model UNTIL MIDNIGHT. If this
|
|
* helper trusted that classifier, a combo would wait until midnight. So this
|
|
* helper uses its OWN allow-list semantics: a reason qualifies for a wait only
|
|
* when it is a known short/transient reason AND is not in the explicit
|
|
* non-retryable set below. The small `maxWaitMs` ceiling is the second barrier.
|
|
*/
|
|
|
|
/**
|
|
* Reasons that must NEVER trigger a wait, regardless of the upstream-provided
|
|
* retry hint. `quota_exhausted` is the critical one (locked until midnight);
|
|
* the auth/not-found reasons can never be cured by waiting a few seconds.
|
|
*/
|
|
export const COMBO_COOLDOWN_NON_RETRYABLE_REASONS: ReadonlySet<string> = new Set([
|
|
"quota_exhausted",
|
|
"auth_error",
|
|
"not_found",
|
|
"not_found_local",
|
|
]);
|
|
|
|
/**
|
|
* Reasons recognised as short/transient and therefore eligible for a wait. We
|
|
* use an allow-list (rather than "anything not in the deny-list") so an unknown
|
|
* or empty reason fails closed — only an explicit transient reason qualifies.
|
|
* These match the model-lockout reasons produced by classifyLockoutReason /
|
|
* recordModelLockoutFailure for rate-limit-class failures.
|
|
*/
|
|
export const COMBO_COOLDOWN_RETRYABLE_REASONS: ReadonlySet<string> = new Set([
|
|
"rate_limit",
|
|
"rate_limited",
|
|
"transient",
|
|
"overloaded",
|
|
"server_error",
|
|
]);
|
|
|
|
export interface ComboCooldownWaitSettings {
|
|
/** Master switch — when false the helper always returns wait=false. */
|
|
enabled: boolean;
|
|
/** Hard ceiling (ms) on a single wait. Inclusive upper bound. */
|
|
maxWaitMs: number;
|
|
/** Maximum number of wait+redispatch attempts for one request. */
|
|
maxAttempts: number;
|
|
/** Total wait budget (ms) for the request — combo.ts decrements it. */
|
|
budgetMs: number;
|
|
}
|
|
|
|
export interface ShouldWaitForComboCooldownInput {
|
|
/** Lock reason from getModelLockoutInfo (may be null/unknown). */
|
|
reason: unknown;
|
|
/** Computed wait derived from the upstream retry-after hint (ms). */
|
|
waitMs: unknown;
|
|
/** Zero-based count of waits already performed for this request. */
|
|
attempt: number;
|
|
/** Remaining wait budget (ms) for this request. */
|
|
budgetLeftMs: number;
|
|
settings: ComboCooldownWaitSettings;
|
|
}
|
|
|
|
export interface ShouldWaitForComboCooldownResult {
|
|
wait: boolean;
|
|
/**
|
|
* The wait duration the caller should pass to waitForCooldownAwareRetry. It
|
|
* is surfaced even when `wait` is false (clamped to a finite >= 0 value) so
|
|
* the caller can log "would have waited Xms" without re-deriving it.
|
|
*/
|
|
waitMs: number;
|
|
}
|
|
|
|
function toFiniteWaitMs(value: unknown): number {
|
|
if (typeof value === "number" && Number.isFinite(value) && value > 0) {
|
|
return value;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
function isRetryableComboCooldownReason(reason: unknown): boolean {
|
|
if (typeof reason !== "string" || reason.length === 0) return false;
|
|
if (COMBO_COOLDOWN_NON_RETRYABLE_REASONS.has(reason)) return false;
|
|
return COMBO_COOLDOWN_RETRYABLE_REASONS.has(reason);
|
|
}
|
|
|
|
/**
|
|
* Decide whether a quota-share combo should wait for a short cooldown and
|
|
* re-dispatch. Pure — no I/O, no clock. All inputs are explicit.
|
|
*
|
|
* Returns wait=true only when ALL hold:
|
|
* - settings.enabled
|
|
* - reason is a recognised transient reason AND not in the non-retryable set
|
|
* (quota_exhausted / auth_error / not_found / not_found_local)
|
|
* - waitMs is finite and 0 < waitMs <= settings.maxWaitMs
|
|
* - attempt < settings.maxAttempts
|
|
* - budgetLeftMs >= waitMs
|
|
*/
|
|
export function shouldWaitForComboCooldown(
|
|
input: ShouldWaitForComboCooldownInput
|
|
): ShouldWaitForComboCooldownResult {
|
|
const { reason, attempt, budgetLeftMs, settings } = input;
|
|
const waitMs = toFiniteWaitMs(input.waitMs);
|
|
|
|
const wait =
|
|
settings.enabled === true &&
|
|
isRetryableComboCooldownReason(reason) &&
|
|
waitMs > 0 &&
|
|
waitMs <= settings.maxWaitMs &&
|
|
attempt < settings.maxAttempts &&
|
|
budgetLeftMs >= waitMs;
|
|
|
|
return { wait, waitMs };
|
|
}
|
|
|
|
/** Minimal lock shape resolved from getModelLockoutInfo. */
|
|
export interface ComboCooldownLockInfo {
|
|
reason: unknown;
|
|
remainingMs: number;
|
|
}
|
|
|
|
/** Minimal combo-target shape this helper inspects. */
|
|
export interface ComboCooldownTarget {
|
|
provider?: string | null;
|
|
connectionId?: string | null;
|
|
/**
|
|
* The target's own model. Heterogeneous combos (priority, weighted,
|
|
* round-robin, …) hold a DIFFERENT model per target, so the lock lookup must
|
|
* be keyed on each target's own model — keying every lookup on the first
|
|
* target's model would miss every other target's lock and silently degrade
|
|
* the reason allow-list to "no lock found".
|
|
*/
|
|
modelStr?: string | null;
|
|
}
|
|
|
|
export interface ResolveComboCooldownDecisionInput {
|
|
/** Combo targets to inspect for an active short lock. */
|
|
targets: ReadonlyArray<ComboCooldownTarget>;
|
|
/** Earliest retry-after the loop crystallized (string | number | Date | null). */
|
|
earliestRetryAfter: unknown;
|
|
attempt: number;
|
|
budgetLeftMs: number;
|
|
settings: ComboCooldownWaitSettings;
|
|
/**
|
|
* Per-target lock lookup (getModelLockoutInfo). Receives the target itself so
|
|
* the caller can key the lookup on that target's own model.
|
|
*/
|
|
lookupLock: (
|
|
provider: string,
|
|
connectionId: string,
|
|
target: ComboCooldownTarget
|
|
) => ComboCooldownLockInfo | null;
|
|
/** Derives the wait (ms) from the retry-after hint (computeClosestRetryAfter). */
|
|
computeWaitMs: (retryAfter: unknown) => number | null;
|
|
}
|
|
|
|
export interface ResolveComboCooldownDecisionResult extends ShouldWaitForComboCooldownResult {
|
|
/** Reason that drove the decision (for logging); null when none resolved. */
|
|
reason: string | null;
|
|
}
|
|
|
|
/**
|
|
* Small safety margin (ms) added on top of the wait so the model lock is
|
|
* reliably EXPIRED when the loop re-checks `isModelLocked` after the wait (real
|
|
* clocks drift a few ms between locking and re-evaluation; under-waiting would
|
|
* make the 2nd pass skip the target again and crystallize a 503/429).
|
|
*/
|
|
export const COMBO_COOLDOWN_WAIT_MARGIN_MS = 50;
|
|
|
|
/**
|
|
* Resolve, from the combo targets and the crystallized retry-after, whether the
|
|
* quota-share combo should wait and re-dispatch. Picks the locked target with
|
|
* the SMALLEST positive remaining cooldown (the soonest to recover) and uses its
|
|
* reason. The wait must last long enough to actually clear that lock, so it is
|
|
* `max(lock remainingMs, upstream retry-after hint) + margin` — the lock's own
|
|
* remaining time is authoritative (it already folds in a longer upstream hint
|
|
* via selectLockoutCooldownMs), and we honor an even longer hint if present. The
|
|
* ceiling check in shouldWaitForComboCooldown then gates on that real duration,
|
|
* so only genuinely SHORT cooldowns are waited. Pure given the injected
|
|
* `lookupLock`/`computeWaitMs` — no I/O, no clock — so it is unit-testable.
|
|
*/
|
|
export function resolveComboCooldownWaitDecision(
|
|
input: ResolveComboCooldownDecisionInput
|
|
): ResolveComboCooldownDecisionResult {
|
|
const {
|
|
targets,
|
|
earliestRetryAfter,
|
|
attempt,
|
|
budgetLeftMs,
|
|
settings,
|
|
lookupLock,
|
|
computeWaitMs,
|
|
} = input;
|
|
|
|
// Short-circuit before any lookup when the feature is off.
|
|
if (!settings.enabled) return { wait: false, waitMs: 0, reason: null };
|
|
|
|
let best: { reason: unknown; remainingMs: number } | null = null;
|
|
for (const target of targets) {
|
|
const provider = typeof target.provider === "string" ? target.provider : "";
|
|
if (!provider) continue;
|
|
const connectionId = typeof target.connectionId === "string" ? target.connectionId : "";
|
|
const info = lookupLock(provider, connectionId, target);
|
|
if (!info) continue;
|
|
const remainingMs =
|
|
typeof info.remainingMs === "number" && Number.isFinite(info.remainingMs)
|
|
? info.remainingMs
|
|
: 0;
|
|
if (remainingMs <= 0) continue;
|
|
if (!best || remainingMs < best.remainingMs) {
|
|
best = { reason: info.reason, remainingMs };
|
|
}
|
|
}
|
|
|
|
if (!best) return { wait: false, waitMs: 0, reason: null };
|
|
|
|
// Wait long enough to actually clear the lock: the larger of the lock's own
|
|
// remaining time and the upstream retry-after hint, plus a small margin.
|
|
const rawHintWaitMs = computeWaitMs(earliestRetryAfter);
|
|
const hintWaitMs =
|
|
typeof rawHintWaitMs === "number" && Number.isFinite(rawHintWaitMs) && rawHintWaitMs > 0
|
|
? rawHintWaitMs
|
|
: 0;
|
|
const waitMs = Math.max(best.remainingMs, hintWaitMs) + COMBO_COOLDOWN_WAIT_MARGIN_MS;
|
|
|
|
const decision = shouldWaitForComboCooldown({
|
|
reason: best.reason,
|
|
waitMs,
|
|
attempt,
|
|
budgetLeftMs,
|
|
settings,
|
|
});
|
|
|
|
return {
|
|
...decision,
|
|
reason: typeof best.reason === "string" ? best.reason : null,
|
|
};
|
|
}
|