From 68ebb2c99675d4b821649f7ddffbc20bf42d7059 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 19 Aug 2026 21:47:57 -0700 Subject: [PATCH] fix(config): provider switch clears the stale model.key_env pointer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Custom-endpoint activation writes model.key_env, but clear_model_endpoint_credentials never popped it and the switch-clears trigger only fired on inline keys — a pointer-only model block survived every provider switch, routing the new provider's requests to the old endpoint's env var. key_env/api_key_env now clear under clear_api_key; the trigger fires on pointer-only blocks too. All key_env writers set it after the clear, so custom-to-custom switches keep the new pointer. --- hermes_cli/config.py | 7 +++++ ...test_update_config_clears_custom_fields.py | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/hermes_cli/config.py b/hermes_cli/config.py index ad60058c4c..d5d26ac68b 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 212848c81e..d385ca58d9 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()