Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(health): let a function worth breaking up lead its file (#3526)
Raghav Chamadiya44d5304b1d29
diff --git a/packages/core/src/repowise/core/analysis/health/models.py b/packages/core/src/repowise/core/analysis/health/models.pyindex 563de1b65..636524cad 100644--- a/packages/core/src/repowise/core/analysis/health/models.py+++ b/packages/core/src/repowise/core/analysis/health/models.py@@ -90,13 +90,20 @@ def primary_finding(findings: Sequence[Any]) -> Any | None:discrete = [item for item in candidates if row_field(item, "biomarker_type") not in continuous]+ # A function worth breaking up leads its file, the largest one first: worth+ # is complexity removed, and impact cannot say so because a file's+ # deduction is shared out. A CCN 279 brain method scored 0.37 and lost the+ # lead to one 7-operator condition inside it at 0.98.+ pool = discrete or candidates+ break_up = _break_up_ccn(pool)# A total order: equal impacts are common (two biomarkers on one file can# quantise to the same deduction), and a bare ``max`` let input order pick# the lead, so two indexes of one tree disagreed. Severity breaks the tie# first; the rest only makes it stable.return min(- discrete or candidates,+ pool,key=lambda item: (+ -break_up.get(id(item), -1),-float(row_field(item, "health_impact") or 0.0),_severity_rank(row_field(item, "severity")),str(row_field(item, "biomarker_type") or ""),@@ -106,6 +113,32 @@ def primary_finding(findings: Sequence[Any]) -> Any | None:)+def _break_up_ccn(findings: Sequence[Any]) -> dict[int, int]:+ """The measured CCN of each size finding, by ``id``, whose function is worth+ breaking up first by the shared rule (:func:`worth.low_priority`, at the+ break-up bar). A function a constant-false flag switches off is left out."""+ from .rows import field as row_field+ from .worth import (+ SIZE_MARKERS,+ WORTH_MAGNITUDE,+ by_function,+ dormant,+ low_priority,+ measure,+ worth_size,+ )++ sized = [f for f in findings if row_field(f, "biomarker_type") in SIZE_MARKERS]+ out: dict[int, int] = {}+ for rows in by_function(sized).values():+ shape = measure(rows)+ if dormant(shape) or worth_size(shape) < WORTH_MAGNITUDE:+ continue+ if low_priority(row_field(rows[0], "biomarker_type"), shape) is None:+ out.update(dict.fromkeys((id(f) for f in rows), shape.get("ccn", 0)))+ return out++def _severity_rank(severity: Any) -> int:"""Position in ``SEVERITY_ORDER`` (critical first); unknown sorts last."""from .aggregation import SEVERITY_ORDERdiff --git a/tests/unit/health/test_primary_and_magnitude.py b/tests/unit/health/test_primary_and_magnitude.pyindex 97e0789a0..8921d46c3 100644--- a/tests/unit/health/test_primary_and_magnitude.py+++ b/tests/unit/health/test_primary_and_magnitude.py@@ -107,3 +107,30 @@ def test_primary_finding_tie_does_not_depend_on_input_order() -> None:severe = _f("f.java", "low_cohesion", 0.871, "lcom 0.9", severity="high")assert primary_finding([complex_, severe]) is severeassert primary_finding([severe, complex_]) is severe+++def test_primary_finding_lets_a_function_worth_breaking_up_lead() -> None:+ """A file's deduction is shared out, so a CCN 279 method scored less than+ one long condition inside it. The function worth breaking up leads, the+ largest first; a short function's size finding still competes on impact."""+ from repowise.core.analysis.health.models import primary_finding++ def sized(marker, function, impact, **details):+ row = _f("g.py", marker, impact)+ row.function_name, row.details = function, details+ return row++ condition = sized("complex_conditional", "run", 0.98, operator_count=7)+ brain = sized("brain_method", "run", 0.37, ccn=279, nloc=1354, max_nesting=5)+ medium = sized("complex_method", "helper", 0.5, ccn=45, nloc=120, max_nesting=3)+ small = sized("complex_method", "tiny", 0.2, ccn=12, nloc=20)+ assert primary_finding([condition, small, medium, brain]) is brain+ assert primary_finding([condition, small, medium]) is medium+ assert primary_finding([condition, small]) is condition++ # One dispatch on one value can wait, so it does not take the lead.+ dispatch = sized("complex_method", "route", 0.3, ccn=45, nloc=120, dispatch_share=0.9)+ assert primary_finding([condition, dispatch]) is condition+ # Nor does a function a constant-false flag switches off.+ gated = sized("brain_method", "off", 0.3, ccn=279, nloc=1354, gated_off=True)+ assert primary_finding([condition, gated]) is condition