zoey-core 0.1.1

ZoeyAI core runtime and types — privacy-first AI agent framework optimized for local models
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
//! Resilience patterns: Circuit breakers, health checks, retry logic

use crate::{ZoeyError, Result};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use tracing::{debug, error, warn};

/// Circuit breaker states
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    /// Circuit is closed, requests flow normally
    Closed,
    /// Circuit is open, requests fail fast
    Open,
    /// Circuit is half-open, testing if service recovered
    HalfOpen,
}

/// Circuit breaker for preventing cascading failures
pub struct CircuitBreaker {
    state: Arc<RwLock<CircuitState>>,
    failure_threshold: usize,
    success_threshold: usize,
    timeout: Duration,
    failure_count: Arc<RwLock<usize>>,
    success_count: Arc<RwLock<usize>>,
    last_failure_time: Arc<RwLock<Option<Instant>>>,
}

impl CircuitBreaker {
    /// Create a new circuit breaker
    pub fn new(failure_threshold: usize, success_threshold: usize, timeout: Duration) -> Self {
        Self {
            state: Arc::new(RwLock::new(CircuitState::Closed)),
            failure_threshold,
            success_threshold,
            timeout,
            failure_count: Arc::new(RwLock::new(0)),
            success_count: Arc::new(RwLock::new(0)),
            last_failure_time: Arc::new(RwLock::new(None)),
        }
    }

    /// Execute a function through the circuit breaker
    pub async fn call<F, T, E>(&self, f: F) -> Result<T>
    where
        F: std::future::Future<Output = std::result::Result<T, E>>,
        E: std::fmt::Display,
    {
        // Check if circuit is open
        {
            let state = *self.state.read().unwrap();
            if state == CircuitState::Open {
                // Check if timeout has elapsed
                if let Some(last_failure) = *self.last_failure_time.read().unwrap() {
                    if last_failure.elapsed() >= self.timeout {
                        // Transition to half-open
                        *self.state.write().unwrap() = CircuitState::HalfOpen;
                        *self.success_count.write().unwrap() = 0;
                        debug!("Circuit breaker transitioning to half-open");
                    } else {
                        return Err(ZoeyError::other("Circuit breaker is open"));
                    }
                }
            }
        }

        // Execute the function
        match f.await {
            Ok(result) => {
                self.on_success();
                Ok(result)
            }
            Err(e) => {
                self.on_failure();
                Err(ZoeyError::other(e.to_string()))
            }
        }
    }

    /// Handle successful call
    fn on_success(&self) {
        let state = *self.state.read().unwrap();

        match state {
            CircuitState::HalfOpen => {
                let mut success_count = self.success_count.write().unwrap();
                *success_count += 1;

                if *success_count >= self.success_threshold {
                    *self.state.write().unwrap() = CircuitState::Closed;
                    *self.failure_count.write().unwrap() = 0;
                    debug!("Circuit breaker closed");
                }
            }
            CircuitState::Closed => {
                *self.failure_count.write().unwrap() = 0;
            }
            CircuitState::Open => {}
        }
    }

    /// Handle failed call
    fn on_failure(&self) {
        let state = *self.state.read().unwrap();

        match state {
            CircuitState::Closed | CircuitState::HalfOpen => {
                let mut failure_count = self.failure_count.write().unwrap();
                *failure_count += 1;

                if *failure_count >= self.failure_threshold {
                    *self.state.write().unwrap() = CircuitState::Open;
                    *self.last_failure_time.write().unwrap() = Some(Instant::now());
                    warn!("Circuit breaker opened after {} failures", failure_count);
                }
            }
            CircuitState::Open => {}
        }
    }

    /// Get current state
    pub fn state(&self) -> CircuitState {
        *self.state.read().unwrap()
    }

    /// Reset the circuit breaker
    pub fn reset(&self) {
        *self.state.write().unwrap() = CircuitState::Closed;
        *self.failure_count.write().unwrap() = 0;
        *self.success_count.write().unwrap() = 0;
        *self.last_failure_time.write().unwrap() = None;
        debug!("Circuit breaker reset");
    }
}

/// Health check result
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum HealthStatus {
    /// Service is healthy
    Healthy,
    /// Service is degraded but operational
    Degraded,
    /// Service is unhealthy
    Unhealthy,
}

/// Health check information
#[derive(Debug, Clone)]
pub struct HealthCheck {
    /// Component name
    pub name: String,
    /// Health status
    pub status: HealthStatus,
    /// Optional message
    pub message: Option<String>,
    /// Last check timestamp
    pub last_check: Instant,
    /// Response time in milliseconds
    pub response_time_ms: u64,
}

/// Health checker for monitoring system health
pub struct HealthChecker {
    checks: Arc<RwLock<HashMap<String, HealthCheck>>>,
}

