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.

feat(health): rank a critical code-shape marker as high in Fix first (#4199)

Raghav Chamadiya6426de50ca81
diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/finding_units.py b/packages/core/src/repowise/core/analysis/health/fix_first/finding_units.py
index 8ab4b6c2b..dc34e5ad2 100644
--- a/packages/core/src/repowise/core/analysis/health/fix_first/finding_units.py
+++ b/packages/core/src/repowise/core/analysis/health/fix_first/finding_units.py
@@ -6,7 +6,7 @@ from collections.abc import Mapping
from typing import Any
from repowise.core.analysis.health.rows import detail_map, field
-from repowise.core.analysis.health.scoring import biomarker_dimension
+from repowise.core.analysis.health.scoring import biomarker_dimension, ranked_severity
from repowise.core.analysis.health.suggestions import suggestion_for
from repowise.core.analysis.health.worth import (
SIZE_MARKERS,
@@ -97,7 +97,7 @@ def _finding_unit(lead: Any, files: _Files, first: FixStep, validate: Validate |
[
FixFact("finding", text.marker_label(marker)),
*_size_facts(rank.shape),
- FixFact("severity", _severity(field(lead, "severity"), rank.low)),
+ FixFact("severity", _severity(marker, field(lead, "severity"), rank.low)),
*files.common_facts(path, function=function is not None),
][:MAX_FACTS]
),
@@ -210,7 +210,8 @@ def _file_findings_call(path: str) -> ActionCommand:
)
-def _severity(severity: str | None, low: str | None) -> str:
- """The detector's severity; on a later item it says the tier overrides it."""
- severity = severity or "unknown"
- return severity if low is None else f"{severity} by the detector; lower priority by shape"
+def _severity(marker: str, severity: str | None, low: str | None) -> str:
+ """The severity as work is ranked (a code-shape marker tops out at high);
+ on a later item it says the tier overrides it."""
+ severity = ranked_severity(marker, severity) or "unknown"
+ return severity if low is None else f"{severity}; lower priority by shape"
diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/model.py b/packages/core/src/repowise/core/analysis/health/fix_first/model.py
index 6b9263de5..78d997efc 100644
--- a/packages/core/src/repowise/core/analysis/health/fix_first/model.py
+++ b/packages/core/src/repowise/core/analysis/health/fix_first/model.py
@@ -15,7 +15,7 @@ from repowise.core.analysis.health.queue.order import DUE_TIERS, Tier
from repowise.core.analysis.next_call import ActionCommand
#: Bumped when ranking, eligibility or the item shape changes meaning.
-FIX_FIRST_MODEL_VERSION = 2
+FIX_FIRST_MODEL_VERSION = 3
FixTier = Tier
FIX_TIERS: tuple[str, ...] = get_args(FixTier)
diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/refactor_units.py b/packages/core/src/repowise/core/analysis/health/fix_first/refactor_units.py
index 7158c376a..aec33601d 100644
--- a/packages/core/src/repowise/core/analysis/health/fix_first/refactor_units.py
+++ b/packages/core/src/repowise/core/analysis/health/fix_first/refactor_units.py
@@ -181,10 +181,12 @@ def _refactor_fields(plan: _RefactorPlan) -> dict[str, Any]:
symbol = plan.sym if lead.get("target_symbol") else None
return {
"title": text.clip(_refactor_title(plan, lead_plan)),
+ # The symbol's own span, as a finding's target gives it; where the
+ # first edit lands is the first step's line.
"target": FixTarget(
path,
symbol,
- fix_steps[0].line,
+ lead.get("line_start") or fix_steps[0].line,
lead.get("line_end"),
),
"why": files.why(
diff --git a/packages/core/src/repowise/core/analysis/health/scoring.py b/packages/core/src/repowise/core/analysis/health/scoring.py
index b85b62f24..62ec555d9 100644
--- a/packages/core/src/repowise/core/analysis/health/scoring.py
+++ b/packages/core/src/repowise/core/analysis/health/scoring.py
@@ -590,6 +590,36 @@ def biomarker_dimension(name: str) -> str:
return "defect"
+#: Categories whose markers read the code's shape: what a refactor changes.
+CODE_SHAPE_CATEGORIES: frozenset[str] = frozenset(
+ {"structural_complexity", "size_and_complexity", "duplication", "error_handling"}
+)
+
+#: The most severe a code-shape marker reads where work is ranked or listed.
+SHAPE_SEVERITY_CAP = "high"
+
+
+def is_code_shape(name: str) -> bool:
+ """True for a scored marker a refactor fixes: not history, coverage,
+ performance or advisory."""
+ return biomarker_dimension(name) in ("defect", "maintainability") and (
+ biomarker_category(name) in CODE_SHAPE_CATEGORIES
+ )
+
+
+def ranked_severity(name: str, severity: str | None) -> str | None:
+ """*severity* as work is ranked and shown: a critical code-shape marker
+ reads :data:`SHAPE_SEVERITY_CAP`.
+
+ Detectors tag a long method critical, and an active repository writes many
+ a week; ranked as critical they would outrank a defect. The stored severity
+ keeps its calibrated deduction (``_SEVERITY_DEDUCTION``).
+ """
+ if severity == "critical" and is_code_shape(name):
+ return SHAPE_SEVERITY_CAP
+ return severity
+
+
def is_advisory(name: str) -> bool:
"""True when *name* is a non-scoring marker (see ``ADVISORY_DIMENSION``)."""
return name in _ADVISORY_HOME
diff --git a/packages/core/src/repowise/core/analysis/health/worth.py b/packages/core/src/repowise/core/analysis/health/worth.py
index e20ae3956..7f2d823cf 100644
--- a/packages/core/src/repowise/core/analysis/health/worth.py
+++ b/packages/core/src/repowise/core/analysis/health/worth.py
@@ -18,6 +18,7 @@ from collections.abc import Callable, Iterable, Mapping, Sequence
from typing import Any, Literal
from .rows import detail_map, field
+from .scoring import ranked_severity
#: Problem-size cut points. Each is a multiple of the detector's own bar: CCN
#: 20 is twice the complex_method bar and 40, 80 and 150 double on from it;
@@ -120,7 +121,8 @@ def measure(
) -> dict[str, int]:
"""A function's largest measured CCN, size and nesting across its findings.
- ``severe`` is set when any of them is critical or a brain method,
+ ``severe`` is set when any of them is critical as work is ranked
+ (``scoring.ranked_severity``: never a code-shape marker) or a brain method,
``deprecated`` when any sits in a function marked deprecated,
``gated_off`` when any sits in a :func:`dormant` function,
``dispatch_pct`` is the largest stored dispatch share in percent,
@@ -131,7 +133,8 @@ def measure(
"""
shape: dict[str, int] = {}
for f in findings:
- if field(f, "severity") == "critical" or field(f, "biomarker_type") == "brain_method":
+ marker = field(f, "biomarker_type") or ""
+ if ranked_severity(marker, field(f, "severity")) == "critical" or marker == "brain_method":
shape["severe"] = 1
details = details_of(f)
if details.get("deprecated"):
diff --git a/tests/unit/cli/test_health_cmd_controls.py b/tests/unit/cli/test_health_cmd_controls.py
index 2dc3165ea..d1deccc68 100644
--- a/tests/unit/cli/test_health_cmd_controls.py
+++ b/tests/unit/cli/test_health_cmd_controls.py
@@ -130,7 +130,7 @@ def test_the_report_leads_with_fix_first() -> None:
# The growing, reachable database loop leads; the refactor follows.
assert " 1. Batch the database calls loops make through load_all " in out
assert " 2. Start breaking up run (CCN 44, 50 lines): first lift lines 20-35 " in out
- assert "src/core.py:20" in out
+ assert "src/core.py:10" in out
assert "verify: pytest tests/test_core_0.py" in out
diff --git a/tests/unit/health/test_fix_first_build.py b/tests/unit/health/test_fix_first_build.py
index a6bd5af67..9b6600120 100644
--- a/tests/unit/health/test_fix_first_build.py
+++ b/tests/unit/health/test_fix_first_build.py
@@ -679,3 +679,13 @@ def test_history_matches_the_items_qualified_symbol() -> None:
# File-level rows and items keep everything; a bare name matches by name.
assert _about(None, "Engine.run") and _about("Other.run", None)
assert _about("run", "Engine.run") and not _about("walk", "Engine.run")
+
+
+def test_a_plan_targets_its_symbol_span_like_a_finding_does() -> None:
+ """The target is the function's own span for every kind; the first edit's
+ line belongs to the first step."""
+ by_kind = {i.kind: i for i in _build(limit=None).items}
+ refactor, finding = by_kind["refactor"], by_kind["finding"]
+ assert (refactor.target.line_start, refactor.target.line_end) == (10, 60)
+ assert refactor.action.steps[0].line == 20
+ assert (finding.target.line_start, finding.target.line_end) == (5, 40)
diff --git a/tests/unit/health/test_worth.py b/tests/unit/health/test_worth.py
index 498916199..171d93d20 100644
--- a/tests/unit/health/test_worth.py
+++ b/tests/unit/health/test_worth.py
@@ -151,7 +151,7 @@ def test_history_orders_within_a_tier_but_never_lifts_a_small_function() -> None
ranked = {f.factor: f.value for f in tidy.why_ranked}
assert "value" not in ranked and "value within later" in ranked
assert ranked["problem size"] == "0"
- assert ("severity", "high by the detector; lower priority by shape") in [
+ assert ("severity", "high; lower priority by shape") in [
(f.label, f.value) for f in tidy.facts
]
@@ -294,3 +294,19 @@ def test_the_default_queue_counts_an_unproven_cause_and_still_adds_up() -> None:
assert counts["total"] == 1
assert counts["excluded"]["unmeasured_cost"] == 1 and counts["excluded"]["test"] == 1
assert counts["total"] + sum(counts["excluded"].values()) == len(rows)
+
+
+def test_a_critical_code_shape_marker_ranks_as_high() -> None:
+ """A detector-critical long method is not a reason to lift its value band;
+ a brain method still is a size claim, and coverage keeps its severity."""
+ from repowise.core.analysis.health.scoring import ranked_severity
+ from repowise.core.analysis.health.worth import measure
+
+ assert ranked_severity("complex_method", "critical") == "high"
+ assert ranked_severity("large_method", "high") == "high"
+ assert ranked_severity("coverage_gap", "critical") == "critical"
+ assert ranked_severity("change_entropy", "critical") == "critical"
+ assert ranked_severity("io_in_loop", "critical") == "critical"
+ row = {"biomarker_type": "complex_method", "severity": "critical", "details": {"ccn": 12}}
+ assert "severe" not in measure([row])
+ assert measure([row, {**row, "biomarker_type": "brain_method"}])["severe"] == 1