server.py 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. """ACP agent server — exposes Hermes Agent via the Agent Client Protocol."""
  2. from __future__ import annotations
  3. import asyncio
  4. from datetime import datetime, timezone, UTC
  5. import contextlib
  6. import contextvars
  7. import logging
  8. import os
  9. import threading
  10. from collections import defaultdict, deque
  11. from concurrent.futures import ThreadPoolExecutor
  12. from dataclasses import dataclass
  13. from typing import Any, Callable, Deque, Optional
  14. import acp
  15. from acp.schema import (
  16. AgentCapabilities, AgentMessageChunk, AuthenticateResponse, ClientCapabilities, ForkSessionResponse,
  17. Implementation, InitializeResponse, ListSessionsResponse, LoadSessionResponse, McpServerHttp, McpServerSse,
  18. McpServerStdio, ModelInfo, NewSessionResponse, PromptCapabilities, PromptResponse, ResumeSessionResponse,
  19. SessionCapabilities, SessionForkCapabilities, SessionInfo, SessionInfoUpdate, SessionListCapabilities,
  20. SessionMode, SessionModeState, SessionModelState, SessionResumeCapabilities, SetSessionConfigOptionResponse,
  21. SetSessionModeResponse, SetSessionModelResponse, TextContentBlock, Usage, UsageUpdate, UserMessageChunk,
  22. )
  23. from acp_adapter.auth import TERMINAL_SETUP_AUTH_METHOD_ID, build_auth_methods, detect_provider
  24. from acp_adapter.commands import SlashCommandsMixin, _estimate_tokens
  25. from acp_adapter.content import PromptBlock, _content_blocks_to_openai_user_content, _extract_text
  26. from acp_adapter.events import (
  27. AssistantMessageIdAllocator, _build_plan_update_from_todo_result, _send_update, flush_open_tool_calls,
  28. make_message_cb, make_step_cb, make_thinking_cb, make_tool_progress_cb,
  29. )
  30. from acp_adapter.model_catalog import build_model_state, encode_model_choice
  31. from acp_adapter import modes as _modes
  32. from acp_adapter.permissions import make_approval_callback
  33. from acp_adapter.provenance import session_provenance_meta
  34. from acp_adapter.session import SessionManager, SessionState, _expand_acp_enabled_toolsets
  35. from acp_adapter.tools import build_tool_complete, build_tool_start, coerce_tool_args
  36. from agent.context_compressor import (COMPRESSED_SUMMARY_METADATA_KEY, ContextCompressor)
  37. from agent.interrupt_compat import request_hard_interrupt
  38. from tools.approval_context import reset_hermes_interactive_context, set_hermes_interactive_context
  39. logger = logging.getLogger(__name__)
  40. # Runs the synchronous AIAgent off the event loop.
  41. _executor = ThreadPoolExecutor(max_workers=4, thread_name_prefix="acp-agent")
  42. # ListSessionsRequest has no client-side limit; clients paginate via `cursor`/`next_cursor`.
  43. _LIST_SESSIONS_PAGE_SIZE = 50
  44. def _flatten_history_text(value: Any) -> str:
  45. """Persisted content/reasoning (str, or list of ``{"text"}`` / ``{"type": "text", "content"}``
  46. parts) -> one stripped string; whitespace-only collapses to ``""`` ("nothing to emit")."""
  47. if isinstance(value, str):
  48. return value.strip()
  49. if isinstance(value, list):
  50. parts: list[str] = []
  51. for item in value:
  52. if isinstance(item, dict):
  53. text = item.get("text")
  54. if isinstance(text, str):
  55. parts.append(text)
  56. elif item.get("type") == "text" and isinstance(item.get("content"), str):
  57. parts.append(item["content"])
  58. elif isinstance(item, str):
  59. parts.append(item)
  60. return "\n".join(part.strip() for part in parts if part and part.strip()).strip()
  61. return ""
  62. def _history_reasoning_text(message: dict[str, Any]) -> str:
  63. """First non-empty of ``reasoning_content`` and ``reasoning`` — both live keys, for
  64. different transports (not old-vs-new)."""
  65. for key in ("reasoning_content", "reasoning"):
  66. text = _flatten_history_text(message.get(key))
  67. if text:
  68. return text
  69. return ""
  70. def _history_summary_meta(message: dict[str, Any], text: str) -> dict[str, Any] | None:
  71. """``_meta`` for a replayed compaction summary, else None.
  72. Summaries persist as ordinary messages, standalone (either role) or merged into the first
  73. preserved tail message. Two keys so clients can't hide real content: ``compactionSummary``
  74. (whole chunk; safe to collapse) vs ``containsCompactionSummary`` (real content + summary).
  75. Uses the in-process flag, falling back to content classification for DB-reloaded sessions."""
  76. kind = ContextCompressor.classify_summary_content(text)
  77. if kind is None and message.get(COMPRESSED_SUMMARY_METADATA_KEY):
  78. # Flagged but unclassified (prefix drift): the flag only marks summaries -> standalone.
  79. kind = "standalone"
  80. if kind == "standalone":
  81. return {"hermes": {"compactionSummary": True}}
  82. if kind == "merged":
  83. return {"hermes": {"containsCompactionSummary": True}}
  84. return None
  85. # role -> (chunk class, session_update tag) for history replay.
  86. _HISTORY_CHUNK_TYPES = {
  87. "user": (UserMessageChunk, "user_message_chunk"), "assistant": (AgentMessageChunk, "agent_message_chunk")
  88. }
  89. def _history_tool_call_name_args(tool_call: dict[str, Any]) -> tuple[str, dict[str, Any]]:
  90. """Extract function name/arguments from an OpenAI-style tool_call."""
  91. function = tool_call.get("function") if isinstance(tool_call.get("function"), dict) else {}
  92. name = str(function.get("name") or tool_call.get("name") or "unknown_tool")
  93. raw_args = function.get("arguments") or tool_call.get("arguments") or tool_call.get("args") or {}
  94. return name, coerce_tool_args(raw_args)
  95. def _history_message_chunk(role: str, message: dict[str, Any]) -> UserMessageChunk | AgentMessageChunk | None:
  96. text = _flatten_history_text(message.get("content"))
  97. if not text:
  98. return None
  99. cls, session_update = _HISTORY_CHUNK_TYPES[role]
  100. return cls(
  101. session_update=session_update, content=TextContentBlock(type="text", text=text),
  102. field_meta=_history_summary_meta(message, text),
  103. )
  104. def _history_replay_updates(history: list[dict[str, Any]]):
  105. """Yield ACP session updates that reconstruct a persisted transcript, in order: user/assistant
  106. text (with compaction ``_meta``), assistant thoughts, and tool-call start/complete pairs
  107. (``todo`` results also re-emit the plan)."""
  108. active_tool_calls: dict[str, tuple[str, dict[str, Any]]] = {}
  109. for message in history:
  110. role = str(message.get("role") or "")
  111. if role == "user":
  112. if (chunk := _history_message_chunk(role, message)) is not None:
  113. yield chunk
  114. elif role == "assistant":
  115. thought = _history_reasoning_text(message)
  116. if thought:
  117. yield acp.update_agent_thought_text(thought)
  118. if (chunk := _history_message_chunk(role, message)) is not None:
  119. yield chunk
  120. tool_calls = message.get("tool_calls")
  121. for tool_call in tool_calls if isinstance(tool_calls, list) else ():
  122. if not isinstance(tool_call, dict):
  123. continue
  124. tool_call_id = str(
  125. tool_call.get("id") or tool_call.get("call_id") or tool_call.get("tool_call_id") or ""
  126. ).strip()
  127. if not tool_call_id:
  128. continue
  129. tool_name, args = _history_tool_call_name_args(tool_call)
  130. active_tool_calls[tool_call_id] = (tool_name, args)
  131. yield build_tool_start(tool_call_id, tool_name, args)
  132. elif role == "tool":
  133. tool_call_id = str(message.get("tool_call_id") or "").strip()
  134. tool_name = str(message.get("tool_name") or "").strip()
  135. function_args: dict[str, Any] | None = None
  136. if tool_call_id in active_tool_calls:
  137. tool_name, function_args = active_tool_calls.pop(tool_call_id)
  138. if not tool_call_id or not tool_name:
  139. continue
  140. result = message.get("content")
  141. result_text = result if isinstance(result, str) else None
  142. yield build_tool_complete(tool_call_id, tool_name, result=result_text, function_args=function_args)
  143. if tool_name == "todo":
  144. plan_update = _build_plan_update_from_todo_result(result_text)
  145. if plan_update is not None:
  146. yield plan_update
  147. def _mcp_server_config(server: McpServerStdio | McpServerHttp | McpServerSse) -> dict:
  148. if isinstance(server, McpServerStdio):
  149. return {"command": server.command, "args": list(server.args), "env": {i.name: i.value for i in server.env}}
  150. return {"url": server.url, "headers": {i.name: i.value for i in server.headers}}
  151. def _restore_env(key: str, value: str | None) -> None:
  152. if value is None:
  153. os.environ.pop(key, None)
  154. else:
  155. os.environ[key] = value
  156. def _bind_guarded(stack: contextlib.ExitStack, label: str, setup: Callable[[], Callable[[], None]]) -> None:
  157. """Run ``setup`` (returns its teardown) and register the teardown; failures in either half only
  158. log — the turn must still run without the binding."""
  159. try:
  160. teardown = setup()
  161. except Exception:
  162. logger.debug("Could not set ACP %s", label, exc_info=True)
  163. return
  164. def _teardown() -> None:
  165. try:
  166. teardown()
  167. except Exception:
  168. logger.debug("Could not restore ACP %s", label, exc_info=True)
  169. stack.callback(_teardown)
  170. def _attach_interrupted_prompt(interrupted_prompt: str, guidance: str) -> str:
  171. return f"{interrupted_prompt}\n\nUser correction/guidance after interrupt: {guidance}"
  172. def _take_interrupted_prompt(state: SessionState) -> tuple[bool, str]:
  173. """``(idle, interrupted_prompt)``; consumes the cancelled prompt only when the session is idle."""
  174. with state.runtime_lock:
  175. if state.is_running:
  176. return False, ""
  177. text, state.interrupted_prompt_text = state.interrupted_prompt_text, ""
  178. return True, text
  179. class ModelRejected(ValueError):
  180. """``switch_model`` refused the requested model (no provider can serve it)."""
  181. @dataclass
  182. class _TurnCallbacks:
  183. """Per-turn ACP streaming callbacks; all None when no client is connected."""
  184. tool_progress_cb: Any = None
  185. reasoning_cb: Any = None
  186. step_cb: Any = None
  187. stream_delta_cb: Any = None
  188. approval_cb: Any = None
  189. edit_approval_requester: Any = None
  190. streamed: bool = False
  191. tool_call_ids: Any = None
  192. tool_call_meta: Any = None
  193. class HermesACPAgent(SlashCommandsMixin, acp.Agent):
  194. """ACP Agent implementation wrapping Hermes AIAgent."""
  195. # mode id -> (edit approval policy, display name, description): ``acp_adapter/modes.py``
  196. _EDIT_APPROVAL_POLICY_CONFIG_ID = _modes.EDIT_APPROVAL_POLICY_CONFIG_ID
  197. _EDIT_APPROVAL_POLICY_DEFAULT = _modes.DEFAULT_EDIT_APPROVAL_POLICY
  198. _MODE_DEFAULT = _modes.DEFAULT_MODE
  199. _MODES = _modes.MODES
  200. _MODE_TO_EDIT_APPROVAL_POLICY = _modes.MODE_TO_EDIT_APPROVAL_POLICY
  201. _EDIT_APPROVAL_POLICY_TO_MODE = _modes.EDIT_APPROVAL_POLICY_TO_MODE
  202. def __new__(cls, session_manager=None):
  203. if session_manager is None:
  204. from acp_adapter.gateway_server import GatewayACPAgent
  205. return GatewayACPAgent()
  206. return super().__new__(cls)
  207. def __init__(self, session_manager: SessionManager | None = None):
  208. super().__init__()
  209. self.session_manager = session_manager or SessionManager()
  210. self._conn: Optional[acp.Client] = None
  211. # ---- Connection lifecycle -----------------------------------------------
  212. def on_connect(self, conn: acp.Client) -> None:
  213. """Store the client connection for sending session updates."""
  214. self._conn = conn
  215. logger.info("ACP client connected")
  216. async def _send(self, session_id: str, update: Any, *, fail_msg: str, level: int = logging.WARNING) -> bool:
  217. """``session_update`` that logs instead of raising; False on failure."""
  218. try:
  219. await self._conn.session_update(session_id=session_id, update=update)
  220. return True
  221. except Exception:
  222. logger.log(level, fail_msg, session_id, exc_info=True)
  223. return False
  224. def _schedule_soon(self, make_coro: Callable[[], Any]) -> None:
  225. """Run a notification coroutine right after the current response is queued."""
  226. if not self._conn:
  227. return
  228. loop = asyncio.get_running_loop()
  229. loop.call_soon(asyncio.create_task, make_coro())
  230. def _session_modes(self, state: SessionState) -> SessionModeState:
  231. """Edit-approval policy as ACP modes. Zed renders ``config_options`` in the model
  232. picker's slot; modes (as Claude/Codex use) coexist with the picker."""
  233. current = str(getattr(state, "mode", "") or self._MODE_DEFAULT)
  234. if current not in self._MODES:
  235. current = self._MODE_DEFAULT
  236. return SessionModeState(
  237. current_mode_id=current,
  238. available_modes=[SessionMode(id=m, name=n, description=d) for m, (_p, n, d) in self._MODES.items()],
  239. )
  240. def _edit_approval_policy_for_state(self, state: SessionState) -> tuple[str, str | None]:
  241. mode = str(getattr(state, "mode", "") or self._MODE_DEFAULT)
  242. policy = self._MODE_TO_EDIT_APPROVAL_POLICY.get(mode, self._EDIT_APPROVAL_POLICY_DEFAULT)
  243. return policy, state.cwd
  244. def _build_model_state(self, state: SessionState) -> SessionModelState | None:
  245. """Authenticated providers + models, from the shared Hermes inventory (same substrate
  246. as ``hermes model``/TUI/dashboard) so the selector isn't just the current curated list."""
  247. model = str(state.model or getattr(state.agent, "model", "") or "").strip()
  248. provider = getattr(state.agent, "provider", None) or detect_provider() or "openrouter"
  249. try:
  250. picker = build_model_state(model, provider, str(getattr(state.agent, "base_url", "") or ""))
  251. if picker is not None:
  252. return picker
  253. except Exception:
  254. logger.debug("Could not build ACP model state", exc_info=True)
  255. if not model:
  256. return None
  257. choice = encode_model_choice(provider, model)
  258. return SessionModelState(available_models=[ModelInfo(model_id=choice, name=model)], current_model_id=choice)
  259. def _switch_model(
  260. self, state: SessionState, raw_model: str, *, keep_endpoint: bool = False
  261. ) -> tuple[str | None, str, str]:
  262. """Rebuild the session agent on a new model -> (old provider, new provider, model).
  263. Resolution goes through ``hermes_cli.model_switch.switch_model`` seeded with the live
  264. agent route — the same catalog/alias/credential validation as CLI/gateway/TUI ``/model``
  265. — so ACP never hands the session a model no provider can serve. ``provider:model`` picker
  266. ids become ``--provider``. ACP never persists. ``keep_endpoint`` carries base_url/api_mode
  267. over when the provider is unchanged."""
  268. from hermes_cli.config import get_compatible_custom_providers, load_config
  269. from hermes_cli.model_switch import switch_model
  270. from hermes_cli.models import parse_model_input
  271. current_provider, current_model = getattr(state.agent, "provider", None), str(state.model or "")
  272. explicit_provider, model_input = parse_model_input(raw_model, "")
  273. cfg = load_config()
  274. result = switch_model(
  275. raw_input=model_input, explicit_provider=explicit_provider,
  276. current_provider=current_provider or "openrouter", current_model=current_model,
  277. current_base_url=str(getattr(state.agent, "base_url", "") or ""),
  278. current_api_key=str(getattr(state.agent, "api_key", "") or ""),
  279. user_providers=cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {},
  280. custom_providers=get_compatible_custom_providers(cfg))
  281. if not result.success:
  282. raise ModelRejected(result.error_message or f"Cannot switch to {raw_model}")
  283. target_provider, new_model = result.target_provider, result.new_model
  284. endpoint: dict[str, Any] = {}
  285. if keep_endpoint and not (current_provider and target_provider != current_provider):
  286. endpoint = {
  287. "base_url": getattr(state.agent, "base_url", None), "api_mode": getattr(state.agent, "api_mode", None)
  288. }
  289. # ACP-provided MCP servers live only on the running agent's toolsets (``_register_session_mcp_servers``);
  290. # a rebuild that re-derived them from config would silently drop every session MCP tool (#42719).
  291. agent = self.session_manager._make_agent(
  292. session_id=state.session_id, cwd=state.cwd, model=new_model,
  293. requested_provider=target_provider, **endpoint,
  294. enabled_toolsets=getattr(state.agent, "enabled_toolsets", None),
  295. disabled_toolsets=getattr(state.agent, "disabled_toolsets", None),
  296. )
  297. # Assign only after the rebuild succeeded so a failed switch leaves the session on its
  298. # working model instead of a model/agent mismatch that persists via save_session.
  299. state.agent, state.model = agent, new_model
  300. self.session_manager.save_session(state.session_id)
  301. from hermes_cli.observability.shared_metrics_events import record_model_switch
  302. record_model_switch(
  303. from_provider=current_provider, to_provider=target_provider, surface="acp", from_model=current_model,
  304. session_id=state.session_id)
  305. return current_provider, target_provider, new_model
  306. @staticmethod
  307. def _build_usage_update(state: SessionState) -> UsageUpdate | None:
  308. """``usage_update`` for Zed's context indicator: ``size`` = context window, ``used`` =
  309. estimated request pressure (system prompt + history + tool schemas)."""
  310. compressor = getattr(state.agent, "context_compressor", None)
  311. size = int(getattr(compressor, "context_length", 0) or 0)
  312. if size <= 0:
  313. return None
  314. try:
  315. used = _estimate_tokens(state.history, state.agent)
  316. except Exception:
  317. logger.debug("Could not estimate ACP native context usage", exc_info=True)
  318. used = int(getattr(compressor, "last_prompt_tokens", 0) or 0)
  319. return UsageUpdate(session_update="usage_update", size=max(size, 0), used=max(used, 0))
  320. async def _send_usage_update(self, state: SessionState) -> None:
  321. if self._conn and (update := self._build_usage_update(state)) is not None:
  322. await self._send(state.session_id, update, fail_msg="Failed to send ACP usage update for session %s")
  323. def _provenance_meta(
  324. self, acp_session_id: str, current_hermes_session_id: str, previous_hermes_session_id: Optional[str] = None
  325. ) -> Optional[dict]:
  326. """Best-effort ``_meta.hermes.sessionProvenance`` for an ACP session."""
  327. try:
  328. return session_provenance_meta(
  329. self.session_manager._get_db(), acp_session_id, current_hermes_session_id,
  330. previous_hermes_session_id=previous_hermes_session_id,
  331. )
  332. except Exception:
  333. logger.debug("Could not build ACP session provenance for %s", acp_session_id, exc_info=True)
  334. return None
  335. async def _send_session_info_update(
  336. self, session_id: str, *,
  337. current_hermes_session_id: Optional[str] = None, previous_hermes_session_id: Optional[str] = None,
  338. ) -> None:
  339. """Session metadata update; pass ``previous_hermes_session_id`` when the internal head
  340. rotated (compression split) so provenance flags the reason."""
  341. if not self._conn:
  342. return
  343. try:
  344. row = self.session_manager._get_db().get_session(session_id)
  345. except Exception:
  346. logger.debug("Could not read ACP session info for %s", session_id, exc_info=True)
  347. return
  348. if not row:
  349. return
  350. title = row.get("title")
  351. # `sessions` has no `updated_at`; "now" is right since this fires when the title changed.
  352. update = SessionInfoUpdate(
  353. session_update="session_info_update",
  354. title=title if isinstance(title, str) and title.strip() else None,
  355. updated_at=datetime.now(UTC).isoformat(),
  356. field_meta=self._provenance_meta(
  357. session_id, current_hermes_session_id or session_id, previous_hermes_session_id
  358. ),
  359. )
  360. await self._send(
  361. session_id, update, fail_msg="Could not send ACP session info update for %s", level=logging.DEBUG
  362. )
  363. async def _register_session_mcp_servers(
  364. self, state: SessionState, mcp_servers: list[McpServerStdio | McpServerHttp | McpServerSse] | None
  365. ) -> None:
  366. """Register ACP-provided MCP servers and refresh the agent tool surface."""
  367. if not mcp_servers:
  368. return
  369. try:
  370. from agent.runtime_cwd import set_session_cwd
  371. from tools.mcp_tool_discovery import register_mcp_servers
  372. configs = {s.name: _mcp_server_config(s) for s in mcp_servers}
  373. def _register_pinned() -> None:
  374. # new_session/load_session run outside the per-turn cwd pin; the session's logical cwd is
  375. # the default stdio child cwd (tools/mcp_tool_transport.py::_run_stdio), so pin it here.
  376. set_session_cwd(state.cwd)
  377. register_mcp_servers(configs)
  378. await asyncio.to_thread(_register_pinned) # to_thread already runs in a copied context
  379. except Exception:
  380. logger.warning("Session %s: failed to register ACP MCP servers", state.session_id, exc_info=True)
  381. return
  382. try:
  383. from model_tools import get_tool_definitions
  384. from agent.memory_manager_tools import inject_memory_provider_tools
  385. agent = state.agent
  386. agent.enabled_toolsets = _expand_acp_enabled_toolsets(
  387. getattr(agent, "enabled_toolsets", None),
  388. mcp_server_names=[s.name for s in mcp_servers],
  389. )
  390. from agent.delegation_backends import saved_backend
  391. agent._delegation_backend = saved_backend() # the session routes on what its delegate_task is built from
  392. agent.tools = get_tool_definitions(
  393. enabled_toolsets=agent.enabled_toolsets,
  394. disabled_toolsets=getattr(agent, "disabled_toolsets", None), quiet_mode=True,
  395. )
  396. agent.valid_tool_names = {tool["function"]["name"] for tool in agent.tools or []}
  397. inject_memory_provider_tools(agent)
  398. if callable(invalidate := getattr(agent, "_invalidate_system_prompt", None)):
  399. invalidate()
  400. logger.info(
  401. "Session %s: refreshed tool surface after ACP MCP registration (%d tools)",
  402. state.session_id, len(agent.tools or []),
  403. )
  404. except Exception:
  405. logger.warning(
  406. "Session %s: failed to refresh tool surface after ACP MCP registration", state.session_id, exc_info=True,
  407. )
  408. def _schedule_mcp_late_refresh(self, state: SessionState) -> None:
  409. """Refresh the tool snapshot when background MCP discovery lands after agent build
  410. (``_make_agent`` only joins ~1.5s). Waits up to 30s off the critical path, then rebuilds
  411. via ``refresh_agent_mcp_tools`` (same as ``/reload-mcp``).
  412. Cache safety: only pre-first-turn (nothing cached yet); afterwards the snapshot stays
  413. frozen and late servers land via the between-turns prologue refresh
  414. (``agent/turn_context.py``). No-op if discovery finished, join timed out, registry
  415. unchanged, or session closed."""
  416. try:
  417. from hermes_cli.mcp_startup import mcp_discovery_in_flight
  418. except Exception:
  419. return
  420. if not mcp_discovery_in_flight():
  421. return
  422. agent, session_id = state.agent, state.session_id
  423. def _wait_then_refresh() -> None:
  424. try:
  425. from hermes_cli.mcp_startup import join_mcp_discovery
  426. if not join_mcp_discovery(timeout=30.0):
  427. return
  428. # In-memory only: ``get_session()`` would restore from DB and build a new AIAgent.
  429. with self.session_manager._lock:
  430. current = self.session_manager._sessions.get(session_id)
  431. if current is None or current.agent is not agent:
  432. return
  433. # ``prompt()`` flips ``is_running`` under ``runtime_lock`` before dispatching, so
  434. # holding it here closes the window where a refresh would swap ``tools=`` mid-turn.
  435. with current.runtime_lock:
  436. if current.is_running:
  437. return
  438. if any(int(getattr(agent, k, 0) or 0) > 0 for k in ("_user_turn_count", "_api_call_count")):
  439. return
  440. from tools.mcp_tool_agent import refresh_agent_mcp_tools
  441. added = refresh_agent_mcp_tools(agent, quiet_mode=True)
  442. if added:
  443. logger.info(
  444. "Session %s: late MCP refresh added %d tools: %s",
  445. session_id, len(added), ", ".join(sorted(added)),
  446. )
  447. except Exception:
  448. logger.debug("Session %s: late MCP refresh failed", session_id, exc_info=True)
  449. threading.Thread(target=_wait_then_refresh, name=f"acp-mcp-late-refresh-{session_id}", daemon=True).start()
  450. # ---- ACP lifecycle ------------------------------------------------------
  451. async def initialize(
  452. self, protocol_version: int | None = None, client_capabilities: ClientCapabilities | None = None,
  453. client_info: Implementation | None = None, **kwargs: Any,
  454. ) -> InitializeResponse:
  455. from hermes_cli.version_info import get_version_info
  456. auth_methods = build_auth_methods()
  457. logger.info(
  458. "Initialize from %s (protocol v%s)", client_info.name if client_info else "unknown",
  459. protocol_version if isinstance(protocol_version, int) else acp.PROTOCOL_VERSION,
  460. )
  461. return InitializeResponse(
  462. protocol_version=acp.PROTOCOL_VERSION,
  463. agent_info=Implementation(name="hermes-agent", version=get_version_info().base_version),
  464. agent_capabilities=AgentCapabilities(
  465. load_session=True,
  466. prompt_capabilities=PromptCapabilities(image=True),
  467. session_capabilities=SessionCapabilities(
  468. fork=SessionForkCapabilities(), list=SessionListCapabilities(), resume=SessionResumeCapabilities(),
  469. ),
  470. ),
  471. auth_methods=auth_methods,
  472. )
  473. async def authenticate(self, method_id: str, **kwargs: Any) -> AuthenticateResponse | None:
  474. # Only acknowledge the method_id advertised in initialize().
  475. if not isinstance(method_id, str):
  476. return None
  477. normalized_method = method_id.strip().lower()
  478. provider = detect_provider()
  479. if normalized_method == TERMINAL_SETUP_AUTH_METHOD_ID:
  480. # Terminal auth runs setup out-of-band; succeed only once credentials exist.
  481. return AuthenticateResponse() if provider else None
  482. if not provider or normalized_method != provider:
  483. return None
  484. return AuthenticateResponse()
  485. # ---- Session management -------------------------------------------------
  486. async def _replay_session_history(self, state: SessionState) -> None:
  487. """Replay history as user/assistant/thought chunks plus reconstructed tool-call
  488. start/complete events so the editor shows the transcript, not a clean thread."""
  489. if not self._conn or not state.history:
  490. return
  491. for update in _history_replay_updates(state.history):
  492. if not await self._send(state.session_id, update, fail_msg="Failed to replay ACP history for session %s"):
  493. return
  494. async def _session_response_fields(self, state: SessionState, replay_verb: str | None = None) -> dict[str, Any]:
  495. """``models``/``modes``/``field_meta`` for session responses, after an optional history replay;
  496. schedules command advertisement + usage refresh.
  497. Per ACP spec, load/resume must stream history via ``session/update`` BEFORE responding
  498. (Codex/Claude Code/OpenCode/Zed rely on this; deferring via ``call_soon`` broke them).
  499. Best-effort: a corrupt message must not turn the load into an error."""
  500. if replay_verb:
  501. try:
  502. # Per ACP spec, `session/load` must stream the prior conversation back to the client via
  503. # `session/update` notifications BEFORE responding, so the client receives the full
  504. # transcript within the load request's lifetime. Awaiting the replay here matches Codex /
  505. # Claude Code / OpenCode / Pi and the Zed client (which registers the session-update routing
  506. # entry before awaiting the loadSession RPC specifically so in-call history replay updates
  507. # can find the thread). Deferring this via `loop.call_soon` (as we did briefly in May 2026)
  508. # broke every spec-compliant ACP client that measures notifications synchronously against
  509. # the load response — see #12285 follow-up.
  510. await self._replay_session_history(state)
  511. except Exception:
  512. logger.warning(
  513. f"ACP history replay raised during session/{replay_verb} for %s — "
  514. f"{replay_verb} will still succeed, partial transcript may be missing",
  515. state.session_id, exc_info=True,
  516. )
  517. self._schedule_available_commands_update(state.session_id)
  518. self._schedule_soon(lambda: self._send_usage_update(state))
  519. return {
  520. "models": self._build_model_state(state),
  521. "modes": self._session_modes(state),
  522. "field_meta": self._provenance_meta(state.session_id, getattr(state.agent, "session_id", state.session_id)),
  523. }
  524. async def _attach_session_mcp(self, state: SessionState, mcp_servers: list | None, log: str, *log_args) -> None:
  525. await self._register_session_mcp_servers(state, mcp_servers)
  526. self._schedule_mcp_late_refresh(state)
  527. logger.info(log, *log_args)
  528. async def new_session(self, cwd: str, mcp_servers: list | None = None, **kwargs: Any) -> NewSessionResponse:
  529. # Agent construction (config, memory-provider import, SessionDB) is slow and fully
  530. # blocking; inline it froze the loop serving every JSON-RPC request (#58083).
  531. state = await asyncio.to_thread(self.session_manager.create_session, cwd=cwd)
  532. await self._attach_session_mcp(state, mcp_servers, "New session %s (cwd=%s)", state.session_id, cwd)
  533. return NewSessionResponse(session_id=state.session_id, **await self._session_response_fields(state))
  534. async def load_session(
  535. self, cwd: str, session_id: str, mcp_servers: list | None = None, **kwargs: Any
  536. ) -> LoadSessionResponse | None:
  537. state = await asyncio.to_thread(self.session_manager.update_cwd, session_id, cwd)
  538. if state is None:
  539. logger.warning("load_session: session %s not found", session_id)
  540. return None
  541. await self._attach_session_mcp(state, mcp_servers, "Loaded session %s", session_id)
  542. return LoadSessionResponse(**await self._session_response_fields(state, "load"))
  543. async def resume_session(
  544. self, cwd: str, session_id: str, mcp_servers: list | None = None, **kwargs: Any
  545. ) -> ResumeSessionResponse:
  546. state = await asyncio.to_thread(self.session_manager.update_cwd, session_id, cwd)
  547. if state is None:
  548. logger.warning("resume_session: session %s not found, creating new", session_id)
  549. state = await asyncio.to_thread(self.session_manager.create_session, cwd=cwd)
  550. await self._attach_session_mcp(state, mcp_servers, "Resumed session %s", state.session_id)
  551. return ResumeSessionResponse(**await self._session_response_fields(state, "resume"))
  552. async def cancel(self, session_id: str, **kwargs: Any) -> None:
  553. # get_session restores a not-in-memory id from the DB (full AIAgent build) and waits
  554. # on the restore lock — off the loop, like new/load/resume/fork (#58083).
  555. state = await asyncio.to_thread(self.session_manager.get_session, session_id)
  556. if not (state and state.cancel_event):
  557. return
  558. with state.runtime_lock:
  559. if state.is_running and state.current_prompt_text:
  560. state.interrupted_prompt_text = state.current_prompt_text
  561. # Cancel + hard-stop under the lock so no other prompt mistakes this turn for
  562. # redirectable work.
  563. state.cancel_event.set()
  564. try:
  565. if state.agent:
  566. request_hard_interrupt(state.agent) # this turn only: background delegations keep running
  567. except Exception:
  568. logger.debug("Failed to interrupt ACP session %s", session_id, exc_info=True)
  569. logger.info("Cancelled session %s", session_id)
  570. async def fork_session(
  571. self, cwd: str, session_id: str, mcp_servers: list | None = None, **kwargs: Any
  572. ) -> ForkSessionResponse:
  573. state = await asyncio.to_thread(self.session_manager.fork_session, session_id, cwd=cwd)
  574. if state is None:
  575. logger.info("Forked session %s -> %s", session_id, "")
  576. return ForkSessionResponse(session_id="")
  577. await self._register_session_mcp_servers(state, mcp_servers)
  578. logger.info("Forked session %s -> %s", session_id, state.session_id)
  579. self._schedule_available_commands_update(state.session_id)
  580. return ForkSessionResponse(
  581. session_id=state.session_id, models=self._build_model_state(state), modes=self._session_modes(state)
  582. )
  583. async def list_sessions(
  584. self, cursor: str | None = None, cwd: str | None = None, **kwargs: Any
  585. ) -> ListSessionsResponse:
  586. """``cursor`` is a ``session_id`` returned as ``next_cursor``; results resume after it
  587. (unknown cursor -> empty page, never the full list). Pages cap at the fixed size."""
  588. infos = self.session_manager.list_sessions(cwd=cwd)
  589. if cursor:
  590. for idx, s in enumerate(infos):
  591. if s["session_id"] == cursor:
  592. infos = infos[idx + 1:]
  593. break
  594. else:
  595. infos = []
  596. has_more = len(infos) > _LIST_SESSIONS_PAGE_SIZE
  597. sessions = [
  598. SessionInfo(
  599. session_id=s["session_id"], cwd=s["cwd"], title=s.get("title"),
  600. updated_at=None if s.get("updated_at") is None else str(s["updated_at"]),
  601. )
  602. for s in infos[:_LIST_SESSIONS_PAGE_SIZE]
  603. ]
  604. next_cursor = sessions[-1].session_id if has_more and sessions else None
  605. return ListSessionsResponse(sessions=sessions, next_cursor=next_cursor)
  606. # ---- Prompt (core) ------------------------------------------------------
  607. def _rewrite_prompt_for_interrupt(
  608. self, state: SessionState, user_text: str, user_content: Any, text_only: bool
  609. ) -> tuple[str, Any]:
  610. """Idle ``/steer`` has nothing to inject into (gateway parity): if a prompt was just
  611. cancelled, replay it with the steer text as explicit correction; otherwise run the steer
  612. payload as a plain prompt rather than silently queueing it as if ``/queue`` was typed.
  613. Plain text after a cancel likewise keeps the cancelled request attached ("stop and
  614. send" clients) so deictic follow-ups have a target."""
  615. if not (text_only and isinstance(user_content, str)):
  616. return user_text, user_content
  617. if user_text.startswith("/steer"):
  618. split = user_text.split(maxsplit=1)
  619. steer_text = split[1].strip() if len(split) > 1 else ""
  620. if not steer_text:
  621. return user_text, user_content
  622. idle, interrupted_prompt = _take_interrupted_prompt(state)
  623. if interrupted_prompt:
  624. return (_attach_interrupted_prompt(interrupted_prompt, steer_text),) * 2
  625. return (steer_text, steer_text) if idle else (user_text, user_content)
  626. if not user_text.startswith("/") and (interrupted_prompt := _take_interrupted_prompt(state)[1]):
  627. return (_attach_interrupted_prompt(interrupted_prompt, user_text),) * 2
  628. return user_text, user_content
  629. def _claim_turn_or_queue(
  630. self, state: SessionState, session_id: str, user_text: str, user_content: Any, text_only: bool
  631. ) -> str | None:
  632. """Mark the session running; if a turn is active, redirect it (text-only, supported
  633. runtime) or queue it. Returns the client message when absorbed, else None."""
  634. with state.runtime_lock:
  635. if not state.is_running and not state.command_op:
  636. state.is_running = True
  637. state.current_prompt_text = user_text or "[Image attachment]"
  638. return None
  639. # Redirect steers a live turn; a state-mutating command (command_op) has none.
  640. if state.is_running and text_only and isinstance(user_content, str) and hasattr(state.agent, "redirect") and (
  641. getattr(state.agent, "_supports_active_turn_redirect", False) is True
  642. ):
  643. try:
  644. if state.agent.redirect(user_content):
  645. return "Redirected the active turn with your correction."
  646. except Exception:
  647. logger.debug("ACP active-turn redirect failed for %s", session_id, exc_info=True)
  648. state.queued_prompts.append(user_text or "[Image attachment]")
  649. return f"Queued for the next turn. ({len(state.queued_prompts)} queued)"
  650. def _run_agent_turn(
  651. self, *, state: SessionState, session_id: str, user_text: str, user_content: Any, conn: Any,
  652. loop: asyncio.AbstractEventLoop, approval_cb: Any, edit_approval_requester: Any,
  653. ) -> dict:
  654. """Executor-thread body of one turn, run inside ``contextvars.copy_context()`` so
  655. ContextVar writes are isolated from concurrent sessions.
  656. Approval routing is thread-local, so it MUST be bound here, not on the loop thread.
  657. Interactive routing is a ``tools.approval`` contextvar, not ``HERMES_INTERACTIVE`` in
  658. os.environ, so concurrent workers can't race a global flag onto the non-interactive
  659. auto-approve path (GHSA-96vc-wcxf-jjff)."""
  660. agent = state.agent
  661. with contextlib.ExitStack() as stack:
  662. # HERMES_SESSION_KEY scopes per-session caches (interactive sudo password) to this
  663. # session, not the reused thread. ``cwd`` pins what the system prompt reports as the
  664. # working directory — otherwise it advertises the Hermes workspace while tools are
  665. # rooted at the client's project and edits land outside it. ``cron_session=""`` masks
  666. # any leaked process-global HERMES_CRON_SESSION.
  667. def _session_context() -> Callable[[], None]:
  668. from gateway.session_context import clear_session_vars, set_session_vars
  669. tokens = set_session_vars(
  670. session_key=session_id, session_id=session_id, cwd=state.cwd, cron_session="",
  671. )
  672. return lambda: clear_session_vars(tokens)
  673. def _approval() -> Callable[[], None]:
  674. from tools import terminal_tool
  675. previous = terminal_tool._get_approval_callback()
  676. terminal_tool.set_approval_callback(approval_cb)
  677. return lambda: terminal_tool.set_approval_callback(previous)
  678. def _edit_approval() -> Callable[[], None]:
  679. from acp_adapter.edit_approval import reset_edit_approval_requester, set_edit_approval_requester
  680. token = set_edit_approval_requester(edit_approval_requester)
  681. return lambda: reset_edit_approval_requester(token)
  682. _bind_guarded(stack, "session context", _session_context)
  683. if approval_cb:
  684. _bind_guarded(stack, "approval callback", _approval)
  685. if edit_approval_requester:
  686. _bind_guarded(stack, "edit approval requester", _edit_approval)
  687. stack.callback(reset_hermes_interactive_context, set_hermes_interactive_context(True))
  688. # Tools tag side-effects with the ACP session (``kanban_create``); save/restore it.
  689. stack.callback(_restore_env, "HERMES_SESSION_ID", os.environ.get("HERMES_SESSION_ID"))
  690. os.environ["HERMES_SESSION_ID"] = session_id
  691. # Auto-titling fires in the turn prologue; push the title now as a session-info update.
  692. def _notify_title_update(_title: str, _source: str) -> None:
  693. if conn:
  694. loop.call_soon_threadsafe(asyncio.create_task, self._send_session_info_update(session_id))
  695. agent._on_session_title = _notify_title_update
  696. try:
  697. return agent.run_conversation(
  698. user_message=user_content, conversation_history=state.history, task_id=session_id,
  699. persist_user_message=user_text or "[Image attachment]",
  700. )
  701. except Exception as e:
  702. logger.exception("Agent error in session %s", session_id)
  703. return {"final_response": f"Error: {e}", "messages": state.history}
  704. async def prompt(self, prompt: list[PromptBlock], session_id: str, **kwargs: Any) -> PromptResponse:
  705. """Run Hermes on the user's prompt and stream events back to the editor."""
  706. state = await asyncio.to_thread(self.session_manager.get_session, session_id)
  707. if state is None:
  708. logger.error("prompt: session %s not found", session_id)
  709. return PromptResponse(stop_reason="refusal")
  710. user_text = _extract_text(prompt).strip()
  711. user_content = _content_blocks_to_openai_user_content(prompt)
  712. text_only_prompt = all(isinstance(block, TextContentBlock) for block in prompt)
  713. if not user_text and not (isinstance(user_content, list) and user_content):
  714. return PromptResponse(stop_reason="end_turn")
  715. user_text, user_content = self._rewrite_prompt_for_interrupt(state, user_text, user_content, text_only_prompt)
  716. # Slash commands are text-only; a prompt with media goes to the agent even if it starts with "/".
  717. if text_only_prompt and isinstance(user_content, str) and user_text.startswith("/"):
  718. # Off the loop: /model validates through switch_model (network I/O) and /compress
  719. # calls the LLM; handlers are sync and hold no loop-bound state.
  720. response_text = await asyncio.to_thread(self._handle_slash_command, user_text, state)
  721. if response_text is not None:
  722. if self._conn:
  723. await self._conn.session_update(session_id, acp.update_agent_message_text(response_text))
  724. await self._send_usage_update(state)
  725. # A mutating command held command_op; prompts that arrived mid-op are queued.
  726. await self._drain_queued_prompts(state, session_id, self._conn)
  727. return PromptResponse(stop_reason="end_turn")
  728. absorbed = self._claim_turn_or_queue(state, session_id, user_text, user_content, text_only_prompt)
  729. if absorbed is not None:
  730. if self._conn:
  731. await self._conn.session_update(session_id, acp.update_agent_message_text(absorbed))
  732. return PromptResponse(stop_reason="end_turn")
  733. logger.info("Prompt on session %s: %s", session_id, user_text[:100])
  734. conn, loop = self._conn, asyncio.get_running_loop()
  735. if state.cancel_event:
  736. state.cancel_event.clear()
  737. cbs = self._wire_turn_callbacks(state, session_id, conn, loop)
  738. def _run_agent() -> dict:
  739. try:
  740. return self._run_agent_turn(
  741. state=state, session_id=session_id, user_text=user_text, user_content=user_content, conn=conn,
  742. loop=loop, approval_cb=cbs.approval_cb, edit_approval_requester=cbs.edit_approval_requester,
  743. )
  744. finally:
  745. # Still on the executor thread: ``_send_update`` blocks on the loop, so flushing
  746. # here (not after ``run_in_executor``) lands the update before the response.
  747. self._flush_turn_tool_calls(cbs, session_id, conn, loop)
  748. try:
  749. # ACP `session_id` is the stable handle; agent.session_id is the internal head that
  750. # compression may rotate — snapshot it to detect rotation after the turn.
  751. pre_turn_hermes_id = getattr(state.agent, "session_id", None)
  752. # Fresh context copy: concurrent sessions on the shared executor must not share ContextVars.
  753. ctx = contextvars.copy_context()
  754. result = await loop.run_in_executor(_executor, ctx.run, _run_agent)
  755. except Exception:
  756. logger.exception("Executor error for session %s", session_id)
  757. with state.runtime_lock:
  758. state.is_running = False
  759. state.current_prompt_text = ""
  760. return PromptResponse(stop_reason="end_turn")
  761. return await self._finish_turn(state, session_id, conn, result, pre_turn_hermes_id, cbs.streamed)
  762. def _flush_turn_tool_calls(
  763. self, cbs: _TurnCallbacks, session_id: str, conn: Any, loop: asyncio.AbstractEventLoop
  764. ) -> None:
  765. """Close any tool call the turn left open, so no bubble spins after the turn ends."""
  766. if not conn or cbs.tool_call_ids is None:
  767. return
  768. try:
  769. flush_open_tool_calls(conn, session_id, loop, cbs.tool_call_ids, cbs.tool_call_meta or {})
  770. except Exception:
  771. logger.debug("Could not flush open ACP tool calls for %s", session_id, exc_info=True)
  772. def _wire_turn_callbacks(
  773. self, state: SessionState, session_id: str, conn: Any, loop: asyncio.AbstractEventLoop
  774. ) -> _TurnCallbacks:
  775. """Install the ACP streaming callbacks on the session agent for one turn."""
  776. cbs = _TurnCallbacks()
  777. if conn:
  778. tool_call_ids: dict[str, deque[str]] = defaultdict(deque)
  779. tool_call_meta: dict[str, dict[str, Any]] = {}
  780. cbs.tool_call_ids, cbs.tool_call_meta = tool_call_ids, tool_call_meta
  781. # Shared with the step callback so a runtime that projects
  782. # ``tool.completed`` closes each call once, not twice.
  783. turn_state: dict[str, Any] = {}
  784. policy_getter = lambda: self._edit_approval_policy_for_state(state)
  785. cbs.tool_progress_cb = make_tool_progress_cb(
  786. conn, session_id, loop, tool_call_ids, tool_call_meta, edit_approval_policy_getter=policy_getter,
  787. turn_state=turn_state,
  788. )
  789. # Per-session allocator: a new turn must never reuse a previous turn's
  790. # assistant messageId (ACP clients replace the bubble with that id).
  791. if state.message_ids is None:
  792. state.message_ids = AssistantMessageIdAllocator()
  793. state.message_ids.close() # new turn -> next chunk opens a fresh id
  794. cbs.reasoning_cb = make_thinking_cb(conn, session_id, loop, state.message_ids)
  795. cbs.step_cb = make_step_cb(conn, session_id, loop, tool_call_ids, tool_call_meta, turn_state)
  796. message_cb = make_message_cb(conn, session_id, loop, state.message_ids)
  797. def stream_delta_cb(text: str) -> None:
  798. cbs.streamed = cbs.streamed or bool(text)
  799. message_cb(text)
  800. cbs.stream_delta_cb = stream_delta_cb
  801. # Closes the synthetic permission-request bubble once the user has answered.
  802. send_update = lambda update: _send_update(conn, session_id, loop, update)
  803. cbs.approval_cb = make_approval_callback(conn.request_permission, loop, session_id, send_update=send_update)
  804. try:
  805. from acp_adapter.edit_approval import make_acp_edit_approval_requester
  806. cbs.edit_approval_requester = make_acp_edit_approval_requester(
  807. conn.request_permission, loop, session_id, auto_approve_getter=policy_getter,
  808. send_update=send_update,
  809. )
  810. except Exception:
  811. logger.debug("Could not create ACP edit approval requester", exc_info=True)
  812. agent = state.agent
  813. agent.tool_progress_callback = cbs.tool_progress_cb
  814. # Thought panes get provider reasoning only — no local status updates, no fake accordion.
  815. agent.thinking_callback = None
  816. agent.reasoning_callback, agent.step_callback = cbs.reasoning_cb, cbs.step_cb
  817. agent.stream_delta_callback = cbs.stream_delta_cb
  818. return cbs
  819. async def _finish_turn(
  820. self, state: SessionState, session_id: str, conn: Any, result: dict, pre_turn_hermes_id: Any,
  821. streamed_message: bool,
  822. ) -> PromptResponse:
  823. """Persist, emit provenance/final text, drain queued prompts, report usage."""
  824. try:
  825. # Key presence, not truthiness: ``messages=[]`` is a legitimate cleared transcript (#10844);
  826. # only a result without the key leaves the history untouched.
  827. if "messages" in result and isinstance(result["messages"], list):
  828. state.history = result["messages"]
  829. self.session_manager.save_session(session_id)
  830. # Head rotated (compression split): emit provenance so clients can render the boundary.
  831. post_turn_hermes_id = getattr(state.agent, "session_id", None)
  832. if conn and post_turn_hermes_id and pre_turn_hermes_id and post_turn_hermes_id != pre_turn_hermes_id:
  833. try:
  834. await self._send_session_info_update(
  835. session_id, current_hermes_session_id=post_turn_hermes_id,
  836. previous_hermes_session_id=pre_turn_hermes_id,
  837. )
  838. except Exception:
  839. logger.debug("Could not emit ACP provenance update after rotation for %s", session_id, exc_info=True)
  840. final_response = result.get("final_response") or "" # None on an interrupted turn
  841. cancelled = bool(state.cancel_event and state.cancel_event.is_set())
  842. # The local "waiting for model" interrupt status is metadata, not prose; stop_reason carries it.
  843. from agent.conversation_loop import INTERRUPT_WAITING_FOR_MODEL_PREFIX
  844. interrupted = bool(result.get("interrupted")) or cancelled
  845. suppress = interrupted and final_response.startswith(INTERRUPT_WAITING_FOR_MODEL_PREFIX)
  846. # Send the final text unless already streamed — or if a plugin hook transformed it after.
  847. if final_response and conn and not suppress and (not streamed_message or result.get("response_transformed")):
  848. update = acp.update_agent_message_text(final_response)
  849. if state.message_ids is not None:
  850. # A plugin-rewritten reply replaces the streamed bubble (same id); an
  851. # unstreamed final response opens its own.
  852. if streamed_message and result.get("response_transformed"):
  853. update.message_id = state.message_ids.last() or state.message_ids.current()
  854. else:
  855. update.message_id = state.message_ids.current()
  856. state.message_ids.close()
  857. await conn.session_update(session_id, update)
  858. finally:
  859. # Go idle before draining so recursive prompt() calls can acquire the session.
  860. with state.runtime_lock:
  861. state.is_running = False
  862. state.current_prompt_text = ""
  863. await self._drain_queued_prompts(state, session_id, conn)
  864. usage = None
  865. if any(result.get(k) is not None for k in ("prompt_tokens", "completion_tokens", "total_tokens")):
  866. usage = Usage(
  867. input_tokens=result.get("prompt_tokens", 0), output_tokens=result.get("completion_tokens", 0),
  868. total_tokens=result.get("total_tokens", 0), thought_tokens=result.get("reasoning_tokens"),
  869. cached_read_tokens=result.get("cache_read_tokens"),
  870. )
  871. await self._send_usage_update(state)
  872. return PromptResponse(stop_reason="cancelled" if cancelled else "end_turn", usage=usage)
  873. async def _drain_queued_prompts(self, state: SessionState, session_id: str, conn: Any) -> None:
  874. """Run queued prompts while the session is idle. Reached from ``_finish_turn`` and
  875. from the slash path after a state-mutating command releases ``command_op``."""
  876. while True:
  877. with state.runtime_lock:
  878. if state.is_running or state.command_op or not state.queued_prompts:
  879. return
  880. next_prompt = state.queued_prompts.pop(0)
  881. if conn:
  882. await conn.session_update(session_id, acp.update_user_message_text(next_prompt))
  883. await self.prompt(prompt=[TextContentBlock(type="text", text=next_prompt)], session_id=session_id)
  884. # ---- Session settings (ACP protocol methods) -----------------------------
  885. async def set_session_model(self, model_id: str, session_id: str, **kwargs: Any) -> SetSessionModelResponse | None:
  886. """Switch the model for a session (called by ACP protocol)."""
  887. state = await asyncio.to_thread(self.session_manager.get_session, session_id)
  888. if state is None:
  889. logger.warning("Session %s: model switch requested for missing session", session_id)
  890. return None
  891. # The picker swaps state.agent wholesale; mid-turn that strands the running agent and
  892. # makes _finish_turn emit a spurious compression-rotation update. Same exclusion as
  893. # the /model slash command.
  894. with state.runtime_lock:
  895. if state.is_running or state.command_op:
  896. raise acp.RequestError(-32603, "Session is busy; switch models while the session is idle")
  897. state.command_op = True
  898. try:
  899. # switch_model() does synchronous network I/O (models.dev, custom-endpoint probes,
  900. # ~10 s cold) — off the loop, like the gateway, so other ACP sessions keep flowing.
  901. try:
  902. _old, requested_provider, resolved_model = await asyncio.to_thread(
  903. self._switch_model, state, model_id, keep_endpoint=True)
  904. except ModelRejected as exc:
  905. # A model no provider can serve is a bad ``modelId`` param (-32602), not an agent
  906. # internal error (-32603): the client attributes it to the request, not to Hermes (#72439).
  907. # Only the switch_model rejection maps here; a ValueError from the rebuild itself
  908. # (disabled provider, context window below the floor) stays on the -32603 path.
  909. from acp.exceptions import RequestError
  910. raise RequestError.invalid_params({"details": str(exc)}) from exc
  911. finally:
  912. with state.runtime_lock:
  913. state.command_op = False
  914. # Drain AFTER this response is queued, never inside it: a prompt that arrived
  915. # mid-switch would otherwise run a whole turn before the client sees the
  916. # (possibly failed) switch result.
  917. self._schedule_soon(lambda: self._drain_queued_prompts(state, session_id, self._conn))
  918. logger.info(
  919. "Session %s: model switched to %s via provider %s", session_id, resolved_model, requested_provider
  920. )
  921. return SetSessionModelResponse()
  922. async def set_session_mode(self, mode_id: str, session_id: str, **kwargs: Any) -> SetSessionModeResponse | None:
  923. """Persist the editor-requested mode so ACP clients do not fail on mode switches."""
  924. state = await asyncio.to_thread(self.session_manager.get_session, session_id)
  925. if state is None:
  926. logger.warning("Session %s: mode switch requested for missing session", session_id)
  927. return None
  928. normalized_mode = str(mode_id or "").strip()
  929. if normalized_mode not in self._MODES:
  930. normalized_mode = self._MODE_DEFAULT
  931. state.mode = normalized_mode
  932. self.session_manager.save_session(session_id)
  933. logger.info("Session %s: mode switched to %s", session_id, normalized_mode)
  934. return SetSessionModeResponse()
  935. async def set_config_option(
  936. self, config_id: str, session_id: str, value: str, **kwargs: Any
  937. ) -> SetSessionConfigOptionResponse | None:
  938. """Accept ACP config option updates even when Hermes has no typed ACP config surface yet."""
  939. state = await asyncio.to_thread(self.session_manager.get_session, session_id)
  940. if state is None:
  941. logger.warning("Session %s: config update requested for missing session", session_id)
  942. return None
  943. if str(config_id) == self._EDIT_APPROVAL_POLICY_CONFIG_ID:
  944. state.mode = self._EDIT_APPROVAL_POLICY_TO_MODE.get(str(value), self._MODE_DEFAULT)
  945. else:
  946. options = getattr(state, "config_options", None)
  947. if not isinstance(options, dict):
  948. options = {}
  949. options[str(config_id)] = value
  950. state.config_options = options
  951. self.session_manager.save_session(session_id)
  952. logger.info("Session %s: config option %s updated", session_id, config_id)
  953. return SetSessionConfigOptionResponse(config_options=[])