Skip to content

fix: honor fallbackProvider and fallbackModel in fallback rules #34

Closed

Description

@stackbilt-admin

Summary

FallbackRule.fallbackProvider is part of the public API and README examples, but the factory currently uses fallback rules only as a boolean "should continue?" decision. The configured fallbackProvider never affects the provider chain.

That means a user can configure { condition: 'error', fallbackProvider: 'anthropic' } and still route to whatever provider happens to be next in the default/cost-optimized chain.

Fractal Decomposition

Macro: Predictable failover policy

  • Consumers use this package to centralize provider failover decisions across products.
  • A fallback rule that reads as provider-directed policy but behaves as generic "try next" policy creates operational surprises during outages, rate limits, or cost controls.

Meso: Provider-chain construction vs fallback evaluation

  • Provider ordering is built up front by buildProviderChain().
  • shouldFallback() only decides whether the loop continues.
  • evaluateFallbackRule() reads condition and threshold, but never returns or uses fallbackProvider / fallbackModel.

Micro: Concrete code paths

  • src/types.ts:204-208: FallbackRule includes fallbackProvider and optional fallbackModel.
  • src/factory.ts:370-400: shouldFallback() returns only a boolean.
  • src/factory.ts:406-428: evaluateFallbackRule() ignores the provider/model specified by the rule.
  • src/factory.ts:434-449: defaults document specific fallback providers that are not actually selected by this logic.
  • README.md:163-176: examples imply callers can customize where fallback goes.

Impact

  • Custom fallback rules do not provide deterministic routing.
  • Cost-based fallback can be triggered only after a failure path and still does not guarantee routing to the configured cheap provider.
  • fallbackModel appears to be a public contract but has no effect.
  • Tests currently exercise "fallback happened", not "the rule's fallbackProvider was honored".

Suggested Fix

  • Change fallback evaluation to return a routing decision, e.g. { shouldFallback, provider?, model? }.
  • When a rule matches, insert or prioritize the specified fallback provider/model as the next attempted target.
  • Define precedence when multiple rules match.
  • Add tests for custom rules such as openai failure -> configured anthropic first, not cost/default priority first.
  • Either implement or remove the unimplemented latency condition so the type does not promise an inert policy.

Verification

  • npm run typecheck passes.
  • npm test passes: 169 tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions