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
perf(health): skip the repo-wide manifest walk and reuse parses in change health (#4311)
Raghav Chamadiya104d729cc2e2
diff --git a/docs/reference/CHANGE_REVIEW_API.md b/docs/reference/CHANGE_REVIEW_API.mdindex cd1ec49af..4d38872f2 100644--- a/docs/reference/CHANGE_REVIEW_API.md+++ b/docs/reference/CHANGE_REVIEW_API.md@@ -84,6 +84,24 @@ identical downstream, which is the one thing this type exists to prevent.---+## Reusing work across comparisons++The health comparison keeps each file's parse and complexity walk by content,+so a file seen in an earlier comparison (the same base on a re-run, or the+previous commit's head in a scan) is not parsed or walked again. With a+checkout that has a `.repowise` directory the cache lives in+`.repowise/change_health/`. Elsewhere there is none unless you name a writable+directory, and the results are the same either way:++```python+from repowise.core.analysis.change_health import ChangeHealthDeltaService++delta_service = ChangeHealthDeltaService(source, cache_dir="/tmp/repowise-cache")+bundle = ChangeReviewService(source, delta_service=delta_service).review(ChangeReviewRequest())+```++---+## Evidence status: the part to read firstA lane that was never consulted and a lane that found nothing are differentdiff --git a/packages/core/src/repowise/core/analysis/change_health/analyzer.py b/packages/core/src/repowise/core/analysis/change_health/analyzer.pyindex f3e6fd5e1..cc781b89d 100644--- a/packages/core/src/repowise/core/analysis/change_health/analyzer.py+++ b/packages/core/src/repowise/core/analysis/change_health/analyzer.py@@ -7,11 +7,13 @@ findings is a difference in the code rather than in how it was analysed.from __future__ import annotations+import contextlibfrom dataclasses import dataclass, fieldfrom datetime import UTC, datetime-from pathlib import PurePosixPath+from pathlib import Path, PurePosixPathfrom typing import TYPE_CHECKING, Any+from ...ingestion import compute_content_hashfrom ...ingestion.graph.builder import GraphBuilderfrom ...ingestion.models import (EXTENSION_TO_LANGUAGE,@@ -20,11 +22,19 @@ from ...ingestion.models import (ParsedFile,Symbol,)+from ...ingestion.package_roots import package_roots_above+from ...ingestion.parse_cache import ParseCachefrom ...ingestion.parser import parse_filefrom ...test_paths import is_test_related_path-from ..health import HealthAnalyzer, HealthFileMetricData, HealthFindingData+from ..health import (+ HEALTH_ANALYZER_VERSION,+ HealthAnalyzer,+ HealthFileMetricData,+ HealthFindingData,+)from ..health.complexity.languages import has_health_dialectfrom ..health.source_reader import MappingSourceReader+from ..health.walk_cache import HealthWalkCacheif TYPE_CHECKING:from .index_inputs import IndexInputs@@ -64,6 +74,47 @@ def is_binary(source: bytes) -> bool:return b"\0" in source[:8192]+@dataclass(frozen=True, slots=True)+class AnalysisCaches:+ """Parse and walk results kept by content, shared by both sides of a comparison.++ A file's parse and complexity walk depend only on its bytes, path and the+ analyzer's versions (the keys carry all of them), so a base or head file+ seen in an earlier comparison is not parsed or walked again. Everything+ after the walk reads the whole changed set and is always recomputed.++ The files are the index's cache formats in their own directory, so a+ comparison never evicts the index's entries. Each save keeps only the+ entries the last comparison used, which bounds the size. Loading and+ saving never raise: a read-only or missing directory is a cold cache.+ """++ directory: Path+ parse: ParseCache+ walk: HealthWalkCache++ @classmethod+ def open(cls, cache_dir: str | Path) -> AnalysisCaches:+ directory = Path(cache_dir)+ caches = cls(+ directory, ParseCache(directory), HealthWalkCache(directory, HEALTH_ANALYZER_VERSION)+ )+ caches.parse.load()+ caches.walk.load()+ return caches++ def save(self) -> None:+ # Ignored by its own .gitignore, so a working-tree comparison never+ # counts the cache as an untracked change of the next one.+ with contextlib.suppress(OSError):+ self.directory.mkdir(parents=True, exist_ok=True)+ ignore = self.directory / ".gitignore"+ if not ignore.exists():+ ignore.write_text("*\n", encoding="utf-8")+ self.parse.save()+ self.walk.save()++@dataclass(slots=True)class RevisionAnalysis:"""One side's findings plus what it could not look at."""@@ -95,6 +146,7 @@ class RevisionHealthAnalyzer:subject_paths: set[str],dependents: dict[str, int] | None = None,index: IndexInputs | None = None,+ caches: AnalysisCaches | None = None,) -> RevisionAnalysis:"""Analyze *sources*, reporting findings for the whole supplied set.@@ -110,9 +162,12 @@ class RevisionHealthAnalyzer:*index* adds what the index knows (history, coverage, test pairing,blame findings); without it a score is the code-shape reading.++ *caches* serves parses and walks seen before; the result is the same+ with or without it."""result = RevisionAnalysis()- parsed, usable = self._parse(sources, result)+ parsed, usable = self._parse(sources, result, caches.parse if caches else None)if not parsed:return resultanalyzer = HealthAnalyzer(@@ -121,6 +176,8 @@ class RevisionHealthAnalyzer:repo_root=self.repo_root,source_reader=MappingSourceReader(usable),file_dependents=dependents,+ walk_cache=caches.walk if caches else None,+ package_roots=_package_roots(self.repo_root, usable),**(_index_kwargs(index) if index is not None else {}),)report = analyzer.analyze(config=self.config, changed_files=set(usable))@@ -134,7 +191,7 @@ class RevisionHealthAnalyzer:# -- internals ----------------------------------------------------------def _parse(- self, sources: dict[str, bytes], result: RevisionAnalysis+ self, sources: dict[str, bytes], result: RevisionAnalysis, cache: ParseCache | None) -> tuple[list[ParsedFile], dict[str, bytes]]:parsed: list[ParsedFile] = []usable: dict[str, bytes] = {}@@ -146,7 +203,7 @@ class RevisionHealthAnalyzer:language = language_for(path)assert language is not None # _reject already screened thistry:- parsed.append(parse_file(_file_info(path, language, raw), raw))+ parsed.append(_parse_one(_file_info(path, language, raw), raw, cache))except Exception:result.skipped[path] = "parse_failed"continue@@ -174,6 +231,32 @@ class RevisionHealthAnalyzer:return builder.build()+def _parse_one(info: FileInfo, raw: bytes, cache: ParseCache | None) -> ParsedFile:+ if cache is None:+ return parse_file(info, raw)+ content_hash = compute_content_hash(raw)+ parsed = cache.get(info, content_hash)+ if parsed is None:+ parsed = parse_file(info, raw)+ # Before the graph build, which mutates the parse in place.+ cache.put(parsed, content_hash)+ return parsed+++def _package_roots(repo_root: str | None, sources: dict[str, bytes]) -> set[str] | None:+ """The package roots above the analysed files, or None to let the engine decide.++ The engine's own scan walks the whole checkout, which on a large repo+ costs more than analysing a small change.+ """+ if repo_root is None:+ return None+ try:+ return package_roots_above(repo_root, sources)+ except OSError:+ return None++def _index_kwargs(index: IndexInputs) -> dict[str, Any]:return {"git_meta_map": index.git_meta,diff --git a/packages/core/src/repowise/core/analysis/change_health/service.py b/packages/core/src/repowise/core/analysis/change_health/service.pyindex 16e4e5765..fbe6d0b33 100644--- a/packages/core/src/repowise/core/analysis/change_health/service.py+++ b/packages/core/src/repowise/core/analysis/change_health/service.py@@ -13,7 +13,8 @@ import timefrom collections import OrderedDictfrom collections.abc import Mappingfrom dataclasses import dataclass-from pathlib import PurePosixPath+from pathlib import Path, PurePosixPath+from typing import Literalfrom ...support_paths import TEMPLATE_EXTENSIONS, is_doc_or_config_pathfrom ...test_paths import is_test_related_path@@ -22,7 +23,13 @@ from ..health import HEALTH_ANALYZER_VERSION, HealthFindingDatafrom ..health.biomarker_labels import biomarker_labelfrom ..health.perf.causal import PERFORMANCE_MODEL_VERSIONfrom ..health.scoring import ADVISORY_DIMENSION, is_advisory-from .analyzer import RevisionAnalysis, RevisionHealthAnalyzer, language_for, language_of+from .analyzer import (+ AnalysisCaches,+ RevisionAnalysis,+ RevisionHealthAnalyzer,+ language_for,+ language_of,+)from .attribution import FindingAttributor, changed_symbols_forfrom .file_scores import file_score_deltasfrom .identity import change_finding_id, finding_key, severity_rank@@ -60,6 +67,10 @@ _NOT_CODE_LANGUAGES = frozenset({"json", "yaml", "toml", "markdown"})_QUERY_SUFFIX = ".scm"_QUERY_DIR = "queries"+#: Where a checkout with a ``.repowise`` directory keeps parses and walks+#: between comparisons, apart from the index's own caches.+_CACHE_SUBDIR = "change_health"+#: Comparisons kept in the process cache._CACHE_CAPACITY = 32@@ -98,6 +109,12 @@ class ChangeHealthDeltaService:Without a checkout, pass a ``MappingRevisionSource`` (diff hunks plus rawbytes at both SHAs). Both sides are re-scored from those bytes, so customhealth rules need ``RevisionHealthAnalyzer(config=...)`` to match the index.++ *cache_dir* keeps per-file parses and walks between comparisons (see+ :class:`AnalysisCaches`). It defaults to ``.repowise/change_health`` when+ *repo_path* holds a ``.repowise`` directory (and no *analyzer* is+ supplied) and to no cache otherwise; pass ``False`` to turn it off.+ Results are the same either way."""def __init__(@@ -107,6 +124,7 @@ class ChangeHealthDeltaService:repo_path: str | None = None,analyzer: RevisionHealthAnalyzer | None = None,rules_fingerprint: str = "",+ cache_dir: str | Path | Literal[False] | None = None,) -> None:if source is None:if repo_path is None:@@ -117,6 +135,11 @@ class ChangeHealthDeltaService:self._custom_analyzer = analyzer is not Noneself.analyzer = analyzer or RevisionHealthAnalyzer(repo_root=repo_path)self.rules_fingerprint = rules_fingerprint+ # A supplied analyzer may predate the ``caches`` argument: it caches+ # only when the caller names a directory.+ if analyzer is not None and cache_dir is None:+ cache_dir = False+ self.cache_dir = _resolve_cache_dir(cache_dir, repo_path)self._cache: OrderedDict[str, ChangeHealthDelta] = OrderedDict()self._lock = threading.Lock()self._inflight: dict[str, threading.Event] = {}@@ -322,12 +345,20 @@ class ChangeHealthDeltaService:base_sources = self.source.read(pair.base_sha, base_paths)rename = pair.rename_map()analyzer = self._analyzer_for([*head_paths, *base_paths])- head_run = analyzer.analyze(head_sources, subject_paths=set(head_paths), dependents=index)+ caches = AnalysisCaches.open(self.cache_dir) if self.cache_dir is not None else None+ # Only when there is one, so an analyzer written before caches still fits.+ cached = {"caches": caches} if caches is not None else {}+ head_run = analyzer.analyze(+ head_sources, subject_paths=set(head_paths), dependents=index, **cached+ )base_run = analyzer.analyze(base_sources,subject_paths=set(base_paths),dependents=index if index is not None else _on_base(head_run, rename),+ **cached,)+ if caches is not None:+ caches.save()return _Sides(head_paths, head_sources, base_sources, head_run, base_run, rename)def _analyzer_for(self, paths: list[str]) -> RevisionHealthAnalyzer:@@ -749,6 +780,19 @@ def _unavailable()+def _resolve_cache_dir(+ cache_dir: str | Path | Literal[False] | None, repo_path: str | None+) -> Path | None:+ if cache_dir is False:+ return None+ if cache_dir is not None:+ return Path(cache_dir)+ if repo_path is None:+ return None+ index_dir = Path(repo_path) / ".repowise"+ return index_dir / _CACHE_SUBDIR if index_dir.is_dir() else None++def _analyzer_config(repo_path: str, paths: list[str]) -> dict[str, object] | None:"""The repo's `.repowise/health-rules.json` as analyzer config, or `None`."""from ..health.config import HealthConfigdiff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.pyindex 940ca780d..ab30ea236 100644--- a/packages/core/src/repowise/core/analysis/health/engine.py+++ b/packages/core/src/repowise/core/analysis/health/engine.py@@ -549,6 +549,8 @@ class HealthAnalyzer:stored_blame_findings: dict[str, list[Any]] | None = None,file_dependents: dict[str, int] | None = None,tested_files: set[str] | frozenset[str] | None = None,+ walk_cache: HealthWalkCache | None = None,+ package_roots: set[str] | None = None,) -> None:self.graph = graphself.git_meta_map = git_meta_map or {}@@ -574,7 +576,10 @@ class HealthAnalyzer:# The walk result for a file is a function of its bytes, its language# and the walker's version, so it is kept beside the index like the# parse trees and the clone windows are. Absent without a cache dir.- self._walk_cache: HealthWalkCache | None = (+ # A *walk_cache* passed in is the caller's: it loads and saves it, so+ # several passes (both sides of a comparison) share one file.+ self._owns_walk_cache = walk_cache is None+ self._walk_cache: HealthWalkCache | None = walk_cache or (HealthWalkCache(duplication_cache_dir, HEALTH_ANALYZER_VERSION)if duplication_cache_dir is not Noneelse None@@ -602,7 +607,8 @@ class HealthAnalyzer:self._file_dependents_override = file_dependents# Files the index records as tested (paired or reached), for a pass holding no tests.self._tested_files = frozenset(tested_files or ())- self._package_roots_cache: set[str] | None = None+ # Supplied by a caller that already knows the roots its files sit under.+ self._package_roots_cache: set[str] | None = package_rootsself._execution_graph_cache: CallGraphIndex | None = Noneself.test_index = RepoTestIndex(self.graph, self.parsed_files, self._execution_graph)self.perf_stage = CrossFunctionPerfStage(@@ -1209,12 +1215,12 @@ class HealthAnalyzer:return kept, _pct_of_kept(file_path, clones, kept, dup_pct)def _load_walk_cache(self) -> None:- if self._walk_cache is not None:+ if self._walk_cache is not None and self._owns_walk_cache:self._walk_cache.load()def _save_walk_cache(self) -> None:"""Persist the walk entries this pass used or produced, if any."""- if self._walk_cache is not None:+ if self._walk_cache is not None and self._owns_walk_cache:self._walk_cache.save()log.debug("health_walk_cache",diff --git a/packages/core/src/repowise/core/ingestion/package_roots.py b/packages/core/src/repowise/core/ingestion/package_roots.pyindex 81f6fcc2c..a3cd433c2 100644--- a/packages/core/src/repowise/core/ingestion/package_roots.py+++ b/packages/core/src/repowise/core/ingestion/package_roots.py@@ -27,12 +27,12 @@ import fnmatchimport osimport reimport subprocess-from collections.abc import Callable, Collection, Mapping+from collections.abc import Callable, Collection, Iterable, Mappingfrom collections.abc import Set as AbstractSetfrom functools import cachefrom pathlib import Path-from ..fs_walk import walk_repo+from ..fs_walk import PRUNED_DIRS, walk_repofrom .languages.registry import REGISTRY as _LANG_REGISTRY@@ -128,6 +128,51 @@ def scan_package_manifests(return dict(cached)+def package_roots_above(repo_root: str | os.PathLike[str], paths: Iterable[str]) -> set[str]:+ """The roots :func:`scan_package_roots` finds among the directories above *paths*.++ Those are the only roots :func:`module_for` can return for *paths*, so for+ them the answer is the full scan's. Only those directories are listed,+ so the cost follows the paths rather than the size of the tree.++ Each directory gets the walk's pruning: a pruned name, the traverser's+ boundary, a nested checkout (its own ``.git``), and any of these on a+ parent. Ceiling: link pruning and the walk's depth cap are not repeated,+ because git tracks no file beneath a linked directory; a path under a+ link or 64 levels deep could see a root the walk skips.+ """+ root = Path(repo_root)+ prune = _default_prune(root)+ names = package_manifest_names()+ dirs: set[str] = set()+ for path in paths:+ parts = path.replace("\\", "/").split("/")+ dirs.update("/".join(parts[:i]) for i in range(1, len(parts)))+ found: dict[str, frozenset[str] | None] = {}+ # Shallowest first, so a pruned parent is decided before its children.+ for rel in sorted(dirs, key=lambda d: d.count("/")):+ parent = rel.rpartition("/")[0]+ pruned = bool(parent) and found.get(parent) is None+ found[rel] = None if pruned else _listed_manifests(root, rel, prune, names)+ return {rel for rel, manifests in found.items() if manifests}+++def _listed_manifests(+ root: Path, rel: str, prune: Callable[[Path], bool], names: frozenset[str]+) -> frozenset[str] | None:+ """The manifests in *rel*, or None when the walk would not enter it."""+ if rel.rpartition("/")[2] in PRUNED_DIRS or prune(Path(rel)):+ return None+ try:+ with os.scandir(root / rel) as listing:+ entries = [(e.name, e.is_dir()) for e in listing]+ except OSError:+ return None+ if any(name == ".git" for name, _ in entries):+ return None+ return _manifests_in([name for name, is_dir in entries if not is_dir], names)++#: Stock-settings scans by (checkout, HEAD commit), most recent last: health,#: the plan finalizers and Fix first share one walk per update, and another#: checkout or commit scans again. Ceiling: a manifest added or removed withoutdiff --git a/tests/unit/change_health/test_analysis_cache.py b/tests/unit/change_health/test_analysis_cache.pynew file mode 100644index 000000000..3b3bbff6f--- /dev/null+++ b/tests/unit/change_health/test_analysis_cache.py@@ -0,0 +1,160 @@+"""Cached parses and walks must never change a comparison.++The cache only skips work whose output is a function of a file's bytes and the+analyzer's versions, so a comparison with it, without it, cold or warm, has to+come out the same; and a version or rules change has to miss rather than serve+an old answer.+"""++from __future__ import annotations++import json+from pathlib import Path++import pytest++from repowise.core.analysis.change_health import ChangeHealthDeltaService, DeltaRequest+from repowise.core.analysis.change_health import analyzer as analyzer_mod+from repowise.core.analysis.health import engine as engine_mod+from repowise.core.ingestion import parse_cache as parse_cache_mod++from .conftest import Repo, python_complex, python_io_in_loop, typescript_complex+++def repo(make_repo) -> Repo:+ repo: Repo = make_repo("cached")+ repo.commit(+ "base",+ {+ "pkg/package.json": '{"name": "pkg"}\n',+ "pkg/app.ts": typescript_complex("run", 2),+ "svc/handler.py": python_io_in_loop(in_loop=False),+ "svc/old.py": python_complex("legacy", 9),+ "svc/gone.py": python_complex("doomed", 7),+ },+ )+ repo.move("svc/old.py", "svc/renamed.py")+ repo.remove("svc/gone.py")+ repo.commit(+ "head",+ {+ "pkg/app.ts": typescript_complex("run", 12),+ "svc/handler.py": python_io_in_loop(in_loop=True),+ "svc/new.py": python_complex("fresh", 14),+ },+ )+ return repo+++def _comparable(repo: Repo, cache_dir) -> tuple:+ service = ChangeHealthDeltaService(repo_path=str(repo.path), cache_dir=cache_dir)+ delta = service.compare(DeltaRequest(str(repo.path), "HEAD~1..HEAD"))+ return (+ delta.status,+ delta.explanation,+ [+ (f.change_finding_id, f.change_kind, f.biomarker_type, f.path, f.symbol, f.line_start)+ for f in delta.findings+ ],+ delta.unchanged_total,+ delta.improved_total,+ delta.moved_total,+ delta.resolved_total,+ sorted(delta.skipped.items()),+ repr(delta.files),+ delta.moved_to,+ )+++class _Counter:+ def __init__(self, monkeypatch) -> None:+ self.parses = 0+ self.walks = 0+ real_parse, real_walk = analyzer_mod.parse_file, engine_mod.walk_file++ def parse(*args, **kwargs):+ self.parses += 1+ return real_parse(*args, **kwargs)++ def walk(*args, **kwargs):+ self.walks += 1+ return real_walk(*args, **kwargs)++ monkeypatch.setattr(analyzer_mod, "parse_file", parse)+ monkeypatch.setattr(engine_mod, "walk_file", walk)+++def test_results_are_identical_with_no_cache_a_cold_one_and_a_warm_one(repo, tmp_path):+ cache = tmp_path / "cache"+ uncached = _comparable(repo, False)+ assert uncached[2], "the fixture must surface findings to compare"+ assert _comparable(repo, cache) == uncached+ assert _comparable(repo, cache) == uncached+++def test_a_warm_cache_parses_and_walks_nothing(repo, tmp_path, monkeypatch):+ cache = tmp_path / "cache"+ _comparable(repo, cache)+ counter = _Counter(monkeypatch)+ _comparable(repo, cache)+ assert (counter.parses, counter.walks) == (0, 0)+++def test_a_new_analyzer_version_walks_again(repo, tmp_path, monkeypatch):+ cache = tmp_path / "cache"+ expected = _comparable(repo, cache)+ monkeypatch.setattr(analyzer_mod, "HEALTH_ANALYZER_VERSION", -1)+ counter = _Counter(monkeypatch)+ assert _comparable(repo, cache) == expected+ assert counter.walks > 0+++def test_a_new_parser_parses_again(repo, tmp_path, monkeypatch):+ cache = tmp_path / "cache"+ _comparable(repo, cache)+ monkeypatch.setattr(parse_cache_mod, "parser_fingerprint", lambda: "another parser")+ counter = _Counter(monkeypatch)+ _comparable(repo, cache)+ assert counter.parses > 0+++def test_changed_rules_are_applied_to_a_warm_cache(repo, tmp_path):+ cache = tmp_path / "cache"+ _comparable(repo, cache)+ (repo.path / ".repowise").mkdir(exist_ok=True)+ (repo.path / ".repowise" / "health-rules.json").write_text(+ json.dumps({"disabled_biomarkers": ["complex_method"]}), encoding="utf-8"+ )+ (repo.path / ".repowise" / "config.yaml").write_text(+ "assertions:\n extra_names: [verify_that]\n", encoding="utf-8"+ )+ assert _comparable(repo, cache) == _comparable(repo, False)+ assert all(f[2] != "complex_method" for f in _comparable(repo, cache)[2])+++def test_an_unwritable_cache_location_is_a_cold_cache(repo, tmp_path):+ blocked = tmp_path / "a-file"+ blocked.write_text("not a directory", encoding="utf-8")+ assert _comparable(repo, blocked) == _comparable(repo, False)+++def test_the_cache_defaults_to_the_index_directory_only_when_there_is_one(repo):+ assert ChangeHealthDeltaService(repo_path=str(repo.path)).cache_dir is None+ (repo.path / ".repowise").mkdir()+ service = ChangeHealthDeltaService(repo_path=str(repo.path))+ assert service.cache_dir == repo.path / ".repowise" / "change_health"+ assert ChangeHealthDeltaService(repo_path=str(repo.path), cache_dir=False).cache_dir is None+++def test_the_cache_never_shows_up_as_a_working_tree_change(repo):+ (repo.path / ".repowise").mkdir()+ repo.write("svc/new.py", python_complex("fresher", 16))+ service = ChangeHealthDeltaService(repo_path=str(repo.path))+ first = service.compare(DeltaRequest(str(repo.path), None))+ assert (repo.path / ".repowise" / "change_health").is_dir()+ second = ChangeHealthDeltaService(repo_path=str(repo.path)).compare(+ DeltaRequest(str(repo.path), None)+ )+ assert second.scope.changed == first.scope.changed+ assert not any(Path(p).parts[:2] == (".repowise", "change_health") for p in second.skipped)diff --git a/tests/unit/ingestion/test_package_roots.py b/tests/unit/ingestion/test_package_roots.pyindex 069425652..ce3cb651f 100644--- a/tests/unit/ingestion/test_package_roots.py+++ b/tests/unit/ingestion/test_package_roots.py@@ -20,6 +20,7 @@ from repowise.core.ingestion.languages.registry import REGISTRYfrom repowise.core.ingestion.package_roots import (module_for,package_manifest_names,+ package_roots_above,package_roots_from_paths,scan_package_manifests,scan_package_roots,@@ -222,3 +223,36 @@ def test_one_commit_is_walked_once_and_a_new_commit_again(tmp_path: Path, monkeysubprocess.run([*git, "commit", "-qm", "b"], cwd=tmp_path, check=True)assert package_roots.scan_package_roots(tmp_path) == {"web", "api"}assert len(walks) == 2+++def test_roots_above_some_paths_agree_with_the_full_scan(tmp_path):+ """The narrow lookup must give every path the module the full scan gives it,+ across each way the walk prunes: a pruned name, an ignored directory, a+ nested checkout, and a vendored tree."""+ _write(tmp_path, "services/api/go.mod", "module api\n")+ _write(tmp_path, "services/api/internal/tools/go.mod", "module tools\n")+ _write(tmp_path, "web/Shop/Shop.csproj", "<Project/>\n")+ _write(tmp_path, "node_modules/dep/package.json", '{"name":"dep"}\n')+ _write(tmp_path, "vendor/lib/go.mod", "module lib\n")+ _write(tmp_path, "packages/tool/package.json", '{"name":"tool"}\n')+ _write(tmp_path, "packages/tool/.gitignore", ".fixtures/\n")+ _write(tmp_path, "packages/tool/.fixtures/v/package.json", '{"name":"v"}\n')+ _write(tmp_path, "nested/.git", "gitdir: elsewhere\n")+ _write(tmp_path, "nested/pkg/package.json", '{"name":"n"}\n')+ paths = [+ "services/api/main.go",+ "services/api/internal/tools/t.go",+ "web/Shop/Program.cs",+ "node_modules/dep/index.js",+ "vendor/lib/lib.go",+ "packages/tool/src/a.ts",+ "packages/tool/.fixtures/v/x.js",+ "nested/pkg/n.js",+ "scripts/run.py",+ "gone/deleted.py",+ "root.py",+ ]+ full = scan_package_roots(tmp_path)+ above = package_roots_above(tmp_path, paths)+ assert above == {"services/api", "services/api/internal/tools", "web/Shop", "packages/tool"}+ assert [module_for(p, above) for p in paths] == [module_for(p, full) for p in paths]