Skip to content

The agent that grows with you

fix(gateway): the welcome host's pinned model is not a fallback either

EloquentBrush0xb23877a5ec7f
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index f8587a0debd..089a632d29c 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -3616,6 +3616,16 @@ class GatewayTurnMixin:
_agent_provider = getattr(_agent, 'provider', '') or ''
if _agent_provider and _agent_provider not in _AGGREGATOR_PROVIDERS:
_cfg_model = normalize_model_for_provider(_cfg_model, _agent_provider)
+ # AIAgent.__init__ also pins the Nous welcome host to its one model (pin_model_for_route), whatever
+ # the chat configured. Apply it on the PRIMARY route: a fallback that lands on that host is drift.
+ _primary_route = getattr(_agent, '_primary_runtime', None) or {}
+ with suppress(Exception):
+ from hermes_cli.anon_auth import pin_model_for_route
+ _cfg_model = pin_model_for_route(
+ _primary_route.get("provider", getattr(_agent, 'provider', None)),
+ _primary_route.get("base_url", getattr(_agent, 'base_url', None)),
+ _cfg_model,
+ )
if _agent.model != _cfg_model and not self._is_intentional_model_switch(session_key, _agent, _cfg_model):
self._evict_cached_agent(session_key)
diff --git a/tests/gateway/test_model_switch_persistence.py b/tests/gateway/test_model_switch_persistence.py
index ac0a1ddbd0d..fcd4342fed2 100644
--- a/tests/gateway/test_model_switch_persistence.py
+++ b/tests/gateway/test_model_switch_persistence.py
@@ -171,15 +171,15 @@ class TestFallbackEvictionHonorsChannelOverrides:
the cached agent after every turn."""
@staticmethod
- def _evicted_after_turn(monkeypatch, source, agent_model):
+ def _evicted_after_turn(monkeypatch, source, agent_model, *, global_model="default/model", **agent_attrs):
from gateway.turn_context import TurnContext
- monkeypatch.setattr("gateway.run._resolve_gateway_model", lambda *a, **k: "default/model")
+ monkeypatch.setattr("gateway.run._resolve_gateway_model", lambda *a, **k: global_model)
runner = _make_runner()
runner.config.platforms[Platform.TELEGRAM].channel_overrides = {"c1": ChannelOverride(model="chan/model")}
runner._evict_cached_agent = MagicMock()
ctx = TurnContext(source=source, session_key=build_session_key(source))
- ctx.agent_holder[0] = SimpleNamespace(model=agent_model, provider="openrouter")
+ ctx.agent_holder[0] = SimpleNamespace(model=agent_model, **({"provider": "openrouter"} | agent_attrs))
ctx.result_holder[0] = {"final_response": "ok"}
runner._run_agent_evict_on_fallback(ctx)
return runner._evict_cached_agent.called
@@ -195,6 +195,33 @@ class TestFallbackEvictionHonorsChannelOverrides:
def test_fallback_off_the_channel_model_still_evicts(self, monkeypatch):
assert self._evicted_after_turn(monkeypatch, _make_source(), "fallback/model") is True
+ _WELCOME = "https://welcome-api.nousresearch.com/v1"
+
+ @pytest.mark.parametrize("global_model", ["nous/welcome", "default/model"])
+ def test_the_welcome_host_pins_its_model_over_the_channel_model(self, monkeypatch, global_model):
+ """AIAgent.__init__ runs a welcome-host agent on ``nous/welcome`` whatever the chat configured."""
+ from hermes_cli.anon_auth import pin_model_for_route
+
+ agent_model = pin_model_for_route("nous", self._WELCOME, "chan/model")
+ assert agent_model == "nous/welcome"
+ assert self._evicted_after_turn(
+ monkeypatch, _make_source(), agent_model, global_model=global_model,
+ provider="nous", base_url=self._WELCOME,
+ ) is False
+
+ def test_the_recorded_welcome_alias_switch_keeps_the_cached_agent(self, monkeypatch):
+ assert self._evicted_after_turn(
+ monkeypatch, _make_source(), "backing/model", provider="nous", base_url=self._WELCOME,
+ _nous_model_switch=("nous/welcome", "backing/model"),
+ ) is False
+
+ def test_a_fallback_onto_the_welcome_host_still_evicts(self, monkeypatch):
+ """The pin applies to the agent's primary route, not the fallback route it is on now."""
+ assert self._evicted_after_turn(
+ monkeypatch, _make_source(), "nous/welcome", provider="nous", base_url=self._WELCOME,
+ _primary_runtime={"provider": "openrouter", "base_url": "https://openrouter.ai/api/v1"},
+ ) is True
+
class TestOneTurnModelOverrideRestore:
"""Verify gateway one-turn overrides restore previous session state."""