repotoire 0.5.3

Graph-powered code analysis CLI. 106 detectors for security, architecture, 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
//! Report context building for the analysis engine.
//!
//! Extracted from `engine/mod.rs` — pure data aggregation methods that
//! assemble `ReportContext` from analysis results for reporters to consume.

use crate::graph::traits::GraphQuery;

impl super::AnalysisEngine {
    // ── Report context building ──────────────────────────────────────────

    /// Build a `ReportContext` from the current engine state.
    ///
    /// Called by the CLI after analysis completes. Reads from the frozen CodeGraph
    /// (via `GraphQuery`), the retained `CoChangeMatrix`, and the filesystem to
    /// assemble a `ReportContext` that reporters consume.
    pub fn build_report_context(
        &self,
        health: crate::models::HealthReport,
        format: crate::reporters::OutputFormat,
    ) -> anyhow::Result<crate::reporters::report_context::ReportContext> {
        use crate::reporters::OutputFormat;
        use crate::reporters::report_context::ReportContext;

        let needs_rich = matches!(format, OutputFormat::Html | OutputFormat::Text);

        let graph_data = if needs_rich {
            self.build_graph_data()
        } else {
            None
        };

        let git_data = if needs_rich {
            self.build_git_data()
        } else {
            None
        };

        let source_snippets = if matches!(format, OutputFormat::Html) {
            self.build_snippets(&health.findings)
        } else {
            Vec::new()
        };

        let previous_health = self.load_previous_health();

        let style_profile = self
            .state
            .as_ref()
            .map(|s| s.style_profile.clone());

        // Enrich modules with finding counts from the health report
        let graph_data = graph_data.map(|mut gd| {
            let mut dir_findings: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
            for finding in &health.findings {
                if let Some(file) = finding.affected_files.first() {
                    let dir = file.parent()
                        .and_then(|p| p.to_str())
                        .unwrap_or(".")
                        .to_string();
                    *dir_findings.entry(dir).or_default() += 1;
                }
            }
            for module in &mut gd.modules {
                module.finding_count = dir_findings.get(&module.path).copied().unwrap_or(0);
                module.finding_density = if module.loc > 0 {
                    (module.finding_count as f64) / (module.loc as f64 / 1000.0)
                } else {
                    0.0
                };
                module.health_score = (100.0 - module.finding_density * 10.0).clamp(0.0, 100.0);
            }
            gd
        });

        Ok(ReportContext {
            health,
            graph_data,
            git_data,
            source_snippets,
            previous_health,
            style_profile,
        })
    }

    /// Build graph-derived data for rich reporters.
    fn build_graph_data(&self) -> Option<crate::reporters::report_context::GraphData> {
        use crate::reporters::report_context::GraphData;

        let graph = self.graph()?;
        let interner = graph.interner();

        // Top PageRank (functions, top 20)
        let mut pr_scores: Vec<(String, f64)> = graph
            .functions_idx()
            .iter()
            .filter_map(|&idx| {
                let node = graph.node_idx(idx)?;
                let score = graph.primitives().page_rank.get(&idx).copied().unwrap_or(0.0);
                if score > 0.0 {
                    Some((interner.resolve(node.qualified_name).to_string(), score))
                } else {
                    None
                }
            })
            .collect();
        pr_scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        pr_scores.truncate(20);

        // Top betweenness (functions, top 20)
        let mut bw_scores: Vec<(String, f64)> = graph
            .functions_idx()
            .iter()
            .filter_map(|&idx| {
                let node = graph.node_idx(idx)?;
                let score = graph.primitives().betweenness.get(&idx).copied().unwrap_or(0.0);
                if score > 0.0 {
                    Some((interner.resolve(node.qualified_name).to_string(), score))
                } else {
                    None
                }
            })
            .collect();
        bw_scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        bw_scores.truncate(20);

        // Articulation points
        let art_points: Vec<String> = graph
            .primitives().articulation_points
            .iter()
            .filter_map(|&idx| {
                let node = graph.node_idx(idx)?;
                Some(interner.resolve(node.qualified_name).to_string())
            })
            .collect();

        // Call cycles
        let call_cycles: Vec<Vec<String>> = graph
            .primitives().call_cycles
            .iter()
            .map(|cycle| {
                cycle
                    .iter()
                    .filter_map(|&idx| {
                        let node = graph.node_idx(idx)?;
                        Some(interner.resolve(node.qualified_name).to_string())
                    })
                    .collect()
            })
            .collect();

        // Aggregate modules
        let modules = self.aggregate_modules(graph);
        let module_edges = self.aggregate_module_edges(graph, &modules);
        let (communities, modularity) = self.map_communities(graph, &modules);

        Some(GraphData {
            modules,
            module_edges,
            communities,
            modularity,
            top_pagerank: pr_scores,
            top_betweenness: bw_scores,
            articulation_points: art_points,
            call_cycles,
        })
    }

    /// Build git-derived data (hidden coupling, co-change, file ownership).
    fn build_git_data(&self) -> Option<crate::reporters::report_context::GitData> {
        use crate::reporters::report_context::GitData;

        let graph = self.graph()?;
        let interner = graph.interner();

        // Hidden coupling from graph primitives
        let hidden_coupling: Vec<(String, String, f32)> = graph
            .primitives().hidden_coupling
            .iter()
            .filter_map(|&(a, b, w, _lift, _confidence)| {
                let na = graph.node_idx(a)?;
                let nb = graph.node_idx(b)?;
                Some((
                    interner.resolve(na.qualified_name).to_string(),
                    interner.resolve(nb.qualified_name).to_string(),
                    w,
                ))
            })
            .collect();

        // Top co-change from CoChangeMatrix (top 20)
        let mut top_co_change: Vec<(String, String, f32)> = Vec::new();
        if let Some(matrix) = self.co_change() {
            let gi = crate::graph::interner::global_interner();
            let mut pairs: Vec<(String, String, f32)> = matrix
                .iter()
                .map(|(&(a, b), &w)| {
                    (gi.resolve(a).to_string(), gi.resolve(b).to_string(), w)
                })
                .collect();
            pairs.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
            pairs.truncate(20);
            top_co_change = pairs;
        }

        // File ownership from ExtraProps
        let file_ownership = self.compute_file_ownership(graph);

        // Bus factor: files with only 1 author
        let bus_factor_files: Vec<(String, usize)> = file_ownership
            .iter()
            .filter(|fo| fo.bus_factor <= 2)
            .map(|fo| (fo.path.clone(), fo.bus_factor))
            .collect();

        // Return None if we have no meaningful git data
        if hidden_coupling.is_empty()
            && top_co_change.is_empty()
            && file_ownership.is_empty()
        {
            return None;
        }

        Some(GitData {
            hidden_coupling,
            top_co_change,
            file_ownership,
            bus_factor_files,
        })
    }

    /// Read source code snippets for the top findings.
    fn build_snippets(
        &self,
        findings: &[crate::models::Finding],
    ) -> Vec<crate::reporters::report_context::FindingSnippet> {
        use crate::reporters::report_context::FindingSnippet;

        findings
            .iter()
            .take(20)
            .filter_map(|f| {
                let file = f.affected_files.first()?;
                let abs_path = if file.is_absolute() {
                    file.clone()
                } else {
                    self.repo_path.join(file)
                };
                let bytes = std::fs::read(&abs_path).ok()?;
                let code = String::from_utf8_lossy(&bytes);

                // Extract relevant lines around the finding
                let start = f.line_start.unwrap_or(1).saturating_sub(1) as usize;
                let end = f.line_end.unwrap_or(f.line_start.unwrap_or(1)) as usize;
                let lines: Vec<&str> = code.lines().collect();

                // Context: 3 lines before, finding lines, 3 lines after
                let ctx_start = start.saturating_sub(3);
                let ctx_end = (end + 3).min(lines.len());
                let snippet: String = lines
                    .get(ctx_start..ctx_end)
                    .unwrap_or(&[])
                    .join("\n");

                // Highlight lines are the finding lines (1-indexed)
                let highlight: Vec<u32> = (f.line_start.unwrap_or(1)..=f.line_end.unwrap_or(f.line_start.unwrap_or(1)))
                    .collect();

                // Detect language from extension
                let language = abs_path
                    .extension()
                    .and_then(|e| e.to_str())
                    .unwrap_or("")
                    .to_string();

                Some(FindingSnippet {
                    finding_id: f.id.clone(),
                    code: snippet,
                    highlight_lines: highlight,
                    language,
                })
            })
            .collect()
    }

