The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(gateway): channel_overrides turns keep one cached agent through the real turn path
teknium1e72180bc7a03
diff --git a/tests/gateway/test_internal_event_pin_wiring.py b/tests/gateway/test_internal_event_pin_wiring.pyindex f28611f516a..ad3d8ec2388 100644--- a/tests/gateway/test_internal_event_pin_wiring.py+++ b/tests/gateway/test_internal_event_pin_wiring.py@@ -11,6 +11,8 @@ reaches ``_run_agent`` is byte-identical on all three turns.from __future__ import annotations+import sys+import typesfrom datetime import datetimefrom unittest.mock import AsyncMock, MagicMock@@ -419,3 +421,83 @@ async def test_internal_event_never_reuses_prompt_pin_from_another_privacy_policassert calls[0]["context_prompt"] != "UNREDACTED CONTEXT"assert calls[0]["channel_prompt"] == "Channel hint."+++# ---------------------------------------------------------------------------+# #131294 — a channel_overrides model is the session's CONFIGURED model, not a+# fallback. The post-turn ``_run_agent_evict_on_fallback`` check baselined+# against the global model only, so every successful turn in an overridden chat+# evicted the cached agent (and cleared its ephemeral pin). These drive the+# REAL ``_run_agent`` / ``TurnRunner.run_sync`` path (only the AIAgent class and+# config reads are substituted), not the ``_capture`` stub above.+# ---------------------------------------------------------------------------++_WELCOME = "https://welcome-api.nousresearch.com/v1"+++class _TurnAgent:+ """AIAgent stand-in: keeps the resolved route, answers without an API call."""++ instances = 0++ def __init__(self, *args, **kwargs):+ type(self).instances += 1+ self.model = kwargs.get("model")+ self.provider = kwargs.get("provider")+ self.base_url = kwargs.get("base_url")+ self.session_id = kwargs.get("session_id")+ self.tools = []+ self.request_overrides = dict(kwargs.get("request_overrides") or {})+ # Mirror AIAgent.__init__: the welcome host pins its one model, and the primary+ # route is snapshotted for the post-turn fallback classifier.+ from hermes_cli.anon_auth import pin_model_for_route+ self.model = pin_model_for_route(self.provider, self.base_url, self.model)+ self._primary_runtime = {"model": self.model, "provider": self.provider, "base_url": self.base_url}++ def run_conversation(self, user_message, conversation_history=None, task_id=None, **kwargs):+ return {+ "final_response": "ok",+ "messages": (conversation_history or []) + [+ {"role": "user", "content": user_message},+ {"role": "assistant", "content": "ok"},+ ],+ "api_calls": 1,+ }+++ "runtime, global_model, expected_model",+ [+ ({"api_key": "fake", "provider": "openrouter"}, "default/model", "chan/model"),+ ({"api_key": "fake", "provider": "nous", "base_url": _WELCOME}, "nous/welcome", "nous/welcome"),+ ],+ ids=["named-channel-model", "welcome-host-pins-over-channel-model"],+)+async def test_channel_override_turns_keep_one_cached_agent_and_the_pin(+ monkeypatch, runtime, global_model, expected_model,+):+ import gateway.run as gr++ config = GatewayConfig()+ config.platforms[Platform.DISCORD] = PlatformConfig(+ enabled=True, channel_overrides={_ORIGIN["chat_id"]: ChannelOverride(model="chan/model")},+ )+ monkeypatch.setattr(gr, "_load_gateway_config", lambda: {"model": {"default": global_model}})+ monkeypatch.setattr(gr, "_resolve_gateway_model", lambda config=None: global_model)+ fake_run_agent = types.ModuleType("run_agent")+ fake_run_agent.AIAgent = _TurnAgent+ monkeypatch.setitem(sys.modules, "run_agent", fake_run_agent)+ _TurnAgent.instances = 0++ runner = _make_runner(monkeypatch, config)+ monkeypatch.setattr(gr, "_resolve_runtime_agent_kwargs", lambda: dict(runtime))+ runner.session_store._entries = {}++ await _drive(runner, ((False, _human_source()), (True, _wake_source()), (False, _human_source())))++ entry = runner._agent_cache.get(KEY)+ assert entry is not None, "channel-override turn evicted the session's cached agent"+ assert entry[0].model == expected_model+ assert _TurnAgent.instances == 1, f"agent rebuilt {_TurnAgent.instances - 1}x across 3 turns"+ assert runner._peek_session_state(KEY).conversation.ephemeral_pin is not None, "context pin cleared"