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(cli): render a refactoring step whose plan falls outside the limit (#2004)

Raghav Chamadiyae1fbab326e26
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 c5b20dd84..d980f39b1 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
@@ -377,7 +377,13 @@ def _plan_detail_console(p: dict) -> list[str]:
extractions appeared three times in three places and the reader had to
reassemble it. The detail itself was the good part and is unchanged; it is
the grouping that went.
+
+ Empty when the step's plan is not in the rendered set: composition runs over
+ every suggestion while the plan list is truncated to ``limit``, so a step can
+ outrank its own plan's detail.
"""
+ if not p:
+ return []
kind = p["refactoring_type"]
pl = p.get("plan") or {}
ev = p.get("evidence") or {}
@@ -449,7 +455,9 @@ def _plan_detail_console(p: dict) -> list[str]:
def _plan_detail_md(p: dict) -> list[str]:
- """The same detail, as Markdown lines."""
+ """The same detail, as Markdown lines. Empty for a plan outside the limit."""
+ if not p:
+ return []
kind = p["refactoring_type"]
pl = p.get("plan") or {}
ev = p.get("evidence") or {}
diff --git a/tests/unit/cli/test_refactoring_targets_render.py b/tests/unit/cli/test_refactoring_targets_render.py
index 3d6a8499a..d4e2aba8d 100644
--- a/tests/unit/cli/test_refactoring_targets_render.py
+++ b/tests/unit/cli/test_refactoring_targets_render.py
@@ -148,3 +148,33 @@ def test_helper_site_falls_back_when_a_legacy_row_has_no_directory(capsys):
legacy = _extract_helper({"module": "ui", "directory": None})
_render_refactoring_targets([], [], [legacy], fmt="md")
assert "near `ui`" in capsys.readouterr().out
+
+
+def _extract_method(i: int) -> RefactoringSuggestion:
+ """One plan per file, so composition yields one step per file."""
+ return RefactoringSuggestion(
+ refactoring_type="extract_method",
+ file_path=f"m{i}.py",
+ target_symbol=f"f{i}",
+ line_start=10,
+ line_end=30,
+ plan={"span": {"start": 10, "end": 30}, "suggested_name": f"_f{i}_part"},
+ evidence={"ccn_removed": 6, "slice_nloc": 20},
+ impact_delta=float(i),
+ effort_bucket="S",
+ blast_radius={"scope": "local"},
+ confidence="high",
+ )
+
+
+def test_a_step_whose_plan_falls_outside_the_limit_still_renders(capsys):
+ """Composition runs over every suggestion; the plan list is truncated.
+
+ So a step can outrank its own plan's detail, and the detail lookup hands
+ the renderer an empty dict. It used to dereference that and raise
+ ``KeyError: 'refactoring_type'``, taking the whole command down.
+ """
+ suggestions = [_extract_method(i) for i in range(25)]
+ for fmt in ("console", "md"):
+ _render_refactoring_targets([], [], suggestions, fmt=fmt, limit=3)
+ assert capsys.readouterr().out