session_deferred_pick.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """A model or reasoning pick on a session with a running turn lands for the next turn (RT-12, MP-24).
  2. The pick is authorized and validated when it arrives, kept durably in ``state_meta`` (latest pick
  3. wins, survives an owner restart), and committed through ``session.mutate``'s own writer by the
  4. drain at the next claim boundary, when nothing is executing. The running turn keeps its route.
  5. A level rides in the same slot beside the model pick and commits first, as ``apply_pending_reasoning``
  6. runs before a model pick: a level picked with a later model pick wins, a later level replaces it.
  7. """
  8. import json
  9. import logging
  10. from hermes_state_mutation_prepared import DEFERRED_PICK_PREFIX as _PREFIX
  11. from hermes_state_runtime import RuntimeStoreError
  12. logger = logging.getLogger(__name__)
  13. def defer_pick(authority, actor, ref, params):
  14. """Record *params* (an authorized ``model`` or ``reasoning`` mutation refused only because a turn
  15. is running).
  16. Blocking SQLite: callers run it off the event loop. Acceptance writes the request's durable
  17. mutation receipt in the same transaction, so an exact retry returns this result instead of
  18. overwriting a newer pick, and the stored pick carries a stable application id."""
  19. from hermes_state_mutations import validate_action
  20. from hermes_state_runtime import _epoch, _json, mutation_receipt_key, replay_mutation_receipt
  21. operation = params.get('operation', 'model')
  22. validate_action(operation, params['payload'])
  23. shown = None
  24. if operation == 'model':
  25. from gateway.session_mutation_model import configured_default, is_model_reset
  26. if is_model_reset(params['payload']): # a queued reset shows the default it adopts, not "reset"
  27. shown = configured_default(authority)[0]
  28. key, digest = mutation_receipt_key(actor.subject, ref.session_id, params['request_id'], operation=operation,
  29. payload=params['payload'], expected_revision=params.get('expected_revision'),
  30. expected_generation=params.get('expected_generation'))
  31. pick = {'principal_id': actor.subject, 'request_id': params['request_id'], 'payload': params['payload'],
  32. 'application_id': 'deferred-pick:' + params['request_id']}
  33. def write(conn):
  34. _epoch(conn, authority.epoch)
  35. replay = replay_mutation_receipt(conn, key, digest)
  36. if replay is not None:
  37. return replay
  38. row = conn.execute('SELECT runtime_revision, runtime_generation FROM sessions WHERE id=?',
  39. (ref.session_id,)).fetchone()
  40. if row is None:
  41. raise RuntimeStoreError('not_found')
  42. stored = conn.execute('SELECT value FROM state_meta WHERE key=?', (_PREFIX + ref.session_id,)).fetchone()
  43. current = json.loads(stored[0]) if stored else {}
  44. if operation == 'reasoning':
  45. current.get('payload', {}).pop('reasoning', None) # this level is newer than the one picked with the model
  46. value, picked = {**current, 'reasoning': pick}, {'effort': params['payload']['effort']}
  47. else:
  48. # A level picked with this model is newer than one deferred before it, which it drops: the
  49. # slot never holds both, so committing the slot's level never strips the model's own.
  50. older = 'reasoning' in current and 'reasoning' not in params['payload']
  51. value = {**pick, **({'reasoning': current['reasoning']} if older else {})}
  52. picked = {'model': shown or params['payload']['model']}
  53. result = {'session_id': ref.session_id, 'operation': operation, 'status': 'deferred', **picked,
  54. 'revision': row[0], 'execution_generation': row[1]}
  55. conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value',
  56. (_PREFIX + ref.session_id, _json(value)))
  57. conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?)', (key, _json({'digest': digest, 'result': result})))
  58. return result
  59. return authority.db._execute_write(write)
  60. async def reask_on_attach(authority, ref, live):
  61. """A guarded pick asked only the viewers present at its boundary (and an owner restart drops
  62. the stream): a newly attached viewer gets the ask again, re-checked at the next boundary."""
  63. import asyncio
  64. def read():
  65. with authority.db._read_ctx() as conn:
  66. return conn.execute('SELECT value FROM state_meta WHERE key=?', (_PREFIX + ref.session_id,)).fetchone()
  67. stored = await asyncio.to_thread(read)
  68. if stored is not None and 'awaiting_confirm' in json.loads(stored[0]):
  69. live.reask_pick = True
  70. authority._schedule(ref)
  71. async def _commit(authority, ref, entry, operation):
  72. """Commit the deferred *entry* as an *operation* mutation. Returns None while a worker still holds
  73. the transcript (the pick waits for the next boundary), else the mutation's result."""
  74. import asyncio
  75. from gateway.session_mutations import write_mutation
  76. from hermes_state_mutation_guards import MUTATION_GUARD_REFUSALS
  77. from hermes_state_mutation_retirement import has_mutation_receipt
  78. if await asyncio.to_thread(has_mutation_receipt, authority.db, entry['principal_id'], ref.session_id,
  79. entry['application_id']):
  80. return {'status': 'replayed'} # committed before a restart, not yet removed: never apply it twice
  81. row = await asyncio.to_thread(authority.db.get_session, ref.session_id)
  82. params = {'session_id': ref.session_id, 'request_id': entry['application_id'],
  83. 'expected_revision': row['runtime_revision'], 'expected_generation': row['runtime_generation'],
  84. 'operation': operation, 'payload': entry['payload']}
  85. try:
  86. return await write_mutation(authority, ref, params, None, principal_id=entry['principal_id'])
  87. except MUTATION_GUARD_REFUSALS:
  88. return None
  89. except RuntimeStoreError as exc:
  90. if exc.reason == 'session_busy':
  91. return None
  92. return {'session_id': ref.session_id, 'operation': operation, 'status': 'refused', 'reason': exc.reason}
  93. except Exception:
  94. # Resolving the pick failed (provider auth, catalog, resolver crash). This runs before the
  95. # claim, so letting it escape would kill or wedge the drain on the same pick forever.
  96. logger.exception('Deferred %s pick on %s failed to resolve', operation, ref.session_id)
  97. return {'session_id': ref.session_id, 'operation': operation, 'status': 'refused',
  98. 'reason': operation + '_resolution_failed'}
  99. async def _commit_level(authority, ref, key, raw, pick):
  100. """Commit the level deferred in *pick* (stored as *raw*) and drop it from the slot. Returns the
  101. remaining slot value, or None when nothing is left or a worker still holds the transcript."""
  102. import asyncio
  103. from hermes_state_runtime import _json
  104. result = await _commit(authority, ref, pick.pop('reasoning'), 'reasoning')
  105. if result is None:
  106. return None
  107. if result.get('status') == 'refused':
  108. logger.warning('Deferred reasoning pick on %s not applied: %s', ref.session_id, result['reason'])
  109. rest = _json(pick) if 'application_id' in pick else None
  110. def settle(conn):
  111. if rest is None:
  112. conn.execute('DELETE FROM state_meta WHERE key=? AND value=?', (key, raw))
  113. else:
  114. conn.execute('UPDATE state_meta SET value=? WHERE key=? AND value=?', (rest, key, raw))
  115. await asyncio.to_thread(authority.db._execute_write, settle)
  116. return rest
  117. async def apply_deferred_pick(authority, ref, live):
  118. """Commit a deferred pick. The drain calls this under the claim gate and mutation lock, before
  119. claiming, so no turn is running. A pick that no longer resolves is dropped and announced."""
  120. import asyncio
  121. key = _PREFIX + ref.session_id
  122. def read():
  123. with authority.db._read_ctx() as conn:
  124. return conn.execute('SELECT value FROM state_meta WHERE key=?', (key,)).fetchone()
  125. stored = await asyncio.to_thread(read)
  126. if stored is None:
  127. return
  128. raw = stored[0]
  129. pick = json.loads(raw)
  130. if 'reasoning' in pick:
  131. raw = await _commit_level(authority, ref, key, raw, pick)
  132. if raw is None:
  133. return
  134. if 'awaiting_confirm' in pick:
  135. if not live.reask_pick:
  136. return # asked; the user's confirmed (or a newer) selection supersedes it
  137. # Asked again for a newly attached viewer: re-prepared under the current fences, so a guard
  138. # that still applies mints a fresh token (the stored one is never replayed as consent).
  139. pick = {k: v for k, v in pick.items() if k != 'awaiting_confirm'}
  140. live.reask_pick = False
  141. result = await _commit(authority, ref, pick, 'model')
  142. if result is None:
  143. return
  144. if result.get('status') == 'confirmation_required':
  145. # A guarded target (cost / data policy / large context): nothing was written. The pick stays
  146. # pending, marked as asked, and the owner's confirmation exchange goes to every viewer; the
  147. # confirmed resend (``payload.confirm``) commits it through session.mutate.
  148. from hermes_state_runtime import _json
  149. await asyncio.to_thread(authority.db._execute_write, lambda conn: conn.execute(
  150. 'UPDATE state_meta SET value=? WHERE key=? AND value=?',
  151. (_json({**pick, 'awaiting_confirm': result['confirm']}), key, raw)))
  152. current = await asyncio.to_thread(authority.db.get_session, ref.session_id) or {}
  153. provider = json.loads(current.get('model_config') or '{}').get('provider')
  154. # The committed selection rides along, so a viewer that declines paints it back.
  155. live.event_stream.publish(ref.session_id, {
  156. **result, 'payload': pick['payload'], 'committed_model': current.get('model'),
  157. **({'committed_provider': provider} if provider else {})}, event_type='session.updated')
  158. return
  159. await asyncio.to_thread(authority.db._execute_write, lambda conn: conn.execute(
  160. 'DELETE FROM state_meta WHERE key=? AND value=?', (key, raw)))
  161. if result.get('status') == 'refused':
  162. logger.warning('Deferred model pick on %s not applied: %s', ref.session_id, result.get('reason'))
  163. # Named for the viewers that showed "applies from your next message": the target that was
  164. # refused, and the committed model they must show again.
  165. current = await asyncio.to_thread(authority.db.get_session, ref.session_id) or {}
  166. provider = json.loads(current.get('model_config') or '{}').get('provider')
  167. live.event_stream.publish(ref.session_id, {
  168. **result, 'target_model': pick['payload']['model'], 'model': current.get('model'),
  169. **({'provider': provider} if provider else {})}, event_type='session.updated')