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
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
//! Per-host evasion state — tracks what works and what doesn't.
//!
//! One struct, one job: maintain a per-host record of which techniques
//! have been tried, which succeeded, and how aggressively we need
//! to escalate. Maintains a pool of proven winners and continuously
//! re-evaluates as the WAF adapts.
//!
//! # Drift detection
//!
//! The [`DriftDetector`] embedded in every [`HostState`] tracks four CUSUM
//! signal streams (median latency, P95 latency, block rate, body-hash entropy)
//! and fires a [`RegimeChange`] when ≥ 2 signals agree that the WAF has
//! changed its enforcement posture.  When [`RegimeChange::LooserNow`] fires,
//! `previously_blocked` is re-queued so the strategy can retry payloads that
//! were blocked under the old regime.

use wafrift_content_type as content_type;
use wafrift_encoding::encoding;
use wafrift_types::Technique;
use wafrift_types::escalation::EscalationLevel;

use crate::drift_window::{DriftDetector, ProbeObservation, RegimeChange};

/// Minimum number of attempts before a technique is eligible for
/// promotion to the winner pool or demotion to the blocklist.
const MIN_ATTEMPTS_FOR_VERDICT: u32 = 3;

/// Success rate (0.0–1.0) above which a technique is promoted to the
/// winner pool and rotated for all subsequent requests.
const WINNER_THRESHOLD: f64 = 0.60;

/// Success rate below which a technique is demoted to the blocklist
/// and no longer used.
const BLOCK_THRESHOLD: f64 = 0.20;

/// Number of consecutive blocks on a previously-winning technique
/// before it is evicted from the winner pool (drift detection).
const DRIFT_BLOCK_LIMIT: u32 = 2;

/// Hard cap on `prioritized_techniques` and `avoided_techniques` so a
/// long-running scan that ingests many adversarial WAF profiles cannot
/// grow `HostState` memory without bound. Audit (2026-05-10).
///
/// Canonical source: [`wafrift_types::HOST_TECHNIQUE_HINTS_CAP`].
/// `wafrift-transport` enforces the same cap when merging inbound
/// WAF profile signals — if this value is changed, that site auto-
/// follows because both alias the same workspace constant.
const MAX_HINTS_PER_LIST: usize = wafrift_types::HOST_TECHNIQUE_HINTS_CAP;

/// Hard cap on `technique_stats` and `winner_consecutive_blocks` —
/// per-host structures that key on the technique name. With ~100
/// distinct names in the standard catalogue, 500 is generous headroom
/// while still bounding worst-case adversarial growth. Audit (2026-05-10).
const MAX_TECHNIQUE_STATS: usize = 500;

/// Per-host evasion state — tracks what works and what doesn't.
#[derive(Debug, Default, Clone)]
pub struct HostState {
    /// Number of requests blocked.
    pub blocks: u32,
    /// Number of requests that succeeded.
    pub successes: u32,
    /// Encoding strategies that have been tried.
    pub tried_encodings: Vec<encoding::Strategy>,
    /// Content-Type variants that have been tried.
    pub tried_content_types: Vec<content_type::ContentTypeTechnique>,
    /// The last strategy that succeeded (if any).
    pub last_success: Option<Technique>,
    /// Per-technique success rate: (`technique_name`, successes, attempts).
    pub technique_stats: Vec<(String, u32, u32)>,
    /// Whether WAF presence has been confirmed via calibration.
    pub waf_confirmed: bool,
    /// Detected WAF name (if identified).
    pub waf_name: Option<String>,

    // ── Adaptive rotation state ─────────────────────────────────────
    /// Techniques with a proven bypass rate above the internal winner threshold.
    /// The proxy rotates only these once the discovery phase ends.
    pub proven_winners: Vec<String>,
    /// Techniques that consistently fail — never used again unless
    /// the winner pool is exhausted and a full re-discovery is needed.
    pub blocklisted: Vec<String>,
    /// Round-robin index into `proven_winners`.
    pub rotation_index: usize,
    /// Per-winner consecutive block counter for drift detection.
    /// Key: technique name, Value: consecutive blocks since last success.
    pub winner_consecutive_blocks: Vec<(String, u32)>,
    /// Whether the initial discovery phase is complete (enough data
    /// collected to populate the winner pool).
    pub discovery_complete: bool,

    // ── WAF-aware strategy hints (populated from ResponseSignal) ────
    /// Techniques the matched WAF profile recommends trying first.
    /// Set by `record_signal()` when a response matches a loaded profile.
    /// The strategy engine reads these to bias MCTS exploration or
    /// shortcut the escalation pipeline.
    pub prioritized_techniques: Vec<String>,
    /// Techniques the matched WAF profile says are a waste of requests.
    /// The strategy engine skips these entirely.
    pub avoided_techniques: Vec<String>,
    /// WAF inspection model hint (e.g. "`single_pass_url_decode`",
    /// "`multi_regex_scoring`"). Informs which encoding dimensions to explore.
    pub inspection_model: Option<String>,
    /// Number of rate-limit (429) responses seen — drives backoff, not
    /// technique change.
    pub rate_limits: u32,
    /// Number of JS challenges (Cloudflare captcha pages, etc.) seen.
    pub challenges: u32,

    // ── Drift-aware evasion window (#115) ───────────────────────────
    /// CUSUM-based drift detector tracking per-target WAF regime changes.
    /// Updated on every probe result via `observe_probe`.
    pub drift: DriftDetector,
    /// Payloads (technique key strings) that were blocked under a previous
    /// WAF regime and are eligible for retry when `LooserNow` fires.
    pub previously_blocked: Vec<String>,
    /// Payloads queued for retry after a `LooserNow` regime change.
    /// The strategy drains this queue before generating new candidates.
    pub drift_retry_queue: Vec<String>,
}

