bitrouter 1.0.0-alpha.28

BitRouter: an LLM API router. CLI + assembly library.
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Auditable evidence contract for classifiers that never control live routing.

use std::collections::BTreeSet;

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

use crate::eval::types::canonical_digest;

pub const SHADOW_CLASSIFIER_SCHEMA_VERSION: u32 = 1;
pub const ROUTE_CONTEXT_SCHEMA_VERSION: u32 = 3;
pub const PROBABILITY_TOTAL_PPM: u32 = 1_000_000;

pub const TASK_FAMILY_LABELS: &[&str] = &[
    "agent:general",
    "agent:memory_operations",
    "agent:multi_step_planning",
    "agent:web_research",
    "agent:workflow_execution",
    "code:debugging",
    "code:devops_config",
    "code:frontend_ui",
    "code:generation",
    "code:repository_analysis",
    "code:review",
    "code:sql_database",
    "unknown",
];
pub const ROLE_LABELS: &[&str] = &[
    "finalize",
    "implement",
    "mechanical",
    "orchestrate",
    "unknown",
    "verify",
];
pub const PROGRESS_LABELS: &[&str] = &[
    "near_done",
    "opening",
    "progressing",
    "recovering",
    "stalled",
    "unknown",
];
pub const RISK_LABELS: &[&str] = &["context", "guarded", "normal"];
pub const CAPABILITY_LABELS: &[&str] = &[
    "code_reasoning",
    "context_retention",
    "planning",
    "precision",
    "research",
    "tool_execution",
];

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ShadowPredictorKind {
    DeterministicScorecard,
    EmbeddingPrototype,
    FrozenEncoderLinear,
    FrozenEncoderMlp,
    DistilledEncoder,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ShadowConfidenceKind {
    HeuristicMargin,
    CalibratedProbability,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RiskAuthority {
    DeterministicRules,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ResourceMeasurements {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model_size_bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub peak_memory_bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub median_latency_micros: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub p95_latency_micros: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub measurement_environment: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ShadowPredictorProvenance {
    pub kind: ShadowPredictorKind,
    pub name: String,
    pub version: String,
    pub artifact_digest: String,
    pub feature_digest: String,
    pub confidence_kind: ShadowConfidenceKind,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub calibration_digest: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub calibration_split_digest: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub training_split_digest: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resources: Option<ResourceMeasurements>,
}

impl ShadowPredictorProvenance {
    pub fn validate(&self) -> Result<()> {
        validate_identifier(&self.name, "predictor.name")?;
        validate_identifier(&self.version, "predictor.version")?;
        validate_digest(&self.artifact_digest, "predictor.artifact_digest")?;
        validate_digest(&self.feature_digest, "predictor.feature_digest")?;
        validate_optional_digest(
            self.training_split_digest.as_deref(),
            "predictor.training_split_digest",
        )?;
        match self.confidence_kind {
            ShadowConfidenceKind::HeuristicMargin => {
                if self.calibration_digest.is_some() || self.calibration_split_digest.is_some() {
                    anyhow::bail!("heuristic confidence cannot declare calibration provenance")
                }
            }
            ShadowConfidenceKind::CalibratedProbability => {
                validate_digest(
                    self.calibration_digest.as_deref().ok_or_else(|| {
                        anyhow::anyhow!("calibrated probability requires a calibration digest")
                    })?,
                    "predictor.calibration_digest",
                )?;
                validate_digest(
                    self.calibration_split_digest.as_deref().ok_or_else(|| {
                        anyhow::anyhow!(
                            "calibrated probability requires a calibration split digest"
                        )
                    })?,
                    "predictor.calibration_split_digest",
                )?;
            }
        }
        if let Some(resources) = &self.resources {
            resources.validate()?;
        }
        Ok(())
    }

    pub fn digest(&self) -> Result<String> {
        self.validate()?;
        canonical_digest(&("bitrouter.shadow-classifier-predictor.v1", self))
    }
}

impl ResourceMeasurements {
    fn validate(&self) -> Result<()> {
        if self
            .p95_latency_micros
            .zip(self.median_latency_micros)
            .is_some_and(|(p95, median)| p95 < median)
        {
            anyhow::bail!("p95 latency must be at least median latency")
        }
        if let Some(environment) = &self.measurement_environment {
            validate_identifier(environment, "resources.measurement_environment")?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ClassProbability {
    pub label: String,
    pub probability_ppm: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CategoricalHead {
    pub predicted_label: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub probabilities: Vec<ClassProbability>,
}

impl CategoricalHead {
    fn validate(&self, labels: &[&str], calibrated: bool, field: &str) -> Result<()> {
        if !labels.contains(&self.predicted_label.as_str()) {
            anyhow::bail!("{field}.predicted_label is not in the declared label space")
        }
        if !calibrated {
            if !self.probabilities.is_empty() {
                anyhow::bail!("{field} heuristic output cannot contain probabilities")
            }
            return Ok(());
        }
        if self.probabilities.len() != labels.len() {
            anyhow::bail!("{field} must contain the complete calibrated label space")
        }
        let actual = self
            .probabilities
            .iter()
            .map(|entry| entry.label.as_str())
            .collect::<Vec<_>>();
        if actual != labels {
            anyhow::bail!("{field} probabilities must use canonical label order")
        }
        let total = self
            .probabilities
            .iter()
            .try_fold(0_u32, |sum, entry| sum.checked_add(entry.probability_ppm))
            .ok_or_else(|| anyhow::anyhow!("{field} probability total overflow"))?;
        if total != PROBABILITY_TOTAL_PPM {
            anyhow::bail!("{field} probabilities must total {PROBABILITY_TOTAL_PPM} ppm")
        }
        let maximum = self
            .probabilities
            .iter()
            .map(|entry| entry.probability_ppm)
            .max()
            .unwrap_or_default();
        if !self
            .probabilities
            .iter()
            .any(|entry| entry.label == self.predicted_label && entry.probability_ppm == maximum)
        {
            anyhow::bail!("{field}.predicted_label must be a maximum-probability label")
        }
        Ok(())
    }

    pub fn top_probability_ppm(&self) -> Option<u32> {
        self.probabilities
            .iter()
            .map(|entry| entry.probability_ppm)
            .max()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CapabilityScore {
    pub label: String,
    pub score_ppm: u32,
    pub threshold_ppm: u32,
    pub selected: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OodAssessment {
    pub score_ppm: u32,
    pub threshold_ppm: u32,
    pub is_ood: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RouteContextV3 {
    pub schema_version: u32,
    pub task_family: CategoricalHead,
    pub next_step_role: CategoricalHead,
    pub progress_state: CategoricalHead,
    pub route_risk: CategoricalHead,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub capabilities: Vec<CapabilityScore>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ood: Option<OodAssessment>,
    pub abstained: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub abstention_reason: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub exemplar_ids: Vec<String>,
    pub risk_authority: RiskAuthority,
}

impl RouteContextV3 {
    fn validate(&self, confidence_kind: ShadowConfidenceKind) -> Result<()> {
        if self.schema_version != ROUTE_CONTEXT_SCHEMA_VERSION {
            anyhow::bail!("unsupported route context schema version")
        }
        let calibrated = confidence_kind == ShadowConfidenceKind::CalibratedProbability;
        self.task_family
            .validate(TASK_FAMILY_LABELS, calibrated, "task_family")?;
        self.next_step_role
            .validate(ROLE_LABELS, calibrated, "next_step_role")?;
        self.progress_state
            .validate(PROGRESS_LABELS, calibrated, "progress_state")?;
        self.route_risk
            .validate(RISK_LABELS, calibrated, "route_risk")?;
        validate_capabilities(&self.capabilities, calibrated)?;
        if let Some(ood) = &self.ood {
            if !calibrated {
                anyhow::bail!("heuristic output cannot declare a calibrated OOD score")
            }
            validate_ppm(ood.score_ppm, "ood.score_ppm")?;
            validate_ppm(ood.threshold_ppm, "ood.threshold_ppm")?;
            if ood.is_ood != (ood.score_ppm >= ood.threshold_ppm) {
                anyhow::bail!("ood.is_ood must match score and threshold")
            }
        }
        match (self.abstained, self.abstention_reason.as_deref()) {
            (true, Some(reason)) => validate_identifier(reason, "abstention_reason")?,
            (true, None) => anyhow::bail!("abstention requires a reason"),
            (false, Some(_)) => anyhow::bail!("non-abstained output cannot carry a reason"),
            (false, None) => {}
        }
        if self.exemplar_ids.len() > 16 {
            anyhow::bail!("exemplar_ids exceeds the bounded evidence limit")
        }
        validate_sorted_identifiers(&self.exemplar_ids, "exemplar_ids")?;
        Ok(())
    }
}

fn validate_capabilities(capabilities: &[CapabilityScore], calibrated: bool) -> Result<()> {
    if !calibrated && !capabilities.is_empty() {
        anyhow::bail!("heuristic output cannot contain capability scores")
    }
    let labels = capabilities
        .iter()
        .map(|capability| capability.label.as_str())
        .collect::<Vec<_>>();
    if !labels.windows(2).all(|pair| pair[0] < pair[1]) {
        anyhow::bail!("capabilities must be unique and canonically ordered")
    }
    for capability in capabilities {
        if !CAPABILITY_LABELS.contains(&capability.label.as_str()) {
            anyhow::bail!("unknown capability label {}", capability.label)
        }
        validate_ppm(capability.score_ppm, "capability.score_ppm")?;
        validate_ppm(capability.threshold_ppm, "capability.threshold_ppm")?;
        if capability.selected != (capability.score_ppm >= capability.threshold_ppm) {
            anyhow::bail!("capability selected flag must match score and threshold")
        }
    }
    Ok(())
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ShadowClassifierPrediction {
    pub fixture_id: String,
    pub dataset_digest: String,
    pub input_projection_digest: String,
    pub predictor_digest: String,
    pub context: RouteContextV3,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ShadowClassifierSubmission {
    pub schema_version: u32,
    pub dataset_digest: String,
    pub predictor: ShadowPredictorProvenance,
    pub predictions: Vec<ShadowClassifierPrediction>,
}

impl ShadowClassifierSubmission {
    pub fn load_json(path: impl AsRef<std::path::Path>) -> Result<Self> {
        let path = path.as_ref();
        let bytes = std::fs::read(path)
            .with_context(|| format!("read classifier submission {}", path.display()))?;
        serde_json::from_slice(&bytes)
            .with_context(|| format!("parse classifier submission {}", path.display()))
    }

    pub fn validate(&self) -> Result<()> {
        if self.schema_version != SHADOW_CLASSIFIER_SCHEMA_VERSION {
            anyhow::bail!("unsupported shadow classifier schema version")
        }
        validate_digest(&self.dataset_digest, "dataset_digest")?;
        self.predictor.validate()?;
        let predictor_digest = self.predictor.digest()?;
        let mut previous = None;
        for prediction in &self.predictions {
            validate_identifier(&prediction.fixture_id, "prediction.fixture_id")?;
            if previous.is_some_and(|value| value >= prediction.fixture_id.as_str()) {
                anyhow::bail!("predictions must be unique and ordered by fixture_id")
            }
            previous = Some(prediction.fixture_id.as_str());
            if prediction.dataset_digest != self.dataset_digest {
                anyhow::bail!("prediction dataset digest does not match submission")
            }
            validate_digest(
                &prediction.input_projection_digest,
                "prediction.input_projection_digest",
            )?;
            if prediction.predictor_digest != predictor_digest {
                anyhow::bail!("prediction predictor digest does not match provenance")
            }
            prediction
                .context
                .validate(self.predictor.confidence_kind)?;
        }
        Ok(())
    }

    pub fn digest(&self) -> Result<String> {
        self.validate()?;
        canonical_digest(&("bitrouter.shadow-classifier-submission.v1", self))
    }
}

fn validate_sorted_identifiers(values: &[String], field: &str) -> Result<()> {
    let mut unique = BTreeSet::new();
    let mut previous: Option<&str> = None;
    for value in values {
        validate_identifier(value, field)?;
        if previous.is_some_and(|prior| prior >= value.as_str()) || !unique.insert(value) {
            anyhow::bail!("{field} must be unique and canonically ordered")
        }
        previous = Some(value);
    }
    Ok(())
}

pub(crate) fn validate_identifier(value: &str, field: &str) -> Result<()> {
    if value.trim().is_empty() || value.len() > 512 || value.chars().any(char::is_control) {
        anyhow::bail!("{field} must be a non-empty bounded identifier")
    }
    Ok(())
}

pub(crate) fn validate_digest(value: &str, field: &str) -> Result<()> {
    let Some(hex_digest) = value.strip_prefix("sha256:") else {
        anyhow::bail!("{field} must be a sha256 digest")
    };
    if hex_digest.len() != 64
        || !hex_digest
            .bytes()
            .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
    {
        anyhow::bail!("{field} must contain 64 lowercase hexadecimal digits")
    }
    Ok(())
}

fn validate_optional_digest(value: Option<&str>, field: &str) -> Result<()> {
    if let Some(value) = value {
        validate_digest(value, field)?;
    }
    Ok(())
}

fn validate_ppm(value: u32, field: &str) -> Result<()> {
    if value > PROBABILITY_TOTAL_PPM {
        anyhow::bail!("{field} must be between 0 and {PROBABILITY_TOTAL_PPM}")
    }
    Ok(())
}

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

    fn digest(byte: char) -> String {
        format!("sha256:{}", byte.to_string().repeat(64))
    }

    fn head(labels: &[&str], predicted: &str) -> CategoricalHead {
        let selected = labels
            .iter()
            .position(|label| *label == predicted)
            .unwrap_or_default();
        CategoricalHead {
            predicted_label: predicted.to_owned(),
            probabilities: labels
                .iter()
                .enumerate()
                .map(|(index, label)| ClassProbability {
                    label: (*label).to_owned(),
                    probability_ppm: u32::from(index == selected) * PROBABILITY_TOTAL_PPM,
                })
                .collect(),
        }
    }

    fn submission() -> ShadowClassifierSubmission {
        let predictor = ShadowPredictorProvenance {
            kind: ShadowPredictorKind::EmbeddingPrototype,
            name: "test-vector".into(),
            version: "1".into(),
            artifact_digest: digest('a'),
            feature_digest: digest('b'),
            confidence_kind: ShadowConfidenceKind::CalibratedProbability,
            calibration_digest: Some(digest('c')),
            calibration_split_digest: Some(digest('1')),
            training_split_digest: Some(digest('d')),
            resources: None,
        };
        let predictor_digest = predictor.digest().unwrap_or_default();
        ShadowClassifierSubmission {
            schema_version: SHADOW_CLASSIFIER_SCHEMA_VERSION,
            dataset_digest: digest('e'),
            predictor,
            predictions: vec![ShadowClassifierPrediction {
                fixture_id: "fixture-1".into(),
                dataset_digest: digest('e'),
                input_projection_digest: digest('f'),
                predictor_digest,
                context: RouteContextV3 {
                    schema_version: ROUTE_CONTEXT_SCHEMA_VERSION,
                    task_family: head(TASK_FAMILY_LABELS, "code:debugging"),
                    next_step_role: head(ROLE_LABELS, "implement"),
                    progress_state: head(PROGRESS_LABELS, "opening"),
                    route_risk: head(RISK_LABELS, "normal"),
                    capabilities: vec![],
                    ood: Some(OodAssessment {
                        score_ppm: 100_000,
                        threshold_ppm: 800_000,
                        is_ood: false,
                    }),
                    abstained: false,
                    abstention_reason: None,
                    exemplar_ids: vec!["exemplar-1".into()],
                    risk_authority: RiskAuthority::DeterministicRules,
                },
            }],
        }
    }

    #[test]
    fn admits_a_complete_calibrated_submission() {
        assert!(submission().validate().is_ok());
    }

    #[test]
    fn rejects_noncanonical_probabilities_and_forged_provenance() {
        let mut reordered = submission();
        reordered.predictions[0]
            .context
            .task_family
            .probabilities
            .swap(0, 1);
        assert!(reordered.validate().is_err());

        let mut wrong_total = submission();
        wrong_total.predictions[0]
            .context
            .next_step_role
            .probabilities[0]
            .probability_ppm = 1;
        assert!(wrong_total.validate().is_err());

        let mut forged = submission();
        forged.predictions[0].predictor_digest = digest('0');
        assert!(forged.validate().is_err());
    }

    #[test]
    fn heuristic_margin_cannot_masquerade_as_probability() {
        let mut value = submission();
        value.predictor.confidence_kind = ShadowConfidenceKind::HeuristicMargin;
        value.predictor.calibration_digest = None;
        value.predictor.calibration_split_digest = None;
        value.predictions[0].predictor_digest = value.predictor.digest().unwrap_or_default();
        assert!(value.validate().is_err());
    }

    #[test]
    fn abstention_and_ood_are_explicit_and_consistent() {
        let mut missing_reason = submission();
        missing_reason.predictions[0].context.abstained = true;
        assert!(missing_reason.validate().is_err());

        let mut wrong_ood = submission();
        wrong_ood.predictions[0].context.ood = Some(OodAssessment {
            score_ppm: 900_000,
            threshold_ppm: 800_000,
            is_ood: false,
        });
        assert!(wrong_ood.validate().is_err());
    }
}