api_server_response_admissions.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. """Responses retries observe their original admission before resolving mutable conversation names."""
  2. from functools import partial
  3. import hashlib
  4. import logging
  5. import uuid
  6. from hermes_state_runtime import RuntimeStoreError
  7. def request_binding(adapter, request, durable_key):
  8. from gateway.platforms.api_server import _make_request_fingerprint
  9. return _make_request_fingerprint({'request': durable_key[1],
  10. 'declared_session_key': adapter._parse_session_key_header(request)[0]},
  11. keys=('request', 'declared_session_key'))
  12. def _request_admission_rows(db, request_id):
  13. """A lost derivative pointer cannot erase accepted identity on retirement."""
  14. from hermes_state_terminal import terminal_api_admissions
  15. with db._read_ctx() as conn:
  16. rows = [dict(row) for row in conn.execute(
  17. "SELECT * FROM session_admissions WHERE principal_id='api' AND request_id=?", (request_id,))]
  18. rows.extend(terminal_api_admissions(conn, request_id))
  19. return rows
  20. def _lookup_admission(authority, store, durable_key, request_id):
  21. """Scope comes from authenticated profile/key identity, never a caller-supplied session id.
  22. -> ``(authority, ref, row)`` or None. Store and state.db reads only (plus the pointer repair):
  23. run it under ``store.atomically`` in a worker, never on the loop, whose wait on the store lock
  24. would last for a concurrent response's whole persistence.
  25. The compact pointer survives response-cache eviction and transcript retirement. A process
  26. loss between the authority commit and pointer commit is repaired from that exact request id.
  27. """
  28. from gateway.session_contract import SessionRef
  29. from hermes_state_runtime import get_session_admission, _row
  30. admission_id = store.request_admission(durable_key[0])
  31. if admission_id:
  32. row = get_session_admission(authority.db, admission_id=admission_id)
  33. if row is None:
  34. # The admission's deletion tombstone aged out: the key is spent, never a new request.
  35. raise RuntimeStoreError('admission_conflict')
  36. else:
  37. rows = _request_admission_rows(authority.db, request_id)
  38. if len(rows) > 1:
  39. raise RuntimeStoreError('admission_conflict')
  40. if not rows:
  41. return None
  42. row = _row(rows[0])
  43. if row['principal_id'] != 'api' or row['request_id'] != request_id:
  44. raise RuntimeStoreError('permission_denied')
  45. if store.request_created_at(durable_key[0]) is None:
  46. # An older admission with no wire fingerprint cannot authorize a changed request just
  47. # because its caller reused the same key. Existing completed cache records still replay.
  48. raise RuntimeStoreError('admission_conflict')
  49. store.bind_request_admission(durable_key[0], row['admission_id'])
  50. return authority, SessionRef(authority.profile_id, row['target_session_id']), row
  51. def _restore_admitted(found):
  52. """The owner-loop half of a found admission: a live one's session is restored and checked."""
  53. if found is not None and found[2]['status'] in {'queued', 'started'}:
  54. from gateway.session_api import restore_api_session
  55. from gateway.session_api_turn import check_api_turn
  56. authority, ref, row = found
  57. restore_api_session(authority, ref.session_id)
  58. check_api_turn(authority, ref, row['payload'])
  59. return found
  60. def _lookup_call(adapter, durable_key, request_id):
  61. """-> ``(store, call)``: ``store.atomically(*call)`` is one ``_lookup_admission`` unit."""
  62. from gateway.session_authorities import require_active_authority
  63. authority = require_active_authority(adapter.gateway_runner, lambda: adapter._ensure_session_db())
  64. store = adapter._current_response_store()
  65. return store, (_lookup_admission, authority, store, durable_key, request_id)
  66. async def find_admission(adapter, durable_key, request_id):
  67. """``_lookup_admission`` off the loop, then the session restore on it."""
  68. store, call = _lookup_call(adapter, durable_key, request_id)
  69. return _restore_admitted(await store.off_loop(*call))
  70. def admitted_context(admitted, *, replay=False):
  71. from gateway.session_api_turn import observe_api_turn
  72. _, ref, row = admitted
  73. payload = row['payload']
  74. data = payload.get('api_turn_v1', {})
  75. from gateway.session_api_media import rehydrate_api_images
  76. return dict(session_id=ref.session_id, user_message=rehydrate_api_images(payload.get('text', ''), data.get('media')),
  77. conversation_history=data.get('history') or [],
  78. instructions=data.get('settings', {}).get('ephemeral_system_prompt'),
  79. run_kwargs={}, run_agent=partial(observe_api_turn, admitted),
  80. terminal_replay=replay or row['status'] == 'terminal')
  81. def _admit_steps(adapter, durable_key, request_id, binding, run_kwargs, model):
  82. """The final lookup and the admission are one API admission drive (``run_steps_off_loop``
  83. serializes them per authority): simultaneous observers converge on one admission. Every
  84. store call is a yielded step, so a worker runs it: only the session restore stays on the loop."""
  85. from gateway.session_api_turn import admit_api_steps
  86. store, lookup = _lookup_call(adapter, durable_key, request_id)
  87. if not (yield partial(store.bind_request_key, durable_key[0], binding, model=model)):
  88. raise RuntimeStoreError('admission_conflict')
  89. admitted = _restore_admitted((yield partial(store.atomically, *lookup)))
  90. if admitted is not None:
  91. return (*admitted, True)
  92. try:
  93. admitted = yield from admit_api_steps(adapter, request_id=request_id,
  94. **{'route_source': 'global', 'confirmed_runtime_lock': False,
  95. 'session_history_delivery': '', **run_kwargs})
  96. except Exception:
  97. # A definite pre-admission refusal does not reserve a poisoned identity. If storage
  98. # cannot prove absence, leave the binding intact for later recovery.
  99. if _restore_admitted((yield partial(store.atomically, *lookup))) is None:
  100. yield partial(store.forget_unadmitted_request, durable_key[0])
  101. raise
  102. yield partial(store.bind_request_admission, durable_key[0], admitted[2]['admission_id'])
  103. return (*admitted, False)
  104. async def _admit(adapter, durable_key, request_id, binding, run_kwargs, model):
  105. from gateway.session_api_turn import admit_api_turn_async
  106. *admitted, replay = await admit_api_turn_async(
  107. adapter, _admit_steps(adapter, durable_key, request_id, binding, run_kwargs, model))
  108. return admitted_context(tuple(admitted), replay=replay)
  109. def _stored_owner(adapter, session_id):
  110. """A stored response keeps the physical transcript tip its turn ended on, which is a
  111. compression child after an out-of-place compression. The canonical admission owner is
  112. the lineage root (the API binding, route and FIFO stay there), so a chained turn admits
  113. on that root; the legacy in-process path still continues the physical tip itself."""
  114. from gateway.session_authorities import active_authority
  115. authority = active_authority(adapter.gateway_runner) if session_id else None
  116. return authority.logical_owner(session_id) if authority is not None else session_id
  117. async def _bind_successor(adapter, session_id, run_kwargs):
  118. """Bind the session a chain moved to before any response starts. A streaming request has
  119. answered 200 by the time its turn admits, so the bind's retryable ``chain_moved`` refusal must
  120. happen here to reach the caller as a 503. The admission's own bind then finds this binding."""
  121. from gateway.session_api import bind_api_steps, run_steps_off_loop
  122. from gateway.session_authorities import active_authority
  123. authority = active_authority(adapter.gateway_runner)
  124. if authority is not None:
  125. try:
  126. await run_steps_off_loop(authority, bind_api_steps(
  127. authority, session_id, walked_from=run_kwargs['walked_from'],
  128. caller_key=run_kwargs['gateway_session_key']))
  129. except RuntimeStoreError:
  130. raise
  131. except Exception as exc:
  132. # A busy writer or I/O error: the caller maps store refusals only, and anything else
  133. # would reach the client as aiohttp's bare 500 instead of the retryable 503.
  134. logging.getLogger(__name__).warning('pre-stream bind of %s failed', session_id, exc_info=True)
  135. raise RuntimeStoreError('storage_unavailable') from exc
  136. async def prepare_context(adapter, request, body, gateway_session_key, durable_key, scope, key):
  137. """Validate first-time requests; exact retries use the accepted payload and result only."""
  138. import asyncio
  139. from gateway.platforms.api_server import (
  140. _auto_truncate_response_history, _content_has_visible_payload, _error_response)
  141. from gateway.platforms.api_server_openai_routes import _parse_responses_input, _parse_conversation_history
  142. request_id = f'responses:{scope}:{key}' if durable_key else None
  143. if durable_key:
  144. admitted = await find_admission(adapter, durable_key, request_id)
  145. if admitted is not None:
  146. return admitted_context(admitted, replay=True), None
  147. previous = body.get('previous_response_id')
  148. response_store = adapter._current_response_store()
  149. if body.get('conversation'):
  150. previous = await response_store.off_loop(response_store.get_conversation, body['conversation'])
  151. messages, error = _parse_responses_input(body['input'])
  152. if error is not None:
  153. return None, error
  154. history = []
  155. if body.get('conversation_history'):
  156. history, error = _parse_conversation_history(body['conversation_history'])
  157. if error is not None:
  158. return None, error
  159. stored_session_id = chain_root = None
  160. instructions = body.get('instructions')
  161. if not history and previous:
  162. stored = await response_store.off_loop(response_store.get, previous)
  163. if stored is None:
  164. return None, _error_response(f'Previous response not found: {previous}', 404)
  165. history = list(stored.get('conversation_history', []))
  166. chain_root = await asyncio.to_thread(_stored_owner, adapter, stored.get('session_id'))
  167. from gateway.platforms.api_server_authority_runs import continued_session
  168. stored_session_id, error = await continued_session(adapter, chain_root, request_id)
  169. if error is not None:
  170. return None, error
  171. if instructions is None:
  172. instructions = stored.get('instructions')
  173. history.extend(messages[:-1])
  174. user_message = messages[-1].get('content', '') if messages else ''
  175. if not _content_has_visible_payload(user_message):
  176. return None, _error_response('No user message found in input', 400)
  177. if body.get('truncation') == 'auto':
  178. history = _auto_truncate_response_history(history)
  179. declared_selected = not stored_session_id and bool(gateway_session_key)
  180. session_id = stored_session_id or await asyncio.to_thread(
  181. adapter._declared_conversation_session, gateway_session_key) or str(uuid.uuid4())
  182. if durable_key and not stored_session_id and not gateway_session_key:
  183. session_id = 'response-' + hashlib.sha256(f'{scope}\0{key}'.encode()).hexdigest()
  184. route, overrides, error = adapter._select_request_route(body, session_id=session_id,
  185. gateway_session_key=gateway_session_key, model_alias=body.get('model'))
  186. if error is not None:
  187. return None, error
  188. from gateway.platforms.api_server import _request_relay_metadata
  189. run_kwargs = dict(user_message=user_message, conversation_history=history,
  190. ephemeral_system_prompt=instructions, session_id=session_id,
  191. gateway_session_key=gateway_session_key, bind_declared_conversation=declared_selected,
  192. **overrides, route=route, relay_metadata=_request_relay_metadata(body))
  193. if stored_session_id and stored_session_id != chain_root:
  194. run_kwargs['walked_from'] = chain_root # the chain moved past its swept stored session
  195. if durable_key:
  196. return await _admit(adapter, durable_key, request_id, request_binding(adapter, request, durable_key),
  197. run_kwargs, body.get('model', adapter._model_name)), None
  198. if 'walked_from' in run_kwargs:
  199. await _bind_successor(adapter, session_id, run_kwargs)
  200. return dict(session_id=session_id, user_message=user_message, conversation_history=history,
  201. instructions=instructions, run_kwargs=run_kwargs, run_agent=adapter._run_agent), None