cognis 0.2.0

LLM application framework built on cognis-core
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
//! Circuit breaker pattern for chat model calls.
//!
//! Provides a [`CircuitBreaker`] that tracks failures and prevents cascading
//! errors by short-circuiting requests when a failure threshold is exceeded.
//! Also provides [`CircuitBreakerChatModel`], a wrapper that applies circuit
//! breaker logic to any [`BaseChatModel`].

use std::future::Future;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use tokio::sync::Mutex;

use cognis_core::error::{CognisError, Result};
use cognis_core::language_models::chat_model::{
    BaseChatModel, ChatStream, ModelProfile, ToolChoice,
};
use cognis_core::messages::Message;
use cognis_core::outputs::ChatResult;
use cognis_core::tools::ToolSchema;

/// The operational state of a circuit breaker.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    /// Normal operation: requests pass through.
    Closed,
    /// Tripped: requests immediately fail without calling the inner service.
    Open,
    /// Testing recovery: one request is allowed through to test if the service
    /// has recovered.
    HalfOpen,
}

/// A circuit breaker that tracks consecutive failures and prevents cascading
/// errors by short-circuiting requests when a failure threshold is exceeded.
///
/// ## State Machine
///
/// ```text
/// ┌────────┐  failure >= threshold   ┌──────┐
/// │ Closed ├────────────────────────►│ Open │
/// └────┬───┘                         └──┬───┘
///      │                                │
///      │  success                       │ reset_timeout elapsed
///      │                                ▼
///      │                          ┌──────────┐
///      └──────────────────────────┤ HalfOpen │
///              success            └────┬─────┘
///                                     │ failure
//////                                  ┌──────┐
///                                  │ Open │
///                                  └──────┘
/// ```
pub struct CircuitBreaker {
    state: Arc<Mutex<CircuitState>>,
    failure_threshold: u32,
    reset_timeout: Duration,
    consecutive_failures: Arc<AtomicU32>,
    last_failure_time: Arc<Mutex<Option<Instant>>>,
}

impl CircuitBreaker {
    /// Create a new circuit breaker.
    ///
    /// # Arguments
    /// * `failure_threshold` - Number of consecutive failures before the circuit opens.
    /// * `reset_timeout` - Duration to wait in the open state before transitioning to half-open.
    pub fn new(failure_threshold: u32, reset_timeout: Duration) -> Self {
        Self {
            state: Arc::new(Mutex::new(CircuitState::Closed)),
            failure_threshold,
            reset_timeout,
            consecutive_failures: Arc::new(AtomicU32::new(0)),
            last_failure_time: Arc::new(Mutex::new(None)),
        }
    }

    /// Returns the current state of the circuit breaker.
    pub async fn state(&self) -> CircuitState {
        let mut state = self.state.lock().await;
        // Check if we should transition from Open to HalfOpen
        if *state == CircuitState::Open {
            let last_failure = self.last_failure_time.lock().await;
            if let Some(t) = *last_failure {
                if t.elapsed() >= self.reset_timeout {
                    *state = CircuitState::HalfOpen;
                }
            }
        }
        *state
    }

    /// Reset the circuit breaker to the closed state.
    pub async fn reset(&self) {
        let mut state = self.state.lock().await;
        *state = CircuitState::Closed;
        self.consecutive_failures.store(0, Ordering::SeqCst);
        let mut last = self.last_failure_time.lock().await;
        *last = None;
    }

