wafrift-strategy 0.3.1

Evasion strategy pipeline — orchestrates all WAF Rift modules into a coherent evasion flow.
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
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
//! Drift-aware evasion window detection (#115).
//!
//! WAF behaviour is not stationary. Rules reload (CF Auto-Tune retrains every
//! ~hour), edges throttle, IP reputation flips. The same payload may be blocked
//! at minute 0 and pass at minute 47.
//!
//! This module implements a CUSUM-based sequential change-point detector that
//! tracks four per-target signals:
//!
//! 1. **Median response time** — slower = heavier inspection.
//! 2. **P95 response time** — spike = new DPI layer spinning up.
//! 3. **Block rate** (over last 50 probes) — direct measure of WAF policy.
//! 4. **Body-hash entropy** — change in response diversity signals new rules.
//!
//! Each signal runs an independent CUSUM detector. A [`RegimeChange`] fires
//! when **≥ 2 signals agree** on the direction of change.
//!
//! The [`HostState`] integration calls [`DriftDetector::observe`] on every
//! probe result and, when [`RegimeChange::LooserNow`] fires, re-queues
//! previously-blocked payloads for retry.

use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

// ── Constants ───────────────────────────────────────────────────────────────

/// Number of probes to keep in the sliding window for baseline statistics.
const DEFAULT_WINDOW_SIZE: usize = 50;

/// CUSUM threshold: how many standard deviations of accumulated drift before
/// we fire a change-point. 4.0 σ balances false-positive rate vs. detection
/// latency at the 50-sample window.
const DEFAULT_THRESHOLD: f64 = 4.0;

/// Number of bodies to track for hash-entropy estimation.
const BODY_HASH_WINDOW: usize = 32;

/// Agreement threshold: how many independent CUSUM signals must agree before
/// a `RegimeChange` fires. Prevents single-signal noise from triggering retries.
const SIGNAL_AGREEMENT: usize = 2;

// ── Public types ─────────────────────────────────────────────────────────────

/// Direction and magnitude of a detected WAF regime change.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RegimeChange {
    /// WAF is blocking less aggressively — retry the blocked corpus now.
    LooserNow,
    /// WAF is blocking more aggressively — back off, slow down probing.
    StricterNow,
    /// Regime changed but signals disagree on direction (e.g. latency went
    /// up while block rate went down). Retry cautiously; do not assume free
    /// passage.
    Unclear,
}

/// A single probe observation fed into [`DriftDetector::observe`].
#[derive(Debug, Clone)]
pub struct ProbeObservation {
    /// Round-trip time of the probe in milliseconds.
    pub response_time_ms: f64,
    /// Whether this probe was blocked by the WAF.
    pub was_blocked: bool,
    /// A cheap hash of the response body (e.g. `hash(body[..512])`).
    /// `None` if the response had no body or it was not read.
    pub body_hash: Option<u64>,
}

/// CUSUM-based sequential change-point detector for a single scalar signal.
///
/// Tracks cumulative sum of deviations above/below a rolling baseline.
/// When either `s_high` or `s_low` exceeds `threshold * baseline_std` a
/// change is detected and the accumulators reset.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct CusumDetector {
    /// Rolling baseline window for mean/stdev estimation.
    window: VecDeque<f64>,
    window_size: usize,
    /// CUSUM accumulator for upward shifts (signal rising above mean).
    s_high: f64,
    /// CUSUM accumulator for downward shifts (signal falling below mean).
    s_low: f64,
    /// Detection threshold as a multiple of baseline stdev.
    threshold: f64,
    /// Direction of the most-recently fired change (+1 = higher, -1 = lower).
    last_direction: i8,
}

impl CusumDetector {
    fn new(window_size: usize, threshold: f64) -> Self {
        Self {
            window: VecDeque::with_capacity(window_size),
            window_size,
            s_high: 0.0,
            s_low: 0.0,
            threshold,
            last_direction: 0,
        }
    }

    /// Push a new observation.  Returns `Some(direction)` (+1 or -1) when a
    /// change-point fires, `None` when still within the stationary regime.
    fn push(&mut self, value: f64) -> Option<i8> {
        // Need at least 4 points to estimate a meaningful baseline.
        if self.window.len() < 4 {
            if self.window.len() >= self.window_size {
                self.window.pop_front();
            }
            self.window.push_back(value);
            return None;
        }

        let (mean, std) = self.mean_std();
        // The CUSUM detection threshold k = threshold × σ. When the baseline
        // is perfectly stationary (σ ≈ 0), k → 0 and ANY deviation fires
        // immediately — a false-positive on perfectly identical synthetic data.
        //
        // Enforce a minimum σ floor to keep the detector from being hair-
        // triggered by floating-point noise, while still allowing large step
        // changes (block rate 0→1, latency 20ms→200ms) to register quickly:
        //
        //   - For signals near zero (mean < 0.1): floor = 0.01 (1% of the
        //     maximum useful magnitude for a rate-like signal in [0,1]).
        //   - For signals with positive mean: floor = 1% of mean.
        //
        // This means a single-step deviation must be at least
        //   `threshold × floor` above the mean to fire. For threshold=3.0
        //   and block rate: k/2 = 3.0 × 0.01 / 2 = 0.015, so a full-scale
        //   step from 0→1 (deviation=1.0) nets 0.985 per observation →
        //   fires after 4 observations, which is the desired behavior.
        // Minimum σ floor: prevents hair-trigger on perfectly stationary
        // baselines where σ=0 would make k=0 and any deviation fires.
        // For near-zero-mean signals (block rate, entropy in [0,1]):
        //   floor = 0.01 — requires a 1% meaningful shift per threshold unit.
        // For positive-mean signals (latency in ms):
        //   floor = 5% of mean — requires a 5% shift to count as signal.
        // This keeps threshold=10 from firing on a 10% nudge (5ms on 50ms
        // baseline) while allowing threshold=3 to fire on a 10× step change.
        let floor = if mean.abs() < 1.0 {
            0.01
        } else {
            mean.abs() * 0.05
        };
        let effective_std = std.max(floor);
        let k = self.threshold * effective_std;

        // CUSUM update: accumulate signed deviation from mean.
        self.s_high = (self.s_high + (value - mean - k / 2.0)).max(0.0);
        self.s_low = (self.s_low + (mean - value - k / 2.0)).max(0.0);

        // Slide the window.
        if self.window.len() >= self.window_size {
            self.window.pop_front();
        }
        self.window.push_back(value);

        // Fire if either accumulator exceeds the detection threshold.
        if self.s_high > k {
            self.s_high = 0.0;
            self.s_low = 0.0;
            self.last_direction = 1;
            return Some(1);
        }
        if self.s_low > k {
            self.s_high = 0.0;
            self.s_low = 0.0;
            self.last_direction = -1;
            return Some(-1);
        }

        None
    }

