asupersync 0.3.0

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//! G8: Anytime-valid regression detection with conformal/e-value guardrails.
//!
//! Provides statistically principled regression detection for RaptorQ decode
//! performance metrics using conformal calibration envelopes and e-process
//! sequential testing. Replaces fixed-threshold regression gates with
//! adaptive, distribution-free guardrails that maintain valid coverage
//! guarantees while reducing false-positive rates.
//!
//! # Architecture
//!
//! ```text
//! DecodeStats ──► RegressionMonitor
//!                    ├─ HealthThresholdCalibrator (conformal envelope)
//!                    ├─ EProcess per metric (anytime-valid evidence)
//!                    └─ RegressionVerdict (accept / regressed / calibrating)
//! ```
//!
//! # Determinism
//!
//! All operations are deterministic for fixed input sequences. No randomness
//! or floating-point non-determinism is introduced. The conformal calibrator
//! uses exact order-statistic computation and the e-process uses a fixed-lambda
//! product martingale.

use crate::lab::conformal::{HealthThresholdCalibrator, HealthThresholdConfig, ThresholdMode};
use crate::lab::oracle::eprocess::{EProcess, EProcessConfig};
use crate::raptorq::decoder::DecodeStats;
use serde::Serialize;
use serde_json::to_string as json_string;
use std::collections::BTreeMap;

/// Schema version for G8 regression artifacts.
pub const G8_SCHEMA_VERSION: &str = "raptorq-g8-anytime-regression-v1";

/// Replay pointer for G8 regression detection events.
pub const G8_REPLAY_REF: &str = "replay:rq-track-g-regression-v1";

/// Minimum calibration samples before regression detection activates.
const MIN_CALIBRATION_SAMPLES: usize = 10;

/// Significance level for conformal coverage guarantee (95% coverage).
const CONFORMAL_ALPHA: f64 = 0.05;

/// Null hypothesis violation probability for e-process (0.1% baseline rate).
const EPROCESS_P0: f64 = 0.10;

/// Bet size for e-process martingale (conservative).
const EPROCESS_LAMBDA: f64 = 0.5;

/// Significance level for e-process rejection.
const EPROCESS_ALPHA: f64 = 0.05;

/// Metric names extracted from `DecodeStats` for regression tracking.
const TRACKED_METRICS: &[&str] = &[
    "gauss_ops",
    "dense_core_rows",
    "dense_core_cols",
    "inactivated",
    "pivots_selected",
    "peel_frontier_peak",
];

/// Verdict from a single regression check.
///
/// Ordered by severity: Accept < Calibrating < Warning < Regressed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum RegressionVerdict {
    /// Observation is within conformal envelope — no regression.
    Accept,
    /// Not enough calibration data yet.
    Calibrating,
    /// Observation exceeds conformal threshold but e-value hasn't rejected.
    Warning,
    /// E-process has rejected H₀ — statistically significant regression.
    Regressed,
}

impl RegressionVerdict {
    /// Label for structured logging.
    #[must_use]
    #[inline]
    pub const fn label(self) -> &'static str {
        match self {
            Self::Calibrating => "calibrating",
            Self::Accept => "accept",
            Self::Warning => "warning",
            Self::Regressed => "regressed",
        }
    }
}

/// Per-metric regression check result.
#[derive(Debug, Clone, Serialize)]
pub struct MetricRegressionResult {
    /// Metric name.
    pub metric: String,
    /// Observed value.
    pub value: f64,
    /// Conformal threshold (if calibrated).
    pub threshold: Option<f64>,
    /// Current e-value (evidence against H₀).
    pub e_value: f64,
    /// Number of calibration samples.
    pub calibration_n: usize,
    /// Whether the observation exceeds the conformal threshold.
    pub exceeds_threshold: bool,
    /// Verdict.
    pub verdict: RegressionVerdict,
}

/// Aggregate regression report for a single observation.
#[derive(Debug, Clone, Serialize)]
pub struct RegressionReport {
    /// Schema version.
    pub schema_version: &'static str,
    /// Replay pointer.
    pub replay_ref: &'static str,
    /// Per-metric results.
    pub metrics: Vec<MetricRegressionResult>,
    /// Overall verdict (worst-case across all metrics).
    pub overall_verdict: RegressionVerdict,
    /// Total observations processed.
    pub total_observations: usize,
    /// Number of metrics currently in regression.
    pub regressed_count: usize,
    /// Number of metrics in warning state.
    pub warning_count: usize,
    /// Regime state covariate (from F6 detector, if available).
    pub regime_state: Option<String>,
}

/// Anytime-valid regression monitor for RaptorQ decode metrics.
///
/// Combines conformal calibration envelopes with e-process sequential testing
/// to provide:
/// - Distribution-free adaptive thresholds (via conformal prediction)
/// - Anytime-valid evidence accumulation (via e-process martingale)
/// - Reduced false-positive rate vs. static thresholds
/// - Deterministic, replayable regression decisions
///
/// # Usage
///
/// 1. Create a monitor with `RegressionMonitor::new()`
/// 2. Feed baseline `DecodeStats` observations via `calibrate()`
/// 3. Check new observations via `check()` which returns a `RegressionReport`
/// 4. The monitor automatically transitions from calibrating → active
///
/// # Safety
///
/// - The conformal guarantee (P(conforming) ≥ 1 - α) holds for exchangeable
///   observations without distributional assumptions.
/// - The e-process guarantee (P(false rejection) ≤ α) holds anytime, not
///   just at a fixed sample size.
pub struct RegressionMonitor {
    /// Conformal calibrator for adaptive thresholds.
    calibrator: HealthThresholdCalibrator,
    /// Per-metric e-process for sequential testing.
    e_processes: BTreeMap<String, EProcess>,
    /// Total observations processed.
    total_observations: usize,
    /// Whether the calibration phase is complete.
    calibration_complete: bool,
}

impl RegressionMonitor {
    /// Create a new regression monitor with default configuration.
    #[must_use]
    #[inline]
    pub fn new() -> Self {
        let config = HealthThresholdConfig::new(CONFORMAL_ALPHA, ThresholdMode::Upper)
            .min_samples(MIN_CALIBRATION_SAMPLES);
        let calibrator = HealthThresholdCalibrator::new(config);

        let eprocess_config = EProcessConfig {
            p0: EPROCESS_P0,
            lambda: EPROCESS_LAMBDA,
            alpha: EPROCESS_ALPHA,
            ..Default::default()
        };

        let mut e_processes = BTreeMap::new();
        for &metric in TRACKED_METRICS {
            e_processes.insert(
                metric.to_string(),
                EProcess::new_without_history(metric, eprocess_config.clone()),
            );
        }

        Self {
            calibrator,
            e_processes,
            total_observations: 0,
            calibration_complete: false,
        }
    }

    /// Feed a baseline observation for calibration.
    ///
    /// Call this with `DecodeStats` from known-good baseline runs.
    pub fn calibrate(&mut self, stats: &DecodeStats) {
        let values = Self::extract_metrics(stats);
        for (metric, value) in &values {
            self.calibrator.calibrate(metric, *value);
        }
        self.total_observations += 1;

        // Check if all metrics are calibrated.
        if !self.calibration_complete {
            self.calibration_complete = TRACKED_METRICS
                .iter()
                .all(|m| self.calibrator.is_metric_calibrated(m));
        }
    }

