loopctl 0.1.0

A trait-based framework for building agent loops with pluggable LLM clients, tools, and memory
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
//! Exponential backoff recovery strategy.
//!
//! [`ExponentialBackoffRecovery`] retries recoverable failures with
//! exponentially increasing delays, up to a configurable maximum.
//! This is the default [`RecoveryStrategy`] used by `BareLoop`.
//!
//! # Quick Start
//!
//! ```
//! use loopctl::reflection::backoff::ExponentialBackoffRecovery;
//! use loopctl::reflection::{RecoveryAction, FailureAnalysis, FailureSeverity, RecoveryStrategy};
//!
//! let strategy = ExponentialBackoffRecovery::new(3);
//! ```

use super::{FailureAnalysis, FailureSeverity, RecoveryAction, RecoveryStrategy};
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;

// ===================================================
// ExponentialBackoffRecovery
// ===================================================

/// A recovery strategy that retries with exponential backoff.
///
/// When a tool call fails with a recoverable error, this strategy
/// retries up to [`max_retries`](ExponentialBackoffRecovery::max_retries) times,
/// with delays that grow exponentially: `base_delay * 2^attempt`.
///
/// # Behaviour by severity
///
/// | Severity   | Recoverable | Correction | Action                                                            |
/// |------------|-------------|------------|-------------------------------------------------------------------|
/// | Low/Medium | ✓           | —          | Retry with backoff                                                |
/// | High       | ✓           | Some       | [`AskUser`](super::RecoveryAction::AskUser) (correction provided) |
/// | High       | ✓           | None       | Retry with backoff                                                |
/// | Critical   | ✗           | —          | Fail immediately                                                  |
/// | any        | ✗           | —          | Fail immediately                                                  |
///
/// # Example
///
/// ```
/// use loopctl::reflection::backoff::ExponentialBackoffRecovery;
///
/// let strategy = ExponentialBackoffRecovery::new(5);
/// ```
#[derive(Debug, Clone)]
pub struct ExponentialBackoffRecovery {
    /// Maximum number of retry attempts before giving up.
    max_retries: u32,
    /// Initial delay applied to the first retry, doubled on each subsequent attempt.
    base_delay: Duration,
    /// Upper bound on the computed delay, regardless of exponential growth.
    max_delay: Duration,
}

impl ExponentialBackoffRecovery {
    /// Create a new strategy with the given maximum retry count.
    ///
    /// Defaults: `base_delay` = 100ms, `max_delay` = 30s.
    #[must_use]
    pub fn new(max_retries: u32) -> Self {
        Self {
            max_retries,
            base_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(30),
        }
    }

    /// Set a custom base delay for the exponential backoff.
    ///
    /// The delay for attempt `n` is `base_delay * 2^n`, capped at `max_delay`.
    #[must_use]
    pub fn with_base_delay(mut self, delay: Duration) -> Self {
        self.base_delay = delay;
        self
    }

    /// Set a maximum delay cap.
    ///
    /// Even as the exponential grows, the delay never exceeds this value.
    #[must_use]
    pub fn with_max_delay(mut self, delay: Duration) -> Self {
        self.max_delay = delay;
        self
    }

    /// Maximum retry attempts before giving up.
    #[must_use]
    pub fn max_retries(&self) -> u32 {
        self.max_retries
    }

    /// Base delay for exponential backoff calculation.
    #[must_use]
    pub fn base_delay(&self) -> Duration {
        self.base_delay
    }

    /// Maximum delay cap.
    #[must_use]
    pub fn max_delay(&self) -> Duration {
        self.max_delay
    }

    /// Calculate the backoff delay for a given attempt.
    ///
    /// Computes `base_delay * 2^attempt`, clamped to [`max_delay`](Self::max_delay).
    ///
    /// Attempt 0 yields `base_delay`, attempt 1 yields `2 * base_delay`, and
    /// so on. Once the exponential exceeds `max_delay`, every subsequent
    /// attempt returns `max_delay`.
    ///
    /// Uses saturating arithmetic to avoid overflow on very large attempt
    /// numbers — once `2^attempt` would overflow, the delay is clamped to
    /// `max_delay`.
    fn delay_for_attempt(&self, attempt: u32) -> Duration {
        let delay_ms = self
            .base_delay
            .as_millis()
            .saturating_mul(1u128.checked_shl(attempt).unwrap_or(u128::MAX));
        let delay_ms = u64::try_from(delay_ms.min(self.max_delay.as_millis())).unwrap_or(u64::MAX);
        Duration::from_millis(delay_ms)
    }
}