impl HostState {
    // ── Drift-aware probe observation (#115) ────────────────────────────

    /// Feed a probe result into the drift detector.
    ///
    /// Call this on **every** probe, in addition to the technique-tracking
    /// methods (`record_block_for`, `record_success_for_many`, etc.).
    ///
    /// When [`RegimeChange::LooserNow`] fires, all entries in
    /// `previously_blocked` are moved to `drift_retry_queue` so the
    /// strategy engine can retry them under the new, more permissive regime.
    ///
    /// Returns the detected [`RegimeChange`] if one was fired, `None`
    /// otherwise.
    pub fn observe_probe(&mut self, obs: ProbeObservation) -> Option<RegimeChange> {
        // Track blocked technique keys for potential retry on LooserNow.
        if obs.was_blocked {
            // `previously_blocked` is drained into `drift_retry_queue` on
            // LooserNow, so it naturally cycles. Cap at 1000 entries to
            // bound memory on a long-running proxy session against a target
            // that blocks 100% of probes.
            const MAX_PREVIOUSLY_BLOCKED: usize = 1_000;
            if self.previously_blocked.len() < MAX_PREVIOUSLY_BLOCKED {
                // Record the probe-level event as a sentinel; callers that
                // have richer context (technique key) can push directly to
                // `previously_blocked` instead of relying on the sentinel.
                // The sentinel keeps the retry queue functional even for
                // callers that only call `observe_probe`.
                let sentinel = format!("probe:{}", self.blocks.saturating_add(1));
                self.previously_blocked.push(sentinel);
            }
        }

        let change = self.drift.observe(obs)?;

        match &change {
            RegimeChange::LooserNow => {
                // WAF relaxed: move the entire blocked corpus to retry queue.
                // The strategy drains `drift_retry_queue` with
                // `drain_drift_retry_queue()`.
                self.drift_retry_queue.append(&mut self.previously_blocked);
            }
            RegimeChange::StricterNow => {
                // WAF tightened: discard the retry queue (those techniques
                // will fail again under the stricter regime).
                self.drift_retry_queue.clear();
            }
            RegimeChange::Unclear => {
                // Mixed signals: keep the retry queue intact but don't add
                // to it. The next observation may resolve the direction.
            }
        }

        Some(change)
    }

    /// Enqueue a specific blocked technique key for potential retry on the
    /// next `LooserNow` regime change.
    ///
    /// Call this alongside `record_block_for` when you have the technique
    /// key handy.  Bounded at 1000 entries per host.
    pub fn mark_blocked_for_retry(&mut self, technique_key: &str) {
        const MAX_PREVIOUSLY_BLOCKED: usize = 1_000;
        if self.previously_blocked.len() < MAX_PREVIOUSLY_BLOCKED
            && !self.previously_blocked.contains(&technique_key.to_string())
        {
            self.previously_blocked.push(technique_key.to_string());
        }
    }

    /// Drain the drift retry queue, returning all technique keys that should
    /// be retried under the newly-detected permissive WAF regime.
    ///
    /// Returns an empty `Vec` if no regime change has fired yet or if the
    /// queue has already been drained.
    pub fn drain_drift_retry_queue(&mut self) -> Vec<String> {
        std::mem::take(&mut self.drift_retry_queue)
    }

    /// Record a blocked response (no technique tracking).
    pub fn record_block(&mut self) {
        self.blocks = self.blocks.saturating_add(1);
    }

    fn bump_block_attempt_for_technique(&mut self, technique_name: &str) {
        if let Some(stat) = self
            .technique_stats
            .iter_mut()
            .find(|(n, _, _)| n == technique_name)
        {
            // saturating_add avoids the u32 overflow audit finding —
            // pre-fix `stat.2 += 1` would panic in debug or wrap in
            // release after 2^32 attempts.
            stat.2 = stat.2.saturating_add(1);
        } else if self.technique_stats.len() < MAX_TECHNIQUE_STATS {
            // Audit (2026-05-10): cap technique_stats so a host that
            // sees an unbounded stream of unique technique names from
            // adversarial profiles cannot grow the vector forever.
            self.technique_stats
                .push((technique_name.to_string(), 0, 1));
        }

        if self.proven_winners.contains(&technique_name.to_string()) {
            if let Some(entry) = self
                .winner_consecutive_blocks
                .iter_mut()
                .find(|(n, _)| n == technique_name)
            {
                entry.1 = entry.1.saturating_add(1);
            } else if self.winner_consecutive_blocks.len() < MAX_TECHNIQUE_STATS {
                self.winner_consecutive_blocks
                    .push((technique_name.to_string(), 1));
            }
        }
    }

    /// Record a blocked response with technique tracking.
    ///
    /// Updates per-technique stats and triggers drift detection if the
    /// technique was previously in the winner pool.
    pub fn record_block_for(&mut self, technique_name: &str) {
        self.blocks = self.blocks.saturating_add(1);
        self.bump_block_attempt_for_technique(technique_name);
        self.prune();
    }

    /// One blocked HTTP response attributed to several active techniques (compound evasion).
    pub fn record_block_for_many(&mut self, technique_names: &[String]) {
        self.blocks = self.blocks.saturating_add(1);
        for name in technique_names {
            self.bump_block_attempt_for_technique(name);
        }
        self.prune();
    }

    fn bump_success_for_technique(&mut self, technique: &Technique) {
        let name = technique.to_string();
        if let Some(stat) = self.technique_stats.iter_mut().find(|(n, _, _)| *n == name) {
            // Use saturating_add to match the block path (audit fix: plain `+= 1`
            // panics in debug or silently wraps in release after 2^32 successes on
            // the same technique in a long-running proxy session).
            stat.1 = stat.1.saturating_add(1);
            stat.2 = stat.2.saturating_add(1);
        } else if self.technique_stats.len() < MAX_TECHNIQUE_STATS {
            // Audit (F133 2026-05-24): the block path already capped new
            // insertions at MAX_TECHNIQUE_STATS; the success path did not,
            // allowing unbounded growth if an adversary stream produced
            // unlimited distinct technique names that all "pass".  Mirror
            // the same guard here.
            self.technique_stats.push((name.clone(), 1, 1));
        }

        if let Some(entry) = self
            .winner_consecutive_blocks
            .iter_mut()
            .find(|(n, _)| *n == name)
        {
            entry.1 = 0;
        }
    }

    /// Record a successful response with technique tracking.
    ///
    /// Resets the drift counter for this technique (it's still working)
    /// and triggers pool re-evaluation.
    pub fn record_success(&mut self, technique: Technique) {
        self.record_success_for_many(&[technique]);
    }

    /// One successful HTTP response when multiple techniques were applied together.
    pub fn record_success_for_many(&mut self, techniques: &[Technique]) {
        if techniques.is_empty() {
            return;
        }
        self.successes = self.successes.saturating_add(1);
        for technique in techniques {
            self.bump_success_for_technique(technique);
        }
        self.last_success = techniques.last().cloned();

        self.evaluate_pools();
    }

    /// Get the technique with highest success rate.
    ///
    /// Requires at least 2 attempts before a technique is considered to
    /// avoid drawing conclusions from a single sample.
    #[must_use]
    pub fn best_technique(&self) -> Option<&str> {
        self.technique_stats
            .iter()
            .filter(|(_, _, attempts)| *attempts >= 2)
            .max_by(|(_, s1, a1), (_, s2, a2)| {
                let rate1 = if *a1 == 0 {
                    0.0
                } else {
                    f64::from(*s1) / f64::from(*a1)
                };
                let rate2 = if *a2 == 0 {
                    0.0
                } else {
                    f64::from(*s2) / f64::from(*a2)
                };
                rate1
                    .partial_cmp(&rate2)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(name, _, _)| name.as_str())
    }

    /// Get success rate for a specific technique.
    #[must_use]
    pub fn technique_success_rate(&self, name: &str) -> f64 {
        self.technique_stats
            .iter()
            .find(|(n, _, _)| n == name)
            .map_or(0.0, |(_, s, a)| {
                if *a > 0 {
                    f64::from(*s) / f64::from(*a)
                } else {
                    0.0
                }
            })
    }

    /// Mark WAF as confirmed present on this host.
    pub fn confirm_waf(&mut self, waf_name: Option<String>) {
        self.waf_confirmed = true;
        self.waf_name = waf_name;
    }

    /// Check if this host needs evasion at all.
    ///
    /// Returns `false` only if we have sent requests and none were blocked
    /// AND the WAF has not been confirmed via calibration. When we have no
    /// data yet the safe default is to assume evasion is needed.
    #[must_use]
    pub fn needs_evasion(&self) -> bool {
        self.waf_confirmed || self.blocks > 0 || (self.successes == 0 && self.blocks == 0)
    }

    /// Get the next encoding strategy to try (one we haven't tried yet
    /// and isn't blocklisted). Blocklist comparison uses the canonical
    /// `Strategy::as_str()` name — same form `record_block_for_many` /
    /// proxy gene-bank persistence use everywhere else. (Earlier
    /// versions used `format!("{s:?}")` here, which produced `PascalCase`
    /// debug names that did not match the kebab-case strings stored on
    /// blocklist.)
    #[must_use]
    pub fn next_encoding(&self) -> Option<encoding::Strategy> {
        encoding::all_strategies().iter().copied().find(|s| {
            !self.tried_encodings.contains(s) && !self.blocklisted.contains(&s.as_str().to_string())
        })
    }

    /// Escalation level based on block count.
    #[must_use]
    pub fn escalation_level(&self) -> EscalationLevel {
        match self.blocks {
            0 => EscalationLevel::None,
            1..=2 => EscalationLevel::Light,
            3..=5 => EscalationLevel::Medium,
            _ => EscalationLevel::Heavy,
        }
    }

    // ── Adaptive rotation API ───────────────────────────────────────

    /// Pick the next technique from the proven winner pool.
    ///
    /// Returns `None` if the winner pool is empty (still in discovery
    /// phase or all winners were pruned). Round-robins through
    /// the pool to avoid WAF pattern detection.
    #[must_use]
    pub fn next_winner(&mut self) -> Option<String> {
        if self.proven_winners.is_empty() {
            return None;
        }
        let idx = self.rotation_index % self.proven_winners.len();
        self.rotation_index = self.rotation_index.wrapping_add(1);
        Some(self.proven_winners[idx].clone())
    }

    /// Whether the host has finished initial discovery and has a
    /// non-empty winner pool to rotate through.
    #[must_use]
    pub fn has_winners(&self) -> bool {
        self.discovery_complete && !self.proven_winners.is_empty()
    }

    /// Re-evaluate all technique stats and populate winner/blocklist pools.
    ///
    /// Called after every success observation. Checks whether enough
    /// data has been collected to declare discovery complete.
    pub fn evaluate_pools(&mut self) {
        // Audit (2026-05-10): pre-fix this used `u32::sum()` which can
        // overflow under sustained scanning (technique_stats.len()
        // × MAX_TECHNIQUE_STATS attempts each). Lift to u64 so the
        // total monotonically grows past u32::MAX without panicking
        // in debug or wrapping in release.
        let total_attempted: u64 = self
            .technique_stats
            .iter()
            .map(|(_, _, a)| u64::from(*a))
            .sum();

        // Don't declare discovery complete until we have meaningful data.
        if total_attempted < 10 {
            return;
        }

        let mut new_winners = Vec::new();
        let mut new_blocked = Vec::new();

        for (name, successes, attempts) in &self.technique_stats {
            if *attempts < MIN_ATTEMPTS_FOR_VERDICT {
                continue;
            }
            let rate = f64::from(*successes) / f64::from(*attempts);
            if rate >= WINNER_THRESHOLD {
                new_winners.push(name.clone());
            } else if rate < BLOCK_THRESHOLD {
                new_blocked.push(name.clone());
            }
        }

        if !new_winners.is_empty() {
            self.proven_winners = new_winners;
            self.discovery_complete = true;
        }

        // Only add newly-discovered blocked techniques — don't
        // remove existing blocklist entries.
        for blocked in new_blocked {
            if !self.blocklisted.contains(&blocked) {
                self.blocklisted.push(blocked);
            }
        }
    }

    /// Continuously prune the winner pool based on drift detection.
    ///
    /// If a previously-winning technique has been blocked repeatedly
    /// (see internal drift limit), it is evicted from
    /// the winner pool and moved to the blocklist. If the winner pool
    /// becomes empty, `discovery_complete` is reset so the system
    /// re-enters discovery mode.
    pub fn prune(&mut self) {
        let mut evicted = Vec::new();

        for (name, consecutive) in &self.winner_consecutive_blocks {
            if *consecutive >= DRIFT_BLOCK_LIMIT {
                evicted.push(name.clone());
            }
        }

        for name in &evicted {
            self.proven_winners.retain(|w| w != name);
            if !self.blocklisted.contains(name) {
                self.blocklisted.push(name.clone());
            }
            self.winner_consecutive_blocks.retain(|(n, _)| n != name);
        }

        // If all winners were pruned, re-enter discovery mode.
        if self.proven_winners.is_empty() && self.discovery_complete {
            self.discovery_complete = false;
            // Clear blocklist to allow full re-discovery with fresh data.
            self.blocklisted.clear();
            // Reset technique stats for a clean slate.
            self.technique_stats.clear();
            self.winner_consecutive_blocks.clear();
        }
    }

    // ── Rich response signal API ────────────────────────────────────
    //
    // Mirrors `wafrift_transport::signal::BlockClass` via boolean flags
    // instead of importing the type — the strategy crate must not depend
    // on transport (transport already depends on strategy). The proxy
    // destructures the transport `ResponseSignal` and passes the fields
    // through here.

    /// Record a rich response signal from the upstream WAF response.
    ///
    /// This is the upgrade path from the binary `record_block()` /
    /// `record_success()` API. Instead of just "blocked or not," the
    /// signal tells us:
    ///
    /// - **Which WAF** produced the block (→ sets `waf_name`)
    /// - **What techniques to prioritize** (→ populates `prioritized_techniques`)
    /// - **What to avoid** (→ populates `avoided_techniques`)
    /// - **Whether this is a rate limit / challenge** (→ doesn't penalize
    ///   the current technique, increments `rate_limits` / `challenges`)
    /// - **The inspection model** (→ stored for MCTS dimension weighting)
    ///
    /// # Arguments
    ///
    /// * `is_hard_block` — WAF returned 403/406/etc.
    /// * `is_soft_block` — 200 OK but body contains WAF block markers.
    /// * `is_rate_limit` — 429 or equivalent — back off, don't change technique.
    /// * `is_challenge` — JS challenge / captcha — back off, don't change technique.
    /// * `matched_waf` — Which WAF profile matched (e.g. "Cloudflare").
    /// * `prioritize` — Techniques the profile recommends.
    /// * `avoid` — Techniques the profile says to skip.
    /// * `inspection_model` — WAF's inspection strategy hint.
    /// * `technique_keys` — The techniques that were applied to this request.
    #[allow(clippy::too_many_arguments)]
    pub fn record_signal(
        &mut self,
        is_hard_block: bool,
        is_soft_block: bool,
        is_rate_limit: bool,
        is_challenge: bool,
        matched_waf: Option<&str>,
        prioritize: &[String],
        avoid: &[String],
        inspection_model: Option<&str>,
        technique_keys: &[String],
    ) {
        if is_rate_limit {
            // Rate limit is NOT a technique failure. Don't penalize
            // the current technique — the WAF is telling us to slow
            // down, not that our payload was caught.
            self.rate_limits = self.rate_limits.saturating_add(1);
        } else if is_challenge {
            // JS challenge (e.g. Cloudflare captcha). Same logic:
            // this isn't a technique failure, it's a bot detection.
            self.challenges = self.challenges.saturating_add(1);
        } else if is_hard_block || is_soft_block {
            // Real technique failure — the WAF caught the payload.
            self.blocks = self.blocks.saturating_add(1);
            for name in technique_keys {
                self.bump_block_attempt_for_technique(name);
            }
            self.prune();
        } else {
            // Pass — bypass confirmed.
            self.successes = self.successes.saturating_add(1);
            // Success attribution is handled by the caller via
            // record_success_for_many() since it needs the full
            // Technique objects, not just string keys.
            //
            // Defensive: also run evaluate_pools here so the winner
            // pool populates EVEN IF a future caller forgets to
            // invoke record_success_for_many. record_success_for_many
            // also calls evaluate_pools so the existing
            // proxy/transport call paths just run it twice (cheap).
            // Without this, a caller that ONLY invoked record_signal
            // would never see discovery_complete flip to true and
            // the next_winner() rotation would never start.
            self.evaluate_pools();
        }

        // Ingest WAF profile hints (only update, never downgrade).
        if let Some(waf_name) = matched_waf
            && self.waf_name.is_none()
        {
            self.waf_name = Some(waf_name.to_string());
            self.waf_confirmed = true;
        }

        // Merge prioritized techniques (union, preserving order).
        // Audit (2026-05-10): pre-fix this grew unboundedly. A long-
        // running scan picking up 100k unique technique names from
        // adversarial profiles would balloon HostState past safe
        // memory limits. Cap at MAX_HINTS_PER_LIST entries.
        for tech in prioritize {
            if self.prioritized_techniques.len() >= MAX_HINTS_PER_LIST {
                break;
            }
            if !self.prioritized_techniques.contains(tech) {
                self.prioritized_techniques.push(tech.clone());
            }
        }

        // Merge avoided techniques (same cap).
        for tech in avoid {
            if self.avoided_techniques.len() >= MAX_HINTS_PER_LIST {
                break;
            }
            if !self.avoided_techniques.contains(tech) {
                self.avoided_techniques.push(tech.clone());
            }
        }

        // Set inspection model hint.
        if let Some(model) = inspection_model
            && self.inspection_model.is_none()
        {
            self.inspection_model = Some(model.to_string());
        }
    }

    /// Check whether a technique should be skipped based on WAF profile hints.
    ///
    /// Returns `true` if the technique name appears in either:
    /// - The WAF profile's `avoid` list (known to waste requests)
    /// - The host's `blocklisted` techniques (proven failures)
    #[must_use]
    pub fn should_skip_technique(&self, technique_name: &str) -> bool {
        self.avoided_techniques.iter().any(|t| t == technique_name)
            || self.blocklisted.contains(&technique_name.to_string())
    }

    /// Get the ordered list of techniques to try, incorporating WAF
    /// profile priorities. Returns prioritized techniques first
    /// (from the matched profile), filtered to exclude blocklisted
    /// and avoided techniques.
    #[must_use]
    pub fn suggested_techniques(&self) -> Vec<String> {
        self.prioritized_techniques
            .iter()
            .filter(|t| !self.should_skip_technique(t))
            .cloned()
            .collect()
    }
}

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

    #[test]
    fn default_state_no_evasion() {
        let state = HostState::default();
        assert_eq!(state.escalation_level(), EscalationLevel::None);
    }

    #[test]
    fn light_after_two_blocks() {
        let mut state = HostState::default();
        state.record_block();
        state.record_block();
        assert_eq!(state.escalation_level(), EscalationLevel::Light);
    }

    #[test]
    fn medium_after_four_blocks() {
        let mut state = HostState::default();
        for _ in 0..4 {
            state.record_block();
        }
        assert_eq!(state.escalation_level(), EscalationLevel::Medium);
    }

    #[test]
    fn heavy_after_many_blocks() {
        let mut state = HostState::default();
        for _ in 0..10 {
            state.record_block();
        }
        assert_eq!(state.escalation_level(), EscalationLevel::Heavy);
    }

    #[test]
    fn record_success_tracks_technique() {
        let mut state = HostState::default();
        state.record_success(Technique::PayloadEncoding("CaseAlternation".into()));
        assert_eq!(state.successes, 1);
        assert!(state.last_success.is_some());
    }

    #[test]
    fn record_block_for_tracks_technique() {
        let mut state = HostState::default();
        state.record_block_for("CaseAlternation");
        state.record_block_for("CaseAlternation");
        assert_eq!(state.blocks, 2);
        assert_eq!(state.technique_stats[0].2, 2); // 2 attempts
    }

    #[test]
    fn record_block_for_many_one_http_block_multi_technique() {
        let mut state = HostState::default();
        state.record_block_for_many(&["a".to_string(), "b".to_string()]);
        assert_eq!(state.blocks, 1);
        assert_eq!(state.technique_stats.len(), 2);
        assert_eq!(
            state
                .technique_stats
                .iter()
                .find(|(n, _, _)| n == "a")
                .unwrap()
                .2,
            1
        );
        assert_eq!(
            state
                .technique_stats
                .iter()
                .find(|(n, _, _)| n == "b")
                .unwrap()
                .2,
            1
        );
    }

    #[test]
    fn record_success_for_many_compound() {
        let mut state = HostState::default();
        state.record_success_for_many(&[
            Technique::PayloadEncoding("A".into()),
            Technique::PayloadEncoding("B".into()),
        ]);
        assert_eq!(state.successes, 1);
        let sa = state
            .technique_stats
            .iter()
            .find(|(n, _, _)| n == "encoding:A")
            .unwrap();
        assert_eq!(sa.1, 1);
        assert_eq!(sa.2, 1);
    }

    #[test]
    fn best_technique_needs_two_attempts() {
        let mut state = HostState::default();
        state.record_success(Technique::PayloadEncoding("DoubleUrlEncode".into()));
        // One attempt — should not be returned
        assert!(state.best_technique().is_none());
    }

    #[test]
    fn needs_evasion_default() {
        let state = HostState::default();
        assert!(state.needs_evasion()); // Safe default
    }

    #[test]
    fn needs_evasion_after_success_no_blocks() {
        let state = HostState {
            successes: 5,
            ..Default::default()
        };
        assert!(!state.needs_evasion());
    }

    #[test]
    fn confirm_waf_sets_flag() {
        let mut state = HostState::default();
        state.confirm_waf(Some("Cloudflare".into()));
        assert!(state.waf_confirmed);
        assert_eq!(state.waf_name.as_deref(), Some("Cloudflare"));
        assert!(state.needs_evasion());
    }

    // ── Adaptive rotation tests ─────────────────────────────────────

    #[test]
    fn no_winners_before_discovery() {
        let state = HostState::default();
        assert!(!state.has_winners());
        assert!(state.proven_winners.is_empty());
    }

    #[test]
    fn evaluate_pools_promotes_winners() {
        let mut state = HostState {
            technique_stats: vec![
                ("GoodTech".into(), 9, 10), // 90% — should be winner
                ("OkTech".into(), 7, 10),   // 70% — should be winner
                ("BadTech".into(), 1, 10),  // 10% — should be blocklisted
                ("TooFew".into(), 2, 2),    // 100% but only 2 attempts — skip
            ],
            ..Default::default()
        };
        state.evaluate_pools();
        assert!(state.discovery_complete);
        assert!(state.proven_winners.contains(&"GoodTech".to_string()));
        assert!(state.proven_winners.contains(&"OkTech".to_string()));
        assert!(!state.proven_winners.contains(&"BadTech".to_string()));
        assert!(!state.proven_winners.contains(&"TooFew".to_string()));
        assert!(state.blocklisted.contains(&"BadTech".to_string()));
    }

    #[test]
    fn evaluate_pools_skips_insufficient_data() {
        // Only 5 total attempts — not enough to declare discovery.
        let mut state = HostState {
            technique_stats: vec![("T1".into(), 3, 5)],
            ..Default::default()
        };
        state.evaluate_pools();
        assert!(!state.discovery_complete);
        assert!(state.proven_winners.is_empty());
    }

    #[test]
    fn next_winner_round_robins() {
        let mut state = HostState {
            proven_winners: vec!["A".into(), "B".into(), "C".into()],
            discovery_complete: true,
            ..Default::default()
        };

        assert_eq!(state.next_winner().as_deref(), Some("A"));
        assert_eq!(state.next_winner().as_deref(), Some("B"));
        assert_eq!(state.next_winner().as_deref(), Some("C"));
        assert_eq!(state.next_winner().as_deref(), Some("A"));
    }

    #[test]
    fn next_winner_returns_none_when_empty() {
        let mut state = HostState::default();
        assert!(state.next_winner().is_none());
    }

    #[test]
    fn drift_detection_evicts_winner() {
        let mut state = HostState {
            proven_winners: vec!["WinTech".into(), "StillGood".into()],
            discovery_complete: true,
            ..Default::default()
        };

        // Two consecutive blocks on WinTech triggers eviction.
        state.record_block_for("WinTech");
        state.record_block_for("WinTech");

        assert!(!state.proven_winners.contains(&"WinTech".to_string()));
        assert!(state.blocklisted.contains(&"WinTech".to_string()));
        // StillGood survives.
        assert!(state.proven_winners.contains(&"StillGood".to_string()));
    }

    #[test]
    fn success_resets_drift_counter() {
        let mut state = HostState {
            proven_winners: vec!["encoding:Tech".into()],
            discovery_complete: true,
            ..Default::default()
        };

        // One block.
        state.record_block_for("encoding:Tech");
        // Then a success — should reset the drift counter.
        state.record_success(Technique::PayloadEncoding("Tech".into()));

        // Another block — should NOT evict because counter was reset.
        state.record_block_for("encoding:Tech");
        assert!(state.proven_winners.contains(&"encoding:Tech".to_string()));
    }

    #[test]
    fn all_winners_evicted_triggers_rediscovery() {
        let mut state = HostState {
            proven_winners: vec!["OnlyWinner".into()],
            discovery_complete: true,
            blocklisted: vec!["PrevBad".into()],
            technique_stats: vec![("OnlyWinner".into(), 5, 10)],
            ..Default::default()
        };

        // Evict the only winner.
        state.record_block_for("OnlyWinner");
        state.record_block_for("OnlyWinner");

        // Should re-enter discovery mode.
        assert!(!state.discovery_complete);
        assert!(state.proven_winners.is_empty());
        // Blocklist and stats are cleared for a clean re-discovery.
        assert!(state.blocklisted.is_empty());
        assert!(state.technique_stats.is_empty());
    }

    #[test]
    fn full_lifecycle_discover_rotate_drift_rediscover() {
        let mut state = HostState::default();

        // Phase 1: Discovery — simulate 15 technique observations.
        for _ in 0..5 {
            state.record_success(Technique::PayloadEncoding("Winner".into()));
        }
        for _ in 0..5 {
            state.record_block_for("Loser");
        }
        // Add some more to reach threshold.
        for _ in 0..5 {
            state.record_success(Technique::PayloadEncoding("AlsoGood".into()));
        }

        // Should have promoted winners.
        assert!(state.discovery_complete);
        assert!(state.has_winners());
        assert!(
            state
                .proven_winners
                .contains(&"encoding:Winner".to_string())
                || state
                    .proven_winners
                    .contains(&"encoding:AlsoGood".to_string())
        );

        // Phase 2: Rotation — get next winner.
        let w = state.next_winner();
        assert!(w.is_some());

        // Phase 3: Drift — block a winner twice.
        let winner_name = state.proven_winners[0].clone();
        state.record_block_for(&winner_name);
        state.record_block_for(&winner_name);

        // Winner should be evicted.
        assert!(!state.proven_winners.contains(&winner_name));
    }

    #[test]
    fn blocklisted_encoding_not_suggested() {
        let mut state = HostState::default();
        // Blocklist a known encoding strategy name.
        state.blocklisted.push("CaseAlternation".into());
        // next_encoding should skip it.
        if let Some(strategy) = state.next_encoding() {
            assert_ne!(format!("{strategy:?}"), "CaseAlternation");
        }
    }

    // ── Rich signal API tests ───────────────────────────────────────