    /// Check a new observation against calibrated thresholds and e-process.
    ///
    /// Returns a regression report with per-metric verdicts and an overall
    /// verdict (worst-case across all metrics).
    #[must_use]
    pub fn check(&mut self, stats: &DecodeStats) -> RegressionReport {
        let values = Self::extract_metrics(stats);
        let mut results = Vec::with_capacity(TRACKED_METRICS.len());
        let mut overall_verdict = RegressionVerdict::Accept;
        let mut regressed_count = 0usize;
        let mut warning_count = 0usize;

        self.total_observations += 1;

        for (metric, value) in &values {
            let check_result = if self.calibrator.threshold(metric).is_some() {
                self.calibrator.check_and_track(metric, *value)
            } else {
                // Grow the split-conformal calibration set until the metric has
                // enough baseline observations. The activating observation is
                // treated as calibration-only, never scored against itself.
                self.calibrator.calibrate(metric, *value);
                None
            };

            let (threshold, exceeds_threshold, calibration_n) =
                check_result.as_ref().map_or((None, false, 0), |cr| {
                    (Some(cr.threshold), !cr.conforming, cr.calibration_n)
                });

            // Do not consume e-process evidence budget before the conformal
            // envelope is active for this metric.
            if threshold.is_some()
                && let Some(ep) = self.e_processes.get_mut(metric.as_str())
            {
                ep.observe(exceeds_threshold);
            }

            let e_value = self
                .e_processes
                .get(metric.as_str())
                .map_or(1.0, EProcess::e_value);

            let e_rejected = self
                .e_processes
                .get(metric.as_str())
                .is_some_and(|ep| ep.rejected);

            let verdict = if threshold.is_none() {
                RegressionVerdict::Calibrating
            } else if e_rejected {
                RegressionVerdict::Regressed
            } else if exceeds_threshold {
                RegressionVerdict::Warning
            } else {
                RegressionVerdict::Accept
            };

            match verdict {
                RegressionVerdict::Regressed => regressed_count += 1,
                RegressionVerdict::Warning => warning_count += 1,
                _ => {}
            }

            // Promote overall verdict.
            if (verdict as u8) > (overall_verdict as u8) {
                overall_verdict = verdict;
            }

            results.push(MetricRegressionResult {
                metric: metric.clone(),
                value: *value,
                threshold,
                e_value,
                calibration_n,
                exceeds_threshold,
                verdict,
            });
        }

        if !self.calibration_complete {
            self.calibration_complete = TRACKED_METRICS
                .iter()
                .all(|metric| self.calibrator.is_metric_calibrated(metric));
        }

        let regime_state = stats
            .policy_mode
            .map(ToString::to_string)
            .or_else(|| stats.hard_regime_branch.map(ToString::to_string))
            .or_else(|| {
                stats
                    .hard_regime_activated
                    .then(|| "hard-regime".to_string())
            });

        RegressionReport {
            schema_version: G8_SCHEMA_VERSION,
            replay_ref: G8_REPLAY_REF,
            metrics: results,
            overall_verdict,
            total_observations: self.total_observations,
            regressed_count,
            warning_count,
            regime_state,
        }
    }

    /// Whether the calibration phase is complete.
    #[must_use]
    pub fn is_calibrated(&self) -> bool {
        self.calibration_complete
    }

    /// Total observations processed (calibration + checks).
    #[must_use]
    pub fn total_observations(&self) -> usize {
        self.total_observations
    }

    /// Get the current e-value for a specific metric.
    #[must_use]
    pub fn e_value(&self, metric: &str) -> Option<f64> {
        self.e_processes.get(metric).map(EProcess::e_value)
    }

    /// Get the current conformal threshold for a specific metric.
    #[must_use]
    pub fn threshold(&self, metric: &str) -> Option<f64> {
        self.calibrator.threshold(metric)
    }

    /// Check if any metric has a statistically significant regression.
    #[must_use]
    pub fn any_regressed(&self) -> bool {
        self.e_processes.values().any(|ep| ep.rejected)
    }

    /// Get names of all regressed metrics.
    #[must_use]
    pub fn regressed_metrics(&self) -> Vec<String> {
        self.e_processes
            .iter()
            .filter(|(_, ep)| ep.rejected)
            .map(|(name, _)| name.clone())
            .collect()
    }

    /// Reset all e-processes (clear accumulated evidence) while keeping
    /// calibration state. Useful after addressing a regression.
    pub fn reset_evidence(&mut self) {
        for ep in self.e_processes.values_mut() {
            ep.reset();
        }
    }

    /// Extract tracked metric values from `DecodeStats`.
    #[allow(clippy::cast_precision_loss)]
    fn extract_metrics(stats: &DecodeStats) -> Vec<(String, f64)> {
        vec![
            ("gauss_ops".to_string(), stats.gauss_ops as f64),
            ("dense_core_rows".to_string(), stats.dense_core_rows as f64),
            ("dense_core_cols".to_string(), stats.dense_core_cols as f64),
            ("inactivated".to_string(), stats.inactivated as f64),
            ("pivots_selected".to_string(), stats.pivots_selected as f64),
            (
                "peel_frontier_peak".to_string(),
                stats.peel_frontier_peak as f64,
            ),
        ]
    }
}

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

