session_execution.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. """Extracted gateway session_execution responsibility; consumed by TurnRunner."""
  2. from __future__ import annotations
  3. import asyncio
  4. import dataclasses
  5. import json
  6. import logging
  7. import queue
  8. import re
  9. import threading
  10. import time
  11. from contextlib import suppress
  12. from datetime import datetime
  13. from typing import TYPE_CHECKING, Any, Dict, List, Optional
  14. from agent.interrupt_compat import _accepts_keyword
  15. from agent.replay_cleanup import strip_stale_dangerous_confirmations
  16. from gateway.config import Platform
  17. from gateway.media_repair import repair_explicit_computer_use_media_paths
  18. from gateway.platforms.base import BasePlatformAdapter
  19. from gateway.session_api_turn import api_runtime_locked
  20. from gateway.turn_context import TurnContext
  21. from hermes_cli.config import cfg_get
  22. from utils import is_truthy_value
  23. if TYPE_CHECKING: # string annotations only; never imported at runtime (cycle)
  24. from gateway.run import GatewayRunner
  25. # Log-record parity with the origin module.
  26. logger = logging.getLogger("gateway.run")
  27. class GatewaySessionAgentMixin:
  28. # ── agent resolution (cache reuse vs fresh build) ───────────────────────────────────────
  29. @dataclasses.dataclass
  30. class _CachedAgentLookup:
  31. agent: Any = None
  32. reused: bool = False
  33. evicted: Any = None # agent evicted under the lock; released off-lock on a daemon thread
  34. def _skip_context_files(self, platform_key) -> bool:
  35. """gateway.platforms.<plat>.skip_context_files: messaging platforms may opt out of
  36. filesystem-heavy context-file discovery (SOUL.md, AGENTS.md, .cursorrules)."""
  37. from gateway.session_policy import policy_for_source
  38. policy = policy_for_source(self._runner, self._ctx.source)
  39. if policy:
  40. return policy.ignore_rules
  41. platforms_cfg = (self._ctx.user_config.get("gateway") or {}).get("platforms") or {}
  42. # ``hermes gateway setup`` writes ``gateway.platforms`` as a LIST of enabled platform names,
  43. # not a dict; treat any non-dict shape as "no per-platform overrides" rather than crashing.
  44. if not isinstance(platforms_cfg, dict):
  45. return False
  46. return bool((platforms_cfg.get(platform_key) or {}).get("skip_context_files"))
  47. def _cached_sid_is_dead(self, cache_lock, cache) -> tuple:
  48. """(peeked cached session_id, is_dead) — checked OUTSIDE the cache lock. "cached sid != current
  49. sid" normally means an intentional switch (reuse), but the routing-key self-heal yields the same
  50. shape with an agent bound to a DEAD session; reusing it re-binds the dead sid and loops."""
  51. ctx = self._ctx
  52. peek_sid = None
  53. if cache_lock and cache is not None:
  54. with cache_lock:
  55. entry = cache.get(ctx.session_key)
  56. if entry and len(entry) > 3:
  57. peek_sid = entry[3]
  58. dead = False
  59. if peek_sid is not None and ctx.session_id is not None and peek_sid != ctx.session_id:
  60. # The cache is keyed by session_key, so the snapshot's row lives in that key's profile
  61. # store; its id is no longer in the routing index once the self-heal moved the key on.
  62. with suppress(Exception):
  63. dead = self._runner.session_store._is_session_ended_in_db(peek_sid, session_key=ctx.session_key)
  64. return peek_sid, dead
  65. def _current_message_count(self):
  66. """Cross-process write guard input: the session's current DB message_count (or None)."""
  67. ctx = self._ctx
  68. if self._runner._session_db is None or not ctx.session_id:
  69. return None
  70. count = None
  71. with suppress(Exception):
  72. # run_sync is off-loop (executor); sync DB is fine.
  73. row = self._runner._session_db._db.get_session(ctx.session_id)
  74. if row:
  75. count = row.get("message_count", 0)
  76. return count
  77. def _pop_cached_agent_for_eviction(self):
  78. """Evict under the lock but DEFER release (release_clients can block on memory-provider /
  79. socket teardown while the idle sweeper waits on this lock). The turn rebuilds a fresh agent, so
  80. the caller does a SOFT release that keeps sandbox / browser / bg processes."""
  81. from gateway.run import _AGENT_PENDING_SENTINEL
  82. evicted = self._runner._agent_cache.pop(self._ctx.session_key, None)
  83. agent = evicted[0] if isinstance(evicted, tuple) and evicted else None
  84. return agent if agent and agent is not _AGENT_PENDING_SENTINEL else None
  85. def _lookup_cached_agent(self, sig, cache_lock, cache, max_iterations, peek_sid, dead, msg_count):
  86. ctx = self._ctx
  87. out = self._CachedAgentLookup()
  88. if not (cache_lock and cache is not None):
  89. return out
  90. with cache_lock:
  91. cached = cache.get(ctx.session_key)
  92. if not (cached and cached[1] == sig):
  93. return out
  94. # cached[2] = message_count at cache time (stale when a second process appended rows);
  95. # cached[3] = the session_id the snapshot was taken for.
  96. cached_mc = cached[2] if len(cached) > 2 else None
  97. cached_sid = cached[3] if len(cached) > 3 else None
  98. # Same session_key, other conversation: the counts track DIFFERENT DB rows, so the
  99. # comparison is meaningless — REUSE rather than bust the prompt cache on every switch.
  100. sid_mismatch = cached_sid is not None and ctx.session_id is not None and cached_sid != ctx.session_id
  101. # Re-validate the outside-lock dead-session peek against the tuple read under THIS lock:
  102. # a stale "dead" verdict must never be applied to a different (possibly live) agent.
  103. if sid_mismatch and dead and cached_sid == peek_sid:
  104. logger.info(
  105. "Agent cache invalidated for session %s: "
  106. "cached agent's session_id %s is ended in "
  107. "state.db (stale self-heal artifact, "
  108. "#54878 x #54947) — discarding instead of "
  109. "reusing across the routing recovery", ctx.session_key, cached_sid,
  110. )
  111. elif not sid_mismatch and cached_mc is not None and msg_count is not None and msg_count != cached_mc:
  112. logger.info(
  113. "Agent cache invalidated for session %s: "
  114. "message_count changed (%s -> %s), "
  115. "possible cross-process write", ctx.session_key, cached_mc, msg_count,
  116. )
  117. else:
  118. out.agent = cached[0]
  119. # Refresh LRU order so cap enforcement evicts truly-oldest entries.
  120. if hasattr(cache, "move_to_end"):
  121. with suppress(KeyError):
  122. cache.move_to_end(ctx.session_key)
  123. self._runner._init_cached_agent_for_turn(out.agent, ctx._interrupt_depth)
  124. # Cached agent may have been created with old config.
  125. out.agent.max_iterations = max_iterations
  126. logger.debug("Reusing cached agent for session %s", ctx.session_key)
  127. out.reused = True
  128. return out
  129. out.evicted = self._pop_cached_agent_for_eviction()
  130. return out
  131. def _release_evicted_agent(self, agent) -> None:
  132. """Off-lock soft release on a daemon thread so teardown never blocks the gateway loop."""
  133. self._runner._spawn_release_thread(
  134. self._runner._release_evicted_agent_soft, (agent,), f"agent-xproc-evict-{str(self._ctx.session_key)[:24]}",
  135. inline_fallback=True,
  136. )
  137. def _build_fresh_agent(self, turn_route, platform_key, combined_ephemeral, max_iterations,
  138. reasoning_config, pr, skip_context_files):
  139. from gateway.run import _checkpoint_agent_kwargs
  140. ctx = self._ctx
  141. runner = self._runner
  142. src = ctx.source
  143. from gateway.session_authorities import active_authority
  144. from gateway.session_policy import policy_for_source
  145. from gateway.session_worker_construct import session_prefill
  146. policy = policy_for_source(runner, src)
  147. return ctx.AIAgent(
  148. model=turn_route["model"], **turn_route["runtime"], **_checkpoint_agent_kwargs(ctx.user_config),
  149. max_iterations=max_iterations, quiet_mode=not bool(policy and policy.verbose), verbose_logging=False,
  150. enabled_toolsets=ctx.enabled_toolsets, disabled_toolsets=ctx.disabled_toolsets,
  151. ephemeral_system_prompt=combined_ephemeral or None,
  152. prefill_messages=(session_prefill(active_authority(runner), policy, src.chat_id) if policy
  153. else runner._prefill_messages) or None,
  154. reasoning_config=reasoning_config, service_tier=runner._service_tier,
  155. request_overrides=turn_route.get("request_overrides"),
  156. providers_allowed=pr.get("only"), providers_ignored=pr.get("ignore"), providers_order=pr.get("order"),
  157. provider_sort=pr.get("sort"), provider_require_parameters=pr.get("require_parameters", False),
  158. provider_data_collection=pr.get("data_collection"),
  159. session_id=ctx.session_id, platform=platform_key,
  160. user_id=src.user_id, user_id_alt=src.user_id_alt, user_name=src.user_name,
  161. chat_id=src.chat_id, chat_name=src.chat_name, chat_type=src.chat_type, thread_id=src.thread_id,
  162. gateway_session_key=ctx.session_key,
  163. session_db=getattr(runner._session_db, "_db", runner._session_db),
  164. # Reload from disk — do not reuse the startup snapshot.
  165. # See #60955.
  166. fallback_model=None if api_runtime_locked() else self._runner._refresh_fallback_model(),
  167. skip_context_files=skip_context_files,
  168. # Keep the persona even with minimal context: soul identity is one small file.
  169. load_soul_identity=not bool(policy and policy.ignore_rules),
  170. skip_memory=bool(policy and policy.ignore_rules),
  171. pass_session_id=bool(policy and policy.pass_session_id),
  172. # A local session's frozen budget (`--run-budget` / its profile's agent.run_budget_seconds);
  173. # None leaves the agent reading the owner's config, as before.
  174. run_budget_seconds=policy.run_budget_seconds if policy else None,
  175. )
  176. def _resolve_turn_agent(self, turn_route, platform_key, combined_ephemeral, max_iterations, reasoning_config, pr):
  177. """Reuse this session's cached AIAgent (frozen system prompt + tool schemas → prompt cache
  178. hits) or build a fresh one. Returns (agent, reused_cached_agent)."""
  179. ctx = self._ctx
  180. runner = self._runner
  181. skip_context_files = self._skip_context_files(platform_key)
  182. sig = runner._agent_config_signature(
  183. turn_route["model"], turn_route["runtime"], ctx.enabled_toolsets, combined_ephemeral,
  184. cache_keys=runner._extract_cache_busting_config(ctx.user_config),
  185. user_id=getattr(ctx.source, "user_id", None),
  186. user_id_alt=getattr(ctx.source, "user_id_alt", None),
  187. skip_context_files=skip_context_files,
  188. )
  189. cache_lock = getattr(runner, "_agent_cache_lock", None)
  190. cache = getattr(runner, "_agent_cache", None)
  191. peek_sid, dead = self._cached_sid_is_dead(cache_lock, cache)
  192. msg_count = self._current_message_count()
  193. found = self._lookup_cached_agent(sig, cache_lock, cache, max_iterations, peek_sid, dead, msg_count)
  194. agent = found.agent
  195. # Lock released — refresh the reused agent's fallback chain from disk OUTSIDE the cache lock
  196. # (disk I/O under the lock stalls the idle-sweep watcher and Discord heartbeats). A chain
  197. # configured after caching must reach the next turn; per-session serialization keeps it safe.
  198. locked = api_runtime_locked() is not None
  199. if found.reused and agent is not None and locked and getattr(agent, "_fallback_activated", False):
  200. # A cached agent already moved onto a fallback provider cannot serve a confirmed lock.
  201. found.evicted, found.reused, agent = agent, False, None
  202. with cache_lock:
  203. cache.pop(ctx.session_key, None)
  204. if found.reused and agent is not None:
  205. self._runner._apply_fallback_chain_to_agent(agent, None if locked else runner._refresh_fallback_model())
  206. if found.evicted is not None:
  207. self._release_evicted_agent(found.evicted)
  208. if agent is None:
  209. from tools.delegate_tool_config import gateway_delegation_inline, gateway_delegation_inline_scope
  210. # delegation.gateway_mode from this turn's own (profile-scoped) config and the chat's resolved interim
  211. # setting (never muted for a heartbeat turn, which shares the user's session and its cached agent);
  212. # read once, by the agent's first tool build, then carried by its delegate_task schema and pin.
  213. inline = gateway_delegation_inline(
  214. ctx.user_config, platform_key, interim_enabled=ctx.interim_assistant_messages_enabled)
  215. with gateway_delegation_inline_scope(inline):
  216. agent = self._build_fresh_agent(
  217. turn_route, platform_key, combined_ephemeral, max_iterations, reasoning_config, pr,
  218. skip_context_files,
  219. )
  220. if cache_lock and cache is not None:
  221. with cache_lock:
  222. # Record the snapshot's session_id with message_count so the cross-process guard
  223. # can skip the meaningless count comparison if the active session_id switches.
  224. cache[ctx.session_key] = (agent, sig, msg_count, ctx.session_id)
  225. runner._enforce_agent_cache_cap()
  226. logger.debug("Created new agent for session %s (sig=%s)", ctx.session_key, sig)
  227. return agent, found.reused