repotoire 0.9.0

Graph-powered code analysis CLI. 110 detectors for security, architecture, bus factor, and code quality.
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
//! AI Duplicate Block Detector
//!
//! Detects near-identical code blocks that AI coding assistants tend to create
//! (copy-paste patterns). Uses AST-based similarity analysis per ICSE 2025 research.
//!
//! AI assistants often generate repetitive code with minor variations like:
//! - Different variable names but same logic
//! - Same structure with different literals
//! - Copy-paste patterns with slight modifications
//!
//! This detector uses normalized identifier hashing and Jaccard similarity
//! to find these near-duplicates. Threshold: ≥70% similarity.

#![allow(dead_code)] // Module under development - structs/helpers used in tests only

use crate::detectors::base::{Detector, DetectorConfig, DetectorScope};
use crate::graph::GraphQueryExt;
use crate::models::{Finding, Severity};
use anyhow::Result;
use std::collections::HashSet;
use std::path::PathBuf;
use tracing::{debug, info};

/// Default thresholds (based on ICSE 2025 research)
const DEFAULT_SIMILARITY_THRESHOLD: f64 = 0.70; // 70% Jaccard similarity
const DEFAULT_GENERIC_NAME_THRESHOLD: f64 = 0.40; // 40% generic identifiers
const DEFAULT_MIN_LOC: usize = 5;
const DEFAULT_MAX_FINDINGS: usize = 50;

/// Generic identifier patterns commonly produced by AI assistants
const GENERIC_IDENTIFIERS: &[&str] = &[
    "result",
    "res",
    "ret",
    "return_value",
    "rv",
    "temp",
    "tmp",
    "t",
    "data",
    "d",
    "value",
    "val",
    "v",
    "item",
    "items",
    "i",
    "obj",
    "object",
    "o",
    "x",
    "y",
    "z",
    "a",
    "b",
    "c",
    "arr",
    "array",
    "list",
    "lst",
    "dict",
    "dictionary",
    "map",
    "mapping",
    "str",
    "string",
    "s",
    "num",
    "number",
    "n",
    "count",
    "cnt",
    "index",
    "idx",
    "key",
    "k",
    "var",
    "variable",
    "input",
    "output",
    "out",
    "response",
    "resp",
    "request",
    "req",
    "config",
    "cfg",
    "args",
    "kwargs",
    "params",
    "parameters",
    "options",
    "opts",
    "settings",
    "handler",
    "callback",
    "cb",
    "func",
    "fn",
    "function",
    "elem",
    "element",
    "node",
    "current",
    "curr",
    "cur",
    "previous",
    "prev",
    "next",
];

/// Processed function data for similarity comparison.
/// Uses an index into a shared signatures array instead of storing full HashSets.
#[derive(Debug, Clone)]
pub struct FunctionData {
    pub qualified_name: String,
    pub name: String,
    pub file_path: String,
    pub line_start: u32,
    pub line_end: u32,
    pub loc: usize,
    /// Index into the shared MinHash signatures array.
    pub sig_idx: usize,
    pub generic_ratio: f64,
    pub ast_size: usize,
    /// String literals extracted from source (sorted, deduplicated).
    /// Used by Tier 4 to distinguish leaf functions with same AST but different constants.
    pub string_literals: Vec<String>,
}

/// Pre-built set for O(1) generic identifier lookup
static GENERIC_SET: std::sync::LazyLock<HashSet<&'static str>> =
    std::sync::LazyLock::new(|| GENERIC_IDENTIFIERS.iter().copied().collect());

/// Calculate ratio of generic identifiers in a function
fn calculate_generic_ratio(identifiers: &[String]) -> f64 {
    if identifiers.is_empty() {
        return 0.0;
    }

    let generic_count = identifiers
        .iter()
        .filter(|id| GENERIC_SET.contains(id.to_lowercase().as_str()))
        .count();

    generic_count as f64 / identifiers.len() as f64
}

