Skip to main content

CODITECT Retry Engine - Resilient Operation Execution with Backoff

Part of Track H.2.5: Inter-Agent Communication Infrastructure Based on AUTONOMOUS-AGENT-SYSTEM-DESIGN.md specifications

This module provides:

  • RetryEngine: Configurable retry execution with multiple backoff strategies
  • RetryPolicy: Delay calculation with jitter support
  • @retry decorator: Easy function wrapping
  • RetryWithCircuitBreaker: Coordinated fault tolerance
  • Comprehensive metrics and monitoring

Backoff Strategies: ┌─────────────────────────────────────────────────────────────────┐ │ EXPONENTIAL: delay = base * (multiplier ^ attempt) │ │ LINEAR: delay = base + (increment * attempt) │ │ FIXED: delay = base (constant) │ │ FIBONACCI: delay = base * fib(attempt) │ │ DECORRELATED: delay = random(base, previous_delay * 3) │ └─────────────────────────────────────────────────────────────────┘

Jitter Types (prevent thundering herd): ┌─────────────────────────────────────────────────────────────────┐ │ NONE: No jitter (exact delay) │ │ FULL: random(0, delay) │ │ EQUAL: delay/2 + random(0, delay/2) │ │ DECORRELATED: random(base, delay * 3) - AWS recommended │ └─────────────────────────────────────────────────────────────────┘

Usage: from scripts.core.retry_engine import ( RetryEngine, RetryConfig, retry, RetryWithCircuitBreaker, )

# Using decorator
@retry(max_attempts=5, backoff="exponential")
async def call_api(endpoint: str):
return await http_client.get(endpoint)

# Using engine directly
engine = RetryEngine(RetryConfig(
max_attempts=5,
base_delay=1.0,
max_delay=60.0,
backoff_strategy=BackoffStrategy.EXPONENTIAL,
jitter=JitterType.FULL,
))

result = await engine.execute(risky_function, arg1, arg2)

# With circuit breaker integration
resilient = RetryWithCircuitBreaker(
retry_config=RetryConfig(max_attempts=3),
circuit_breaker=breaker,
)
result = await resilient.execute(api_call)

Author: CODITECT Framework Created: January 8, 2026 Version: 1.0.0

File: retry_engine.py

Classes

BackoffStrategy

Backoff strategy for retry delays.

JitterType

Jitter type to add randomness to delays.

RetryDecision

Decision for whether to retry.

RetryError

Base exception for retry errors.

MaxRetriesExceeded

Raised when max retries exceeded.

RetryTimeout

Raised when total retry time exceeded.

RetryConfig

Configuration for retry behavior.

RetryAttempt

Record of a single retry attempt.

RetryMetrics

Metrics for retry operations.

RetryPolicy

Calculates retry delays based on configuration.

Functions

retry(max_attempts, base_delay, max_delay, multiplier, backoff, jitter, retry_exceptions, ignore_exceptions, total_timeout, on_retry)

Decorator to add retry logic to a function.

create_retry_engine(max_attempts, base_delay, backoff, jitter)

Create a retry engine with common settings.

exponential_backoff(max_attempts, base_delay, max_delay, multiplier)

Create retry engine with exponential backoff.

linear_backoff(max_attempts, base_delay, max_delay, increment)

Create retry engine with linear backoff.

constant_backoff(max_attempts, delay)

Create retry engine with constant delay.

retry_sync(func)

Convenience function to retry a sync function.

get_retry_config(name)

Get a predefined retry configuration.

to_dict()

Convert to dictionary (without callables).

calculate_delay(attempt)

Calculate delay for a given attempt number.

should_retry(attempt, exception, elapsed)

Decide whether to retry.

reset()

Reset policy state (for decorrelated backoff).

execute_sync(func)

Execute a synchronous function with retry logic.

get_metrics()

Get retry metrics.

reset_metrics()

Reset all metrics.

execute_sync(func)

Execute synchronously with retry and circuit breaker.

Usage

python retry_engine.py