lean-ctx-protocol 0.3.0

Open wire contracts for lean-ctx: events, observations, assignments, evidence gaps.
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
//! Deterministic, frozen circuit-breaker reference behavior.
//!
//! This implementation isolates a task class/provider pair after repeated
//! failures or quality regressions. It does not learn, rank candidates, or
//! aggregate customer data; production intelligence remains Class D in
//! `lean-ctx-enterprise`.

use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    time::{Duration, Instant},
};

/// Static thresholds for the reference circuit breaker.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CircuitBreakerConfig {
    pub failure_threshold: u32,
    pub recovery_timeout: Duration,
    pub half_open_requests: u32,
}

impl Default for CircuitBreakerConfig {
    fn default() -> Self {
        Self {
            failure_threshold: 3,
            recovery_timeout: Duration::from_secs(30),
            half_open_requests: 1,
        }
    }
}

impl CircuitBreakerConfig {
    /// Validate thresholds needed for a terminating state machine.
    pub fn validate(&self) -> Result<(), String> {
        if self.failure_threshold == 0 {
            return Err("failure_threshold must be greater than zero".to_owned());
        }
        if self.recovery_timeout.is_zero() {
            return Err("recovery_timeout must be greater than zero".to_owned());
        }
        if self.half_open_requests == 0 {
            return Err("half_open_requests must be greater than zero".to_owned());
        }
        Ok(())
    }
}

/// Operational state for one task-class/provider circuit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CircuitBreakerState {
    Closed,
    Open,
    HalfOpen,
}

/// Result of applying a circuit breaker to a candidate route.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CircuitBreakerDecision {
    pub allowed: bool,
    pub selected_model: String,
    pub selected_provider: String,
    pub used_fallback: bool,
}

/// Alias using routing terminology for callers that prefer it.
pub type CircuitBreakerRoute = CircuitBreakerDecision;

#[derive(Debug, Clone)]
struct CircuitEntry {
    state: CircuitBreakerState,
    failures: u32,
    half_open_in_flight: u32,
    half_open_successes: u32,
    opened_at: Option<Instant>,
}

impl Default for CircuitEntry {
    fn default() -> Self {
        Self {
            state: CircuitBreakerState::Closed,
            failures: 0,
            half_open_in_flight: 0,
            half_open_successes: 0,
            opened_at: None,
        }
    }
}

/// Per-task-class and per-provider deterministic circuit-breaker manager.
#[derive(Debug, Clone)]
pub struct CircuitBreaker {
    config: CircuitBreakerConfig,
    entries: HashMap<(String, String), CircuitEntry>,
}

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

impl CircuitBreaker {
    /// Create a breaker. Call [`Self::try_new`] when invalid configuration
    /// should be rejected at construction time.
    pub fn new(config: CircuitBreakerConfig) -> Self {
        Self {
            config,
            entries: HashMap::new(),
        }
    }

    /// Create a breaker after validating its static configuration.
    pub fn try_new(config: CircuitBreakerConfig) -> Result<Self, String> {
        config.validate()?;
        Ok(Self::new(config))
    }

    /// Return the immutable configuration used by this breaker.
    pub fn config(&self) -> &CircuitBreakerConfig {
        &self.config
    }

    /// Return the state for a pair; unseen pairs start closed.
    pub fn state(&self, task_class: &str, provider: &str) -> CircuitBreakerState {
        self.entries
            .get(&key(task_class, provider))
            .map_or(CircuitBreakerState::Closed, |entry| entry.state)
    }

    /// Return the number of recorded consecutive failures for a pair.
    pub fn failure_count(&self, task_class: &str, provider: &str) -> u32 {
        self.entries
            .get(&key(task_class, provider))
            .map_or(0, |entry| entry.failures)
    }

    /// Test whether a candidate request is allowed using the current clock.
    pub fn allow_request(&mut self, task_class: &str, provider: &str) -> bool {
        self.allow_request_at(task_class, provider, Instant::now())
    }

    /// Test whether a candidate request is allowed at a supplied instant.
    pub fn allow_request_at(&mut self, task_class: &str, provider: &str, now: Instant) -> bool {
        let recovery_timeout = self.config.recovery_timeout;
        let half_open_requests = self.config.half_open_requests;
        let entry = self.entries.entry(key(task_class, provider)).or_default();
        match entry.state {
            CircuitBreakerState::Closed => true,
            CircuitBreakerState::Open => {
                let recovered = entry.opened_at.is_some_and(|opened_at| {
                    now >= opened_at && now.duration_since(opened_at) >= recovery_timeout
                });
                if recovered {
                    entry.state = CircuitBreakerState::HalfOpen;
                    entry.half_open_in_flight = 0;
                    entry.half_open_successes = 0;
                    entry.opened_at = None;
                    reserve_half_open_request(entry, half_open_requests)
                } else {
                    false
                }
            }
            CircuitBreakerState::HalfOpen => reserve_half_open_request(entry, half_open_requests),
        }
    }

    /// Record a successful candidate request using the current clock.
    pub fn record_success(&mut self, task_class: &str, provider: &str) {
        self.record_success_at(task_class, provider, Instant::now());
    }

    /// Record a successful candidate request at a supplied instant.
    pub fn record_success_at(&mut self, task_class: &str, provider: &str, _now: Instant) {
        let half_open_requests = self.config.half_open_requests;
        let entry = self.entries.entry(key(task_class, provider)).or_default();
        match entry.state {
            CircuitBreakerState::Closed => entry.failures = 0,
            CircuitBreakerState::Open => {}
            CircuitBreakerState::HalfOpen => {
                entry.half_open_in_flight = entry.half_open_in_flight.saturating_sub(1);
                entry.half_open_successes = entry.half_open_successes.saturating_add(1);
                if entry.half_open_successes >= half_open_requests {
                    close(entry);
                }
            }
        }
    }

    /// Record a failed request using the current clock.
    pub fn record_failure(&mut self, task_class: &str, provider: &str) {
        self.record_failure_at(task_class, provider, Instant::now());
    }

    /// Record a failed request at a supplied instant.
    pub fn record_failure_at(&mut self, task_class: &str, provider: &str, now: Instant) {
        let failure_threshold = self.config.failure_threshold;
        let entry = self.entries.entry(key(task_class, provider)).or_default();
        match entry.state {
            CircuitBreakerState::Closed => {
                entry.failures = entry.failures.saturating_add(1);
                if entry.failures >= failure_threshold {
                    open(entry, now);
                }
            }
            CircuitBreakerState::Open => {}
            CircuitBreakerState::HalfOpen => open(entry, now),
        }
    }

    /// Quality regression is a circuit failure in the frozen reference model.
    pub fn record_quality_regression(&mut self, task_class: &str, provider: &str) {
        self.record_failure(task_class, provider);
    }

    /// Record a quality regression at a supplied instant, useful for
    /// deterministic callers and state-machine verification.
    pub fn record_quality_regression_at(&mut self, task_class: &str, provider: &str, now: Instant) {
        self.record_failure_at(task_class, provider, now);
    }

    /// Record either a successful quality outcome or a quality regression.
    pub fn record_quality_outcome(
        &mut self,
        task_class: &str,
        provider: &str,
        quality_regressed: bool,
    ) {
        if quality_regressed {
            self.record_quality_regression(task_class, provider);
        } else {
            self.record_success(task_class, provider);
        }
    }

    /// Reset one pair to the initial closed state.
    pub fn reset(&mut self, task_class: &str, provider: &str) {
        self.entries.remove(&key(task_class, provider));
    }

    /// Select the candidate when its circuit is available, otherwise use the
    /// supplied baseline model/provider. The baseline is always allowed.
    pub fn route(
        &mut self,
        task_class: &str,
        candidate_provider: &str,
        candidate_model: &str,
        baseline_provider: &str,
        baseline_model: &str,
    ) -> CircuitBreakerDecision {
        self.route_at(
            task_class,
            candidate_provider,
            candidate_model,
            baseline_provider,
            baseline_model,
            Instant::now(),
        )
    }

    /// Deterministic route selection at a supplied instant, useful for tests.
    pub fn route_at(
        &mut self,
        task_class: &str,
        candidate_provider: &str,
        candidate_model: &str,
        baseline_provider: &str,
        baseline_model: &str,
        now: Instant,
    ) -> CircuitBreakerDecision {
        if self.allow_request_at(task_class, candidate_provider, now) {
            CircuitBreakerDecision {
                allowed: true,
                selected_model: candidate_model.to_owned(),
                selected_provider: candidate_provider.to_owned(),
                used_fallback: false,
            }
        } else {
            CircuitBreakerDecision {
                allowed: true,
                selected_model: baseline_model.to_owned(),
                selected_provider: baseline_provider.to_owned(),
                used_fallback: true,
            }
        }
    }
}

