ftui-runtime 0.3.1

Elm-style runtime loop and subscriptions for FrankenTUI.
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
//! Anytime-Valid Flake Detector (bd-1plj).
//!
//! Detects flaky timing regressions in E2E tests without inflating false positives,
//! using anytime-valid e-process statistics.
//!
//! # Mathematical Model
//!
//! For sub-Gaussian residuals `r_t` (mean 0 under null), the e-value at time `t` is:
//!
//! ```text
//! e_t = exp(λ × r_t − (λ² × σ²) / 2)
//! E_t = ∏_{i=1}^t e_i
//! ```
//!
//! We reject H₀ (system is stable) when `E_t > 1/α`, providing anytime-valid
//! Type I error control.
//!
//! # Key Properties
//!
//! - **Anytime-valid**: Can stop testing early without invalid inference
//! - **No false positives in stable runs**: E[E_t] ≤ 1 under H₀
//! - **Early detection**: Strong evidence triggers early failure
//! - **Variable-length support**: Works with different test run lengths
//!
//! # Failure Modes
//!
//! | Condition | Behavior | Rationale |
//! |-----------|----------|-----------|
//! | σ = 0 | Clamp to σ_MIN | Division by zero guard |
//! | E_t underflow | Clamp to E_MIN | Prevents permanent zero-lock |
//! | E_t overflow | Clamp to E_MAX | Numerical stability |
//! | No observations | E_t = 1 | Identity element |
//!
//! # Example
//!
//! ```rust,ignore
//! use ftui_runtime::flake_detector::{FlakeDetector, FlakeConfig};
//!
//! let mut detector = FlakeDetector::new(FlakeConfig::default());
//!
//! // Observe latency deviations
//! let decision = detector.observe(latency_deviation);
//! if decision.is_flaky {
//!     eprintln!("Flaky test detected");
//! }
//! ```

#![forbid(unsafe_code)]

use std::collections::VecDeque;

/// Minimum sigma to prevent division by zero.
const SIGMA_MIN: f64 = 1e-9;

/// Minimum e-value floor to prevent permanent zero-lock.
const E_MIN: f64 = 1e-100;

/// Maximum e-value to prevent overflow.
const E_MAX: f64 = 1e100;

/// Default significance level.
const DEFAULT_ALPHA: f64 = 0.05;

/// Default lambda (betting intensity).
const DEFAULT_LAMBDA: f64 = 0.5;

// =============================================================================
// Configuration
// =============================================================================

/// Configuration for the flake detector.
#[derive(Debug, Clone)]
pub struct FlakeConfig {
    /// Significance level `α`. Fail when `E_t > 1/α`.
    /// Lower α → more conservative (fewer false alarms). Default: 0.05.
    pub alpha: f64,

    /// Betting intensity `λ`. Higher values detect deviations faster
    /// but are more sensitive to noise. Default: 0.5.
    pub lambda: f64,

    /// Prior estimate of standard deviation for latency residuals.
    /// Used in the e-value formula. Default: 1.0 (normalized units).
    pub sigma: f64,

    /// Rolling window size for empirical variance estimation.
    /// Set to 0 to use fixed sigma. Default: 50.
    pub variance_window: usize,

    /// Minimum observations before making decisions.
    /// Helps with warm-up. Default: 3.
    pub min_observations: usize,

    /// Enable JSONL-compatible evidence logging. Default: false.
    pub enable_logging: bool,

    /// Minimum e-value before flagging as flaky (1/alpha).
    /// Computed from alpha but can be overridden.
    pub threshold: Option<f64>,
}

impl Default for FlakeConfig {
    fn default() -> Self {
        Self {
            alpha: DEFAULT_ALPHA,
            lambda: DEFAULT_LAMBDA,
            sigma: 1.0,
            variance_window: 50,
            min_observations: 3,
            enable_logging: false,
            threshold: None,
        }
    }
}

impl FlakeConfig {
    /// Create a new configuration with the given alpha.
    #[must_use]
    pub fn new(alpha: f64) -> Self {
        Self {
            alpha: alpha.clamp(1e-10, 0.5),
            ..Default::default()
        }
    }

    /// Set the betting intensity lambda.
    #[must_use]
    pub fn with_lambda(mut self, lambda: f64) -> Self {
        self.lambda = lambda.clamp(0.01, 2.0);
        self
    }

    /// Set the prior sigma.
    #[must_use]
    pub fn with_sigma(mut self, sigma: f64) -> Self {
        self.sigma = sigma.max(SIGMA_MIN);
        self
    }

    /// Set the variance window size.
    #[must_use]
    pub fn with_variance_window(mut self, window: usize) -> Self {
        self.variance_window = window;
        self
    }

    /// Set minimum observations.
    #[must_use]
    pub fn with_min_observations(mut self, min: usize) -> Self {
        self.min_observations = min.max(1);
        self
    }

    /// Enable logging.
    #[must_use]
    pub fn with_logging(mut self, enabled: bool) -> Self {
        self.enable_logging = enabled;
        self
    }

    /// Get the threshold (1/alpha).
    #[must_use]
    pub fn threshold(&self) -> f64 {
        self.threshold.unwrap_or(1.0 / self.alpha)
    }
}

// =============================================================================
// Decision Types
// =============================================================================

/// Decision returned by the flake detector.
#[derive(Debug, Clone, PartialEq)]
pub struct FlakeDecision {
    /// Whether the test is flagged as flaky.
    pub is_flaky: bool,
    /// Current cumulative e-value.
    pub e_value: f64,
    /// Threshold for flakiness (1/alpha).
    pub threshold: f64,
    /// Number of observations so far.
    pub observation_count: usize,
    /// Current variance estimate.
    pub variance_estimate: f64,
    /// Whether we have enough observations.
    pub warmed_up: bool,
}

impl FlakeDecision {
    /// Check if we should fail the test.
    #[must_use]
    pub fn should_fail(&self) -> bool {
        self.is_flaky && self.warmed_up
    }
}

/// Log entry for evidence tracking.
#[derive(Debug, Clone)]
pub struct EvidenceLog {
    /// Observation index.
    pub observation_idx: usize,
    /// The residual value observed.
    pub residual: f64,
    /// The incremental e-value for this observation.
    pub e_increment: f64,
    /// Cumulative e-value after this observation.
    pub e_cumulative: f64,
    /// Variance estimate at this point.
    pub variance: f64,
    /// Decision at this point.
    pub decision: bool,
}

impl EvidenceLog {
    /// Serialize to JSONL format.
    #[must_use]
    pub fn to_jsonl(&self) -> String {
        format!(
            r#"{{"idx":{},"residual":{:.6},"e_inc":{:.6},"e_cum":{:.6},"var":{:.6},"decision":{}}}"#,
            self.observation_idx,
            self.residual,
            self.e_increment,
            self.e_cumulative,
            self.variance,
            self.decision
        )
    }
}

// =============================================================================
// Flake Detector
// =============================================================================

/// Anytime-valid flake detector using e-process statistics.
#[derive(Debug, Clone)]
pub struct FlakeDetector {
    /// Configuration.
    config: FlakeConfig,
    /// Cumulative e-value (product of incremental e-values).
    e_cumulative: f64,
    /// Observation count.
    observation_count: usize,
    /// Rolling window for variance estimation.
    variance_window: VecDeque<f64>,
    /// Evidence log (if logging enabled).
    evidence_log: Vec<EvidenceLog>,
    /// Observation index where a flaky decision was first reached.
    first_flaky_at: Option<usize>,
    /// Maximum cumulative e-value observed over the detector lifetime.
    max_e_value: f64,
}

impl FlakeDetector {
    /// Create a new flake detector.
    #[must_use]
    pub fn new(config: FlakeConfig) -> Self {
        let capacity = if config.variance_window > 0 {
            config.variance_window
        } else {
            1
        };
        Self {
            config,
            e_cumulative: 1.0, // Identity element
            observation_count: 0,
            variance_window: VecDeque::with_capacity(capacity),
            evidence_log: Vec::new(),
            first_flaky_at: None,
            max_e_value: 1.0,
        }
    }

