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(workspace): route Postgres-backed watch/update through the shared DB instead of local wiki.db (#2160)

Aman0cb85a8c05c1
diff --git a/packages/core/src/repowise/core/persistence/database.py b/packages/core/src/repowise/core/persistence/database.py
index eba23438b..7ae81763b 100644
--- a/packages/core/src/repowise/core/persistence/database.py
+++ b/packages/core/src/repowise/core/persistence/database.py
@@ -262,6 +262,7 @@ def create_engine(
# Pass use_static_pool=True explicitly when creating in-memory test engines.
use_static_pool: bool = False,
busy_timeout_ms: int | None = None,
+ short_lived: bool = True,
) -> AsyncEngine:
"""Create an AsyncEngine for the given database URL.
@@ -274,6 +275,25 @@ def create_engine(
small value for best-effort secondary writers that must
never stall the primary writer (issue #326). Ignored
for non-SQLite backends.
+ short_lived: Whether this engine is created, used, and disposed
+ within a single call (the pattern almost every caller
+ follows: one CLI command, one workspace update, one
+ background task). Defaults to True, which uses
+ NullPool for PostgreSQL — one connection per checkout,
+ closed on dispose, so a short-lived engine can never
+ hold more than a single Postgres server slot, and an
+ engine that outlives its creating event loop can never
+ hand back a dead pooled connection to a later one
+ (issue #2062's failure class). Pass False only for an
+ engine stored for a process's lifetime and reused
+ across many requests — currently just the FastAPI app
+ and the MCP server — where SQLAlchemy's pooled
+ AsyncAdaptedQueuePool is the correct choice and
+ NullPool would open a fresh connection per request.
+ Ignored for SQLite, which already always uses
+ NullPool (or StaticPool for :memory:) regardless of
+ this flag — SQLite has no equivalent long-lived-pool
+ need since ``aiosqlite`` connections are cheap.
"""
db_url = get_db_url(url)
is_sqlite = db_url.startswith("sqlite")
@@ -290,8 +310,21 @@ def create_engine(
else:
kwargs["poolclass"] = NullPool
else:
- # PostgreSQL — asyncpg handles its own connection pool
+ # PostgreSQL. SQLAlchemy pools these connections with
+ # AsyncAdaptedQueuePool by default — asyncpg does NOT provide its own
+ # pool here (that only happens if something calls asyncpg.create_pool,
+ # which nothing in this codebase does). Every create_engine() call in
+ # this codebase except the long-lived server/MCP engines is
+ # short-lived (create, use, dispose within one async function), so
+ # there's no reuse to gain from pooling and every pooled-but-idle
+ # connection is a Postgres server slot held for no benefit — or,
+ # worse, one that survives past a closed event loop and gets handed
+ # to a later, unrelated caller (#2062's failure class). NullPool caps
+ # a short-lived engine's footprint at exactly one connection instead
+ # of up to 15 (pool_size=5 + max_overflow=10) sitting idle.
kwargs["pool_pre_ping"] = True
+ if short_lived:
+ kwargs["poolclass"] = NullPool
engine = create_async_engine(db_url, **kwargs)
if is_sqlite:
diff --git a/packages/core/src/repowise/core/workspace/update.py b/packages/core/src/repowise/core/workspace/update.py
index b1e24419b..dfd8687fe 100644
--- a/packages/core/src/repowise/core/workspace/update.py
+++ b/packages/core/src/repowise/core/workspace/update.py
@@ -52,26 +52,73 @@ from .config import WorkspaceConfig
_log = logging.getLogger("repowise.workspace.update")
-def _merged_repo_excludes(
+async def _merged_repo_excludes(
repo_path: Path,
extra_exclude_patterns: list[str] | None = None,
) -> list[str]:
+ """Merge config.yaml excludes with the persisted repo settings' excludes.
+
+ Local SQLite indexes store ``settings_json`` in the repo-local
+ ``wiki.db``, read directly since this call is on the hot per-update path
+ and a raw sqlite3 read is cheaper than opening an engine for one row.
+ When a shared database is configured, that DB is the source of truth
+ instead — reading only ``config.yaml`` there would silently drop every
+ pattern the user added through the shared repository's settings, so
+ every workspace update would run with a narrower exclude set than the
+ repo was actually indexed with.
+ """
from ..repo_config import load_repo_config
patterns: list[str] = list(load_repo_config(repo_path).get("exclude_patterns") or [])
- db_path = repo_path / ".repowise" / "wiki.db"
- if db_path.is_file():
+
+ from ..persistence.database import get_configured_db_url
+
+ configured_url = get_configured_db_url()
+ settings_json: str | None = None
+
+ if configured_url is None:
+ db_path = repo_path / ".repowise" / "wiki.db"
+ if db_path.is_file():
+ try:
+ with sqlite3.connect(str(db_path)) as conn:
+ row = conn.execute("SELECT settings_json FROM repositories LIMIT 1").fetchone()
+ if row and row[0]:
+ settings_json = row[0]
+ except Exception:
+ pass
+ else:
+ from ..persistence import (
+ create_engine,
+ create_session_factory,
+ get_session,
+ init_db,
+ )
+ from ..persistence.crud import get_repository_by_path
+
+ try:
+ engine = create_engine(configured_url)
+ try:
+ await init_db(engine)
+ sf = create_session_factory(engine)
+ async with get_session(sf) as session:
+ repo = await get_repository_by_path(session, str(repo_path))
+ if repo is not None and repo.settings_json:
+ settings_json = repo.settings_json
+ finally:
+ await engine.dispose()
+ except Exception:
+ pass
+
+ if settings_json:
try:
- with sqlite3.connect(str(db_path)) as conn:
- row = conn.execute("SELECT settings_json FROM repositories LIMIT 1").fetchone()
- if row and row[0]:
- settings = _json.loads(row[0])
- if isinstance(settings, dict):
- for value in settings.get("exclude_patterns") or []:
- if isinstance(value, str) and value not in patterns:
- patterns.append(value)
+ settings = _json.loads(settings_json)
+ if isinstance(settings, dict):
+ for value in settings.get("exclude_patterns") or []:
+ if isinstance(value, str) and value not in patterns:
+ patterns.append(value)
except Exception:
pass
+
for pattern in extra_exclude_patterns or []:
if pattern not in patterns:
patterns.append(pattern)
@@ -321,6 +368,7 @@ async def reconcile_repo_head_commit(repo_path: Path, head: str | None) -> None:
if not head or not has_db_store(repo_path):
return
+
from ..persistence import (
create_engine,
create_session_factory,
@@ -414,16 +462,14 @@ async def _incremental_repo_update(
# New commits but nothing the index cares about changed (merge/empty
# commits, or every change excluded). Report success so the caller
# bumps ``last_sync_commit`` instead of re-diffing forever.
- return RepoUpdateResult(
- alias=alias, updated=True, working_tree_paths=working_tree_paths
- )
+ return RepoUpdateResult(alias=alias, updated=True, working_tree_paths=working_tree_paths)
# Per-repo config, like the single-repo update path. The workspace-level
# ``exclude_patterns`` (when provided) apply on top.
from ..repo_config import load_repo_config
cfg = load_repo_config(repo_path)
- merged_excludes = _merged_repo_excludes(repo_path, exclude_patterns)
+ merged_excludes = await _merged_repo_excludes(repo_path, exclude_patterns)
# Decay-only rows for idle files the anchor advance recovered (#728);
# persisted alongside the changed rows, kept out of git_meta_map so partial
@@ -557,6 +603,36 @@ async def _incremental_repo_update(
)
+async def _has_persisted_repo_index(repo_path: Path) -> bool:
+ """Return whether this repo has persisted index data.
+
+ Local SQLite indexes are identified by their repo-local wiki.db.
+ When a shared database is configured, the repository row is the source
+ of truth instead; a repo-local wiki.db is not expected to exist.
+ """
+ from ..persistence.database import get_configured_db_url
+
+ if get_configured_db_url() is None:
+ return (repo_path / ".repowise" / "wiki.db").is_file()
+
+ from ..persistence import (
+ create_engine,
+ create_session_factory,
+ get_session,
+ init_db,
+ )
+ from ..persistence.crud import get_repository_by_path
+
+ engine = create_engine(get_configured_db_url())
+ try:
+ await init_db(engine)
+ sf = create_session_factory(engine)
+ async with get_session(sf) as session:
+ return await get_repository_by_path(session, str(repo_path)) is not None
+ finally:
+ await engine.dispose()
+
+
async def update_single_repo_index(
repo_path: Path,
*,
@@ -585,7 +661,7 @@ async def update_single_repo_index(
alias = repo_path.name
state = read_repo_state(repo_path)
base_ref = state.get("last_sync_commit")
- merged_excludes = _merged_repo_excludes(repo_path, exclude_patterns)
+ merged_excludes = await _merged_repo_excludes(repo_path, exclude_patterns)
repo_config = load_repo_config(repo_path)
repo_commit_depth = int(repo_config.get("commit_limit", commit_depth))
repo_follow_renames = bool(repo_config.get("follow_renames", False))
@@ -626,7 +702,8 @@ async def update_single_repo_index(
& (changed_dependencies or set())
)
)
- if requires_full_reindex and (repo_path / ".repowise" / "wiki.db").is_file():
+ has_persisted_index = await _has_persisted_repo_index(repo_path)
+ if requires_full_reindex and has_persisted_index:
_log.info(
"workspace_update: %s config fingerprint drifted — full re-index "
"so health scores reflect the new config",
@@ -636,7 +713,7 @@ async def update_single_repo_index(
if (
not requires_full_reindex
and base_ref
- and (repo_path / ".repowise" / "wiki.db").is_file()
+ and has_persisted_index
and commit_exists(repo_path, str(base_ref))
):
try:
@@ -866,9 +943,7 @@ async def update_workspace(
if new_head:
with suppress(OSError):
(path / ".repowise").mkdir(parents=True, exist_ok=True)
- (path / ".repowise" / ".update.pending").write_text(
- new_head, encoding="utf-8"
- )
+ (path / ".repowise" / ".update.pending").write_text(new_head, encoding="utf-8")
return [
RepoUpdateResult(
alias=alias,
@@ -916,7 +991,9 @@ async def update_workspace(
)
# Record pending so the running update can roll forward.
with suppress(OSError):
- (path / ".repowise" / ".update.pending").write_text(new_head, encoding="utf-8")
+ (path / ".repowise" / ".update.pending").write_text(
+ new_head, encoding="utf-8"
+ )
return RepoUpdateResult(
alias=alias,
updated=False,
@@ -1103,9 +1180,7 @@ async def run_cross_repo_hooks(
else WorkspaceIndex({})
)
try:
- await _run_phases(
- ws_config, workspace_root, changed_repos, timings, workspace_index
- )
+ await _run_phases(ws_config, workspace_root, changed_repos, timings, workspace_index)
finally:
await workspace_index.close()
@@ -1171,7 +1246,9 @@ async def _run_phases(
timings.on_phase_done(phase)
overlay_result, store_result = await asyncio.gather(
- _timed("cross_repo_analysis", run_cross_repo_analysis(ws_config, workspace_root, changed_repos)),
+ _timed(
+ "cross_repo_analysis", run_cross_repo_analysis(ws_config, workspace_root, changed_repos)
+ ),
_timed(
"contract_extraction",
run_contract_extraction(
diff --git a/packages/server/src/repowise/server/app.py b/packages/server/src/repowise/server/app.py
index 67a9c76d9..0d00ff1d0 100644
--- a/packages/server/src/repowise/server/app.py
+++ b/packages/server/src/repowise/server/app.py
@@ -274,7 +274,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
Shutdown: dispose engine, stop scheduler, close vector store.
"""
db_url = _resolve_server_db_url()
- engine = create_engine(db_url)
+ engine = create_engine(db_url, short_lived=False)
await init_db(engine)
session_factory = create_session_factory(engine)
await _reset_stale_jobs(session_factory)
diff --git a/packages/server/src/repowise/server/mcp_server/_server.py b/packages/server/src/repowise/server/mcp_server/_server.py
index 901214294..26a64b22f 100644
--- a/packages/server/src/repowise/server/mcp_server/_server.py
+++ b/packages/server/src/repowise/server/mcp_server/_server.py
@@ -639,7 +639,7 @@ async def _lifespan(server: FastMCP):
db_url = resolve_db_url(_state._repo_path)
_log.info("repowise MCP: initialising database…")
- engine = create_engine(db_url)
+ engine = create_engine(db_url, short_lived=False)
try:
await init_db(engine)
except (OSError, OperationalError):
diff --git a/tests/unit/server/mcp/test_config.py b/tests/unit/server/mcp/test_config.py
index 8dadcd6dd..969f6c510 100644
--- a/tests/unit/server/mcp/test_config.py
+++ b/tests/unit/server/mcp/test_config.py
@@ -62,8 +62,9 @@ async def test_mcp_lifespan_uses_cli_database_env_var(monkeypatch):
async def fake_load_vector_stores(repo_path: str | None) -> None:
return None
- def fake_create_engine(url: str) -> DummyEngine:
+ def fake_create_engine(url: str, **kwargs) -> DummyEngine:
captured["url"] = url
+ captured["short_lived"] = kwargs.get("short_lived")
return DummyEngine()
monkeypatch.setenv("REPOWISE_DB_URL", "sqlite+aiosqlite:///tmp/from-cli.db")
@@ -88,6 +89,7 @@ async def test_mcp_lifespan_uses_cli_database_env_var(monkeypatch):
try:
async with mcp_server._lifespan(mcp_server.mcp):
assert captured["url"] == "sqlite+aiosqlite:///tmp/from-cli.db"
+ assert captured["short_lived"] is False
finally:
_state._repo_path = original_repo_path
_state._vector_store = original_vector_store
diff --git a/tests/unit/workspace/test_incremental_update.py b/tests/unit/workspace/test_incremental_update.py
index f4dc7c6ce..6a46624de 100644
--- a/tests/unit/workspace/test_incremental_update.py
+++ b/tests/unit/workspace/test_incremental_update.py
@@ -474,3 +474,129 @@ def test_read_repo_state_missing_or_malformed(tmp_path):
(tmp_path / ".repowise").mkdir()
(tmp_path / ".repowise" / "state.json").write_text("not json{")
assert read_repo_state(tmp_path) == {}
+
+
+def test_shared_db_indexed_repo_takes_incremental_path(tmp_path, forbid_full_pipeline, monkeypatch):
+ """A repo indexed in the configured shared DB must update incrementally
+ even when no repo-local .repowise/wiki.db exists."""
+ repo = _make_git_repo(tmp_path)
+ base = get_head_commit(repo)
+
+ # Use a file-backed SQLite database as a stand-in for the configured
+ # external/shared database. The routing decision must depend on the
+ # configured DB, not on the presence of repo-local wiki.db.
+ shared_db = tmp_path / "shared.db"
+ monkeypatch.setenv(
+ "REPOWISE_DB_URL",
+ f"sqlite+aiosqlite:///{shared_db}",
+ )
+
+ from repowise.core.persistence import (
+ create_engine,
+ create_session_factory,
+ get_session,
+ init_db,
+ upsert_repository,
+ )
+
+ async def _seed() -> None:
+ engine = create_engine(f"sqlite+aiosqlite:///{shared_db}")
+ try:
+ await init_db(engine)
+ sf = create_session_factory(engine)
+ async with get_session(sf) as session:
+ await upsert_repository(
+ session,
+ name=repo.name,
+ local_path=str(repo),
+ head_commit=base,
+ )
+ finally:
+ await engine.dispose()
+
+ asyncio.run(_seed())
+
+ # Persist the incremental anchor, but deliberately do NOT create
+ # <repo>/.repowise/wiki.db.
+ state_dir = repo / ".repowise"
+ state_dir.mkdir(parents=True, exist_ok=True)
+ (state_dir / "state.json").write_text(
+ json.dumps({"last_sync_commit": base}),
+ encoding="utf-8",
+ )
+ assert not (state_dir / "wiki.db").exists()
+
+ _add_commit(repo, "b.py")
+
+ result = asyncio.run(update_single_repo_index(repo))
+
+ assert result.error is None
+ assert result.updated is True
+ assert result.file_count >= 2
+ assert result.symbol_count == 2
+
+
+def test_shared_db_config_drift_does_not_crash(tmp_path, stub_full_pipeline, monkeypatch):
+ """Config-fingerprint drift on a shared-DB-indexed repo (no local
+ wiki.db) must still fall back to the full pipeline cleanly.
+
+ Regression test: an earlier version of the shared-DB gate computed
+ ``has_persisted_index`` *after* the config-drift branch already read it,
+ which raised UnboundLocalError the moment config_changed was True for a
+ repo with no local wiki.db (i.e. exactly the shared-DB case). The
+ existing test_config_drift_runs_full_reindex only exercises this branch
+ for the local-SQLite case (wiki.db present via _mark_indexed), so it
+ never caught the ordering bug.
+ """
+ repo = _make_git_repo(tmp_path)
+ base = get_head_commit(repo)
+
+ # Use a file-backed SQLite database as a stand-in for the configured
+ # external/shared database, same as test_shared_db_indexed_repo_takes_incremental_path.
+ shared_db = tmp_path / "shared.db"
+ monkeypatch.setenv(
+ "REPOWISE_DB_URL",
+ f"sqlite+aiosqlite:///{shared_db}",
+ )
+
+ from repowise.core.persistence import (
+ create_engine,
+ create_session_factory,
+ get_session,
+ init_db,
+ upsert_repository,
+ )
+
+ async def _seed() -> None:
+ engine = create_engine(f"sqlite+aiosqlite:///{shared_db}")
+ try:
+ await init_db(engine)
+ sf = create_session_factory(engine)
+ async with get_session(sf) as session:
+ await upsert_repository(
+ session,
+ name=repo.name,
+ local_path=str(repo),
+ head_commit=base,
+ )
+ finally:
+ await engine.dispose()
+
+ asyncio.run(_seed())
+
+ # A stored config fingerprint that will not match the freshly computed
+ # one — this drives config_changed=True. Deliberately do NOT create
+ # <repo>/.repowise/wiki.db: the shared DB is the source of truth here.
+ state_dir = repo / ".repowise"
+ state_dir.mkdir(parents=True, exist_ok=True)
+ (state_dir / "state.json").write_text(
+ json.dumps({"last_sync_commit": base, "config_fingerprint": "0" * 64}),
+ encoding="utf-8",
+ )
+ assert not (state_dir / "wiki.db").exists()
+
+ _add_commit(repo, "b.py")
+
+ result = asyncio.run(update_single_repo_index(repo))
+
+ _assert_full_pipeline_fallback(result, stub_full_pipeline)