cognis-llm 0.3.1

LLM client and provider abstractions for Cognis: Client, LLMProvider trait, chat options, tool definitions, and streaming. Provider implementations (OpenAI, Anthropic, Google, Ollama, Azure) are feature-gated.
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
//! Circuit breaker — drops calls to an unhealthy provider until it has
//! a chance to recover.
//!
//! State transitions:
//!
//! - **Closed** (normal): every call passes through. Consecutive
//!   failures are counted; after `failure_threshold` the breaker opens.
//! - **Open**: every call is rejected immediately with a
//!   [`CognisError::Configuration`]. After `cooldown` elapses, the
//!   breaker transitions to half-open on the next call.
//! - **HalfOpen**: a single probe call is allowed through. Success
//!   closes the breaker; failure re-opens it.
//!
//! Customization:
//! - [`FailureClassifier`] — pluggable predicate that decides which
//!   errors count toward the failure budget. Defaults to "all errors
//!   count".
//! - [`RetryableClassifier`] — only retryable errors trip the breaker
//!   (network/transient errors, rate limits). Useful when you don't
//!   want a `Configuration`-class error to pop the breaker.

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

use async_trait::async_trait;

use cognis_core::{CognisError, Result, RunnableStream};

use crate::chat::{ChatOptions, ChatResponse, HealthStatus, StreamChunk};
use crate::provider::{LLMProvider, Provider};
use crate::tools::ToolDefinition;
use crate::Message;

/// Decides whether a given error counts as a failure for the circuit.
pub trait FailureClassifier: Send + Sync {
    /// True if `err` should increment the failure counter.
    fn is_failure(&self, err: &CognisError) -> bool;
}

/// Default classifier — every error counts.
#[derive(Debug, Default, Clone, Copy)]
pub struct AllErrorsAreFailures;

impl FailureClassifier for AllErrorsAreFailures {
    fn is_failure(&self, _err: &CognisError) -> bool {
        true
    }
}

/// Only retryable errors (network/transient/rate-limited) count toward
/// the failure budget.
#[derive(Debug, Default, Clone, Copy)]
pub struct RetryableClassifier;

impl FailureClassifier for RetryableClassifier {
    fn is_failure(&self, err: &CognisError) -> bool {
        err.is_retryable()
    }
}

/// Closure-based classifier.
impl<F> FailureClassifier for F
where
    F: Fn(&CognisError) -> bool + Send + Sync,
{
    fn is_failure(&self, err: &CognisError) -> bool {
        (self)(err)
    }
}

/// Circuit state visible via [`CircuitBreakerProvider::stats`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    /// Normal operation; calls pass through.
    Closed,
    /// Circuit is open; calls are rejected.
    Open,
    /// One probe call permitted to test recovery.
    HalfOpen,
}

/// Snapshot of circuit-breaker counters.
#[derive(Debug, Clone, Copy)]
pub struct CircuitStats {
    /// Current state.
    pub state: CircuitState,
    /// Consecutive failures since the last success.
    pub consecutive_failures: u64,
    /// Total calls observed by the breaker.
    pub total_calls: u64,
    /// Total trips (state transitioned Closed → Open).
    pub trips: u64,
}

struct InnerState {
    state: CircuitState,
    opened_at: Option<Instant>,
}

/// Circuit-breaker wrapper.
pub struct CircuitBreakerProvider {
    inner: std::sync::Arc<dyn LLMProvider>,
    classifier: Box<dyn FailureClassifier>,
    failure_threshold: u64,
    cooldown: Duration,
    consecutive_failures: AtomicU64,
    total_calls: AtomicU64,
    trips: AtomicU64,
    state: Mutex<InnerState>,
}

impl CircuitBreakerProvider {
    /// Wrap `inner` with a circuit breaker. Defaults: threshold 5
    /// failures, cooldown 30s, classifier = "all errors are failures".
    pub fn new(inner: std::sync::Arc<dyn LLMProvider>) -> Self {
        Self {
            inner,
            classifier: Box::new(AllErrorsAreFailures),
            failure_threshold: 5,
            cooldown: Duration::from_secs(30),
            consecutive_failures: AtomicU64::new(0),
            total_calls: AtomicU64::new(0),
            trips: AtomicU64::new(0),
            state: Mutex::new(InnerState {
                state: CircuitState::Closed,
                opened_at: None,
            }),
        }
    }

    /// Override the failure threshold (consecutive failures that trip
    /// the breaker).
    pub fn with_failure_threshold(mut self, n: u64) -> Self {
        self.failure_threshold = n.max(1);
        self
    }

    /// Override the cooldown before the breaker transitions to half-open.
    pub fn with_cooldown(mut self, d: Duration) -> Self {
        self.cooldown = d;
        self
    }