    /// Observe a latency deviation (residual).
    ///
    /// The residual should be the difference between observed latency
    /// and expected latency, ideally normalized.
    pub fn observe(&mut self, residual: f64) -> FlakeDecision {
        if residual.is_nan() {
            return FlakeDecision {
                is_flaky: false,
                e_value: self.e_cumulative,
                threshold: self.config.threshold(),
                observation_count: self.observation_count,
                variance_estimate: self.current_sigma().powi(2),
                warmed_up: self.observation_count >= self.config.min_observations,
            };
        }

        self.observation_count += 1;

        // Update variance estimate
        self.update_variance(residual);
        let sigma = self.current_sigma();

        // Compute incremental e-value: e_t = exp(λ × r_t − (λ² × σ²) / 2)
        let lambda = self.config.lambda;
        let exponent = lambda * residual - (lambda * lambda * sigma * sigma) / 2.0;
        let e_increment = exponent.exp().clamp(E_MIN, E_MAX);

        // Update cumulative e-value
        self.e_cumulative = (self.e_cumulative * e_increment).clamp(E_MIN, E_MAX);

        // Check threshold
        let threshold = self.config.threshold();
        let is_flaky = self.e_cumulative > threshold;
        let warmed_up = self.observation_count >= self.config.min_observations;
        let decision = is_flaky && warmed_up;

        if decision && self.first_flaky_at.is_none() {
            self.first_flaky_at = Some(self.observation_count);
        }
        self.max_e_value = self.max_e_value.max(self.e_cumulative);

        // Log if enabled
        if self.config.enable_logging {
            self.evidence_log.push(EvidenceLog {
                observation_idx: self.observation_count,
                residual,
                e_increment,
                e_cumulative: self.e_cumulative,
                variance: sigma * sigma,
                decision,
            });
        }

        FlakeDecision {
            is_flaky,
            e_value: self.e_cumulative,
            threshold,
            observation_count: self.observation_count,
            variance_estimate: sigma * sigma,
            warmed_up,
        }
    }

    /// Observe multiple residuals and return the final decision.
    pub fn observe_batch(&mut self, residuals: &[f64]) -> FlakeDecision {
        let mut decision = FlakeDecision {
            is_flaky: false,
            e_value: self.e_cumulative,
            threshold: self.config.threshold(),
            observation_count: self.observation_count,
            variance_estimate: self.current_sigma().powi(2),
            warmed_up: false,
        };

        for &r in residuals {
            decision = self.observe(r);
            if decision.should_fail() {
                break; // Early stopping with anytime-valid guarantee
            }
        }

        decision
    }

    /// Reset the detector state.
    pub fn reset(&mut self) {
        self.e_cumulative = 1.0;
        self.observation_count = 0;
        self.variance_window.clear();
        self.evidence_log.clear();
        self.first_flaky_at = None;
        self.max_e_value = 1.0;
    }

    /// Get the current e-value.
    #[must_use]
    pub fn e_value(&self) -> f64 {
        self.e_cumulative
    }

    /// Get the observation count.
    #[must_use]
    pub fn observation_count(&self) -> usize {
        self.observation_count
    }

    /// Check if the detector has warmed up.
    #[must_use]
    pub fn is_warmed_up(&self) -> bool {
        self.observation_count >= self.config.min_observations
    }

    /// Get the evidence log.
    #[must_use]
    pub fn evidence_log(&self) -> &[EvidenceLog] {
        &self.evidence_log
    }

