provenant-cli 0.0.28

Rust-based ScanCode-compatible scanner for licenses, package metadata, SBOMs, and provenance data.
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
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

//! License Detection Engine

pub mod aho_match;
pub mod automaton;
pub mod build_policy;
pub mod dataset;
pub(crate) mod detection;
pub mod embedded;
pub mod license_cache;
mod position_set;
mod token_multiset;
mod token_set;

#[cfg(test)]
mod embedded_test;
pub mod expression;
#[cfg(feature = "golden-tests")]
pub mod golden_utils;
pub mod hash_match;
pub mod index;
mod match_refine;
pub mod models;
pub mod query;
pub mod rules;
pub mod seq_match;
pub mod spdx_lid;
pub mod spdx_mapping;
#[cfg(test)]
mod test_utils;
pub mod tokenize;
pub mod unknown_match;

use bit_set::BitSet;
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::sync::Arc;
use std::time::Instant;

use anyhow::Result;

use crate::license_detection::build_policy::EMBEDDED_LICENSE_INDEX_SOURCE;
use crate::license_detection::dataset::{
    CUSTOM_LICENSE_DATASET_SOURCE, LoadedLicenseDataset, compute_dataset_fingerprint_string,
    load_license_dataset_from_root,
};
use crate::license_detection::embedded::index::{
    load_embedded_artifact_metadata_from_bytes, load_loader_snapshot_from_bytes,
};
use crate::license_detection::index::build_index_from_loaded;
use crate::license_detection::license_cache::{
    LicenseCacheConfig, LicenseCacheNamespace, cache_file_size, compute_artifact_fingerprint,
    compute_rules_fingerprint, delete_cache, load_cached_index, save_cached_index,
};
use crate::license_detection::query::Query;
use crate::license_detection::spdx_mapping::{SpdxMapping, build_spdx_mapping};
use crate::models::LicenseIndexProvenance;
use crate::utils::text::strip_utf8_bom_str;

use crate::license_detection::detection::{
    attach_source_path_to_detections, empty_detection, populate_detection_from_group_with_spdx,
    split_groups_across_frontmatter_boundary,
};
use crate::license_detection::models::MatcherKind;

/// Path to the license rules directory in the reference scancode-toolkit submodule.
/// Used by test code and the xtask generate-license-loader-artifact binary.
#[allow(dead_code)]
pub const SCANCODE_LICENSES_RULES_PATH: &str =
    "reference/scancode-toolkit/src/licensedcode/data/rules";

/// Path to the licenses directory in the reference scancode-toolkit submodule.
/// Used by test code and the xtask generate-license-loader-artifact binary.
#[allow(dead_code)]
pub const SCANCODE_LICENSES_LICENSES_PATH: &str =
    "reference/scancode-toolkit/src/licensedcode/data/licenses";

/// Path to the license data directory in the reference scancode-toolkit submodule.
/// Used by test code and the xtask generate-license-loader-artifact binary.
#[allow(dead_code)]
pub const SCANCODE_LICENSES_DATA_PATH: &str = "reference/scancode-toolkit/src/licensedcode/data";

pub const DEFAULT_LICENSEDB_URL_TEMPLATE: &str = "https://scancode-licensedb.aboutcode.org/{}";
pub(crate) const LICENSE_DETECTION_TIMEOUT_MESSAGE: &str = "license detection timed out";

pub(crate) use detection::{
    LicenseDetection, group_matches_by_region, post_process_detections, sort_matches_by_line,
};
pub use models::LicenseMatch;

pub use aho_match::aho_match;
pub use hash_match::hash_match;
pub use match_refine::{
    filter_invalid_contained_unknown_matches, merge_overlapping_matches, refine_matches,
    refine_matches_without_false_positive_filter, split_weak_matches,
};
pub use position_set::PositionSet;
pub use spdx_lid::spdx_lid_match;
pub use token_multiset::TokenMultiset;
pub use token_set::TokenSet;
pub use unknown_match::unknown_match;

use self::seq_match::{
    MAX_NEAR_DUPE_CANDIDATES, select_seq_candidates_with_deadline,
    seq_match_with_candidates_and_deadline,
};

/// License detection engine that orchestrates the detection pipeline.
///
/// The engine loads license rules and builds an index for efficient matching.
/// It supports multiple matching strategies (hash, SPDX-LID, Aho-Corasick, sequence)
/// and combines their results into final license detections.
#[derive(Debug, Clone)]
pub struct LicenseDetectionEngine {
    index: Arc<index::LicenseIndex>,
    spdx_mapping: SpdxMapping,
    spdx_license_list_version: Option<String>,
    license_index_provenance: Option<LicenseIndexProvenance>,
}

