The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(acp): run switch_model off the event loop
teknium1b57e7262e86f
diff --git a/acp_adapter/commands.py b/acp_adapter/commands.pyindex a9f82f767e4..5a79309d92c 100644--- a/acp_adapter/commands.py+++ b/acp_adapter/commands.py@@ -95,7 +95,7 @@ class SlashCommandsMixin:return Nonehandler = getattr(self, f"_cmd_{cmd}")- # Handlers run on the loop thread, outside the per-turn cwd-pinning context. ``/compress``+ # Handlers run outside the per-turn cwd-pinning context. ``/compress``# and ``/model`` REBUILD the system prompt, so unpinned they'd bake the Hermes install tree# into the persisted cached prompt. Pin inside a fresh context: no leak, no teardown.def _dispatch() -> str | None:diff --git a/acp_adapter/server.py b/acp_adapter/server.pyindex 70f708aec1c..eb18d795131 100644--- a/acp_adapter/server.py+++ b/acp_adapter/server.py@@ -794,7 +794,9 @@ class HermesACPAgent(SlashCommandsMixin, acp.Agent):# Slash commands are text-only; a prompt with media goes to the agent even if it starts with "/".if text_only_prompt and isinstance(user_content, str) and user_text.startswith("/"):- response_text = self._handle_slash_command(user_text, state)+ # Off the loop: /model validates through switch_model (network I/O) and /compress+ # calls the LLM; handlers are sync and hold no loop-bound state.+ response_text = await asyncio.to_thread(self._handle_slash_command, user_text, state)if response_text is not None:if self._conn:await self._conn.session_update(session_id, acp.update_agent_message_text(response_text))@@ -936,7 +938,10 @@ class HermesACPAgent(SlashCommandsMixin, acp.Agent):"""Switch the model for a session (called by ACP protocol)."""state = self.session_manager.get_session(session_id)if state:- _old, requested_provider, resolved_model = self._switch_model(state, model_id, keep_endpoint=True)+ # switch_model() does synchronous network I/O (models.dev, custom-endpoint probes,+ # ~10 s cold) — off the loop, like the gateway, so other ACP sessions keep flowing.+ _old, requested_provider, resolved_model = await asyncio.to_thread(+ self._switch_model, state, model_id, keep_endpoint=True)logger.info("Session %s: model switched to %s via provider %s", session_id, resolved_model, requested_provider)diff --git a/tests/acp_adapter/test_acp_dashboard_model_switch_validation.py b/tests/acp_adapter/test_acp_dashboard_model_switch_validation.pyindex 581c40c9843..e8e9a0d5877 100644--- a/tests/acp_adapter/test_acp_dashboard_model_switch_validation.py+++ b/tests/acp_adapter/test_acp_dashboard_model_switch_validation.py@@ -69,3 +69,30 @@ def test_acp_explicit_provider_prefix_becomes_explicit_provider(monkeypatch):assert (seen["explicit_provider"], seen["raw_input"]) == ("anthropic", "claude-sonnet-5")assert (old, new_provider, model) == ("anthropic", "anthropic", "claude-sonnet-5")assert made["requested_provider"] == "anthropic" and made["base_url"] == "https://api.anthropic.com"+++def test_acp_set_session_model_runs_switch_model_off_the_event_loop(monkeypatch):+ """``switch_model`` does ~10 s of sync network I/O on a cold cache; ACP must run it on a+ worker thread (like the gateway) or every session in the process stalls."""+ import asyncio+ import threading++ seen: dict = {}++ def _switch(**kw):+ seen["thread"] = threading.current_thread()+ return ModelSwitchResult(success=True, new_model=kw["raw_input"], target_provider="anthropic")++ monkeypatch.setattr("hermes_cli.model_switch.switch_model", _switch)+ agent, _made = _acp_agent()+ state = _state()+ agent.session_manager.get_session = lambda sid: state++ async def _run():+ loop_thread = threading.current_thread()+ resp = await agent.set_session_model("anthropic:claude-sonnet-5", "s1")+ return resp, loop_thread++ resp, loop_thread = asyncio.run(_run())+ assert resp is not None and state.model == "claude-sonnet-5"+ assert seen["thread"] is not loop_thread