/** * Retry Logic and Backoff Strategies * * Implements retry logic with various backoff strategies including * exponential, linear, and fixed delays with optional jitter. * * @module execution/resilience/retry */ import type { RetryPolicy, ExecutionAttempt } from './types.js'; import type { ExecutionResult } from '../engine/types.js'; /** * Calculate backoff delay for a given attempt * * Supports multiple backoff strategies: * - Exponential: baseDelay * 2^(attempt-1) - e.g., 1s, 2s, 4s, 8s, 16s * - Linear: baseDelay * attempt - e.g., 1s, 2s, 3s, 4s, 5s * - Fixed: constant baseDelay - e.g., 1s, 1s, 1s, 1s, 1s * * Applies maxDelay cap and optional jitter to prevent thundering herd. * * @param attempt - Attempt number (1-indexed) * @param config - Backoff configuration from retry policy * @returns Delay in milliseconds * * @example * ```typescript * // Exponential backoff with jitter * const delay = calculateBackoff(3, { * type: 'exponential', * baseDelayMs: 1000, * maxDelayMs: 30000, * jitter: true, * }); * // Returns ~4000ms ± 10% jitter * ``` */ export declare function calculateBackoff(attempt: number, config: RetryPolicy['backoff']): number; /** * Check if an error should trigger a retry * * Matches error message against the list of retryable error patterns * defined in the retry policy. * * @param error - Error that occurred * @param policy - Retry policy with retryable error patterns * @returns True if error should be retried * * @example * ```typescript * const error = new Error('Connection timeout'); * const shouldRetry = isRetryableError(error, { * retryableErrors: ['timeout', 'ECONNREFUSED'], * // ... other policy fields * }); * // Returns true * ``` */ export declare function isRetryableError(error: Error, policy: RetryPolicy): boolean; /** * Check if an exit code should trigger a retry * * Matches exit code against the list of retryable exit codes * defined in the retry policy. * * @param exitCode - Process exit code * @param policy - Retry policy with retryable exit codes * @returns True if exit code should be retried * * @example * ```typescript * const shouldRetry = isRetryableExitCode(1, { * retryableExitCodes: [1, 137], * // ... other policy fields * }); * // Returns true * ``` */ export declare function isRetryableExitCode(exitCode: number, policy: RetryPolicy): boolean; /** * Check if an execution result should trigger a retry * * Checks both the exit code and error message (if present) to determine * if the execution should be retried. * * @param result - Execution result from task execution * @param policy - Retry policy * @returns True if execution should be retried * * @example * ```typescript * const result: ExecutionResult = { * taskId: 'task-1', * executionId: 'proc-123', * success: false, * exitCode: 1, * error: 'Connection timeout', * // ... other fields * }; * * const shouldRetry = isRetryableResult(result, policy); * // Returns true if exitCode is retryable OR error contains retryable pattern * ``` */ export declare function isRetryableResult(result: ExecutionResult, policy: RetryPolicy): boolean; /** * Promise-based sleep utility * * Returns a promise that resolves after the specified delay. * Useful for implementing retry backoff. * * @param ms - Delay in milliseconds * @returns Promise that resolves after delay * * @example * ```typescript * console.log('Starting...'); * await sleep(1000); * console.log('1 second later'); * ``` */ export declare function sleep(ms: number): Promise; /** * Create an execution attempt record * * Helper function to create a properly structured ExecutionAttempt object. * * @param attemptNumber - Attempt number (1-indexed) * @param success - Whether the attempt succeeded * @param options - Optional fields for the attempt * @returns ExecutionAttempt object */ export declare function createAttempt(attemptNumber: number, success: boolean, options?: { error?: Error; exitCode?: number; duration?: number; willRetry?: boolean; nextRetryAt?: Date; }): ExecutionAttempt; /** * Calculate total delay from all retry attempts * * Sums up all the backoff delays that would be applied for a given * number of retry attempts. Useful for timeout calculations. * * @param maxAttempts - Maximum number of attempts * @param backoffConfig - Backoff configuration * @returns Total delay in milliseconds * * @example * ```typescript * const totalDelay = calculateTotalRetryDelay(3, { * type: 'exponential', * baseDelayMs: 1000, * maxDelayMs: 30000, * jitter: false, * }); * // Returns 7000ms (1s + 2s + 4s) * ``` */ export declare function calculateTotalRetryDelay(maxAttempts: number, backoffConfig: RetryPolicy['backoff']): number; //# sourceMappingURL=retry.d.ts.map