const MAX_DETECTION_SIZE: usize = 10 * 1024 * 1024; // 10MB
const MAX_REGULAR_SEQ_CANDIDATES: usize = 70;
const MAX_REDUNDANT_SEQ_CONTAINER_BOUNDARY_GAP: usize = 8;
const MAX_REDUNDANT_SEQ_CONTAINER_UNMATCHED_GAP: usize = 2;

pub(crate) fn deadline_exceeded(deadline: Option<Instant>) -> bool {
    deadline.is_some_and(|deadline| Instant::now() >= deadline)
}

pub(crate) fn ensure_within_deadline(deadline: Option<Instant>) -> Result<()> {
    if deadline_exceeded(deadline) {
        Err(anyhow::anyhow!(LICENSE_DETECTION_TIMEOUT_MESSAGE))
    } else {
        Ok(())
    }
}

fn truncate_detection_text(clean_text: &str) -> &str {
    if clean_text.len() <= MAX_DETECTION_SIZE {
        return clean_text;
    }

    log::debug!(
        "Content size {} exceeds limit {}, truncating for detection",
        clean_text.len(),
        MAX_DETECTION_SIZE
    );

    let boundary = clean_text.floor_char_boundary(MAX_DETECTION_SIZE);
    &clean_text[..boundary]
}

fn query_span_for_match(m: &LicenseMatch) -> Option<models::PositionSpan> {
    (!m.query_span().is_empty()).then(|| m.query_span().clone())
}

fn has_full_match_coverage(m: &LicenseMatch) -> bool {
    m.coverage() == 100.0
}

fn is_redundant_same_expression_seq_container(
    container: &LicenseMatch,
    candidate_contained_matches: &[LicenseMatch],
) -> bool {
    let container_is_redundant_coverage =
        has_full_match_coverage(container) || container.coverage() >= 99.0;
    if container.matcher != MatcherKind::Seq || !container_is_redundant_coverage {
        return false;
    }

    let container_qspan_set = container.qspan_set();

    let mut contained: Vec<&LicenseMatch> = candidate_contained_matches
        .iter()
        .filter(|m| {
            m.matcher == MatcherKind::Aho
                && has_full_match_coverage(m)
                && m.license_expression == container.license_expression
                && m.overlaps_with(&container_qspan_set)
        })
        .collect();

    if contained.len() < 2 {
        return false;
    }

    let material_children = contained.iter().filter(|m| m.matched_length > 1).count();
    if material_children < 2 {
        return false;
    }

    contained.sort_by_key(|m| m.qspan_bounds());

    let mut child_union = PositionSet::new();
    for m in &contained {
        child_union.extend_from_span(m.query_span());
    }

    let container_only_positions = container_qspan_set.difference(&child_union);
    let child_only_positions = child_union.difference(&container_qspan_set);

    let mut bridge_positions = BitSet::new();
    for pair in contained.windows(2) {
        let (_, previous_end) = pair[0].qspan_bounds();
        let (next_start, _) = pair[1].qspan_bounds();

        if next_start < previous_end {
            return false;
        }

        for pos in previous_end..next_start {
            bridge_positions.insert(pos);
        }
    }

    let container_only_boundary_positions = container_only_positions
        .iter()
        .filter(|&pos| !bridge_positions.contains(pos))
        .count();

    if container_only_positions.len() == 1
        && container_only_boundary_positions == 0
        && child_only_positions.is_empty()
    {
        return false;
    }

    if child_only_positions.is_empty()
        && container_only_positions.len() == container_only_boundary_positions
        && container_only_boundary_positions <= 3
    {
        let earliest_child = contained
            .iter()
            .map(|m| m.qspan_bounds().0)
            .min()
            .unwrap_or(usize::MAX);
        let latest_child = contained
            .iter()
            .map(|m| m.qspan_bounds().1.saturating_sub(1))
            .max()
            .unwrap_or(0);

        let is_one_sided_boundary = container_only_positions
            .iter()
            .all(|pos| pos < earliest_child)
            || container_only_positions
                .iter()
                .all(|pos| pos > latest_child);

        if is_one_sided_boundary {
            return false;
        }
    }

    let max_container_only_positions =
        MAX_REDUNDANT_SEQ_CONTAINER_BOUNDARY_GAP * contained.len() + 1;
    let max_container_boundary_positions =
        MAX_REDUNDANT_SEQ_CONTAINER_BOUNDARY_GAP * (contained.len() - 1);
    let max_child_only_positions = MAX_REDUNDANT_SEQ_CONTAINER_UNMATCHED_GAP + 1;

    container_only_positions.len() <= max_container_only_positions
        && container_only_boundary_positions <= max_container_boundary_positions
        && child_only_positions.len() <= max_child_only_positions
}

fn filter_redundant_same_expression_seq_containers(
    seq_matches: Vec<LicenseMatch>,
    candidate_contained_matches: &[LicenseMatch],
) -> Vec<LicenseMatch> {
    seq_matches
        .into_iter()
        .filter(|m| !is_redundant_same_expression_seq_container(m, candidate_contained_matches))
        .collect()
}

fn is_redundant_low_coverage_composite_seq_wrapper(
    container: &LicenseMatch,
    candidate_contained_matches: &[LicenseMatch],
) -> bool {
    if container.matcher != seq_match::MATCH_SEQ || container.coverage() >= 30.0 {
        return false;
    }

    let container_qspan_set = container.qspan_set();

    let children: Vec<&LicenseMatch> = candidate_contained_matches
        .iter()
        .filter(|m| {
            m.matcher == aho_match::MATCH_AHO
                && has_full_match_coverage(m)
                && m.license_expression != container.license_expression
                && m.overlaps_with(&container_qspan_set)
        })
        .collect();

    if children.len() < 2 {
        return false;
    }

    let unique_expressions: HashSet<&str> = children
        .iter()
        .map(|m| m.license_expression.as_str())
        .collect();
    if unique_expressions.len() < 2 {
        return false;
    }

    let mut child_union = PositionSet::new();
    for m in &children {
        child_union.extend_from_span(m.query_span());
    }

    let container_only_positions = container_qspan_set.difference(&child_union);
    let child_only_positions = child_union.difference(&container_qspan_set);

    let mut sorted_children = children;
    sorted_children.sort_by_key(|m| m.qspan_bounds());

    let mut bridge_positions = BitSet::new();
    for pair in sorted_children.windows(2) {
        let (_, previous_end) = pair[0].qspan_bounds();
        let (next_start, _) = pair[1].qspan_bounds();
        for pos in previous_end..next_start {
            bridge_positions.insert(pos);
        }
    }

    let container_only_boundary_positions = container_only_positions
        .iter()
        .filter(|&pos| !bridge_positions.contains(pos))
        .count();

    child_only_positions.is_empty()
        && container_only_positions.len() <= MAX_REDUNDANT_SEQ_CONTAINER_BOUNDARY_GAP
        && container_only_boundary_positions <= MAX_REDUNDANT_SEQ_CONTAINER_BOUNDARY_GAP
}

fn filter_redundant_low_coverage_composite_seq_wrappers(
    seq_matches: Vec<LicenseMatch>,
    candidate_contained_matches: &[LicenseMatch],
) -> Vec<LicenseMatch> {
    seq_matches
        .into_iter()
        .filter(|m| {
            !is_redundant_low_coverage_composite_seq_wrapper(m, candidate_contained_matches)
        })
        .collect()
}

fn subtract_spdx_match_qspans(
    query: &mut Query<'_>,
    matched_qspans: &mut Vec<models::PositionSpan>,
    aho_extra_matchables: &mut PositionSet,
    spdx_matches: &[LicenseMatch],
) {
    for m in spdx_matches {
        let Some(span) = query_span_for_match(m) else {
            continue;
        };

        aho_extra_matchables.extend_from_span(&span);
        query.subtract(&span);

        if has_full_match_coverage(m) {
            matched_qspans.push(span);
        }
    }
}

fn merge_and_prepare_aho_matches(
    index: &index::LicenseIndex,
    query: &mut Query<'_>,
    matched_qspans: &mut Vec<models::PositionSpan>,
    refined_aho: &[LicenseMatch],
) -> (Vec<LicenseMatch>, bool) {
    let merged_aho = merge_overlapping_matches(refined_aho);
    let mut saw_long_exact_license_text_match = false;

    for m in &merged_aho {
        let Some(span) = query_span_for_match(m) else {
            continue;
        };

        if has_full_match_coverage(m) {
            matched_qspans.push(span.clone());
        }

        if index
            .rules_by_rid
            .get(m.rid)
            .is_some_and(|rule| rule.is_license_text())
            && m.rule_length > 120
            && m.coverage() > 98.0
        {
            query.subtract(&span);
            saw_long_exact_license_text_match = true;
        }
    }

    (merged_aho, saw_long_exact_license_text_match)
}

fn collect_whole_query_exact_followup_matches(
    index: &index::LicenseIndex,
    query: &mut Query<'_>,
    matched_qspans: &mut Vec<models::PositionSpan>,
    whole_run: &query::QueryRun<'_>,
    deadline: Option<Instant>,
) -> Result<Vec<LicenseMatch>> {
    let mut seq_all_matches = Vec::new();

    if whole_run.is_matchable(false, matched_qspans) {
        let near_dupe_candidates = if deadline.is_some() {
            select_seq_candidates_with_deadline(
                index,
                whole_run,
                true,
                MAX_NEAR_DUPE_CANDIDATES,
                deadline,
            )?
        } else {
            self::seq_match::select_seq_candidates(index, whole_run, true, MAX_NEAR_DUPE_CANDIDATES)
        };

        if !near_dupe_candidates.is_empty() {
            let near_dupe_matches = if deadline.is_some() {
                seq_match_with_candidates_and_deadline(
                    index,
                    whole_run,
                    &near_dupe_candidates,
                    deadline,
                )?
            } else {
                self::seq_match::seq_match_with_candidates(index, whole_run, &near_dupe_candidates)
            };

            for m in &near_dupe_matches {
                if !m.query_span().is_empty() {
                    let span = m.query_span().clone();
                    query.subtract(&span);
                    matched_qspans.push(span);
                }
            }

            seq_all_matches.extend(near_dupe_matches);
        }
    }

    Ok(seq_all_matches)
}

