Skip to content

The agent that grows with you

fix(gateway): a custom pick over a provider-less endpoint keeps its route

Teknium4dd4c87fdd83
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index 1296a44948e..ed94a7aa711 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -354,10 +354,17 @@ 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
- from hermes_cli.models import normalize_provider
- held = str(config.get('model', {}).get('provider') or '').strip()
- # Aliases (`vllm` for `custom`) name the same provider, so they keep its route.
- if 'provider' in params and not (held and normalize_provider(held) == normalize_provider(str(params['provider']))):
+ from gateway.session_route_identity import resolved_route
+ held_cfg = dict(config.get('model') or {}) if isinstance(config.get('model'), dict) else {}
+ if str(held_cfg.get('provider') or '').strip().lower() in ('', 'auto') and str(held_cfg.get('base_url') or '').strip():
+ # A block with an endpoint and no provider (or `auto`) already routes to `custom` there.
+ held_cfg['provider'] = 'custom'
+ held_id = resolved_route(held_cfg, config)[0]
+ # The same resolved provider under another spelling (`vllm` for `custom`, `deep-infra` for
+ # `deepinfra`, `custom:<name>` for a named entry) is the same route, so it keeps its pins.
+ same = 'provider' in params and held_id != 'auto' and isinstance(params['provider'], str) \
+ and params['provider'].strip() and resolved_route({'provider': params['provider']}, config)[0] == held_id
+ if 'provider' in params and not same:
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
diff --git a/tests/gateway/test_session_policy.py b/tests/gateway/test_session_policy.py
index dd82d946e5b..377f3b10dae 100644
--- a/tests/gateway/test_session_policy.py
+++ b/tests/gateway/test_session_policy.py
@@ -203,6 +203,26 @@ def test_provider_alias_pick_keeps_the_profiles_endpoint(tmp_path, monkeypatch):
assert (runtime['base_url'], runtime['api_key']) == ('http://localhost:1234/v1', 'sk-local')
+
+def test_custom_pick_over_unrecorded_or_auto_profile_keeps_its_endpoint(tmp_path, monkeypatch):
+ """A profile block with a base_url and no provider (or `provider: auto`) already routes to
+ `custom` at that URL, so a `custom` pick is the same route: it must keep the endpoint and
+ key pointer instead of dropping them and failing with no endpoint configured."""
+ from gateway.session_policy import build_policy
+ from hermes_cli.runtime_provider import frozen_runtime_config, resolve_runtime_provider
+ for var in ('CUSTOM_BASE_URL', 'OPENAI_BASE_URL'):
+ monkeypatch.delenv(var, raising=False)
+ monkeypatch.setenv('LOCAL_KEY', 'sk-local')
+ for held in ({}, {'provider': 'auto'}):
+ cfg = {'model': {'default': 'qwen', 'base_url': 'http://localhost:1234/v1', 'key_env': 'LOCAL_KEY', **held}}
+ policy = build_policy(dict(cwd=str(tmp_path), model='qwen', provider='custom'), cfg)
+ assert policy.config()['model']['key_env'] == 'LOCAL_KEY', held
+ with frozen_runtime_config(policy.config()):
+ runtime = resolve_runtime_provider(requested='custom', target_model='qwen')
+ assert (runtime['provider'], runtime['base_url'], runtime['api_key']) == \
+ ('custom', 'http://localhost:1234/v1', 'sk-local'), held
+
+
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."""