/** * CircuitBreaker (L13) * * Prevents cascade failures in agentic workflows. * Implements the circuit breaker pattern for LLM operations. * * Threat Model: * - ASI08: Cascading Failures * - Runaway agent behavior * - Resource exhaustion via retries * * Protection Capabilities: * - Failure rate monitoring * - Automatic circuit opening * - Graceful degradation * - Recovery detection * - Rollback triggers */ export interface CircuitBreakerConfig { /** Failure threshold percentage to open circuit (0-100) */ failureThreshold?: number; /** Minimum number of requests before threshold applies */ minimumRequests?: number; /** Time window for failure counting in milliseconds */ windowSize?: number; /** Time to wait before attempting recovery in milliseconds */ recoveryTimeout?: number; /** Number of successful requests to close circuit */ successThreshold?: number; /** Enable automatic recovery attempts */ autoRecover?: boolean; /** Maximum consecutive failures before forced open */ maxConsecutiveFailures?: number; /** Callback when circuit opens */ onOpen?: (circuitId: string, stats: CircuitStats) => void; /** Callback when circuit closes */ onClose?: (circuitId: string, stats: CircuitStats) => void; /** Callback when circuit half-opens */ onHalfOpen?: (circuitId: string) => void; } export type CircuitState = "closed" | "open" | "half-open"; export interface CircuitStats { totalRequests: number; successfulRequests: number; failedRequests: number; consecutiveFailures: number; consecutiveSuccesses: number; failureRate: number; lastFailure?: number; lastSuccess?: number; stateChangedAt: number; } export interface CircuitBreakerResult { allowed: boolean; state: CircuitState; reason: string; request_id: string; stats: CircuitStats; fallback_recommended: boolean; retry_after?: number; } export interface OperationResult { success: boolean; duration: number; error?: string; } export declare class CircuitBreaker { private config; private circuits; constructor(config?: CircuitBreakerConfig); /** * Check if operation should be allowed through circuit */ check(circuitId: string, requestId?: string): CircuitBreakerResult; /** * Record operation result */ recordResult(circuitId: string, result: OperationResult): void; /** * Record a successful operation */ recordSuccess(circuitId: string, duration?: number): void; /** * Record a failed operation */ recordFailure(circuitId: string, error?: string, duration?: number): void; /** * Get current state of a circuit */ getState(circuitId: string): CircuitState; /** * Get stats for a circuit */ getStats(circuitId: string): CircuitStats | null; /** * Get all circuit IDs */ getCircuitIds(): string[]; /** * Force open a circuit */ forceOpen(circuitId: string): void; /** * Force close a circuit */ forceClose(circuitId: string): void; /** * Reset a circuit to initial state */ reset(circuitId: string): void; /** * Reset all circuits */ resetAll(): void; /** * Execute operation with circuit breaker protection */ execute(circuitId: string, operation: () => Promise, fallback?: () => Promise): Promise<{ result?: T; fallbackUsed: boolean; error?: string; }>; /** * Health check across all circuits */ healthCheck(): { healthy: boolean; circuits: Array<{ id: string; state: CircuitState; failureRate: number; }>; openCircuits: number; }; private getOrCreateCircuit; private openCircuit; private closeCircuit; private transitionToHalfOpen; private cleanupWindow; private countInWindow; }