session_policy_refresh.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. """A resumed local session re-reads its config-derived settings (CFG-2); its MCP servers change the same way (CFG-3).
  2. Main's ``hermes -c`` was a new process: toolsets, MCP servers, ``terminal.backend``, ``max_turns``, checkpoints
  3. and the reasoning default came from the config it started with, while the stored session kept its model/provider.
  4. The owner keeps that line on ``session.resume``: it re-derives exactly those fields from the session profile's
  5. current config and commits them as a new policy generation. ``reload.mcp`` commits the MCP part alone for the
  6. calling session. The creation flags (request_json), the model route and a committed ``/reasoning`` or workspace
  7. pick stay; a session with a turn running or queued keeps the policy that turn was admitted under. A Bot Chat an
  8. earlier build committed frozen follows its profile from the open on (``follow_bot_chat``).
  9. """
  10. import asyncio
  11. import json
  12. import logging
  13. from dataclasses import asdict, replace
  14. from functools import partial
  15. from pathlib import Path
  16. logger = logging.getLogger(__name__)
  17. def _follow(target, live, key):
  18. if key in live:
  19. target[key] = live[key]
  20. else:
  21. target.pop(key, None)
  22. def refreshed_policy(policy, live, terminal, *, picked=(), mcp_only=False):
  23. """``(policy, secrets, replaced)``: *policy* with its config-derived fields from *live* (the profile's
  24. config) and *terminal* (its terminal scope), or only its MCP servers (*mcp_only*); explicit picks (launch
  25. flags, the *picked* policy edits) stay. *secrets* are the refreshed sections' private values, *replaced*
  26. the sections (``None``: terminal) whose stored private values they supersede. *policy* itself when
  27. nothing moved; a rotated private value alone (its redacted tree unchanged) moves it."""
  28. from gateway.session_policy import _extract_config_secrets, present_sections
  29. from gateway.session_policy_credentials import secrets_moved
  30. old, config = json.loads(policy.config_json), json.loads(policy.config_json)
  31. live, secrets, replaced = present_sections(live), {}, set()
  32. if mcp_only:
  33. toolsets, terminal = policy.toolsets, json.loads(policy.terminal_json)
  34. else:
  35. toolsets = _settings(policy, config, live, picked)
  36. terminal = _terminal(policy, terminal, picked)
  37. _extract_config_secrets(terminal, secrets, (None,))
  38. replaced.add(None)
  39. toolsets = _mcp_servers(policy, config, live, toolsets, secrets, replaced)
  40. if (config == old and toolsets == policy.toolsets and terminal == json.loads(policy.terminal_json)
  41. and not secrets_moved(policy.config_secret_ref, secrets, replaced)):
  42. return policy, {}, set()
  43. policy = replace(policy, toolsets=toolsets, config_json=json.dumps(config), terminal_json=json.dumps(terminal))
  44. return policy, secrets, replaced
  45. def _settings(policy, config, live, picked):
  46. """Follow the config-derived settings into *config*; the toolsets they select."""
  47. from gateway.session_policy import _resolve_toolsets, session_pinned
  48. request = json.loads(policy.request_json)
  49. live_agent = live.get('agent') or {}
  50. agent = dict(config.get('agent') or {})
  51. keys = [] if 'max_turns' in request else ['max_turns']
  52. # The session's own record (a pick, or a reset back to the profile) wins over the receipt history.
  53. chosen = session_pinned(config, 'reasoning', 'reasoning' in request or 'reasoning' in picked)
  54. keys += [] if chosen else ['reasoning_effort', 'reasoning_overrides']
  55. toolsets = policy.toolsets
  56. if 'toolsets' not in request and policy.editor_mcp_json is None:
  57. keys.append('disabled_toolsets')
  58. _follow(config, live, 'platform_toolsets')
  59. toolsets = tuple(sorted(_resolve_toolsets({}, live, policy.source, policy.safe_mode, policy.coding_posture)))
  60. for key in keys:
  61. _follow(agent, live_agent, key)
  62. if agent or 'agent' in config:
  63. config['agent'] = agent
  64. if 'max_turns' not in request:
  65. _follow(config, live, 'max_turns')
  66. _follow(config, live, 'checkpoints')
  67. if request.get('checkpoints') is True:
  68. section = config.get('checkpoints')
  69. section = dict(section) if isinstance(section, dict) else {'enabled': bool(section)} if section else {}
  70. config['checkpoints'] = {**section, 'enabled': True}
  71. return toolsets
  72. def _terminal(policy, terminal, picked):
  73. from gateway.session_policy import terminal_uses_host_cwd
  74. terminal, request = dict(terminal), json.loads(policy.request_json)
  75. # The session's own folder, where it now is, under build_policy's rule; a committed Move to project is
  76. # the user's pick on every backend (it rewrote the policy, never the launch request).
  77. if request.get('cwd_explicit') is True or 'workspace' in picked or terminal_uses_host_cwd(terminal):
  78. terminal['TERMINAL_CWD'] = policy.cwd
  79. return terminal
  80. def _mcp_servers(policy, config, live, toolsets, secrets, replaced):
  81. """The profile's MCP servers as this session's own selection takes them now (its explicit pick or platform
  82. allowlist, ``no_mcp`` and disabled entries respected): their definitions into *config*, private values split
  83. off as build_policy does (a managed worker binds this config, so it connects exactly these), and their
  84. names into the returned toolsets. An editor-MCP session keeps its frozen servers."""
  85. from gateway.session_policy import _extract_config_secrets, _resolve_toolsets
  86. from hermes_cli.tools_config import enabled_mcp_server_names
  87. from hermes_state_runtime import RuntimeStoreError
  88. if policy.editor_mcp_json is not None:
  89. return toolsets
  90. request = json.loads(policy.request_json)
  91. sections = [section if isinstance(section, dict) else {} for section in (config.get('mcp_servers'), live.get('mcp_servers'))]
  92. pick, picked, enabled = request.get('toolsets'), set(), enabled_mcp_server_names(live)
  93. if pick is not None:
  94. # The launch pick's servers are an allowlist: each stays only while config enables it, and once all
  95. # are gone the chat has none, never every enabled server. A server that is now disabled or removed
  96. # leaves the pick before it is re-validated (it no longer validates, and keeping the frozen
  97. # definitions would start the server the user turned off).
  98. known = set(sections[0]) | set(sections[1])
  99. server = partial(_server_name, known)
  100. picked = {server(name) for name in pick if isinstance(name, str) and server(name) in known}
  101. pick = [name for name in pick if not isinstance(name, str) or server(name) not in picked - enabled]
  102. try:
  103. selected = _resolve_toolsets({} if pick is None else {'toolsets': pick},
  104. json.loads(json.dumps(live)), policy.source, policy.safe_mode, policy.coding_posture)
  105. except RuntimeStoreError:
  106. return toolsets # the rest of the launch pick no longer validates against the profile: keep the frozen servers
  107. # A picked server config removed keeps a disabled stub in the stored section, so every later refresh
  108. # (after an owner restart too) still knows that name as one of the pick's servers, not an unknown toolset.
  109. stubs = {name: {'enabled': False} for name in picked - set(sections[1])}
  110. if 'mcp_servers' in live or stubs:
  111. config['mcp_servers'] = json.loads(json.dumps({**sections[1], **stubs} if stubs else live['mcp_servers']))
  112. _extract_config_secrets(config['mcp_servers'], secrets, ('mcp_servers',))
  113. else:
  114. config.pop('mcp_servers', None)
  115. replaced.add('mcp_servers')
  116. servers = selected & enabled & picked if picked else selected & enabled
  117. return tuple(sorted((set(toolsets) - set(sections[0]) - set(sections[1])) | servers))
  118. def _server_name(servers, name):
  119. """The server a pick name selects: ``mcp-<server>`` is its bare alias unless a server has that name."""
  120. return name[4:] if name.startswith('mcp-') and name not in servers else name
  121. def _prepare(authority, session_id, mcp_only=False, adopt_only=False):
  122. """Off the owner loop: the stored policy, its refresh from this profile home's files, and the generation
  123. it was derived at. None when the session takes no refresh or nothing moved. *adopt_only*: the adoption
  124. of a frozen Bot Chat alone, without the config-derived settings (a later open on the same attachment)."""
  125. from gateway.session_local import load_profile_config
  126. from gateway.session_policy import SURFACES, restore_policy
  127. from hermes_state_local import local_receipt
  128. from hermes_state_mutation_prepared import policy_picks
  129. from tools.terminal_scope import build_profile_terminal_scope
  130. generation = authority.db.get_session(session_id)['runtime_generation']
  131. stored = local_receipt(authority.db, session_id)['policy']
  132. old = restore_policy(stored)
  133. # A bypass launch never reads the profile; cron, Kanban and room routes are not resumed CLI sessions.
  134. if old.ignore_user_config or old.source not in SURFACES:
  135. return None
  136. home = Path(authority.db.db_path).resolve().parent
  137. terminal = {} if mcp_only or adopt_only else build_profile_terminal_scope(home)
  138. live, picked = load_profile_config(home), policy_picks(authority.db, session_id)
  139. base = old
  140. if not mcp_only:
  141. # Opening a Bot Chat an earlier build froze adopts it: it follows its profile from now on.
  142. from gateway.session_local_migration import follow_bot_chat
  143. base = follow_bot_chat(authority, session_id, old, live, picked)
  144. if adopt_only:
  145. return None if base is old else (stored, old, base, {}, set(), generation)
  146. policy, secrets, replaced = refreshed_policy(base, live, terminal, picked=picked, mcp_only=mcp_only)
  147. return None if policy is old else (stored, old, policy, secrets, replaced, generation)
  148. def _rebind(authority, session_id, old, policy, fresh, replaced):
  149. """Re-bind the private values like a policy mutation: policy identity covers config and terminal, and the
  150. refreshed sections' private values are the profile's current ones (the stored ones there are neither
  151. re-read nor checked: a rotated key is what the refresh picks up, not a revoked credential). A policy
  152. that follows its profile from this open on keeps none of its followed sections' frozen values."""
  153. from gateway.session_policy import FOLLOW_SECTIONS, bind_launch_key, launch_key
  154. from gateway.session_policy_credentials import recover_config_secrets
  155. skip = (*(FOLLOW_SECTIONS if policy.follows_profile else ()), *replaced)
  156. secrets = recover_config_secrets(authority, old, skip) if old.config_secret_ref else {}
  157. secrets.update(fresh)
  158. return bind_launch_key(authority, session_id, policy, launch_key(authority, old), config_secrets=secrets)
  159. def _pending_adoptions(authority):
  160. """Frozen Bot Chats an open found to adopt but could not commit (a turn running or queued, a concurrent
  161. edit): the next open retries, whether or not that client is already attached."""
  162. found = getattr(authority, '_pending_adoptions', None)
  163. if found is None:
  164. found = authority._pending_adoptions = set()
  165. return found
  166. def _in_flight(authority):
  167. found = getattr(authority, '_policy_refreshes', None)
  168. if found is None:
  169. found = authority._policy_refreshes = {}
  170. return found
  171. async def refresh_settled(authority, session_id):
  172. """Wait out a refresh of *session_id* in flight. Its commit lands before its terminal cleanup (a sandbox
  173. teardown can take 10-15 s) and its publication; an attach meanwhile would find the stored policy ahead
  174. of the live one, which restore_local_session refuses."""
  175. refreshes = _in_flight(authority)
  176. while (running := refreshes.get(session_id)) is not None and not running.done():
  177. logger.debug('Session %s waits for its policy refresh to publish', session_id)
  178. await asyncio.wait({running}) # its outcome is its caller's; a cancelled waiter leaves it running
  179. async def commit_refresh(authority, ref, *, mcp_only=False, adopt_only=False):
  180. """Commit an idle local session's refresh as its next policy generation: True when one was committed,
  181. False when nothing moved. Raises the store's refusal (``session_busy`` while a turn runs or waits).
  182. One refresh per session at a time, run to its publication even when the caller is cancelled: a second
  183. waits for the first, then usually finds nothing moved."""
  184. from gateway.session_runtime_workers import track_mutation
  185. await refresh_settled(authority, ref.session_id)
  186. refreshes = _in_flight(authority)
  187. task = refreshes[ref.session_id] = track_mutation(authority, _commit_refresh(authority, ref, mcp_only, adopt_only))
  188. task.add_done_callback(lambda done: refreshes.pop(ref.session_id, None) if refreshes.get(ref.session_id)
  189. is done else None)
  190. return await asyncio.shield(task)
  191. async def _commit_refresh(authority, ref, mcp_only, adopt_only=False):
  192. from gateway.session_runtime_workers import tracked_write
  193. from hermes_state_mutation_prepared import commit_refreshed_policy
  194. prepared = await asyncio.to_thread(_prepare, authority, ref.session_id, mcp_only, adopt_only)
  195. # An MCP-only refresh never runs follow_bot_chat: it neither settles nor commits an adoption, so it
  196. # leaves a pending one for the next open.
  197. pending = _pending_adoptions(authority) if not mcp_only else set()
  198. if prepared is None:
  199. pending.discard(ref.session_id)
  200. return False
  201. stored, old, policy, secrets, replaced, generation = prepared
  202. if policy.follows_profile and not old.follows_profile:
  203. pending.add(ref.session_id) # until this commit lands
  204. moved = json.loads(policy.terminal_json) != json.loads(old.terminal_json)
  205. policy = _rebind(authority, ref.session_id, old, policy, secrets, replaced)
  206. # Compare-and-swap against the receipt as stored (JSON lists), not its restored tuple form. The
  207. # publication is the commit's own shielded follow-up: a caller cancelled mid-commit cannot leave the
  208. # stored policy ahead of the live one (restore_local_session refuses that pair).
  209. await tracked_write(authority, partial(commit_refreshed_policy, authority.db, epoch=authority.epoch,
  210. session_id=ref.session_id, old=stored, new=asdict(policy),
  211. generation=generation),
  212. then=lambda _generation: _publish(authority, ref, moved))
  213. pending.discard(ref.session_id)
  214. return True
  215. async def refresh_resumed_policy(authority, ref, *, attached=False):
  216. """Commit the config refresh of an idle local session before it is attached. Never fails the resume:
  217. a busy session, a revoked launch credential or an unreadable profile keeps the stored policy. Only a turn
  218. running or queued (or admitted during the commit) is expected; anything else is a WARNING.
  219. *attached*: this client already holds the session (Desktop's warm reopen). The settings refresh is once
  220. per attachment, but a frozen Bot Chat whose adoption a busy open refused is adopted on any later open."""
  221. if attached and ref.session_id not in _pending_adoptions(authority):
  222. return
  223. try:
  224. await (commit_refresh(authority, ref, adopt_only=True) if attached else commit_refresh(authority, ref))
  225. except Exception as exc: # health: allow BLE001 -- a refresh is best-effort; the resume itself must proceed
  226. reason = getattr(exc, 'reason', None) or type(exc).__name__
  227. busy = reason in {'session_busy', 'unknown_execution', 'revision_conflict'}
  228. logger.log(logging.INFO if busy else logging.WARNING, 'Session %s keeps its stored policy on resume: %s',
  229. ref.session_id, reason)
  230. def drop_session_terminal(authority, ref):
  231. """The session's own cached terminal environments (keyed by its route, or its ids under per-session
  232. isolation): the terminal tool reuses one by key whatever backend built it. A profile-shared one stays.
  233. Its recorded cwd (keyed by its route or task id) goes too: it wins over the new binding's directory."""
  234. from gateway.session_authorities import owner_scope
  235. from hermes_state_local import local_receipt
  236. from tools.terminal_tool import _qualify_task_key, clear_session_cwd
  237. from tools.terminal_tool_lifecycle import cleanup_vm
  238. ids = {ref.session_id, local_receipt(authority.db, ref.session_id)['entry']['session_id']}
  239. route = authority.sessions[ref.session_id].route
  240. from gateway.session_policy import unbind_turn_workspace
  241. with owner_scope(authority):
  242. keys = {_qualify_task_key(key) for key in (f'session:{route}', *ids)}
  243. for key in (route, *ids):
  244. # The turn's workspace binding goes with them: it wins over the new binding's cwd too.
  245. unbind_turn_workspace(key)
  246. clear_session_cwd(key)
  247. for key in keys:
  248. cleanup_vm(key)
  249. async def _publish(authority, ref, moved=False):
  250. from gateway.session_local import local_session_info, publish_local_policy
  251. if moved: # before the drain can run a turn on the new binding with the old environment
  252. await asyncio.to_thread(drop_session_terminal, authority, ref)
  253. publish_local_policy(authority, ref.session_id)
  254. # A drain that ran during the off-loop commit saw the new stored policy beside the old live one and
  255. # paused; with both equal again, anything admitted meanwhile runs.
  256. authority._schedule(ref)
  257. # Every viewer already attached adopts the new generation (its next fenced edit would otherwise be
  258. # refused as stale) and repaints from the refreshed policy, as a reasoning/workspace edit publishes.
  259. authority._publish_pending(ref)
  260. authority.sessions[ref.session_id].event_stream.publish(
  261. ref.session_id, local_session_info(authority, ref), event_type='session.info')