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): give `--refactoring-targets --recompute` plans their findings (#3496) (#3521)

Amir59f5aa71a2fe
diff --git a/packages/cli/src/repowise/cli/commands/health_cmd/command.py b/packages/cli/src/repowise/cli/commands/health_cmd/command.py
index 0f97e5a0c..c10b69908 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd/command.py
@@ -340,7 +340,7 @@ def health_command(
suggestions = [s for s in suggestions if s.file_path.startswith(module_filter)]
# Attaches the same batched validation block the read surfaces emit;
# code generation remains explicitly requested and never auto-applies.
- _load_recommendations(repo_path, suggestions, metrics_sorted)
+ _load_recommendations(repo_path, suggestions, metrics_sorted, findings)
_generate_refactoring_code(repo_path, suggestions, generate_code, fmt=fmt)
return
@@ -350,7 +350,7 @@ def health_command(
suggestions = [s for s in suggestions if s.file_path == file_filter]
if module_filter:
suggestions = [s for s in suggestions if s.file_path.startswith(module_filter)]
- recommendations = _load_recommendations(repo_path, suggestions, metrics_sorted)
+ recommendations = _load_recommendations(repo_path, suggestions, metrics_sorted, findings)
_render_refactoring_targets(metrics_sorted, findings, recommendations, fmt=fmt)
return
diff --git a/packages/cli/src/repowise/cli/commands/health_cmd/persist.py b/packages/cli/src/repowise/cli/commands/health_cmd/persist.py
index 0f2e6b5ef..0a05c43e4 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd/persist.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd/persist.py
@@ -145,7 +145,10 @@ def _load_stored_report(repo_path: object) -> tuple[Any, set[str], dict[str, str
def _load_recommendations(
- repo_path: Path, suggestions: Sequence[Any], metrics: Sequence[Any]
+ repo_path: Path,
+ suggestions: Sequence[Any],
+ metrics: Sequence[Any],
+ findings: Sequence[Any] | None = None,
) -> list[dict[str, Any]]:
"""Canonical CLI recommendations, enriched from the local store in bulk."""
from repowise.cli.helpers import get_db_url_for_repo, reconcile_schema_best_effort
@@ -169,7 +172,12 @@ def _load_recommendations(
if repo is None:
return []
recommendations = await hydrate_recommendations(
- session, repo.id, suggestions, metric_rows=metrics, checkout=str(repo_path)
+ session,
+ repo.id,
+ suggestions,
+ metric_rows=metrics,
+ findings=findings,
+ checkout=str(repo_path),
)
return serialize_recommendations(recommendations)
@@ -183,6 +191,7 @@ def _load_recommendations(
build_recommendations(
suggestions,
metric_by_path={getattr(metric, "file_path", ""): metric for metric in metrics},
+ findings=findings,
)
)
diff --git a/packages/cli/src/repowise/cli/commands/health_cmd/refactoring_targets.py b/packages/cli/src/repowise/cli/commands/health_cmd/refactoring_targets.py
index 0094c708a..af49b9867 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd/refactoring_targets.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd/refactoring_targets.py
@@ -64,7 +64,9 @@ def _render_refactoring_targets(
serialize_recommendations,
)
- ranked_plans = serialize_recommendations(build_recommendations(suggestions))[:limit]
+ ranked_plans = serialize_recommendations(
+ build_recommendations(suggestions, findings=findings)
+ )[:limit]
else:
ranked_plans = [_list_row(_suggestion_to_dict(s)) for s in suggestions][:limit]
diff --git a/tests/unit/cli/test_health_recommendations_checkout.py b/tests/unit/cli/test_health_recommendations_checkout.py
index 9ad8176ab..977c770a2 100644
--- a/tests/unit/cli/test_health_recommendations_checkout.py
+++ b/tests/unit/cli/test_health_recommendations_checkout.py
@@ -9,6 +9,7 @@ import pytest
from repowise.cli.commands.health_cmd.persist import _load_recommendations
from repowise.cli.helpers import run_async
from repowise.core.persistence.crud.analysis import refactoring_recommendations
+from tests.unit.health.test_worth_after_plan import _finding, _plan
async def _store_repo(repo_path: Path) -> None:
@@ -48,3 +49,35 @@ def test_the_cli_fallback_passes_its_checkout(
monkeypatch.setattr(refactoring_recommendations, "hydrate_recommendations", hydrate)
assert _load_recommendations(root, [], []) == []
assert seen["checkout"] == str(root)
+
+
+def test_the_cli_passes_its_findings_to_hydrate(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)
+ monkeypatch.delenv("REPOWISE_DATABASE_URL", raising=False)
+ root = tmp_path.resolve()
+ run_async(_store_repo(root))
+ findings = [_finding("big", 90, 10)]
+ seen: dict[str, object] = {}
+
+ async def hydrate(session, repository_id, rows, **kwargs):
+ seen.update(kwargs)
+ return []
+
+ monkeypatch.setattr(refactoring_recommendations, "hydrate_recommendations", hydrate)
+ assert _load_recommendations(root, [], [], findings) == []
+ assert seen["findings"] == findings
+
+
+def test_the_cli_fallback_marks_a_plan_that_leaves_the_bar(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)
+ monkeypatch.delenv("REPOWISE_DATABASE_URL", raising=False)
+ big, small = _plan("big", 10, 10, impact=3.0), _plan("small", 10, 10, impact=1.0)
+ findings = [_finding("big", 90, 10), _finding("small", 30, 10)]
+ rows = _load_recommendations(tmp_path.resolve(), [big, small], [], findings)
+ assert [row["target_symbol"] for row in rows] == ["small", "big"]
+ assert rows[1]["leaves_above_bar"].startswith("leaves big at CCN 80")
+ assert "leaves_above_bar" not in rows[0]
diff --git a/tests/unit/cli/test_refactoring_targets_render.py b/tests/unit/cli/test_refactoring_targets_render.py
index 7268f58ce..1e80a3c9a 100644
--- a/tests/unit/cli/test_refactoring_targets_render.py
+++ b/tests/unit/cli/test_refactoring_targets_render.py
@@ -276,3 +276,27 @@ def test_an_unattached_split_with_an_unnamed_group_says_it_needs_design(capsys):
text = capsys.readouterr().out
assert "1 need design: a group has no name yet." in text
assert "- **big.py -> 2 files** (pkg/big.py) needs design" in text
+
+
+def _size_finding(target: str, ccn: int, line: int) -> HealthFindingData:
+ return HealthFindingData(
+ biomarker_type="complex_method",
+ severity=Severity.HIGH,
+ file_path=f"pkg/{target}.py",
+ function_name=target,
+ line_start=line,
+ line_end=line + 200,
+ details={"ccn": ccn, "nloc": 150},
+ health_impact=1.0,
+ )
+
+
+def test_raw_plans_carry_the_above_the_bar_mark_and_rank_by_it(capsys):
+ from tests.unit.health.test_worth_after_plan import _plan
+
+ big, small = _plan("big", 10, 10, impact=3.0), _plan("small", 10, 10, impact=1.0)
+ findings = [_size_finding("big", 90, 10), _size_finding("small", 30, 10)]
+ _render_refactoring_targets([], findings, [big, small], fmt="json")
+ plans = json.loads(capsys.readouterr().out)["refactoring_plans"]
+ assert [plan["target_symbol"] for plan in plans] == ["small", "big"]
+ assert plans[1]["leaves_above_bar"].startswith("leaves big at CCN 80")