    /// Plug in a custom failure classifier.
    pub fn with_classifier<C>(mut self, c: C) -> Self
    where
        C: FailureClassifier + 'static,
    {
        self.classifier = Box::new(c);
        self
    }

    /// Snapshot the breaker's counters and current state.
    pub fn stats(&self) -> CircuitStats {
        let state_lock = self.state.lock().expect("circuit state mutex poisoned");
        CircuitStats {
            state: state_lock.state,
            consecutive_failures: self.consecutive_failures.load(Ordering::Relaxed),
            total_calls: self.total_calls.load(Ordering::Relaxed),
            trips: self.trips.load(Ordering::Relaxed),
        }
    }

    /// Force-close the breaker (test/admin helper).
    pub fn reset(&self) {
        let mut s = self.state.lock().expect("circuit state mutex poisoned");
        s.state = CircuitState::Closed;
        s.opened_at = None;
        self.consecutive_failures.store(0, Ordering::Relaxed);
    }

    /// Check whether a call may proceed; transitions Open → HalfOpen on
    /// cooldown expiry. Returns the gate decision.
    fn try_acquire(&self) -> Result<()> {
        let mut s = self.state.lock().expect("circuit state mutex poisoned");
        match s.state {
            CircuitState::Closed | CircuitState::HalfOpen => Ok(()),
            CircuitState::Open => {
                let elapsed = s.opened_at.map(|t| t.elapsed()).unwrap_or(Duration::ZERO);
                if elapsed >= self.cooldown {
                    s.state = CircuitState::HalfOpen;
                    Ok(())
                } else {
                    Err(CognisError::Configuration(format!(
                        "circuit breaker open for `{}` (cooldown {}ms remaining)",
                        self.inner.name(),
                        (self.cooldown.saturating_sub(elapsed)).as_millis()
                    )))
                }
            }
        }
    }

    fn on_success(&self) {
        self.consecutive_failures.store(0, Ordering::Relaxed);
        let mut s = self.state.lock().expect("circuit state mutex poisoned");
        s.state = CircuitState::Closed;
        s.opened_at = None;
    }

    fn on_failure(&self, err: &CognisError) {
        if !self.classifier.is_failure(err) {
            return;
        }
        let n = self.consecutive_failures.fetch_add(1, Ordering::Relaxed) + 1;
        if n >= self.failure_threshold {
            let mut s = self.state.lock().expect("circuit state mutex poisoned");
            if !matches!(s.state, CircuitState::Open) {
                self.trips.fetch_add(1, Ordering::Relaxed);
            }
            s.state = CircuitState::Open;
            s.opened_at = Some(Instant::now());
        }
    }
}

#[async_trait]
impl LLMProvider for CircuitBreakerProvider {
    fn name(&self) -> &str {
        self.inner.name()
    }

    fn provider_type(&self) -> Provider {
        self.inner.provider_type()
    }

    async fn chat_completion(
        &self,
        messages: Vec<Message>,
        opts: ChatOptions,
    ) -> Result<ChatResponse> {
        self.total_calls.fetch_add(1, Ordering::Relaxed);
        self.try_acquire()?;
        let res = self.inner.chat_completion(messages, opts).await;
        match &res {
            Ok(_) => self.on_success(),
            Err(e) => self.on_failure(e),
        }
        res
    }

    async fn chat_completion_stream(
        &self,
        messages: Vec<Message>,
        opts: ChatOptions,
    ) -> Result<RunnableStream<StreamChunk>> {
        self.total_calls.fetch_add(1, Ordering::Relaxed);
        self.try_acquire()?;
        let res = self.inner.chat_completion_stream(messages, opts).await;
        match &res {
            Ok(_) => self.on_success(),
            Err(e) => self.on_failure(e),
        }
        res
    }

    async fn chat_completion_with_tools(
        &self,
        messages: Vec<Message>,
        tools: Vec<ToolDefinition>,
        opts: ChatOptions,
    ) -> Result<ChatResponse> {
        self.total_calls.fetch_add(1, Ordering::Relaxed);
        self.try_acquire()?;
        let res = self
            .inner
            .chat_completion_with_tools(messages, tools, opts)
            .await;
        match &res {
            Ok(_) => self.on_success(),
            Err(e) => self.on_failure(e),
        }
        res
    }

