## PRD: Enhance Email Retry Mechanism with Exponential Backoff

This PRD builds upon the basic retry mechanism for failed email sends by implementing exponential backoff to improve reliability and avoid overwhelming the email service.

### GPM-065: Implement Exponential Backoff Calculation

**Goal:** Calculate the delay for each retry attempt using an exponential backoff formula.
**Acceptance Criteria:**
- A function `calculate_backoff(attempt)` exists that returns a delay in seconds.
- The delay increases exponentially with each attempt, starting with a base delay (e.g., 1 second).
- A maximum delay is enforced to prevent excessive delays.
**Files:** `aiva/email/retry.py`
**Spawns:** Configure initial backoff delay and max delay values.

### GPM-066: Integrate Exponential Backoff into Retry Logic

**Goal:** Use the calculated delay from GPM-065 in the email retry mechanism.
**Acceptance Criteria:**
- The email sending function pauses for the calculated backoff duration before each retry attempt.
- The existing retry limit is respected.
- Logging accurately reflects the delay used for each retry.
**Files:** `aiva/email/sender.py`, `aiva/email/retry.py`
**Spawns:** Implement jitter to avoid synchronized retry attempts.

### GPM-067: Configure Initial Backoff Delay and Max Delay

**Goal:** Make the initial backoff delay and maximum delay configurable via environment variables.
**Acceptance Criteria:**
-  Two new environment variables, `EMAIL_RETRY_INITIAL_DELAY` and `EMAIL_RETRY_MAX_DELAY`, are introduced.
-  The values of these environment variables are used to configure the exponential backoff calculation.
-  Default values are used if the environment variables are not set.
**Files:** `aiva/config.py`, `aiva/email/retry.py`
**Spawns:** Add monitoring for retry attempts and success/failure rates.

### GPM-068: Implement Jitter to Avoid Synchronized Retries

**Goal:** Add a random jitter to the backoff delay to avoid synchronized retry attempts.
**Acceptance Criteria:**
- A random value, up to a defined percentage of the calculated backoff delay, is added to the delay.
- The jitter is applied consistently across all retry attempts.
- The overall delay remains within reasonable bounds.
**Files:** `aiva/email/retry.py`
**Spawns:** Implement different backoff strategies (e.g., full jitter, decorrelated jitter).

### GPM-069: Add Logging for Retry Attempts and Success/Failure Rates

**Goal:** Enhance logging to track retry attempts, successes, and failures.
**Acceptance Criteria:**
- Log messages are generated at each retry attempt, including the attempt number and the calculated backoff delay.
- Log messages are generated upon successful email sending after a retry.
- Log messages are generated when the retry limit is reached and the email sending fails.
- Implement metrics on total email sends, retries, and failures.
**Files:** `aiva/email/sender.py`, `aiva/email/retry.py`
**Spawns:** Implement circuit breaker pattern to prevent repeated failed retries.

### GPM-070: Implement Circuit Breaker Pattern for Email Sending

**Goal:** Implement a circuit breaker to prevent repeated failed retry attempts to potentially overwhelmed email services.
**Acceptance Criteria:**
- A circuit breaker is implemented around the email sending function.
- The circuit breaker opens if the failure rate exceeds a defined threshold.
- When open, the circuit breaker prevents further email sending attempts for a defined period.
- The circuit breaker automatically attempts to close after the defined period, allowing email sending to resume.
**Files:** `aiva/email/circuit_breaker.py`, `aiva/email/sender.py`
**Spawns:** Implement different email sending providers and failover mechanisms.
