prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
//! Retry state management for checkpoint persistence and restoration
//!
//! This module handles the preservation and restoration of retry state across workflow
//! interruptions and resumptions, ensuring retry logic functions correctly during resumed execution.

use anyhow::{anyhow, Result};
use chrono::{DateTime, Duration as ChronoDuration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

use crate::cook::retry_v2::{BackoffStrategy, RetryConfig};

/// Enhanced retry checkpoint state for comprehensive persistence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryCheckpointState {
    /// Per-command retry states
    pub command_retry_states: HashMap<String, CommandRetryState>,
    /// Global retry configuration
    pub global_retry_config: Option<RetryConfig>,
    /// Execution history for all retries
    pub retry_execution_history: Vec<RetryExecution>,
    /// Circuit breaker states
    pub circuit_breaker_states: HashMap<String, CircuitBreakerState>,
    /// Correlation IDs for tracking
    pub retry_correlation_map: HashMap<String, String>,
    /// Timestamp of checkpoint
    pub checkpointed_at: DateTime<Utc>,
}

/// Retry state for an individual command
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandRetryState {
    /// Unique command identifier
    pub command_id: String,
    /// Number of attempts made
    pub attempt_count: u32,
    /// Maximum attempts allowed
    pub max_attempts: u32,
    /// Last attempt timestamp
    pub last_attempt_at: Option<DateTime<Utc>>,
    /// Next scheduled retry time
    pub next_retry_at: Option<DateTime<Utc>>,
    /// Current backoff state
    pub backoff_state: BackoffState,
    /// History of retry attempts
    pub retry_history: Vec<RetryAttempt>,
    /// Current retry strategy
    pub retry_config: Option<RetryConfig>,
    /// Whether circuit breaker is open
    pub is_circuit_broken: bool,
    /// Time when retry budget expires
    pub retry_budget_expires_at: Option<DateTime<Utc>>,
    /// Total time spent in retries
    pub total_retry_duration: Duration,
}

/// State of backoff strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackoffState {
    /// Backoff strategy being used
    pub strategy: BackoffStrategy,
    /// Current delay duration
    #[serde(with = "humantime_serde")]
    pub current_delay: Duration,
    /// Base delay for calculations
    #[serde(with = "humantime_serde")]
    pub base_delay: Duration,
    /// Maximum delay allowed
    #[serde(with = "humantime_serde")]
    pub max_delay: Duration,
    /// Multiplier for exponential backoff
    pub multiplier: f64,
    /// Whether jitter is applied
    pub jitter_enabled: bool,
    /// Jitter factor (0.0 to 1.0)
    pub jitter_factor: f64,
    /// Last sequence values for fibonacci
    pub fibonacci_prev: Option<u64>,
    pub fibonacci_curr: Option<u64>,
}

/// Record of a single retry attempt
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryAttempt {
    /// Attempt number (1-based)
    pub attempt_number: u32,
    /// When the attempt was executed
    pub executed_at: DateTime<Utc>,
    /// Duration of the attempt
    #[serde(with = "humantime_serde")]
    pub duration: Duration,
    /// Whether the attempt succeeded
    pub success: bool,
    /// Error message if failed
    pub error: Option<String>,
    /// Backoff delay applied before this attempt
    #[serde(with = "humantime_serde")]
    pub backoff_applied: Duration,
    /// Exit code if applicable
    pub exit_code: Option<i32>,
}

/// Comprehensive retry execution record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryExecution {
    /// Command that was retried
    pub command_id: String,
    /// Correlation ID for tracking
    pub correlation_id: String,
    /// Start time of retry sequence
    pub started_at: DateTime<Utc>,
    /// End time of retry sequence
    pub completed_at: Option<DateTime<Utc>>,
    /// Total attempts made
    pub total_attempts: u32,
    /// Whether it ultimately succeeded
    pub succeeded: bool,
    /// Final error if failed
    pub final_error: Option<String>,
}

/// Circuit breaker state persistence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CircuitBreakerState {
    /// Current state of circuit breaker
    pub state: CircuitState,
    /// Number of consecutive failures
    pub failure_count: u32,
    /// Failure threshold before opening
    pub failure_threshold: u32,
    /// Last failure timestamp
    pub last_failure_at: Option<DateTime<Utc>>,
    /// Recovery timeout duration
    #[serde(with = "humantime_serde")]
    pub recovery_timeout: Duration,
    /// Maximum calls in half-open state
    pub half_open_max_calls: u32,
    /// Success count in half-open state
    pub half_open_success_count: u32,
}