    async fn health_check(&self) -> Result<HealthStatus> {
        self.inner.health_check().await
    }
}

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

    /// Test provider that lets us script success/failure per call.
    struct Scripted {
        outcomes: Mutex<Vec<bool>>, // pop_front per call: true = ok, false = err
    }

    impl Scripted {
        fn new(outcomes: Vec<bool>) -> Self {
            Self {
                outcomes: Mutex::new(outcomes.into_iter().rev().collect()),
            }
        }
    }

    #[async_trait]
    impl LLMProvider for Scripted {
        fn name(&self) -> &str {
            "scripted"
        }
        fn provider_type(&self) -> Provider {
            Provider::OpenAI
        }
        async fn chat_completion(
            &self,
            _messages: Vec<Message>,
            _opts: ChatOptions,
        ) -> Result<ChatResponse> {
            let next = self.outcomes.lock().unwrap().pop().unwrap_or(true);
            if next {
                Ok(ChatResponse {
                    message: Message::ai("ok"),
                    usage: None,
                    finish_reason: "stop".into(),
                    model: "test".into(),
                })
            } else {
                Err(CognisError::Internal("scripted failure".into()))
            }
        }
        async fn chat_completion_stream(
            &self,
            _: Vec<Message>,
            _: ChatOptions,
        ) -> Result<RunnableStream<StreamChunk>> {
            unimplemented!()
        }
        async fn health_check(&self) -> Result<HealthStatus> {
            Ok(HealthStatus::Healthy { latency_ms: 0 })
        }
    }

    #[tokio::test]
    async fn closed_state_passes_through() {
        let inner = Arc::new(Scripted::new(vec![true, true, true]));
        let cb = CircuitBreakerProvider::new(inner);
        for _ in 0..3 {
            assert!(cb
                .chat_completion(vec![], ChatOptions::default())
                .await
                .is_ok());
        }
        assert_eq!(cb.stats().state, CircuitState::Closed);
        assert_eq!(cb.stats().trips, 0);
    }

    #[tokio::test]
    async fn opens_after_threshold_failures() {
        let inner = Arc::new(Scripted::new(vec![false, false, false]));
        let cb = CircuitBreakerProvider::new(inner).with_failure_threshold(3);
        for _ in 0..3 {
            let _ = cb.chat_completion(vec![], ChatOptions::default()).await;
        }
        let stats = cb.stats();
        assert_eq!(stats.state, CircuitState::Open);
        assert_eq!(stats.trips, 1);
        // Subsequent call rejected without hitting inner.
        let err = cb
            .chat_completion(vec![], ChatOptions::default())
            .await
            .unwrap_err();
        assert!(format!("{err}").contains("circuit breaker open"));
    }

    #[tokio::test]
    async fn half_opens_after_cooldown_and_closes_on_success() {
        let inner = Arc::new(Scripted::new(vec![false, false, true]));
        let cb = CircuitBreakerProvider::new(inner)
            .with_failure_threshold(2)
            .with_cooldown(Duration::from_millis(20));
        let _ = cb.chat_completion(vec![], ChatOptions::default()).await;
        let _ = cb.chat_completion(vec![], ChatOptions::default()).await;
        assert_eq!(cb.stats().state, CircuitState::Open);

        tokio::time::sleep(Duration::from_millis(30)).await;
        // Probe call: scripted returns Ok → breaker closes.
        assert!(cb
            .chat_completion(vec![], ChatOptions::default())
            .await
            .is_ok());
        assert_eq!(cb.stats().state, CircuitState::Closed);
    }

    #[tokio::test]
    async fn classifier_skips_non_retryable_errors() {
        let inner = Arc::new(Scripted::new(vec![false, false, false]));
        let cb = CircuitBreakerProvider::new(inner)
            .with_failure_threshold(2)
            // Internal errors are *not* retryable, so they don't count.
            .with_classifier(RetryableClassifier);
        for _ in 0..3 {
            let _ = cb.chat_completion(vec![], ChatOptions::default()).await;
        }
        assert_eq!(cb.stats().state, CircuitState::Closed);
        assert_eq!(cb.stats().trips, 0);
    }

    #[tokio::test]
    async fn reset_clears_state() {
        let inner = Arc::new(Scripted::new(vec![false, false]));
        let cb = CircuitBreakerProvider::new(inner).with_failure_threshold(2);
        let _ = cb.chat_completion(vec![], ChatOptions::default()).await;
        let _ = cb.chat_completion(vec![], ChatOptions::default()).await;
        assert_eq!(cb.stats().state, CircuitState::Open);
        cb.reset();
        assert_eq!(cb.stats().state, CircuitState::Closed);
    }

    #[tokio::test]
    async fn closure_classifier_works() {
        let inner = Arc::new(Scripted::new(vec![false, false, false]));
        let cb = CircuitBreakerProvider::new(inner)
            .with_failure_threshold(2)
            .with_classifier(|_e: &CognisError| false); // never count
        for _ in 0..3 {
            let _ = cb.chat_completion(vec![], ChatOptions::default()).await;
        }
        assert_eq!(cb.stats().state, CircuitState::Closed);
    }
}