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): Fix first Verify for findings with no plan reads measured coverage, then reaching tests (#3033)
Raghav Chamadiyab4b1aa844c87
diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/build.py b/packages/core/src/repowise/core/analysis/health/fix_first/build.pyindex 8155cac20..a28f1757f 100644--- a/packages/core/src/repowise/core/analysis/health/fix_first/build.py+++ b/packages/core/src/repowise/core/analysis/health/fix_first/build.py@@ -280,6 +280,10 @@ def _risk(files_touched: int, dependents: int | None) -> FixRisk:return FixRisk(level, dependents, files_touched, "; ".join(parts).capitalize() + ".")+#: Validation profile for a finding with no stored plan: (path, function, line start, line end).+Validate = Callable[[str, Any, Any, Any], Mapping[str, Any] | None]++def _verify(profile: Mapping[str, Any] | None) -> FixVerify:"""The stored validation profile, shown short. Never recomputed here."""if not profile:@@ -1094,7 +1098,7 @@ def _perf_unit(# --- findings with no plan -----------------------------------------------------------def _finding_unit(lead: Any, files: _Files, first: FixStep) -> _Unit:+def _finding_unit(lead: Any, files: _Files, first: FixStep, validate: Validate | None) -> _Unit:path = field(lead, "file_path")marker = field(lead, "biomarker_type") or ""function = field(lead, "function_name")@@ -1143,7 +1147,11 @@ def _finding_unit(lead: Any, files: _Files, first: FixStep) -> _Unit:"confidence": FixConfidence("medium", "Measured from the code; no stored plan has checked a fix."),- "verify": _verify(None),+ "verify": _verify(+ validate(path, function, field(lead, "line_start"), field(lead, "line_end"))+ if validate+ else None+ ),"context": files.context(path),"source": FixSource(None, (), (public_id,) if public_id else ()),"next_call": ActionCommand.call(@@ -1322,6 +1330,7 @@ def build_fix_first(item_id: str | None = None,hot_cuts: tuple[float, float] | None = None,symbol_lines: Mapping[str, int] | None = None,+ validate: Validate | None = None,) -> FixFirstQueue:"""One ranked queue of what to fix, from stored rows (shapes in the module docstring).@@ -1333,7 +1342,10 @@ def build_fix_first(caller measured them over more files than it passed in ``metrics``; see:func:`hot_cut` for the rule. ``symbol_lines`` maps a symbol id(``path::name``) to its first line, for a performance plan step that- names a function but stored no line.+ names a function but stored no line. ``validate(path, function, start, end)``+ is the validation profile (``basis``, ``via``, ``total``, ``tests``,+ ``commands``) of a finding with no plan, read lazily for the items shown;+ without it such an item's Verify stays unknown."""metrics = list(metrics)findings = list(findings)@@ -1442,7 +1454,7 @@ def build_fix_first(if eligible is None:excluded[reasons[id(lead)] or next(r for r in reasons.values() if r)] += 1continue- units.append(_finding_unit(eligible, files, files.first_step(eligible)))+ units.append(_finding_unit(eligible, files, files.first_step(eligible), validate))ordered = _order(units)if item_id is not None:diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py b/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.pyindex 925cdf1f4..46e858a48 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py@@ -22,6 +22,7 @@ one aggregate read.from __future__ import annotationsfrom collections import OrderedDict, namedtuple+from collections.abc import Callablefrom dataclasses import replacefrom typing import Any@@ -33,6 +34,14 @@ from repowise.core.analysis.health.fix_first import DEFAULT_LIMIT, FixFirstQueuefrom repowise.core.analysis.health.fix_first.build import MIN_WORTH, hot_cut, hot_cut_offsetfrom repowise.core.analysis.health.perf.opportunity_rank import DEFAULT_QUEUE_STATESfrom repowise.core.analysis.health.refactoring.identity import REFACTORING_MODEL_VERSION+from repowise.core.analysis.health.refactoring.models import (+ RefactoringSuggestion as PlanSuggestion,+)+from repowise.core.analysis.health.refactoring.recommendations import (+ DEFAULT_TEST_LIMIT,+ _validation_inputs,+ build_validation_plan,+)from repowise.core.analysis.health.rows import detail_mapfrom repowise.core.analysis.health.scoring import history_biomarkers@@ -368,6 +377,61 @@ async def _symbol_lines(session: AsyncSession, repo_id: str, performance: list[Areturn {node_id: line for node_id, line in rows.all()}+def _span_suggestion(path: str, function: Any, start: Any, end: Any) -> PlanSuggestion:+ """A finding's lines as the plan shape the validation read takes."""+ return PlanSuggestion(+ refactoring_type="finding",+ file_path=path,+ target_symbol=function or "",+ line_start=start if isinstance(start, int) else None,+ line_end=end if isinstance(end, int) else None,+ plan={},+ evidence={},+ impact_delta=0.0,+ effort_bucket="M",+ blast_radius={},+ confidence="medium",+ )+++async def _finding_validator(+ session: AsyncSession, repo_id: str, findings: list[Any]+) -> Callable[[str, Any, Any, Any], dict[str, Any] | None]:+ """Tests for a finding with no plan, by the rule a plan's tests follow.++ Measured coverage of the finding's lines first, then the tests that reach+ the file through the call and import graphs, then a matching test name.+ One batched read covers every candidate finding; the builder asks only for+ the items it writes. Ceiling: tests are not ordered by symbol-level reach+ (the plan path reads that evidence per symbol); upgrade path is passing+ ``evidence`` to ``build_validation_plan``.+ """+ spans = {+ (f.file_path, f.line_start, f.line_end): f+ for f in findings+ if f.file_path and isinstance(f.line_start, int)+ }+ if not spans:+ return lambda *_: None+ suggestions = [_span_suggestion(p, f.function_name, s, e) for (p, s, e), f in spans.items()]+ inputs = await _validation_inputs(+ session, repo_id, suggestions, sorted({s.file_path for s in suggestions})+ )++ def validate(path: str, function: Any, start: Any, end: Any) -> dict[str, Any] | None:+ plan = build_validation_plan(+ _span_suggestion(path, function, start, end),+ inputs.measured,+ inputs.inferred,+ test_limit=DEFAULT_TEST_LIMIT,+ order_tests=False,+ )+ # No test found: unknown, not the bare repo-wide command a plan falls back to.+ return plan.as_dict() if plan.total else None++ return validate++#: Built queues kept in process. A handful covers the shapes one surface asks#: for (dashboard, CLI, Do next, one id), per repository a server holds.CACHE_SIZE = 32@@ -490,6 +554,7 @@ async def _build(basis=await _basis(session, repository_id),hot_cuts=await _hot_cuts(session, repository_id),symbol_lines=await _symbol_lines(session, repository_id, performance),+ validate=await _finding_validator(session, repository_id, findings),)diff --git a/tests/unit/health/test_fix_first_build.py b/tests/unit/health/test_fix_first_build.pyindex 3c042041f..ffac48ad2 100644--- a/tests/unit/health/test_fix_first_build.py+++ b/tests/unit/health/test_fix_first_build.py@@ -219,3 +219,31 @@ def test_every_open_refactoring_opportunity_carries_its_reason() -> None:}# Off the wire: the reasons are a read-model input, not part of the queue.assert "refactoring_reasons" not in queue.as_dict()+++def test_a_finding_item_takes_its_tests_from_the_validate_callback() -> None:+ asked: list[tuple] = []++ def validate(path, function, start, end):+ asked.append((path, function, start, end))+ return {+ "basis": "inferred",+ "via": "call-graph",+ "total": 1,+ "tests": ["tests/test_plain.py::test_walk"],+ "commands": ["pytest tests/test_plain.py::test_walk"],+ }++ queue = build_fix_first(+ metrics=METRICS,+ findings=FINDINGS,+ refactoring=REFACTORING,+ performance=PERFORMANCE,+ plans=PLANS,+ validate=validate,+ )+ walk = next(i for i in queue.items if i.target.file_path == "src/plain.py")+ assert asked == [("src/plain.py", "walk", 5, 40)]+ assert walk.verify.basis == "inferred"+ assert [t.path for t in walk.verify.tests] == ["tests/test_plain.py::test_walk"]+ assert "call graph" in walk.verify.tests[0].reasondiff --git a/tests/unit/persistence/test_fix_first_loader.py b/tests/unit/persistence/test_fix_first_loader.pyindex f04dfeb8d..0c6753d9d 100644--- a/tests/unit/persistence/test_fix_first_loader.py+++ b/tests/unit/persistence/test_fix_first_loader.py@@ -248,3 +248,39 @@ async def test_every_limit_and_id_is_a_slice_of_one_build(async_session, monkeyp**rows, item_id=second)assert builds == 1+++async def test_a_finding_item_verifies_with_measured_tests_of_its_lines(async_session) -> None:+ """A finding with no plan takes its tests from coverage of its own lines."""+ from repowise.core.persistence.models import TestCoverageEntry++ rid = await seed_fix_first(async_session)+ for test_id, lines in (+ ("tests/test_plain.py::test_walk", [6, 7, 8]),+ ("tests/test_plain.py::test_elsewhere", [50, 51]),+ ):+ async_session.add(+ TestCoverageEntry(+ repository_id=rid,+ test_id=test_id,+ test_file="tests/test_plain.py",+ source_file="src/plain.py",+ covered_lines_json=json.dumps(lines),+ source_format="coverage_json",+ )+ )+ await async_session.flush()+ loaded = await load_fix_first(async_session, rid)+ walk = next(i for i in loaded.items if i.target.file_path == "src/plain.py")+ assert walk.kind == "finding"+ assert walk.verify.basis == "measured"+ assert [t.path for t in walk.verify.tests] == ["tests/test_plain.py::test_walk"]+ assert walk.verify.tests_total == 1+ assert walk.verify.command == "pytest tests/test_plain.py::test_walk"+++async def test_a_finding_item_with_no_tests_stays_unknown(async_session) -> None:+ rid = await seed_fix_first(async_session)+ loaded = await load_fix_first(async_session, rid)+ walk = next(i for i in loaded.items if i.target.file_path == "src/plain.py")+ assert (walk.verify.tests, walk.verify.basis) == ((), "unknown")