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(change-health): match findings by identity across renames, line shifts and centrality (#3715)
Swati Ahuja755c1bef62bf
diff --git a/packages/core/src/repowise/core/analysis/change_health/analyzer.py b/packages/core/src/repowise/core/analysis/change_health/analyzer.pyindex fd7c5a7e8..460dc1ad9 100644--- a/packages/core/src/repowise/core/analysis/change_health/analyzer.py+++ b/packages/core/src/repowise/core/analysis/change_health/analyzer.py@@ -61,6 +61,8 @@ class RevisionAnalysis:findings: list[HealthFindingData] = field(default_factory=list)analyzed: set[str] = field(default_factory=set)skipped: dict[str, str] = field(default_factory=dict)+ #: Dependents count per file, as this side's centrality gates read it.+ dependents: dict[str, int] = field(default_factory=dict)def findings_for(self, paths: set[str]) -> list[HealthFindingData]:return [f for f in self.findings if f.file_path in paths]@@ -73,12 +75,24 @@ class RevisionHealthAnalyzer:self.repo_root = repo_rootself.config = config- def analyze(self, sources: dict[str, bytes], *, subject_paths: set[str]) -> RevisionAnalysis:+ def analyze(+ self,+ sources: dict[str, bytes],+ *,+ subject_paths: set[str],+ dependents: dict[str, int] | None = None,+ ) -> RevisionAnalysis:"""Analyze *sources*, reporting findings for the whole supplied set.*subject_paths* names the files the caller actually cares about; theremainder are closure context that shapes resolution without beingcharged to the change.++ *dependents* is the other side's :attr:`RevisionAnalysis.dependents`.+ Both sides see only the changed files, so a file's dependents count+ here is a slice of its real one, and an import added between two+ changed files can carry it across a centrality gate. Scoring both+ sides against one count keeps that from reading as a code change."""result = RevisionAnalysis()parsed, usable = self._parse(sources, result)@@ -89,10 +103,12 @@ class RevisionHealthAnalyzer:parsed_files=parsed,repo_root=self.repo_root,source_reader=MappingSourceReader(usable),+ file_dependents=dependents,)report = analyzer.analyze(config=self.config, changed_files=set(usable))result.findings = list(report.findings)result.analyzed = set(usable)+ result.dependents = analyzer.file_dependents()return result# -- internals ----------------------------------------------------------diff --git a/packages/core/src/repowise/core/analysis/change_health/identity.py b/packages/core/src/repowise/core/analysis/change_health/identity.pyindex 1347d9c3a..40b928a03 100644--- a/packages/core/src/repowise/core/analysis/change_health/identity.py+++ b/packages/core/src/repowise/core/analysis/change_health/identity.py@@ -62,9 +62,21 @@ def normalize_path(path: str, rename_map: dict[str, str]) -> str:return rename_map.get(path, path)-def line_distance(left: HealthFindingData, right: HealthFindingData) -> int:- """Proximity tie-breaker for two findings that share an identity."""- a, b = left.line_start, right.line_start- if a is None or b is None:- return 0- return abs(a - b)+def measured_shape(finding: HealthFindingData, *, path: str | None = None) -> tuple[Any, ...]:+ """What *finding* measured, with its owning symbol's name left out.++ Two findings with one shape on a renamed symbol are the same finding: the+ reason carries the measured value (a CCN, a length, a depth) and the span+ its extent, so a rename that also changes the body has a different shape.+ """+ reason = finding.reason or ""+ if finding.function_name:+ reason = reason.replace(finding.function_name, "{symbol}")+ span = (finding.line_end or 0) - (finding.line_start or 0)+ return (+ str(finding.dimension),+ str(finding.biomarker_type),+ path or finding.file_path,+ reason,+ span,+ )diff --git a/packages/core/src/repowise/core/analysis/change_health/matcher.py b/packages/core/src/repowise/core/analysis/change_health/matcher.pyindex 7088084b2..a43dfac84 100644--- a/packages/core/src/repowise/core/analysis/change_health/matcher.py+++ b/packages/core/src/repowise/core/analysis/change_health/matcher.py@@ -2,11 +2,13 @@from __future__ import annotations+from collections.abc import Iterablefrom dataclasses import dataclass, field+from ..changed_lines import map_old_linefrom ..health import HealthFindingDatafrom ..health.aggregation import finding_raw_deduction-from .identity import finding_key, line_distance, normalize_path, severity_rank+from .identity import finding_key, measured_shape, normalize_path, severity_rankfrom .models import ChangeKind, FindingKey#: Health-impact movement below this is rounding, not a regression.@@ -47,11 +49,22 @@ class FindingMatcher:Pairing is per identity group: findings sharing a key are zipped by lineproximity so a marker that fires twice in one symbol keeps a stablecorrespondence, and a same-marker replacement reads as one unchanged- finding rather than one introduced plus one resolved.+ finding rather than one introduced plus one resolved. Proximity reads a+ base line where the diff moved it (*hunks*, by head path), so lines+ inserted above a symbol do not cross-pair two findings inside it.++ A second pass pairs what is left over across a symbol rename: a head+ finding with no peer takes a resolved base finding of the same measured+ shape in the same file."""- def __init__(self, rename_map: dict[str, str] | None = None) -> None:+ def __init__(+ self,+ rename_map: dict[str, str] | None = None,+ hunks: dict[str, list[tuple[int, int, int, int]]] | None = None,+ ) -> None:self.rename_map = rename_map or {}+ self.hunks = hunks or {}def match(self, base: list[HealthFindingData], head: list[HealthFindingData]) -> MatchResult:base_groups = self._group(base, normalize=True)@@ -65,10 +78,46 @@ class FindingMatcher:result.resolved.extend(unmatched)for leftovers in base_groups.values():result.resolved.extend(leftovers)+ self._pair_renames(result)return result# -- internals ----------------------------------------------------------+ def _pair_renames(self, result: MatchResult) -> None:+ """Pair introduced head findings with resolved base findings of one shape.++ Only symbol-owned findings whose symbol name differs: the exact pass+ already settled every same-name pair, so what remains with an equal+ shape is the same finding under a new name.+ """+ introduced = [m for m in result.matched if m.base is None and m.head.function_name]+ base = [f for f in result.resolved if f.function_name]+ if not introduced or not base:+ return+ shapes = [measured_shape(f, path=self._base_path(f)) for f in base]+ peers = _closest_first(+ (self._distance(b, m.head), mi, bi)+ for mi, m in enumerate(introduced)+ for bi, b in enumerate(base)+ if shapes[bi] == measured_shape(m.head) and b.function_name != m.head.function_name+ )+ for mi, bi in peers.items():+ m = introduced[mi]+ m.base, m.kind = base[bi], _classify(base[bi], m.head)+ claimed = {id(base[bi]) for bi in peers.values()}+ result.resolved = [f for f in result.resolved if id(f) not in claimed]++ def _base_path(self, finding: HealthFindingData) -> str:+ return normalize_path(finding.file_path, self.rename_map)++ def _distance(self, base: HealthFindingData, head: HealthFindingData) -> int:+ """Line distance after moving *base* to where the diff put its line."""+ a, b = base.line_start, head.line_start+ if a is None or b is None:+ return 0+ hunks = self.hunks.get(self._base_path(base))+ return abs((map_old_line(hunks, a) if hunks else a) - b)+def _group(self, findings: list[HealthFindingData], *, normalize: bool) -> dict[FindingKey, list[HealthFindingData]]:@@ -94,25 +143,32 @@ class FindingMatcher:unrelated new finding absorb a moved one, which both hides the newfinding and reports the moved one as introduced."""- candidates = sorted(- (line_distance(b, h), hi, bi)- for hi, h in enumerate(head)- for bi, b in enumerate(base)+ peers = _closest_first(+ (self._distance(b, h), hi, bi) for hi, h in enumerate(head) for bi, b in enumerate(base))- peers: dict[int, HealthFindingData] = {}- taken: set[int] = set()- for _distance, head_index, base_index in candidates:- if head_index in peers or base_index in taken:- continue- peers[head_index] = base[base_index]- taken.add(base_index)- matched = [- MatchedFinding(item, peers.get(ordinal), key, ordinal, _classify(peers.get(ordinal), item))- for ordinal, item in enumerate(head)- ]+ matched = []+ for ordinal, item in enumerate(head):+ peer = base[peers[ordinal]] if ordinal in peers else None+ matched.append(MatchedFinding(item, peer, key, ordinal, _classify(peer, item)))+ taken = set(peers.values())return matched, [b for i, b in enumerate(base) if i not in taken]+def _closest_first(candidates: Iterable[tuple[int, int, int]]) -> dict[int, int]:+ """``{head_index: base_index}`` from ``(distance, head, base)`` candidates.++ Each side is used once, nearest pairs first.+ """+ peers: dict[int, int] = {}+ taken: set[int] = set()+ for _distance, head_index, base_index in sorted(candidates):+ if head_index in peers or base_index in taken:+ continue+ peers[head_index] = base_index+ taken.add(base_index)+ return peers++def _classify(base: HealthFindingData | None, head: HealthFindingData) -> ChangeKind:"""Introduced when nothing matched; worsened only on a real regression."""if base is None:diff --git a/packages/core/src/repowise/core/analysis/change_health/service.py b/packages/core/src/repowise/core/analysis/change_health/service.pyindex a1a80337c..a1af78ff8 100644--- a/packages/core/src/repowise/core/analysis/change_health/service.py+++ b/packages/core/src/repowise/core/analysis/change_health/service.py@@ -19,7 +19,7 @@ from ..finding_registry import excluded_types, gate_for, is_served, language_labfrom ..health import HEALTH_ANALYZER_VERSION, HealthFindingDatafrom ..health.perf.causal import PERFORMANCE_MODEL_VERSIONfrom ..health.scoring import ADVISORY_DIMENSION, is_advisory-from .analyzer import RevisionHealthAnalyzer, language_for, language_of+from .analyzer import RevisionAnalysis, RevisionHealthAnalyzer, language_for, language_offrom .attribution import FindingAttributor, changed_symbols_forfrom .identity import change_finding_id, finding_key, severity_rankfrom .matcher import FindingMatcher, MatchedFinding@@ -260,8 +260,11 @@ class ChangeHealthDeltaService:)base_sources = self.source.read(pair.base_sha, base_paths)+ rename = pair.rename_map()head_run = self.analyzer.analyze(head_sources, subject_paths=set(head_paths))- base_run = self.analyzer.analyze(base_sources, subject_paths=set(base_paths))+ base_run = self.analyzer.analyze(+ base_sources, subject_paths=set(base_paths), dependents=_on_base(head_run, rename)+ )skipped.update(head_run.skipped)# A file whose BASE side failed has no baseline, so every finding at its@@ -282,8 +285,7 @@ class ChangeHealthDeltaService:1 for r in skipped.values() if r.removeprefix("base_") in {"parse_failed", "unreadable"})- rename = pair.rename_map()- matcher = FindingMatcher(rename)+ matcher = FindingMatcher(rename, _hunks(eligible))# Both sides are restricted to the same comparable set, so an excluded# file cannot contribute a one-sided "introduced" or "resolved".# Advisory markers deduct nothing, so adding one is not a regression@@ -445,6 +447,21 @@ def _limits(head_findings: list[HealthFindingData]) -> list[str]:return limits+def _on_base(head_run: RevisionAnalysis, rename: dict[str, str]) -> dict[str, int]:+ """Head's dependents counts under base-side paths, to score base against.++ See :meth:`RevisionHealthAnalyzer.analyze`: one centrality context for both+ sides, so only the code can move a centrality-gated finding.+ """+ to_base = {head: base for base, head in rename.items()}+ return {to_base.get(path, path): count for path, count in head_run.dependents.items()}+++def _hunks(changes: list[FileChange]) -> dict[str, list[tuple[int, int, int, int]]]:+ """Each changed file's diff hunks, by head path, for the matcher's line map."""+ return {c.head_path: c.diff.hunks for c in changes if c.head_path and c.diff}++def _comparable(finding: HealthFindingData, withheld: frozenset[str]) -> bool:"""A finding both sides of a comparison count: not advisory, not a test'sperformance finding, and one the registry serves."""diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.pyindex 6502a40df..c678441ea 100644--- a/packages/core/src/repowise/core/analysis/health/engine.py+++ b/packages/core/src/repowise/core/analysis/health/engine.py@@ -664,7 +664,7 @@ def _dependents_count(graph: Any, path: str) -> int:return len(set(file_dependency_neighbors(graph, path, incoming=True, uses_only=True)))-def _compute_repo_dependents_p80(parsed_files: list[Any], graph: Any) -> int | None:+def _compute_repo_dependents_p80(dependents: dict[str, int]) -> int | None:"""Repo-wide 80th percentile of file-level in-degree (dependents).Restricted to files that actually have ≥1 dependent — this is the@@ -676,17 +676,7 @@ def _compute_repo_dependents_p80(parsed_files: list[Any], graph: Any) -> int | Nsparse-graph languages (TS/Rust) instead of assuming Python's denserimport graph."""- if graph is None:- return None- counts: list[int] = []- for pf in parsed_files:- path = pf.file_info.path- if path not in graph:- continue- deg = _dependents_count(graph, path)- if deg > 0:- counts.append(deg)- return _percentile_p80(counts)+ return _percentile_p80([deg for deg in dependents.values() if deg > 0])def _compute_repo_active_contributors(git_meta_map: dict[str, dict]) -> int | None:@@ -798,6 +788,7 @@ class HealthAnalyzer:repo_root: Any | None = None,source_reader: SourceReader | None = None,stored_blame_findings: dict[str, list[Any]] | None = None,+ file_dependents: dict[str, int] | None = None,) -> None:self.graph = graphself.git_meta_map = git_meta_map or {}@@ -845,10 +836,34 @@ class HealthAnalyzer:# without a blame index (a re-score from stored git metadata) so the# pass does not delete what it cannot recompute.self.stored_blame_findings = stored_blame_findings or {}+ # Dependents counts to gate centrality on instead of this graph's. A+ # revision comparison passes the other side's, so a file crossing a+ # centrality gate inside a partial graph is not read as a code change.+ self._file_dependents_override = file_dependentsself._package_roots_cache: set[str] | None = Noneself._tests_reach_cache: set[str] | None = Noneself._execution_graph_cache: CallGraphIndex | None = None+ def file_dependents(self) -> dict[str, int]:+ """Each analysed file's dependents count, as the centrality gates read it."""+ if self._file_dependents_override is not None:+ return dict(self._file_dependents_override)+ if self.graph is None:+ return {}+ return {+ pf.file_info.path: _dependents_count(self.graph, pf.file_info.path)+ for pf in self.parsed_files+ if pf.file_info.path in self.graph+ }++ def _dependents(self, path: str) -> int:+ override = self._file_dependents_override+ if override is not None and path in override:+ return override[path]+ if self.graph is None or path not in self.graph:+ return 0+ return _dependents_count(self.graph, path)+def _execution_graph(self) -> CallGraphIndex | None:"""Build the reliable execution index at most once per analysis."""if self.graph is not None and self._execution_graph_cache is None:@@ -1205,7 +1220,7 @@ class HealthAnalyzer:graph_view=ImportEdgeView(self.graph) if self.graph is not None else None,dup_report=dup_report,function_mod_p80=fn_mod_p80,- dependents_p80=_compute_repo_dependents_p80(self.parsed_files, self.graph),+ dependents_p80=_compute_repo_dependents_p80(self.file_dependents()),active_contributors=_compute_repo_active_contributors(self.git_meta_map),# Repo-wide SCC index (import cycles), computed once so Break# Cycle never recomputes it per file.@@ -1299,7 +1314,7 @@ class HealthAnalyzer:path = s.file_pathif path in out or path not in self.graph:continue- out[path] = float(_dependents_count(self.graph, path))+ out[path] = float(self._dependents(path))return outdef _apply_cross_file_oracles(self, walked: list[tuple[Any, FileComplexity]]) -> None:@@ -1628,9 +1643,7 @@ class HealthAnalyzer:max_nesting = max((fc.max_nesting for fc in fc_list), default=0)nloc = fcx.file_nloc- dependents_count = 0- if self.graph is not None:- dependents_count = _dependents_count(self.graph, file_path)+ dependents_count = self._dependents(file_path)cov = self.coverage_map.get(file_path)if cov is None:diff --git a/tests/unit/change_health/test_comparison.py b/tests/unit/change_health/test_comparison.pyindex c706f2250..7064febdb 100644--- a/tests/unit/change_health/test_comparison.py+++ b/tests/unit/change_health/test_comparison.py@@ -139,6 +139,65 @@ def test_replacing_one_marker_hit_with_another_is_not_a_regression(make_repo):assert delta.resolved_total == 0+# -- symbol identity --------------------------------------------------------++[email protected]("language", sorted(LANGUAGES))+def test_renaming_a_function_carries_its_finding_across(make_repo, language):+ path, build = LANGUAGES[language]+ repo = make_repo()+ repo.commit("seed", {path: build("tangle", 14)})+ repo.commit("rename", {path: build("knot", 14)})++ delta = compare(repo)++ assert delta.introduced_total == 0+ assert delta.resolved_total == 0+ assert delta.unchanged_total >= 1+++def test_a_rename_that_also_grows_the_body_is_still_introduced(make_repo):+ repo = make_repo()+ repo.commit("seed", {"app.py": python_complex("tangle", 14)})+ repo.commit("rename", {"app.py": python_complex("knot", 15)})++ delta = compare(repo)++ assert any(f.symbol == "knot" and f.change_kind == "introduced" for f in delta.findings)+ assert delta.resolved_total >= 1+++def test_a_copy_beside_the_unchanged_original_is_introduced(make_repo):+ """Same shape as an existing finding, but the original is still there."""+ repo = make_repo()+ repo.commit("seed", {"app.py": python_complex("tangle", 14)})+ repo.commit("copy", {"app.py": python_complex("tangle", 14) + python_complex("knot", 14)})++ delta = compare(repo)++ assert any(f.symbol == "knot" and f.change_kind == "introduced" for f in delta.findings)+ assert not any(f.symbol == "tangle" for f in delta.findings)+ assert delta.resolved_total == 0+++def test_new_importers_of_a_changed_file_do_not_introduce_its_old_findings(make_repo):+ """Only the changed files are parsed, so a file's dependents are a slice.++ Adding importers inside the change lifts that slice over a centrality gate+ (Brain Method) on head only; the function itself did not change.+ """+ repo = make_repo()+ core = python_complex("tangle", 40)+ repo.commit("seed", {"core.py": core})+ importers = {f"use_{i}.py": "from core import tangle\n\nprint(tangle(1))\n" for i in range(4)}+ repo.commit("use", {"core.py": core + "\n# touched\n", **importers})++ delta = compare(repo)++ assert not any(f.symbol == "tangle" for f in delta.findings)+ assert delta.unchanged_total >= 1++# -- attribution ------------------------------------------------------------diff --git a/tests/unit/change_health/test_regressions.py b/tests/unit/change_health/test_regressions.pyindex 11b7f8a99..c5957ce75 100644--- a/tests/unit/change_health/test_regressions.py+++ b/tests/unit/change_health/test_regressions.py@@ -80,6 +80,24 @@ def test_an_unrelated_new_finding_does_not_absorb_a_moved_one():assert by_line[50].base is None+def test_lines_inserted_above_a_symbol_do_not_cross_pair_its_findings():+ """Raw proximity paired the moved high finding with its medium neighbour."""+ base = [+ finding(line=388, severity=Severity.HIGH, symbol="walk"),+ finding(line=407, severity=Severity.MEDIUM, symbol="walk"),+ ]+ head = [+ finding(line=422, severity=Severity.HIGH, symbol="walk"),+ finding(line=441, severity=Severity.MEDIUM, symbol="walk"),+ ]+ inserted_34_lines_at_100 = [(100, 0, 101, 34)]++ result = FindingMatcher(hunks={"app.py": inserted_34_lines_at_100}).match(base, head)++ assert [m.kind for m in result.matched] == ["unchanged", "unchanged"]+ assert [m.base.line_start for m in result.matched] == [388, 407]++def test_a_base_finding_left_over_inside_a_matched_group_is_resolved():base = [finding(line=10), finding(line=200)]head = [finding(line=12)]