Skip to content

The agent that grows with you

fix(config): provider switch clears the stale model.key_env pointer

Teknium68ebb2c99675
diff --git a/hermes_cli/config.py b/hermes_cli/config.py
index ad60058c4c6..d5d26ac68b0 100644
--- a/hermes_cli/config.py
+++ b/hermes_cli/config.py
@@ -829,6 +829,13 @@ def clear_model_endpoint_credentials(
if clear_api_key:
model_cfg.pop("api_key", None)
model_cfg.pop("api", None)
+ # key_env is a first-class credential POINTER (runtime_provider and
+ # auxiliary_client resolve it), written by custom-endpoint activation.
+ # Leaving it behind on a provider switch routes the NEW provider's
+ # requests to the OLD endpoint's env var — same staleness class as an
+ # inline api_key, so it clears under the same flag.
+ model_cfg.pop("key_env", None)
+ model_cfg.pop("api_key_env", None)
if clear_api_mode:
model_cfg.pop("api_mode", None)
if clear_base_url:
diff --git a/tests/hermes_cli/test_update_config_clears_custom_fields.py b/tests/hermes_cli/test_update_config_clears_custom_fields.py
index 212848c81e3..d385ca58d99 100644
--- a/tests/hermes_cli/test_update_config_clears_custom_fields.py
+++ b/tests/hermes_cli/test_update_config_clears_custom_fields.py
@@ -66,6 +66,33 @@ class TestUpdateConfigForProviderClearsStaleCustomFields:
assert "api_mode" not in model_cfg
assert model_cfg["provider"] == "openrouter"
+ def test_clear_model_endpoint_credentials_removes_key_env_pointer(self):
+ # key_env is a live credential pointer (runtime_provider /
+ # auxiliary_client resolve it), written by custom-endpoint activation.
+ # Surviving a provider switch it routes the NEW provider's requests to
+ # the OLD endpoint's env var — it clears with the inline key.
+ model_cfg = {
+ "provider": "custom_myendpoint",
+ "default": "some-model",
+ "key_env": "CUSTOM_MYENDPOINT_API_KEY",
+ "api_key_env": "LEGACY_PTR",
+ }
+
+ clear_model_endpoint_credentials(model_cfg)
+
+ assert "key_env" not in model_cfg
+ assert "api_key_env" not in model_cfg
+
+ def test_clear_api_key_false_preserves_key_env(self):
+ # The clear_api_key=False flavor (same-provider re-pick) must keep the
+ # pointer exactly as it keeps the inline key.
+ model_cfg = {"provider": "custom_x", "key_env": "CUSTOM_X_API_KEY", "api_mode": "openai"}
+
+ clear_model_endpoint_credentials(model_cfg, clear_api_key=False)
+
+ assert model_cfg["key_env"] == "CUSTOM_X_API_KEY"
+ assert "api_mode" not in model_cfg
+
def test_switching_to_openrouter_clears_api_key_and_api_mode(self):
_seed_custom_provider_config()