repotoire 0.7.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
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
//! Incremental file fingerprinting and cache system for fast re-analysis
//!
//! This module provides caching of detector findings keyed by file content hash,
//! enabling incremental analysis that only re-runs detectors on changed files.
//!
//! Uses XXH3 (xxhash-rust) for fast, non-cryptographic content fingerprinting.
//!
//! # Example
//!
//! ```ignore
//! let config = repotoire::config::ProjectConfig::default();
//! let cache = IncrementalCache::new(Path::new("/repo/.repotoire/cache"), &config, false);
//! let changed = cache.changed_files(&all_files);
//! for f in changed {
//!     let findings = run_detector(&f);
//!     cache.cache_findings(&f, &findings);
//! }
//! cache.save_cache()?;
//! ```

use crate::models::{Finding, Grade, Severity};
use crate::parsers::ParseResult;
use anyhow::{Context, Result};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::{debug, info, warn};

/// Cache format version - bump when schema changes
const CACHE_VERSION: u32 = 5;

/// Buffer size for hashing large files (64KB chunks)
const HASH_BUFFER_SIZE: usize = 65536;

/// Upper bound on number of per-file cache entries retained across runs.
/// `prune_stale_entries` already drops entries for files that no longer exist
/// in the current analysis, but in degenerate scenarios (huge monorepos,
/// cross-repo reuse of a shared cache dir) it is possible for the map to
/// balloon before pruning runs. Once exceeded, oldest-timestamp entries are
/// evicted until the map fits.
const MAX_CACHE_FILES: usize = 100_000;

/// Cached file entry
#[derive(Debug, Clone, Serialize, Deserialize)]
struct CachedFile {
    hash: String,
    findings: Vec<Finding>,
    timestamp: u64,
    /// Qualified names of cross-file values this file depends on (e.g., "config.TIMEOUT")
    #[serde(default)]
    value_dependencies: Vec<String>,
    /// Hash of each dependency's resolved value at cache time
    #[serde(default)]
    value_hashes: HashMap<String, u64>,
}

/// Cached score result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedScoreResult {
    pub score: f64,
    pub grade: Grade,
    pub total_files: usize,
    pub total_functions: usize,
    pub total_classes: usize,
    #[serde(default)]
    pub structure_score: Option<f64>,
    #[serde(default)]
    pub quality_score: Option<f64>,
    #[serde(default)]
    pub architecture_score: Option<f64>,
    #[serde(default)]
    pub total_loc: Option<usize>,
}

/// Graph-level cache data
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct GraphCache {
    hash: Option<String>,
    detectors: HashMap<String, Vec<Finding>>,
    #[serde(default)]
    score: Option<CachedScoreResult>,
}

/// Cached parse result for a file
#[derive(Debug, Clone, Serialize, Deserialize)]
struct CachedParseResult {
    hash: String,
    result: crate::parsers::ParseResult,
}

/// Full cache structure
#[derive(Debug, Clone, Serialize, Deserialize)]
struct CacheData {
    version: u32,
    /// Binary version that created this cache (#66)
    #[serde(default)]
    binary_version: String,
    files: HashMap<String, CachedFile>,
    graph: GraphCache,
    #[serde(default)]
    parse_cache: HashMap<String, CachedParseResult>,
    /// Cache fingerprint — hash of binary + config + analysis mode + schema version.
    /// Old caches without this field deserialize as None and auto-invalidate.
    #[serde(default)]
    fingerprint: Option<u64>,
}

impl Default for CacheData {
    // repotoire:ignore[mutual-recursion] — false positive: new() → load_cache() → invalidate_all() → default() is a call-graph cycle but not actual recursion; each function is called at most once per construction.
    fn default() -> Self {
        Self {
            version: CACHE_VERSION,
            binary_version: env!("CARGO_PKG_VERSION").to_string(),
            files: HashMap::new(),
            graph: GraphCache::default(),
            parse_cache: HashMap::new(),
            fingerprint: None,
        }
    }
}

/// Cache statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CacheStats {
    pub cached_files: usize,
    pub total_findings: usize,
    pub graph_hash: Option<String>,
    pub graph_detectors: usize,
    pub graph_findings: usize,
    pub cache_version: u32,
}

/// Hash the repotoire binary itself for dev-rebuild detection.
/// Returns None if the binary can't be read (deleted, permissions, etc).
pub fn binary_file_hash() -> Option<u64> {
    let exe = std::env::current_exe().ok()?;
    let bytes = fs::read(&exe).ok()?;
    Some(xxhash_rust::xxh3::xxh3_64(&bytes))
}

/// Recursively sort JSON object keys for deterministic serialization.
/// HashMap iteration order is non-deterministic; this normalizes it.
fn sort_json_keys(value: serde_json::Value) -> serde_json::Value {
    match value {
        serde_json::Value::Object(map) => {
            let sorted: serde_json::Map<String, serde_json::Value> = map
                .into_iter()
                .map(|(k, v)| (k, sort_json_keys(v)))
                .collect::<std::collections::BTreeMap<_, _>>()
                .into_iter()
                .collect();
            serde_json::Value::Object(sorted)
        }
        serde_json::Value::Array(arr) => {
            serde_json::Value::Array(arr.into_iter().map(sort_json_keys).collect())
        }
        other => other,
    }
}

/// Compute cache fingerprint from all analysis inputs.
pub fn compute_fingerprint(
    binary_hash: u64,
    config: &crate::config::ProjectConfig,
    all_detectors: bool,
) -> u64 {
    let mut buf = Vec::with_capacity(256);
    buf.extend_from_slice(&binary_hash.to_le_bytes());
    match serde_json::to_value(config).and_then(|v| serde_json::to_string(&sort_json_keys(v))) {
        Ok(json) => buf.extend_from_slice(json.as_bytes()),
        Err(_) => buf.extend_from_slice(b"__config_serialize_error__"),
    }
    buf.push(all_detectors as u8);
    buf.extend_from_slice(&CACHE_VERSION.to_le_bytes());
    xxhash_rust::xxh3::xxh3_64(&buf)
}

/// File fingerprinting and findings cache for incremental analysis
///
/// Stores file hashes and associated findings to avoid re-running detectors
/// on unchanged files. Cache is persisted to disk as bincode.
pub struct IncrementalCache {
    cache_dir: PathBuf,
    cache_file: PathBuf,
    cache: CacheData,
    dirty: bool,
    /// Memoized all-files hash to avoid re-hashing on every call
    memoized_files_hash: Option<(usize, String)>, // (file_count, hash)
    /// Stored for fingerprint computation in save_cache
    fingerprint_config: crate::config::ProjectConfig,
    fingerprint_all_detectors: bool,
}

impl IncrementalCache {
    /// Create a new cache
    pub fn new(
        cache_dir: &Path,
        config: &crate::config::ProjectConfig,
        all_detectors: bool,
    ) -> Self {
        let cache_dir = cache_dir.to_path_buf();
        let cache_file = cache_dir.join("findings_cache.bin");

        // Ensure cache directory exists
        if let Err(e) = fs::create_dir_all(&cache_dir) {
            warn!("Failed to create cache directory: {}", e);
        }

        let mut instance = Self {
            cache_dir,
            cache_file,
            cache: CacheData::default(),
            dirty: false,
            memoized_files_hash: None,
            fingerprint_config: config.clone(),
            fingerprint_all_detectors: all_detectors,
        };

        // Load existing cache
        if let Err(e) = instance.load_cache() {
            debug!("Failed to load cache: {}", e);
        }

        instance
    }

    /// Check if a warm cache exists (for auto-incremental)
    pub fn has_cache(&self) -> bool {
        !self.cache.files.is_empty() || !self.cache.parse_cache.is_empty()
    }

    /// Cached parse result for a file if unchanged
    #[allow(dead_code)] // Public API, now primarily used via ConcurrentCacheView
    pub fn cached_parse(&self, path: &Path) -> Option<crate::parsers::ParseResult> {
        let key = path.to_string_lossy().to_string();
        let hash = self.file_hash(path);

        self.cache.parse_cache.get(&key).and_then(|cached| {
            if cached.hash == hash {
                Some(cached.result.clone())
            } else {
                None
            }
        })
    }

    /// Cache a parse result for a file
    pub fn cache_parse_result(&mut self, path: &Path, result: &crate::parsers::ParseResult) {
        let key = path.to_string_lossy().to_string();
        let hash = self.file_hash(path);

        self.cache.parse_cache.insert(
            key,
            CachedParseResult {
                hash,
                result: result.clone(),
            },
        );
        self.dirty = true;
    }

    /// Compute fast content hash of a file using XXH3 (3-5x faster than SipHash)
    pub fn file_hash(&self, path: &Path) -> String {
        match fs::File::open(path) {
            Ok(mut file) => {
                let mut hasher = xxhash_rust::xxh3::Xxh3::new();
                let mut buffer = [0u8; HASH_BUFFER_SIZE];

                loop {
                    match file.read(&mut buffer) {
                        Ok(0) => break,
                        Ok(n) => hasher.update(&buffer[..n]),
                        Err(_) => break,
                    }
                }

                format!("{:016x}", hasher.digest())
            }
            Err(_) => format!("error:{}", path.display()),
        }
    }

    /// Load cache from disk
    fn load_cache(&mut self) -> Result<()> {
        if !self.cache_file.exists() {
            debug!("No cache file found at {:?}", self.cache_file);
            return Ok(());
        }

        let bytes = fs::read(&self.cache_file).context("Failed to read cache file")?;
        let data: CacheData = bitcode::deserialize(&bytes).context("Failed to parse cache")?;

        // Version check - rebuild if schema changed
        if data.version != CACHE_VERSION {
            info!(
                "Cache version mismatch (got {}, expected {}), rebuilding",
                data.version, CACHE_VERSION
            );
            self.invalidate_all();
            return Ok(());
        }

        // Binary version check — prevent stale detector results across upgrades (#66)
        let current_version = env!("CARGO_PKG_VERSION");
        if !data.binary_version.is_empty() && data.binary_version != current_version {
            info!(
                "Binary version changed ({} → {}), invalidating cache",
                data.binary_version, current_version
            );
            self.invalidate_all();
            return Ok(());
        }

        // Fingerprint check — catches config changes, dev rebuilds, mode changes.
        // Binary hash is only computed when version string matches (lazy — saves ~3ms for release users).
        let binary_hash = match binary_file_hash() {
            Some(h) => h,
            None => {
                info!("Cannot hash binary, forcing cache invalidation");
                self.invalidate_all();
                return Ok(());
            }
        };
        let current_fp = compute_fingerprint(
            binary_hash,
            &self.fingerprint_config,
            self.fingerprint_all_detectors,
        );
        if data.fingerprint != Some(current_fp) {
            info!("Cache fingerprint mismatch, rebuilding");
            self.invalidate_all();
            return Ok(());
        }

        self.cache = data;
        debug!("Loaded cache with {} files", self.cache.files.len());

        Ok(())
    }

    /// Persist cache to disk
    pub fn save_cache(&mut self) -> Result<()> {
        if !self.dirty {
            return Ok(());
        }

        // Compute and store fingerprint before serialization
        if let Some(binary_hash) = binary_file_hash() {
            self.cache.fingerprint = Some(compute_fingerprint(
                binary_hash,
                &self.fingerprint_config,
                self.fingerprint_all_detectors,
            ));
        }

        // Write to temp file first, then rename (atomic on POSIX)
        let tmp_file = self.cache_file.with_extension("tmp");

        let bytes = bitcode::serialize(&self.cache).context("Failed to serialize cache")?;
        fs::write(&tmp_file, &bytes).context("Failed to write temp cache file")?;

        // Atomic rename
        fs::rename(&tmp_file, &self.cache_file).context("Failed to rename temp cache")?;

        self.dirty = false;
        debug!("Saved cache with {} files", self.cache.files.len());

        Ok(())
    }

    /// Check if file has changed since last cache
    #[allow(dead_code)] // Public API
    pub fn is_file_changed(&self, path: &Path) -> bool {
        let path_key = self.path_key(path);
        match self.cache.files.get(&path_key) {
            None => true,
            Some(cached) => {
                let current_hash = self.file_hash(path);
                cached.hash != current_hash
            }
        }
    }

    /// Retrieve cached findings for a file
    pub fn cached_findings(&self, path: &Path) -> Vec<Finding> {
        let path_key = self.path_key(path);

        match self.cache.files.get(&path_key) {
            None => vec![],
            Some(cached) => {
                // Check if file changed - if so, cached findings are stale
                let current_hash = self.file_hash(path);
                if cached.hash != current_hash {
                    return vec![];
                }

                cached.findings.clone()
            }
        }
    }

    /// Store findings for a file in the cache
    pub fn cache_findings(&mut self, path: &Path, findings: &[Finding]) {
        let path_key = self.path_key(path);
        let file_hash = self.file_hash(path);

        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        self.cache.files.insert(
            path_key,
            CachedFile {
                hash: file_hash,
                findings: findings.to_vec(),
                timestamp,
                value_dependencies: Vec::new(),
                value_hashes: HashMap::new(),
            },
        );
        self.dirty = true;
        self.evict_oldest_if_over_cap();
    }

    /// If `cache.files` exceeds `MAX_CACHE_FILES`, drop the oldest-timestamp
    /// entries (and their parse-cache siblings) until it fits.
    fn evict_oldest_if_over_cap(&mut self) {
        if self.cache.files.len() <= MAX_CACHE_FILES {
            return;
        }
        let mut stamps: Vec<(String, u64)> = self
            .cache
            .files
            .iter()
            .map(|(k, v)| (k.clone(), v.timestamp))
            .collect();
        stamps.sort_by_key(|(_, ts)| *ts);
        let to_drop = stamps.len() - MAX_CACHE_FILES;
        for (key, _) in stamps.into_iter().take(to_drop) {
            self.cache.files.remove(&key);
            self.cache.parse_cache.remove(&key);
        }
        warn!(
            "Evicted {} cache entries to stay under MAX_CACHE_FILES={}",
            to_drop, MAX_CACHE_FILES
        );
    }

