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): resolve the provider-key repo by id, not the request path (#985)

Raghav Chamadiya4b33d88fd3e2
diff --git a/packages/server/src/repowise/server/routers/providers.py b/packages/server/src/repowise/server/routers/providers.py
index 1683b59aa..af97bf09a 100644
--- a/packages/server/src/repowise/server/routers/providers.py
+++ b/packages/server/src/repowise/server/routers/providers.py
@@ -6,7 +6,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request
from repowise.core.persistence import crud
from repowise.core.persistence.database import get_session
-from repowise.server.deps import resolve_request_session_factory, verify_api_key
+from repowise.server.deps import resolve_session_factory, verify_api_key
from repowise.server.provider_config import (
list_provider_status,
set_active_provider,
@@ -27,11 +27,16 @@ async def _repo_path_for(request: Request, repo_id: str | None) -> str | None:
Lets the providers endpoint report the active provider/model that *this*
repo's chat will actually use. Returns ``None`` (server-global resolution)
when no ``repo_id`` is given or the repo can't be found.
+
+ Routes by ``repo_id`` directly (not the request's path/query), so it reaches
+ a workspace repo's own ``wiki.db`` even when ``repo_id`` arrived in the body
+ (the ``POST .../key`` case). The primary DB does not hold the canonical row
+ for a non-primary workspace repo, so a request-scoped lookup would miss it.
"""
if not repo_id:
return None
try:
- factory = resolve_request_session_factory(request)
+ factory = resolve_session_factory(request.app.state, repo_id)
async with get_session(factory) as session:
repo = await crud.get_repository(session, repo_id)
return repo.local_path if repo else None
diff --git a/tests/unit/server/test_provider_key_endpoint.py b/tests/unit/server/test_provider_key_endpoint.py
index 70d3d3416..c962a6376 100644
--- a/tests/unit/server/test_provider_key_endpoint.py
+++ b/tests/unit/server/test_provider_key_endpoint.py
@@ -71,6 +71,47 @@ async def test_remove_key_with_repo_id_clears_repo_env(client: AsyncClient) -> N
assert "ANTHROPIC_API_KEY" not in load_repo_env(Path(repo["local_path"]))
+async def test_add_key_resolves_non_primary_workspace_repo(client: AsyncClient, app, tmp_path) -> None:
+ # Workspace mode: a non-primary repo's row lives only in its own wiki.db, not
+ # the primary DB. The key endpoint gets repo_id in the body, so resolution
+ # must route by repo_id (not the request path/query) to reach that DB.
+ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
+ from sqlalchemy.pool import StaticPool
+
+ from repowise.core.persistence import crud
+ from repowise.core.persistence.database import init_db
+ from repowise.core.repo_config import load_repo_env
+
+ ws_engine = create_async_engine(
+ "sqlite+aiosqlite:///:memory:",
+ connect_args={"check_same_thread": False},
+ poolclass=StaticPool,
+ )
+ await init_db(ws_engine)
+ ws_factory = async_sessionmaker(ws_engine, expire_on_commit=False, class_=AsyncSession)
+
+ repo_dir = tmp_path / "frontend"
+ (repo_dir / ".repowise").mkdir(parents=True)
+ async with ws_factory() as s:
+ repo = await crud.upsert_repository(s, name="frontend", local_path=str(repo_dir))
+ await s.commit()
+ repo_id = repo.id
+
+ # Register the per-repo DB; its row is deliberately absent from the primary DB.
+ app.state.workspace_sessions = {repo_id: ws_factory}
+ try:
+ resp = await client.post(
+ "/api/providers/anthropic/key",
+ json={"api_key": "sk-ws-key", "repo_id": repo_id},
+ )
+ assert resp.status_code == 204
+ assert load_repo_env(repo_dir)["ANTHROPIC_API_KEY"] == "sk-ws-key"
+ finally:
+ app.state.workspace_sessions = {}
+ await ws_engine.dispose()
+
+
@pytest.mark.asyncio
async def test_get_providers_returns_no_key_material(client: AsyncClient) -> None:
repo = await create_test_repo(client)