    /// Export evidence log as JSONL.
    #[must_use]
    pub fn evidence_to_jsonl(&self) -> String {
        self.evidence_log
            .iter()
            .map(|e| e.to_jsonl())
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Get the current sigma estimate.
    #[must_use]
    pub fn current_sigma(&self) -> f64 {
        if self.config.variance_window == 0 || self.variance_window.len() < 2 {
            return self.config.sigma.max(SIGMA_MIN);
        }

        let n = self.variance_window.len() as f64;
        let mean = self.variance_window.iter().sum::<f64>() / n;
        let variance = self
            .variance_window
            .iter()
            .map(|&x| {
                let diff = x - mean;
                diff * diff
            })
            .sum::<f64>()
            / (n - 1.0);

        variance.sqrt().max(SIGMA_MIN)
    }

    /// Update variance estimate with new observation.
    fn update_variance(&mut self, residual: f64) {
        if self.config.variance_window == 0 {
            return;
        }

        // Maintain rolling window
        if self.variance_window.len() >= self.config.variance_window {
            self.variance_window.pop_front();
        }
        self.variance_window.push_back(residual);
    }

    /// Get configuration.
    #[must_use]
    pub fn config(&self) -> &FlakeConfig {
        &self.config
    }
}

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

// =============================================================================
// Summary Statistics
// =============================================================================

/// Summary of flake detection run.
#[derive(Debug, Clone)]
pub struct FlakeSummary {
    /// Total observations.
    pub total_observations: usize,
    /// Final e-value.
    pub final_e_value: f64,
    /// Whether flagged as flaky.
    pub is_flaky: bool,
    /// Observation index where flakiness was first detected (if any).
    pub first_flaky_at: Option<usize>,
    /// Maximum e-value observed.
    pub max_e_value: f64,
    /// Threshold used.
    pub threshold: f64,
}

impl FlakeDetector {
    /// Generate summary statistics.
    #[must_use]
    pub fn summary(&self) -> FlakeSummary {
        FlakeSummary {
            total_observations: self.observation_count,
            final_e_value: self.e_cumulative,
            is_flaky: self.e_cumulative > self.config.threshold(),
            first_flaky_at: self.first_flaky_at,
            max_e_value: self.max_e_value,
            threshold: self.config.threshold(),
        }
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn unit_eprocess_threshold() {
        // Test that we fail when E_t > 1/alpha
        let config = FlakeConfig::new(0.05).with_min_observations(1);
        let mut detector = FlakeDetector::new(config);

        // Feed large positive residuals to drive e-value up
        for _ in 0..20 {
            let decision = detector.observe(3.0); // Large deviation
            if decision.should_fail() {
                // Should eventually trigger
                assert!(decision.e_value > decision.threshold);
                return;
            }
        }

        // If we didn't fail, check threshold
        let decision = detector.observe(0.0);
        assert!(
            decision.e_value > decision.threshold || !decision.is_flaky,
            "Should either have triggered or not be flaky"
        );
    }

    #[test]
    fn unit_eprocess_nonnegative() {
        // E-values should never be negative
        let mut detector = FlakeDetector::default();

        // Test with various residuals including negative
        let residuals = [-5.0, -2.0, 0.0, 2.0, 5.0, -10.0, 10.0];
        for r in residuals {
            let decision = detector.observe(r);
            assert!(
                decision.e_value > 0.0,
                "E-value must be positive, got {}",
                decision.e_value
            );
        }
    }

    #[test]
    fn unit_optional_stopping() {
        // Stopping early should preserve decision validity
        let config = FlakeConfig::new(0.05)
            .with_lambda(0.3)
            .with_min_observations(1)
            .with_logging(true);
        let mut detector = FlakeDetector::new(config);

        // Simulate stable run (small residuals around 0)
        let stable_residuals: Vec<f64> = (0..100).map(|i| (i as f64 * 0.1).sin() * 0.1).collect();

        let decision = detector.observe_batch(&stable_residuals);

        // Under H₀ (stable), we shouldn't flag as flaky
        // Note: Due to random variation, we check the e-value is reasonable
        assert!(
            decision.e_value <= decision.threshold * 2.0 || !decision.should_fail(),
            "Stable run should rarely trigger flakiness"
        );
    }

    #[test]
    fn unit_stable_run_no_false_positives() {
        // A truly stable run should not trigger false positives
        let config = FlakeConfig::new(0.05)
            .with_sigma(1.0)
            .with_lambda(0.5)
            .with_min_observations(3);
        let mut detector = FlakeDetector::new(config);

        // Zero residuals (perfectly stable)
        for _ in 0..50 {
            let decision = detector.observe(0.0);
            // With zero residuals, e_increment = exp(-λ²σ²/2) < 1
            // So e_cumulative should decrease over time
            assert!(
                !decision.should_fail(),
                "Zero residuals should never trigger flakiness"
            );
        }
    }

    #[test]
    fn unit_spike_detection() {
        // Inject latency spikes and verify detection
        let config = FlakeConfig::new(0.05)
            .with_sigma(1.0)
            .with_lambda(0.5)
            .with_min_observations(3)
            .with_logging(true);
        let mut detector = FlakeDetector::new(config);

        // Start with some normal observations
        for _ in 0..5 {
            detector.observe(0.1);
        }

        // Inject spike
        let mut detected = false;
        for _ in 0..20 {
            let decision = detector.observe(5.0); // Large spike
            if decision.should_fail() {
                detected = true;
                break;
            }
        }

        assert!(detected, "Should detect sustained spike");
    }

    #[test]
    fn unit_reset() {
        let mut detector = FlakeDetector::default();
        detector.observe(1.0);
        detector.observe(2.0);

        assert_eq!(detector.observation_count(), 2);

        detector.reset();

        assert_eq!(detector.observation_count(), 0);
        assert!((detector.e_value() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn unit_variance_estimation() {
        let config = FlakeConfig::default().with_variance_window(10);
        let mut detector = FlakeDetector::new(config);

        // Feed constant residuals
        for _ in 0..20 {
            detector.observe(1.0);
        }

        // With constant input, variance should be low
        let sigma = detector.current_sigma();
        assert!(
            sigma < 0.1 || (sigma - 1.0).abs() < 0.5,
            "Variance should converge"
        );
    }

    #[test]
    fn unit_evidence_log() {
        let config = FlakeConfig::default()
            .with_logging(true)
            .with_min_observations(1);
        let mut detector = FlakeDetector::new(config);

        detector.observe(0.5);
        detector.observe(1.0);
        detector.observe(-0.5);

        assert_eq!(detector.evidence_log().len(), 3);

        let jsonl = detector.evidence_to_jsonl();
        assert!(jsonl.contains("\"idx\":1"));
        assert!(jsonl.contains("\"idx\":2"));
        assert!(jsonl.contains("\"idx\":3"));
    }

    #[test]
    fn unit_summary() {
        let config = FlakeConfig::default()
            .with_logging(true)
            .with_min_observations(1);
        let mut detector = FlakeDetector::new(config);

        for _ in 0..10 {
            detector.observe(0.1);
        }

        let summary = detector.summary();
        assert_eq!(summary.total_observations, 10);
        assert!(summary.final_e_value > 0.0);
        assert!(summary.threshold > 0.0);
    }

    #[test]
    fn unit_batch_observe() {
        let config = FlakeConfig::default().with_min_observations(1);
        let mut detector = FlakeDetector::new(config);

        let residuals = vec![0.1, 0.2, 0.3, 0.4, 0.5];
        let decision = detector.observe_batch(&residuals);

        assert_eq!(decision.observation_count, 5);
    }

    #[test]
    fn unit_config_builder() {
        let config = FlakeConfig::new(0.01)
            .with_lambda(0.3)
            .with_sigma(2.0)
            .with_variance_window(100)
            .with_min_observations(5)
            .with_logging(true);

        assert!((config.alpha - 0.01).abs() < 1e-10);
        assert!((config.lambda - 0.3).abs() < 1e-10);
        assert!((config.sigma - 2.0).abs() < 1e-10);
        assert_eq!(config.variance_window, 100);
        assert_eq!(config.min_observations, 5);
        assert!(config.enable_logging);
        assert!((config.threshold() - 100.0).abs() < 1e-10);
    }

    #[test]
    fn unit_numerical_stability() {
        let mut detector = FlakeDetector::default();

        // Very large residuals
        for _ in 0..10 {
            let decision = detector.observe(1000.0);
            assert!(decision.e_value.is_finite());
            assert!(decision.e_value > 0.0);
        }

        detector.reset();

        // Very small negative residuals
        for _ in 0..10 {
            let decision = detector.observe(-1000.0);
            assert!(decision.e_value.is_finite());
            assert!(decision.e_value > 0.0);
        }
    }

    // ── FlakeConfig defaults ─────────────────────────────────────

    #[test]
    fn config_default_values() {
        let config = FlakeConfig::default();
        assert!((config.alpha - DEFAULT_ALPHA).abs() < f64::EPSILON);
        assert!((config.lambda - DEFAULT_LAMBDA).abs() < f64::EPSILON);
        assert!((config.sigma - 1.0).abs() < f64::EPSILON);
        assert_eq!(config.variance_window, 50);
        assert_eq!(config.min_observations, 3);
        assert!(!config.enable_logging);
        assert!(config.threshold.is_none());
    }

    #[test]
    fn config_threshold_computed_from_alpha() {
        let config = FlakeConfig::new(0.05);
        assert!((config.threshold() - 20.0).abs() < 1e-10);
    }

    #[test]
    fn config_threshold_override() {
        let mut config = FlakeConfig::new(0.05);
        config.threshold = Some(42.0);
        assert!((config.threshold() - 42.0).abs() < f64::EPSILON);
    }

    // ── FlakeConfig clamping ─────────────────────────────────────

    #[test]
    fn config_new_clamps_alpha_low() {
        let config = FlakeConfig::new(0.0);
        assert!(config.alpha >= 1e-10);
    }

    #[test]
    fn config_new_clamps_alpha_high() {
        let config = FlakeConfig::new(1.0);
        assert!(config.alpha <= 0.5);
    }

    #[test]
    fn config_with_lambda_clamps_low() {
        let config = FlakeConfig::default().with_lambda(0.0);
        assert!(config.lambda >= 0.01);
    }

    #[test]
    fn config_with_lambda_clamps_high() {
        let config = FlakeConfig::default().with_lambda(100.0);
        assert!(config.lambda <= 2.0);
    }

    #[test]
    fn config_with_sigma_clamps_to_min() {
        let config = FlakeConfig::default().with_sigma(0.0);
        assert!(config.sigma >= SIGMA_MIN);
    }

    #[test]
    fn config_with_min_observations_clamps_to_one() {
        let config = FlakeConfig::default().with_min_observations(0);
        assert!(config.min_observations >= 1);
    }

    // ── FlakeDecision ────────────────────────────────────────────

    #[test]
    fn decision_should_fail_requires_both_flaky_and_warmed_up() {
        let d1 = FlakeDecision {
            is_flaky: true,
            warmed_up: false,
            e_value: 100.0,
            threshold: 20.0,
            observation_count: 1,
            variance_estimate: 1.0,
        };
        assert!(!d1.should_fail());

        let d2 = FlakeDecision {
            is_flaky: false,
            warmed_up: true,
            e_value: 1.0,
            threshold: 20.0,
            observation_count: 5,
            variance_estimate: 1.0,
        };
        assert!(!d2.should_fail());

        let d3 = FlakeDecision {
            is_flaky: true,
            warmed_up: true,
            e_value: 100.0,
            threshold: 20.0,
            observation_count: 5,
            variance_estimate: 1.0,
        };
        assert!(d3.should_fail());
    }

    // ── EvidenceLog JSONL ────────────────────────────────────────

    #[test]
    fn evidence_log_to_jsonl_format() {
        let log = EvidenceLog {
            observation_idx: 3,
            residual: 1.5,
            e_increment: 2.1,
            e_cumulative: 4.2,
            variance: 0.9,
            decision: true,
        };
        let jsonl = log.to_jsonl();
        assert!(jsonl.contains("\"idx\":3"));
        assert!(jsonl.contains("\"residual\":"));
        assert!(jsonl.contains("\"e_inc\":"));
        assert!(jsonl.contains("\"e_cum\":"));
        assert!(jsonl.contains("\"var\":"));
        assert!(jsonl.contains("\"decision\":true"));
    }

    #[test]
    fn evidence_log_to_jsonl_false_decision() {
        let log = EvidenceLog {
            observation_idx: 1,
            residual: 0.0,
            e_increment: 1.0,
            e_cumulative: 1.0,
            variance: 1.0,
            decision: false,
        };
        let jsonl = log.to_jsonl();
        assert!(jsonl.contains("\"decision\":false"));
    }

    // ── FlakeDetector accessors ──────────────────────────────────

    #[test]
    fn detector_default_initial_state() {
        let detector = FlakeDetector::default();
        assert_eq!(detector.observation_count(), 0);
        assert!((detector.e_value() - 1.0).abs() < f64::EPSILON);
        assert!(!detector.is_warmed_up());
        assert!(detector.evidence_log().is_empty());
    }

    #[test]
    fn detector_config_accessor() {
        let config = FlakeConfig::new(0.01).with_lambda(0.3);
        let detector = FlakeDetector::new(config);
        assert!((detector.config().alpha - 0.01).abs() < 1e-10);
        assert!((detector.config().lambda - 0.3).abs() < 1e-10);
    }

    #[test]
    fn detector_is_warmed_up_after_min_observations() {
        let config = FlakeConfig::default().with_min_observations(3);
        let mut detector = FlakeDetector::new(config);
        assert!(!detector.is_warmed_up());
        detector.observe(0.0);
        detector.observe(0.0);
        assert!(!detector.is_warmed_up());
        detector.observe(0.0);
        assert!(detector.is_warmed_up());
    }

    // ── Variance window = 0 (fixed sigma) ────────────────────────

    #[test]
    fn fixed_sigma_when_variance_window_zero() {
        let config = FlakeConfig::default()
            .with_sigma(3.0)
            .with_variance_window(0);
        let mut detector = FlakeDetector::new(config);
        detector.observe(10.0);
        detector.observe(20.0);
        assert!((detector.current_sigma() - 3.0).abs() < f64::EPSILON);
    }

    // ── Summary edge cases ───────────────────────────────────────

    #[test]
    fn summary_empty_detector() {
        let detector = FlakeDetector::new(FlakeConfig::default().with_logging(true));
        let summary = detector.summary();
        assert_eq!(summary.total_observations, 0);
        assert!((summary.final_e_value - 1.0).abs() < f64::EPSILON);
        assert!(!summary.is_flaky);
        assert!(summary.first_flaky_at.is_none());
        assert!((summary.max_e_value - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn summary_first_flaky_at_recorded() {
        let config = FlakeConfig::new(0.05)
            .with_min_observations(1)
            .with_logging(true);
        let mut detector = FlakeDetector::new(config);
        for _ in 0..50 {
            detector.observe(5.0);
        }
        let summary = detector.summary();
        if summary.is_flaky {
            assert!(
                summary.first_flaky_at.is_some(),
                "should record first flaky index"
            );
            assert!(summary.first_flaky_at.unwrap() > 0);
        }
    }

    // ── Determinism ──────────────────────────────────────────────

    #[test]
    fn deterministic_same_inputs() {
        let config = FlakeConfig::new(0.05).with_lambda(0.5).with_sigma(1.0);
        let residuals = [0.1, -0.2, 0.5, -0.1, 3.0, 0.0, -1.0, 2.0];
        let mut d1 = FlakeDetector::new(config.clone());
        let mut d2 = FlakeDetector::new(config);
        for &r in &residuals {
            d1.observe(r);
            d2.observe(r);
        }
        assert!((d1.e_value() - d2.e_value()).abs() < 1e-10);
        assert_eq!(d1.observation_count(), d2.observation_count());
    }

    // ── Batch early stopping ─────────────────────────────────────

    #[test]
    fn batch_early_stops_on_flaky() {
        let config = FlakeConfig::new(0.05)
            .with_min_observations(1)
            .with_lambda(0.5);
        let mut detector = FlakeDetector::new(config);
        let mut residuals = vec![10.0; 20];
        residuals.extend(vec![0.0; 80]);
        let decision = detector.observe_batch(&residuals);
        if decision.should_fail() {
            assert!(
                decision.observation_count < 100,
                "should stop early, count={}",
                decision.observation_count
            );
        }
    }

    // ── E-value monotone under positive residuals ────────────────

    #[test]
    fn e_value_increases_under_consistent_positive_residuals() {
        let config = FlakeConfig::default()
            .with_variance_window(0)
            .with_sigma(1.0);
        let mut detector = FlakeDetector::new(config);
        let mut prev_e = 1.0;
        for _ in 0..5 {
            let decision = detector.observe(2.0);
            assert!(
                decision.e_value >= prev_e,
                "e-value should increase: prev={prev_e}, cur={}",
                decision.e_value
            );
            prev_e = decision.e_value;
        }
    }

    // ── Evidence log only when enabled ───────────────────────────

    #[test]
    fn no_evidence_log_when_disabled() {
        let config = FlakeConfig::default();
        let mut detector = FlakeDetector::new(config);
        detector.observe(1.0);
        detector.observe(2.0);
        assert!(detector.evidence_log().is_empty());
        assert!(detector.evidence_to_jsonl().is_empty());
    }

    #[test]
    fn summary_tracks_metrics_when_logging_disabled() {
        let config = FlakeConfig::new(0.05).with_min_observations(1);
        let mut detector = FlakeDetector::new(config);
        detector.observe(5.0);
        detector.observe(5.0);
        detector.observe(-1.0);

        let summary = detector.summary();
        assert_eq!(summary.first_flaky_at, Some(2));
        assert!(summary.max_e_value > summary.threshold);
        assert!(summary.max_e_value + f64::EPSILON >= summary.final_e_value);
    }

    // ── Reset clears everything ──────────────────────────────────

    #[test]
    fn reset_clears_evidence_log() {
        let config = FlakeConfig::default().with_logging(true);
        let mut detector = FlakeDetector::new(config);
        detector.observe(1.0);
        assert_eq!(detector.evidence_log().len(), 1);
        detector.reset();
        assert!(detector.evidence_log().is_empty());
    }
}