    /// Load the previous health report from the cache directory.
    fn load_previous_health(&self) -> Option<crate::models::HealthReport> {
        let path = crate::cache::paths::health_cache_path(&self.repo_path);
        let json = std::fs::read_to_string(&path).ok()?;
        serde_json::from_str(&json).ok()
    }

    /// Group file nodes by parent directory into ModuleNodes.
    fn aggregate_modules(
        &self,
        graph: &dyn GraphQuery,
    ) -> Vec<crate::reporters::report_context::ModuleNode> {
        use crate::reporters::report_context::ModuleNode;
        use std::collections::HashMap;

        let interner = graph.interner();

        // Collect file info grouped by parent directory
        struct FileInfo {
            loc: usize,
            complexity_sum: f64,
            complexity_count: usize,
            community_id: Option<usize>,
        }

        let mut modules: HashMap<String, Vec<FileInfo>> = HashMap::new();

        for &idx in graph.files_idx() {
            let node = match graph.node_idx(idx) {
                Some(n) => n,
                None => continue,
            };
            let file_path = interner.resolve(node.file_path);
            let parent = std::path::Path::new(file_path)
                .parent()
                .and_then(|p| p.to_str())
                .unwrap_or(".")
                .to_string();

            // Aggregate function complexities in this file
            let funcs_in_file = graph.functions_in_file_idx(file_path);
            let (cx_sum, cx_count) = funcs_in_file
                .iter()
                .filter_map(|&fidx| graph.node_idx(fidx))
                .fold((0.0, 0usize), |(sum, cnt), f| {
                    (sum + f.complexity as f64, cnt + 1)
                });

            let community = graph.primitives().community.get(&idx).copied();

            modules.entry(parent).or_default().push(FileInfo {
                loc: node.loc() as usize,
                complexity_sum: cx_sum,
                complexity_count: cx_count,
                community_id: community,
            });
        }

        modules
            .into_iter()
            .map(|(path, files)| {
                let file_count = files.len();
                let loc: usize = files.iter().map(|f| f.loc).sum();
                let total_cx: f64 = files.iter().map(|f| f.complexity_sum).sum();
                let total_cx_count: usize = files.iter().map(|f| f.complexity_count).sum();
                let avg_complexity = if total_cx_count > 0 {
                    total_cx / total_cx_count as f64
                } else {
                    0.0
                };

                // Majority-vote community for this module
                let community_id = {
                    let mut votes: HashMap<usize, usize> = HashMap::new();
                    for f in &files {
                        if let Some(c) = f.community_id {
                            *votes.entry(c).or_default() += 1;
                        }
                    }
                    votes.into_iter().max_by_key(|&(_, count)| count).map(|(id, _)| id)
                };

                ModuleNode {
                    path,
                    loc,
                    file_count,
                    finding_count: 0, // populated by caller if needed
                    finding_density: 0.0,
                    avg_complexity,
                    community_id,
                    health_score: 0.0, // populated by caller if needed
                }
            })
            .collect()
    }

