fix(db): ticket CLI always reads/writes main worktree database

Uses git rev-parse --git-common-dir to find the main worktree root,
then resolves the DB path there. Prevents WAL/journal pollution on
feature branch worktrees that caused merge conflicts on commonwealth.db.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-11 19:00:16 +01:00
co-authored by Claude Opus 4.6
parent 039eac608d
commit 70a6f850c6
+25 -1
View File
@@ -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