fn collect_regular_seq_matches(
    index: &index::LicenseIndex,
    query: &Query<'_>,
    matched_qspans: &[models::PositionSpan],
    candidate_contained_matches: &[LicenseMatch],
    deadline: Option<Instant>,
) -> Result<Vec<LicenseMatch>> {
    let mut seq_all_matches = Vec::new();

    for (query_run_index, query_run) in query.query_runs().into_iter().enumerate() {
        if query_run_index % 8 == 0 {
            ensure_within_deadline(deadline)?;
        }

        if !query_run.is_matchable(false, matched_qspans) {
            continue;
        }

        let candidates = if deadline.is_some() {
            select_seq_candidates_with_deadline(
                index,
                &query_run,
                false,
                MAX_REGULAR_SEQ_CANDIDATES,
                deadline,
            )?
        } else {
            self::seq_match::select_seq_candidates(
                index,
                &query_run,
                false,
                MAX_REGULAR_SEQ_CANDIDATES,
            )
        };
        if !candidates.is_empty() {
            let matches = if deadline.is_some() {
                seq_match_with_candidates_and_deadline(index, &query_run, &candidates, deadline)?
            } else {
                self::seq_match::seq_match_with_candidates(index, &query_run, &candidates)
            };
            seq_all_matches.extend(matches);
        }
    }

    let merged_seq = merge_overlapping_matches(&seq_all_matches);
    let filtered_same_expression =
        filter_redundant_same_expression_seq_containers(merged_seq, candidate_contained_matches);
    Ok(filter_redundant_low_coverage_composite_seq_wrappers(
        filtered_same_expression,
        candidate_contained_matches,
    ))
}

impl LicenseDetectionEngine {
    /// Create a new license detection engine from a pre-built license index.
    ///
    /// This is an internal constructor used by `from_directory()` and `from_embedded()`.
    /// It builds the SPDX mapping from the licenses in the index.
    fn from_index(
        index: index::LicenseIndex,
        spdx_license_list_version: Option<String>,
        license_index_provenance: Option<LicenseIndexProvenance>,
    ) -> Result<Self> {
        let mut license_vec: Vec<_> = index.licenses_by_key.values().cloned().collect();
        license_vec.sort_by(|a, b| a.key.cmp(&b.key));
        let spdx_mapping = build_spdx_mapping(&license_vec);

        Ok(Self {
            index: Arc::new(index),
            spdx_mapping,
            spdx_license_list_version,
            license_index_provenance,
        })
    }

    #[cfg(test)]
    pub(crate) fn from_test_index(index: index::LicenseIndex) -> Self {
        Self::from_index(index, None, None).expect("test index should build license engine")
    }

    /// Create a new license detection engine from the embedded license index.
    ///
    /// Convenience method that uses the default Provenant cache root and does
    /// not force a reindex.
    pub fn from_embedded() -> Result<Self> {
        let cache_config =
            LicenseCacheConfig::new(LicenseCacheConfig::default_root_dir(), false, true);
        Self::from_embedded_with_cache(&cache_config)
    }

    /// Create a new license detection engine from the embedded license index.
    ///
    /// This method loads the build-time embedded license artifact and constructs
    /// the runtime license index. This eliminates the runtime dependency on the
    /// ScanCode rules directory.
    ///
    /// If a valid cache exists (matching fingerprint), the index is loaded from
    /// the rkyv cache file instead of being rebuilt from scratch.
    ///
    /// # Arguments
    /// * `cache_config` - Cache configuration (directory and reindex flag)
    ///
    /// # Returns
    /// A Result containing the engine or an error
    pub fn from_embedded_with_cache(cache_config: &LicenseCacheConfig) -> Result<Self> {
        let artifact_bytes = include_bytes!("../../resources/license_detection/license_index.zst");
        let fingerprint = compute_artifact_fingerprint(artifact_bytes);
        let artifact_metadata = load_embedded_artifact_metadata_from_bytes(artifact_bytes)
            .map_err(|e| {
                anyhow::anyhow!("Failed to load embedded license artifact metadata: {}", e)
            })?;
        debug_assert_eq!(
            artifact_metadata.license_index_provenance.source,
            EMBEDDED_LICENSE_INDEX_SOURCE
        );
        let spdx_version = Some(artifact_metadata.spdx_license_list_version.clone());
        let provenance = Some(artifact_metadata.license_index_provenance.clone());

        if !cache_config.reindex {
            if let Some(cached) =
                load_cached_index(cache_config, LicenseCacheNamespace::Embedded, &fingerprint)?
            {
                let start = Instant::now();
                eprintln!(
                    "License index loaded from rkyv cache in {:.2}s",
                    start.elapsed().as_secs_f64()
                );
                return Self::from_index(cached, spdx_version, provenance);
            }
        } else {
            delete_cache(cache_config, LicenseCacheNamespace::Embedded, &fingerprint)?;
        }

        let snapshot = load_loader_snapshot_from_bytes(artifact_bytes)
            .map_err(|e| anyhow::anyhow!("Failed to load embedded license index: {}", e))?;
        let spdx_version = Some(snapshot.metadata.spdx_license_list_version.clone());
        let provenance = Some(snapshot.metadata.license_index_provenance.clone());

        let start = Instant::now();
        let index = build_index_from_loaded(snapshot.rules, snapshot.licenses, false);
        eprintln!(
            "License index built from embedded artifact in {:.2}s",
            start.elapsed().as_secs_f64()
        );

        let mut index = index;
        index.spdx_license_list_version = spdx_version.clone();
        if let Err(e) = save_cached_index(
            cache_config,
            LicenseCacheNamespace::Embedded,
            &index,
            &fingerprint,
        ) {
            eprintln!("Warning: failed to save license index cache: {}", e);
        } else if let Some(size) =
            cache_file_size(cache_config, LicenseCacheNamespace::Embedded, &fingerprint)
        {
            eprintln!(
                "License index cache saved ({:.1} MB)",
                size as f64 / 1_048_576.0
            );
        }

        Self::from_index(index, spdx_version, provenance)
    }

    /// Create a new license detection engine from a license dataset root.
    ///
    /// Convenience method that uses the default Provenant cache root and does
    /// not force a reindex.
    pub fn from_directory(rules_path: &Path) -> Result<Self> {
        let cache_config =
            LicenseCacheConfig::new(LicenseCacheConfig::default_root_dir(), false, true);
        Self::from_directory_with_cache(rules_path, &cache_config)
    }

    /// Create a new license detection engine from a directory of license rules.
    ///
    /// If a valid cache exists (matching fingerprint of the dataset), the index is
    /// loaded from the rkyv cache file instead of being rebuilt from scratch.
    ///
    /// # Arguments
    /// * `rules_path` - Path to dataset root containing rules/ and licenses/
    /// * `cache_config` - Cache configuration (directory and reindex flag)
    ///
    /// # Returns
    /// A Result containing the engine or an error
    pub fn from_directory_with_cache(
        rules_path: &Path,
        cache_config: &LicenseCacheConfig,
    ) -> Result<Self> {
        let LoadedLicenseDataset {
            manifest,
            rules: loaded_rules,
            licenses: loaded_licenses,
        } = load_license_dataset_from_root(rules_path)?;

        let fingerprint = compute_rules_fingerprint(&loaded_rules, &loaded_licenses)?;
        let provenance = Some(LicenseIndexProvenance {
            source: CUSTOM_LICENSE_DATASET_SOURCE.to_string(),
            dataset_fingerprint: compute_dataset_fingerprint_string(
                &loaded_rules,
                &loaded_licenses,
            )?,
            ignored_rules: vec![],
            ignored_licenses: vec![],
            ignored_rules_due_to_licenses: vec![],
            added_rules: vec![],
            replaced_rules: vec![],
            added_licenses: vec![],
            replaced_licenses: vec![],
        });

        if !cache_config.reindex {
            if let Some(cached) = load_cached_index(
                cache_config,
                LicenseCacheNamespace::CustomRules,
                &fingerprint,
            )? {
                let start = Instant::now();
                eprintln!(
                    "License index loaded from rkyv cache in {:.2}s",
                    start.elapsed().as_secs_f64()
                );
                return Self::from_index(
                    cached,
                    Some(manifest.spdx_license_list_version),
                    provenance,
                );
            }
        } else {
            delete_cache(
                cache_config,
                LicenseCacheNamespace::CustomRules,
                &fingerprint,
            )?;
        }

        let start = Instant::now();
        let index = build_index_from_loaded(loaded_rules, loaded_licenses, false);
        eprintln!(
            "License index built from custom dataset in {:.2}s",
            start.elapsed().as_secs_f64()
        );

        if let Err(e) = save_cached_index(
            cache_config,
            LicenseCacheNamespace::CustomRules,
            &index,
            &fingerprint,
        ) {
            eprintln!("Warning: failed to save license index cache: {}", e);
        } else if let Some(size) = cache_file_size(
            cache_config,
            LicenseCacheNamespace::CustomRules,
            &fingerprint,
        ) {
            eprintln!(
                "License index cache saved ({:.1} MB)",
                size as f64 / 1_048_576.0
            );
        }

        Self::from_index(index, Some(manifest.spdx_license_list_version), provenance)
    }

