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): a function hotspot needs at least five changes (#2918)

Raghav Chamadiya0a691043fccc
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 b82c06619..fe8bb0fbb 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
@@ -13,12 +13,18 @@ detector emits zero findings. The same no-op holds when the engine
could not compute the repo-wide p80 (no functions had a non-zero
mod_count).
-Severity — combined evidence:
+The churn bar is ``max(p80, 5)`` changes: a repo-relative top quintile, but
+never fewer than five commits. Two to four edits are what any function under
+active work collects, and on a thin window (a shallow clone, whose blame
+credits every older line to its boundary commit, or a young repository) the
+p80 is 1 or 2, so the quintile alone would flag a function changed twice.
-* MEDIUM when the function is at-or-above p80 churn AND meets the
+Severity — combined evidence, relative to that bar:
+
+* MEDIUM when the function is at-or-above the bar AND meets the
structural floor (``ccn >= 10`` or ``max_nesting >= 3``).
-* HIGH when *both* axes are top-decile (mod_count >= 2 * p80 OR ccn >= 15).
-* CRITICAL when both axes are top-5% (mod_count >= 3 * p80 AND ccn >= 20).
+* HIGH when *both* axes are top-decile (mod_count >= 2 * bar OR ccn >= 15).
+* CRITICAL when both axes are top-5% (mod_count >= 3 * bar AND ccn >= 20).
The relative cut-offs are a calibrated approximation of the Z-score
criterion from the spec — easier to reason about, no per-repo
@@ -33,17 +39,16 @@ from ..models import Severity
from .base import BiomarkerResult, FileContext
_CCN_FLOOR = 10
+_MIN_CHANGES = 5
_NESTING_FLOOR = 3
_CCN_HIGH = 15
_CCN_CRITICAL = 20
-def _severity_for(mod_count: int, p80: int, ccn: int) -> Severity:
- if p80 <= 0:
- return Severity.MEDIUM
- if mod_count >= 3 * p80 and ccn >= _CCN_CRITICAL:
+def _severity_for(mod_count: int, bar: int, ccn: int) -> Severity:
+ if mod_count >= 3 * bar and ccn >= _CCN_CRITICAL:
return Severity.CRITICAL
- if mod_count >= 2 * p80 or ccn >= _CCN_HIGH:
+ if mod_count >= 2 * bar or ccn >= _CCN_HIGH:
return Severity.HIGH
return Severity.MEDIUM
@@ -59,15 +64,16 @@ class FunctionHotspotDetector:
p80 = ctx.repo_function_mod_p80
if p80 is None or p80 <= 0:
return []
+ bar = max(p80, _MIN_CHANGES)
findings: list[BiomarkerResult] = []
for fc in ctx.all_functions:
mod_count = len(distinct_commits_in_range(idx, fc.start_line, fc.end_line))
- if mod_count < p80:
+ if mod_count < bar:
continue
if fc.ccn < _CCN_FLOOR and fc.max_nesting < _NESTING_FLOOR:
continue
- severity = _severity_for(mod_count, p80, fc.ccn)
+ severity = _severity_for(mod_count, bar, fc.ccn)
findings.append(
BiomarkerResult(
biomarker_type=self.name,
diff --git a/tests/unit/health/test_function_hotspot.py b/tests/unit/health/test_function_hotspot.py
index 5cef8d496..578221af7 100644
--- a/tests/unit/health/test_function_hotspot.py
+++ b/tests/unit/health/test_function_hotspot.py
@@ -104,3 +104,34 @@ def test_severity_escalates_on_extreme_combined_axes():
assert len(findings) == 1
# mod_count >= 3*p80 (15) and ccn >= 20 → CRITICAL
assert findings[0].severity == Severity.CRITICAL
+
+
+def _changed(n: int) -> BlameIndex:
+ return _blame({(1, 30): [f"sha{i:040d}" for i in range(n)]})
+
+
+def test_a_thin_window_needs_five_changes():
+ # A shallow clone's p80 is 1: two changes used to read as HIGH. A function
+ # changed often inside the window still is a hotspot.
+ fc = _fc("hot", 1, 30, ccn=25, nesting=6)
+ assert FunctionHotspotDetector().detect(_ctx(fc, _changed(2), p80=1)) == []
+ found = FunctionHotspotDetector().detect(_ctx(fc, _changed(20), p80=1))
+ assert [f.severity for f in found] == [Severity.CRITICAL]
+
+
+def test_four_changes_never_make_a_hotspot():
+ fc = _fc("busy", 1, 30, ccn=12, nesting=3)
+ assert FunctionHotspotDetector().detect(_ctx(fc, _changed(4), p80=3)) == []
+ found = FunctionHotspotDetector().detect(_ctx(fc, _changed(5), p80=3))
+ assert [f.severity for f in found] == [Severity.MEDIUM]
+
+
+def test_severity_scales_from_the_floor_not_a_small_p80():
+ # 2 * p80 would be HIGH at 6 changes; the bar is 5, so HIGH needs 10.
+ fc = _fc("busy", 1, 30, ccn=12, nesting=3)
+ assert FunctionHotspotDetector().detect(_ctx(fc, _changed(6), p80=3))[0].severity == (
+ Severity.MEDIUM
+ )
+ assert FunctionHotspotDetector().detect(_ctx(fc, _changed(10), p80=3))[0].severity == (
+ Severity.HIGH
+ )