veridict 0.19.7

Statistical regression gate for A/B and hypothesis testing: compare candidate vs baseline results with win-rate, Elo, SPRT, and bootstrap confidence intervals.
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
//! Turns an effect size + confidence interval into a pass/fail/inconclusive
//! decision.
//!
//! The gate compares the CI, not the point estimate, against the thresholds:
//! a candidate only passes if it beats `pass_above` *even at the pessimistic
//! (lower) end* of the interval, and only fails if it's below `fail_below`
//! *even at the optimistic (upper) end*. Anything else is inconclusive. This
//! is deliberately conservative per AGENTS.md: "a false pass is worse than
//! an inconclusive result."

use crate::error::VeridictError;
use crate::metrics;
use crate::report::{MultiReport, Report};
use crate::stats::{elo, exact, jeffreys, wilson};
use crate::{CiMethod, FailureCaps, InconclusiveKind, MetricKind, Promotion, Validity, Verdict};

pub struct Thresholds {
    pub pass_above: f64,
    pub fail_below: f64,
}

impl Thresholds {
    pub fn new(pass_above: f64, fail_below: f64) -> Result<Self, VeridictError> {
        if !pass_above.is_finite() || !fail_below.is_finite() {
            return Err(VeridictError::InvalidThreshold(
                "pass_above/fail_below must be finite".to_string(),
            ));
        }
        if fail_below > pass_above {
            return Err(VeridictError::InvalidThreshold(format!(
                "fail_below ({fail_below}) must be <= pass_above ({pass_above})"
            )));
        }
        Ok(Self {
            pass_above,
            fail_below,
        })
    }

    /// Symmetric thresholds around zero: pass above `+min_effect`, fail
    /// below `-min_effect`. What `--min-effect` maps to on the CLI.
    pub fn symmetric(min_effect: f64) -> Result<Self, VeridictError> {
        Self::new(min_effect, -min_effect)
    }
}

/// Combines several metrics' verdicts into one overall verdict for a
/// multi-metric run: any `Fail` sinks the whole run, else any
/// `Inconclusive` holds it back, else `Pass`. An empty iterator has nothing
/// to fail or hold back on, so it counts as `Pass` (compare_many never
/// calls this with zero metrics in practice, since the CLI requires at
/// least one `--metric`).
pub fn aggregate(verdicts: impl IntoIterator<Item = Verdict>) -> Verdict {
    let mut worst = Verdict::Pass;
    for v in verdicts {
        match v {
            Verdict::Fail => return Verdict::Fail,
            Verdict::Inconclusive => worst = Verdict::Inconclusive,
            Verdict::Pass => {}
        }
    }
    worst
}

