Skip to content

The agent that grows with you

fix(gateway): keep the profile route only with an endpoint; prefer the entry runtime resolves

unsupportedpastelsd7f80b52d424
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index ea5a7981a89..8fdc4a087f7 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -541,10 +541,11 @@ def _with_entry_pick(params, config):
different provider than the entry the profile runs (#140107). Only the held entry's bare key
is that entry (Desktop's guard). A keyed entry's display name is not; a keyless legacy
``custom_providers:`` entry's is (``configured_entry_key``). A bare ``custom`` pick is the billing
- class a client echoed from the runtime, not a route: on a bare ``custom`` profile with no other
- endpoint picked it is that profile's route; otherwise it names the configured entry serving its
- endpoint/model (the profile's own first), else (no endpoint picked) the profile's own entry.
- Other picks stay as they came.
+ class a client echoed from the runtime, not a route: on a bare ``custom`` profile with an endpoint
+ it is that profile's route unless it picks another endpoint, or another entry lists a model that
+ is not the profile's default; otherwise it names the configured entry serving its endpoint/model
+ (the profile's own first), else (no endpoint picked) the profile's own entry. A launch key for an
+ entry the model alone chose is refused. Other picks stay as they came.
"""
from gateway.session_route_identity import configured_entry_key, profile_route_identity
picked = params.get('provider')
@@ -552,13 +553,16 @@ def _with_entry_pick(params, config):
return params
identity = profile_route_identity(config)
if picked.strip().lower() == 'custom':
- from hermes_cli.route_identity import normalize_route_base_url
- from hermes_cli.runtime_provider_custom import routed_custom_identity
- url = params.get('base_url')
- own_url = (config.get('model') or {}).get('base_url') if isinstance(config.get('model'), dict) else None
- if identity == 'custom' and (url is None or normalize_route_base_url(str(url)) == normalize_route_base_url(str(own_url or ''))):
+ from hermes_cli.runtime_provider_custom import _route_key, routed_custom_identity
+ url, model = params.get('base_url'), str(params.get('model') or '')
+ own_url = _route_key(model_route(config.get('model'))[1])
+ if identity == 'custom' and own_url and (
+ _route_key(url) == own_url if url is not None
+ else model.strip().lower() == profile_target(config)[0].lower()
+ or not routed_custom_identity(None, model, config)):
return params
- healed = routed_custom_identity(url, params.get('model'), config, held=identity) or (
+ held = identity if identity.startswith('custom:') else None
+ healed = routed_custom_identity(url, model, config, held=held) or (
identity if identity.startswith('custom:') and url is None else None)
if healed and healed != identity and url is None and 'api_key' in params:
# The model alone chose this entry: a launch key the user gave is never sent to a host they did not name.
diff --git a/hermes_cli/runtime_provider_custom.py b/hermes_cli/runtime_provider_custom.py
index 24cf467e4ad..49739d8985c 100644
--- a/hermes_cli/runtime_provider_custom.py
+++ b/hermes_cli/runtime_provider_custom.py
@@ -301,20 +301,25 @@ def codex_model_provider_id(requested_provider: str) -> Optional[str]:
def _find_custom_identity(matches: Callable[[dict[str, Any]], bool],
config: Optional[dict[str, Any]] = None, prefer: Optional[str] = None) -> Optional[str]:
"""First enabled entry in ``providers:`` then legacy ``custom_providers:`` where ``matches(entry)``
- holds (*prefer* when it is one of them), as its canonical ``custom:<name>`` slug. *config*
- defaults to the active profile's."""
+ holds (*prefer* when it is one of them), as its canonical ``custom:<name>`` slug. A legacy entry
+ whose slug a ``providers:`` entry with an endpoint owns never matches: runtime resolution runs that one.
+ *config* defaults to the active profile's."""
from hermes_cli.config import is_provider_enabled
rp = _rp()
try:
config = rp.load_config() if config is None else config
except Exception:
return None
- first = None
+ first, owned = None, set()
providers = config.get("providers")
if isinstance(providers, dict):
for ep_name, entry in providers.items():
- if isinstance(entry, dict) and is_provider_enabled(entry) and matches(entry):
- slug = custom_provider_slug(str(ep_name), str(ep_name))
+ if not (isinstance(entry, dict) and is_provider_enabled(entry)):
+ continue
+ slug = custom_provider_slug(str(ep_name), str(ep_name))
+ if _entry_url(entry):
+ owned.add(slug)
+ if matches(entry):
if prefer is None or slug == prefer:
return slug
first = first or slug
@@ -326,6 +331,8 @@ def _find_custom_identity(matches: Callable[[dict[str, Any]], bool],
name = entry.get("name") if isinstance(entry, dict) else None
if isinstance(name, str) and name.strip() and matches(entry):
slug = custom_provider_slug(name, str(entry.get("provider_key", "") or ""))
+ if slug in owned and not entry.get("provider_key"):
+ continue
if prefer is None or slug == prefer:
return slug
first = first or slug
diff --git a/tests/gateway/test_bare_custom_launch_keeps_profile_route.py b/tests/gateway/test_bare_custom_launch_keeps_profile_route.py
index 7ac3dfda9c2..8b895dada4e 100644
--- a/tests/gateway/test_bare_custom_launch_keeps_profile_route.py
+++ b/tests/gateway/test_bare_custom_launch_keeps_profile_route.py
@@ -26,6 +26,7 @@ BARE_LOCAL = {'model': {'provider': 'custom', 'base_url': LOCAL, 'default': 'qwe
def keys(monkeypatch):
monkeypatch.setenv('LOCAL_KEY', 'local-key')
monkeypatch.setenv('RELAY_KEY', 'relay-key')
+ monkeypatch.delenv('CUSTOM_BASE_URL', raising=False)
@pytest.mark.parametrize('pick, key', [
@@ -46,6 +47,52 @@ def test_an_entry_on_the_profiles_own_url_does_not_take_over_its_pins(tmp_path,
assert (runtime['api_key'], model_cfg.get('context_length')) == ('local-key', 32768)
+def test_a_model_only_another_entry_lists_leaves_the_profiles_endpoint(tmp_path):
+ config = {**BARE_LOCAL, 'providers': {'relay': {'api': RELAY, 'key_env': 'RELAY_KEY', 'models': ['relay-only']}}}
+ _cfg, runtime = _launch(tmp_path, config, model='relay-only')
+ assert (runtime['base_url'].rstrip('/'), runtime['api_key']) == (RELAY, 'relay-key')
+
+
+ENDPOINTLESS = {'model': {'provider': 'custom', 'default': 'qwen3-32b'},
+ 'providers': {'box': {'api': LOCAL, 'key_env': 'LOCAL_KEY', 'models': ['qwen3-32b']}}}
+
+
+def test_an_endpointless_bare_custom_profile_still_finds_the_entry_serving_the_model(tmp_path):
+ _cfg, runtime = _launch(tmp_path, ENDPOINTLESS, model='qwen3-32b')
+ assert (runtime['base_url'].rstrip('/'), runtime['api_key']) == (LOCAL, 'local-key')
+
+
+def test_a_launch_key_on_an_endpointless_bare_custom_profile_gets_the_model_only_refusal(tmp_path):
+ from hermes_state_runtime import RuntimeStoreError
+ with pytest.raises(RuntimeStoreError) as refused:
+ _launch(tmp_path, ENDPOINTLESS, 'sk-launch', model='qwen3-32b')
+ assert (refused.value.reason, refused.value.field) == ('invalid_params', 'api_key')
+
+
+SHADOWED = {'model': {'provider': 'custom:local', 'default': 'local-only', 'base_url': LOCAL, 'key_env': 'LOCAL_KEY',
+ 'context_length': 32768},
+ 'providers': {'relay': {'api': RELAY, 'default_model': 'relay-only', 'key_env': 'RELAY_KEY'},
+ 'local': {'api': LOCAL, 'default_model': 'local-only', 'key_env': 'LOCAL_KEY'}},
+ # Same name as providers.local, which runtime resolution gives the slug to.
+ 'custom_providers': [{'name': 'local', 'base_url': RELAY, 'model': 'relay-only', 'key_env': 'RELAY_KEY'}]}
+
+
[email protected]('pick', [{'base_url': RELAY}, {}], ids=['relay-url', 'no-url'])
+def test_a_shadowed_legacy_entry_does_not_win_the_profile_preference(tmp_path, pick):
+ model_cfg, runtime = _launch(tmp_path, SHADOWED, model='relay-only', **pick)
+ assert (runtime['base_url'].rstrip('/'), runtime['api_key'], model_cfg.get('context_length')) == (
+ RELAY, 'relay-key', None)
+
+
+def test_an_endpointless_providers_block_does_not_hide_the_legacy_entry_of_that_name(tmp_path):
+ config = {'model': {'provider': 'openrouter', 'default': 'anthropic/claude-sonnet-4'},
+ # Tuning only: runtime resolution skips it and runs the legacy entry.
+ 'providers': {'bedrock': {'stale_timeout_seconds': 600}},
+ 'custom_providers': [{'name': 'bedrock', 'base_url': RELAY, 'model': 'relay-only', 'key_env': 'RELAY_KEY'}]}
+ _cfg, runtime = _launch(tmp_path, config, model='relay-only')
+ assert (runtime['base_url'].rstrip('/'), runtime['api_key']) == (RELAY, 'relay-key')
+
+
def test_a_bare_custom_pick_naming_another_endpoint_uses_that_entrys_own_key(tmp_path):
_cfg, runtime = _launch(tmp_path, BARE_LOCAL, model='qwen3-32b', base_url=RELAY)
assert (runtime['base_url'].rstrip('/'), runtime['api_key']) == (RELAY, 'relay-key')
@@ -118,3 +165,31 @@ async def test_a_session_create_with_a_bare_custom_pick_stays_on_the_profiles_en
finally:
await owner.close()
store.close_all_db_handles()
+
+
+async def test_adopting_a_legacy_custom_row_recovers_the_entry_serving_its_model(monkeypatch):
+ """Real ``adopt_legacy_session``: the row recorded bare ``custom``, no endpoint, and a model only another entry lists."""
+ import json
+ from gateway import run
+ from gateway.session_local_migration import resolve_local_target
+ from hermes_constants import get_hermes_home
+ from tests.gateway.test_session_follow_profile_config import _owner, _route
+ home = get_hermes_home()
+ home.mkdir(parents=True, exist_ok=True)
+ monkeypatch.setattr(run, '_hermes_home', home)
+ (home / 'config.yaml').write_text(json.dumps({
+ 'model': {'provider': 'custom', 'base_url': LOCAL, 'default': 'qwen3-32b', 'key_env': 'LOCAL_KEY'},
+ 'providers': {'relay': {'api': RELAY, 'key_env': 'RELAY_KEY', 'models': ['saved-model']}}}))
+ store, runner, authority, owner = _owner(home)
+ runner._peek_session_state = lambda key: None
+ try:
+ authority.db.create_session('old-tui', cwd=str(home), source='tui', model='saved-model',
+ model_config={'provider': 'custom'})
+ authority.db.end_session('old-tui', 'tui_close')
+ authority._native_legacy_transports = {owner.actor.transport_id: owner.actor}
+ live = authority.sessions[resolve_local_target(authority, owner.actor, 'old-tui').session_id]
+ assert _route(runner, live) == ('saved-model', RELAY, 'relay-key')
+ finally:
+ await owner.close()
+ store.close_all_db_handles()