Skip to content

The agent that grows with you

fix(model_switch): provider switch clears model.key_env/api_key_env in the canonical persist shape

teknium11987b5e46198
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py
index e019505746b..9976efb6a2a 100644
--- a/hermes_cli/model_switch.py
+++ b/hermes_cli/model_switch.py
@@ -1543,7 +1543,11 @@ def model_selection_config_updates(result: ModelSwitchResult, current_model_cfg:
``model.api_key`` is a leftover that would contaminate later custom resolution. For custom
targets the inline key belongs to ONE endpoint: it survives only a same-route re-pick (same
provider and base_url) — ``custom:a`` -> ``custom:b`` must not hand endpoint A's secret to B.
- The dashboard re-adds an explicitly submitted key after this (``_apply_main_model_assignment``)."""
+ The ``key_env`` / ``api_key_env`` credential POINTER (written by custom-endpoint activation,
+ resolved by runtime_provider / auxiliary_client) clears under the same rule: left behind, it
+ routes the NEW provider's requests to the OLD endpoint's env var. The dashboard re-adds an
+ explicitly submitted key / the target provider's own pointer after this
+ (``_apply_main_model_assignment`` / ``_resolve_assignment_credentials``)."""
model_cfg = current_model_cfg if isinstance(current_model_cfg, dict) else {}
updates: dict[str, Any] = {
"default": result.new_model, "provider": result.target_provider,
@@ -1557,7 +1561,7 @@ def model_selection_config_updates(result: ModelSwitchResult, current_model_cfg:
updates["context_length"] = None
target = str(result.target_provider or "").strip().lower()
if not target.startswith("custom") or _route_changed(model_cfg, result):
- for key in ("api_key", "api"):
+ for key in ("api_key", "api", "key_env", "api_key_env"):
if key in model_cfg:
updates[key] = None
return updates
diff --git a/tests/hermes_cli/test_model_persist_one_shape.py b/tests/hermes_cli/test_model_persist_one_shape.py
index eb9939982b0..6b9f32e93d8 100644
--- a/tests/hermes_cli/test_model_persist_one_shape.py
+++ b/tests/hermes_cli/test_model_persist_one_shape.py
@@ -132,6 +132,24 @@ def test_custom_to_other_custom_endpoint_drops_the_inline_key(seeded_home):
assert "api_key" not in _model_block(seeded_home)
+def test_provider_switch_drops_the_key_env_pointer_but_a_same_route_repick_keeps_it(seeded_home):
+ """``model.key_env`` is a credential POINTER (custom-endpoint activation writes it with no inline
+ key). Surviving a provider switch it routes the new provider's requests to the old endpoint's
+ env var, so it clears like ``api_key``; a same-route re-pick keeps it like ``api_key``."""
+ from hermes_cli.model_switch import persist_model_selection
+ seed = _SEED.replace(" api_key: sk-stale\n", " key_env: CUSTOM_BOX_API_KEY\n")
+ (seeded_home / "config.yaml").write_text(seed, encoding="utf-8")
+ persist_model_selection(_RESULT)
+ assert "key_env" not in _model_block(seeded_home)
+
+ (seeded_home / "config.yaml").write_text(seed, encoding="utf-8")
+ same_route = ModelSwitchResult(
+ success=True, new_model="local-model", target_provider="custom",
+ base_url="http://localhost:1234/v1", api_mode="anthropic_messages", is_global=True)
+ persist_model_selection(same_route)
+ assert _model_block(seeded_home)["key_env"] == "CUSTOM_BOX_API_KEY"
+
+
def test_gateway_persists_to_the_profile_config_it_was_given(tmp_path, monkeypatch):
"""Multiplexed gateway: the write lands in the routed profile's config.yaml, never the
process-level HERMES_HOME."""