repotoire 0.8.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
//! DOA-based ownership model for bus factor analysis.
//!
//! Implements Degree of Authorship (Avelino et al. 2016) with exponential
//! recency decay. Used by bus factor detectors and knowledge risk reporting.

use std::collections::HashMap;

/// Runtime config for ownership computation (resolved from TOML defaults).
#[derive(Debug, Clone)]
pub struct OwnershipConfig {
    pub half_life_days: f64,
    pub author_threshold: f64,
    pub inactive_months: u32,
    pub min_file_loc: usize,
}

impl Default for OwnershipConfig {
    fn default() -> Self {
        Self {
            half_life_days: 150.0,
            author_threshold: 0.75,
            inactive_months: 6,
            min_file_loc: 10,
        }
    }
}

/// DOA score for one author on one file.
#[derive(Debug, Clone)]
pub struct FileAuthorDOA {
    pub author: String,
    pub email: String,
    pub raw_doa: f64,
    pub normalized_doa: f64,
    pub is_author: bool,
    pub is_first_author: bool,
    pub commit_count: u32,
    pub last_active: i64,
    pub is_active: bool,
}

/// Per-file ownership summary.
#[derive(Debug, Clone)]
pub struct FileOwnershipDOA {
    pub path: String,
    pub authors: Vec<FileAuthorDOA>,
    pub bus_factor: usize,
    pub hhi: f64,
    pub max_doa: f64,
}

/// Per-module (directory) ownership summary.
#[derive(Debug, Clone)]
pub struct ModuleOwnershipSummary {
    pub path: String,
    pub bus_factor: usize,
    pub avg_bus_factor: f64,
    pub hhi: f64,
    pub top_authors: Vec<(String, f64)>,
    pub risk_score: f64,
    pub file_count: usize,
    pub at_risk_file_count: usize,
    pub at_risk_pct: f64,
}

/// Author profile across all files.
#[derive(Debug, Clone)]
pub struct AuthorProfile {
    pub name: String,
    pub email: String,
    pub files_authored: usize,
    pub last_active: i64,
    pub is_active: bool,
}

/// Full ownership model, computed once and shared via Arc.
#[derive(Debug, Clone)]
pub struct OwnershipModel {
    pub files: HashMap<String, FileOwnershipDOA>,
    pub modules: HashMap<String, ModuleOwnershipSummary>,
    pub project_bus_factor: usize,
    pub author_profiles: HashMap<String, AuthorProfile>,
}

impl OwnershipModel {
    /// Empty model for when ownership is disabled or no git data.
    pub fn empty() -> Self {
        Self {
            files: HashMap::new(),
            modules: HashMap::new(),
            project_bus_factor: 0,
            author_profiles: HashMap::new(),
        }
    }
}

/// Compute DOA(e, f) = 3.293 + 1.098*FA + 0.164*DL - 0.321*ln(1+AC)
pub fn compute_doa(is_first_author: bool, dl: f64, ac: f64) -> f64 {
    let fa = if is_first_author { 1.0 } else { 0.0 };
    3.293 + 1.098 * fa + 0.164 * dl - 0.321 * (1.0 + ac).ln()
}

/// Compute exponential decay weight for a commit.
pub fn decay_weight(age_days: f64, half_life_days: f64) -> f64 {
    (-f64::ln(2.0) * age_days / half_life_days).exp()
}

/// Compute HHI from ownership shares. Shares need not sum to 1 -- they are normalized.
pub fn compute_hhi(shares: &[f64]) -> f64 {
    let total: f64 = shares.iter().sum();
    if total <= 0.0 {
        return 1.0;
    }
    shares.iter().map(|s| (s / total).powi(2)).sum()
}

use crate::git::history::CommitInfo;

/// Per-file commit data extracted from git history.
struct FileCommitData {
    first_author: String,
    author_commits: HashMap<String, (String, f64, i64)>,
    total_other_commits: HashMap<String, f64>,
}

