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(server): register workspace members and fan-out search on shared database (#2138) (#2195)

Rishabh Mishraf24ca5475314
diff --git a/packages/core/src/repowise/core/persistence/search.py b/packages/core/src/repowise/core/persistence/search.py
index 1afce0a84..4d9a7bd8c 100644
--- a/packages/core/src/repowise/core/persistence/search.py
+++ b/packages/core/src/repowise/core/persistence/search.py
@@ -551,12 +551,15 @@ class FullTextSearch:
rows = await conn.execute(text("SELECT id FROM wiki_pages"))
return {r[0] for r in rows.fetchall()}
- async def search(self, query: str, limit: int = 10) -> list[SearchResult]:
+ async def search(
+ self, query: str, limit: int = 10, repository_id: str | None = None
+ ) -> list[SearchResult]:
"""Search for pages matching *query*.
Args:
query: Natural-language search query.
limit: Maximum number of results to return.
+ repository_id: Optional repository ID to narrow the search scope.
Returns:
List of SearchResult objects sorted by relevance (descending).
@@ -565,8 +568,8 @@ class FullTextSearch:
return []
if self._dialect == "sqlite":
- return await self._search_sqlite(query, limit)
- return await self._search_postgresql(query, limit)
+ return await self._search_sqlite(query, limit, repository_id=repository_id)
+ return await self._search_postgresql(query, limit, repository_id=repository_id)
async def _document_frequency(self, conn, term: str) -> int:
"""How many indexed pages *term* matches. ``""`` is the corpus size.
@@ -616,20 +619,37 @@ class FullTextSearch:
counts[term] = await df(term)
return _build_fts5_query(query, counts.__getitem__)
- async def _matching_rows(self, conn, fts_query: str, limit: int) -> list:
- rows = await conn.execute(
- text(
- f"SELECT f.page_id, f.title, f.content, {_BM25_SCORE} "
- "FROM page_fts f "
- "WHERE page_fts MATCH :q "
- f"ORDER BY {_BM25_SCORE} "
- "LIMIT :lim"
- ),
- {"q": fts_query, "lim": limit},
- )
+ async def _matching_rows(
+ self, conn, fts_query: str, limit: int, repository_id: str | None = None
+ ) -> list:
+ if repository_id is not None:
+ rows = await conn.execute(
+ text(
+ f"SELECT f.page_id, f.title, f.content, {_BM25_SCORE} "
+ "FROM page_fts f "
+ "JOIN wiki_pages p ON p.id = f.page_id "
+ "WHERE page_fts MATCH :q AND p.repository_id = :repo_id "
+ f"ORDER BY {_BM25_SCORE} "
+ "LIMIT :lim"
+ ),
+ {"q": fts_query, "lim": limit, "repo_id": repository_id},
+ )
+ else:
+ rows = await conn.execute(
+ text(
+ f"SELECT f.page_id, f.title, f.content, {_BM25_SCORE} "
+ "FROM page_fts f "
+ "WHERE page_fts MATCH :q "
+ f"ORDER BY {_BM25_SCORE} "
+ "LIMIT :lim"
+ ),
+ {"q": fts_query, "lim": limit},
+ )
return list(rows.fetchall())
- async def _search_sqlite(self, query: str, limit: int) -> list[SearchResult]:
+ async def _search_sqlite(
+ self, query: str, limit: int, repository_id: str | None = None
+ ) -> list[SearchResult]:
"""FTS5 search. bm25 is negative; we negate it to get a positive score."""
async with self._engine.connect() as conn:
@@ -637,7 +657,7 @@ class FullTextSearch:
return await self._document_frequency(conn, term)
fts_query = await self._build_selective_query(query, df)
- raw = await self._matching_rows(conn, fts_query, limit)
+ raw = await self._matching_rows(conn, fts_query, limit, repository_id=repository_id)
# The frequency ceiling can cut a question down to terms that
# nothing carries together. Retrying with every term is the prior
@@ -649,7 +669,9 @@ class FullTextSearch:
seen = {r[0] for r in raw}
extra = [
r
- for r in await self._matching_rows(conn, widened, limit)
+ for r in await self._matching_rows(
+ conn, widened, limit, repository_id=repository_id
+ )
if r[0] not in seen
]
if extra:
@@ -757,7 +779,9 @@ class FullTextSearch:
kept = selective
return " | ".join(_pg_term(t) for t in kept)
- async def _search_postgresql(self, query: str, limit: int) -> list[SearchResult]:
+ async def _search_postgresql(
+ self, query: str, limit: int, repository_id: str | None = None
+ ) -> list[SearchResult]:
"""PostgreSQL tsvector search with ts_rank scoring.
The query used to be handed to ``plainto_tsquery`` whole, which strips
@@ -777,17 +801,31 @@ class FullTextSearch:
ts_query = await self._build_ts_query(conn, query)
if not ts_query:
return []
- rows = await conn.execute(
- text(
- f"SELECT id, title, content, page_type, target_path, "
- f" ts_rank({PG_FTS_EXPRESSION}, to_tsquery('english', :q)) AS rank "
- f"FROM wiki_pages "
- f"WHERE {PG_FTS_EXPRESSION} @@ to_tsquery('english', :q) "
- f"ORDER BY rank DESC "
- f"LIMIT :lim",
- ),
- {"q": ts_query, "lim": limit},
- )
+ if repository_id is not None:
+ rows = await conn.execute(
+ text(
+ f"SELECT id, title, content, page_type, target_path, "
+ f" ts_rank({PG_FTS_EXPRESSION}, to_tsquery('english', :q)) AS rank "
+ f"FROM wiki_pages "
+ f"WHERE {PG_FTS_EXPRESSION} @@ to_tsquery('english', :q) "
+ f" AND repository_id = :repo_id "
+ f"ORDER BY rank DESC "
+ f"LIMIT :lim",
+ ),
+ {"q": ts_query, "lim": limit, "repo_id": repository_id},
+ )
+ else:
+ rows = await conn.execute(
+ text(
+ f"SELECT id, title, content, page_type, target_path, "
+ f" ts_rank({PG_FTS_EXPRESSION}, to_tsquery('english', :q)) AS rank "
+ f"FROM wiki_pages "
+ f"WHERE {PG_FTS_EXPRESSION} @@ to_tsquery('english', :q) "
+ f"ORDER BY rank DESC "
+ f"LIMIT :lim",
+ ),
+ {"q": ts_query, "lim": limit},
+ )
raw = rows.fetchall()
return [
diff --git a/packages/server/src/repowise/server/app.py b/packages/server/src/repowise/server/app.py
index 36a46e25a..75ae4c14a 100644
--- a/packages/server/src/repowise/server/app.py
+++ b/packages/server/src/repowise/server/app.py
@@ -284,6 +284,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
app.state.cross_repo_enricher = None
app.state.repo_registry = None # RepoRegistry, workspace mode only
app.state.workspace_sessions = {} # repo_id → session_factory
+ app.state.workspace_path_to_repo_id = {} # local_path → repo_id
app.state.workspace_engines = [] # engines to dispose on shutdown
# Per-repo FTS instances keyed by repo_id, used by the search router
# to fan out across every workspace repo (single-repo FTS lives on
@@ -310,54 +311,80 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
app.state.workspace_config = ws_config
app.state.workspace_root = str(ws_root)
- # Create per-repo DB engines so all workspace repos are accessible
- # via the same REST API (sidebar, repo-specific pages, etc.)
- import sqlite3 as _sqlite3
-
- for repo_entry in ws_config.repos:
- repo_path = (_Path(ws_root) / repo_entry.path).resolve()
- repo_db = repo_path / ".repowise" / "wiki.db"
- if not repo_db.exists():
- continue
- # Read repo_id from this DB
- try:
- conn = _sqlite3.connect(str(repo_db))
- row = conn.execute("SELECT id FROM repositories LIMIT 1").fetchone()
- conn.close()
- if not row:
+ from repowise.core.persistence.database import get_configured_db_url
+
+ configured_db_url = get_configured_db_url()
+ if configured_db_url is not None:
+ # Shared database mode (e.g. PostgreSQL, REPOWISE_DB_URL).
+ # Member repos are registered in the shared database and do not
+ # have per-repo .repowise/wiki.db files.
+ from repowise.core.persistence.crud import get_repository_by_path
+
+ async with get_session(session_factory) as session:
+ for repo_entry in ws_config.repos:
+ repo_path = (_Path(ws_root) / repo_entry.path).resolve()
+ try:
+ repo = await get_repository_by_path(session, str(repo_path))
+ if repo is not None:
+ app.state.workspace_sessions[repo.id] = session_factory
+ app.state.workspace_fts[repo.id] = fts
+ app.state.workspace_path_to_repo_id[str(repo_path)] = repo.id
+ except Exception:
+ logger.debug(
+ "workspace_shared_db_repo_lookup_failed",
+ extra={"path": str(repo_path)},
+ exc_info=True,
+ )
+ else:
+ # Create per-repo DB engines so all workspace repos are accessible
+ # via the same REST API (sidebar, repo-specific pages, etc.)
+ import sqlite3 as _sqlite3
+
+ for repo_entry in ws_config.repos:
+ repo_path = (_Path(ws_root) / repo_entry.path).resolve()
+ repo_db = repo_path / ".repowise" / "wiki.db"
+ if not repo_db.exists():
continue
- repo_id = row[0]
- except Exception:
- continue
-
- # Skip if this is the primary DB we already connected to
- # (the main engine already serves this repo) — but still
- # register the primary's FTS under its repo_id so the
- # search fan-out can include it.
- db_url_posix = repo_db.as_posix()
- if db_url and db_url_posix in db_url.replace("\\", "/"):
- app.state.workspace_fts[repo_id] = fts
- continue
-
- repo_engine = create_engine(f"sqlite+aiosqlite:///{db_url_posix}")
- await init_db(repo_engine)
- repo_sf = create_session_factory(repo_engine)
- app.state.workspace_sessions[repo_id] = repo_sf
- app.state.workspace_engines.append(repo_engine)
-
- # Build a per-repo FTS instance so the search router can
- # fan out queries across every workspace repo. Without
- # this, full-text search only ever sees the primary DB.
- try:
- repo_fts = FullTextSearch(repo_engine)
- await repo_fts.ensure_index()
- app.state.workspace_fts[repo_id] = repo_fts
- except Exception:
- logger.debug(
- "workspace_fts_init_failed",
- extra={"repo_id": repo_id},
- exc_info=True,
- )
+ # Read repo_id from this DB
+ try:
+ conn = _sqlite3.connect(str(repo_db))
+ row = conn.execute("SELECT id FROM repositories LIMIT 1").fetchone()
+ conn.close()
+ if not row:
+ continue
+ repo_id = row[0]
+ app.state.workspace_path_to_repo_id[str(repo_path)] = repo_id
+ except Exception:
+ continue
+
+ # Skip if this is the primary DB we already connected to
+ # (the main engine already serves this repo) — but still
+ # register the primary's FTS under its repo_id so the
+ # search fan-out can include it.
+ db_url_posix = repo_db.as_posix()
+ if db_url and db_url_posix in db_url.replace("\\", "/"):
+ app.state.workspace_fts[repo_id] = fts
+ continue
+
+ repo_engine = create_engine(f"sqlite+aiosqlite:///{db_url_posix}")
+ await init_db(repo_engine)
+ repo_sf = create_session_factory(repo_engine)
+ app.state.workspace_sessions[repo_id] = repo_sf
+ app.state.workspace_engines.append(repo_engine)
+
+ # Build a per-repo FTS instance so the search router can
+ # fan out queries across every workspace repo. Without
+ # this, full-text search only ever sees the primary DB.
+ try:
+ repo_fts = FullTextSearch(repo_engine)
+ await repo_fts.ensure_index()
+ app.state.workspace_fts[repo_id] = repo_fts
+ except Exception:
+ logger.debug(
+ "workspace_fts_init_failed",
+ extra={"repo_id": repo_id},
+ exc_info=True,
+ )
if app.state.workspace_sessions:
logger.info(
diff --git a/packages/server/src/repowise/server/repo_db.py b/packages/server/src/repowise/server/repo_db.py
index dfc507594..3a1582947 100644
--- a/packages/server/src/repowise/server/repo_db.py
+++ b/packages/server/src/repowise/server/repo_db.py
@@ -44,11 +44,17 @@ def repo_db_file(local_path: str | Path) -> Path:
def is_primary_db_for(app_state, local_path: str | Path) -> bool:
- """True when the server's primary database *is* this repo's wiki.db.
+ """True when the server's primary database serves this repo.
- In that case no per-repo engine is needed — the ambient session factory
- already reads and writes the canonical file.
+ When a database URL is explicitly configured (e.g. PostgreSQL or a shared
+ database), all repositories in the workspace share the ambient primary
+ database regardless of file path. Otherwise (repo-local SQLite mode), returns
+ True only when the ambient database file matches this repo's ``wiki.db``.
"""
+ from repowise.core.persistence.database import get_configured_db_url
+
+ if get_configured_db_url() is not None:
+ return True
db_url: str = getattr(app_state, "db_url", "") or ""
return repo_db_file(local_path).as_posix() in db_url.replace("\\", "/")
diff --git a/packages/server/src/repowise/server/routers/search.py b/packages/server/src/repowise/server/routers/search.py
index d227a07f6..ddc1ac696 100644
--- a/packages/server/src/repowise/server/routers/search.py
+++ b/packages/server/src/repowise/server/routers/search.py
@@ -11,6 +11,7 @@ from pathlib import Path
from fastapi import APIRouter, Depends, Query, Request
+from repowise.core.persistence.search import SearchResult
from repowise.core.providers.embedding import store_has_semantic_vectors
from repowise.server.deps import (
get_fts,
@@ -65,18 +66,11 @@ async def search(
- Workspace mode without ``repo_id``: fans out across every loaded
repo's index and merges results by score.
"""
- # A keyless index has no semantic vectors, so a semantic request is served
- # lexically rather than refused. Returning nothing would read as "not in the
- # codebase"; returning that store's nearest neighbours would be worse still,
- # because on it they are noise. Full-text is what the mode actually offers,
- # and what the docs already promise it offers.
if search_type == "fulltext" or not store_has_semantic_vectors(vector_store):
results = await _fulltext(request, query, limit, repo_id=repo_id, primary_fts=fts)
else:
results = await _semantic(request, query, limit, repo_id=repo_id, primary_vs=vector_store)
if results is None:
- # The scoped repo turned out to be keyless even though the primary
- # store is not. Serve it lexically, same as the whole-index case.
results = await _fulltext(request, query, limit, repo_id=repo_id, primary_fts=fts)
return [_to_response(r) for r in results]
@@ -93,7 +87,7 @@ async def _fulltext(request: Request, query: str, limit: int, *, repo_id, primar
# Single-repo mode (no workspace_fts registry) → use the primary FTS.
if not ws_fts:
- return await primary_fts.search(query, limit=limit)
+ return await primary_fts.search(query, limit=limit, repository_id=repo_id)
# Workspace mode with explicit repo_id.
if repo_id is not None:
@@ -106,13 +100,17 @@ async def _fulltext(request: Request, query: str, limit: int, *, repo_id, primar
if target_fts is None:
# Unknown repo_id — fall back to primary so callers don't
# silently get nothing.
- return await primary_fts.search(query, limit=limit)
- return await target_fts.search(query, limit=limit)
+ return await primary_fts.search(query, limit=limit, repository_id=repo_id)
+ return await target_fts.search(query, limit=limit, repository_id=repo_id)
# Workspace mode, no filter → fan out across every loaded FTS,
# merge by score, and cap at limit.
all_results = []
+ seen_fts = set()
for fts_inst in ws_fts.values():
+ if fts_inst in seen_fts:
+ continue
+ seen_fts.add(fts_inst)
try:
per_repo = await fts_inst.search(query, limit=limit)
except Exception:
@@ -149,9 +147,9 @@ async def _semantic(request: Request, query: str, limit: int, *, repo_id, primar
# repo-local database used by `repowise serve`.
return await primary_vs.search(query, limit=limit)
- # Fan-out: iterate over every workspace repo with an indexed wiki.db
- # and try to load its persisted LanceDB store. Repos without one
- # fall back to FTS so the user still gets some signal.
+ # Fan-out: iterate over every workspace repo and try to load its
+ # persisted LanceDB store. Repos without one fall back to FTS so the
+ # user still gets some signal.
ws_root = getattr(request.app.state, "workspace_root", None)
if ws_root is None:
return await primary_vs.search(query, limit=limit)
@@ -159,30 +157,33 @@ async def _semantic(request: Request, query: str, limit: int, *, repo_id, primar
all_results = []
workspace_fts = getattr(request.app.state, "workspace_fts", {}) or {}
+ path_to_rid = getattr(request.app.state, "workspace_path_to_repo_id", None) or {}
for entry in ws_config.repos:
repo_path = (ws_root_path / entry.path).resolve()
- db_path = repo_path / ".repowise" / "wiki.db"
- if not db_path.exists():
+ rid = path_to_rid.get(str(repo_path))
+ if rid is None:
+ db_path = repo_path / ".repowise" / "wiki.db"
+ if db_path.exists():
+ import sqlite3 as _sql
+
+ try:
+ with _sql.connect(str(db_path)) as conn:
+ row = conn.execute("SELECT id FROM repositories LIMIT 1").fetchone()
+ except Exception:
+ row = None
+ rid = row[0] if row else None
+
+ if rid is None:
continue
- # Resolve repo_id from the per-repo DB once (cached on app.state).
- import sqlite3 as _sql
-
- try:
- with _sql.connect(str(db_path)) as conn:
- row = conn.execute("SELECT id FROM repositories LIMIT 1").fetchone()
- except Exception:
- row = None
- rid = row[0] if row else None
# Try the vector store first.
per_repo = []
vs = None
- if rid is not None:
- try:
- vs = await resolve_repo_vector_store(request.app.state, rid)
- except Exception:
- vs = None
+ try:
+ vs = await resolve_repo_vector_store(request.app.state, rid, repo_path=repo_path)
+ except Exception:
+ vs = None
# A keyless repo is skipped here rather than searched, so it reaches the
# FTS fallback below. Its vector leg never returns empty (mock scores
# sit near 0.75 for anything), so without this the noise window would
@@ -196,11 +197,16 @@ async def _semantic(request: Request, query: str, limit: int, *, repo_id, primar
# than silently returning nothing on workspaces that haven't
# built LanceDB indexes yet.
if not per_repo and rid in workspace_fts:
+ fts_inst = workspace_fts[rid]
try:
- per_repo = await workspace_fts[rid].search(query, limit=limit)
+ per_repo = await fts_inst.search(query, limit=limit, repository_id=rid)
except Exception:
per_repo = []
all_results.extend(per_repo)
- all_results.sort(key=lambda r: r.score, reverse=True)
+ unique_results: dict[str, SearchResult] = {}
+ for r in all_results:
+ if r.page_id not in unique_results or r.score > unique_results[r.page_id].score:
+ unique_results[r.page_id] = r
+ all_results = sorted(unique_results.values(), key=lambda r: r.score, reverse=True)
return all_results[:limit]
diff --git a/tests/unit/persistence/test_search.py b/tests/unit/persistence/test_search.py
index e28391258..2d800f987 100644
--- a/tests/unit/persistence/test_search.py
+++ b/tests/unit/persistence/test_search.py
@@ -254,3 +254,60 @@ async def test_path_only_match_outranks_content_only_match(fts):
results = await fts.search("telemetry")
assert [r.page_id for r in results] == ["path-match", "content-match"]
+
+
+async def test_full_text_search_repository_id_filter(async_engine, async_session):
+ """When repository_id is given, only pages belonging to that repository are returned."""
+ from repowise.core.persistence import crud
+
+ repo_a = await crud.upsert_repository(
+ async_session, name="repo-a", local_path="/tmp/repo-a", repo_id="repo-a-id"
+ )
+ repo_b = await crud.upsert_repository(
+ async_session, name="repo-b", local_path="/tmp/repo-b", repo_id="repo-b-id"
+ )
+
+ await crud.upsert_page(
+ async_session,
+ page_id="p-a",
+ repository_id=repo_a.id,
+ page_type="file_page",
+ title="Worker Service",
+ content="Handles background queue jobs in worker service.",
+ summary="Worker queue",
+ target_path="worker.py",
+ source_hash="ha",
+ model_name="m",
+ provider_name="p",
+ )
+ await crud.upsert_page(
+ async_session,
+ page_id="p-b",
+ repository_id=repo_b.id,
+ page_type="file_page",
+ title="Queue Handler",
+ content="Handles background queue jobs in queue handler.",
+ summary="Queue handler",
+ target_path="queue.py",
+ source_hash="hb",
+ model_name="m",
+ provider_name="p",
+ )
+
+ fts = FullTextSearch(async_engine)
+ await fts.ensure_index()
+ await fts.index("p-a", "Worker Service", "Handles background queue jobs in worker service.")
+ await fts.index("p-b", "Queue Handler", "Handles background queue jobs in queue handler.")
+
+ # Unscoped returns both
+ all_hits = await fts.search("queue")
+ assert {r.page_id for r in all_hits} == {"p-a", "p-b"}
+
+ # Scoped to repo-a returns only p-a
+ a_hits = await fts.search("queue", repository_id="repo-a-id")
+ assert [r.page_id for r in a_hits] == ["p-a"]
+
+ # Scoped to repo-b returns only p-b
+ b_hits = await fts.search("queue", repository_id="repo-b-id")
+ assert [r.page_id for r in b_hits] == ["p-b"]
+
diff --git a/tests/unit/server/test_workspace_search_postgres.py b/tests/unit/server/test_workspace_search_postgres.py
new file mode 100644
index 000000000..5e5aaea71
--- /dev/null
+++ b/tests/unit/server/test_workspace_search_postgres.py
@@ -0,0 +1,303 @@
+"""Tests for workspace serve and search fan-out in shared DB / PostgreSQL mode."""
+
+from __future__ import annotations
+
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from httpx import ASGITransport, AsyncClient
+
+import repowise.server.mcp_server as mcp_mod
+from repowise.core.persistence import crud
+from repowise.core.persistence.database import (
+ create_engine,
+ create_session_factory,
+ get_session,
+ init_db,
+)
+from repowise.core.persistence.search import FullTextSearch
+from repowise.core.workspace.config import RepoEntry, WorkspaceConfig
+from repowise.server.app import create_app, lifespan
+
+
[email protected](autouse=True)
+def restore_tool_globals():
+ """The real lifespan writes process-global MCP tool state — put it back."""
+ saved_mcp = (
+ getattr(mcp_mod, "_registry", None),
+ getattr(mcp_mod, "_workspace_root", None),
+ getattr(mcp_mod, "_cross_repo_enricher", None),
+ getattr(mcp_mod, "_session_factory", None),
+ getattr(mcp_mod, "_fts", None),
+ getattr(mcp_mod, "_vector_store", None),
+ )
+ yield
+ (
+ mcp_mod._registry,
+ mcp_mod._workspace_root,
+ mcp_mod._cross_repo_enricher,
+ mcp_mod._session_factory,
+ mcp_mod._fts,
+ mcp_mod._vector_store,
+ ) = saved_mcp
+
+
+async def shared_db_workspace(tmp_path: Path):
+ """Set up a workspace with 2 repositories on a shared database."""
+ ws_root = tmp_path / "workspace"
+ ws_root.mkdir()
+
+ repo_a = ws_root / "repo-a"
+ repo_a.mkdir()
+ repo_b = ws_root / "repo-b"
+ repo_b.mkdir()
+
+ ws_config = WorkspaceConfig(
+ repos=[
+ RepoEntry(alias="repo-a", path="repo-a", is_primary=True),
+ RepoEntry(alias="repo-b", path="repo-b", is_primary=False),
+ ],
+ )
+ ws_config.save(ws_root)
+
+ # Shared database (simulating PostgreSQL / configured DB URL)
+ db_file = tmp_path / "shared.db"
+ db_url = f"sqlite+aiosqlite:///{db_file.as_posix()}"
+ engine = create_engine(db_url)
+ await init_db(engine)
+ session_factory = create_session_factory(engine)
+
+ # Register both repos in the shared DB
+ async with get_session(session_factory) as session:
+ r_a = await crud.upsert_repository(
+ session,
+ name="repo-a",
+ local_path=str(repo_a.resolve()),
+ default_branch="main",
+ repo_id="repo-a-id",
+ )
+ r_b = await crud.upsert_repository(
+ session,
+ name="repo-b",
+ local_path=str(repo_b.resolve()),
+ default_branch="main",
+ repo_id="repo-b-id",
+ )
+ # Add wiki pages for repo-a and repo-b
+ await crud.upsert_page(
+ session,
+ page_id="file_page:repo-a/worker.py",
+ repository_id=r_a.id,
+ page_type="file_page",
+ title="worker.py",
+ content="# Worker Module\n\nHandles background queue jobs in repo-a.",
+ summary="Worker for queue jobs",
+ target_path="worker.py",
+ source_hash="hash-a",
+ model_name="mock",
+ provider_name="mock",
+ )
+ await crud.upsert_page(
+ session,
+ page_id="file_page:repo-b/service.py",
+ repository_id=r_b.id,
+ page_type="file_page",
+ title="service.py",
+ content="# Service Module\n\nHandles background queue jobs in repo-b.",
+ summary="Service for queue jobs",
+ target_path="service.py",
+ source_hash="hash-b",
+ model_name="mock",
+ provider_name="mock",
+ )
+
+ fts = FullTextSearch(engine)
+ await fts.ensure_index()
+ await fts.index(
+ "file_page:repo-a/worker.py",
+ "worker.py",
+ "Handles background queue jobs in repo-a",
+ summary="Worker for queue jobs",
+ target_path="worker.py",
+ )
+ await fts.index(
+ "file_page:repo-b/service.py",
+ "service.py",
+ "Handles background queue jobs in repo-b",
+ summary="Service for queue jobs",
+ target_path="service.py",
+ )
+
+ yield {
+ "ws_root": ws_root,
+ "repo_a": repo_a,
+ "repo_b": repo_b,
+ "db_url": db_url,
+ "engine": engine,
+ "session_factory": session_factory,
+ "fts": fts,
+ }
+
+ await engine.dispose()
+
+
+async def test_workspace_lifespan_registers_members_on_shared_db(shared_db_workspace, monkeypatch):
+ """Workspace lifespan populates workspace_sessions and workspace_fts for member repos without wiki.db."""
+ ws_root = shared_db_workspace["ws_root"]
+ db_url = shared_db_workspace["db_url"]
+
+ monkeypatch.setenv("REPOWISE_DB_URL", db_url)
+ with (
+ patch("repowise.core.workspace.config.find_workspace_root", return_value=ws_root),
+ patch("repowise.server.app.setup_scheduler"),
+ ):
+ app = create_app()
+ async with lifespan(app):
+ assert app.state.workspace_config is not None
+ # Both member repos should be registered to the shared session factory and FTS
+ assert "repo-a-id" in app.state.workspace_sessions
+ assert "repo-b-id" in app.state.workspace_sessions
+ assert "repo-a-id" in app.state.workspace_fts
+ assert "repo-b-id" in app.state.workspace_fts
+ assert app.state.workspace_sessions["repo-a-id"] == app.state.session_factory
+ assert app.state.workspace_sessions["repo-b-id"] == app.state.session_factory
+
+
+async def test_workspace_search_fanout_shared_db(shared_db_workspace):
+ """Workspace search finds hits in non-primary member repos on a shared DB without wiki.db."""
+ from repowise.core.persistence.vector_store import InMemoryVectorStore
+ from repowise.core.providers.embedding.base import MockEmbedder
+ from tests.unit.server.conftest import _create_test_app
+
+ ws_root = shared_db_workspace["ws_root"]
+ repo_a = shared_db_workspace["repo_a"]
+ repo_b = shared_db_workspace["repo_b"]
+
+ app = _create_test_app()
+ vector_store = InMemoryVectorStore(embedder=MockEmbedder())
+
+ app.state.engine = shared_db_workspace["engine"]
+ app.state.session_factory = shared_db_workspace["session_factory"]
+ app.state.fts = shared_db_workspace["fts"]
+ app.state.vector_store = vector_store
+ app.state.background_tasks = set()
+ app.state.workspace_root = str(ws_root)
+ app.state.workspace_config = WorkspaceConfig(
+ repos=[
+ RepoEntry(alias="repo-a", path="repo-a", is_primary=True),
+ RepoEntry(alias="repo-b", path="repo-b", is_primary=False),
+ ],
+ )
+ app.state.workspace_path_to_repo_id = {
+ str(repo_a.resolve()): "repo-a-id",
+ str(repo_b.resolve()): "repo-b-id",
+ }
+ app.state.workspace_sessions = {
+ "repo-a-id": shared_db_workspace["session_factory"],
+ "repo-b-id": shared_db_workspace["session_factory"],
+ }
+ app.state.workspace_fts = {
+ "repo-a-id": shared_db_workspace["fts"],
+ "repo-b-id": shared_db_workspace["fts"],
+ }
+
+ async with AsyncClient(
+ transport=ASGITransport(app=app),
+ base_url="http://test",
+ headers={"X-API-Key": "test"},
+ ) as client:
+ # Fulltext search fan-out across workspace returns hits from all member repos
+ resp = await client.get(
+ "/api/search",
+ params={"query": "queue", "search_type": "fulltext"},
+ )
+ assert resp.status_code == 200
+ hits = resp.json()
+ assert len(hits) == 2
+ page_ids = {h["page_id"] for h in hits}
+ assert page_ids == {"file_page:repo-a/worker.py", "file_page:repo-b/service.py"}
+
+ # Scoped search to repo-b returns only repo-b's page
+ resp_b = await client.get(
+ "/api/search",
+ params={"query": "queue", "search_type": "fulltext", "repo_id": "repo-b-id"},
+ )
+ assert resp_b.status_code == 200
+ hits_b = resp_b.json()
+ assert len(hits_b) == 1
+ assert hits_b[0]["page_id"] == "file_page:repo-b/service.py"
+
+ # Scoped search to repo-a returns only repo-a's page
+ resp_a = await client.get(
+ "/api/search",
+ params={"query": "queue", "search_type": "fulltext", "repo_id": "repo-a-id"},
+ )
+ assert resp_a.status_code == 200
+ hits_a = resp_a.json()
+ assert len(hits_a) == 1
+ assert hits_a[0]["page_id"] == "file_page:repo-a/worker.py"
+
+ await vector_store.close()
+
+
+async def test_workspace_semantic_search_fanout_fallback(shared_db_workspace):
+ """Semantic search fans out across member repos without wiki.db and falls back to FTS."""
+ from repowise.core.persistence.vector_store import InMemoryVectorStore
+ from repowise.core.providers.embedding.base import MockEmbedder
+ from tests.unit.server.conftest import _create_test_app
+
+ ws_root = shared_db_workspace["ws_root"]
+ repo_a = shared_db_workspace["repo_a"]
+ repo_b = shared_db_workspace["repo_b"]
+
+ app = _create_test_app()
+ vector_store = InMemoryVectorStore(embedder=MockEmbedder())
+
+ app.state.engine = shared_db_workspace["engine"]
+ app.state.session_factory = shared_db_workspace["session_factory"]
+ app.state.fts = shared_db_workspace["fts"]
+ app.state.vector_store = vector_store
+ app.state.background_tasks = set()
+ app.state.workspace_root = str(ws_root)
+ app.state.workspace_config = WorkspaceConfig(
+ repos=[
+ RepoEntry(alias="repo-a", path="repo-a", is_primary=True),
+ RepoEntry(alias="repo-b", path="repo-b", is_primary=False),
+ ],
+ )
+ app.state.workspace_path_to_repo_id = {
+ str(repo_a.resolve()): "repo-a-id",
+ str(repo_b.resolve()): "repo-b-id",
+ }
+ app.state.workspace_sessions = {
+ "repo-a-id": shared_db_workspace["session_factory"],
+ "repo-b-id": shared_db_workspace["session_factory"],
+ }
+ app.state.workspace_fts = {
+ "repo-a-id": shared_db_workspace["fts"],
+ "repo-b-id": shared_db_workspace["fts"],
+ }
+
+ async with AsyncClient(
+ transport=ASGITransport(app=app),
+ base_url="http://test",
+ headers={"X-API-Key": "test"},
+ ) as client:
+ resp = await client.get(
+ "/api/search",
+ params={"query": "queue", "search_type": "semantic"},
+ )
+ assert resp.status_code == 200
+ hits = resp.json()
+ assert len(hits) == 2
+ page_ids = {h["page_id"] for h in hits}
+ assert page_ids == {"file_page:repo-a/worker.py", "file_page:repo-b/service.py"}
+
+ await vector_store.close()
+
+
diff --git a/tests/unit/server/test_workspace_sidebar.py b/tests/unit/server/test_workspace_sidebar.py
index e0d297c89..f3b56b75b 100644
--- a/tests/unit/server/test_workspace_sidebar.py
+++ b/tests/unit/server/test_workspace_sidebar.py
@@ -75,7 +75,7 @@ async def _build_repo_engine(repo_path: Path):
)
await init_db(engine)
sf = async_sessionmaker(engine, expire_on_commit=False)
- from repowise.core.persistence import upsert_repository
+ from repowise.core.persistence import crud, upsert_repository
from repowise.core.persistence.database import get_session
async with get_session(sf) as session:
@@ -91,12 +91,29 @@ async def _build_repo_engine(repo_path: Path):
node_type="file",
)
)
+ await crud.upsert_page(
+ session,
+ page_id="p1",
+ repository_id=repo.id,
+ page_type="file_page",
+ title="Auth module",
+ content="Authentication and JWT verification flow.",
+ summary="Auth module",
+ target_path="auth.py",
+ source_hash="h1",
+ model_name="mock",
+ provider_name="mock",
+ )
await session.commit()
fts = FullTextSearch(engine)
await fts.ensure_index()
- # Index a single page directly into FTS — we don't need a real
- # wiki_pages row for the search router tests.
- await fts.index("p1", "Auth module", "Authentication and JWT verification flow.")
+ await fts.index(
+ "p1",
+ "Auth module",
+ "Authentication and JWT verification flow.",
+ summary="Auth module",
+ target_path="auth.py",
+ )
return engine, sf, fts, repo.id