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
perf(update): skip clone detection and dead code when only prose changed (#4217)
Raghav Chamadiya4aaf334bdbd1
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/command.py b/packages/cli/src/repowise/cli/commands/update_cmd/command.pyindex b02d0d2b4..c8917a8e3 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/command.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/command.py@@ -1424,6 +1424,7 @@ def _run_partial_analyses(run: UpdateRun) -> None:stored_performance_callers=stored_performance_callers,repo_function_mod_p80=repo_function_mod_p80,timings=run.timings,+ dead_code=not _dead_code_unchanged(run),)run.doc_drift_report = _run_doc_drift_partial(run.graph_builder,@@ -1435,6 +1436,40 @@ def _run_partial_analyses(run: UpdateRun) -> None:)+def _is_prose(path: str) -> bool:+ from repowise.core.ingestion.languages.registry import REGISTRY++ suffix = Path(path).suffix.lower()+ # .mdx imports mark components reachable; .rst is read for symbol names.+ return suffix != ".mdx" and REGISTRY.from_extension(suffix) in ("markdown", "asciidoc")+++def _dead_code_unchanged(run: UpdateRun) -> bool:+ """Whether the stored dead-code findings still stand and nothing this run reads new ones.++ True only when every changed file is prose, no analyzer, parser, renderer,+ history or config change forces a re-score, and no page render in this run+ consumes the report. (The knowledge graph takes the report but does not read it.)+ """+ config = run.config+ if (+ run.analyzer_changed+ or run.extraction_changed+ or run.renderer_changed+ or run.history_stale+ or config.changed+ ):+ return False+ changed = [path for fd in run.file_diffs for path in (fd.path, fd.old_path) if path]+ if not all(map(_is_prose, changed)):+ return False+ # Index-only renders template wikis only.+ if run.index_only and resolve_docs_mode(run.state) != "deterministic":+ return True+ rendered = run.affected.regenerate if run.affected is not None else []+ return all(map(_is_prose, rendered)) and not run.stale_deterministic_ids++def _refresh_knowledge_graph_stage(run: UpdateRun) -> None:"""Rebuild the knowledge graph when its fingerprint moved (not before a commit)."""if not run.working_tree_only:diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/incremental.py b/packages/cli/src/repowise/cli/commands/update_cmd/incremental.pyindex 765bb28a2..bf2100a36 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/incremental.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/incremental.py@@ -218,6 +218,7 @@ def _run_partial_analysis(stored_performance_callers: set[str] | None = None,repo_function_mod_p80: int | None = None,timings: PhaseTimings | None = None,+ dead_code: bool = True,) -> tuple[Any, Any]:"""Run partial code-health + repo-wide dead-code analysis.@@ -248,6 +249,7 @@ def _run_partial_analysis(coverage_map=coverage_map,log=console.print,timings=timings,+ dead_code=dead_code,)diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.pyindex ac808ce65..ea51af273 100644--- a/packages/core/src/repowise/core/analysis/health/engine.py+++ b/packages/core/src/repowise/core/analysis/health/engine.py@@ -790,7 +790,8 @@ class HealthAnalyzer:"""from repowise.core.pipeline.phase_timing import timed- if "dry_violation" in run.disabled:+ # An empty splice set: no file whose clone list a biomarker reads changed.+ if "dry_violation" in run.disabled or changed == set():return DuplicationReport()try:with timed(timings, "analysis.health.duplication"):diff --git a/packages/core/src/repowise/core/pipeline/incremental/analysis.py b/packages/core/src/repowise/core/pipeline/incremental/analysis.pyindex df64d9b4b..8602f478f 100644--- a/packages/core/src/repowise/core/pipeline/incremental/analysis.py+++ b/packages/core/src/repowise/core/pipeline/incremental/analysis.py@@ -51,6 +51,7 @@ def run_partial_analysis(coverage_map: dict[str, dict] | None = None,log: LogFn | None = None,timings: PhaseTimings | None = None,+ dead_code: bool = True,) -> tuple[Any, Any]:"""Run partial code-health + repo-wide dead-code analysis.@@ -87,6 +88,9 @@ def run_partial_analysis(``timings`` records the two analyses as ``analysis.health`` and``analysis.dead_code``. Their costs move independently with the edit, sothey are never folded into one row.++ ``dead_code=False`` skips the dead-code pass and returns ``None`` for it,+ which keeps the stored findings."""log = log or _noop_log@@ -106,6 +110,7 @@ def run_partial_analysis(from repowise.core.analysis.execution_graph import ExecutionGraphIndexfrom repowise.core.analysis.health import HealthAnalyzerfrom repowise.core.analysis.health.config import HealthConfig+ from repowise.core.analysis.health.scope import scores_language_health_changed = {fd.path for fd in file_diffs if fd.status in ("added", "modified", "renamed")@@ -143,7 +148,13 @@ def run_partial_analysis(changed_files=_health_scope,repo_function_mod_p80=repo_function_mod_p80,timings=timings,- duplication_files=_health_changed,+ # Clone pairs are read only for code; a prose-only change splices nothing.+ duplication_files={+ pf.file_info.path+ for pf in parsed_files+ if pf.file_info.path in _health_changed+ and scores_language(pf.file_info.language)+ },)# The closure exists only to refresh interprocedural performance.# Preserve the historical changed-file scope for every other@@ -187,6 +198,8 @@ def run_partial_analysis(# short-circuit, which left the closure's reference to# ``dead_code_report`` unbound and crashed every ``--index-only`` run.dead_code_report = None+ if not dead_code:+ return partial_health_report, Noneif timings is not None:timings.start("analysis.dead_code")try:diff --git a/tests/unit/cli/test_update_dead_code_gate.py b/tests/unit/cli/test_update_dead_code_gate.pynew file mode 100644index 000000000..0920a701e--- /dev/null+++ b/tests/unit/cli/test_update_dead_code_gate.py@@ -0,0 +1,77 @@+"""An update keeps the stored dead-code findings when nothing it changed can move them."""++from __future__ import annotations++from types import SimpleNamespace++import pytest++from repowise.cli.commands.update_cmd import command+++def _run(tmp_path, *paths: str, **overrides) -> SimpleNamespace:+ fields = {+ "analyzer_changed": False,+ "extraction_changed": False,+ "renderer_changed": False,+ "history_stale": False,+ "config": SimpleNamespace(changed=False),+ "file_diffs": [SimpleNamespace(path=p, old_path=None) for p in paths],+ "affected": SimpleNamespace(regenerate=[]),+ "stale_deterministic_ids": set(),+ "index_only": False,+ "state": {"docs_mode": "deterministic"},+ }+ return SimpleNamespace(**{**fields, **overrides})+++def test_prose_only_change_keeps_stored_findings(tmp_path) -> None:+ assert command._dead_code_unchanged(_run(tmp_path, "README.md", "docs/guide.adoc"))+++def test_no_change_keeps_stored_findings(tmp_path) -> None:+ assert command._dead_code_unchanged(_run(tmp_path))+++ "overrides",+ [+ {"analyzer_changed": True},+ {"history_stale": True},+ {"config": SimpleNamespace(changed=True)},+ {"affected": SimpleNamespace(regenerate=["src/a.py"])},+ {"stale_deterministic_ids": {"scc_page:x"}},+ ],+)+def test_anything_that_reads_or_moves_findings_reruns(tmp_path, overrides) -> None:+ assert not command._dead_code_unchanged(_run(tmp_path, "README.md", **overrides))+++def test_code_or_manifest_change_reruns(tmp_path) -> None:+ assert not command._dead_code_unchanged(_run(tmp_path, "README.md", "src/a.py"))+ assert not command._dead_code_unchanged(_run(tmp_path, "requirements.txt"))+ assert not command._dead_code_unchanged(_run(tmp_path, "package.json"))++[email protected]("path", ["docs/page.mdx", "docs/api.rst"])+def test_prose_that_names_code_reruns(tmp_path, path) -> None:+ assert not command._dead_code_unchanged(_run(tmp_path, path))+++def test_rename_from_code_reruns(tmp_path) -> None:+ run = _run(tmp_path)+ run.file_diffs = [SimpleNamespace(path="notes.md", old_path="src/notes.py")]+ assert not command._dead_code_unchanged(run)+++def test_index_only_model_wiki_renders_nothing(tmp_path) -> None:+ run = _run(+ tmp_path,+ "README.md",+ index_only=True,+ state={"docs_mode": "llm"},+ affected=SimpleNamespace(regenerate=["src/a.py"]),+ )+ assert command._dead_code_unchanged(run)+ run.file_diffs = [SimpleNamespace(path="src/a.py", old_path=None)]+ assert not command._dead_code_unchanged(run)diff --git a/tests/unit/pipeline/test_partial_health_scope.py b/tests/unit/pipeline/test_partial_health_scope.pyindex eda49a678..eb554859d 100644--- a/tests/unit/pipeline/test_partial_health_scope.py+++ b/tests/unit/pipeline/test_partial_health_scope.py@@ -62,3 +62,54 @@ def test_duplication_is_narrowed_to_the_changed_files(monkeypatch, tmp_path) ->assert seen["changed_files"] == {"a.py"}analyzer.analyze(None, changed_files={"a.py", "b.py"})assert seen["changed_files"] == {"a.py", "b.py"}+++def test_empty_splice_set_skips_the_clone_detector(monkeypatch) -> None:+ import networkx as nx++ from repowise.core.analysis.health import engine as engine_mod++ def fail(*_args, **_kwargs):+ raise AssertionError("detect_clones ran with nothing to splice")++ monkeypatch.setattr(engine_mod, "detect_clones", fail)+ analyzer = engine_mod.HealthAnalyzer(nx.DiGraph(), git_meta_map={}, parsed_files=[])+ analyzer.analyze(None, changed_files={"README.md"}, duplication_files=set())+++def test_prose_changes_are_not_spliced_and_dead_code_can_be_skipped(monkeypatch) -> None:+ from types import SimpleNamespace++ import networkx as nx++ from repowise.core.analysis.health import engine as engine_mod+ from repowise.core.pipeline.incremental import analysis++ seen: dict = {}++ class FakeAnalyzer:+ def __init__(self, *_args, **_kwargs) -> None:+ pass++ def analyze(self, config=None, **kwargs):+ seen["duplication_files"] = kwargs.get("duplication_files")+ return engine_mod.HealthReport(repo_id="", analyzed_at=None)++ monkeypatch.setattr("repowise.core.analysis.health.HealthAnalyzer", FakeAnalyzer)+ parsed = [+ SimpleNamespace(file_info=SimpleNamespace(path=p, language=lang))+ for p, lang in (("a.py", "python"), ("README.md", "markdown"))+ ]+ diffs = [SimpleNamespace(path=p, status="modified") for p in ("a.py", "README.md")]+ builder = SimpleNamespace(graph=lambda: nx.DiGraph(), community_detection=lambda: {})+ monkeypatch.setattr(+ "repowise.core.analysis.communities.file_community_labels", lambda _b: {}+ )++ health, dead = analysis.run_partial_analysis(+ ".", builder, {}, parsed, diffs, dead_code=False+ )++ assert seen["duplication_files"] == {"a.py"}+ assert health is not None+ assert dead is None