/// Extract string literals from a function's source lines.
/// Returns sorted, deduplicated set of strings found between double quotes.
/// Used by Tier 4 to distinguish leaf functions that match on the same enum
/// but return different string constants.
fn extract_string_literals(content: &str, line_start: u32, line_end: u32) -> Vec<String> {
    let mut literals = HashSet::new();
    for (idx, line) in content.lines().enumerate() {
        let line_num = idx as u32 + 1;
        if line_num < line_start || line_num > line_end {
            continue;
        }
        // Simple extraction: scan for "..." substrings
        let bytes = line.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            if bytes[i] == b'"' {
                let start = i + 1;
                i = start;
                while i < bytes.len() {
                    if bytes[i] == b'\\' {
                        i += 2; // skip escaped char
                        continue;
                    }
                    if bytes[i] == b'"' {
                        let lit = &line[start..i];
                        if !lit.is_empty() && lit.len() < 100 {
                            literals.insert(lit.to_string());
                        }
                        break;
                    }
                    i += 1;
                }
            }
            i += 1;
        }
    }
    let mut sorted: Vec<String> = literals.into_iter().collect();
    sorted.sort();
    sorted
}

/// Detect near-identical code blocks typical of AI-generated code
pub struct AIDuplicateBlockDetector {
    config: DetectorConfig,
    similarity_threshold: f64,
    generic_name_threshold: f64,
    min_loc: usize,
    max_findings: usize,
}

impl AIDuplicateBlockDetector {
    /// Create a new detector with default settings
    pub fn new() -> Self {
        Self {
            config: DetectorConfig::new(),
            similarity_threshold: DEFAULT_SIMILARITY_THRESHOLD,
            generic_name_threshold: DEFAULT_GENERIC_NAME_THRESHOLD,
            min_loc: DEFAULT_MIN_LOC,
            max_findings: DEFAULT_MAX_FINDINGS,
        }
    }

    /// Create with custom config
    pub fn with_config(config: DetectorConfig) -> Self {
        Self {
            similarity_threshold: config
                .get_option_or("similarity_threshold", DEFAULT_SIMILARITY_THRESHOLD),
            generic_name_threshold: config
                .get_option_or("generic_name_threshold", DEFAULT_GENERIC_NAME_THRESHOLD),
            min_loc: config.get_option_or("min_loc", DEFAULT_MIN_LOC),
            max_findings: config.get_option_or("max_findings", DEFAULT_MAX_FINDINGS),
            config,
        }
    }

    /// Compute similarity threshold based on average function size.
    /// Small functions need higher thresholds because they have fewer
    /// distinguishing bigrams (higher chance of coincidental similarity).
    fn size_adaptive_threshold(&self, loc1: usize, loc2: usize) -> f64 {
        let avg_loc = (loc1 + loc2) / 2;
        if avg_loc <= 10 {
            0.90
        } else if avg_loc <= 20 {
            // Linear interpolation: 0.90 at 10 LOC → base threshold at 21+ LOC
            let base = self.similarity_threshold;
            0.90 - (avg_loc as f64 - 10.0) * (0.90 - base) / 10.0
        } else {
            self.similarity_threshold
        }
    }

    /// Find duplicate pairs using MinHash/LSH + MinHash-estimated Jaccard.
    ///
    /// Uses LSH banding on pre-computed MinHash signatures for candidate generation,
    /// then verifies candidates with MinHash-estimated Jaccard (±0.1 at k=100).
    /// Drops the full `HashSet<String>` bigram sets — only signatures are needed.
    fn find_duplicates(
        &self,
        functions: &[FunctionData],
        signatures: &[[u64; 100]],
    ) -> Vec<(FunctionData, FunctionData, f64)> {
        use rayon::prelude::*;

        if functions.len() < 2 {
            return Vec::new();
        }

        let candidates =
            crate::detectors::ast_fingerprint::lsh_candidate_pairs_from_sigs(signatures);

        debug!(
            "LSH: {} candidates from {} functions ({:.1}% of {:.0} total pairs)",
            candidates.len(),
            functions.len(),
            candidates.len() as f64 / (functions.len() as f64 * (functions.len() - 1) as f64 / 2.0)
                * 100.0,
            functions.len() as f64 * (functions.len() - 1) as f64 / 2.0,
        );

        // Parallel MinHash Jaccard verification on LSH candidates
        let threshold = self.similarity_threshold;
        let candidate_vec: Vec<_> = candidates.into_iter().collect();
        let mut duplicates: Vec<(FunctionData, FunctionData, f64)> = candidate_vec
            .par_iter()
            .filter_map(|&(i, j)| {
                let func1 = &functions[i];
                let func2 = &functions[j];

                // Skip same-file comparisons
                if func1.file_path == func2.file_path {
                    return None;
                }

                // Size-ratio pre-filter (Jaccard upper bound)
                if func1.ast_size > 0 && func2.ast_size > 0 {
                    let size_ratio = func1.ast_size.min(func2.ast_size) as f64
                        / func1.ast_size.max(func2.ast_size) as f64;
                    if size_ratio < threshold {
                        return None;
                    }
                }

                // MinHash-estimated Jaccard (±0.1 at k=100)
                let similarity = crate::detectors::ast_fingerprint::minhash_jaccard(
                    &signatures[func1.sig_idx],
                    &signatures[func2.sig_idx],
                );
                // Size-adaptive threshold: small functions need higher similarity
                let adaptive_threshold = self.size_adaptive_threshold(func1.loc, func2.loc);
                if similarity >= adaptive_threshold {
                    Some((func1.clone(), func2.clone(), similarity))
                } else {
                    None
                }
            })
            .collect();

        // Sort by similarity (highest first)
        duplicates.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));

        duplicates
    }

    /// Check if a qualified name represents a trait implementation.
    fn is_trait_impl(qn: &str) -> bool {
        qn.contains("impl<") && qn.contains(" for ")
    }

    /// Extract the trait name from a trait impl qualified name.
    /// "src/foo.rs::impl<Display for MyStruct>::fmt:30" -> Some("Display")
    fn extract_trait_name(qn: &str) -> Option<&str> {
        let impl_start = qn.find("impl<")? + 5;
        let for_pos = qn[impl_start..].find(" for ")?;
        Some(&qn[impl_start..impl_start + for_pos])
    }

    /// Extract the implementing type from a qualified name.
    /// "src/foo.rs::impl<Display for MyStruct>::fmt:30" -> Some("MyStruct")
    /// "src/foo.rs::impl<MyStruct>::new:10" -> Some("MyStruct")
    fn extract_impl_type(qn: &str) -> Option<&str> {
        let impl_start = qn.find("impl<")? + 5;
        let close = qn[impl_start..].find('>')?;
        let inner = &qn[impl_start..impl_start + close];
        if let Some(for_pos) = inner.find(" for ") {
            Some(&inner[for_pos + 5..])
        } else {
            Some(inner)
        }
    }

    /// Resolve a FunctionData to its graph QN via find_function_at.
    /// Falls back to the detector's simplified QN if not found in graph.
    fn resolve_graph_qn(func: &FunctionData, graph: &dyn crate::graph::GraphQuery) -> String {
        let i = graph.interner();
        graph
            .find_function_at(&func.file_path, func.line_start)
            .map(|n| n.qn(i).to_string())
            .unwrap_or_else(|| func.qualified_name.clone())
    }

    /// Verify that a candidate duplicate pair is semantically real, not coincidental.
    /// Returns false if the pair should be rejected as a false positive.
    fn verify_semantic_overlap(
        func1: &FunctionData,
        func2: &FunctionData,
        _similarity: f64,
        graph: &dyn crate::graph::GraphQuery,
    ) -> bool {
        // Resolve to graph QNs (which include impl<Type> info)
        let gqn1 = Self::resolve_graph_qn(func1, graph);
        let gqn2 = Self::resolve_graph_qn(func2, graph);

        // Tier 1: Trait impl filter
        // Same trait on different types -> not a real clone (idiomatic pattern)
        if Self::is_trait_impl(&gqn1) && Self::is_trait_impl(&gqn2) {
            let trait1 = Self::extract_trait_name(&gqn1);
            let trait2 = Self::extract_trait_name(&gqn2);
            let type1 = Self::extract_impl_type(&gqn1);
            let type2 = Self::extract_impl_type(&gqn2);
            if trait1 == trait2 && type1 != type2 {
                return false;
            }
        }

        // Tier 2: Callee overlap check (for functions with callees)
        let i = graph.interner();
        let callees1: HashSet<String> = graph
            .get_callees(&gqn1)
            .into_iter()
            .map(|n| n.qn(i).to_string())
            .collect();
        let callees2: HashSet<String> = graph
            .get_callees(&gqn2)
            .into_iter()
            .map(|n| n.qn(i).to_string())
            .collect();

        if !callees1.is_empty() || !callees2.is_empty() {
            let intersection = callees1.intersection(&callees2).count();
            let union = callees1.union(&callees2).count();
            let overlap = if union > 0 {
                intersection as f64 / union as f64
            } else {
                0.0
            };
            if overlap < 0.3 {
                return false;
            }
        }

        // Tier 3: Leaf function context (no callees on either side)
        // Leaf functions in different impl types are likely coincidental
        if callees1.is_empty() && callees2.is_empty() {
            let type1 = Self::extract_impl_type(&gqn1);
            let type2 = Self::extract_impl_type(&gqn2);
            if type1.is_some() && type2.is_some() && type1 != type2 {
                return false;
            }

            // Tier 4: String literal divergence for leaf functions.
            // Functions with identical AST structure but different string constants
            // (e.g. match on same enum returning different strings) are template
            // siblings, not true duplicates.
            if !func1.string_literals.is_empty() || !func2.string_literals.is_empty() {
                let set1: HashSet<&str> =
                    func1.string_literals.iter().map(|s| s.as_str()).collect();
                let set2: HashSet<&str> =
                    func2.string_literals.iter().map(|s| s.as_str()).collect();
                let intersection = set1.intersection(&set2).count();
                let union = set1.union(&set2).count();
                let literal_overlap = if union > 0 {
                    intersection as f64 / union as f64
                } else {
                    1.0
                };
                if literal_overlap < 0.2 {
                    return false;
                }
            }
        }

        true
    }

    /// Create a finding from a duplicate pair
    fn create_finding(
        &self,
        func1: &FunctionData,
        func2: &FunctionData,
        similarity: f64,
    ) -> Finding {
        let similarity_pct = (similarity * 100.0) as u32;

        // Check for generic naming pattern
        let has_generic_naming = func1.generic_ratio >= self.generic_name_threshold
            || func2.generic_ratio >= self.generic_name_threshold;

        // Determine severity based on similarity and generic naming.
        // Duplicate code is a refactoring smell, never a security/data-loss issue.
        // Cap at Medium (exact/near-exact matches) or Low (partial matches).
        let severity = if similarity >= 0.85 {
            Severity::Medium
        } else {
            Severity::Low
        };

        // Build description
        let mut description = format!(
            "Functions '{}' and '{}' have {}% AST similarity, \
             indicating structural duplication.\n\n\
             **{}** ({} LOC): `{}`\n\
             **{}** ({} LOC): `{}`\n\n",
            func1.name,
            func2.name,
            similarity_pct,
            func1.name,
            func1.loc,
            func1.file_path,
            func2.name,
            func2.loc,
            func2.file_path,
        );

        if has_generic_naming {
            let avg_generic = (func1.generic_ratio + func2.generic_ratio) / 2.0;
            description.push_str(&format!(
                "⚠️ **Generic naming detected**: {:.0}% of identifiers \
                 use generic names (result, temp, data, etc.), suggesting low-effort duplication.\n\n",
                avg_generic * 100.0
            ));
        }

        description.push_str(
            "Near-identical functions increase maintenance burden and \
             can lead to inconsistent bug fixes.",
        );

        let suggestion = "Consider one of the following approaches:\n\
             1. **Extract common logic** into a shared helper function\n\
             2. **Use a template/factory pattern** if variations are intentional\n\
             3. **Consolidate** into a single implementation if truly duplicates\n\
             4. **Add documentation** explaining why similar implementations exist"
            .to_string();

        let mut affected_files = Vec::new();
        if func1.file_path != "unknown" {
            affected_files.push(PathBuf::from(&func1.file_path));
        }
        if func2.file_path != "unknown" {
            affected_files.push(PathBuf::from(&func2.file_path));
        }

        Finding {
            id: String::new(),
            detector: "AIDuplicateBlockDetector".to_string(),
            severity,
            title: format!(
                "Structural duplicate: {}{} ({}% AST)",
                func1.name, func2.name, similarity_pct
            ),
            description,
            affected_files,
            line_start: Some(func1.line_start),
            line_end: Some(func1.line_end),
            suggested_fix: Some(suggestion),
            estimated_effort: Some("Medium (1-2 hours)".to_string()),
            category: Some("duplication".to_string()),
            cwe_id: None,
            why_it_matters: Some(
                "Near-identical code duplicates increase maintenance burden. \
                 When bugs are found, they must be fixed in multiple places. \
                 When requirements change, all copies must be updated consistently."
                    .to_string(),
            ),
            ..Default::default()
        }
    }
}

