The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep a bare custom launch pick on the profile's route
unsupportedpastels4f6666f133e6
diff --git a/gateway/session_policy.py b/gateway/session_policy.pyindex 20287402a2c..ea5a7981a89 100644--- a/gateway/session_policy.py+++ b/gateway/session_policy.py@@ -541,8 +541,10 @@ def _with_entry_pick(params, config):different provider than the entry the profile runs (#140107). Only the held entry's bare keyis 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: it names the configured entry serving its- endpoint/model, 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 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."""from gateway.session_route_identity import configured_entry_key, profile_route_identitypicked = params.get('provider')@@ -550,9 +552,17 @@ def _with_entry_pick(params, config):return paramsidentity = profile_route_identity(config)if picked.strip().lower() == 'custom':+ from hermes_cli.route_identity import normalize_route_base_urlfrom hermes_cli.runtime_provider_custom import routed_custom_identity- healed = routed_custom_identity(params.get('base_url'), params.get('model'), config) or (- identity if identity.startswith('custom:') and 'base_url' not in params else None)+ 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 ''))):+ return params+ healed = routed_custom_identity(url, params.get('model'), config, held=identity) 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.+ _refuse('api_key', f'{healed} was chosen by model; name it with --provider or --base-url to use a launch key')return {**params, 'provider': healed} if healed else paramsif identity == 'auto' or picked.strip().lower() != (configured_entry_key(identity, config) or None):return paramsdiff --git a/hermes_cli/runtime_provider_custom.py b/hermes_cli/runtime_provider_custom.pyindex ccd882b8b01..cf9392d1f64 100644--- a/hermes_cli/runtime_provider_custom.py+++ b/hermes_cli/runtime_provider_custom.py@@ -299,20 +299,22 @@ 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) -> Optional[str]:+ 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, 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. *config*+ defaults to the active profile's."""from hermes_cli.config import is_provider_enabledrp = _rp()try:config = rp.load_config() if config is None else configexcept Exception:return None+ found = []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):- return custom_provider_slug(str(ep_name), str(ep_name))+ found.append(custom_provider_slug(str(ep_name), str(ep_name)))try:custom_providers = rp.get_compatible_custom_providers(config)except Exception:@@ -320,8 +322,8 @@ def _find_custom_identity(matches: Callable[[dict[str, Any]], bool],for entry in custom_providers or []:name = entry.get("name") if isinstance(entry, dict) else Noneif isinstance(name, str) and name.strip() and matches(entry):- return custom_provider_slug(name, str(entry.get("provider_key", "") or ""))- return None+ found.append(custom_provider_slug(name, str(entry.get("provider_key", "") or "")))+ return prefer if prefer in found else next(iter(found), None)def find_custom_provider_identity(base_url: str) -> Optional[str]:@@ -359,16 +361,17 @@ def _entry_serves_model(entry: dict[str, Any], target: str) -> bool:def routed_custom_identity(base_url: Optional[str], model: Optional[str],- config: Optional[dict[str, Any]] = None) -> Optional[str]:+ config: Optional[dict[str, Any]] = None, held: Optional[str] = None) -> Optional[str]:"""The configured ``custom:<name>`` entry a bare ``custom`` pick (the billing class every namedendpoint reports) names: on *base_url*, the entry also serving *model* beats the first one there;- without a URL the model alone names it."""+ without a URL the model alone names it. Among entries serving *model*, the *held* one (the+ profile's own) wins over config order."""url = _route_key(base_url)target = str(model or "").strip().lower()def on_url(entry: dict[str, Any]) -> bool:return not url or _route_key(_entry_url(entry)) == url- return ((target and _find_custom_identity(lambda e: on_url(e) and _entry_serves_model(e, target), config))+ return ((target and _find_custom_identity(lambda e: on_url(e) and _entry_serves_model(e, target), config, held))or (url and _find_custom_identity(on_url, config)) or None)diff --git a/tests/gateway/test_bare_custom_launch_keeps_profile_route.py b/tests/gateway/test_bare_custom_launch_keeps_profile_route.pynew file mode 100644index 00000000000..7ac3dfda9c2--- /dev/null+++ b/tests/gateway/test_bare_custom_launch_keeps_profile_route.py@@ -0,0 +1,120 @@+"""A bare ``custom`` launch pick stays on the profile's own route whenever that route can serve it."""+import pytest++LOCAL, RELAY = 'http://127.0.0.1:8000/v1', 'https://relay.example.com/v1'+++def _launch(tmp_path, config, launch_key=None, **pick):+ """session.create's route steps, then the runtime the first turn resolves from the frozen policy."""+ from gateway.session_local_route import resolve_launch_route+ from gateway.session_policy import build_policy+ from gateway.session_worker_construct import route_runtime+ params = {'cwd': str(tmp_path), 'toolsets': [], 'provider': 'custom', **pick}+ params = resolve_launch_route({**params, **({'api_key': launch_key} if launch_key else {})}, config)+ policy = build_policy(params, config, profile_terminal=False)+ frozen, model = policy.effective()+ runtime, _ = route_runtime(policy, frozen, model, launch_key, fallback=False)+ return frozen['model'], runtime+++BARE_LOCAL = {'model': {'provider': 'custom', 'base_url': LOCAL, 'default': 'qwen3-32b', 'key_env': 'LOCAL_KEY',+ 'context_length': 32768},+ 'providers': {'relay': {'api': RELAY, 'key_env': 'RELAY_KEY', 'models': ['qwen3-32b']}}}++[email protected](autouse=True)+def keys(monkeypatch):+ monkeypatch.setenv('LOCAL_KEY', 'local-key')+ monkeypatch.setenv('RELAY_KEY', 'relay-key')++[email protected]('pick, key', [+ ({'model': 'qwen3-32b'}, 'local-key'), # --provider custom -m / a Desktop pick+ ({'model': 'qwen3-32b', 'base_url': LOCAL}, 'local-key'), # ... with the profile's own --base-url+ ({'model': 'qwen3-32b', 'launch_key': 'sk-launch'}, 'sk-launch'), # ... with --api-key+], ids=['no-url', 'own-url', 'launch-key'])+def test_a_bare_custom_pick_on_a_bare_custom_profile_stays_on_its_endpoint(tmp_path, pick, key):+ model_cfg, runtime = _launch(tmp_path, BARE_LOCAL, **pick)+ assert (runtime['base_url'].rstrip('/'), runtime['api_key'], model_cfg.get('context_length')) == (+ LOCAL, key, 32768)++[email protected]('pick', [{}, {'base_url': LOCAL}], ids=['no-url', 'own-url'])+def test_an_entry_on_the_profiles_own_url_does_not_take_over_its_pins(tmp_path, pick):+ config = {**BARE_LOCAL, 'providers': {'box': {'api': LOCAL, 'key_env': 'RELAY_KEY', 'default_model': 'qwen3-32b'}}}+ model_cfg, runtime = _launch(tmp_path, config, model='qwen3-32b', **pick)+ assert (runtime['api_key'], model_cfg.get('context_length')) == ('local-key', 32768)+++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')+++def test_a_named_profile_keeps_its_entry_when_it_serves_the_model(tmp_path):+ config = {'model': {'provider': 'custom:local', 'default': 'qwen3-32b'},+ 'providers': {'relay': {'api': RELAY, 'key_env': 'RELAY_KEY', 'models': ['qwen3-32b']},+ 'local': {'api': LOCAL, 'key_env': 'LOCAL_KEY', 'default_model': 'qwen3-32b'}}}+ _cfg, runtime = _launch(tmp_path, config, model='qwen3-32b')+ assert (runtime['base_url'].rstrip('/'), runtime['api_key']) == (LOCAL, 'local-key')+++def test_a_launch_key_is_refused_for_an_entry_only_the_model_chose(tmp_path):+ """Only another entry lists the model: the gateway would guess its host, so the user's key is not sent there."""+ from hermes_state_runtime import RuntimeStoreError+ config = {'model': {'provider': 'custom:local', 'default': 'qwen3-32b'},+ 'providers': {'relay': {'api': RELAY, 'key_env': 'RELAY_KEY', 'models': ['relay-only']},+ 'local': {'api': LOCAL, 'key_env': 'LOCAL_KEY', 'default_model': 'qwen3-32b'}}}+ with pytest.raises(RuntimeStoreError) as refused:+ _launch(tmp_path, config, model='relay-only', launch_key='sk-launch')+ assert (refused.value.reason, refused.value.field) == ('invalid_params', 'api_key')+++def test_a_kanban_custom_provider_override_stays_on_the_profiles_endpoint(tmp_path, monkeypatch):+ from contextlib import closing+ from types import SimpleNamespace+ from gateway.session_contract import Principal+ from gateway.session_kanban import build_kanban_policy+ from hermes_cli import kanban_db as kb+ from hermes_cli.kanban_db_connect import connect+ monkeypatch.setenv('HERMES_KANBAN_HOME', str(tmp_path))+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))+ monkeypatch.delenv('HERMES_KANBAN_DB', raising=False)+ monkeypatch.setattr('hermes_cli.profiles.resolve_profile_env', lambda name: str(tmp_path))+ kb.create_board('owned')+ with closing(connect(board='owned')) as conn:+ task_id = kb.create_task(conn, title='t', assignee='default', workspace_kind='dir',+ workspace_path=str(tmp_path), model_override='qwen3-32b', provider_override='custom')+ kb.recompute_ready(conn)+ task = kb.claim_task(conn, task_id)+ connection = SimpleNamespace(authority=SimpleNamespace(profile_id=str(tmp_path)), native_owner=True,+ actor=Principal('owner', str(tmp_path), frozenset({'session:create'}), 'transport'))+ policy, _secrets = build_kanban_policy(connection, dict(board='owned', task_id=task.id, run_id=task.current_run_id,+ claim_lock=task.claim_lock), BARE_LOCAL)+ model_cfg = policy.effective()[0]['model']+ assert (model_cfg['base_url'], model_cfg.get('key_env'), model_cfg.get('context_length')) == (+ LOCAL, 'LOCAL_KEY', 32768)+++async def test_a_session_create_with_a_bare_custom_pick_stays_on_the_profiles_endpoint(monkeypatch):+ """The real ``session.create`` (``stage_local_session``) and the turn's runtime resolver."""+ import json+ from gateway import run+ 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(BARE_LOCAL))+ store, runner, authority, owner = _owner(home)+ runner._peek_session_state = lambda key: None+ try:+ response = await owner.dispatch({'id': 'c', 'method': 'session.create', 'params': dict(+ request_id='c', source='cli', cwd=str(home), toolsets=[], provider='custom', model='qwen3-32b')})+ assert 'result' in response, response+ live = authority.sessions[response['result']['session_id']]+ assert _route(runner, live) == ('qwen3-32b', LOCAL, 'local-key')+ finally:+ await owner.close()+ store.close_all_db_handles()