Skip to content

fix(fallback): use shared CircuitBreaker instead of undefined constants - #1485

Merged
diegosouzapw merged 2 commits into
cheaper-inference:release/v3.7.0from
clousky2020:provider
Apr 21, 2026
Merged

diegosouzapw merged 2 commits into
cheaper-inference:release/v3.7.0from
clousky2020:provider

Conversation

@clousky2020

Copy link
Copy Markdown
Contributor

Summary

Fixes a runtime ReferenceError in recordProviderFailure() where three constants (PROVIDER_FAILURE_WINDOW_MS, PROVIDER_FAILURE_THRESHOLD, PROVIDER_COOLDOWN_MS) were being used but never defined.

The fix removes the redundant custom provider failure tracking implementation (64 lines) and delegates to the existing shared CircuitBreaker utility which already handles failure counting, threshold detection, and state transitions.

Also fixes: Indefinite lockout bug - skip recording failures when already in cooldown to prevent resetting the cooldown timer.

Related Issues

  • Fixes regression introduced in v3.7.0 resilience feature

Validation

  • Code compiles without errors
  • Logic reviewed for edge cases

Tests Added Or Updated

  • No new tests needed - fix uses existing tested CircuitBreaker infrastructure
  • Existing account-fallback-service.test.ts covers the affected functions

Coverage Notes

  • Change reduces code complexity by removing redundant implementation
  • Delegates to existing well-tested getCircuitBreaker() and _onFailure() patterns

Reviewer Notes

  • This fix aligns with the existing pattern used in src/sse/handlers/chat.ts:628,856
  • No breaking changes - function signatures remain unchanged
  • Added guard clause to prevent indefinite lockout when breaker is already OPEN

clousky2020 and others added 2 commits April 21, 2026 22:47
recordProviderFailure was referencing PROVIDER_FAILURE_WINDOW_MS,
PROVIDER_FAILURE_THRESHOLD, and PROVIDER_COOLDOWN_MS which don't exist,
causing runtime ReferenceError. Replace with direct delegation to the
existing getCircuitBreaker utility which already handles failure counting,
threshold detection, and state transitions. Remove redundant
ProviderFailureEntry type, providerFailureState Map, and
providerFailureLocks Set.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request refactors the provider-level circuit breaker logic in open-sse/services/accountFallback.ts by replacing manual state management and re-entrancy locks with a shared CircuitBreaker utility. While this simplifies the code, the review identifies a regression where the removal of the failure window causes errors to accumulate indefinitely without a reset mechanism. Additionally, the implementation incorrectly uses connection-level configuration thresholds instead of the specific provider-level settings, leading to shorter cooldown periods than intended.

});
return;
}
breaker._onFailure();

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

The migration to the shared CircuitBreaker utility introduces a regression in failure tracking logic. The original implementation included a 'failure window' (resetAfterMs) that reset the failure count if errors were sufficiently spread out (lines 552-562 in the previous version). The CircuitBreaker utility lacks this sliding window and only resets failureCount upon a successful execution or manual reset. Since this service does not appear to record successes for the provider-level breaker (no call to _onSuccess or clearProviderFailure on successful requests), failures will accumulate indefinitely across the process lifetime, eventually tripping the breaker even for rare, non-consecutive errors.

try {
const now = Date.now();
const entry = providerFailureState.get(provider);
const breaker = getProviderBreaker(provider);

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

The getProviderBreaker helper (defined at line 488) is configured using profile.failureThreshold and profile.resetTimeoutMs, which are connection-level resilience settings. For provider-level circuit breaking, it should use profile.providerFailureThreshold and profile.providerCooldownMs. This discrepancy causes the provider-level breaker to use incorrect thresholds and a significantly shorter cooldown than intended (e.g., 1 minute instead of the 5 minutes defined in PROVIDER_PROFILES for OAuth providers).

@diegosouzapw
diegosouzapw merged commit 3502c5a into cheaper-inference:release/v3.7.0 Apr 21, 2026
2 checks passed
@diegosouzapw diegosouzapw mentioned this pull request Apr 22, 2026
Poid-ZA pushed a commit to Poid-ZA/OmniRoute that referenced this pull request Aug 5, 2026
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants