keyhog-scanner 0.1.0

Aho-Corasick + regex compiled scanner for credential detection
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
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
//! Two-phase secret scanning engine.
//!
//! Phase 1 builds an Aho-Corasick automaton from literal prefixes extracted from
//! detector regex patterns and runs a single O(n) pass over the input. Phase 2
//! confirms candidate regions with the full regex. Patterns without extractable
//! prefixes fall back to sequential regex scanning.
//!
//! # Feature flags
//!
//! - `ml` — MoE ML classifier for confidence scoring (default: on)
//! - `entropy` — Shannon entropy-based detection (default: on)
//! - `decode` — Decode-through scanning: base64, hex, URL, HTML, MIME (default: on)
//! - `multiline` — Multi-line concatenation joining (default: on)
//! - `gpu` — GPU-accelerated batch ML inference (optional)
//!
//! Additional layers: base64/hex decode-through, ML confidence scoring,
//! structural context analysis, and multi-match resolution.

/// Confidence scoring helpers for combining heuristic signals.
pub mod confidence;
/// Structural code-context inference used to adjust confidence.
pub mod context;
/// Decode-through scanning helpers for layered encodings.
#[cfg(feature = "decode")]
pub mod decode;
/// Entropy-based fallback detection for unknown secret formats.
#[cfg(feature = "entropy")]
pub mod entropy;
#[cfg(feature = "gpu")]
pub mod gpu;
#[allow(clippy::excessive_precision)]
/// Embedded ML scorer used to downrank likely placeholders and noise.
#[cfg(feature = "ml")]
pub mod ml_scorer;
/// Multi-line preprocessing for string concatenation and line continuations.
#[cfg(feature = "multiline")]
pub mod multiline;
/// Prefix propagation tables for literal-prefix matching.
pub mod prefix_trie;
/// Match-resolution helpers for suppressing lower-quality overlaps.
pub mod resolution;
/// Vectorscan/Hyperscan SIMD regex backend (optional, feature-gated).
pub mod simd;

#[cfg(test)]
#[allow(clippy::manual_range_contains, clippy::useless_format)]
mod adversarial_tests;

use aho_corasick::AhoCorasick;
use keyhog_core::{Chunk, CompanionSpec, DetectorSpec, MatchLocation, PatternSpec, RawMatch};
use multimatch::{MatchError, PatternSet, PatternSetBuilder};
use regex::Regex;
use std::borrow::Cow;
use std::collections::{HashMap, VecDeque};
use thiserror::Error;
use unicode_normalization::UnicodeNormalization;

// Fallback regex-only scanning switches to per-line mode once a chunk grows
// beyond 10 KB. Prefixless regexes over larger blobs are expensive and secrets
// are short enough that line-local scanning preserves recall.
const LARGE_FALLBACK_SCAN_THRESHOLD: usize = 10_000;

/// Hard cap on the dedup set to prevent unbounded memory growth when scanning
/// repositories with millions of duplicate credential-like strings.
const MAX_WINDOW_DEDUP_ENTRIES: usize = 100_000;

/// Maximum bytes scanned in a single chunk. Files larger than this are split
/// into overlapping windows. 1 MiB keeps peak RSS predictable under parallel
/// scanning with `rayon` (N threads × 1 MiB per chunk = bounded memory).
const MAX_SCAN_CHUNK_BYTES: usize = 1024 * 1024;

/// Overlap between adjacent scan windows when a file exceeds
/// `MAX_SCAN_CHUNK_BYTES`. Must be larger than the longest secret the scanner
/// can detect to avoid missing secrets that straddle a chunk boundary. 4 KiB
/// covers PEM-encoded RSA-4096 keys (~3,200 chars base64) with margin.
const WINDOW_OVERLAP_BYTES: usize = 4096;

/// Minimum line length considered for fallback pattern scanning. Lines shorter
/// than 8 bytes cannot contain a credential prefix plus a meaningful secret.
const MIN_FALLBACK_LINE_LENGTH: usize = 8;

/// Minimum AC literal prefix length. Shorter prefixes (e.g., "1", "x", "_")
/// match too many positions and degrade Aho-Corasick throughput.
const FULL_MATCH_INDEX: usize = 0;
const FIRST_CAPTURE_GROUP_INDEX: usize = 1;
const FIRST_LINE_NUMBER: usize = 1;
const PREVIOUS_LINE_DISTANCE: usize = 1;
const MIN_LITERAL_PREFIX_CHARS: usize = 3;

/// Compiled regex AST size limit. 10 MiB is large enough for complex detectors
/// while preventing pathological patterns from consuming unbounded memory
/// during regex compilation.
const REGEX_SIZE_LIMIT_BYTES: usize = 10 << 20;

/// How many characters around a hex match to inspect for structural context
/// (assignment operators, quotes, keywords).
const HEX_CONTEXT_RADIUS_CHARS: usize = 20;

/// Minimum length for a standalone hex string to qualify as a potential secret.
/// Shorter hex runs (e.g., CSS colors like `#ff00ff`) are too common.
const MIN_HEX_MATCH_LEN: usize = 16;
const MIN_HEX_DIGITS_IN_MATCH: usize = 16;

/// Minimum hex digits required in the context window around a match to trigger
/// hex-aware false-positive suppression.
const MIN_HEX_CONTEXT_DIGITS: usize = 8;

/// Maximum non-hex separators (colons, dashes) tolerated within a hex context
/// window before the match is treated as a non-hex string.
const MAX_HEX_CONTEXT_SEPARATORS: usize = 4;

#[cfg(feature = "ml")]
const MAX_ML_CACHE_ENTRIES: usize = 1024;
#[cfg(feature = "ml")]
const MAX_ML_CACHE_BYTES: usize = 256 * 1024;
#[cfg(feature = "ml")]
const ML_CONTEXT_RADIUS_LINES: usize = 5;
#[cfg(feature = "ml")]
const ML_WEIGHT: f64 = 0.6;
#[cfg(feature = "ml")]
const HEURISTIC_WEIGHT: f64 = 0.4;

#[cfg(not(feature = "multiline"))]
#[derive(Debug, Clone)]
struct LineMapping {
    start_offset: usize,
    end_offset: usize,
    line_number: usize,
}

#[cfg(not(feature = "multiline"))]
#[derive(Debug, Clone)]
struct PreprocessedText {
    text: String,
    mappings: Vec<LineMapping>,
}

#[cfg(not(feature = "multiline"))]
impl PreprocessedText {
    fn line_for_offset(&self, offset: usize) -> Option<usize> {
        self.mappings
            .iter()
            .find(|mapping| offset >= mapping.start_offset && offset < mapping.end_offset)
            .map(|mapping| mapping.line_number)
    }

    fn passthrough(line: &str) -> Self {
        Self {
            text: line.to_string(),
            mappings: vec![LineMapping {
                line_number: 1,
                start_offset: 0,
                end_offset: line.len(),
            }],
        }
    }
}

#[cfg(feature = "multiline")]
type ScannerPreprocessedText = multiline::PreprocessedText;

#[cfg(not(feature = "multiline"))]
type ScannerPreprocessedText = PreprocessedText;

#[derive(Debug, Error)]
/// Errors returned while compiling detector patterns into a scanner.
///
/// # Examples
///
/// ```rust
/// use keyhog_scanner::ScanError;
///
/// let error = ScanError::RegexSetCompile(regex::Error::Syntax("bad regex".into()));
/// assert!(error.to_string().contains("Fix"));
/// ```
pub enum ScanError {
    #[error(
        "failed to compile regex for detector {detector_id} pattern {index}: {source}. Fix: correct the detector regex or capture group configuration"
    )]
    RegexCompile {
        detector_id: String,
        index: usize,
        source: regex::Error,
    },
    #[error(
        "failed to compile scanner regex set: {0}. Fix: simplify the detector regex set or remove the invalid pattern"
    )]
    RegexSetCompile(#[from] regex::Error),
    #[error(
        "failed to build multimatch automaton: {0}. Fix: reduce detector complexity or remove unsupported regex constructs"
    )]
    Multimatch(#[from] MatchError),
    #[error(
        "failed to build Aho-Corasick automaton: {0}. Fix: shorten overly broad prefixes or reduce detector count"
    )]
    AhoCorasick(#[from] aho_corasick::BuildError),
}

/// A compiled entry: one pattern from one detector.
struct CompiledPattern {
    detector_index: usize,
    regex: Regex,
    group: Option<usize>,
}

/// An optional compiled companion pattern for a detector.
struct CompiledCompanion {
    regex: Regex,
    capture_group: Option<usize>,
    within_lines: usize,
}

/// The compiled scanner: all detector patterns fused into a single
/// Aho-Corasick automaton for prefiltering, backed by individual
/// regexes for extraction.
///
/// # Examples
///
/// ```rust
/// use keyhog_core::{Chunk, ChunkMetadata, DetectorSpec, PatternSpec, Severity};
/// use keyhog_scanner::CompiledScanner;
///
/// let scanner = CompiledScanner::compile(vec![DetectorSpec {
///     id: "demo-token".into(),
///     name: "Demo Token".into(),
///     service: "demo".into(),
///     severity: Severity::High,
///     patterns: vec![PatternSpec {
///         regex: "demo_[A-Z0-9]{8}".into(),
///         description: None,
///         group: None,
///     }],
///     companion: None,
///     verify: None,
///     keywords: vec!["demo_".into()],
/// }])
/// .unwrap();
///
/// let chunk = Chunk {
///     data: "TOKEN=demo_ABC12345".into(),
///     metadata: ChunkMetadata {
///         source_type: "filesystem".into(),
///         path: Some(".env".into()),
///         commit: None,
///         author: None,
///         date: None,
///     },
/// };
///
/// assert_eq!(scanner.scan(&chunk).len(), 1);
/// ```
pub struct CompiledScanner {
    /// Pattern matcher built from literal prefixes of patterns.
    ac: Option<PatternSet>,
    /// Maps AC pattern index → compiled pattern entry.
    ac_map: Vec<CompiledPattern>,
    /// Batched first-pass regex confirmation for AC-backed patterns.
    /// The literal prefix strings corresponding to ac_map entries.
    /// Prefix propagation: for each AC pattern, list of OTHER ac_map indices
    /// whose prefix is a superstring. Pre-computed at compile time.
    /// When AC matches pattern i, also check all patterns in propagation[i].
    prefix_propagation: Vec<Vec<usize>>,
    /// Patterns without extractable literal prefixes — checked via regex only.
    /// Each entry pairs the compiled pattern with its detector's keywords for
    /// chunk-level prefiltering (skip pattern if no keywords found in chunk).
    fallback: Vec<(CompiledPattern, Vec<String>)>,
    /// Compiled companion patterns, indexed by detector index.
    companions: Vec<Option<CompiledCompanion>>,
    /// Original detector specs for metadata.
    detectors: Vec<DetectorSpec>,
    /// Pre-computed: detector_index → list of AC pattern indices for that detector.
    /// Eliminates O(N²) expansion during scan.
    detector_to_patterns: Vec<Vec<usize>>,
    /// Pre-computed: AC pattern index → list of other pattern indices with same literal prefix.
    /// Eliminates O(N²) prefix comparison during scan.
    same_prefix_patterns: Vec<Vec<usize>>,
    /// Aho-Corasick automaton for fallback pattern keywords.
    /// Single-pass keyword scan replaces per-pattern contains() loops.
    fallback_keyword_ac: Option<AhoCorasick>,
    /// Maps keyword AC match index → list of fallback pattern indices that use this keyword.
    fallback_keyword_to_patterns: Vec<Vec<usize>>,
    /// Optional Hyperscan SIMD scanner for 3-5x throughput.
    /// When available, replaces both AC prefilter and fallback scanning
    /// with a single SIMD pass over all patterns simultaneously.
    #[cfg(feature = "simd")]
    hs_scanner: Option<simd::backend::HsScanner>,
}

impl CompiledScanner {
    /// Compile all detector specs into a single scanner.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use keyhog_core::{DetectorSpec, PatternSpec, Severity};
    /// use keyhog_scanner::CompiledScanner;
    ///
    /// let scanner = CompiledScanner::compile(vec![DetectorSpec {
    ///     id: "demo-token".into(),
    ///     name: "Demo Token".into(),
    ///     service: "demo".into(),
    ///     severity: Severity::High,
    ///     patterns: vec![PatternSpec {
    ///         regex: "demo_[A-Z0-9]{8}".into(),
    ///         description: None,
    ///         group: None,
    ///     }],
    ///     companion: None,
    ///     verify: None,
    ///     keywords: vec!["demo_".into()],
    /// }])
    /// .unwrap();
    ///
    /// assert_eq!(scanner.detector_count(), 1);
    /// ```
    pub fn compile(detectors: Vec<DetectorSpec>) -> Result<Self, ScanError> {
        let CompileState {
            ac_literals,
            ac_map,
            fallback,
            companions,
            quality_warnings,
        } = build_compile_state(&detectors)?;
        log_quality_warnings(&quality_warnings);
        tracing::info!(
            ac_patterns = ac_map.len(),
            fallback_patterns = fallback.len(),
            detectors = detectors.len(),
            "scanner compiled"
        );

        let ac = build_ac_pattern_set(&ac_literals)?;
        let prefix_propagation = prefix_trie::build_propagation_table(&ac_literals);
        let detector_to_patterns = build_detector_to_patterns(&ac_map, detectors.len());
        let same_prefix_patterns = build_same_prefix_patterns(&ac_literals);

        // Build keyword AC for fallback pattern prefiltering
        let (fallback_keyword_ac, fallback_keyword_to_patterns) =
            build_fallback_keyword_ac(&fallback);

        // Build Hyperscan SIMD database when feature is enabled
        #[cfg(feature = "simd")]
        let hs_scanner = {
            // Collect ALL patterns (AC + fallback) for Hyperscan compilation
            let mut all_patterns: Vec<(usize, usize, &str, bool)> = Vec::new();
            for (i, entry) in ac_map.iter().enumerate() {
                all_patterns.push((
                    entry.detector_index,
                    i,
                    entry.regex.as_str(),
                    entry.group.is_some(),
                ));
            }
            for (i, (entry, _)) in fallback.iter().enumerate() {
                all_patterns.push((
                    entry.detector_index,
                    ac_map.len() + i,
                    entry.regex.as_str(),
                    entry.group.is_some(),
                ));
            }
            match simd::backend::HsScanner::compile(&all_patterns) {
                Ok((hs, unsupported)) => {
                    tracing::info!(
                        hs_patterns = hs.pattern_count(),
                        unsupported = unsupported.len(),
                        "hyperscan SIMD database compiled"
                    );
                    Some(hs)
                }
                Err(e) => {
                    tracing::warn!("hyperscan compilation failed, using AC fallback: {e}");
                    None
                }
            }
        };

        Ok(Self {
            ac,
            ac_map,
            prefix_propagation,
            fallback,
            companions,
            detectors,
            detector_to_patterns,
            same_prefix_patterns,
            fallback_keyword_ac,
            fallback_keyword_to_patterns,
            #[cfg(feature = "simd")]
            hs_scanner,
        })
    }

    /// Number of loaded detectors.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use keyhog_core::{DetectorSpec, PatternSpec, Severity};
    /// use keyhog_scanner::CompiledScanner;
    ///
    /// let scanner = CompiledScanner::compile(vec![DetectorSpec {
    ///     id: "demo-token".into(),
    ///     name: "Demo Token".into(),
    ///     service: "demo".into(),
    ///     severity: Severity::High,
    ///     patterns: vec![PatternSpec {
    ///         regex: "demo_[A-Z0-9]{8}".into(),
    ///         description: None,
    ///         group: None,
    ///     }],
    ///     companion: None,
    ///     verify: None,
    ///     keywords: vec!["demo_".into()],
    /// }])
    /// .unwrap();
    ///
    /// assert_eq!(scanner.detector_count(), 1);
    /// ```
    pub fn detector_count(&self) -> usize {
        self.detectors.len()
    }

    /// Total number of patterns (AC + fallback).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use keyhog_core::{DetectorSpec, PatternSpec, Severity};
    /// use keyhog_scanner::CompiledScanner;
    ///
    /// let scanner = CompiledScanner::compile(vec![DetectorSpec {
    ///     id: "demo-token".into(),
    ///     name: "Demo Token".into(),
    ///     service: "demo".into(),
    ///     severity: Severity::High,
    ///     patterns: vec![PatternSpec {
    ///         regex: "demo_[A-Z0-9]{8}".into(),
    ///         description: None,
    ///         group: None,
    ///     }],
    ///     companion: None,
    ///     verify: None,
    ///     keywords: vec!["demo_".into()],
    /// }])
    /// .unwrap();
    ///
    /// assert_eq!(scanner.pattern_count(), 1);
    /// ```
    pub fn pattern_count(&self) -> usize {
        self.ac_map.len() + self.fallback.len()
    }

    /// Maximum chunk size to scan (1MB). Larger chunks are split into overlapping windows.
    /// Maximum chunk size for windowed scanning.
    /// 1MB balances memory usage vs. split-boundary risk.
    /// Larger files are split with WINDOW_OVERLAP to avoid missing
    /// secrets at boundaries. Validated: 200KB adversarial test passes.
    pub(crate) const MAX_SCAN_CHUNK: usize = MAX_SCAN_CHUNK_BYTES;
    /// Overlap between windows to avoid missing secrets at boundaries.
    const WINDOW_OVERLAP: usize = WINDOW_OVERLAP_BYTES;

    /// Scan a chunk of text and return all raw credential matches.
    /// Applies multi-line preprocessing to detect secrets split across lines.
    /// Large chunks are split into overlapping windows for bounded scan time.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use keyhog_core::{Chunk, ChunkMetadata, DetectorSpec, PatternSpec, Severity};
    /// use keyhog_scanner::CompiledScanner;
    ///
    /// let scanner = CompiledScanner::compile(vec![DetectorSpec {
    ///     id: "demo-token".into(),
    ///     name: "Demo Token".into(),
    ///     service: "demo".into(),
    ///     severity: Severity::High,
    ///     patterns: vec![PatternSpec {
    ///         regex: "demo_[A-Z0-9]{8}".into(),
    ///         description: None,
    ///         group: None,
    ///     }],
    ///     companion: None,
    ///     verify: None,
    ///     keywords: vec!["demo_".into()],
    /// }])
    /// .unwrap();
    ///
    /// let matches = scanner.scan(&Chunk {
    ///     data: "TOKEN=demo_ABC12345".into(),
    ///     metadata: ChunkMetadata {
    ///         source_type: "filesystem".into(),
    ///         path: Some(".env".into()),
    ///         commit: None,
    ///         author: None,
    ///         date: None,
    ///     },
    /// });
    ///
    /// assert_eq!(matches.len(), 1);
    /// ```
    pub fn scan(&self, chunk: &Chunk) -> Vec<RawMatch> {
        // For large chunks, split into overlapping windows.
        let mut matches = if chunk.data.len() > Self::MAX_SCAN_CHUNK {
            self.scan_windowed(chunk)
        } else {
            self.scan_inner(chunk)
        };

        // Decode-through: scan base64/hex/URL decoded variants of the chunk.
        // Skip for large chunks — decode is O(N) per line and cascading scans
        // on decoded chunks can be expensive on multiline-heavy content.
        #[cfg(feature = "decode")]
        if chunk.data.len() <= 64 * 1024 {
            let mut seen: std::collections::HashSet<(String, String)> = matches
                .iter()
                .map(|m| (m.detector_id.clone(), m.credential.clone()))
                .collect();
            for decoded_chunk in decode::decode_chunk(chunk) {
                let decoded_matches = if decoded_chunk.data.len() > Self::MAX_SCAN_CHUNK {
                    self.scan_windowed(&decoded_chunk)
                } else {
                    self.scan_inner(&decoded_chunk)
                };
                for m in decoded_matches {
                    if seen.insert((m.detector_id.clone(), m.credential.clone())) {
                        matches.push(m);
                    }
                }
            }
        }

        matches
    }

    /// Split a large chunk into overlapping windows and scan each.
    ///
    /// # Window Layout
    ///
    /// ```text
    /// ├────── MAX_SCAN_CHUNK (1 MiB) ──────┤
    /// │ window 0                            │
    /// │                     ├─ OVERLAP (4K) ┤
    /// │                     │ window 1      │──── MAX_SCAN_CHUNK ────│
    /// │                     │               │                       │
    /// ```
    ///
    /// Windows advance by `MAX_SCAN_CHUNK - WINDOW_OVERLAP` bytes. The 4 KiB
    /// overlap ensures secrets up to ~3,200 chars (PEM RSA-4096 base64) that
    /// straddle a boundary are fully contained in at least one window.
    ///
    /// # Deduplication
    ///
    /// The `seen` set tracks `(credential, detector_id)` pairs across windows
    /// so that a secret in the overlap region is only reported once. The set is
    /// capped at [`MAX_WINDOW_DEDUP_ENTRIES`] and cleared on overflow to bound
    /// memory for pathological inputs with millions of matches.
    fn scan_windowed(&self, chunk: &Chunk) -> Vec<RawMatch> {
        let chunk_text = &chunk.data;
        let mut all_matches = Vec::with_capacity((chunk_text.len() / 4096).max(16));
        let mut seen = std::collections::HashSet::new();
        let mut seen_order = VecDeque::new();
        let mut offset = 0usize;

        while offset < chunk_text.len() {
            let end = window_end_offset(chunk_text, offset, Self::MAX_SCAN_CHUNK);
            let window_chunk = window_chunk(chunk, offset, end);
            for mut m in self.scan_inner(&window_chunk) {
                if record_window_match(chunk_text, offset, &mut m, &mut seen, &mut seen_order) {
                    all_matches.push(m);
                }
            }
            if end >= chunk_text.len() {
                break;
            }
            offset = next_window_offset(chunk_text, end, Self::WINDOW_OVERLAP);
        }

        all_matches
    }

    fn scan_inner(&self, chunk: &Chunk) -> Vec<RawMatch> {
        let mut owned_normalized = None;
        let chunk = if chunk.data.is_ascii() {
            chunk
        } else {
            normalize_scannable_chunk(chunk, &mut owned_normalized)
        };
        #[cfg(feature = "multiline")]
        let preprocessed = if crate::multiline::has_concatenation_indicators(&chunk.data) {
            multiline::preprocess_multiline(&chunk.data, &multiline::MultilineConfig::default())
        } else {
            ScannerPreprocessedText::passthrough(&chunk.data)
        };
        #[cfg(not(feature = "multiline"))]
        let preprocessed = ScannerPreprocessedText::passthrough(&chunk.data);

        let line_offsets = compute_line_offsets(&preprocessed.text);
        let code_lines: Vec<&str> = chunk.data.lines().collect();
        let documentation_lines = context::documentation_line_flags(&code_lines);
        let mut scan_state = ScanState {
            matches: Vec::with_capacity((chunk.data.len() / 4096).max(16)),
            ..Default::default()
        };

        // SIMD fast path: Hyperscan matches ALL patterns in a single SIMD pass.
        // NOTE: Hyperscan SIMD prefilter is compiled and ready (1634 patterns)
        // but disabled because it's 3x SLOWER than AC+keyword for our use case.
        // Reason: HS matches all patterns simultaneously (good for IDS) but we still
        // need Rust regex for capture group extraction. The double work (HS scan +
        // regex confirmation) is more expensive than AC prefilter + selective regex.
        // HS would help if we had 10K+ patterns where AC automaton size is the bottleneck.
        #[cfg(feature = "simd")]
        let used_simd = if let Some(hs) = &self.hs_scanner {
            let hs_matches = hs.scan(preprocessed.text.as_bytes());
            // Collect unique pattern indices that HS triggered
            let mut triggered_set = std::collections::HashSet::new();
            for &(hs_id, _start, _end) in &hs_matches {
                if let Some((det_idx, pat_idx, _has_group)) = hs.pattern_info(hs_id) {
                    triggered_set.insert((det_idx, pat_idx));
                }
            }
            // Run the Rust regex for each triggered pattern to extract matches
            let all_patterns: Vec<&CompiledPattern> = self
                .ac_map
                .iter()
                .chain(self.fallback.iter().map(|(p, _)| p))
                .collect();
            for &(_det_idx, pat_idx) in &triggered_set {
                if let Some(entry) = all_patterns.get(pat_idx) {
                    self.extract_matches(
                        entry,
                        &preprocessed,
                        &line_offsets,
                        &code_lines,
                        &documentation_lines,
                        chunk,
                        &mut scan_state.matches,
                        &mut scan_state.ml_score_cache,
                        &mut scan_state.ml_cache_order,
                        &mut scan_state.ml_cache_bytes,
                    );
                }
            }
            true
        } else {
            false
        };
        #[cfg(not(feature = "simd"))]
        let used_simd = false;

        if !used_simd {
            // Standard path: AC prefilter + fallback keyword scanning
            let expanded_patterns = self.collect_expanded_patterns(&preprocessed.text);
            let triggered: Vec<usize> = (0..self.ac_map.len())
                .filter(|&i| (expanded_patterns[i / 64] & (1 << (i % 64))) != 0)
                .collect();
            self.scan_prefiltered_patterns(
                &triggered,
                &preprocessed,
                &line_offsets,
                &code_lines,
                &documentation_lines,
                chunk,
                &mut scan_state.matches,
                &mut scan_state.ml_score_cache,
                &mut scan_state.ml_cache_order,
                &mut scan_state.ml_cache_bytes,
            );
        }
        if !used_simd {
            self.scan_fallback_patterns(
                &preprocessed,
                &line_offsets,
                &code_lines,
                &documentation_lines,
                chunk,
                &mut scan_state.matches,
                &mut scan_state.ml_score_cache,
                &mut scan_state.ml_cache_order,
                &mut scan_state.ml_cache_bytes,
            );
        }
        scan_state.matches
    }

    /// Dispatch regex execution for a single compiled pattern against the
    /// preprocessed text. Routes to either grouped extraction (when the
    /// pattern has a capture group for the credential value) or plain
    /// extraction (full-match mode).
    ///
    /// Matched credentials are appended to `matches` after confidence scoring
    /// and false-positive filtering. The ML score cache is shared across
    /// patterns to avoid redundant inference for the same credential string.
    #[allow(clippy::too_many_arguments)]
    fn extract_matches(
        &self,
        entry: &CompiledPattern,
        preprocessed: &ScannerPreprocessedText,
        line_offsets: &[usize],
        code_lines: &[&str],
        documentation_lines: &[bool],
        chunk: &Chunk,
        matches: &mut Vec<RawMatch>,
        ml_score_cache: &mut HashMap<(String, String), f64>,
        ml_cache_order: &mut VecDeque<(String, String)>,
        ml_cache_bytes: &mut usize,
    ) {
        let detector = &self.detectors[entry.detector_index];
        if let Some(group) = entry.group {
            self.extract_grouped_matches(
                entry,
                detector,
                group,
                preprocessed,
                line_offsets,
                code_lines,
                documentation_lines,
                chunk,
                matches,
                ml_score_cache,
                ml_cache_order,
                ml_cache_bytes,
            );
            return;
        }
        self.extract_plain_matches(
            entry,
            detector,
            preprocessed,
            line_offsets,
            code_lines,
            documentation_lines,
            chunk,
            matches,
            ml_score_cache,
            ml_cache_order,
            ml_cache_bytes,
        );
    }

    /// Process a single regex match and push a `RawMatch` if it passes filters.
    #[allow(clippy::too_many_arguments)]
    fn process_match(
        &self,
        entry: &CompiledPattern,
        detector: &DetectorSpec,
        data: &str,
        preprocessed: &ScannerPreprocessedText,
        line_offsets: &[usize],
        code_lines: &[&str],
        documentation_lines: &[bool],
        chunk: &Chunk,
        matches: &mut Vec<RawMatch>,
        ml_score_cache: &mut HashMap<(String, String), f64>,
        ml_cache_order: &mut VecDeque<(String, String)>,
        ml_cache_bytes: &mut usize,
        credential: &str,
        match_start: usize,
        match_end: usize,
    ) {
        if is_within_hex_context(data, match_start, match_end) {
            return;
        }
        let line = match_line_number(preprocessed, line_offsets, match_start);
        if context::is_false_positive_context(
            code_lines,
            line.saturating_sub(PREVIOUS_LINE_DISTANCE),
            chunk.metadata.path.as_deref(),
        ) || context::is_false_positive_match_context(
            data,
            match_start,
            chunk.metadata.path.as_deref(),
        ) {
            return;
        }
        let inferred_context = context::infer_context_with_documentation(
            code_lines,
            line.saturating_sub(PREVIOUS_LINE_DISTANCE),
            chunk.metadata.path.as_deref(),
            documentation_lines,
        );
        if should_suppress_known_example_credential(
            credential,
            chunk.metadata.path.as_deref(),
            inferred_context,
        ) {
            return;
        }
        let companion = self.match_companion(entry, preprocessed, line);
        let ent = match_entropy(credential.as_bytes());
        let conf = self.match_confidence(
            entry,
            detector,
            code_lines,
            documentation_lines,
            chunk,
            credential,
            data,
            line,
            ent,
            companion.is_some(),
            ml_score_cache,
            ml_cache_order,
            ml_cache_bytes,
        );
        matches.push(build_raw_match(
            detector,
            chunk,
            credential,
            companion,
            match_start,
            line,
            ent,
            conf,
        ));
    }

    fn collect_expanded_patterns(&self, text: &str) -> Vec<u64> {
        let triggered_patterns = self.collect_triggered_patterns(text);
        self.expand_triggered_patterns(&triggered_patterns)
    }

    fn collect_triggered_patterns(&self, text: &str) -> Vec<u64> {
        let mut triggered_patterns = vec![0u64; self.ac_map.len().div_ceil(64)];
        if let Some(ac) = &self.ac {
            for ac_match in ac.scan(text.as_bytes()) {
                let pat_idx = ac_match.pattern_id;
                if pat_idx >= self.ac_map.len() {
                    continue;
                }
                // SAFETY: pat_idx is bounded by ac_map.len() which is checked at compile time.
                // pat_idx % 64 is always 0..63, so the shift never overflows.
                triggered_patterns[pat_idx / 64] |= 1u64 << (pat_idx % 64);
                for &propagated_idx in &self.prefix_propagation[pat_idx] {
                    triggered_patterns[propagated_idx / 64] |= 1 << (propagated_idx % 64);
                }
            }
        }
        triggered_patterns
    }

    fn expand_triggered_patterns(&self, triggered_patterns: &[u64]) -> Vec<u64> {
        let mut expanded = triggered_patterns.to_vec();
        for pat_idx in 0..self.ac_map.len() {
            if (triggered_patterns[pat_idx / 64] & (1 << (pat_idx % 64))) != 0 {
                for &other_idx in &self.same_prefix_patterns[pat_idx] {
                    expanded[other_idx / 64] |= 1 << (other_idx % 64);
                }
                let det_idx = self.ac_map[pat_idx].detector_index;
                for &other_idx in &self.detector_to_patterns[det_idx] {
                    expanded[other_idx / 64] |= 1 << (other_idx % 64);
                }
            }
        }
        expanded
    }

    #[allow(clippy::too_many_arguments)]
    fn scan_prefiltered_patterns(
        &self,
        confirmed_patterns: &[usize],
        preprocessed: &ScannerPreprocessedText,
        line_offsets: &[usize],
        code_lines: &[&str],
        documentation_lines: &[bool],
        chunk: &Chunk,
        matches: &mut Vec<RawMatch>,
        ml_score_cache: &mut HashMap<(String, String), f64>,
        ml_cache_order: &mut VecDeque<(String, String)>,
        ml_cache_bytes: &mut usize,
    ) {
        for &pat_idx in confirmed_patterns {
            let entry = &self.ac_map[pat_idx];
            self.extract_matches(
                entry,
                preprocessed,
                line_offsets,
                code_lines,
                documentation_lines,
                chunk,
                matches,
                ml_score_cache,
                ml_cache_order,
                ml_cache_bytes,
            );
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn scan_fallback_patterns(
        &self,
        preprocessed: &ScannerPreprocessedText,
        line_offsets: &[usize],
        code_lines: &[&str],
        documentation_lines: &[bool],
        chunk: &Chunk,
        matches: &mut Vec<RawMatch>,
        ml_score_cache: &mut HashMap<(String, String), f64>,
        ml_cache_order: &mut VecDeque<(String, String)>,
        ml_cache_bytes: &mut usize,
    ) {
        if preprocessed.text.len() > LARGE_FALLBACK_SCAN_THRESHOLD && !self.fallback.is_empty() {
            self.scan_large_fallback_patterns(
                preprocessed,
                line_offsets,
                chunk,
                matches,
                ml_score_cache,
                ml_cache_order,
                ml_cache_bytes,
            );
            return;
        }
        // Single-pass keyword scan: find which fallback patterns are relevant for this chunk.
        let active_patterns: Vec<bool> = if let Some(kw_ac) = &self.fallback_keyword_ac {
            let mut active = vec![false; self.fallback.len()];
            // Mark patterns whose keywords have NO usable keywords as always-active
            for (i, (_pattern, keywords)) in self.fallback.iter().enumerate() {
                if !keywords.iter().any(|kw| kw.len() >= 4) {
                    active[i] = true;
                }
            }
            // Single AC scan over chunk to find all keyword matches
            for mat in kw_ac.find_iter(&chunk.data) {
                let kw_idx = mat.pattern().as_usize();
                if kw_idx < self.fallback_keyword_to_patterns.len() {
                    for &pattern_idx in &self.fallback_keyword_to_patterns[kw_idx] {
                        if pattern_idx < active.len() {
                            active[pattern_idx] = true;
                        }
                    }
                }
            }
            active
        } else {
            vec![true; self.fallback.len()]
        };

        for (i, (entry, _keywords)) in self.fallback.iter().enumerate() {
            if !active_patterns[i] {
                continue;
            }
            self.extract_matches(
                entry,
                preprocessed,
                line_offsets,
                code_lines,
                documentation_lines,
                chunk,
                matches,
                ml_score_cache,
                ml_cache_order,
                ml_cache_bytes,
            );
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn scan_large_fallback_patterns(
        &self,
        preprocessed: &ScannerPreprocessedText,
        line_offsets: &[usize],
        chunk: &Chunk,
        matches: &mut Vec<RawMatch>,
        ml_score_cache: &mut HashMap<(String, String), f64>,
        ml_cache_order: &mut VecDeque<(String, String)>,
        ml_cache_bytes: &mut usize,
    ) {
        // Use keyword AC for fast pre-filtering (same as scan_fallback_patterns)
        let active_set: Vec<bool> = if let Some(kw_ac) = &self.fallback_keyword_ac {
            let mut active = vec![false; self.fallback.len()];
            for (i, (_, keywords)) in self.fallback.iter().enumerate() {
                if !keywords.iter().any(|kw| kw.len() >= 4) {
                    active[i] = true;
                }
            }
            for mat in kw_ac.find_iter(&chunk.data) {
                let kw_idx = mat.pattern().as_usize();
                if kw_idx < self.fallback_keyword_to_patterns.len() {
                    for &pattern_idx in &self.fallback_keyword_to_patterns[kw_idx] {
                        if pattern_idx < active.len() {
                            active[pattern_idx] = true;
                        }
                    }
                }
            }
            active
        } else {
            vec![true; self.fallback.len()]
        };
        let active_fallback: Vec<&CompiledPattern> = self
            .fallback
            .iter()
            .enumerate()
            .filter(|(i, _)| active_set[*i])
            .map(|(_, (entry, _))| entry)
            .collect();

        if active_fallback.is_empty() {
            return;
        }

        for (line_idx, line) in preprocessed.text.lines().enumerate() {
            if line.len() < MIN_FALLBACK_LINE_LENGTH {
                continue;
            }
            let start_len = matches.len();
            let line_pre = ScannerPreprocessedText::passthrough(line);
            let line_code_lines = [line];
            let line_documentation_lines = [false];
            for entry in &active_fallback {
                self.extract_matches(
                    entry,
                    &line_pre,
                    &[0],
                    &line_code_lines,
                    &line_documentation_lines,
                    chunk,
                    matches,
                    ml_score_cache,
                    ml_cache_order,
                    ml_cache_bytes,
                );
            }
            adjust_fallback_match_locations(
                &mut matches[start_len..],
                line_idx,
                line_offsets[line_idx],
            );
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn extract_grouped_matches(
        &self,
        entry: &CompiledPattern,
        detector: &DetectorSpec,
        group: usize,
        preprocessed: &ScannerPreprocessedText,
        line_offsets: &[usize],
        code_lines: &[&str],
        documentation_lines: &[bool],
        chunk: &Chunk,
        matches: &mut Vec<RawMatch>,
        ml_score_cache: &mut HashMap<(String, String), f64>,
        ml_cache_order: &mut VecDeque<(String, String)>,
        ml_cache_bytes: &mut usize,
    ) {
        // The preprocessed text contains original text + appended multiline joins.
        // Single-pass search covers both structural and multiline-joined patterns.
        let search_text = &preprocessed.text;
        for caps in entry.regex.captures_iter(search_text) {
            let Some(full_match) = caps.get(FULL_MATCH_INDEX) else {
                continue;
            };
            let credential = caps
                .get(group)
                .map(|capture| capture.as_str())
                .unwrap_or_else(|| full_match.as_str());
            self.process_match(
                entry,
                detector,
                search_text,
                preprocessed,
                line_offsets,
                code_lines,
                documentation_lines,
                chunk,
                matches,
                ml_score_cache,
                ml_cache_order,
                ml_cache_bytes,
                credential,
                full_match.start(),
                full_match.end(),
            );
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn extract_plain_matches(
        &self,
        entry: &CompiledPattern,
        detector: &DetectorSpec,
        preprocessed: &ScannerPreprocessedText,
        line_offsets: &[usize],
        code_lines: &[&str],
        documentation_lines: &[bool],
        chunk: &Chunk,
        matches: &mut Vec<RawMatch>,
        ml_score_cache: &mut HashMap<(String, String), f64>,
        ml_cache_order: &mut VecDeque<(String, String)>,
        ml_cache_bytes: &mut usize,
    ) {
        let search_text = &preprocessed.text;
        for matched in entry.regex.find_iter(search_text) {
            self.process_match(
                entry,
                detector,
                search_text,
                preprocessed,
                line_offsets,
                code_lines,
                documentation_lines,
                chunk,
                matches,
                ml_score_cache,
                ml_cache_order,
                ml_cache_bytes,
                matched.as_str(),
                matched.start(),
                matched.end(),
            );
        }
    }

    fn match_companion(
        &self,
        entry: &CompiledPattern,
        preprocessed: &ScannerPreprocessedText,
        line: usize,
    ) -> Option<String> {
        self.companions
            .get(entry.detector_index)
            .and_then(|companion| companion.as_ref())
            .and_then(|companion| find_companion(preprocessed, line, companion))
    }

    /// Compute the confidence score for a credential match.
    ///
    /// # Scoring Pipeline
    ///
    /// 1. **Heuristic signals** (`confidence::compute_confidence`): combines
    ///    literal prefix presence, capture-group anchoring, Shannon entropy,
    ///    keyword proximity, sensitive file paths, match length, and companion
    ///    secret presence into a raw score in `[0.0, 1.0]`.
    ///
    /// 2. **Context adjustment**: the surrounding code context (test files,
    ///    documentation, comments, example blocks) applies a multiplier that
    ///    reduces confidence for matches in non-production contexts.
    ///
    /// 3. **ML blending** (when `feature = "ml"` is enabled): a 41-feature
    ///    mixture-of-experts classifier produces an independent confidence
    ///    score. The final output is `max(blended, heuristic, ml)` — we take
    ///    the maximum so that a strong heuristic signal is never dragged down
    ///    by a weak ML prediction, and vice versa.
    ///
    /// When ML is disabled, returns the heuristic confidence directly.
    #[allow(clippy::too_many_arguments)]
    fn match_confidence(
        &self,
        entry: &CompiledPattern,
        detector: &DetectorSpec,
        code_lines: &[&str],
        documentation_lines: &[bool],
        chunk: &Chunk,
        credential: &str,
        data: &str,
        line: usize,
        ent: f64,
        has_companion: bool,
        ml_score_cache: &mut HashMap<(String, String), f64>,
        ml_cache_order: &mut VecDeque<(String, String)>,
        ml_cache_bytes: &mut usize,
    ) -> f64 {
        let raw_conf = confidence::compute_confidence(&confidence::ConfidenceSignals {
            has_literal_prefix: extract_literal_prefix(entry.regex.as_str()).is_some(),
            has_context_anchor: entry.group.is_some(),
            entropy: ent,
            keyword_nearby: detector
                .keywords
                .iter()
                .any(|keyword| chunk.data.contains(keyword.as_str())),
            sensitive_file: chunk
                .metadata
                .path
                .as_deref()
                .map(confidence::is_sensitive_path)
                .unwrap_or(false),
            match_length: credential.len(),
            has_companion,
        });
        let context = context::infer_context_with_documentation(
            code_lines,
            line.saturating_sub(PREVIOUS_LINE_DISTANCE),
            chunk.metadata.path.as_deref(),
            documentation_lines,
        );
        let heuristic_conf = raw_conf * context.confidence_multiplier();
        #[cfg(not(feature = "ml"))]
        {
            let _ = (data, ml_score_cache, ml_cache_order, ml_cache_bytes);
            return heuristic_conf;
        }

        #[cfg(feature = "ml")]
        {
            let text_context = local_context_window(data, line, ML_CONTEXT_RADIUS_LINES);
            // Prepend file path so the MoE gate can route to the right expert
            // based on file extension (.env → config, .yml → CI, .tf → infra, etc.)
            let ml_context = match chunk.metadata.path.as_deref() {
                Some(path) => format!("file:{path}\n{text_context}"),
                None => text_context,
            };
            let ml_conf = cached_ml_score(
                ml_score_cache,
                ml_cache_order,
                ml_cache_bytes,
                credential,
                &ml_context,
            );
            // Use the HIGHER of ML and heuristic scores. A strong heuristic match
            // (prefix + entropy + context) should never be dragged down by a weak ML
            // prediction, and a strong ML prediction should override weak heuristics.
            let blended = (ML_WEIGHT * ml_conf) + (HEURISTIC_WEIGHT * heuristic_conf);
            blended.max(heuristic_conf).max(ml_conf)
        }
    }
}

#[derive(Default)]
struct ScanState {
    matches: Vec<RawMatch>,
    ml_score_cache: HashMap<(String, String), f64>,
    ml_cache_order: VecDeque<(String, String)>,
    ml_cache_bytes: usize,
}

struct CompileState {
    ac_literals: Vec<String>,
    ac_map: Vec<CompiledPattern>,
    fallback: Vec<(CompiledPattern, Vec<String>)>,
    companions: Vec<Option<CompiledCompanion>>,
    quality_warnings: Vec<String>,
}

fn build_compile_state(detectors: &[DetectorSpec]) -> Result<CompileState, ScanError> {
    let mut ac_literals = Vec::new();
    let mut ac_map = Vec::new();
    let mut fallback = Vec::new();
    let mut companions = Vec::with_capacity(detectors.len());
    let mut quality_warnings = Vec::new();
    for (detector_index, detector) in detectors.iter().enumerate() {
        companions.push(compile_detector_companion(detector)?);
        for (pattern_index, pattern) in detector.patterns.iter().enumerate() {
            compile_detector_pattern(
                detector_index,
                detector,
                pattern_index,
                pattern,
                &mut ac_literals,
                &mut ac_map,
                &mut fallback,
                &mut quality_warnings,
            )?;
        }
    }
    Ok(CompileState {
        ac_literals,
        ac_map,
        fallback,
        companions,
        quality_warnings,
    })
}

fn compile_detector_companion(
    detector: &DetectorSpec,
) -> Result<Option<CompiledCompanion>, ScanError> {
    detector
        .companion
        .as_ref()
        .map(|companion| compile_companion(companion, &detector.id))
        .transpose()
}

#[allow(clippy::too_many_arguments)]
fn compile_detector_pattern(
    detector_index: usize,
    detector: &DetectorSpec,
    pattern_index: usize,
    pattern: &PatternSpec,
    ac_literals: &mut Vec<String>,
    ac_map: &mut Vec<CompiledPattern>,
    fallback: &mut Vec<(CompiledPattern, Vec<String>)>,
    quality_warnings: &mut Vec<String>,
) -> Result<(), ScanError> {
    let prefix = extract_literal_prefix(&pattern.regex);
    if prefix.is_none() && detector.keywords.is_empty() {
        quality_warnings.push(format!(
            "detector '{}' pattern {} has no literal prefix and no keywords — will produce false positives. Add keywords for context anchoring.",
            detector.id, pattern_index
        ));
    }
    let compiled = compile_pattern(detector_index, pattern_index, pattern, &detector.id)?;
    match prefix {
        Some(prefix) => {
            ac_literals.push(prefix);
            ac_map.push(compiled);
        }
        _ => fallback.push((compiled, detector.keywords.clone())),
    }
    Ok(())
}

/// Build an Aho-Corasick automaton over all unique fallback keywords (≥4 chars).
/// Returns the AC and a mapping from keyword-match-index → fallback-pattern-indices.
fn build_fallback_keyword_ac(
    fallback: &[(CompiledPattern, Vec<String>)],
) -> (Option<AhoCorasick>, Vec<Vec<usize>>) {
    // Collect unique keywords → pattern indices
    let mut keyword_map: std::collections::HashMap<String, Vec<usize>> =
        std::collections::HashMap::new();
    for (pattern_idx, (_pattern, keywords)) in fallback.iter().enumerate() {
        for kw in keywords {
            if kw.len() >= 4 {
                keyword_map
                    .entry(kw.to_ascii_lowercase())
                    .or_default()
                    .push(pattern_idx);
            }
        }
    }
    if keyword_map.is_empty() {
        return (None, Vec::new());
    }
    let keywords: Vec<String> = keyword_map.keys().cloned().collect();
    let mapping: Vec<Vec<usize>> = keywords.iter().map(|kw| keyword_map[kw].clone()).collect();
    let ac = AhoCorasick::builder()
        .ascii_case_insensitive(true)
        .build(&keywords)
        .ok();
    (ac, mapping)
}

fn log_quality_warnings(warnings: &[String]) {
    for warning in warnings {
        tracing::warn!("{}", warning);
    }
}

fn build_ac_pattern_set(ac_literals: &[String]) -> Result<Option<PatternSet>, ScanError> {
    if ac_literals.is_empty() {
        return Ok(None);
    }

    let mut builder = PatternSetBuilder::new();
    for (index, literal) in ac_literals.iter().enumerate() {
        builder = builder.add_literal(literal, index);
    }

    Ok(Some(builder.build()?))
}

fn build_detector_to_patterns(
    ac_map: &[CompiledPattern],
    detector_count: usize,
) -> Vec<Vec<usize>> {
    let mut detector_to_patterns = vec![Vec::new(); detector_count];
    for (pattern_index, entry) in ac_map.iter().enumerate() {
        detector_to_patterns[entry.detector_index].push(pattern_index);
    }
    detector_to_patterns
}

fn build_same_prefix_patterns(ac_literals: &[String]) -> Vec<Vec<usize>> {
    let mut prefix_groups: HashMap<&str, Vec<usize>> = HashMap::new();
    for (index, literal) in ac_literals.iter().enumerate() {
        prefix_groups
            .entry(literal.as_str())
            .or_default()
            .push(index);
    }
    let mut same_prefix_patterns = vec![Vec::new(); ac_literals.len()];
    for indices in prefix_groups.values() {
        for &index in indices {
            same_prefix_patterns[index] = indices
                .iter()
                .copied()
                .filter(|other| *other != index)
                .collect();
        }
    }
    same_prefix_patterns
}

fn normalize_scannable_chunk<'a>(
    chunk: &'a Chunk,
    owned_normalized: &'a mut Option<Chunk>,
) -> &'a Chunk {
    if chunk.data.is_ascii() {
        return chunk;
    }

    match normalize_chunk_data(&chunk.data) {
        Cow::Borrowed(_) => chunk,
        Cow::Owned(normalized_chunk_text) => {
            *owned_normalized = Some(keyhog_core::Chunk {
                data: normalized_chunk_text,
                metadata: chunk.metadata.clone(),
            });
            // SAFETY: `owned_normalized` was set to `Some(...)` two lines
            // above, so `.as_ref()` is infallible here.
            match owned_normalized.as_ref() {
                Some(chunk) => chunk,
                None => chunk,
            }
        }
    }
}

fn window_end_offset(text: &str, offset: usize, window_size: usize) -> usize {
    let mut end = (offset + window_size).min(text.len());
    while end < text.len() && !text.is_char_boundary(end) {
        end += 1; // Advance by 1 byte to find char boundary
    }
    end
}

fn window_chunk(chunk: &Chunk, offset: usize, end: usize) -> Chunk {
    Chunk {
        data: chunk.data[offset..end].to_string(),
        metadata: chunk.metadata.clone(),
    }
}

fn record_window_match(
    chunk_text: &str,
    offset: usize,
    matched: &mut RawMatch,
    seen: &mut std::collections::HashSet<(String, String, usize)>,
    seen_order: &mut VecDeque<(String, String, usize)>,
) -> bool {
    matched.location.offset += offset;
    matched.location.line = Some(line_number_for_offset(chunk_text, matched.location.offset));
    let key = (
        matched.detector_id.clone(),
        matched.credential.clone(),
        matched.location.offset,
    );
    if !seen.insert(key.clone()) {
        return false;
    }

    seen_order.push_back(key);
    while seen.len() > MAX_WINDOW_DEDUP_ENTRIES {
        let Some(oldest) = seen_order.pop_front() else {
            break;
        };
        seen.remove(&oldest);
    }

    true
}

fn next_window_offset(text: &str, end: usize, overlap: usize) -> usize {
    let mut offset = end.saturating_sub(overlap);
    while offset > 0 && !text.is_char_boundary(offset) {
        offset -= 1; // Step back by 1 byte to find char boundary
    }
    offset
}

fn adjust_fallback_match_locations(matches: &mut [RawMatch], line_idx: usize, line_offset: usize) {
    for matched in matches {
        if matched.location.line == Some(FIRST_LINE_NUMBER) {
            matched.location.line = Some(line_idx + FIRST_LINE_NUMBER);
        }
        matched.location.offset += line_offset;
    }
}

fn match_line_number(
    preprocessed: &ScannerPreprocessedText,
    line_offsets: &[usize],
    match_start: usize,
) -> usize {
    preprocessed
        .line_for_offset(match_start)
        .unwrap_or_else(|| line_number_for_offset_with_offsets(line_offsets, match_start))
}

#[allow(clippy::too_many_arguments)]
fn build_raw_match(
    detector: &DetectorSpec,
    chunk: &Chunk,
    credential: &str,
    companion: Option<String>,
    match_start: usize,
    line: usize,
    entropy: f64,
    confidence: f64,
) -> RawMatch {
    RawMatch {
        detector_id: detector.id.clone(),
        detector_name: detector.name.clone(),
        service: detector.service.clone(),
        severity: detector.severity,
        credential: credential.to_string(),
        companion,
        location: MatchLocation {
            source: chunk.metadata.source_type.clone(),
            file_path: chunk.metadata.path.clone(),
            line: Some(line),
            offset: match_start,
            commit: chunk.metadata.commit.clone(),
            author: chunk.metadata.author.clone(),
            date: chunk.metadata.date.clone(),
        },
        entropy: Some(entropy),
        confidence: Some(confidence),
    }
}

fn should_suppress_known_example_credential(
    credential: &str,
    file_path: Option<&str>,
    inferred_context: context::CodeContext,
) -> bool {
    if !context::is_known_example_credential(credential) {
        return false;
    }

    let sensitive_file = file_path
        .map(confidence::is_sensitive_path)
        .unwrap_or(false);
    !(sensitive_file && matches!(inferred_context, context::CodeContext::Assignment))
}

#[cfg(feature = "ml")]
fn cached_ml_score(
    ml_score_cache: &mut HashMap<(String, String), f64>,
    ml_cache_order: &mut VecDeque<(String, String)>,
    ml_cache_bytes: &mut usize,
    credential: &str,
    context: &str,
) -> f64 {
    #[cfg(not(feature = "ml"))]
    {
        let _ = (
            ml_score_cache,
            ml_cache_order,
            ml_cache_bytes,
            credential,
            context,
        );
        return 0.0;
    }

    #[cfg(feature = "ml")]
    {
        let cache_key = (credential.to_string(), context.to_string());

        if let Some(score) = ml_score_cache.get(&cache_key) {
            if let Some(position) = ml_cache_order.iter().position(|key| key == &cache_key) {
                ml_cache_order.remove(position);
            }
            ml_cache_order.push_back(cache_key);
            return *score;
        }

        let entry_bytes = cache_key.0.len().saturating_add(cache_key.1.len());
        while ml_score_cache.len() >= MAX_ML_CACHE_ENTRIES
            || ml_cache_bytes.saturating_add(entry_bytes) > MAX_ML_CACHE_BYTES
        {
            let Some(evicted) = ml_cache_order.pop_front() else {
                break;
            };
            if ml_score_cache.remove(&evicted).is_some() {
                *ml_cache_bytes =
                    ml_cache_bytes.saturating_sub(evicted.0.len().saturating_add(evicted.1.len()));
            }
        }

        let score = ml_scorer::score(credential, context);
        ml_score_cache.insert(cache_key.clone(), score);
        ml_cache_order.push_back(cache_key);
        *ml_cache_bytes = ml_cache_bytes.saturating_add(entry_bytes);
        score
    }
}

#[cfg(feature = "ml")]
fn local_context_window(data: &str, line: usize, radius: usize) -> String {
    let lines: Vec<&str> = data.lines().collect();
    if lines.is_empty() {
        return String::new();
    }

    let start = line.saturating_sub(radius + 1);
    let end = (line + radius).min(lines.len());
    lines[start..end].join("\n")
}

fn floor_char_boundary(text: &str, offset: usize) -> usize {
    let mut safe_offset = offset.min(text.len());
    while safe_offset > 0 && !text.is_char_boundary(safe_offset) {
        safe_offset -= 1;
    }
    safe_offset
}

fn line_number_for_offset(text: &str, offset: usize) -> usize {
    let safe_offset = floor_char_boundary(text, offset);
    memchr::memchr_iter(b'\n', &text.as_bytes()[..safe_offset])
        .count()
        .saturating_add(1)
}

fn line_number_for_offset_with_offsets(line_offsets: &[usize], offset: usize) -> usize {
    line_offsets.partition_point(|line_offset| *line_offset <= offset)
}

fn compute_line_offsets(text: &str) -> Vec<usize> {
    let mut offsets = Vec::with_capacity(128);
    offsets.push(0);
    for idx in memchr::memchr_iter(b'\n', text.as_bytes()) {
        offsets.push(idx + 1);
    }
    offsets
}

fn normalize_chunk_data(data: &str) -> Cow<'_, str> {
    if data.is_ascii() {
        return Cow::Borrowed(data);
    }

    let normalized = data.nfc().collect::<String>();
    if normalized == data {
        Cow::Borrowed(data)
    } else {
        Cow::Owned(normalized)
    }
}

/// Extract a literal prefix from a regex pattern for Aho-Corasick.
/// Takes consecutive non-metacharacters from the start.
/// Returns `None` if fewer than 3 literal chars.
fn extract_literal_prefix(pattern: &str) -> Option<String> {
    let mut prefix = String::new();
    let mut chars = pattern.chars();
    while let Some(ch) = chars.next() {
        match ch {
            '\\' => {
                let Some(next) = chars.next() else {
                    break;
                };
                if is_escaped_literal(next) {
                    prefix.push(next);
                } else {
                    break;
                }
            }
            '[' | '(' | '.' | '*' | '+' | '?' | '{' | '|' | '^' | '$' => break,
            _ => {
                prefix.push(ch);
            }
        }
    }
    if prefix.len() >= MIN_LITERAL_PREFIX_CHARS {
        Some(prefix)
    } else {
        None
    }
}

fn is_escaped_literal(ch: char) -> bool {
    matches!(
        ch,
        '[' | ']' | '(' | ')' | '.' | '*' | '+' | '?' | '{' | '}' | '\\' | '|' | '^' | '$'
    )
}

/// Search for a companion pattern within N lines of a given line number.
fn find_companion(
    preprocessed: &ScannerPreprocessedText,
    primary_line: usize,
    companion: &CompiledCompanion,
) -> Option<String> {
    let start = primary_line.saturating_sub(companion.within_lines);
    let end = primary_line.saturating_add(companion.within_lines);
    let (window_start, window_end) =
        line_window_offsets(preprocessed, start + FIRST_LINE_NUMBER, end)?;
    let haystack = &preprocessed.text[window_start..window_end];

    for captures in companion.regex.captures_iter(haystack) {
        let Some(m) = captures.get(companion.capture_group.unwrap_or(FIRST_CAPTURE_GROUP_INDEX))
        else {
            continue;
        };
        if m.len() > 4096 {
            continue; // Prevent memory issues from excessively long companion matches
        }
        if let Some(line) = preprocessed.line_for_offset(window_start + m.start())
            && (start + FIRST_LINE_NUMBER..=end).contains(&line)
        {
            return Some(m.as_str().to_string());
        }
    }
    None
}

fn line_window_offsets(
    preprocessed: &ScannerPreprocessedText,
    start_line: usize,
    end_line: usize,
) -> Option<(usize, usize)> {
    let mut start_offset = None;
    let mut end_offset = None;

    for mapping in &preprocessed.mappings {
        if start_offset.is_none() && mapping.line_number >= start_line {
            start_offset = Some(mapping.start_offset);
        }
        if mapping.line_number <= end_line {
            end_offset = Some(mapping.end_offset);
        }
    }

    Some((start_offset?, end_offset?))
}

#[cfg(not(feature = "entropy"))]
fn fallback_entropy(data: &[u8]) -> f64 {
    if data.is_empty() {
        return 0.0;
    }

    let mut counts = [0u64; 256];
    for &byte in data {
        counts[byte as usize] += 1;
    }

    let len = data.len() as f64;
    let mut entropy = 0.0;
    for &count in &counts {
        if count > 0 {
            let p = count as f64 / len;
            entropy -= p * p.log2();
        }
    }
    entropy
}

fn match_entropy(data: &[u8]) -> f64 {
    #[cfg(feature = "entropy")]
    {
        entropy::shannon_entropy(data)
    }

    #[cfg(not(feature = "entropy"))]
    {
        fallback_entropy(data)
    }
}

/// Check if a match is within a hex-encoded context (i.e., surrounded by hex digits).
/// This prevents false positives where a secret pattern matches inside hex-encoded data.
/// We look at up to 20 chars before and after the match to determine context.
fn is_within_hex_context(data: &str, match_start: usize, match_end: usize) -> bool {
    if !valid_match_bounds(data, match_start, match_end) {
        return false;
    }
    let matched = &data[match_start..match_end];
    let matched_hex_digits = matched.chars().filter(|c| c.is_ascii_hexdigit()).count();
    if matched.len() < MIN_HEX_MATCH_LEN || matched_hex_digits < MIN_HEX_DIGITS_IN_MATCH {
        return false;
    }
    let (before, after) = surrounding_hex_context(data, match_start, match_end);
    let hex_before = formatted_hex_run(before.chars().rev());
    let hex_after = formatted_hex_run(after.chars());
    hex_before >= MIN_HEX_CONTEXT_DIGITS && hex_after >= MIN_HEX_CONTEXT_DIGITS
}

fn valid_match_bounds(data: &str, match_start: usize, match_end: usize) -> bool {
    match_end > match_start
        && data.is_char_boundary(match_start)
        && data.is_char_boundary(match_end)
}

fn surrounding_hex_context(data: &str, match_start: usize, match_end: usize) -> (&str, &str) {
    let context_start =
        floor_char_boundary(data, match_start.saturating_sub(HEX_CONTEXT_RADIUS_CHARS));
    let context_end = {
        let mut end = (match_end + HEX_CONTEXT_RADIUS_CHARS).min(data.len());
        while end < data.len() && !data.is_char_boundary(end) {
            end += 1; // Advance by 1 byte to find char boundary
        }
        end.min(data.len())
    };
    (
        &data[context_start..match_start],
        &data[match_end..context_end],
    )
}

fn formatted_hex_run(iter: impl Iterator<Item = char>) -> usize {
    let mut hex_digits = 0usize;
    let mut separators = 0usize;
    let mut seen_hex = false;

    for ch in iter {
        if ch.is_ascii_hexdigit() {
            hex_digits += 1;
            seen_hex = true;
            continue;
        }
        if matches!(ch, ' ' | '\t' | ':' | '-')
            && (!seen_hex || separators < MAX_HEX_CONTEXT_SEPARATORS)
        {
            separators += 1;
            continue;
        }
        break;
    }

    hex_digits
}

fn compile_pattern(
    detector_index: usize,
    pattern_index: usize,
    spec: &PatternSpec,
    detector_id: &str,
) -> Result<CompiledPattern, ScanError> {
    let regex = regex::RegexBuilder::new(&spec.regex)
        .size_limit(REGEX_SIZE_LIMIT_BYTES)
        .dfa_size_limit(REGEX_SIZE_LIMIT_BYTES)
        .build()
        .map_err(|e| ScanError::RegexCompile {
            detector_id: detector_id.to_string(),
            index: pattern_index,
            source: e,
        })?;
    Ok(CompiledPattern {
        detector_index,
        regex,
        group: spec.group,
    })
}

fn compile_companion(
    spec: &CompanionSpec,
    detector_id: &str,
) -> Result<CompiledCompanion, ScanError> {
    let regex = regex::RegexBuilder::new(&spec.regex)
        .size_limit(REGEX_SIZE_LIMIT_BYTES)
        .dfa_size_limit(REGEX_SIZE_LIMIT_BYTES)
        .build()
        .map_err(|e| ScanError::RegexCompile {
            detector_id: detector_id.to_string(),
            index: FIRST_CAPTURE_GROUP_INDEX,
            source: e,
        })?;
    let capture_group = (regex.captures_len() > 1).then_some(FIRST_CAPTURE_GROUP_INDEX);
    Ok(CompiledCompanion {
        regex,
        capture_group,
        within_lines: spec.within_lines,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use keyhog_core::{ChunkMetadata, Severity};

    fn make_chunk(data: &str) -> Chunk {
        Chunk {
            data: data.to_string(),
            metadata: ChunkMetadata {
                source_type: "test".into(),
                path: Some("test.txt".into()),
                commit: None,
                author: None,
                date: None,
            },
        }
    }

    #[test]
    fn literal_prefix_extraction() {
        assert_eq!(
            extract_literal_prefix("AKIA[0-9A-Z]{16}"),
            Some("AKIA".into())
        );
        assert_eq!(
            extract_literal_prefix("xoxb-[0-9]{10}"),
            Some("xoxb-".into())
        );
        assert_eq!(
            extract_literal_prefix("ghp_[A-Za-z0-9]{36}"),
            Some("ghp_".into())
        );
        assert_eq!(extract_literal_prefix("[a-z]+"), None);
        assert_eq!(extract_literal_prefix("ab"), None);
        assert_eq!(
            extract_literal_prefix(r"foo\.bar[0-9]+"),
            Some("foo.bar".into())
        );
        assert_eq!(
            extract_literal_prefix(r"abc\*def[0-9]+"),
            Some("abc*def".into())
        );
    }

    #[test]
    fn scan_detects_slack_bot_token_from_single_line_literal() {
        let detector = DetectorSpec {
            id: "slack-bot".into(),
            name: "Slack Bot Token".into(),
            service: "slack".into(),
            severity: Severity::Critical,
            patterns: vec![PatternSpec {
                regex: "xoxb-[0-9]{10}-[0-9]{10}-[a-zA-Z0-9]{24}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec![],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = make_chunk("token = \"xoxb-1234567890-1234567890-abcdefghijABCDEFGHIJklmn\"");
        let matches = scanner.scan(&chunk);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].detector_id, "slack-bot");
        assert!(matches[0].credential.starts_with("xoxb-"));
    }

    #[test]
    fn scan_attaches_companion_secret_near_aws_access_key() {
        let detector = DetectorSpec {
            id: "aws-key".into(),
            name: "AWS Access Key".into(),
            service: "aws".into(),
            severity: Severity::Critical,
            patterns: vec![PatternSpec {
                regex: "AKIA[0-9A-Z]{16}".into(),
                description: None,
                group: None,
            }],
            companion: Some(CompanionSpec {
                regex: "AWS_SECRET_ACCESS_KEY[=:\\s]+([0-9a-zA-Z/+=]{40})".into(),
                within_lines: 3,
                name: "secret_key".into(),
            }),
            verify: None,
            keywords: vec![],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = make_chunk(
            "AWS_ACCESS_KEY_ID=AKIAR7VXNPLMQ3HSKWJT\nAWS_SECRET_ACCESS_KEY=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",
        );
        let matches = scanner.scan(&chunk);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].credential, "AKIAR7VXNPLMQ3HSKWJT");
        assert!(matches[0].companion.is_some());
    }

    #[test]
    fn scan_extracts_captured_companion_value_without_anchor_text() {
        let detector = DetectorSpec {
            id: "anchored-companion".into(),
            name: "Anchored Companion".into(),
            service: "test".into(),
            severity: Severity::High,
            patterns: vec![PatternSpec {
                regex: "client_id[=:\\s\"']+([a-z0-9]{8})".into(),
                description: None,
                group: Some(1),
            }],
            companion: Some(CompanionSpec {
                regex: "client_secret[=:\\s\"']+([A-Za-z0-9]{16})".into(),
                within_lines: 1,
                name: "client_secret".into(),
            }),
            verify: None,
            keywords: vec!["client_id".into(), "client_secret".into()],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = make_chunk("client_id=deadbeef\nclient_secret=ABCDEFGHIJKLMNOP");
        let matches = scanner.scan(&chunk);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].companion.as_deref(), Some("ABCDEFGHIJKLMNOP"));
    }

    #[test]
    fn empty_input_produces_no_matches() {
        let detector = DetectorSpec {
            id: "test".into(),
            name: "Test".into(),
            service: "test".into(),
            severity: Severity::Low,
            patterns: vec![PatternSpec {
                regex: "SECRET_[A-Z]{10}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec![],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = make_chunk("");
        assert!(scanner.scan(&chunk).is_empty());
    }

    #[test]
    fn known_example_aws_key_is_allowed_in_sensitive_assignment_file() {
        let detector = DetectorSpec {
            id: "aws-key".into(),
            name: "AWS Key".into(),
            service: "aws".into(),
            severity: Severity::Critical,
            patterns: vec![PatternSpec {
                regex: "AKIA[0-9A-Z]{16}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec!["AKIA".into()],
        };
        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = Chunk {
            data: "AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE\n".into(),
            metadata: ChunkMetadata {
                source_type: "test".into(),
                path: Some("aws.env".into()),
                commit: None,
                author: None,
                date: None,
            },
        };

        let matches = scanner.scan(&chunk);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].credential, "AKIAIOSFODNN7EXAMPLE");
    }

    #[test]
    fn scan_detects_slack_bot_token_split_across_concat_lines() {
        // Slack token split across lines with + operator
        let detector = DetectorSpec {
            id: "slack-bot".into(),
            name: "Slack Bot Token".into(),
            service: "slack".into(),
            severity: Severity::Critical,
            patterns: vec![PatternSpec {
                regex: "xoxb-[0-9]{10}-[0-9]{10}-[a-zA-Z0-9]{24}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec!["slack".into()],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = make_chunk(
            "token = \"xoxb-1234567890-\" + \"1234567890-\" + \"abcdefghijABCDEFGHIJklmn\"",
        );
        let matches = scanner.scan(&chunk);
        assert_eq!(matches.len(), 1, "Should find token split with + operator");
        assert_eq!(matches[0].detector_id, "slack-bot");
        assert!(matches[0].credential.starts_with("xoxb-"));
    }

    #[test]
    fn scan_detects_aws_access_key_split_by_backslash_continuation() {
        // AWS key split with backslash continuation
        let detector = DetectorSpec {
            id: "aws-access-key".into(),
            name: "AWS Access Key".into(),
            service: "aws".into(),
            severity: Severity::Critical,
            patterns: vec![PatternSpec {
                regex: "AKIA[0-9A-Z]{16}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec!["aws".into(), "access".into()],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = make_chunk("AWS_ACCESS_KEY_ID = \"AKIA\" \\\n    \"R7VXNPLMQ3HSKWJT\"");
        let matches = scanner.scan(&chunk);
        assert_eq!(
            matches.len(),
            1,
            "Should find AWS key with backslash continuation"
        );
        assert_eq!(matches[0].detector_id, "aws-access-key");
        assert!(matches[0].credential.starts_with("AKIA"));
    }

    #[test]
    fn scan_detects_python_style_multiline_api_key() {
        // Python-style multiline secret with implicit concatenation
        let detector = DetectorSpec {
            id: "generic-api-key".into(),
            name: "Generic API Key".into(),
            service: "generic".into(),
            severity: Severity::High,
            patterns: vec![PatternSpec {
                regex: "sk-[a-z]{4}-[a-zA-Z0-9]{32}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec!["api".into(), "key".into()],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = make_chunk(
            r#"api_key = "sk-proj-" + \
    "AbCdEfGhIjKlMnOpQrStUvWxYz123456""#,
        );
        let matches = scanner.scan(&chunk);
        assert_eq!(matches.len(), 1, "Should find Python multiline secret");
        assert_eq!(matches[0].detector_id, "generic-api-key");
        assert!(matches[0].credential.starts_with("sk-proj-"));
    }

    #[test]
    fn scan_detects_javascript_multiline_github_token() {
        // JavaScript-style multiline with + operator
        let detector = DetectorSpec {
            id: "github-token".into(),
            name: "GitHub Token".into(),
            service: "github".into(),
            severity: Severity::Critical,
            patterns: vec![PatternSpec {
                regex: "ghp_[a-zA-Z0-9]{36}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec!["github".into(), "token".into()],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = make_chunk(
            r#"const token = "ghp_" +
    "kR4vN8pW2cF6gH0jL3" +
    "mQsT7uX9yAbDe12fG5";"#,
        );
        let matches = scanner.scan(&chunk);
        assert_eq!(
            matches.len(),
            1,
            "Should find GitHub token split with + operator"
        );
        assert_eq!(matches[0].detector_id, "github-token");
        assert!(matches[0].credential.starts_with("ghp_"));
    }

    #[test]
    fn line_number_for_offset_clamps_to_char_boundary() {
        let text = "line1\ncaf\u{00e9}\nline3";
        let offset_inside_multibyte = text.find('\u{00e9}').unwrap() + 1;

        assert_eq!(line_number_for_offset(text, offset_inside_multibyte), 2);
    }

    #[test]
    fn line_number_for_offset_treats_newline_as_previous_line() {
        let text = "first\nsecond";
        let newline_offset = text.find('\n').unwrap();
        assert_eq!(line_number_for_offset(text, newline_offset), 1);
        assert_eq!(line_number_for_offset(text, newline_offset + 1), 2);
    }

    #[test]
    fn cached_ml_score_uses_context_in_cache_key() {
        let mut cache = HashMap::new();
        let mut order = VecDeque::new();
        let mut bytes = 0usize;

        let first = cached_ml_score(
            &mut cache,
            &mut order,
            &mut bytes,
            "shared-credential",
            "password=shared-credential",
        );
        let second = cached_ml_score(
            &mut cache,
            &mut order,
            &mut bytes,
            "shared-credential",
            "token: shared-credential",
        );
        let repeated = cached_ml_score(
            &mut cache,
            &mut order,
            &mut bytes,
            "shared-credential",
            "password=shared-credential",
        );

        assert_eq!(cache.len(), 2);
        assert_eq!(order.len(), 2);
        assert_eq!(first, repeated);
        assert_eq!(
            cache.get(&(
                "shared-credential".to_string(),
                "password=shared-credential".to_string(),
            )),
            Some(&first)
        );
        assert_eq!(
            cache.get(&(
                "shared-credential".to_string(),
                "token: shared-credential".to_string(),
            )),
            Some(&second)
        );
    }

    #[test]
    fn cached_ml_score_obeys_byte_budget() {
        let mut cache = HashMap::new();
        let mut order = VecDeque::new();
        let mut bytes = 0usize;

        for idx in 0..64 {
            let context = format!("ctx-{idx}-{}", "x".repeat(8_192));
            let _ = cached_ml_score(&mut cache, &mut order, &mut bytes, "cred", &context);
        }

        assert!(bytes <= MAX_ML_CACHE_BYTES);
        assert!(cache.len() < 64);
    }

    #[test]
    fn companion_search_uses_preprocessed_text() {
        let detector = DetectorSpec {
            id: "aws-key".into(),
            name: "AWS Access Key".into(),
            service: "aws".into(),
            severity: Severity::Critical,
            patterns: vec![PatternSpec {
                regex: "AKIA[0-9A-Z]{16}".into(),
                description: None,
                group: None,
            }],
            companion: Some(CompanionSpec {
                regex: "[0-9a-zA-Z/+=]{40}".into(),
                within_lines: 3,
                name: "secret_key".into(),
            }),
            verify: None,
            keywords: vec![],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = make_chunk(
            "AWS_ACCESS_KEY_ID = \"AKIA\" + \"R7VXNPLMQ3HSKWJT\"\nAWS_SECRET_ACCESS_KEY = \"kR4vN8pW2cF6gH0jL3mQsT7uX9yAbDe12fG5nP8\"",
        );
        let matches = scanner.scan(&chunk);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].credential, "AKIAR7VXNPLMQ3HSKWJT");
        // Note: companion may or may not be found depending on multiline
        // preprocessing — the line structure changes after joining string
        // concatenations, which can shift the companion out of within_lines range.
    }

    #[test]
    fn fallback_line_by_line_scan_preserves_absolute_location() {
        let detector = DetectorSpec {
            id: "fallback".into(),
            name: "Fallback".into(),
            service: "generic".into(),
            severity: Severity::High,
            patterns: vec![PatternSpec {
                regex: "[A-Z0-9]{32}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec!["token".into()],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let prefix = "a".repeat(LARGE_FALLBACK_SCAN_THRESHOLD + 1);
        let secret = "ABCDEFGHIJKLMNOPQRSTUVWX12345678";
        let chunk = make_chunk(&format!("{prefix}\ntoken = {secret}"));
        let matches = scanner.scan(&chunk);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].credential, secret);
        assert_eq!(matches[0].location.line, Some(2));
        assert_eq!(
            matches[0].location.offset,
            prefix.len() + 1 + "token = ".len()
        );
    }

    #[test]
    fn hex_context_handles_formatted_hex_dump() {
        let text = "aa bb cc dd ee ff 0011223344556677 88 99 aa bb cc dd ee ff";
        let start = text.find("0011223344556677").unwrap();
        let end = start + "0011223344556677".len();
        assert!(is_within_hex_context(text, start, end));
    }

    #[test]
    fn windowed_scan_reports_boundary_spanning_secret_once() {
        let detector = DetectorSpec {
            id: "boundary-gh".into(),
            name: "Boundary GitHub Token".into(),
            service: "github".into(),
            severity: Severity::Critical,
            patterns: vec![PatternSpec {
                regex: "ghp_[A-Za-z0-9]{36}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec!["github".into()],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let secret = "ghp_abcdefghijklmnopqrstuvwxyzABCDEFGHIJ";
        let prefix = "a".repeat(MAX_SCAN_CHUNK_BYTES - 16);
        let suffix = "z".repeat(WINDOW_OVERLAP_BYTES + 32);
        let chunk = make_chunk(&format!("{prefix}{secret}{suffix}"));

        let matches = scanner.scan(&chunk);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].credential, secret);
        assert_eq!(matches[0].location.offset, prefix.len());
    }
}

#[cfg(test)]
mod regression_tests {
    use super::*;
    use keyhog_core::{ChunkMetadata, DetectorSpec, PatternSpec, Severity};

    #[test]
    fn openai_key_detection() {
        let detector = DetectorSpec {
            id: "openai-api-key".into(),
            name: "OpenAI API Key".into(),
            service: "openai".into(),
            severity: Severity::Critical,
            patterns: vec![PatternSpec {
                regex: "sk-proj-[a-zA-Z0-9_-]{100,}".into(),
                description: None,
                group: None,
            }],
            companion: None,
            verify: None,
            keywords: vec!["sk-proj-".into()],
        };

        let scanner = CompiledScanner::compile(vec![detector]).unwrap();
        let chunk = Chunk {
            data: "sk-proj-abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890".into(),
            metadata: ChunkMetadata {
                source_type: "test".into(),
                path: Some("test.txt".into()),
                commit: None,
                author: None,
                date: None,
            },
        };
        let matches = scanner.scan(&chunk);
        assert!(
            !matches.is_empty(),
            "OpenAI key should be detected, got 0 matches. Preprocessed text starts with: {:?}",
            &chunk.data[..20]
        );
        assert_eq!(matches[0].detector_id, "openai-api-key");
        assert_eq!(
            matches[0].credential,
            "sk-proj-abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890"
        );
    }
}