    /// Filter to only files that have changed since last cache
    pub fn changed_files(&self, all_files: &[PathBuf]) -> Vec<PathBuf> {
        let mut changed = Vec::new();

        for path in all_files {
            let path_key = self.path_key(path);
            match self.cache.files.get(&path_key) {
                None => changed.push(path.clone()),
                Some(cached) => {
                    let current_hash = self.file_hash(path);
                    if cached.hash != current_hash {
                        changed.push(path.clone());
                    }
                }
            }
        }

        debug!(
            "Incremental analysis: {}/{} files changed",
            changed.len(),
            all_files.len()
        );
        changed
    }

    /// Remove a file from the cache
    pub fn invalidate_file(&mut self, path: &Path) {
        let path_key = self.path_key(path);
        if self.cache.files.remove(&path_key).is_some() {
            self.dirty = true;
        }
    }

    /// Clear the entire cache
    pub fn invalidate_all(&mut self) {
        self.cache = CacheData::default();
        self.dirty = true;
    }

    /// Remove cache entries for files that no longer exist in the file list.
    /// Call this periodically to prevent unbounded cache growth.
    pub fn prune_stale_entries(&mut self, current_files: &[PathBuf]) {
        let current_keys: std::collections::HashSet<String> =
            current_files.iter().map(|p| self.path_key(p)).collect();

        let before_files = self.cache.files.len();
        let before_parse = self.cache.parse_cache.len();

        self.cache.files.retain(|k, _| current_keys.contains(k));
        self.cache
            .parse_cache
            .retain(|k, _| current_keys.contains(k));

        let pruned_files = before_files - self.cache.files.len();
        let pruned_parse = before_parse - self.cache.parse_cache.len();

        if pruned_files > 0 || pruned_parse > 0 {
            debug!(
                "Pruned {} stale file entries, {} stale parse entries",
                pruned_files, pruned_parse
            );
            self.dirty = true;
        }
    }

    // -------------------------------------------------------------------------
    // Graph-level caching methods
    // -------------------------------------------------------------------------

    /// Check if the graph has changed since last cache
    pub fn is_graph_changed(&self, current_hash: &str) -> bool {
        match &self.cache.graph.hash {
            None => true,
            Some(cached_hash) => cached_hash != current_hash,
        }
    }

    /// Compute a combined hash of all files for graph-level cache validation.
    /// Result is memoized per file count to avoid re-hashing 200+ files multiple times.
    pub fn compute_all_files_hash(&mut self, files: &[std::path::PathBuf]) -> String {
        // Return memoized hash if file count matches (same analysis run)
        if let Some((count, ref hash)) = self.memoized_files_hash {
            if count == files.len() {
                return hash.clone();
            }
        }

        let mut hasher = xxhash_rust::xxh3::Xxh3::new();

        // Sort files for deterministic hashing
        let mut sorted_files: Vec<_> = files.iter().collect();
        sorted_files.sort();

        for path in sorted_files {
            // Hash file path and content hash
            let path_bytes = path.to_string_lossy();
            hasher.update(path_bytes.as_bytes());
            hasher.update(self.file_hash(path).as_bytes());
        }

        let hash = format!("{:016x}", hasher.digest());
        self.memoized_files_hash = Some((files.len(), hash.clone()));
        hash
    }

    /// Check if we can use cached detector results
    pub fn can_use_cached_detectors(&mut self, files: &[std::path::PathBuf]) -> bool {
        let current_hash = self.compute_all_files_hash(files);
        !self.is_graph_changed(&current_hash) && !self.cache.graph.detectors.is_empty()
    }

    /// Store findings from a graph-level detector
    pub fn cache_graph_findings(&mut self, detector_name: &str, findings: &[Finding]) {
        self.cache
            .graph
            .detectors
            .insert(detector_name.to_string(), findings.to_vec());
        self.dirty = true;
    }

    /// Retrieve cached findings for a specific graph detector
    #[allow(dead_code)] // Public API
    pub fn cached_graph_findings(&self, detector_name: &str) -> Vec<Finding> {
        self.cache
            .graph
            .detectors
            .get(detector_name)
            .cloned()
            .unwrap_or_default()
    }

    /// Retrieve all cached findings from all graph detectors
    pub fn all_cached_graph_findings(&self) -> Vec<Finding> {
        self.cache
            .graph
            .detectors
            .values()
            .flatten()
            .cloned()
            .collect()
    }

    /// Update the cached graph hash after running graph detectors
    pub fn update_graph_hash(&mut self, hash: &str) {
        self.cache.graph.hash = Some(hash.to_string());
        self.dirty = true;
    }

    /// Cache the score result with sub-scores
    pub fn cache_score_with_subscores(&mut self, result: CachedScoreResult) {
        self.cache.graph.score = Some(result);
        self.dirty = true;
    }

    /// Cached score if available
    pub fn cached_score(&self) -> Option<&CachedScoreResult> {
        self.cache.graph.score.as_ref()
    }

    /// Check if we have a complete cached result (findings + score)
    pub fn has_complete_cache(&mut self, files: &[std::path::PathBuf]) -> bool {
        let current_hash = self.compute_all_files_hash(files);
        !self.is_graph_changed(&current_hash)
            && !self.cache.graph.detectors.is_empty()
            && self.cache.graph.score.is_some()
    }

    /// Cache statistics
    pub fn stats(&self) -> CacheStats {
        let total_findings: usize = self.cache.files.values().map(|f| f.findings.len()).sum();
        let graph_findings: usize = self.cache.graph.detectors.values().map(|f| f.len()).sum();

        CacheStats {
            cached_files: self.cache.files.len(),
            total_findings,
            graph_hash: self.cache.graph.hash.clone(),
            graph_detectors: self.cache.graph.detectors.len(),
            graph_findings,
            cache_version: self.cache.version,
        }
    }

    /// Record which cross-file values a file depends on, along with their current hashes.
    #[allow(dead_code)] // API for future incremental invalidation based on value changes
    pub fn set_value_dependencies(
        &mut self,
        file: &Path,
        deps: Vec<String>,
        hashes: HashMap<String, u64>,
    ) {
        let key = self.path_key(file);
        if let Some(cached) = self.cache.files.get_mut(&key) {
            cached.value_dependencies = deps;
            cached.value_hashes = hashes;
        }
    }

    /// Check if a cached file's value dependencies are still valid.
    /// Returns true if all dependencies have the same hash as when cached.
    #[allow(dead_code)] // API for future incremental invalidation based on value changes
    pub fn value_deps_valid(&self, file: &Path, current_hashes: &HashMap<String, u64>) -> bool {
        let key = self.path_key(file);
        if let Some(cached) = self.cache.files.get(&key) {
            if cached.value_dependencies.is_empty() {
                return true; // No dependencies, always valid
            }
            for dep in &cached.value_dependencies {
                let cached_hash = cached.value_hashes.get(dep);
                let current_hash = current_hashes.get(dep);
                if cached_hash != current_hash {
                    return false; // Dependency value changed
                }
            }
            true
        } else {
            true // No cache entry, nothing to invalidate
        }
    }

    /// Write a last_used marker for stale cache pruning.
    pub fn touch_last_used(&self) {
        let marker = self.cache_dir.join(".last_used");
        let _ = fs::write(&marker, chrono::Utc::now().to_rfc3339().as_bytes());
    }

    /// Convert path to cache key
    fn path_key(&self, path: &Path) -> String {
        path.canonicalize()
            .unwrap_or_else(|_| path.to_path_buf())
            .to_string_lossy()
            .to_string()
    }
}

/// Delete cache directories not used in the given duration.
/// Called once at startup. Errors silently ignored.
pub fn prune_stale_caches(max_age: std::time::Duration) {
    let cache_base = match dirs::cache_dir() {
        Some(d) => d.join("repotoire"),
        None => return,
    };

    let Ok(entries) = fs::read_dir(&cache_base) else {
        return;
    };
    let cutoff = std::time::SystemTime::now() - max_age;

    for entry in entries.flatten() {
        let marker = entry.path().join(".last_used");
        let last_used = fs::metadata(&marker)
            .and_then(|m| m.modified())
            .unwrap_or(std::time::UNIX_EPOCH);

        if last_used < cutoff {
            let _ = fs::remove_dir_all(entry.path());
            debug!("Pruned stale cache: {}", entry.path().display());
        }
    }
}

/// Compute the XXH3 content hash of a file without needing an `IncrementalCache` instance.
/// Used by `ConcurrentCacheView` to pre-validate cache entries.
fn file_hash_standalone(path: &Path) -> String {
    match fs::File::open(path) {
        Ok(mut file) => {
            let mut hasher = xxhash_rust::xxh3::Xxh3::new();
            let mut buffer = [0u8; HASH_BUFFER_SIZE];

            loop {
                match file.read(&mut buffer) {
                    Ok(0) => break,
                    Ok(n) => hasher.update(&buffer[..n]),
                    Err(_) => break,
                }
            }

            format!("{:016x}", hasher.digest())
        }
        Err(_) => format!("error:{}", path.display()),
    }
}

/// Lock-free concurrent view for parallel read access during parsing.
///
/// Created from `IncrementalCache` before entering a `par_iter` loop.
/// Contains pre-validated cache entries (file hash already checked) so the
/// parallel loop can do a simple `DashMap::get()` with no file I/O for cache
/// hits.  New parse results are collected into a separate `DashMap` and merged
/// back into the `IncrementalCache` after the loop finishes.
pub struct ConcurrentCacheView {
    /// Pre-validated cached parse results keyed by file path.
    /// Wrapped in `Arc` so cache hits are cheap atomic increments
    /// instead of deep `Vec<FunctionInfo>` / `Vec<ClassInfo>` clones.
    pub parse_cache: DashMap<PathBuf, Arc<ParseResult>>,
}

