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): brain_method reports judged CCN, not raw, in reason and details (#4184)

Masyarakat AI0e98c41f8ea1
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/brain_method.py b/packages/core/src/repowise/core/analysis/health/biomarkers/brain_method.py
index 53587e32f..2e4ed41f4 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/brain_method.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/brain_method.py
@@ -71,9 +71,17 @@ class BrainMethodDetector:
out: list[BiomarkerResult] = []
for fn in ctx.all_functions:
- severity = self.severity_for(judged_ccn(fn), fn.nloc)
+ judged = judged_ccn(fn)
+ severity = self.severity_for(judged, fn.nloc)
if severity is None:
continue
+ details = {"ccn": fn.ccn, "nloc": fn.nloc, "max_nesting": fn.max_nesting}
+ # Severity was judged on the dispatch-flat number, so the reason
+ # leads with it — the raw one stays in ``details["ccn"]``.
+ reason = f"Brain Method: {fn.name} is {fn.nloc} lines, CCN {judged}"
+ if judged != fn.ccn:
+ details["judged_ccn"] = judged
+ reason += f" ({fn.ccn} counting every arm of its dispatch)"
out.append(
BiomarkerResult(
biomarker_type=self.name,
@@ -82,17 +90,12 @@ class BrainMethodDetector:
line_start=fn.start_line,
line_end=fn.end_line,
details={
- "ccn": fn.ccn,
- "nloc": fn.nloc,
- "max_nesting": fn.max_nesting,
+ **details,
"dependents_count": ctx.dependents_count,
"centrality_floor": floor,
**shape_details(fn),
},
- reason=(
- f"Brain Method: {fn.name} is {fn.nloc} lines, CCN {fn.ccn}, "
- f"in a file imported by {ctx.dependents_count} others"
- ),
+ reason=f"{reason}, in a file imported by {ctx.dependents_count} others",
)
)
return out
diff --git a/tests/unit/health/test_method_size_accuracy.py b/tests/unit/health/test_method_size_accuracy.py
index f5d7df759..a2d977e1c 100644
--- a/tests/unit/health/test_method_size_accuracy.py
+++ b/tests/unit/health/test_method_size_accuracy.py
@@ -2,7 +2,10 @@
from __future__ import annotations
+from dataclasses import replace
+
from repowise.core.analysis.health.biomarkers import FileContext
+from repowise.core.analysis.health.biomarkers.brain_method import BrainMethodDetector
from repowise.core.analysis.health.biomarkers.complex_method import ComplexMethodDetector
from repowise.core.analysis.health.biomarkers.large_method import LargeMethodDetector
from repowise.core.analysis.health.complexity.models import FunctionComplexity
@@ -52,6 +55,32 @@ def test_complex_method_reason_is_unchanged_without_a_dominant_dispatch() -> Non
assert "judged_ccn" not in finding.details
+def _central(*functions: FunctionComplexity) -> FileContext:
+ """A file that clears brain_method's centrality gate (floor defaults to 8)."""
+ return replace(_ctx(*functions), dependents_count=10)
+
+
+def test_brain_method_prints_the_judged_ccn_beside_the_raw_one() -> None:
+ # The severity is graded on judged_ccn(fn), so the reason quotes it too.
+ fn = _fn(ccn=30, dispatch_share=0.6, dispatch_arm=2)
+ [finding] = BrainMethodDetector().detect(_central(fn))
+ assert finding.severity == "high"
+ assert finding.reason == (
+ "Brain Method: tokenize is 80 lines, CCN 15 "
+ "(30 counting every arm of its dispatch), in a file imported by 10 others"
+ )
+ assert finding.details["ccn"] == 30
+ assert finding.details["judged_ccn"] == 15
+
+
+def test_brain_method_reason_is_unchanged_without_a_dominant_dispatch() -> None:
+ [finding] = BrainMethodDetector().detect(_central(_fn(ccn=12)))
+ assert finding.reason == (
+ "Brain Method: tokenize is 80 lines, CCN 12, in a file imported by 10 others"
+ )
+ assert "judged_ccn" not in finding.details
+
+
_ROWS = "\n".join(
f' <li className="row-{i}">\n <span>Row {i}</span>\n </li>'
for i in range(25)