bob-core 0.2.2

Core domain types and port traits for Bob Agent Framework
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
//! # Resilience Patterns
//!
//! Circuit breaker and retry configuration for preventing cascading failures.
//!
//! The [`CircuitBreaker`] is a pure state machine that tracks call outcomes
//! and transitions between `Closed` → `Open` → `HalfOpen` → `Closed`.
//! Adapters wrap their port implementations with [`CircuitBreaker::call`]
//! to add automatic failure detection and recovery probing.
//!
//! [`RetryConfig`] provides exponential backoff parameters for adapters
//! that wish to retry transient failures before surfacing errors.

use std::{
    sync::Mutex,
    time::{Duration, Instant},
};

// ── Circuit Breaker ──────────────────────────────────────────────────

/// Circuit breaker states.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    /// Requests flow normally.
    Closed,
    /// Requests fail fast without calling the underlying service.
    Open,
    /// A single probe request is allowed to test recovery.
    HalfOpen,
}

/// Circuit breaker configuration.
#[derive(Debug, Clone)]
pub struct CircuitBreakerConfig {
    /// Number of consecutive failures before opening the circuit.
    pub failure_threshold: u32,
    /// Number of consecutive successes in `HalfOpen` before closing the circuit.
    pub success_threshold: u32,
    /// How long to wait in `Open` before transitioning to `HalfOpen`.
    pub cooldown: Duration,
}

impl Default for CircuitBreakerConfig {
    fn default() -> Self {
        Self { failure_threshold: 5, success_threshold: 2, cooldown: Duration::from_secs(30) }
    }
}

/// Inner state protected by a mutex.
#[derive(Debug)]
struct CircuitBreakerInner {
    state: CircuitState,
    failure_count: u32,
    success_count: u32,
    last_failure_at: Option<Instant>,
}

/// Circuit breaker for preventing cascading failures.
///
/// ## States
///
/// ```text
/// Closed ──(failures ≥ threshold)──▶ Open
///   ▲                                    │
///   │                                    │ cooldown elapsed
///   │                                    ▼
///   └──(successes ≥ threshold)── HalfOpen ◀┘
//////          (any failure) ──────────────▶ Open
/// ```
///
/// ## Example
///
/// ```rust,ignore
/// use bob_core::resilience::{CircuitBreaker, CircuitBreakerConfig};
/// use std::time::Duration;
///
/// let cb = CircuitBreaker::new(CircuitBreakerConfig {
///     failure_threshold: 3,
///     success_threshold: 1,
///     cooldown: Duration::from_secs(10),
/// });
///
/// let result = cb.call(|| async { Ok::<_, &str>(42) }).await;
/// assert_eq!(result, Ok(42));
/// ```
#[derive(Debug)]
pub struct CircuitBreaker {
    config: CircuitBreakerConfig,
    inner: Mutex<CircuitBreakerInner>,
}

impl CircuitBreaker {
    /// Create a new circuit breaker with the given configuration.
    #[must_use]
    pub fn new(config: CircuitBreakerConfig) -> Self {
        Self {
            config,
            inner: Mutex::new(CircuitBreakerInner {
                state: CircuitState::Closed,
                failure_count: 0,
                success_count: 0,
                last_failure_at: None,
            }),
        }
    }

    /// Returns the current circuit state.
    ///
    /// If the circuit is `Open` and the cooldown has elapsed, this
    /// transitions to `HalfOpen` before returning.
    pub fn state(&self) -> CircuitState {
        let mut inner = match self.inner.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        Self::maybe_transition_to_half_open(&mut inner, &self.config);
        inner.state
    }

    /// Execute an async operation through the circuit breaker.
    ///
    /// Returns `Err(CircuitBreakerError::CircuitOpen)` immediately if the
    /// circuit is open and the cooldown has not elapsed.
    pub async fn call<F, Fut, T, E>(&self, f: F) -> Result<T, CircuitBreakerError<E>>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T, E>>,
    {
        // Pre-check: is the circuit open?
        {
            let mut inner = match self.inner.lock() {
                Ok(guard) => guard,
                Err(poisoned) => poisoned.into_inner(),
            };
            Self::maybe_transition_to_half_open(&mut inner, &self.config);
            if inner.state == CircuitState::Open {
                return Err(CircuitBreakerError::CircuitOpen);
            }
        }

        // Execute the operation.
        match f().await {
            Ok(value) => {
                self.on_success();
                Ok(value)
            }
            Err(err) => {
                self.on_failure();
                Err(CircuitBreakerError::Inner(err))
            }
        }
    }

