Skip to main content

firstpass_proxy/
calibrate.rs

1//! Recalibrate the conformal serving threshold from real deferred feedback (SPEC §10.1, run
2//! against live traffic instead of a static benchmark suite) — the "learns your quality bar"
3//! loop.
4//!
5//! Enumerates stored traces, pairs each trace that has a deferred outcome with the score of the
6//! attempt actually served, and hands the pairs to [`firstpass_core::conformal`]. This produces
7//! a recommended threshold and its feasibility; it does not (yet) feed back into the request hot
8//! path — that wiring is a deliberate follow-on once an operator has reviewed a report.
9
10use std::path::Path;
11
12use firstpass_core::conformal::{self, ConformalResult};
13use firstpass_core::{Attempt, DeferredVerdict, GateResult, Score, Trace, Verdict};
14
15use crate::store::{self, StoreError};
16
17/// The result of calibrating a conformal threshold against real deferred feedback.
18#[derive(Debug, Clone)]
19pub struct CalibrationReport {
20    /// Number of `(score, correct)` pairs calibration ran on — one per trace with at least one
21    /// deferred verdict recorded.
22    pub n_pairs: usize,
23    /// The conformal calibration result (threshold, feasibility, calibration risk).
24    pub conformal: ConformalResult,
25    /// Empirical served-failure rate at `conformal.threshold`, measured on the same pairs used
26    /// to calibrate (a sanity check, not a held-out estimate — the proxy doesn't yet split
27    /// feedback into separate calibration/test batches).
28    pub empirical_served_failure: f64,
29    /// How many pairs would be served at the calibrated threshold.
30    pub n_served: usize,
31}
32
33impl CalibrationReport {
34    /// Render the report as human-readable lines for `firstpass calibrate`.
35    #[must_use]
36    pub fn render(&self) -> String {
37        format!(
38            "pairs: {n_pairs} ({n_served} served at threshold)\n\
39             threshold: {threshold:.4}\n\
40             feasible: {feasible}\n\
41             target alpha: {alpha:.4} (delta {delta:.4})\n\
42             calibration risk: {calib_risk:.4}\n\
43             empirical served-failure: {empirical:.4}\n",
44            n_pairs = self.n_pairs,
45            n_served = self.n_served,
46            threshold = self.conformal.threshold,
47            feasible = self.conformal.feasible,
48            alpha = self.conformal.alpha,
49            delta = self.conformal.delta,
50            calib_risk = self.conformal.calib_risk,
51            empirical = self.empirical_served_failure,
52        )
53    }
54}
55
56/// Calibrate a conformal threshold from `(score, correct)` pairs — a thin wrapper over
57/// [`firstpass_core::conformal`] that also reports the empirical served-failure at the chosen
58/// threshold.
59#[must_use]
60pub fn calibrate_pairs(
61    pairs: &[(f64, bool)],
62    alpha: f64,
63    delta: f64,
64    min_n: usize,
65) -> CalibrationReport {
66    let result = conformal::calibrate(pairs, alpha, delta, min_n);
67    let (empirical_served_failure, n_served) =
68        conformal::served_failure_rate(pairs, result.threshold);
69    CalibrationReport {
70        n_pairs: pairs.len(),
71        conformal: result,
72        empirical_served_failure,
73        n_served,
74    }
75}
76
77/// The aggregate score for a set of gate results at a given verdict: the mean of the numeric gate
78/// scores, or — when no gate reported a numeric score at all — `1.0` if it passed and `0.0` if it
79/// didn't. A bare pass/fail with no score still needs to sit somewhere on the `[0, 1]` axis
80/// conformal thresholds against; treating a scoreless pass as maximally confident and a scoreless
81/// fail as minimally confident keeps "higher score = more servable" true either way.
82///
83/// Shared by [`attempt_score`] (calibration, offline) and the router's `serve_threshold` decision
84/// (serving, online) so the two agree on what "the score" means.
85pub(crate) fn gate_score(gates: &[GateResult], verdict: Verdict) -> f64 {
86    let numeric: Vec<f64> = gates
87        .iter()
88        .filter_map(|g| g.score.map(Score::value))
89        .collect();
90    if numeric.is_empty() {
91        f64::from(verdict == Verdict::Pass)
92    } else {
93        numeric.iter().sum::<f64>() / numeric.len() as f64
94    }
95}
96
97/// The aggregate score for a served attempt (see [`gate_score`]).
98fn attempt_score(attempt: &Attempt) -> f64 {
99    gate_score(&attempt.gates, attempt.verdict)
100}
101
102/// Build a `(score, correct)` pair for one trace, if it has deferred feedback and a served
103/// attempt. `correct` is whether the MOST RECENT deferred verdict for the trace is `Pass` (later
104/// feedback supersedes earlier — e.g. a flaky CI run retried).
105fn trace_pair(trace: &Trace, deferred: &[DeferredVerdict]) -> Option<(f64, bool)> {
106    let last = deferred.last()?;
107    let served_rung = trace.final_.served_rung?;
108    let attempt = trace.attempts.iter().find(|a| a.rung == served_rung)?;
109    Some((attempt_score(attempt), last.verdict == Verdict::Pass))
110}
111
112/// Calibrate a conformal threshold from every trace in the store that has a deferred outcome
113/// recorded.
114///
115/// # Errors
116/// Returns [`StoreError`] if a stored trace's deferred verdicts cannot be read. An unreadable or
117/// not-yet-initialized store is treated as zero traces (a 0-pair, infeasible report), matching the
118/// forgiving behaviour of `firstpass trace` — calibrating before any traffic is a valid state, not
119/// an error.
120pub fn calibrate_from_store(
121    db_path: impl AsRef<Path>,
122    alpha: f64,
123    delta: f64,
124    min_n: usize,
125) -> Result<CalibrationReport, StoreError> {
126    let traces = store::load_all_traces(&db_path).unwrap_or_default();
127    let mut pairs = Vec::with_capacity(traces.len());
128    for trace in &traces {
129        let deferred = store::load_deferred(&db_path, &trace.trace_id.to_string())?;
130        if let Some(pair) = trace_pair(trace, &deferred) {
131            pairs.push(pair);
132        }
133    }
134    Ok(calibrate_pairs(&pairs, alpha, delta, min_n))
135}
136
137#[cfg(test)]
138mod tests {
139    use firstpass_core::{
140        Features, FinalOutcome, GENESIS_HASH, GateResult, Mode, PolicyRef, RequestInfo, ServedFrom,
141        TaskKind,
142    };
143
144    use super::*;
145    use crate::store;
146
147    /// A minimal trace serving rung 0 with a single deterministic gate score, mirroring
148    /// `store::sample_trace` but with a caller-chosen score.
149    fn trace_with_score(score: f64) -> Trace {
150        let verdict = if score >= 0.5 {
151            Verdict::Pass
152        } else {
153            Verdict::Fail
154        };
155        let attempt = Attempt {
156            rung: 0,
157            model: "claude-haiku-4-5".to_owned(),
158            provider: "anthropic".to_owned(),
159            in_tokens: 10,
160            out_tokens: 5,
161            cost_usd: 0.001,
162            latency_ms: 12,
163            gates: vec![GateResult {
164                gate_id: "gate@v1".to_owned(),
165                verdict,
166                score: Some(Score::clamped(score)),
167                cost_usd: 0.0,
168                ms: 10,
169                reason: None,
170                evidence_ref: None,
171            }],
172            verdict,
173        };
174        let mut trace = Trace {
175            trace_id: uuid::Uuid::now_v7(),
176            prev_hash: GENESIS_HASH.to_owned(),
177            tenant_id: "tenant-a".to_owned(),
178            session_id: "session-1".to_owned(),
179            ts: jiff::Timestamp::now(),
180            mode: Mode::Enforce,
181            policy: PolicyRef {
182                id: "test@v0".to_owned(),
183                explore: false,
184            },
185            request: RequestInfo {
186                api: "anthropic.messages".to_owned(),
187                prompt_hash: "deadbeef".to_owned(),
188                features: Features::new(TaskKind::Other),
189            },
190            attempts: vec![attempt],
191            deferred: Vec::new(),
192            final_: FinalOutcome {
193                served_rung: Some(0),
194                served_from: ServedFrom::Attempt,
195                total_cost_usd: 0.001,
196                gate_cost_usd: 0.0,
197                total_latency_ms: 12,
198                escalations: 0,
199                counterfactual_baseline_usd: 0.001,
200                savings_usd: 0.0,
201            },
202        };
203        trace.recompute_savings();
204        trace
205    }
206
207    #[test]
208    fn calibrate_pairs_finds_a_feasible_threshold_on_clean_pairs() {
209        // Scores cleanly separate correct (>=0.7) from incorrect (<0.3). alpha=0.2 tolerates
210        // some incorrect items being served, so conformal maximizes coverage — not just
211        // separation — up to that budget; alpha=0.2 also keeps the Hoeffding slack satisfiable
212        // at this sample size (min_n=30 wants a workable n, not the hundreds needed to certify
213        // alpha=0.1 at zero observed failures).
214        let mut pairs = Vec::new();
215        for i in 0..60u32 {
216            pairs.push((0.7 + f64::from(i % 10) * 0.01, true));
217        }
218        for i in 0..60u32 {
219            pairs.push((0.2 + f64::from(i % 10) * 0.01, false));
220        }
221        let report = calibrate_pairs(&pairs, 0.2, 0.1, 30);
222        assert!(
223            report.conformal.feasible,
224            "clean separation must be feasible"
225        );
226        assert!(
227            report.conformal.threshold >= 0.2 && report.conformal.threshold <= 0.79,
228            "threshold {} must land inside the observed score range",
229            report.conformal.threshold
230        );
231        assert_eq!(report.n_pairs, 120);
232        assert!(
233            report.empirical_served_failure <= 0.2 + 1e-9,
234            "empirical served-failure {} must respect alpha — the conformal guarantee",
235            report.empirical_served_failure
236        );
237    }
238
239    #[test]
240    fn calibrate_pairs_infeasible_below_min_n() {
241        let pairs = vec![(0.9, true), (0.9, true), (0.1, false)];
242        let report = calibrate_pairs(&pairs, 0.1, 0.1, 30);
243        assert!(
244            !report.conformal.feasible,
245            "too few pairs must be infeasible"
246        );
247    }
248
249    #[tokio::test]
250    async fn calibrate_from_store_pairs_only_traces_with_deferred_feedback() {
251        let db_path = std::env::temp_dir().join(format!(
252            "firstpass-calibrate-test-{}.db",
253            uuid::Uuid::now_v7()
254        ));
255        let (tx, handle) = store::open(&db_path).unwrap();
256
257        // 40 high-score traces confirmed correct, 40 low-score traces confirmed incorrect, and
258        // 5 traces with no deferred verdict at all (must be excluded from calibration).
259        let mut correct_ids = Vec::new();
260        let mut incorrect_ids = Vec::new();
261        for i in 0..40u32 {
262            let t = trace_with_score(0.7 + f64::from(i % 10) * 0.01);
263            correct_ids.push(t.trace_id.to_string());
264            tx.try_send(t).unwrap();
265        }
266        for i in 0..40u32 {
267            let t = trace_with_score(0.2 + f64::from(i % 10) * 0.01);
268            incorrect_ids.push(t.trace_id.to_string());
269            tx.try_send(t).unwrap();
270        }
271        for i in 0..5u32 {
272            tx.try_send(trace_with_score(0.5 + f64::from(i) * 0.01))
273                .unwrap();
274        }
275        drop(tx);
276        handle.await.unwrap();
277
278        for trace_id in &correct_ids {
279            let dv = DeferredVerdict {
280                gate_id: "outcome".to_owned(),
281                verdict: Verdict::Pass,
282                score: None,
283                reported_at: jiff::Timestamp::now(),
284                reporter: "unit-test".to_owned(),
285            };
286            store::append_deferred(&db_path, trace_id, &dv).unwrap();
287        }
288        for trace_id in &incorrect_ids {
289            let dv = DeferredVerdict {
290                gate_id: "outcome".to_owned(),
291                verdict: Verdict::Fail,
292                score: None,
293                reported_at: jiff::Timestamp::now(),
294                reporter: "unit-test".to_owned(),
295            };
296            store::append_deferred(&db_path, trace_id, &dv).unwrap();
297        }
298
299        // alpha=0.2 for the same Hoeffding-slack reason as the calibrate_pairs test above.
300        let report = calibrate_from_store(&db_path, 0.2, 0.1, 30).unwrap();
301        assert_eq!(
302            report.n_pairs, 80,
303            "only the 80 traces with deferred feedback pair up"
304        );
305        assert!(report.conformal.feasible);
306        assert!(
307            report.empirical_served_failure <= 0.2 + 1e-9,
308            "empirical served-failure {} must respect alpha on clean synthetic data",
309            report.empirical_served_failure
310        );
311
312        let _ = std::fs::remove_file(&db_path);
313    }
314}