impl IncrementalCache {
    /// Create a concurrent view populated from existing cache data.
    ///
    /// Only entries whose on-disk file hash still matches the cached hash are
    /// included, so consumers can treat every entry as valid without re-hashing.
    ///
    /// `files` limits which paths are checked — entries for files not in the
    /// list are skipped to avoid unnecessary I/O.
    pub fn concurrent_view(&self, files: &[PathBuf]) -> ConcurrentCacheView {
        let parse_cache = DashMap::with_capacity(files.len());

        for file in files {
            let key = file.to_string_lossy().to_string();
            if let Some(cached) = self.cache.parse_cache.get(&key) {
                let current_hash = file_hash_standalone(file);
                if cached.hash == current_hash {
                    parse_cache.insert(file.clone(), Arc::new(cached.result.clone()));
                }
            }
        }

        ConcurrentCacheView { parse_cache }
    }

    /// Merge new parse results from a `DashMap` back into the persistent cache.
    ///
    /// Call this after the parallel parsing loop to persist newly parsed files.
    /// Accepts `Arc<ParseResult>` — the Arc is unwrapped (or cloned if shared)
    /// because the on-disk cache stores owned `ParseResult` values.
    pub fn merge_new_parse_results(&mut self, new_results: DashMap<PathBuf, Arc<ParseResult>>) {
        for (path, arc_result) in new_results.into_iter() {
            let result = Arc::try_unwrap(arc_result).unwrap_or_else(|arc| (*arc).clone());
            self.cache_parse_result(&path, &result);
        }
    }
}

impl crate::cache::CacheLayer for IncrementalCache {
    fn name(&self) -> &str {
        "incremental-findings"
    }

    fn is_populated(&self) -> bool {
        self.has_cache()
    }

    fn invalidate_files(&mut self, changed_files: &[&std::path::Path]) {
        for path in changed_files {
            self.invalidate_file(path);
        }
    }

    fn invalidate_all(&mut self) {
        // Clear all cached data
        self.cache = CacheData::default();
        self.dirty = true;
    }
}