/// Rough estimate of how many *additional* trials (beyond `paired_count`)
/// would be needed to turn an `Inconclusive` verdict decisive, assuming the
/// CI half-width keeps shrinking as `O(1/sqrt(n))` (the standard CLT
/// scaling) and the effect size itself doesn't move. `None` when there's
/// nothing meaningful to suggest:
/// - the verdict is already `Pass`/`Fail` (nothing left to resolve),
/// - `paired_count == 0` (no rate to scale from),
/// - `effect` is inside `[fail_below, pass_above]` (inclusive) - the "dead
///   zone". This is deliberate, not a missing case: shrinking the CI
///   *around a fixed point estimate already inside the dead zone* can never
///   cross either boundary, no matter how large `n` gets - verified
///   concretely (effect=0.0, thresholds=+-0.02, n=100): the naive formula
///   suggests ~2313 more trials, but the verdict is still `Inconclusive` at
///   that n, and still `Inconclusive` at n=10,000,000. Only a genuinely
///   different effect size resolves a dead-zone result, not more data alone.
/// - the current CI half-width is non-finite, non-positive, or already at
///   or below the target (can happen since real CIs, e.g. Wilson's, aren't
///   perfectly symmetric around `effect` - this formula assumes they are).
///
/// For `winrate`/`sign-test`/`elo`, this binary-searches against the exact
/// same tested CI function the report displays (`wilson`/`jeffreys`/`exact`,
/// per `ci_method` - Wilson/Jeffreys/Exact differ in width at the same `n`,
/// so searching the wrong one would contradict the report's own shown CI),
/// holding the point estimate fixed - not an approximation, an exact search
/// against real, already-tested math. `mean-diff`/`quantile-diff`/`relative-diff` are the
/// exception (`metrics::lacks_closed_form_ci`): there is no "CI width at a hypothetical `n`"
/// function for a bootstrap CI without real resampled data, so all three keep the original
/// `O(1/sqrt(n))` CLT-scaling model, with its own known, quantified bias: verified within
/// ~1.5% of an actual re-run for a clean 4x sample-size jump at moderate n,
/// but a real ~18% *under*-estimate at n=100, because e.g. Wilson's CI also
/// shrinks via an `O(z^2/n)` recentering term this simple `1/sqrt(n)` model
/// doesn't capture (measured for `mean-diff`; `quantile-diff`/`relative-diff` reuse the same
/// model unverified for their own bootstrap CI, since it's the same "no closed
/// form" shape). Treat any of the three metrics' result as "roughly this
/// many, plausibly more," not a guarantee; the other metrics' result is exact
/// (mod float precision) for the stated model (point estimate unchanged).
#[allow(clippy::too_many_arguments)]
pub fn estimate_additional_trials(
    metric: MetricKind,
    ci_method: CiMethod,
    verdict: Verdict,
    effect: f64,
    ci_low: f64,
    ci_high: f64,
    paired_count: u64,
    thresholds: &Thresholds,
    confidence: f64,
) -> Option<u64> {
    if verdict != Verdict::Inconclusive || paired_count == 0 {
        return None;
    }

    let target_half_width = if effect > thresholds.pass_above {
        effect - thresholds.pass_above
    } else if effect < thresholds.fail_below {
        thresholds.fail_below - effect
    } else {
        return None;
    };

    let current_half_width = (ci_high - ci_low) / 2.0;
    if !current_half_width.is_finite() || current_half_width <= target_half_width {
        return None;
    }

    let ratio = current_half_width / target_half_width;
    let naive_required_n = paired_count as f64 * ratio * ratio;
    if !naive_required_n.is_finite() || naive_required_n >= u64::MAX as f64 {
        return Some(u64::MAX);
    }
    let naive_estimate = (naive_required_n as u64).saturating_sub(paired_count);

    if metrics::lacks_closed_form_ci(metric) {
        return Some(naive_estimate);
    }

    // Elo's p_hat isn't in `effect` directly (it's already been through
    // `elo_from_score`'s logistic transform) - recover it via that
    // transform's inverse, the standard logistic Elo win probability.
    let p_hat = if metric == MetricKind::Elo {
        1.0 / (1.0 + 10f64.powf(-effect / 400.0))
    } else {
        effect + 0.5
    };
    let half_width_at = |n: u64| -> Option<f64> {
        let n_f = n as f64;
        let (lo, hi) = if metric == MetricKind::Elo {
            let (lo, hi) = wilson::wilson_ci_from_proportion(p_hat, n_f, confidence).ok()?;
            (elo::elo_from_score(lo), elo::elo_from_score(hi))
        } else {
            match ci_method {
                CiMethod::Wilson => {
                    wilson::wilson_ci_from_proportion(p_hat, n_f, confidence).ok()?
                }
                CiMethod::Jeffreys => {
                    let successes = (p_hat * n_f).round().clamp(0.0, n_f) as u64;
                    jeffreys::jeffreys_ci(successes, n, confidence).ok()?
                }
                CiMethod::Exact => {
                    let successes = (p_hat * n_f).round().clamp(0.0, n_f) as u64;
                    exact::clopper_pearson_ci(successes, n, confidence).ok()?
                }
            }
        };
        Some((hi - lo) / 2.0)
    };

    // The naive O(1/sqrt(n)) estimate is a known *under*-estimate on the
    // metrics being searched here, so doubling it is a safe search bracket
    // (comfortable margin for Elo's nonlinearity shifting the true crossing
    // point relative to the linear-scale approximation).
    let hi_n = ((naive_required_n * 2.0) as u64).max(paired_count + 1);
    if half_width_at(hi_n).is_none_or(|w| w > target_half_width) {
        // The search bracket doesn't hold (a CI function failed, or behaved
        // non-monotonically at this n) - fall back to the always-available
        // approximation rather than returning a possibly-wrong result.
        return Some(naive_estimate);
    }
    let mut lo_n = paired_count;
    let mut hi_n = hi_n;
    while lo_n < hi_n {
        let mid = lo_n + (hi_n - lo_n) / 2;
        match half_width_at(mid) {
            Some(w) if w <= target_half_width => hi_n = mid,
            _ => lo_n = mid + 1,
        }
    }
    Some(lo_n.saturating_sub(paired_count))
}