/// Build per-file commit data from a flat list of commits.
fn build_file_commit_data(
    commits: &[CommitInfo],
    half_life_days: f64,
    now_ts: i64,
) -> HashMap<String, FileCommitData> {
    let mut files: HashMap<String, FileCommitData> = HashMap::new();

    for commit in commits {
        let commit_ts = chrono::DateTime::parse_from_rfc3339(&commit.timestamp)
            .map(|dt| dt.timestamp())
            .unwrap_or(0);
        let age_days = ((now_ts - commit_ts) as f64 / 86400.0).max(0.0);
        let weight = decay_weight(age_days, half_life_days);

        for file_path in &commit.files_changed {
            let data = files
                .entry(file_path.clone())
                .or_insert_with(|| FileCommitData {
                    first_author: commit.author.clone(),
                    author_commits: HashMap::new(),
                    total_other_commits: HashMap::new(),
                });

            let entry = data.author_commits.entry(commit.author.clone()).or_insert((
                commit.author_email.clone(),
                0.0,
                0,
            ));
            entry.1 += weight;
            if commit_ts > entry.2 {
                entry.2 = commit_ts;
            }
        }
    }

    // Fix first_author: oldest commit touching each file
    for (file_path, data) in &mut files {
        for commit in commits.iter().rev() {
            if commit.files_changed.contains(file_path) {
                data.first_author = commit.author.clone();
                break;
            }
        }

        let total_weighted: f64 = data.author_commits.values().map(|v| v.1).sum();
        for (author, (_, weighted, _)) in &data.author_commits {
            data.total_other_commits
                .insert(author.clone(), total_weighted - weighted);
        }
    }

    files
}

