Skip to content

fix(server): read the embedder key from the process env so CLI and MCP agree - #1810

Merged
RaghavChamadiya merged 1 commit into
repowise-dev:mainfrom
sloemo01:fix/embedder-key-precedence
Aug 21, 2026
Merged

RaghavChamadiya merged 1 commit into
repowise-dev:mainfrom
sloemo01:fix/embedder-key-precedence

Conversation

@sloemo01

Copy link
Copy Markdown
Contributor

What

Fixes #1711. 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.

Root cause

Two resolvers with different precedence:

  • CLI (cli/providers/keys.py::resolve_embedder_api_key): process env → repo .env → global config.
  • MCP server (_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 .env and global config — matching cli/providers/keys.py. This makes CLI and MCP resolve identically on every machine, and keeps the existing precedence contract intact.

Tests

  • New test_env_export_wins_in_both_cli_and_server — asserts an exported OPENAI_API_KEY wins 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 existing test_env_wins_over_everything).
  • Ruff clean.

…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
@repowise-bot

repowise-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

✅ Health of changed files: 3.7 → 3.9 (+0.1)

📋 At a glance
2 hotspots touched · 1 new finding introduced · 2 files with recent fix history.

Files & modules (2)
  • packages (1 file)
    • .../mcp_server/_server.py
  • tests (1 file)
    • .../cli/test_embedder_key_resolution.py

✅ Health gate: passed

📌 Before you merge

  • Run .../cli/test_mcp_transport.py, .../mcp/test_config.py, .../mcp/test_embedder_resolution.py: they import the changed files
🔎 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
Loading

Solid arrows: code that imports the changed files (4 direct dependents, from the last indexed snapshot). Dashed: history/tests.

🔥 Hotspots touched (2)

  • .../mcp_server/_server.py: 22 commits/90d, 9 dependents · primary owner: Raghav Chamadiya (91%)
  • .../cli/test_embedder_key_resolution.py: 1 commits/90d, 0 dependents · primary owner: Raghav Chamadiya (100%)

👀 Suggested reviewers @RaghavChamadiya


📊 See the full report for this PR
Your repo map with this PR's blast radius lit up, every caller of the contracts it changes, and health before and after. No sign-in. · ⭐ Star Repowise · 📥 Install bot · Silence on a single PR with [skip repowise] in the title · Per-repo toggle on repowise.dev/settings?tab=bot · Updated 2026-08-21 11:35 UTC

@RaghavChamadiya RaghavChamadiya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@RaghavChamadiya
RaghavChamadiya merged commit cd86664 into repowise-dev:main Aug 21, 2026
9 checks passed
RaghavChamadiya pushed a commit that referenced this pull request Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI vs MCP embedder key precedence drift: exported OPENAI_API_KEY outranks the repo's .repowise/.env for CLI commands but not the MCP server

2 participants