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
feat(update): refresh stored git history once when its rules change (#3386)
Raghav Chamadiya6ee30ef74e37
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/init_cmd/persistence.pyindex f412dae2c..4566391aa 100644--- a/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py+++ b/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py@@ -35,6 +35,7 @@ from repowise.core.index_scope import (file_page_scope,stamp_index_scope,)+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEYfrom repowise.core.repo_config import config_dependency_fingerprintslogger = structlog.get_logger(__name__)@@ -339,10 +340,15 @@ def effective_run_mode_for_resume(repo_path: Path, run_mode: str, resume: bool)def apply_git_history_coverage_state(state: dict[str, Any], result: Any) -> None:- """Replace achieved Git coverage, clearing stale data when unavailable."""+ """Replace achieved Git coverage, clearing stale data when unavailable.++ A run that walked git also wrote every row under the current history+ rules, so it stamps their version and the next update does not walk again.+ """summary = getattr(result, "git_summary", None)if summary is None:return+ state[GIT_HISTORY_VERSION_KEY] = GIT_HISTORY_VERSIONcoverage = getattr(summary, "history_coverage", None)if coverage is None:state.pop("git_history_coverage", None)diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/command.py b/packages/cli/src/repowise/cli/commands/update_cmd/command.pyindex 73da77e0d..ffec2638b 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/command.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/command.py@@ -41,6 +41,11 @@ from repowise.cli.helpers import (write_update_pending,)from repowise.core.docs_mode import docs_mode_state_fields, resolve_docs_mode+from repowise.core.ingestion.git_indexer import (+ GIT_HISTORY_VERSION,+ GIT_HISTORY_VERSION_KEY,+ git_history_stale,+)from repowise.core.pipeline import PhaseTimings, timedfrom repowise.core.reasoning import REASONING_MODES@@ -1012,6 +1017,10 @@ def run_update(_repair_module_attribution(repo_path)analyzer_changed = health_analyzer_changed(state)+ # Stored per-file git rows written under older history rules. Only a full+ # walk rewrites the rows of files no commit touched, so this forces one+ # (and nothing else: no page is regenerated for it).+ history_stale = git_history_stale(state)git_is_current = bool(headand head == base_ref@@ -1019,6 +1028,7 @@ def run_update(and not renderer_changedand not working_tree_diffsand not analyzer_changed+ and not history_stale)# The parse is read from the store, so it is asked only when git and the# analyzer would otherwise let the run exit.@@ -1034,6 +1044,11 @@ def run_update(if analyzer_changedelse "[yellow]Parser changed since this index was built; re-parsing.[/yellow]")+ if history_stale:+ console.print(+ "[yellow]Git history rules changed since this index was built; "+ "re-reading the full history once.[/yellow]"+ )# A page can be stale for a reason no commit explains: a cascade that ran# out of budget, an interrupted run, an expiry. Git says nothing changed,# so the shortcut below is the only place such a page could be skipped@@ -1308,6 +1323,7 @@ def run_update(and not renderer_changedand not analyzer_changedand not extraction_changed+ and not history_staleand not stale_db_pathsand not stale_deterministic_ids):@@ -1376,6 +1392,7 @@ def run_update(and not file_diffsand not git_config_changedand not generation_config_changed+ and not history_stale):if dry_run:action = (@@ -1472,6 +1489,7 @@ def run_update(or renderer_changedor analyzer_changedor extraction_changed+ or history_stale))head_ts = None if working_tree_only else _head_commit_ts(repo_path)@@ -1485,7 +1503,7 @@ def run_update(include_submodules=bool(state.get("include_submodules", False)),include_nested_repos=bool(state.get("include_nested_repos", False)),idle_decay_sink=None if working_tree_only else git_decay_map,- force_full_git=git_config_changed,+ force_full_git=git_config_changed or history_stale,git_summary_sink=full_git_summaries,timings=timings,)@@ -1806,6 +1824,10 @@ def run_update(head_ts=head_ts,force_full_rescore=health_config_changed,full_git_summary=(full_git_summaries[0] if full_git_summaries else None),+ # Only a history-window change invalidates mined decisions and+ # per-commit health; a walk forced by the history-rules stamp+ # keeps both.+ history_window_changed=git_config_changed,reconcile_full_scope=traversal_config_changed,full_generation_page_ids=({page.page_id for page in det_pages}@@ -2461,6 +2483,7 @@ def run_update(reconcile_full_generation=generation_config_changed,require_config_rebuild_success=config_rebuild_required,require_decision_persist_success=git_config_changed,+ history_window_changed=git_config_changed,timings=timings,)except Exception as exc:@@ -2553,6 +2576,8 @@ def run_update(state["git_history_coverage"] = coverage.to_dict()else:state.pop("git_history_coverage", None)+ # Every row was rewritten under the current rules.+ state[GIT_HISTORY_VERSION_KEY] = GIT_HISTORY_VERSIONsave_state(repo_path, state)# --- Pending-marker cleanup --------------------------------------------diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.pyindex c479f70e8..3c6fd5376 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py@@ -29,6 +29,7 @@ from repowise.cli.helpers import ()from repowise.core.analysis.health import HEALTH_ANALYZER_VERSIONfrom repowise.core.analysis.security_scan import SECURITY_SCANNER_VERSION+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEYfrom repowise.core.pipeline import PhaseTimings, timedfrom .incremental import _build_repo_graph@@ -437,6 +438,7 @@ def _persist_index_only_update(dependency_fingerprints: dict[str, str] | None = None,vector_store: Any | None = None,require_config_rebuild_success: bool = False,+ history_window_changed: bool = False,timings: PhaseTimings | None = None,) -> None:"""Persist the index-only update (graph + symbols + git + dead-code + health + KG),@@ -477,6 +479,7 @@ def _persist_index_only_update(parsed_files=parsed_files,git_decay_map=git_decay_map,full_git_summary=full_git_summary,+ history_window_changed=history_window_changed,reconcile_full_scope=reconcile_full_scope,full_generation_page_ids=full_generation_page_ids,vector_store=vector_store,@@ -557,6 +560,10 @@ def _persist_index_only_update(new_state["git_history_coverage"] = coverage.to_dict()else:new_state.pop("git_history_coverage", None)+ # Every row was rewritten under the current rules, unless they never+ # reached the store; then the next update walks again.+ if "Git persist" not in failed_steps:+ new_state[GIT_HISTORY_VERSION_KEY] = GIT_HISTORY_VERSION# Before save_state, and reading ``state`` (the pre-update dict) for the old# pointer: this is what keeps a degraded run recoverable now that the# pointer below advances to head regardless.@@ -715,6 +722,7 @@ def _persist_full_update(reconcile_full_generation: bool = False,require_config_rebuild_success: bool = False,require_decision_persist_success: bool = False,+ history_window_changed: bool = False,timings: PhaseTimings | None = None,) -> int:"""Persist a full (LLM-regenerating) update in one transaction.@@ -756,6 +764,7 @@ def _persist_full_update(reconcile_full_generation=reconcile_full_generation,require_config_rebuild_success=require_config_rebuild_success,require_decision_persist_success=require_decision_persist_success,+ history_window_changed=history_window_changed,timings=timings,))@@ -785,6 +794,7 @@ async def _persist_full_update_async(reconcile_full_generation: bool = False,require_config_rebuild_success: bool = False,require_decision_persist_success: bool = False,+ history_window_changed: bool = False,timings: PhaseTimings | None = None,) -> int:from repowise.cli.helpers import get_db_url_for_repo@@ -1030,10 +1040,15 @@ async def _persist_full_update_async(git_meta_map,git_decay_map,full_git_summary,+ # Set only on a history-window (git config) change,+ # the one walk that invalidates per-commit health.+ keep_commit_health=not history_window_changed,)except Exception as exc:_skip("Git persist", exc)- if require_config_rebuild_success:+ # A full walk stamps the history version once persisted,+ # so losing its rows here must fail the run, not stamp it.+ if require_config_rebuild_success or full_git_summary is not None:raisetry:with timed(timings, "persist.commits"):diff --git a/packages/cli/src/repowise/cli/commands/upgrade_flow.py b/packages/cli/src/repowise/cli/commands/upgrade_flow.pyindex 6d069e13f..68bcc1b1f 100644--- a/packages/cli/src/repowise/cli/commands/upgrade_flow.py+++ b/packages/cli/src/repowise/cli/commands/upgrade_flow.py@@ -47,6 +47,7 @@ from repowise.cli.helpers import (from repowise.core.analysis.health import HEALTH_ANALYZER_VERSIONfrom repowise.core.docs_mode import docs_mode_state_fieldsfrom repowise.core.index_scope import file_page_scope, stamp_index_scope+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEYfrom repowise.core.update_lock import release_update_lock, try_acquire_update_lock@@ -824,6 +825,8 @@ def upgrade_to_full(# from the current analyzer. Without the stamp the next plain `update`# would read a stale version and pay a redundant full re-score.state["health_analyzer_version"] = HEALTH_ANALYZER_VERSION+ # The git backfill walked every file under the current history rules.+ state[GIT_HISTORY_VERSION_KEY] = GIT_HISTORY_VERSIONstate["full_upgrade"] = {"status": "complete","retryable": False,diff --git a/packages/cli/src/repowise/cli/commands/workspace_cmd.py b/packages/cli/src/repowise/cli/commands/workspace_cmd.pyindex d8bb1728d..9f7b6f4c5 100644--- a/packages/cli/src/repowise/cli/commands/workspace_cmd.py+++ b/packages/cli/src/repowise/cli/commands/workspace_cmd.py@@ -648,6 +648,8 @@ def _run_index_for_repo(docs_skip_reason = f"generation error: {exc}"# Persist state.json so `repowise update` has a baseline commit.+ from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEY+head = get_head_commit(repo_path)state: dict = {"last_sync_commit": head,@@ -655,6 +657,8 @@ def _run_index_for_repo(# No template fallback on this path: without a provider the added repo# is indexed with no pages at all.**docs_mode_state_fields("llm" if generate_docs and provider is not None else "none"),+ # The full index just walked git under the current history rules.+ GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION,}if generate_docs and provider is not None:state["provider"] = provider.provider_namediff --git a/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py b/packages/core/src/repowise/core/ingestion/git_indexer/__init__.pyindex f32b8a9b6..40f88455c 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py@@ -19,7 +19,13 @@ This package was split out of a single 1k-line module; the public import pathfrom __future__ import annotations-from ._constants import HOTSPOT_HALFLIFE_DAYS, is_fix_commit+from ._constants import (+ GIT_HISTORY_VERSION,+ GIT_HISTORY_VERSION_KEY,+ HOTSPOT_HALFLIFE_DAYS,+ git_history_stale,+ is_fix_commit,+)from .agent_provenance import (AgentProvenance,AgentProvenanceClassifier,@@ -62,6 +68,8 @@ from .tiers import GitIndexTier__all__ = ["BACKFILL_PHASE",+ "GIT_HISTORY_VERSION",+ "GIT_HISTORY_VERSION_KEY","HOTSPOT_HALFLIFE_DAYS","SHAPE_KINDS",# Re-exported for ``git_commit_index`` which builds the shared commit index.@@ -95,6 +103,7 @@ __all__ = ["count_active_contributors","detect_original_path","get_blame_ownership",+ "git_history_stale","index_file","is_fix_commit","is_significant_commit",diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/_constants.py b/packages/core/src/repowise/core/ingestion/git_indexer/_constants.pyindex 0396aacef..7e5a1b6da 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/_constants.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/_constants.py@@ -327,6 +327,29 @@ _CO_CHANGE_COMMIT_DECAY_TAU: float = CO_CHANGE_COMMIT_DECAY_TAU# Hotspot temporal decay: half-life for exponentially weighted churn score.HOTSPOT_HALFLIFE_DAYS: float = 180.0+#: The rules that turn git history into stored per-file rows (change numbers,+#: author identity). Stamped into state.json as ``git_history_version`` by every+#: full git walk; an update that finds another value, or none, walks the whole+#: history once instead of only the changed files, since a changed file's row is+#: the only one an incremental walk rewrites. Bump it when such a rule changes.+#:+#: v1: change numbers and author identity read through ``repowise.core.forges``+#: (GitLab ``!N`` merge requests, Azure and Bitbucket PRs, GitLab noreply and+#: forge bot accounts).+GIT_HISTORY_VERSION: int = 1+GIT_HISTORY_VERSION_KEY = "git_history_version"+++def git_history_stale(state: dict[str, Any]) -> bool:+ """Whether the stored git rows predate :data:`GIT_HISTORY_VERSION`.++ Unlike the health analyzer stamp, a missing stamp counts as stale: the+ rows of an index written before it carry the change-number and identity+ defects v1 fixes, and only a full walk rewrites the rows of files that+ do not change.+ """+ return state.get(GIT_HISTORY_VERSION_KEY) != GIT_HISTORY_VERSION+# Absolute activity floors for hotspot classification (issue #361). The# churn percentile is repo-relative, so on a quiet repo "top quartile"# degenerates to "any file touched in the last 90 days" — a single drive-bydiff --git a/packages/core/src/repowise/core/pipeline/incremental.py b/packages/core/src/repowise/core/pipeline/incremental.pyindex 90881954d..0b8fb88c1 100644--- a/packages/core/src/repowise/core/pipeline/incremental.py+++ b/packages/core/src/repowise/core/pipeline/incremental.py@@ -1837,6 +1837,7 @@ async def persist_incremental_index(parsed_files: list[Any] | None = None,git_decay_map: dict | None = None,full_git_summary: Any | None = None,+ history_window_changed: bool = False,reconcile_full_scope: bool = False,full_generation_page_ids: set[str] | None = None,vector_store: Any | None = None,@@ -2012,6 +2013,7 @@ async def persist_incremental_index(git_meta_map,git_decay_map,full_git_summary,+ keep_commit_health=not history_window_changed,)except Exception as exc:_skip("Git persist", exc, range_scoped=True)@@ -2163,7 +2165,9 @@ async def persist_incremental_index()await purge_proposed_decisions_by_source(session, repo_id, "code_comment")- if full_git_summary is not None:+ # A history-window change re-mines these; a walk that only+ # refreshes rows under new rules does not, so it keeps them.+ if full_git_summary is not None and history_window_changed:await purge_proposed_decisions_by_source(session, repo_id, "git_archaeology")diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.pyindex 9897db446..55dc13cd9 100644--- a/packages/core/src/repowise/core/pipeline/persist.py+++ b/packages/core/src/repowise/core/pipeline/persist.py@@ -1778,8 +1778,18 @@ async def replace_git_history(repo_id: str,git_meta_map: dict[str, dict],git_summary: Any,+ *,+ keep_commit_health: bool = False,) -> None:- """Replace every history-derived row from one authoritative Git walk."""+ """Replace every history-derived row from one authoritative Git walk.++ ``keep_commit_health`` keeps the per-commit health rows. A walk that only+ re-reads the same window under new rules leaves each commit's code, so its+ health is still right, and the capped ``refresh_commit_health`` could not+ rebuild rows past its budget. Those rows key on ``(repository_id, sha)``+ alone, so nothing dangles; unreachable shas are pruned by the update's+ orphan sweep.+ """from types import SimpleNamespacefrom sqlalchemy import delete@@ -1796,14 +1806,8 @@ async def replace_git_history(# Function blame describes the current source tree and is produced by the# health pass, not by persist_git. Preserve it across a history-window# replacement; scope reconciliation prunes entries for excluded files.- for model in (- FixEvent,- GitCommit,- GitCommitFile,- GitCommitHealthDelta,- GitCommitHealthFinding,- GitMetadata,- ):+ health = () if keep_commit_health else (GitCommitHealthDelta, GitCommitHealthFinding)+ for model in (FixEvent, GitCommit, GitCommitFile, *health, GitMetadata):await session.execute(delete(model).where(model.repository_id == repo_id))await persist_git(SimpleNamespace(@@ -1821,10 +1825,18 @@ async def persist_git_refresh(git_meta_map: dict[str, dict],git_decay_map: dict[str, dict] | None,full_git_summary: Any | None,+ *,+ keep_commit_health: bool = False,) -> None:"""Persist either a full history replacement or an incremental refresh."""if full_git_summary is not None:- await replace_git_history(session, repo_id, git_meta_map, full_git_summary)+ await replace_git_history(+ session,+ repo_id,+ git_meta_map,+ full_git_summary,+ keep_commit_health=keep_commit_health,+ )returnfrom sqlalchemy import selectdiff --git a/packages/core/src/repowise/core/workspace/update.py b/packages/core/src/repowise/core/workspace/update.pyindex ec2fb2bda..9d2d4e3d7 100644--- a/packages/core/src/repowise/core/workspace/update.py+++ b/packages/core/src/repowise/core/workspace/update.py@@ -46,6 +46,11 @@ from repowise.core.update_lock import (from ..docs_mode import docs_mode_state_fieldsfrom ..ingestion.change_detector import has_working_tree_changes+from ..ingestion.git_indexer import (+ GIT_HISTORY_VERSION,+ GIT_HISTORY_VERSION_KEY,+ git_history_stale,+)from ..pipeline.phase_timing import PhaseTimingRecorderfrom .config import WorkspaceConfig@@ -156,6 +161,9 @@ class RepoUpdateResult:# commit-anchored path, which leaves the stored list alone.working_tree_paths: list[str] | None = Nonephase_timings: dict[str, float] | None = None+ # True when this run walked the whole git history and persisted every row,+ # so the caller stamps ``git_history_version``.+ git_history_refreshed: bool = False# ---------------------------------------------------------------------------@@ -413,6 +421,7 @@ async def _incremental_repo_update(base_ref: str,exclude_patterns: list[str] | None = None,include_working_tree: bool = False,+ force_full_git: bool = False,) -> RepoUpdateResult | None:"""Refresh an already-indexed repo through the incremental update path.@@ -458,7 +467,7 @@ async def _incremental_repo_update(detector.get_changed_files(base_ref, head),working_tree_diffs,)- if not file_diffs:+ if not file_diffs and not force_full_git:# New commits but nothing the index cares about changed (merge/empty# commits, or every change excluded). Report success so the caller# bumps ``last_sync_commit`` instead of re-diffing forever.@@ -475,6 +484,7 @@ async def _incremental_repo_update(# persisted alongside the changed rows, kept out of git_meta_map so partial# health's repo-wide aggregates are unaffected (mirrors the CLI path).git_decay_map: dict[str, dict] = {}+ full_git_summaries: list[Any] = [](parsed_files,source_map,@@ -491,6 +501,8 @@ async def _incremental_repo_update(include_submodules=bool(state.get("include_submodules", False)),include_nested_repos=bool(state.get("include_nested_repos", False)),idle_decay_sink=git_decay_map,+ force_full_git=force_full_git,+ git_summary_sink=full_git_summaries,log=_log.info,)@@ -564,6 +576,8 @@ async def _incremental_repo_update(log=_log.info,)+ # A full walk is stamped only when its rows reached the store.+ failed_steps: list[str] = []await persist_incremental_index(repo_path,graph_builder,@@ -580,6 +594,8 @@ async def _incremental_repo_update(knowledge_graph_result=kg,parsed_files=parsed_files,git_decay_map=git_decay_map,+ full_git_summary=full_git_summaries[0] if full_git_summaries else None,+ failed_steps=failed_steps,log=_log.info,)@@ -600,6 +616,7 @@ async def _incremental_repo_update(symbol_count=sum(len(pf.symbols) for pf in parsed_files),kg_state=kg_state,working_tree_paths=working_tree_paths,+ git_history_refreshed=bool(full_git_summaries) and "Git persist" not in failed_steps,)@@ -723,6 +740,7 @@ async def update_single_repo_index(base_ref=str(base_ref),exclude_patterns=exclude_patterns,include_working_tree=include_working_tree,+ force_full_git=git_history_stale(state),)if incremental_result is not None:return incremental_result@@ -763,6 +781,7 @@ async def update_single_repo_index(file_count=result.file_count,symbol_count=result.symbol_count,kg_state=kg_state,+ git_history_refreshed=getattr(result, "git_summary", None) is not None,)except Exception as exc:return RepoUpdateResult(@@ -893,6 +912,15 @@ async def update_workspace(if not is_stale and include_working_tree and has_working_tree_changes(abs_path):is_stale = True+ # Rows written under older git history rules: one full walk refreshes+ # them, as the single-repo update does, even with HEAD unmoved.+ if (+ not is_stale+ and git_history_stale(state)+ and await _has_persisted_repo_index(abs_path)+ ):+ is_stale = True+if not is_stale:# Nothing to regenerate, but the DB freshness stamp can still be# behind (e.g. a row left drifted by a pre-fix run). Reconcile it so@@ -1038,6 +1066,8 @@ async def update_workspace(state["working_tree_paths"] = result.working_tree_pathsif result.phase_timings is not None:state["phase_timings"] = result.phase_timings+ if result.git_history_refreshed:+ state[GIT_HISTORY_VERSION_KEY] = GIT_HISTORY_VERSION# Stamp the config fingerprint so the drift check in# update_single_repo_index stays calibrated (and legacy repos# without one stop re-triggering the full re-index).diff --git a/packages/server/src/repowise/server/job_state.py b/packages/server/src/repowise/server/job_state.pyindex 870c7d153..417ce85f6 100644--- a/packages/server/src/repowise/server/job_state.py+++ b/packages/server/src/repowise/server/job_state.py@@ -75,6 +75,7 @@ def _persist_initial_index_state(and style the run used, so later CLI ``update`` runs and server jobsresolve the same configuration."""+ from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEYfrom repowise.core.repo_config import config_fingerprint# config.yaml is written first: the fingerprint below covers it.@@ -95,6 +96,8 @@ def _persist_initial_index_state(state["docs_skip_reason"] = docs_skip_reasonstate["run_mode"] = "standard"state["git_tier"] = "full"+ # The full index just walked git under the current history rules.+ state[GIT_HISTORY_VERSION_KEY] = GIT_HISTORY_VERSIONstate["include_submodules"] = Falsestate["total_pages"] = total_pagesif llm_client is not None:diff --git a/tests/unit/cli/test_update_git_history_stamp.py b/tests/unit/cli/test_update_git_history_stamp.pynew file mode 100644index 000000000..828299abe--- /dev/null+++ b/tests/unit/cli/test_update_git_history_stamp.py@@ -0,0 +1,195 @@+"""Stored git rows written under older history rules are refreshed once.++Only a full git walk rewrites the per-file rows of files no commit touched, so+``git_history_version`` (missing counts as stale) forces exactly one on the next+update, and nothing else: no page is regenerated for it.+"""++from __future__ import annotations++import asyncio+from types import SimpleNamespace++import pytest++from repowise.cli.commands.init_cmd.persistence import apply_git_history_coverage_state+from repowise.cli.helpers import load_state, save_state+from repowise.core.analysis.health import HEALTH_ANALYZER_VERSION+from repowise.core.ingestion.git_indexer import (+ GIT_HISTORY_VERSION,+ GIT_HISTORY_VERSION_KEY,+ GitIndexer,+ git_history_stale,+)++from .test_update_up_to_date_lock import _indexed_repo, _invoke_update++UP_TO_DATE = "Already up to date"+++def walks(monkeypatch: pytest.MonkeyPatch) -> list[int]:+ """Count full git walks (``GitIndexer.index_repo``)."""+ calls: list[int] = []+ real = GitIndexer.index_repo++ async def _spy(self, *a, **k):+ calls.append(1)+ return await real(self, *a, **k)++ monkeypatch.setattr(GitIndexer, "index_repo", _spy)+ return calls+++def persist_kwargs(monkeypatch: pytest.MonkeyPatch) -> list[dict]:+ """The index-only persist call's scope flags."""+ from repowise.cli.commands.update_cmd import command as upd_cmd++ seen: list[dict] = []+ real = upd_cmd._persist_index_only_update++ def _spy(*a, **k):+ seen.append(k)+ return real(*a, **k)++ monkeypatch.setattr(upd_cmd, "_persist_index_only_update", _spy)+ return seen+++def _current(repo, **extra) -> None:+ save_state(+ repo,+ {**load_state(repo), "health_analyzer_version": HEALTH_ANALYZER_VERSION, **extra},+ )++[email protected]("stored", [None, GIT_HISTORY_VERSION - 1])+def test_stale_stamp_walks_once_then_updates_incrementally(+ tmp_path, walks, persist_kwargs, stored+) -> None:+ repo, c2 = _indexed_repo(tmp_path)+ walks.clear() # the fixture's own index walked once+ state = {**load_state(repo), "health_analyzer_version": HEALTH_ANALYZER_VERSION}+ state.pop(GIT_HISTORY_VERSION_KEY, None) # an index written before the stamp+ if stored is not None:+ state[GIT_HISTORY_VERSION_KEY] = stored+ save_state(repo, state)++ out = _invoke_update(repo)++ assert UP_TO_DATE not in out+ assert "Git history rules changed" in out+ assert len(walks) == 1+ state = load_state(repo)+ assert state[GIT_HISTORY_VERSION_KEY] == GIT_HISTORY_VERSION+ assert state["last_sync_commit"] == c2+ # The forced walk regenerates nothing: no full-scope reconcile, no page+ # set, no config-driven re-score.+ (kwargs,) = persist_kwargs+ assert kwargs["full_git_summary"] is not None+ assert kwargs["full_generation_page_ids"] is None+ assert kwargs["reconcile_full_scope"] is False+ assert kwargs["force_full_rescore"] is False+ assert kwargs["require_config_rebuild_success"] is False+ # Mined git-archaeology decisions survive: nothing re-mines them here.+ assert kwargs["history_window_changed"] is False++ # Stamped, so the next run is quiet and walks nothing.+ assert UP_TO_DATE in _invoke_update(repo)+ assert len(walks) == 1+++def test_current_stamp_forces_no_walk(tmp_path, walks) -> None:+ repo, _ = _indexed_repo(tmp_path)+ walks.clear()+ _current(repo, **{GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION})++ assert UP_TO_DATE in _invoke_update(repo)+ assert not walks+++def test_missing_stamp_is_stale() -> None:+ assert git_history_stale({})+ assert git_history_stale({GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION - 1})+ assert not git_history_stale({GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION})+++def test_init_stamps_only_when_git_ran() -> None:+ walked: dict = {}+ apply_git_history_coverage_state(walked, SimpleNamespace(git_summary=SimpleNamespace()))+ assert walked[GIT_HISTORY_VERSION_KEY] == GIT_HISTORY_VERSION++ skipped: dict = {}+ apply_git_history_coverage_state(skipped, SimpleNamespace(git_summary=None))+ assert GIT_HISTORY_VERSION_KEY not in skipped+++def test_workspace_update_walks_a_stale_repo_once(tmp_path, walks) -> None:+ from repowise.core.workspace.update import update_single_repo_index++ repo, _ = _indexed_repo(tmp_path)+ walks.clear()+ state = load_state(repo)+ state.pop(GIT_HISTORY_VERSION_KEY)+ save_state(repo, state)++ result = asyncio.run(update_single_repo_index(repo))+ assert result.updated and result.git_history_refreshed+ assert len(walks) == 1++ _current(repo, **{GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION})+ result = asyncio.run(update_single_repo_index(repo))+ assert result.updated and not result.git_history_refreshed+ assert len(walks) == 1+++def _health_shas(repo) -> dict[str, int]:+ from sqlalchemy import select++ 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 GitCommitHealthDelta++ async def _read() -> dict[str, int]:+ engine = create_engine(resolve_db_url(repo))+ try:+ async with get_session(create_session_factory(engine)) as session:+ rows = (await session.execute(select(GitCommitHealthDelta))).scalars()+ return {r.sha: r.introduced_count for r in rows}+ finally:+ await engine.dispose()++ return asyncio.run(_read())+++def test_stamp_forced_walk_keeps_commit_health(tmp_path) -> None:+ """The stamp re-reads the same window under new rules, so per-commit health+ (capped when rebuilt) survives; only a history-window change wipes it."""+ from sqlalchemy import select++ 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 GitCommitHealthDelta, Repository++ repo, _ = _indexed_repo(tmp_path)+ old_sha = "a" * 40 # a commit past what a rebuild would rescan++ async def _seed() -> None:+ engine = create_engine(resolve_db_url(repo))+ try:+ async with get_session(create_session_factory(engine)) as session:+ repo_id = (await session.execute(select(Repository.id))).scalar_one()+ session.add(+ GitCommitHealthDelta(repository_id=repo_id, sha=old_sha, introduced_count=7)+ )+ finally:+ await engine.dispose()++ asyncio.run(_seed())+ state = {**load_state(repo), "health_analyzer_version": HEALTH_ANALYZER_VERSION}+ state.pop(GIT_HISTORY_VERSION_KEY)+ save_state(repo, state)++ assert "Git history rules changed" in _invoke_update(repo)+ assert _health_shas(repo).get(old_sha) == 7diff --git a/tests/unit/cli/test_update_progress_json.py b/tests/unit/cli/test_update_progress_json.pyindex 375e2bd76..a7cf5e86e 100644--- a/tests/unit/cli/test_update_progress_json.py+++ b/tests/unit/cli/test_update_progress_json.py@@ -17,6 +17,10 @@ from click.testing import CliRunnerfrom repowise.cli.commands.update_cmd.reporting import JsonProgressEmitterfrom repowise.cli.main import cli+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEY++# An index written by this build: its git rows follow the current rules.+CURRENT_GIT_HISTORY = {GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION}class TestJsonProgressEmitter:@@ -145,7 +149,7 @@ class TestUpdateProgressJsonCli:os.makedirs(os.path.join(td, ".repowise"))state_path = os.path.join(td, ".repowise", "state.json")with open(state_path, "w") as f:- json.dump({"last_sync_commit": "deadbeef"}, f)+ json.dump({"last_sync_commit": "deadbeef", **CURRENT_GIT_HISTORY}, f)result = runner.invoke(cli, ["update", td, "--since", "deadbeef", "--progress", "json"])diff --git a/tests/unit/cli/test_update_reconciles_stale_pages.py b/tests/unit/cli/test_update_reconciles_stale_pages.pyindex 8637fd476..d1d74ea51 100644--- a/tests/unit/cli/test_update_reconciles_stale_pages.py+++ b/tests/unit/cli/test_update_reconciles_stale_pages.py@@ -15,6 +15,11 @@ from pathlib import Pathfrom sqlalchemy import select+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEY++# An index written by this build: its git rows follow the current rules.+CURRENT_GIT_HISTORY = {GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION}+def _git(repo: Path, *args: str) -> str:return subprocess.run(@@ -103,7 +108,12 @@ def _state_at(repo: Path, head: str) -> None:save_state(repo,- {"last_sync_commit": head, "last_docs_commit": head, "docs_mode": "deterministic"},+ {+ "last_sync_commit": head,+ "last_docs_commit": head,+ "docs_mode": "deterministic",+ **CURRENT_GIT_HISTORY,+ },)diff --git a/tests/unit/cli/test_update_up_to_date_lock.py b/tests/unit/cli/test_update_up_to_date_lock.pyindex 45d6a6689..9f20388bb 100644--- a/tests/unit/cli/test_update_up_to_date_lock.py+++ b/tests/unit/cli/test_update_up_to_date_lock.py@@ -18,6 +18,10 @@ import pytestfrom repowise.cli.commands.update_cmd import command as upd_cmdfrom repowise.cli.helpers import save_state, write_update_pending+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEY++# An index written by this build: its git rows follow the current rules.+CURRENT_GIT_HISTORY = {GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION}def _git(repo: Path, *args: str) -> str:@@ -64,7 +68,15 @@ def _indexed_repo(tmp_path: Path) -> tuple[Path, str]:asyncio.run(index_repo_full(repo))# index_repo_full does not persist state.json in the test harness; record# the sync/docs pointers at HEAD so the update resolves to "already current".- save_state(repo, {"last_sync_commit": c2, "last_docs_commit": c2, "docs_enabled": False})+ save_state(+ repo,+ {+ "last_sync_commit": c2,+ "last_docs_commit": c2,+ "docs_enabled": False,+ **CURRENT_GIT_HISTORY,+ },+ )return repo, c2diff --git a/tests/unit/cli/test_update_working_tree.py b/tests/unit/cli/test_update_working_tree.pyindex 727cde099..78f34e9c2 100644--- a/tests/unit/cli/test_update_working_tree.py+++ b/tests/unit/cli/test_update_working_tree.py@@ -10,8 +10,12 @@ from sqlalchemy import selectfrom repowise.cli.commands.update_cmd import command as update_command_modfrom repowise.cli.main import cli+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEYfrom tests.unit.cli.test_update_e2e import _git, _index_full, _make_git_repo+# An index written by this build: its git rows follow the current rules.+CURRENT_GIT_HISTORY = {GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION}+def _capture_run_update(monkeypatch) -> dict:seen: dict = {}@@ -63,7 +67,7 @@ def test_uncommitted_rename_is_indexed_with_working_tree(tmp_path: Path) -> Nonerepo = _make_git_repo(tmp_path)_index_full(repo)head = _git(repo, "rev-parse", "HEAD")- save_state(repo, {"last_sync_commit": head, "docs_enabled": False})+ save_state(repo, {"last_sync_commit": head, "docs_enabled": False, **CURRENT_GIT_HISTORY})# Rename the function and its caller's call site, without committing.(repo / "a.py").write_text("def alpha_renamed():\n return 1\n")@@ -89,7 +93,14 @@ def test_workspace_update_indexes_uncommitted_work_with_working_tree(repo = _make_workspace(tmp_path)_index_full(repo)- save_state(repo, {"last_sync_commit": _git(repo, "rev-parse", "HEAD"), "docs_enabled": False})+ save_state(+ repo,+ {+ "last_sync_commit": _git(repo, "rev-parse", "HEAD"),+ "docs_enabled": False,+ **CURRENT_GIT_HISTORY,+ },+ )monkeypatch.chdir(repo)# Records the indexed head in the workspace config, so only the edit below is new.assert CliRunner().invoke(cli, ["update", "--workspace"]).exit_code == 0@@ -198,7 +209,7 @@ def test_working_tree_only_update_skips_history_phases_until_the_commit(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})+ save_state(repo, {"last_sync_commit": head, "docs_enabled": False, **CURRENT_GIT_HISTORY})health_at_head = asyncio.run(_store_view(repo))["health"]analysis = _spy(monkeypatch, update_command_mod, "_run_partial_analysis")@@ -235,7 +246,14 @@ def test_working_tree_update_then_commit_converges_with_a_fresh_index(tmp_path:repo = _make_git_repo(tmp_path)_index_full(repo)- save_state(repo, {"last_sync_commit": _git(repo, "rev-parse", "HEAD"), "docs_enabled": False})+ save_state(+ repo,+ {+ "last_sync_commit": _git(repo, "rev-parse", "HEAD"),+ "docs_enabled": False,+ **CURRENT_GIT_HISTORY,+ },+ )(repo / "a.py").write_text(_EDITED_A)(repo / "b.py").write_text(_EDITED_B)@@ -269,6 +287,7 @@ def test_working_tree_update_after_a_config_change_still_rescores("last_sync_commit": _git(repo, "rev-parse", "HEAD"),"docs_enabled": False,"config_fingerprint": "an-older-config",+ **CURRENT_GIT_HISTORY,},)analysis = _spy(monkeypatch, update_command_mod, "_run_partial_analysis")diff --git a/tests/unit/cli/test_workspace_autodetect_e2e.py b/tests/unit/cli/test_workspace_autodetect_e2e.pyindex 71dd7fe31..4bc13ec47 100644--- a/tests/unit/cli/test_workspace_autodetect_e2e.py+++ b/tests/unit/cli/test_workspace_autodetect_e2e.py@@ -15,6 +15,10 @@ from click.testing import CliRunnerfrom repowise.cli.commands.status_cmd import status_commandfrom repowise.cli.commands.update_cmd import update_command+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEY++# An index written by this build: its git rows follow the current rules.+CURRENT_GIT_HISTORY = {GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION}def _build_workspace(root: Path) -> None:@@ -24,7 +28,7 @@ def _build_workspace(root: Path) -> None:(frontend / ".git").mkdir(parents=True)(backend / ".repowise").mkdir()(backend / ".repowise" / "state.json").write_text(- json.dumps({"last_sync_commit": "abc123", "docs_enabled": True}),+ json.dumps({"last_sync_commit": "abc123", "docs_enabled": True, **CURRENT_GIT_HISTORY}),)(root / ".repowise-workspace.yaml").write_text("version: 1\n"diff --git a/tests/unit/cli/test_workspace_docs_pointer.py b/tests/unit/cli/test_workspace_docs_pointer.pyindex c13bc1dce..6f5ad8b79 100644--- a/tests/unit/cli/test_workspace_docs_pointer.py+++ b/tests/unit/cli/test_workspace_docs_pointer.py@@ -17,8 +17,12 @@ from click.testing import CliRunnerfrom repowise.cli.helpers import load_state, save_statefrom repowise.cli.main import cli+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEYfrom repowise.core.workspace.config import RepoEntry, WorkspaceConfig+# An index written by this build: its git rows follow the current rules.+CURRENT_GIT_HISTORY = {GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION}+DOCS_POINTER_KEY = "last_docs_commit"SYNC_POINTER_KEY = "last_sync_commit"@@ -81,7 +85,9 @@ def test_workspace_update_backfills_docs_pointer(tmp_path: Path) -> None:# Simulate a state.json written before last_docs_commit existed. docs are# disabled so `update --workspace` resolves to the index-only path — the# path this pointer-backfill guard is about.- save_state(repo, {SYNC_POINTER_KEY: c0, "docs_enabled": False})+ save_state(+ repo, {SYNC_POINTER_KEY: c0, "docs_enabled": False, **CURRENT_GIT_HISTORY}+ )assert DOCS_POINTER_KEY not in load_state(repo)c1 = _commit_change(repo, "c.py", "def gamma():\n return 3\n", "add c.py")@@ -111,7 +117,9 @@ def test_stale_prose_is_reachable_after_workspace_update(tmp_path: Path) -> Nonec0 = _git(repo, "rev-parse", "HEAD")# docs disabled -> `update --workspace` stays index-only and only walks the# sync pointer, which is the scenario that used to strand the docs pointer.- save_state(repo, {SYNC_POINTER_KEY: c0, "docs_enabled": False})+ save_state(+ repo, {SYNC_POINTER_KEY: c0, "docs_enabled": False, **CURRENT_GIT_HISTORY}+ )c1 = _commit_change(repo, "c.py", "def gamma():\n return 3\n", "add c.py")old_cwd = os.getcwd()@@ -153,7 +161,10 @@ def test_workspace_update_regenerates_docs_for_docs_enabled_repo(tmp_path: Path)_index_full(repo)c0 = _git(repo, "rev-parse", "HEAD")- save_state(repo, {SYNC_POINTER_KEY: c0, DOCS_POINTER_KEY: c0, "docs_enabled": True})+ save_state(+ repo,+ {SYNC_POINTER_KEY: c0, DOCS_POINTER_KEY: c0, "docs_enabled": True, **CURRENT_GIT_HISTORY},+ )c1 = _commit_change(repo, "c.py", "def gamma():\n return 3\n", "add c.py")@@ -186,7 +197,10 @@ def test_workspace_docs_update_reports_deferral_not_regeneration(tmp_path: Path)_index_full(repo)c0 = _git(repo, "rev-parse", "HEAD")- save_state(repo, {SYNC_POINTER_KEY: c0, DOCS_POINTER_KEY: c0, "docs_enabled": True})+ save_state(+ repo,+ {SYNC_POINTER_KEY: c0, DOCS_POINTER_KEY: c0, "docs_enabled": True, **CURRENT_GIT_HISTORY},+ )c1 = _commit_change(repo, "c.py", "def gamma():\n return 3\n", "add c.py")@@ -224,7 +238,7 @@ def test_docs_early_exit_advances_docs_pointer(tmp_path: Path) -> None:c0 = _git(repo, "rev-parse", "HEAD")# Legacy state: no docs pointer recorded yet.- save_state(repo, {SYNC_POINTER_KEY: c0, "docs_enabled": True})+ save_state(repo, {SYNC_POINTER_KEY: c0, "docs_enabled": True, **CURRENT_GIT_HISTORY})_git(repo, "commit", "--allow-empty", "-m", "empty")c1 = _git(repo, "rev-parse", "HEAD")@@ -251,7 +265,10 @@ def test_index_only_early_exit_advances_sync_pointer(tmp_path: Path) -> None:c0 = _git(repo, "rev-parse", "HEAD")# docs disabled -> a plain `update` resolves to index-only mode.- save_state(repo, {SYNC_POINTER_KEY: c0, DOCS_POINTER_KEY: c0, "docs_enabled": False})+ save_state(+ repo,+ {SYNC_POINTER_KEY: c0, DOCS_POINTER_KEY: c0, "docs_enabled": False, **CURRENT_GIT_HISTORY},+ )_git(repo, "commit", "--allow-empty", "-m", "empty")c1 = _git(repo, "rev-parse", "HEAD")diff --git a/tests/unit/workspace/test_incremental_update.py b/tests/unit/workspace/test_incremental_update.pyindex 6a46624de..5287798f5 100644--- a/tests/unit/workspace/test_incremental_update.py+++ b/tests/unit/workspace/test_incremental_update.py@@ -19,6 +19,7 @@ from pathlib import Pathimport pytest+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEYfrom repowise.core.workspace.update import (commit_exists,get_head_commit,@@ -26,6 +27,9 @@ from repowise.core.workspace.update import (update_single_repo_index,)+# An index written by this build: its git rows follow the current rules.+CURRENT_GIT_HISTORY = {GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION}+# ---------------------------------------------------------------------------# Helpers# ---------------------------------------------------------------------------@@ -53,7 +57,7 @@ def _mark_indexed(repo: Path, commit: str, **extra_state) -> None:state_dir = repo / ".repowise"state_dir.mkdir(parents=True, exist_ok=True)(state_dir / "state.json").write_text(- json.dumps({"last_sync_commit": commit, **extra_state}), encoding="utf-8"+ json.dumps({"last_sync_commit": commit, **CURRENT_GIT_HISTORY, **extra_state}), encoding="utf-8")(state_dir / "wiki.db").touch()diff --git a/tests/unit/workspace/test_update.py b/tests/unit/workspace/test_update.pyindex da546148b..0b5a63529 100644--- a/tests/unit/workspace/test_update.py+++ b/tests/unit/workspace/test_update.py@@ -10,6 +10,7 @@ from unittest.mock import AsyncMock, patchimport pytest+from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEYfrom repowise.core.workspace.config import RepoEntry, WorkspaceConfigfrom repowise.core.workspace.update import (RepoUpdateResult,@@ -141,6 +142,7 @@ def _write_state(repo: Path, commit: str | None) -> None:state = {}if commit:state["last_sync_commit"] = commit+ state[GIT_HISTORY_VERSION_KEY] = GIT_HISTORY_VERSION(state_dir / "state.json").write_text(json.dumps(state), encoding="utf-8")