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.

fix: tombstone symbol spotlight pages when source files are removed (#3464)

Yunus Ozenef55ad783f5e
diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.py
index 773651844..76212710b 100644
--- a/packages/core/src/repowise/core/pipeline/persist.py
+++ b/packages/core/src/repowise/core/pipeline/persist.py
@@ -23,6 +23,10 @@ from repowise.core.generation.models import (
logger = structlog.get_logger(__name__)
+#: Candidate paths per spotlight query: each path is one ``LIKE`` term, and
+#: SQLite caps how deep an expression tree may grow.
+_SPOTLIGHT_PATHS_PER_QUERY = 200
+
# A second, stdlib logger for the one message here that has to survive
# ``repowise init``. ``configure_cli_logging`` pins both ``repowise.core`` and
# structlog to ERROR for the progress-bar commands, so anything below ERROR is
@@ -55,7 +59,8 @@ def tombstone_candidates(file_diffs: list[Any]) -> list[tuple[str, list[str]]]:
async def mark_tombstone_pages(
session: Any, repo_id: str, candidates: list[tuple[str, list[str]]]
) -> list[str]:
- """Mark file pages for deleted/renamed files as tombstones.
+ """Mark the pages of deleted/renamed files as tombstones: each file's page
+ and the symbol spotlight pages of the symbols it defined.
A ``freshness_status="fresh"`` page for a file that no longer exists is
an active trap: retrieval serves it, agents cite it, and the index-age
@@ -73,16 +78,42 @@ async def mark_tombstone_pages(
"""
if not candidates:
return []
- from sqlalchemy import select
+ from sqlalchemy import or_, select
from repowise.core.persistence.models import Page
page_ids = {f"file_page:{path}": (path, successors) for path, successors in candidates}
- res = await session.execute(
- select(Page).where(Page.repository_id == repo_id, Page.id.in_(page_ids))
+ pages = list(
+ (
+ await session.execute(
+ select(Page).where(Page.repository_id == repo_id, Page.id.in_(page_ids))
+ )
+ ).scalars()
)
+ # A symbol spotlight page targets ``<path>::<symbol>``, so its id is never
+ # ``file_page:<path>``; it goes with its file all the same. Matched on the
+ # ``<path>::`` prefix, so a file whose name merely starts with *path* stays.
+ successors_by_path = dict(candidates)
+ paths = list(successors_by_path)
+ for start in range(0, len(paths), _SPOTLIGHT_PATHS_PER_QUERY):
+ chunk = paths[start : start + _SPOTLIGHT_PATHS_PER_QUERY]
+ res = await session.execute(
+ select(Page).where(
+ Page.repository_id == repo_id,
+ Page.page_type == "symbol_spotlight",
+ or_(*(Page.target_path.startswith(f"{p}::", autoescape=True) for p in chunk)),
+ )
+ )
+ for page in res.scalars():
+ path = (page.target_path or "").split("::", 1)[0]
+ # SQLite LIKE ignores ASCII case, so ``pkg/Foo.py::`` also matches
+ # a live ``pkg/foo.py``; keep only exact path matches.
+ if path not in successors_by_path:
+ continue
+ page_ids[page.id] = (path, successors_by_path.get(path, []))
+ pages.append(page)
marked: list[str] = []
- for page in res.scalars().all():
+ for page in pages:
_, successors = page_ids[page.id]
page.freshness_status = "tombstone"
try:
diff --git a/tests/unit/pipeline/test_tombstone_fts_removal.py b/tests/unit/pipeline/test_tombstone_fts_removal.py
index 8e012c183..d28ba68e0 100644
--- a/tests/unit/pipeline/test_tombstone_fts_removal.py
+++ b/tests/unit/pipeline/test_tombstone_fts_removal.py
@@ -127,3 +127,79 @@ async def test_a_renamed_file_is_dropped_under_its_old_path(session, engine):
await fts.delete_many(marked)
assert await fts.search("xylophone", limit=10) == []
+
+
+async def _spotlight(session, repo_id: str, target: str) -> str:
+ page_id = f"symbol_spotlight:{target}"
+ await upsert_page(
+ session,
+ page_id=page_id,
+ repository_id=repo_id,
+ page_type="symbol_spotlight",
+ title=f"Symbol: {target}",
+ content="Spotlight.",
+ summary="Spotlight.",
+ target_path=target,
+ source_hash="h",
+ model_name="mock",
+ provider_name="mock",
+ )
+ return page_id
+
+
+async def _status(session, page_id: str) -> tuple[str, list[str] | None]:
+ import json
+
+ from repowise.core.persistence.models import Page
+
+ page = await session.get(Page, page_id)
+ return page.freshness_status, json.loads(page.metadata_json or "{}").get("successor_paths")
+
+
+async def test_a_deleted_files_spotlight_pages_are_tombstoned_and_returned(session, engine):
+ """A symbol spotlight page belongs to its file: when the file goes, so does
+ the page, and the caller is told about it so the index drops it too (#3457)."""
+ fts = FullTextSearch(engine)
+ await fts.ensure_index()
+ repo_id = await _seed(session, fts, "pkg/gone.py", "pkg/gone.pyx")
+ gone = await _spotlight(session, repo_id, "pkg/gone.py::helper")
+ sibling = await _spotlight(session, repo_id, "pkg/gone.pyx::other")
+ await session.commit()
+
+ marked = await mark_tombstone_pages(session, repo_id, [("pkg/gone.py", ["pkg/new.py"])])
+
+ assert sorted(marked) == ["file_page:pkg/gone.py", gone]
+ assert await _status(session, gone) == ("tombstone", ["pkg/new.py"])
+ assert (await _status(session, sibling))[0] == "fresh"
+
+
+async def test_a_path_is_matched_literally_not_as_a_like_pattern(session, engine):
+ """``_`` and ``%`` in a path are not wildcards: deleting ``a_b.py`` leaves
+ the spotlight pages of ``aXb.py`` alone."""
+ fts = FullTextSearch(engine)
+ await fts.ensure_index()
+ repo_id = await _seed(session, fts, "pkg/a_b.py", "pkg/aXb.py")
+ gone = await _spotlight(session, repo_id, "pkg/a_b.py::f")
+ other = await _spotlight(session, repo_id, "pkg/aXb.py::f")
+ await session.commit()
+
+ marked = await mark_tombstone_pages(session, repo_id, [("pkg/a_b.py", [])])
+
+ assert sorted(marked) == ["file_page:pkg/a_b.py", gone]
+ assert (await _status(session, other))[0] == "fresh"
+
+
+async def test_a_case_only_sibling_keeps_its_spotlight_pages(session, engine):
+ """SQLite LIKE ignores ASCII case: deleting ``pkg/Foo.py`` must not
+ tombstone the spotlight pages of a live ``pkg/foo.py``."""
+ fts = FullTextSearch(engine)
+ await fts.ensure_index()
+ repo_id = await _seed(session, fts, "pkg/Foo.py", "pkg/foo.py")
+ gone = await _spotlight(session, repo_id, "pkg/Foo.py::f")
+ kept = await _spotlight(session, repo_id, "pkg/foo.py::f")
+ await session.commit()
+
+ marked = await mark_tombstone_pages(session, repo_id, [("pkg/Foo.py", [])])
+
+ assert sorted(marked) == ["file_page:pkg/Foo.py", gone]
+ assert (await _status(session, kept))[0] == "fresh"