Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(cli): resolve --embedder auto through the repo pin (#2032)
Azhar Dewji49411aee6e4b
diff --git a/packages/cli/src/repowise/cli/commands/reindex_cmd.py b/packages/cli/src/repowise/cli/commands/reindex_cmd.pyindex 3a81ef134..30d26c652 100644--- a/packages/cli/src/repowise/cli/commands/reindex_cmd.py+++ b/packages/cli/src/repowise/cli/commands/reindex_cmd.py@@ -48,20 +48,32 @@ async def _reindex(repo_path, embedder_name: str, batch_size: int) -> None:from sqlalchemy import selectfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker- from repowise.cli.providers.embedders import build_embedder+ from repowise.cli.providers.embedders import build_embedder, resolve_embedder_for_repofrom repowise.core.persistence.database import create_engine, init_dbfrom repowise.core.persistence.models import Pagefrom repowise.core.providers.embedding.base import MockEmbedder# --- Resolve embedder ---- requested_embedder = embedder_nameif embedder_name == "auto":- from repowise.cli.commands.init_cmd import _resolve_embedder-- embedder_name = _resolve_embedder(None)+ # Resolve through the repo, not the environment. `_resolve_embedder(None)`+ # reads REPOWISE_EMBEDDER and API keys only, so `auto` never saw the+ # `embedder` pin in config.yaml despite this flag's help text promising+ # "env vars / config". A repo pinned to a keyless embedder therefore+ # aborted with "No real embedder available", and a repo pinned to one+ # embedder in a shell exporting a different provider's key had its table+ # REWRITTEN by that other provider — the exact writer/reader split+ # `resolve_embedder_for_repo` was added to prevent, and one this command+ # is on the writing side of.+ embedder_name = resolve_embedder_for_repo(repo_path)embedder_impl = build_embedder(embedder_name, repo_path)- if isinstance(embedder_impl, MockEmbedder) and requested_embedder != "mock":+ # Guard on the RESOLVED name, not the flag. `mock` reached here two ways:+ # asked for, or fallen back to because nothing else was configured. Only the+ # second is an error. Now that `auto` reads the pin, `embedder: mock` in+ # config.yaml is a third way, and it is as deliberate as passing the flag —+ # comparing against `requested_embedder` would abort a repo that is pinned+ # to mock on purpose.+ if isinstance(embedder_impl, MockEmbedder) and embedder_name != "mock":console.print("[red]No real embedder available. Set a real embedder key, configure Ollama, or pass --embedder mock for test vectors.[/red]")diff --git a/tests/unit/cli/test_reindex_cmd.py b/tests/unit/cli/test_reindex_cmd.pyindex 06a675dbb..0fd2b5aa8 100644--- a/tests/unit/cli/test_reindex_cmd.py+++ b/tests/unit/cli/test_reindex_cmd.py@@ -160,3 +160,82 @@ async def test_reindex_aborts_when_every_item_failed(with pytest.raises(click.Abort):await reindex_cmd._reindex(tmp_path, "openai", batch_size=20)+++async def test_reindex_auto_honours_the_repo_pinned_embedder(+ monkeypatch,+ tmp_path: Path,+) -> None:+ """`--embedder auto` must read config.yaml's pin, not just the environment.++ The pin is what wrote the vector table, so it is what must rewrite it. When+ `auto` resolved from env vars alone, a repo pinned to one embedder in a+ shell exporting another provider's key had its table rewritten by that other+ provider, leaving every reader — which does honour the pin — querying+ vectors of the wrong width.+ """+ db_url = f"sqlite+aiosqlite:///{tmp_path / 'wiki.db'}"+ (tmp_path / ".repowise").mkdir(parents=True, exist_ok=True)+ (tmp_path / ".repowise" / "config.yaml").write_text(+ "embedder: ollama\n", encoding="utf-8"+ )++ built: dict[str, str] = {}++ def fake_build_embedder(name: str, _repo_path: object):+ built["name"] = name+ raise click.Abort()++ # REPOWISE_EMBEDDER legitimately outranks the pin, so a test about pin+ # precedence has to clear it or it is asserting the wrong rule. It is also+ # genuinely leaked into this process by+ # test_embedder_resolution.py::test_the_reader_prefers_the_repo_pin, whose+ # `monkeypatch.delenv(..., raising=False)` records nothing when the variable+ # is already absent — so the value the production code then sets has no+ # saved state to restore and survives into later tests.+ monkeypatch.delenv("REPOWISE_EMBEDDER", raising=False)+ # An unrelated provider key exported in the environment. Before the fix this+ # is what `auto` resolved to, silently overriding the repo's own pin.+ monkeypatch.setenv("OPENAI_API_KEY", "sk-test")+ monkeypatch.setattr(reindex_cmd, "get_db_url_for_repo", lambda _repo_path: db_url)+ monkeypatch.setattr(+ "repowise.cli.providers.embedders.build_embedder", fake_build_embedder+ )++ with pytest.raises(click.Abort):+ await reindex_cmd._reindex(tmp_path, "auto", batch_size=20)++ assert built["name"] == "ollama"+++async def test_reindex_does_not_abort_on_a_deliberately_pinned_mock_embedder(+ monkeypatch,+ tmp_path: Path,+) -> None:+ """`embedder: mock` in config.yaml is a choice, not a missing embedder.++ The abort exists for the fallback case — nothing configured, so build_embedder+ quietly hands back a MockEmbedder and a real reindex would write meaningless+ vectors. A repo that pins mock has said what it wants, the same as passing+ --embedder mock, and must not be treated as unconfigured.+ """+ db_url = f"sqlite+aiosqlite:///{tmp_path / 'wiki.db'}"+ (tmp_path / ".repowise").mkdir(parents=True, exist_ok=True)+ (tmp_path / ".repowise" / "config.yaml").write_text(+ "embedder: mock\n", encoding="utf-8"+ )++ monkeypatch.delenv("REPOWISE_EMBEDDER", raising=False)+ monkeypatch.setattr(reindex_cmd, "get_db_url_for_repo", lambda _repo_path: db_url)+ monkeypatch.setattr(+ "repowise.core.persistence.database.create_engine", lambda _url: _DummyEngine()+ )++ async def fake_init_db(_engine: object) -> None:+ return None++ monkeypatch.setattr("repowise.core.persistence.database.init_db", fake_init_db)+ monkeypatch.setattr("sqlalchemy.ext.asyncio.async_sessionmaker", _sessionmaker)++ # No Abort: reaches the normal empty-database path and returns.+ await reindex_cmd._reindex(tmp_path, "auto", batch_size=20)