redact-secret 0.1.0-beta.15

Deterministic secret detection and redaction for runtime data and AI context: scans text locally before it reaches logs, storage, telemetry, tools, or model context. Side-effect free: no runtime network, filesystem, environment, telemetry, secret storage, or UI.
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
//! A shared literal prefilter for built-in and ruleset detectors (issues
//! #983, #1057).
//!
//! Most built-in detectors can only propose a candidate when the scan copy
//! contains one of a few case-sensitive literals: a provider prefix
//! (`ghp_`), a marker (`.atlasv1.`) or a separator pair. Each such detector
//! declares those literals when it is registered ([`RequiredLiterals`]).
//! A built-in registry compiles every declaration once into one
//! [`LiteralMatcher`], and the pipeline makes one pass over each scan copy
//! with it: the pass reports exactly which declaring detectors have a
//! literal in the input. A detector none of whose literals occurs is
//! skipped: it would have searched the whole input and returned nothing.
//!
//! The test is exact. The matcher compares every literal byte for byte at
//! every offset whose two bytes begin one, so a detector is skipped exactly
//! when none of its literals occurs anywhere in the scan copy. (The #983
//! version tested a hashed 1,024-bit set of byte pairs instead; beyond a few
//! KB of text the set filled up and almost nothing was skipped.)
//!
//! The declaration is private to the registry. It is never read from the
//! public `Detector` trait, so custom detectors, and the built-ins that
//! cannot name such a literal (`generic-token`, keyword-gated detectors,
//! bare-shape detectors such as `discord`), always run.
//!
//! A ruleset detector (`super::ruleset_adapter`) is registered through that
//! public trait like any custom detector, so the registry cannot tell it
//! from one and it declares nothing there. It filters itself instead, with
//! [`ruleset_literal_may_occur`]: the pipeline marks the scan copy it is
//! scanning ([`ScanScope`]), and a ruleset detector handed exactly that copy
//! asks the byte pairs of the copy, built once per call and shared by every
//! ruleset detector, whether its prefix can occur.

use std::cell::RefCell;
use std::marker::PhantomData;
use std::rc::Rc;

use super::pattern::PrefixShape;

/// Bits in a [`PairSet`]. A power of two, so the hash keeps its top bits.
const PAIR_SET_BITS: usize = 1024;

/// How far [`pair_slot`] shifts its product to keep `log2(PAIR_SET_BITS)`
/// bits.
const PAIR_SLOT_SHIFT: u32 = u32::BITS - PAIR_SET_BITS.trailing_zeros();

/// Every 16-bit byte pair, one bit each: an exact [`PairTable`] and the
/// [`LiteralMatcher`]'s lead set.
const PAIR_TABLE_WORDS: usize = (1 << 16) / 64;

/// Scan copies at least this long get an exact [`PairTable`] instead of a
/// hashed [`PairSet`]: past a few KB of text the 1,024-bit set is mostly
/// full, while the table's 8 KiB is cheap next to the text itself.
const EXACT_PAIRS_FROM: usize = 4 * 1024;

/// The bitset slot of the byte pair `(first, second)`: a multiplicative
/// hash of the pair's 16-bit value.
fn pair_slot(first: u8, second: u8) -> usize {
    let pair = u32::from(u16::from_le_bytes([first, second]));
    // The top bits of a product by an odd constant (Fibonacci hashing).
    (pair.wrapping_mul(0x9E37_79B1) >> PAIR_SLOT_SHIFT) as usize
}

/// The 16-bit value of the byte pair `(first, second)`, the index of its
/// bit in a [`PairTable`] or a [`LiteralMatcher`]'s lead set.
const fn pair_index(first: u8, second: u8) -> usize {
    ((first as usize) << 8) | second as usize
}

/// Which adjacent byte pairs occur in one short scan copy, as a hashed
/// bitset.
///
/// A 1024-bit set costs 128 bytes to clear, which matters because an
/// incremental session scans one closed line at a time; an exact
/// 65,536-bit [`PairTable`] would cost 8 KiB per line.
pub(crate) struct PairSet {
    bits: [u64; PAIR_SET_BITS / 64],
}

impl PairSet {
    /// The pairs of `text`.
    pub(crate) fn of(text: &[u8]) -> Self {
        let mut bits = [0u64; PAIR_SET_BITS / 64];
        for pair in text.windows(2) {
            let slot = pair_slot(pair[0], pair[1]);
            bits[slot / 64] |= 1 << (slot % 64);
        }
        Self { bits }
    }

    /// `false` only when the pair `(first, second)` occurs nowhere in the
    /// text.
    fn may_contain(&self, first: u8, second: u8) -> bool {
        let slot = pair_slot(first, second);
        self.bits[slot / 64] & (1 << (slot % 64)) != 0
    }

    /// `false` only when `literal` cannot occur in the text: one of its
    /// consecutive pairs occurs nowhere.
    fn may_contain_literal(&self, literal: &[u8]) -> bool {
        literal
            .windows(2)
            .all(|pair| self.may_contain(pair[0], pair[1]))
    }
}

/// Which adjacent byte pairs occur in one scan copy, exactly: one bit per
/// 16-bit pair, so it never fills up however long the text is.
struct PairTable {
    bits: [u64; PAIR_TABLE_WORDS],
}

impl PairTable {
    /// The pairs of `text`.
    fn of(text: &[u8]) -> Box<Self> {
        let mut table = Box::new(Self {
            bits: [0; PAIR_TABLE_WORDS],
        });
        for pair in text.windows(2) {
            let index = pair_index(pair[0], pair[1]);
            table.bits[index / 64] |= 1 << (index % 64);
        }
        table
    }

    /// `false` exactly when the pair `(first, second)` occurs nowhere in
    /// the text.
    const fn contains(&self, first: u8, second: u8) -> bool {
        let index = pair_index(first, second);
        self.bits[index / 64] & (1 << (index % 64)) != 0
    }

    /// `false` only when `literal` cannot occur in the text: one of its
    /// consecutive pairs occurs nowhere. Two pairs from different places
    /// still pass, so this is conservative, never exact.
    fn may_contain_literal(&self, literal: &[u8]) -> bool {
        literal
            .windows(2)
            .all(|pair| self.contains(pair[0], pair[1]))
    }
}

/// The byte pairs of a scan copy, sized to it: a hashed [`PairSet`] for a
/// short copy, an exact [`PairTable`] for a long one.
enum Pairs {
    Hashed(PairSet),
    Exact(Box<PairTable>),
}

impl Pairs {
    fn of(text: &[u8]) -> Self {
        if text.len() < EXACT_PAIRS_FROM {
            Self::Hashed(PairSet::of(text))
        } else {
            Self::Exact(PairTable::of(text))
        }
    }

    fn may_contain_literal(&self, literal: &[u8]) -> bool {
        match self {
            Self::Hashed(pairs) => pairs.may_contain_literal(literal),
            Self::Exact(pairs) => pairs.may_contain_literal(literal),
        }
    }
}

/// One group of a detector's declared literals, named the way its grammar
/// already holds them, so a declaration reuses the scan's own constants
/// instead of retyping them. Plain data: a declaration is a `const`, and
/// walking it is one non-generic function, which keeps the declarations
/// out of the WebAssembly code size.
#[derive(Clone, Copy)]
pub(super) enum Literals {
    /// Byte-string constants (`b"SG."`).
    Bytes(&'static [&'static [u8]]),
    /// String constants (`"ghp_"`).
    Strs(&'static [&'static str]),
    /// The first string of each pair of a `(prefix, finding type)` table.
    Prefixes(&'static [(&'static str, &'static str)]),
    /// The prefix of each shape of a [`PrefixShape`] table.
    Shapes(&'static [PrefixShape<'static>]),
}

impl Literals {
    fn for_each(self, visit: &mut dyn FnMut(&'static [u8])) {
        match self {
            Self::Bytes(literals) => literals.iter().for_each(|literal| visit(literal)),
            Self::Strs(literals) => literals
                .iter()
                .for_each(|literal| visit(literal.as_bytes())),
            Self::Prefixes(pairs) => pairs
                .iter()
                .for_each(|(prefix, _)| visit(prefix.as_bytes())),
            Self::Shapes(shapes) => shapes
                .iter()
                .for_each(|shape| visit(shape.prefix.as_bytes())),
        }
    }
}

/// The case-sensitive literals at least one of which occurs in the scan
/// copy whenever a detector proposes any candidate.
///
/// Every literal has at least two bytes, so the [`LiteralMatcher`] can key
/// it by its first byte pair. [`Self::any_of`] refuses a list with a
/// shorter one, and the detector then always runs. The declaration borrows
/// the detector's own `'static` grammar tables, so registering a built-in
/// allocates nothing for it.
#[derive(Clone, Copy)]
pub(crate) struct RequiredLiterals {
    groups: &'static [Literals],
}

impl std::fmt::Debug for RequiredLiterals {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RequiredLiterals").finish_non_exhaustive()
    }
}

impl RequiredLiterals {
    /// A declaration from every literal in `groups`, or `None` (always run
    /// the detector) when there is none or one is shorter than two bytes.
    pub(super) fn any_of(groups: &'static [Literals]) -> Option<Self> {
        let mut count = 0usize;
        let mut too_short = false;
        for group in groups {
            group.for_each(&mut |literal| {
                count += 1;
                too_short |= literal.len() < 2;
            });
        }
        (count > 0 && !too_short).then_some(Self { groups })
    }

    /// Calls `visit` with every declared literal, in declaration order.
    fn for_each(self, visit: &mut dyn FnMut(&'static [u8])) {
        for group in self.groups {
            group.for_each(visit);
        }
    }

    /// Every declared literal, in declaration order.
    #[cfg(test)]
    pub(crate) fn literals(self) -> Vec<&'static [u8]> {
        let mut literals = Vec::new();
        self.for_each(&mut |literal| literals.push(literal));
        literals
    }
}

/// Words in a [`SlotSet`].
const SLOT_WORDS: usize = 4;

/// Detectors one [`LiteralMatcher`] can cover: registry positions
/// `0..MAX_SLOTS`. A registry with a declaration past it gets no matcher,
/// and every detector then runs.
const MAX_SLOTS: usize = SLOT_WORDS * 64;

/// A set of registry positions, one bit each.
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct SlotSet {
    bits: [u64; SLOT_WORDS],
}

impl SlotSet {
    fn insert(&mut self, slot: usize) {
        self.bits[slot / 64] |= 1 << (slot % 64);
    }

    /// `true` when `slot` is in the set.
    pub(crate) fn contains(&self, slot: usize) -> bool {
        slot < MAX_SLOTS && self.bits[slot / 64] & (1 << (slot % 64)) != 0
    }

    /// `true` when every slot of `self` is in `other`.
    fn is_subset_of(&self, other: &Self) -> bool {
        self.bits
            .iter()
            .zip(&other.bits)
            .all(|(mine, theirs)| mine & !theirs == 0)
    }

    fn len(&self) -> u32 {
        self.bits.iter().map(|word| word.count_ones()).sum()
    }
}

/// One declared literal and the registry position of the detector that
/// declared it.
#[derive(Clone, Copy)]
struct Entry {
    literal: &'static [u8],
    slot: u16,
}

/// Every literal a registry's detectors declared, compiled once so that one
/// pass over a scan copy finds exactly which detectors have one.
///
/// The pass tests each offset's two bytes against a 65,536-bit lead set;
/// only an offset where some literal begins goes on to compare the
/// literals with that first pair. The matcher is built from `'static`
/// declarations and reused by every registry of the same profile.
pub(crate) struct LiteralMatcher {
    /// Bit `pair_index(a, b)` is set when some literal begins with `a b`.
    leads: Box<[u64; PAIR_TABLE_WORDS]>,
    /// Per word of `leads`, the number of set bits in the words before it:
    /// with a popcount of the bits below a lead, the lead's rank, which is
    /// its index into `groups`.
    ranks: Box<[u32; PAIR_TABLE_WORDS]>,
    /// Every declared literal, sorted by its first pair (then by slot).
    entries: Box<[Entry]>,
    /// The `entries` beginning with each lead pair, in lead-rank order.
    groups: Box<[Group]>,
    /// The slots that declared anything.
    declared: SlotSet,
}

