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.

feat(change-risk): store improved/moved totals and moved_from in commit-health rows (#4127)

Raghav Chamadiyae99746180713
diff --git a/packages/api-client/src/types/git.ts b/packages/api-client/src/types/git.ts
index 7fae86442..5195a65cc 100644
--- a/packages/api-client/src/types/git.ts
+++ b/packages/api-client/src/types/git.ts
@@ -171,6 +171,8 @@ export interface CommitHealthFindingResponse {
* `unknown`. Separates what a change wrote from what it merely touched. */
attribution_basis: string;
reason: string;
+ /** Set when the commit moved this finding's code here from another file. */
+ moved_from?: { path: string; symbol?: string | null } | null;
}
/** What a commit did to code health, computed at index time.
@@ -185,6 +187,10 @@ export interface CommitHealthResponse {
introduced_count: number;
worsened_count: number;
resolved_count: number;
+ /** Findings whose metric dropped, at the same place or after a move. */
+ improved_count?: number;
+ /** Findings carried to another file with their code; a worsened or improved one is in that count too. */
+ moved_count?: number;
files_analyzed: number;
files_skipped: number;
/** Worst first, capped. `introduced_count + worsened_count` is the true
diff --git a/packages/core/alembic/versions/0115_commit_health_moves.py b/packages/core/alembic/versions/0115_commit_health_moves.py
new file mode 100644
index 000000000..ef8943407
--- /dev/null
+++ b/packages/core/alembic/versions/0115_commit_health_moves.py
@@ -0,0 +1,38 @@
+"""Improved and moved totals, and move origins, on stored commit health.
+
+Rows scanned before this read 0 and NULL; local SQLite gets the columns from ``init_db``.
+
+Revision ID: 0115
+Revises: 0114
+Create Date: 2026-10-18
+"""
+
+from __future__ import annotations
+
+from collections.abc import Sequence
+
+import sqlalchemy as sa
+from alembic import op
+
+# revision identifiers
+revision: str = "0115"
+down_revision: str | None = "0114"
+branch_labels: str | Sequence[str] | None = None
+depends_on: str | Sequence[str] | None = None
+
+
+def upgrade() -> None:
+ for name in ("improved_count", "moved_count"):
+ op.add_column(
+ "git_commit_health_deltas",
+ sa.Column(name, sa.Integer, nullable=False, server_default="0"),
+ )
+ for name in ("moved_from_path", "moved_from_symbol"):
+ op.add_column("git_commit_health_findings", sa.Column(name, sa.Text, nullable=True))
+
+
+def downgrade() -> None:
+ for name in ("moved_from_symbol", "moved_from_path"):
+ op.drop_column("git_commit_health_findings", name)
+ for name in ("moved_count", "improved_count"):
+ op.drop_column("git_commit_health_deltas", name)
diff --git a/packages/core/src/repowise/core/analysis/change_health/commit_scan.py b/packages/core/src/repowise/core/analysis/change_health/commit_scan.py
index 6cc1d7fae..c3b7b84bf 100644
--- a/packages/core/src/repowise/core/analysis/change_health/commit_scan.py
+++ b/packages/core/src/repowise/core/analysis/change_health/commit_scan.py
@@ -51,6 +51,8 @@ def _delta_row(sha: str, delta: ChangeHealthDelta, kept: int) -> dict:
"introduced_count": delta.introduced_total,
"worsened_count": delta.worsened_total,
"resolved_count": delta.resolved_total,
+ "improved_count": delta.improved_total,
+ "moved_count": delta.moved_total,
"files_analyzed": delta.scope.analyzed,
"files_skipped": delta.scope.skipped,
"findings_stored": kept,
@@ -90,6 +92,8 @@ def _finding_rows(sha: str, findings: list[ChangeFinding]) -> list[dict]:
"attribution_basis": f.attribution_basis,
"health_impact": float(f.health_impact or 0.0),
"reason": f.reason,
+ "moved_from_path": (f.moved_from or {}).get("path"),
+ "moved_from_symbol": (f.moved_from or {}).get("symbol"),
}
for position, f in enumerate(findings)
]
diff --git a/packages/core/src/repowise/core/persistence/models.py b/packages/core/src/repowise/core/persistence/models.py
index 4bac8c82c..e41af09f6 100644
--- a/packages/core/src/repowise/core/persistence/models.py
+++ b/packages/core/src/repowise/core/persistence/models.py
@@ -949,6 +949,11 @@ class GitCommitHealthDelta(Base):
introduced_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
worsened_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
resolved_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
+ # Findings that dropped a metric, and findings carried to another file with their code.
+ improved_count: Mapped[int] = mapped_column(
+ Integer, nullable=False, default=0, server_default="0"
+ )
+ moved_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
files_analyzed: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
files_skipped: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
findings_stored: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
@@ -1005,6 +1010,9 @@ class GitCommitHealthFinding(Base):
attribution_basis: Mapped[str] = mapped_column(String(24), nullable=False, default="unknown")
health_impact: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
reason: Mapped[str] = mapped_column(Text, nullable=False, default="")
+ # Set when the finding's code moved here from another file in this commit.
+ moved_from_path: Mapped[str | None] = mapped_column(Text, nullable=True)
+ moved_from_symbol: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, default=_now_utc
diff --git a/packages/server/src/repowise/server/routers/git.py b/packages/server/src/repowise/server/routers/git.py
index feb96e3d4..edadc72f7 100644
--- a/packages/server/src/repowise/server/routers/git.py
+++ b/packages/server/src/repowise/server/routers/git.py
@@ -77,6 +77,7 @@ from repowise.server.schemas import (
RiskHistogramBucket,
RiskRangeResponse,
)
+from repowise.server.schemas.git import MovedFromResponse
from repowise.server.services.module_health import top_level_module
from repowise.server.services.reviewer_suggestions import suggest_reviewers
@@ -272,6 +273,8 @@ async def _commit_health(
introduced_count=delta.introduced_count,
worsened_count=delta.worsened_count,
resolved_count=delta.resolved_count,
+ improved_count=delta.improved_count,
+ moved_count=delta.moved_count,
files_analyzed=delta.files_analyzed,
files_skipped=delta.files_skipped,
findings=[
@@ -287,6 +290,11 @@ async def _commit_health(
line_end=f.line_end,
attribution_basis=f.attribution_basis,
reason=f.reason,
+ moved_from=(
+ MovedFromResponse(path=f.moved_from_path, symbol=f.moved_from_symbol)
+ if f.moved_from_path
+ else None
+ ),
)
for f in rows
],
diff --git a/packages/server/src/repowise/server/schemas/git.py b/packages/server/src/repowise/server/schemas/git.py
index 3179c2950..806a72295 100644
--- a/packages/server/src/repowise/server/schemas/git.py
+++ b/packages/server/src/repowise/server/schemas/git.py
@@ -256,6 +256,13 @@ class RiskDriverResponse(BaseModel):
label: str
+class MovedFromResponse(BaseModel):
+ """Where a finding's code was before the commit moved it."""
+
+ path: str
+ symbol: str | None = None
+
+
class CommitHealthFindingResponse(BaseModel):
"""One thing a commit introduced or worsened."""
@@ -274,6 +281,8 @@ class CommitHealthFindingResponse(BaseModel):
#: merely touched.
attribution_basis: str
reason: str
+ #: Set when the commit moved this finding's code here from another file.
+ moved_from: MovedFromResponse | None = None
class CommitHealthResponse(BaseModel):
@@ -290,6 +299,10 @@ class CommitHealthResponse(BaseModel):
introduced_count: int
worsened_count: int
resolved_count: int
+ #: Findings whose metric dropped, at the same place or after a move.
+ improved_count: int = 0
+ #: Findings carried to another file with their code; a worsened or improved one is in that count too.
+ moved_count: int = 0
files_analyzed: int
files_skipped: int
#: Worst first, capped. ``introduced_count + worsened_count`` is the true
diff --git a/packages/types/src/generated/http.ts b/packages/types/src/generated/http.ts
index 938ec25a8..46b7a0d4e 100644
--- a/packages/types/src/generated/http.ts
+++ b/packages/types/src/generated/http.ts
@@ -843,6 +843,7 @@ export interface CommitHealthFindingResponse {
line_end?: number | null;
attribution_basis: string;
reason: string;
+ moved_from?: MovedFromResponse | null;
}
/**
@@ -857,6 +858,8 @@ export interface CommitHealthResponse {
introduced_count: number;
worsened_count: number;
resolved_count: number;
+ improved_count?: number;
+ moved_count?: number;
files_analyzed: number;
files_skipped: number;
findings?: CommitHealthFindingResponse[];
@@ -2557,6 +2560,12 @@ export interface ModuleNodeResponse {
primary_owner?: string | null;
}
+/** Where a finding's code was before the commit moved it. */
+export interface MovedFromResponse {
+ path: string;
+ symbol?: string | null;
+}
+
export interface NeighboringCommunity {
community_id: number;
label: string;
diff --git a/packages/types/src/git.ts b/packages/types/src/git.ts
index 95e690691..83f040a7d 100644
--- a/packages/types/src/git.ts
+++ b/packages/types/src/git.ts
@@ -202,6 +202,8 @@ export interface CommitHealthFinding {
* `unknown`. Separates what a change wrote from what it merely touched. */
attribution_basis: string;
reason: string;
+ /** Set when the commit moved this finding's code here from another file. */
+ moved_from?: { path: string; symbol?: string | null } | null;
}
/** What a commit did to code health, computed at index time.
@@ -216,6 +218,10 @@ export interface CommitHealth {
introduced_count: number;
worsened_count: number;
resolved_count: number;
+ /** Findings whose metric dropped, at the same place or after a move. */
+ improved_count?: number;
+ /** Findings carried to another file with their code; a worsened or improved one is in that count too. */
+ moved_count?: number;
files_analyzed: number;
files_skipped: number;
/** Worst first, capped. `introduced_count + worsened_count` is the true
diff --git a/tests/unit/analysis/test_commit_health_scan.py b/tests/unit/analysis/test_commit_health_scan.py
index b48764681..3382435e9 100644
--- a/tests/unit/analysis/test_commit_health_scan.py
+++ b/tests/unit/analysis/test_commit_health_scan.py
@@ -157,3 +157,16 @@ def test_no_shas_is_not_an_error() -> None:
scan = scan_commits("/repo", [], service=_StubService({}))
assert (scan.delta_rows, scan.finding_rows, scan.scanned) == ([], [], 0)
+
+
+def test_rows_keep_improved_and_moved_totals_and_where_a_finding_moved_from() -> None:
+ moved = _finding("f0", change_kind="worsened", moved_from={"path": "old.py", "symbol": "g"})
+ delta = _delta([moved, _finding("f1")], improved_total=3, moved_total=5)
+
+ scan = scan_commits("/repo", ["aaa"], service=_StubService({"aaa": delta}))
+
+ (row,) = scan.delta_rows
+ assert (row["improved_count"], row["moved_count"]) == (3, 5)
+ by_id = {r["change_finding_id"]: r for r in scan.finding_rows}
+ assert (by_id["f0"]["moved_from_path"], by_id["f0"]["moved_from_symbol"]) == ("old.py", "g")
+ assert (by_id["f1"]["moved_from_path"], by_id["f1"]["moved_from_symbol"]) == (None, None)
diff --git a/tests/unit/server/test_git.py b/tests/unit/server/test_git.py
index 562c543b6..e2868e204 100644
--- a/tests/unit/server/test_git.py
+++ b/tests/unit/server/test_git.py
@@ -481,6 +481,53 @@ async def test_commit_detail_reports_what_the_commit_did_to_health(
assert finding["severity_before"] == "high"
assert finding["path"] == "src/main.py"
assert finding["attribution_basis"] == "added_lines"
+ assert (health["improved_count"], health["moved_count"]) == (0, 0)
+ assert finding["moved_from"] is None
+
+
+async def test_commit_detail_names_where_a_moved_finding_came_from(
+ client: AsyncClient, app
+) -> None:
+ repo = await create_test_repo(client)
+ await _insert_git_commits(app.state.session_factory, repo["id"])
+ async with get_session(app.state.session_factory) as session:
+ await crud.upsert_commit_health_bulk(
+ session,
+ repo["id"],
+ [
+ {
+ "sha": "bbbbbbbb22",
+ "status": "available",
+ "worsened_count": 1,
+ "improved_count": 2,
+ "moved_count": 7,
+ "findings_stored": 1,
+ }
+ ],
+ [
+ {
+ "sha": "bbbbbbbb22",
+ "change_finding_id": "f0",
+ "position": 0,
+ "change_kind": "worsened",
+ "dimension": "defect",
+ "biomarker_type": "complex_method",
+ "severity": "critical",
+ "file_path": "src/pkg/run.py",
+ "symbol": "run",
+ "attribution_basis": "moved",
+ "reason": "run has cyclomatic complexity 26",
+ "moved_from_path": "src/main.py",
+ "moved_from_symbol": "run",
+ }
+ ],
+ )
+
+ health = (await client.get(f"/api/repos/{repo['id']}/commits/bbbbbbbb")).json()["health"]
+
+ assert (health["improved_count"], health["moved_count"]) == (2, 7)
+ assert health["findings"][0]["moved_from"] == {"path": "src/main.py", "symbol": "run"}
@pytest.mark.asyncio