fn key(task_class: &str, provider: &str) -> (String, String) {
    (task_class.to_owned(), provider.to_owned())
}

fn reserve_half_open_request(entry: &mut CircuitEntry, limit: u32) -> bool {
    if entry.half_open_in_flight < limit {
        entry.half_open_in_flight += 1;
        true
    } else {
        false
    }
}

fn open(entry: &mut CircuitEntry, now: Instant) {
    entry.state = CircuitBreakerState::Open;
    entry.failures = 0;
    entry.half_open_in_flight = 0;
    entry.half_open_successes = 0;
    entry.opened_at = Some(now);
}

fn close(entry: &mut CircuitEntry) {
    entry.state = CircuitBreakerState::Closed;
    entry.failures = 0;
    entry.half_open_in_flight = 0;
    entry.half_open_successes = 0;
    entry.opened_at = None;
}

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

    fn config() -> CircuitBreakerConfig {
        CircuitBreakerConfig {
            failure_threshold: 2,
            recovery_timeout: Duration::from_secs(10),
            half_open_requests: 2,
        }
    }

    #[test]
    fn config_and_state_round_trip() {
        let config = config();
        let state = CircuitBreakerState::HalfOpen;
        let config_json = serde_json::to_string(&config).expect("config serializes");
        let state_json = serde_json::to_string(&state).expect("state serializes");
        assert_eq!(
            serde_json::from_str::<CircuitBreakerConfig>(&config_json).unwrap(),
            config
        );
        assert_eq!(
            serde_json::from_str::<CircuitBreakerState>(&state_json).unwrap(),
            state
        );
        assert!(config.validate().is_ok());
    }

    #[test]
    fn state_machine_trips_recovers_and_closes_after_half_open_successes() {
        let start = Instant::now();
        let mut breaker = CircuitBreaker::try_new(config()).expect("valid config");

        breaker.record_quality_regression_at("coding", "provider-a", start);
        assert_eq!(
            breaker.state("coding", "provider-a"),
            CircuitBreakerState::Closed
        );
        breaker.record_failure_at("coding", "provider-a", start);
        assert_eq!(
            breaker.state("coding", "provider-a"),
            CircuitBreakerState::Open
        );
        assert!(!breaker.allow_request_at("coding", "provider-a", start));

        let recovered_at = start + Duration::from_secs(10);
        assert!(breaker.allow_request_at("coding", "provider-a", recovered_at));
        assert!(breaker.allow_request_at("coding", "provider-a", recovered_at));
        assert!(!breaker.allow_request_at("coding", "provider-a", recovered_at));
        assert_eq!(
            breaker.state("coding", "provider-a"),
            CircuitBreakerState::HalfOpen
        );

        breaker.record_success_at("coding", "provider-a", recovered_at);
        assert_eq!(
            breaker.state("coding", "provider-a"),
            CircuitBreakerState::HalfOpen
        );
        breaker.record_success_at("coding", "provider-a", recovered_at);
        assert_eq!(
            breaker.state("coding", "provider-a"),
            CircuitBreakerState::Closed
        );
    }

    #[test]
    fn state_is_isolated_by_task_class_and_provider_and_falls_back() {
        let start = Instant::now();
        let mut breaker = CircuitBreaker::try_new(CircuitBreakerConfig {
            failure_threshold: 1,
            recovery_timeout: Duration::from_secs(30),
            half_open_requests: 1,
        })
        .expect("valid config");
        breaker.record_quality_regression_at("coding", "provider-a", start);

        let fallback = breaker.route_at(
            "coding",
            "provider-a",
            "candidate",
            "provider-baseline",
            "baseline",
            start,
        );
        assert!(fallback.allowed);
        assert!(fallback.used_fallback);
        assert_eq!(fallback.selected_model, "baseline");

        let independent = breaker.route_at(
            "review",
            "provider-a",
            "candidate",
            "provider-baseline",
            "baseline",
            start,
        );
        assert!(!independent.used_fallback);
    }
}