/** * Circuit Breaker — fail-fast wrapper for adapter execution. * * Implements the classic three-state machine: * CLOSED → normal pass-through; failure counter increments on every throw * OPEN → fast-fail with `CircuitOpenError`; no downstream calls made * HALF_OPEN → probe phase after `recoveryTimeoutMs`; a single success closes * the circuit, a single failure re-opens it * * Usage: * ```typescript * const breaker = new CircuitBreaker('my-adapter', { failureThreshold: 5 }); * const result = await breaker.execute(() => adapter.executeAgent(...)); * ``` */ /** States a circuit breaker can be in. */ export type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN'; /** Configuration for `CircuitBreaker`. */ export interface CircuitBreakerConfig { /** * Number of consecutive failures that trips the circuit to OPEN. * @default 3 */ failureThreshold?: number; /** * Milliseconds to wait in OPEN state before transitioning to HALF_OPEN. * @default 30_000 */ recoveryTimeoutMs?: number; /** * Consecutive successes in HALF_OPEN needed to close the circuit. * @default 1 */ successThreshold?: number; /** * Called whenever the circuit changes state. * Must not throw — exceptions are silently swallowed. */ onStateChange?: (from: CircuitState, to: CircuitState, adapterName: string) => void; } /** * Thrown by `CircuitBreaker.execute()` when the circuit is OPEN. * Callers can catch this to implement fallback logic without masking real errors. */ export declare class CircuitOpenError extends Error { readonly adapterName: string; constructor(adapterName: string); } /** * Generic circuit breaker. Thread-safe within a single Node.js event loop. * * @typeParam — no explicit type param; use `execute()` per call. */ export declare class CircuitBreaker { readonly adapterName: string; private state; private failures; private successes; private openedAt; private readonly failureThreshold; private readonly recoveryTimeoutMs; private readonly successThreshold; private readonly onStateChangeCb?; /** * @param adapterName Identifies this breaker in events and error messages. * @param config Optional tuning parameters. */ constructor(adapterName: string, config?: CircuitBreakerConfig); /** * Execute `fn` through the circuit breaker. * * - CLOSED: calls `fn` and tracks successes / failures. * - OPEN: throws `CircuitOpenError` immediately (no call to `fn`). * - HALF_OPEN: calls `fn`; success closes, failure re-opens. * * @throws `CircuitOpenError` when circuit is OPEN. * @throws Whatever `fn` throws when circuit is CLOSED or HALF_OPEN. */ execute(fn: () => Promise): Promise; /** Return current state, promoting OPEN → HALF_OPEN after recovery timeout. */ getState(): CircuitState; /** * Force the circuit to OPEN (useful for testing or manual intervention). * Resets the opened-at timer so recovery timeout begins from now. */ trip(): void; /** Force the circuit back to CLOSED and reset counters. */ reset(): void; private onSuccess; private onFailure; private transition; } //# sourceMappingURL=circuit-breaker.d.ts.map