Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

perf(impacted-tests): stop redoing per-file work on every selection (#3927)

Raghav Chamadiya9289e0805069
diff --git a/packages/core/src/repowise/core/analysis/conftest_routes.py b/packages/core/src/repowise/core/analysis/conftest_routes.py
index bd3ddf37c..30aad005a 100644
--- a/packages/core/src/repowise/core/analysis/conftest_routes.py
+++ b/packages/core/src/repowise/core/analysis/conftest_routes.py
@@ -64,6 +64,7 @@ import symtable
from collections import Counter
from collections.abc import Callable, Collection, Iterator, Mapping
from dataclasses import dataclass, field
+from functools import lru_cache
from pathlib import PurePosixPath
from typing import Any
@@ -481,6 +482,9 @@ def _not_local(deps: Mapping[str, set[str] | None]) -> set[str]:
return bad
+# Keyed on the text alone, so an edited file is a new key and a long-lived MCP
+# server stops re-walking the same unchanged entries on every selection.
+@lru_cache(maxsize=1024)
def local_names(source: str) -> frozenset[str]:
"""Names a Python module binds once, at its top level, whose use is its own text alone.
diff --git a/packages/core/src/repowise/core/analysis/test_collection.py b/packages/core/src/repowise/core/analysis/test_collection.py
index 93e1b760a..86fc5ebf5 100644
--- a/packages/core/src/repowise/core/analysis/test_collection.py
+++ b/packages/core/src/repowise/core/analysis/test_collection.py
@@ -176,9 +176,13 @@ def plan_scopes(
namers: dict[str, list[str]] = {}
if asked:
namers = file_namers(asked, _scan_texts(checkout, asked, cancelled), is_manifest)
- code = [p for p in checkout.tracked if is_code_file(p)]
- test_code = [p for p in code if is_test_related_path(p)]
- other = [p for p in code if not is_test_related_path(p)]
+ # One classification per file: it walks the path's parts, and the
+ # checkout is every tracked file.
+ test_code: list[str] = []
+ other: list[str] = []
+ for p in checkout.tracked:
+ if is_code_file(p):
+ (test_code if is_test_related_path(p) else other).append(p)
namers = with_fixture_readers(namers, test_code, other, checkout.read, checkout.holding)
scopes = trigger_scopes(paths, checkout.tracked, namers, config)
routes = sorted({r for scope in scopes.values() for r in scope.routes} - set(paths))
diff --git a/tests/unit/analysis/test_conftest_routes.py b/tests/unit/analysis/test_conftest_routes.py
index b647f21da..97d80d5b3 100644
--- a/tests/unit/analysis/test_conftest_routes.py
+++ b/tests/unit/analysis/test_conftest_routes.py
@@ -351,6 +351,16 @@ def test_local_constants_are_names_bound_once_at_top_level_to_immutable_literals
assert local_names("def broken(:\n") == frozenset()
+def test_local_names_reads_each_text_once() -> None:
+ """A long-lived server asks about the same unchanged entries on every selection."""
+ source = "SIZE = 3\n\ndef f():\n return SIZE\n"
+ local_names.cache_clear()
+ first = local_names(source)
+ assert local_names(source) is first
+ assert local_names(source + "\nEXTRA = 1\n") == {"SIZE", "f", "EXTRA"}
+ assert local_names.cache_info().hits == 1
+
+
def test_local_functions_use_nothing_the_module_imports_from_elsewhere() -> None:
source = dedent(
"""