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): keep history markers through the update's full re-score (#2614)

Raghav Chamadiya8242cf656f1d
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/__init__.py b/packages/cli/src/repowise/cli/commands/update_cmd/__init__.py
index 189e4672c..922a06fdc 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/__init__.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/__init__.py
@@ -19,7 +19,6 @@ from .incremental import (
)
from .mode import _resolve_index_only_mode
from .persistence import (
- _git_metadata_to_dict,
_persist_incremental_commits,
_persist_index_only_update,
_persist_partial_health,
@@ -32,7 +31,6 @@ __all__ = [
"_build_filtered_changed_paths",
"_build_repo_graph",
"_build_update_vector_store",
- "_git_metadata_to_dict",
"_persist_incremental_commits",
"_persist_index_only_update",
"_persist_partial_health",
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 4b81cfd73..af1e132a8 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
@@ -1355,44 +1355,6 @@ async def _persist_full_update_async(
await engine.dispose()
-def _git_metadata_to_dict(gm: Any) -> dict[str, Any]:
- """Convert a GitMetadata ORM row to the dict format HealthAnalyzer expects."""
- return {
- "file_path": gm.file_path,
- "commit_count_total": gm.commit_count_total,
- "commit_count_90d": gm.commit_count_90d,
- "commit_count_30d": gm.commit_count_30d,
- "first_commit_at": gm.first_commit_at,
- "last_commit_at": gm.last_commit_at,
- "primary_owner_name": gm.primary_owner_name,
- "primary_owner_email": gm.primary_owner_email,
- "primary_owner_commit_pct": gm.primary_owner_commit_pct,
- "top_authors_json": gm.top_authors_json,
- "significant_commits_json": gm.significant_commits_json,
- "co_change_partners_json": gm.co_change_partners_json,
- "commit_categories_json": gm.commit_categories_json,
- "is_hotspot": gm.is_hotspot,
- "is_stable": gm.is_stable,
- "churn_percentile": gm.churn_percentile,
- "age_days": gm.age_days,
- "commit_count_capped": gm.commit_count_capped,
- "lines_added_90d": gm.lines_added_90d,
- "lines_deleted_90d": gm.lines_deleted_90d,
- "avg_commit_size": gm.avg_commit_size,
- "recent_owner_name": gm.recent_owner_name,
- "recent_owner_commit_pct": gm.recent_owner_commit_pct,
- "bus_factor": gm.bus_factor,
- "contributor_count": gm.contributor_count,
- "original_path": gm.original_path,
- "merge_commit_count_90d": gm.merge_commit_count_90d,
- "temporal_hotspot_score": gm.temporal_hotspot_score,
- "prior_defect_count": gm.prior_defect_count,
- "prior_defect_raw_count": gm.prior_defect_raw_count,
- "change_entropy": gm.change_entropy,
- "change_entropy_pct": gm.change_entropy_pct,
- }
-
-
async def _rescore_health_from_db(
repo_path: Any,
graph_builder: Any,
@@ -1424,6 +1386,10 @@ async def _rescore_health_from_db(
from repowise.cli.helpers import get_db_url_for_repo
from repowise.core.analysis.health import HealthAnalyzer
from repowise.core.analysis.health.config import HealthConfig
+ from repowise.core.analysis.health.history_refresh import (
+ BLAME_MARKERS,
+ git_meta_rows_to_map,
+ )
from repowise.core.persistence import (
create_engine,
create_session_factory,
@@ -1432,7 +1398,7 @@ async def _rescore_health_from_db(
upsert_repository,
)
from repowise.core.persistence.crud import save_coverage_files
- from repowise.core.persistence.models import GitMetadata
+ from repowise.core.persistence.models import GitMetadata, HealthFinding
from repowise.core.pipeline.persist import (
persist_graph_nodes,
save_full_health_report,
@@ -1466,11 +1432,23 @@ async def _rescore_health_from_db(
)
await session.flush()
- git_meta_map = {
- gm.file_path: _git_metadata_to_dict(gm)
+ # Every stored column, so a gate added with a new column reaches the
+ # detectors here as it does on a full index.
+ git_meta_map = git_meta_rows_to_map(
+ gm
for gm in git_rows
if exclude_spec is None or not exclude_spec.match_file(gm.file_path)
- }
+ )
+ stored_blame_findings: dict[str, list[HealthFinding]] = {}
+ for finding in (
+ await session.execute(
+ select(HealthFinding).where(
+ HealthFinding.repository_id == repo_id,
+ HealthFinding.biomarker_type.in_(BLAME_MARKERS),
+ )
+ )
+ ).scalars():
+ stored_blame_findings.setdefault(finding.file_path, []).append(finding)
# Preserve coverage across a re-score. The previous behaviour
# rebuilt the analyzer with no coverage_map, nulling every file's
@@ -1491,6 +1469,7 @@ async def _rescore_health_from_db(
coverage_map=coverage_map,
duplication_cache_dir=Path(repo_path) / ".repowise",
repo_root=repo_path,
+ stored_blame_findings=stored_blame_findings,
)
hcfg = HealthConfig.load(repo_path)
analyzer_config = (
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index 9d1302bd3..075b3b6f0 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -46,6 +46,7 @@ from .coverage import is_test_file as _coverage_is_test_file
from .dataflow import FileDataflowCache
from .duplication import DuplicationReport
from .duplication.isolation import detect_clones_with_isolation as detect_clones
+from .history_refresh import BLAME_MARKERS, as_biomarker_result
from .models import HealthFileMetricData, HealthFindingData, HealthReport, Severity
from .perf import (
CallGraphIndex,
@@ -111,6 +112,11 @@ log = structlog.get_logger(__name__)
# and a declining call's arguments are not scanned, so an assertion passed as an
# argument still does not stand in for the header's oracle.
#
+# v35: the update's full re-score reads every stored git column, so the
+# percentile gates of ``prior_defect`` and ``co_change_scatter`` see their
+# inputs, and it keeps the stored blame-marker findings it cannot recompute.
+# A store an older re-score wrote is missing those findings.
+#
# v34: ``hidden_coupling`` is advisory: still detected and stored, it no longer
# deducts from the defect score, so every stored score that carried one moves.
#
@@ -315,7 +321,7 @@ log = structlog.get_logger(__name__)
# forms. Files that were counted untested and are not become tested, which
# moves untested-hotspot findings and the scores that carry them, on every
# language with a prefix or spec convention rather than Ruby alone.
-HEALTH_ANALYZER_VERSION = 34
+HEALTH_ANALYZER_VERSION = 35
def walked_functions(
@@ -510,6 +516,7 @@ class HealthAnalyzer:
duplication_cache_dir: Any | None = None,
repo_root: Any | None = None,
source_reader: SourceReader | None = None,
+ stored_blame_findings: dict[str, list[Any]] | None = None,
) -> None:
self.graph = graph
self.git_meta_map = git_meta_map or {}
@@ -543,6 +550,10 @@ class HealthAnalyzer:
# Every source read in the pass. Defaults to the working tree; a
# revision comparison supplies bytes instead.
self.read_source: SourceReader = source_reader or disk_source_reader
+ # Stored ``BLAME_MARKERS`` findings by path, replayed for a file scored
+ # 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 {}
self._package_roots_cache: set[str] | None = None
self._tests_reach_cache: set[str] | None = None
self._execution_graph_cache: CallGraphIndex | None = None
@@ -1360,6 +1371,11 @@ class HealthAnalyzer:
)
biomarker_results = detect_all(ctx, disabled=disabled)
+ if blame_index is None:
+ for stored in self.stored_blame_findings.get(file_path, ()):
+ replayed = as_biomarker_result(stored)
+ if replayed.biomarker_type in BLAME_MARKERS and replayed.biomarker_type not in disabled:
+ biomarker_results.append(replayed)
biomarker_results = remap_severities(biomarker_results, severity_overrides)
scores, deductions = score_file(biomarker_results)
findings = attach_impacts(biomarker_results, deductions)
diff --git a/packages/core/src/repowise/core/analysis/health/history_refresh.py b/packages/core/src/repowise/core/analysis/health/history_refresh.py
index 6e495cd4a..b3b527983 100644
--- a/packages/core/src/repowise/core/analysis/health/history_refresh.py
+++ b/packages/core/src/repowise/core/analysis/health/history_refresh.py
@@ -38,6 +38,9 @@ REFRESHABLE_MARKERS: frozenset[str] = frozenset(
}
)
+#: History markers that read a per-line blame index, which is never persisted.
+BLAME_MARKERS: frozenset[str] = frozenset({"code_age_volatility", "function_hotspot"})
+
@dataclass
class RefreshedFile:
diff --git a/tests/unit/cli/test_commands.py b/tests/unit/cli/test_commands.py
index fdb893d06..47deb76d2 100644
--- a/tests/unit/cli/test_commands.py
+++ b/tests/unit/cli/test_commands.py
@@ -378,59 +378,6 @@ class TestBuildFilteredChangedPaths:
assert result == ["src/main.py", ".claude/config.yml"]
-class TestGitMetadataToDict:
- def test_converts_orm_row_to_dict(self):
- from types import SimpleNamespace
-
- from repowise.cli.commands.update_cmd import _git_metadata_to_dict
-
- gm = SimpleNamespace(
- file_path="src/main.py",
- commit_count_total=42,
- commit_count_90d=10,
- commit_count_30d=3,
- first_commit_at=None,
- last_commit_at=None,
- primary_owner_name="alice",
- primary_owner_email="[email protected]",
- primary_owner_commit_pct=0.7,
- top_authors_json="[]",
- significant_commits_json="[]",
- co_change_partners_json="[]",
- commit_categories_json="{}",
- is_hotspot=True,
- is_stable=False,
- churn_percentile=0.9,
- age_days=100,
- commit_count_capped=False,
- lines_added_90d=120,
- lines_deleted_90d=30,
- avg_commit_size=15.0,
- recent_owner_name="alice",
- recent_owner_commit_pct=0.8,
- bus_factor=2,
- contributor_count=4,
- original_path=None,
- merge_commit_count_90d=1,
- temporal_hotspot_score=0.8,
- prior_defect_count=5,
- prior_defect_raw_count=9,
- change_entropy=0.42,
- change_entropy_pct=0.6,
- )
-
- d = _git_metadata_to_dict(gm)
- assert d["file_path"] == "src/main.py"
- assert d["commit_count_total"] == 42
- assert d["is_hotspot"] is True
- assert d["bus_factor"] == 2
- # Columns added by the newer health biomarkers must flow through too.
- assert d["prior_defect_count"] == 5
- assert d["prior_defect_raw_count"] == 9
- assert d["change_entropy"] == 0.42
- assert d["change_entropy_pct"] == 0.6
-
-
class TestRescoreFailureFingerprint:
def test_failed_rescore_does_not_advance_fingerprint(self, tmp_path, monkeypatch):
"""A failed re-score must not persist the new fingerprint, so the next
diff --git a/tests/unit/cli/test_rescore_rebuilds_read_models.py b/tests/unit/cli/test_rescore_rebuilds_read_models.py
index 10b16dc3f..4a61173a1 100644
--- a/tests/unit/cli/test_rescore_rebuilds_read_models.py
+++ b/tests/unit/cli/test_rescore_rebuilds_read_models.py
@@ -27,7 +27,9 @@ from repowise.core.persistence import (
upsert_repository,
)
from repowise.core.persistence.models import (
+ GitMetadata,
HealthFileMetric,
+ HealthFinding,
PerformanceOpportunity,
PerformanceSummary,
)
@@ -181,3 +183,70 @@ class TestRescoreRebuildsTheReadModels:
assert summary_commit == head
# The column the index-freshness signal is read from.
assert metrics[_SCORED] == head
+
+
+async def _seed_history(repo_path: Path) -> str:
+ """Stored git metadata that clears every percentile gate, plus a stored
+ blame-marker finding the re-score has no blame index to recompute."""
+ engine = create_engine(get_db_url_for_repo(repo_path))
+ await init_db(engine)
+ factory = create_session_factory(engine)
+ async with get_session(factory) as session:
+ repo = await upsert_repository(session, name=repo_path.name, local_path=str(repo_path))
+ session.add(
+ GitMetadata(
+ repository_id=repo.id,
+ file_path=_SCORED,
+ commit_count_total=40,
+ commit_count_90d=30,
+ is_hotspot=True,
+ prior_defect_count=6,
+ prior_defect_pct=0.99,
+ co_change_partner_count=60,
+ co_change_mass=9.0,
+ co_change_scatter_pct=0.99,
+ )
+ )
+ session.add(
+ HealthFinding(
+ repository_id=repo.id,
+ file_path=_SCORED,
+ biomarker_type="function_hotspot",
+ severity="high",
+ function_name="run",
+ line_start=1,
+ line_end=2,
+ details_json='{"modification_count": 12, "repo_p80": 4}',
+ reason="run has been modified across 12 commits",
+ )
+ )
+ await session.commit()
+ repo_id = repo.id
+ await engine.dispose()
+ return repo_id
+
+
+async def _finding_types(repo_path: Path, repo_id: str) -> set[str]:
+ engine = create_engine(get_db_url_for_repo(repo_path))
+ async with get_session(create_session_factory(engine)) as session:
+ rows = await session.execute(
+ select(HealthFinding.biomarker_type).where(
+ HealthFinding.repository_id == repo_id, HealthFinding.file_path == _SCORED
+ )
+ )
+ types = set(rows.scalars())
+ await engine.dispose()
+ return types
+
+
+class TestRescoreKeepsHistoryMarkers:
+ async def test_stored_history_reaches_every_history_marker(self, git_repo: Path):
+ repo_id = await _seed_history(git_repo)
+
+ await _rescore_health_from_db(git_repo, _EmptyGraphBuilder(), [_parsed_file(git_repo)], [])
+
+ types = await _finding_types(git_repo, repo_id)
+ # Percentile-gated markers read columns a hand-kept projection dropped.
+ assert {"prior_defect", "co_change_scatter"} <= types
+ # No blame index is persisted, so the stored finding is kept, not deleted.
+ assert "function_hotspot" in types
diff --git a/tests/unit/health/test_model_version_coupling.py b/tests/unit/health/test_model_version_coupling.py
index b9d058d82..4e9703d41 100644
--- a/tests/unit/health/test_model_version_coupling.py
+++ b/tests/unit/health/test_model_version_coupling.py
@@ -22,7 +22,7 @@ from repowise.core.analysis.health.refactoring.identity import (
# The three stamps as they stand together. Moving either identity model means
# moving the analyzer with it, and updating this tuple in the same commit.
-_STAMPS = (34, 2, 2)
+_STAMPS = (35, 2, 2)
def test_the_identity_models_and_the_analyzer_stamp_are_pinned_together() -> None: