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.

perf(update): skip history and analysis phases on a working-tree-only update (#3107)

Raghav Chamadiya6575b7eedec4
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/command.py b/packages/cli/src/repowise/cli/commands/update_cmd/command.py
index 6344f37fe..859a126cd 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/command.py
@@ -1440,7 +1440,23 @@ def run_update(
# idle-file health re-score gate.
git_decay_map: dict[str, dict] = {}
full_git_summaries: list[Any] = []
- head_ts = _head_commit_ts(repo_path)
+ # No commit since the last sync: only uncommitted files changed, so the
+ # history-driven phases (commit capture, health, dead code, doc drift, the
+ # periodic re-score) have nothing new to read. The sync pointer stays put,
+ # so the next commit's update runs them over these same files.
+ working_tree_only = bool(
+ working_tree_diffs
+ and head
+ and head == base_ref
+ and not (
+ health_config_changed
+ or generation_config_changed
+ or renderer_changed
+ or analyzer_changed
+ or extraction_changed
+ )
+ )
+ head_ts = None if working_tree_only else _head_commit_ts(repo_path)
parsed_files, source_map, graph_builder, repo_structure, file_count, git_meta_map = (
_rebuild_graph_and_git(
repo_path,
@@ -1450,7 +1466,7 @@ def run_update(
git_tier=state.get("git_tier"),
include_submodules=bool(state.get("include_submodules", False)),
include_nested_repos=bool(state.get("include_nested_repos", False)),
- idle_decay_sink=git_decay_map,
+ idle_decay_sink=None if working_tree_only else git_decay_map,
force_full_git=git_config_changed,
git_summary_sink=full_git_summaries,
timings=timings,
@@ -1544,30 +1560,32 @@ def run_update(
# as "no commits" without the stored rows. The stored function-mod p80
# keeps the hotspot gate repo-wide instead of derived from the changed
# subset (issue #1484).
- with timed(timings, "analysis.stored_reads"):
- stored_git_meta = _load_stored_git_meta(repo_path)
- stored_performance_callers = _load_stored_performance_callers(repo_path, file_diffs)
- repo_function_mod_p80 = _load_stored_function_mod_p80(repo_path)
- partial_health_report, dead_code_report = _run_partial_analysis(
- repo_path,
- graph_builder,
- git_meta_map,
- parsed_files,
- file_diffs,
- source_map,
- stored_git_meta=stored_git_meta,
- stored_performance_callers=stored_performance_callers,
- repo_function_mod_p80=repo_function_mod_p80,
- timings=timings,
- )
- doc_drift_report = _run_doc_drift_partial(
- graph_builder,
- source_map,
- repo_path=repo_path,
- timings=timings,
- base_ref=base_ref,
- file_diffs=file_diffs,
- )
+ partial_health_report = dead_code_report = doc_drift_report = None
+ if not working_tree_only:
+ with timed(timings, "analysis.stored_reads"):
+ stored_git_meta = _load_stored_git_meta(repo_path)
+ stored_performance_callers = _load_stored_performance_callers(repo_path, file_diffs)
+ repo_function_mod_p80 = _load_stored_function_mod_p80(repo_path)
+ partial_health_report, dead_code_report = _run_partial_analysis(
+ repo_path,
+ graph_builder,
+ git_meta_map,
+ parsed_files,
+ file_diffs,
+ source_map,
+ stored_git_meta=stored_git_meta,
+ stored_performance_callers=stored_performance_callers,
+ repo_function_mod_p80=repo_function_mod_p80,
+ timings=timings,
+ )
+ doc_drift_report = _run_doc_drift_partial(
+ graph_builder,
+ source_map,
+ repo_path=repo_path,
+ timings=timings,
+ base_ref=base_ref,
+ file_diffs=file_diffs,
+ )
# Partial health has consumed the per-file ``BlameIndex``; drop it before
# the metadata reaches persistence / regeneration so the transient,
@@ -1575,21 +1593,28 @@ def run_update(
from repowise.core.pipeline.phases.git import drop_transient_git_signals
drop_transient_git_signals(list(git_meta_map.values()))
+ # With no new commit the stored git rows are already current, and an empty
+ # map is what keeps the persist from walking for new commits.
+ persisted_git_meta = {} if working_tree_only else git_meta_map
# Refresh the knowledge graph (layers/tour/entry points) when the graph
# shape changed — previously init-only, so update served a stale
# orientation snapshot to CLAUDE.md/get_overview forever (#669). None
# means fingerprint-unchanged: the persisted artifact is still current.
- with timed(timings, "knowledge_graph"):
- knowledge_graph_result = _refresh_knowledge_graph(
- repo_path,
- parsed_files,
- graph_builder,
- repo_structure,
- git_meta_map,
- dead_code_report,
- (state.get("knowledge_graph") or {}).get("fingerprint"),
- )
+ # Skipped before a commit: its curation reads the dead-code report, and
+ # the kept fingerprint lets the next commit's update rebuild it.
+ knowledge_graph_result = None
+ if not working_tree_only:
+ with timed(timings, "knowledge_graph"):
+ knowledge_graph_result = _refresh_knowledge_graph(
+ repo_path,
+ parsed_files,
+ graph_builder,
+ repo_structure,
+ git_meta_map,
+ dead_code_report,
+ (state.get("knowledge_graph") or {}).get("fingerprint"),
+ )
if generation_config_changed and knowledge_graph_result is not None:
# Repo-wide pages (especially onboarding's guided tour) read the
# persisted KG context during generation. Publish the freshly rebuilt
@@ -1741,7 +1766,7 @@ def run_update(
_persist_index_only_update(
repo_path,
graph_builder,
- git_meta_map,
+ persisted_git_meta,
dead_code_report,
partial_health_report,
state,
@@ -1918,7 +1943,8 @@ def run_update(
changed_paths = [fd.path for fd in file_diffs if fd.status in ("added", "modified")]
# The rescan is the inline_marker source; it was running regardless of
- # whether that source was switched off.
+ # whether that source was switched off. It also reads uncommitted
+ # files on purpose: a marker is source text, not history.
if changed_paths and decision_policy.source_enabled("inline_marker"):
extractor = DecisionExtractor(
repo_path=repo_path,
@@ -2399,7 +2425,7 @@ def run_update(
repo_name=repo_name,
generated_pages=generated_pages,
file_diffs=file_diffs,
- git_meta_map=git_meta_map,
+ git_meta_map=persisted_git_meta,
new_decision_markers=[*new_decision_markers, *session_decisions],
decision_vector_store=decision_vector_store,
provider=provider,
diff --git a/tests/unit/cli/test_update_working_tree.py b/tests/unit/cli/test_update_working_tree.py
index efe806032..727cde099 100644
--- a/tests/unit/cli/test_update_working_tree.py
+++ b/tests/unit/cli/test_update_working_tree.py
@@ -104,3 +104,176 @@ def test_workspace_update_indexes_uncommitted_work_with_working_tree(
assert result.exit_code == 0, result.output
nodes, _ = asyncio.run(_symbols_and_call_edges(repo))
assert "a.py::alpha_renamed" in nodes
+
+
+_EDITED_A = (
+ "def alpha():\n return 1\n\n\n"
+ "def gamma(x):\n if x:\n for i in range(x):\n"
+ " if i > 2:\n return i\n return 0\n"
+)
+_EDITED_B = "from a import alpha, gamma\n\n\ndef beta():\n return alpha() + gamma(4)\n"
+
+
+async def _store_view(repo: Path) -> dict:
+ """Symbols, call edges, health, dead-code and git rows of the two edited files."""
+ from repowise.core.persistence import create_engine, create_session_factory, get_session
+ from repowise.core.persistence.database import resolve_db_url
+ from repowise.core.persistence.models import (
+ DeadCodeFinding,
+ GitMetadata,
+ HealthFileMetric,
+ WikiSymbol,
+ )
+
+ edited = ("a.py", "b.py")
+
+ nodes, calls = await _symbols_and_call_edges(repo)
+ # The update also writes editor files that a clone never has.
+ nodes = {n for n in nodes if n.startswith(edited)}
+ engine = create_engine(resolve_db_url(repo))
+ try:
+ async with get_session(create_session_factory(engine)) as session:
+ symbols = {
+ (s.file_path, s.symbol_id, s.start_line, s.end_line)
+ for s in (await session.execute(select(WikiSymbol))).scalars()
+ }
+ health = {
+ m.file_path: (m.nloc, m.max_ccn, m.max_nesting, m.score)
+ for m in (await session.execute(select(HealthFileMetric))).scalars()
+ if m.file_path in edited
+ }
+ dead = {
+ (d.file_path, d.kind, d.symbol_name, d.start_line)
+ for d in (await session.execute(select(DeadCodeFinding))).scalars()
+ if d.file_path in edited
+ }
+ git = {
+ g.file_path: (g.commit_count_total, g.last_commit_at, g.co_change_partners_json)
+ for g in (await session.execute(select(GitMetadata))).scalars()
+ if g.file_path in edited
+ }
+ finally:
+ await engine.dispose()
+ return {
+ "nodes": nodes,
+ "calls": calls,
+ "symbols": symbols,
+ "health": health,
+ "dead_code": dead,
+ "git": git,
+ }
+
+
+def _spy(monkeypatch, owner, name: str) -> list:
+ calls: list = []
+ real = getattr(owner, name)
+
+ def _wrapped(*args, **kwargs):
+ calls.append(name)
+ return real(*args, **kwargs)
+
+ monkeypatch.setattr(owner, name, _wrapped)
+ return calls
+
+
+def _commit_edit(repo: Path) -> None:
+ # The first run's lock is released at exit, which never comes in-process.
+ update_command_mod.release_update_lock(repo)
+ _git(repo, "add", "a.py", "b.py")
+ _git(repo, "commit", "-m", "add gamma")
+
+
+def _phase_rows(repo: Path) -> set[str]:
+ from repowise.cli.helpers import load_state
+
+ return set(load_state(repo).get("phase_timings") or {})
+
+
+def test_working_tree_only_update_skips_history_phases_until_the_commit(
+ tmp_path: Path, monkeypatch
+) -> None:
+ from repowise.cli.helpers import load_state, save_state
+ from repowise.core.pipeline import incremental as core_incremental
+
+ repo = _make_git_repo(tmp_path)
+ _index_full(repo)
+ head = _git(repo, "rev-parse", "HEAD")
+ save_state(repo, {"last_sync_commit": head, "docs_enabled": False})
+ health_at_head = asyncio.run(_store_view(repo))["health"]
+
+ analysis = _spy(monkeypatch, update_command_mod, "_run_partial_analysis")
+ drift = _spy(monkeypatch, update_command_mod, "_run_doc_drift_partial")
+ commits = _spy(monkeypatch, core_incremental, "persist_incremental_commits")
+
+ (repo / "a.py").write_text(_EDITED_A)
+ (repo / "b.py").write_text(_EDITED_B)
+ result = CliRunner().invoke(cli, ["update", str(repo), "--no-workspace", "--working-tree"])
+ assert result.exit_code == 0, result.output
+
+ assert (analysis, drift, commits) == ([], [], [])
+ history_phases = {"persist.commits", "persist.health", "analysis.health", "rescore"}
+ assert not history_phases & _phase_rows(repo)
+ view = asyncio.run(_store_view(repo))
+ assert "a.py::gamma" in view["nodes"]
+ assert ("b.py::beta", "a.py::gamma") in view["calls"]
+ # Health still describes HEAD, and the pointer has not moved past it.
+ assert view["health"] == health_at_head
+ assert load_state(repo)["last_sync_commit"] == head
+
+ _commit_edit(repo)
+ result = CliRunner().invoke(cli, ["update", str(repo), "--no-workspace"])
+ assert result.exit_code == 0, result.output
+
+ assert analysis and drift and commits
+ # Each label is real: the commit's update records every one of them.
+ assert history_phases <= _phase_rows(repo)
+ assert load_state(repo)["last_sync_commit"] == _git(repo, "rev-parse", "HEAD")
+
+
+def test_working_tree_update_then_commit_converges_with_a_fresh_index(tmp_path: Path) -> None:
+ from repowise.cli.helpers import save_state
+
+ repo = _make_git_repo(tmp_path)
+ _index_full(repo)
+ save_state(repo, {"last_sync_commit": _git(repo, "rev-parse", "HEAD"), "docs_enabled": False})
+
+ (repo / "a.py").write_text(_EDITED_A)
+ (repo / "b.py").write_text(_EDITED_B)
+ runner = CliRunner()
+ wt = runner.invoke(cli, ["update", str(repo), "--no-workspace", "--working-tree"])
+ assert wt.exit_code == 0, wt.output
+ _commit_edit(repo)
+ plain = runner.invoke(cli, ["update", str(repo), "--no-workspace"])
+ assert plain.exit_code == 0, plain.output
+
+ fresh = tmp_path / "fresh"
+ _git(tmp_path, "clone", "-q", str(repo), str(fresh))
+ _index_full(fresh)
+
+ updated, expected = asyncio.run(_store_view(repo)), asyncio.run(_store_view(fresh))
+ assert expected["git"] and expected["dead_code"] and expected["health"]
+ assert updated == expected
+
+
+def test_working_tree_update_after_a_config_change_still_rescores(
+ tmp_path: Path, monkeypatch
+) -> None:
+ from repowise.cli.helpers import save_state
+
+ repo = _make_git_repo(tmp_path)
+ _index_full(repo)
+ # A fingerprint with no per-dependency breakdown reads as a full config change.
+ save_state(
+ repo,
+ {
+ "last_sync_commit": _git(repo, "rev-parse", "HEAD"),
+ "docs_enabled": False,
+ "config_fingerprint": "an-older-config",
+ },
+ )
+ analysis = _spy(monkeypatch, update_command_mod, "_run_partial_analysis")
+
+ (repo / "a.py").write_text(_EDITED_A)
+ result = CliRunner().invoke(cli, ["update", str(repo), "--no-workspace", "--working-tree"])
+ assert result.exit_code == 0, result.output
+ assert analysis