    /// Execute a future through the circuit breaker.
    ///
    /// - If the circuit is **closed**, the future is executed normally.
    /// - If the circuit is **open** and the reset timeout has not elapsed,
    ///   an error is returned immediately.
    /// - If the circuit is **half-open**, one request is allowed through.
    ///   On success the circuit closes; on failure it re-opens.
    pub async fn call<F, Fut, T>(&self, f: F) -> Result<T>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = Result<T>>,
    {
        let current_state = self.state().await;

        match current_state {
            CircuitState::Open => Err(CognisError::Other(
                "Circuit breaker is open: too many consecutive failures".into(),
            )),
            CircuitState::HalfOpen | CircuitState::Closed => {
                match f().await {
                    Ok(result) => {
                        // Success: reset failures, close circuit
                        self.consecutive_failures.store(0, Ordering::SeqCst);
                        let mut state = self.state.lock().await;
                        *state = CircuitState::Closed;
                        Ok(result)
                    }
                    Err(e) => {
                        let failures = self.consecutive_failures.fetch_add(1, Ordering::SeqCst) + 1;
                        let mut last = self.last_failure_time.lock().await;
                        *last = Some(Instant::now());

                        if current_state == CircuitState::HalfOpen
                            || failures >= self.failure_threshold
                        {
                            let mut state = self.state.lock().await;
                            *state = CircuitState::Open;
                        }
                        Err(e)
                    }
                }
            }
        }
    }
}

/// A chat model wrapper that applies circuit breaker logic.
///
/// When the circuit is open, calls to `_generate` and `_stream` fail
/// immediately with a descriptive error instead of hitting the downstream
/// service.
pub struct CircuitBreakerChatModel {
    inner: Box<dyn BaseChatModel>,
    breaker: CircuitBreaker,
}

impl CircuitBreakerChatModel {
    /// Wrap a chat model with a circuit breaker.
    ///
    /// # Arguments
    /// * `inner` - The chat model to wrap.
    /// * `failure_threshold` - Consecutive failures before the circuit opens (default: 5).
    /// * `reset_timeout` - Time to wait before testing recovery (default: 60s).
    pub fn new(
        inner: Box<dyn BaseChatModel>,
        failure_threshold: u32,
        reset_timeout: Duration,
    ) -> Self {
        Self {
            inner,
            breaker: CircuitBreaker::new(failure_threshold, reset_timeout),
        }
    }

    /// Returns the current circuit state.
    pub async fn circuit_state(&self) -> CircuitState {
        self.breaker.state().await
    }

    /// Manually reset the circuit breaker.
    pub async fn reset(&self) {
        self.breaker.reset().await;
    }
}

#[async_trait]
impl BaseChatModel for CircuitBreakerChatModel {
    async fn _generate(&self, messages: &[Message], stop: Option<&[String]>) -> Result<ChatResult> {
        // We need to capture references for the closure
        let inner = &self.inner;
        self.breaker
            .call(|| async move { inner._generate(messages, stop).await })
            .await
    }

    fn llm_type(&self) -> &str {
        self.inner.llm_type()
    }

    async fn _stream(&self, messages: &[Message], stop: Option<&[String]>) -> Result<ChatStream> {
        let inner = &self.inner;
        self.breaker
            .call(|| async move { inner._stream(messages, stop).await })
            .await
    }

    fn bind_tools(
        &self,
        tools: &[ToolSchema],
        tool_choice: Option<ToolChoice>,
    ) -> Result<Box<dyn BaseChatModel>> {
        self.inner.bind_tools(tools, tool_choice)
    }

    fn profile(&self) -> ModelProfile {
        self.inner.profile()
    }

