You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
discussion: Circuit breaker, exhaustion registry, and CreditLedger state is in-memory — resets per Cloudflare Worker invocation #65
In Cloudflare Workers, each invocation runs in an isolated context. The global singleton instances (defaultCircuitBreakerManager, defaultExhaustionRegistry) are re-created on every request. This means:
A provider that was OPEN after 5 consecutive failures appears CLOSED on the next request
QuotaExceededError marks a provider exhausted for 5 minutes, but that mark is lost instantly
Monthly budget spend in CreditLedger resets to zero each invocation
The package is described as "Cloudflare Workers native" — this is a fundamental gap for that use case.
What's already there
CreditLedger has snapshot() and restore() methods (src/utils/credit-ledger.ts:523–580) for persistence — but no equivalent exists on CircuitBreaker or ExhaustionRegistry, and none of the three are wired up automatically.
Proposed direction
Add serialize() / deserialize() to CircuitBreaker and ExhaustionRegistry — lightweight JSON snapshots that callers can persist to KV or cache
Document the CF Workers limitation in the README under Circuit Breaker and Cost Tracking sections — currently the package presents as fully Workers-native without noting this constraint
Optional: provide a Durable Objects integration as a separate export or companion package — a DO that owns the circuit breaker state and is shared across invocations
Acceptance criteria for a minimal fix
CircuitBreaker exposes serialize(): string and static deserialize(json: string): CircuitBreaker
ExhaustionRegistry exposes equivalent snapshot/restore API
README documents that state does not persist across Workers invocations and how to wire up persistence
Example snippet showing KV-backed persistence in a Workers handler
Found by
Codebase audit (automated) — independently flagged by circuit-breaker and cost-tracking audit slices
60ec143 feat: persist circuit breaker manager state
Acceptance coverage:
CircuitBreaker exposes serialize(), restore(), and static deserialize().
CircuitBreakerManager now exposes snapshot(), serialize(), restore(), and static deserialize() so Workers users can persist all provider breaker state instead of one provider at a time.
ExhaustionRegistry exposes snapshot(), serialize(), restore(), and static deserialize().
CreditLedger already had snapshot()/restore(); README shows it alongside breaker and exhaustion persistence.
README documents that default reliability/budget state is in-memory in Cloudflare Workers and shows KV-backed restore/save in a Workers handler.
Validation:
npm test -- src/tests/circuit-breaker.test.ts src/tests/observability.test.ts
npm run typecheck
npm test (19 files, 421 tests)
npm run build
npm run test:package
No release/tag yet; this closes only the tracker item.
Activity
stackbilt-admin commentedon May 31, 2026
Resolved on main.
Commits:
Acceptance coverage:
Validation:
No release/tag yet; this closes only the tracker item.