Skip to content

The agent that grows with you

fix(gateway): a respelled providers: key replaces its stale frozen spelling

Teknium675702e50460
diff --git a/gateway/session_mutation_model.py b/gateway/session_mutation_model.py
index 2a95acc8020..bd4a1e9bdd0 100644
--- a/gateway/session_mutation_model.py
+++ b/gateway/session_mutation_model.py
@@ -161,8 +161,12 @@ def _provider_resolution_config(authority, config, bypass=False):
resolved = dict(config)
frozen_providers, current_providers = config.get('providers'), current.get('providers')
if isinstance(frozen_providers, dict) or isinstance(current_providers, dict):
- resolved['providers'] = {**(frozen_providers if isinstance(frozen_providers, dict) else {}),
- **(current_providers if isinstance(current_providers, dict) else {})}
+ current_providers = current_providers if isinstance(current_providers, dict) else {}
+ slugs = {_provider_key_slug(key) for key in current_providers}
+ # A respelled key is the same provider: the profile's spelling replaces the frozen one.
+ resolved['providers'] = {**{key: entry for key, entry in (
+ frozen_providers if isinstance(frozen_providers, dict) else {}).items()
+ if _provider_key_slug(key) not in slugs}, **current_providers}
frozen_custom, current_custom = config.get('custom_providers'), current.get('custom_providers')
if isinstance(frozen_custom, list) or isinstance(current_custom, list):
current_custom = list(current_custom) if isinstance(current_custom, list) else []
@@ -213,7 +217,10 @@ def _freeze_selected_provider(config, frozen, provider):
copied = deepcopy(entries[index])
retired, moved = {(section, index)}, {}
if section == 'providers':
- frozen[section] = {**(frozen.get(section) if isinstance(frozen.get(section), dict) else {}), index: copied}
+ previous = frozen.get(section) if isinstance(frozen.get(section), dict) else {}
+ stale = {key for key in previous if _provider_key_slug(key) == _provider_key_slug(index)}
+ frozen[section] = {**{k: v for k, v in previous.items() if k not in stale}, index: copied}
+ retired |= {(section, key) for key in stale}
else:
frozen_entries = list(frozen.get(section) or [])
name = _custom_provider_name(copied)
@@ -236,6 +243,12 @@ def _freeze_selected_provider(config, frozen, provider):
return secrets, retired, moved
+def _provider_key_slug(key):
+ """A ``providers:`` key's canonical identity: ``Review Prov`` and ``review-prov`` are one."""
+ from hermes_cli.providers import custom_provider_slug
+ return custom_provider_slug('', str(key))
+
+
def _alias_credential(authority, result):
"""``(key_env, source_path, literal_key)`` the direct alias a switch resolved declares for its own
endpoint, read from THIS owner's config.yaml: the file a restarted owner re-reads, never the
diff --git a/tests/gateway/test_session_mutation_model_provider_identity.py b/tests/gateway/test_session_mutation_model_provider_identity.py
index abf3ca72a99..ed4eec3fc40 100644
--- a/tests/gateway/test_session_mutation_model_provider_identity.py
+++ b/tests/gateway/test_session_mutation_model_provider_identity.py
@@ -1,5 +1,5 @@
"""A model pick freezes only the picked provider: an unrelated legacy entry it displaces keeps its
-own secret (real policy/resolver, warm and cold)."""
+own secret, and a respelled ``providers:`` key replaces its stale spelling (real policy/resolver)."""
from copy import deepcopy
from dataclasses import asdict
import json
@@ -56,3 +56,18 @@ async def test_displaced_unchanged_legacy_entry_keeps_its_key(tmp_path, monkeypa
assert _entry(warm_cfg, 'x-prov')['api_key'] == 'x-review-key'
assert _entry(cold_cfg, 'x-prov')['api_key'] == 'x-review-key'
+
+async def test_respelled_providers_key_replaces_the_stale_spelling(tmp_path, monkeypatch):
+ """Frozen ``providers: {'Review Prov': A}``, profile now ``{'review-prov': B}``: the pick runs B
+ and the stale spelling (and its secret) is gone from the frozen session."""
+ before = {'model': {'provider': 'review-prov', 'default': 'original'},
+ 'providers': {'Review Prov': {'base_url': 'http://127.0.0.1:9/a', 'api_key': 'a-review-key',
+ 'models': {'switched': {}}}}}
+ after = {**before, 'providers': {'review-prov': {'base_url': 'http://127.0.0.1:9/b',
+ 'api_key': 'b-review-key', 'models': {'switched': {}}}}}
+ warm_cfg, warm, cold_cfg, cold, frozen = await _pick(tmp_path, monkeypatch, before, after, 'review-prov')
+ assert (warm['base_url'], warm['api_key']) == ('http://127.0.0.1:9/b', 'b-review-key')
+ assert (cold['base_url'], cold['api_key']) == ('http://127.0.0.1:9/b', 'b-review-key')
+ assert list(frozen['providers']) == ['review-prov']
+ assert list(warm_cfg['providers']) == list(cold_cfg['providers']) == ['review-prov']