    fn get_num_tokens_from_messages(&self, messages: &[Message]) -> usize {
        self.inner.get_num_tokens_from_messages(messages)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use cognis_core::messages::{AIMessage, HumanMessage};
    use cognis_core::outputs::ChatGeneration;
    use std::sync::atomic::AtomicU32;

    /// A mock chat model that fails a configurable number of times then succeeds.
    struct MockChatModel {
        fail_count: u32,
        attempts: AtomicU32,
    }

    impl MockChatModel {
        fn always_fails() -> Self {
            Self {
                fail_count: u32::MAX,
                attempts: AtomicU32::new(0),
            }
        }

        fn fails_n_times(n: u32) -> Self {
            Self {
                fail_count: n,
                attempts: AtomicU32::new(0),
            }
        }
    }

    #[async_trait]
    impl BaseChatModel for MockChatModel {
        async fn _generate(
            &self,
            _messages: &[Message],
            _stop: Option<&[String]>,
        ) -> Result<ChatResult> {
            let attempt = self.attempts.fetch_add(1, Ordering::SeqCst);
            if attempt < self.fail_count {
                Err(CognisError::HttpError {
                    status: 500,
                    body: "Internal Server Error".into(),
                })
            } else {
                Ok(ChatResult {
                    generations: vec![ChatGeneration {
                        text: "OK".into(),
                        message: Message::Ai(AIMessage::new("OK")),
                        generation_info: None,
                    }],
                    llm_output: None,
                })
            }
        }

        fn llm_type(&self) -> &str {
            "mock"
        }
    }

    #[tokio::test]
    async fn test_circuit_breaker_closed_on_success() {
        let model = CircuitBreakerChatModel::new(
            Box::new(MockChatModel::fails_n_times(0)),
            3,
            Duration::from_secs(60),
        );

        let msgs = vec![Message::Human(HumanMessage::new("hi"))];
        let result = model._generate(&msgs, None).await;
        assert!(result.is_ok());
        assert_eq!(model.circuit_state().await, CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_opens_after_threshold() {
        let model = CircuitBreakerChatModel::new(
            Box::new(MockChatModel::always_fails()),
            3,
            Duration::from_secs(60),
        );

        let msgs = vec![Message::Human(HumanMessage::new("hi"))];

        // Fail 3 times to trip the breaker
        for _ in 0..3 {
            let _ = model._generate(&msgs, None).await;
        }

        assert_eq!(model.circuit_state().await, CircuitState::Open);

        // Next call should fail immediately with circuit breaker error
        let result = model._generate(&msgs, None).await;
        assert!(result.is_err());
        let err = format!("{}", result.unwrap_err());
        assert!(
            err.contains("Circuit breaker is open"),
            "Expected circuit breaker error, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_circuit_breaker_half_open_to_closed() {
        let model = CircuitBreakerChatModel::new(
            Box::new(MockChatModel::fails_n_times(3)), // fails 3 times, then succeeds
            3,
            Duration::from_millis(50), // short timeout for test
        );

        let msgs = vec![Message::Human(HumanMessage::new("hi"))];

        // Trip the breaker
        for _ in 0..3 {
            let _ = model._generate(&msgs, None).await;
        }
        assert_eq!(model.circuit_state().await, CircuitState::Open);

        // Wait for reset timeout
        tokio::time::sleep(Duration::from_millis(60)).await;

        // Should be half-open now
        assert_eq!(model.circuit_state().await, CircuitState::HalfOpen);

        // Successful call should close the circuit
        let result = model._generate(&msgs, None).await;
        assert!(result.is_ok());
        assert_eq!(model.circuit_state().await, CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_half_open_to_open() {
        let model = CircuitBreakerChatModel::new(
            Box::new(MockChatModel::always_fails()),
            3,
            Duration::from_millis(50),
        );

        let msgs = vec![Message::Human(HumanMessage::new("hi"))];

        // Trip the breaker
        for _ in 0..3 {
            let _ = model._generate(&msgs, None).await;
        }
        assert_eq!(model.circuit_state().await, CircuitState::Open);

        // Wait for reset timeout
        tokio::time::sleep(Duration::from_millis(60)).await;
        assert_eq!(model.circuit_state().await, CircuitState::HalfOpen);

        // Failure in half-open should re-open the circuit
        let result = model._generate(&msgs, None).await;
        assert!(result.is_err());
        assert_eq!(model.circuit_state().await, CircuitState::Open);
    }

    #[tokio::test]
    async fn test_circuit_breaker_reset() {
        let model = CircuitBreakerChatModel::new(
            Box::new(MockChatModel::always_fails()),
            2,
            Duration::from_secs(60),
        );

        let msgs = vec![Message::Human(HumanMessage::new("hi"))];

        // Trip the breaker
        for _ in 0..2 {
            let _ = model._generate(&msgs, None).await;
        }
        assert_eq!(model.circuit_state().await, CircuitState::Open);

        // Manual reset
        model.reset().await;
        assert_eq!(model.circuit_state().await, CircuitState::Closed);
    }
}