The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): run a same-provider --once alias on its own endpoint and wire
Tekniumfa736558872a
diff --git a/gateway/session_cli_turn.py b/gateway/session_cli_turn.pyindex 490c1f9f7bf..95c6bc7706f 100644--- a/gateway/session_cli_turn.py+++ b/gateway/session_cli_turn.py@@ -102,17 +102,23 @@ def one_turn_runtime(override, model, runtime_kwargs, config):runtime = dict(runtime_kwargs)current_provider = runtime_kwargs.get('provider') or ''same_provider = (result.target_provider or '').strip().lower() == current_provider.strip().lower()- if same_provider:- # The explicit provider on a one-turn override re-resolves the provider's default endpoint- # and key. The session already has both, so keep them. The wire follows the target, as a- # session /model writes it: Copilot, Nous Portal and OpenCode pick it per model.+ if same_provider and not result.resolved_via_alias:+ # A plain model pick on the session's provider: the explicit provider re-resolved that provider's+ # default endpoint and key, and the session already has both (a mirror, a proxy, a pool), so keep+ # them. The wire follows the target only where the provider picks it per model (Copilot, Nous+ # Portal, OpenCode; ``_rederive_per_model_route``); otherwise ``result.api_mode`` describes the+ # default host, not this session's endpoint, and the session's wire stays.+ from hermes_cli.model_switch import model_derived_api_modefrom hermes_cli.models import normalize_opencode_base_url, opencode_provider_family- api_mode = result.api_mode or runtime.get('api_mode')- runtime['api_mode'] = api_mode or None+ if model_derived_api_mode(result.target_provider, result.new_model) is not None and result.api_mode:+ runtime['api_mode'] = result.api_mode+ api_mode = runtime.get('api_mode') or None+ runtime['api_mode'] = api_modeif runtime.get('base_url') and opencode_provider_family(result.target_provider):runtime['base_url'] = normalize_opencode_base_url(result.target_provider, api_mode, runtime['base_url'])else:- # A different provider does not keep this session's pool, endpoint, or key.+ # A different provider, or an alias that names its own endpoint and key (``model_aliases``), does+ # not keep this session's pool, endpoint, or key: the route ``switch_model`` resolved runs whole.runtime.update(credential_pool=None, command=None, args=[],api_key=result.api_key, base_url=result.base_url, api_mode=result.api_mode or None,request_overrides=dict(result.request_overrides or {}),diff --git a/tests/gateway/test_one_turn_route_publish.py b/tests/gateway/test_one_turn_route_publish.pyindex adb86d6f98f..e83c9c17745 100644--- a/tests/gateway/test_one_turn_route_publish.py+++ b/tests/gateway/test_one_turn_route_publish.py@@ -2,6 +2,8 @@override must keep the session endpoint and key."""from types import SimpleNamespace+import pytest+from gateway.session_cli_turn import cli_turn_scope, one_turn_runtimefrom hermes_cli.model_switch import ModelSwitchResult@@ -169,3 +171,54 @@ def test_settled_info_pairs_model_and_provider_from_one_profile_read(monkeypatchwith cli_turn_scope("adm", {"model_override": {"model": "once-model", "provider": "nous"}}):publish_settled_info(turn, agent)assert (published[-1]["model"], published[-1]["provider"]) == ("openai/gpt-b", "openrouter")+++def test_same_provider_override_keeps_the_sessions_wire_when_the_model_does_not_pick_one(monkeypatch):+ """Review of #140969: ``result.api_mode`` is derived from the provider's DEFAULT host. A session on+ openai-api behind a chat-completions proxy kept the proxy but took ``codex_responses``, a wire the+ proxy does not speak. Only a provider that picks its wire per model overrides the session's."""+ runtime = _same_provider_override(+ monkeypatch, "openai-api", "gpt-5.4", "codex_responses", "https://api.openai.com/v1",+ {"base_url": "https://proxy.example/v1", "api_mode": "chat_completions"})+ assert (runtime["base_url"], runtime["api_mode"]) == ("https://proxy.example/v1", "chat_completions")+++SESSION_URL, ALIAS_URL = "http://127.0.0.1:7001/v1", "http://127.0.0.1:7002/v1"+++def alias_config(monkeypatch):+ """A ``model_aliases`` entry on the session's own provider with its own endpoint and key, resolved+ by the real ``switch_model`` (only the catalog probe is stubbed)."""+ cfg = {"model": {"default": "session-model", "provider": "custom", "base_url": SESSION_URL},+ "model_aliases": {"fast": {"model": "fast-model", "provider": "custom", "base_url": ALIAS_URL,+ "api_key": "sk-alias"}}}+ monkeypatch.setattr("hermes_cli.config.load_config", lambda *a, **k: cfg)+ monkeypatch.setattr("hermes_cli.runtime_provider.load_config", lambda *a, **k: cfg)+ monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", lambda *a, **k: {+ "accepted": True, "persist": True, "recognized": True, "message": ""})+ monkeypatch.setattr("hermes_cli.model_switch.DIRECT_ALIASES", {})+ return cfg++[email protected]("override", [{"model": "fast"}, {"model": "fast-model", "provider": "custom"}],+ ids=["alias-name", "cli-resolved-form"])+def test_a_same_provider_alias_runs_on_its_own_endpoint_and_key(alias_config, override):+ """Review of #140969: the same-provider branch kept the session's endpoint and key for an alias that+ names its own, so the turn asked the session's server for the alias's model with the session's key."""+ model, runtime = one_turn_runtime(+ override, "session-model",+ {"provider": "custom", "api_key": "sk-session", "base_url": SESSION_URL, "api_mode": "chat_completions",+ "credential_pool": object()},+ alias_config)+ assert model == "fast-model"+ assert (runtime["base_url"], runtime["api_key"]) == (ALIAS_URL, "sk-alias")+ assert runtime["credential_pool"] is None # the session's pool holds the session's keys+++def test_a_plain_same_provider_pick_still_keeps_the_session_endpoint(alias_config):+ _model, runtime = one_turn_runtime(+ {"model": "other-model", "provider": "custom"}, "session-model",+ {"provider": "custom", "api_key": "sk-session", "base_url": SESSION_URL, "api_mode": "chat_completions"},+ alias_config)+ assert (runtime["base_url"], runtime["api_key"]) == (SESSION_URL, "sk-session")