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(coupling): count a chain through a re-export barrel as a dependency (#2573)
Raghav Chamadiya4debe3914da2
diff --git a/packages/core/src/repowise/core/analysis/graph_view.py b/packages/core/src/repowise/core/analysis/graph_view.pyindex 3ac2b7162..0a422b0f0 100644--- a/packages/core/src/repowise/core/analysis/graph_view.py+++ b/packages/core/src/repowise/core/analysis/graph_view.py@@ -16,8 +16,13 @@ from typing import Any, Protocolfrom ..ingestion.languages import REGISTRYfrom ..ingestion.models import FILE_DEPENDENCY_EDGE_TYPES+from .dead_code.file_reachability import BARREL_FILENAMES-__all__ = ["HasEdge", "ImportEdgeView", "can_carry_dependency"]+__all__ = ["BARREL_DEPENDENCY", "HasEdge", "ImportEdgeView", "can_carry_dependency"]++#: ``dependency_kind`` for a pair joined through a re-export barrel rather than+#: by a direct edge: ``a -> index.ts -> b``.+BARREL_DEPENDENCY = "barrel"def _language_of(path: str) -> str:@@ -65,10 +70,11 @@ class ImportEdgeView:a caller pass ``None`` instead of branching."""- __slots__ = ("_graph",)+ __slots__ = ("_barrels", "_graph")def __init__(self, graph: Any) -> None:self._graph = graph+ self._barrels: dict[str, tuple[str, ...]] = {}def has_edge(self, src: str, dst: str, key: str = "imports") -> bool:g = self._graph@@ -91,8 +97,41 @@ class ImportEdgeView:framework binding are different claims about why two files movetogether. Direction is not reported: the pair is undirected, and``a -> b`` is tried first only to make the answer deterministic.++ A consumer importing a package's barrel (``index.ts``, ``__init__.py``)+ that re-exports the other file depends on it as surely as a direct+ import, so a two-hop path whose middle node is a barrel answers+ :data:`BARREL_DEPENDENCY`. Only a chain through the barrel counts: two+ files a barrel merely re-exports side by side, or two consumers of one+ barrel, share a neighbour and nothing more."""- return self._typed(a, b) or self._typed(b, a)+ return self._typed(a, b) or self._typed(b, a) or self._via_barrel(a, b)++ def _via_barrel(self, a: str, b: str) -> str | None:+ for src, dst in ((a, b), (b, a)):+ for barrel in self._barrels_of(src):+ if self._typed(barrel, dst):+ return BARREL_DEPENDENCY+ return None++ def _barrels_of(self, path: str) -> tuple[str, ...]:+ """The barrels *path* depends on directly, cached per file."""+ cached = self._barrels.get(path)+ if cached is not None:+ return cached+ found: tuple[str, ...] = ()+ g = self._graph+ if g is not None:+ try:+ found = tuple(+ mid+ for mid in g.successors(path)+ if PurePosixPath(mid).name in BARREL_FILENAMES and self._typed(path, mid)+ )+ except Exception:+ found = ()+ self._barrels[path] = found+ return founddef _typed(self, src: str, dst: str) -> str | None:g = self._graphdiff --git a/packages/core/src/repowise/core/co_change.py b/packages/core/src/repowise/core/co_change.pyindex 7258131f0..2dd098c83 100644--- a/packages/core/src/repowise/core/co_change.py+++ b/packages/core/src/repowise/core/co_change.py@@ -87,7 +87,8 @@ class CoChangePartner:zero on an index written before they were recorded.``dependency_kind`` is the ``edge_type`` behind a ``corroborated``- verdict, and is ``None`` for every other verdict.+ verdict, or ``"barrel"`` when the pair is joined through a re-export+ barrel, and is ``None`` for every other verdict.``record`` is the verbatim source record, for callers that put it back onthe wire or read a field this module does not model; it is excluded fromdiff --git a/tests/unit/ingestion/test_co_change_edges.py b/tests/unit/ingestion/test_co_change_edges.pyindex a38dbfa98..978858dba 100644--- a/tests/unit/ingestion/test_co_change_edges.py+++ b/tests/unit/ingestion/test_co_change_edges.py@@ -153,6 +153,45 @@ class TestStructuralLabel:assert builder.label_co_change_structure(partners) == 1assert "dependency_kind" not in partners["a.py"][0]+ def test_a_chain_through_a_barrel_explains_the_pair(self, builder: GraphBuilder) -> None:+ """``a -> pkg/__init__.py -> b``: a imports the package whose barrel+ re-exports b, which is a dependency whichever side is asked."""+ builder._graph.add_node("pkg/__init__.py", language="python")+ builder._graph.add_edge("a.py", "pkg/__init__.py", edge_type="imports")+ builder._graph.add_edge("pkg/__init__.py", "b.py", edge_type="imports")+ partners = {+ "a.py": [{"file_path": "b.py", "frequency": 9}],+ "b.py": [{"file_path": "a.py", "frequency": 9}],+ }+ assert builder.label_co_change_structure(partners) == 0+ for record in (partners["a.py"][0], partners["b.py"][0]):+ assert record["structural"] == "corroborated"+ assert record["dependency_kind"] == "barrel"++ def test_a_shared_neighbour_explains_nothing(self, builder: GraphBuilder) -> None:+ """Two files one barrel re-exports, two consumers of one barrel, and a+ chain through an ordinary module are not dependencies of each other."""+ for path in ("pkg/__init__.py", "lib/__init__.py", "c.py", "d.py", "hub.py"):+ builder._graph.add_node(path)+ builder._graph.add_edge("pkg/__init__.py", "a.py", edge_type="imports")+ builder._graph.add_edge("pkg/__init__.py", "b.py", edge_type="imports")+ builder._graph.add_edge("c.py", "lib/__init__.py", edge_type="imports")+ builder._graph.add_edge("d.py", "lib/__init__.py", edge_type="imports")+ builder._graph.add_edge("c.py", "hub.py", edge_type="imports")+ builder._graph.add_edge("hub.py", "a.py", edge_type="imports")+ partners = {+ "a.py": [{"file_path": "b.py", "frequency": 9}],+ "c.py": [{"file_path": "d.py", "frequency": 9}, {"file_path": "a.py", "frequency": 9}],+ }+ assert builder.label_co_change_structure(partners) == 3++ def test_a_co_change_edge_through_a_barrel_is_not_a_chain(self, builder: GraphBuilder) -> None:+ builder._graph.add_node("pkg/__init__.py", language="python")+ builder._graph.add_edge("a.py", "pkg/__init__.py", edge_type="co_changes")+ builder._graph.add_edge("pkg/__init__.py", "b.py", edge_type="imports")+ partners = {"a.py": [{"file_path": "b.py", "frequency": 9}]}+ assert builder.label_co_change_structure(partners) == 1+class TestLabelPersistence:"""``label_co_change_structure`` writes through the JSON column.