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(dead-code): say why a safe-only view is empty (#3646)

Swati Ahuja4ffedf43c102
diff --git a/packages/cli/src/repowise/cli/commands/dead_code_cmd.py b/packages/cli/src/repowise/cli/commands/dead_code_cmd.py
index cacb52b5a..cfa80e568 100644
--- a/packages/cli/src/repowise/cli/commands/dead_code_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/dead_code_cmd.py
@@ -22,6 +22,7 @@ from repowise.cli.helpers import (
from repowise.cli.output import notice_console
from repowise.core.analysis.dead_code.models import DeadCodeFindingData, DeadCodeReport
from repowise.core.analysis.dead_code.risk_factors import RISK_CAP_CONFIDENCE
+from repowise.core.analysis.dead_code.serving import safe_only_note
#: The floor ``init`` and ``update`` store findings at: they run the analyzer
#: with its default config, which keeps nothing below this and runs all four
@@ -295,7 +296,10 @@ def dead_code_command(
findings = sorted(report.findings, key=_finding_order)
if safe_only:
- findings = [f for f in findings if f.safe_to_delete]
+ safe = [f for f in findings if f.safe_to_delete]
+ if note := safe_only_note(len(findings), len(safe)):
+ notices.print(f"[dim]{note}[/dim]")
+ findings = safe
if fmt == "json":
output = []
diff --git a/packages/core/src/repowise/core/analysis/dead_code/serving.py b/packages/core/src/repowise/core/analysis/dead_code/serving.py
index 4735b448e..c52b197a2 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/serving.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/serving.py
@@ -205,6 +205,18 @@ def merge_summary_counts(parts: list[dict[str, Any]]) -> dict[str, Any]:
return merged
+def safe_only_note(total: int, safe: int) -> str | None:
+ """Why a safe-only view is empty when findings exist but none is
+ deletion-ready; None otherwise. The MCP summary and the CLI share it."""
+ if safe or not total:
+ return None
+ return (
+ f"{total} finding(s), none deletion-ready: a finding is deletion-ready only "
+ "when missing imports are evidence for its language. Review the ranked list "
+ "without the safe-only filter."
+ )
+
+
def build_summary(
counts: dict[str, Any],
filtered_count: int,
@@ -222,6 +234,10 @@ def build_summary(
"by_kind": counts["by_kind"],
}
filters.summarize(summary)
+ if filters.safe_only and (
+ note := safe_only_note(counts["total_findings"], counts["safe_to_delete_count"])
+ ):
+ summary["safe_only_note"] = note
if withheld:
summary["withheld_types"] = withheld
if gated:
diff --git a/tests/unit/cli/test_dead_code_stored.py b/tests/unit/cli/test_dead_code_stored.py
index 18408000c..060d35d20 100644
--- a/tests/unit/cli/test_dead_code_stored.py
+++ b/tests/unit/cli/test_dead_code_stored.py
@@ -164,3 +164,34 @@ def test_json_marks_an_export_its_own_file_uses_as_export_only(indexed_repo):
assert site["safe_to_delete"] is False
(link,) = [f for f in findings if f["symbol_name"] == "link"]
assert link["export_only"] is False
+
+
+def _md(root: Path, *args: str) -> str:
+ result = CliRunner().invoke(
+ dead_code_command, [str(root), "--format", "md", "--no-workspace", *args]
+ )
+ assert result.exit_code == 0, result.output
+ return result.output
+
+
+def test_safe_only_says_why_it_is_empty(indexed_repo, monkeypatch):
+ monkeypatch.setattr(dead_code_cmd, "_analyze_live", None)
+ assert "none deletion-ready" not in _md(indexed_repo, "--safe-only")
+
+ unproven = {**_STORED_ONLY, "confidence": 0.6, "safe_to_delete": False}
+ run_async(_store(indexed_repo, [unproven]))
+ out = _md(indexed_repo, "--safe-only")
+ assert "1 finding(s), none deletion-ready" in out
+ assert "none deletion-ready" not in _md(indexed_repo)
+
+
+def test_safe_only_rereads_a_stored_safe_flag_through_the_gate(indexed_repo, monkeypatch):
+ # Stored safe, but a runtime-loaded config path: the read surfaces'
+ # gate downgrades it, so --safe-only hides it and explains why.
+ monkeypatch.setattr(dead_code_cmd, "_analyze_live", None)
+ risky = {**_STORED_ONLY, "file_path": "gone/settings.py"}
+ run_async(_store(indexed_repo, [risky]))
+
+ out = _md(indexed_repo, "--safe-only")
+ assert "gone/settings.py" not in out
+ assert "1 finding(s), none deletion-ready" in out
diff --git a/tests/unit/dead_code/test_serving.py b/tests/unit/dead_code/test_serving.py
index a1a5a3280..7497fec39 100644
--- a/tests/unit/dead_code/test_serving.py
+++ b/tests/unit/dead_code/test_serving.py
@@ -26,6 +26,7 @@ from repowise.core.analysis.dead_code.serving import (
merge_summary_counts,
rollup_by_directory,
rollup_by_owner,
+ safe_only_note,
serialize_finding,
summary_counts,
)
@@ -135,6 +136,21 @@ def test_build_summary_counts_every_shown_finding() -> None:
assert "withheld_types" not in summary
+def test_safe_only_note_explains_an_empty_safe_only_view() -> None:
+ unproven = [_row("src/a/one.py", 0.6, safe_to_delete=False)]
+ summary = build_summary(summary_counts(unproven), 0, _filters(safe_only=True), {})
+ assert summary["safe_only_note"] == safe_only_note(1, 0)
+ assert "1 finding(s), none deletion-ready" in summary["safe_only_note"]
+
+ # Safe rows exist, nothing to explain; nor when there are no findings.
+ safe = build_summary(summary_counts(_ROWS), 3, _filters(safe_only=True), {})
+ assert "safe_only_note" not in safe
+ empty = build_summary(summary_counts([]), 0, _filters(safe_only=True), {})
+ assert "safe_only_note" not in empty
+ # Without safe_only the summary never carries it.
+ assert "safe_only_note" not in build_summary(summary_counts(unproven), 1, _filters(), {})
+
+
def test_serialization_is_the_same_for_rows_and_mappings() -> None:
row = _ROWS[0]
as_dict = serialize_finding(row, repository="r")
diff --git a/tests/unit/server/mcp/test_dead_code.py b/tests/unit/server/mcp/test_dead_code.py
index 02c322ace..d48afd72a 100644
--- a/tests/unit/server/mcp/test_dead_code.py
+++ b/tests/unit/server/mcp/test_dead_code.py
@@ -60,6 +60,8 @@ async def test_get_dead_code_safe_only(setup_mcp):
for tier_data in result["tiers"].values():
for f in tier_data["findings"]:
assert f["safe_to_delete"] is True
+ # A deletion-ready finding exists, so there is no empty view to explain.
+ assert "safe_only_note" not in result["summary"]
@pytest.mark.asyncio