impl Default for AIDuplicateBlockDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl Detector for AIDuplicateBlockDetector {
    fn name(&self) -> &'static str {
        "AIDuplicateBlockDetector"
    }

    fn description(&self) -> &'static str {
        "Detects near-identical code blocks using AST similarity (≥70%)"
    }

    fn category(&self) -> &'static str {
        "ai_generated"
    }

    fn requires_graph(&self) -> bool {
        true
    }

    fn detector_scope(&self) -> DetectorScope {
        // Produces cross-file findings (compares code blocks across files).
        DetectorScope::FileScopedGraph
    }

    fn config(&self) -> Option<&DetectorConfig> {
        Some(&self.config)
    }

    fn file_extensions(&self) -> &'static [&'static str] {
        &[
            "py", "js", "ts", "jsx", "tsx", "java", "go", "rs", "c", "cpp", "cs",
        ]
    }

    fn detect(
        &self,
        ctx: &crate::detectors::analysis_context::AnalysisContext,
    ) -> Result<Vec<Finding>> {
        let files = &ctx.as_file_provider();
        use rayon::prelude::*;

        let source_exts = &["py", "js", "ts", "jsx", "tsx", "java", "go", "rs"];

        // Collect file paths + content upfront (FileProvider requires &self borrows)
        let file_data: Vec<_> = files
            .files_with_extensions(source_exts)
            .into_iter()
            .filter(|path| !crate::detectors::base::is_test_path(&path.to_string_lossy()))
            .filter_map(|path| {
                let content = files.content(path)?;
                let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
                let has_functions = match ext {
                    "py" => content.contains("\ndef ") || content.starts_with("def "),
                    "js" | "jsx" => {
                        content.contains("function ")
                            || content.contains("=> ")
                            || content.contains("=>{")
                    }
                    "ts" | "tsx" => {
                        content.contains("function ")
                            || content.contains("=> ")
                            || content.contains("=>{")
                    }
                    "go" => content.contains("\nfunc ") || content.starts_with("func "),
                    "rs" => {
                        content.contains("\nfn ")
                            || content.starts_with("fn ")
                            || content.contains(" fn ")
                    }
                    "java" => {
                        content.contains("void ")
                            || content.contains("public ")
                            || content.contains("private ")
                    }
                    _ => true,
                };
                if !has_functions {
                    return None;
                }
                Some((path.to_path_buf(), content, ext.to_string()))
            })
            .collect();

        // Collect functions AND their MinHash signatures in parallel.
        // FunctionData stores only a sig_idx (index into signatures vec).
        let min_loc = self.min_loc;
        let mut all_functions: Vec<FunctionData> = Vec::new();
        let mut all_signatures: Vec<[u64; 100]> = Vec::new();

        // Parallel phase: collect (FunctionData-minus-sig_idx, signature) tuples
        struct FuncWithSig {
            qualified_name: String,
            name: String,
            file_path: String,
            line_start: u32,
            line_end: u32,
            string_literals: Vec<String>,
            loc: usize,
            generic_ratio: f64,
            ast_size: usize,
            sig: [u64; 100],
        }

        let func_sigs: Vec<FuncWithSig> = file_data
            .par_iter()
            .flat_map_iter(|(path, content, ext)| {
                let path_str = path.to_string_lossy().to_string();

                // Fast path: use cached fingerprints + pre-computed sigs from parse phase
                if let Some(cached) = crate::parsers::get_cached_fps(&path_str) {
                    return cached
                        .into_iter()
                        .filter_map(|fp| {
                            let loc = (fp.line_end - fp.line_start + 1) as usize;
                            if loc < min_loc || fp.normalized_bigrams.is_empty() {
                                return None;
                            }
                            let generic_ratio = calculate_generic_ratio(&fp.identifiers);
                            let ast_size = fp.normalized_bigrams.len();
                            // Use pre-computed sig or compute from bigrams
                            let sig = fp.minhash_sig.unwrap_or_else(|| {
                                crate::detectors::ast_fingerprint::compute_minhash_signature(
                                    &fp.normalized_bigrams,
                                )
                            });
                            let string_literals =
                                extract_string_literals(content, fp.line_start, fp.line_end);
                            Some(FuncWithSig {
                                qualified_name: format!("{}::{}", path_str, fp.name),
                                name: fp.name,
                                file_path: path_str.clone(),
                                line_start: fp.line_start,
                                line_end: fp.line_end,
                                string_literals,
                                loc,
                                generic_ratio,
                                ast_size,
                                sig,
                            })
                        })
                        .collect::<Vec<_>>();
                }

                // Slow path: fallback to re-parsing (e.g. tests with MockFileProvider)
                let lang = crate::parsers::lightweight::Language::from_extension(ext);
                let functions =
                    crate::detectors::ast_fingerprint::parse_functions_with_fingerprints(
                        content, lang,
                    );

                functions
                    .into_iter()
                    .filter_map(move |(func, fp)| {
                        let loc = (func.line_end - func.line_start + 1) as usize;
                        if loc < min_loc || fp.normalized_bigrams.is_empty() {
                            return None;
                        }
                        let generic_ratio = calculate_generic_ratio(&fp.identifiers);
                        let ast_size = fp.normalized_bigrams.len();
                        let sig = crate::detectors::ast_fingerprint::compute_minhash_signature(
                            &fp.normalized_bigrams,
                        );
                        let string_literals =
                            extract_string_literals(content, func.line_start, func.line_end);
                        Some(FuncWithSig {
                            qualified_name: format!("{}::{}", path_str, func.name),
                            name: func.name,
                            file_path: path_str.clone(),
                            line_start: func.line_start,
                            line_end: func.line_end,
                            string_literals,
                            loc,
                            generic_ratio,
                            ast_size,
                            sig,
                        })
                    })
                    .collect::<Vec<_>>()
            })
            .collect();

        // Filter out test functions BEFORE building parallel arrays.
        // Inline #[cfg(test)] modules live in production source files, so the
        // file-level is_test_path filter doesn't catch them. Use graph context
        // when available, fall back to name heuristic.
        let func_sigs: Vec<FuncWithSig> = func_sigs
            .into_iter()
            .filter(|fs| {
                if let Some(graph_func) = ctx.graph.find_function_at(&fs.file_path, fs.line_start) {
                    let i = ctx.graph.interner();
                    let qn = graph_func.qn(i);
                    if ctx.is_test_function(qn) {
                        return false;
                    }
                    let decos = ctx.decorators(qn);
                    for d in decos {
                        if d == "test" || d.starts_with("cfg(test") {
                            return false;
                        }
                    }
                    true
                } else {
                    if fs.name.starts_with("test_") {
                        info!(
                            "AIDupBlock filter: find_function_at MISS for test fn {}:{} (file={})",
                            fs.name, fs.line_start, fs.file_path
                        );
                    }
                    !fs.name.starts_with("test_")
                }
            })
            .collect();

        // Assign sig_idx sequentially (signatures must be contiguous)
        for fs in func_sigs {
            let sig_idx = all_signatures.len();
            all_signatures.push(fs.sig);
            all_functions.push(FunctionData {
                qualified_name: fs.qualified_name,
                name: fs.name,
                file_path: fs.file_path,
                line_start: fs.line_start,
                line_end: fs.line_end,
                loc: fs.loc,
                sig_idx,
                generic_ratio: fs.generic_ratio,
                ast_size: fs.ast_size,
                string_literals: fs.string_literals,
            });
        }

        let duplicates = self.find_duplicates(&all_functions, &all_signatures);

        // Graph-verified semantic overlap: reject coincidental matches
        let duplicates: Vec<_> = duplicates
            .into_iter()
            .filter(|(func1, func2, similarity)| {
                Self::verify_semantic_overlap(func1, func2, *similarity, ctx.graph)
            })
            .collect();

        let mut findings = Vec::new();
        for (func1, func2, similarity) in &duplicates {
            findings.push(self.create_finding(func1, func2, *similarity));
            if findings.len() >= self.max_findings {
                break;
            }
        }

        info!("AIDuplicateBlockDetector found {} findings", findings.len());
        Ok(findings)
    }
}