    /// Manually reset the circuit breaker to the closed state.
    pub fn reset(&self) {
        let mut inner = match self.inner.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        inner.state = CircuitState::Closed;
        inner.failure_count = 0;
        inner.success_count = 0;
        inner.last_failure_at = None;
    }

    fn maybe_transition_to_half_open(
        inner: &mut CircuitBreakerInner,
        config: &CircuitBreakerConfig,
    ) {
        if inner.state == CircuitState::Open &&
            let Some(last_failure) = inner.last_failure_at &&
            last_failure.elapsed() >= config.cooldown
        {
            inner.state = CircuitState::HalfOpen;
            inner.success_count = 0;
        }
    }

    fn on_success(&self) {
        let mut inner = match self.inner.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        match inner.state {
            CircuitState::HalfOpen => {
                inner.success_count = inner.success_count.saturating_add(1);
                if inner.success_count >= self.config.success_threshold {
                    inner.state = CircuitState::Closed;
                    inner.failure_count = 0;
                    inner.success_count = 0;
                }
            }
            CircuitState::Closed => {
                inner.failure_count = 0;
            }
            CircuitState::Open => {}
        }
    }

    fn on_failure(&self) {
        let mut inner = match self.inner.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        match inner.state {
            CircuitState::Closed | CircuitState::HalfOpen => {
                inner.failure_count = inner.failure_count.saturating_add(1);
                if inner.failure_count >= self.config.failure_threshold {
                    inner.state = CircuitState::Open;
                    inner.last_failure_at = Some(Instant::now());
                }
            }
            CircuitState::Open => {}
        }
    }
}

// ── Circuit Breaker Error ────────────────────────────────────────────

/// Error returned by [`CircuitBreaker::call`].
pub enum CircuitBreakerError<E> {
    /// The circuit is open; the call was not attempted.
    CircuitOpen,
    /// The underlying operation returned an error.
    Inner(E),
}

impl<E> std::fmt::Debug for CircuitBreakerError<E>
where
    E: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CircuitOpen => f.write_str("CircuitOpen"),
            Self::Inner(e) => f.debug_tuple("Inner").field(e).finish(),
        }
    }
}

impl<E: std::fmt::Display> std::fmt::Display for CircuitBreakerError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CircuitOpen => f.write_str("circuit breaker is open"),
            Self::Inner(e) => write!(f, "{e}"),
        }
    }
}

impl<E: std::fmt::Debug + std::fmt::Display> std::error::Error for CircuitBreakerError<E> {}

impl<E> CircuitBreakerError<E> {
    /// Stable error code.
    #[must_use]
    pub fn code(&self) -> &'static str {
        match self {
            Self::CircuitOpen => "BOB_CIRCUIT_OPEN",
            Self::Inner(_) => "BOB_CIRCUIT_INNER",
        }
    }
}

// ── Retry Config ─────────────────────────────────────────────────────

/// Exponential backoff retry parameters.
///
/// Adapters consume this configuration to drive retry loops.
/// The struct itself is pure data; the actual `sleep` call
/// is left to the adapter (which has access to `tokio::time`).
#[derive(Debug, Clone)]
pub struct RetryConfig {
    /// Maximum number of retry attempts after the first failure.
    pub max_retries: u32,
    /// Initial delay between attempts.
    pub initial_delay: Duration,
    /// Maximum delay (caps the exponential growth).
    pub max_delay: Duration,
    /// Multiplier applied to the delay after each failure.
    pub multiplier: f64,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            initial_delay: Duration::from_millis(200),
            max_delay: Duration::from_secs(10),
            multiplier: 2.0,
        }
    }
}

impl RetryConfig {
    /// Compute the delay for a given attempt number (0-indexed).
    #[must_use]
    pub fn delay_for_attempt(&self, attempt: u32) -> Duration {
        let base = self.initial_delay.as_millis() as f64;
        let scaled = base * self.multiplier.powi(attempt as i32);
        let capped = scaled.min(self.max_delay.as_millis() as f64);
        Duration::from_millis(capped as u64)
    }
}

// ── Health Check ─────────────────────────────────────────────────────

/// Simple health status for a single component.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HealthStatus {
    /// Component is operating normally.
    Healthy,
    /// Component is operational but degraded (e.g. high latency).
    Degraded,
    /// Component is not operational.
    Unhealthy,
}