/// The `entries[start..end]` beginning with one lead pair, and the slots
/// that declared them.
#[derive(Clone, Copy)]
struct Group {
    start: u32,
    end: u32,
    slots: SlotSet,
}

impl std::fmt::Debug for LiteralMatcher {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LiteralMatcher").finish_non_exhaustive()
    }
}

impl LiteralMatcher {
    /// Compiles the declarations of a registry's detectors, given in
    /// registry order: the `n`th item is the declaration of the detector at
    /// position `n`, if any. `None` when a declaring detector sits past
    /// [`MAX_SLOTS`], so no detector could be skipped safely.
    pub(crate) fn compile<'a>(
        declarations: impl IntoIterator<Item = Option<&'a RequiredLiterals>>,
    ) -> Option<Self> {
        let mut entries = Vec::new();
        let mut declared = SlotSet::default();
        for (slot, required) in declarations.into_iter().enumerate() {
            let Some(required) = required else {
                continue;
            };
            let Ok(slot_id) = u16::try_from(slot) else {
                return None;
            };
            if slot >= MAX_SLOTS {
                return None;
            }
            declared.insert(slot);
            required.for_each(&mut |literal| {
                entries.push(Entry {
                    literal,
                    slot: slot_id,
                });
            });
        }
        let lead = |entry: &Entry| pair_index(entry.literal[0], entry.literal[1]);
        entries.sort_by_key(|entry| (lead(entry), entry.slot));
        let mut leads = Box::new([0u64; PAIR_TABLE_WORDS]);
        let mut groups: Vec<Group> = Vec::new();
        let mut last_pair = None;
        for (index, entry) in entries.iter().enumerate() {
            let pair = lead(entry);
            let index = u32::try_from(index).ok()?;
            if last_pair == Some(pair) {
                if let Some(group) = groups.last_mut() {
                    group.end = index + 1;
                    group.slots.insert(usize::from(entry.slot));
                }
            } else {
                leads[pair / 64] |= 1 << (pair % 64);
                let mut slots = SlotSet::default();
                slots.insert(usize::from(entry.slot));
                groups.push(Group {
                    start: index,
                    end: index + 1,
                    slots,
                });
                last_pair = Some(pair);
            }
        }
        let mut ranks = Box::new([0u32; PAIR_TABLE_WORDS]);
        let mut seen = 0u32;
        for (rank, word) in ranks.iter_mut().zip(leads.iter()) {
            *rank = seen;
            seen += word.count_ones();
        }
        Some(Self {
            leads,
            ranks,
            entries: entries.into_boxed_slice(),
            groups: groups.into_boxed_slice(),
            declared,
        })
    }

    /// The group of the literals whose first pair is `lead`, which is in
    /// the lead set: its rank among the set bits is its index.
    fn group(&self, lead: usize) -> &Group {
        let word = lead / 64;
        let below = self.leads[word] & ((1u64 << (lead % 64)) - 1);
        &self.groups[self.ranks[word] as usize + below.count_ones() as usize]
    }

    /// The registry positions whose detector declared a literal that occurs
    /// in `text`. The pass stops early once every declaring detector has
    /// one.
    pub(crate) fn present(&self, text: &[u8]) -> SlotSet {
        let mut present = SlotSet::default();
        let mut remaining = self.declared.len();
        if remaining == 0 {
            return present;
        }
        for (offset, pair) in text.windows(2).enumerate() {
            let lead = pair_index(pair[0], pair[1]);
            if self.leads[lead / 64] & (1 << (lead % 64)) == 0 {
                continue;
            }
            let group = self.group(lead);
            if group.slots.is_subset_of(&present) {
                continue;
            }
            let rest = &text[offset..];
            for entry in &self.entries[group.start as usize..group.end as usize] {
                let slot = usize::from(entry.slot);
                if !present.contains(slot) && rest.starts_with(entry.literal) {
                    present.insert(slot);
                    remaining -= 1;
                    if remaining == 0 {
                        return present;
                    }
                }
            }
        }
        present
    }
}

/// The scan copy the pipeline is scanning on this thread, and its byte
/// pairs once a ruleset detector has asked for them.
struct ActiveScan {
    /// Address and length of the scan copy: a detector handed a `&str` with
    /// both is handed the very same, still borrowed, bytes.
    start: usize,
    len: usize,
    pairs: Option<Pairs>,
    /// The lines holding a long alphabet run, as byte ranges, once a
    /// bare-shape detector has asked (issue #1075). Never text.
    long_run_lines: Option<Rc<[(usize, usize)]>>,
    /// Which provider words occur in the scan copy, once a keyword-gated
    /// detector has asked (issue #1092). A bit set, never text.
    provider_words: Option<u8>,
}

thread_local! {
    static ACTIVE_SCAN: RefCell<Option<ActiveScan>> = const { RefCell::new(None) };
}

/// Marks `scanned` as the scan copy the pipeline is running detectors on,
/// for [`ruleset_literal_may_occur`], until dropped. Scopes nest: a detector
/// that itself scans restores the outer scan's mark when its scan returns.
pub(crate) struct ScanScope {
    previous: Option<ActiveScan>,
    /// A scope restores this thread's mark, so it must stay on it.
    _not_send: PhantomData<*const ()>,
}

impl ScanScope {
    pub(crate) fn enter(scanned: &str) -> Self {
        let active = ActiveScan {
            start: scanned.as_ptr() as usize,
            len: scanned.len(),
            pairs: None,
            long_run_lines: None,
            provider_words: None,
        };
        let previous = ACTIVE_SCAN
            .try_with(|cell| {
                cell.try_borrow_mut()
                    .ok()
                    .and_then(|mut current| current.replace(active))
            })
            .ok()
            .flatten();
        Self {
            previous,
            _not_send: PhantomData,
        }
    }
}

impl Drop for ScanScope {
    fn drop(&mut self) {
        let previous = self.previous.take();
        let _ = ACTIVE_SCAN.try_with(|cell| {
            if let Ok(mut current) = cell.try_borrow_mut() {
                *current = previous;
            }
        });
    }
}

/// `false` only when `literal` provably occurs nowhere in `input`: `input`
/// is the scan copy of the innermost [`ScanScope`] on this thread, and one
/// of `literal`'s byte pairs occurs nowhere in it. Every other case —
/// outside a scan, a different or partial input, a literal shorter than two
/// bytes — answers `true`, so a caller only ever skips work that would have
/// found nothing.
pub(crate) fn ruleset_literal_may_occur(input: &str, literal: &[u8]) -> bool {
    if literal.len() < 2 {
        return true;
    }
    ACTIVE_SCAN
        .try_with(|cell| {
            let Ok(mut current) = cell.try_borrow_mut() else {
                return true;
            };
            match current.as_mut() {
                Some(active)
                    if active.start == input.as_ptr() as usize && active.len == input.len() =>
                {
                    active
                        .pairs
                        .get_or_insert_with(|| Pairs::of(input.as_bytes()))
                        .may_contain_literal(literal)
                }
                _ => true,
            }
        })
        .unwrap_or(true)
}

// Test hook (issue #1083): makes `long_run_lines` answer `None` on this
// thread, so a scan walks every line as it does outside a scan copy.
#[cfg(test)]
thread_local! {
    static FORCE_BYPASS_LONG_RUN_INDEX: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// The lines of `input` that hold a run of 32 or more `[A-Za-z0-9+/=_-]`
/// bytes ([`super::text::long_run_line_spans`]), when `input` is the scan
/// copy of the innermost [`ScanScope`] on this thread; built once per scan
/// copy and shared by every bare-shape detector. `None` in every other case
/// (outside a scan, a different or partial input), where the caller walks
/// every line as before.
///
/// There is deliberately no density guard that bypasses the index when every
/// line holds a long run (issue #1083): measured on the current tree the
/// index never costs instructions, even with a long run on every line
/// (hex-heavy-log-256k 1.337 G with it, 1.345 G without; 256 KiB of dense
/// lines 269 M vs 271 M), because building it reads each byte about once and
/// iterating it is cheaper than walking the lines. A sampling guard measured
/// +1% on those inputs, so it was dropped.
pub(super) fn long_run_lines(input: &str) -> Option<Rc<[(usize, usize)]>> {
    ACTIVE_SCAN
        .try_with(|cell| {
            let mut current = cell.try_borrow_mut().ok()?;
            let active = current.as_mut()?;
            if active.start != input.as_ptr() as usize || active.len != input.len() {
                return None;
            }
            #[cfg(test)]
            if FORCE_BYPASS_LONG_RUN_INDEX.with(std::cell::Cell::get) {
                return None;
            }
            let spans = active.long_run_lines.get_or_insert_with(|| {
                let spans = super::text::long_run_line_spans(input);
                // Every skipped line must hold no long run, so a detector
                // skipping it would have found nothing.
                #[cfg(debug_assertions)]
                assert_eq!(spans, super::text::long_run_line_spans_by_line(input));
                spans.into()
            });
            Some(Rc::clone(spans))
        })
        .ok()
        .flatten()
}

/// The provider-word bit set of `input` (`build` over its bytes), when
/// `input` is the scan copy of the innermost [`ScanScope`] on this thread;
/// built once per scan copy and shared by the keyword-gated detectors.
/// `None` in every other case, where the caller scans for itself.
pub(super) fn provider_word_mask(input: &str, build: fn(&[u8]) -> u8) -> Option<u8> {
    ACTIVE_SCAN
        .try_with(|cell| {
            let mut current = cell.try_borrow_mut().ok()?;
            let active = current.as_mut()?;
            if active.start != input.as_ptr() as usize || active.len != input.len() {
                return None;
            }
            Some(
                *active
                    .provider_words
                    .get_or_insert_with(|| build(input.as_bytes())),
            )
        })
        .ok()
        .flatten()
}

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

    #[test]
    fn a_literal_is_possible_exactly_when_all_its_pairs_occur() {
        let pairs = PairSet::of(b"xx re_ yy");
        assert!(pairs.may_contain_literal(b"re_"));
        // `re` alone occurs, `e_` alone occurs, but `ab` does not.
        assert!(!pairs.may_contain_literal(b"ab"));
        let split = PairSet::of(b"re e_");
        // Both pairs occur, apart: a false positive the prefilter allows.
        assert!(split.may_contain_literal(b"re_"));
        let neither = PairSet::of(b"r e _");
        assert!(!neither.may_contain_literal(b"re_"));
    }

    #[test]
    fn every_occurring_literal_is_reported_possible() {
        let text = b"prefix ghp_ AKIA sk-proj- xoxb- .atlasv1. 0: \xff\x00 end";
        let pairs = PairSet::of(text);
        let table = PairTable::of(text);
        for start in 0..text.len() {
            for end in start + 2..=text.len() {
                assert!(pairs.may_contain_literal(&text[start..end]));
                assert!(table.may_contain_literal(&text[start..end]));
            }
        }
    }

    #[test]
    fn the_pair_table_is_exact_for_every_pair() {
        let text = b"ab\xff\x00zz";
        let table = PairTable::of(text);
        for first in 0..=u8::MAX {
            for second in 0..=u8::MAX {
                let occurs = text.windows(2).any(|pair| pair == [first, second]);
                assert_eq!(table.contains(first, second), occurs);
            }
        }
    }

    #[test]
    fn declarations_refuse_short_or_empty_literal_lists() {
        assert!(RequiredLiterals::any_of(&[]).is_none());
        assert!(RequiredLiterals::any_of(&[Literals::Strs(&[])]).is_none());
        assert!(RequiredLiterals::any_of(&[Literals::Strs(&["ghp_", "x"])]).is_none());
        assert!(
            RequiredLiterals::any_of(&[Literals::Strs(&["ghp_"]), Literals::Bytes(&[b"x"])])
                .is_none()
        );
        assert!(RequiredLiterals::any_of(&[Literals::Strs(&["ghp_", "gho_"])]).is_some());
    }

    #[test]
    fn a_declaration_matches_exactly_when_a_literal_occurs() {
        let github = RequiredLiterals::any_of(&[
            Literals::Prefixes(&[("ghp_", "github_token")]),
            Literals::Bytes(&[b"glpat-"]),
        ])
        .unwrap();
        let separator = RequiredLiterals::any_of(&[Literals::Strs(&["0:", "==", "re_"])]).unwrap();
        let matcher =
            LiteralMatcher::compile([None, Some(&github), None, Some(&separator)]).unwrap();
        let present = |text: &[u8]| {
            let found = matcher.present(text);
            (0..4)
                .filter(|&slot| found.contains(slot))
                .collect::<Vec<_>>()
        };
        assert_eq!(present(b"token glpat-abc"), [1]);
        assert_eq!(present(b"plain prose only"), [] as [usize; 0]);
        assert_eq!(present(b""), [] as [usize; 0]);
        assert_eq!(present(b"g"), [] as [usize; 0]);
        // Every pair of `re_` occurs, apart: the pair tests would pass it,
        // the matcher does not.
        assert_eq!(present(b"re e_ glpat"), [] as [usize; 0]);
        assert_eq!(present(b"x==ghp_"), [1, 3]);
        assert_eq!(present(b"ghp"), [] as [usize; 0]);
        assert_eq!(present(b"10:"), [3]);
    }

    #[test]
    fn a_declaration_past_the_slot_limit_compiles_no_matcher() {
        let github = RequiredLiterals::any_of(&[Literals::Strs(&["ghp_"])]).unwrap();
        let mut declarations = vec![None; MAX_SLOTS];
        declarations.push(Some(&github));
        assert!(LiteralMatcher::compile(declarations).is_none());
        let mut within = vec![None; MAX_SLOTS - 1];
        within.push(Some(&github));
        let matcher = LiteralMatcher::compile(within).unwrap();
        assert!(matcher.present(b"ghp_").contains(MAX_SLOTS - 1));
    }

    #[test]
    fn a_ruleset_literal_is_ruled_out_only_for_the_marked_scan_copy() {
        let scanned = String::from("prose with no prefix at all");
        // Outside any scan, nothing is ruled out.
        assert!(ruleset_literal_may_occur(&scanned, b"ACME_"));
        {
            let _scope = ScanScope::enter(&scanned);
            assert!(!ruleset_literal_may_occur(&scanned, b"ACME_"));
            assert!(ruleset_literal_may_occur(&scanned, b"prefix"));
            // A part of the copy, or another string, is not the copy.
            assert!(ruleset_literal_may_occur(&scanned[1..], b"ACME_"));
            assert!(ruleset_literal_may_occur(&scanned[..4], b"ACME_"));
            let other = String::from("prose with no prefix at all");
            assert!(ruleset_literal_may_occur(&other, b"ACME_"));
            {
                // A nested scan marks its own copy, then restores the outer.
                let inner = String::from("ACME_ inner");
                let _inner = ScanScope::enter(&inner);
                assert!(ruleset_literal_may_occur(&inner, b"ACME_"));
                assert!(ruleset_literal_may_occur(&scanned, b"ACME_"));
            }
            assert!(!ruleset_literal_may_occur(&scanned, b"ACME_"));
        }
        assert!(ruleset_literal_may_occur(&scanned, b"ACME_"));
    }

    use crate::detectors::{built_in_entries, common_built_in_entries};
    use crate::registry::DetectorRegistry;
    use crate::types::DetectorContext;
    use crate::{DefaultPolicy, scan};

    /// Short text mixing prose, non-ASCII and invisible code points.
    const SHORT_BASE: &str = "config: value é 漢字 🙂 \u{200B}x\u{FEFF}y end\n";

    /// Over [`EXACT_PAIRS_FROM`] bytes of mixed log, JSON, source and
    /// non-ASCII lines; ASCII digits and colons are common in it, as in
    /// real logs.
    fn long_base() -> String {
        let line = "2026-09-30T12:34:56Z INFO request id=42 path=/v1/items status=200 \
                    {\"name\":\"value\",\"list\":[1,2,3]} fn main() { let x = y; } \
                    naïve café 東京 🙂 \u{200B}zero\u{2060}width\n";
        let text = line.repeat(EXACT_PAIRS_FROM / line.len() + 2);
        assert!(text.len() > EXACT_PAIRS_FROM);
        text
    }

    /// `text` with `inserted` placed at the char boundary at or after
    /// `at`.
    fn insert_at(text: &str, at: usize, inserted: &str) -> String {
        let mut at = at.min(text.len());
        while !text.is_char_boundary(at) {
            at += 1;
        }
        format!("{}{inserted}{}", &text[..at], &text[at..])
    }

    /// Inputs that contain `literal`, lack it by one byte, or split it with
    /// an invisible code point, at the start, middle and end of `base`.
    fn variants(base: &str, literal: &str) -> Vec<String> {
        let value = format!("{literal}SYNTHETIC0123456789abcdefREVOKED0000");
        let mut split = literal.to_owned();
        let second = literal
            .char_indices()
            .nth(1)
            .map_or(literal.len(), |(at, _)| at);
        split.insert(second, '\u{200B}');
        let truncated = &literal[..literal.len() - 1];
        vec![
            format!("{value}{base}"),
            insert_at(base, base.len() / 2, &format!(" {value} ")),
            format!("{base}{literal}"),
            insert_at(base, base.len() / 3, truncated),
            insert_at(base, base.len() / 3, &format!("{split}SYNTHETIC0123456789")),
            format!("{base}{truncated}"),
        ]
    }

    /// Checks one registry's matcher on `text` against a substring oracle,
    /// and that every detector it would skip proposes nothing; then scans
    /// `text`, whose debug-build check covers the normalized scan copy.
    fn check_exact(registry: &DetectorRegistry, text: &str) {
        let matcher = registry.prefilter().unwrap();
        let present = matcher.present(text.as_bytes());
        let context = DetectorContext::new(text.len());
        for (slot, registered) in registry.detectors().iter().enumerate() {
            let Some(required) = registered.required_literals() else {
                assert!(!present.contains(slot));
                continue;
            };
            let occurs = required.literals().iter().any(|literal| {
                text.as_bytes()
                    .windows(literal.len())
                    .any(|window| window == *literal)
            });
            assert_eq!(present.contains(slot), occurs, "{}", registered.id());
            if !occurs {
                assert!(
                    registered
                        .detector()
                        .detect(text, &context)
                        .is_ok_and(|candidates| candidates.is_empty()),
                    "{} would be skipped but proposes a candidate",
                    registered.id()
                );
            }
        }
        let _ = scan(text, registry, &DefaultPolicy);
    }

    /// The compiled matcher finds exactly the detectors with a literal in
    /// the input — never fewer, which would skip an emitting detector, and
    /// never more — on short and long (exact-table-sized) inputs that
    /// contain, lack, truncate, or invisibly split each declared literal.
    #[test]
    fn the_matcher_is_exact_and_never_skips_an_emitting_detector() {
        let long = long_base();
        for registry in [
            DetectorRegistry::with_built_in([]).unwrap(),
            DetectorRegistry::with_common_built_in([]).unwrap(),
        ] {
            let literals: Vec<&str> = registry
                .detectors()
                .iter()
                .filter_map(|registered| registered.required_literals())
                .flat_map(|required| required.literals())
                .map(|literal| std::str::from_utf8(literal).unwrap())
                .collect();
            check_exact(&registry, "");
            check_exact(&registry, SHORT_BASE);
            check_exact(&registry, &long);
            for literal in &literals {
                for text in variants(SHORT_BASE, literal) {
                    check_exact(&registry, &text);
                }
            }
            // Long inputs cost more per check in a debug build; every
            // seventh literal still covers every detector family's shape.
            for literal in literals.iter().step_by(7) {
                for text in variants(&long, literal) {
                    check_exact(&registry, &text);
                }
            }
        }
    }

    /// A ruleset detector returns the same candidates inside a pipeline
    /// scan, where it may rule its prefix out from shared byte pairs, as
    /// outside one, where it always searches.
    #[test]
    fn ruleset_detectors_find_the_same_with_and_without_the_prefilter() {
        let ruleset = "ruleset-revision: 1\n\
                       detector: acme-internal-token\n\
                       specificity: contextual\n\
                       prefix: \"ACME_\"\n\
                       alphabet: alnum-dash\n\
                       run: at-least 12\n\
                       validator: none\n\
                       detector: acme-checksum-token\n\
                       specificity: entropy\n\
                       prefix: \"re_q\"\n\
                       alphabet: alnum\n\
                       run: at-least 8\n\
                       validator: trailing-lower-hex\n\
                       detector: acme-unicode-token\n\
                       specificity: contextual\n\
                       prefix: \"é_k\"\n\
                       alphabet: alnum\n\
                       run: exact 10\n\
                       validator: none\n";
        let detectors = crate::load_ruleset(ruleset.as_bytes()).unwrap();
        let long = long_base();
        for base in [SHORT_BASE, long.as_str()] {
            let _scope = ScanScope::enter(base);
            // The prefilter does rule a prefix out, so the comparison
            // below is not vacuous.
            assert!(!ruleset_literal_may_occur(base, b"ACME_"));
        }
        let mut inputs = vec![String::new(), SHORT_BASE.to_owned(), long.clone()];
        for base in [SHORT_BASE, long.as_str()] {
            for literal in ["ACME_", "re_q", "é_k"] {
                inputs.extend(variants(base, literal));
            }
            // Every pair of `re_q` present, apart.
            inputs.push(format!("{base} re e_ _q"));
        }
        for input in &inputs {
            let context = DetectorContext::new(input.len());
            for detector in &detectors {
                let unfiltered = detector.detect(input, &context).unwrap();
                let filtered = {
                    let _scope = ScanScope::enter(input);
                    detector.detect(input, &context).unwrap()
                };
                assert_eq!(filtered, unfiltered, "{}", detector.id());
            }
        }
        let registry = DetectorRegistry::with_built_in(detectors).unwrap();
        for input in &inputs {
            let _ = scan(input, &registry, &DefaultPolicy);
        }
    }

    #[test]
    fn full_declares_103_detectors_and_common_4() {
        let declared = |entries: Vec<super::super::BuiltIn>| {
            entries
                .iter()
                .filter(|entry| entry.required.is_some())
                .count()
        };
        assert_eq!(declared(built_in_entries()), 103);
        assert_eq!(declared(common_built_in_entries()), 4);
    }

    /// The set of built-ins that declare nothing is exactly the reviewed
    /// list of detectors that cannot name a required case-sensitive literal
    /// (issue #983 research), so a new detector is a deliberate choice.
    #[test]
    fn only_the_reviewed_built_ins_run_on_every_call() {
        let always: Vec<String> = built_in_entries()
            .into_iter()
            .filter(|entry| entry.required.is_none())
            .map(|entry| entry.detector.id().to_owned())
            .collect();
        assert_eq!(
            always,
            [
                "twilio-auth-token",
                "twilio-api-key-secret",
                "discord-bot-token",
                "datadog-api-key",
                "datadog-application-key-legacy",
                "new-relic-license-key",
                "pinecone-api-key",
                "confluent-cloud-api-secret-legacy",
                "heroku-api-key-legacy",
                "travisci-api-token",
                "mistral-api-key",
                "cohere-api-key",
                "ai21-api-key",
                "deepgram-api-key",
                "convex-deployment-key",
                "bearer-token",
                "generic-token",
            ]
        );
    }

    /// Issue #1075: a bare-shape detector handed the scan copy walks only the
    /// lines holding a long run; handed anything else it walks every line.
    /// Both must find exactly the same candidates.
    #[test]
    fn bare_shape_detectors_agree_inside_and_outside_a_scan() {
        use crate::types::{Detector, DetectorContext};
        let detectors: Vec<Box<dyn Detector>> = vec![
            Box::new(super::super::twilio::TwilioAuthTokenDetector),
            Box::new(super::super::twilio::TwilioApiKeySecretDetector),
            Box::new(super::super::datadog::DatadogApiKeyDetector),
            Box::new(super::super::datadog::DatadogApplicationKeyLegacyDetector),
            Box::new(super::super::new_relic::NewRelicLicenseKeyDetector),
            Box::new(super::super::heroku::HerokuApiKeyLegacyDetector),
            Box::new(super::super::confluent::ConfluentLegacyApiSecretDetector),
            Box::new(super::super::pinecone::PineconeApiKeyDetector),
            Box::new(super::super::mailchimp::MailchimpMarketingApiKeyDetector),
        ];
        let hex32 = "0123456789abcdef0123456789abcdef";
        let hex40 = "0123456789abcdef0123456789abcdef01234567";
        let uuid = "01234567-89ab-cdef-0123-456789abcdef";
        let b64 = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789+/AbCdEfGhIjKlMnOpQrStUvWxYz01";
        let alnum32 = "AbCdEfGhIjKlMnOpQrStUvWxYz012345";
        let pool: Vec<String> = vec![
            format!("TWILIO_AUTH_TOKEN={hex32}"),
            format!("AC{hex32} {hex32}"),
            format!("SK{alnum32} {alnum32}"),
            format!("twilio api {alnum32}"),
            "twilio profiles:list --properties authToken".to_owned(),
            "ID     Auth Token".to_owned(),
            format!("pf_one  {hex32}"),
            format!("DD_API_KEY={hex32}"),
            format!("datadog {hex40}"),
            format!("DD_APPLICATION_KEY={hex40}"),
            format!("NEW_RELIC_LICENSE_KEY={hex40}"),
            format!("{}FFFFNRAL", &hex32),
            format!("eu01xx{}FFFFNRAL", &hex32[..26]),
            format!("HEROKU_API_KEY={uuid}"),
            "heroku auth:token".to_owned(),
            format!("machine api.heroku.test password {uuid}"),
            format!("confluent.api.secret={b64}"),
            "schema.registry.url=https://confluent.test".to_owned(),
            format!("basic.auth.user.info=key:{b64}"),
            format!("pinecone_api_key = {uuid}"),
            format!("mailchimp {hex32}-us12"),
            format!("{hex32}-us1"),
            format!("{hex32}-us1.example.test"),
            "short line".to_owned(),
            "0123456789abcdef".to_owned(),
            String::new(),
            "caf\u{e9} \u{597D} 0123".to_owned(),
        ];
        let mut state = 0x9e37_79b9_7f4a_7c15u64;
        let mut next = move || {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            usize::try_from(state >> 33).unwrap()
        };
        let mut found = 0usize;
        for _ in 0..600 {
            let mut input = String::new();
            for _ in 0..=(next() % 9) {
                input.push_str(&pool[next() % pool.len()]);
                input.push_str(["\n", "\r\n", "\r", " ", "\n\n"][next() % 5]);
            }
            let context = DetectorContext::new(input.len());
            for detector in &detectors {
                let outside = detector.detect(&input, &context).unwrap();
                let inside = {
                    let _scope = ScanScope::enter(&input);
                    detector.detect(&input, &context).unwrap()
                };
                assert_eq!(inside, outside, "{} {input:?}", detector.id());
                found += outside.len();
            }
        }
        assert!(found > 500, "the inputs must exercise matches, saw {found}");
    }

    /// Issue #1083: inside a scan copy, with the line index used or forced
    /// off, no bare-shape detector changes a result, on inputs where most
    /// lines hold a long run and on inputs where few do.
    /// Generators stay within 32-bit `usize`.
    #[test]
    fn bare_shape_detectors_agree_with_the_index_built_or_bypassed() {
        struct Reset;
        impl Drop for Reset {
            fn drop(&mut self) {
                FORCE_BYPASS_LONG_RUN_INDEX.with(|force| force.set(false));
            }
        }
        use crate::types::{Detector, DetectorContext};
        let detectors: Vec<Box<dyn Detector>> = vec![
            Box::new(super::super::twilio::TwilioAuthTokenDetector),
            Box::new(super::super::twilio::TwilioApiKeySecretDetector),
            Box::new(super::super::datadog::DatadogApiKeyDetector),
            Box::new(super::super::datadog::DatadogApplicationKeyLegacyDetector),
            Box::new(super::super::new_relic::NewRelicLicenseKeyDetector),
            Box::new(super::super::heroku::HerokuApiKeyLegacyDetector),
            Box::new(super::super::confluent::ConfluentLegacyApiSecretDetector),
            Box::new(super::super::pinecone::PineconeApiKeyDetector),
            Box::new(super::super::mailchimp::MailchimpMarketingApiKeyDetector),
        ];
        let _reset = Reset;
        let hex32 = "0123456789abcdef0123456789abcdef";
        let hex40 = "0123456789abcdef0123456789abcdef01234567";
        let uuid = "01234567-89ab-cdef-0123-456789abcdef";
        let b64 = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789+/AbCdEfGhIjKlMnOpQrStUvWxYz01";
        let alnum32 = "AbCdEfGhIjKlMnOpQrStUvWxYz012345";
        let dense: Vec<String> = vec![
            format!("TWILIO_AUTH_TOKEN={hex32}"),
            format!("AC{hex32} {hex32}"),
            format!("SK{alnum32} {alnum32}"),
            format!("DD_API_KEY={hex32}"),
            format!("datadog {hex40}"),
            format!("NEW_RELIC_LICENSE_KEY={hex40}"),
            format!("{hex32}FFFFNRAL"),
            format!("HEROKU_API_KEY={uuid}"),
            format!("confluent.api.secret={b64}"),
            format!("pinecone_api_key = {uuid}"),
            format!("mailchimp {hex32}-us12"),
            format!("{hex32}-us1.example.test"),
            format!("{hex40}{hex40}"),
        ];
        let sparse: Vec<String> = vec![
            "twilio profiles:list --properties authToken".to_owned(),
            "ID     Auth Token".to_owned(),
            "heroku auth:token".to_owned(),
            "schema.registry.url=https://confluent.test".to_owned(),
            "short line".to_owned(),
            "0123456789abcdef".to_owned(),
            String::new(),
            "caf\u{e9} \u{597D} 0123".to_owned(),
        ];
        let mut state = 0x2545_f491_4f6c_dd1du64;
        let mut next = move || {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            usize::try_from(state >> 33).unwrap()
        };
        let (mut mostly_indexed, mut mostly_skipped, mut found) = (0usize, 0usize, 0usize);
        for round in 0..640 {
            // From every line holding a long run to only a few doing so.
            let lines = 1 + next() % 80;
            let sparse_in_ten = [0, 0, 1, 10][round % 4];
            let mut input = String::new();
            for _ in 0..lines {
                let pool = if next() % 10 < sparse_in_ten {
                    &sparse
                } else {
                    &dense
                };
                input.push_str(&pool[next() % pool.len()]);
                input.push_str(["\n", "\n", "\n", "\r\n", "\r"][next() % 5]);
            }
            if super::super::text::long_run_line_spans_by_line(&input).len() * 2
                > input.lines().count()
            {
                mostly_indexed += 1;
            } else {
                mostly_skipped += 1;
            }
            let context = DetectorContext::new(input.len());
            for detector in &detectors {
                let outside = detector.detect(&input, &context).unwrap();
                for forced in [false, true] {
                    FORCE_BYPASS_LONG_RUN_INDEX.with(|force| force.set(forced));
                    let _scope = ScanScope::enter(&input);
                    let inside = detector.detect(&input, &context).unwrap();
                    assert_eq!(inside, outside, "{} {forced:?} {input:?}", detector.id());
                }
                found += outside.len();
            }
        }
        assert!(
            mostly_indexed > 100 && mostly_skipped > 100,
            "{mostly_indexed}/{mostly_skipped}"
        );
        assert!(
            found > 5000,
            "the inputs must exercise matches, saw {found}"
        );
    }

    #[test]
    fn the_long_run_index_is_only_handed_out_for_the_active_scan_copy() {
        let input = format!("{}\nshort\n", "a".repeat(40));
        assert!(long_run_lines(&input).is_none());
        let _scope = ScanScope::enter(&input);
        assert_eq!(long_run_lines(&input).as_deref(), Some(&[(0, 40)][..]));
        assert!(long_run_lines(&input[..45]).is_none());
        assert!(long_run_lines(&input.clone()).is_none());
    }

    /// The straightforward oracle the lead-group lookup replaced: every
    /// declared literal searched as a substring.
    fn present_oracle(matcher: &LiteralMatcher, text: &[u8]) -> Vec<usize> {
        let mut slots: Vec<usize> = matcher
            .entries
            .iter()
            .filter(|entry| {
                text.windows(entry.literal.len())
                    .any(|window| window == entry.literal)
            })
            .map(|entry| usize::from(entry.slot))
            .collect();
        slots.sort_unstable();
        slots.dedup();
        slots
    }

    fn present_slots(matcher: &LiteralMatcher, text: &[u8]) -> Vec<usize> {
        let found = matcher.present(text);
        (0..MAX_SLOTS)
            .filter(|&slot| found.contains(slot))
            .collect()
    }

    /// The rank-indexed group lookup and the exhausted-group skip agree
    /// with a substring oracle on random text built from shared-lead,
    /// duplicated and truncated literals, non-ASCII and invisible pieces.
    #[test]
    fn lead_group_lookup_matches_a_substring_oracle() {
        use crate::test_rng::XorShift32;
        let a =
            RequiredLiterals::any_of(&[Literals::Strs(&["ghp_", "gho_", "gh", "ab_cd"])]).unwrap();
        let b = RequiredLiterals::any_of(&[Literals::Strs(&["ghp_", "ghx", "zz"])]).unwrap();
        let c = RequiredLiterals::any_of(&[Literals::Strs(&["ab_", "ab_cd", "é!", "\u{200B}x"])])
            .unwrap();
        let matcher = LiteralMatcher::compile([Some(&a), None, Some(&b), Some(&c)]).unwrap();
        let pieces = [
            "g", "gh", "ghp", "ghp_", "gho", "gho_", "ghx", "ab", "ab_", "ab_c", "ab_cd", "zz",
            "z", "é", "é!", "\u{200B}", "x", " ", "~", "_", "漢",
        ];
        let mut rng = XorShift32::new(0x1093);
        for _ in 0..4000 {
            let text = rng.text(&pieces, 12);
            assert_eq!(
                present_slots(&matcher, text.as_bytes()),
                present_oracle(&matcher, text.as_bytes()),
                "{text:?}"
            );
        }
        for registry in [
            DetectorRegistry::with_built_in([]).unwrap(),
            DetectorRegistry::with_common_built_in([]).unwrap(),
        ] {
            let matcher = registry.prefilter().unwrap();
            let literals: Vec<String> = matcher
                .entries
                .iter()
                .map(|entry| String::from_utf8_lossy(entry.literal).into_owned())
                .collect();
            let pieces: Vec<&str> = literals
                .iter()
                .flat_map(|literal| {
                    let cut = literal
                        .char_indices()
                        .nth(2)
                        .map_or(literal.len(), |(i, _)| i);
                    [literal.as_str(), &literal[..cut]]
                })
                .chain(["é", " ", "\u{200B}", "~"])
                .collect();
            for _ in 0..1500 {
                let text = rng.text(&pieces, 10);
                assert_eq!(
                    present_slots(matcher, text.as_bytes()),
                    present_oracle(matcher, text.as_bytes()),
                    "{text:?}"
                );
            }
        }
    }
}