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
feat(change-risk): pair findings across moved code and count improved findings (#4112)
Raghav Chamadiyaf378acb4fe71
diff --git a/packages/core/src/repowise/core/analysis/change_health/analyzer.py b/packages/core/src/repowise/core/analysis/change_health/analyzer.pyindex 460dc1ad9..c1fa77797 100644--- a/packages/core/src/repowise/core/analysis/change_health/analyzer.py+++ b/packages/core/src/repowise/core/analysis/change_health/analyzer.py@@ -12,7 +12,13 @@ from datetime import UTC, datetimefrom pathlib import PurePosixPathfrom ...ingestion.graph.builder import GraphBuilder-from ...ingestion.models import EXTENSION_TO_LANGUAGE, SPECIAL_FILENAMES, FileInfo, ParsedFile+from ...ingestion.models import (+ EXTENSION_TO_LANGUAGE,+ SPECIAL_FILENAMES,+ FileInfo,+ ParsedFile,+ Symbol,+)from ...ingestion.parser import parse_filefrom ...test_paths import is_test_related_pathfrom ..health import HealthAnalyzer, HealthFindingData@@ -63,6 +69,7 @@ class RevisionAnalysis: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)+ symbols: dict[str, list[Symbol]] = 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]@@ -109,6 +116,7 @@ class RevisionHealthAnalyzer:result.findings = list(report.findings)result.analyzed = set(usable)result.dependents = analyzer.file_dependents()+ result.symbols = {pf.file_info.path: pf.symbols for pf in parsed}return result# -- internals ----------------------------------------------------------diff --git a/packages/core/src/repowise/core/analysis/change_health/attribution.py b/packages/core/src/repowise/core/analysis/change_health/attribution.pyindex 076b2ad97..3c2d30e46 100644--- a/packages/core/src/repowise/core/analysis/change_health/attribution.py+++ b/packages/core/src/repowise/core/analysis/change_health/attribution.py@@ -11,6 +11,7 @@ from dataclasses import dataclassfrom ..health import HealthFindingDatafrom .models import ATTRIBUTION_CONFIDENCE, AttributionBasis+from .moves import SymbolKeyfrom .sources import FileChange@@ -47,7 +48,15 @@ class FindingAttributor:*,changed_symbols: set[tuple[str, str]] | None = None,changed_call_edge: str | None = None,+ moved_from: SymbolKey | None = None,) -> Attribution:+ if moved_from is not None:+ # Ahead of new_file: a split's new file holds code the change only carried.+ return Attribution(+ "moved",+ ATTRIBUTION_CONFIDENCE["moved"],+ f"Moved here from {moved_from[0]} ({moved_from[1]}).",+ )change = self.changes.get(finding.file_path)if change is None:return _UNKNOWNdiff --git a/packages/core/src/repowise/core/analysis/change_health/matcher.py b/packages/core/src/repowise/core/analysis/change_health/matcher.pyindex a43dfac84..7ecac2ac7 100644--- a/packages/core/src/repowise/core/analysis/change_health/matcher.py+++ b/packages/core/src/repowise/core/analysis/change_health/matcher.py@@ -10,10 +10,14 @@ from ..health import HealthFindingDatafrom ..health.aggregation import finding_raw_deductionfrom .identity import finding_key, measured_shape, normalize_path, severity_rankfrom .models import ChangeKind, FindingKey+from .moves import MoveMap, SymbolKey#: Health-impact movement below this is rounding, not a regression._IMPACT_EPSILON = 0.01+#: Measured values a finding's details may carry, compared for "improved".+_METRICS = ("ccn", "nloc", "max_nesting", "params")+@dataclass(slots=True)class MatchedFinding:@@ -24,6 +28,7 @@ class MatchedFinding:key: FindingKeyordinal: intkind: ChangeKind+ moved_from: SymbolKey | None = None@propertydef severity_before(self) -> str | None:@@ -39,6 +44,14 @@ class MatchResult:def unchanged_total(self) -> int:return sum(1 for m in self.matched if m.kind == "unchanged")+ @property+ def improved_total(self) -> int:+ return sum(1 for m in self.matched if m.kind == "improved")++ @property+ def moved_total(self) -> int:+ return sum(1 for m in self.matched if m.moved_from is not None)+def of_kind(self, *kinds: ChangeKind) -> list[MatchedFinding]:return [m for m in self.matched if m.kind in kinds]@@ -53,8 +66,9 @@ class FindingMatcher:base line where the diff moved it (*hunks*, by head path), so linesinserted 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+ Two more passes pair what is left over: across a move (*moves*), a head+ finding takes the resolved finding of the symbol its code came from; across+ a symbol rename, it takes a resolved base finding of the same measuredshape in the same file."""@@ -62,9 +76,11 @@ class FindingMatcher:self,rename_map: dict[str, str] | None = None,hunks: dict[str, list[tuple[int, int, int, int]]] | None = None,+ moves: MoveMap | None = None,) -> None:self.rename_map = rename_map or {}self.hunks = hunks or {}+ self.moves = moves or MoveMap()def match(self, base: list[HealthFindingData], head: list[HealthFindingData]) -> MatchResult:base_groups = self._group(base, normalize=True)@@ -78,11 +94,33 @@ class FindingMatcher:result.resolved.extend(unmatched)for leftovers in base_groups.values():result.resolved.extend(leftovers)+ self._pair_moves(result)self._pair_renames(result)return result# -- internals ----------------------------------------------------------+ def _pair_moves(self, result: MatchResult) -> None:+ """Pair introduced head findings with the resolved finding of their code's origin."""+ if not self.moves.moved_total:+ return+ candidates = []+ for mi, m in enumerate(result.matched):+ origin = self.moves.origin_of(m.head.file_path, m.head.function_name)+ if m.base is None and origin is not None:+ candidates += [+ (_shape_gap(b, m.head), mi, bi)+ for bi, b in enumerate(result.resolved)+ if _same_marker_at(b, m.head, origin)+ ]+ peers = _closest_first(candidates)+ for mi, bi in peers.items():+ m, base = result.matched[mi], result.resolved[bi]+ m.base, m.kind = base, _classify(base, m.head, carried="moved")+ m.moved_from = (base.file_path, str(base.function_name))+ claimed = {id(result.resolved[bi]) for bi in peers.values()}+ result.resolved = [f for f in result.resolved if id(f) not in claimed]+def _pair_renames(self, result: MatchResult) -> None:"""Pair introduced head findings with resolved base findings of one shape.@@ -169,14 +207,41 @@ def _closest_first(candidates: Iterable[tuple[int, int, int]]) -> dict[int, int]return peers-def _classify(base: HealthFindingData | None, head: HealthFindingData) -> ChangeKind:- """Introduced when nothing matched; worsened only on a real regression."""+def _same_marker_at(base: HealthFindingData, head: HealthFindingData, origin: SymbolKey) -> bool:+ """*base* is *head*'s marker on the symbol *origin* names."""+ return (base.file_path, base.function_name) == origin and (+ base.dimension,+ base.biomarker_type,+ ) == (head.dimension, head.biomarker_type)+++def _shape_gap(base: HealthFindingData, head: HealthFindingData) -> int:+ """0 when both measured the same, else 1; line numbers do not travel across files."""+ return 0 if measured_shape(base, path=head.file_path) == measured_shape(head) else 1+++def _classify(+ base: HealthFindingData | None, head: HealthFindingData, *, carried: ChangeKind = "unchanged"+) -> ChangeKind:+ """Introduced when unmatched, worsened or improved on a real difference, else *carried*."""if base is None:return "introduced"- if severity_rank(head.severity) > severity_rank(base.severity):- return "worsened"- if severity_rank(head.severity) < severity_rank(base.severity):- return "unchanged"- if finding_raw_deduction(head) - finding_raw_deduction(base) > _IMPACT_EPSILON:- return "worsened"- return "unchanged"+ rank = severity_rank(head.severity) - severity_rank(base.severity)+ if rank:+ return "worsened" if rank > 0 else "improved"+ impact = finding_raw_deduction(head) - finding_raw_deduction(base)+ if abs(impact) > _IMPACT_EPSILON:+ return "worsened" if impact > 0 else "improved"+ # Lower metrics within one band read as improved; higher ones stay quiet,+ # since only a deduction change is a regression worth surfacing.+ return "improved" if _metrics_dropped(base, head) else carried+++def _metrics_dropped(base: HealthFindingData, head: HealthFindingData) -> bool:+ """True when no shared metric rose and at least one fell."""+ before, after = base.details or {}, head.details or {}+ shared = [(before[k], after[k]) for k in _METRICS if k in before and k in after]+ pairs = [+ (b, a) for b, a in shared if isinstance(b, (int, float)) and isinstance(a, (int, float))+ ]+ return any(a < b for b, a in pairs) and all(a <= b for b, a in pairs)diff --git a/packages/core/src/repowise/core/analysis/change_health/models.py b/packages/core/src/repowise/core/analysis/change_health/models.pyindex e886cd812..c7e7d7559 100644--- a/packages/core/src/repowise/core/analysis/change_health/models.py+++ b/packages/core/src/repowise/core/analysis/change_health/models.py@@ -17,11 +17,15 @@ DeltaStatus = Literal["stale_baseline",]-ChangeKind = Literal["introduced", "worsened", "unchanged", "resolved", "uncertain"]+#: ``moved`` is a finding carried to another file with its code, shape unchanged.+ChangeKind = Literal[+ "introduced", "worsened", "improved", "moved", "unchanged", "resolved", "uncertain"+]#: How a head-side finding is tied to the change. Ordered most to least direct;#: ``unknown`` never claims the change caused the finding.AttributionBasis = Literal[+ "moved","added_lines","changed_symbol","changed_call_edge",@@ -32,6 +36,7 @@ AttributionBasis = Literal[]ATTRIBUTION_CONFIDENCE: dict[str, str] = {+ "moved": "high","added_lines": "high","changed_symbol": "high","new_file": "high",@@ -142,6 +147,8 @@ class ChangeFinding:opportunity_rank: int | None = None#: Canonical typed reference, only when the head finding exists in storage.health_reference: dict[str, Any] | None = None+ #: ``{"path", "symbol"}`` the finding's code was moved from, when it moved.+ moved_from: dict[str, str] | None = Noneevidence: dict[str, Any] = field(default_factory=dict)@@ -180,6 +187,10 @@ class ChangeHealthDelta:resolved_total: int = 0unchanged_total: int = 0uncertain_total: int = 0+ #: Findings paired with a lower-metric peer at the same place or across a move.+ improved_total: int = 0+ #: Findings that moved to another file with their code, whatever their shape did.+ moved_total: int = 0#: ``{path: reason}``. Any reason outside ``NO_CODE_GAP`` means the run was partial.skipped: dict[str, str] = field(default_factory=dict)timing_ms: float = 0.0diff --git a/packages/core/src/repowise/core/analysis/change_health/moves.py b/packages/core/src/repowise/core/analysis/change_health/moves.pyindex 18feef611..12c0993a0 100644--- a/packages/core/src/repowise/core/analysis/change_health/moves.py+++ b/packages/core/src/repowise/core/analysis/change_health/moves.py@@ -93,6 +93,18 @@ def symbol_bodies(path: str, raw: bytes, symbols: Iterable[Symbol]) -> list[Symbreturn out+def bodies_for(+ sources: Mapping[str, bytes], symbols: Mapping[str, Iterable[Symbol]]+) -> list[SymbolBody]:+ """Every symbol body across the files in *symbols*, read from *sources*."""+ return [+ body+ for path, file_symbols in symbols.items()+ if path in sources+ for body in symbol_bodies(path, sources[path], file_symbols)+ ]++def build_move_map(base: Iterable[SymbolBody],head: Iterable[SymbolBody],diff --git a/packages/core/src/repowise/core/analysis/change_health/service.py b/packages/core/src/repowise/core/analysis/change_health/service.pyindex 46c77b185..9df3d9c48 100644--- a/packages/core/src/repowise/core/analysis/change_health/service.py+++ b/packages/core/src/repowise/core/analysis/change_health/service.py@@ -34,6 +34,7 @@ from .models import (RevisionId,ScopeCounts,)+from .moves import bodies_for, build_move_mapfrom .perf_delta import PerfOpportunityView, index_by_finding, opportunities_forfrom .sources import (FileChange,@@ -287,7 +288,12 @@ class ChangeHealthDeltaService:1 for r in skipped.values() if r.removeprefix("base_") in {"parse_failed", "unreadable"})- matcher = FindingMatcher(rename, _hunks(eligible))+ moves = build_move_map(+ bodies_for(base_sources, base_run.symbols),+ bodies_for(head_sources, head_run.symbols),+ rename_map=rename,+ )+ matcher = FindingMatcher(rename, _hunks(eligible), moves)# 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@@ -336,6 +342,8 @@ class ChangeHealthDeltaService:findings=findings,resolved_total=len(match.resolved),unchanged_total=match.unchanged_total,+ improved_total=match.improved_total,+ moved_total=match.moved_total,skipped=skipped,limits=_limits(head_run.findings_for(subject)),)@@ -352,8 +360,9 @@ class ChangeHealthDeltaService:) -> ChangeFinding:head = matched.headedge = perf.intervention_symbol if perf and perf.is_cross_function else None+ moved_from = matched.moved_fromattribution = attributor.attribute(- head, changed_symbols=changed_symbols, changed_call_edge=edge+ head, changed_symbols=changed_symbols, changed_call_edge=edge, moved_from=moved_from)key = finding_key(head)return ChangeFinding(@@ -377,6 +386,7 @@ class ChangeHealthDeltaService:opportunity_id=perf.opportunity_id if perf else None,opportunity_rank=perf.rank_score if perf else None,evidence=_evidence(head, perf),+ moved_from={"path": moved_from[0], "symbol": moved_from[1]} if moved_from else None,)diff --git a/packages/core/src/repowise/core/analysis/change_review/models.py b/packages/core/src/repowise/core/analysis/change_review/models.pyindex 2b32bab01..8ab9e0a9f 100644--- a/packages/core/src/repowise/core/analysis/change_review/models.py+++ b/packages/core/src/repowise/core/analysis/change_review/models.py@@ -226,6 +226,8 @@ def _delta_dict(delta: ChangeHealthDelta) -> dict[str, Any]:"worsened_total": delta.worsened_total,"resolved_total": delta.resolved_total,"unchanged_total": delta.unchanged_total,+ "improved_total": delta.improved_total,+ "moved_total": delta.moved_total,"uncertain_total": delta.uncertain_total,"findings": [asdict(finding) for finding in delta.findings],"skipped": dict(delta.skipped),diff --git a/packages/server/src/repowise/server/mcp_server/_change_health.py b/packages/server/src/repowise/server/mcp_server/_change_health.pyindex efefc2384..165322288 100644--- a/packages/server/src/repowise/server/mcp_server/_change_health.py+++ b/packages/server/src/repowise/server/mcp_server/_change_health.py@@ -179,6 +179,7 @@ def health_delta_block(delta: ChangeHealthDelta, *, revspec: str | None) -> dictif len(delta.findings) > len(emitted):block["findings_reduced_reason"] = "top_findings_cap"block["all_findings_via"] = _call(revspec, extra="include=['findings']")+ block.update(_nonzero(improved=delta.improved_total, moved=delta.moved_total))if delta.skipped:block["skipped"] = _skipped(delta.skipped)if delta.limits:@@ -211,6 +212,8 @@ def finding_row(finding: ChangeFinding, revspec: str | None) -> dict[str, Any]:row["lines"] = [finding.line_start, finding.line_end or finding.line_start]if finding.severity_before:row["severity_before"] = finding.severity_before+ if finding.moved_from:+ row["moved_from"] = finding.moved_fromif finding.opportunity_id:row["opportunity_id"] = finding.opportunity_idrow["opportunity_rank"] = finding.opportunity_rank@@ -231,10 +234,9 @@ def finding_detail(finding: ChangeFinding, revspec: str | None) -> dict[str, Any# -- internals ------------------------------------------------------------------+def _nonzero(**counts: int) -> dict[str, int]:+ """Only the counts that are set, so an ordinary change's block does not grow."""+ return {name: count for name, count in counts.items() if count}def _counts(findings: list[ChangeFinding], attribute: str) -> dict[str, int]:diff --git a/tests/unit/change_health/test_moved_findings.py b/tests/unit/change_health/test_moved_findings.pynew file mode 100644index 000000000..979fd72e5--- /dev/null+++ b/tests/unit/change_health/test_moved_findings.py@@ -0,0 +1,175 @@+"""Findings that travel with moved code, and findings whose metric dropped."""++from __future__ import annotations++from types import MappingProxyType++from repowise.core.analysis.change_health.matcher import FindingMatcher+from repowise.core.analysis.change_health.moves import MoveMap+from repowise.core.analysis.change_health.service import ChangeHealthDeltaService, DeltaRequest+from repowise.core.analysis.health import HealthFindingData, Severity++from .conftest import python_complex+++def finding(path, symbol, *, severity=Severity.HIGH, ccn=20, line=10):+ return HealthFindingData(+ biomarker_type="complex_method",+ severity=severity,+ file_path=path,+ function_name=symbol,+ line_start=line,+ line_end=line + 30,+ details={"ccn": ccn},+ health_impact=1.0,+ reason=f"{symbol} has cyclomatic complexity {ccn}",+ dimension="maintainability",+ )+++KNOT_MOVED = MoveMap(MappingProxyType({("pkg/knot.py", "knot"): ("tool.py", "knot")}))+++def compare(repo):+ service = ChangeHealthDeltaService(repo_path=str(repo.path))+ return service.compare(DeltaRequest(str(repo.path), "HEAD"))+++# -- matcher -----------------------------------------------------------------+++def test_a_finding_that_moved_with_its_code_pairs_with_its_origin():+ base = [finding("tool.py", "knot", line=200)]+ head = [finding("pkg/knot.py", "knot", line=1)]++ result = FindingMatcher(moves=KNOT_MOVED).match(base, head)++ assert [m.kind for m in result.matched] == ["moved"]+ assert result.matched[0].moved_from == ("tool.py", "knot")+ assert result.resolved == []+ assert result.moved_total == 1+++def test_a_moved_finding_that_measures_worse_is_worsened():+ base = [finding("tool.py", "knot", severity=Severity.MEDIUM)]+ head = [finding("pkg/knot.py", "knot", severity=Severity.CRITICAL, ccn=60)]++ result = FindingMatcher(moves=KNOT_MOVED).match(base, head)++ assert [m.kind for m in result.matched] == ["worsened"]+ assert result.moved_total == 1+++def test_a_moved_symbol_only_claims_its_own_marker():+ base = [finding("tool.py", "knot")]+ other = finding("pkg/knot.py", "knot")+ other.biomarker_type = "deep_nesting"++ result = FindingMatcher(moves=KNOT_MOVED).match(base, [other])++ assert [m.kind for m in result.matched] == ["introduced"]+ assert len(result.resolved) == 1+++def test_a_shrinking_function_with_a_lower_metric_is_improved_not_new():+ base = [finding("app.py", "init_command", severity=Severity.CRITICAL, ccn=163)]+ head = [finding("app.py", "init_command", severity=Severity.CRITICAL, ccn=74, line=40)]+ head[0].health_impact = 0.4++ result = FindingMatcher().match(base, head)++ assert [m.kind for m in result.matched] == ["improved"]+ assert result.of_kind("introduced", "worsened") == []+++def test_a_lower_severity_on_the_same_symbol_is_improved():+ base = [finding("app.py", "run_update", severity=Severity.CRITICAL, ccn=282)]+ head = [finding("app.py", "run_update", severity=Severity.HIGH, ccn=30)]++ result = FindingMatcher().match(base, head)++ assert [m.kind for m in result.matched] == ["improved"]+ assert result.improved_total == 1+++# -- end to end --------------------------------------------------------------+++def test_a_partial_split_reports_the_moved_findings_as_moved(make_repo):+ repo = make_repo()+ repo.commit("seed", {"tool.py": python_complex("tangle", 14) + python_complex("knot", 15)})+ repo.commit(+ "split", {"tool.py": python_complex("tangle", 14), "knots.py": python_complex("knot", 15)}+ )++ delta = compare(repo)++ assert delta.introduced_total == 0+ assert delta.resolved_total == 0+ assert delta.moved_total >= 1+++def test_a_moved_function_that_grew_surfaces_as_worsened_with_its_origin(make_repo):+ repo = make_repo()+ repo.commit("seed", {"tool.py": python_complex("tangle", 14) + python_complex("knot", 5)})+ repo.commit(+ "split", {"tool.py": python_complex("tangle", 14), "knots.py": python_complex("knot", 9)}+ )++ delta = compare(repo)++ knots = [f for f in delta.findings if f.path == "knots.py"]+ assert knots and all(f.change_kind == "worsened" for f in knots)+ assert {f.attribution_basis for f in knots} == {"moved"}+ assert knots[0].moved_from == {"path": "tool.py", "symbol": "knot"}+ assert delta.introduced_total == 0+++def test_a_function_that_shrank_in_place_counts_as_improved(make_repo):+ repo = make_repo()+ repo.commit("seed", {"app.py": python_complex("tangle", 40)})+ repo.commit("shrink", {"app.py": python_complex("tangle", 16)})++ delta = compare(repo)++ assert delta.introduced_total == 0+ assert delta.worsened_total == 0+ assert delta.improved_total >= 1+++def test_two_markers_on_one_origin_pair_by_what_they_measured():+ base = [+ finding("tool.py", "knot", ccn=20, line=10),+ finding("tool.py", "knot", ccn=30, line=90),+ ]+ head = [finding("pkg/knot.py", "knot", ccn=30, line=5), finding("pkg/knot.py", "knot", ccn=20)]++ result = FindingMatcher(moves=KNOT_MOVED).match(base, head)++ pairs = {m.head.details["ccn"]: m.base.details["ccn"] for m in result.matched}+ assert pairs == {20: 20, 30: 30}+ assert [m.kind for m in result.matched] == ["moved", "moved"]+++def test_a_nesting_drop_within_one_band_is_improved():+ base, head = finding("app.py", "walk"), finding("app.py", "walk")+ base.biomarker_type = head.biomarker_type = "deep_nesting"+ base.details, head.details = {"max_nesting": 6}, {"max_nesting": 5}++ assert [m.kind for m in FindingMatcher().match([base], [head]).matched] == ["improved"]+++def test_the_mcp_block_names_moved_findings_and_stays_lean_without_them(make_repo):+ from repowise.server.mcp_server._change_health import health_delta_block++ repo = make_repo()+ repo.commit("seed", {"tool.py": python_complex("tangle", 14) + python_complex("knot", 5)})+ repo.commit(+ "split", {"tool.py": python_complex("tangle", 14), "knots.py": python_complex("knot", 9)}+ )+ block = health_delta_block(compare(repo), revspec="HEAD")+ assert block["moved"] >= 1+ assert block["top_findings"][0]["moved_from"] == {"path": "tool.py", "symbol": "knot"}++ repo.commit("touch", {"tool.py": python_complex("tangle", 14) + "\n"})+ assert {"moved", "improved"}.isdisjoint(health_delta_block(compare(repo), revspec="HEAD"))diff --git a/tests/unit/change_health/test_regressions.py b/tests/unit/change_health/test_regressions.pyindex c5957ce75..30db05999 100644--- a/tests/unit/change_health/test_regressions.py+++ b/tests/unit/change_health/test_regressions.py@@ -272,7 +272,7 @@ def test_a_continuous_marker_still_worsens_when_its_own_deduction_grows():assert [m.kind for m in worse] == ["worsened"]better = FindingMatcher().match([gradient(0.6)], [gradient(0.2)]).matched- assert [m.kind for m in better] == ["unchanged"]+ assert [m.kind for m in better] == ["improved"]def test_an_expected_cause_says_there_is_nothing_to_change():