/// Applies `caps` to an already-built report, the same "mutate a finished
/// report" shape `correction::apply_correction` already uses for a
/// different cross-cutting concern. If any cap in `caps` is breached, forces
/// `report.verdict` to `Inconclusive` and overwrites `report.reason` (a
/// failure-invalidated run must never surface as a clean `Pass`/`Fail`,
/// possible today under `--failure-policy loss` where a crash can tip the
/// numeric verdict) and clears `estimated_additional_trials`/`inconclusive_kind` (more trials
/// can't fix a technical-failure problem, and a failure-cap-forced `Inconclusive` isn't a real CI
/// judgment either). Always sets `report.validity`/
/// `report.promotion`, even under `FailureCaps::default()` (every cap
/// `None`), which always yields `Validity::Valid` - a run that never passes
/// a `--max-*` flag is unaffected.
pub fn apply_failure_caps(report: &mut Report, caps: &FailureCaps) {
    let (validity, reason) = caps.check(report.timeouts, report.crashes, report.invalid);
    report.validity = validity;
    if validity == Validity::Invalid {
        report.verdict = Verdict::Inconclusive;
        report.estimated_additional_trials = None;
        report.inconclusive_kind = None;
        if let Some(reason) = reason {
            report.reason = format!("INVALID: {reason}. Strength not evaluated.");
        }
    }
    report.promotion = Promotion::decide(report.validity, report.verdict);
}

/// `apply_failure_caps` for every report in a multi-metric run, then
/// recomputes `MultiReport.verdict`/`validity`/`promotion` from the
/// (now caps-applied) per-report values: `validity` is `Invalid` if any
/// report's is, `verdict` re-aggregates via `aggregate` (a report forced to
/// `Inconclusive` here must actually hold the overall verdict back, not just
/// its own), and `promotion` follows the same rule as a single report.
pub fn apply_failure_caps_to_multi(multi: &mut MultiReport, caps: &FailureCaps) {
    for report in &mut multi.reports {
        apply_failure_caps(report, caps);
    }
    multi.verdict = aggregate(multi.reports.iter().map(|r| r.verdict));
    multi.validity = if multi
        .reports
        .iter()
        .any(|r| r.validity == Validity::Invalid)
    {
        Validity::Invalid
    } else {
        Validity::Valid
    };
    multi.promotion = Promotion::decide(multi.validity, multi.verdict);
}

