Skip to content

fix(resilience): scope state per client and honor breaker and retry policy #111

Open

Description

@stackbilt-admin

Priority

P1 · impact H · effort M · risk M (default isolation belongs in major; offer opt-in now).

Problem and evidence

Independent clients with different provider credentials share health, exhaustion and analytics by the provider name:

Verified: force-open the default OpenAI breaker; a separately constructed client with enableCircuitBreaker: false still fails without dispatching. This is not a lost JavaScript increment: synchronous mutations are safe within an isolate. The defects are state ownership, non-atomic admission across awaits, and completion-order semantics.

Proposed scope

Add an injectable ResilienceContext holding CircuitBreakerManager, ExhaustionRegistry, CostTracker and LatencyHistogram. Pass the same context to planning, adapters and telemetry; direct providers must support the same injection. Account/credential scopes should use consumer-supplied non-secret IDs, never log keys.

Add opt-in client-local context in a minor; make isolated context the next-major default, with explicit shared context for intentional sharing. Make breaker disable bypass execution as well as route precheck; document retry-option precedence and test it. Decide whether invalid/auth/content-policy errors count against health; default to infrastructure failures.

Fix breaker time decay so rejected traffic cannot postpone recovery indefinitely. Preserve graduated probabilistic recovery; do not call it a single-probe half-open lock that the implementation does not provide.

Fallback truth:

Document a precise error-policy table and safe defaults. Treat a policy change in content-filter/auth fallback as explicit configuration or a major, not a silent patch.

Acceptance

  • Two clients/credentials never share failures, exhaustion, metrics or reset effects unless given shared context.
  • Disabling breakers/retries has deterministic tested semantics.
  • Rejected direct calls do not continually postpone recovery.
  • User/input/policy errors do not degrade unrelated provider service health.
  • Out-of-order concurrent success/failure behavior is specified and tested.
  • Unsupported latency policy rejects configuration or is implemented; cost-trigger semantics are honest.
  • README error classification matches implementation.

Related: closed #32 fixed one retry path; this covers remaining precedence and state scope. Atomic distributed budget admission is a separate quota issue.

Activity

  1. added
    bug
    on Oct 11, 2026
  2. stackbilt-admin commented on Oct 13, 2026

    @stackbilt-admin
    MemberAuthor

    Local implementation checkpoint — 2026-10-12

    This work is uncommitted/unpushed on main, based on 8a9eda6c058d63f0ec08332f6a408aaf409232cf, and is not merged or released. Keep this issue open pending review/integration. The local handoff is docs/maintenance/issue-sweep.md; it will travel with the code.

    Compatible state/policy phase implemented:

    • stateScope: 'client' creates independent breaker, exhaustion, factory cost and latency state; an explicit ResilienceContext intentionally shares state. Factories and direct adapters use the injected context for health. fromEnv accepts the options.
    • Global state remains the compatibility default. Resetting an isolated client's state does not affect another client; shared contexts intentionally share reset/exhaustion effects.
    • enableCircuitBreaker: false now bypasses adapter admission and health recording, including streams.
    • Rejected traffic no longer postpones decay indefinitely. Caller/input/auth/configuration/local-policy/account-quota failures are neutral for provider service health.
    • Streaming execution permits settle once; concurrent completion-order success/failure behavior is tested.

    Evidence: resilience-context.ts, base/factory/breaker changes and 12 resilience-context.test.ts cases. Existing schema-drift tests now reset breaker history because streaming failures genuinely record health outcomes.

    Remaining/compatibility decisions: isolated defaults need the next major; legacy retry precedence is preserved (explicit provider maxRetries wins over enableRetries: false, so set each provider to zero for guaranteed disablement); unsupported latency rules/cost-trigger semantics remain unchanged. README now documents these limits and stops promising streaming authentication fallback.

    Validation for the combined local sweep: 605 tests pass across 27 files, TypeScript typecheck passes, Worker-targeted bundle/import smoke passes. Packed entrypoint exports/client methods and all 33 JavaScript sourcemaps were checked on Windows with Node 24.19.0/npm 11.17.0. No live/billable providers or workerd execution; this does not prove minimum Node-version compatibility.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions