Skip to content

The agent that grows with you

fix(gateway): drop the previous provider's route on a provider-only launch pick

liuhao1024dcad577784e9
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index f0f7df3fa66..1df4c686a06 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -354,6 +354,7 @@ def _apply_launch_overrides(params, config):
from urllib.parse import urlsplit
from hermes_cli.config import resolve_turn_limit
from hermes_constants import parse_reasoning_effort
+ held = str(config.get('model', {}).get('provider') or '').strip().lower()
for key in ('provider', 'base_url'):
if key in params:
value = params[key]
@@ -365,6 +366,16 @@ def _apply_launch_overrides(params, config):
or 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')
config.setdefault('model', {})[key] = value
+ if 'provider' in params and 'base_url' not in params and held != str(params['provider']).strip().lower():
+ model = config.setdefault('model', {})
+ # A provider-only 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
+ # with them sends its key to the old host (401) or the old key down the new route.
+ # Dropping them lets the target provider resolve its own — the same route keys a
+ # committed /model switch rewrites (#138843). A caller that passes base_url keeps the
+ # route it built, and a same-provider re-pick keeps the profile's pins.
+ for stale in ('base_url', 'api_mode', 'key_env', 'api_key_env', 'api_key', 'api', 'api_key_from'):
+ model.pop(stale, None)
bad_flag = next((flag for flag in ('ignore_rules', 'yolo', 'checkpoints', 'accept_hooks', 'pass_session_id',
'follow_profile_config', 'cwd_explicit', 'verbose')
if flag in params and type(params[flag]) is not bool), None)
diff --git a/tests/gateway/test_session_policy.py b/tests/gateway/test_session_policy.py
index 9aafdb7330b..bd224a45c6a 100644
--- a/tests/gateway/test_session_policy.py
+++ b/tests/gateway/test_session_policy.py
@@ -143,6 +143,29 @@ def test_launch_options_are_frozen_and_validated(tmp_path):
build_policy(dict(cwd=str(tmp_path), **bad), cfg)
+def test_provider_override_drops_the_previous_providers_route(tmp_path):
+ """A provider-only pick is a whole-route change: folding it must not keep the profile's
+ endpoint/wire-protocol/credential pointers, else the new provider's key is sent to the old
+ host (#138843). An explicit base_url and a same-provider re-pick keep what they carried."""
+ from gateway.session_policy import build_policy
+ cfg = {'model': {'default': 'deepseek-chat', 'provider': 'deepseek',
+ 'base_url': 'https://api.deepseek.com/v1', 'api_mode': 'chat_completions',
+ 'key_env': 'HERMES_CUSTOM_QWENAI_API_KEY'}}
+ policy = build_policy(dict(cwd=str(tmp_path), model='mimo-v2.6-flash', provider='xiaomi'), cfg)
+ assert policy.config()['model'] == {'default': 'deepseek-chat', 'provider': 'xiaomi'}
+ explicit = build_policy(dict(cwd=str(tmp_path), model='m', provider='xiaomi',
+ base_url='https://proxy.example/v1'), cfg)
+ assert explicit.config()['model']['base_url'] == 'https://proxy.example/v1'
+ assert explicit.config()['model']['key_env'] == 'HERMES_CUSTOM_QWENAI_API_KEY'
+ same = build_policy(dict(cwd=str(tmp_path), model='deepseek-r2', provider='deepseek'), cfg)
+ assert same.config()['model']['base_url'] == 'https://api.deepseek.com/v1'
+ # A profile without a recorded provider still leaves its endpoint behind for the pick to replace.
+ unrecorded = build_policy(dict(cwd=str(tmp_path), provider='xiaomi'),
+ {'model': {'default': 'm', 'base_url': 'https://api.deepseek.com/v1',
+ 'api_key': 'sk-inline'}})
+ assert unrecorded.config()['model'] == {'default': 'm', 'provider': 'xiaomi'}
+
+
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.create
freezes them as the unlimited budget, and `hermes chat` sends 0 instead of dropping it as falsy."""