kaccy-ai 0.2.0

AI-powered intelligence for Kaccy Protocol - forecasting, optimization, and insights
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
//! Circuit breaker pattern for external API resilience
//!
//! Prevents cascading failures by temporarily blocking requests to failing services.

use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

use crate::error::{AiError, Result};

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

/// Circuit breaker configuration
#[derive(Debug, Clone)]
pub struct CircuitBreakerConfig {
    /// Number of failures before opening the circuit
    pub failure_threshold: u32,
    /// How long to keep circuit open before trying again
    pub timeout: Duration,
    /// How many successful requests needed to close circuit from half-open
    pub success_threshold: u32,
    /// Time window for counting failures
    pub failure_window: Duration,
}

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

impl CircuitBreakerConfig {
    /// Create a new circuit breaker config
    #[must_use]
    pub fn new(failure_threshold: u32, timeout: Duration) -> Self {
        Self {
            failure_threshold,
            timeout,
            ..Default::default()
        }
    }

    /// Set success threshold
    #[must_use]
    pub fn with_success_threshold(mut self, threshold: u32) -> Self {
        self.success_threshold = threshold;
        self
    }

    /// Set failure window
    #[must_use]
    pub fn with_failure_window(mut self, window: Duration) -> Self {
        self.failure_window = window;
        self
    }
}

/// Internal state tracking for circuit breaker
#[derive(Debug)]
struct CircuitBreakerState {
    /// Current state
    state: CircuitState,
    /// Number of consecutive failures
    failure_count: u32,
    /// Number of consecutive successes (in half-open state)
    success_count: u32,
    /// When the circuit was opened
    opened_at: Option<Instant>,
    /// Recent failure timestamps
    recent_failures: Vec<Instant>,
}

impl CircuitBreakerState {
    fn new() -> Self {
        Self {
            state: CircuitState::Closed,
            failure_count: 0,
            success_count: 0,
            opened_at: None,
            recent_failures: Vec::new(),
        }
    }

    /// Clean up old failures outside the window
    fn clean_old_failures(&mut self, window: Duration) {
        let cutoff = Instant::now().checked_sub(window).unwrap();
        self.recent_failures.retain(|&t| t > cutoff);
    }
}

/// Circuit breaker for protecting against cascading failures
pub struct CircuitBreaker {
    config: CircuitBreakerConfig,
    state: Arc<RwLock<CircuitBreakerState>>,
    name: String,
}

impl CircuitBreaker {
    /// Create a new circuit breaker
    pub fn new(name: impl Into<String>, config: CircuitBreakerConfig) -> Self {
        Self {
            config,
            state: Arc::new(RwLock::new(CircuitBreakerState::new())),
            name: name.into(),
        }
    }

    /// Get current circuit state
    pub async fn state(&self) -> CircuitState {
        let state = self.state.read().await;
        state.state
    }

    /// Execute an operation through the circuit breaker
    pub async fn call<F, Fut, T>(&self, operation: F) -> Result<T>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        // Check if circuit should transition to half-open
        self.check_timeout().await;

        // Check current state
        let current_state = {
            let state = self.state.read().await;
            state.state
        };

        match current_state {
            CircuitState::Open => {
                tracing::warn!(
                    circuit = %self.name,
                    "Circuit breaker is open, rejecting request"
                );
                Err(AiError::ServiceUnavailable)
            }
            CircuitState::Closed | CircuitState::HalfOpen => {
                // Try the operation
                match operation().await {
                    Ok(result) => {
                        self.on_success().await;
                        Ok(result)
                    }
                    Err(err) => {
                        self.on_failure().await;
                        Err(err)
                    }
                }
            }
        }
    }

    /// Check if circuit should transition from open to half-open
    async fn check_timeout(&self) {
        let mut state = self.state.write().await;

        if state.state == CircuitState::Open {
            if let Some(opened_at) = state.opened_at {
                if opened_at.elapsed() >= self.config.timeout {
                    tracing::info!(
                        circuit = %self.name,
                        "Circuit breaker timeout elapsed, transitioning to half-open"
                    );
                    state.state = CircuitState::HalfOpen;
                    state.success_count = 0;
                }
            }
        }
    }

    /// Handle successful operation
    async fn on_success(&self) {
        let mut state = self.state.write().await;

        match state.state {
            CircuitState::HalfOpen => {
                state.success_count += 1;
                if state.success_count >= self.config.success_threshold {
                    tracing::info!(
                        circuit = %self.name,
                        "Circuit breaker closing after {} successful requests",
                        state.success_count
                    );
                    state.state = CircuitState::Closed;
                    state.failure_count = 0;
                    state.success_count = 0;
                    state.recent_failures.clear();
                    state.opened_at = None;
                }
            }
            CircuitState::Closed => {
                // Reset failure count on success
                state.failure_count = 0;
            }
            CircuitState::Open => {
                // Shouldn't happen, but reset if it does
                state.failure_count = 0;
            }
        }
    }

    /// Handle failed operation
    async fn on_failure(&self) {
        let mut state = self.state.write().await;

        state.recent_failures.push(Instant::now());
        state.clean_old_failures(self.config.failure_window);

        match state.state {
            CircuitState::HalfOpen => {
                // Any failure in half-open state reopens the circuit
                tracing::warn!(
                    circuit = %self.name,
                    "Circuit breaker reopening after failure in half-open state"
                );
                state.state = CircuitState::Open;
                state.opened_at = Some(Instant::now());
                state.success_count = 0;
            }
            CircuitState::Closed => {
                state.failure_count += 1;

                // Check if we should open the circuit
                if state.recent_failures.len() >= self.config.failure_threshold as usize {
                    tracing::warn!(
                        circuit = %self.name,
                        failures = state.recent_failures.len(),
                        threshold = self.config.failure_threshold,
                        "Circuit breaker opening due to failure threshold"
                    );
                    state.state = CircuitState::Open;
                    state.opened_at = Some(Instant::now());
                }
            }
            CircuitState::Open => {
                // Already open, just log
                tracing::debug!(
                    circuit = %self.name,
                    "Additional failure while circuit is open"
                );
            }
        }
    }

    /// Manually reset the circuit breaker
    pub async fn reset(&self) {
        let mut state = self.state.write().await;
        tracing::info!(circuit = %self.name, "Manually resetting circuit breaker");
        state.state = CircuitState::Closed;
        state.failure_count = 0;
        state.success_count = 0;
        state.recent_failures.clear();
        state.opened_at = None;
    }

    /// Get circuit breaker metrics
    pub async fn metrics(&self) -> CircuitBreakerMetrics {
        let state = self.state.read().await;
        CircuitBreakerMetrics {
            state: state.state,
            failure_count: state.failure_count,
            success_count: state.success_count,
            recent_failure_count: state.recent_failures.len() as u32,
        }
    }
}

/// Circuit breaker metrics
#[derive(Debug, Clone)]
pub struct CircuitBreakerMetrics {
    /// Current state
    pub state: CircuitState,
    /// Consecutive failures
    pub failure_count: u32,
    /// Consecutive successes (in half-open)
    pub success_count: u32,
    /// Recent failures in window
    pub recent_failure_count: u32,
}

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

    #[tokio::test]
    async fn test_circuit_breaker_closed_state() {
        let config = CircuitBreakerConfig::new(3, Duration::from_secs(1));
        let cb = CircuitBreaker::new("test", config);

        // Initially closed
        assert_eq!(cb.state().await, CircuitState::Closed);

        // Successful calls should keep it closed
        let result = cb.call(|| async { Ok::<_, AiError>(42) }).await;
        assert!(result.is_ok());
        assert_eq!(cb.state().await, CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_opens_on_failures() {
        let config = CircuitBreakerConfig::new(3, Duration::from_secs(1))
            .with_failure_window(Duration::from_secs(10));
        let cb = CircuitBreaker::new("test", config);

        // Trigger failures to open circuit
        for _ in 0..3 {
            let _ = cb
                .call(|| async { Err::<i32, _>(AiError::ServiceUnavailable) })
                .await;
        }

        // Circuit should be open
        assert_eq!(cb.state().await, CircuitState::Open);

        // Requests should be rejected
        let result = cb.call(|| async { Ok::<_, AiError>(42) }).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_circuit_breaker_half_open_transition() {
        let config =
            CircuitBreakerConfig::new(2, Duration::from_millis(100)).with_success_threshold(2);
        let cb = CircuitBreaker::new("test", config);

        // Open the circuit
        for _ in 0..2 {
            let _ = cb
                .call(|| async { Err::<i32, _>(AiError::ServiceUnavailable) })
                .await;
        }

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

        // Wait for timeout
        sleep(Duration::from_millis(150)).await;

        // Next call should transition to half-open and succeed
        let result = cb.call(|| async { Ok::<_, AiError>(42) }).await;
        assert!(result.is_ok());

        // Still half-open, need one more success
        let metrics = cb.metrics().await;
        assert_eq!(metrics.success_count, 1);

        // One more success should close the circuit
        let result = cb.call(|| async { Ok::<_, AiError>(42) }).await;
        assert!(result.is_ok());
        assert_eq!(cb.state().await, CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_half_open_reopens_on_failure() {
        let config = CircuitBreakerConfig::new(2, Duration::from_millis(100));
        let cb = CircuitBreaker::new("test", config);

        // Open the circuit
        for _ in 0..2 {
            let _ = cb
                .call(|| async { Err::<i32, _>(AiError::ServiceUnavailable) })
                .await;
        }

        // Wait for timeout
        sleep(Duration::from_millis(150)).await;

        // Failure in half-open should reopen
        let _ = cb
            .call(|| async { Err::<i32, _>(AiError::ServiceUnavailable) })
            .await;

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

    #[tokio::test]
    async fn test_circuit_breaker_reset() {
        let config = CircuitBreakerConfig::new(2, Duration::from_secs(10));
        let cb = CircuitBreaker::new("test", config);

        // Open the circuit
        for _ in 0..2 {
            let _ = cb
                .call(|| async { Err::<i32, _>(AiError::ServiceUnavailable) })
                .await;
        }

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

        // Manual reset should close it
        cb.reset().await;
        assert_eq!(cb.state().await, CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_failure_window() {
        let config = CircuitBreakerConfig::new(3, Duration::from_secs(1))
            .with_failure_window(Duration::from_millis(200));
        let cb = CircuitBreaker::new("test", config);

        // Two failures
        for _ in 0..2 {
            let _ = cb
                .call(|| async { Err::<i32, _>(AiError::ServiceUnavailable) })
                .await;
        }

        // Wait for failures to expire
        sleep(Duration::from_millis(250)).await;

        // Circuit should still be closed since failures expired
        assert_eq!(cb.state().await, CircuitState::Closed);

        // One more failure shouldn't open it (old failures expired)
        let _ = cb
            .call(|| async { Err::<i32, _>(AiError::ServiceUnavailable) })
            .await;

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