substrate-core 0.3.10

Hexagonal core contracts (domain + ports) for substrate. Depends on nothing but serde/thiserror/uuid/async-trait.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Routing superset — load-balancing strategies, circuit breakers, weighted fallback.
//!
//! Pure types and selection logic live here (no I/O). Adapters such as
//! `routing-phenotype-router` and the legacy `omniroute-adapter` wire these
//! primitives to real providers.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::domain::RoutingDecision;
use crate::error::{Result, SubstrateError};

/// Load-balancing strategy over a pool of candidate targets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RoutingStrategy {
    /// Cycle through healthy targets in order.
    RoundRobin,
    /// Select proportionally to each target's `weight`.
    Weighted,
    /// Pick the healthy target with the lowest `in_flight` count.
    LeastUsed,
    /// Sample two healthy targets and pick the one with lower load.
    PowerOfTwoChoices,
}

/// Circuit breaker state for a single target.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CircuitState {
    /// Requests flow normally.
    Closed,
    /// Requests are rejected until the reset timeout elapses.
    Open,
    /// A single probe request is allowed after the reset timeout.
    HalfOpen,
}

/// Thresholds controlling circuit-breaker transitions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct CircuitBreakerConfig {
    /// Consecutive failures in `Closed` before opening the circuit.
    pub failure_threshold: u32,
    /// Seconds the circuit stays `Open` before lazy recovery to `HalfOpen`.
    pub reset_timeout_secs: u64,
}

impl Default for CircuitBreakerConfig {
    fn default() -> Self {
        CircuitBreakerConfig {
            failure_threshold: 5,
            reset_timeout_secs: 30,
        }
    }
}

/// Per-target circuit breaker with lazy recovery on read.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CircuitBreaker {
    state: CircuitState,
    consecutive_failures: u32,
    opened_at_secs: Option<u64>,
    config: CircuitBreakerConfig,
}

impl CircuitBreaker {
    /// Create a breaker in the `Closed` state.
    pub fn new(config: CircuitBreakerConfig) -> Self {
        CircuitBreaker {
            state: CircuitState::Closed,
            consecutive_failures: 0,
            opened_at_secs: None,
            config,
        }
    }

    /// Current stored state (does not apply lazy timeout recovery).
    pub fn state(&self) -> CircuitState {
        self.state
    }

    /// Consecutive failure count (for tests and diagnostics).
    pub fn consecutive_failures(&self) -> u32 {
        self.consecutive_failures
    }

    /// Effective state after applying lazy open→half-open recovery.
    pub fn effective_state(&self, now_secs: u64) -> CircuitState {
        match self.state {
            CircuitState::Open => {
                if let Some(opened) = self.opened_at_secs {
                    if now_secs.saturating_sub(opened) >= self.config.reset_timeout_secs {
                        CircuitState::HalfOpen
                    } else {
                        CircuitState::Open
                    }
                } else {
                    CircuitState::Open
                }
            }
            other => other,
        }
    }

    /// Whether a request may be sent to this target at `now_secs`.
    pub fn allow_request(&self, now_secs: u64) -> bool {
        matches!(
            self.effective_state(now_secs),
            CircuitState::Closed | CircuitState::HalfOpen
        )
    }

    /// Record a successful response.
    pub fn record_success(&mut self, now_secs: u64) {
        match self.effective_state(now_secs) {
            CircuitState::Closed | CircuitState::HalfOpen => {
                self.state = CircuitState::Closed;
                self.consecutive_failures = 0;
                self.opened_at_secs = None;
            }
            CircuitState::Open => {}
        }
    }

    /// Record a failed response.
    pub fn record_failure(&mut self, now_secs: u64) {
        match self.effective_state(now_secs) {
            CircuitState::Closed => {
                self.consecutive_failures += 1;
                if self.consecutive_failures >= self.config.failure_threshold {
                    self.state = CircuitState::Open;
                    self.opened_at_secs = Some(now_secs);
                }
            }
            CircuitState::HalfOpen => {
                self.state = CircuitState::Open;
                self.opened_at_secs = Some(now_secs);
                self.consecutive_failures = self.config.failure_threshold;
            }
            CircuitState::Open => {}
        }
    }
}

/// A routable engine/model target in a load-balanced pool.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoutingTarget {
    /// Stable identifier for health tracking.
    pub id: String,
    /// Engine name (e.g. `"forge"`).
    pub engine: String,
    /// Model identifier passed to the engine.
    pub model: String,
    /// Relative weight for [`RoutingStrategy::Weighted`].
    pub weight: u32,
}

/// One entry in an ordered fallback chain.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FallbackEntry {
    /// Lower rank = higher priority. Entries at the same rank compete by weight.
    pub rank: u32,
    /// The target to route to when this entry is selected.
    pub target: RoutingTarget,
    /// Weight among healthy entries sharing the same `rank`.
    pub weight: u32,
}

/// Health and load counters for a single target.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TargetHealth {
    /// Circuit breaker for this target.
    pub breaker: CircuitBreaker,
    /// In-flight request count (for least-used / P2C).
    pub in_flight: u64,
}

impl Default for TargetHealth {
    fn default() -> Self {
        TargetHealth {
            breaker: CircuitBreaker::new(CircuitBreakerConfig::default()),
            in_flight: 0,
        }
    }
}

/// Mutable routing pool state (cursors, counters, per-target health).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RoutingPoolState {
    /// Cursor for round-robin among healthy indices.
    pub round_robin_cursor: usize,
    /// Monotonic counter driving weighted / P2C selection.
    pub selection_counter: u64,
    /// Per-target health keyed by target id.
    pub health: HashMap<String, TargetHealth>,
}

impl RoutingPoolState {
    /// Ensure every target in `pool` has a health entry.
    pub fn ensure_targets(&mut self, pool: &[RoutingTarget], config: CircuitBreakerConfig) {
        for t in pool {
            self.health
                .entry(t.id.clone())
                .or_insert_with(|| TargetHealth {
                    breaker: CircuitBreaker::new(config),
                    in_flight: 0,
                });
        }
    }

    fn health_for<'a>(&'a self, id: &str) -> Option<&'a TargetHealth> {
        self.health.get(id)
    }
}

/// Pure selection helpers — deterministic given inputs and mutable state.
#[derive(Debug, Clone, Copy, Default)]
pub struct RoutingSelector;

impl RoutingSelector {
    /// Select a pool index using `strategy`. Returns `None` if no healthy target exists.
    pub fn select(
        strategy: RoutingStrategy,
        pool: &[RoutingTarget],
        state: &mut RoutingPoolState,
        now_secs: u64,
        p2c_seed: u64,
    ) -> Option<usize> {
        let healthy: Vec<usize> = pool
            .iter()
            .enumerate()
            .filter(|(_, t)| {
                state
                    .health_for(&t.id)
                    .map(|h| h.breaker.allow_request(now_secs))
                    .unwrap_or(true)
            })
            .map(|(i, _)| i)
            .collect();

        if healthy.is_empty() {
            return None;
        }

        let pick = match strategy {
            RoutingStrategy::RoundRobin => {
                let cursor = state.round_robin_cursor % healthy.len();
                state.round_robin_cursor = state.round_robin_cursor.wrapping_add(1);
                healthy[cursor]
            }
            RoutingStrategy::Weighted => {
                let weights: Vec<u32> = healthy.iter().map(|&i| pool[i].weight.max(1)).collect();
                let total: u32 = weights.iter().sum();
                let mut pick_weight = (state.selection_counter % total as u64) as u32;
                state.selection_counter = state.selection_counter.wrapping_add(1);
                let mut chosen = healthy[0];
                for (idx, &w) in weights.iter().enumerate() {
                    if pick_weight < w {
                        chosen = healthy[idx];
                        break;
                    }
                    pick_weight -= w;
                }
                chosen
            }
            RoutingStrategy::LeastUsed => {
                let mut best = healthy[0];
                let mut best_load = u64::MAX;
                for &i in &healthy {
                    let load = state
                        .health_for(&pool[i].id)
                        .map(|h| h.in_flight)
                        .unwrap_or(0);
                    if load < best_load {
                        best_load = load;
                        best = i;
                    }
                }
                best
            }
            RoutingStrategy::PowerOfTwoChoices => {
                let n = healthy.len();
                if n == 1 {
                    healthy[0]
                } else {
                    let seed = state.selection_counter.wrapping_add(p2c_seed);
                    state.selection_counter = state.selection_counter.wrapping_add(1);
                    let i = (seed as usize) % n;
                    let j = (seed.wrapping_mul(31).wrapping_add(17) as usize) % n;
                    let (a, b) = if i == j {
                        (healthy[i], healthy[(i + 1) % n])
                    } else {
                        (healthy[i], healthy[j])
                    };
                    let load_a = state
                        .health_for(&pool[a].id)
                        .map(|h| h.in_flight)
                        .unwrap_or(0);
                    let load_b = state
                        .health_for(&pool[b].id)
                        .map(|h| h.in_flight)
                        .unwrap_or(0);
                    if load_a <= load_b {
                        a
                    } else {
                        b
                    }
                }
            }
        };

        Some(pick)
    }