    #[test]
    fn signal_rate_limit_does_not_penalize_technique() {
        let mut state = HostState::default();
        state.record_signal(
            false,                                     // not hard block
            false,                                     // not soft block
            true,                                      // IS rate limit
            false,                                     // not challenge
            Some("Cloudflare"),                        // matched WAF
            &["DoubleUrlEncode".to_string()],          // prioritize
            &["CaseAlternation".to_string()],          // avoid
            Some("single_pass_url_decode"),            // inspection model
            &["encoding:DoubleUrlEncode".to_string()], // technique keys
        );
        // Rate limit should NOT increase blocks.
        assert_eq!(state.blocks, 0);
        assert_eq!(state.rate_limits, 1);
        // But should still ingest WAF hints.
        assert_eq!(state.waf_name.as_deref(), Some("Cloudflare"));
        assert!(
            state
                .prioritized_techniques
                .contains(&"DoubleUrlEncode".to_string())
        );
    }

    #[test]
    fn signal_challenge_does_not_penalize_technique() {
        let mut state = HostState::default();
        state.record_signal(
            false,
            false,
            false,
            true, // challenge
            Some("Cloudflare"),
            &[],
            &[],
            None,
            &["encoding:UrlEncode".to_string()],
        );
        assert_eq!(state.blocks, 0);
        assert_eq!(state.challenges, 1);
    }

    #[test]
    fn signal_hard_block_records_block_with_technique() {
        let mut state = HostState::default();
        state.record_signal(
            true, // hard block
            false,
            false,
            false,
            Some("ModSecurity CRS"),
            &["CommentObfuscation".to_string()],
            &[],
            Some("multi_regex_scoring"),
            &["encoding:UrlEncode".to_string()],
        );
        assert_eq!(state.blocks, 1);
        assert_eq!(state.waf_name.as_deref(), Some("ModSecurity CRS"));
        assert_eq!(
            state.inspection_model.as_deref(),
            Some("multi_regex_scoring")
        );
        // Technique should have been attributed.
        assert!(
            state
                .technique_stats
                .iter()
                .any(|(n, _, a)| n == "encoding:UrlEncode" && *a == 1)
        );
    }

    #[test]
    fn signal_pass_records_success() {
        let mut state = HostState::default();
        state.record_signal(
            false,
            false,
            false,
            false, // pass
            None,
            &[],
            &[],
            None,
            &[],
        );
        assert_eq!(state.successes, 1);
        assert_eq!(state.blocks, 0);
    }

    #[test]
    fn signal_merges_prioritized_and_avoided() {
        let mut state = HostState::default();
        // First signal.
        state.record_signal(
            true,
            false,
            false,
            false,
            Some("TestWAF"),
            &["A".to_string(), "B".to_string()],
            &["X".to_string()],
            None,
            &[],
        );
        // Second signal with overlapping and new techniques.
        state.record_signal(
            true,
            false,
            false,
            false,
            None,
            &["B".to_string(), "C".to_string()],
            &["X".to_string(), "Y".to_string()],
            None,
            &[],
        );
        // Union — no duplicates.
        assert_eq!(state.prioritized_techniques, vec!["A", "B", "C"]);
        assert_eq!(state.avoided_techniques, vec!["X", "Y"]);
    }

    #[test]
    fn should_skip_technique_checks_both_lists() {
        let mut state = HostState::default();
        state.avoided_techniques.push("CaseAlternation".into());
        state.blocklisted.push("UrlEncode".into());
        assert!(state.should_skip_technique("CaseAlternation"));
        assert!(state.should_skip_technique("UrlEncode"));
        assert!(!state.should_skip_technique("DoubleUrlEncode"));
    }

    #[test]
    fn suggested_techniques_filters_skipped() {
        let state = HostState {
            prioritized_techniques: vec![
                "DoubleUrlEncode".into(),
                "CaseAlternation".into(),
                "UnicodeHomoglyph".into(),
            ],
            avoided_techniques: vec!["CaseAlternation".into()],
            ..HostState::default()
        };
        let suggested = state.suggested_techniques();
        assert_eq!(suggested, vec!["DoubleUrlEncode", "UnicodeHomoglyph"]);
    }

    #[test]
    fn waf_name_not_overwritten_by_subsequent_signals() {
        let mut state = HostState::default();
        state.record_signal(
            true,
            false,
            false,
            false,
            Some("Cloudflare"),
            &[],
            &[],
            None,
            &[],
        );
        state.record_signal(
            true,
            false,
            false,
            false,
            Some("ModSecurity"),
            &[],
            &[],
            None,
            &[],
        );
        // First detection wins — don't flip-flop.
        assert_eq!(state.waf_name.as_deref(), Some("Cloudflare"));
    }

    // ── Overflow guard tests ────────────────────────────────────────────

    #[test]
    fn bump_success_saturates_at_u32_max_not_wraps() {
        // Pre-fix: plain `+= 1` would overflow in debug (panic) or
        // silently wrap to 0 in release after 2^32 successes on the
        // same technique in a long-running proxy session.
        let mut state = HostState::default();
        // Inject a stat entry already at (u32::MAX - 1, u32::MAX - 1)
        // to force the boundary on the very next success.
        state
            .technique_stats
            .push(("encoding:Test".to_string(), u32::MAX - 1, u32::MAX - 1));
        // Record one more success — this used to plain-add, now saturates.
        state.bump_success_for_technique(&Technique::PayloadEncoding("Test".into()));
        let stat = state
            .technique_stats
            .iter()
            .find(|(n, _, _)| n == "encoding:Test")
            .expect("stat entry must exist");
        // Both successes (stat.1) and attempts (stat.2) must saturate, not wrap.
        assert_eq!(stat.1, u32::MAX, "successes must saturate at u32::MAX");
        assert_eq!(stat.2, u32::MAX, "attempts must saturate at u32::MAX");

        // One more: must stay at MAX, not wrap back to 0.
        state.bump_success_for_technique(&Technique::PayloadEncoding("Test".into()));
        let stat2 = state
            .technique_stats
            .iter()
            .find(|(n, _, _)| n == "encoding:Test")
            .expect("stat entry must exist");
        assert_eq!(
            stat2.1,
            u32::MAX,
            "successes must remain at u32::MAX after second saturating add"
        );
        assert_eq!(
            stat2.2,
            u32::MAX,
            "attempts must remain at u32::MAX after second saturating add"
        );
    }

