session_local_migration.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. """Owner-only adoption of pre-authority local transcripts.
  2. An old transcript has no authenticated subject. Only the machine owner (a redeemed same-profile
  3. native control ticket, or the owner signed in to the Webapp: gateway.owner_identity) may adopt it.
  4. The historical ID becomes the stable logical owner without copying its messages.
  5. Compression and reset may later advance the physical target, never FIFO identity.
  6. """
  7. from dataclasses import asdict, replace
  8. from datetime import datetime
  9. import json
  10. from pathlib import Path
  11. from gateway.config import Platform
  12. from gateway.session import SessionEntry, SessionSource
  13. from gateway.session_contract import SessionRef
  14. from hermes_state_runtime import RuntimeStoreError
  15. def bind_native_transport(authority, actor, identity):
  16. # The machine owner signed in to the Webapp is the same person native Desktop is (owner_identity).
  17. from gateway.owner_identity import host_owner_subject
  18. native = (identity.get('provider') == 'local' and identity.get('profile_id') == authority.profile_id
  19. and identity.get('instance_id') == authority.instance_id)
  20. if ((native or actor.subject == host_owner_subject())
  21. and actor.profile_id == authority.profile_id
  22. and 'session:create' in actor.capabilities):
  23. if not hasattr(authority, '_native_legacy_transports'):
  24. authority._native_legacy_transports = {}
  25. authority._native_legacy_transports[actor.transport_id] = actor
  26. def unbind_native_transport(authority, actor):
  27. getattr(authority, '_native_legacy_transports', {}).pop(actor.transport_id, None)
  28. def require_history_claim(authority, conn, actor, sid):
  29. """Import receipts confer ownership; merely knowing an old ID does not."""
  30. from gateway.owner_identity import same_owner
  31. from hermes_state_mutation_binding import BINDING_PREFIX, _owner_user
  32. row = conn.execute('SELECT * FROM sessions WHERE id=?', (sid,)).fetchone()
  33. if row is None or row['session_key'] or row['chat_id'] or row['origin_json']:
  34. return
  35. if conn.execute('SELECT 1 FROM state_meta WHERE key=?', (BINDING_PREFIX + sid,)).fetchone():
  36. return # authorize_history checks the already committed importer identity.
  37. own_profile = row['profile_name'] in (None, '', authority.db._own_profile_name() or 'default')
  38. if own_profile and _owner_user(row) and same_owner(row['user_id'], actor.subject):
  39. return # Its owner (a login stamp is the machine owner's); authorize_history re-checks it.
  40. if getattr(authority, '_native_legacy_transports', {}).get(actor.transport_id) != actor:
  41. raise RuntimeStoreError('permission_denied')
  42. def resolve_local_target(authority, actor, sid):
  43. """Resolve physical history IDs without silently adopting canonical corruption."""
  44. db = authority.db
  45. row = db.get_session(sid)
  46. if row is None:
  47. raise RuntimeStoreError('not_found')
  48. from hermes_state_local import POLICY_PREFIX
  49. with db._read_ctx() as conn:
  50. from hermes_state_local_migration import LEGACY_PREFIX
  51. bound = conn.execute('SELECT 1 FROM state_meta WHERE key IN (?,?)',
  52. (POLICY_PREFIX + (row.get('chat_id') or sid), LEGACY_PREFIX + (row.get('chat_id') or sid))).fetchone()
  53. if bound and row.get('chat_id'):
  54. from hermes_state_local import local_lineage_owner
  55. # A copied chat_id is not evidence that this row belongs to the owner's lineage.
  56. if local_lineage_owner(conn, sid) != row['chat_id']:
  57. raise RuntimeStoreError('storage_unavailable')
  58. if bound or str(row.get('chat_id') or '').startswith('local-'):
  59. ref = SessionRef(authority.profile_id, row['chat_id'])
  60. authority.authorize(actor, ref, 'session:read')
  61. from gateway.session_local_recovery import restore_local_session
  62. return restore_local_session(authority, ref.session_id) if bound else ref
  63. return adopt_legacy_session(authority, actor, row)
  64. def legacy_launch_params(row):
  65. """``(create params, api_mode)`` a legacy row resumes on.
  66. The old local surfaces persisted their effective route and runtime choices in
  67. ``model_config``. Preserve those choices while adopting them, rather than
  68. combining their model with a later profile default. Older ACP rows also hold
  69. their workspace only there. ``api_mode`` is no create field, so it is
  70. returned apart.
  71. """
  72. params = {key: row[key] for key in ('cwd', 'model') if row.get(key)}
  73. from hermes_state_sessions import _parse_model_config
  74. saved = _parse_model_config(row.get('model_config'))
  75. from hermes_state_gateway import SessionGatewayMixin
  76. route = SessionGatewayMixin.session_gateway_runtime(row)
  77. route = {key: value.strip() for key, value in route.items() if isinstance(value, str) and value.strip()}
  78. billing_base_url = row.get('billing_base_url')
  79. if 'base_url' not in route and isinstance(billing_base_url, str) and billing_base_url.strip():
  80. route['base_url'] = billing_base_url.strip()
  81. # The reader returns a bare ``custom`` billing provider with its endpoint and drops ``auto``:
  82. # an endpoint left without a provider is a custom route, never one to pair with the
  83. # profile's provider.
  84. route.setdefault('provider', 'custom')
  85. if row['source'] == 'acp' and 'cwd' not in params and Path(str(saved.get('cwd') or '')).is_absolute():
  86. params['cwd'] = saved['cwd']
  87. params.update({key: route[key] for key in ('provider', 'base_url') if key in route})
  88. reasoning = saved.get('reasoning_config')
  89. if isinstance(reasoning, dict):
  90. reasoning = 'none' if reasoning.get('enabled') is False else reasoning.get('effort')
  91. if isinstance(reasoning, str):
  92. from hermes_constants import parse_reasoning_effort
  93. if parse_reasoning_effort(reasoning) is not None:
  94. params['reasoning'] = reasoning
  95. tier = saved.get('service_tier')
  96. if isinstance(tier, str):
  97. from agent.fast_mode import parse_exact_service_tier
  98. try:
  99. parse_exact_service_tier(tier)
  100. except ValueError:
  101. pass
  102. else:
  103. params['service_tier'] = tier
  104. return params, route.get('api_mode')
  105. def bot_chat_row(row):
  106. """Main's Bot Chat rule for a stored row: the ``follow_profile_config`` marker, or, for a row
  107. written before the marker existed, the exact title ``Bot Chat``."""
  108. from hermes_state_sessions import _parse_model_config
  109. saved = _parse_model_config(row.get('model_config'))
  110. return saved.get('follow_profile_config') is True or str(row.get('title') or '').strip() == 'Bot Chat'
  111. def composer_pick_holds(row, config):
  112. """Whether *row*'s composer pick still holds: the profile (*config*) targets what it diverged from."""
  113. from gateway.session_policy import profile_target
  114. from hermes_state_sessions import _parse_model_config
  115. composer = _parse_model_config(row.get('model_config')).get('composer_override_profile')
  116. return isinstance(composer, dict) and (
  117. str(composer.get('model') or '').strip(), str(composer.get('provider') or '').strip(),
  118. ) == profile_target(config)
  119. def legacy_bot_chat_launch(row, params, api_mode, config):
  120. """``(params, api_mode)`` for adoption. A Bot Chat (``bot_chat_row``) follows its profile.
  121. A composer pick is kept only while the profile still targets what the pick diverged from, and
  122. is adopted the way a create records one: held until the next profile edit. Otherwise only the
  123. workspace and source carry over, so the route last run is not frozen and its wire mode is not
  124. either. Any other row is unchanged.
  125. """
  126. if not bot_chat_row(row):
  127. return params, api_mode
  128. if not ('model' in params and composer_pick_holds(row, config)):
  129. params = {key: params[key] for key in ('cwd', 'source') if key in params}
  130. api_mode = None
  131. return {**params, 'follow_profile_config': True}, api_mode
  132. def follow_bot_chat(authority, session_id, policy, live, picked=()):
  133. """*policy* following its profile when the session is a Bot Chat an earlier build committed
  134. frozen (its delivery door, a CLI titled create, an adoption), else *policy* itself.
  135. The caller is a client opening the chat (``session.resume``), which adopts it on the same rule
  136. and composer pick as adoption above; *live* is the profile's config the open read. A request
  137. that names ``follow_profile_config`` (``false`` included) is the client's own choice, and a
  138. bypass launch never reads the profile. The request is not rewritten. A committed ``/model``
  139. (*picked*, the session's policy edits) holds like a composer pick, and a committed ``/reasoning``
  140. stays the session's level, recorded as its session pin.
  141. The caller re-binds the private values without the followed sections, so a frozen key never
  142. reaches the profile's route.
  143. """
  144. from gateway.session_policy import FOLLOW_ADOPTED, SESSION_PINS, present_sections, profile_target
  145. if (policy.follows_profile or policy.ignore_user_config
  146. or 'follow_profile_config' in json.loads(policy.request_json)):
  147. return policy
  148. db = authority.db
  149. owner = db.get_session(session_id) or {}
  150. if not bot_chat_row(owner):
  151. # Compression can carry the canonical title to a later segment of the same conversation.
  152. titled = db.get_session_by_title('Bot Chat')
  153. if titled is None:
  154. return policy
  155. from hermes_state_local import local_conversation_ids
  156. with db._read_ctx() as conn:
  157. if titled['id'] not in local_conversation_ids(conn, session_id):
  158. return policy
  159. live = present_sections(live)
  160. config = json.loads(policy.config_json)
  161. model = config.get('model') if isinstance(config.get('model'), dict) else {}
  162. if composer_pick_holds(owner, live) or ('model' in picked and model.get('selection_profile') is None):
  163. # Recorded the way a create records a pick: it holds until the profile's model or provider moves.
  164. # A ``/model`` committed while the chat was frozen is such a pick too (its receipt, in *picked*).
  165. config.setdefault('model', {})['selection_profile'] = list(profile_target(live))
  166. pins = config.get(SESSION_PINS) if isinstance(config.get(SESSION_PINS), dict) else {}
  167. if 'reasoning' in picked and not isinstance(pins.get('reasoning'), bool):
  168. config[SESSION_PINS] = {**pins, 'reasoning': True}
  169. return replace(policy, config_json=json.dumps({**config, FOLLOW_ADOPTED: True}))
  170. def _route_is_usage_stamp(row):
  171. """True when the row's only route record is the ``billing_provider='custom'`` + ``billing_base_url``
  172. usage stamp: no ``gateway_runtime`` and no top-level ``provider``/``base_url`` in ``model_config``.
  173. That stamp records which endpoint billed the last turn (the profile's route at the time), not a
  174. route the user picked for this session."""
  175. from hermes_state_sessions import _parse_model_config
  176. saved = _parse_model_config(row.get('model_config'))
  177. runtime = saved.get('gateway_runtime')
  178. if isinstance(runtime, dict) and runtime.get('provider'):
  179. return False
  180. if any(isinstance(saved.get(k), str) and saved[k].strip() for k in ('provider', 'base_url')):
  181. return False
  182. return str(row.get('billing_provider') or '').strip().lower() == 'custom'
  183. def _configured_owner(saved, config):
  184. """The configured named entry (``providers:``/``custom_providers:``) serving the saved URL, or None."""
  185. from gateway.session_route_identity import resolved_route, same_route
  186. names = list(config.get('providers') or {}) if isinstance(config.get('providers'), dict) else []
  187. legacy = config.get('custom_providers')
  188. names += [e.get('name') for e in legacy if isinstance(e, dict)] if isinstance(legacy, list) else []
  189. for name in names:
  190. owner, url = resolved_route({'provider': str(name or '')}, config)
  191. if url and same_route(saved, {'provider': owner, 'base_url': url}, config):
  192. return owner
  193. return None
  194. def _endpoint_owner(saved, config):
  195. """The provider whose credential may serve the saved endpoint. A named entry that now points
  196. elsewhere owns a credential for THAT host: rebind to the entry still serving the saved URL, or
  197. refuse by name rather than send the moved entry's key to the old host."""
  198. from gateway.session_route_identity import resolved_route, same_route
  199. identity, entry_url = resolved_route({'provider': saved['provider']}, config)
  200. if not entry_url or same_route(saved, {'provider': identity, 'base_url': entry_url}, config):
  201. return saved['provider']
  202. owner = _configured_owner(saved, config)
  203. if owner:
  204. return owner
  205. raise RuntimeStoreError('launch_credentials_unavailable', field='provider',
  206. detail=f'{identity} no longer serves the saved endpoint; select a model to continue')
  207. def adopts_client_history(authority, actor, row):
  208. """Whether this resume adopts *row*, a phone app's self-labelled chat
  209. (``hermes_state_local_migration.client_history_row``), as a writable conversation: only for the
  210. native owner adoption requires, and only when it validates for that owner. A read-only view of it
  211. already open (a metadata read came first) is promoted in place (restore_local_session); a session
  212. this authority serves any other way is left alone."""
  213. from gateway.session_history_mount import is_history_mount
  214. from hermes_state_local_migration import client_history_row, validate_legacy_row
  215. live = authority.sessions.get(row['id'])
  216. if (not client_history_row(row) or (live is not None and not is_history_mount(live))
  217. or getattr(authority, '_native_legacy_transports', {}).get(actor.transport_id) != actor):
  218. return False
  219. try:
  220. validate_legacy_row(authority.db, row, subject=actor.subject)
  221. except RuntimeStoreError:
  222. return False
  223. return True
  224. def adopt_legacy_session(authority, actor, row):
  225. if actor.profile_id != authority.profile_id:
  226. raise RuntimeStoreError('profile_mismatch')
  227. if (getattr(authority, '_native_legacy_transports', {}).get(actor.transport_id) != actor
  228. or not {'session:read', 'session:create'} <= actor.capabilities):
  229. raise RuntimeStoreError('not_found')
  230. from hermes_state_local_migration import legacy_creation_source, validate_legacy_row
  231. validate_legacy_row(authority.db, row, subject=actor.subject)
  232. authority._require_admission_open()
  233. from gateway.run import _load_gateway_config, _resolve_gateway_model
  234. from gateway.session_policy import build_policy, bind_launch_key
  235. from gateway.session_local_recovery import restore_local_session
  236. from hermes_state_local import commit_local_session
  237. from hermes_cli.active_sessions import active_session_liveness_guard, ActiveSessionRegistryError
  238. request_id = 'legacy:' + row['id']
  239. sid = row['id']
  240. params, api_mode = legacy_launch_params(row)
  241. params['source'] = legacy_creation_source(row)
  242. private = {}
  243. config = _load_gateway_config()
  244. params, api_mode = legacy_bot_chat_launch(row, params, api_mode, config)
  245. model_cfg = config.get('model')
  246. if isinstance(model_cfg, dict) and ('provider' in params or 'base_url' in params):
  247. from gateway.session_route_identity import ROUTE_BOUND_KEYS, resolved_route, same_route
  248. saved = {'provider': params.get('provider', model_cfg.get('provider')), 'base_url': params.get('base_url')}
  249. # ``auto`` matches every route, so an ``auto`` side proves nothing about the saved endpoint.
  250. if ('auto' not in (resolved_route(model_cfg, config)[0], resolved_route(saved, config)[0])
  251. and same_route(saved, model_cfg, config)):
  252. # The profile still selects this endpoint: resume on the profile's route, so its own
  253. # credential (inline key, ${ENV} key, key_env) keeps its normal precedence.
  254. params.pop('provider', None)
  255. params.pop('base_url', None)
  256. elif (_route_is_usage_stamp(row) and saved['base_url']
  257. and resolved_route(saved, config)[0] == 'custom' and not _configured_owner(saved, config)):
  258. # T21/F1: the only record is the usage stamp of a bare custom endpoint that no configured
  259. # entry serves any more (the profile's server moved). Pre-authority resume healed that stamp
  260. # to nothing and used the profile route; do the same rather than freeze a dead endpoint into
  261. # the policy, where every turn retries it until the cycle gives up.
  262. params.pop('provider', None)
  263. params.pop('base_url', None)
  264. else:
  265. # Another route than the profile's: its endpoint, wire mode, context pin and credential
  266. # (inline key, key_env pointer, api_key_from) stay with the profile, never paired with the
  267. # legacy route. ``provider`` stays: the saved route's own provider is in params, or it is
  268. # the profile's when the saved row carried only a base_url (resolved by _endpoint_owner).
  269. dropped = tuple(k for k in ROUTE_BOUND_KEYS if k != 'provider')
  270. config['model'] = {k: v for k, v in model_cfg.items() if k not in dropped}
  271. if saved['base_url']:
  272. params['provider'] = _endpoint_owner(saved, config)
  273. policy = build_policy(params, config, private_secrets=private)
  274. if api_mode:
  275. config = json.loads(policy.config_json)
  276. config.setdefault('model', {})['api_mode'] = api_mode
  277. policy = replace(policy, config_json=json.dumps(config))
  278. # restore_policy rejects a blank model, and the commit below is what the next open reads.
  279. # A following Bot Chat drops the saved name out of the request, so a profile with no
  280. # default would store ''. Put a name on the receipt only: the profile's resolved model,
  281. # else the row's. The request is unchanged, so a later profile default still wins.
  282. named = str(policy.model or '').strip() or (_resolve_gateway_model(policy.config()) or '').strip()
  283. if not named:
  284. named = str(row.get('model') or '').strip()
  285. if not named:
  286. raise RuntimeStoreError('invalid_params', field='model',
  287. detail='the profile names no model and the saved session has none')
  288. if policy.model != named:
  289. policy = replace(policy, model=named)
  290. policy = bind_launch_key(authority, sid, policy, None, config_secrets=private)
  291. from gateway.session_local_recovery import local_source
  292. source = local_source(authority, sid, actor.subject)
  293. route = authority.runner.session_store._generate_session_key(source)
  294. now = datetime.fromtimestamp(row['started_at'])
  295. entry = SessionEntry(route, row['id'], now, now, origin=source, platform=Platform.LOCAL)
  296. receipt = {'profile_id': authority.profile_id, 'principal_id': actor.subject, 'request_id': request_id,
  297. 'session_id': sid, 'route': route, 'entry': entry.to_dict(), 'policy': asdict(policy),
  298. 'legacy_session_id': row['id'], 'lineage': [row['id']]}
  299. try:
  300. # Same lock order as old Bot Chat: mailbox (if any), liveness, SQLite.
  301. with active_session_liveness_guard(row['id'], registry_home=Path(authority.db.db_path).parent) as active:
  302. if active:
  303. raise RuntimeStoreError('runtime_coordination_required')
  304. commit_local_session(authority.db, epoch=authority.epoch, receipt=receipt)
  305. except ActiveSessionRegistryError as exc:
  306. raise RuntimeStoreError('runtime_coordination_required') from exc
  307. return restore_local_session(authority, sid)