Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(health): exclude dynamic_uses hint edges from cycle detection (#3077)
Amane66f56ecfd1f
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/graph_signals.py b/packages/core/src/repowise/core/analysis/health/refactoring/graph_signals.pyindex 2bb0c444f..99397409c 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/graph_signals.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/graph_signals.py@@ -45,9 +45,18 @@ _CYCLE_EDGE_TYPES = FILE_DEPENDENCY_EDGE_TYPESdef _is_cycle_edge(data: Any) -> bool:- """True if *data* is a file-level edge that can legitimately close a cycle."""+ """True if *data* is a file-level edge that can legitimately close a cycle.++ ``dynamic_uses`` is excluded on top of the allowlist: the hint extractors+ that emit it (.NET among others) link every file declaring a type of a+ given short name, so two unrelated classes that happen to share a name+ across projects form a false two-file cycle (#2886). ``framework``,+ ``dynamic_imports`` and ``dynamic_url_route`` are not excluded — they were+ not the edge type the measured false positives came from.+ """return (data.get("edge_type") in _CYCLE_EDGE_TYPES+ and data.get("edge_type") != "dynamic_uses"and not is_cohesion_edge(data)and not data.get("type_only"))diff --git a/packages/core/src/repowise/core/ingestion/graph/_metrics.py b/packages/core/src/repowise/core/ingestion/graph/_metrics.pyindex 761b87bc0..c07ad6471 100644--- a/packages/core/src/repowise/core/ingestion/graph/_metrics.py+++ b/packages/core/src/repowise/core/ingestion/graph/_metrics.py@@ -131,7 +131,8 @@ class MetricsMixin:return subdef cycle_subgraph(self) -> nx.DiGraph:- """Return :meth:`file_subgraph` minus cohesion edges, for cycle detection.+ """Return :meth:`file_subgraph` minus cohesion, type-only and ``dynamic_uses`` edges,+ for cycle detection.A cohesion edge records that two files are one compilation unit — Gopackage siblings, JVM same-package classes, C# partial fragments, a C++@@ -139,6 +140,14 @@ class MetricsMixin::mod:`repowise.core.ingestion.cohesion`. Left in, they turn everycohesive package into a fabricated import cycle.+ A ``dynamic_uses`` edge is a hint match, not a resolved dependency — the+ .NET extractor among others links every file declaring a type of a given+ short name, so two unrelated same-named classes in different projects+ form a false two-file cycle (#2886). Excluded here and in+ ``refactoring.graph_signals._is_cycle_edge``, which must agree: this+ method feeds the wiki SCC pages and the repo-overview cycle list, that+ one feeds ``break_cycle`` plans.+Kept separate from :meth:`file_subgraph` deliberately: PageRank,betweenness and the degree kernels must keep seeing cohesion edges,because dead-code and orphan detection are the whole reason the resolver@@ -151,7 +160,11 @@ class MetricsMixin:"""return self._file_edge_view("_cycle_subgraph_cache",- lambda base, u, v, d: is_cohesion_edge(d) or d.get("type_only") is True,+ lambda base, u, v, d: (+ is_cohesion_edge(d)+ or d.get("type_only") is True+ or d.get("edge_type") == "dynamic_uses"+ ),)def centrality_subgraph(self) -> nx.DiGraph:diff --git a/tests/unit/health/test_refactoring_break_cycle.py b/tests/unit/health/test_refactoring_break_cycle.pyindex 5f252f185..885f0f74a 100644--- a/tests/unit/health/test_refactoring_break_cycle.py+++ b/tests/unit/health/test_refactoring_break_cycle.py@@ -127,6 +127,43 @@ def test_large_cut_is_dropped():assert out == []+def test_dynamic_uses_edges_do_not_form_a_cycle():+ # Only dynamic_uses hint edges, both ways: a hint match (#2886), not a+ # resolved dependency, so no structural cycle and no break_cycle plan.+ g = nx.DiGraph()+ g.add_node("CmdPal/UnsupportedSettingsHelper.cs", node_type="file")+ g.add_node("PTRun/UnsupportedSettingsHelper.cs", node_type="file")+ g.add_edge(+ "CmdPal/UnsupportedSettingsHelper.cs",+ "PTRun/UnsupportedSettingsHelper.cs",+ edge_type="dynamic_uses",+ )+ g.add_edge(+ "PTRun/UnsupportedSettingsHelper.cs",+ "CmdPal/UnsupportedSettingsHelper.cs",+ edge_type="dynamic_uses",+ )+ assert build_file_scc_index(g) == {}+ assert _detect(g, "CmdPal/UnsupportedSettingsHelper.cs") == []+++def test_a_real_import_cycle_still_reports_when_a_third_file_only_shares_a_dynamic_uses_edge():+ # a.py<->b.py is a real import cycle; b.py<->c.py is hint-only. The real+ # cycle must still be found, scoped to {a.py, b.py} and not widened to c.py.+ g = nx.DiGraph()+ for f in ("a.py", "b.py", "c.py"):+ g.add_node(f, node_type="file")+ g.add_edge("a.py", "b.py", edge_type="imports")+ g.add_edge("b.py", "a.py", edge_type="imports")+ g.add_edge("b.py", "c.py", edge_type="dynamic_uses")+ g.add_edge("c.py", "b.py", edge_type="dynamic_uses")+ idx = build_file_scc_index(g)+ assert idx == {"a.py": ("a.py", "b.py"), "b.py": ("a.py", "b.py")}+ out = _detect(g, "a.py")+ assert len(out) == 1+ assert out[0].plan["cycle"] == ["a.py", "b.py"]++def test_co_change_edges_do_not_form_a_cycle():# Only a co_changes edge between the pair → not a structural cycle.g = nx.DiGraph()diff --git a/tests/unit/ingestion/test_cohesion_cycles.py b/tests/unit/ingestion/test_cohesion_cycles.pyindex 68407dd36..2034e0e86 100644--- a/tests/unit/ingestion/test_cohesion_cycles.py+++ b/tests/unit/ingestion/test_cohesion_cycles.py@@ -157,6 +157,43 @@ class TestCycleSubgraph:assert b.cycle_subgraph().number_of_edges() == 0+class TestDynamicUsesIsNotACycle:+ """A hint match is not a resolved dependency and must not close a cycle (#2886)."""++ def _graph_with(self, edge_type: str) -> GraphBuilder:+ import threading++ b = GraphBuilder.__new__(GraphBuilder)+ b._graph = nx.DiGraph()+ b._built = True+ b._subgraph_lock = threading.Lock()+ b._file_subgraph_cache = None+ b._symbol_subgraph_cache = None+ b._cycle_subgraph_cache = None+ b._graph.add_node("a.cs", node_type="file")+ b._graph.add_node("b.cs", node_type="file")+ b._graph.add_edge("a.cs", "b.cs", edge_type=edge_type)+ b._graph.add_edge("b.cs", "a.cs", edge_type=edge_type)+ return b++ def test_dynamic_uses_pair_is_not_a_cycle(self) -> None:+ b = self._graph_with("dynamic_uses")+ assert _cycles(b.cycle_subgraph()) == []++ def test_dynamic_uses_edge_stays_in_file_subgraph(self) -> None:+ # Reachability, blast radius and communities must keep seeing the+ # hint edge — only cycle detection excludes it.+ b = self._graph_with("dynamic_uses")+ fs = b.file_subgraph()+ assert fs.number_of_edges() == 2+ assert fs.in_degree("a.cs") == 1+ assert b.cycle_subgraph().number_of_edges() == 0++ def test_a_real_import_pair_is_still_a_cycle(self) -> None:+ b = self._graph_with("imports")+ assert _cycles(b.cycle_subgraph()) == [{"a.cs", "b.cs"}]++class TestHealthCycleDefinitionAgrees:"""break_cycle must not propose cutting an edge the wiki calls no cycle."""