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

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.
This commit is contained in:
Teknium
2026-08-19 21:47:57 -07:00
parent 88f2844d46
commit 68ebb2c996
2 changed files with 34 additions and 0 deletions
+7
View File
@@ -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:
@@ -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()