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(mcp): start and report no index in an unindexed directory instead of exiting (#4670)

Raghav Chamadiya30cb47dce148
diff --git a/packages/server/src/repowise/server/mcp_server/_server.py b/packages/server/src/repowise/server/mcp_server/_server.py
index 116ccb744..0ebbba27b 100644
--- a/packages/server/src/repowise/server/mcp_server/_server.py
+++ b/packages/server/src/repowise/server/mcp_server/_server.py
@@ -52,6 +52,11 @@ class StoreUnavailableError(RuntimeError):
"""
+#: Store served for a directory with no ``.repowise/``: empty, so tools report
+#: "not indexed", and never written to disk.
+_NO_INDEX_DB_URL = "sqlite+aiosqlite:///:memory:"
+
+
def _store_unavailable(
where: str | None, repo_path: str | None, exc: BaseException
) -> StoreUnavailableError:
@@ -582,30 +587,30 @@ async def _lifespan(server: FastMCP):
# When repo path is set and no env override, prefer repo-local DB.
store_location: str | None = None
+ db_url: str | None = None
try:
if _state._repo_path and configured_db_url is None:
db_path = get_repo_db_path(_state._repo_path)
repowise_dir = db_path.parent
store_location = str(repowise_dir)
- if not repowise_dir.is_dir():
- # Creating the store directory here made whatever directory an
- # MCP host spawned the server from look initialised: it left a
- # stray .repowise/ behind, silenced the CLI's "run 'repowise
- # init'" warning for every later start, and seeded an empty
- # wiki.db. Fail the way an unopenable store does instead (#3163).
- await _abort_startup(_release_task, _warm_task)
- raise _store_unavailable(
- store_location,
- _state._repo_path,
- FileNotFoundError(f"no {repowise_dir.name} directory"),
+ if not repowise_dir.exists():
+ # An unindexed directory, often just wherever a globally
+ # installed plugin was launched. Exiting here made the host
+ # respawn the server in a loop and the agent never saw why.
+ # Serve an empty in-memory store instead, so every tool returns
+ # the "no index, run repowise init" notice, without leaving a
+ # stray .repowise/ on disk (#3163).
+ _log.warning(
+ "No %s in %s; run 'repowise init'", repowise_dir.name, repowise_dir.parent
)
- if not db_path.exists():
+ db_url = _NO_INDEX_DB_URL
+ elif not db_path.exists():
_log.warning(
"No wiki.db in %s — run 'repowise init' to generate the wiki",
repowise_dir,
)
- db_url = resolve_db_url(_state._repo_path)
+ db_url = db_url or resolve_db_url(_state._repo_path)
_log.info("repowise MCP: initialising database…")
engine = create_engine(db_url, short_lived=False)
@@ -666,6 +671,7 @@ async def _lifespan(server: FastMCP):
# Create the MCP server
# ---------------------------------------------------------------------------
+
class _RepowiseMCP(FastMCP):
"""FastMCP plus the HTTP bearer gate and per-call principal scope.
diff --git a/tests/unit/server/mcp/test_startup_guards.py b/tests/unit/server/mcp/test_startup_guards.py
index 3e6ed63ea..b9faca786 100644
--- a/tests/unit/server/mcp/test_startup_guards.py
+++ b/tests/unit/server/mcp/test_startup_guards.py
@@ -4,7 +4,8 @@ An MCP host restarts a stdio server that dies at startup. Two failures used
to escape as bare tracebacks and loop that way: an index directory the
process cannot create or open, and a tool module whose optional dependency
is missing. The first now raises a message naming the path and the fix; the
-second is skipped so the rest of the surface still serves.
+second is skipped so the rest of the surface still serves. A directory that
+was never indexed is not a failure: the server starts and its tools say so.
"""
from __future__ import annotations
@@ -50,15 +51,18 @@ async def test_lifespan_names_the_path_when_the_store_cannot_be_opened(
@pytest.mark.asyncio
-async def test_missing_store_directory_is_not_created(
+async def test_unindexed_directory_starts_and_reports_no_index(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
- """An uninitialised repo fails startup instead of getting a stray .repowise/.
+ """A directory with no .repowise/ serves, rather than exiting, and stays clean.
- Creating the directory made any directory an MCP host spawned the server
- from look initialised, and the empty wiki.db it seeded masked the state the
- CLI's own warning is meant to report (#3163).
+ Exiting made the host respawn the server in a loop and the agent never saw
+ the cause. Creating the directory instead left a stray .repowise/ and an
+ empty wiki.db that made the directory look initialised (#3163).
"""
+ from repowise.server.mcp_server import get_symbol
+ from repowise.server.mcp_server._failure_shield import shield
+
for name in ("REPOWISE_DB_URL", "REPOWISE_DATABASE_URL"):
monkeypatch.delenv(name, raising=False)
monkeypatch.setattr(_state, "_repo_path", str(tmp_path))
@@ -69,12 +73,11 @@ async def test_missing_store_directory_is_not_created(
monkeypatch.setattr(_server, "_warm_lancedb", _no_warm)
monkeypatch.setattr(_server, "_detect_workspace", lambda _p: (None, None, None))
- with pytest.raises(StoreUnavailableError) as raised:
- async with _server._lifespan(_server.mcp):
- pass
- message = str(raised.value)
- assert str(tmp_path / ".repowise") in message
- assert "repowise init" in message
+ async with _server._lifespan(_server.mcp):
+ result = await shield(get_symbol)("pkg/mod.py::alpha")
+
+ assert "no repowise index" in result["error"].lower()
+ assert "repowise init" in result["remedy"]
assert not (tmp_path / ".repowise").exists()
assert _state._lancedb_ready is None