diff --git a/db/connectors/ticket b/db/connectors/ticket index eab87097d..bf0b2ef46 100755 --- a/db/connectors/ticket +++ b/db/connectors/ticket @@ -24,6 +24,7 @@ All output is JSON on stdout. import json import os import sqlite3 +import subprocess import sys from pathlib import Path @@ -31,10 +32,33 @@ SCRIPT_DIR = Path(__file__).resolve().parent CONFIG_PATH = SCRIPT_DIR / "config.json" +def _main_worktree(): + """Find the main worktree root via git's common dir. + + In a worktree layout like: + settled-reach/main/ (main worktree, holds .git/) + settled-reach/server/ (linked worktree, .git is a file) + git rev-parse --git-common-dir always points to main's .git/, + so its parent is the main worktree root. + """ + try: + common = subprocess.check_output( + ["git", "rev-parse", "--git-common-dir"], + stderr=subprocess.DEVNULL, text=True + ).strip() + return Path(common).resolve().parent + except (subprocess.CalledProcessError, OSError): + # Fallback: assume script lives in the main worktree + return SCRIPT_DIR.parent.parent + + def load_config(): with open(CONFIG_PATH, "r") as f: cfg = json.load(f) - db_path = (SCRIPT_DIR / cfg["sqlite_db"]).resolve() + # Always resolve DB path in the main worktree to avoid + # WAL/journal pollution on feature branches causing merge conflicts. + main_root = _main_worktree() + db_path = (main_root / "db" / "commonwealth.db").resolve() cfg["sqlite_db_resolved"] = str(db_path) return cfg