    fn mean_std(&self) -> (f64, f64) {
        let n = self.window.len() as f64;
        if n == 0.0 {
            return (0.0, 0.0);
        }
        let mean: f64 = self.window.iter().sum::<f64>() / n;
        let variance: f64 = self.window.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / n;
        (mean, variance.sqrt())
    }
}

/// Per-target drift detector.  Tracks four independent CUSUM streams and
/// fires a [`RegimeChange`] when ≥ 2 agree.
///
/// # Example
///
/// ```rust
/// use wafrift_strategy::drift_window::{DriftDetector, ProbeObservation};
///
/// let mut det = DriftDetector::default();
/// for _ in 0..60 {
///     det.observe(ProbeObservation {
///         response_time_ms: 50.0,
///         was_blocked: true,
///         body_hash: Some(0xdeadbeef),
///     });
/// }
/// // After a sudden drop in block rate the detector should eventually fire
/// // LooserNow.
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftDetector {
    /// Window size passed to each CUSUM channel.
    pub window_size: usize,
    /// Detection threshold (σ-multiples) passed to each CUSUM channel.
    pub threshold: f64,

    // ── Four independent CUSUM channels ──────────────────────────────
    cusum_median_rt: CusumDetector,
    cusum_p95_rt: CusumDetector,
    cusum_block_rate: CusumDetector,
    cusum_body_entropy: CusumDetector,

    // ── Sliding windows for computing the four signals ────────────────
    /// Raw response times for the current window (for median + p95).
    rt_window: VecDeque<f64>,
    /// Boolean blocked flags for the last `window_size` probes (for block rate).
    block_window: VecDeque<bool>,
    /// Recent body hashes for Shannon entropy estimation.
    body_hash_window: VecDeque<u64>,

    /// Total probes observed (monotonically increasing).
    pub probe_count: u64,
}

impl Default for DriftDetector {
    fn default() -> Self {
        Self::new(DEFAULT_WINDOW_SIZE, DEFAULT_THRESHOLD)
    }
}

impl DriftDetector {
    /// Create a detector with explicit window and threshold parameters.
    pub fn new(window_size: usize, threshold: f64) -> Self {
        let ws = window_size.max(8); // minimum 8 for meaningful statistics
        Self {
            window_size: ws,
            threshold,
            cusum_median_rt: CusumDetector::new(ws, threshold),
            cusum_p95_rt: CusumDetector::new(ws, threshold),
            cusum_block_rate: CusumDetector::new(ws, threshold),
            cusum_body_entropy: CusumDetector::new(ws, threshold),
            rt_window: VecDeque::with_capacity(ws),
            block_window: VecDeque::with_capacity(ws),
            body_hash_window: VecDeque::with_capacity(BODY_HASH_WINDOW),
            probe_count: 0,
        }
    }

    /// Feed a probe observation and return a [`RegimeChange`] if detected.
    ///
    /// Returns `None` when the regime is stationary (or insufficient data).
    /// Returns `Some(RegimeChange)` when ≥ 2 CUSUM channels agree.
    pub fn observe(&mut self, obs: ProbeObservation) -> Option<RegimeChange> {
        self.probe_count = self.probe_count.saturating_add(1);

        // ── 1. Update sliding windows ─────────────────────────────────
        if self.rt_window.len() >= self.window_size {
            self.rt_window.pop_front();
        }
        self.rt_window.push_back(obs.response_time_ms);

        if self.block_window.len() >= self.window_size {
            self.block_window.pop_front();
        }
        self.block_window.push_back(obs.was_blocked);

        if let Some(hash) = obs.body_hash {
            if self.body_hash_window.len() >= BODY_HASH_WINDOW {
                self.body_hash_window.pop_front();
            }
            self.body_hash_window.push_back(hash);
        }

        // ── 2. Derive the four signals ────────────────────────────────
        let median_rt = self.compute_median_rt();
        let p95_rt = self.compute_p95_rt();
        let block_rate = self.compute_block_rate();
        let body_entropy = self.compute_body_entropy();

        // ── 3. Feed each signal into its CUSUM channel ────────────────
        //
        // Directional signals (block rate + latency) determine whether the
        // WAF became looser or stricter. Body-hash entropy is a
        // non-directional "something changed" witness — it contributes to
        // the total change-event count but not to the directional split,
        // because entropy can rise or fall regardless of enforcement posture.
        let mut up_votes: i32 = 0;
        let mut down_votes: i32 = 0;
        // Non-directional: entropy change just adds to total witness count.
        let mut witness_events: i32 = 0;

        for direction in [
            self.cusum_median_rt.push(median_rt),
            self.cusum_p95_rt.push(p95_rt),
            self.cusum_block_rate.push(block_rate),
        ]
        .iter()
        .flatten()
        {
            if *direction > 0 {
                up_votes += 1;
            } else {
                down_votes += 1;
            }
        }

        // Entropy fires as a non-directional witness.
        if self.cusum_body_entropy.push(body_entropy).is_some() {
            witness_events += 1;
        }

        // ── 4. Agreement gate — need ≥ 2 signals agreeing ─────────────
        // Directional vote count (block_rate + latencies fire), augmented
        // by the entropy witness if it also fired.
        let directional_votes = up_votes + down_votes;
        let total_change_witnesses = directional_votes + witness_events;

        // Must have at least 2 total witnesses of change.
        if total_change_witnesses < SIGNAL_AGREEMENT as i32 {
            return None;
        }

        // Direction is determined by the directional signals only.
        // If there are no directional votes but entropy fired, emit Unclear.
        if directional_votes == 0 {
            return Some(RegimeChange::Unclear);
        }

        // Higher latency + higher block rate = StricterNow.
        // Lower latency + lower block rate = LooserNow.
        // Mixed directional signals = Unclear.
        if up_votes >= SIGNAL_AGREEMENT as i32 && down_votes == 0 {
            Some(RegimeChange::StricterNow)
        } else if down_votes >= SIGNAL_AGREEMENT as i32 && up_votes == 0 {
            Some(RegimeChange::LooserNow)
        } else if up_votes > 0 && down_votes == 0 {
            // Only 1 directional up-vote but entropy corroborated — weak
            // evidence of stricter regime.
            Some(RegimeChange::StricterNow)
        } else if down_votes > 0 && up_votes == 0 {
            // Only 1 directional down-vote but entropy corroborated — weak
            // evidence of looser regime.
            Some(RegimeChange::LooserNow)
        } else {
            Some(RegimeChange::Unclear)
        }
    }

    // ── Signal derivation helpers ─────────────────────────────────────────

    /// Median response time over the current RT window (ms).
    fn compute_median_rt(&self) -> f64 {
        if self.rt_window.is_empty() {
            return 0.0;
        }
        let mut sorted: Vec<f64> = self.rt_window.iter().copied().collect();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let mid = sorted.len() / 2;
        if sorted.len().is_multiple_of(2) {
            (sorted[mid - 1] + sorted[mid]) / 2.0
        } else {
            sorted[mid]
        }
    }

    /// 95th-percentile response time over the current RT window (ms).
    fn compute_p95_rt(&self) -> f64 {
        if self.rt_window.is_empty() {
            return 0.0;
        }
        let mut sorted: Vec<f64> = self.rt_window.iter().copied().collect();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        // Nearest-rank P95.
        let idx = ((sorted.len() as f64 * 0.95).ceil() as usize).saturating_sub(1);
        sorted[idx.min(sorted.len() - 1)]
    }

    /// Fraction of probes blocked over the current block window.
    fn compute_block_rate(&self) -> f64 {
        if self.block_window.is_empty() {
            return 0.0;
        }
        let blocked = self.block_window.iter().filter(|&&b| b).count();
        blocked as f64 / self.block_window.len() as f64
    }

    /// Shannon entropy of the body-hash distribution (bits).
    ///
    /// A sudden shift in the diversity of response bodies (new error pages,
    /// new challenge bodies) signals a WAF rule change.
    fn compute_body_entropy(&self) -> f64 {
        if self.body_hash_window.len() < 2 {
            return 0.0;
        }
        // Count frequency of each unique hash.
        let mut counts: Vec<(u64, usize)> = Vec::new();
        for &h in &self.body_hash_window {
            if let Some(entry) = counts.iter_mut().find(|(hh, _)| *hh == h) {
                entry.1 += 1;
            } else {
                counts.push((h, 1));
            }
        }
        let total = self.body_hash_window.len() as f64;
        counts
            .iter()
            .map(|(_, c)| {
                let p = *c as f64 / total;
                if p > 0.0 { -p * p.log2() } else { 0.0 }
            })
            .sum()
    }

    /// Returns `true` if the detector has accumulated enough observations to
    /// produce meaningful change-point estimates (at least `window_size / 2`
    /// probes).
    #[must_use]
    pub fn has_baseline(&self) -> bool {
        self.probe_count >= (self.window_size / 2) as u64
    }

    /// Snapshot of the four current signal values (for diagnostics/logging).
    /// Order: `[median_rt_ms, p95_rt_ms, block_rate, body_entropy_bits]`.
    #[must_use]
    pub fn signal_snapshot(&self) -> [f64; 4] {
        [
            self.compute_median_rt(),
            self.compute_p95_rt(),
            self.compute_block_rate(),
            self.compute_body_entropy(),
        ]
    }
}

// ── Bypass-rate CUSUM change-point monitor (C-11) ────────────────────────────

/// Event returned from [`BypassRateMonitor::observe`].
///
/// `NoChange` means the CUSUM accumulator is below the decision threshold.
/// `AlarmFired` means a statistically significant drop in bypass rate was
/// detected — a WAF rule update likely pushed bypasses that were working
/// into blocked territory.
#[derive(Debug, Clone, PartialEq)]
pub enum ChangePointEvent {
    /// Bypass rate is stationary; no action needed.
    NoChange,
    /// CUSUM threshold crossed — bypass rate dropped significantly.
    AlarmFired {
        /// Current windowed bypass rate (fraction in `[0.0, 1.0]`).
        observed_rate: f64,
        /// Baseline rate at the time the alarm fired.
        baseline_rate: f64,
        /// Absolute drop in percentage points (baseline − observed) × 100.
        drop_pp: f64,
    },
}

/// Online CUSUM-based bypass-rate change-point detector.
///
/// Tracks a sliding window of bypass/block outcomes and detects downward
/// shifts in the bypass rate (i.e. "WAF started blocking more stuff").
///
/// # Algorithm
///
/// Maintains a one-sided lower CUSUM:
///
/// ```text
/// S_n = max(0, S_{n-1} + (p_baseline - p_observed - k))
/// ```
///
/// where `p_observed` is the current windowed bypass rate, `p_baseline`
/// is the rate at the start of the current stationary regime, and `k` is
/// a slack parameter (half the minimum detectable shift).  When `S_n > h`
/// (decision threshold), an alarm fires, the baseline resets to
/// `p_observed`, and `S_n` resets to zero.
///
/// # Parameters
///
/// | Parameter       | Meaning                                                        | Default |
/// |-----------------|----------------------------------------------------------------|---------|
/// | `window_size`   | Sliding window length for bypass-rate estimation               | 50      |
/// | `k`             | Slack (allowable drift per sample before CUSUM accumulates)    | 0.05    |
/// | `h`             | Decision threshold (CUSUM value that triggers an alarm)        | 0.5     |
///
/// With defaults:
/// - A steady 5 pp/sample drop accumulates into an alarm after ~10 samples.
/// - A perfectly stationary rate never fires.
///
/// # Example
///
/// ```rust
/// use wafrift_strategy::drift_window::{BypassRateMonitor, ChangePointEvent};
///
/// let mut monitor = BypassRateMonitor::new_default();
/// // Fill baseline window with 30% bypass rate.
/// for i in 0..50 {
///     monitor.observe(i % 3 == 0); // ~33% bypass
/// }
/// // Rate collapses to 0% — alarm should fire within 20 more samples.
/// let mut fired = false;
/// for _ in 0..30 {
///     if let ChangePointEvent::AlarmFired { .. } = monitor.observe(false) {
///         fired = true;
///         break;
///     }
/// }
/// assert!(fired, "alarm must fire on a 33%→0% bypass rate drop");
/// ```
#[derive(Debug, Clone)]
pub struct BypassRateMonitor {
    /// Sliding window of recent bypass outcomes (true = bypassed).
    window: VecDeque<bool>,
    /// Maximum number of samples in the sliding window.
    window_size: usize,
    /// Slack parameter k: per-sample allowable drift before CUSUM accumulates.
    k: f64,
    /// Decision threshold h: CUSUM value that triggers an alarm.
    h: f64,
    /// Current lower CUSUM accumulator.
    s: f64,
    /// Baseline bypass rate for the current stationary regime.
    /// `None` until `window_size` samples have been collected.
    baseline: Option<f64>,
}

impl BypassRateMonitor {
    /// Create a monitor with explicit parameters.
    ///
    /// - `window_size`: samples for bypass-rate estimation (min 4).
    /// - `k`: slack (typ. 0.5 × minimum detectable shift in rate).
    /// - `h`: decision threshold (larger = fewer false positives but slower
    ///   detection; smaller = faster detection but noisier).
    #[must_use]
    pub fn new(window_size: usize, k: f64, h: f64) -> Self {
        let ws = window_size.max(4);
        Self {
            window: VecDeque::with_capacity(ws),
            window_size: ws,
            k: k.max(0.0),
            h: h.max(0.0),
            s: 0.0,
            baseline: None,
        }
    }

    /// Create a monitor with production-ready defaults:
    /// `window_size = 50`, `k = 0.05`, `h = 0.5`.
    #[must_use]
    pub fn new_default() -> Self {
        Self::new(50, 0.05, 0.5)
    }

    /// Record one attempt outcome and return whether a change-point was detected.
    ///
    /// `bypassed = true` means the payload evaded the WAF; `false` means blocked.
    ///
    /// This is O(1) per call regardless of window size.
    pub fn observe(&mut self, bypassed: bool) -> ChangePointEvent {
        // Slide the window.
        if self.window.len() >= self.window_size {
            self.window.pop_front();
        }
        self.window.push_back(bypassed);

        // Need a full window to compute a meaningful rate.
        let p_observed = self.current_rate_inner();

        // Set baseline from the first full window.
        let baseline = match self.baseline {
            Some(b) => b,
            None => {
                if self.window.len() < self.window_size {
                    return ChangePointEvent::NoChange;
                }
                // First full window: establish baseline, CUSUM starts at 0.
                self.baseline = Some(p_observed);
                return ChangePointEvent::NoChange;
            }
        };

        // One-sided lower CUSUM: accumulates when rate falls below baseline.
        // S_n = max(0, S_{n-1} + (baseline - p_observed - k))
        self.s = (self.s + (baseline - p_observed - self.k)).max(0.0);

        if self.s > self.h {
            // Alarm fired: reset accumulator and update baseline to current rate.
            self.s = 0.0;
            let old_baseline = baseline;
            self.baseline = Some(p_observed);
            let drop_pp = (old_baseline - p_observed) * 100.0;
            return ChangePointEvent::AlarmFired {
                observed_rate: p_observed,
                baseline_rate: old_baseline,
                drop_pp,
            };
        }

        ChangePointEvent::NoChange
    }

    /// Current windowed bypass rate in `[0.0, 1.0]`.
    ///
    /// Returns `None` if fewer than `window_size` samples have been observed
    /// (no reliable estimate yet).
    #[must_use]
    pub fn current_rate(&self) -> Option<f64> {
        if self.window.len() < self.window_size {
            return None;
        }
        Some(self.current_rate_inner())
    }

    /// Current baseline rate.
    ///
    /// Returns `None` until the first full window has been observed.
    #[must_use]
    pub fn baseline_rate(&self) -> Option<f64> {
        self.baseline
    }

    fn current_rate_inner(&self) -> f64 {
        if self.window.is_empty() {
            return 0.0;
        }
        let bypassed = self.window.iter().filter(|&&b| b).count();
        bypassed as f64 / self.window.len() as f64
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    // ── Helper builders ───────────────────────────────────────────────────

    fn blocked_obs(rt_ms: f64) -> ProbeObservation {
        ProbeObservation {
            response_time_ms: rt_ms,
            was_blocked: true,
            body_hash: Some(0xaaaa_aaaa_aaaa_aaaa),
        }
    }

    fn pass_obs(rt_ms: f64) -> ProbeObservation {
        ProbeObservation {
            response_time_ms: rt_ms,
            was_blocked: false,
            body_hash: Some(0xbbbb_bbbb_bbbb_bbbb),
        }
    }

    fn pass_obs_varied(rt_ms: f64, hash: u64) -> ProbeObservation {
        ProbeObservation {
            response_time_ms: rt_ms,
            was_blocked: false,
            body_hash: Some(hash),
        }
    }

    /// Feed `n` identical stationary observations.
    fn feed_stationary(det: &mut DriftDetector, n: usize, rt: f64, blocked: bool, hash: u64) {
        for _ in 0..n {
            det.observe(ProbeObservation {
                response_time_ms: rt,
                was_blocked: blocked,
                body_hash: Some(hash),
            });
        }
    }

    // ── 1. Step change detected (latency only) ────────────────────────────

    #[test]
    fn latency_step_change_detected() {
        let mut det = DriftDetector::new(20, 3.0);
        // Establish baseline: 20 ms, not blocked.
        feed_stationary(&mut det, 30, 20.0, false, 0x1111);
        // Sudden step up to 200 ms (WAF DPI layer spinning up).
        let mut fired = false;
        for _ in 0..30 {
            if det.observe(blocked_obs(200.0)).is_some() {
                fired = true;
                break;
            }
        }
        assert!(fired, "latency step change must be detected");
    }

    // ── 2. Block-rate-only change detected ───────────────────────────────

    #[test]
    fn block_rate_step_change_detected() {
        let mut det = DriftDetector::new(20, 3.0);
        // Baseline: 0% block rate, constant latency.
        feed_stationary(&mut det, 30, 50.0, false, 0x2222);
        // Sudden 100% block rate (new WAF rule deployed).
        let mut fired = false;
        for _ in 0..30 {
            if det.observe(blocked_obs(52.0)).is_some() {
                fired = true;
                break;
            }
        }
        assert!(fired, "block-rate step change must be detected");
    }

    // ── 3. No false positives on stationary Gaussian noise ───────────────

    #[test]
    fn no_false_positives_stationary_noise() {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut det = DriftDetector::new(50, 4.5);
        // Use a deterministic pseudo-random sequence (FNV-style hash chain)
        // so this test is reproducible without adding a rand dep.
        let mut seed: u64 = 0xdead_beef_cafe_babe;
        let mut false_positives = 0u32;

        for i in 0u64..500 {
            // LCG: cheap deterministic noise in [40, 60] ms range.
            seed = seed
                .wrapping_mul(6_364_136_223_846_793_005)
                .wrapping_add(1_442_695_040_888_963_407);
            let noise = ((seed >> 33) % 21) as f64; // 0..20
            let rt = 40.0 + noise;

            let mut h = DefaultHasher::new();
            i.hash(&mut h);
            let hash = h.finish() % 4; // 4 distinct bodies → stable entropy

            let obs = ProbeObservation {
                response_time_ms: rt,
                was_blocked: (seed >> 60) == 0, // ~6% block rate, stable
                body_hash: Some(hash),
            };
            if det.observe(obs).is_some() {
                false_positives += 1;
            }
        }

        // At threshold 4.5σ on stationary noise we expect 0 false positives
        // over 500 samples. Allow 1 for edge-case tolerance.
        assert!(
            false_positives <= 1,
            "too many false positives on stationary noise: {false_positives}"
        );
    }

    // ── 4. LooserNow fires when block rate drops ──────────────────────────

    #[test]
    fn looser_now_fires_on_block_rate_drop() {
        // Use a small window (8) so the baseline flushes quickly after the
        // regime change, and a low threshold (2.0) for fast detection.
        // 80 transition observations is generous — the CUSUM should fire
        // well before that once both latency and block-rate signals agree.
        let mut det = DriftDetector::new(8, 2.0);
        // Baseline: 100% block, high latency.
        feed_stationary(&mut det, 20, 150.0, true, 0xaaaa);
        // WAF reloads: drops to 0% block, low latency.
        let mut regime = None;
        for _ in 0..80 {
            regime = det.observe(pass_obs(30.0));
            if regime.is_some() {
                break;
            }
        }
        assert_eq!(
            regime,
            Some(RegimeChange::LooserNow),
            "must detect LooserNow when block rate drops"
        );
    }

    // ── 5. StricterNow fires when block rate rises ────────────────────────

    #[test]
    fn stricter_now_fires_on_block_rate_rise() {
        // Small window + low threshold for fast detection.
        let mut det = DriftDetector::new(8, 2.0);
        // Baseline: 0% block, low latency.
        feed_stationary(&mut det, 20, 30.0, false, 0x1111);
        // WAF tightens: 100% block, high latency.
        let mut regime = None;
        for _ in 0..80 {
            regime = det.observe(blocked_obs(200.0));
            if regime.is_some() {
                break;
            }
        }
        assert_eq!(
            regime,
            Some(RegimeChange::StricterNow),
            "must detect StricterNow when block rate rises"
        );
    }

    // ── 6. Multi-signal agreement required (single-signal does not fire) ──

    #[test]
    fn single_signal_alone_does_not_fire() {
        // Use a very high threshold so only latency changes; block rate stays
        // constant. With threshold=10 and window=100, two signals firing at
        // once is extremely unlikely from a single-direction latency nudge.
        // We verify the detector stays silent for a small nudge.
        let mut det = DriftDetector::new(50, 10.0);
        feed_stationary(&mut det, 60, 50.0, false, 0xcccc);

        // Tiny latency nudge — not enough to move multiple signals past threshold.
        let mut fired = false;
        for _ in 0..10 {
            if det.observe(pass_obs(55.0)).is_some() {
                fired = true;
                break;
            }
        }
        assert!(
            !fired,
            "tiny single-signal nudge must not fire with high threshold"
        );
    }

    // ── 7. Window-size boundary: detector still works at minimum window ───

    #[test]
    fn minimum_window_size_respected() {
        // window_size=0 is clamped to 8 internally.
        let mut det = DriftDetector::new(0, 2.0);
        assert_eq!(
            det.window_size, 8,
            "window_size must be clamped to minimum 8"
        );

        // Should still detect a gross step change.
        feed_stationary(&mut det, 20, 20.0, false, 0x1234);
        let mut fired = false;
        for _ in 0..30 {
            if det.observe(blocked_obs(500.0)).is_some() {
                fired = true;
                break;
            }
        }
        assert!(
            fired,
            "detector with minimum window must still detect step changes"
        );
    }

    // ── 8. Threshold sensitivity: lower threshold = faster detection ──────

    #[test]
    fn lower_threshold_detects_faster() {
        let mut fast = DriftDetector::new(20, 1.5);
        let mut slow = DriftDetector::new(20, 5.0);

        feed_stationary(&mut fast, 25, 30.0, false, 0x9999);
        feed_stationary(&mut slow, 25, 30.0, false, 0x9999);

        let mut fast_detection = None;
        let mut slow_detection = None;

        for i in 0..50u64 {
            let obs = blocked_obs(200.0);
            if fast_detection.is_none() && fast.observe(obs.clone()).is_some() {
                fast_detection = Some(i);
            }
            if slow_detection.is_none() && slow.observe(obs).is_some() {
                slow_detection = Some(i);
            }
        }

        assert!(fast_detection.is_some(), "low-threshold detector must fire");
        assert!(
            fast_detection <= slow_detection.or(Some(u64::MAX)),
            "low-threshold must detect at least as fast as high-threshold"
        );
    }

    // ── 9. JSON serialization round-trips ────────────────────────────────

    #[test]
    fn json_serialization_round_trips() {
        let mut det = DriftDetector::new(30, 3.5);
        feed_stationary(&mut det, 15, 40.0, false, 0xdead);
        det.observe(blocked_obs(300.0));

        let json = serde_json::to_string(&det).expect("serialization must succeed");
        let restored: DriftDetector =
            serde_json::from_str(&json).expect("deserialization must succeed");

        assert_eq!(restored.window_size, det.window_size);
        assert_eq!(restored.threshold, det.threshold);
        assert_eq!(restored.probe_count, det.probe_count);
    }

    // ── 10. Body-entropy change alone contributes a signal ───────────────

    #[test]
    fn body_entropy_signal_contributes() {
        let mut det = DriftDetector::new(20, 2.0);

        // Baseline: all responses identical body hash (entropy = 0).
        feed_stationary(&mut det, 30, 50.0, false, 0xAAAA_AAAA);

        // Now each response has a unique body hash (high entropy) — new
        // challenge pages appearing signals rule change.
        let mut body_entropy_fired = false;
        for i in 0u64..40 {
            let obs = pass_obs_varied(52.0, i * 0xdead_beef + 1);
            // snapshot entropy increasing
            let snap_before = det.signal_snapshot()[3];
            det.observe(obs);
            let snap_after = det.signal_snapshot()[3];
            if snap_after > snap_before + 0.01 {
                body_entropy_fired = true;
                break;
            }
        }
        assert!(
            body_entropy_fired,
            "body entropy signal must increase on hash diversity"
        );
    }

    // ── 11. has_baseline returns false before window/2 probes ────────────

    #[test]
    fn has_baseline_gated_on_probe_count() {
        let mut det = DriftDetector::new(40, 4.0);
        assert!(!det.has_baseline(), "no baseline before any probes");

        for _ in 0..19 {
            det.observe(pass_obs(50.0));
        }
        assert!(!det.has_baseline(), "baseline not ready at 19/40 probes");

        det.observe(pass_obs(50.0)); // 20th probe = window_size/2
        assert!(
            det.has_baseline(),
            "baseline must be ready at window_size/2 probes"
        );
    }

    // ── 12. probe_count saturates at u64::MAX ────────────────────────────

    #[test]
    fn probe_count_saturates_not_wraps() {
        let mut det = DriftDetector::new(8, 4.0);
        // Inject a near-max count directly (can't loop 2^64 times).
        det.probe_count = u64::MAX - 1;
        det.observe(pass_obs(50.0));
        assert_eq!(
            det.probe_count,
            u64::MAX,
            "probe_count must saturate at u64::MAX"
        );
        det.observe(pass_obs(50.0));
        assert_eq!(
            det.probe_count,
            u64::MAX,
            "probe_count must remain at u64::MAX after second saturating add"
        );
    }

    // ── 13. signal_snapshot returns correct structure ─────────────────────

    #[test]
    fn signal_snapshot_structure() {
        let mut det = DriftDetector::default();
        // Zero-state snapshot.
        let snap = det.signal_snapshot();
        assert_eq!(snap.len(), 4);
        for v in &snap {
            assert!(
                v.is_finite(),
                "all signal values must be finite at zero state"
            );
        }

        // After observations the snapshot must update.
        feed_stationary(&mut det, 10, 75.0, true, 0xBEEF);
        let snap2 = det.signal_snapshot();
        // median and p95 must be ~75.0.
        assert!((snap2[0] - 75.0).abs() < 1.0, "median RT must be ~75 ms");
        // block rate must be 1.0 (all blocked).
        assert!((snap2[2] - 1.0).abs() < 0.01, "block rate must be ~1.0");
    }

    // ═══════════════════════════════════════════════════════════════════════
    // BypassRateMonitor tests (C-11 — CUSUM bypass-rate change-point)
    // ═══════════════════════════════════════════════════════════════════════

    // ── BRM-1. Empty window: current_rate is None, baseline is None ───────

    #[test]
    fn bypass_monitor_empty_window_returns_none() {
        let monitor = BypassRateMonitor::new(50, 0.05, 0.5);
        assert!(
            monitor.current_rate().is_none(),
            "no rate before window fills"
        );
        assert!(
            monitor.baseline_rate().is_none(),
            "no baseline before window fills"
        );
    }

    // ── BRM-2. Monotone good rate (30% bypass, steady) → NO alarm ─────────

    #[test]
    fn bypass_monitor_steady_rate_no_alarm() {
        let mut monitor = BypassRateMonitor::new(50, 0.05, 0.5);
        // 200 samples at ~30% bypass rate (deterministic: every 3rd is bypass).
        let mut fired = false;
        for i in 0..200usize {
            if let ChangePointEvent::AlarmFired { .. } = monitor.observe(i % 3 == 0) {
                fired = true;
                break;
            }
        }
        assert!(!fired, "steady 33% bypass rate must not fire an alarm");
    }

    // ── BRM-3. Monotone bad rate (0% bypass after baseline) → alarm fires ─

    #[test]
    fn bypass_monitor_zero_rate_fires_alarm() {
        let mut monitor = BypassRateMonitor::new(20, 0.05, 0.5);
        // Establish baseline at 50% bypass (10 bypasses in first 20).
        for i in 0..20usize {
            monitor.observe(i % 2 == 0);
        }
        assert!(monitor.baseline_rate().is_some());
        // Drop to 0% — alarm must fire within 30 more samples.
        let mut fired = false;
        for _ in 0..30 {
            if let ChangePointEvent::AlarmFired { .. } = monitor.observe(false) {
                fired = true;
                break;
            }
        }
        assert!(
            fired,
            "zero bypass rate after 50% baseline must trigger alarm"
        );
    }

    // ── BRM-4. Bimodal pattern: alarm at the break ─────────────────────────

    #[test]
    fn bypass_monitor_bimodal_alarm_at_break() {
        let mut monitor = BypassRateMonitor::new(30, 0.05, 0.5);
        // Phase 1: 60% bypass (steady regime).
        for i in 0..60usize {
            monitor.observe(i % 5 < 3); // 3/5 = 60%
        }
        // Phase 2: 0% bypass (WAF rule update).
        let mut alarm_idx: Option<usize> = None;
        for i in 0..60usize {
            if let ChangePointEvent::AlarmFired { .. } = monitor.observe(false) {
                alarm_idx = Some(i);
                break;
            }
        }
        assert!(
            alarm_idx.is_some(),
            "bimodal pattern must trigger alarm in phase-2 region"
        );
        // Alarm must fire reasonably quickly (within 40 samples of the break).
        assert!(
            alarm_idx.unwrap() < 40,
            "alarm should fire within 40 samples of the regime break"
        );
    }

    // ── BRM-5. High threshold (h=10) does not fire on moderate drop ───────

    #[test]
    fn bypass_monitor_high_threshold_no_fire() {
        let mut monitor = BypassRateMonitor::new(30, 0.05, 10.0);
        // Establish 50% baseline.
        for i in 0..30usize {
            monitor.observe(i % 2 == 0);
        }
        // Drop to 40% — a moderate, not catastrophic, decrease.
        let mut fired = false;
        for i in 0..60usize {
            if let ChangePointEvent::AlarmFired { .. } = monitor.observe(i % 5 < 2) {
                fired = true;
                break;
            }
        }
        assert!(!fired, "h=10 must NOT fire on a moderate rate drop");
    }

    // ── BRM-6. Low threshold (h=0.01) fires near-immediately ─────────────

    #[test]
    fn bypass_monitor_low_threshold_fires_fast() {
        let mut monitor = BypassRateMonitor::new(10, 0.05, 0.01);
        // Establish 100% bypass baseline.
        for _ in 0..10 {
            monitor.observe(true);
        }
        // First blocked sample should fire almost immediately.
        let mut alarm_idx: Option<usize> = None;
        for i in 0..10 {
            if let ChangePointEvent::AlarmFired { .. } = monitor.observe(false) {
                alarm_idx = Some(i);
                break;
            }
        }
        assert!(
            alarm_idx.is_some(),
            "h=0.01 must fire almost immediately on any downward deviation"
        );
        assert!(
            alarm_idx.unwrap() <= 5,
            "h=0.01 must fire within 5 samples of the change (got {:?})",
            alarm_idx
        );
    }

    // ── BRM-7. Reset-after-alarm: baseline re-established at new level ─────

    #[test]
    fn bypass_monitor_reset_after_alarm() {
        // Use window_size=4 so the window fully drains in 4 steps.
        // With h=0.5 and k=0.05, a 100%→0% drop will fire alarm
        // after a few samples, then the window drains within 4 more.
        let mut monitor = BypassRateMonitor::new(4, 0.05, 0.5);
        // Establish 100% bypass baseline.
        for _ in 0..4 {
            monitor.observe(true);
        }
        // Drive to 0% — run enough samples to (a) fire the alarm AND
        // (b) fully flush all `true` values from the window before
        //     the second-alarm check begins.
        let mut first_alarm_fired = false;
        for _ in 0..20 {
            let evt = monitor.observe(false);
            if let ChangePointEvent::AlarmFired { observed_rate, .. } = evt {
                first_alarm_fired = true;
                // After reset, baseline must equal the observed rate.
                let new_baseline = monitor.baseline_rate().unwrap();
                assert!(
                    (new_baseline - observed_rate).abs() < 0.05,
                    "baseline must reset to observed rate after alarm: new_baseline={new_baseline:.3}, observed={observed_rate:.3}"
                );
                // Continue the loop (don't break) so the window drains
                // to all-false before the second-alarm test below.
                // The alarm has fired and baseline has been reset; we
                // need a few more calls to drain the old `true` entries.
            }
        }
        assert!(first_alarm_fired, "first alarm must have fired");
        // After 20 blocked calls on a size-4 window, the window is
        // definitely all-false (0% bypass rate = 0%) and baseline = 0%.

        // Now stay at 0% — no second alarm should fire within 100 samples
        // (CUSUM accumulator stays at 0 when baseline ≈ p_observed).
        let mut second_alarm = false;
        for _ in 0..100 {
            if let ChangePointEvent::AlarmFired { .. } = monitor.observe(false) {
                second_alarm = true;
                break;
            }
        }
        assert!(
            !second_alarm,
            "no second alarm when staying at 0% after baseline reset and window drain"
        );
    }

    // ── BRM-8. ANTI-RIG: alarm fires within 20 attempts of 30%→0% drop ───

    #[test]
    fn bypass_monitor_alarm_within_20_samples_of_drop() {
        // Uses default params (window=50, k=0.05, h=0.5).
        let mut monitor = BypassRateMonitor::new_default();
        // Fill baseline window at exactly 30% bypass rate.
        // 50 samples: 15 bypassed, 35 blocked. Deterministic.
        for i in 0..50usize {
            monitor.observe(i % 10 < 3); // 3/10 = 30%
        }
        let baseline = monitor.baseline_rate().expect("baseline must be set");
        assert!(
            (baseline - 0.3).abs() < 0.05,
            "baseline must be ~30%: got {baseline:.3}"
        );

        // Drop to 0% bypass — alarm MUST fire within 20 samples.
        let mut alarm_idx: Option<usize> = None;
        for i in 0..20 {
            if let ChangePointEvent::AlarmFired { .. } = monitor.observe(false) {
                alarm_idx = Some(i);
                break;
            }
        }
        assert!(
            alarm_idx.is_some(),
            "alarm must fire within 20 samples of a 30%→0% bypass rate drop"
        );
    }

    // ── BRM-9. ANTI-RIG: no alarm on steady 30% for 200 samples ──────────

    #[test]
    fn bypass_monitor_no_alarm_on_steady_30pct_200_samples() {
        let mut monitor = BypassRateMonitor::new_default();
        // 200 samples at exactly 30% bypass rate (deterministic).
        let mut fired = false;
        for i in 0..200usize {
            if let ChangePointEvent::AlarmFired { .. } = monitor.observe(i % 10 < 3) {
                fired = true;
                break;
            }
        }
        assert!(
            !fired,
            "must NOT fire on a perfectly steady 30% bypass rate over 200 samples"
        );
    }

    // ── BRM-10. current_rate tracks the window accurately ─────────────────

    #[test]
    fn bypass_monitor_current_rate_accurate() {
        let mut monitor = BypassRateMonitor::new(10, 0.05, 0.5);
        // Fill with exactly 7 bypassed out of 10.
        for i in 0..10usize {
            monitor.observe(i < 7);
        }
        let rate = monitor
            .current_rate()
            .expect("rate must be available after window fills");
        assert!(
            (rate - 0.7).abs() < 0.01,
            "current_rate must be ~70% but got {rate:.3}"
        );
    }
}