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(health): stamp the commit on rows the history refresh rescores (#3531)

Raghav Chamadiya3275ec1a5360
diff --git a/packages/core/src/repowise/core/pipeline/incremental.py b/packages/core/src/repowise/core/pipeline/incremental.py
index ee6ac2ee0..fd28f2ce6 100644
--- a/packages/core/src/repowise/core/pipeline/incremental.py
+++ b/packages/core/src/repowise/core/pipeline/incremental.py
@@ -1135,6 +1135,7 @@ async def refresh_unchanged_history(
upsert_health_findings,
upsert_health_metrics,
)
+ from repowise.core.pipeline.persist import _analyzed_commit
stored = {
m.file_path: m
@@ -1191,6 +1192,16 @@ async def refresh_unchanged_history(
file_paths=[r.file_path for r in finding_writes],
)
if metric_writes:
+ # A rescored row is scored against this commit: its history is
+ # read from it, and the file is unchanged since its last parse.
+ # Keeping the old stamp made the newest row name a commit 100
+ # behind, and the health basis reads the newest row. Set on each
+ # row, not passed as the writer's default: when HEAD cannot be read
+ # the stamp is cleared, since "not recorded" is honest and the old
+ # commit is not.
+ commit = await _analyzed_commit(session, repo_id)
+ for row in metric_writes:
+ row["analyzed_commit"] = commit
await upsert_health_metrics(session, repo_id, metric_writes)
logger.debug(
"health_history_refreshed",
diff --git a/tests/unit/persistence/test_health_dimensions_crud.py b/tests/unit/persistence/test_health_dimensions_crud.py
index 469bc3b81..7ef402aaa 100644
--- a/tests/unit/persistence/test_health_dimensions_crud.py
+++ b/tests/unit/persistence/test_health_dimensions_crud.py
@@ -258,3 +258,54 @@ async def test_get_health_findings_accepts_comma_separated_biomarker_types(async
# Single value still behaves as an exact match.
single = await get_health_findings(async_session, repo.id, biomarker_type="change_entropy")
assert {r.biomarker_type for r in single} == {"change_entropy"}
+
+
[email protected]("head", ["new", None])
+async def test_a_history_rescore_stamps_the_commit_it_read(async_session, monkeypatch, head):
+ """The health basis reads the newest metric row. A history refresh rewrote
+ unchanged files' rows but kept their first commit, so the basis read 100
+ commits behind. With HEAD unreadable the stamp is cleared, not kept."""
+ from types import SimpleNamespace
+
+ import repowise.core.analysis.health.history_refresh as history_refresh
+ import repowise.core.persistence.crud as crud
+ import repowise.core.pipeline.persist as persist
+ from repowise.core.pipeline.incremental import refresh_unchanged_history
+
+ repo = await insert_repo(async_session)
+ await upsert_health_metrics(
+ async_session, repo.id, [_metric("a.py"), _metric("b.py")], analyzed_commit="old"
+ )
+ await async_session.commit()
+
+ async def git_rows(*_args, **_kwargs):
+ return {"a.py": object()}
+
+ def rescore(**_kwargs):
+ return [
+ SimpleNamespace(
+ file_path="a.py",
+ score=5.0,
+ defect_score=5.0,
+ maintainability_score=6.0,
+ performance_score=None,
+ structure_deduction=1.0,
+ history_deduction=1.5,
+ findings=[],
+ )
+ ]
+
+ async def read_head(*_args):
+ return head
+
+ monkeypatch.setattr(crud, "get_all_git_metadata", git_rows)
+ monkeypatch.setattr(history_refresh, "git_meta_rows_to_map", lambda rows: {"a.py": {}})
+ monkeypatch.setattr(history_refresh, "active_contributors", lambda meta: 1)
+ monkeypatch.setattr(history_refresh, "refresh_history", rescore)
+ monkeypatch.setattr(persist, "_analyzed_commit", read_head)
+
+ await refresh_unchanged_history(async_session, repo.id, ".", skip_paths=set())
+ rows = {r.file_path: r for r in await get_health_metrics(async_session, repo.id)}
+ assert (rows["a.py"].score, rows["a.py"].analyzed_commit) == (5.0, head)
+ assert rows["b.py"].analyzed_commit == "old" # not rescored, not restamped