    #[test]
    fn bump_success_and_block_both_use_saturating_arithmetic() {
        // Prove the two paths are symmetric: the block path already
        // had saturating_add; the success path now also has it.
        // Both must reach u32::MAX and stay there.
        let mut state = HostState::default();
        let name = "encoding:Sym".to_string();

        // Start at u32::MAX - 2 so we can hit the boundary in two ops.
        state
            .technique_stats
            .push((name.clone(), u32::MAX - 2, u32::MAX - 2));

        // Two successes take successes+attempts to MAX then stick.
        state.bump_success_for_technique(&Technique::PayloadEncoding("Sym".into()));
        state.bump_success_for_technique(&Technique::PayloadEncoding("Sym".into()));
        // One extra must not wrap.
        state.bump_success_for_technique(&Technique::PayloadEncoding("Sym".into()));

        let stat = state
            .technique_stats
            .iter()
            .find(|(n, _, _)| n == &name)
            .unwrap();
        assert_eq!(stat.1, u32::MAX);
        assert_eq!(stat.2, u32::MAX);
    }

    // ── F133: technique_stats cap on success path ───────────────────────

    #[test]
    fn bump_success_respects_max_technique_stats_cap() {
        // Before F133 the success path had no cap; the block path did.
        // Fill technique_stats to exactly MAX_TECHNIQUE_STATS entries via
        // the block path (which is already capped), then attempt to add a
        // brand-new unique technique via the success path.  The cap must
        // prevent the vector growing beyond MAX_TECHNIQUE_STATS.
        let mut state = HostState::default();

        // Fill to the cap using direct insertion (bypasses both code paths).
        for i in 0..MAX_TECHNIQUE_STATS {
            state.technique_stats.push((format!("dummy:{i}"), 0, 1));
        }
        assert_eq!(state.technique_stats.len(), MAX_TECHNIQUE_STATS);

        // Now attempt to add a new technique via the success path.
        // The name "encoding:NewTech" does NOT exist in technique_stats yet.
        state.bump_success_for_technique(&Technique::PayloadEncoding("NewTech".into()));

        // Vector must NOT have grown.
        assert_eq!(
            state.technique_stats.len(),
            MAX_TECHNIQUE_STATS,
            "technique_stats grew past MAX_TECHNIQUE_STATS on success path"
        );
        // And the new entry must NOT be present.
        assert!(
            !state
                .technique_stats
                .iter()
                .any(|(n, _, _)| n == "encoding:NewTech"),
            "new entry was inserted despite cap being reached"
        );
    }

    #[test]
    fn bump_success_updates_existing_entry_at_capacity() {
        // Even when at capacity, updating an EXISTING entry must still work.
        let mut state = HostState::default();

        // Insert the target entry first.
        state
            .technique_stats
            .push(("encoding:Existing".to_string(), 1, 2));

        // Fill the rest to the cap.
        for i in 0..(MAX_TECHNIQUE_STATS - 1) {
            state.technique_stats.push((format!("filler:{i}"), 0, 1));
        }
        assert_eq!(state.technique_stats.len(), MAX_TECHNIQUE_STATS);

        // Record a success for the already-present entry.
        state.bump_success_for_technique(&Technique::PayloadEncoding("Existing".into()));

        // Stats for the existing entry must have incremented.
        let stat = state
            .technique_stats
            .iter()
            .find(|(n, _, _)| n == "encoding:Existing")
            .expect("entry must exist");
        assert_eq!(stat.1, 2, "success count must increment");
        assert_eq!(stat.2, 3, "attempt count must increment");

        // Length unchanged.
        assert_eq!(state.technique_stats.len(), MAX_TECHNIQUE_STATS);
    }

    #[test]
    fn success_and_block_paths_symmetric_cap_enforcement() {
        // Both paths must refuse to insert beyond MAX_TECHNIQUE_STATS.
        // Fill to cap, then try one of each — neither may grow the vec.
        let mut state = HostState::default();

        for i in 0..MAX_TECHNIQUE_STATS {
            state.technique_stats.push((format!("pre:{i}"), 0, 1));
        }

        // Success path — new technique.
        state.bump_success_for_technique(&Technique::PayloadEncoding("SuccessNew".into()));
        // Block path — new technique name.
        state.bump_block_attempt_for_technique("BlockNew");

        assert_eq!(
            state.technique_stats.len(),
            MAX_TECHNIQUE_STATS,
            "neither path may grow technique_stats past the cap"
        );
    }

    #[test]
    fn record_success_for_many_capped_at_max_technique_stats() {
        // record_success_for_many calls bump_success_for_technique in a loop;
        // the cap must hold even when multiple unique techniques are passed.
        let mut state = HostState::default();

        for i in 0..MAX_TECHNIQUE_STATS {
            state.technique_stats.push((format!("existing:{i}"), 0, 1));
        }

        // Pass four brand-new unique techniques at once.
        state.record_success_for_many(&[
            Technique::PayloadEncoding("Bulk1".into()),
            Technique::PayloadEncoding("Bulk2".into()),
            Technique::PayloadEncoding("Bulk3".into()),
            Technique::PayloadEncoding("Bulk4".into()),
        ]);

        assert_eq!(
            state.technique_stats.len(),
            MAX_TECHNIQUE_STATS,
            "record_success_for_many must not bypass the per-technique cap"
        );
    }
}