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
//! Core data models for Repotoire
//!
//! These models are used throughout the codebase for representing
//! code entities, findings, and analysis results.

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Generate a deterministic finding ID based on content hash.
///
/// This ensures findings have stable IDs across runs, enabling:
/// - Tracking findings over time (fixed vs new vs recurring)
/// - Suppression by ID in config files
/// - Reliable deduplication
///
/// The ID is a 16-character hex string derived from hashing:
/// - detector name (which detector found it)
/// - file path (where it was found)
/// - line number (specific location)
/// - title (what the issue is)
pub fn deterministic_finding_id(detector: &str, file: &str, line: u32, _title: &str) -> String {
    // Note: postprocessing overwrites all IDs via finding_id() (#73).
    // Uses FNV-1a for cross-toolchain stability (see finding_id).
    crate::detectors::base::finding_id(detector, file, line)
}

/// Severity levels for findings
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    Default,
    clap::ValueEnum,
)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
    #[default]
    Info,
    Low,
    Medium,
    High,
    Critical,
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Severity::Info => write!(f, "info"),
            Severity::Low => write!(f, "low"),
            Severity::Medium => write!(f, "medium"),
            Severity::High => write!(f, "high"),
            Severity::Critical => write!(f, "critical"),
        }
    }
}

impl std::str::FromStr for Severity {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "critical" => Ok(Severity::Critical),
            "high" => Ok(Severity::High),
            "medium" => Ok(Severity::Medium),
            "low" => Ok(Severity::Low),
            "info" => Ok(Severity::Info),
            _ => Err(anyhow::anyhow!(
                "Unknown severity '{}'. Valid: critical, high, medium, low, info",
                s
            )),
        }
    }
}

fn serialize_empty_map_as_null<S>(
    map: &std::collections::BTreeMap<String, String>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    if map.is_empty() {
        Option::<std::collections::BTreeMap<String, String>>::None.serialize(serializer)
    } else {
        Some(map).serialize(serializer)
    }
}

/// Deserialize a BTreeMap that may be `null` in JSON (treat null as empty map)
fn deserialize_null_as_empty_map<'de, D>(
    // repotoire:ignore[surprisal]
    deserializer: D,
) -> Result<std::collections::BTreeMap<String, String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let opt = Option::<std::collections::BTreeMap<String, String>>::deserialize(deserializer)?;
    Ok(opt.unwrap_or_default())
}

/// Status of a finding relative to the baseline.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum FindingStatus {
    #[default]
    New,
    Baselined,
    Fixed,
    Stale,
}

/// Attribution of a finding to a delta (changed code vs unrelated).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum Attribution {
    InChangedNode,
    InCallerOfChanged,
    #[default]
    InUnrelated,
}

/// Confidence level for a finding (Low < Medium < High).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Confidence {
    Low,
    Medium,
    High,
}

impl Confidence {
    /// Map a numeric score (0.0–1.0) to a confidence level.
    pub fn from_score(score: f64) -> Self {
        if score >= 0.75 {
            Confidence::High
        } else if score >= 0.5 {
            Confidence::Medium
        } else {
            Confidence::Low
        }
    }
}

/// Inclusive line range within a file, used both as a query target
/// (git history, blame) and as the source-position pair on `Finding`.
///
/// Construct via [`LineRange::new`] for a known range, or pass an
/// `Option<LineRange>` when either endpoint may be missing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct LineRange {
    pub start: u32,
    pub end: u32,
}

impl LineRange {
    pub fn new(start: u32, end: u32) -> Self {
        Self { start, end }
    }

    /// True when the start line is non-zero and the end line is at
    /// least the start line. Mirrors the validation that several
    /// callers (e.g. `blame_lines`) already perform inline.
    pub fn is_valid(self) -> bool {
        self.start != 0 && self.end >= self.start
    }

    pub fn contains_line(self, line: u32) -> bool {
        line >= self.start && line <= self.end
    }
}

/// A code smell or issue finding
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Finding {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub detector: String,
    #[serde(default)]
    pub severity: Severity,
    #[serde(default)]
    pub title: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub affected_files: Vec<PathBuf>,
    #[serde(default)]
    pub line_start: Option<u32>,
    #[serde(default)]
    pub line_end: Option<u32>,
    #[serde(default)]
    pub suggested_fix: Option<String>,
    #[serde(default)]
    pub estimated_effort: Option<String>,
    #[serde(default)]
    pub category: Option<String>,
    #[serde(default)]
    pub cwe_id: Option<String>,
    #[serde(default)]
    pub why_it_matters: Option<String>,
    /// Confidence score from 0.0 to 1.0 (set by voting engine or detector)
    #[serde(default)]
    pub confidence: Option<f64>,
    /// Whether this finding was produced by a deterministic (mathematically provable) detector.
    /// Deterministic findings bypass statistical FP classifiers.
    #[serde(default)]
    pub deterministic: bool,
    /// Threshold metadata for adaptive explainability
    /// Keys: threshold_source, effective_threshold, actual_value, default_threshold
    #[serde(
        default,
        serialize_with = "serialize_empty_map_as_null",
        deserialize_with = "deserialize_null_as_empty_map"
    )]
    pub threshold_metadata: std::collections::BTreeMap<String, String>,
    /// Finding status relative to baseline (New, Baselined, Fixed, Stale).
    #[serde(default)]
    pub status: FindingStatus,
    /// Attribution to delta analysis (changed node, caller, or unrelated).
    #[serde(default)]
    pub attribution: Attribution,
    /// Original severity before config remap (if remapped).
    #[serde(default)]
    pub original_severity: Option<Severity>,
}

/// Default confidence value when no category-specific default applies.
const DEFAULT_CONFIDENCE: f64 = 0.70;

impl Finding {
    /// Set a default confidence value if none has been set by a detector.
    ///
    /// This is a builder-style method: it returns `self` so it can be chained.
    /// If `self.confidence` is already `Some(_)`, the value is left untouched.
    pub fn with_default_confidence(mut self, default: f64) -> Self {
        if self.confidence.is_none() {
            self.confidence = Some(default);
        }
        self
    }

    /// Return the effective confidence for this finding.
    ///
    /// If a detector or the postprocess pipeline has set an explicit confidence,
    /// that value is returned. Otherwise falls back to 0.70.
    pub fn effective_confidence(&self) -> f64 {
        self.confidence.unwrap_or(DEFAULT_CONFIDENCE)
    }

    /// Return the default confidence for a finding based on its category string.
    ///
    /// | Category          | Default | Rationale                                  |
    /// |-------------------|---------|--------------------------------------------|
    /// | "architecture"    | 0.85    | Structural evidence is strong              |
    /// | "security"        | 0.75    | Taint analysis is good but not perfect     |
    /// | "design"          | 0.65    | Code smell detection has higher FP rate     |
    /// | "dead-code"/"dead_code" | 0.70 | Graph-based but may miss dynamic dispatch |
    /// | "ai_watchdog"     | 0.60    | Heuristic detection                        |
    /// | Others            | 0.70    | Reasonable default                         |
    pub fn default_confidence_for_category(category: Option<&str>) -> f64 {
        match category {
            Some("architecture") => 0.85,
            Some("security") => 0.75,
            Some("design") => 0.65,
            Some("dead-code") | Some("dead_code") => 0.70,
            Some("ai_watchdog") => 0.60,
            _ => DEFAULT_CONFIDENCE,
        }
    }
}

/// Summary of findings by severity
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FindingsSummary {
    pub critical: usize,
    pub high: usize,
    pub medium: usize,
    pub low: usize,
    pub info: usize,
    pub total: usize,
}

impl FindingsSummary {
    pub fn from_findings(findings: &[Finding]) -> Self {
        let mut summary = Self::default();
        for f in findings {
            match f.severity {
                Severity::Critical => summary.critical += 1,
                Severity::High => summary.high += 1,
                Severity::Medium => summary.medium += 1,
                Severity::Low => summary.low += 1,
                Severity::Info => summary.info += 1,
            }
            summary.total += 1;
        }
        summary
    }
}

/// Letter grades for code health (13 levels: A+ through F).
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default,
)]
pub enum Grade {
    #[default]
    F,
    #[serde(rename = "D-")]
    DMinus,
    D,
    #[serde(rename = "D+")]
    DPlus,
    #[serde(rename = "C-")]
    CMinus,
    C,
    #[serde(rename = "C+")]
    CPlus,
    #[serde(rename = "B-")]
    BMinus,
    B,
    #[serde(rename = "B+")]
    BPlus,
    #[serde(rename = "A-")]
    AMinus,
    A,
    #[serde(rename = "A+")]
    APlus,
}

impl std::fmt::Display for Grade {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Grade::APlus => write!(f, "A+"),
            Grade::A => write!(f, "A"),
            Grade::AMinus => write!(f, "A-"),
            Grade::BPlus => write!(f, "B+"),
            Grade::B => write!(f, "B"),
            Grade::BMinus => write!(f, "B-"),
            Grade::CPlus => write!(f, "C+"),
            Grade::C => write!(f, "C"),
            Grade::CMinus => write!(f, "C-"),
            Grade::DPlus => write!(f, "D+"),
            Grade::D => write!(f, "D"),
            Grade::DMinus => write!(f, "D-"),
            Grade::F => write!(f, "F"),
        }
    }
}

