Repository navigation
fix(server): read the embedder key from the process env so CLI and MCP agree - #1810
Conversation
…P agree The MCP server's _persisted_embedder_key never consulted the process environment, so on any machine with an exported OPENAI_API_KEY (or other provider key) of a different credential than the repo's .repowise/.env, the CLI (which reads env first) and the MCP server (which skipped env) resolved different keys for the same repo in the same shell. Semantic search degraded/hung through the CLI while the MCP tool answered (issue repowise-dev#1711). Unify the other way, preserving the CLI's deliberate 'exported key is an explicit override' contract: the server now reads the process env tier first, before the repo .env and global config — matching cli/providers/keys.py. Fixes repowise-dev#1711
|
✅ Health of changed files: 3.7 → 3.9 (+0.1) 📋 At a glance Files & modules (2)
✅ Health gate: passed 📌 Before you merge
🔎 More signals (2)🗺️ Change map flowchart LR
subgraph PR ["Changed in this PR (1 with dependents)"]
f_packages_server_src_repowise_server_mcp_server__server_py[".../mcp_server/_server.py 🔥"]:::changed
end
f_packages_cli_src_repowise_cli_helpers_py[".../cli/helpers.py"]
f_packages_server_src_repowise_server_mcp_server__server_py --> f_packages_cli_src_repowise_cli_helpers_py
f_packages_core_src_repowise_core_workspace_update_py[".../workspace/update.py"]
f_packages_server_src_repowise_server_mcp_server__server_py --> f_packages_core_src_repowise_core_workspace_update_py
f_packages_server_src_repowise_server_app_py[".../server/app.py"]
f_packages_server_src_repowise_server_mcp_server__server_py --> f_packages_server_src_repowise_server_app_py
f_packages_server_src_repowise_server_mcp_server___init___py[".../mcp_server/__init__.py"]
f_packages_server_src_repowise_server_mcp_server__server_py --> f_packages_server_src_repowise_server_mcp_server___init___py
t_tests_unit_cli_test_mcp_transport_py(["✅ .../cli/test_mcp_transport.py"]):::guard
t_tests_unit_cli_test_mcp_transport_py -.-> f_packages_server_src_repowise_server_mcp_server__server_py
classDef changed fill:#dbeafe,stroke:#1d4ed8,color:#1e3a5f
classDef warn fill:#fef3c7,stroke:#b45309,color:#78350f
classDef guard fill:#dcfce7,stroke:#15803d,color:#14532d
Solid arrows: code that imports the changed files (4 direct dependents, from the last indexed snapshot). Dashed: history/tests. 🔥 Hotspots touched (2)
👀 Suggested reviewers @RaghavChamadiya 📊 See the full report for this PR |
RaghavChamadiya
left a comment
There was a problem hiding this comment.
Thanks @sloemo01 for resolving this in the direction that keeps the existing contract rather than the one the issue proposed. That was the judgement call in this fix and you got it right.
Confirmed both resolvers on main. The CLI checks os.environ first (cli/providers/keys.py:81), then the repo .env, then the global config. _persisted_embedder_key went straight to the repo .env and then the global config, with no env tier at all. Unifying upward preserves test_env_wins_over_everything; unifying downward would have satisfied the issue and broken that test, and with it the deliberate rule that an exported key is an explicit override.
The failure this produces is nastier than a plain mismatch, which is probably why it took a report to find. Both sides succeed. Both build a real embedder. The vectors just cannot match the index one of them wrote, so semantic search degrades to full-text on one surface and works on the other, in the same directory, in the same shell.
One non-blocking note for whenever you are next in that file: the docstring above the function still reads "Order: the served repo's .repowise/.env first, then the global config", which now understates it by a tier. Not holding the merge on a comment.
What
Fixes #1711. The MCP server's
_persisted_embedder_keynever consulted the process environment, so on any machine with an exportedOPENAI_API_KEY(or other provider key) of a different credential than the repo's.repowise/.env, the CLI (which reads env first) and the MCP server (which skipped env) resolved different keys for the same repo in the same shell. Semantic search degraded/hung through the CLI while the MCP tool answered.Root cause
Two resolvers with different precedence:
cli/providers/keys.py::resolve_embedder_api_key): process env → repo.env→ global config._server.py::_persisted_embedder_key): repo.env→ global config — never the process env.So on any machine with an exported credential, the two disagreed about the same directory.
Fix
Unify the other way than the issue suggested, to preserve the CLI's deliberate contract that an exported key is an explicit override (
test_env_wins_over_everything): the server now reads the process env tier first, before the repo.envand global config — matchingcli/providers/keys.py. This makes CLI and MCP resolve identically on every machine, and keeps the existing precedence contract intact.Tests
test_env_export_wins_in_both_cli_and_server— asserts an exportedOPENAI_API_KEYwins on both the CLI and the MCP server for the same repo.tests/unit/cli/test_embedder_key_resolution.py— 20 passed, no regression (including the existingtest_env_wins_over_everything).