impl HealthChecker {
    /// Create a new health checker
    pub fn new() -> Self {
        Self {
            checks: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Register a health check
    pub async fn check<F, T, E>(&self, name: &str, f: F) -> HealthStatus
    where
        F: std::future::Future<Output = std::result::Result<T, E>>,
        E: std::fmt::Display,
    {
        let start = Instant::now();

        let status = match f.await {
            Ok(_) => HealthStatus::Healthy,
            Err(e) => {
                error!("Health check failed for {}: {}", name, e);
                HealthStatus::Unhealthy
            }
        };

        let response_time_ms = start.elapsed().as_millis() as u64;

        // Determine degraded status based on response time
        let final_status = if status == HealthStatus::Healthy && response_time_ms > 1000 {
            HealthStatus::Degraded
        } else {
            status
        };

        let check = HealthCheck {
            name: name.to_string(),
            status: final_status,
            message: None,
            last_check: Instant::now(),
            response_time_ms,
        };

        self.checks.write().unwrap().insert(name.to_string(), check);

        final_status
    }

    /// Get overall health status
    pub fn overall_health(&self) -> HealthStatus {
        let checks = self.checks.read().unwrap();

        if checks.is_empty() {
            return HealthStatus::Healthy;
        }

        let mut has_unhealthy = false;
        let mut has_degraded = false;

        for check in checks.values() {
            match check.status {
                HealthStatus::Unhealthy => has_unhealthy = true,
                HealthStatus::Degraded => has_degraded = true,
                HealthStatus::Healthy => {}
            }
        }

        if has_unhealthy {
            HealthStatus::Unhealthy
        } else if has_degraded {
            HealthStatus::Degraded
        } else {
            HealthStatus::Healthy
        }
    }

    /// Get all health checks
    pub fn get_all_checks(&self) -> Vec<HealthCheck> {
        self.checks.read().unwrap().values().cloned().collect()
    }

    /// Get specific health check
    pub fn get_check(&self, name: &str) -> Option<HealthCheck> {
        self.checks.read().unwrap().get(name).cloned()
    }
}

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

/// Retry configuration
#[derive(Debug, Clone)]
pub struct RetryConfig {
    /// Maximum number of retries
    pub max_retries: usize,
    /// Initial delay between retries
    pub initial_delay: Duration,
    /// Maximum delay between retries
    pub max_delay: Duration,
    /// Backoff multiplier
    pub multiplier: f64,
}

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

/// Execute a function with retry logic
pub async fn retry_with_backoff<F, T, E>(config: RetryConfig, mut f: F) -> Result<T>
where
    F: FnMut() -> std::pin::Pin<
        Box<dyn std::future::Future<Output = std::result::Result<T, E>> + Send>,
    >,
    E: std::fmt::Display,
{
    let mut attempt = 0;
    let mut delay = config.initial_delay;

    loop {
        match f().await {
            Ok(result) => return Ok(result),
            Err(e) => {
                attempt += 1;

                if attempt > config.max_retries {
                    error!("All {} retry attempts failed", config.max_retries);
                    return Err(ZoeyError::other(format!(
                        "Retry failed after {} attempts: {}",
                        config.max_retries, e
                    )));
                }

                warn!("Attempt {} failed: {}. Retrying in {:?}", attempt, e, delay);
                tokio::time::sleep(delay).await;

                // Exponential backoff
                delay = Duration::from_millis(
                    ((delay.as_millis() as f64) * config.multiplier)
                        .min(config.max_delay.as_millis() as f64) as u64,
                );
            }
        }
    }
}

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

    #[tokio::test]
    async fn test_circuit_breaker_closed() {
        let cb = CircuitBreaker::new(3, 2, Duration::from_secs(5));
        assert_eq!(cb.state(), CircuitState::Closed);

        // Successful call
        let result = cb.call(async { Ok::<_, String>(42) }).await;
        assert!(result.is_ok());
        assert_eq!(cb.state(), CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_opens() {
        let cb = CircuitBreaker::new(3, 2, Duration::from_secs(5));

        // Three failures should open the circuit
        for _ in 0..3 {
            let _ = cb.call(async { Err::<(), _>("error") }).await;
        }

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

        // Next call should fail fast
        let result = cb.call(async { Ok::<_, String>(42) }).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_health_checker() {
        let checker = HealthChecker::new();

        // Healthy check
        let status = checker
            .check("test_service", async { Ok::<_, String>(()) })
            .await;
        assert_eq!(status, HealthStatus::Healthy);

        // Unhealthy check
        let status = checker
            .check("failing_service", async { Err::<(), _>("error") })
            .await;
        assert_eq!(status, HealthStatus::Unhealthy);

        // Overall health should be unhealthy
        assert_eq!(checker.overall_health(), HealthStatus::Unhealthy);
    }

    #[tokio::test]
    async fn test_retry_success() {
        let config = RetryConfig {
            max_retries: 3,
            initial_delay: Duration::from_millis(10),
            max_delay: Duration::from_millis(100),
            multiplier: 2.0,
        };

        let mut attempts = 0;
        let result = retry_with_backoff(config, || {
            attempts += 1;
            Box::pin(async move {
                if attempts < 2 {
                    Err("not yet")
                } else {
                    Ok(42)
                }
            })
        })
        .await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 42);
    }

    #[tokio::test]
    async fn test_retry_failure() {
        let config = RetryConfig {
            max_retries: 2,
            initial_delay: Duration::from_millis(10),
            max_delay: Duration::from_millis(100),
            multiplier: 2.0,
        };

        let result =
            retry_with_backoff(config, || Box::pin(async { Err::<(), _>("always fails") })).await;

        assert!(result.is_err());
    }
}