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.

refactor(persist): one stale-page UPDATE instead of two copies (#1199)

Raghav Chamadiyaaaa889c573a4
diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.py
index 36c2ff0c3..c87cf538f 100644
--- a/packages/core/src/repowise/core/pipeline/persist.py
+++ b/packages/core/src/repowise/core/pipeline/persist.py
@@ -189,7 +189,7 @@ async def tombstone_absent_file_pages(
return marked
-async def mark_stale_pages(session: Any, repo_id: str, paths: list[str]) -> int:
+async def mark_stale_pages(session: Any, repo_id: str, paths: Iterable[str]) -> int:
"""Decay weakly-affected file pages to ``freshness_status='stale'``.
``ChangeDetector.get_affected_pages`` returns ``decay_only`` — pages hit
@@ -201,28 +201,21 @@ async def mark_stale_pages(session: Any, repo_id: str, paths: list[str]) -> int:
already-stale pages keep their stronger status, and pages regenerated in
this run are never in ``decay_only`` by construction.
- Returns the number of pages marked.
- """
- if not paths:
- return 0
- from sqlalchemy import update
+ Maps each path to its ``file_page:<path>`` id and hands off to
+ :func:`mark_page_ids_stale`, so both entry points share one UPDATE.
- from repowise.core.persistence.models import Page
+ The two were separate copies of the same statement, and only the sibling
+ was chunked against ``SQLITE_MAX_VARIABLE_NUMBER``. That is not a crash
+ anyone is hitting today: SQLite has defaulted to 32766 bind variables since
+ 3.32, and a ``decay_only`` set that large would take an enormous cascade.
+ It is reachable on the 999-variable builds older interpreters still ship,
+ but the reason to collapse the two is narrower than that. They answered the
+ same question differently, in the file with the most bug fixes in this repo,
+ and now there is one place to answer it.
- page_ids = [f"file_page:{path}" for path in paths]
- res = await session.execute(
- update(Page)
- .where(
- Page.repository_id == repo_id,
- Page.id.in_(page_ids),
- Page.freshness_status == "fresh",
- )
- .values(freshness_status="stale")
- )
- marked = int(res.rowcount or 0)
- if marked:
- logger.info("pages_decayed_stale", repo_id=repo_id, count=marked)
- return marked
+ Returns the number of pages marked.
+ """
+ return await mark_page_ids_stale(session, repo_id, (f"file_page:{path}" for path in paths))
async def mark_page_ids_stale(session: Any, repo_id: str, page_ids: Iterable[str]) -> int:
diff --git a/tests/unit/persistence/test_mark_stale_pages.py b/tests/unit/persistence/test_mark_stale_pages.py
index a942e6ffb..9391e1d9e 100644
--- a/tests/unit/persistence/test_mark_stale_pages.py
+++ b/tests/unit/persistence/test_mark_stale_pages.py
@@ -65,3 +65,43 @@ async def test_missing_pages_and_empty_input_are_noops(async_session):
assert await mark_stale_pages(async_session, repo.id, []) == 0
assert await mark_stale_pages(async_session, repo.id, ["src/never_indexed.py"]) == 0
+
+
+async def test_a_cascade_spanning_several_chunks_counts_them_all(async_session):
+ """More paths than one chunk, so the batched UPDATE has to accumulate.
+
+ This does not reproduce the bind-variable cap. SQLite has allowed 32766
+ variables since 3.32, so the unchunked version passed it too. What it pins
+ is the arithmetic the shared path introduces: three batches of 500, with a
+ rowcount summed across them rather than taken from the last one.
+ """
+ repo = await insert_repo(async_session)
+ paths = [f"src/mod{i}.py" for i in range(1200)] # > _STALE_ID_CHUNK (500)
+ for path in paths:
+ await _seed_page(async_session, repo.id, path)
+
+ marked = await mark_stale_pages(async_session, repo.id, paths)
+ await async_session.commit()
+
+ assert marked == len(paths)
+ assert len(await get_stale_pages(async_session, repo.id)) == len(paths)
+
+
+async def test_a_generator_of_paths_is_accepted(async_session):
+ """The signature takes any iterable, matching ``mark_page_ids_stale``.
+
+ The old ``if not paths`` early-return would have been wrong for a lazy
+ iterable, since a generator is always truthy. Widening the annotation
+ without moving that check would have traded one inconsistency for a subtler
+ one. The shared path materializes before it tests.
+ """
+ repo = await insert_repo(async_session)
+ await _seed_page(async_session, repo.id, "src/a.py")
+
+ marked = await mark_stale_pages(async_session, repo.id, (p for p in ["src/a.py"]))
+ await async_session.commit()
+
+ assert marked == 1
+ assert await mark_stale_pages(async_session, repo.id, (p for p in [])) == 0