/// Compute the full ownership model from git history.
pub fn compute_ownership_model(commits: &[CommitInfo], config: &OwnershipConfig) -> OwnershipModel {
    if commits.is_empty() {
        return OwnershipModel::empty();
    }

    let now_ts = chrono::Utc::now().timestamp();
    let inactive_cutoff = now_ts - (config.inactive_months as i64 * 30 * 86400);

    let file_data = build_file_commit_data(commits, config.half_life_days, now_ts);

    let mut files: HashMap<String, FileOwnershipDOA> = HashMap::new();
    let mut author_profiles: HashMap<String, AuthorProfile> = HashMap::new();

    for (path, data) in &file_data {
        let mut author_doas: Vec<FileAuthorDOA> = Vec::new();

        for (author, (email, weighted_commits, last_ts)) in &data.author_commits {
            let is_first = *author == data.first_author;
            let ac = data.total_other_commits.get(author).copied().unwrap_or(0.0);
            let raw_doa = compute_doa(is_first, *weighted_commits, ac);

            author_doas.push(FileAuthorDOA {
                author: author.clone(),
                email: email.clone(),
                raw_doa,
                normalized_doa: 0.0,
                is_author: false,
                is_first_author: is_first,
                commit_count: *weighted_commits as u32,
                last_active: *last_ts,
                is_active: *last_ts >= inactive_cutoff,
            });

            let profile = author_profiles
                .entry(author.clone())
                .or_insert_with(|| AuthorProfile {
                    name: author.clone(),
                    email: email.clone(),
                    files_authored: 0,
                    last_active: 0,
                    is_active: false,
                });
            if *last_ts > profile.last_active {
                profile.last_active = *last_ts;
                profile.is_active = *last_ts >= inactive_cutoff;
            }
        }

        if !author_doas.is_empty() {
            let min_doa = author_doas
                .iter()
                .map(|a| a.raw_doa)
                .fold(f64::INFINITY, f64::min);
            let max_doa = author_doas
                .iter()
                .map(|a| a.raw_doa)
                .fold(f64::NEG_INFINITY, f64::max);
            let range = max_doa - min_doa;

            for a in &mut author_doas {
                a.normalized_doa = if range > 0.0 {
                    (a.raw_doa - min_doa) / range
                } else {
                    1.0
                };
                a.is_author = a.normalized_doa > config.author_threshold && a.raw_doa > 3.293;
            }
        }

        for a in &author_doas {
            if a.is_author {
                if let Some(p) = author_profiles.get_mut(&a.author) {
                    p.files_authored += 1;
                }
            }
        }

        author_doas.sort_by(|a, b| {
            b.raw_doa
                .partial_cmp(&a.raw_doa)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let bus_factor = author_doas.iter().filter(|a| a.is_author).count();
        let shares: Vec<f64> = author_doas.iter().map(|a| a.normalized_doa).collect();
        let hhi = compute_hhi(&shares);
        let max_doa_val = author_doas.first().map(|a| a.normalized_doa).unwrap_or(0.0);

        files.insert(
            path.clone(),
            FileOwnershipDOA {
                path: path.clone(),
                authors: author_doas,
                bus_factor,
                hhi,
                max_doa: max_doa_val,
            },
        );
    }

    let modules = aggregate_modules(&files);
    let project_bus_factor = compute_project_bus_factor(&files);

    OwnershipModel {
        files,
        modules,
        project_bus_factor,
        author_profiles,
    }
}

/// Aggregate file ownership into module (directory) summaries.
fn aggregate_modules(
    files: &HashMap<String, FileOwnershipDOA>,
) -> HashMap<String, ModuleOwnershipSummary> {
    use crate::detectors::base::is_non_production_file;

    let mut module_files: HashMap<String, Vec<&FileOwnershipDOA>> = HashMap::new();

    for (path, ownership) in files {
        if is_non_production_file(std::path::Path::new(path)) {
            continue;
        }
        // Note: `Path::new("foo.py").parent()` returns `Some("")` (an empty
        // path), not `None`. We treat that — and any other empty parent — as
        // the repository root marker `.` so it never propagates as `''` into
        // user-facing finding titles or affected_files paths.
        let dir = std::path::Path::new(path)
            .parent()
            .and_then(|p| p.to_str())
            .filter(|s| !s.is_empty())
            .unwrap_or(".")
            .to_string();
        module_files.entry(dir).or_default().push(ownership);
    }

    let mut modules = HashMap::new();
    for (dir, file_ownerships) in &module_files {
        if file_ownerships.is_empty() {
            continue;
        }

        let file_count = file_ownerships.len();
        let mut bus_factors: Vec<usize> = file_ownerships.iter().map(|f| f.bus_factor).collect();
        bus_factors.sort();

        let p10_idx = (file_count as f64 * 0.1).ceil() as usize;
        let p10_idx = p10_idx.saturating_sub(1).min(bus_factors.len() - 1);
        let bus_factor = bus_factors[p10_idx];

        let avg_bus_factor = bus_factors.iter().sum::<usize>() as f64 / file_count as f64;
        let avg_hhi = file_ownerships.iter().map(|f| f.hhi).sum::<f64>() / file_count as f64;
        let at_risk_count = file_ownerships.iter().filter(|f| f.bus_factor <= 1).count();
        let at_risk_pct = at_risk_count as f64 / file_count as f64;

        let mut author_totals: HashMap<String, f64> = HashMap::new();
        for fo in file_ownerships {
            for a in &fo.authors {
                *author_totals.entry(a.author.clone()).or_default() += a.normalized_doa;
            }
        }
        let mut top_authors: Vec<(String, f64)> = author_totals.into_iter().collect();
        top_authors.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        top_authors.truncate(3);

        let risk_score =
            ((1.0 - avg_bus_factor / 5.0).max(0.0) * 0.4 + avg_hhi * 0.3 + at_risk_pct * 0.3)
                .clamp(0.0, 1.0);

        modules.insert(
            dir.clone(),
            ModuleOwnershipSummary {
                path: dir.clone(),
                bus_factor,
                avg_bus_factor,
                hhi: avg_hhi,
                top_authors,
                risk_score,
                file_count,
                at_risk_file_count: at_risk_count,
                at_risk_pct,
            },
        );
    }

    modules
}

/// Greedy set cover: minimum authors to remove before >50% files are orphaned.
fn compute_project_bus_factor(files: &HashMap<String, FileOwnershipDOA>) -> usize {
    let mut author_files: HashMap<String, Vec<String>> = HashMap::new();
    for (path, fo) in files {
        for a in &fo.authors {
            if a.is_author {
                author_files
                    .entry(a.author.clone())
                    .or_default()
                    .push(path.clone());
            }
        }
    }

    let total_files = files.len();
    if total_files == 0 {
        return 0;
    }

    let mut file_author_count: HashMap<String, usize> = files
        .iter()
        .map(|(p, fo)| (p.clone(), fo.authors.iter().filter(|a| a.is_author).count()))
        .collect();

    let threshold = total_files / 2;
    let mut removed = 0;

    loop {
        let covered = file_author_count.values().filter(|&&c| c > 0).count();
        if covered <= threshold {
            break;
        }

        let best_author = author_files
            .iter()
            .filter(|(_, af)| !af.is_empty())
            .max_by_key(|(_, af)| {
                af.iter()
                    .filter(|f| file_author_count.get(*f).copied().unwrap_or(0) > 0)
                    .count()
            });

        let best = match best_author {
            Some((author, _)) => author.clone(),
            None => break,
        };

        if let Some(affected_files) = author_files.remove(&best) {
            for f in &affected_files {
                if let Some(count) = file_author_count.get_mut(f) {
                    *count = count.saturating_sub(1);
                }
            }
        }
        removed += 1;
    }

    removed
}

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

    #[test]
    fn test_doa_first_author_no_others() {
        let doa = compute_doa(true, 10.0, 0.0);
        assert!((doa - 6.031).abs() < 0.001, "got {doa}");
    }

    #[test]
    fn test_doa_non_first_author() {
        let doa = compute_doa(false, 5.0, 20.0);
        assert!((doa - 3.135).abs() < 0.05, "got {doa}");
    }

    #[test]
    fn test_decay_weight_zero_age() {
        assert!((decay_weight(0.0, 150.0) - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_decay_weight_one_half_life() {
        assert!((decay_weight(150.0, 150.0) - 0.5).abs() < 0.001);
    }

    #[test]
    fn test_decay_weight_two_half_lives() {
        assert!((decay_weight(300.0, 150.0) - 0.25).abs() < 0.001);
    }

    #[test]
    fn test_hhi_single_author() {
        assert!((compute_hhi(&[1.0]) - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_hhi_equal_four() {
        assert!((compute_hhi(&[0.25, 0.25, 0.25, 0.25]) - 0.25).abs() < 0.001);
    }

    #[test]
    fn test_hhi_dominated() {
        assert!((compute_hhi(&[0.9, 0.1]) - 0.82).abs() < 0.001);
    }

    #[test]
    fn test_hhi_empty() {
        assert!((compute_hhi(&[]) - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_project_bus_factor_single_author() {
        let mut files = HashMap::new();
        for i in 0..10 {
            files.insert(
                format!("file{i}.rs"),
                FileOwnershipDOA {
                    path: format!("file{i}.rs"),
                    authors: vec![FileAuthorDOA {
                        author: "alice".into(),
                        email: "a@x.com".into(),
                        raw_doa: 5.0,
                        normalized_doa: 1.0,
                        is_author: true,
                        is_first_author: true,
                        commit_count: 10,
                        last_active: 0,
                        is_active: true,
                    }],
                    bus_factor: 1,
                    hhi: 1.0,
                    max_doa: 1.0,
                },
            );
        }
        assert_eq!(compute_project_bus_factor(&files), 1);
    }

    #[test]
    fn test_project_bus_factor_two_authors_even_split() {
        let mut files = HashMap::new();
        for i in 0..10 {
            let author = if i < 5 { "alice" } else { "bob" };
            files.insert(
                format!("file{i}.rs"),
                FileOwnershipDOA {
                    path: format!("file{i}.rs"),
                    authors: vec![FileAuthorDOA {
                        author: author.into(),
                        email: "x@x.com".into(),
                        raw_doa: 5.0,
                        normalized_doa: 1.0,
                        is_author: true,
                        is_first_author: true,
                        commit_count: 10,
                        last_active: 0,
                        is_active: true,
                    }],
                    bus_factor: 1,
                    hhi: 1.0,
                    max_doa: 1.0,
                },
            );
        }
        assert_eq!(compute_project_bus_factor(&files), 1);
    }

    #[test]
    fn test_empty_model() {
        let model = OwnershipModel::empty();
        assert_eq!(model.project_bus_factor, 0);
        assert!(model.files.is_empty());
    }

    /// Files at the repository root have `Path::parent() == Some("")`, not
    /// `None`, so the previous `unwrap_or(".")` never fired. This used to
    /// produce a module key of `""` which surfaced as `'' ` in finding
    /// titles (qa-audit-2026-05-07/03-functional.md #2). The fix filters
    /// empty strings before falling back to `.`.
    #[test]
    fn test_aggregate_modules_root_files_normalize_to_dot() {
        let mut files = HashMap::new();
        for name in ["main.py", "helper.py"] {
            files.insert(
                name.to_string(),
                FileOwnershipDOA {
                    path: name.to_string(),
                    authors: vec![FileAuthorDOA {
                        author: "alice".into(),
                        email: "a@x.com".into(),
                        raw_doa: 5.0,
                        normalized_doa: 1.0,
                        is_author: true,
                        is_first_author: true,
                        commit_count: 5,
                        last_active: 0,
                        is_active: true,
                    }],
                    bus_factor: 1,
                    hhi: 1.0,
                    max_doa: 1.0,
                },
            );
        }
        let modules = aggregate_modules(&files);
        // Empty-string key would mean the bug regressed.
        assert!(
            !modules.contains_key(""),
            "root-level files must not produce an empty module key, got: {:?}",
            modules.keys().collect::<Vec<_>>()
        );
        // They should be aggregated under "." instead.
        assert!(
            modules.contains_key("."),
            "root-level files should aggregate under '.', got: {:?}",
            modules.keys().collect::<Vec<_>>()
        );
        assert_eq!(modules["."].file_count, 2);
        assert_eq!(modules["."].path, ".");
    }

    /// Mixed root + subdirectory files: each should land in its own module
    /// bucket (`.` and `src`), not collapse together.
    #[test]
    fn test_aggregate_modules_mixed_root_and_subdir() {
        let mut files = HashMap::new();
        let mk = |name: &str| FileOwnershipDOA {
            path: name.to_string(),
            authors: vec![FileAuthorDOA {
                author: "alice".into(),
                email: "a@x.com".into(),
                raw_doa: 5.0,
                normalized_doa: 1.0,
                is_author: true,
                is_first_author: true,
                commit_count: 5,
                last_active: 0,
                is_active: true,
            }],
            bus_factor: 1,
            hhi: 1.0,
            max_doa: 1.0,
        };
        files.insert("main.py".to_string(), mk("main.py"));
        files.insert("src/lib.py".to_string(), mk("src/lib.py"));
        files.insert("src/util.py".to_string(), mk("src/util.py"));

        let modules = aggregate_modules(&files);
        assert!(!modules.contains_key(""));
        assert_eq!(modules.get(".").map(|m| m.file_count), Some(1));
        assert_eq!(modules.get("src").map(|m| m.file_count), Some(2));
    }
}