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
refactor(health): move the analyzer's test lookups into RepoTestIndex (#4008)
Raghav Chamadiya67a5c8410441
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.pyindex 2a85e731f..0be8b10ea 100644--- a/packages/core/src/repowise/core/analysis/health/engine.py+++ b/packages/core/src/repowise/core/analysis/health/engine.py@@ -42,12 +42,9 @@ from ...ingestion.package_roots import module_for as _module_forfrom ...ingestion.package_roots import package_roots_from_paths as _package_rootsfrom ...ingestion.package_roots import scan_package_roots as _scan_package_rootsfrom ...installed_components import is_installed_component-from ...test_paths import paired_test_namesfrom ..dead_code.file_reachability import file_dependency_neighborsfrom ..execution_roles import ExecutionRoles-from ..function_test_map import FunctionTests, function_test_mapfrom ..graph_view import HasEdge, ImportEdgeView-from ..test_reachability import files_reached_by_tests, files_with_paired_testsfrom .asserts.lexicon import AssertVocabularyfrom .asserts.oracle_reach import collect_cross_file_oraclesfrom .biomarkers import FileContext, detect_all@@ -84,6 +81,9 @@ from .refactoring import (rank_suggestions,)from .refactoring.graph_signals import build_file_scc_index, build_methods_by_file+from .repo_tests import RepoTestIndex+from .repo_tests import _has_paired_test_file as _has_paired_test_file+from .repo_tests import _path_basenames as _path_basenamesfrom .scope import scores_languagefrom .scoring import (attach_impacts,@@ -702,23 +702,6 @@ def _compute_repo_active_contributors(git_meta_map: dict[str, dict]) -> int | Noreturn None-def _path_basenames(all_paths: set[str]) -> set[str]:- """Final path components of *all_paths*, split on ``/`` only.-- A path matches ``other.endswith("/" + c) or other == c`` for a- slash-free candidate filename ``c`` exactly when its ``/``-basename- equals ``c``, so one precomputed basename set answers every- ``_has_paired_test_file`` lookup. Splitting on ``/`` only (not ``\\``)- preserves that equivalence for any non-POSIX path that slips in.- """- return {p.rsplit("/", 1)[-1] for p in all_paths}---def _has_paired_test_file(rel_path: str, path_basenames: set[str]) -> bool:- """Whether any analyzed file is named like a test for *rel_path*."""- return not paired_test_names(rel_path).isdisjoint(path_basenames)--@dataclass(frozen=True, slots=True)class _RunConfig:"""The analyzer config one pass reads, with its per-file overrides."""@@ -846,8 +829,8 @@ class HealthAnalyzer:# centrality gate inside a partial graph is not read as a code change.self._file_dependents_override = file_dependentsself._package_roots_cache: set[str] | None = None- self._tests_reach_cache: set[str] | None = Noneself._execution_graph_cache: CallGraphIndex | None = None+ self.test_index = RepoTestIndex(self.graph, self.parsed_files, self._execution_graph)def file_dependents(self) -> dict[str, int]:"""Each analysed file's dependents count, as the centrality gates read it."""@@ -875,46 +858,6 @@ class HealthAnalyzer:self._execution_graph_cache = CallGraphIndex(self.graph)return self._execution_graph_cache- def _files_reached_by_tests(self) -> set[str]:- """Non-test files that some test file can execute into, per the call graph.-- The graph-backed half of "is this file tested". Computed once per- analyzer over the whole file set (one multi-source walk, not one per- file), because every ``_evaluate_file`` call asks the same question of- the same graph.-- Inferred, and it over-claims: a call edge says control *can* reach the- file, not that a given run did. Consumers must use it only as a floor -- "something tests this" - never as a coverage quantity. See- ``analysis.test_reachability``.- """- if self._tests_reach_cache is None:- test_files = {pf.file_info.path for pf in self.parsed_files if pf.file_info.is_test}- index = self._execution_graph()- self._tests_reach_cache = files_reached_by_tests(index or CallGraphIndex(), test_files)- return self._tests_reach_cache-- def _paired_tests(self, analyzed_paths: set[str]) -> set[str]:- """Analyzed files a test is paired with; see ``files_with_paired_tests``."""- return files_with_paired_tests(self.graph, analyzed_paths, self._all_test_files())-- def _all_test_files(self) -> set[str]:- """Test files from this pass and the graph, so an incremental run that- re-parses only the changed files still sees every test."""- test_files = {pf.file_info.path for pf in self.parsed_files if pf.file_info.is_test}- if self.graph is not None:- test_files.update(- node- for node, data in self.graph.nodes(data=True)- if data.get("is_test") and data.get("node_type") != "symbol"- )- return test_files-- def _function_tests(self) -> FunctionTests | None:- """Each function's nearest test functions, over the whole graph."""- index = self._execution_graph()- return None if index is None else function_test_map(index, self._all_test_files())-def _package_boundaries(self, analyzed_paths: set[str]) -> set[str]:"""Package roots for this repo, decided once per analyzer.@@ -1190,7 +1133,7 @@ class HealthAnalyzer:execution_roles = self._mark_perf_entry_reachability(findings)suggestions = self._with_performance_plans(run, findings, suggestions)with timed(timings, "analysis.health.function_tests"):- function_tests = self._function_tests()+ function_tests = self.test_index.function_tests()return HealthReport(repo_id="",analyzed_at=datetime.now(UTC),@@ -1220,7 +1163,7 @@ class HealthAnalyzer:# signal (cheap, deterministic, conservative).analyzed_paths = {pf.file_info.path for pf in self.parsed_files}return _RepoInputs(- paired_tests=self._paired_tests(analyzed_paths),+ paired_tests=self.test_index.paired(analyzed_paths),package_roots=self._package_boundaries(analyzed_paths),graph_view=ImportEdgeView(self.graph) if self.graph is not None else None,dup_report=dup_report,@@ -1697,7 +1640,7 @@ class HealthAnalyzer:# call graph records a test reaching this file". They disagree# often, and collapsing them would leave no way to say which signal# answered, or that one of them over-claims.- reached_by_tests=file_path in self._files_reached_by_tests(),+ reached_by_tests=file_path in self.test_index.reached_files(),module=module,all_functions=fns,class_metrics=fcx.classes,diff --git a/packages/core/src/repowise/core/analysis/health/repo_tests.py b/packages/core/src/repowise/core/analysis/health/repo_tests.pynew file mode 100644index 000000000..4da743236--- /dev/null+++ b/packages/core/src/repowise/core/analysis/health/repo_tests.py@@ -0,0 +1,67 @@+"""Which files and functions a repo's tests reach, decided once per health pass."""++from __future__ import annotations++from collections.abc import Callable+from typing import Any++from ...test_paths import paired_test_names+from ..function_test_map import FunctionTests, function_test_map+from ..test_reachability import files_reached_by_tests, files_with_paired_tests+from .perf import CallGraphIndex+++def _path_basenames(all_paths: set[str]) -> set[str]:+ """Final ``/``-separated component of each path, for paired-test lookups."""+ return {p.rsplit("/", 1)[-1] for p in all_paths}+++def _has_paired_test_file(rel_path: str, path_basenames: set[str]) -> bool:+ """Whether any analyzed file is named like a test for *rel_path*."""+ return not paired_test_names(rel_path).isdisjoint(path_basenames)+++class RepoTestIndex:+ """Test lookups for one pass; *execution_graph* returns the shared call index."""++ def __init__(+ self,+ graph: Any,+ parsed_files: list[Any],+ execution_graph: Callable[[], CallGraphIndex | None],+ ) -> None:+ self._graph = graph+ self._parsed_files = parsed_files+ self._execution_graph = execution_graph+ self._reached: set[str] | None = None++ def reached_files(self) -> set[str]:+ """Non-test files some test can execute into, walked once per pass.++ Inferred and over-claiming: a floor ("something tests this"), never a coverage quantity.+ """+ if self._reached is None:+ test_files = {pf.file_info.path for pf in self._parsed_files if pf.file_info.is_test}+ index = self._execution_graph()+ self._reached = files_reached_by_tests(index or CallGraphIndex(), test_files)+ return self._reached++ def paired(self, analyzed_paths: set[str]) -> set[str]:+ """Analyzed files a test is paired with; see ``files_with_paired_tests``."""+ return files_with_paired_tests(self._graph, analyzed_paths, self.all_test_files())++ def all_test_files(self) -> set[str]:+ """Test files from this pass and the graph, so an incremental run sees every test."""+ test_files = {pf.file_info.path for pf in self._parsed_files if pf.file_info.is_test}+ if self._graph is not None:+ test_files.update(+ node+ for node, data in self._graph.nodes(data=True)+ if data.get("is_test") and data.get("node_type") != "symbol"+ )+ return test_files++ def function_tests(self) -> FunctionTests | None:+ """Each function's nearest test functions, over the whole graph."""+ index = self._execution_graph()+ return None if index is None else function_test_map(index, self.all_test_files())diff --git a/packages/core/src/repowise/core/analysis/test_reachability.py b/packages/core/src/repowise/core/analysis/test_reachability.pyindex f91ae8698..38f26c040 100644--- a/packages/core/src/repowise/core/analysis/test_reachability.py+++ b/packages/core/src/repowise/core/analysis/test_reachability.py@@ -433,7 +433,7 @@ async def load_test_files(session: AsyncSession, repo_id: str) -> set[str]:# request reads the test files once whoever asks: Fix first's validation and# Do next both walk from them. Ceiling: Do next's per-file reach is still# walked once per index; persisting it at index time (the engine's-# ``_files_reached_by_tests`` set, stored as a per-file bool) is the upgrade path.+# ``RepoTestIndex.reached_files`` set, stored as a per-file bool) is the upgrade path._CACHE_SIZE = 32_cache: OrderedDict[tuple[Any, ...], Any] = OrderedDict()diff --git a/tests/unit/health/test_function_test_map.py b/tests/unit/health/test_function_test_map.pyindex 7f4a2cd5f..90479965c 100644--- a/tests/unit/health/test_function_test_map.py+++ b/tests/unit/health/test_function_test_map.py@@ -116,10 +116,10 @@ def test_the_analyzer_maps_tests_over_the_whole_graph() -> None:graph.add_node(symbol, node_type="symbol", name=symbol.rsplit("::", 1)[-1], file_path=path)graph.add_edge(path, symbol, edge_type="defines")graph.add_edge(f"{T}::test_parse", "core.py::parse", edge_type="calls")- assert HealthAnalyzer(graph)._function_tests() == {+ assert HealthAnalyzer(graph).test_index.function_tests() == {"core.py::parse": (MappedTest(f"{T}::test_parse", 1),)}- assert HealthAnalyzer(None)._function_tests() is None+ assert HealthAnalyzer(None).test_index.function_tests() is None@pytest.fixturediff --git a/tests/unit/health/test_reachability_wiring.py b/tests/unit/health/test_reachability_wiring.pyindex 7ab9cf58f..f592ce2f9 100644--- a/tests/unit/health/test_reachability_wiring.py+++ b/tests/unit/health/test_reachability_wiring.py@@ -137,7 +137,7 @@ def test_test_and_performance_passes_share_one_execution_index(tmp_path):analyzer = HealthAnalyzer(_graph(calls=True), parsed_files=_tree(tmp_path))first = analyzer._execution_graph()- analyzer._files_reached_by_tests()+ analyzer.test_index.reached_files()analyzer._apply_crossfn_perf([])assert first is not None