api_server_bot_chat.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. """Peer turns into the canonical Bot Chat under session authority.
  2. ``hermes peer dm`` (``/api/sessions/{id}/chat[/stream]``) and ``hermes peer run`` (``/v1/runs``)
  3. aim at the profile's Bot Chat. Under session authority that chat is a LOCAL session whose only
  4. writer is the authority FIFO — a Desktop showing it is a viewer, never a lease-holding executor —
  5. so the API edge admits the turn through the same door as ``bot_relay.deliver``
  6. (``gateway/session_bot.py``) and reads the reply from that admission's receipt. Binding the
  7. session as an API conversation (``run_api_turn``) would be a second writer and is refused
  8. (``permission_denied``) by design. Without an authority the legacy live-owner mailbox applies.
  9. """
  10. import asyncio
  11. import logging
  12. from pathlib import Path
  13. from types import SimpleNamespace
  14. import uuid
  15. from gateway.session_contract import SessionActor
  16. from hermes_state_runtime import RuntimeStoreError
  17. logger = logging.getLogger(__name__)
  18. _PENDING = ('queued', 'claimed')
  19. # Owner of a Bot Chat the gateway mints for a peer's create-if-missing (no native client asked).
  20. _PEER_SUBJECT = 'api-peer'
  21. # The whole body ``hermes peer dm`` sends (``hermes_cli/subcommands/peer.py::_ensure_bot_chat``).
  22. _PEER_CREATE_FIELDS = frozenset({'title', 'source'})
  23. def _authority(runner):
  24. from gateway.session_authorities import active_authority
  25. return active_authority(runner) if getattr(runner, 'session_authority', None) is not None else None
  26. async def create_peer_bot_chat(adapter, body):
  27. """``POST /api/sessions`` titled "Bot Chat" (``hermes peer dm``'s create-if-missing) under session
  28. authority: the owner mints the chat through the step a first ``bot_relay.deliver`` takes
  29. (``session_bot._create_bot_chat``), so a headless gateway's Bot Chat is a LOCAL session its FIFO
  30. owns, not an API row no remote sender can ever bind (#139370). None when the ordinary handler
  31. answers: no authority, another title, an explicit id, or the chat exists (its title conflict).
  32. The owner mints on the profile's own policy, so a create carrying anything else (a model
  33. lock, route, options, system prompt) is refused by name before anything is written."""
  34. from tools.bot_mode_probe import BOT_CHAT_TITLE
  35. authority = _authority(adapter.gateway_runner)
  36. if (authority is None or str(body.get('title') or '').strip() != BOT_CHAT_TITLE
  37. or body.get('id') or body.get('session_id')):
  38. return None
  39. from aiohttp import web
  40. from gateway.platforms.api_server import _error_response
  41. if unsupported := sorted(set(body) - _PEER_CREATE_FIELDS):
  42. return _error_response(
  43. f"Bot Chat creation refused: bot_chat_parameters_unsupported ({', '.join(unsupported)}); "
  44. "the Bot Chat is created with the profile's own model and prompt", 400,
  45. code='bot_chat_parameters_unsupported')
  46. from gateway.session_bot import _create_bot_chat
  47. from gateway.session_runtime_workers import track_mutation
  48. actor = SessionActor(_PEER_SUBJECT, authority.profile_id,
  49. frozenset({'session:create', 'session:read', 'session:submit'}), 'api-peer-dm')
  50. try:
  51. authority._require_admission_open()
  52. ref = await asyncio.shield(track_mutation(authority, _create_bot_chat(authority, actor)))
  53. except RuntimeStoreError as exc:
  54. return _error_response(f'Bot Chat creation refused: {exc.reason}', 503, code=exc.reason)
  55. if ref is None:
  56. return None
  57. row = await asyncio.to_thread(authority.db.get_session, ref.session_id)
  58. return web.json_response({'object': 'hermes.session', 'session': adapter._session_response(row)}, status=201)
  59. def _refusal(authority, tip, exc):
  60. """``(reason, error)`` for a refused peer turn. A ``not_found`` on a Bot Chat that is no local
  61. chat is named: a remote sender never binds unowned history, so a bare code would leave the
  62. peer nothing to act on. The full clause stays in gateway.log."""
  63. reason = exc.reason
  64. if reason == 'not_found' and not str(tip.get('chat_id') or '').startswith('local-'):
  65. from hermes_state_local_migration import validate_legacy_row
  66. try:
  67. validate_legacy_row(authority.db, tip)
  68. reason, hint = 'bot_chat_unbound', 'open it once with `hermes chat -c "Bot Chat"` on that host'
  69. except RuntimeStoreError:
  70. from gateway.session_bot import displaceable_bot_chat
  71. reason = 'bot_chat_not_local'
  72. if displaceable_bot_chat(authority, tip) is not None:
  73. hint = "it is a conversation the gateway cannot own; it is moved aside as 'Bot Chat (<source> <id>)' on the next delivery"
  74. else: # local history a peer may not adopt (a served remote login's Desktop row, another profile's): never moved
  75. hint = 'it is local history owned by another login or profile; open or rename it from the client that owns it'
  76. logger.warning('Peer DM into Bot Chat %s refused: %s (not_found: field=%s detail=%s)',
  77. tip['id'], reason, exc.field, exc.detail)
  78. return reason, f"Bot Chat admission refused: {reason} (this profile's 'Bot Chat' is not a local chat; {hint})"
  79. return reason, f'Bot Chat admission refused: {reason}'
  80. async def reset_local_title_owner(db, title, include_hidden, source=None):
  81. """``[row]`` for the exact-title lookup of a LOCAL conversation that was reset (``/new``, the
  82. compression-exhaustion auto-reset), else ``[]``. The listing shows a reset lineage as its latest,
  83. untitled segment, so ``hermes peer dm`` stopped finding its Bot Chat and its create collided
  84. with the title. The titled creation id still owns the FIFO and is what the chat door admits.
  85. It applies the listing's own filters: hidden only with ``include_hidden``, never an archived
  86. row (a deliberate archive stays put; the reaper-archive resurrection is the caller's), and
  87. ``source`` when one is given."""
  88. return await asyncio.to_thread(_reset_local_title_owner, db, title, include_hidden, source) if title else []
  89. def _reset_local_title_owner(db, title, include_hidden, source=None):
  90. row = db.get_session_by_title(title)
  91. if (row is None or row.get('end_reason') != 'session_reset' or row.get('archived')
  92. or (row.get('hidden') and not include_hidden) or (source and row.get('source') != source)):
  93. return []
  94. from hermes_state_local import local_receipt
  95. try:
  96. lineage = local_receipt(db, row['id']).get('lineage') or []
  97. except RuntimeStoreError:
  98. return []
  99. return [row] if len(lineage) > 1 else []
  100. def bot_chat_tip(authority, session_id):
  101. """The canonical Bot Chat's tip row when *session_id* is in its lineage, else None."""
  102. db = authority.db
  103. row = db.get_session_by_title('Bot Chat')
  104. if row is None:
  105. return None
  106. tip = db.get_compression_tip(row['id'])
  107. if not tip or db.get_compression_tip(session_id) != tip:
  108. return None
  109. return db.get_session(tip)
  110. async def admit_peer_turn(runner, session_id, message, author):
  111. """``(profile home, receipt)`` when *session_id* is the Bot Chat and the turn was admitted to its
  112. FIFO; None when the target is any other session (the caller runs it as an API turn)."""
  113. from gateway.session_authorities import served_profile_name
  114. from gateway.session_bot import deliver
  115. authority = _authority(runner)
  116. if authority is None:
  117. return None
  118. tip = bot_chat_tip(authority, session_id)
  119. if tip is None:
  120. return None
  121. home = Path(authority.db.db_path).parent.resolve()
  122. from gateway.session_bot import displaceable_bot_chat
  123. if displaceable_bot_chat(authority, authority.db.get_session_by_title('Bot Chat') or tip) is not None:
  124. # The title is held by an API conversation no FIFO can own (#140769, MSG-21): the owner
  125. # moves it aside and mints the local Bot Chat for the peer, as ``create_peer_bot_chat`` does.
  126. actor = SessionActor(_PEER_SUBJECT, authority.profile_id,
  127. frozenset({'session:create', 'session:read', 'session:submit'}), 'api-peer-dm')
  128. else:
  129. # The API key already authenticated the peer; the turn enters the chat as its owner's own
  130. # input, the way owner recovery re-admits queued deliveries (``recover_bot_deliveries``).
  131. actor = SessionActor(tip['user_id'], authority.profile_id,
  132. frozenset({'session:submit', 'session:read'}), 'api-peer-dm')
  133. key = uuid.uuid4().hex
  134. params = dict(id=key, profile=served_profile_name(home), message=message)
  135. if author is not None:
  136. params['author'] = dict(author)
  137. try:
  138. record = await deliver(SimpleNamespace(authority=authority, actor=actor), params)
  139. except RuntimeStoreError as exc:
  140. reason, error = _refusal(authority, tip, exc)
  141. record = {'status': 'failed', 'delivery_id': key, 'reason': reason, 'error': error}
  142. return home, record
  143. async def await_live_delivery(adapter, home, record, timeout, *, should_stop=None):
  144. """Wait for a live Bot Chat receipt through whichever door admitted it: the authority FIFO
  145. (in-process waiter) or the legacy owner mailbox (``await_delivery_async``)."""
  146. authority = _authority(adapter.gateway_runner)
  147. if authority is not None and record.get('admission_id'):
  148. return await await_peer_receipt(authority, record, timeout, should_stop=should_stop)
  149. from tools.bot_live_delivery import await_delivery_async
  150. return await await_delivery_async(home, record['delivery_id'], timeout, should_stop=should_stop) or record
  151. async def await_peer_receipt(authority, record, timeout, *, should_stop=None):
  152. """The delivery's receipt after it settles for good, *timeout* lapses, or *should_stop* says so.
  153. Each pass re-reads which admission is live (``peer_wait_admission``): after a transient failure
  154. the owner admits ONE retry, whose id is the only future that fires again; the original's interim
  155. ``failed`` (retry still eligible) reads as pending, never as the answer."""
  156. from gateway.session_bot import peer_wait_admission
  157. if record['status'] not in _PENDING:
  158. return record
  159. loop = asyncio.get_running_loop()
  160. deadline = None if timeout is None else loop.time() + timeout
  161. while True:
  162. live, interim = await asyncio.to_thread(peer_wait_admission, authority, record)
  163. # Register the waiter BEFORE re-reading: a settle between the two either shows in the read
  164. # or resolves this future; a settle before both would otherwise leave a future nobody pops.
  165. waiter = None if live is None or interim else authority.waiters.setdefault(live, loop.create_future())
  166. current = _receipt(authority, record)
  167. if live is None and current['status'] in _PENDING:
  168. continue # The derived retry committed while the receipt was being read.
  169. if live == record['admission_id'] and not interim and current['status'] == 'failed' \
  170. and not current.get('retry_admission_id'):
  171. continue # The original settled failed after the live read: re-derive (its retry may be owed).
  172. if interim:
  173. current['status'] = 'claimed' # the retry is being admitted
  174. if live is None or current['status'] not in _PENDING:
  175. await _owner_published(authority, record, deadline)
  176. return current
  177. if should_stop is not None and should_stop():
  178. return current
  179. remaining = None if deadline is None else deadline - loop.time()
  180. if remaining is not None and remaining <= 0:
  181. return current
  182. tick = 0.05 if interim else (0.5 if should_stop is not None else remaining)
  183. if waiter is None:
  184. await asyncio.sleep(tick if remaining is None else min(tick, remaining))
  185. else:
  186. await asyncio.wait([waiter], timeout=tick if remaining is None else min(tick, remaining))
  187. async def _owner_published(authority, record, deadline):
  188. """Answer only once the owner's reply watcher published the outcome this waiter read from the
  189. FIFO: its receipt write runs off-loop, and a re-read of the delivery must not say ``queued``
  190. after the sender already holds the reply."""
  191. watcher = getattr(authority, '_bot_reply_watchers', {}).get(record['delivery_id'])
  192. if watcher is not None and not watcher[1].done():
  193. remaining = None if deadline is None else max(0.0, deadline - asyncio.get_running_loop().time())
  194. await asyncio.wait([watcher[1]], timeout=remaining)
  195. def _receipt(authority, record):
  196. from gateway.session_bot import _result
  197. return {**record, **_result(authority, record)}