Skip to content

The agent that grows with you

fix: custom-provider key_env in the CLI picker/catalog helpers reads through the profile secret scope

teknium141ead01d6d10
diff --git a/hermes_cli/model_setup_flows_custom.py b/hermes_cli/model_setup_flows_custom.py
index fb250f6e3ef..ef444622ece 100644
--- a/hermes_cli/model_setup_flows_custom.py
+++ b/hermes_cli/model_setup_flows_custom.py
@@ -334,9 +334,11 @@ def _model_flow_named_custom(config, provider_info):
saved_model = provider_info.get("model", "")
provider_key = (provider_info.get("provider_key") or "").strip()
- # Resolve key from env var if api_key not set directly
+ # Resolve key_env through the profile secret scope (fresh .env; never another
+ # profile's process env under multiplexing), like the runtime does (#67935).
if not api_key and key_env:
- api_key = os.environ.get(key_env, "")
+ from agent.secret_scope import get_secret_str
+ api_key = get_secret_str(key_env, "")
# Only configured credentials may be persisted, never a short-lived probe token.
config_api_key = _custom_provider_api_key_config_value(provider_info, api_key)
diff --git a/hermes_cli/models_local.py b/hermes_cli/models_local.py
index 5597f1f953b..9f0315e2d76 100644
--- a/hermes_cli/models_local.py
+++ b/hermes_cli/models_local.py
@@ -19,6 +19,7 @@ import urllib.parse
import urllib.request
from pathlib import Path
from typing import Any, NamedTuple, Optional
+from agent.secret_scope import get_secret_str
from hermes_cli.urllib_security import url_origin
# Log-record parity with the origin module.
@@ -116,12 +117,16 @@ def _get_ollama_base_url() -> str:
def _api_key_from_provider_config(entry: dict, *env_keys: str) -> str:
- """``api_key`` from a provider config block, else the env var named by the first set *env_keys*."""
+ """``api_key`` from a provider config block, else the env var named by the first set *env_keys*.
+
+ The variable is read through the profile secret scope (fresh ``.env``, never another
+ profile's process env under multiplexing), like every other credential read (#67935).
+ """
api_key = str(entry.get("api_key") or "").strip()
if api_key:
return api_key
key_env = str(next((entry.get(k) for k in env_keys if entry.get(k)), "") or "").strip()
- return os.getenv(key_env, "").strip() if key_env else ""
+ return get_secret_str(key_env, "").strip() if key_env else ""
def _drop_authorization(headers: dict[str, str]) -> None:
diff --git a/tests/hermes_cli/test_custom_provider_key_env_scope.py b/tests/hermes_cli/test_custom_provider_key_env_scope.py
new file mode 100644
index 00000000000..22d9e10cb94
--- /dev/null
+++ b/tests/hermes_cli/test_custom_provider_key_env_scope.py
@@ -0,0 +1,54 @@
+"""Custom-provider ``key_env`` reads in the CLI picker/catalog helpers go through the profile
+secret scope (#67935): a key that lives only in the profile's ``.env`` must be found, and
+another profile's process-env value must never be picked up under multiplexing."""
+
+from unittest.mock import patch
+
+import pytest
+
+from agent import secret_scope
+
+
+def scoped_profile(tmp_path, monkeypatch):
+ """Gateway shape: multiplex on, this profile's ``.env`` installed as the secret scope,
+ the variable absent from (or different in) the process environment."""
+ home = tmp_path / "hermes"
+ home.mkdir()
+ (home / "config.yaml").write_text("model: old-model\ncustom_providers: []\n")
+ (home / ".env").write_text("EXAMPLE_PROVIDER_API_KEY=sk-from-profile-dotenv\n")
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setenv("EXAMPLE_PROVIDER_API_KEY", "sk-other-profile-process-env")
+ secret_scope.set_multiplex_active(True)
+ token = secret_scope.set_secret_scope(secret_scope.build_profile_secret_scope(home))
+ try:
+ yield home
+ finally:
+ secret_scope.reset_secret_scope(token)
+ secret_scope.set_multiplex_active(False)
+
+
+def test_provider_config_key_env_resolves_through_secret_scope(scoped_profile):
+ from hermes_cli.models_local import _api_key_from_provider_config
+
+ entry = {"base_url": "https://ollama.internal/v1", "key_env": "EXAMPLE_PROVIDER_API_KEY"}
+ assert _api_key_from_provider_config(entry, "key_env", "api_key_env") == "sk-from-profile-dotenv"
+
+
+def test_named_custom_flow_probes_with_scoped_key_env(scoped_profile):
+ from hermes_cli.model_setup_flows import _model_flow_named_custom
+
+ provider_info = {
+ "name": "Example Provider",
+ "base_url": "https://api.example-provider.test/v1",
+ "api_key": "",
+ "key_env": "EXAMPLE_PROVIDER_API_KEY",
+ "model": "qwen3.6-35b-fast",
+ }
+ with patch("hermes_cli.models.fetch_api_models", return_value=["qwen3.6-35b-fast"]) as mock_fetch, \
+ patch("hermes_cli.curses_ui.curses_radiolist", side_effect=ImportError), \
+ patch("builtins.input", return_value="1"), \
+ patch("builtins.print"):
+ _model_flow_named_custom({}, provider_info)
+
+ assert mock_fetch.call_args.args[0] == "sk-from-profile-dotenv"