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
//! Tests of the shadow aggregation contract (issue #770). Every value is
//! synthetic.

use super::*;
use crate::detectors::built_in_detectors;
use crate::types::{ByteRange, DetectorContext};

/// Index of `min_entropy_q16` in [`FEATURE_NAMES`].
const MIN_ENTROPY_FEATURE: usize = 6;
/// Index of `alphabet_efficiency_permille` in [`FEATURE_NAMES`].
const ALPHABET_EFFICIENCY_FEATURE: usize = 18;
/// Index of `distinct_ratio_permille` in [`FEATURE_NAMES`].
const DISTINCT_RATIO_FEATURE: usize = 19;

/// Four correlated randomness measurements that all saturate on a
/// random-looking value: the case the halving rule and the cap exist for.
const CORRELATED_RANDOMNESS: [SignalRule; MAX_GROUP_SIGNALS] = [
    SignalRule::FeatureRamp {
        feature: SHANNON_ENTROPY_FEATURE,
        lo: 254_345,
        hi: 313_536,
        max: 60,
    },
    SignalRule::FeatureRamp {
        feature: MIN_ENTROPY_FEATURE,
        lo: 200_000,
        hi: 300_000,
        max: 60,
    },
    SignalRule::FeatureRamp {
        feature: ALPHABET_EFFICIENCY_FEATURE,
        lo: 500,
        hi: 800,
        max: 60,
    },
    SignalRule::FeatureRamp {
        feature: DISTINCT_RATIO_FEATURE,
        lo: 500,
        hi: 900,
        max: 60,
    },
];

/// The product model with `signals` in place of its randomness group and
/// `randomness_cap` as that group's cap.
const fn with_randomness(signals: &'static [SignalRule], randomness_cap: u32) -> AggregationModel {
    let mut model = SHADOW_MODEL;
    model.id = "test-only";
    model.groups[EvidenceGroup::Randomness as usize].signals = signals;
    model.groups[EvidenceGroup::Randomness as usize].cap = randomness_cap;
    model
}

/// Deterministic synthetic text: a xorshift stream over base62.
fn synthetic(seed: u64, len: usize) -> String {
    let alphabet: Vec<char> = ('a'..='z').chain('A'..='Z').chain('0'..='9').collect();
    let mut state = seed | 1;
    (0..len)
        .map(|_| {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            alphabet[usize::try_from(state % 62).unwrap()]
        })
        .collect()
}

/// Random-looking values of several lengths.
fn random_values() -> impl Iterator<Item = String> {
    (1_u64..=120).map(|seed| synthetic(seed, usize::try_from(seed % 60 + 8).unwrap()))
}

fn inputs(value: &str, context: ContextClass) -> ShadowInputs {
    ShadowInputs::of_value(value, context)
}

fn statistical(value: &str, context: ContextClass) -> EvidenceExplanation {
    aggregate(&SHADOW_MODEL, &inputs(value, context))
}

/// Each candidate the built-in detectors emit for `input`, with its value.
fn candidates(input: &str) -> Vec<(Candidate, &str)> {
    let context = DetectorContext::new(input.len());
    built_in_detectors()
        .iter()
        .flat_map(|detector| detector.detect(input, &context).unwrap())
        .map(|candidate| {
            let range = candidate.range();
            (candidate, &input[range.start()..range.end()])
        })
        .collect()
}

/// 20 distinct symbols, each once, no step or copy: residual entropy
/// `log2(20)`, which earns randomness points but alone stays below `low`.
const AMBIGUOUS_VALUE: &str = "k3Z9pQ7vW2mX8nR4tL6y";
/// 32 distinct symbols: Shannon entropy 5 bits, above the ramp's top.
const RANDOM_VALUE: &str = "Q7vK2mZp9LxR4tWb8NcY3hJd6FsG1eUa";

// --- the model and its invariants ---------------------------------------

#[test]
fn the_product_model_satisfies_the_contract_invariants() {
    let model = SHADOW_MODEL;
    assert_eq!(model.violation(), None);
    let bands = model.bands;
    assert!(0 < bands.low && bands.low < bands.medium && bands.medium < bands.high);
    for config in model.groups {
        if !config.group.is_negative() {
            assert!(config.cap < bands.high, "{config:?}");
        }
    }
    let cap = |group: EvidenceGroup| model.groups[group as usize].cap;
    assert!(cap(EvidenceGroup::Randomness) + cap(EvidenceGroup::Lexical) < bands.high);
    assert_eq!(model.groups.map(|config| config.group), EvidenceGroup::ALL);
    assert_eq!(model.feature_schema, FEATURE_SCHEMA_VERSION);
    assert_eq!(
        FEATURE_NAMES[SHANNON_ENTROPY_FEATURE],
        "shannon_entropy_q16"
    );
    assert_eq!(
        FEATURE_NAMES[RESIDUAL_ENTROPY_FEATURE],
        "residual_entropy_q16"
    );
    assert_eq!(FEATURE_NAMES[MIN_ENTROPY_FEATURE], "min_entropy_q16");
    assert_eq!(
        FEATURE_NAMES[ALPHABET_EFFICIENCY_FEATURE],
        "alphabet_efficiency_permille"
    );
    assert_eq!(
        FEATURE_NAMES[DISTINCT_RATIO_FEATURE],
        "distinct_ratio_permille"
    );
}

#[test]
fn the_product_model_is_the_calibrated_selection() {
    // #829, redact-secret-benchmarks PR #437
    // (5823751c16df4776035f5a0bd6f9640f8013a6a7). Changing any value
    // here is a new model identity (ADR section 10).
    let model = SHADOW_MODEL;
    assert_eq!(model.id, "evidence-aggregation/v3");
    let caps = model
        .groups
        .map(|config| (config.group.as_str(), config.cap));
    assert_eq!(
        caps,
        [
            ("randomness", 30),
            ("lexical", 0),
            ("contextual", 50),
            ("validation", 50),
            ("negative", 130),
        ]
    );
    assert_eq!(
        model.groups[0].signals,
        [SignalRule::FeatureRamp {
            feature: RESIDUAL_ENTROPY_FEATURE,
            lo: 234_123,
            hi: 263_562,
            max: 30,
        }]
    );
    assert_eq!(
        model.groups[2].signals,
        [SignalRule::Context {
            classes: &[
                ContextClass::CredentialName,
                ContextClass::AuthorizationHeader,
                ContextClass::UrlUserinfo,
            ],
            points: 50,
        }]
    );
    assert!(model.groups[1].signals.is_empty());
    assert!(model.groups[3].signals.is_empty());
    assert_eq!(
        model.groups[4].signals,
        [SignalRule::StrictExclusion { points: 130 }]
    );
    assert_eq!(
        model.bands,
        BandThresholds {
            low: 35,
            medium: 40,
            high: 51
        }
    );
}

#[test]
fn models_that_break_an_invariant_are_rejected() {
    type Breaker = fn(&mut AggregationModel);
    let broken: [(&str, Breaker); 9] = [
        ("thresholds", |m| m.bands.medium = m.bands.low),
        ("zero low", |m| m.bands.low = 0),
        ("cap at t_high", |m| m.groups[2].cap = m.bands.high),
        ("randomness plus lexical", |m| m.groups[1].cap = 1),
        ("negative cap", |m| m.groups[4].cap = 129),
        ("group order", |m| m.groups.swap(0, 1)),
        ("exclusion outside negative", |m| {
            m.groups[0].signals = &[SignalRule::StrictExclusion { points: 1 }];
        }),
        ("statistics inside negative", |m| {
            m.groups[4].signals = &CORRELATED_RANDOMNESS[..1];
        }),
        ("ramp", |m| {
            m.groups[0].signals = &[SignalRule::FeatureRamp {
                feature: FEATURE_COUNT,
                lo: 0,
                hi: 1,
                max: 1,
            }];
        }),
    ];
    for (name, breaker) in broken {
        let mut model = SHADOW_MODEL;
        breaker(&mut model);
        assert!(model.violation().is_some(), "{name}");
    }
    let mut context_elsewhere = SHADOW_MODEL;
    context_elsewhere.groups[3].signals = context_elsewhere.groups[2].signals;
    assert!(context_elsewhere.violation().is_some());
}

#[test]
fn bands_follow_the_integer_thresholds() {
    let model = SHADOW_MODEL;
    for (total, band) in [
        (0, ShadowBand::None),
        (34, ShadowBand::None),
        (35, ShadowBand::Low),
        (39, ShadowBand::Low),
        (40, ShadowBand::Medium),
        (50, ShadowBand::Medium),
        (51, ShadowBand::High),
        (u32::MAX, ShadowBand::High),
    ] {
        assert_eq!(model.band(total), band, "{total}");
    }
}

#[test]
fn ramp_is_zero_below_capped_above_and_floors_between() {
    assert_eq!(ramp(0, 10, 20, 60), 0);
    assert_eq!(ramp(10, 10, 20, 60), 0);
    assert_eq!(ramp(11, 10, 20, 60), 6);
    assert_eq!(ramp(19, 10, 20, 60), 54);
    assert_eq!(ramp(20, 10, 20, 60), 60);
    assert_eq!(ramp(u32::MAX, 10, 20, 60), 60);
    // floor(60 * (283241 - 254345) / (313536 - 254345)) = floor(29.29...)
    assert_eq!(ramp(283_241, 254_345, 313_536, 60), 29);
    assert_eq!(ramp(u32::MAX - 1, 0, u32::MAX, u32::MAX), u32::MAX - 1);
}

// --- the halving rule -----------------------------------------------------

#[test]
fn halving_follows_the_contract_formula() {
    assert_eq!(halving_sum(&mut [], 100), 0);
    assert_eq!(halving_sum(&mut [60], 100), 60);
    // Sorted descending first: 60 + (40 >> 1) + (10 >> 2).
    assert_eq!(halving_sum(&mut [10, 60, 40], 1000), 60 + 20 + 2);
    // Four equal correlated signals: 60 + 30 + 15 + 7, not 240.
    assert_eq!(halving_sum(&mut [60; 4], 1000), 112);
    assert_eq!(halving_sum(&mut [60; 4], 60), 60);
    // Saturating, and a shift past the width is zero.
    assert_eq!(halving_sum(&mut [u32::MAX, u32::MAX], u32::MAX), u32::MAX);
    let mut many = [1_u32; 40];
    assert_eq!(halving_sum(&mut many, u32::MAX), 1);
}

/// Every vector of up to four contributions from `values`.
fn small_vectors(values: &[u32]) -> Vec<Vec<u32>> {
    let mut vectors = vec![Vec::new()];
    let mut frontier = vec![Vec::new()];
    for _ in 0..MAX_GROUP_SIGNALS {
        let mut next = Vec::new();
        for vector in &frontier {
            for &value in values {
                let mut longer: Vec<u32> = vector.clone();
                longer.push(value);
                next.push(longer);
            }
        }
        vectors.extend(next.iter().cloned());
        frontier = next;
    }
    vectors
}

#[test]
fn halving_is_monotone_in_each_signal_and_in_adding_a_signal() {
    // ADR section 4, exhaustively over small vectors.
    let values = [0, 1, 2, 3, 5, 7, 8, 13, 31, 60];
    for cap in [7, 60, u32::MAX] {
        for vector in small_vectors(&values) {
            let base = halving_sum(&mut vector.clone(), cap);
            assert!(base <= cap);
            for index in 0..vector.len() {
                for raise in [1, 2, 17] {
                    let mut raised = vector.clone();
                    raised[index] += raise;
                    assert!(halving_sum(&mut raised, cap) >= base, "{vector:?}");
                }
            }
            if vector.len() < MAX_GROUP_SIGNALS {
                for &added in &values {
                    let mut extended = vector.clone();
                    extended.push(added);
                    assert!(halving_sum(&mut extended, cap) >= base, "{vector:?}");
                }
            }
        }
    }
}

// --- correlated randomness cannot manufacture `high` ------------------------

#[test]
fn correlated_randomness_signals_do_not_accumulate_linearly() {
    // Test-only model: four correlated randomness signals, each saturating
    // on a random-looking value, with a cap large enough to show the rule.
    let uncapped = with_randomness(&CORRELATED_RANDOMNESS, 50);
    let explanation = aggregate(&uncapped, &inputs(RANDOM_VALUE, ContextClass::Bare));
    let randomness = explanation.groups[EvidenceGroup::Randomness as usize];
    assert_eq!(
        randomness
            .signals()
            .iter()
            .map(|s| s.points)
            .collect::<Vec<_>>(),
        [60, 60, 60, 60]
    );
    // 60 + 30 + 15 + 7 before the cap, not 4 * 60.
    assert_eq!(randomness.uncapped, 112);
    assert_eq!(randomness.contribution, 50);
    assert!(randomness.is_capped());
    assert_eq!(explanation.band, ShadowBand::Medium);
}

#[test]
fn correlated_randomness_never_reaches_high_by_quantity_alone() {
    let models = [
        SHADOW_MODEL,
        with_randomness(&CORRELATED_RANDOMNESS[..1], 30),
        with_randomness(&CORRELATED_RANDOMNESS[..2], 30),
        with_randomness(&CORRELATED_RANDOMNESS[..3], 30),
        with_randomness(&CORRELATED_RANDOMNESS, 30),
    ];
    for model in models {
        assert_eq!(model.violation(), None);
        for value in random_values().chain([RANDOM_VALUE.to_owned()]) {
            for context in [ContextClass::Bare, ContextClass::OtherName] {
                let explanation = aggregate(&model, &inputs(&value, context));
                assert!(explanation.band <= ShadowBand::Medium, "{explanation:?}");
            }
        }
    }
    // The selected product randomness group alone stays below `low`.
    assert_eq!(
        statistical(RANDOM_VALUE, ContextClass::Bare).band,
        ShadowBand::None
    );
}

#[test]
fn adding_a_correlated_signal_never_lowers_the_score() {
    for value in random_values() {
        let mut previous = 0;
        for count in 0..=MAX_GROUP_SIGNALS {
            let model = with_randomness(&CORRELATED_RANDOMNESS[..count], 60);
            let score = aggregate(&model, &inputs(&value, ContextClass::Bare)).score;
            assert!(score >= previous, "{value}: {count}");
            previous = score;
        }
    }
}

// --- independent context strengthens an ambiguous candidate ---------------

#[test]
fn context_plus_randomness_strengthens_an_ambiguous_candidate() {
    let alone = statistical(AMBIGUOUS_VALUE, ContextClass::Bare);
    assert_eq!(alone.band, ShadowBand::None);
    assert_eq!(
        alone.contributing_groups().collect::<Vec<_>>(),
        [EvidenceGroup::Randomness]
    );
    for context in [
        ContextClass::CredentialName,
        ContextClass::AuthorizationHeader,
        ContextClass::UrlUserinfo,
    ] {
        let with_context = statistical(AMBIGUOUS_VALUE, context);
        assert_eq!(with_context.band, ShadowBand::High, "{context:?}");
        assert_eq!(with_context.score, alone.score + 50);
        assert_eq!(
            with_context.contributing_groups().collect::<Vec<_>>(),
            [EvidenceGroup::Randomness, EvidenceGroup::Contextual]
        );
    }
    // Context alone proposes `medium`; `other-name` is not credential-bearing.
    assert_eq!(
        statistical("aaaaaaaaaaaaaaaa", ContextClass::CredentialName).band,
        ShadowBand::Medium
    );
    assert_eq!(
        statistical(AMBIGUOUS_VALUE, ContextClass::OtherName).score,
        alone.score
    );
}

/// The Shannon-entropy ramp `evidence-aggregation/v2` used, for contrast.
const V2_RANDOMNESS: [SignalRule; 1] = [SignalRule::FeatureRamp {
    feature: SHANNON_ENTROPY_FEATURE,
    lo: 226_998,
    hi: 265_935,
    max: 30,
}];

#[test]
fn benign_periodic_and_sequence_values_no_longer_reach_high_in_context() {
    // #829: under the v2 Shannon ramp these values in a credential-bearing
    // context reached `high`; the residual entropy leaves them at the
    // `medium` that context alone proposes.
    let v2 = with_randomness(&V2_RANDOMNESS, 30);
    for benign in [
        "abcdefghijklmnopqrstuvwxyz",
        "0123456789abcdef0123456789abcdef",
        "ZYXWVUTSRQPONMLKJIHGFEDCBA",
        "aAbBcCdDeEfFgGhHiIjJkKlL",
    ] {
        let context = ContextClass::CredentialName;
        assert_eq!(
            aggregate(&v2, &inputs(benign, context)).band,
            ShadowBand::High,
            "{benign}"
        );
        let v3 = statistical(benign, context);
        assert_eq!(v3.band, ShadowBand::Medium, "{benign}");
        assert_eq!(
            v3.groups[EvidenceGroup::Randomness as usize].contribution,
            0
        );
    }
}

#[test]
fn inserted_repetition_does_not_lower_random_material_in_context() {
    // #829: runs of one symbol inserted into random material in a
    // credential-bearing context keep it at `high`.
    for seed in 1_u64..=40 {
        let body = synthetic(seed, 32);
        let symbols: Vec<char> = body.chars().collect();
        let mut reshaped = String::new();
        for chunk in symbols.chunks(4) {
            reshaped.extend(chunk);
            reshaped.push_str("zzzz");
        }
        let original = statistical(&body, ContextClass::CredentialName);
        let after = statistical(&reshaped, ContextClass::CredentialName);
        assert_eq!(original.band, ShadowBand::High, "{seed}");
        assert_eq!(after.band, ShadowBand::High, "{seed}");
    }
}

#[test]
fn a_real_contextual_candidate_is_strengthened_through_its_detector_signals() {
    let input = format!("API_KEY={AMBIGUOUS_VALUE}");
    let found = candidates(&input);
    let (candidate, value) = found
        .iter()
        .find(|(c, _)| c.type_name() == "contextual_secret")
        .unwrap();
    assert_eq!(*value, AMBIGUOUS_VALUE);
    let shadow = shadow_evidence(&SHADOW_MODEL, candidate, value);
    assert_eq!(shadow.authority, ShadowAuthority::Statistical);
    assert_eq!(shadow.band, ShadowBand::High);
    let explanation = shadow.explanation.unwrap();
    assert_eq!(explanation.context, ContextClass::CredentialName);
    // The same value as an entropy-only candidate has no context.
    let bare = Candidate::new("x", Confidence::Medium, candidate.range())
        .with_specificity(Specificity::Entropy);
    assert_eq!(
        shadow_evidence(&SHADOW_MODEL, &bare, value).band,
        ShadowBand::None
    );
}

// --- negative evidence is structurally bounded ------------------------------

#[test]
fn a_full_exclusion_grammar_match_floors_the_score() {
    for value in [
        "{{ secrets.API_KEY }}",
        "${API_KEY}",
        "$(cat /run/secrets/api_key)",
        "<your-api-key>",
        "****************",
        "your-api-key-here",
    ] {
        let explanation = statistical(value, ContextClass::CredentialName);
        assert!(explanation.exclusion.is_some(), "{value}");
        assert_eq!(explanation.negative, 130, "{value}");
        assert_eq!(explanation.score, 0, "{value}");
        assert_eq!(explanation.band, ShadowBand::None, "{value}");
    }
}

#[test]
fn attacker_controlled_lookalikes_do_not_suppress() {
    let lookalikes = [
        format!("EXAMPLE{RANDOM_VALUE}"),
        format!("{RANDOM_VALUE}_placeholder"),
        format!("your-{RANDOM_VALUE}-here"),
        format!("{{{{ x }}}}{RANDOM_VALUE}"),
        format!("{RANDOM_VALUE}{{{{ x }}}}"),
        format!("${{API_KEY}}{RANDOM_VALUE}"),
        format!("{RANDOM_VALUE}$API_KEY"),
        format!("<API_KEY>{RANDOM_VALUE}"),
        format!("****{RANDOM_VALUE}****"),
        format!("$(x){RANDOM_VALUE}"),
        // The product does not treat dotted references as negative: the same
        // shape matches dotted credential grammars.
        "SG.Q7vK2mZp9LxR4tWb.8NcY3hJd6FsG1eUa".to_owned(),
    ];
    for value in &lookalikes {
        let with = statistical(value, ContextClass::CredentialName);
        assert_eq!(with.exclusion, None, "{value}");
        assert_eq!(with.negative, 0, "{value}");
        // The band comes from the positive groups alone.
        assert_eq!(with.score, with.positive, "{value}");
        assert_eq!(with.band, ShadowBand::High, "{value}");
    }
}

// --- deterministic authority ------------------------------------------------

#[test]
fn deterministic_candidates_are_invariant_under_statistical_manipulation() {
    let range = ByteRange::new(0, 1).unwrap();
    let manipulations = [
        RANDOM_VALUE.to_owned(),
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
        "{{ secrets.API_KEY }}".to_owned(),
        "your-api-key-here".to_owned(),
        "****************".to_owned(),
        "k3Z9".repeat(64),
        String::new(),
    ];
    for specificity in [
        Specificity::PrivateKey,
        Specificity::Provider,
        Specificity::Structural,
    ] {
        for confidence in [Confidence::Low, Confidence::Medium, Confidence::High] {
            let candidate = Candidate::new("authoritative", confidence, range)
                .with_specificity(specificity)
                .with_signals(["high-signal-name"]);
            for value in &manipulations {
                let shadow = shadow_evidence(&SHADOW_MODEL, &candidate, value);
                assert_eq!(
                    shadow,
                    ShadowEvidence {
                        authority: ShadowAuthority::Deterministic,
                        band: ShadowBand::of_confidence(confidence),
                        explanation: None,
                    }
                );
            }
        }
    }
}

#[test]
fn real_detector_candidates_keep_their_authority_and_are_not_changed() {
    // Synthetic inputs across detector classes: structural (Authorization,
    // Bearer, connection URL), private-key and contextual shapes.
    let inputs = [
        "Authorization: Basic ZmFrZXVzZXI6ZmFrZXBhc3N3b3Jk".to_owned(),
        format!("Authorization: Bearer {RANDOM_VALUE}"),
        "postgres://fakeuser:Zp9LxR4tWb8NcY3h@db.example.invalid:5432/app".to_owned(),
        format!("API_KEY={AMBIGUOUS_VALUE}"),
        format!("password = \"{RANDOM_VALUE}\""),
        [
            "-----BEGIN PRIVATE KEY-----",
            "U1lOVEhFVElDX1JFVk9LRURfTk8=",
            "-----END PRIVATE KEY-----",
        ]
        .join("\n"),
    ];
    let mut seen_deterministic = 0;
    let mut seen_statistical = 0;
    for input in &inputs {
        for (candidate, value) in candidates(input) {
            let before = candidate.clone();
            let first = shadow_evidence(&SHADOW_MODEL, &candidate, value);
            let second = shadow_evidence(&SHADOW_MODEL, &candidate, value);
            assert_eq!(first, second, "deterministic");
            assert_eq!(candidate, before, "the candidate is untouched");
            match candidate.effective_specificity() {
                Specificity::PrivateKey | Specificity::Provider | Specificity::Structural => {
                    seen_deterministic += 1;
                    assert_eq!(first.authority, ShadowAuthority::Deterministic);
                    assert_eq!(
                        first.band,
                        ShadowBand::of_confidence(candidate.confidence())
                    );
                    assert!(first.explanation.is_none());
                }
                Specificity::Contextual | Specificity::Entropy => {
                    seen_statistical += 1;
                    assert_eq!(first.authority, ShadowAuthority::Statistical);
                    assert!(first.explanation.is_some());
                }
            }
        }
    }
    assert!(seen_deterministic >= 4, "{seen_deterministic}");
    assert!(seen_statistical >= 2, "{seen_statistical}");
}

// --- monotonicity (ADR section 4) ---------------------------------------------

#[test]
fn raising_a_positive_signal_never_lowers_the_score() {
    let base = inputs(RANDOM_VALUE, ContextClass::Bare);
    let mut previous = 0;
    for entropy in (0..=400_000).step_by(997) {
        let mut raised = base;
        raised.features.residual.entropy_q16 = entropy;
        let score = aggregate(&SHADOW_MODEL, &raised).score;
        assert!(score >= previous, "{entropy}");
        previous = score;
    }
    for value in random_values() {
        let bare_score = statistical(&value, ContextClass::Bare).score;
        let credential_score = statistical(&value, ContextClass::CredentialName).score;
        assert!(credential_score >= bare_score, "{value}");
    }
}

#[test]
fn raising_the_negative_contribution_never_raises_the_score() {
    for value in random_values() {
        for context in [ContextClass::Bare, ContextClass::CredentialName] {
            let without = inputs(&value, context);
            let mut with = without;
            with.exclusion = Some(ExclusionGrammar::Mask);
            assert!(
                aggregate(&SHADOW_MODEL, &with).score <= aggregate(&SHADOW_MODEL, &without).score
            );
        }
    }
}

#[test]
fn the_band_is_a_non_decreasing_step_function_of_the_score() {
    let model = SHADOW_MODEL;
    for total in 0..=500 {
        assert!(model.band(total) <= model.band(total + 1), "{total}");
    }
}

// --- explanation ----------------------------------------------------------------

#[test]
fn every_band_is_explained_by_its_groups() {
    let contexts = [
        ContextClass::Bare,
        ContextClass::OtherName,
        ContextClass::CredentialName,
    ];
    let values =
        random_values().chain([AMBIGUOUS_VALUE, "{{ x }}", "aaaaaaaa", ""].map(str::to_owned));
    for value in values {
        for context in contexts {
            let explanation = statistical(&value, context);
            let mut positive = 0;
            let mut negative = 0;
            for (group, config) in explanation.groups.iter().zip(SHADOW_MODEL.groups) {
                assert_eq!(group.group, config.group);
                let mut points: Vec<u32> = group.signals().iter().map(|s| s.points).collect();
                assert_eq!(group.uncapped, halving_sum(&mut points, u32::MAX));
                assert_eq!(group.contribution, group.uncapped.min(config.cap));
                if group.group.is_negative() {
                    negative += group.contribution;
                } else {
                    positive += group.contribution;
                }
            }
            assert_eq!(explanation.positive, positive);
            assert_eq!(explanation.negative, negative);
            assert_eq!(explanation.score, positive.saturating_sub(negative));
            assert_eq!(explanation.band, SHADOW_MODEL.band(explanation.score));
            assert_eq!(explanation.model, SHADOW_MODEL.id);
            assert_eq!(explanation.context, context);
            assert_eq!(explanation.band == ShadowBand::None, explanation.score < 35);
        }
    }
}

#[test]
fn explanations_carry_no_part_of_the_value() {
    let value = "SYNTHETICvalueMARKERq7Zk";
    let range = ByteRange::new(0, value.len()).unwrap();
    let candidate = Candidate::new("contextual_secret", Confidence::High, range)
        .with_specificity(Specificity::Contextual)
        .with_signals(["high-signal-name"]);
    let shadow = shadow_evidence(&SHADOW_MODEL, &candidate, value);
    let rendered = format!("{shadow:?}");
    for fragment in ["SYNTHETIC", "MARKER", "value", "q7Zk"] {
        assert!(!rendered.contains(fragment), "{rendered}");
    }
    let signals: Vec<&str> = shadow
        .explanation
        .unwrap()
        .groups
        .iter()
        .flat_map(|group| group.signals().iter().map(|s| s.signal))
        .collect();
    assert_eq!(
        signals,
        [
            "residual_entropy_q16",
            "credential-context",
            "strict-exclusion"
        ]
    );
}