impl std::str::FromStr for Grade {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "A+" => Ok(Grade::APlus),
            "A" => Ok(Grade::A),
            "A-" => Ok(Grade::AMinus),
            "B+" => Ok(Grade::BPlus),
            "B" => Ok(Grade::B),
            "B-" => Ok(Grade::BMinus),
            "C+" => Ok(Grade::CPlus),
            "C" => Ok(Grade::C),
            "C-" => Ok(Grade::CMinus),
            "D+" => Ok(Grade::DPlus),
            "D" => Ok(Grade::D),
            "D-" => Ok(Grade::DMinus),
            "F" => Ok(Grade::F),
            _ => Err(anyhow::anyhow!("Unknown grade '{}'", s)),
        }
    }
}

/// Overall health report for a codebase
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthReport {
    pub overall_score: f64,
    pub grade: Grade,
    pub structure_score: f64,
    pub quality_score: f64,
    pub architecture_score: Option<f64>,
    pub findings: Vec<Finding>,
    pub findings_summary: FindingsSummary,
    pub total_files: usize,
    pub total_functions: usize,
    pub total_classes: usize,
    pub total_loc: usize,
}

impl HealthReport {
    /// Calculate grade from score
    pub fn grade_from_score(score: f64) -> String {
        match score {
            s if s >= 90.0 => "A".to_string(),
            s if s >= 80.0 => "B".to_string(),
            s if s >= 70.0 => "C".to_string(),
            s if s >= 60.0 => "D".to_string(),
            _ => "F".to_string(),
        }
    }
}

/// A function in the code graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Function {
    pub name: String,
    pub qualified_name: String,
    pub file_path: PathBuf,
    pub line_start: u32,
    pub line_end: u32,
    pub parameters: Vec<String>,
    pub return_type: Option<String>,
    pub is_async: bool,
    pub complexity: Option<u32>,
    /// Maximum nesting depth within this function
    pub max_nesting: Option<u32>,
    /// Doc comment (Javadoc, JSDoc, Go doc, etc.)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc_comment: Option<String>,
    /// Annotations/decorators (e.g., Java @Override, @Deprecated)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<String>,
}

/// A class in the code graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Class {
    pub name: String,
    pub qualified_name: String,
    pub file_path: PathBuf,
    pub line_start: u32,
    pub line_end: u32,
    pub methods: Vec<String>,
    /// Number of struct fields (Rust named/tuple) or enum variants.
    #[serde(default)]
    pub field_count: usize,
    pub bases: Vec<String>,
    /// Doc comment (Javadoc, JSDoc, Go doc, etc.)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc_comment: Option<String>,
    /// Annotations/decorators (e.g., Java @Override, @Deprecated)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<String>,
}

