session_mutation_model.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. """Resolve explicit model changes using the existing source model switch pipeline."""
  2. import asyncio
  3. import json
  4. from dataclasses import asdict, replace
  5. from typing import NamedTuple
  6. from gateway.session_route_identity import ROUTE_BOUND_KEYS
  7. from hermes_state_runtime import RuntimeStoreError
  8. class _Route(NamedTuple):
  9. provider: str
  10. model: str
  11. base_url: str
  12. api_key: object # a callable bearer (Azure Entra) stays callable
  13. def _current_route(authority, live, old):
  14. """``(config, route)`` the session runs now, both from ONE read of the owning profile.
  15. A following session runs the profile's current route, so its frozen creation endpoint beside
  16. the current key would send that key to a host the profile left. Main switched from the live
  17. runtime's whole tuple (``tui_gateway.model_switch._current_model_runtime``). The provider is
  18. the configured name: a named ``providers:`` entry resolves to the bare ``custom`` class, and a
  19. pick on the same entry must not read as a route change. Only the session's own route counts:
  20. a fallback is where a turn lands, not what the user switches from. A route that cannot
  21. authenticate here must not block leaving it: like main's agent-less ``--provider`` path, the
  22. pick then switches from the configured provider alone and resolves its own target.
  23. """
  24. from gateway.session_policy import profile_read_scope
  25. from hermes_cli.auth import AuthError
  26. with profile_read_scope(old, authority):
  27. config = old.config(authority)
  28. block = config.get('model') or {}
  29. try:
  30. model, runtime = authority.runner._resolve_session_agent_runtime(
  31. source=live.source, session_key=live.route, fallback=False)
  32. except AuthError:
  33. return config, _Route(block.get('provider') or '', old.current_model(authority) or '',
  34. block.get('base_url') or '', '')
  35. return config, _Route(block.get('provider') or runtime.get('provider') or '', model or '',
  36. runtime.get('base_url') or '', runtime.get('api_key') or '')
  37. async def prepare_model(authority, live, payload, prepared):
  38. from gateway.session_policy import restore_policy, bind_launch_key, bound_route_holds, route_launch_key
  39. from gateway.session_policy_credentials import recover_config_secrets
  40. from hermes_cli.model_switch import apply_model_selection, switch_model
  41. from hermes_cli.config import get_compatible_custom_providers
  42. old = restore_policy(prepared['snapshot']['receipt']['policy'])
  43. config, current = _current_route(authority, live, old)
  44. reset = is_model_reset(payload)
  45. if reset:
  46. payload = await asyncio.to_thread(_reset_payload, authority, old, current)
  47. if 'status' in payload:
  48. return payload
  49. # A pick is an explicit user action: it resolves against the owning profile's CURRENT named
  50. # providers, as CLI/TUI ``/model`` do (#139740). A following session's ``config`` already is
  51. # the profile's current read; every other session keeps ``config`` for its own route below.
  52. resolution = config if old.follows_profile else await asyncio.to_thread(
  53. _provider_resolution_config, authority, config, old.ignore_user_config)
  54. def switch(**kwargs):
  55. result = switch_model(**kwargs)
  56. return result, _alias_credential(authority, result)
  57. result, alias_credential = await asyncio.to_thread(switch, raw_input=payload['model'],
  58. current_provider=current.provider, current_model=current.model, current_base_url=current.base_url,
  59. current_api_key=current.api_key, explicit_provider=payload.get('provider', ''),
  60. is_global=False, user_providers=resolution.get('providers'),
  61. custom_providers=get_compatible_custom_providers(resolution))
  62. if not result.success:
  63. raise RuntimeStoreError('model_resolution_failed')
  64. # The configured default is the user's own choice: a reset is never held behind a cost ask.
  65. refusal = None if reset else await _selection_guard_refusal(
  66. authority, live, payload, prepared['snapshot'], current, result)
  67. if refusal is not None:
  68. return refusal
  69. frozen = old.config()
  70. # The committed selection freezes the ONE picked endpoint (its key stays a private secret).
  71. provider_secrets, retired, moved = ({}, (), {}) if old.follows_profile else _freeze_selected_provider(
  72. resolution, frozen, result.target_provider)
  73. if old.follows_profile:
  74. # A following session switches FROM the profile's current route: measured from its creation
  75. # endpoint, a pick on the current one read as a route change and dropped its key_env pointer.
  76. followed = config.get('model') or {}
  77. # An inline key (or an alias key's pointer) authenticates the route it was picked on: once the
  78. # followed route moved, the same-route cleanup below would keep it for the new endpoint.
  79. held = bound_route_holds(frozen.get('model'), frozen, followed, config)
  80. # Every other route-bound key follows the profile's current route.
  81. kept = ('api_key', 'api', 'api_key_from') if held else ()
  82. route_keys = tuple(k for k in ROUTE_BOUND_KEYS if k not in kept)
  83. frozen['model'] = {k: v for k, v in (frozen.get('model') or {}).items() if k not in route_keys}
  84. # The profile's inline key and its private-secret pointer are not copied into the receipt:
  85. # its secrets stay at the profile's own path, never re-committed under this session.
  86. frozen['model'].update({k: followed[k] for k in route_keys
  87. if k in followed and k not in ('api_key', 'api', 'api_key_from')})
  88. # The one selection shape config.yaml gets (#25106): api_mode follows the target, a context pin
  89. # and the endpoint-bound credential fields (inline key, key_env/api_key_env) drop on a route change.
  90. # Off-loop: the context-pin check can do cold-start disk I/O.
  91. frozen['model'] = await asyncio.to_thread(apply_model_selection, frozen.get('model'), result)
  92. if 'reasoning' in payload:
  93. # The level picked with the model lands in the same commit (as prepare_reasoning writes it).
  94. from gateway.session_policy import pin_reasoning_effort
  95. pin_reasoning_effort(frozen, payload['reasoning'], session=True)
  96. if old.follows_profile:
  97. # A committed /model holds while the profile still targets what it targets now (main's
  98. # composer_override_profile); a later profile edit supersedes it.
  99. from pathlib import Path
  100. from gateway.run import _load_gateway_config
  101. from gateway.session_policy import present_sections, profile_target
  102. live = await asyncio.to_thread(
  103. lambda: _load_gateway_config(Path(authority.db.db_path).resolve().parent / 'config.yaml'))
  104. frozen['model']['selection_profile'] = list(profile_target(present_sections(live)))
  105. # A direct alias's own credential is part of the selection (never ``result.api_key``, which the
  106. # switch resolved for its probe): its env pointer is committed and re-read every turn; a literal
  107. # key is a private config secret at the alias entry's own path (what a restarted owner re-reads)
  108. # and the receipt keeps only a pointer to it, which config() projects into ``model.api_key``.
  109. key_env, source, alias_key = alias_credential
  110. key_env = key_env or _selected_provider_key_env(resolution, result.target_provider)
  111. if key_env:
  112. frozen['model']['key_env'] = key_env
  113. if source:
  114. frozen['model'].update(api_key=None, api_key_from=list(source))
  115. elif 'api_key' not in frozen['model']:
  116. frozen['model'].pop('api_key_from', None)
  117. # Creation identity (request_json) remains immutable; runtime selection lives in the policy.
  118. policy = replace(old, model=result.new_model, config_json=json.dumps(frozen))
  119. secrets = recover_config_secrets(authority, old) if old.config_secret_ref else {}
  120. # The picked entry's private subtree is replaced whole: a key or header its current declaration
  121. # withdrew must not follow it to the new endpoint.
  122. # An unrelated entry the pick displaced carries its own secrets to its new index.
  123. secrets = {moved.get(path[:2], path[:2]) + path[2:]: value for path, value in secrets.items()}
  124. secrets = {path: value for path, value in secrets.items() if path[:2] not in retired}
  125. secrets.update(provider_secrets)
  126. # A cleared field's private value must not be re-hydrated into the new route by config().
  127. secrets = {path: value for path, value in secrets.items()
  128. if not (len(path) == 2 and path[0] == 'model' and path[1] not in frozen['model'])}
  129. if source:
  130. secrets.pop(('model', 'api_key'), None)
  131. secrets[source] = alias_key
  132. # Re-fingerprint frozen config references because policy identity includes model.
  133. # An explicit launch key belongs to the endpoint it authenticated, not to a provider name:
  134. # custom -> custom on another base_url must not carry it (same route identity as config keys).
  135. # Measured on resolved routes (a respelled provider on one endpoint is one route; a named entry
  136. # whose base_url moved is another): the key survives only while it still authenticates the route
  137. # the session runs now and the pick stays on that route.
  138. key = route_launch_key(authority, old, config)
  139. if key is None or not bound_route_holds(config.get('model'), config, frozen['model'], config):
  140. policy = replace(policy, credential_ref=None)
  141. key = None
  142. policy = bind_launch_key(authority, prepared['snapshot']['receipt']['session_id'], policy, key,
  143. config_secrets=secrets)
  144. return dict(prepared, policy=asdict(policy))
  145. def is_model_reset(payload):
  146. """``/model reset`` as a ``model`` mutation; with any other selection field it is refused, as on
  147. every surface (``parse_model_switch_args``)."""
  148. from hermes_cli.model_switch import (
  149. MODEL_SWITCH_ERR_RESET_WITH_FLAGS, MODEL_SWITCH_ERROR_TEXT, MODEL_SWITCH_RESET_TOKEN)
  150. if payload['model'].strip().lower() != MODEL_SWITCH_RESET_TOKEN:
  151. return False
  152. if set(payload) - {'model', 'confirm'}:
  153. raise RuntimeStoreError('invalid_params', field='payload',
  154. detail=MODEL_SWITCH_ERROR_TEXT[MODEL_SWITCH_ERR_RESET_WITH_FLAGS])
  155. return True
  156. def configured_default(authority):
  157. """``(model, provider)`` the owning profile's config.yaml names now (``auto`` reads as no provider)."""
  158. from gateway.run import _resolve_gateway_model
  159. from gateway.session_policy import profile_config
  160. profile = profile_config(authority)
  161. block = profile.get('model')
  162. if not block:
  163. return '', ''
  164. provider = str(block.get('provider') or '') if isinstance(block, dict) else ''
  165. return _resolve_gateway_model(profile), '' if provider.lower() == 'auto' else provider
  166. def _reset_payload(authority, old, current):
  167. """``/model reset``: the pick becomes the owning profile's configured model, read fresh, so the
  168. session runs (and, when following, keeps following) its profile again; history is kept. Returns a
  169. ``noop`` report when the session already runs that model. A safe-mode session never read the
  170. profile, so it has no configured default to return to."""
  171. from agent.i18n import t
  172. model, provider = ('', '') if old.ignore_user_config else configured_default(authority)
  173. if not model:
  174. raise RuntimeStoreError('invalid_params', field='payload.model', detail=t('cli.model.reset_no_default'))
  175. if model == current.model and provider in ('', current.provider):
  176. return {'status': 'noop', 'model': model, 'note': t('cli.model.reset_none', model=model)}
  177. return {'model': model, **({'provider': provider} if provider else {})}
  178. def _provider_resolution_config(authority, config, bypass=False):
  179. """*config* with the owning profile's CURRENT named providers over its frozen ones, used only to
  180. resolve and freeze a pick. Reads this owner's own config.yaml (never another profile's); a
  181. frozen entry the profile no longer declares stays resolvable. A session that bypasses the
  182. profile (``ignore_user_config``, implied by safe mode) resolves only its own frozen config.
  183. ``${VAR}`` refs expand against the profile's CURRENT ``.env``, not the daemon's start env."""
  184. from pathlib import Path
  185. from gateway.session_local import load_profile_config
  186. if bypass:
  187. return config
  188. current = load_profile_config(Path(authority.db.db_path).resolve().parent)
  189. if not isinstance(current, dict):
  190. return config
  191. resolved = dict(config)
  192. frozen_providers, current_providers = config.get('providers'), current.get('providers')
  193. if isinstance(frozen_providers, dict) or isinstance(current_providers, dict):
  194. current_providers = current_providers if isinstance(current_providers, dict) else {}
  195. slugs = {_provider_key_slug(key) for key in current_providers}
  196. # A respelled key is the same provider: the profile's spelling replaces the frozen one.
  197. resolved['providers'] = {**{key: entry for key, entry in (
  198. frozen_providers if isinstance(frozen_providers, dict) else {}).items()
  199. if _provider_key_slug(key) not in slugs}, **current_providers}
  200. frozen_custom, current_custom = config.get('custom_providers'), current.get('custom_providers')
  201. if isinstance(frozen_custom, list) or isinstance(current_custom, list):
  202. current_custom = list(current_custom) if isinstance(current_custom, list) else []
  203. names = {_custom_provider_name(entry) for entry in current_custom if isinstance(entry, dict)}
  204. # Current entries keep their profile positions (the path cold recovery re-reads).
  205. resolved['custom_providers'] = current_custom + [
  206. entry for entry in (frozen_custom if isinstance(frozen_custom, list) else [])
  207. if not isinstance(entry, dict) or _custom_provider_name(entry) not in names]
  208. resolved['_profile_custom_count'] = len(current_custom)
  209. return resolved
  210. def _custom_provider_name(entry):
  211. """The runtime's canonical identity: ``My Provider`` and ``my-provider`` are one provider."""
  212. from hermes_cli.providers import custom_provider_slug
  213. return custom_provider_slug(str(entry.get('name') or ''), str(entry.get('provider_key') or ''))
  214. def _selected_provider_key_env(config, provider):
  215. """Return a selected ``providers.<slug>`` credential pointer, never its secret value."""
  216. providers = config.get('providers')
  217. entry = providers.get(provider) if isinstance(providers, dict) else None
  218. if not isinstance(entry, dict):
  219. return None
  220. return str(entry.get('key_env') or entry.get('api_key_env') or '').strip() or None
  221. def _freeze_selected_provider(config, frozen, provider):
  222. """Copy only the picked named provider's declaration into the frozen session route.
  223. The next turn resolves from the session's own configuration, not a later profile edit. Private
  224. fields move to the policy's secret store, as policy creation does; nothing else moves. Returns
  225. ``(secrets, retired)``: the entry's private values and every ``(section, key)`` whose previous
  226. private subtree they replace, plus ``moved``: ``{old (section, index): new}`` for an unrelated entry
  227. the pick displaced. A legacy entry from the profile is frozen at its profile index (the path cold
  228. recovery re-reads); an equivalent earlier spelling is blanked, never left to win.
  229. """
  230. from copy import deepcopy
  231. from gateway.session_policy import _extract_config_secrets
  232. from hermes_cli.runtime_provider_custom import named_custom_provider_config_path
  233. path = named_custom_provider_config_path(provider, config)
  234. if path is None:
  235. return {}, (), {}
  236. section, index = path
  237. entries = config.get(section)
  238. if not isinstance(entries, (dict, list)) or not isinstance(entries[index], dict):
  239. return {}, (), {}
  240. copied = deepcopy(entries[index])
  241. retired, moved = {(section, index)}, {}
  242. if section == 'providers':
  243. previous = frozen.get(section) if isinstance(frozen.get(section), dict) else {}
  244. stale = {key for key in previous if _provider_key_slug(key) == _provider_key_slug(index)}
  245. frozen[section] = {**{k: v for k, v in previous.items() if k not in stale}, index: copied}
  246. retired |= {(section, key) for key in stale}
  247. else:
  248. frozen_entries = list(frozen.get(section) or [])
  249. name = _custom_provider_name(copied)
  250. same = [i for i, entry in enumerate(frozen_entries)
  251. if isinstance(entry, dict) and _custom_provider_name(entry) == name]
  252. if index >= config.get('_profile_custom_count', len(entries)):
  253. index = same[0] # a frozen-only entry stays where it is
  254. frozen_entries += [{}] * (index + 1 - len(frozen_entries))
  255. displaced = frozen_entries[index]
  256. if isinstance(displaced, dict) and displaced and _custom_provider_name(displaced) != name:
  257. moved[(section, index)] = (section, len(frozen_entries))
  258. frozen_entries.append(displaced) # still resolvable by name, with its own secrets
  259. for i in same:
  260. frozen_entries[i] = {}
  261. frozen_entries[index] = copied
  262. retired |= {(section, i) for i in same} | {(section, index)}
  263. frozen[section] = frozen_entries
  264. secrets = {}
  265. _extract_config_secrets(frozen[section][index], secrets, (section, index))
  266. return secrets, retired, moved
  267. def _provider_key_slug(key):
  268. """A ``providers:`` key's canonical identity: ``Review Prov`` and ``review-prov`` are one."""
  269. from hermes_cli.providers import custom_provider_slug
  270. return custom_provider_slug('', str(key))
  271. def _alias_credential(authority, result):
  272. """``(key_env, source_path, literal_key)`` the direct alias a switch resolved declares for its own
  273. endpoint, read from THIS owner's config.yaml: the file a restarted owner re-reads, never the
  274. process-global alias cache another served profile may refill after the switch. A raw
  275. ``api_key: "${VAR}"`` is the pointer ``VAR``; ``(None, None, None)`` when it declares none."""
  276. from pathlib import Path
  277. from gateway.run import _load_gateway_config
  278. from hermes_cli.config import _env_ref_var_name, read_user_config_raw
  279. name = result.resolved_via_alias if result.success else ''
  280. path = Path(authority.db.db_path).resolve().parent / 'config.yaml'
  281. raw = read_user_config_raw(path) if name else {}
  282. for section in (('model_aliases',), ('model', 'aliases')):
  283. model = raw.get('model') # `model: name` (a scalar) is valid config and declares no aliases
  284. entries = raw.get(section[0]) if len(section) == 1 else (model.get('aliases') if isinstance(model, dict) else None)
  285. for written, entry in (entries.items() if isinstance(entries, dict) else ()):
  286. if not isinstance(entry, dict) or not entry.get('model') or str(written).strip().lower() != name:
  287. continue
  288. key = str(entry.get('api_key') or '').strip()
  289. pointer = _env_ref_var_name(key[2:-1]) if key.startswith('${') and key.endswith('}') else None
  290. if pointer:
  291. return pointer, None, None
  292. if not key:
  293. return str(entry.get('key_env') or '').strip() or None, None, None
  294. value = _load_gateway_config(path)
  295. for part in (*section, written, 'api_key'):
  296. value = value.get(part) if isinstance(value, dict) else None
  297. if not value:
  298. raise RuntimeStoreError('model_resolution_failed')
  299. return None, (*section, written, 'api_key'), value
  300. return None, None, None
  301. def _persisted_selection_context(authority, snapshot, current):
  302. from agent.usage_anchor import persisted_anchor_tokens
  303. from hermes_cli.model_selection_guards import SelectionContext
  304. target = snapshot.get('target') or snapshot['receipt']['session_id']
  305. tokens = persisted_anchor_tokens(authority.db, target, authority.db.get_messages_as_conversation(target))
  306. return SelectionContext(context_tokens=tokens, current_model=current.model or None) if tokens else None
  307. async def _selection_guard_refusal(authority, live, payload, snapshot, current, result):
  308. """The shared selection-guard registry (cost, data-policy, large-context switch) runs before the
  309. commit. A flagged target is refused with ``status: confirmation_required`` and a ``confirm``
  310. token bound to the resolved target, the selection it replaces and the session revision; nothing
  311. is written. Only that exact token in ``payload['confirm']`` applies it: a stale or foreign
  312. token (target re-resolved elsewhere, another switch landed, a turn settled) gets a fresh
  313. refusal, never a silent apply."""
  314. import hashlib
  315. from hermes_cli.model_selection_guards import combined_message, selection_context_for_agent, selection_warnings
  316. from hermes_cli.route_identity import normalize_route_base_url
  317. lookup = (getattr(authority.runner, '_resident_agent_for', None)
  318. or getattr(authority.runner, '_cached_agent_for', None))
  319. agent = lookup(live.route) if live is not None and callable(lookup) else None
  320. context = selection_context_for_agent(agent)
  321. if context is None:
  322. # No resident agent (evicted by the previous switch, or an owner restart): the session's
  323. # persisted usage anchor still measures the context at stake, so the large-context guard
  324. # does not go quiet on exactly the switch that follows another one.
  325. context = await asyncio.to_thread(_persisted_selection_context, authority, snapshot, current)
  326. # Off-loop: pricing lookups may hit models.dev on a cache miss.
  327. warnings = await asyncio.to_thread(
  328. selection_warnings, result.new_model, provider=result.target_provider,
  329. base_url=result.base_url or current.base_url, api_key=result.api_key or current.api_key,
  330. model_info=result.model_info, selection_context=context)
  331. if not warnings:
  332. return None
  333. binding = {'target': [result.new_model, result.target_provider, normalize_route_base_url(result.base_url),
  334. result.api_mode or ''],
  335. 'current': [current.model, current.provider, normalize_route_base_url(current.base_url)],
  336. 'session': [snapshot['target'], snapshot['target_revision']],
  337. 'guards': sorted(w.kind for w in warnings)}
  338. token = hashlib.sha256(json.dumps(binding, sort_keys=True).encode()).hexdigest()
  339. if payload.get('confirm') == token:
  340. return None
  341. # A fresh dict: the prepared snapshot carries the frozen policy and never leaves the owner. No
  342. # ``model`` key: a client that predates the exchange must not paint the target as applied.
  343. return {'status': 'confirmation_required', 'confirm_required': True, 'confirm': token,
  344. 'target_model': result.new_model, 'target_provider': result.target_provider,
  345. 'confirm_message': combined_message(warnings),
  346. 'warnings': [{'kind': w.kind, 'title': w.title, 'message': w.message} for w in warnings]}