/// Circuit breaker state enum
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CircuitState {
    /// Circuit is closed (normal operation)
    Closed,
    /// Circuit is open (rejecting calls)
    Open,
    /// Circuit is half-open (testing recovery)
    HalfOpen,
}

/// Manages retry state across checkpoint operations
pub struct RetryStateManager {
    /// Checkpoint state storage
    checkpoint_state: Arc<RwLock<Option<RetryCheckpointState>>>,
    /// Active command retry states
    command_states: Arc<RwLock<HashMap<String, CommandRetryState>>>,
    /// Circuit breakers
    circuit_breakers: Arc<RwLock<HashMap<String, CircuitBreakerState>>>,
}

impl Default for RetryStateManager {
    fn default() -> Self {
        Self::new()
    }
}

impl RetryStateManager {
    #[cfg(test)]
    pub(crate) fn get_command_states(&self) -> Arc<RwLock<HashMap<String, CommandRetryState>>> {
        self.command_states.clone()
    }

    #[cfg(test)]
    pub(crate) fn get_circuit_breakers(&self) -> Arc<RwLock<HashMap<String, CircuitBreakerState>>> {
        self.circuit_breakers.clone()
    }

    /// Create a new retry state manager
    pub fn new() -> Self {
        Self {
            checkpoint_state: Arc::new(RwLock::new(None)),
            command_states: Arc::new(RwLock::new(HashMap::new())),
            circuit_breakers: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Create checkpoint state for persistence
    pub async fn create_checkpoint_state(&self) -> Result<RetryCheckpointState> {
        let command_states = self.command_states.read().await;
        let circuit_breakers = self.circuit_breakers.read().await;

        let checkpoint = RetryCheckpointState {
            command_retry_states: command_states.clone(),
            global_retry_config: None, // Will be set by caller if needed
            retry_execution_history: Vec::new(), // Populated separately
            circuit_breaker_states: circuit_breakers.clone(),
            retry_correlation_map: HashMap::new(), // Populated separately
            checkpointed_at: Utc::now(),
        };

        Ok(checkpoint)
    }

    /// Restore retry state from checkpoint
    pub async fn restore_from_checkpoint(&self, checkpoint: &RetryCheckpointState) -> Result<()> {
        info!(
            "Restoring retry state from checkpoint at {}",
            checkpoint.checkpointed_at
        );

        // Validate checkpoint consistency
        self.validate_checkpoint_consistency(checkpoint)?;

        // Restore command retry states
        let mut command_states = self.command_states.write().await;
        for (command_id, state) in &checkpoint.command_retry_states {
            debug!(
                "Restoring retry state for command {}: {} attempts",
                command_id, state.attempt_count
            );

            // Adjust timing for elapsed time since checkpoint
            let mut restored_state = state.clone();
            if let Some(next_retry) = state.next_retry_at {
                let elapsed = Utc::now() - checkpoint.checkpointed_at;
                restored_state.next_retry_at = Some(next_retry + elapsed);
            }

            command_states.insert(command_id.clone(), restored_state);
        }

        // Restore circuit breaker states
        let mut circuit_breakers = self.circuit_breakers.write().await;
        for (command_id, cb_state) in &checkpoint.circuit_breaker_states {
            debug!(
                "Restoring circuit breaker for {}: {:?}",
                command_id, cb_state.state
            );

            // Check if circuit should transition based on elapsed time
            let mut restored_cb = cb_state.clone();
            if cb_state.state == CircuitState::Open {
                if let Some(last_failure) = cb_state.last_failure_at {
                    let elapsed = Utc::now() - last_failure;
                    if elapsed.num_seconds() as u64 >= cb_state.recovery_timeout.as_secs() {
                        restored_cb.state = CircuitState::HalfOpen;
                        restored_cb.half_open_success_count = 0;
                        info!(
                            "Circuit breaker for {} transitioned to half-open",
                            command_id
                        );
                    }
                }
            }

            circuit_breakers.insert(command_id.clone(), restored_cb);
        }

        // Store checkpoint for reference
        let mut checkpoint_state = self.checkpoint_state.write().await;
        *checkpoint_state = Some(checkpoint.clone());

        Ok(())
    }

    /// Get retry state for a specific command
    pub async fn get_command_retry_state(&self, command_id: &str) -> Option<CommandRetryState> {
        let states = self.command_states.read().await;
        states.get(command_id).cloned()
    }

    /// Update retry state after an attempt
    pub async fn update_retry_state(
        &self,
        command_id: &str,
        attempt: RetryAttempt,
        config: &RetryConfig,
    ) -> Result<()> {
        let mut states = self.command_states.write().await;

        let state = states
            .entry(command_id.to_string())
            .or_insert_with(|| CommandRetryState {
                command_id: command_id.to_string(),
                attempt_count: 0,
                max_attempts: config.attempts,
                last_attempt_at: None,
                next_retry_at: None,
                backoff_state: self.create_initial_backoff_state(config),
                retry_history: Vec::new(),
                retry_config: Some(config.clone()),
                is_circuit_broken: false,
                retry_budget_expires_at: config
                    .retry_budget
                    .map(|budget| Utc::now() + ChronoDuration::from_std(budget).unwrap()),
                total_retry_duration: Duration::from_secs(0),
            });

        // Update state
        state.attempt_count += 1;
        state.last_attempt_at = Some(attempt.executed_at);
        state.retry_history.push(attempt.clone());
        state.total_retry_duration += attempt.duration;

        // Calculate next retry time if not successful
        if !attempt.success && state.attempt_count < state.max_attempts {
            let next_delay = self.calculate_next_delay(&mut state.backoff_state)?;
            state.next_retry_at = Some(Utc::now() + ChronoDuration::from_std(next_delay)?);
            state.backoff_state.current_delay = next_delay;
        }

        // Update circuit breaker
        if !attempt.success {
            self.update_circuit_breaker(command_id, false).await?;
        } else {
            self.update_circuit_breaker(command_id, true).await?;
        }

        Ok(())
    }

    /// Check if command can retry
    pub async fn can_retry(&self, command_id: &str) -> Result<bool> {
        let states = self.command_states.read().await;
        let circuit_breakers = self.circuit_breakers.read().await;

        // Check command retry state
        if let Some(state) = states.get(command_id) {
            // Check attempt limit
            if state.attempt_count >= state.max_attempts {
                debug!("Command {} exceeded max attempts", command_id);
                return Ok(false);
            }

            // Check retry budget
            if let Some(expires_at) = state.retry_budget_expires_at {
                if Utc::now() >= expires_at {
                    debug!("Command {} retry budget expired", command_id);
                    return Ok(false);
                }
            }

            // Check circuit breaker
            if let Some(cb) = circuit_breakers.get(command_id) {
                if cb.state == CircuitState::Open {
                    debug!("Circuit breaker open for command {}", command_id);
                    return Ok(false);
                }
            }

            Ok(true)
        } else {
            // No retry state yet, can retry
            Ok(true)
        }
    }

    /// Calculate the next backoff delay
    fn calculate_next_delay(&self, backoff_state: &mut BackoffState) -> Result<Duration> {
        let base_delay = match &backoff_state.strategy {
            BackoffStrategy::Fixed => backoff_state.base_delay,
            BackoffStrategy::Linear { increment } => backoff_state.current_delay + *increment,
            BackoffStrategy::Exponential { base } => {
                let millis = (backoff_state.current_delay.as_millis() as f64 * base) as u64;
                Duration::from_millis(millis)
            }
            BackoffStrategy::Fibonacci => {
                let (prev, curr) = if let (Some(p), Some(c)) =
                    (backoff_state.fibonacci_prev, backoff_state.fibonacci_curr)
                {
                    (p, c)
                } else {
                    (1, 1)
                };

                let next = prev + curr;
                backoff_state.fibonacci_prev = Some(curr);
                backoff_state.fibonacci_curr = Some(next);

                Duration::from_secs(next)
            }
            BackoffStrategy::Custom { delays } => {
                // Use the delay for the current attempt, or last one if exceeded
                let index = backoff_state.current_delay.as_secs() as usize;
                delays
                    .get(index)
                    .or_else(|| delays.last())
                    .copied()
                    .unwrap_or(backoff_state.base_delay)
            }
        };

        // Apply jitter if enabled
        let delay = if backoff_state.jitter_enabled {
            let jitter_range = base_delay.as_millis() as f64 * backoff_state.jitter_factor;
            let jitter = rand::random::<f64>() * jitter_range - (jitter_range / 2.0);
            let millis = (base_delay.as_millis() as f64 + jitter).max(0.0) as u64;
            Duration::from_millis(millis)
        } else {
            base_delay
        };

        // Enforce max delay
        Ok(delay.min(backoff_state.max_delay))
    }

    /// Create initial backoff state from config
    fn create_initial_backoff_state(&self, config: &RetryConfig) -> BackoffState {
        BackoffState {
            strategy: config.backoff.clone(),
            current_delay: config.initial_delay,
            base_delay: config.initial_delay,
            max_delay: config.max_delay,
            multiplier: match &config.backoff {
                BackoffStrategy::Exponential { base } => *base,
                _ => 2.0,
            },
            jitter_enabled: config.jitter,
            jitter_factor: config.jitter_factor,
            fibonacci_prev: None,
            fibonacci_curr: None,
        }
    }

    /// Update circuit breaker state
    async fn update_circuit_breaker(&self, command_id: &str, success: bool) -> Result<()> {
        let mut breakers = self.circuit_breakers.write().await;

        let breaker = breakers.entry(command_id.to_string()).or_insert_with(|| {
            CircuitBreakerState {
                state: CircuitState::Closed,
                failure_count: 0,
                failure_threshold: 5, // Default threshold
                last_failure_at: None,
                recovery_timeout: Duration::from_secs(60),
                half_open_max_calls: 3,
                half_open_success_count: 0,
            }
        });

        match breaker.state {
            CircuitState::Closed => {
                if !success {
                    breaker.failure_count += 1;
                    breaker.last_failure_at = Some(Utc::now());

                    if breaker.failure_count >= breaker.failure_threshold {
                        breaker.state = CircuitState::Open;
                        warn!("Circuit breaker opened for command {}", command_id);
                    }
                } else {
                    breaker.failure_count = 0;
                }
            }
            CircuitState::Open => {
                // Check if we should transition to half-open
                if let Some(last_failure) = breaker.last_failure_at {
                    let elapsed = Utc::now() - last_failure;
                    if elapsed.num_seconds() as u64 >= breaker.recovery_timeout.as_secs() {
                        breaker.state = CircuitState::HalfOpen;
                        breaker.half_open_success_count = 0;
                        info!("Circuit breaker half-open for command {}", command_id);
                    }
                }
            }
            CircuitState::HalfOpen => {
                if success {
                    breaker.half_open_success_count += 1;
                    if breaker.half_open_success_count >= breaker.half_open_max_calls {
                        breaker.state = CircuitState::Closed;
                        breaker.failure_count = 0;
                        info!("Circuit breaker closed for command {}", command_id);
                    }
                } else {
                    breaker.state = CircuitState::Open;
                    breaker.last_failure_at = Some(Utc::now());
                    warn!("Circuit breaker re-opened for command {}", command_id);
                }
            }
        }

        Ok(())
    }

    /// Validate checkpoint consistency
    fn validate_checkpoint_consistency(&self, checkpoint: &RetryCheckpointState) -> Result<()> {
        // Check for inconsistent retry states
        for (command_id, state) in &checkpoint.command_retry_states {
            if state.attempt_count > state.max_attempts + 1 {
                return Err(anyhow!(
                    "Inconsistent retry state for {}: attempts {} > max {}",
                    command_id,
                    state.attempt_count,
                    state.max_attempts
                ));
            }

            // Validate retry history
            if state.retry_history.len() as u32 != state.attempt_count {
                warn!(
                    "Retry history mismatch for {}: {} history entries vs {} attempts",
                    command_id,
                    state.retry_history.len(),
                    state.attempt_count
                );
            }
        }

        Ok(())
    }

    /// Clear retry state for a command
    pub async fn clear_command_state(&self, command_id: &str) {
        let mut states = self.command_states.write().await;
        let mut breakers = self.circuit_breakers.write().await;

        states.remove(command_id);
        breakers.remove(command_id);

        debug!("Cleared retry state for command {}", command_id);
    }

    /// Get summary of all retry states
    pub async fn get_retry_summary(&self) -> HashMap<String, (u32, u32, bool)> {
        let states = self.command_states.read().await;
        let breakers = self.circuit_breakers.read().await;

        let mut summary = HashMap::new();

        for (command_id, state) in states.iter() {
            let is_open = breakers
                .get(command_id)
                .map(|b| b.state == CircuitState::Open)
                .unwrap_or(false);

            summary.insert(
                command_id.clone(),
                (state.attempt_count, state.max_attempts, is_open),
            );
        }

        summary
    }
}

/// Coordinated retry state for MapReduce operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoordinatedRetryState {
    /// Work item retry states
    pub work_item_retries: HashMap<String, WorkItemRetryState>,
    /// Failed items in DLQ
    pub dlq_retries: Vec<DlqRetryState>,
    /// Cross-agent consistency check result
    pub consistency_valid: bool,
}

/// Retry state for a work item in MapReduce
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkItemRetryState {
    /// Work item identifier
    pub work_item_id: String,
    /// Agent processing this item
    pub agent_id: String,
    /// Retry attempt count
    pub attempt_count: u32,
    /// Last attempt timestamp
    pub last_attempt_at: DateTime<Utc>,
}

/// DLQ retry state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DlqRetryState {
    /// Work item in DLQ
    pub work_item_id: String,
    /// Number of times retried from DLQ
    pub dlq_retry_count: u32,
    /// When it entered DLQ
    pub entered_dlq_at: DateTime<Utc>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_retry_state_persistence_and_restoration() {
        let manager = RetryStateManager::new();

        // Create some retry state
        let attempt = RetryAttempt {
            attempt_number: 1,
            executed_at: Utc::now(),
            duration: Duration::from_secs(2),
            success: false,
            error: Some("Test error".to_string()),
            backoff_applied: Duration::from_secs(0),
            exit_code: Some(1),
        };

        let config = RetryConfig::default();
        manager
            .update_retry_state("test_cmd", attempt, &config)
            .await
            .unwrap();

        // Create checkpoint
        let checkpoint = manager.create_checkpoint_state().await.unwrap();
        assert_eq!(checkpoint.command_retry_states.len(), 1);

        // Create new manager and restore
        let new_manager = RetryStateManager::new();
        new_manager
            .restore_from_checkpoint(&checkpoint)
            .await
            .unwrap();

        // Verify state was restored
        let restored = new_manager.get_command_retry_state("test_cmd").await;
        assert!(restored.is_some());
        assert_eq!(restored.unwrap().attempt_count, 1);
    }

    #[tokio::test]
    async fn test_circuit_breaker_state_transitions() {
        let manager = RetryStateManager::new();

        // Simulate failures to open circuit
        for i in 0..5 {
            let attempt = RetryAttempt {
                attempt_number: i + 1,
                executed_at: Utc::now(),
                duration: Duration::from_secs(1),
                success: false,
                error: Some("Failed".to_string()),
                backoff_applied: Duration::from_secs(i as u64),
                exit_code: Some(1),
            };

            let config = RetryConfig::default();
            manager
                .update_retry_state("test_cmd", attempt, &config)
                .await
                .unwrap();
        }

        // Check circuit is open
        let can_retry = manager.can_retry("test_cmd").await.unwrap();
        assert!(!can_retry, "Circuit should be open after failures");

        // Verify state in checkpoint
        let checkpoint = manager.create_checkpoint_state().await.unwrap();
        let cb_state = checkpoint.circuit_breaker_states.get("test_cmd").unwrap();
        assert_eq!(cb_state.state, CircuitState::Open);
    }

    #[tokio::test]
    async fn test_retry_budget_enforcement() {
        let manager = RetryStateManager::new();

        let config = RetryConfig {
            retry_budget: Some(Duration::from_secs(5)),
            attempts: 100, // High limit to test budget
            ..Default::default()
        };

        // Create initial state with budget
        let attempt = RetryAttempt {
            attempt_number: 1,
            executed_at: Utc::now() - ChronoDuration::seconds(10), // 10 seconds ago
            duration: Duration::from_secs(1),
            success: false,
            error: Some("Failed".to_string()),
            backoff_applied: Duration::from_secs(0),
            exit_code: Some(1),
        };

        manager
            .update_retry_state("budget_cmd", attempt, &config)
            .await
            .unwrap();

        // Manually expire budget for testing
        {
            let mut states = manager.command_states.write().await;
            if let Some(state) = states.get_mut("budget_cmd") {
                state.retry_budget_expires_at = Some(Utc::now() - ChronoDuration::seconds(1));
            }
        }

        // Check retry is not allowed
        let can_retry = manager.can_retry("budget_cmd").await.unwrap();
        assert!(!can_retry, "Should not retry after budget expired");
    }
}