Skip to content

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

fix(refactoring): give every health writer the community label map (#2830)

Raghav Chamadiya0439db6452fd
diff --git a/packages/cli/src/repowise/cli/commands/health_cmd/command.py b/packages/cli/src/repowise/cli/commands/health_cmd/command.py
index 968c4e6b8..3e18607cb 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd/command.py
@@ -187,6 +187,7 @@ def health_command(
from pathlib import Path as PathlibPath
+ from repowise.core.analysis.communities import file_community_labels
from repowise.core.analysis.health import HealthAnalyzer
from repowise.core.ingestion import ASTParser, FileTraverser, GraphBuilder
@@ -317,6 +318,7 @@ def health_command(
graph_builder.graph(),
git_meta_map=git_meta_map,
parsed_files=parsed_files,
+ community_label_map=file_community_labels(graph_builder),
coverage_map=coverage_map,
duplication_cache_dir=Path(repo_path) / ".repowise",
repo_root=repo_path,
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
index 454fb7bc9..2b94e6a22 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
@@ -1403,6 +1403,7 @@ async def _rescore_health_from_db(
from sqlalchemy import delete, select
from repowise.cli.helpers import get_db_url_for_repo
+ from repowise.core.analysis.communities import file_community_labels
from repowise.core.analysis.health import HealthAnalyzer
from repowise.core.analysis.health.config import HealthConfig
from repowise.core.analysis.health.history_refresh import (
@@ -1480,6 +1481,7 @@ async def _rescore_health_from_db(
graph_builder.graph(),
git_meta_map=git_meta_map,
parsed_files=parsed_files,
+ community_label_map=file_community_labels(graph_builder),
coverage_map=coverage.coverage_map,
duplication_cache_dir=Path(repo_path) / ".repowise",
repo_root=repo_path,
diff --git a/packages/core/src/repowise/core/analysis/communities.py b/packages/core/src/repowise/core/analysis/communities.py
index 353555af2..d90d4d78e 100644
--- a/packages/core/src/repowise/core/analysis/communities.py
+++ b/packages/core/src/repowise/core/analysis/communities.py
@@ -16,6 +16,7 @@ import sys
from collections import Counter
from dataclasses import dataclass
from pathlib import PurePosixPath
+from typing import Any
import networkx as nx
import structlog
@@ -680,6 +681,28 @@ def detect_file_communities(
return file_assignment, communities_info, algorithm
+def file_community_labels(graph_builder: Any) -> dict[str, str]:
+ """``{file path: community label}`` off *graph_builder*'s cached file communities.
+
+ The refactoring detectors key foreign modules by this label, so every path
+ that writes refactoring plans (init, update, re-score, ``repowise health``)
+ must pass the same map or the same file gets a different plan. Empty when
+ community detection fails.
+ """
+ try:
+ assignment = graph_builder.community_detection()
+ info = graph_builder.community_info()
+ except Exception as exc:
+ log.debug("file_community_labels_failed", error=str(exc))
+ return {}
+ labels: dict[str, str] = {}
+ for node_id, community_id in assignment.items():
+ label = getattr(info.get(community_id), "label", None)
+ if label:
+ labels[node_id] = label
+ return labels
+
+
# ---------------------------------------------------------------------------
# Symbol-level community detection
# ---------------------------------------------------------------------------
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/extract_helper.py b/packages/core/src/repowise/core/analysis/health/refactoring/extract_helper.py
index ba6eaa6eb..2360d53dd 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/extract_helper.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/extract_helper.py
@@ -537,9 +537,9 @@ class ExtractHelperDetector(RefactoringDetector):
in** — ``module: "ui"`` for a block shared by ``packages/api-client``,
``packages/types`` and ``packages/ui``. Acting on it files shared code
into a package two thirds of its callers are not in.
- - **The namespace depended on the writer.** ``community_label_map`` is populated
+ - **The namespace depended on the writer.** ``community_label_map`` was populated
only by the full-index path; the incremental, re-score and
- ``repowise health`` paths leave it empty, so the same clone got
+ ``repowise health`` paths left it empty, so the same clone got
``"ui"`` or ``None`` depending on which pass last wrote the row. That
is exactly the defect the ``module`` column was fixed for; the
refactoring payload had kept it.
diff --git a/packages/core/src/repowise/core/pipeline/incremental.py b/packages/core/src/repowise/core/pipeline/incremental.py
index b89ac4496..dd683cc50 100644
--- a/packages/core/src/repowise/core/pipeline/incremental.py
+++ b/packages/core/src/repowise/core/pipeline/incremental.py
@@ -711,6 +711,7 @@ def run_partial_analysis(
# execution closure so a changed caller can still see an unchanged sink
# and an unchanged caller can react to a changed sink. The index is
# built once; this is a multi-source walk, not one walk per finding.
+ from repowise.core.analysis.communities import file_community_labels
from repowise.core.analysis.execution_graph import ExecutionGraphIndex
from repowise.core.analysis.health import HealthAnalyzer
from repowise.core.analysis.health.config import HealthConfig
@@ -731,6 +732,7 @@ def run_partial_analysis(
graph_builder.graph(),
git_meta_map=git_meta_map,
parsed_files=parsed_files,
+ community_label_map=file_community_labels(graph_builder),
duplication_cache_dir=Path(repo_path) / ".repowise",
repo_root=repo_path,
coverage_map=coverage_map,
diff --git a/packages/core/src/repowise/core/pipeline/phases/analysis.py b/packages/core/src/repowise/core/pipeline/phases/analysis.py
index 8e1edab24..5f59dca1f 100644
--- a/packages/core/src/repowise/core/pipeline/phases/analysis.py
+++ b/packages/core/src/repowise/core/pipeline/phases/analysis.py
@@ -261,6 +261,7 @@ async def _run_health_analysis(
) -> Any | None:
"""Run code-health analysis (complexity + biomarkers + scoring)."""
try:
+ from repowise.core.analysis.communities import file_community_labels
from repowise.core.analysis.health import HealthAnalyzer
from repowise.core.analysis.health.config import HealthConfig
@@ -271,21 +272,6 @@ async def _run_health_analysis(
# the entire pre-walk — most of the phase's wall-clock.
progress.on_phase_start("health", 2 * len(parsed_files))
- # Build a {file_path → community label} map for the refactoring
- # detectors. Community detection is already computed for the graph
- # view, so this is essentially free. It is not the ``module`` column:
- # that is a path, written from the package boundaries.
- community_label_map: dict[str, str] = {}
- try:
- cd = graph_builder.community_detection()
- ci = graph_builder.community_info()
- for node_id, comm_id in cd.items():
- info = ci.get(comm_id)
- label = getattr(info, "label", None) if info else None
- if label:
- community_label_map[node_id] = label
- except Exception as exc:
- logger.debug("health_community_label_map_failed", error=str(exc))
# Ingest coverage (auto-discovered or explicitly passed) so biomarkers
# see real line/branch coverage instead of the has_test_file fallback.
@@ -307,7 +293,7 @@ async def _run_health_analysis(
graph_builder.graph(),
git_meta_map=git_meta_map,
parsed_files=parsed_files,
- community_label_map=community_label_map,
+ community_label_map=file_community_labels(graph_builder),
coverage_map=coverage_map,
duplication_cache_dir=(repo_path / ".repowise") if repo_path is not None else None,
repo_root=repo_path,
diff --git a/tests/integration/test_split_file_path_parity.py b/tests/integration/test_split_file_path_parity.py
new file mode 100644
index 000000000..2e9967abb
--- /dev/null
+++ b/tests/integration/test_split_file_path_parity.py
@@ -0,0 +1,172 @@
+"""Split File builds the same weighted graph on a full index and on an update.
+
+The shared-foreign-module fallback keys each foreign callee by its graph
+community label. A path that hands the analyzer no labels keys by raw file path
+instead, so the same file on the same commit gets a sparser graph, a different
+partition and a different plan id depending on whether ``init`` or ``update``
+wrote it.
+"""
+
+from __future__ import annotations
+
+import os
+import sqlite3
+import subprocess
+from contextlib import closing
+from pathlib import Path
+
+import pytest
+from click.testing import CliRunner
+
+from repowise.cli.helpers import release_update_lock
+from repowise.cli.main import cli
+from repowise.core.analysis.change_health.analyzer import RevisionHealthAnalyzer
+from repowise.core.analysis.health.refactoring import split_file
+
+_GROUPS = ("ledger", "mailer")
+_PER_GROUP = 5
+
+
+def _git(repo: Path, *args: str) -> None:
+ env = {
+ **os.environ,
+ "GIT_AUTHOR_NAME": "Split Parity",
+ "GIT_AUTHOR_EMAIL": "[email protected]",
+ "GIT_COMMITTER_NAME": "Split Parity",
+ "GIT_COMMITTER_EMAIL": "[email protected]",
+ }
+ subprocess.run(["git", *args], cwd=repo, check=True, capture_output=True, env=env)
+
+
+def _helper_module(group: str, i: int) -> str:
+ # Chain each package's modules by import so they land in one community.
+ head = f"from {group}_pkg.m{i - 1} import step_{i - 1}\n\n\n" if i else ""
+ body = f" return step_{i - 1}(x) + {i}\n" if i else " return x\n"
+ return f"{head}def step_{i}(x):\n{body}"
+
+
+def _package_init() -> str:
+ return "".join(f"from .m{i} import step_{i}\n" for i in range(_PER_GROUP))
+
+
+def _big_module() -> str:
+ # Importing through the package re-export puts the ``imports`` edge on
+ # ``__init__.py`` while each call lands in a submodule, so no symbol has an
+ # imported-name surface toward its callee's file and the detector falls
+ # back to the shared-foreign-module signal.
+ out: list[str] = []
+ for group in _GROUPS:
+ aliases = ", ".join(f"step_{i} as {group}_step_{i}" for i in range(_PER_GROUP))
+ out.append(f"from {group}_pkg import {aliases}")
+ out.extend(["", ""])
+ for group in _GROUPS:
+ for i in range(_PER_GROUP):
+ out.append(f"def {group}_task_{i}(x):")
+ out.append(f" y = {group}_step_{i}(x)")
+ out.extend(f" y = y * {k} + {i}" for k in range(1, 30))
+ out.append(" return y")
+ out.append("")
+ out.append("")
+ return "\n".join(out)
+
+
+def _make_repo(path: Path) -> None:
+ path.mkdir(parents=True)
+ for group in _GROUPS:
+ pkg = path / f"{group}_pkg"
+ pkg.mkdir()
+ (pkg / "__init__.py").write_text(_package_init(), encoding="utf-8")
+ for i in range(_PER_GROUP):
+ (pkg / f"m{i}.py").write_text(_helper_module(group, i), encoding="utf-8")
+ (path / "big.py").write_text(_big_module(), encoding="utf-8")
+ _git(path, "init", "-q")
+ _git(path, "add", "big.py", *(f"{group}_pkg" for group in _GROUPS))
+ _git(path, "commit", "-q", "-m", "initial")
+
+
+def _invoke(runner: CliRunner, repo: Path, *args: str) -> None:
+ try:
+ result = runner.invoke(cli, [*args, str(repo)], catch_exceptions=False)
+ assert result.exit_code == 0, result.output
+ finally:
+ release_update_lock(repo)
+
+
+def _split_plans(repo: Path) -> list[tuple[str, str]]:
+ with closing(sqlite3.connect(repo / ".repowise" / "wiki.db")) as connection:
+ return connection.execute(
+ "SELECT file_path, public_id FROM refactoring_suggestions "
+ "WHERE refactoring_type = 'split_file' AND status = 'open' ORDER BY file_path"
+ ).fetchall()
+
+
+def test_split_file_graph_and_plan_match_across_init_and_update(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ graphs: list[tuple[tuple[str, str, float], ...]] = []
+ original = split_file._weighted_graph
+ original_revision = RevisionHealthAnalyzer.analyze
+ in_revision = [False]
+
+ def _recording(*args: object, **kwargs: object):
+ wg, counts = original(*args, **kwargs)
+ # Revision deltas analyze a changed-files-only graph and store no plans.
+ if wg is not None and not in_revision[0]:
+ edges = sorted((a, b, round(d["weight"], 6)) for a, b, d in wg.edges(data=True))
+ graphs.append(tuple(edges))
+ return wg, counts
+
+ def _revision(self: RevisionHealthAnalyzer, *args: object, **kwargs: object):
+ in_revision[0] = True
+ try:
+ return original_revision(self, *args, **kwargs)
+ finally:
+ in_revision[0] = False
+
+ monkeypatch.setattr(split_file, "_weighted_graph", _recording)
+ monkeypatch.setattr(RevisionHealthAnalyzer, "analyze", _revision)
+ init_args = (
+ "init",
+ "--no-prose",
+ "--embedder",
+ "mock",
+ "--no-editor-setup",
+ "--no-hook",
+ "--no-onboarding",
+ "--no-seed",
+ "--no-agents",
+ "--no-codex",
+ "--no-claude-md",
+ "--no-cost-tracking",
+ "--no-workspace",
+ "-y",
+ )
+ runner = CliRunner()
+
+ incremental = tmp_path / "incremental" / "repo"
+ _make_repo(incremental)
+ monkeypatch.setenv(
+ "REPOWISE_DB_URL", f"sqlite+aiosqlite:///{incremental / '.repowise' / 'wiki.db'}"
+ )
+ _invoke(runner, incremental, *init_args)
+ with (incremental / "big.py").open("a", encoding="utf-8") as source:
+ source.write("\n# harmless incremental edit\n")
+ _git(incremental, "add", "big.py")
+ _git(incremental, "commit", "-q", "-m", "harmless edit")
+ graphs.clear()
+ _invoke(runner, incremental, "update", "--no-workspace", "--no-agents")
+ update_graphs = list(graphs)
+
+ full = tmp_path / "full" / "repo"
+ subprocess.run(["git", "clone", "-q", str(incremental), str(full)], check=True)
+ monkeypatch.setenv("REPOWISE_DB_URL", f"sqlite+aiosqlite:///{full / '.repowise' / 'wiki.db'}")
+ graphs.clear()
+ _invoke(runner, full, *init_args)
+ full_graphs = list(graphs)
+
+ assert update_graphs, "the update never rebuilt big.py's split graph"
+ assert full_graphs
+ assert set(update_graphs) == set(full_graphs)
+ full_plans = _split_plans(full)
+ assert full_plans
+ assert _split_plans(incremental) == full_plans