/// A file in the code graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct File {
    pub path: PathBuf,
    pub language: String,
    pub lines_of_code: usize,
    pub functions: usize,
    pub classes: usize,
}

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

    #[test]
    fn test_finding_serde_round_trip() {
        let finding = Finding {
            id: "test-1".into(),
            detector: "TestDetector".into(),
            severity: Severity::High,
            title: "Test finding".into(),
            description: "A test".into(),
            threshold_metadata: {
                let mut m = std::collections::BTreeMap::new();
                m.insert("key".into(), "value".into());
                m
            },
            ..Default::default()
        };
        let json = serde_json::to_string(&finding).expect("serialize finding");
        let back: Finding = serde_json::from_str(&json).expect("deserialize finding");
        assert_eq!(back.id, "test-1");
        assert_eq!(
            back.threshold_metadata.get("key").expect("key exists"),
            "value"
        );
    }

    #[test]
    fn test_finding_deserialize_null_threshold_metadata() {
        let json = r#"{"id":"t1","detector":"D","severity":"high","title":"T","description":"","affected_files":[],"threshold_metadata":null}"#;
        let finding: Finding =
            serde_json::from_str(json).expect("deserialize finding with null metadata");
        assert!(finding.threshold_metadata.is_empty());
    }

    #[test]
    fn test_finding_deserialize_missing_threshold_metadata() {
        let json = r#"{"id":"t1","detector":"D","severity":"high","title":"T","description":"","affected_files":[]}"#;
        let finding: Finding =
            serde_json::from_str(json).expect("deserialize finding with missing metadata");
        assert!(finding.threshold_metadata.is_empty());
    }

    #[test]
    fn test_finding_bincode_round_trip_with_threshold_metadata() {
        let finding = Finding {
            id: "test-bin".into(),
            detector: "TestDetector".into(),
            severity: Severity::High,
            title: "Test finding".into(),
            description: "A test".into(),
            confidence: Some(0.85),
            threshold_metadata: {
                let mut m = std::collections::BTreeMap::new();
                m.insert("threshold_source".into(), "adaptive".into());
                m.insert("effective_threshold".into(), "15".into());
                m
            },
            ..Default::default()
        };

        let bytes = bitcode::serialize(&finding).expect("serialize finding");
        let back: Finding = bitcode::deserialize(&bytes).expect("deserialize finding");

        assert_eq!(back.id, "test-bin");
        assert_eq!(back.confidence, Some(0.85));
        assert_eq!(
            back.threshold_metadata
                .get("threshold_source")
                .expect("key exists"),
            "adaptive"
        );
        assert_eq!(
            back.threshold_metadata
                .get("effective_threshold")
                .expect("key exists"),
            "15"
        );
    }

    #[test]
    fn test_health_report_grade_from_score() {
        assert_eq!(HealthReport::grade_from_score(95.0), "A");
        assert_eq!(HealthReport::grade_from_score(85.0), "B");
        assert_eq!(HealthReport::grade_from_score(75.0), "C");
        assert_eq!(HealthReport::grade_from_score(65.0), "D");
        assert_eq!(HealthReport::grade_from_score(50.0), "F");
    }

    #[test]
    fn test_findings_summary_from_findings() {
        let findings = vec![
            Finding {
                severity: Severity::Critical,
                ..Default::default()
            },
            Finding {
                severity: Severity::High,
                ..Default::default()
            },
            Finding {
                severity: Severity::High,
                ..Default::default()
            },
            Finding {
                severity: Severity::Medium,
                ..Default::default()
            },
            Finding {
                severity: Severity::Low,
                ..Default::default()
            },
        ];
        let summary = FindingsSummary::from_findings(&findings);
        assert_eq!(summary.critical, 1);
        assert_eq!(summary.high, 2);
        assert_eq!(summary.medium, 1);
        assert_eq!(summary.low, 1);
        assert_eq!(summary.total, 5);
    }

    // ── with_default_confidence ────────────────────────────────────

    #[test]
    fn test_with_default_confidence_sets_when_none() {
        let finding = Finding {
            confidence: None,
            ..Default::default()
        };
        let finding = finding.with_default_confidence(0.85);
        assert_eq!(finding.confidence, Some(0.85));
    }

    #[test]
    fn test_with_default_confidence_preserves_existing() {
        let finding = Finding {
            confidence: Some(0.90),
            ..Default::default()
        };
        let finding = finding.with_default_confidence(0.50);
        assert_eq!(finding.confidence, Some(0.90));
    }

    // ── effective_confidence ────────────────────────────────────────

    #[test]
    fn test_effective_confidence_returns_set_value() {
        let finding = Finding {
            confidence: Some(0.42),
            ..Default::default()
        };
        assert!((finding.effective_confidence() - 0.42).abs() < f64::EPSILON);
    }

    #[test]
    fn test_effective_confidence_returns_default_when_none() {
        let finding = Finding {
            confidence: None,
            ..Default::default()
        };
        assert!((finding.effective_confidence() - 0.70).abs() < f64::EPSILON);
    }

    // ── default_confidence_for_category ─────────────────────────────

    #[test]
    fn test_default_confidence_architecture() {
        assert!(
            (Finding::default_confidence_for_category(Some("architecture")) - 0.85).abs()
                < f64::EPSILON
        );
    }

    #[test]
    fn test_default_confidence_security() {
        assert!(
            (Finding::default_confidence_for_category(Some("security")) - 0.75).abs()
                < f64::EPSILON
        );
    }

    #[test]
    fn test_default_confidence_design() {
        assert!(
            (Finding::default_confidence_for_category(Some("design")) - 0.65).abs() < f64::EPSILON
        );
    }

    #[test]
    fn test_default_confidence_dead_code_hyphen() {
        assert!(
            (Finding::default_confidence_for_category(Some("dead-code")) - 0.70).abs()
                < f64::EPSILON
        );
    }

    #[test]
    fn test_default_confidence_dead_code_underscore() {
        assert!(
            (Finding::default_confidence_for_category(Some("dead_code")) - 0.70).abs()
                < f64::EPSILON
        );
    }

    #[test]
    fn test_default_confidence_ai_watchdog() {
        assert!(
            (Finding::default_confidence_for_category(Some("ai_watchdog")) - 0.60).abs()
                < f64::EPSILON
        );
    }

    #[test]
    fn test_default_confidence_unknown_category() {
        assert!(
            (Finding::default_confidence_for_category(Some("testing")) - 0.70).abs() < f64::EPSILON
        );
    }

    #[test]
    fn test_default_confidence_none_category() {
        assert!((Finding::default_confidence_for_category(None) - 0.70).abs() < f64::EPSILON);
    }
}