    /// Walk the fallback chain by rank; within each rank, weighted-select among healthy entries.
    pub fn select_fallback<'a>(
        chain: &'a [FallbackEntry],
        health: &HashMap<String, TargetHealth>,
        counter: &mut u64,
        now_secs: u64,
    ) -> Option<&'a RoutingTarget> {
        if chain.is_empty() {
            return None;
        }
        let min_rank = chain.iter().map(|e| e.rank).min().unwrap();
        let max_rank = chain.iter().map(|e| e.rank).max().unwrap();

        for rank in min_rank..=max_rank {
            let tier: Vec<&FallbackEntry> = chain
                .iter()
                .filter(|e| e.rank == rank)
                .filter(|e| {
                    health
                        .get(&e.target.id)
                        .map(|h| h.breaker.allow_request(now_secs))
                        .unwrap_or(true)
                })
                .collect();
            if tier.is_empty() {
                continue;
            }
            if tier.len() == 1 {
                return Some(&tier[0].target);
            }
            let weights: Vec<u32> = tier.iter().map(|e| e.weight.max(1)).collect();
            let total: u32 = weights.iter().sum();
            let mut pick = (*counter % total as u64) as u32;
            *counter = counter.wrapping_add(1);
            for (entry, w) in tier.iter().zip(weights.iter()) {
                if pick < *w {
                    return Some(&entry.target);
                }
                pick -= w;
            }
            return Some(&tier[0].target);
        }
        None
    }
}

/// A routing decision enriched with the chosen target id.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SupersetRoutingDecision {
    /// Chosen target id (for outcome recording).
    pub target_id: String,
    /// Engine + model decision for downstream engines.
    pub decision: RoutingDecision,
}

impl SupersetRoutingDecision {
    fn from_target(target: &RoutingTarget, reason: impl Into<String>) -> Self {
        SupersetRoutingDecision {
            target_id: target.id.clone(),
            decision: RoutingDecision {
                engine: target.engine.clone(),
                model: target.model.clone(),
                reason: Some(reason.into()),
            },
        }
    }
}

/// Bundled routing superset: pool load-balancing with fallback chain and health tracking.
#[derive(Debug, Clone)]
pub struct RoutingSuperset {
    pool: Vec<RoutingTarget>,
    fallback: Vec<FallbackEntry>,
    strategy: RoutingStrategy,
    breaker_config: CircuitBreakerConfig,
    state: RoutingPoolState,
}

impl RoutingSuperset {
    /// Create a new superset router.
    pub fn new(
        pool: Vec<RoutingTarget>,
        fallback: Vec<FallbackEntry>,
        strategy: RoutingStrategy,
        breaker_config: CircuitBreakerConfig,
    ) -> Self {
        let mut state = RoutingPoolState::default();
        state.ensure_targets(&pool, breaker_config);
        for entry in &fallback {
            state
                .health
                .entry(entry.target.id.clone())
                .or_insert_with(|| TargetHealth {
                    breaker: CircuitBreaker::new(breaker_config),
                    in_flight: 0,
                });
        }
        RoutingSuperset {
            pool,
            fallback,
            strategy,
            breaker_config,
            state,
        }
    }

    /// Select a target and return a routing decision. Increments in-flight for the chosen target.
    pub fn route(&mut self, now_secs: u64) -> Result<SupersetRoutingDecision> {
        if let Some(idx) =
            RoutingSelector::select(self.strategy, &self.pool, &mut self.state, now_secs, 0)
        {
            let target = &self.pool[idx];
            if let Some(h) = self.state.health.get_mut(&target.id) {
                h.in_flight += 1;
            }
            return Ok(SupersetRoutingDecision::from_target(
                target,
                format!("routing-superset:{strategy:?}", strategy = self.strategy),
            ));
        }

        if let Some(target) = RoutingSelector::select_fallback(
            &self.fallback,
            &self.state.health,
            &mut self.state.selection_counter,
            now_secs,
        ) {
            if let Some(h) = self.state.health.get_mut(&target.id) {
                h.in_flight += 1;
            }
            return Ok(SupersetRoutingDecision::from_target(
                target,
                "routing-superset:fallback",
            ));
        }

        Err(SubstrateError::Routing(
            "no healthy routing target available".to_string(),
        ))
    }

    /// Record success or failure for a previously routed target.
    pub fn record_outcome(&mut self, target_id: &str, success: bool, now_secs: u64) {
        if let Some(h) = self.state.health.get_mut(target_id) {
            h.in_flight = h.in_flight.saturating_sub(1);
            if success {
                h.breaker.record_success(now_secs);
            } else {
                h.breaker.record_failure(now_secs);
            }
        }
    }

    /// Current load-balancing strategy.
    pub fn strategy(&self) -> RoutingStrategy {
        self.strategy
    }

    /// Primary target pool.
    pub fn pool(&self) -> &[RoutingTarget] {
        &self.pool
    }

    /// Fallback chain entries.
    pub fn fallback(&self) -> &[FallbackEntry] {
        &self.fallback
    }

    /// Breaker configuration applied to new health entries.
    pub fn breaker_config(&self) -> CircuitBreakerConfig {
        self.breaker_config
    }
}