impl Drop for IncrementalCache {
    fn drop(&mut self) {
        if let Err(e) = self.save_cache() {
            warn!("Failed to save cache on drop: {}", e);
        }
    }
}

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

    fn create_test_finding(file: &str) -> Finding {
        Finding {
            id: "test-1".to_string(),
            detector: "TestDetector".to_string(),
            severity: Severity::Medium,
            title: "Test finding".to_string(),
            description: "Test description".to_string(),
            affected_files: vec![PathBuf::from(file)],
            line_start: Some(10),
            line_end: Some(20),
            suggested_fix: None,
            estimated_effort: None,
            category: None,
            cwe_id: None,
            why_it_matters: None,
            ..Default::default()
        }
    }

    #[test]
    fn test_cache_creation() {
        let tmp = TempDir::new().expect("should create temp dir");
        let cache =
            IncrementalCache::new(tmp.path(), &crate::config::ProjectConfig::default(), false);
        let stats = cache.stats();
        assert_eq!(stats.cached_files, 0);
        assert_eq!(stats.cache_version, CACHE_VERSION);
    }

    #[test]
    fn test_file_hash() {
        let tmp = TempDir::new().expect("should create temp dir");
        let file_path = tmp.path().join("test.txt");
        fs::write(&file_path, "hello world").expect("should write test file");

        let cache =
            IncrementalCache::new(tmp.path(), &crate::config::ProjectConfig::default(), false);
        let hash1 = cache.file_hash(&file_path);
        let hash2 = cache.file_hash(&file_path);

        // Same content should have same hash
        assert_eq!(hash1, hash2);

        // Different content should have different hash
        fs::write(&file_path, "changed content").expect("should write test file");
        let hash3 = cache.file_hash(&file_path);
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn test_cache_findings() {
        let tmp = TempDir::new().expect("should create temp dir");
        let file_path = tmp.path().join("test.py");
        fs::write(&file_path, "def test(): pass").expect("should write test file");

        let mut cache =
            IncrementalCache::new(tmp.path(), &crate::config::ProjectConfig::default(), false);
        let finding = create_test_finding(&file_path.to_string_lossy());

        // Cache findings
        cache.cache_findings(&file_path, std::slice::from_ref(&finding));

        // Retrieve cached findings
        let cached = cache.cached_findings(&file_path);
        assert_eq!(cached.len(), 1);
        assert_eq!(cached[0].id, finding.id);
    }

    #[test]
    fn test_changed_files() {
        let tmp = TempDir::new().expect("should create temp dir");
        let file1 = tmp.path().join("file1.py");
        let file2 = tmp.path().join("file2.py");
        fs::write(&file1, "content1").expect("should write test file");
        fs::write(&file2, "content2").expect("should write test file");

        let mut cache =
            IncrementalCache::new(tmp.path(), &crate::config::ProjectConfig::default(), false);

        // Cache file1
        cache.cache_findings(&file1, &[]);

        // Check changed files
        let all_files = vec![file1.clone(), file2.clone()];
        let changed = cache.changed_files(&all_files);

        // Only file2 should be marked as changed (not in cache)
        assert_eq!(changed.len(), 1);
        assert_eq!(changed[0], file2);
    }

    #[test]
    fn test_graph_cache() {
        let tmp = TempDir::new().expect("should create temp dir");
        let mut cache =
            IncrementalCache::new(tmp.path(), &crate::config::ProjectConfig::default(), false);

        // Cache graph findings
        let finding = create_test_finding("test.py");
        cache.cache_graph_findings("TestDetector", &[finding]);
        cache.update_graph_hash("hash123");

        // Check graph cache
        assert!(!cache.is_graph_changed("hash123"));
        assert!(cache.is_graph_changed("different_hash"));

        let cached = cache.cached_graph_findings("TestDetector");
        assert_eq!(cached.len(), 1);
    }

    #[test]
    fn test_invalidation() {
        let tmp = TempDir::new().expect("should create temp dir");
        let file_path = tmp.path().join("test.py");
        fs::write(&file_path, "content").expect("should write test file");

        let mut cache =
            IncrementalCache::new(tmp.path(), &crate::config::ProjectConfig::default(), false);
        cache.cache_findings(&file_path, &[create_test_finding("test.py")]);

        assert_eq!(cache.stats().cached_files, 1);

        cache.invalidate_file(&file_path);
        assert_eq!(cache.stats().cached_files, 0);

        cache.cache_findings(&file_path, &[create_test_finding("test.py")]);
        cache.invalidate_all();
        assert_eq!(cache.stats().cached_files, 0);
    }

    #[test]
    fn test_incremental_cache_implements_cache_layer() {
        use crate::cache::CacheLayer;

        let tmp = TempDir::new().expect("should create temp dir");
        let mut cache =
            IncrementalCache::new(tmp.path(), &crate::config::ProjectConfig::default(), false);

        // Verify trait name
        assert_eq!(cache.name(), "incremental-findings");

        // Empty cache should not be populated
        assert!(!cache.is_populated());

        // Add file-level and parse-level entries to populate the cache
        let file_a = tmp.path().join("a.py");
        let file_b = tmp.path().join("b.py");
        fs::write(&file_a, "def foo(): pass").expect("should write test file");
        fs::write(&file_b, "def bar(): pass").expect("should write test file");

        cache.cache_findings(&file_a, &[create_test_finding("a.py")]);
        cache.cache_findings(&file_b, &[create_test_finding("b.py")]);

        // Now it should be populated
        assert!(cache.is_populated());
        assert_eq!(cache.stats().cached_files, 2);

        // invalidate_files should remove only the specified file
        let path_a_ref: &Path = &file_a;
        cache.invalidate_files(&[path_a_ref]);
        assert_eq!(cache.stats().cached_files, 1);
        // file_b should still be present
        assert!(cache.is_populated());

        // invalidate_all should clear everything
        cache.invalidate_all();
        assert!(!cache.is_populated());
        assert_eq!(cache.stats().cached_files, 0);
    }

    #[test]
    fn test_cached_finding_round_trip_preserves_threshold_metadata() {
        use crate::models::{Finding, Severity};
        use std::collections::BTreeMap;

        let mut meta = BTreeMap::new();
        meta.insert("threshold_source".to_string(), "adaptive".to_string());
        meta.insert("effective_threshold".to_string(), "15".to_string());

        let finding = Finding {
            id: "rt-1".into(),
            detector: "TestDetector".into(),
            severity: Severity::High,
            title: "Test".into(),
            description: "Desc".into(),
            confidence: Some(0.85),
            threshold_metadata: meta,
            ..Default::default()
        };

        // Round-trip through bincode (simulates cache write/read)
        let bytes = bitcode::serialize(&finding).expect("serialize");
        let restored: Finding = bitcode::deserialize(&bytes).expect("deserialize");
        assert_eq!(restored.id, "rt-1");
        assert_eq!(restored.confidence, Some(0.85));
        assert_eq!(
            restored
                .threshold_metadata
                .get("effective_threshold")
                .expect("metadata key should exist"),
            "15"
        );
        assert_eq!(
            restored
                .threshold_metadata
                .get("threshold_source")
                .expect("key should exist"),
            "adaptive"
        );
    }

    #[test]
    fn test_cache_value_dependencies_valid() {
        let dir = TempDir::new().unwrap();
        let cache_dir = dir.path().join("cache");
        std::fs::create_dir_all(&cache_dir).unwrap();
        let mut cache =
            IncrementalCache::new(&cache_dir, &crate::config::ProjectConfig::default(), false);

        let file = dir.path().join("handler.py");
        fs::write(&file, "import config").unwrap();

        // Cache the file first
        cache.cache_findings(&file, &[]);

        // Set dependencies
        let deps = vec!["config.TIMEOUT".to_string()];
        let mut hashes = HashMap::new();
        hashes.insert("config.TIMEOUT".to_string(), 12345u64);
        cache.set_value_dependencies(&file, deps, hashes);

        // Check with same hash — should be valid
        let mut current = HashMap::new();
        current.insert("config.TIMEOUT".to_string(), 12345u64);
        assert!(cache.value_deps_valid(&file, &current));
    }

    #[test]
    fn test_cache_invalidates_on_value_dependency_change() {
        let dir = TempDir::new().unwrap();
        let cache_dir = dir.path().join("cache");
        std::fs::create_dir_all(&cache_dir).unwrap();
        let mut cache =
            IncrementalCache::new(&cache_dir, &crate::config::ProjectConfig::default(), false);

        let file = dir.path().join("handler.py");
        fs::write(&file, "import config").unwrap();

        cache.cache_findings(&file, &[]);

        let deps = vec!["config.TIMEOUT".to_string()];
        let mut hashes = HashMap::new();
        hashes.insert("config.TIMEOUT".to_string(), 12345u64);
        cache.set_value_dependencies(&file, deps, hashes);

        // Check with different hash — should be invalid
        let mut current = HashMap::new();
        current.insert("config.TIMEOUT".to_string(), 99999u64);
        assert!(!cache.value_deps_valid(&file, &current));
    }

    #[test]
    fn test_cache_no_dependencies_always_valid() {
        let dir = TempDir::new().unwrap();
        let cache_dir = dir.path().join("cache");
        std::fs::create_dir_all(&cache_dir).unwrap();
        let mut cache =
            IncrementalCache::new(&cache_dir, &crate::config::ProjectConfig::default(), false);

        let file = dir.path().join("simple.py");
        fs::write(&file, "x = 1").unwrap();

        cache.cache_findings(&file, &[]);

        // No dependencies set — should always be valid
        let current = HashMap::new();
        assert!(cache.value_deps_valid(&file, &current));
    }

    #[test]
    fn test_cache_missing_dependency_in_current_invalidates() {
        let dir = TempDir::new().unwrap();
        let cache_dir = dir.path().join("cache");
        std::fs::create_dir_all(&cache_dir).unwrap();
        let mut cache =
            IncrementalCache::new(&cache_dir, &crate::config::ProjectConfig::default(), false);

        let file = dir.path().join("handler.py");
        fs::write(&file, "import config").unwrap();

        cache.cache_findings(&file, &[]);

        let deps = vec!["config.TIMEOUT".to_string()];
        let mut hashes = HashMap::new();
        hashes.insert("config.TIMEOUT".to_string(), 12345u64);
        cache.set_value_dependencies(&file, deps, hashes);

        // Dependency no longer in current hashes (e.g., constant was removed)
        let current = HashMap::new();
        assert!(!cache.value_deps_valid(&file, &current));
    }
}

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

    #[test]
    fn test_compute_fingerprint_deterministic() {
        let config = crate::config::ProjectConfig::default();
        let fp1 = compute_fingerprint(12345, &config, false);
        let fp2 = compute_fingerprint(12345, &config, false);
        assert_eq!(fp1, fp2, "Same inputs should produce same fingerprint");
    }

    #[test]
    fn test_compute_fingerprint_changes_on_binary_hash() {
        let config = crate::config::ProjectConfig::default();
        let fp1 = compute_fingerprint(12345, &config, false);
        let fp2 = compute_fingerprint(99999, &config, false);
        assert_ne!(fp1, fp2, "Different binary hash should change fingerprint");
    }

    #[test]
    fn test_compute_fingerprint_changes_on_mode() {
        let config = crate::config::ProjectConfig::default();
        let fp1 = compute_fingerprint(12345, &config, false);
        let fp2 = compute_fingerprint(12345, &config, true);
        assert_ne!(
            fp1, fp2,
            "Different all_detectors should change fingerprint"
        );
    }

    #[test]
    fn test_sort_json_keys_deterministic() {
        let json1 = serde_json::json!({"b": 2, "a": 1, "c": {"z": 3, "y": 4}});
        let json2 = serde_json::json!({"c": {"y": 4, "z": 3}, "a": 1, "b": 2});
        let s1 = serde_json::to_string(&sort_json_keys(json1)).unwrap();
        let s2 = serde_json::to_string(&sort_json_keys(json2)).unwrap();
        assert_eq!(
            s1, s2,
            "Same data with different key order should produce same output"
        );
    }
}