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): surface untested_complexity, measured findings by default (#4330)

Raghav Chamadiya65b6d0f0bf7d
diff --git a/docs/agent/MCP_TOOLS.md b/docs/agent/MCP_TOOLS.md
index f5b3dd132..ce56ab39a 100644
--- a/docs/agent/MCP_TOOLS.md
+++ b/docs/agent/MCP_TOOLS.md
@@ -329,7 +329,7 @@ Code-health scores and findings from the stored analysis, across defect risk, ma
| Parameter | Type | Default | Meaning |
|-----------|------|---------|---------|
| `targets` | list[string] | none (dashboard) | File paths or `module:<name>`. Misses are named in `unresolved` |
-| `include` | list[string] | none | Blocks: `biomarkers`, `refactoring`, `trend`, `coverage`, `accuracy`, `signals`, `churn_complexity`, `doc_drift`, `semantics`, `unverified`, `recipe` (with `plan_id`), `what_if`. Dimension filters: `performance`, `defect`, `maintainability`, `advisory` |
+| `include` | list[string] | none | Blocks: `biomarkers`, `refactoring`, `trend`, `coverage`, `accuracy`, `signals`, `churn_complexity`, `untested_complexity`, `doc_drift`, `semantics`, `unverified`, `recipe` (with `plan_id`), `what_if`. Dimension filters: `performance`, `defect`, `maintainability`, `advisory` |
| `only` | list[string] | none | Keep just these top-level keys; identity, totals and recovery fields always survive. An unknown key is named in `unknown_only_keys` with `unknown_only_keys_hint` |
| `limit` | int | `20` | Max rows in every ranked list, capped at 50; `0` for none. `fix_first` shows five unless `limit` is passed with `only=["fix_first"]`, which pages up to 25; `refactoring_plans` up to 25 |
| `cursor` | int | `0` | Offset into a ranked list (and into `fix_first` when named in `only`); the `recovery` block names the next call |
diff --git a/docs/architecture/code-health.md b/docs/architecture/code-health.md
index 3817203e4..391705773 100644
--- a/docs/architecture/code-health.md
+++ b/docs/architecture/code-health.md
@@ -113,6 +113,7 @@ analysis/health/
├── untested_hotspot.py
├── coverage_gap.py
├── coverage_gradient.py
+ ├── untested_complexity.py
├── developer_congestion.py
├── knowledge_loss.py
├── hidden_coupling.py
@@ -302,17 +303,17 @@ returning a list of `BiomarkerResult`s.
### The full roster
-`biomarkers/registry.py` registers **53 detectors**; counting the three
+`biomarkers/registry.py` registers **54 detectors**; counting the three
governance findings written by the additive pass (`governance.py`) there
-are **56 marker ids**. They divide by what each is permitted to affect:
+are **57 marker ids**. They divide by what each is permitted to affect:
| Group | Count | Scores into |
|---|---:|---|
-| Defect-scoring | 25 | `defect` (11 of them also `maintainability`) |
+| Defect-scoring | 24 | `defect` (11 of them also `maintainability`) |
| Performance | 22 | `performance` only |
| SQL | 3 | `maintainability` only |
| Governance | 3 | nothing: the finding surfaces, the score is untouched |
-| Advisory | 3 | nothing: measured by construction, kept out of impact-ranked lists unless requested |
+| Advisory | 5 | nothing: measured by construction, kept out of impact-ranked lists unless requested |
The authority is `scoring._BIOMARKER_DIMENSIONS`. Any biomarker **not** listed
there defaults into `defect`, which is why every `sql_*` and every performance
@@ -325,7 +326,7 @@ golden guarantee (§6).
|------------------------|------|------------|
| Organizational | −3.5‡ | developer_congestion, knowledge_loss, function_hotspot, code_age_volatility, ownership_risk, churn_risk, change_entropy, co_change_scatter, prior_defect, ungoverned_hotspot†, stale_governance†, contradictory_decision† |
| Structural complexity | −2.5 | low_cohesion, god_class, nested_complexity, repeated_nesting, complex_conditional |
-| Test coverage | −2.0 | untested_hotspot, coverage_gap |
+| Test coverage | −2.0 | untested_hotspot, coverage_gap, untested_complexity (advisory) |
| Test coverage gradient | −2.0 | coverage_gradient |
| Size & complexity | −1.5 | complex_method, large_method, primitive_obsession |
| Duplication | −1.0 | dry_violation |
diff --git a/docs/layers/CODE_HEALTH.md b/docs/layers/CODE_HEALTH.md
index ec2d99f3b..1ff4e87f2 100644
--- a/docs/layers/CODE_HEALTH.md
+++ b/docs/layers/CODE_HEALTH.md
@@ -206,12 +206,12 @@ dominating a file.
| Duplication | 1 | 1.0 | Cloned blocks |
| Error handling | 1 | 0.5 | Swallowed or catch-all handlers, `unwrap`/panic, discarded Go errors |
| Change history | 10 | 1.0 to 3.5 | Churn, change entropy, co-change scatter, ownership, fading ownership, prior bug fixes |
-| Test coverage | 3 | 2.0 + 2.0 | Untested hotspots, coverage gaps, uncovered lines (needs a coverage report) |
+| Test coverage | 4 | 2.0 + 2.0 | Untested hotspots, coverage gaps, uncovered lines (needs a coverage report), untested complex functions (advisory) |
| Test quality | 4 | 0.5 | Oversized or duplicated assertion blocks; two advisory markers |
| SQL | 4 | none | Complex SQL, `SELECT *`, `UPDATE`/`DELETE` with no `WHERE`, cartesian joins; maintainability or performance only |
| Performance | 21 | none | See [PERFORMANCE.md](PERFORMANCE.md); performance only |
-Twenty-five detectors move code health. Three markers are advisory: they are
+Twenty-four detectors move code health. Five markers are advisory: they are
listed, carry zero impact, and stay out of impact-ranked lists unless you ask for
the advisory dimension. Three governance findings (an ungoverned hotspot, stale
governance, a contradictory decision) come from the
@@ -232,6 +232,7 @@ same burst of activity.
| `developer_congestion` | 5 or more contributors, top quarter of churn, and nobody owns half |
| `assertion_free_test` | Advisory. A Python or TypeScript/JavaScript test that checks nothing |
| `mock_saturated_test` | Advisory. Many mock setup statements per assertion (same languages) |
+| `untested_complexity` | Advisory. A function's CRAP score (complexity weighted by how little of it tests run) is 30 or more |
On a small team, `knowledge_loss` and `ownership_risk` are capped at low severity
unless the file is a hotspot, and the `small-team` profile lowers them further.
@@ -251,7 +252,12 @@ git-history half moved, so there is nothing in the code to act on).
Coverage reports (LCOV, Cobertura, Clover, JaCoCo, Go coverprofile, Repowise JSON)
feed the coverage detectors: `repowise coverage add coverage.lcov`. With no report,
-only `untested_hotspot` runs, and no file is treated as uncovered. After adding a
+only `untested_hotspot` runs, and no file is treated as uncovered. `untested_complexity`
+scores each function as `CC² × (1 − coverage)³ + CC`: medium from 30, and high
+from 50 or critical from 100 when a report measured the function. Without a
+measurement its coverage is a tier (a test calls it, tests reach it, or neither),
+never a percentage. Only measured findings are listed by default; estimated ones
+appear when you ask for the type by name or opt into unverified findings. After adding a
report, run `repowise health --recompute`, which analyzes the working tree
instead of reading the stored analysis and, on a whole-repo table run, writes the
result to the index. `repowise health --badge` prints README badge Markdown.
diff --git a/docs/layers/TEST_INTELLIGENCE.md b/docs/layers/TEST_INTELLIGENCE.md
index bfd7f4626..0c859238e 100644
--- a/docs/layers/TEST_INTELLIGENCE.md
+++ b/docs/layers/TEST_INTELLIGENCE.md
@@ -173,7 +173,11 @@ a test file.
Severity is `CRITICAL` at 15% coverage or less with 10+ dependents, `HIGH` at one of
those two, `MEDIUM` otherwise. `coverage_gap` covers thin-but-present coverage, and
-`coverage_gradient` deducts in proportion to the uncovered share. The graph check
+`coverage_gradient` deducts in proportion to the uncovered share. Per function,
+`untested_complexity` reads the CRAP score: complexity weighted by the share of
+the function a report measured as run, or by its reach tier when no report covers
+it. A tier never becomes a percentage, and only measured findings are listed by
+default. The graph check
matters because a filename convention misses tests in both directions
([examples](../architecture/test-intelligence.md#why-a-graph-signal)).
diff --git a/docs/reference/MCP_RESPONSE_FIELDS.md b/docs/reference/MCP_RESPONSE_FIELDS.md
index f0a89bae2..85852a45d 100644
--- a/docs/reference/MCP_RESPONSE_FIELDS.md
+++ b/docs/reference/MCP_RESPONSE_FIELDS.md
@@ -385,7 +385,7 @@ Items are ordered by value first, then tier. History markers (churn, ownership,
| `top_findings` / `findings`, `test_findings` | Production and test findings; their totals sum to the whole open set |
| metric row | `score` (defect dimension and headline), `maintainability_score`, `performance_score`, `weighted_deficit` = (8 - score) x NLOC, `is_test`, `has_test_file`, `primary_biomarker`, `total_deduction`, `structure_deduction` and `history_deduction` (the score is 10 minus both, before the 1.0 floor; history reads 0.0 under `counts="code_shape"`), `history: "short"` only when the file has fewer than 5 recorded commits (new, split out, or renamed without follow), where most history markers cannot fire yet, and `withheld_deduction` when part of it comes from a withheld finding type |
-`weighted_deficit` and the gap fields share one unit, score points x NLOC; they compare only with each other. `primary_biomarker` prefers a discrete finding over the continuous `coverage_gradient`. Each finding carries a `dimension`; `advisory` findings (`assertion_free_test`, `mock_saturated_test`) never deduct and appear only with `include=["advisory"]`.
+`weighted_deficit` and the gap fields share one unit, score points x NLOC; they compare only with each other. `primary_biomarker` prefers a discrete finding over the continuous `coverage_gradient`. Each finding carries a `dimension`; `advisory` findings (`assertion_free_test`, `mock_saturated_test`, `hidden_coupling`, `brain_method`, `untested_complexity`) never deduct and appear only with `include=["advisory"]`.
Provisional finding types appear only with `include=["unverified"]`, each marked `verification: "unverified"`. A withheld type never lists but still lowers the score it was counted in, so a file row reports it as `withheld_deduction` and `_meta.withheld` names the type, its count and why it is held back. Targeted mode is never split into production and test.
@@ -406,6 +406,7 @@ On test files, history markers (ownership, churn, change entropy, co-change and
| `accuracy` | `defect_accuracy`: of the K least-healthy files, how many were recently bug-fixed versus the base rate (precision@K, `lift`). `null` on repos with too little history |
| `signals` | Targeted mode: per-file prior defects, change scatter, 90-day churn, owners, graph degree. `null` per missing field, never an imputed zero |
| `churn_complexity` | One point per recently changed file: commits, max CCN, NLOC, score, churn percentile |
+| `untested_complexity` | Functions ranked by CRAP, worst first: `crap`, `ccn`, `coverage_basis`, then `coverage_pct` when measured or `coverage_tier` (`direct`, `reached`, `none`) when estimated, never both; estimated rows only with `include=["unverified"]` |
| `doc_drift` | `findings` naming the document to edit, its line, the `target` it wrongly claims, `reason`, `kind`, `confidence`, optional `suggestion`; plus `findings_total`, `documents`, `basis`. A finding is evidence to check, not a proven defect |
| `semantics` | `_meta.health_semantics`: the legend for deficit points and percentiles |
| `what_if` | Up to 10 worst files (or the targets), each with `ceiling` (the score with no structure deduction left, history's cap shrinking with it: the most an edit can reach), `target_out_of_reach` when that is below 8.0, `fix_all_structure` gains and its top 3 `findings`; `top_fixes` ranks single fixes across them. Each fix has `points` (its impact, an upper bound when its category is at its cap) and `gain`: `file`, `average` (headline points) and `hotspot` (when the file is a hotspot). `structure_deduction` can include withheld finding types no listed fix accounts for |
diff --git a/packages/core/src/repowise/core/analysis/actions/build.py b/packages/core/src/repowise/core/analysis/actions/build.py
index bb702969a..316fc7e1a 100644
--- a/packages/core/src/repowise/core/analysis/actions/build.py
+++ b/packages/core/src/repowise/core/analysis/actions/build.py
@@ -280,8 +280,9 @@ def build_authors(rows: Rows, *, since: datetime | None) -> dict[str, Any]:
#: Markers that read a measured coverage report, so "worsened" is coverage the
#: change lost. ``untested_hotspot`` is left out: without a report it fires on a
-#: missing test file, which no commit's lines decide.
-COVERAGE_MARKERS = frozenset({"coverage_gap", "coverage_gradient"})
+#: missing test file, which no commit's lines decide. ``untested_complexity``
+#: reaches the serious severities only on measured coverage.
+COVERAGE_MARKERS = frozenset({"coverage_gap", "coverage_gradient", "untested_complexity"})
#: Commit-health rows a loader reads: the authored kinds, at a severity that
#: counts, or a performance marker of any severity (its proof is the cause's
diff --git a/packages/core/src/repowise/core/analysis/change_health/matcher.py b/packages/core/src/repowise/core/analysis/change_health/matcher.py
index 2819f2e27..714ec8d64 100644
--- a/packages/core/src/repowise/core/analysis/change_health/matcher.py
+++ b/packages/core/src/repowise/core/analysis/change_health/matcher.py
@@ -8,12 +8,15 @@ from dataclasses import dataclass, field
from ..changed_lines import map_old_line
from ..health import HealthFindingData
from ..health.aggregation import finding_raw_deduction
+from ..health.crap import measured_crap
from .identity import finding_key, measured_shape, normalize_path, severity_rank
from .models import ChangeKind, FindingKey
from .moves import MoveMap, SymbolKey
#: Health-impact movement below this is rounding, not a regression.
_IMPACT_EPSILON = 0.01
+#: Stored CRAP is rounded to one decimal; smaller moves are rounding.
+_CRAP_EPSILON = 0.5
#: Measured values a finding's details may carry, compared for "improved".
_METRICS = ("ccn", "nloc", "max_nesting", "params")
@@ -248,11 +251,23 @@ def _classify(
impact = finding_raw_deduction(head) - finding_raw_deduction(base)
if abs(impact) > _IMPACT_EPSILON:
return "worsened" if impact > 0 else "improved"
+ crap = _crap_change(base, head)
+ if crap:
+ return "worsened" if crap > 0 else "improved"
# Lower metrics within one band read as improved; higher ones stay quiet,
# since only a deduction change is a regression worth surfacing.
return "improved" if _metrics_dropped(base, head) else carried
+def _crap_change(base: HealthFindingData, head: HealthFindingData) -> float:
+ """How far a measured CRAP moved past rounding; 0 unless both sides measured it."""
+ before = measured_crap(base.biomarker_type, base.details)
+ after = measured_crap(head.biomarker_type, head.details)
+ if before is None or after is None or abs(after - before) < _CRAP_EPSILON:
+ return 0.0
+ return after - before
+
+
def _metrics_dropped(base: HealthFindingData, head: HealthFindingData) -> bool:
"""True when no shared metric rose and at least one fell."""
before, after = base.details or {}, head.details or {}
diff --git a/packages/core/src/repowise/core/analysis/change_health/service.py b/packages/core/src/repowise/core/analysis/change_health/service.py
index 4e6bbffc1..bef521096 100644
--- a/packages/core/src/repowise/core/analysis/change_health/service.py
+++ b/packages/core/src/repowise/core/analysis/change_health/service.py
@@ -21,6 +21,7 @@ from ...test_paths import is_test_related_path
from ..finding_registry import excluded_types, gate_for, is_served, language_label
from ..health import HEALTH_ANALYZER_VERSION, HealthFindingData
from ..health.biomarker_labels import biomarker_label
+from ..health.crap import measured_crap
from ..health.finding_identity import finding_public_id
from ..health.perf.causal import PERFORMANCE_MODEL_VERSION
from ..health.scoring import ADVISORY_DIMENSION, is_advisory
@@ -686,10 +687,13 @@ def _hunks(changes: list[FileChange]) -> dict[str, list[tuple[int, int, int, int
def _comparable(finding: HealthFindingData, withheld: frozenset[str]) -> bool:
- """A finding both sides of a comparison count: not advisory, not a test's
- performance finding, and one the registry serves."""
+ """A finding both sides of a comparison count: not advisory (save CRAP on
+ measured coverage), not a test's performance finding, and one the registry serves."""
return (
- not is_advisory(finding.biomarker_type)
+ (
+ not is_advisory(finding.biomarker_type)
+ or measured_crap(finding.biomarker_type, finding.details) is not None
+ )
and not _is_test_perf(finding)
and is_served("health", finding.biomarker_type, finding.file_path, withheld)
)
diff --git a/packages/core/src/repowise/core/analysis/finding_registry.py b/packages/core/src/repowise/core/analysis/finding_registry.py
index ccb305ff3..876c7f5fe 100644
--- a/packages/core/src/repowise/core/analysis/finding_registry.py
+++ b/packages/core/src/repowise/core/analysis/finding_registry.py
@@ -30,6 +30,7 @@ is served unlabelled.
from __future__ import annotations
+import json
from collections.abc import Callable, Iterable, Mapping
from dataclasses import dataclass
from functools import lru_cache
@@ -91,13 +92,6 @@ REGISTRY: dict[str, FindingTypeStatus] = {
measured_on="2026-09-29",
reason="Flags duplicated import blocks and data literals as design duplication.",
),
- # New and unmeasured. Measured CRAP is arithmetic over the report, but an
- # estimate rests on call-graph reach, whose recall is partial; flip to
- # validated once a labelled sample clears the floor.
- "untested_complexity": FindingTypeStatus(
- "provisional",
- reason="Not yet measured; estimated coverage rests on call-graph reach.",
- ),
}
@@ -216,6 +210,31 @@ FILE_HOLDS: dict[str, str] = {
"test": "Plans held back on test files: tests change with the code they cover.",
}
+#: Health rows held back by their own evidence rather than their type or file:
+#: an ``untested_complexity`` finding on estimated coverage rests on call-graph
+#: reach, whose recall is partial, so only measured ones are served by default.
+#: Same opt-in as a gate, or ask for the type by name.
+ROW_HOLDS: dict[str, str] = {
+ "estimated": "Held back: coverage estimated from call-graph reach, not measured.",
+}
+#: The types :data:`ROW_HOLDS` ``estimated`` applies to, and the detail it reads.
+ESTIMATE_HELD_TYPES: frozenset[str] = frozenset({"untested_complexity"})
+COVERAGE_BASIS_KEY = "coverage_basis"
+#: Every file and row hold with its reason, as a surface reports them.
+HOLD_REASONS: dict[str, str] = {**FILE_HOLDS, **ROW_HOLDS}
+
+
+def is_estimated(kind: Any, details: Any) -> bool:
+ """Whether a health row of *kind* rests on estimated coverage (see :data:`ROW_HOLDS`)."""
+ if kind not in ESTIMATE_HELD_TYPES:
+ return False
+ if isinstance(details, str):
+ try:
+ details = json.loads(details)
+ except ValueError:
+ return False
+ return isinstance(details, Mapping) and details.get(COVERAGE_BASIS_KEY) == "estimated"
+
#: The stored ``code_origin`` values a health finding is never served on.
HELD_ORIGINS: tuple[str, ...] = ("vendored", "generated")
@@ -412,8 +431,8 @@ def gated_summary(layer: GateLayer, counts: Mapping[str, int]) -> dict[str, dict
count = counts.get(gate.language, 0)
if count:
out[gate.language] = {"count": count, "reason": held_back_note([gate.language])}
- # File holds ride beside the languages under their own keys, with their reason.
- for key, reason in FILE_HOLDS.items():
+ # File and row holds ride beside the languages under their own keys, with their reason.
+ for key, reason in HOLD_REASONS.items():
if counts.get(key):
out[key] = {"count": counts[key], "reason": reason}
return out
@@ -429,7 +448,12 @@ def row_hold(
hold_history: bool = True,
) -> str | None:
"""The :data:`FILE_HOLDS` key on *row*, read off its file's metric row in
- *files* (``code_origin``, ``is_test``); a path missing from it is not held."""
+ *files* (``code_origin``, ``is_test``), or the :data:`ROW_HOLDS` key read off
+ the row itself; a path missing from *files* is not file-held."""
+ if layer == "health" and get(row, kind) in ESTIMATE_HELD_TYPES:
+ details = _row_details(row, get)
+ if is_estimated(get(row, kind), details):
+ return "estimated"
metric = (files or {}).get(get(row, "file_path") or "")
if metric is None:
return None
@@ -442,6 +466,18 @@ def row_hold(
)
+def _row_details(row: Any, get: Callable[[Any, str], Any]) -> Any:
+ """A row's details: a mapping, or the stored JSON text of an ORM row."""
+ for key in ("details", "details_json"):
+ try:
+ value = get(row, key)
+ except (AttributeError, KeyError):
+ continue
+ if value is not None:
+ return value
+ return None
+
+
def split_held(
layer: GateLayer,
rows: Iterable[Any],
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/README.md b/packages/core/src/repowise/core/analysis/health/biomarkers/README.md
index fa3bbfc40..d02cf9ee2 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/README.md
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/README.md
@@ -35,6 +35,10 @@ Duplication (cap −1.0):
Test coverage (cap −2.0):
- `untested_hotspot` — hotspot × low coverage × many dependents.
- `coverage_gap` — non-test files with meaningful uncovered surface.
+- `untested_complexity` — functions whose CRAP score (`CC² × (1 − cov)³ + CC`,
+ `health/crap.py`) is 30 or more, on measured coverage or a reach tier.
+ Advisory: filed here, deducts nothing, since complexity and coverage already
+ deduct through their own markers.
Test coverage — continuous (cap −2.0, own category `test_coverage_gradient`):
- `coverage_gradient` — a per-file deduction that scales **continuously** with
diff --git a/packages/core/src/repowise/core/analysis/health/crap.py b/packages/core/src/repowise/core/analysis/health/crap.py
index a8f744c76..62ac440b0 100644
--- a/packages/core/src/repowise/core/analysis/health/crap.py
+++ b/packages/core/src/repowise/core/analysis/health/crap.py
@@ -15,9 +15,9 @@ never leaves this module as a percentage.
from __future__ import annotations
from bisect import bisect_left, bisect_right
-from collections.abc import Collection, Sequence
+from collections.abc import Collection, Iterable, Mapping, Sequence
from dataclasses import dataclass
-from typing import Literal
+from typing import Any, Literal
#: The CRAP at which a function is worth a finding: an untested ``CC`` of 5.
CRAP_THRESHOLD: float = 30.0
@@ -109,3 +109,49 @@ def function_crap(ccn: int, measured: float | None, tier: CoverageTier) -> Funct
crap_score(ccn, measured), "measured", coverage_pct=round(measured * 100.0, 1)
)
return FunctionCrap(crap_score(ccn, _TIER_FRACTION[tier]), "estimated", tier=tier)
+
+
+def ranked_by_crap(findings: Iterable[Mapping[str, Any]]) -> list[dict[str, Any]]:
+ """``untested_complexity`` findings as a list ranked by CRAP, worst first.
+
+ Each row keeps its basis and either the measured percentage or the tier,
+ never both, so an estimate is never read as a percentage.
+ """
+ rows = []
+ for f in findings:
+ details = f.get("details") or {}
+ measured = details.get("coverage_basis") == "measured"
+ rows.append(
+ {
+ "id": f.get("id"),
+ "file_path": f.get("file_path"),
+ "function_name": f.get("function_name"),
+ "line_start": f.get("line_start"),
+ "severity": f.get("severity"),
+ "crap": details.get("crap"),
+ "ccn": details.get("judged_ccn", details.get("ccn")),
+ "coverage_basis": details.get("coverage_basis"),
+ **(
+ {"coverage_pct": details.get("coverage_pct")}
+ if measured
+ else {"coverage_tier": details.get("coverage_tier")}
+ ),
+ "action": details.get("action"),
+ }
+ )
+ rows.sort(key=lambda r: (-(r["crap"] or 0.0), r["file_path"] or "", r["line_start"] or 0))
+ return rows
+
+
+def measured_crap(biomarker_type: str, details: Mapping[str, Any] | None) -> float | None:
+ """The CRAP of an ``untested_complexity`` finding on measured coverage, else ``None``.
+
+ The one coverage reading a base-to-head comparison keeps: an estimate rests
+ on call-graph reach and never makes a finding introduced or worsened.
+ """
+ if biomarker_type != "untested_complexity" or not details:
+ return None
+ if details.get("coverage_basis") != "measured":
+ return None
+ crap = details.get("crap")
+ return float(crap) if isinstance(crap, int | float) else None
diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/text.py b/packages/core/src/repowise/core/analysis/health/fix_first/text.py
index 4b9ffec9d..05327768b 100644
--- a/packages/core/src/repowise/core/analysis/health/fix_first/text.py
+++ b/packages/core/src/repowise/core/analysis/health/fix_first/text.py
@@ -38,6 +38,7 @@ PROBLEM: dict[str, str] = {
"error_handling": "handles an exception in a way that can hide a failure",
"coverage_gap": "has low measured test coverage",
"untested_hotspot": "changes often and has low measured test coverage",
+ "untested_complexity": "is complex and little of it runs under test",
"function_hotspot": "is complex and keeps changing",
"split_file": "holds several loosely related parts in one file",
"break_cycle": "sits in an import cycle",
@@ -58,6 +59,7 @@ FINDING_TITLE: dict[str, str] = {
"error_handling": "Fix the exception handling in {where}",
"coverage_gap": "Add tests for {where}",
"untested_hotspot": "Add tests for {where}",
+ "untested_complexity": "Test the branches of {where}",
"function_hotspot": "Simplify {where}, which keeps changing",
}
diff --git a/packages/core/src/repowise/core/analysis/health_gate.py b/packages/core/src/repowise/core/analysis/health_gate.py
index b19723b1b..dad243ea9 100644
--- a/packages/core/src/repowise/core/analysis/health_gate.py
+++ b/packages/core/src/repowise/core/analysis/health_gate.py
@@ -227,15 +227,20 @@ def _level(f: ChangeFinding, fail_on: str, fail_at: str) -> str:
"""Error when the change wrote a code-shape finding at or above *fail_on* and *fail_at*.
A finding the change only carried (moved) or sits away from its lines
- (incidental) is a note, as is anything that is not code shape. Below
+ (incidental) is a note, as is anything that is not code shape save measured
+ CRAP, which warns. Below
*fail_at* a finding warns at most (by default medium: CCN 9 to 14, nesting
depth 4, just past a detector's threshold), and so does one in a test
file: long test functions are the change's own scaffolding more often than
a defect.
"""
authored = f.attribution_basis != "moved" and f.attribution_basis not in INCIDENTAL_BASES
- if not authored or not is_code_shape(f.biomarker_type) or f.change_kind not in _KIND_RANK:
+ if not authored or f.change_kind not in _KIND_RANK:
return NOTE
+ if not is_code_shape(f.biomarker_type):
+ # CRAP on measured coverage, the one coverage reading a comparison keeps
+ # (estimates never reach it): worth a warning, never a failure.
+ return WARNING if f.biomarker_type == "untested_complexity" else NOTE
if (
_KIND_RANK[f.change_kind] >= _KIND_RANK[fail_on]
and severity_rank(f.severity) >= severity_rank(fail_at)
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/shown.py b/packages/core/src/repowise/core/persistence/crud/analysis/shown.py
index 583c6435f..ff2184eb1 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/shown.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/shown.py
@@ -17,6 +17,8 @@ from sqlalchemy import and_, case, func, not_, or_, select, true
from sqlalchemy.ext.asyncio import AsyncSession
from ....analysis.finding_registry import (
+ COVERAGE_BASIS_KEY,
+ ESTIMATE_HELD_TYPES,
HELD_ORIGINS,
GateLayer,
excluded_types,
@@ -41,7 +43,12 @@ _HELD_MODELS: dict[str, tuple[Any, ...]] = {
def _file_holds(
- layer: GateLayer, path_col: Any, kind_col: Any, *, hold_history: bool = True
+ layer: GateLayer,
+ path_col: Any,
+ kind_col: Any,
+ *,
+ hold_history: bool = True,
+ requested: Iterable[str] = (),
) -> list[tuple[str, Any]]:
"""``(FILE_HOLDS key, row matches it)`` for health findings and plans, read
from the file's stored metric row; ahead of the language cells, as
@@ -72,9 +79,26 @@ def _file_holds(
# ``file_hold``'s ``is_test is not True``: an unknown flag is not a test.
not_test = HealthFileMetric.is_test.is_not(True)
cells.append(("history", and_(history, on_file(not_test))))
+ held = sorted(ESTIMATE_HELD_TYPES - set(requested))
+ if held:
+ cells.append(("estimated", _estimated(kind_col, model.details_json, held)))
return cells
+def _estimated(kind_col: Any, details_col: Any, kinds: list[str]) -> Any:
+ """Rows of *kinds* whose stored details say ``coverage_basis: estimated``
+ (``finding_registry.is_estimated``), as written with or without a space."""
+ return and_(
+ kind_col.in_(kinds),
+ or_(
+ *(
+ details_col.like(f'%"{COVERAGE_BASIS_KEY}":{gap}"estimated"%')
+ for gap in ("", " ")
+ )
+ ),
+ )
+
+
def _cells(
layer: GateLayer,
path_col: Any,
@@ -82,10 +106,13 @@ def _cells(
*,
file_holds: bool = True,
hold_history: bool = True,
+ requested: Iterable[str] = (),
) -> list[tuple[str, Any]]:
- """``(key, row matches it)`` for each file hold, then each gate on *layer*."""
+ """``(key, row matches it)`` for each file and row hold, then each gate on *layer*."""
cells = (
- _file_holds(layer, path_col, kind_col, hold_history=hold_history) if file_holds else []
+ _file_holds(layer, path_col, kind_col, hold_history=hold_history, requested=requested)
+ if file_holds
+ else []
)
for gate in gates_on(layer):
match = or_(*(path_col.ilike(f"%{ext}") for ext in gated_extensions(gate.language)))
@@ -103,15 +130,22 @@ def ungated(
include_unverified: bool = False,
file_holds: bool = True,
hold_history: bool = True,
+ requested: Iterable[str] = (),
) -> Any:
- """Rows of *layer* no language gate or file hold keeps back (everything when
- opted in). ``file_holds=False`` applies the language gates alone;
- ``hold_history=False`` keeps history markers on production files."""
+ """Rows of *layer* no language gate, file hold or row hold keeps back
+ (everything when opted in). ``file_holds=False`` applies the language gates
+ alone; ``hold_history=False`` keeps history markers on production files; a
+ type in *requested* is not row-held."""
cells = (
[]
if include_unverified
else _cells(
- layer, path_col, kind_col, file_holds=file_holds, hold_history=hold_history
+ layer,
+ path_col,
+ kind_col,
+ file_holds=file_holds,
+ hold_history=hold_history,
+ requested=requested,
)
)
return not_(or_(*(match for _, match in cells))) if cells else true()
@@ -144,6 +178,7 @@ def shown(
include_unverified=include_unverified,
file_holds=file_holds,
hold_history=hold_history,
+ requested=requested,
),
)
diff --git a/packages/server/src/repowise/server/mcp_server/_budget/contracts.py b/packages/server/src/repowise/server/mcp_server/_budget/contracts.py
index 13cc12d2a..6c6624be6 100644
--- a/packages/server/src/repowise/server/mcp_server/_budget/contracts.py
+++ b/packages/server/src/repowise/server/mcp_server/_budget/contracts.py
@@ -322,6 +322,7 @@ _CONTRACTS: dict[str, ResponseBudgetContract] = {
"trend.recent[]",
"trend.alerts[]",
"churn_complexity[]",
+ "untested_complexity[]",
"doc_drift.findings[]",
"test_findings[]",
"top_findings[]",
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/blocks.py b/packages/server/src/repowise/server/mcp_server/tool_health/blocks.py
index fb6c1b705..9e4beac62 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/blocks.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/blocks.py
@@ -52,6 +52,8 @@ def add_optional_blocks(
)
if "doc_drift" in include:
result["doc_drift"] = _doc_drift_block(data, pager)
+ if "untested_complexity" in include:
+ result["untested_complexity"] = pager.bound(data.crap_rows, "untested_complexity")
# The dimension filter (``include=["performance"]`` etc.) is applied where
# rows are selected: filtering here would filter a list already capped.
if "refactoring" in include and req.wants("suggestion_legend"):
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/findings.py b/packages/server/src/repowise/server/mcp_server/tool_health/findings.py
index 34e6f243f..3af47c8ee 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/findings.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/findings.py
@@ -8,13 +8,16 @@ from typing import Any
from sqlalchemy import select
from repowise.core.analysis.finding_registry import split_held
+from repowise.core.analysis.health.crap import ranked_by_crap
from repowise.core.analysis.health.worth import LOW_PRIORITY_LABEL, finding_priorities
from repowise.core.persistence.crud.analysis.plain_rows import plain_rows
from repowise.core.persistence.crud.analysis.shown import gated_counts, ungated
from repowise.core.persistence.models import HealthFinding
from repowise.server.mcp_server.tool_health.population import Population
from repowise.server.mcp_server.tool_health.request import HealthRequest
-from repowise.server.mcp_server.tool_health.serialize import _rank_emitted
+from repowise.server.mcp_server.tool_health.serialize import _rank_emitted, _serialize_finding
+
+UNTESTED_COMPLEXITY = "untested_complexity"
def _in_dimensions(row: Any, dimensions: set[str]) -> bool:
@@ -259,6 +262,33 @@ async def _read_findings_by_id(session: Any, ids: list[str]) -> dict[str, Any]:
}
+async def read_crap_rows(
+ session: Any, repository: Any, pop: Population, req: HealthRequest, reference: str
+) -> list[dict[str, Any]]:
+ """Open ``untested_complexity`` findings ranked by CRAP, for that include.
+
+ Measured ones only, unless the call also opts into unverified rows: an
+ estimate rests on call-graph reach (``finding_registry.ROW_HOLDS``).
+ """
+ if UNTESTED_COMPLEXITY not in req.include_set or pop.nothing_resolved:
+ return []
+ query = select(HealthFinding).where(
+ HealthFinding.repository_id == repository.id,
+ HealthFinding.status == "open",
+ HealthFinding.biomarker_type == UNTESTED_COMPLEXITY,
+ ungated(
+ "health",
+ HealthFinding.file_path,
+ HealthFinding.biomarker_type,
+ include_unverified=req.include_unverified,
+ ),
+ )
+ if pop.scoped:
+ query = query.where(HealthFinding.file_path.in_(pop.effective_targets))
+ rows = pop.in_scope_rows(list((await session.execute(query)).scalars().all()))
+ return ranked_by_crap(_serialize_finding(f, reference) for f in rows)
+
+
async def read_withheld_rows(
session: Any, repository: Any, pop: Population, req: HealthRequest
) -> list[Any]:
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/loading.py b/packages/server/src/repowise/server/mcp_server/tool_health/loading.py
index 4741efd41..a70e8a66f 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/loading.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/loading.py
@@ -57,6 +57,7 @@ from repowise.server.mcp_server.tool_health.findings import (
load_dashboard_findings,
load_targeted_findings,
read_accuracy_rows,
+ read_crap_rows,
read_withheld_rows,
)
from repowise.server.mcp_server.tool_health.pillars import (
@@ -104,6 +105,8 @@ class HealthData:
drift_rows: list[Any] = field(default_factory=list)
drift_unavailable: str | None = None
churn_points: list[dict[str, Any]] = field(default_factory=list)
+ #: ``untested_complexity`` findings ranked by CRAP, read only on that include.
+ crap_rows: list[dict[str, Any]] = field(default_factory=list)
snapshots: list[Any] = field(default_factory=list)
fix_first: FixFirstQueue | None = None
#: The whole queue the page was cut from: its lead and its counts.
@@ -190,6 +193,7 @@ async def load_health_data(
session, repository, pop, req
)
data.churn_points = await _read_churn_points(session, repository, pop, req)
+ data.crap_rows = await read_crap_rows(session, repository, pop, req, reference_repository)
data.snapshots = await _read_snapshots(session, repository, pop, req)
data.withheld_rows = await read_withheld_rows(session, repository, pop, req)
data.by_leverage, data.leads = _rank_leverage_and_leads(
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/paging.py b/packages/server/src/repowise/server/mcp_server/tool_health/paging.py
index 360c8c80d..d1717528c 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/paging.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/paging.py
@@ -26,6 +26,7 @@ _PAGED_COLLECTIONS = frozenset(
"test_findings",
"modules",
"churn_complexity",
+ "untested_complexity",
"coverage.files",
"doc_drift.findings",
"refactoring_plans",
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/request.py b/packages/server/src/repowise/server/mcp_server/tool_health/request.py
index 5fa0f9be6..b75418777 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/request.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/request.py
@@ -50,6 +50,8 @@ _KNOWN_INCLUDES = frozenset(
"accuracy",
"signals",
"churn_complexity",
+ # Functions ranked by CRAP; measured only unless ``unverified`` too.
+ "untested_complexity",
"doc_drift",
"performance",
"defect",
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/tool.py b/packages/server/src/repowise/server/mcp_server/tool_health/tool.py
index 22989a7f7..cbc5f9e39 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/tool.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/tool.py
@@ -14,7 +14,7 @@ from time import perf_counter
from typing import Any
from repowise.core.analysis.finding_registry import (
- FILE_HOLDS,
+ HOLD_REASONS,
GateLayer,
served_previews,
withheld_summary,
@@ -296,12 +296,12 @@ def _attach_gated(meta: dict[str, Any], data: HealthData) -> None:
# once under ``reasons`` and only when the call lists no finding, so a
# default response grows by a few tokens at most.
gated: dict[str, Any] = {
- layer: {k: v["count"] if k in FILE_HOLDS else v for k, v in cells.items()}
+ layer: {k: v["count"] if k in HOLD_REASONS else v for k, v in cells.items()}
for layer, cells in data.gated.items()
}
- held = sorted({k for cells in data.gated.values() for k in cells if k in FILE_HOLDS})
+ held = sorted({k for cells in data.gated.values() for k in cells if k in HOLD_REASONS})
if held and not data.findings.emitted:
- gated["reasons"] = {k: FILE_HOLDS[k] for k in held}
+ gated["reasons"] = {k: HOLD_REASONS[k] for k in held}
meta["gated"] = {**gated, "opt_in": {"include": ["unverified"]}}
@@ -412,6 +412,7 @@ def _finish(
"top_findings": findings.findings_total,
"test_findings": findings.test_findings_total,
"churn_complexity": len(data.churn_points),
+ "untested_complexity": len(data.crap_rows),
"refactoring_plans": (
data.refactoring_plans.total if data.refactoring_plans is not None else 0
),
diff --git a/packages/ui/__tests__/health/untested-complexity-details.test.tsx b/packages/ui/__tests__/health/untested-complexity-details.test.tsx
new file mode 100644
index 000000000..018db4a96
--- /dev/null
+++ b/packages/ui/__tests__/health/untested-complexity-details.test.tsx
@@ -0,0 +1,33 @@
+import { describe, it, expect } from "vitest";
+import { render, screen } from "@testing-library/react";
+import { BiomarkerDetails } from "../../src/health/biomarker-details.js";
+import { biomarkerInfo, ADVISORY_HOME_BIOMARKERS } from "../../src/health/biomarker-glossary.js";
+
+describe("untested_complexity details", () => {
+ it("shows a measured percentage", () => {
+ render(
+ <BiomarkerDetails
+ biomarkerType="untested_complexity"
+ details={{ crap: 56, ccn: 7, coverage_basis: "measured", coverage_pct: 0 }}
+ />,
+ );
+ expect(screen.getByText("CRAP 56 · complexity 7 · 0% covered")).toBeInTheDocument();
+ });
+
+ it("shows an estimate as its tier, never a percentage", () => {
+ render(
+ <BiomarkerDetails
+ biomarkerType="untested_complexity"
+ details={{ crap: 43, ccn: 12, coverage_basis: "estimated", coverage_tier: "reached", action: "add_tests" }}
+ />,
+ );
+ const line = screen.getByText(/CRAP 43/);
+ expect(line.textContent).toBe("CRAP 43 · complexity 12 · estimated: tests reach it");
+ expect(line.textContent).not.toContain("%");
+ });
+
+ it("has a glossary entry and an advisory home", () => {
+ expect(biomarkerInfo("untested_complexity").category).toBe("test_coverage");
+ expect(ADVISORY_HOME_BIOMARKERS.has("untested_complexity")).toBe(true);
+ });
+});
diff --git a/packages/ui/src/health/biomarker-details.tsx b/packages/ui/src/health/biomarker-details.tsx
index 4289bd9a3..02b8cdf69 100644
--- a/packages/ui/src/health/biomarker-details.tsx
+++ b/packages/ui/src/health/biomarker-details.tsx
@@ -26,6 +26,13 @@ function str(v: unknown): string | null {
return typeof v === "string" && v.length > 0 ? v : null;
}
+/** What an estimated ``untested_complexity`` tier means, in words. */
+const CRAP_TIER_LABEL: Record<string, string> = {
+ direct: "a test calls it",
+ reached: "tests reach it",
+ none: "no test found reaching it",
+};
+
function pct(v: number | null, digits = 0): string | null {
return v == null ? null : `${(v * 100).toFixed(digits)}%`;
}
@@ -371,6 +378,24 @@ export function BiomarkerDetails({
);
break;
}
+ case "untested_complexity": {
+ // Measured coverage reads as a percentage; an estimate names its tier
+ // and never a percentage.
+ const measured = str(details.coverage_basis) === "measured";
+ const tier = str(details.coverage_tier);
+ line = joinStats(
+ num(details.crap) != null ? `CRAP ${num(details.crap)?.toFixed(0)}` : null,
+ (num(details.judged_ccn) ?? num(details.ccn)) != null
+ ? `complexity ${num(details.judged_ccn) ?? num(details.ccn)}`
+ : null,
+ measured && num(details.coverage_pct) != null
+ ? `${num(details.coverage_pct)?.toFixed(0)}% covered`
+ : null,
+ !measured && tier ? `estimated: ${CRAP_TIER_LABEL[tier] ?? tier}` : null,
+ str(details.action) === "refactor" ? "simplify first" : null,
+ );
+ break;
+ }
case "coverage_gradient":
line = joinStats(
num(details.line_coverage_pct) != null
diff --git a/packages/ui/src/health/biomarker-glossary.ts b/packages/ui/src/health/biomarker-glossary.ts
index 6ae00a842..92c5943bb 100644
--- a/packages/ui/src/health/biomarker-glossary.ts
+++ b/packages/ui/src/health/biomarker-glossary.ts
@@ -116,6 +116,12 @@ export const BIOMARKER_GLOSSARY: Record<string, BiomarkerInfo> = {
description:
"Specific uncovered lines in a file. Surfaced when a coverage report has been ingested.",
},
+ untested_complexity: {
+ label: BIOMARKER_LABELS.untested_complexity,
+ category: "test_coverage",
+ description:
+ "A function whose CRAP score (complexity weighted by how little of it tests run) is 30 or more. Coverage is measured from a report when one covers the function, otherwise estimated from call-graph reach and shown as a tier, never a percentage. Advisory: it never deducts.",
+ },
coverage_gradient: {
label: BIOMARKER_LABELS.coverage_gradient,
category: "test_coverage_gradient",
@@ -401,6 +407,7 @@ export const ADVISORY_HOME_BIOMARKERS: ReadonlySet<string> = new Set([
"mock_saturated_test",
"hidden_coupling",
"brain_method",
+ "untested_complexity",
]);
/**
diff --git a/packages/ui/src/health/plain-language.ts b/packages/ui/src/health/plain-language.ts
index 6b935103e..6bb81826f 100644
--- a/packages/ui/src/health/plain-language.ts
+++ b/packages/ui/src/health/plain-language.ts
@@ -102,6 +102,10 @@ const BY_BIOMARKER: Record<string, PlainIssue> = {
title: "Changes often and is untested",
why: "Frequent edits with nothing to catch a mistake.",
},
+ untested_complexity: {
+ title: "Complex and little tested",
+ why: "Many paths through it, and few of them run under test.",
+ },
prior_defect: {
title: "Fixed for bugs often",
why: "Files with past bug fixes tend to get more.",
diff --git a/tests/unit/change_health/test_untested_complexity_compare.py b/tests/unit/change_health/test_untested_complexity_compare.py
new file mode 100644
index 000000000..c1664c8c8
--- /dev/null
+++ b/tests/unit/change_health/test_untested_complexity_compare.py
@@ -0,0 +1,119 @@
+"""``untested_complexity`` in a base-to-head comparison.
+
+Only CRAP on measured coverage takes part: a function's measured CRAP rising
+worsens it and a new one crossing the bar introduces it. An estimate rests on
+call-graph reach and never makes a finding introduced or worsened.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.analysis.change_health.analyzer import RevisionHealthAnalyzer
+from repowise.core.analysis.change_health.index_inputs import IndexInputs
+from repowise.core.analysis.change_health.matcher import _classify
+from repowise.core.analysis.change_health.models import ChangeFinding
+from repowise.core.analysis.change_health.service import _comparable
+from repowise.core.analysis.health.coverage.model import coverage_map_entry, file_coverage
+from repowise.core.analysis.health.models import HealthFindingData, Severity
+from repowise.core.analysis.health_gate import NOTE, WARNING, _level
+
+from .conftest import python_complex
+
+_PATH = "src/hot.py"
+
+
+def _finding(basis: str, crap: float, severity: Severity = Severity.HIGH) -> HealthFindingData:
+ cov = {"coverage_pct": 10.0} if basis == "measured" else {"coverage_tier": "none"}
+ return HealthFindingData(
+ biomarker_type="untested_complexity",
+ severity=severity,
+ file_path=_PATH,
+ function_name="run",
+ line_start=1,
+ line_end=30,
+ details={"crap": crap, "ccn": 8, "coverage_basis": basis, **cov},
+ reason="run has CRAP",
+ health_impact=0.0,
+ dimension="advisory",
+ )
+
+
+def test_only_measured_crap_is_compared() -> None:
+ assert _comparable(_finding("measured", 56.0), frozenset())
+ assert not _comparable(_finding("estimated", 72.0), frozenset())
+
+
+ ("before", "after", "kind"),
+ [(56.0, 80.0, "worsened"), (80.0, 56.0, "improved"), (56.0, 56.2, "unchanged")],
+)
+def test_measured_crap_moving_within_a_band(before: float, after: float, kind: str) -> None:
+ assert _classify(_finding("measured", before), _finding("measured", after)) == kind
+
+
+def _run(index: IndexInputs | None):
+ return RevisionHealthAnalyzer().analyze(
+ {_PATH: python_complex("run", 8).encode()}, subject_paths={_PATH}, index=index
+ )
+
+
+def _crap(run) -> list[HealthFindingData]:
+ return [f for f in run.findings if f.biomarker_type == "untested_complexity"]
+
+
+def test_without_a_report_nothing_is_comparable() -> None:
+ """A comparison reads no stored coverage, so every finding is an estimate."""
+ found = _crap(_run(None))
+ assert found and all(f.details["coverage_basis"] == "estimated" for f in found)
+ assert not any(_comparable(f, frozenset()) for f in found)
+
+
+def test_the_same_report_on_both_sides_changes_nothing() -> None:
+ """Base and head read one stored report, so unchanged code stays unchanged."""
+ lines = range(1, 200)
+ entry = coverage_map_entry(file_coverage(_PATH, (), lines), "lcov")
+ index = IndexInputs(coverage={_PATH: entry})
+ base, head = _crap(_run(index)), _crap(_run(index))
+ assert base and all(f.details["coverage_basis"] == "measured" for f in head)
+ assert [_classify(b, h) for b, h in zip(base, head, strict=True)] == ["unchanged"] * len(head)
+
+
+def _change(**kw) -> ChangeFinding:
+ base = dict(
+ change_finding_id="chf_1",
+ change_kind="introduced",
+ dimension="advisory",
+ biomarker_type="untested_complexity",
+ severity="critical",
+ path=_PATH,
+ symbol="run",
+ line_start=1,
+ line_end=30,
+ reason="run has CRAP 110",
+ attribution_basis="added_lines",
+ attribution_confidence="high",
+ attribution_detail="",
+ suggestion="",
+ follow_up="",
+ finding_id="finding_abc",
+ )
+ base.update(kw)
+ return ChangeFinding(**base)
+
+
+def test_a_measured_regression_warns_and_never_fails() -> None:
+ assert _level(_change(), "introduced", "medium") == WARNING
+ assert _level(_change(change_kind="worsened"), "worsened", "medium") == WARNING
+ assert _level(_change(attribution_basis="moved"), "worsened", "medium") == NOTE
+
+
+def test_sarif_carries_the_rule_with_the_line_free_finding_id() -> None:
+ from repowise.core.analysis.health_gate import evaluate, render_sarif
+ from tests.unit.analysis.test_health_gate import delta
+
+ result = evaluate(delta(_change()))
+ assert result.verdict == "warn"
+ run = render_sarif(result, tool_version="0")["runs"][0]
+ assert [r["id"] for r in run["tool"]["driver"]["rules"]] == ["untested_complexity"]
+ assert run["results"][0]["partialFingerprints"] == {"repowiseHealth/v1": "finding_abc"}
diff --git a/tests/unit/persistence/test_untested_complexity_hold.py b/tests/unit/persistence/test_untested_complexity_hold.py
new file mode 100644
index 000000000..cc5c43d0b
--- /dev/null
+++ b/tests/unit/persistence/test_untested_complexity_hold.py
@@ -0,0 +1,84 @@
+"""Only ``untested_complexity`` findings on measured coverage are served by default.
+
+An estimate rests on call-graph reach, so it is held back like a gated row:
+shown when the type is asked for by name or the caller opts into unverified rows.
+"""
+
+from __future__ import annotations
+
+import json
+
+import pytest
+
+from repowise.core.analysis.finding_registry import (
+ ROW_HOLDS,
+ gated_summary,
+ is_estimated,
+ split_held,
+)
+from repowise.core.persistence.crud.analysis import get_health_findings
+from repowise.core.persistence.models import HealthFinding
+from tests.unit.persistence.helpers import insert_repo
+
+
+def _details(basis: str, crap: float) -> dict:
+ tier = {"coverage_pct": 0.0} if basis == "measured" else {"coverage_tier": "none"}
+ return {"crap": crap, "ccn": 7, "coverage_basis": basis, "action": "add_tests", **tier}
+
+
+def test_is_estimated_reads_the_basis() -> None:
+ assert is_estimated("untested_complexity", _details("estimated", 42))
+ assert is_estimated("untested_complexity", json.dumps(_details("estimated", 42)))
+ assert not is_estimated("untested_complexity", _details("measured", 42))
+ assert not is_estimated("complex_method", {"coverage_basis": "estimated"})
+ assert not is_estimated("untested_complexity", "not json")
+
+
+def test_in_memory_rows_hold_estimates_and_say_why() -> None:
+ rows = [
+ {"biomarker_type": "untested_complexity", "file_path": "a.py", "details": _details("measured", 56)},
+ {"biomarker_type": "untested_complexity", "file_path": "a.py", "details": _details("estimated", 42)},
+ ]
+ kept, counts = split_held("health", rows, {}, kind="biomarker_type", get=lambda r, k: r.get(k))
+ assert [r["details"]["coverage_basis"] for r in kept] == ["measured"]
+ assert counts == {"estimated": 1}
+ assert gated_summary("health", counts) == {"estimated": {"count": 1, "reason": ROW_HOLDS["estimated"]}}
+
+
+async def _seed(session, repo_id: str) -> None:
+ for basis, crap in (("measured", 56.0), ("estimated", 42.0)):
+ session.add(
+ HealthFinding(
+ repository_id=repo_id,
+ file_path="src/app.py",
+ biomarker_type="untested_complexity",
+ severity="high" if basis == "measured" else "medium",
+ function_name=f"f_{basis}",
+ line_start=1,
+ line_end=20,
+ details_json=json.dumps(_details(basis, crap)),
+ dimension="advisory",
+ )
+ )
+ await session.commit()
+
+
+async def test_the_shared_list_serves_measured_rows_by_default(async_session) -> None:
+ repo = await insert_repo(async_session)
+ await _seed(async_session, repo.id)
+
+ rows = await get_health_findings(async_session, repo.id)
+ assert [r.function_name for r in rows] == ["f_measured"]
+
+
+ "kwargs", [{"biomarker_type": "untested_complexity"}, {"include_unverified": True}]
+)
+async def test_asking_by_name_or_opting_in_serves_estimates(async_session, kwargs) -> None:
+ repo = await insert_repo(async_session)
+ await _seed(async_session, repo.id)
+
+ rows = await get_health_findings(async_session, repo.id, **kwargs)
+ assert sorted(r.function_name for r in rows) == ["f_estimated", "f_measured"]
diff --git a/tests/unit/server/mcp/test_health_untested_complexity.py b/tests/unit/server/mcp/test_health_untested_complexity.py
new file mode 100644
index 000000000..0abb21edc
--- /dev/null
+++ b/tests/unit/server/mcp/test_health_untested_complexity.py
@@ -0,0 +1,98 @@
+"""``get_health(include=["untested_complexity"])``: functions ranked by CRAP.
+
+Measured rows carry a percentage and estimated rows a tier, never both, and an
+estimate is listed only when the call also opts into unverified rows.
+"""
+
+from __future__ import annotations
+
+import json
+
+import pytest
+
+from repowise.core.persistence.models import HealthFinding
+
+
+def _row(repo_id: str, name: str, basis: str, crap: float) -> HealthFinding:
+ cov = {"coverage_pct": 10.0} if basis == "measured" else {"coverage_tier": "reached"}
+ return HealthFinding(
+ repository_id=repo_id,
+ file_path="src/auth/service.py",
+ biomarker_type="untested_complexity",
+ severity="high" if crap >= 50 else "medium",
+ function_name=name,
+ line_start=10,
+ line_end=40,
+ details_json=json.dumps(
+ {"crap": crap, "ccn": 8, "coverage_basis": basis, "action": "add_tests", **cov}
+ ),
+ reason=f"{name} has CRAP {crap:.0f}",
+ dimension="advisory",
+ )
+
+
+async def crap_rows(session, repo_id):
+ session.add_all(
+ [
+ _row(repo_id, "login", "measured", 40.0),
+ _row(repo_id, "refresh", "measured", 80.0),
+ _row(repo_id, "guess", "estimated", 60.0),
+ ]
+ )
+ await session.commit()
+ return repo_id
+
+
+async def test_the_list_is_absent_until_asked_for(setup_mcp, crap_rows):
+ from repowise.server.mcp_server import get_health
+
+ result = await get_health()
+ assert "untested_complexity" not in result
+ assert "untested_complexity" not in (result.get("unknown_include_keys") or [])
+
+
+async def test_measured_rows_rank_by_crap_with_a_percentage(setup_mcp, crap_rows):
+ from repowise.server.mcp_server import get_health
+
+ result = await get_health(include=["untested_complexity"])
+ rows = result["untested_complexity"]
+ assert [r["function_name"] for r in rows] == ["refresh", "login"]
+ assert all(r["coverage_basis"] == "measured" and "coverage_tier" not in r for r in rows)
+ assert rows[0]["coverage_pct"] == 10.0
+ assert result["untested_complexity_total"] == 2
+
+
+async def test_estimates_join_only_when_opted_in_and_never_as_a_percentage(setup_mcp, crap_rows):
+ from repowise.server.mcp_server import get_health
+
+ rows = (await get_health(include=["untested_complexity", "unverified"]))["untested_complexity"]
+ assert [r["function_name"] for r in rows] == ["refresh", "guess", "login"]
+ guess = rows[1]
+ assert guess["coverage_tier"] == "reached"
+ assert "coverage_pct" not in guess
+
+
+async def test_the_advisory_findings_list_holds_estimates(setup_mcp, crap_rows):
+ from repowise.server.mcp_server import get_health
+
+ result = await get_health(targets=["src/auth/service.py"], include=["advisory"])
+ names = {
+ f.get("function_name")
+ for f in result.get("findings", [])
+ if f["biomarker_type"] == "untested_complexity"
+ }
+ assert names == {"login", "refresh"}
+
+
+async def test_held_estimates_are_counted_not_listed(setup_mcp, crap_rows):
+ from repowise.server.mcp_server import get_health
+
+ gated = (await get_health(targets=["src/auth/service.py"], include=["advisory"]))["_meta"]["gated"]
+ assert gated["health"]["estimated"] == 1
+ assert gated["opt_in"] == {"include": ["unverified"]}