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): give Go, Maven and Ruby monorepos real module attribution, without a re-index (#1366)
Raghav Chamadiyac40a47e01fb7
diff --git a/docs/architecture/language-support.md b/docs/architecture/language-support.mdindex 62499ea9e..ee60c7534 100644--- a/docs/architecture/language-support.md+++ b/docs/architecture/language-support.md@@ -162,7 +162,8 @@ SPEC = LanguageSpec(scm_file="mylang.scm", # query file nameheritage_node_types=frozenset({"class_declaration"}),entry_point_patterns=("main.ml",),- manifest_files=("mylang.toml",),+ manifest_files=("mylang.toml", "mylang.build.json"),+ build_config_manifests=("mylang.build.json",), # usually empty — see belowshebang_tokens=("mylang",),builtin_calls=frozenset({"print", "len"}), # filter from call graphbuiltin_parents=frozenset({"Object"}), # filter from heritage@@ -170,6 +171,36 @@ SPEC = LanguageSpec()```+#### `manifest_files` also decides package boundaries++Every name in `manifest_files` that is *not* in `build_config_manifests` marks+its directory as a **package root**. That drives two things beyond ingestion:+monorepo detection (`RepoStructure.packages`), and code health's `module`+attribution — the label the dashboard, the module rollups and `module:` target+expansion all group on. Declaring your manifests is therefore all it takes to+give a monorepo in your language proper per-package health; there is no second+list to edit. `REGISTRY.package_manifest_filenames()` is the single source of+truth, and both consumers read it.++`build_config_manifests` defaults to empty, which is nearly always right. Add+to it only when a name in `manifest_files` configures a build rather than+declaring a distributable unit, because such files appear in directories that+are not packages and each one becomes a false root that fragments the rollup.+The shipped cases are the .NET `Directory.Build.props` / `global.json` family+(measured in 135 non-package directories), `vite.config.js` / `nuxt.config.ts`,+`svelte.config.js`, Cabal's `Setup.hs`, and `lean-toolchain`.++`manifest_files` holds exact filenames only, so a language whose package file is+a *pattern* cannot be expressed. .NET is the live example: `*.csproj` is the+real package declaration, everything C# declares is build configuration, and a+.NET monorepo therefore still falls back to the top-level directory.++One caveat worth knowing: package roots are read from a **directory scan**, not+from the indexed file list, because the traverser only emits files whose+language it can detect and drops 18 manifest names on that rule (`go.mod`,+`pom.xml`, `build.gradle`, `Gemfile`, `build.sbt` among them). So a manifest+counts as a package root whether or not your language's files parse.+Then register it in `languages/specs/__init__.py` by importing the module andslotting it into the `ALL_SPECS` tuple. **Order matters**: `LanguageRegistry`builds its extension map first-spec-wins, so place more specific languagesdiff --git a/packages/cli/src/repowise/cli/commands/health_cmd/command.py b/packages/cli/src/repowise/cli/commands/health_cmd/command.pyindex b40b199e9..ae378f678 100644--- a/packages/cli/src/repowise/cli/commands/health_cmd/command.py+++ b/packages/cli/src/repowise/cli/commands/health_cmd/command.py@@ -17,6 +17,7 @@ from repowise.cli._setup import configure_cli_loggingfrom repowise.cli.helpers import (console,err_console,+ load_config,load_state,resolve_command_target,run_async,@@ -175,11 +176,18 @@ def health_command(state = load_state(repo_path)include_submodules = bool(state.get("include_submodules", False))include_nested_repos = bool(state.get("include_nested_repos", False))+ # `repowise health` persists metrics into the same rows the indexer writes,+ # so it has to analyze the same file set. Without the config's exclude+ # patterns it scored — and overwrote rows for — files the index had+ # deliberately dropped, and on a repo excluding a manifest directory it+ # could write a different `module` than the index did.+ exclude_patterns: list[str] = list(load_config(repo_path).get("exclude_patterns") or [])traverser = FileTraverser(repo_path,include_submodules=include_submodules,include_nested_repos=include_nested_repos,+ extra_exclude_patterns=exclude_patterns or None,)file_infos = list(traverser.traverse())parser = ASTParser()@@ -230,6 +238,7 @@ def health_command(parsed_files=parsed_files,coverage_map=coverage_map,duplication_cache_dir=Path(repo_path) / ".repowise",+ repo_root=repo_path,)# Load any .repowise/health-rules.json the user keeps in the repo.from repowise.core.analysis.health.config import HealthConfigdiff --git a/packages/cli/src/repowise/cli/commands/update_cmd/command.py b/packages/cli/src/repowise/cli/commands/update_cmd/command.pyindex 72fac8def..13c24d6d7 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/command.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/command.py@@ -50,6 +50,7 @@ from .incremental import (from .mode import _resolve_index_only_modefrom .persistence import (_persist_index_only_update,+ _repair_module_attribution,_run_full_health_rescore,heal_commit_offsets,stamp_head_commit,@@ -782,6 +783,12 @@ def run_update(f"[dim]Uncommitted changes: [bold]{len(working_tree_diffs)}[/bold] file(s)[/dim]")+ # Before the early return, so a repo with no new commits still picks this+ # up. A module label is a pure function of the repo layout, so correcting+ # it must not wait on a re-score (7-day decay) or cost an indexing run.+ if not dry_run:+ _repair_module_attribution(repo_path)+if (headand head == base_refdiff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.pyindex 492e5b43d..6b1b0935a 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py@@ -24,6 +24,66 @@ from .incremental import _build_repo_graphlog = structlog.get_logger(__name__)+def _repair_module_attribution(repo_path: Path) -> int:+ """Re-derive every health row's ``module`` from the repo layout on disk.++ Runs on every ``repowise update``, before the "already up to date" return,+ and is cheap enough to justify that: one pruned directory walk looking for+ manifest filenames, then a write only for rows whose label actually moved.+ A repo already correct writes nothing, so the steady-state cost is the walk.++ It exists because ``module`` is *persisted*, and a change to how it is+ derived would otherwise only reach stored rows when something rewrote them+ — a full health re-score at best, a re-index at worst. Neither is an+ acceptable price for a directory label, and neither is necessary: the label+ is a pure function of ``(file_path, package_roots)``, and package roots are+ just the directories holding a manifest. No parse, no embedding, no model.++ Failure-isolated: an unreadable tree or a missing index returns 0 rather+ than failing the update. The next update retries and nothing else depends+ on the result.+ """+ from repowise.cli.helpers import get_db_url_for_repo+ from repowise.core.ingestion.traverser import FileTraverser+ from repowise.core.persistence import (+ create_engine,+ create_session_factory,+ get_session,+ init_db,+ upsert_repository,+ )+ from repowise.core.persistence.crud import backfill_module_attribution++ async def _run() -> int:+ # Scan through the traverser so it honours the same gitignore,+ # submodule and exclude boundaries the index does.+ roots = FileTraverser(Path(repo_path)).package_root_dirs()+ engine = create_engine(get_db_url_for_repo(repo_path))+ try:+ await init_db(engine)+ sf = create_session_factory(engine)+ async with get_session(sf) as session:+ repo = await upsert_repository(+ session, name=Path(repo_path).name, local_path=str(repo_path)+ )+ return await backfill_module_attribution(session, repo.id, roots)+ finally:+ # Matches the other update-time DB opens. File SQLite uses+ # NullPool so nothing is held open across the call, but leaving a+ # live engine behind on a path that runs every update is the kind+ # of thing that turns into a lock report later.+ await engine.dispose()++ try:+ changed = run_async(_run())+ except Exception as exc:+ log.debug("module_attribution_repair_failed", error=str(exc))+ return 0+ if changed:+ console.print(f"[dim]Module attribution: [bold]{changed}[/bold] file(s) corrected[/dim]")+ return changed++async def _coverage_for_rescore(session: Any,repo_id: str,@@ -970,6 +1030,7 @@ async def _rescore_health_from_db(parsed_files=parsed_files,coverage_map=coverage_map,duplication_cache_dir=Path(repo_path) / ".repowise",+ repo_root=repo_path,)hcfg = HealthConfig.load(repo_path)analyzer_config = (diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.pyindex 64a295bf6..53c618933 100644--- a/packages/core/src/repowise/core/analysis/health/engine.py+++ b/packages/core/src/repowise/core/analysis/health/engine.py@@ -31,6 +31,12 @@ from ...ingestion.git_indexer.function_blame import (BlameIndex,distinct_commits_in_range,)++# Package attribution lives in one place, shared with the traverser and with+# the `repowise update` backfill, so all three agree on what a package is.+from ...ingestion.package_roots import module_for as _module_for+from ...ingestion.package_roots import package_roots_from_paths as _package_roots+from ...ingestion.package_roots import scan_package_roots as _scan_package_rootsfrom .biomarkers import FileContext, detect_allfrom .biomarkers.base import HasEdgefrom .complexity import FileComplexity, FunctionComplexity, walk_file@@ -107,92 +113,6 @@ def _log_duplication_diagnostics(report: DuplicationReport) -> None:log.debug("health_duplication_limits", **diag)-def _fallback_module(rel_path: str) -> str | None:- """Top-level directory as a stand-in module label.-- Returns ``None`` for root-level files so the rollup endpoint doesn't- create a phantom "" bucket.- """- norm = rel_path.replace("\\", "/")- if "/" not in norm:- return None- head = norm.split("/", 1)[0]- return head or None---# Files that mark a directory as the root of a distributable package.-#-# Every name here is one the traverser actually emits. That is the binding-# constraint, not the set of manifests that exist: the analyzer sees-# ``parsed_files``, and the traverser drops any file whose language it cannot-# detect. Verified by running ``FileTraverser`` over a directory holding one of-# each — ``go.mod``, ``pom.xml``, ``build.gradle``, ``Gemfile``, ``setup.cfg``-# and ``*.csproj`` are all dropped, so listing them here would be dead entries-# that read as support. Go, Maven, Groovy-Gradle, Ruby and .NET repos therefore-# fall back to the top-level directory, which is exactly what they get today —-# no regression, just no improvement until the traverser yields those files.-#-# Kept conservative besides: a false root fragments the rollup, and the-# top-level fallback is already reasonable. ``requirements.txt`` is deliberately-# absent — it appears in subdirectories that are not packages.-_PACKAGE_MANIFESTS = frozenset(- {- "package.json",- "pyproject.toml",- "setup.py",- "Cargo.toml",- "build.gradle.kts",- "composer.json",- }-)---def _package_roots(all_paths: set[str]) -> set[str]:- """Directories holding a package manifest, from the analyzed file list.-- A manifest at the repo root is ignored: it would put every file in one- bucket, which is the degenerate case this exists to avoid.- """- roots: set[str] = set()- for p in all_paths:- norm = p.replace("\\", "/")- if "/" not in norm:- continue- head, base = norm.rsplit("/", 1)- if base in _PACKAGE_MANIFESTS:- roots.add(head)- return roots---def _module_for(rel_path: str, package_roots: set[str]) -> str | None:- """The package boundary a file belongs to.-- ``module`` is the directory/package axis and nothing else. It used to be- ``community_label or top_level_dir``, which mixed two namespaces in one- field: graph community labels are semantic clusters named after a member- directory, so a file could report a module it does not live in (measured on- this repo: 1,355 of 3,263 files, e.g. every ``packages/api-client`` source- file reporting ``tests/unit``). Worse, only the full-index path ever passed- a community map — the incremental, re-score and ``repowise health`` paths- did not — so which namespace a row carried depended on which code path last- wrote it.-- The top-level directory alone is not enough either: on a monorepo it buckets- 69% of this repo under ``packages``. So prefer the deepest enclosing package- manifest and fall back to the top-level directory, which is exactly the old- behaviour on a repo with no nested packages.- """- norm = rel_path.replace("\\", "/")- if "/" not in norm:- return None- if package_roots:- parts = norm.split("/")- # Deepest first, so a nested package wins over the one containing it.- for i in range(len(parts) - 1, 0, -1):- candidate = "/".join(parts[:i])- if candidate in package_roots:- return candidate- return _fallback_module(norm)def _read_source_lines(abs_path: str) -> list[str] | None:@@ -379,6 +299,7 @@ class HealthAnalyzer:coverage_map: dict[str, dict[str, Any]] | None = None,module_map: dict[str, str] | None = None,duplication_cache_dir: Any | None = None,+ repo_root: Any | None = None,) -> None:self.graph = graphself.git_meta_map = git_meta_map or {}@@ -398,6 +319,39 @@ class HealthAnalyzer:# repo's ``.repowise``). None disables caching — the duplication# pass then re-tokenizes everything, exactly as before.self.duplication_cache_dir = duplication_cache_dir+ # Checkout root, used only to read package boundaries off disk. None+ # falls back to inferring them from the analyzed file list, which sees+ # only the manifests the traverser emitted.+ self.repo_root = repo_root+ self._package_roots_cache: set[str] | None = None++ def _package_boundaries(self, analyzed_paths: set[str]) -> set[str]:+ """Package roots for this repo, decided once per analyzer.++ One answer for every file, so ``module`` is a property of the repo+ layout rather than of whichever code path happens to be running — the+ defect this replaced was four call sites disagreeing.++ Read off disk when a ``repo_root`` is known, because the analyzed file+ list only carries manifests the traverser could language-detect, and it+ drops ``go.mod``, ``pom.xml``, ``build.gradle``, ``Gemfile``,+ ``build.sbt`` and a dozen more. Falls back to the file list otherwise+ (in-memory callers and tests), which is the previous behaviour.+ """+ if self._package_roots_cache is not None:+ return self._package_roots_cache+ roots: set[str] | None = None+ if self.repo_root is not None:+ try:+ roots = _scan_package_roots(self.repo_root)+ except OSError as exc:+ # An unreadable tree must not fail the health pass; the file+ # list still gives the pre-existing answer.+ log.debug("health_package_root_scan_failed", error=str(exc))+ if roots is None:+ roots = _package_roots(analyzed_paths)+ self._package_roots_cache = roots+ return rootsdef analyze(self,@@ -429,10 +383,7 @@ class HealthAnalyzer:# signal (cheap, deterministic, conservative).analyzed_paths = {pf.file_info.path for pf in self.parsed_files}path_basenames = _path_basenames(analyzed_paths)- # One scan of the file list decides the package boundaries for every- # file, so ``module`` is a property of the repo layout rather than of- # whichever code path happens to be running.- package_roots = _package_roots(analyzed_paths)+ package_roots = self._package_boundaries(analyzed_paths)repo_commit_counts = _build_repo_commit_counts(self.git_meta_map)graph_view: HasEdge | None = _ImportEdgeView(self.graph) if self.graph is not None else None@@ -595,10 +546,7 @@ class HealthAnalyzer:analyzed_paths = {pf.file_info.path for pf in self.parsed_files}path_basenames = _path_basenames(analyzed_paths)- # One scan of the file list decides the package boundaries for every- # file, so ``module`` is a property of the repo layout rather than of- # whichever code path happens to be running.- package_roots = _package_roots(analyzed_paths)+ package_roots = self._package_boundaries(analyzed_paths)repo_commit_counts = _build_repo_commit_counts(self.git_meta_map)graph_view: HasEdge | None = _ImportEdgeView(self.graph) if self.graph is not None else Nonediff --git a/packages/core/src/repowise/core/ingestion/languages/registry.py b/packages/core/src/repowise/core/ingestion/languages/registry.pyindex 0a6380164..e36c345b6 100644--- a/packages/core/src/repowise/core/ingestion/languages/registry.py+++ b/packages/core/src/repowise/core/ingestion/languages/registry.py@@ -179,6 +179,27 @@ class LanguageRegistry:"""Return the union of all manifest filenames."""return frozenset(f for s in self._specs.values() for f in s.manifest_files)+ def package_manifest_filenames(self) -> frozenset[str]:+ """Filenames that mark their directory as the root of a package.++ The single source of truth for "is this directory a package". Both+ monorepo detection (:mod:`..traverser`) and health's ``module``+ attribution read this, so registering a language grants a repo in that+ language monorepo bucketing with no second edit.++ This is :meth:`manifest_filenames` minus each spec's+ ``build_config_manifests`` — the whole union is not usable directly+ because it also carries build configuration (``vite.config.js``, the+ .NET ``Directory.Build.*`` family) that appears in directories which+ are not packages, and a false root fragments the module rollup.+ """+ return frozenset(+ f+ for s in self._specs.values()+ for f in s.manifest_files+ if f not in s.build_config_manifests+ )+def blocked_dirs(self) -> frozenset[str]:"""Return the union of all per-language blocked directories."""return frozenset(d for s in self._specs.values() for d in s.blocked_dirs)diff --git a/packages/core/src/repowise/core/ingestion/languages/spec.py b/packages/core/src/repowise/core/ingestion/languages/spec.pyindex e53e8d192..5d0b96147 100644--- a/packages/core/src/repowise/core/ingestion/languages/spec.py+++ b/packages/core/src/repowise/core/ingestion/languages/spec.py@@ -119,6 +119,17 @@ class LanguageSpec:# -- Ecosystem -------------------------------------------------------entry_point_patterns: tuple[str, ...] = () # ("main.py", "app.py")manifest_files: tuple[str, ...] = () # ("pyproject.toml",)+ # The subset of ``manifest_files`` that configures a build rather than+ # declaring a distributable unit. Every name in ``manifest_files`` and not+ # here is a *package root*: it marks its directory as a package boundary+ # for monorepo detection and for health's ``module`` attribution. See+ # ``LanguageRegistry.package_manifest_filenames``.+ #+ # Empty by default on purpose — a new language's manifests grant monorepo+ # bucketing without a second edit, and over-inclusion only makes the+ # rollup finer, where omission makes the language invisible. Populate it+ # for names measured to sit in directories that are not packages.+ build_config_manifests: tuple[str, ...] = () # ("vite.config.js",)lock_files: tuple[str, ...] = () # ("poetry.lock",)generated_suffixes: tuple[str, ...] = () # ("_pb2.py",)shebang_tokens: tuple[str, ...] = () # ("python",)diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/csharp.py b/packages/core/src/repowise/core/ingestion/languages/specs/csharp.pyindex 9eebd3b6d..02d47030a 100644--- a/packages/core/src/repowise/core/ingestion/languages/specs/csharp.py+++ b/packages/core/src/repowise/core/ingestion/languages/specs/csharp.py@@ -39,6 +39,19 @@ SPEC = LanguageSpec("nuget.config","NuGet.Config",),+ # None of these declare a package — the .csproj does. They are MSBuild /+ # NuGet settings that sit at a solution root and are re-declared under+ # tests/, templates/ and misc/, so treating them as package roots would+ # fragment a .NET repo's module rollup (135 such directories measured+ # across the repos censused when this field landed).+ build_config_manifests=(+ "Directory.Build.props",+ "Directory.Build.targets",+ "Directory.Packages.props",+ "global.json",+ "nuget.config",+ "NuGet.Config",+ ),lock_files=("packages.lock.json",),generated_suffixes=(".g.cs",diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/haskell.py b/packages/core/src/repowise/core/ingestion/languages/specs/haskell.pyindex ef37aee15..7780019c5 100644--- a/packages/core/src/repowise/core/ingestion/languages/specs/haskell.py+++ b/packages/core/src/repowise/core/ingestion/languages/specs/haskell.py@@ -11,6 +11,9 @@ SPEC = LanguageSpec(# Setup.hs/Setup.lhs are the Cabal build-script convention — package# plumbing, not domain code; manifests never anchor or face layers.manifest_files=("package.yaml", "stack.yaml", "Setup.hs", "Setup.lhs"),+ # Cabal build scripts, not package declarations — package.yaml/stack.yaml+ # are the roots, and Setup.hs is routinely absent from real packages.+ build_config_manifests=("Setup.hs", "Setup.lhs"),extensions=frozenset({".hs", ".lhs"}),is_passthrough=True,# Lightweight regex resolver: import statements → module declaration index.diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/lean.py b/packages/core/src/repowise/core/ingestion/languages/specs/lean.pyindex 3c65e2a49..5ddf8e333 100644--- a/packages/core/src/repowise/core/ingestion/languages/specs/lean.py+++ b/packages/core/src/repowise/core/ingestion/languages/specs/lean.py@@ -8,6 +8,8 @@ SPEC = LanguageSpec(entry_point_patterns=("Main.lean",),# Lake build manifests / toolchain pin — package plumbing, not domain code.manifest_files=("lakefile.lean", "lakefile.toml", "lean-toolchain"),+ # A toolchain version pin, not a package declaration — the lakefile is.+ build_config_manifests=("lean-toolchain",),extensions=frozenset({".lean"}),is_passthrough=True,# Lightweight regex resolver: import/open statements → module-name index.diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/luau.py b/packages/core/src/repowise/core/ingestion/languages/specs/luau.pyindex c57713624..de30c41f9 100644--- a/packages/core/src/repowise/core/ingestion/languages/specs/luau.py+++ b/packages/core/src/repowise/core/ingestion/languages/specs/luau.py@@ -14,6 +14,8 @@ SPEC = LanguageSpec(heritage_node_types=frozenset(),entry_point_patterns=("init.luau", "init.lua"),manifest_files=("default.project.json", "wally.toml", ".rojo.json"),+ # Rojo CLI settings; the project/wally files declare the package.+ build_config_manifests=(".rojo.json",),blocked_dirs=("Packages", "ServerPackages", "DevPackages"),builtin_calls=frozenset({diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/svelte.py b/packages/core/src/repowise/core/ingestion/languages/specs/svelte.pyindex aa4f01575..99a41b7b4 100644--- a/packages/core/src/repowise/core/ingestion/languages/specs/svelte.py+++ b/packages/core/src/repowise/core/ingestion/languages/specs/svelte.py@@ -24,6 +24,8 @@ SPEC = LanguageSpec(# imported, so they anchor reachability for everything they pull in.entry_point_patterns=("+page.svelte", "+layout.svelte", "+error.svelte", "App.svelte"),manifest_files=("package.json", "svelte.config.js"),+ # Framework config, not a package declaration — package.json is the root.+ build_config_manifests=("svelte.config.js",),lock_files=("package-lock.json", "yarn.lock", "pnpm-lock.yaml"),blocked_dirs=("node_modules", ".svelte-kit", "dist", "build"),# Svelte 5 runes and the template builtins. These are compiler intrinsics,diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/vue.py b/packages/core/src/repowise/core/ingestion/languages/specs/vue.pyindex 5e7c34995..f160fec0e 100644--- a/packages/core/src/repowise/core/ingestion/languages/specs/vue.py+++ b/packages/core/src/repowise/core/ingestion/languages/specs/vue.py@@ -35,6 +35,8 @@ SPEC = LanguageSpec(# they pull in.entry_point_patterns=("App.vue", "app.vue", "error.vue", "default.vue"),manifest_files=("package.json", "vite.config.js", "nuxt.config.ts"),+ # Bundler config, not a package declaration — package.json is the root.+ build_config_manifests=("vite.config.js", "nuxt.config.ts"),lock_files=("package-lock.json", "yarn.lock", "pnpm-lock.yaml"),blocked_dirs=("node_modules", ".nuxt", ".output", "dist", "build"),# Vue's compiler macros and template intrinsics. These are compilerdiff --git a/packages/core/src/repowise/core/ingestion/package_roots.py b/packages/core/src/repowise/core/ingestion/package_roots.pynew file mode 100644index 000000000..3f4b0dcce--- /dev/null+++ b/packages/core/src/repowise/core/ingestion/package_roots.py@@ -0,0 +1,149 @@+"""Which directories of a repo are package roots, read from disk.++One walk, one manifest list. The list is+:meth:`LanguageRegistry.package_manifest_filenames` — the single source of+truth for "is this directory a package" — so registering a language grants a+repo in that language monorepo bucketing with no second edit here.++Why a disk scan rather than the indexed file list: the health analyzer used to+derive package roots from ``parsed_files``, and the traverser drops any file+whose language it cannot detect. Measured on the current tree, 18 manifest+names are dropped that way — ``go.mod``, ``pom.xml``, ``build.gradle``,+``Gemfile``, ``build.sbt``, ``deps.edn``, ``rebar.config`` and ``setup.cfg``+among them — so Go, Maven, Groovy-Gradle, Ruby, Scala, Clojure and Erlang+monorepos silently got the top-level-directory fallback. Reading the manifests'+*paths* off disk needs no parse, no embedding and no ingestion: a package root+is a directory name, and nothing here opens a manifest.++.NET is **not** among the languages this fixes, and deliberately so: the file+that declares a .NET package is the ``.csproj``, which is a glob and therefore+cannot live in a spec's exact-filename ``manifest_files``. Every name C# does+declare is MSBuild/NuGet configuration and is excluded as such, so C#+contributes no package roots and a .NET monorepo keeps the top-level fallback.+Closing that needs glob support in the manifest list, which is its own change.+"""++from __future__ import annotations++import os+from collections.abc import Callable+from pathlib import Path++from ..fs_walk import walk_repo+from .languages.registry import REGISTRY as _LANG_REGISTRY+++def package_manifest_names() -> frozenset[str]:+ """The manifest filenames that mark a directory as a package root."""+ return _LANG_REGISTRY.package_manifest_filenames()+++def _default_prune(repo_root: Path) -> Callable[[Path], bool]:+ """Traversal's own boundary test, imported lazily to avoid a cycle.++ ``walk_repo`` already prunes vendored directories and nested checkouts,+ but not gitignored ones, and a gitignored tree can be large: this repo's+ ``packages/vscode/.vscode-test`` is a downloaded VS Code archive carrying+ 500 vendored ``package.json`` files. Reusing the traverser's test rather+ than restating it also means the scan stops exactly where indexing stops.++ Stock settings on purpose. A repo indexed with ``--include-nested-repos``+ analyzes files the default traverser prunes, so a manifest inside a nested+ repo is not seen as a package root and those files keep the top-level+ fallback. Every writer of ``module`` resolves roots the same way, so they+ agree; passing per-repo settings to some callers and not others is what+ would make them disagree, and disagreement means each ``repowise update``+ reports work forever.+ """+ from .traverser import FileTraverser++ return FileTraverser(repo_root).dir_chain_skipped+++def scan_package_roots(+ repo_root: str | os.PathLike[str],+ *,+ is_pruned: Callable[[Path], bool] | None = None,+) -> set[str]:+ """Repo-relative POSIX directories holding a package manifest.++ A manifest at the repo root is omitted: it would put every file in one+ bucket, which is the degenerate case package attribution exists to avoid.++ Walks via :func:`~repowise.core.fs_walk.walk_repo`, so vendored trees,+ nested git repos and junction cycles are already excluded. ``is_pruned``+ adds the ignore-file layer on top: it receives each candidate directory as+ a repo-relative :class:`Path` and returns True to skip it and everything+ beneath it. It defaults to a stock :class:`FileTraverser`'s boundary test;+ pass a configured traverser's :meth:`~FileTraverser.dir_chain_skipped` (or+ use :meth:`FileTraverser.package_root_dirs`) when the repo has submodule+ or exclude settings.+ """+ root = Path(repo_root)+ names = package_manifest_names()+ prune = is_pruned if is_pruned is not None else _default_prune(root)+ roots: set[str] = set()++ for dirpath, dirnames, filenames in walk_repo(root):+ rel_dir = Path(dirpath).relative_to(root)+ # Prune in place so the walk never descends — the point of the scan is+ # to be cheap enough to run on every update.+ dirnames[:] = [d for d in dirnames if not prune(rel_dir / d)]+ if rel_dir == Path() or not names.intersection(filenames):+ continue+ roots.add(rel_dir.as_posix())++ return roots+++def module_for(rel_path: str, package_roots: set[str]) -> str | None:+ """The package boundary *rel_path* belongs to, or its top-level directory.++ ``module`` is the directory/package axis and nothing else. It used to be+ ``community_label or top_level_dir``, which mixed two namespaces in one+ field: graph community labels are semantic clusters named after a member+ directory, so a file could report a module it does not live in (measured on+ this repo: 1,355 of 3,263 files, e.g. every ``packages/api-client`` source+ file reporting ``tests/unit``). Worse, only the full-index path ever passed+ a community map, so which namespace a row carried depended on which code+ path last wrote it.++ The top-level directory alone is not enough either: on a monorepo it+ buckets 69% of this repo under ``packages``. So prefer the deepest+ enclosing package root and fall back to the top-level directory, which is+ exactly the old behaviour on a repo with no nested packages.++ Root-level files return ``None`` so the rollup does not grow a phantom+ ``""`` bucket.+ """+ norm = rel_path.replace("\\", "/")+ if "/" not in norm:+ return None+ if package_roots:+ parts = norm.split("/")+ # Deepest first, so a nested package wins over the one containing it.+ for i in range(len(parts) - 1, 0, -1):+ candidate = "/".join(parts[:i])+ if candidate in package_roots:+ return candidate+ head = norm.split("/", 1)[0]+ return head or None+++def package_roots_from_paths(all_paths: set[str]) -> set[str]:+ """Package roots inferred from a file list, for callers with no checkout.++ The fallback when :func:`scan_package_roots` cannot run. It sees only+ manifests the traverser emitted, so it misses every language in the dropped+ list above — which is the gap the disk scan exists to close.+ """+ names = package_manifest_names()+ roots: set[str] = set()+ for p in all_paths:+ norm = p.replace("\\", "/")+ if "/" not in norm:+ continue+ head, base = norm.rsplit("/", 1)+ if base in names:+ roots.add(head)+ return rootsdiff --git a/packages/core/src/repowise/core/ingestion/traverser.py b/packages/core/src/repowise/core/ingestion/traverser.pyindex 6d8c7d2fd..98fe04ed7 100644--- a/packages/core/src/repowise/core/ingestion/traverser.py+++ b/packages/core/src/repowise/core/ingestion/traverser.py@@ -200,9 +200,12 @@ _GENERATED_MARKERS: tuple[str, ...] = (_GENERATED_SUFFIXES: tuple[str, ...] = tuple(_LANG_REGISTRY.generated_suffixes())# Manifest files that indicate a package root (for monorepo detection)-_MANIFEST_FILES: frozenset[str] = frozenset(- {"pyproject.toml", "package.json", "Cargo.toml", "go.mod"}-)+# Registry-derived, so registering a language grants its repos monorepo+# detection with no edit here. This was a hardcoded four (pyproject.toml,+# package.json, Cargo.toml, go.mod), which is why a Maven, Ruby or Scala+# monorepo reported no packages at all. .NET still does: its package file is+# the ``.csproj`` glob, which an exact-filename list cannot express.+_MANIFEST_FILES: frozenset[str] = _LANG_REGISTRY.package_manifest_filenames()# Entry-point evidence, all registry-derived: exact filenames (Main.kt,# config.ru), "*"-prefixed filename suffixes (OTP's <name>_app.erl), and@@ -289,17 +292,14 @@ class FileTraverser:self._submodule_paths: frozenset[str] = _parse_gitmodules(self.repo_root)self._include_submodules = include_submodulesself._include_nested_repos = include_nested_repos- # Dotted-module targets of pyproject console scripts. A CLI entry- # module (``repowise-augment = "repowise.cli.augment_hook:main"``)- # has no in-repo importer, so without this it reads as unreachable- # unless its filename happens to match an entry-stem heuristic.- console_scripts = _collect_console_scripts(- self.repo_root,- prune_nested_git=not (include_submodules or include_nested_repos),- )- self._console_script_names = console_scripts.names- self._console_script_modules = console_scripts.modules- self._distributions = console_scripts.distributions+ # Console-script tables are read lazily: collecting them walks the+ # tree parsing every pyproject.toml, which measured at 2.0s of a 2.3s+ # construction on this repo — and callers that only want the boundary+ # test (:meth:`package_root_dirs`, :meth:`dir_chain_skipped`) never+ # need them. Building one per `repowise update` is the reason this is+ # not eager.+ self._console_scripts: ConsoleScriptTables | None = None+ self._console_scripts_prune_nested = not (include_submodules or include_nested_repos)self.stats = TraversalStats()self._count_lock = threading.Lock()log.info(@@ -311,6 +311,36 @@ class FileTraverser:include_nested_repos=include_nested_repos,)+ # ------------------------------------------------------------------+ # Console scripts (lazy — see __init__)+ # ------------------------------------------------------------------++ def _console_script_tables(self) -> ConsoleScriptTables:+ """Dotted-module targets of pyproject console scripts, read once.++ A CLI entry module (``repowise-augment =+ "repowise.cli.augment_hook:main"``) has no in-repo importer, so+ without this it reads as unreachable unless its filename happens to+ match an entry-stem heuristic.+ """+ if self._console_scripts is None:+ self._console_scripts = _collect_console_scripts(+ self.repo_root, prune_nested_git=self._console_scripts_prune_nested+ )+ return self._console_scripts++ @property+ def _console_script_names(self) -> frozenset[str]:+ return self._console_script_tables().names++ @property+ def _console_script_modules(self) -> frozenset[str]:+ return self._console_script_tables().modules++ @property+ def _distributions(self) -> frozenset[str]:+ return self._console_script_tables().distributions+# ------------------------------------------------------------------# Public API# ------------------------------------------------------------------@@ -576,7 +606,7 @@ class FileTraverser:rel_pkg = rel_pkg_path.as_posix()if rel_pkg in seen_paths:continue- if self._dir_chain_skipped(rel_pkg_path):+ if self.dir_chain_skipped(rel_pkg_path):continueseen_paths.add(rel_pkg)lang = _primary_language_in(pkg_dir, prune_nested_git=prune_nested)@@ -596,17 +626,41 @@ class FileTraverser:packages.sort(key=lambda p: p.path)return packages, len(packages) > 1- def _dir_chain_skipped(self, rel_dir: Path) -> bool:+ def package_root_dirs(self) -> set[str]:+ """Every directory holding a package manifest, at any depth.++ Shares :func:`.package_roots.scan_package_roots` with health's module+ attribution, and this traverser's own skip semantics, so the two agree+ on what a package is. Distinct from :meth:`get_repo_structure`'s+ ``packages``, which stops at depth 2 and pays for language and+ entry-point detection per package.+ """+ from .package_roots import scan_package_roots++ return scan_package_roots(self.repo_root, is_pruned=self.dir_chain_skipped)++ def dir_chain_skipped(self, rel_dir: Path) -> bool:"""True if *rel_dir* (or any ancestor) would be pruned by ``_walk``.Reuses :meth:`_should_skip_dir` level by level so monorepo packagedetection has exactly the same boundary semantics as file traversal(blocked dirs, submodules, nested git repos, gitignore/excludes).++ That includes each level's *nested* ignore spec, which this used to+ omit: ``_should_skip_dir`` takes the containing directory's spec as an+ argument and got ``None`` here, so a directory ignored by a nested+ ``.gitignore`` was reported as walkable even though ``_walk`` prunes+ it. On this repo that let ``packages/vscode/.vscode-test`` — a+ downloaded VS Code archive ignored by ``packages/vscode/.gitignore`` —+ through, carrying 500 vendored ``package.json`` files with it."""cur = Path()for part in rel_dir.parts:+ parent_abs = self.repo_root / curcur = cur / part- if self._should_skip_dir(part, cur, self.repo_root / cur):+ if self._should_skip_dir(+ part, cur, self.repo_root / cur, self._get_dir_ignore(parent_abs)+ ):return Truereturn Falsediff --git a/packages/core/src/repowise/core/persistence/crud/analysis/__init__.py b/packages/core/src/repowise/core/persistence/crud/analysis/__init__.pyindex bb668d644..82aa85ae5 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/__init__.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/__init__.py@@ -32,6 +32,7 @@ from .health import (HEALTH_SNAPSHOT_RETENTION,HealthSnapshotHeadline,HealthSnapshotScalars,+ backfill_module_attribution,get_average_health,get_deduction_by_path,get_file_language_map,@@ -62,6 +63,7 @@ __all__ = ["HEALTH_SNAPSHOT_RETENTION","HealthSnapshotHeadline","HealthSnapshotScalars",+ "backfill_module_attribution","covered_source_files","files_covered_by","get_average_health",diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/health.py b/packages/core/src/repowise/core/persistence/crud/analysis/health.pyindex f240e57d0..befed5d0b 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/health.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/health.py@@ -220,6 +220,43 @@ async def save_health_metrics(await session.flush()+async def backfill_module_attribution(+ session: AsyncSession,+ repository_id: str,+ package_roots: set[str],+) -> int:+ """Recompute every metric row's ``module`` from the repo layout.++ Returns the number of rows changed, so a caller can stay silent when there+ was nothing to correct.++ ``module`` is a pure function of ``(file_path, package_roots)`` — no parse,+ no symbols, no git, no model — so correcting it never needs the rows+ rewritten by a re-score, let alone a re-index. That is the whole point:+ a directory label must not cost users an indexing run.++ Idempotent by construction, and it converges with the indexer rather than+ fighting it: both call the same :func:`module_for` over the same+ disk-scanned roots, so a full index writing a *different* value would mean+ the two disagree about the repo layout, and running this again would flip+ it back rather than settle.+ """+ from ....ingestion.package_roots import module_for++ result = await session.execute(+ select(HealthFileMetric).where(HealthFileMetric.repository_id == repository_id)+ )+ changed = 0+ for row in result.scalars().all():+ expected = module_for(row.file_path, package_roots)+ if row.module != expected:+ row.module = expected+ changed += 1+ if changed:+ await session.flush()+ return changed++async def _health_exclude_spec(session: AsyncSession, repository_id: str) -> Any:repo = await session.get(Repository, repository_id)if repo is None:diff --git a/packages/core/src/repowise/core/pipeline/incremental.py b/packages/core/src/repowise/core/pipeline/incremental.pyindex 85f0aaf63..b8f564fe8 100644--- a/packages/core/src/repowise/core/pipeline/incremental.py+++ b/packages/core/src/repowise/core/pipeline/incremental.py@@ -345,6 +345,7 @@ def run_partial_analysis(git_meta_map=git_meta_map,parsed_files=parsed_files,duplication_cache_dir=Path(repo_path) / ".repowise",+ repo_root=repo_path,)_health_changed = {fd.path for fd in file_diffs if fd.status in ("added", "modified")}if _health_changed:diff --git a/packages/core/src/repowise/core/pipeline/phases/analysis.py b/packages/core/src/repowise/core/pipeline/phases/analysis.pyindex c03acb1f7..b2609ac3c 100644--- a/packages/core/src/repowise/core/pipeline/phases/analysis.py+++ b/packages/core/src/repowise/core/pipeline/phases/analysis.py@@ -210,6 +210,7 @@ async def _run_health_analysis(module_map=module_map,coverage_map=coverage_map,duplication_cache_dir=(repo_path / ".repowise") if repo_path is not None else None,+ repo_root=repo_path,)# Load per-file override rules from `.repowise/health-rules.json`.diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.pyindex 10e0c02bf..6a2055c55 100644--- a/tests/integration/test_cli.py+++ b/tests/integration/test_cli.py@@ -513,6 +513,97 @@ class TestUpdateWorkingTree:assert "uncommitted_addition" not in self._symbol_names(git_work_repo)+class TestModuleAttributionRepair:+ """A wrong ``module`` corrects itself on a plain update, quiet repo included.++ ``module`` is persisted, so changing how it is derived only reaches stored+ rows when something rewrites them. The alternative trigger — bumping+ ``HEALTH_ANALYZER_VERSION`` — buys that at the price of a full health+ re-score, and its gate is only consulted once an update reaches the+ incremental path, so a repo with no new commits never picks it up at all.+ """++ def _modules(self, repo):+ import sqlite3+ from contextlib import closing++ db = repo / ".repowise" / "wiki.db"+ with closing(sqlite3.connect(db)) as conn:+ return dict(conn.execute("SELECT file_path, module FROM health_file_metrics"))++ def _corrupt(self, repo):+ """Write the labels the old community-map path produced."""+ import sqlite3+ from contextlib import closing++ db = repo / ".repowise" / "wiki.db"+ with closing(sqlite3.connect(db)) as conn:+ conn.execute("UPDATE health_file_metrics SET module = 'tests/unit (3)'")+ conn.commit()++ def test_a_quiet_update_repairs_stale_module_labels(self, runner, git_work_repo):+ r0 = runner.invoke(+ cli, ["init", str(git_work_repo), "--index-only"], catch_exceptions=False+ )+ assert r0.exit_code == 0, r0.output+ indexed = self._modules(git_work_repo)+ assert indexed, "no health rows to test against"++ self._corrupt(git_work_repo)+ assert set(self._modules(git_work_repo).values()) == {"tests/unit (3)"}++ # No new commits and no config change: update takes the early return.+ r1 = runner.invoke(+ cli, ["update", str(git_work_repo), "--index-only"], catch_exceptions=False+ )+ assert r1.exit_code == 0, r1.output+ assert "Already up to date" in r1.output++ # Repaired anyway, and back to exactly what the indexer wrote.+ assert self._modules(git_work_repo) == indexed++ def test_dry_run_writes_nothing(self, runner, git_work_repo):+ """``--dry-run`` means nothing written, and the repair is a write.++ It runs before the early return so a quiet repo is still corrected,+ which puts it upstream of every other write in the command — and+ therefore upstream of the guard they all sit behind.+ """+ r0 = runner.invoke(+ cli, ["init", str(git_work_repo), "--index-only"], catch_exceptions=False+ )+ assert r0.exit_code == 0, r0.output++ self._corrupt(git_work_repo)+ corrupted = self._modules(git_work_repo)++ r1 = runner.invoke(+ cli,+ ["update", str(git_work_repo), "--index-only", "--dry-run"],+ catch_exceptions=False,+ )+ assert r1.exit_code == 0, r1.output+ assert "Module attribution" not in r1.output+ assert self._modules(git_work_repo) == corrupted++ def test_a_second_update_changes_nothing(self, runner, git_work_repo):+ """Idempotent, and silent when there is nothing to do.++ If the repair and the indexer disagreed about the repo layout, the two+ would alternate and every update would report work.+ """+ r0 = runner.invoke(+ cli, ["init", str(git_work_repo), "--index-only"], catch_exceptions=False+ )+ assert r0.exit_code == 0, r0.output++ r1 = runner.invoke(+ cli, ["update", str(git_work_repo), "--index-only"], catch_exceptions=False+ )+ assert r1.exit_code == 0, r1.output+ assert "Module attribution" not in r1.output++class TestUpdateConfigChangeDetection:def _state(self, repo):import jsondiff --git a/tests/unit/cli/test_health_cmd_exclusions.py b/tests/unit/cli/test_health_cmd_exclusions.pynew file mode 100644index 000000000..ff9ac7999--- /dev/null+++ b/tests/unit/cli/test_health_cmd_exclusions.py@@ -0,0 +1,82 @@+"""``repowise health`` must analyze the file set the index analyzed.++It persists into the same ``health_file_metrics`` rows the indexer writes, so+the two have to agree on which files exist. It built its traverser with the+submodule and nested-repo flags from ``state.json`` but *not* the config's+``exclude_patterns``, so on a repo with exclusions it scored — and overwrote+rows for — files the index had deliberately dropped. On a repo excluding a+directory that holds a package manifest it could also write a different+``module`` than the index did, which is the "whichever path last wrote the row"+defect module attribution exists to end.+"""++from __future__ import annotations++import pytest+from click.testing import CliRunner++from repowise.cli.commands.health_cmd import command as health_cmd+from repowise.cli.main import cli+++class _StopHere(RuntimeError):+ """Raised once the traverser has been constructed — nothing after it is+ under test, and everything after it wants a real index."""+++def _traverser_spy(monkeypatch: pytest.MonkeyPatch) -> dict:+ """Capture FileTraverser's kwargs, then abort the command."""+ import repowise.core.ingestion as ingestion++ seen: dict = {}++ class _Recorder:+ def __init__(self, repo_path, **kwargs):+ seen.update(kwargs)+ raise _StopHere++ monkeypatch.setattr(ingestion, "FileTraverser", _Recorder)+ return seen+++def _invoke(monkeypatch: pytest.MonkeyPatch, tmp_path, config: dict) -> dict:+ seen = _traverser_spy(monkeypatch)+ monkeypatch.setattr(health_cmd, "load_state", lambda _p: {})+ monkeypatch.setattr(health_cmd, "load_config", lambda _p: config)++ result = CliRunner().invoke(cli, ["health", str(tmp_path)])++ assert isinstance(result.exception, _StopHere), result.output+ return seen+++def test_the_config_exclusions_reach_the_traverser(monkeypatch, tmp_path) -> None:+ seen = _invoke(monkeypatch, tmp_path, {"exclude_patterns": ["vendor/**", "*.gen.ts"]})+ assert seen["extra_exclude_patterns"] == ["vendor/**", "*.gen.ts"]+++def test_no_exclusions_configured_passes_none(monkeypatch, tmp_path) -> None:+ """``None`` is the traverser's documented "nothing to add" value; an empty+ list would be equivalent but reads as "exclusions were configured"."""+ seen = _invoke(monkeypatch, tmp_path, {})+ assert seen["extra_exclude_patterns"] is None+++def test_the_state_flags_still_reach_it(monkeypatch, tmp_path) -> None:+ """Guard against the exclusion fix displacing what was already passed."""+ seen = _traverser_spy(monkeypatch)+ monkeypatch.setattr(+ health_cmd,+ "load_state",+ lambda _p: {"include_submodules": True, "include_nested_repos": True},+ )+ monkeypatch.setattr(health_cmd, "load_config", lambda _p: {"exclude_patterns": ["x/**"]})++ result = CliRunner().invoke(cli, ["health", str(tmp_path)])++ assert isinstance(result.exception, _StopHere), result.output+ assert seen == {+ "include_submodules": True,+ "include_nested_repos": True,+ "extra_exclude_patterns": ["x/**"],+ }diff --git a/tests/unit/cli/test_module_attribution_repair.py b/tests/unit/cli/test_module_attribution_repair.pynew file mode 100644index 000000000..179d24b29--- /dev/null+++ b/tests/unit/cli/test_module_attribution_repair.py@@ -0,0 +1,105 @@+"""``repowise update`` repairs module attribution, including on a quiet repo.++The trigger placement is the design decision here. A ``HEALTH_ANALYZER_VERSION``+bump would also reach these rows, but it buys a directory label at the price of+a full health re-score — and the re-score gate is only consulted once an update+reaches the incremental path, so a repo with no new commits returns at "already+up to date" and never picks the correction up. The repair runs before that+return and costs one pruned directory walk.+"""++from __future__ import annotations++from pathlib import Path++import pytest++from repowise.cli.commands.update_cmd import persistence+++def test_it_scans_through_the_traverser_and_writes_what_changed(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ """Roots come from the traverser, not a bare walk.++ A bare walk descends into nested checkouts and vendored trees. On this repo+ that took the scanned-root count from 9 to 512 and the scan from 2.3s to+ minutes, on a repo that physically contains sixteen worktrees.+ """+ seen: dict = {}++ class _Traverser:+ def __init__(self, repo_path):+ seen["repo_path"] = repo_path++ def package_root_dirs(self):+ return {"services/api"}++ async def _fake_backfill(_session, _repo_id, package_roots):+ seen["roots"] = package_roots+ return 4++ import repowise.core.ingestion.traverser as traverser_mod+ import repowise.core.persistence.crud as crud_mod++ monkeypatch.setattr(traverser_mod, "FileTraverser", _Traverser)+ monkeypatch.setattr(crud_mod, "backfill_module_attribution", _fake_backfill)++ assert persistence._repair_module_attribution(tmp_path) == 4+ assert seen["roots"] == {"services/api"}+ assert Path(seen["repo_path"]) == tmp_path+++def test_a_failure_never_fails_the_update(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ """An unreadable tree or a missing index must not abort ``update``.++ Nothing downstream consumes the result, and the next update retries, so+ swallowing beats propagating.+ """+ import repowise.core.ingestion.traverser as traverser_mod++ def _boom(_repo_path):+ raise OSError("tree is gone")++ monkeypatch.setattr(traverser_mod, "FileTraverser", _boom)++ assert persistence._repair_module_attribution(tmp_path) == 0+++def test_it_reports_only_when_it_changed_something(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys+) -> None:+ """A no-op update must stay silent.++ It runs on every update including quiet ones, so a line per run would be+ noise on every repo that is already correct — which, censused, is every+ repo without a nested manifest.+ """++ class _Traverser:+ def __init__(self, repo_path):+ pass++ def package_root_dirs(self):+ return set()++ import repowise.core.ingestion.traverser as traverser_mod+ import repowise.core.persistence.crud as crud_mod++ monkeypatch.setattr(traverser_mod, "FileTraverser", _Traverser)++ async def _no_change(_s, _r, _roots):+ return 0++ monkeypatch.setattr(crud_mod, "backfill_module_attribution", _no_change)+ assert persistence._repair_module_attribution(tmp_path) == 0+ assert "Module attribution" not in capsys.readouterr().out++ async def _changed(_s, _r, _roots):+ return 7++ monkeypatch.setattr(crud_mod, "backfill_module_attribution", _changed)+ assert persistence._repair_module_attribution(tmp_path) == 7+ assert "Module attribution" in capsys.readouterr().outdiff --git a/tests/unit/health/test_module_attribution.py b/tests/unit/health/test_module_attribution.pyindex 1d8d78f70..1971933cc 100644--- a/tests/unit/health/test_module_attribution.py+++ b/tests/unit/health/test_module_attribution.py@@ -10,8 +10,6 @@ change: 1,355 of 3,263 files reported a module they do not live in.from __future__ import annotationsfrom repowise.core.analysis.health.engine import (- _PACKAGE_MANIFESTS,- _fallback_module,_module_for,_package_roots,)@@ -30,28 +28,24 @@ def test_package_roots_are_read_from_manifests_in_the_file_list():assert roots == {"packages/core", "packages/ui"}-def test_every_listed_manifest_is_one_the_traverser_actually_emits():- """The list is bounded by what reaches ``parsed_files``, not by what exists.+def test_the_file_list_fallback_only_sees_manifests_the_traverser_emits():+ """Why the file list is a fallback and the disk scan is the real answer.- The analyzer only ever sees traversed files, and the traverser drops any- file whose language it cannot detect — ``go.mod``, ``pom.xml``,- ``build.gradle``, ``Gemfile`` and ``setup.cfg`` among them. Listing those- would be dead entries that read as support for Go, Maven and Ruby- monorepos. This pins the constraint so the list cannot quietly grow past it.+ The analyzer's ``parsed_files`` only carry manifests the traverser could+ language-detect, and it drops 18 of them — ``go.mod``, ``pom.xml``,+ ``build.gradle``, ``Gemfile``, ``build.sbt`` among them. Deriving roots+ from that list therefore cannot see a Go, Maven, Groovy-Gradle, Ruby or+ Scala package, which is exactly the gap ``scan_package_roots`` closes."""- import tempfile- from pathlib import Path+ from repowise.core.ingestion.package_roots import package_manifest_names- from repowise.core.ingestion.traverser import FileTraverser-- root = Path(tempfile.mkdtemp())- for i, name in enumerate(sorted(_PACKAGE_MANIFESTS)):- pkg = root / f"pkg{i}"- pkg.mkdir()- (pkg / name).write_text("{}\n" if name.endswith("json") else "x = 1\n", encoding="utf-8")-- emitted = {p.path.replace("\\", "/").rsplit("/", 1)[-1] for p in FileTraverser(root).traverse()}- assert emitted >= _PACKAGE_MANIFESTS, sorted(_PACKAGE_MANIFESTS - emitted)+ dropped = {"go.mod", "pom.xml", "build.gradle", "Gemfile", "build.sbt"}+ # They are package manifests...+ assert dropped <= package_manifest_names()+ # ...but a file list can only offer what the traverser emitted, and these+ # never arrive, so the fallback yields nothing for them.+ assert _package_roots({f"svc/{name}" for name in dropped}) == {"svc"}+ assert _package_roots({"svc/main.go", "libs/Main.java"}) == set()def test_a_repo_root_manifest_is_not_a_package_root():@@ -93,8 +87,12 @@ def test_no_manifests_anywhere_is_exactly_the_old_behaviour():a Next.js frontend): zero nested manifests, so both keep the attributionthey already had."""- for path in ("app/routers/files.py", "src/components/x.tsx", "tests/unit/a.py"):- assert _module_for(path, set()) == _fallback_module(path)+ for path, top_level in (+ ("app/routers/files.py", "app"),+ ("src/components/x.tsx", "src"),+ ("tests/unit/a.py", "tests"),+ ):+ assert _module_for(path, set()) == top_leveldef test_root_level_files_have_no_module():@@ -144,6 +142,108 @@ def test_the_engine_wires_real_package_roots_through_to_the_metric(tmp_path):assert modules["packages/ui/widget.py"] == "packages/ui"+def test_the_engine_reads_package_roots_off_disk_not_the_file_list(tmp_path):+ """The case the whole change exists for, and the only one that proves it.++ A ``pyproject.toml`` fixture cannot: the traverser emits it, so the file+ list and the disk scan agree and the analyzer looks correct either way.+ ``go.mod`` is dropped as a file, so its package is visible only to the+ scan — if the engine falls back to ``parsed_files`` this returns+ ``services``, the top-level container, which is the bug.+ """+ from repowise.core.analysis.health import HealthAnalyzer+ from repowise.core.ingestion.parser import parse_file+ from repowise.core.ingestion.traverser import FileTraverser++ for rel, body in (+ ("services/api/go.mod", "module example.com/api\n\ngo 1.22\n"),+ ("services/api/main.go", "package main\n\nfunc main() {}\n"),+ # Nested module inside another: the deepest-first loop's reason to exist.+ ("services/api/internal/tools/go.mod", "module example.com/api/tools\n"),+ ("services/api/internal/tools/tool.go", "package tools\n\nfunc T() {}\n"),+ # No manifest anywhere above it -> top-level fallback, unchanged.+ ("scripts/deploy.py", "def deploy():\n return 1\n"),+ ):+ p = tmp_path / rel+ p.parent.mkdir(parents=True, exist_ok=True)+ p.write_text(body, encoding="utf-8")++ parsed = [+ parse_file(f, (tmp_path / f.path).read_bytes()) for f in FileTraverser(tmp_path).traverse()+ ]+ report = HealthAnalyzer(None, parsed_files=parsed, repo_root=tmp_path).analyze()+ modules = {m.file_path: m.module for m in report.metrics}++ assert modules["services/api/main.go"] == "services/api"+ assert modules["services/api/internal/tools/tool.go"] == "services/api/internal/tools"+ assert modules["scripts/deploy.py"] == "scripts"+++def test_without_a_repo_root_the_engine_still_produces_the_old_answer(tmp_path):+ """The fallback is a degradation, not a failure.++ In-memory callers and hosted paths that cannot supply a checkout must keep+ working; they just cannot see a manifest the traverser dropped.+ """+ from repowise.core.analysis.health import HealthAnalyzer+ from repowise.core.ingestion.parser import parse_file+ from repowise.core.ingestion.traverser import FileTraverser++ for rel, body in (+ ("services/api/go.mod", "module example.com/api\n"),+ ("services/api/main.go", "package main\n\nfunc main() {}\n"),+ ):+ p = tmp_path / rel+ p.parent.mkdir(parents=True, exist_ok=True)+ p.write_text(body, encoding="utf-8")++ parsed = [+ parse_file(f, (tmp_path / f.path).read_bytes()) for f in FileTraverser(tmp_path).traverse()+ ]+ report = HealthAnalyzer(None, parsed_files=parsed).analyze()+ modules = {m.file_path: m.module for m in report.metrics}++ assert modules["services/api/main.go"] == "services"+++def test_every_analyzer_call_site_supplies_a_repo_root():+ """The convergence claim rests on this, and nothing else asserts it.++ ``module`` is written by four paths — full index, incremental update,+ ``_rescore_health_from_db`` and ``repowise health`` — and corrected by a+ fifth, the ``repowise update`` repair. They agree only because all five+ scan the same repo off disk. Drop ``repo_root`` at one analyzer call site+ and that path silently reverts to the file-list fallback: on a Go monorepo+ it writes ``services`` while the repair writes ``services/api``, so every+ update from then on reports files corrected and the two never settle.++ Asserted on the call sites because the failure is an *omitted* keyword —+ there is no value to observe, and a test that constructs the analyzer+ directly (as the rest of this file does) cannot see the omission at all.+ """+ import ast+ from pathlib import Path as _Path++ import repowise.cli.commands.health_cmd.command as health_cmd+ import repowise.cli.commands.update_cmd.persistence as rescore+ import repowise.core.pipeline.incremental as incremental+ import repowise.core.pipeline.phases.analysis as full_index++ for module in (full_index, incremental, rescore, health_cmd):+ source = _Path(module.__file__).read_text(encoding="utf-8")+ calls = [+ node+ for node in ast.walk(ast.parse(source))+ if isinstance(node, ast.Call)+ and isinstance(node.func, ast.Name)+ and node.func.id == "HealthAnalyzer"+ ]+ assert calls, f"no HealthAnalyzer call found in {module.__name__}"+ for call in calls:+ kwargs = {kw.arg for kw in call.keywords}+ assert "repo_root" in kwargs, f"{module.__name__}:{call.lineno} omits repo_root"++def test_a_community_map_can_no_longer_change_the_answer(tmp_path):"""The regression that motivated this, as an executable invariant.diff --git a/tests/unit/ingestion/test_package_roots.py b/tests/unit/ingestion/test_package_roots.pynew file mode 100644index 000000000..60460c05c--- /dev/null+++ b/tests/unit/ingestion/test_package_roots.py@@ -0,0 +1,169 @@+"""One answer to "is this directory a package", read off disk.++Three lists used to answer it and disagree: the traverser's hardcoded four, the+registry's every-manifest union, and health's own six. The registry is now the+single source, and package roots are scanned from the filesystem rather than+inferred from the indexed file list — because the traverser drops 18 manifest+names it cannot language-detect, which silently cost Go, Maven, Groovy-Gradle,+Ruby, Scala, Clojure and Erlang monorepos any package attribution at all.++.NET is not among them: its package file is the ``.csproj`` glob, which an+exact-filename list cannot express, so every name C# declares is build+configuration and is excluded as such.+"""++from __future__ import annotations++from pathlib import Path++from repowise.core.ingestion.languages.registry import REGISTRY+from repowise.core.ingestion.package_roots import (+ module_for,+ package_manifest_names,+ scan_package_roots,+)+from repowise.core.ingestion.traverser import FileTraverser++# Names the traverser drops as files but which unambiguously declare a package.+# Measured by running FileTraverser over a directory holding one of each.+DROPPED_BUT_REAL = ("go.mod", "pom.xml", "build.gradle", "Gemfile", "build.sbt")+++def _write(root: Path, rel: str, body: str = "x = 1\n") -> None:+ p = root / rel+ p.parent.mkdir(parents=True, exist_ok=True)+ p.write_text(body, encoding="utf-8")+++# ---------------------------------------------------------------------------+# The single source of truth+# ---------------------------------------------------------------------------+++def test_registering_a_language_grants_monorepo_bucketing():+ """A new spec's manifests count as package roots with no second edit.++ The gap this closes: the traverser's list was four hardcoded names, so+ adding a language to the registry did nothing for monorepo detection and+ the omission was invisible. ``build_config_manifests`` defaults to empty+ precisely so the default is "works".+ """+ names = package_manifest_names()+ for spec in REGISTRY._specs.values():+ expected = set(spec.manifest_files) - set(spec.build_config_manifests)+ assert expected <= names, spec.tag+++def test_build_config_is_excluded_because_it_is_not_a_package():+ """The registry union is not usable directly, which is why this subtracts.++ ``vite.config.js`` is the entry usually cited, but the measured offender is+ the .NET family: 135 directories across the repos censused hold a+ ``Directory.Build.props`` / ``global.json`` / ``nuget.config`` and no+ package at all. Each would be a false root, and a false root fragments the+ module rollup.+ """+ names = package_manifest_names()+ for config_only in (+ "vite.config.js",+ "nuxt.config.ts",+ "svelte.config.js",+ "Directory.Build.props",+ "Directory.Packages.props",+ "global.json",+ "nuget.config",+ "lean-toolchain",+ "Setup.hs",+ ):+ assert config_only in REGISTRY.manifest_filenames(), config_only+ assert config_only not in names, config_only+++def test_the_traverser_reads_the_same_list():+ """Not a third copy: monorepo detection and health share one source."""+ from repowise.core.ingestion.traverser import _MANIFEST_FILES++ assert package_manifest_names() == _MANIFEST_FILES+++# ---------------------------------------------------------------------------+# The disk scan+# ---------------------------------------------------------------------------+++def test_the_scan_finds_manifests_the_traverser_drops_as_files(tmp_path):+ """The whole point. These never reach ``parsed_files``, but they are roots."""+ for i, name in enumerate(DROPPED_BUT_REAL):+ _write(tmp_path, f"pkg{i}/{name}", "<x/>\n")+ _write(tmp_path, f"pkg{i}/code.py", "y = 1\n")++ emitted = {fi.path.replace("\\", "/") for fi in FileTraverser(tmp_path).traverse()}+ for i, name in enumerate(DROPPED_BUT_REAL):+ assert f"pkg{i}/{name}" not in emitted, name++ assert scan_package_roots(tmp_path) == {f"pkg{i}" for i in range(len(DROPPED_BUT_REAL))}+++def test_a_repo_root_manifest_is_not_a_package_root(tmp_path):+ """Otherwise a plain single-package repo puts every file in one bucket."""+ _write(tmp_path, "go.mod", "module x\n")+ _write(tmp_path, "main.go", "package main\n")+ assert scan_package_roots(tmp_path) == set()+++def test_a_directory_with_only_build_config_is_not_a_root(tmp_path):+ _write(tmp_path, "solution/Directory.Build.props", "<Project/>\n")+ _write(tmp_path, "solution/global.json", "{}\n")+ _write(tmp_path, "app/pom.xml", "<project/>\n")+ assert scan_package_roots(tmp_path) == {"app"}+++def test_a_nested_package_is_found_at_any_depth(tmp_path):+ """``RepoStructure.packages`` stops at depth 2; module attribution must not.++ ``packages/vscode/webview`` on this repo is depth 3, and a Go module under+ ``services/api/internal/tools`` is depth 4.+ """+ _write(tmp_path, "services/api/go.mod", "module api\n")+ _write(tmp_path, "services/api/internal/tools/go.mod", "module tools\n")+ roots = scan_package_roots(tmp_path)+ assert roots == {"services/api", "services/api/internal/tools"}+ assert module_for("services/api/internal/tools/t.go", roots) == "services/api/internal/tools"+ assert module_for("services/api/main.go", roots) == "services/api"+++def test_a_package_with_no_manifest_falls_back_to_its_top_level_directory(tmp_path):+ _write(tmp_path, "libs/billing/pom.xml", "<project/>\n")+ roots = scan_package_roots(tmp_path)+ assert module_for("scripts/deploy/run.py", roots) == "scripts"+++def test_the_scan_stops_where_traversal_stops(tmp_path):+ """A vendored or ignored tree must not contribute roots.++ Cost, mostly: a root under ``node_modules`` can never match an analyzed+ file, but walking one is the difference between milliseconds and minutes.+ """+ _write(tmp_path, "app/go.mod", "module app\n")+ _write(tmp_path, "node_modules/dep/package.json", '{"name":"dep"}\n')+ _write(tmp_path, "vendor/lib/go.mod", "module lib\n")+ assert scan_package_roots(tmp_path) == {"app"}+++def test_a_nested_gitignore_prunes_the_scan(tmp_path):+ """``dir_chain_skipped`` used to ignore per-directory ignore files.++ It passes ``_should_skip_dir`` the containing directory's spec, and got+ ``None``, so a directory ignored by a nested ``.gitignore`` was reported as+ walkable even though ``_walk`` prunes it. On this repo that admitted+ ``packages/vscode/.vscode-test`` — a downloaded VS Code archive ignored by+ ``packages/vscode/.gitignore`` — and the 500 vendored ``package.json``+ files inside it, taking the scanned-root count from 9 to 512.+ """+ _write(tmp_path, "packages/tool/package.json", '{"name":"tool"}\n')+ _write(tmp_path, "packages/tool/.gitignore", ".test-fixtures/\n")+ _write(tmp_path, "packages/tool/.test-fixtures/vendored/package.json", '{"name":"v"}\n')++ traverser = FileTraverser(tmp_path)+ assert traverser.dir_chain_skipped(Path("packages/tool/.test-fixtures"))+ assert traverser.package_root_dirs() == {"packages/tool"}diff --git a/tests/unit/persistence/test_module_backfill.py b/tests/unit/persistence/test_module_backfill.pynew file mode 100644index 000000000..68d84cc73--- /dev/null+++ b/tests/unit/persistence/test_module_backfill.py@@ -0,0 +1,163 @@+"""Correcting ``module`` must never cost an index run.++``module`` is a persisted column, so a change to how it is derived would+normally only reach stored rows when something rewrote them — a full health+re-score at best, a re-index at worst. Neither is an acceptable price for a+directory label, and neither is necessary: the label is a pure function of+``(file_path, package_roots)``, so it can be recomputed in place from a+directory scan alone. No parse, no embedding, no model.+"""++from __future__ import annotations++from repowise.core.persistence.crud import (+ backfill_module_attribution,+ get_health_metrics,+ save_health_metrics,+ upsert_repository,+)++ROOTS = {"services/api", "services/api/internal/tools", "libs/billing"}+++def _metric(path: str, module: str | None) -> dict:+ return {+ "file_path": path,+ "score": 5.0,+ "max_ccn": 1,+ "max_nesting": 1,+ "nloc": 10,+ "has_test_file": False,+ "module": module,+ }+++async def _modules(session, repo_id) -> dict[str, str | None]:+ rows = await get_health_metrics(session, repo_id)+ return {r.file_path: r.module for r in rows}+++async def test_it_corrects_rows_no_rescore_would_have_touched(async_session, tmp_path) -> None:+ """The rows a Go or Maven monorepo has today: bucketed by top-level dir.++ Nothing rewrites these until a full re-score fires, which is gated on a+ 7-day decay timer. This is the mechanism that makes the correction land+ without waiting for it, and without an index run.+ """+ repo = await upsert_repository(async_session, name="repo", local_path=str(tmp_path))+ await save_health_metrics(+ async_session,+ repo.id,+ [+ _metric("services/api/main.go", "services"),+ _metric("services/api/internal/tools/t.go", "services"),+ _metric("libs/billing/src/Main.java", "libs"),+ _metric("scripts/deploy.py", "scripts"),+ ],+ )++ changed = await backfill_module_attribution(async_session, repo.id, ROOTS)++ assert changed == 3+ assert await _modules(async_session, repo.id) == {+ "services/api/main.go": "services/api",+ # Deepest package wins over the one containing it.+ "services/api/internal/tools/t.go": "services/api/internal/tools",+ "libs/billing/src/Main.java": "libs/billing",+ # No enclosing package: the top-level fallback is already right.+ "scripts/deploy.py": "scripts",+ }+++async def test_running_it_again_changes_nothing(async_session, tmp_path) -> None:+ """Idempotent, because it runs on every update — including quiet ones.++ A second pass reporting work would mean it is fighting whatever wrote the+ rows rather than agreeing with it.+ """+ repo = await upsert_repository(async_session, name="repo", local_path=str(tmp_path))+ await save_health_metrics(async_session, repo.id, [_metric("services/api/main.go", "services")])++ assert await backfill_module_attribution(async_session, repo.id, ROOTS) == 1+ assert await backfill_module_attribution(async_session, repo.id, ROOTS) == 0+ assert await backfill_module_attribution(async_session, repo.id, ROOTS) == 0+++async def test_it_agrees_with_what_the_indexer_writes(async_session, tmp_path) -> None:+ """The backfill and the analyzer must not disagree about the repo layout.++ Both call ``module_for`` over the same disk-scanned roots. If a full index+ wrote a different value, the next update would flip it back and the two+ would alternate forever — so this pins that they are the same function.+ """+ from repowise.core.ingestion.package_roots import module_for++ repo = await upsert_repository(async_session, name="repo", local_path=str(tmp_path))+ paths = [+ "services/api/main.go",+ "services/api/internal/tools/t.go",+ "libs/billing/src/Main.java",+ "scripts/deploy.py",+ "README.md",+ ]+ # Seed with exactly what the analyzer would compute, then backfill.+ await save_health_metrics(+ async_session, repo.id, [_metric(p, module_for(p, ROOTS)) for p in paths]+ )++ assert await backfill_module_attribution(async_session, repo.id, ROOTS) == 0+++async def test_a_root_level_file_is_cleared_to_none(async_session, tmp_path) -> None:+ """``None``, not ``""`` — the rollup must not grow a phantom empty bucket."""+ repo = await upsert_repository(async_session, name="repo", local_path=str(tmp_path))+ await save_health_metrics(async_session, repo.id, [_metric("README.md", "README")])++ assert await backfill_module_attribution(async_session, repo.id, ROOTS) == 1+ assert (await _modules(async_session, repo.id))["README.md"] is None+++async def test_it_clears_the_community_labels_left_by_old_rows(async_session, tmp_path) -> None:+ """The namespace this replaced, including its dedupe suffix.++ ``module`` used to be ``community_label or top_level_dir``, so a row could+ name a directory the file does not live in — measured at 1,355 of 3,263+ files on this repo — and could carry a ``" (N)"`` disambiguation suffix.+ Those rows only correct themselves if something rewrites them.+ """+ repo = await upsert_repository(async_session, name="repo", local_path=str(tmp_path))+ await save_health_metrics(+ async_session,+ repo.id,+ [+ _metric("services/api/main.go", "tests/unit"),+ _metric("libs/billing/src/Main.java", "repowise/ingestion (40)"),+ ],+ )++ assert await backfill_module_attribution(async_session, repo.id, ROOTS) == 2+ assert await _modules(async_session, repo.id) == {+ "services/api/main.go": "services/api",+ "libs/billing/src/Main.java": "libs/billing",+ }+++async def test_no_package_roots_leaves_a_correct_repo_alone(async_session, tmp_path) -> None:+ """The two sibling repos in this workspace: no nested manifests, no change.++ Censused on the real indexes of a FastAPI backend (674 rows) and a Next.js+ frontend (1,058 rows): zero rows move. A backfill that rewrote them would+ be a regression, not a fix.+ """+ repo = await upsert_repository(async_session, name="repo", local_path=str(tmp_path))+ await save_health_metrics(+ async_session,+ repo.id,+ [+ _metric("app/routers/files.py", "app"),+ _metric("src/components/x.tsx", "src"),+ _metric("tests/unit/a.py", "tests"),+ ],+ )++ assert await backfill_module_attribution(async_session, repo.id, set()) == 0