    pub fn embedded_spdx_license_list_version() -> Result<String> {
        let artifact_bytes = include_bytes!("../../resources/license_detection/license_index.zst");
        Ok(load_embedded_artifact_metadata_from_bytes(artifact_bytes)
            .map_err(|e| {
                anyhow::anyhow!("Failed to load embedded license artifact metadata: {}", e)
            })?
            .spdx_license_list_version)
    }

    pub fn detect_with_kind(
        &self,
        text: &str,
        unknown_licenses: bool,
        binary_derived: bool,
    ) -> Result<Vec<LicenseDetection>> {
        self.detect_with_kind_with_score_and_deadline(
            text,
            unknown_licenses,
            binary_derived,
            0.0,
            None,
        )
    }

    pub fn detect_with_kind_with_score(
        &self,
        text: &str,
        unknown_licenses: bool,
        binary_derived: bool,
        min_score: f32,
    ) -> Result<Vec<LicenseDetection>> {
        self.detect_with_kind_with_score_and_deadline(
            text,
            unknown_licenses,
            binary_derived,
            min_score,
            None,
        )
    }

    pub(crate) fn detect_with_kind_with_score_and_deadline(
        &self,
        text: &str,
        unknown_licenses: bool,
        binary_derived: bool,
        min_score: f32,
        deadline: Option<Instant>,
    ) -> Result<Vec<LicenseDetection>> {
        ensure_within_deadline(deadline)?;
        let clean_text = strip_utf8_bom_str(text);

        let content = truncate_detection_text(clean_text);

        ensure_within_deadline(deadline)?;
        let mut query = if deadline.is_some() {
            Query::from_extracted_text_with_deadline(
                content,
                &self.index,
                binary_derived,
                deadline,
            )?
        } else {
            Query::from_extracted_text(content, &self.index, binary_derived)?
        };
        let whole_query_run = query.whole_query_run();

        let mut all_matches = Vec::new();
        let mut candidate_contained_matches = Vec::new();
        let mut aho_extra_matchables = PositionSet::new();
        let mut matched_qspans: Vec<models::PositionSpan> = Vec::new();

        // Phase 1a: Hash matching
        // Python returns immediately if hash matches found (index.py:987-991)
        {
            ensure_within_deadline(deadline)?;
            let hash_matches = hash_match(&self.index, &whole_query_run);

            if !hash_matches.is_empty() {
                let mut matches = hash_matches;
                sort_matches_by_line(&mut matches);

                let groups = split_groups_across_frontmatter_boundary(
                    group_matches_by_region(&matches),
                    Some(content),
                );
                let detections: Vec<LicenseDetection> = groups
                    .iter()
                    .map(|group| {
                        let mut detection = empty_detection();
                        populate_detection_from_group_with_spdx(
                            &mut detection,
                            group,
                            &self.spdx_mapping,
                            Some(content),
                        );
                        detection
                    })
                    .collect();

                return Ok(post_process_detections(detections, min_score));
            }
        }

        // Phase 1b: SPDX-LID matching
        {
            ensure_within_deadline(deadline)?;
            let spdx_matches = spdx_lid_match(&self.index, &query);
            subtract_spdx_match_qspans(
                &mut query,
                &mut matched_qspans,
                &mut aho_extra_matchables,
                &spdx_matches,
            );
            all_matches.extend(spdx_matches);
        }

        // Phase 1c: Aho-Corasick matching
        {
            ensure_within_deadline(deadline)?;
            let aho_matches = if aho_extra_matchables.is_empty() {
                if deadline.is_some() {
                    aho_match::aho_match_with_deadline(&self.index, &whole_query_run, deadline)?
                } else {
                    aho_match(&self.index, &whole_query_run)
                }
            } else {
                if deadline.is_some() {
                    aho_match::aho_match_with_extra_matchables(
                        &self.index,
                        &whole_query_run,
                        Some(&aho_extra_matchables),
                        deadline,
                    )?
                } else {
                    aho_match::aho_match_with_extra_matchables(
                        &self.index,
                        &whole_query_run,
                        Some(&aho_extra_matchables),
                        None,
                    )?
                }
            };

            // Python's get_exact_matches() calls refine_matches with merge=False
            // This applies quality filters including required phrase filtering
            let refined_aho = match_refine::refine_aho_matches(&self.index, aho_matches, &query);
            candidate_contained_matches.extend(refined_aho.clone());
            let (merged_aho, _) = merge_and_prepare_aho_matches(
                &self.index,
                &mut query,
                &mut matched_qspans,
                &refined_aho,
            );
            all_matches.extend(merged_aho);

            let whole_query_followup = collect_whole_query_exact_followup_matches(
                &self.index,
                &mut query,
                &mut matched_qspans,
                &whole_query_run,
                deadline,
            )?;
            all_matches.extend(whole_query_followup);

            let merged_seq = collect_regular_seq_matches(
                &self.index,
                &query,
                &matched_qspans,
                &candidate_contained_matches,
                deadline,
            )?;
            all_matches.extend(merged_seq);
        }

        // Step 1: Initial refine WITHOUT false positive filtering
        // Python: refine_matches with filter_false_positive=False (index.py:1073-1080)
        ensure_within_deadline(deadline)?;
        let merged_matches =
            refine_matches_without_false_positive_filter(&self.index, all_matches, &query);

        // Step 2: Unknown detection and weak match handling
        // Python: index.py:1079-1118 - only runs when unknown_licenses=True
        let refined_matches = if unknown_licenses {
            // Split weak from good - Python: index.py:1083
            let (good_matches, weak_matches) = split_weak_matches(&self.index, &merged_matches);

            // Unknown detection on uncovered regions - Python: index.py:1093-1114
            let unknown_matches = unknown_match(&self.index, &query, &good_matches);
            let filtered_unknown =
                filter_invalid_contained_unknown_matches(&unknown_matches, &good_matches);

            let mut all_matches = good_matches;
            all_matches.extend(filtered_unknown);
            // reinject weak matches and let refine matches keep the bests
            // Python: index.py:1117-1118
            all_matches.extend(weak_matches);
            all_matches
        } else {
            merged_matches
        };

        // Step 5: Final refine WITH false positive filtering - Python: index.py:1130-1145
        ensure_within_deadline(deadline)?;
        let refined = refine_matches(&self.index, refined_matches, &query);

        let mut sorted = refined;
        sort_matches_by_line(&mut sorted);

        let groups = split_groups_across_frontmatter_boundary(
            group_matches_by_region(&sorted),
            Some(content),
        );

        let detections: Vec<LicenseDetection> = groups
            .iter()
            .map(|group| {
                let mut detection = empty_detection();
                populate_detection_from_group_with_spdx(
                    &mut detection,
                    group,
                    &self.spdx_mapping,
                    Some(content),
                );
                detection
            })
            .collect();

        let detections = post_process_detections(detections, min_score);

        ensure_within_deadline(deadline)?;
        Ok(detections)
    }

