The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): freeze a picked legacy provider by canonical identity at its profile index
Tekniuma06fd699fe61
diff --git a/gateway/session_mutation_model.py b/gateway/session_mutation_model.pyindex cf3ec3eeac9..52d1b2b05a2 100644--- a/gateway/session_mutation_model.py+++ b/gateway/session_mutation_model.py@@ -165,14 +165,18 @@ def _provider_resolution_config(authority, config, bypass=False):if isinstance(frozen_custom, list) or isinstance(current_custom, list):current_custom = list(current_custom) if isinstance(current_custom, list) else []names = {_custom_provider_name(entry) for entry in current_custom if isinstance(entry, dict)}+ # Current entries keep their profile positions (the path cold recovery re-reads).resolved['custom_providers'] = current_custom + [entry for entry in (frozen_custom if isinstance(frozen_custom, list) else [])if not isinstance(entry, dict) or _custom_provider_name(entry) not in names]+ resolved['_profile_custom_count'] = len(current_custom)return resolveddef _custom_provider_name(entry):- return str(entry.get('provider_key') or entry.get('name') or '').strip().lower()+ """The runtime's canonical identity: ``My Provider`` and ``my-provider`` are one provider."""+ from hermes_cli.providers import custom_provider_slug+ return custom_provider_slug(str(entry.get('name') or ''), str(entry.get('provider_key') or ''))def _selected_provider_key_env(config, provider):@@ -189,8 +193,9 @@ def _freeze_selected_provider(config, frozen, provider):The next turn resolves from the session's own configuration, not a later profile edit. Privatefields move to the policy's secret store, as policy creation does; nothing else moves. Returns- ``(secrets, retired)``: the entry's private values and the ``(section, key)`` whose previous- private subtree they replace.+ ``(secrets, retired)``: the entry's private values and every ``(section, key)`` whose previous+ private subtree they replace. A legacy entry from the profile is frozen at its profile index (the+ path cold recovery re-reads); an equivalent earlier spelling is blanked, never left to win."""from copy import deepcopyfrom gateway.session_policy import _extract_config_secrets@@ -203,6 +208,7 @@ def _freeze_selected_provider(config, frozen, provider):if not isinstance(entries, (dict, list)) or not isinstance(entries[index], dict):return {}, ()copied = deepcopy(entries[index])+ retired = {(section, index)}if section == 'providers':frozen[section] = {**(frozen.get(section) if isinstance(frozen.get(section), dict) else {}), index: copied}else:@@ -210,16 +216,20 @@ def _freeze_selected_provider(config, frozen, provider):name = _custom_provider_name(copied)same = [i for i, entry in enumerate(frozen_entries)if isinstance(entry, dict) and _custom_provider_name(entry) == name]- if same:- index = same[0]- frozen_entries[index] = copied- else:- frozen_entries.append(copied)- index = len(frozen_entries) - 1+ if index >= config.get('_profile_custom_count', len(entries)):+ index = same[0] # a frozen-only entry stays where it is+ frozen_entries += [{}] * (index + 1 - len(frozen_entries))+ displaced = frozen_entries[index]+ if isinstance(displaced, dict) and displaced and _custom_provider_name(displaced) != name:+ frozen_entries.append(displaced) # still resolvable by name; its index-bound secrets retire+ for i in same:+ frozen_entries[i] = {}+ frozen_entries[index] = copied+ retired |= {(section, i) for i in same} | {(section, index)}frozen[section] = frozen_entriessecrets = {}_extract_config_secrets(frozen[section][index], secrets, (section, index))- return secrets, {(section, index)}+ return secrets, retireddef _alias_credential(authority, result):diff --git a/tests/gateway/test_session_mutation_model_provider_replacement.py b/tests/gateway/test_session_mutation_model_provider_replacement.pyindex 60d52fb36e8..a881e12071f 100644--- a/tests/gateway/test_session_mutation_model_provider_replacement.py+++ b/tests/gateway/test_session_mutation_model_provider_replacement.py@@ -89,6 +89,18 @@ async def test_named_provider_replacement_does_not_keep_withdrawn_secrets(tmp_paassert config['providers']['reviewprovider'].get('extra_headers') is None+async def test_legacy_provider_respelling_picks_new_endpoint(tmp_path, monkeypatch):+ with peers() as (a, au, b, bu):+ before = {'model': {'provider':'custom:my-provider', 'default':'original'},+ 'custom_providers':[{'name':'My Provider','base_url':au,'api_key':'same-review-key'}]}+ after = {'model':before['model'],+ 'custom_providers':[{'name':'my-provider','base_url':bu,'api_key':'same-review-key'}]}+ _, runtime = await pick(tmp_path, monkeypatch, before, after, 'custom:my-provider')+ assert b.bearers == ['Bearer same-review-key'] and not a.bearers+ assert runtime['base_url'].rstrip('/') == bu++@pytest.mark.asyncio@pytest.mark.parametrize('bypass', ['ignore_user_config', 'safe_mode'])async def test_config_only_pick_does_not_import_profile_provider(tmp_path, monkeypatch, bypass):@@ -104,6 +116,18 @@ async def test_config_only_pick_does_not_import_profile_provider(tmp_path, monkeassert not b.bearers, 'ignore_user_config session imported profile-only provider'+async def test_appended_legacy_provider_keeps_inline_key_after_cold_restore(tmp_path, monkeypatch):+ with peers() as (a, au, b, bu):+ before = {'model': {'provider':'custom:old-review', 'default':'original'},+ 'custom_providers':[{'name':'old-review','base_url':au,'api_key':'old-review-key'}]}+ after = {'model':before['model'],+ 'custom_providers':[{'name':'new-review','base_url':bu,'api_key':'new-review-key'}]}+ _, runtime = await pick(tmp_path, monkeypatch, before, after, 'custom:new-review')+ assert b.bearers == ['Bearer new-review-key'] and not a.bearers+ assert runtime['base_url'].rstrip('/') == bu++@pytest.mark.platforms('linux')def test_removed_provider_key_is_not_sent_to_retargeted_endpoint(tmp_path):root = Path(__file__).resolve().parents[2]