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
fix(update): stamp the parser at init and read a missing stamp as a change (#3558)
Raghav Chamadiyaa882b994fd9a
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.pyindex 2dfa1504d..136e0cceb 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py@@ -1829,9 +1829,13 @@ def parser_changed(repo_path: Path) -> bool:"""Whether the stored graph edges were written by a different parser.Compares the fingerprint ``persist_incremental_edges`` stamped on the repo- row with the running ``parser_fingerprint()``. An unstamped row, or a store- that cannot be read, is **not** a change, for the reason- :func:`health_analyzer_changed` gives: the next commit's widen stamps it.+ row with the running ``parser_fingerprint()``. An unstamped row **is** a+ change: ``init`` stamps a fresh store, so an unstamped one was written+ before the stamp or re-indexed over older edges, and the re-parse this+ triggers is the widen that reconciles and stamps it. Reading absence as+ current let a parser-only release report "already up to date" on a quiet+ checkout. A store that cannot be read, has no repository row, or has no+ edges to reconcile, is not.Also true when a test file was never checked for walking the source tree(an index built before that check): selection would run everything until@@ -1846,26 +1850,33 @@ def parser_changed(repo_path: Path) -> bool:get_repository_by_path,get_session,)+ from repowise.core.pipeline.persist import has_graph_edges- async def _stored() -> tuple[str | None, bool]:+ async def _stored() -> bool:engine = create_engine(get_db_url_for_repo(repo_path))try:sf = create_session_factory(engine)async with get_session(sf) as session:repo = await get_repository_by_path(session, str(repo_path))if repo is None:- return None, False- unscanned = bool(await unscanned_test_files(session, repo.id))- return repo.graph_edges_parser_fingerprint, unscanned+ return False+ stored = repo.graph_edges_parser_fingerprint+ if stored is None:+ # Unstamped with no edges is a store nothing ever indexed+ # into: there is nothing a re-parse could reconcile.+ if await has_graph_edges(session, repo.id):+ return True+ elif stored != parser_fingerprint():+ return True+ return bool(await unscanned_test_files(session, repo.id))finally:await engine.dispose()try:- stored, unscanned = run_async(_stored())+ return run_async(_stored())except Exception as exc:log.debug("parser_change_check_failed", error=str(exc))return False- return unscanned or (stored is not None and stored != parser_fingerprint())def health_analyzer_changed(state: dict) -> bool:diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.pyindex 788a6854b..ca44883c8 100644--- a/packages/core/src/repowise/core/pipeline/persist.py+++ b/packages/core/src/repowise/core/pipeline/persist.py@@ -784,6 +784,20 @@ async def _edges_predate_cohesion(session: Any, repo_id: str, graph_builder: Anyreturn row is None+async def has_graph_edges(session: Any, repo_id: str) -> bool:+ """Whether this repo already has any ``graph_edges`` row."""+ from sqlalchemy import select++ from repowise.core.persistence.models import GraphEdge++ row = (+ await session.execute(+ select(GraphEdge.repository_id).where(GraphEdge.repository_id == repo_id).limit(1)+ )+ ).first()+ return row is not None++async def _stored_edges_parser_fingerprint(session: Any, repo_id: str) -> str | None:"""The ``parser_fingerprint()`` recorded when this repo's edges were written.@@ -804,15 +818,16 @@ async def _stored_edges_parser_fingerprint(session: Any, repo_id: str) -> str |async def stamp_edges_parser_fingerprint(session: Any, repo_id: str, fingerprint: str) -> None:"""Record the parser build that just wrote this repo's ``graph_edges``.- Written only by :func:`persist_incremental_edges`, and only after a widen.- Deliberately *not* written by the full :func:`persist_ingestion` path, which- upserts edges (:func:`batch_upsert_graph_edges`) and prunes only the nodes- of files that vanished: a re-index over an existing store therefore adds the- new build's edges without removing the old build's surplus ones. Stamping- there would mark such a store parser-current and permanently suppress the- delete-then-insert reconcile that is the only thing which clears them. The- cost of not stamping is one widen on the first update after an index, which- is wasted on a fresh store and exactly the repair on a re-indexed one.+ Written by :func:`persist_incremental_edges` after a widen, and by the full+ :func:`persist_ingestion` path only when the store held no edges before it.+ That path upserts edges (:func:`batch_upsert_graph_edges`) and prunes only+ the nodes of files that vanished, so a re-index over an existing store adds+ the new build's edges without removing the old build's surplus ones.+ Stamping such a store would mark it parser-current and permanently suppress+ the delete-then-insert reconcile that is the only thing which clears them;+ it stays unstamped, and the first update widens. A fresh store has no+ surplus, so it is stamped: unstamped, the next parser change could not be+ told apart from "already up to date" on a quiet checkout."""from sqlalchemy import update@@ -854,7 +869,9 @@ async def persist_incremental_edges(change; :func:`_edges_predate_cohesion` is the bespoke predecessor, keptuntil shipped stores have all been stamped."""- if graph_builder is None or not parsed_files:+ # An empty parse still counts: a repo with no source files has nothing to+ # widen, and it must be stamped, or a missing stamp re-parses it forever.+ if graph_builder is None or parsed_files is None:returnfrom repowise.core.ingestion.parse_cache import parser_fingerprintfrom repowise.core.persistence.crud import reconcile_edges_for_files@@ -873,26 +890,28 @@ async def persist_incremental_edges(logger.info("graph_edges_cohesion_backfill", repo_id=repo_id, files=len(changed_paths))reconcile_paths, edges = _changed_file_edges(graph_builder, parsed_files, changed_paths)- if not reconcile_paths:- return- await reconcile_edges_for_files(session, repo_id, reconcile_paths, edges)+ if reconcile_paths:+ await reconcile_edges_for_files(session, repo_id, reconcile_paths, edges)+ # Every reconciled file, not only a widen's: a symbol renamed or removed+ # in a changed file otherwise keeps its node beside the new one.+ await _prune_vanished_symbol_nodes(session, repo_id, graph_builder, reconcile_paths)# Only after the widen actually ran to completion. Raising here (or in any# caller before the commit) leaves the old stamp, so the next update retries# rather than stranding the files it never reached.if parser_changed:- await _prune_vanished_symbol_nodes(session, repo_id, graph_builder, parsed_files)await stamp_edges_parser_fingerprint(session, repo_id, fingerprint)async def _prune_vanished_symbol_nodes(- session: Any, repo_id: str, graph_builder: Any, parsed_files: list[Any]+ session: Any, repo_id: str, graph_builder: Any, paths: list[str]) -> None:- """Delete symbol ``graph_nodes`` a re-extraction no longer produces.+ """Delete symbol ``graph_nodes`` of *paths* a re-extraction no longer produces.``persist_graph_nodes`` upserts the current graph and never deletes, so a- symbol whose id changed under a new parser (an overload gaining its own id)- would otherwise keep its old row beside the new ones. Scoped to files this- run parsed; a file that is gone is :func:`_prune_stale_file_rows`'s job.+ symbol whose id changed (a rename, or an overload gaining its own id under+ a new parser) would otherwise keep its old row beside the new ones. Scoped+ to files this run reconciled; a file that is gone is+ :func:`_prune_stale_file_rows`'s job.Membership rows prune themselves on their next write, and ``graph_metrics``holds files only."""@@ -901,7 +920,6 @@ async def _prune_vanished_symbol_nodes(from repowise.core.persistence.models import GraphEdge, GraphNodegraph = graph_builder.graph()- paths = [pf.file_info.path for pf in parsed_files]stale: list[str] = []for i in range(0, len(paths), _PRUNE_CHUNK):rows = await session.execute(@@ -1712,8 +1730,13 @@ async def persist_ingestion(result: Any, session: Any, repo_id: str) -> int:# ---- Graph edges ---------------------------------------------------------graph = result.graph_builder.graph()edges = [_edge_row(u, v, data) for u, v, data in graph.edges(data=True)]+ fresh_store = not await has_graph_edges(session, repo_id)if edges:await batch_upsert_graph_edges(session, repo_id, edges)+ if fresh_store:+ from repowise.core.ingestion.parse_cache import parser_fingerprint++ await stamp_edges_parser_fingerprint(session, repo_id, parser_fingerprint())# ---- External systems (C4 L1) -------------------------------------------# Persist before symbols so the FK linkage step below sees the IDs.diff --git a/tests/unit/cli/test_update_version_change_gate.py b/tests/unit/cli/test_update_version_change_gate.pyindex ccc1da4c3..1dda2ce51 100644--- a/tests/unit/cli/test_update_version_change_gate.py+++ b/tests/unit/cli/test_update_version_change_gate.py@@ -86,6 +86,61 @@ def test_parser_change_reparses_an_unchanged_checkout(assert rescored+def test_missing_parser_stamp_reparses_an_unchanged_checkout(+ tmp_path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """An index written before init stamped the parser re-parses once, then is quiet."""+ import asyncio++ from repowise.core.persistence import create_engine+ from repowise.core.persistence.models import Repository++ async def _all_scanned(*_a):+ return []++ # Isolate the stamp from the test-file scan, the gate's other trigger.+ monkeypatch.setattr(+ "repowise.core.analysis.test_reachability.unscanned_test_files", _all_scanned+ )+ from repowise.core.pipeline.full_index import index_repo_full++ from .test_update_reconciles_stale_pages import CURRENT_GIT_HISTORY, _repo_at_head++ # Source with edges, so the unstamped store has something to reconcile.+ repo, c2 = _repo_at_head(tmp_path)+ asyncio.run(index_repo_full(repo))+ save_state(+ repo,+ {+ "last_sync_commit": c2,+ "last_docs_commit": c2,+ "docs_enabled": False,+ "health_analyzer_version": HEALTH_ANALYZER_VERSION,+ **CURRENT_GIT_HISTORY,+ },+ )+ assert not upd_cmd.parser_changed(repo)++ async def _unstamp() -> None:+ engine = create_engine(f"sqlite+aiosqlite:///{repo / '.repowise' / 'wiki.db'}")+ async with engine.begin() as conn:+ await conn.execute(+ Repository.__table__.update().values(graph_edges_parser_fingerprint=None)+ )+ await engine.dispose()++ asyncio.run(_unstamp())+ assert upd_cmd.parser_changed(repo)++ out = _invoke_update(repo)++ assert UP_TO_DATE not in out+ assert "Parser changed" in out+ assert not upd_cmd.parser_changed(repo)+ assert load_state(repo)["last_sync_commit"] == c2+ assert UP_TO_DATE in _invoke_update(repo)++@pytest.mark.parametrize("stored", [None, SECURITY_SCANNER_VERSION - 1])def test_stale_scanner_rescans_an_unchanged_checkout_without_a_reparse(tmp_path, monkeypatch: pytest.MonkeyPatch, storeddiff --git a/tests/unit/persistence/test_incremental_edge_persist.py b/tests/unit/persistence/test_incremental_edge_persist.pyindex 8ea8e06a6..ce1dbd64f 100644--- a/tests/unit/persistence/test_incremental_edge_persist.py+++ b/tests/unit/persistence/test_incremental_edge_persist.py@@ -273,6 +273,34 @@ async def test_matching_fingerprint_does_not_widen(async_session, tmp_path):assert ("c.py", "a.py", "imports") not in await _db_edge_set(async_session, repo.id)+async def test_index_into_a_fresh_store_stamps_the_parser(async_session, tmp_path):+ """``init`` stamps the build that wrote the edges when nothing preceded them.++ A fresh store has no surplus edges for a widen to clear. Left unstamped, a+ later parser-only release could not tell it from a current one on a quiet+ checkout.+ """+ repo = await insert_repo(async_session)+ (tmp_path / "a.py").write_text("x = 1\n")+ (tmp_path / "b.py").write_text("from a import x\n")+ gb, parsed = _build_graph(tmp_path)++ await persist_ingestion(+ SimpleNamespace(+ parsed_files=parsed,+ graph_builder=gb,+ external_systems=[],+ execution_flow_report=None,+ source_map={},+ ),+ async_session,+ repo.id,+ )+ await async_session.commit()++ assert await _stored_fingerprint(async_session, repo.id) == parser_fingerprint()++async def test_full_reindex_does_not_suppress_the_widen(async_session, tmp_path):"""A re-index must not mark a store parser-current — it cannot clear surplus edges.