    pub fn detect_with_kind_and_source(
        &self,
        text: &str,
        unknown_licenses: bool,
        binary_derived: bool,
        source_path: &str,
    ) -> Result<Vec<LicenseDetection>> {
        self.detect_with_kind_and_source_with_deadline(
            text,
            unknown_licenses,
            binary_derived,
            source_path,
            None,
        )
    }

    pub(crate) fn detect_with_kind_and_source_with_deadline(
        &self,
        text: &str,
        unknown_licenses: bool,
        binary_derived: bool,
        source_path: &str,
        deadline: Option<Instant>,
    ) -> Result<Vec<LicenseDetection>> {
        let mut detections = self.detect_with_kind_with_score_and_deadline(
            text,
            unknown_licenses,
            binary_derived,
            0.0,
            deadline,
        )?;
        attach_source_path_to_detections(&mut detections, source_path);
        Ok(detections)
    }

    pub fn detect_with_kind_and_source_with_score(
        &self,
        text: &str,
        unknown_licenses: bool,
        binary_derived: bool,
        source_path: &str,
        min_score: f32,
    ) -> Result<Vec<LicenseDetection>> {
        let mut detections =
            self.detect_with_kind_with_score(text, unknown_licenses, binary_derived, min_score)?;
        attach_source_path_to_detections(&mut detections, source_path);
        Ok(detections)
    }

    pub(crate) fn detect_with_kind_and_source_with_score_and_deadline(
        &self,
        text: &str,
        unknown_licenses: bool,
        binary_derived: bool,
        source_path: &str,
        min_score: f32,
        deadline: Option<Instant>,
    ) -> Result<Vec<LicenseDetection>> {
        let mut detections = self.detect_with_kind_with_score_and_deadline(
            text,
            unknown_licenses,
            binary_derived,
            min_score,
            deadline,
        )?;
        attach_source_path_to_detections(&mut detections, source_path);
        Ok(detections)
    }

