The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep a shadowed custom pick on the profile entry's route
686f6c6107048d99fcc6
diff --git a/gateway/session_policy.py b/gateway/session_policy.pyindex 2c6b67984d9..9153d29713a 100644--- a/gateway/session_policy.py+++ b/gateway/session_policy.py@@ -420,14 +420,32 @@ def _keeps_profile_route(picked, config):`deepinfra`, `custom:<name>` for a named entry) is the same route. A block with an endpointand no provider (or `auto`) already routes to `custom` there."""- from gateway.session_route_identity import resolved_route- held = dict(config.get('model') or {}) if isinstance(config.get('model'), dict) else {}- if str(held.get('provider') or '').strip().lower() in ('', 'auto') and str(held.get('base_url') or '').strip():- held['provider'] = 'custom'- held_id = resolved_route(held, config)[0]+ from gateway.session_route_identity import configured_entry_aliases, profile_route_identity, resolved_route+ held_id = profile_route_identity(config)if held_id == 'auto' or not isinstance(picked, str) or not picked.strip():return False- return resolved_route({'provider': picked}, config)[0] == held_id+ if resolved_route({'provider': picked}, config)[0] == held_id:+ return True+ # A configured entry named like a canonical built-in (`providers: deepseek`) is shadowed by+ # the built-in in raw provider resolution, so the resolver cannot see the pick as the entry+ # (#140107). The entry's own alias set can: a pick spelled with the held entry's key is that+ # entry's route, and freezing it verbatim would pin the session to the built-in instead.+ return picked.strip().lower() in configured_entry_aliases(held_id, config)+++def _frozen_provider_pick(picked, config):+ """The spelling a provider pick is frozen under when it names the profile entry's own endpoint.++ A pick that is one of the held entry's aliases keeps that entry's ``custom:<key>`` identity:+ the raw resolver defers the bare spelling to the canonical built-in, so freezing it as-is+ would pin the session to a different provider than the entry the profile runs (#140107). Any+ other pick is frozen as it came, which is the spelling the caller asked for.+ """+ from gateway.session_route_identity import configured_entry_aliases, profile_route_identity+ identity = profile_route_identity(config)+ if identity == 'auto' or picked.strip().lower() not in configured_entry_aliases(identity, config):+ return picked+ return identitydef _apply_launch_overrides(params, config):@@ -436,7 +454,8 @@ def _apply_launch_overrides(params, config):from hermes_cli.config import resolve_turn_limitfrom hermes_constants import parse_reasoning_effortparams = _without_auto_pick(params)- if 'provider' in params and not _keeps_profile_route(params['provider'], config):+ picks_profile_entry = 'provider' in params and _keeps_profile_route(params['provider'], config)+ if 'provider' in params and not picks_profile_entry:model = config.setdefault('model', {})# A provider pick is a route change: the profile's endpoint, wire protocol and# credential pointers belong to the provider being replaced, and pairing the new one@@ -459,6 +478,10 @@ def _apply_launch_overrides(params, config):if (url.scheme not in {'http', 'https'} or not url.hostnameor url.username or url.password or url.query or url.fragment):_refuse('base_url', 'must be an http(s) URL without credentials, query or fragment')+ elif picks_profile_entry:+ # The pick is the profile entry's own endpoint under another spelling: freeze the+ # entry's identity, never a spelling the runtime resolves elsewhere (#140107).+ value = _frozen_provider_pick(value, config)config.setdefault('model', {})[key] = valuebad_flag = next((flag for flag in ('ignore_rules', 'yolo', 'checkpoints', 'accept_hooks', 'pass_session_id','follow_profile_config', 'cwd_explicit', 'verbose')diff --git a/gateway/session_route_identity.py b/gateway/session_route_identity.pyindex 9839d7d7ffc..a2e0dbbc260 100644--- a/gateway/session_route_identity.py+++ b/gateway/session_route_identity.py@@ -34,6 +34,29 @@ def _canonical(name: str) -> str:return canonical+def configured_entry_aliases(identity: str, config: dict) -> frozenset[str]:+ """Every spelling the configured entry behind a route identity accepts, empty when it names none.++ Raw provider resolution defers a bare name that is a canonical built-in to the built-in, so a+ ``providers:`` entry named ``deepseek`` is reachable only through its ``custom:`` spelling —+ the entry's own alias set is what tells its keys apart from the built-in's head-on name.+ """+ entry = _named_entry(str(identity or '').strip().lower(), config)+ if not entry:+ return frozenset()+ from hermes_cli.providers import custom_provider_aliases+ return custom_provider_aliases(str(entry.get('name') or ''), str(entry.get('provider_key') or ''))+++def profile_route_identity(config: dict) -> str:+ """The identity the profile's own ``model`` block resolves to — ``custom`` for an endpoint with+ no provider named (or ``auto``), which already routes to ``custom`` at that URL."""+ held = dict(config.get('model') or {}) if isinstance(config.get('model'), dict) else {}+ if str(held.get('provider') or '').strip().lower() in ('', 'auto') and str(held.get('base_url') or '').strip():+ held['provider'] = 'custom'+ return resolved_route(held, config)[0]++def resolved_route(model_cfg: dict, config: dict) -> tuple[str, str]:"""``(provider_identity, effective_base_url)`` the runtime resolves ``model_cfg`` to."""model_cfg = model_cfg if isinstance(model_cfg, dict) else {}diff --git a/tests/gateway/test_session_policy.py b/tests/gateway/test_session_policy.pyindex 6a70b1580ce..93f2baba691 100644--- a/tests/gateway/test_session_policy.py+++ b/tests/gateway/test_session_policy.py@@ -259,6 +259,29 @@ def test_provider_alias_spelling_and_auto_pick_keep_the_profiles_route(tmp_path,('custom', 'http://localhost:1234/v1', 'sk-local')+def test_bare_pick_of_a_shadowed_custom_entry_keeps_the_profiles_route(tmp_path, monkeypatch):+ """#140107: an entry named like a canonical built-in (`providers: deepseek`, or the legacy+ list) is shadowed by the built-in in raw provider resolution, so a Desktop pick of its row+ arrives as the entry's bare key. Freezing that spelling pins the session to the built-in —+ a different provider than the row the user picked — and drops the entry's endpoint and key+ pointer with it. The entry's own alias set is what says the pick is the same route."""+ from gateway.session_policy import build_policy+ for var in ('DEEPSEEK_API_KEY', 'OPENAI_API_KEY'):+ monkeypatch.delenv(var, raising=False)+ new_style = {'providers': {'deepseek': {'name': 'deepseek', 'api': 'https://api.deepseek.com',+ 'key_env': 'DEEPSEEK_ENTRY_KEY', 'transport': 'chat_completions',+ 'default_model': 'deepseek-flash'}}}+ legacy = {'custom_providers': [{'name': 'deepseek', 'base_url': 'https://api.deepseek.com',+ 'key_env': 'DEEPSEEK_ENTRY_KEY', 'model': 'deepseek-flash'}]}+ for extra in (new_style, legacy):+ cfg = {**extra, 'model': {'default': 'deepseek-flash', 'provider': 'custom:deepseek',+ 'base_url': 'https://api.deepseek.com', 'key_env': 'DEEPSEEK_ENTRY_KEY'}}+ model = build_policy(dict(cwd=str(tmp_path), model='deepseek-flash', provider='deepseek'), cfg).config()['model']+ assert model['provider'] == 'custom:deepseek', extra+ assert model['base_url'] == 'https://api.deepseek.com', extra+ assert model['key_env'] == 'DEEPSEEK_ENTRY_KEY', extra++def test_unlimited_max_turns_spellings_create_an_uncapped_session(tmp_path):"""`--max-turns 0` / -1 / none / unlimited meant "no cap" before the gateway cutover: session.createfreezes them as the unlimited budget, and `hermes chat` sends 0 instead of dropping it as falsy."""