    /// Count cross-module import edges.
    fn aggregate_module_edges(
        &self,
        graph: &dyn GraphQuery,
        _modules: &[crate::reporters::report_context::ModuleNode],
    ) -> Vec<crate::reporters::report_context::ModuleEdge> {
        use crate::reporters::report_context::ModuleEdge;
        use std::collections::HashMap;

        let interner = graph.interner();

        // Build file -> module mapping for edge aggregation

        let file_to_module: HashMap<String, String> = graph
            .files_idx()
            .iter()
            .filter_map(|&idx| {
                let node = graph.node_idx(idx)?;
                let fp = interner.resolve(node.file_path).to_string();
                let parent = std::path::Path::new(&fp)
                    .parent()
                    .and_then(|p| p.to_str())
                    .unwrap_or(".")
                    .to_string();
                Some((fp, parent))
            })
            .collect();

        // Count cross-module edges
        let mut edge_counts: HashMap<(String, String), usize> = HashMap::new();
        for &(from_idx, to_idx) in graph.all_import_edges() {
            let from_node = match graph.node_idx(from_idx) {
                Some(n) => n,
                None => continue,
            };
            let to_node = match graph.node_idx(to_idx) {
                Some(n) => n,
                None => continue,
            };
            let from_fp = interner.resolve(from_node.file_path).to_string();
            let to_fp = interner.resolve(to_node.file_path).to_string();

            let from_mod = file_to_module.get(&from_fp).cloned().unwrap_or_default();
            let to_mod = file_to_module.get(&to_fp).cloned().unwrap_or_default();

            if from_mod != to_mod && !from_mod.is_empty() && !to_mod.is_empty() {
                *edge_counts.entry((from_mod, to_mod)).or_default() += 1;
            }
        }

        edge_counts
            .into_iter()
            .map(|((from, to), weight)| ModuleEdge {
                from,
                to,
                weight,
                is_cycle: false, // could be enriched with cycle detection
            })
            .collect()
    }

    /// Map file-level communities to module-level via majority vote.
    fn map_communities(
        &self,
        graph: &dyn GraphQuery,
        modules: &[crate::reporters::report_context::ModuleNode],
    ) -> (Vec<crate::reporters::report_context::Community>, f64) {
        use crate::reporters::report_context::Community;
        use std::collections::HashMap;

        let modularity = graph.primitives().modularity;

        let mut community_modules: HashMap<usize, Vec<String>> = HashMap::new();
        for m in modules {
            if let Some(cid) = m.community_id {
                community_modules
                    .entry(cid)
                    .or_default()
                    .push(m.path.clone());
            }
        }

        let communities: Vec<Community> = community_modules
            .into_iter()
            .map(|(id, mods)| {
                // Label: longest common directory prefix, or module with most LOC
                let label = if mods.len() == 1 {
                    mods[0].clone()
                } else {
                    common_path_prefix(&mods).unwrap_or_else(|| {
                        // Fallback: module with most LOC
                        mods.iter()
                            .filter_map(|m| modules.iter().find(|n| n.path == *m))
                            .max_by_key(|n| n.loc)
                            .map(|n| n.path.clone())
                            .unwrap_or_else(|| format!("Community {}", id))
                    })
                };
                Community {
                    id,
                    modules: mods,
                    label,
                }
            })
            .collect();

        (communities, modularity)
    }
}

/// Find the longest common directory prefix among a set of paths.
fn common_path_prefix(paths: &[String]) -> Option<String> {
    if paths.is_empty() {
        return None;
    }
    let first = &paths[0];
    // Use char_indices to track byte positions (avoids UTF-8 byte-slice panic)
    let prefix_len = paths.iter().skip(1).fold(first.len(), |acc, p| {
        let common_bytes = first
            .char_indices()
            .zip(p.chars())
            .take_while(|((_, a), b)| a == b)
            .last()
            .map(|((i, c), _)| i + c.len_utf8())
            .unwrap_or(0);
        acc.min(common_bytes)
    });
    let prefix = &first[..prefix_len];
    // Trim to last '/' to get a clean directory path
    prefix
        .rfind('/')
        .map(|i| first[..=i].to_string())
}

impl super::AnalysisEngine {
    /// Extract file ownership info from ExtraProps author field.
    fn compute_file_ownership(
        &self,
        graph: &dyn GraphQuery,
    ) -> Vec<crate::reporters::report_context::FileOwnership> {
        use crate::reporters::report_context::FileOwnership;

        let interner = graph.interner();

        graph
            .files_idx()
            .iter()
            .filter_map(|&idx| {
                let node = graph.node_idx(idx)?;
                let props = graph.extra_props_ref(node.qualified_name)?;
                let author_key = props.author?;
                let author = interner.resolve(author_key);
                if author.is_empty() {
                    return None;
                }

                let file_path = interner.resolve(node.file_path).to_string();

                // Simple model: single author with 100% ownership
                // A more sophisticated version would parse blame data
                Some(FileOwnership {
                    path: file_path,
                    authors: vec![(author.to_string(), 1.0)],
                    bus_factor: 1,
                })
            })
            .collect()
    }
}