    /// Detect licenses and return raw matches (like Python's idx.match()).
    ///
    /// This is primarily used by golden tests and maintenance tooling that need
    /// raw match sequences before grouping or post-processing into detections.
    #[cfg(any(test, feature = "golden-tests"))]
    pub fn detect_matches_with_kind(
        &self,
        text: &str,
        unknown_licenses: bool,
        binary_derived: bool,
    ) -> Result<Vec<LicenseMatch>> {
        let clean_text = strip_utf8_bom_str(text);

        let content = truncate_detection_text(clean_text);

        let mut query = Query::from_extracted_text(content, &self.index, binary_derived)?;
        let whole_query_run = query.whole_query_run();

        let mut all_matches = Vec::new();
        let mut candidate_contained_matches = Vec::new();
        let mut aho_extra_matchables = PositionSet::new();
        let mut matched_qspans: Vec<models::PositionSpan> = Vec::new();

        // Phase 1a: Hash matching
        {
            let hash_matches = hash_match(&self.index, &whole_query_run);

            if !hash_matches.is_empty() {
                let mut matches = hash_matches;
                sort_matches_by_line(&mut matches);
                return Ok(matches);
            }
        }

        // Phase 1b: SPDX-LID matching
        {
            let spdx_matches = spdx_lid_match(&self.index, &query);
            subtract_spdx_match_qspans(
                &mut query,
                &mut matched_qspans,
                &mut aho_extra_matchables,
                &spdx_matches,
            );
            all_matches.extend(spdx_matches);
        }

        // Phase 1c: Aho-Corasick matching
        {
            let aho_matches = if aho_extra_matchables.is_empty() {
                aho_match(&self.index, &whole_query_run)
            } else {
                aho_match::aho_match_with_extra_matchables(
                    &self.index,
                    &whole_query_run,
                    Some(&aho_extra_matchables),
                    None,
                )?
            };
            let refined_aho = match_refine::refine_aho_matches(&self.index, aho_matches, &query);
            candidate_contained_matches.extend(refined_aho.clone());
            let (merged_aho, _) = merge_and_prepare_aho_matches(
                &self.index,
                &mut query,
                &mut matched_qspans,
                &refined_aho,
            );
            all_matches.extend(merged_aho);

            let whole_query_followup = collect_whole_query_exact_followup_matches(
                &self.index,
                &mut query,
                &mut matched_qspans,
                &whole_query_run,
                None,
            )?;
            all_matches.extend(whole_query_followup);

            let merged_seq = collect_regular_seq_matches(
                &self.index,
                &query,
                &matched_qspans,
                &candidate_contained_matches,
                None,
            )?;
            all_matches.extend(merged_seq);
        }

        // Step 1: Initial refine WITHOUT false positive filtering
        let merged_matches =
            refine_matches_without_false_positive_filter(&self.index, all_matches, &query);

        // Step 2: Unknown detection and weak match handling
        let refined_matches = if unknown_licenses {
            let (good_matches, weak_matches) = split_weak_matches(&self.index, &merged_matches);
            let unknown_matches = unknown_match(&self.index, &query, &good_matches);
            let filtered_unknown =
                filter_invalid_contained_unknown_matches(&unknown_matches, &good_matches);

            let mut all_matches = good_matches;
            all_matches.extend(filtered_unknown);
            all_matches.extend(weak_matches);
            all_matches
        } else {
            merged_matches
        };

        // Step 3: Final refine WITH false positive filtering - Python: index.py:1130-1145
        let refined = refine_matches(&self.index, refined_matches, &query);

        let mut sorted = refined;
        sort_matches_by_line(&mut sorted);

        // Return raw matches (NOT grouped) - this is Python's idx.match() behavior
        Ok(sorted)
    }

    /// Get a reference to the license index.
    pub fn index(&self) -> &index::LicenseIndex {
        &self.index
    }

    pub fn spdx_license_list_version(&self) -> Option<&str> {
        self.spdx_license_list_version.as_deref()
    }

    pub fn license_index_provenance(&self) -> Option<&LicenseIndexProvenance> {
        self.license_index_provenance.as_ref()
    }

    /// Get a reference to the SPDX mapping.
    #[cfg(test)]
    pub fn spdx_mapping(&self) -> &SpdxMapping {
        &self.spdx_mapping
    }
}

pub fn detect_scancode_spdx_license_list_version(search_path: &Path) -> Result<Option<String>> {
    for ancestor in search_path.ancestors() {
        let candidate = ancestor.join("scancode_config.py");
        if candidate.is_file() {
            let config = fs::read_to_string(&candidate)?;
            return Ok(parse_scancode_spdx_license_list_version(&config));
        }
    }

    Ok(None)
}

fn parse_scancode_spdx_license_list_version(config: &str) -> Option<String> {
    config.lines().find_map(|line| {
        let trimmed = line.trim();
        let (_, value) = trimmed.split_once('=')?;
        (trimmed.starts_with("spdx_license_list_version")).then(|| {
            value
                .trim()
                .trim_matches('"')
                .trim_matches('\'')
                .to_string()
        })
    })
}

#[cfg(test)]
mod tests;