/// Render structured NDJSON log lines for a regression check.
///
/// Callers decide whether to write, persist, or discard the rendered lines.
#[must_use]
pub fn regression_log_lines(report: &RegressionReport) -> Vec<String> {
    report
        .metrics
        .iter()
        .map(|result| {
            let schema_version =
                json_string(report.schema_version).expect("schema version should serialize");
            let replay_ref = json_string(report.replay_ref).expect("replay ref should serialize");
            let metric = json_string(&result.metric).expect("metric should serialize");
            let regime_state = json_string(report.regime_state.as_deref().unwrap_or("unknown"))
                .expect("regime state should serialize");
            let verdict = json_string(result.verdict.label()).expect("verdict should serialize");
            format!(
                "{{\"schema_version\":{},\"replay_ref\":{},\
             \"metric\":{},\"value\":{:.3},\"threshold\":{},\
             \"e_value\":{:.6},\"exceeds_threshold\":{},\
             \"verdict\":{},\"calibration_n\":{},\
             \"total_observations\":{},\"regime_state\":{}}}",
                schema_version,
                replay_ref,
                metric,
                result.value,
                result
                    .threshold
                    .map_or_else(|| "null".to_string(), |t| format!("{t:.3}")),
                result.e_value,
                result.exceeds_threshold,
                verdict,
                result.calibration_n,
                report.total_observations,
                regime_state,
            )
        })
        .collect()
}

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

    fn make_baseline_stats(gauss_ops: usize, inactivated: usize) -> DecodeStats {
        DecodeStats {
            gauss_ops,
            inactivated,
            dense_core_rows: gauss_ops / 2,
            dense_core_cols: gauss_ops / 3,
            pivots_selected: inactivated,
            peel_frontier_peak: 4,
            policy_mode: Some("stable"),
            ..Default::default()
        }
    }

    #[test]
    fn monitor_starts_in_calibrating_state() {
        let mut monitor = RegressionMonitor::new();
        let stats = make_baseline_stats(10, 3);
        let report = monitor.check(&stats);
        assert_eq!(
            report.overall_verdict,
            RegressionVerdict::Calibrating,
            "should be calibrating before min samples"
        );
        for metric in TRACKED_METRICS {
            assert!(
                (monitor.e_value(metric).expect("tracked metric") - 1.0).abs() < f64::EPSILON,
                "pre-calibration check should not mutate e-value for {metric}"
            );
        }
    }

    #[test]
    fn check_only_warmup_marks_monitor_calibrated() {
        let mut monitor = RegressionMonitor::new();

        for i in 0..(MIN_CALIBRATION_SAMPLES + 5) {
            let stats = make_baseline_stats(10 + i % 3, 3 + i % 2);
            let _ = monitor.check(&stats);
        }

        assert!(
            monitor.is_calibrated(),
            "check-only warmup should flip the public calibration state once all metrics are calibrated"
        );
        assert!(
            TRACKED_METRICS
                .iter()
                .all(|metric| monitor.threshold(metric).is_some()),
            "every tracked metric should expose a threshold after check-only warmup"
        );
    }

    #[test]
    fn regression_log_lines_render_one_line_per_metric() {
        let report = RegressionReport {
            schema_version: G8_SCHEMA_VERSION,
            replay_ref: G8_REPLAY_REF,
            metrics: vec![MetricRegressionResult {
                metric: "gauss_ops".to_string(),
                value: 12.0,
                threshold: Some(15.0),
                e_value: 1.25,
                calibration_n: 10,
                exceeds_threshold: false,
                verdict: RegressionVerdict::Accept,
            }],
            overall_verdict: RegressionVerdict::Accept,
            total_observations: 11,
            regressed_count: 0,
            warning_count: 0,
            regime_state: Some("stable".to_string()),
        };

        let lines = regression_log_lines(&report);

        assert_eq!(lines.len(), 1);
        assert!(lines[0].contains("\"metric\":\"gauss_ops\""));
        assert!(lines[0].contains("\"verdict\":\"accept\""));
        assert!(lines[0].contains("\"regime_state\":\"stable\""));
    }

    #[test]
    fn regression_log_lines_escape_string_fields() {
        let regime_state = "retuned\\fallback\"line\nbreak".to_string();
        let metric = "gauss\"ops\nburst".to_string();
        let report = RegressionReport {
            schema_version: G8_SCHEMA_VERSION,
            replay_ref: G8_REPLAY_REF,
            metrics: vec![MetricRegressionResult {
                metric: metric.clone(),
                value: 12.0,
                threshold: Some(15.0),
                e_value: 1.25,
                calibration_n: 10,
                exceeds_threshold: false,
                verdict: RegressionVerdict::Accept,
            }],
            overall_verdict: RegressionVerdict::Accept,
            total_observations: 11,
            regressed_count: 0,
            warning_count: 0,
            regime_state: Some(regime_state.clone()),
        };

        let lines = regression_log_lines(&report);
        let parsed: serde_json::Value =
            serde_json::from_str(&lines[0]).expect("rendered line must stay valid JSON");

        assert_eq!(parsed["metric"].as_str(), Some(metric.as_str()));
        assert_eq!(parsed["regime_state"].as_str(), Some(regime_state.as_str()));
    }

    #[test]
    fn monitor_transitions_to_accept_after_calibration() {
        let mut monitor = RegressionMonitor::new();

        // Calibrate with baseline observations.
        for i in 0..MIN_CALIBRATION_SAMPLES + 5 {
            let stats = make_baseline_stats(10 + i % 3, 3);
            monitor.calibrate(&stats);
        }
        assert!(
            monitor.is_calibrated(),
            "should be calibrated after {} samples",
            MIN_CALIBRATION_SAMPLES + 5
        );

        // Check a normal observation.
        let stats = make_baseline_stats(11, 3);
        let report = monitor.check(&stats);
        assert_eq!(
            report.overall_verdict,
            RegressionVerdict::Accept,
            "normal observation should be accepted"
        );
    }

    #[test]
    fn monitor_detects_warning_on_threshold_exceedance() {
        let mut monitor = RegressionMonitor::new();

        // Calibrate with small values.
        for i in 0..(MIN_CALIBRATION_SAMPLES + 5) {
            let stats = make_baseline_stats(10 + i % 2, 3);
            monitor.calibrate(&stats);
        }

        // Check with a much larger value.
        let stats = make_baseline_stats(1000, 3);
        let report = monitor.check(&stats);
        assert!(
            matches!(
                report.overall_verdict,
                RegressionVerdict::Warning | RegressionVerdict::Regressed
            ),
            "large deviation should trigger warning or regression, got {:?}",
            report.overall_verdict
        );
    }

    #[test]
    fn monitor_accumulates_evidence_for_regression() {
        let mut monitor = RegressionMonitor::new();

        // Calibrate with small values.
        for i in 0..(MIN_CALIBRATION_SAMPLES + 5) {
            let stats = make_baseline_stats(10 + i % 2, 3);
            monitor.calibrate(&stats);
        }

        // Feed many anomalous observations to accumulate e-value evidence.
        let mut any_regressed = false;
        for _ in 0..200 {
            let stats = make_baseline_stats(1000, 100);
            let report = monitor.check(&stats);
            if report.overall_verdict == RegressionVerdict::Regressed {
                any_regressed = true;
                break;
            }
        }

        assert!(
            any_regressed,
            "sustained large deviation should trigger regression detection"
        );
        assert!(monitor.any_regressed());
        assert!(!monitor.regressed_metrics().is_empty());
    }

    #[test]
    fn pre_calibration_checks_do_not_dilute_later_regression_evidence() {
        let mut clean = RegressionMonitor::new();
        let mut with_prechecks = RegressionMonitor::new();

        for i in 0..MIN_CALIBRATION_SAMPLES {
            let stats = make_baseline_stats(10 + i % 2, 3);
            clean.calibrate(&stats);
            let report = with_prechecks.check(&stats);
            assert_eq!(
                report.overall_verdict,
                RegressionVerdict::Calibrating,
                "warmup observations fed via check() must remain calibration-only until activation"
            );
        }

        let anomaly = make_baseline_stats(1_000, 100);
        let clean_report = clean.check(&anomaly);
        let prechecked_report = with_prechecks.check(&anomaly);

        assert_eq!(
            clean_report.overall_verdict, prechecked_report.overall_verdict,
            "pre-calibration checks should not change the first live verdict"
        );
        for metric in TRACKED_METRICS {
            let clean_e = clean.e_value(metric).expect("tracked metric");
            let prechecked_e = with_prechecks.e_value(metric).expect("tracked metric");
            assert!(
                (clean_e - prechecked_e).abs() < f64::EPSILON,
                "pre-calibration checks should not dilute e-process evidence for {metric}: clean={clean_e}, prechecked={prechecked_e}"
            );
        }
    }

    #[test]
    fn metamorphic_decode_tolerance_bound_is_monotone_at_threshold_edge() {
        fn calibrated_monitor() -> RegressionMonitor {
            let mut monitor = RegressionMonitor::new();
            for i in 0..(MIN_CALIBRATION_SAMPLES + 5) {
                let stats = make_baseline_stats(10 + i % 3, 3);
                monitor.calibrate(&stats);
            }
            monitor
        }

        let threshold_probe = calibrated_monitor();
        let threshold = threshold_probe
            .threshold("gauss_ops")
            .expect("gauss_ops threshold should be calibrated");

        let tolerated_value = threshold.floor().max(10.0) as usize;
        let violating_value = threshold.ceil() as usize + 1;

        let mut tolerated_stats = make_baseline_stats(10, 3);
        tolerated_stats.gauss_ops = tolerated_value;
        let mut violating_stats = tolerated_stats.clone();
        violating_stats.gauss_ops = violating_value;

        let tolerated_report = calibrated_monitor().check(&tolerated_stats);
        let violating_report = calibrated_monitor().check(&violating_stats);

        let tolerated_metric = tolerated_report
            .metrics
            .iter()
            .find(|metric| metric.metric == "gauss_ops")
            .expect("gauss_ops metric missing from tolerated report");
        let violating_metric = violating_report
            .metrics
            .iter()
            .find(|metric| metric.metric == "gauss_ops")
            .expect("gauss_ops metric missing from violating report");

        assert!(
            !tolerated_metric.exceeds_threshold,
            "value {tolerated_value} should remain within threshold {threshold}"
        );
        assert_eq!(
            tolerated_metric.verdict,
            RegressionVerdict::Accept,
            "within-threshold observation should be accepted"
        );
        assert!(
            violating_metric.exceeds_threshold,
            "value {violating_value} should exceed threshold {threshold}"
        );
        assert!(
            matches!(
                violating_metric.verdict,
                RegressionVerdict::Warning | RegressionVerdict::Regressed
            ),
            "threshold violation should escalate verdict, got {:?}",
            violating_metric.verdict
        );
        assert!(
            (violating_report.overall_verdict as u8) >= (tolerated_report.overall_verdict as u8),
            "crossing the learned tolerance bound must not lower overall severity"
        );
    }

    #[test]
    fn monitor_stable_workload_no_false_alarm() {
        let mut monitor = RegressionMonitor::new();

        // Calibrate.
        for i in 0..(MIN_CALIBRATION_SAMPLES + 5) {
            let stats = make_baseline_stats(10 + i % 3, 3 + i % 2);
            monitor.calibrate(&stats);
        }

        // Check 100 normal observations — no false alarm.
        for i in 0..100 {
            let stats = make_baseline_stats(10 + i % 3, 3 + i % 2);
            let report = monitor.check(&stats);
            assert_ne!(
                report.overall_verdict,
                RegressionVerdict::Regressed,
                "stable workload should not trigger false alarm at check {i}"
            );
        }

        assert!(
            !monitor.any_regressed(),
            "stable workload should have no regressions"
        );
    }

    #[test]
    fn monitor_reset_evidence_clears_e_processes() {
        let mut monitor = RegressionMonitor::new();

        // Calibrate and force regression.
        for i in 0..(MIN_CALIBRATION_SAMPLES + 5) {
            let stats = make_baseline_stats(10, 3 + i % 2);
            monitor.calibrate(&stats);
        }
        for _ in 0..200 {
            let stats = make_baseline_stats(1000, 100);
            let _ = monitor.check(&stats);
        }

        // Reset evidence.
        monitor.reset_evidence();
        assert!(
            !monitor.any_regressed(),
            "reset should clear regression state"
        );

        // Normal observations should be accepted.
        let stats = make_baseline_stats(11, 3);
        let report = monitor.check(&stats);
        assert_ne!(
            report.overall_verdict,
            RegressionVerdict::Regressed,
            "post-reset should not show regression"
        );
    }

    #[test]
    fn regression_report_schema_and_replay_ref() {
        let mut monitor = RegressionMonitor::new();

        for i in 0..(MIN_CALIBRATION_SAMPLES + 5) {
            let stats = make_baseline_stats(10 + i, 3);
            monitor.calibrate(&stats);
        }

        let stats = make_baseline_stats(10, 3);
        let report = monitor.check(&stats);
        assert_eq!(report.schema_version, G8_SCHEMA_VERSION);
        assert_eq!(report.replay_ref, G8_REPLAY_REF);
        assert_eq!(report.metrics.len(), TRACKED_METRICS.len());
    }

    #[test]
    fn verdict_ordering_is_correct() {
        assert!((RegressionVerdict::Accept as u8) < (RegressionVerdict::Calibrating as u8));
        assert!((RegressionVerdict::Calibrating as u8) < (RegressionVerdict::Warning as u8));
        assert!((RegressionVerdict::Warning as u8) < (RegressionVerdict::Regressed as u8));
    }

    #[test]
    fn e_value_and_threshold_accessors() {
        let mut monitor = RegressionMonitor::new();

        // Before calibration.
        assert!(monitor.threshold("gauss_ops").is_none());

        for i in 0..(MIN_CALIBRATION_SAMPLES + 5) {
            let stats = make_baseline_stats(10 + i, 3);
            monitor.calibrate(&stats);
        }

        // After calibration.
        assert!(monitor.threshold("gauss_ops").is_some());
        assert!(monitor.e_value("gauss_ops").is_some());
    }

    #[test]
    fn regime_state_covariate_captured() {
        let mut monitor = RegressionMonitor::new();

        for i in 0..(MIN_CALIBRATION_SAMPLES + 5) {
            let stats = make_baseline_stats(10 + i, 3);
            monitor.calibrate(&stats);
        }

        let mut stats = make_baseline_stats(10, 3);
        stats.policy_mode = Some("retuned");
        let report = monitor.check(&stats);
        assert_eq!(
            report.regime_state,
            Some("retuned".to_string()),
            "regime state should be captured from DecodeStats"
        );
    }

    #[test]
    fn deterministic_replay_produces_identical_reports() {
        let observations: Vec<DecodeStats> = (0..50)
            .map(|i| make_baseline_stats(10 + i % 5, 3 + i % 3))
            .collect();

        let mut mon_a = RegressionMonitor::new();
        let mut mon_b = RegressionMonitor::new();

        // Calibrate both.
        for obs in observations.iter().take(MIN_CALIBRATION_SAMPLES + 5) {
            mon_a.calibrate(obs);
            mon_b.calibrate(obs);
        }

        // Check remaining.
        for obs in observations.iter().skip(MIN_CALIBRATION_SAMPLES + 5) {
            let report_a = mon_a.check(obs);
            let report_b = mon_b.check(obs);

            assert_eq!(
                report_a.overall_verdict, report_b.overall_verdict,
                "deterministic replay violated"
            );
            for (ra, rb) in report_a.metrics.iter().zip(report_b.metrics.iter()) {
                assert_eq!(ra.metric, rb.metric);
                assert!(
                    (ra.e_value - rb.e_value).abs() < f64::EPSILON,
                    "expected {}, got {}",
                    rb.e_value,
                    ra.e_value
                );
                assert_eq!(ra.verdict, rb.verdict);
            }
        }
    }
}