impl crate::detectors::RegisteredDetector for AIDuplicateBlockDetector {
    fn create(_init: &crate::detectors::DetectorInit) -> std::sync::Arc<dyn Detector> {
        std::sync::Arc::new(Self::new())
    }

    fn max_tier() -> crate::models::Tier {
        crate::models::Tier::Deep
    }
}

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

    #[test]
    fn test_minhash_jaccard_identical() {
        let set: HashSet<String> = ["a", "b", "c", "d"].iter().map(|s| s.to_string()).collect();
        let sig = crate::detectors::ast_fingerprint::compute_minhash_signature(&set);
        let sim = crate::detectors::ast_fingerprint::minhash_jaccard(&sig, &sig);
        assert_eq!(sim, 1.0, "Identical sets should have Jaccard 1.0");
    }

    #[test]
    fn test_minhash_jaccard_disjoint() {
        let set1: HashSet<String> = (0..50).map(|i| format!("a_{}", i)).collect();
        let set2: HashSet<String> = (50..100).map(|i| format!("b_{}", i)).collect();
        let sig1 = crate::detectors::ast_fingerprint::compute_minhash_signature(&set1);
        let sig2 = crate::detectors::ast_fingerprint::compute_minhash_signature(&set2);
        let sim = crate::detectors::ast_fingerprint::minhash_jaccard(&sig1, &sig2);
        assert!(
            sim < 0.2,
            "Disjoint sets should have low Jaccard, got {}",
            sim
        );
    }

    #[test]
    fn test_generic_ratio() {
        let identifiers = vec![
            "result".to_string(),
            "temp".to_string(),
            "user_id".to_string(),
            "data".to_string(),
        ];
        let ratio = calculate_generic_ratio(&identifiers);
        // result, temp, data are generic (3/4 = 0.75)
        assert!((ratio - 0.75).abs() < 0.01);

        let no_generic = vec!["user_id".to_string(), "order_amount".to_string()];
        assert_eq!(calculate_generic_ratio(&no_generic), 0.0);

        let empty: Vec<String> = vec![];
        assert_eq!(calculate_generic_ratio(&empty), 0.0);
    }

    #[test]
    fn test_detector_defaults() {
        let detector = AIDuplicateBlockDetector::new();
        assert!((detector.similarity_threshold - 0.70).abs() < 0.01);
        assert!((detector.generic_name_threshold - 0.40).abs() < 0.01);
        assert_eq!(detector.min_loc, 5);
    }

    #[test]
    fn test_detects_near_duplicates() {
        // Two functions with identical structure but different variable names
        // across different files — classic Type-2 clone.
        let store = crate::graph::GraphBuilder::new().freeze();
        let detector = AIDuplicateBlockDetector::new();
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            (
                "services/user_service.py",
                "def process_user(data):\n    result = validate(data)\n    if result is None:\n        raise ValueError('invalid')\n    output = transform(result)\n    return output\n",
            ),
            (
                "services/order_service.py",
                "def process_order(info):\n    value = validate(info)\n    if value is None:\n        raise ValueError('invalid')\n    output = transform(value)\n    return output\n",
            ),
        ]);
        let findings = detector
            .detect(&ctx)
            .expect("should detect duplicate blocks");
        assert!(
            !findings.is_empty(),
            "Should detect near-duplicate functions with same structure but different variable names"
        );
        assert_eq!(findings[0].detector, "AIDuplicateBlockDetector");
        assert!(
            findings[0].title.contains("process_user")
                || findings[0].title.contains("process_order"),
            "Finding title should reference the duplicate function names. Got: {}",
            findings[0].title
        );
        // Duplicate code is a refactoring smell, never Critical or High
        assert!(
            findings[0].severity == Severity::Medium || findings[0].severity == Severity::Low,
            "Duplicate-block severity must be at most Medium, got {:?}",
            findings[0].severity
        );
    }

    #[test]
    fn test_severity_cap_exact_match() {
        // create_finding with similarity = 1.0 (100% AST) must not exceed Medium
        let detector = AIDuplicateBlockDetector::new();
        let func1 = FunctionData {
            qualified_name: "a.rs::escapeHtml".to_string(),
            name: "escapeHtml".to_string(),
            file_path: "src/a.rs".to_string(),
            line_start: 1,
            line_end: 10,
            loc: 10,
            sig_idx: 0,
            generic_ratio: 0.0,
            ast_size: 20,
            string_literals: vec![],
        };
        let func2 = FunctionData {
            qualified_name: "b.rs::escapeHtml".to_string(),
            name: "escapeHtml".to_string(),
            file_path: "src/b.rs".to_string(),
            line_start: 1,
            line_end: 10,
            loc: 10,
            sig_idx: 1,
            generic_ratio: 0.0,
            ast_size: 20,
            string_literals: vec![],
        };
        // 100% AST match — must be Medium, never Critical or High
        let finding = detector.create_finding(&func1, &func2, 1.0);
        assert_eq!(
            finding.severity,
            Severity::Medium,
            "100% AST-match duplicate must be Severity::Medium, got {:?}",
            finding.severity
        );
    }

    #[test]
    fn test_severity_cap_partial_match() {
        // create_finding with similarity = 0.75 (partial match) must be Low
        let detector = AIDuplicateBlockDetector::new();
        let func1 = FunctionData {
            qualified_name: "a.rs::parseJsonOrText".to_string(),
            name: "parseJsonOrText".to_string(),
            file_path: "src/a.rs".to_string(),
            line_start: 1,
            line_end: 8,
            loc: 8,
            sig_idx: 0,
            generic_ratio: 0.0,
            ast_size: 15,
            string_literals: vec![],
        };
        let func2 = FunctionData {
            qualified_name: "b.rs::parseJsonOrText".to_string(),
            name: "parseJsonOrText".to_string(),
            file_path: "src/b.rs".to_string(),
            line_start: 1,
            line_end: 8,
            loc: 8,
            sig_idx: 1,
            generic_ratio: 0.0,
            ast_size: 15,
            string_literals: vec![],
        };
        // 75% match — below the 0.85 threshold, must be Low
        let finding = detector.create_finding(&func1, &func2, 0.75);
        assert_eq!(
            finding.severity,
            Severity::Low,
            "75% AST-match duplicate must be Severity::Low, got {:?}",
            finding.severity
        );
    }

    #[test]
    fn test_no_finding_for_different_functions() {
        // Two structurally different functions — should produce no duplicates.
        let store = crate::graph::GraphBuilder::new().freeze();
        let detector = AIDuplicateBlockDetector::new();
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            (
                "auth.py",
                "def login(username, password):\n    user = authenticate(username, password)\n    if user is None:\n        raise AuthError('Invalid credentials')\n    token = create_token(user)\n    return token\n",
            ),
            (
                "export.py",
                "def export_csv(data, output_path):\n    with open(output_path, 'w') as f:\n        writer = csv.writer(f)\n        writer.writerow(data[0].keys())\n        for row in data:\n            writer.writerow(row.values())\n",
            ),
        ]);
        let findings = detector
            .detect(&ctx)
            .expect("should detect different functions");
        assert!(
            findings.is_empty(),
            "Should not flag structurally different functions. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }
}