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(worktree): re-key the repo-level pages when a seeded index adopts its checkout (#2748)

Raghav Chamadiya1e5cc1d2933b
diff --git a/packages/cli/src/repowise/cli/worktree.py b/packages/cli/src/repowise/cli/worktree.py
index 711851b52..280111e41 100644
--- a/packages/cli/src/repowise/cli/worktree.py
+++ b/packages/cli/src/repowise/cli/worktree.py
@@ -90,9 +90,11 @@ def _adopt_repository_identity(repowise_dir: Path, *, src_repo: Path, dest_repo:
update in the worktree upserts a second repository row named after the
worktree dir, and every regenerated page lands under it while the seeded
pages stay under the base row — prior-page reuse and repo-scoped queries
- both silently split. Rewriting name + local_path (and retargeting the two
- repo-level pages that are keyed by repo name) makes the copy fully the
- worktree's own. Best-effort: an unmatched row means the base index is
+ both silently split. Rewriting name + local_path (and re-keying the two
+ repo-level pages whose id carries the repo name) makes the copy fully the
+ worktree's own. Ceiling: the vector store keeps the old page id until the
+ page is regenerated, so semantic search can return an id that no longer
+ resolves; re-keying vectors needs the store open, which seeding avoids. Best-effort: an unmatched row means the base index is
unusual, and falling through leaves today's (pre-fix) behavior.
"""
import sqlite3
@@ -114,12 +116,32 @@ def _adopt_repository_identity(repowise_dir: Path, *, src_repo: Path, dest_repo:
"UPDATE repositories SET name = ?, local_path = ? WHERE id = ?",
(dest_repo.name, str(dest_repo), repo_id),
)
- conn.execute(
- "UPDATE wiki_pages SET target_path = ? "
- "WHERE repository_id = ? AND target_path = ? "
- "AND page_type IN ('repo_overview', 'architecture_diagram')",
- (dest_repo.name, repo_id, old_name),
- )
+ # A page id is ``<type>:<target_path>``, so the id moves with the target:
+ # generation writes the new id, and a page left under the old one is
+ # never regenerated and never found by ``generate --page``.
+ has_fts = conn.execute(
+ "SELECT 1 FROM sqlite_master WHERE name = 'page_fts'"
+ ).fetchone()
+ for page_type in ("repo_overview", "architecture_diagram"):
+ old_id, new_id = f"{page_type}:{old_name}", f"{page_type}:{dest_repo.name}"
+ conn.execute(
+ "UPDATE wiki_pages SET id = ?, target_path = ? "
+ "WHERE repository_id = ? AND id = ?",
+ (new_id, dest_repo.name, repo_id, old_id),
+ )
+ conn.execute(
+ "UPDATE wiki_page_versions SET page_id = ? WHERE page_id = ?", (new_id, old_id)
+ )
+ conn.execute(
+ "UPDATE wiki_pages SET parent_page_id = ? "
+ "WHERE repository_id = ? AND parent_page_id = ?",
+ (new_id, repo_id, old_id),
+ )
+ if has_fts:
+ conn.execute(
+ "UPDATE page_fts SET page_id = ?, target_path = ? WHERE page_id = ?",
+ (new_id, dest_repo.name, old_id),
+ )
conn.commit()
diff --git a/tests/unit/cli/test_worktree.py b/tests/unit/cli/test_worktree.py
index 7211932d0..7b37b23ee 100644
--- a/tests/unit/cli/test_worktree.py
+++ b/tests/unit/cli/test_worktree.py
@@ -69,3 +69,44 @@ def test_base_is_seedable_requires_state_and_db(tmp_path: Path) -> None:
assert not base_is_seedable(repo)
(repo / ".repowise" / "wiki.db").write_text("", encoding="utf-8")
assert base_is_seedable(repo)
+
+
+def test_adopting_identity_rekeys_the_repo_level_pages(tmp_path: Path) -> None:
+ """The overview id carries the repo name; the seeded copy must be findable and regenerable."""
+ import sqlite3
+ from contextlib import closing
+
+ from repowise.cli.worktree import _adopt_repository_identity
+
+ src, dest = tmp_path / "base", tmp_path / "feature-tree"
+ db = tmp_path / "wiki.db"
+ with closing(sqlite3.connect(db)) as conn:
+ conn.executescript(
+ "CREATE TABLE repositories (id TEXT, name TEXT, local_path TEXT);"
+ "CREATE TABLE wiki_pages (id TEXT, repository_id TEXT, page_type TEXT,"
+ " target_path TEXT, parent_page_id TEXT);"
+ "CREATE TABLE wiki_page_versions (id TEXT, page_id TEXT);"
+ "CREATE VIRTUAL TABLE page_fts USING fts5(page_id UNINDEXED, title, target_path);"
+ )
+ conn.execute("INSERT INTO repositories VALUES ('r1', 'base', ?)", (str(src),))
+ conn.execute(
+ "INSERT INTO wiki_pages VALUES ('repo_overview:base', 'r1', 'repo_overview', 'base', NULL)"
+ )
+ conn.execute(
+ "INSERT INTO wiki_pages VALUES ('module_page:src', 'r1', 'module_page', 'src',"
+ " 'repo_overview:base')"
+ )
+ conn.execute("INSERT INTO wiki_page_versions VALUES ('v1', 'repo_overview:base')")
+ conn.execute("INSERT INTO page_fts VALUES ('repo_overview:base', 'Overview', 'base')")
+ conn.commit()
+
+ _adopt_repository_identity(tmp_path, src_repo=src, dest_repo=dest)
+
+ with closing(sqlite3.connect(db)) as conn:
+ pages = conn.execute("SELECT id, target_path, parent_page_id FROM wiki_pages").fetchall()
+ versions = conn.execute("SELECT page_id FROM wiki_page_versions").fetchall()
+ fts = conn.execute("SELECT page_id, target_path FROM page_fts").fetchall()
+ assert ("repo_overview:feature-tree", "feature-tree", None) in pages
+ assert ("module_page:src", "src", "repo_overview:feature-tree") in pages
+ assert versions == [("repo_overview:feature-tree",)]
+ assert fts == [("repo_overview:feature-tree", "feature-tree")]