impl RecoveryStrategy for ExponentialBackoffRecovery {
    fn decide(
        &self,
        analysis: &FailureAnalysis,
        attempt: u32,
        max_attempts: u32,
    ) -> Pin<Box<dyn Future<Output = RecoveryAction> + Send + '_>> {
        // Honour both ceilings: the strategy-local retry limit and the
        // framework-imposed `max_attempts` budget. Whichever is reached
        // first causes us to give up.
        let max_retries = self.max_retries.min(max_attempts);
        let action = if !analysis.is_recoverable {
            RecoveryAction::Fail(analysis.root_cause.clone())
        } else if attempt >= max_retries {
            RecoveryAction::Fail(format!("max retries ({max_retries}) exceeded"))
        } else if analysis.severity >= FailureSeverity::High && analysis.correction.is_some() {
            RecoveryAction::AskUser(format!(
                "high-severity failure with correction available: {}",
                analysis.root_cause
            ))
        } else {
            RecoveryAction::Retry {
                delay: self.delay_for_attempt(attempt),
            }
        };
        Box::pin(async move { action })
    }
}

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

    #[test]
    fn backoff_builder_defaults() {
        let strategy = ExponentialBackoffRecovery::new(3);
        assert_eq!(strategy.max_retries(), 3);
        assert_eq!(strategy.base_delay(), Duration::from_millis(100));
        assert_eq!(strategy.max_delay(), Duration::from_secs(30));
    }

    #[test]
    fn backoff_builder_custom() {
        let strategy = ExponentialBackoffRecovery::new(5)
            .with_base_delay(Duration::from_millis(200))
            .with_max_delay(Duration::from_secs(60));
        assert_eq!(strategy.max_retries(), 5);
        assert_eq!(strategy.base_delay(), Duration::from_millis(200));
        assert_eq!(strategy.max_delay(), Duration::from_secs(60));
    }

    #[tokio::test]
    async fn backoff_recoverable_first_attempt() {
        let strategy = ExponentialBackoffRecovery::new(3);
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "timeout".to_string(),
            severity: FailureSeverity::Medium,
            correction: None,
            context: String::new(),
        };
        let action = strategy.decide(&analysis, 0, 5).await;
        assert!(action.is_retry());
        assert_eq!(action.delay(), Some(Duration::from_millis(100)));
    }

    #[tokio::test]
    async fn backoff_recoverable_second_attempt() {
        let strategy = ExponentialBackoffRecovery::new(3);
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "timeout".to_string(),
            severity: FailureSeverity::Medium,
            correction: None,
            context: String::new(),
        };
        let action = strategy.decide(&analysis, 1, 5).await;
        assert!(action.is_retry());
        assert_eq!(action.delay(), Some(Duration::from_millis(200)));
    }

    #[tokio::test]
    async fn backoff_recoverable_third_attempt() {
        let strategy = ExponentialBackoffRecovery::new(3);
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "timeout".to_string(),
            severity: FailureSeverity::Medium,
            correction: None,
            context: String::new(),
        };
        let action = strategy.decide(&analysis, 2, 5).await;
        assert!(action.is_retry());
        assert_eq!(action.delay(), Some(Duration::from_millis(400)));
    }

    #[tokio::test]
    async fn backoff_max_retries_exceeded() {
        let strategy = ExponentialBackoffRecovery::new(3);
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "timeout".to_string(),
            severity: FailureSeverity::Low,
            correction: None,
            context: String::new(),
        };
        let action = strategy.decide(&analysis, 3, 5).await;
        assert!(action.is_fail());
        let reason = match action {
            RecoveryAction::Fail(r) => r,
            _ => String::new(),
        };
        assert!(reason.contains("max retries"));
    }

    #[tokio::test]
    async fn backoff_respects_framework_max_attempts() {
        // strategy allows up to 10 retries, but the framework budget is 2.
        // At attempt == max_attempts we must give up, ignoring the higher
        // strategy-local limit.
        let strategy = ExponentialBackoffRecovery::new(10);
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "timeout".to_string(),
            severity: FailureSeverity::Medium,
            correction: None,
            context: String::new(),
        };
        let action = strategy.decide(&analysis, 2, 2).await;
        assert!(action.is_fail());
        let reason = match action {
            RecoveryAction::Fail(r) => r,
            _ => String::new(),
        };
        assert!(reason.contains("max retries (2)"));
    }

    #[tokio::test]
    async fn backoff_unrecoverable_fails_immediately() {
        let strategy = ExponentialBackoffRecovery::new(3);
        let analysis = FailureAnalysis {
            is_recoverable: false,
            root_cause: "invalid api key".to_string(),
            severity: FailureSeverity::Critical,
            correction: None,
            context: String::new(),
        };
        let action = strategy.decide(&analysis, 0, 5).await;
        assert!(action.is_fail());
        let reason = match action {
            RecoveryAction::Fail(r) => r,
            _ => String::new(),
        };
        assert_eq!(reason, "invalid api key");
    }

    #[tokio::test]
    async fn backoff_delay_capped_at_max() {
        let strategy = ExponentialBackoffRecovery::new(10)
            .with_base_delay(Duration::from_secs(1))
            .with_max_delay(Duration::from_secs(5));
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "timeout".to_string(),
            severity: FailureSeverity::Medium,
            correction: None,
            context: String::new(),
        };
        // 1 * 2^5 = 32s, capped at 5s
        let action = strategy.decide(&analysis, 5, 10).await;
        assert_eq!(action.delay(), Some(Duration::from_secs(5)));
    }

    #[tokio::test]
    async fn backoff_low_severity_retries() {
        let strategy = ExponentialBackoffRecovery::new(3);
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "transient hiccup".to_string(),
            severity: FailureSeverity::Low,
            correction: None,
            context: String::new(),
        };
        let action = strategy.decide(&analysis, 0, 5).await;
        assert!(action.is_retry());
    }

    #[tokio::test]
    async fn backoff_high_severity_with_correction_asks_user() {
        use super::super::{Correction, CorrectionType};
        let strategy = ExponentialBackoffRecovery::new(3);
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "bad parameter".to_string(),
            severity: FailureSeverity::High,
            correction: Some(Correction {
                correction_type: CorrectionType::InputFix,
                description: "fix the file path".to_string(),
                modified_input: None,
                alternative_tool: None,
                guidance: None,
            }),
            context: String::new(),
        };
        let action = strategy.decide(&analysis, 0, 5).await;
        assert!(action.is_ask_user());
    }

    #[tokio::test]
    async fn backoff_high_severity_without_correction_retries() {
        let strategy = ExponentialBackoffRecovery::new(3);
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "timeout".to_string(),
            severity: FailureSeverity::High,
            correction: None,
            context: String::new(),
        };
        let action = strategy.decide(&analysis, 0, 5).await;
        assert!(action.is_retry());
    }

    #[test]
    fn backoff_debug_format() {
        let strategy = ExponentialBackoffRecovery::new(3);
        let debug = format!("{strategy:?}");
        assert!(debug.contains("ExponentialBackoffRecovery"));
        assert!(debug.contains("max_retries"));
    }

    #[test]
    fn delay_for_attempt_zero_yields_base() {
        let strategy = ExponentialBackoffRecovery::new(3);
        assert_eq!(strategy.delay_for_attempt(0), Duration::from_millis(100));
    }

    #[test]
    fn delay_for_attempt_one_doubles() {
        let strategy = ExponentialBackoffRecovery::new(3);
        assert_eq!(strategy.delay_for_attempt(1), Duration::from_millis(200));
    }

    #[test]
    fn delay_for_attempt_two_quadruples() {
        let strategy = ExponentialBackoffRecovery::new(3);
        assert_eq!(strategy.delay_for_attempt(2), Duration::from_millis(400));
    }

    #[test]
    fn delay_for_attempt_capped_at_max() {
        let strategy = ExponentialBackoffRecovery::new(10)
            .with_base_delay(Duration::from_secs(1))
            .with_max_delay(Duration::from_secs(5));
        // 1 * 2^5 = 32s, capped at 5s
        assert_eq!(strategy.delay_for_attempt(5), Duration::from_secs(5));
    }

    #[test]
    fn delay_for_attempt_exactly_at_max() {
        let strategy = ExponentialBackoffRecovery::new(10)
            .with_base_delay(Duration::from_secs(1))
            .with_max_delay(Duration::from_secs(8));
        // 1 * 2^3 = 8s, exactly at cap
        assert_eq!(strategy.delay_for_attempt(3), Duration::from_secs(8));
    }

    #[test]
    fn delay_for_attempt_stays_capped_for_large_attempts() {
        let strategy = ExponentialBackoffRecovery::new(100)
            .with_base_delay(Duration::from_millis(100))
            .with_max_delay(Duration::from_secs(30));
        let late = strategy.delay_for_attempt(50);
        assert_eq!(late, Duration::from_secs(30));
    }

    #[test]
    fn delay_for_attempt_custom_base() {
        let strategy =
            ExponentialBackoffRecovery::new(5).with_base_delay(Duration::from_millis(250));
        assert_eq!(strategy.delay_for_attempt(0), Duration::from_millis(250));
        assert_eq!(strategy.delay_for_attempt(1), Duration::from_millis(500));
        assert_eq!(strategy.delay_for_attempt(2), Duration::from_secs(1));
    }

    #[test]
    fn delay_for_attempt_overflow_does_not_panic() {
        let strategy = ExponentialBackoffRecovery::new(3)
            .with_base_delay(Duration::from_secs(1))
            .with_max_delay(Duration::from_secs(10));
        // attempt = u32::MAX would overflow 2^n; should saturate, not panic
        let delay = strategy.delay_for_attempt(u32::MAX);
        assert_eq!(delay, Duration::from_secs(10));
    }
}