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(cli): resolve DB identity on delegated worktree updates (#841)

Paarth SIkka8a01a0773a33
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/command.py b/packages/cli/src/repowise/cli/commands/init_cmd/command.py
index 14f551291..222b45d24 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/command.py
@@ -830,34 +830,52 @@ def init_command(
repo_paths=[r.path for r in scan.repos],
seed_base=seed_base,
include_submodules=include_submodules,
+ dry_run=dry_run,
)
if seeded:
+ if dry_run:
+ console.print(
+ "[dim]Dry run: Would seed worktree index and delegate to update.[/dim]"
+ )
+ return
+
console.print(f"[{OK}]Worktree index seeded successfully. Delegating to update...[/]")
from repowise.cli.commands.update_cmd.command import run_update
is_workspace = len(scan.repos) > 1 and not no_workspace
- # Delegate to update
- run_update(
- path=str(repo_path),
- provider_name=provider_name,
- model=model,
- since=None,
- reasoning=reasoning,
- cascade_budget=None,
- dry_run=dry_run,
- workspace=is_workspace,
- no_workspace=no_workspace,
- repo_alias=None,
- index_only=index_only,
- docs_flag=None,
- full=force,
- agents_md=agents_md,
- concurrency=concurrency,
- no_cost_tracking=no_cost_tracking,
- verbose=verbose,
- progress=progress,
- )
+ from repowise.core.persistence.database import DB_ENV_VARS
+
+ # Strip db-pin env vars to enforce worktree DB resolution (note: potential process-global race condition).
+ old_db_urls = {}
+ for env_key in DB_ENV_VARS:
+ if env_key in os.environ:
+ old_db_urls[env_key] = os.environ.pop(env_key)
+
+ try:
+ # Delegate to update
+ run_update(
+ path=str(repo_path),
+ provider_name=provider_name,
+ model=model,
+ since=None,
+ reasoning=reasoning,
+ cascade_budget=None,
+ dry_run=dry_run,
+ workspace=is_workspace,
+ no_workspace=no_workspace,
+ repo_alias=None,
+ index_only=index_only,
+ docs_flag=None,
+ full=force,
+ agents_md=agents_md,
+ concurrency=concurrency,
+ no_cost_tracking=no_cost_tracking,
+ verbose=verbose,
+ progress=progress,
+ )
+ finally:
+ os.environ.update(old_db_urls)
return
if len(scan.repos) > 1 and not no_workspace:
_workspace_init(
@@ -1529,7 +1547,6 @@ def init_command(
# ---- Post-run: config, state, MCP, editor project files ----
if commit_limit is not None:
save_config_partial(repo_path, commit_limit=resolved_commit_limit)
-
# One flag, one meaning: --no-editor-setup now suppresses the project-local
# writes as well as the global registration. The paths come back so the
# completion panel can name what landed in the working tree.
diff --git a/packages/cli/src/repowise/cli/worktree.py b/packages/cli/src/repowise/cli/worktree.py
index df5e90877..29ae9e11c 100644
--- a/packages/cli/src/repowise/cli/worktree.py
+++ b/packages/cli/src/repowise/cli/worktree.py
@@ -12,11 +12,14 @@ import json
import shutil
import subprocess
import tempfile
+import time
import uuid
from pathlib import Path
from repowise.cli.helpers import console
+_SEED_TEMPDIR_STALENESS_SECS = 3600
+
def _git_output(args: list[str], cwd: Path) -> str:
try:
@@ -59,11 +62,21 @@ def base_is_seedable(base: Path) -> bool:
def sweep_stale_seed_backups(paths: list[Path]) -> None:
- """Remove ``.repowise.bak.*`` leftovers from previously disrupted seeds."""
+ """Remove leftovers from previously disrupted seeds."""
+ now = time.time()
for root in paths:
- for p in root.glob(".repowise.bak.*"):
- if p.is_dir():
- shutil.rmtree(p, ignore_errors=True)
+ for glob_pattern in (".repowise.bak.*", ".repowise-seed-*"):
+ for p in root.glob(glob_pattern):
+ if p.is_dir():
+ try:
+ if ".repowise.bak." in p.name:
+ shutil.rmtree(p, ignore_errors=True)
+ else:
+ mtime = p.stat().st_mtime
+ if now - mtime > _SEED_TEMPDIR_STALENESS_SECS:
+ shutil.rmtree(p, ignore_errors=True)
+ except OSError:
+ pass
def _get_initial_commit(p: Path) -> str:
@@ -116,6 +129,7 @@ def seed_index_from_base(
repo_paths: list[Path],
seed_base: Path,
include_submodules: bool | None = None,
+ dry_run: bool = False,
) -> bool:
"""Copy ``.repowise/`` from a base checkout into each target repo.
@@ -188,40 +202,78 @@ def seed_index_from_base(
f"from last synced commit {last_sync_commit[:8]}.[/dim]"
)
- temp_dir = Path(tempfile.mkdtemp(prefix=".repowise-seed-", dir=r_path))
- temp_dirs.append((r_path, temp_dir))
+ if dry_run:
+ continue
+
+ try:
+ temp_dir = Path(tempfile.mkdtemp(prefix=".repowise-seed-", dir=r_path))
+ temp_dirs.append((r_path, temp_dir))
- shutil.copytree(src_repo / ".repowise", temp_dir, dirs_exist_ok=True)
+ shutil.copytree(src_repo / ".repowise", temp_dir, dirs_exist_ok=True)
- # Since config.yaml is copied atomically alongside state.json, the
- # config_fingerprint remains valid.
- st_data = json.loads((temp_dir / "state.json").read_text(encoding="utf-8"))
+ # Since config.yaml is copied atomically alongside state.json, the
+ # config_fingerprint remains valid.
+ st_data = json.loads((temp_dir / "state.json").read_text(encoding="utf-8"))
- if include_submodules is not None:
- state_include = st_data.get("include_submodules", False)
- if include_submodules != state_include:
- console.print(
- f"[yellow]Warning: --include-submodules={include_submodules} "
- f"conflicts with copied state ({state_include}). Seeded state "
- f"will take precedence.[/yellow]"
- )
+ if include_submodules is not None:
+ state_include = st_data.get("include_submodules", False)
+ if include_submodules != state_include:
+ console.print(
+ f"[yellow]Warning: --include-submodules={include_submodules} "
+ f"conflicts with copied state ({state_include}). Seeded state "
+ f"will take precedence.[/yellow]"
+ )
- (temp_dir / "state.json").write_text(json.dumps(st_data, indent=2), encoding="utf-8")
+ (temp_dir / "state.json").write_text(json.dumps(st_data, indent=2), encoding="utf-8")
- _adopt_repository_identity(temp_dir, src_repo=src_repo, dest_repo=r_path)
+ _adopt_repository_identity(temp_dir, src_repo=src_repo, dest_repo=r_path)
+ except Exception as e:
+ console.print(
+ f"[yellow]Error staging seed for {r_path}: {e}. Falling back to full init.[/yellow]"
+ )
+ success = False
+ break
if not success:
for _, temp_dir in temp_dirs:
shutil.rmtree(temp_dir, ignore_errors=True)
return False
- for r_path, temp_dir in temp_dirs:
- target = r_path / ".repowise"
- backup = None
- if target.exists():
- backup = r_path / f".repowise.bak.{uuid.uuid4().hex[:8]}"
- target.rename(backup)
- temp_dir.rename(target)
- if backup:
+ if dry_run:
+ return True
+
+ backups_created: list[tuple[Path, Path]] = []
+ renamed_targets: list[Path] = []
+
+ try:
+ # Pass 1: backup existing
+ for r_path, _ in temp_dirs:
+ target = r_path / ".repowise"
+ if target.exists():
+ backup = r_path / f".repowise.bak.{uuid.uuid4().hex[:8]}"
+ target.rename(backup)
+ backups_created.append((target, backup))
+
+ # Pass 2: rename temp to target
+ for r_path, temp_dir in temp_dirs:
+ target = r_path / ".repowise"
+ temp_dir.rename(target)
+ renamed_targets.append(target)
+
+ # Pass 3: clean backups
+ for _, backup in backups_created:
shutil.rmtree(backup, ignore_errors=True)
+ except Exception:
+ # Rollback target renames
+ for target in renamed_targets:
+ shutil.rmtree(target, ignore_errors=True)
+ # Rollback backups
+ for target, backup in backups_created:
+ if backup.exists():
+ backup.rename(target)
+ # Clean temp dirs
+ for _, temp_dir in temp_dirs:
+ shutil.rmtree(temp_dir, ignore_errors=True)
+ raise
+
return True
diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py
index ea14f923c..40607302d 100644
--- a/tests/integration/test_cli.py
+++ b/tests/integration/test_cli.py
@@ -1104,7 +1104,8 @@ class TestInitSeedFrom:
from click.testing import CliRunner
- monkeypatch.delenv("REPOWISE_DB_URL", raising=False)
+ # Leave REPOWISE_DB_URL pinned (as git_work_repo sets it) to verify
+ # that the delegated update does not leak worktree pages into the base DB.
# Every file gets a structural page now, so no coverage knob is needed
# to keep the new file from being tier-gated out.
@@ -1116,7 +1117,7 @@ class TestInitSeedFrom:
assert r0.exit_code == 0, r0.output
_git(["checkout", "-b", "feature"], git_work_repo)
- (git_work_repo / "new_file.py").write_text("print('hello')\n", encoding="utf-8")
+ (git_work_repo / "new_file.py").write_text("def my_func(): pass\n", encoding="utf-8")
_git(["add", "new_file.py"], git_work_repo)
_git(["commit", "-m", "feature commit"], git_work_repo)
@@ -1151,11 +1152,9 @@ class TestInitSeedFrom:
"SELECT target_path FROM wiki_pages",
)
assert len(paths) > 0, "Seeded pages should have survived"
- # Note: no assertion that new_file.py gets its own file page. The
- # update-time selection budget is computed over the affected-file
- # subset (not the whole repo), so a small update batch rarely
- # selects a brand-new file for a page. Tracked as #746;
- # independent of seeding.
+
+ # Since the file has a symbol and we have 1.0 coverage, it must be selected
+ assert "new_file.py" in paths, f"Expected 'new_file.py' in {paths}"
state = json.loads(
(worktree_dir / ".repowise" / "state.json").read_text(encoding="utf-8")
)