Skip to main content

lean_ctx_ext/core/graph_analysis/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Static dependency-graph analyses (God-Nodes, import cycles).
3//!
4//! These run on the *real* directed dependency edges — `import` and `reexport` —
5//! and deliberately exclude the co-location heuristics (`sibling`, `cochange`)
6//! and the ambiguous `module` edges. That way the results reflect genuine code
7//! dependencies (graphify-style) instead of directory layout coincidences.
8
9mod centrality;
10mod cycles;
11mod god_nodes;
12mod surprising;
13
14pub use centrality::{BridgeCentrality, compute_bridge_centrality, compute_bridge_nodes};
15pub use cycles::{ImportCycle, find_import_cycles};
16pub use god_nodes::{GodNode, compute_god_nodes};
17pub use surprising::{SurprisingConnection, find_surprising_connections};
18
19use crate::core::graph_provider::EdgeInfo;
20
21/// Edge kinds that represent a genuine *directed* code dependency
22/// (`from` depends on `to`).
23pub(crate) const DEP_EDGE_KINDS: [&str; 2] = ["import", "reexport"];
24
25/// True when an edge kind is a genuine directed dependency.
26pub fn is_dependency_kind(kind: &str) -> bool {
27    DEP_EDGE_KINDS.contains(&kind)
28}
29
30/// Directed dependency edges as `(from, to)` pairs, with self-loops removed.
31/// Borrows from `edges` to avoid allocating new strings.
32pub fn dependency_edges(edges: &[EdgeInfo]) -> Vec<(&str, &str)> {
33    edges
34        .iter()
35        .filter(|e| is_dependency_kind(&e.kind))
36        .filter(|e| e.from != e.to)
37        .map(|e| (e.from.as_str(), e.to.as_str()))
38        .collect()
39}
40
41/// Confidence that an edge represents a *real* relationship, in `0.0..=1.0`.
42///
43/// Explicit code references (`import`/`reexport`) are certain; structural
44/// `module` grouping is fairly reliable; the co-location/co-change heuristics
45/// are weaker and scale with their observed `weight` (e.g. how often two files
46/// changed together). The dashboard styles edges by this score so heuristic
47/// links read as faint/dashed while real dependencies stay solid (#273).
48pub fn edge_confidence(kind: &str, weight: f64) -> f64 {
49    match kind {
50        "import" => 1.0,
51        "reexport" => 0.95,
52        "module" => 0.6,
53        // More co-changes ⇒ more confidence, with diminishing returns.
54        "cochange" => (0.30 + weight.max(0.0).ln_1p() * 0.18).clamp(0.30, 0.85),
55        "sibling" => 0.25,
56        _ => 0.5,
57    }
58}
59
60#[cfg(test)]
61mod confidence_tests {
62    use super::edge_confidence;
63
64    #[test]
65    fn explicit_refs_rank_above_heuristics() {
66        let import = edge_confidence("import", 0.0);
67        let reexport = edge_confidence("reexport", 0.0);
68        let module = edge_confidence("module", 0.0);
69        let sibling = edge_confidence("sibling", 0.0);
70        assert!(import >= reexport);
71        assert!(reexport > module);
72        assert!(module > sibling);
73        assert!((0.0..=1.0).contains(&sibling));
74    }
75
76    #[test]
77    fn cochange_scales_with_weight_and_is_bounded() {
78        let low = edge_confidence("cochange", 1.0);
79        let high = edge_confidence("cochange", 50.0);
80        assert!(high > low, "more co-changes should raise confidence");
81        assert!((0.30..=0.85).contains(&low));
82        assert!((0.30..=0.85).contains(&high));
83    }
84
85    #[test]
86    fn unknown_kind_is_neutral() {
87        assert_eq!(edge_confidence("mystery", 0.0), 0.5);
88    }
89}