/// A health check result for one component.
#[derive(Debug, Clone)]
pub struct ComponentHealth {
    /// Component name (e.g. `"llm:openai"`, `"mcp:filesystem"`).
    pub name: String,
    /// Current health status.
    pub status: HealthStatus,
    /// Optional human-readable detail.
    pub detail: Option<String>,
}

// ── Tests ────────────────────────────────────────────────────────────

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

    #[tokio::test]
    async fn circuit_breaker_allows_calls_when_closed() {
        let cb = CircuitBreaker::new(CircuitBreakerConfig::default());
        assert_eq!(cb.state(), CircuitState::Closed);

        let result = cb.call(|| async { Ok::<_, &str>(42) }).await;
        assert!(result.is_ok());
        assert_eq!(result.expect("should succeed"), 42);
        assert_eq!(cb.state(), CircuitState::Closed);
    }

    #[tokio::test]
    async fn circuit_breaker_opens_after_threshold() {
        let cb = CircuitBreaker::new(CircuitBreakerConfig {
            failure_threshold: 3,
            success_threshold: 1,
            cooldown: Duration::from_secs(60),
        });

        for _ in 0..3 {
            let _ = cb.call(|| async { Err::<(), _>("fail") }).await;
        }

        assert_eq!(cb.state(), CircuitState::Open);

        let result = cb.call(|| async { Ok::<_, &str>(42) }).await;
        assert!(matches!(result, Err(CircuitBreakerError::CircuitOpen)));
    }

    #[tokio::test]
    async fn circuit_breaker_half_open_after_cooldown() {
        let cb = CircuitBreaker::new(CircuitBreakerConfig {
            failure_threshold: 1,
            success_threshold: 1,
            cooldown: Duration::from_millis(50),
        });

        let _ = cb.call(|| async { Err::<(), _>("fail") }).await;
        assert_eq!(cb.state(), CircuitState::Open);

        tokio::time::sleep(Duration::from_millis(80)).await;
        assert_eq!(cb.state(), CircuitState::HalfOpen);
    }

    #[tokio::test]
    async fn circuit_breaker_closes_after_success_in_half_open() {
        let cb = CircuitBreaker::new(CircuitBreakerConfig {
            failure_threshold: 1,
            success_threshold: 1,
            cooldown: Duration::from_millis(10),
        });

        let _ = cb.call(|| async { Err::<(), _>("fail") }).await;
        tokio::time::sleep(Duration::from_millis(20)).await;

        let result = cb.call(|| async { Ok::<_, &str>(42) }).await;
        assert!(result.is_ok());
        assert_eq!(result.expect("should succeed"), 42);
        assert_eq!(cb.state(), CircuitState::Closed);
    }

    #[tokio::test]
    async fn circuit_breaker_reopens_on_failure_in_half_open() {
        let cb = CircuitBreaker::new(CircuitBreakerConfig {
            failure_threshold: 1,
            success_threshold: 2,
            cooldown: Duration::from_millis(10),
        });

        let _ = cb.call(|| async { Err::<(), _>("fail") }).await;
        tokio::time::sleep(Duration::from_millis(20)).await;
        assert_eq!(cb.state(), CircuitState::HalfOpen);

        let _ = cb.call(|| async { Err::<(), _>("fail again") }).await;
        assert_eq!(cb.state(), CircuitState::Open);
    }

    #[tokio::test]
    async fn circuit_breaker_reset() {
        let cb = CircuitBreaker::new(CircuitBreakerConfig {
            failure_threshold: 1,
            success_threshold: 1,
            cooldown: Duration::from_secs(60),
        });

        let _ = cb.call(|| async { Err::<(), _>("fail") }).await;
        assert!(cb.call(|| async { Ok::<_, &str>(1) }).await.is_err());

        cb.reset();
        assert_eq!(cb.state(), CircuitState::Closed);
    }

    #[test]
    fn retry_config_delay_computation() {
        let config = RetryConfig {
            max_retries: 3,
            initial_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(5),
            multiplier: 2.0,
        };

        assert_eq!(config.delay_for_attempt(0), Duration::from_millis(100));
        assert_eq!(config.delay_for_attempt(1), Duration::from_millis(200));
        assert_eq!(config.delay_for_attempt(2), Duration::from_millis(400));
        assert_eq!(config.delay_for_attempt(10), Duration::from_secs(5)); // capped
    }

    #[test]
    fn retry_config_default() {
        let config = RetryConfig::default();
        assert_eq!(config.max_retries, 3);
        assert_eq!(config.delay_for_attempt(0), Duration::from_millis(200));
    }
}