Skip to content

The agent that grows with you

fix(gateway): clear the replaced provider's route before an explicit base_url folds in

liuhao1024f079cd77efde
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index 1df4c686a06..d846f81a3af 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -355,6 +355,17 @@ def _apply_launch_overrides(params, config):
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()
+ if 'provider' in params and held != str(params['provider']).strip().lower():
+ 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
+ # with them sends its key to the old host (401) or the old key down the new route.
+ # They are dropped before the overrides fold in, so the target provider resolves its
+ # own — the same route keys a committed /model switch rewrites (#138843). An explicit
+ # base_url is applied after the drop, so a pinned endpoint survives without carrying
+ # the replaced provider's other pins, and a same-provider re-pick keeps the profile's.
+ for stale in ('base_url', 'api_mode', 'key_env', 'api_key_env', 'api_key', 'api', 'api_key_from'):
+ model.pop(stale, None)
for key in ('provider', 'base_url'):
if key in params:
value = params[key]
@@ -366,16 +377,6 @@ 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 bd224a45c6a..ade6f2569dc 100644
--- a/tests/gateway/test_session_policy.py
+++ b/tests/gateway/test_session_policy.py
@@ -144,19 +144,22 @@ def test_launch_options_are_frozen_and_validated(tmp_path):
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
+ """A provider 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."""
+ host (#138843). An explicit base_url is applied after the drop, and a same-provider
+ re-pick keeps what it 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'}
+ # A provider change plus an explicit URL still drops the replaced provider's wire protocol
+ # and credential pointer — only the pinned endpoint comes back.
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'
+ assert explicit.config()['model'] == {'default': 'deepseek-chat', 'provider': 'xiaomi',
+ 'base_url': 'https://proxy.example/v1'}
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.