pub fn decide(ci_low: f64, ci_high: f64, thresholds: &Thresholds) -> (Verdict, String) {
    if ci_low >= thresholds.pass_above {
        (
            Verdict::Pass,
            format!(
                "CI lower bound {ci_low:.4} meets the pass threshold {:.4}",
                thresholds.pass_above
            ),
        )
    } else if ci_high <= thresholds.fail_below {
        (
            Verdict::Fail,
            format!(
                "CI upper bound {ci_high:.4} is at or below the fail threshold {:.4}",
                thresholds.fail_below
            ),
        )
    } else {
        (
            Verdict::Inconclusive,
            format!(
                "CI [{ci_low:.4}, {ci_high:.4}] does not clearly cross the pass ({:.4}) or fail ({:.4}) threshold",
                thresholds.pass_above, thresholds.fail_below
            ),
        )
    }
}

/// Sub-classifies an `Inconclusive` result by whether `[ci_low, ci_high]` excludes zero: `Noise`
/// if it still straddles zero (inclusive boundary, matching `decide`'s own `>=`/`<=` convention -
/// a CI that merely touches zero can't rule it out either), else `Directional`. Only meaningful
/// when the caller already knows the verdict is `Inconclusive` from a real CI (see `build_report`
/// for the cases that must skip this entirely: `Pass`/`Fail`, zero-trial reports, and
/// failure-cap-forced `Inconclusive`).
///
/// Deliberately a different test from `data_quality.effect_within_noise_floor`
/// (`|effect| < half_width`, which assumes a symmetric CI): this one reads `ci_low`/`ci_high`
/// directly, so it stays exact on an asymmetric bootstrap CI (e.g. `relative-diff`'s `bca`
/// method), where the two can disagree.
pub fn classify_inconclusive(ci_low: f64, ci_high: f64) -> InconclusiveKind {
    if ci_low <= 0.0 && 0.0 <= ci_high {
        InconclusiveKind::Noise
    } else {
        InconclusiveKind::Directional
    }
}

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

    #[test]
    fn aggregate_fail_dominates_pass() {
        let v = aggregate([Verdict::Pass, Verdict::Fail, Verdict::Pass]);
        assert_eq!(v, Verdict::Fail);
    }

    #[test]
    fn aggregate_inconclusive_holds_back_pass() {
        let v = aggregate([Verdict::Pass, Verdict::Inconclusive, Verdict::Pass]);
        assert_eq!(v, Verdict::Inconclusive);
    }

    #[test]
    fn aggregate_all_pass_is_pass() {
        let v = aggregate([Verdict::Pass, Verdict::Pass]);
        assert_eq!(v, Verdict::Pass);
    }

    #[test]
    fn passes_when_lower_bound_clears_threshold() {
        let t = Thresholds::symmetric(0.02).unwrap();
        let (v, _) = decide(0.03, 0.10, &t);
        assert_eq!(v, Verdict::Pass);
    }

    #[test]
    fn fails_when_upper_bound_below_negative_threshold() {
        let t = Thresholds::symmetric(0.02).unwrap();
        let (v, _) = decide(-0.10, -0.03, &t);
        assert_eq!(v, Verdict::Fail);
    }

    #[test]
    fn inconclusive_when_ci_straddles_threshold() {
        let t = Thresholds::symmetric(0.02).unwrap();
        let (v, _) = decide(-0.01, 0.05, &t);
        assert_eq!(v, Verdict::Inconclusive);
    }

    #[test]
    fn boundary_is_inclusive_for_pass() {
        let t = Thresholds::symmetric(0.02).unwrap();
        let (v, _) = decide(0.02, 0.05, &t);
        assert_eq!(v, Verdict::Pass);
    }

    #[test]
    fn rejects_fail_above_pass() {
        let result = Thresholds::new(0.0, 0.01);
        assert!(matches!(result, Err(VeridictError::InvalidThreshold(_))));
    }

    #[test]
    fn rejects_non_finite_thresholds() {
        assert!(matches!(
            Thresholds::new(f64::NAN, 0.0),
            Err(VeridictError::InvalidThreshold(_))
        ));
        assert!(matches!(
            Thresholds::new(0.0, f64::NEG_INFINITY),
            Err(VeridictError::InvalidThreshold(_))
        ));
    }

    // --- classify_inconclusive ---

    #[test]
    fn classify_noise_when_ci_straddles_zero() {
        assert_eq!(classify_inconclusive(-0.01, 0.02), InconclusiveKind::Noise);
    }

    #[test]
    fn classify_directional_when_ci_is_all_positive() {
        assert_eq!(
            classify_inconclusive(0.001, 0.02),
            InconclusiveKind::Directional
        );
    }

    #[test]
    fn classify_directional_when_ci_is_all_negative() {
        assert_eq!(
            classify_inconclusive(-0.02, -0.001),
            InconclusiveKind::Directional
        );
    }

    #[test]
    fn classify_noise_on_ci_low_boundary_touching_zero() {
        assert_eq!(classify_inconclusive(0.0, 0.02), InconclusiveKind::Noise);
    }

    #[test]
    fn classify_noise_on_ci_high_boundary_touching_zero() {
        assert_eq!(classify_inconclusive(-0.02, 0.0), InconclusiveKind::Noise);
    }

    // --- estimate_additional_trials ---
    // MetricKind::MeanDiff exercises the unchanged O(1/sqrt(n)) formula;
    // the dead-zone/already-decided/zero-paired-count guards below all
    // return `None` before ever touching metric-specific logic, so any
    // metric/ci_method placeholder is fine for those.

    #[test]
    fn already_decided_verdicts_need_no_more_trials() {
        let t = Thresholds::symmetric(0.02).unwrap();
        assert_eq!(
            estimate_additional_trials(
                MetricKind::MeanDiff,
                CiMethod::Wilson,
                Verdict::Pass,
                0.10,
                0.05,
                0.15,
                100,
                &t,
                0.95
            ),
            None
        );
        assert_eq!(
            estimate_additional_trials(
                MetricKind::MeanDiff,
                CiMethod::Wilson,
                Verdict::Fail,
                -0.10,
                -0.15,
                -0.05,
                100,
                &t,
                0.95
            ),
            None
        );
    }

    #[test]
    fn dead_zone_point_estimate_has_no_estimate_even_at_ten_million_trials() {
        // The concrete case that surfaced the "naive formula never
        // terminates" bug: effect sits inside the threshold band itself, so
        // no amount of CI shrinkage around that fixed point can cross
        // either boundary.
        let t = Thresholds::symmetric(0.02).unwrap();
        assert_eq!(
            estimate_additional_trials(
                MetricKind::MeanDiff,
                CiMethod::Wilson,
                Verdict::Inconclusive,
                0.0,
                -0.01,
                0.03,
                100,
                &t,
                0.95
            ),
            None
        );
        assert_eq!(
            estimate_additional_trials(
                MetricKind::MeanDiff,
                CiMethod::Wilson,
                Verdict::Inconclusive,
                0.0,
                -0.01,
                0.03,
                10_000_000,
                &t,
                0.95
            ),
            None
        );
    }

    #[test]
    fn effect_exactly_on_threshold_is_dead_zone() {
        let t = Thresholds::symmetric(0.02).unwrap();
        assert_eq!(
            estimate_additional_trials(
                MetricKind::MeanDiff,
                CiMethod::Wilson,
                Verdict::Inconclusive,
                0.02,
                -0.01,
                0.05,
                100,
                &t,
                0.95
            ),
            None
        );
        assert_eq!(
            estimate_additional_trials(
                MetricKind::MeanDiff,
                CiMethod::Wilson,
                Verdict::Inconclusive,
                -0.02,
                -0.05,
                0.01,
                100,
                &t,
                0.95
            ),
            None
        );
    }

    #[test]
    fn zero_paired_count_has_no_estimate() {
        let t = Thresholds::symmetric(0.02).unwrap();
        assert_eq!(
            estimate_additional_trials(
                MetricKind::MeanDiff,
                CiMethod::Wilson,
                Verdict::Inconclusive,
                0.05,
                -0.01,
                0.10,
                0,
                &t,
                0.95
            ),
            None
        );
    }

    #[test]
    fn mean_diff_outside_dead_zone_suggests_a_plausible_trial_count() {
        // effect=0.03 is past pass_above=0.02, but the CI [-0.01, 0.07] still
        // dips below it - resolvable by more data, unlike the dead-zone case.
        // Not asserting an exact number (the formula has a documented,
        // quantified bias): just that it lands in a sane ballpark.
        let t = Thresholds::symmetric(0.02).unwrap();
        let estimate = estimate_additional_trials(
            MetricKind::MeanDiff,
            CiMethod::Wilson,
            Verdict::Inconclusive,
            0.03,
            -0.01,
            0.07,
            50,
            &t,
            0.95,
        );
        assert!(matches!(estimate, Some(n) if (100..=2000).contains(&n)));
    }

    #[test]
    fn mean_diff_tiny_target_half_width_saturates_instead_of_panicking() {
        // effect is a hair past pass_above, so target_half_width is tiny -
        // this must saturate to u64::MAX, not overflow/panic.
        let t = Thresholds::symmetric(0.02).unwrap();
        let estimate = estimate_additional_trials(
            MetricKind::MeanDiff,
            CiMethod::Wilson,
            Verdict::Inconclusive,
            0.020000001,
            -0.5,
            0.5,
            100,
            &t,
            0.95,
        );
        assert_eq!(estimate, Some(u64::MAX));
    }

    #[test]
    fn winrate_wilson_search_matches_a_direct_wilson_recompute() {
        // effect=p_hat-0.5=0.05 (p_hat=0.55), target_half_width =
        // effect-pass_above = 0.05-0.02 = 0.03; the search's answer, applied
        // back through wilson_ci_from_proportion at the SAME p_hat, must
        // actually clear that width - not just "close to a formula", an
        // exact self-consistency check against the real CI function.
        let t = Thresholds::symmetric(0.02).unwrap();
        let target_half_width = 0.05 - 0.02;
        let (lo, hi) = wilson::wilson_ci(55, 100, 0.95).unwrap();
        let estimate = estimate_additional_trials(
            MetricKind::WinRate,
            CiMethod::Wilson,
            Verdict::Inconclusive,
            0.05,
            lo - 0.5,
            hi - 0.5,
            100,
            &t,
            0.95,
        )
        .unwrap();
        let required_n = 100 + estimate;
        let (rlo, rhi) = wilson::wilson_ci_from_proportion(0.55, required_n as f64, 0.95).unwrap();
        assert!((rhi - rlo) / 2.0 <= target_half_width + 1e-6);
        // One fewer trial should NOT already clear it (search found the
        // smallest n, not an overshoot).
        let (rlo2, rhi2) =
            wilson::wilson_ci_from_proportion(0.55, (required_n - 1) as f64, 0.95).unwrap();
        assert!((rhi2 - rlo2) / 2.0 > target_half_width - 1e-6);
    }

    #[test]
    fn elo_search_recovers_a_consistent_p_hat_and_clears_the_target() {
        let t = Thresholds::new(50.0, -50.0).unwrap();
        let score = 0.6;
        let (wlo, whi) = wilson::wilson_ci_from_proportion(score, 40.0, 0.95).unwrap();
        let effect = elo::elo_from_score(score);
        let target_half_width = effect - 50.0;
        let estimate = estimate_additional_trials(
            MetricKind::Elo,
            CiMethod::Wilson,
            Verdict::Inconclusive,
            effect,
            elo::elo_from_score(wlo),
            elo::elo_from_score(whi),
            40,
            &t,
            0.95,
        )
        .unwrap();
        let required_n = 40 + estimate;
        let (rlo, rhi) = wilson::wilson_ci_from_proportion(score, required_n as f64, 0.95).unwrap();
        let elo_half_width = (elo::elo_from_score(rhi) - elo::elo_from_score(rlo)) / 2.0;
        assert!(elo_half_width <= target_half_width + 1e-6);
    }

    #[test]
    fn jeffreys_search_differs_from_wilson_search_at_the_same_inputs() {
        // Wilson and Jeffreys have different widths at the same n, so
        // searching the wrong one would contradict the report's own shown
        // CI - this checks the two searches are NOT simply reusing the same
        // underlying computation, i.e. `ci_method` is actually load-bearing.
        let t = Thresholds::symmetric(0.02).unwrap();
        let (wlo, whi) = wilson::wilson_ci(55, 100, 0.95).unwrap();
        let (jlo, jhi) = jeffreys::jeffreys_ci(55, 100, 0.95).unwrap();
        let wilson_estimate = estimate_additional_trials(
            MetricKind::WinRate,
            CiMethod::Wilson,
            Verdict::Inconclusive,
            0.05,
            wlo - 0.5,
            whi - 0.5,
            100,
            &t,
            0.95,
        );
        let jeffreys_estimate = estimate_additional_trials(
            MetricKind::WinRate,
            CiMethod::Jeffreys,
            Verdict::Inconclusive,
            0.05,
            jlo - 0.5,
            jhi - 0.5,
            100,
            &t,
            0.95,
        );
        assert!(wilson_estimate.is_some() && jeffreys_estimate.is_some());
        assert_ne!(wilson_estimate, jeffreys_estimate);
    }

    #[test]
    fn search_based_estimate_is_closer_to_a_true_rerun_than_the_old_naive_formula() {
        // The documented ~18%-under-estimate case (n=100, p_hat past
        // threshold): the search-based estimate's suggested n must actually
        // clear the target width when re-run for real, unlike the old
        // formula (which the module doc already records as falling short
        // here).
        let t = Thresholds::symmetric(0.02).unwrap();
        let p_hat = 0.55;
        let target_half_width = (p_hat - 0.5) - 0.02;
        let (lo, hi) = wilson::wilson_ci_from_proportion(p_hat, 100.0, 0.95).unwrap();
        let estimate = estimate_additional_trials(
            MetricKind::WinRate,
            CiMethod::Wilson,
            Verdict::Inconclusive,
            p_hat - 0.5,
            lo - 0.5,
            hi - 0.5,
            100,
            &t,
            0.95,
        )
        .unwrap();
        let required_n = 100 + estimate;
        let (rlo, rhi) = wilson::wilson_ci_from_proportion(p_hat, required_n as f64, 0.95).unwrap();
        assert!(
            (rhi - rlo) / 2.0 <= target_half_width + 1e-6,
            "search-based estimate must actually clear the target width on a real re-run"
        );
    }

    // --- apply_failure_caps / apply_failure_caps_to_multi ---

    fn report_with(verdict: Verdict, timeouts: u64, crashes: u64, invalid: u64) -> Report {
        Report {
            schema_version: crate::report::REPORT_SCHEMA_VERSION,
            verdict,
            validity: Validity::Valid,
            promotion: Promotion::decide(Validity::Valid, verdict),
            metric: MetricKind::WinRate,
            baseline_count: 20,
            candidate_count: 80,
            paired_count: 100,
            effect: 0.06,
            confidence: 0.95,
            ci_low: 0.02,
            ci_high: 0.10,
            pass_above: 0.02,
            fail_below: -0.02,
            timeouts,
            crashes,
            invalid,
            failure_breakdown: crate::metrics::FailureBreakdown::default(),
            reason: "ok".to_string(),
            estimated_additional_trials: None,
            inconclusive_kind: None,
            warnings: Vec::new(),
            data_quality: crate::report::DataQuality::default(),
            quantile: None,
            cluster_count: None,
            max_cluster_size: None,
            effective_sample_size: None,
            design_effect: None,
            scale_diagnostics: None,
            tied_count: None,
            pair_score_counts: None,
            correction_method: None,
            family_size: None,
            achieved_alpha: None,
            adjusted_alpha_threshold: None,
            unadjusted_verdict: None,
            family_adjusted_verdict: None,
            family_adjusted_promotion: None,
        }
    }

    #[test]
    fn uncapped_report_is_untouched() {
        let mut report = report_with(Verdict::Pass, 5, 5, 5);
        apply_failure_caps(&mut report, &FailureCaps::default());
        assert_eq!(report.verdict, Verdict::Pass);
        assert_eq!(report.validity, Validity::Valid);
        assert_eq!(report.promotion, Promotion::Promoted);
    }

    #[test]
    fn breached_crash_cap_forces_inconclusive_and_not_promoted() {
        let mut report = report_with(Verdict::Pass, 0, 3, 0);
        let caps = FailureCaps {
            max_crashes: Some(2),
            ..Default::default()
        };
        apply_failure_caps(&mut report, &caps);
        assert_eq!(report.verdict, Verdict::Inconclusive);
        assert_eq!(report.validity, Validity::Invalid);
        assert_eq!(report.promotion, Promotion::NotPromoted);
        assert!(report.reason.contains("INVALID"));
        assert!(report.reason.contains("3 crash(es)"));
        assert_eq!(report.estimated_additional_trials, None);
    }

    #[test]
    fn breached_cap_clears_a_pre_existing_inconclusive_kind() {
        // A failure-cap-forced Inconclusive isn't a real CI judgment, even if the report already
        // carried a noise/directional label from before the cap was applied.
        let mut report = report_with(Verdict::Inconclusive, 0, 3, 0);
        report.inconclusive_kind = Some(InconclusiveKind::Directional);
        let caps = FailureCaps {
            max_crashes: Some(2),
            ..Default::default()
        };
        apply_failure_caps(&mut report, &caps);
        assert_eq!(report.inconclusive_kind, None);
    }

    #[test]
    fn cap_at_exactly_the_limit_is_still_valid() {
        // A cap of 0 means "zero tolerance", not "at least one" - the boundary
        // itself (count == max) must stay Valid, only count > max invalidates.
        let mut report = report_with(Verdict::Pass, 0, 0, 0);
        let caps = FailureCaps {
            max_crashes: Some(0),
            ..Default::default()
        };
        apply_failure_caps(&mut report, &caps);
        assert_eq!(report.validity, Validity::Valid);
        assert_eq!(report.verdict, Verdict::Pass);
    }

    #[test]
    fn multi_report_invalidated_if_any_member_breaches_a_cap() {
        let clean = report_with(Verdict::Pass, 0, 0, 0);
        let dirty = report_with(Verdict::Fail, 0, 0, 0);
        let mut multi = MultiReport {
            schema_version: crate::report::REPORT_SCHEMA_VERSION,
            verdict: Verdict::Fail,
            validity: Validity::Valid,
            promotion: Promotion::NotPromoted,
            simultaneous_claims_promotion: None,
            reports: vec![clean, dirty],
        };
        // Give the second report 5 timeouts directly (report_with's dirty
        // report starts clean; mutate after construction for clarity here).
        multi.reports[1].timeouts = 5;
        let caps = FailureCaps {
            max_timeouts: Some(1),
            ..Default::default()
        };
        apply_failure_caps_to_multi(&mut multi, &caps);
        assert_eq!(multi.validity, Validity::Invalid);
        assert_eq!(multi.reports[0].validity, Validity::Valid);
        assert_eq!(multi.reports[1].validity, Validity::Invalid);
        // The dirty report's own verdict was forced to Inconclusive, which
        // must now hold back the aggregate too, even though the clean
        // report still passes.
        assert_eq!(multi.reports[1].verdict, Verdict::Inconclusive);
        assert_eq!(multi.verdict, Verdict::Inconclusive);
        assert_eq!(multi.promotion, Promotion::NotPromoted);
    }
}