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): drop replayed function hotspots whose function no longer clears the complexity floor (#3891)

Raghav Chamadiya02730482963e
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/function_hotspot.py b/packages/core/src/repowise/core/analysis/health/biomarkers/function_hotspot.py
index fe8bb0fbb..bb345bcde 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/function_hotspot.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/function_hotspot.py
@@ -45,6 +45,31 @@ _CCN_HIGH = 15
_CCN_CRITICAL = 20
+def meets_structural_floor(fc) -> bool:
+ return fc.ccn >= _CCN_FLOOR or fc.max_nesting >= _NESTING_FLOOR
+
+
+def refresh_replayed(replayed: BiomarkerResult, functions) -> bool:
+ """Bring a replayed hotspot's complexity up to date; False if it no longer qualifies.
+
+ A re-score with no blame index replays the stored row, whose complexity
+ figures an older walker wrote. Churn cannot be re-measured there, but
+ complexity can, so a function that no longer clears the floor is dropped.
+ """
+ for fc in functions:
+ if fc.name == replayed.function_name and meets_structural_floor(fc):
+ replayed.details = {**replayed.details, "ccn": fc.ccn, "max_nesting": fc.max_nesting}
+ mods, p80 = replayed.details.get("modification_count"), replayed.details.get("repo_p80")
+ if isinstance(mods, int) and isinstance(p80, int):
+ replayed.severity = _severity_for(mods, max(p80, _MIN_CHANGES), fc.ccn)
+ replayed.reason = (
+ f"{fc.name} has been modified across {mods} commits (repo p80={p80}) "
+ f"and carries CCN={fc.ccn} / nesting={fc.max_nesting}"
+ )
+ return True
+ return False
+
+
def _severity_for(mod_count: int, bar: int, ccn: int) -> Severity:
if mod_count >= 3 * bar and ccn >= _CCN_CRITICAL:
return Severity.CRITICAL
@@ -71,7 +96,7 @@ class FunctionHotspotDetector:
mod_count = len(distinct_commits_in_range(idx, fc.start_line, fc.end_line))
if mod_count < bar:
continue
- if fc.ccn < _CCN_FLOOR and fc.max_nesting < _NESTING_FLOOR:
+ if not meets_structural_floor(fc):
continue
severity = _severity_for(mod_count, bar, fc.ccn)
findings.append(
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index a4df13a2c..2a85e731f 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -51,6 +51,7 @@ from ..test_reachability import files_reached_by_tests, files_with_paired_tests
from .asserts.lexicon import AssertVocabulary
from .asserts.oracle_reach import collect_cross_file_oracles
from .biomarkers import FileContext, detect_all
+from .biomarkers.function_hotspot import refresh_replayed
from .complexity import FileComplexity, FunctionComplexity, walk_file
from .complexity.languages import has_health_dialect
from .dataflow import FileDataflowCache
@@ -1725,8 +1726,11 @@ class HealthAnalyzer:
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)
+ if replayed.biomarker_type not in BLAME_MARKERS or replayed.biomarker_type in disabled:
+ continue
+ if replayed.biomarker_type == "function_hotspot" and not refresh_replayed(replayed, fns):
+ continue
+ 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/tests/unit/cli/test_rescore_rebuilds_read_models.py b/tests/unit/cli/test_rescore_rebuilds_read_models.py
index de48eb0e0..9b2780c1d 100644
--- a/tests/unit/cli/test_rescore_rebuilds_read_models.py
+++ b/tests/unit/cli/test_rescore_rebuilds_read_models.py
@@ -55,7 +55,16 @@ class _EmptyGraphBuilder:
_SCORED = "src/app.py"
-_SOURCE = "def run(items):\n return [i for i in items if i]\n"
+# Nested three deep so a replayed function_hotspot on `run` still clears the
+# complexity floor the re-score checks it against.
+_SOURCE = (
+ "def run(items):\n"
+ " for i in items:\n"
+ " if i:\n"
+ " while i:\n"
+ " i -= 1\n"
+ " return items\n"
+)
def _parsed_file(repo_path: Path) -> SimpleNamespace:
diff --git a/tests/unit/health/test_function_hotspot.py b/tests/unit/health/test_function_hotspot.py
index 578221af7..ec3679afc 100644
--- a/tests/unit/health/test_function_hotspot.py
+++ b/tests/unit/health/test_function_hotspot.py
@@ -2,9 +2,10 @@
from __future__ import annotations
-from repowise.core.analysis.health.biomarkers.base import FileContext
+from repowise.core.analysis.health.biomarkers.base import BiomarkerResult, FileContext
from repowise.core.analysis.health.biomarkers.function_hotspot import (
FunctionHotspotDetector,
+ refresh_replayed,
)
from repowise.core.analysis.health.complexity import FunctionComplexity
from repowise.core.analysis.health.models import Severity
@@ -135,3 +136,23 @@ def test_severity_scales_from_the_floor_not_a_small_p80():
assert FunctionHotspotDetector().detect(_ctx(fc, _changed(10), p80=3))[0].severity == (
Severity.HIGH
)
+
+
+def test_refresh_replayed_updates_complexity_or_drops_hotspot():
+ def replayed() -> BiomarkerResult:
+ return BiomarkerResult(
+ biomarker_type="function_hotspot",
+ severity=Severity.MEDIUM,
+ function_name="hot",
+ line_start=1,
+ line_end=10,
+ details={"ccn": 9, "max_nesting": 7, "modification_count": 12, "repo_p80": 5},
+ )
+
+ kept = replayed()
+ assert refresh_replayed(kept, [_fc("hot", 1, 10, ccn=12, nesting=5)])
+ assert (kept.details["ccn"], kept.details["max_nesting"]) == (12, 5)
+ assert kept.severity == Severity.HIGH
+ assert "CCN=12 / nesting=5" in kept.reason
+ assert not refresh_replayed(replayed(), [_fc("hot", 1, 10, ccn=9, nesting=2)])
+ assert not refresh_replayed(replayed(), [_fc("other", 1, 10, ccn=30)])