gateway_tools_notify.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. """Background-process notification turns the gateway admits for this CLI's session (W11).
  2. On main the classic CLI drained ``tools.process_registry`` itself, queued a
  3. ``ProcessNotificationBatch`` into ``_pending_input`` and ran the ``[IMPORTANT: Background process
  4. ...]`` turn. Over the session authority the process runs in the gateway: the gateway drains the
  5. completion, admits the notification turn (``authority.admit_automation``) and runs it. The CLI
  6. neither drains the registry nor claims delivery (IPS-086) — it only watches its session's stream.
  7. The claim's ``message.start`` carries the admission's async-result card (``display_kind`` /
  8. ``display_text`` / model ``text``; ``gateway.session_automation.admission_display``). The idle tick
  9. turns it into main's ``TimelineNotification``, so main's input path paints the same ``◈ Background
  10. Process Finished: ...`` header, and ``GatewayAgent.run_conversation`` pumps that already-admitted
  11. turn (no ``prompt.submit``) through the same callbacks as a typed turn.
  12. """
  13. from __future__ import annotations
  14. import queue
  15. def gateway_store(cli):
  16. """The CLI's gateway session store, or None (a test double / an unavailable store)."""
  17. from hermes_cli.gateway_session_store import GatewaySessionStore
  18. store = getattr(cli, "_session_db", None)
  19. return store if isinstance(store, GatewaySessionStore) else None
  20. def _card(params):
  21. payload = params.get("payload")
  22. if not isinstance(payload, dict):
  23. return None
  24. kind, display, text = payload.get("display_kind"), payload.get("display_text"), payload.get("text")
  25. if not kind or not isinstance(display, str) or not display.strip() or not isinstance(text, str) or not text:
  26. return None
  27. return payload
  28. def queue_admitted_turn(cli, store) -> None:
  29. """Idle tick: find a gateway-admitted notification turn's ``message.start`` for this session and
  30. queue main's ``TimelineNotification`` for it. Frames before it are the between-turn leftovers
  31. ``run_conversation`` would discard anyway; frames after it stay queued for the pump."""
  32. from tools.process_registry_notifications import TimelineNotification
  33. connection, session_id = store.connection, getattr(cli, "session_id", None)
  34. state = connection.state(session_id)
  35. if _adoption_waiting(cli, state):
  36. return # an adopted /queue turn is waiting for the input loop: its frames stay queued for its pump
  37. state.admitted_turn = None # a header queued earlier whose turn never ran is stale now
  38. events = connection.events
  39. while True:
  40. try:
  41. frame = events.get_nowait()
  42. except queue.Empty:
  43. recover_queued(cli, store)
  44. return
  45. if isinstance(frame, Exception):
  46. from hermes_cli.gateway_connection import lost_before
  47. if lost_before(frame, getattr(connection, "epoch", None)):
  48. continue # a connection a reconnect already replaced
  49. events.put(frame) # the disconnect belongs to the next turn
  50. return
  51. params = frame.get("params") if isinstance(frame, dict) else None
  52. if not isinstance(params, dict) or params.get("session_id") != session_id:
  53. continue
  54. if params.get("type") == "review.summary":
  55. # The background review (/refine, the post-turn review) reports after its turn settled.
  56. from hermes_cli.gateway_agent import review_summary_line
  57. line = review_summary_line(params.get("payload"))
  58. if line:
  59. cli._agent_status_print(line)
  60. continue
  61. if params.get("type") != "message.start" or not params.get("admission_id"):
  62. continue
  63. queued_text = state.queued_inputs.get(params["admission_id"])
  64. if queued_text is not None:
  65. # A ``/queue`` prompt this viewer admitted (#134889 N5): the owner runs it now, and main's
  66. # input path paints and pumps it as a typed turn. Literal: it is a prompt, never a command.
  67. # Adopted by admission id (the item carries it), never by text: the input path may rewrite
  68. # the text, and a second ``prompt.submit`` would run the prompt twice.
  69. from cli import _SeededQueryMessage
  70. _adopt(cli, state, {"admission_id": params["admission_id"],
  71. "execution_generation": params.get("execution_generation")}, queued_text)
  72. return
  73. card = _card(params)
  74. if card is None:
  75. continue
  76. state.admitted_turn = {"admission_id": params["admission_id"], "text": card["text"],
  77. "execution_generation": params.get("execution_generation")}
  78. _put_first(cli._pending_input, TimelineNotification(
  79. card["text"], card["display_text"], card["display_kind"],
  80. card.get("notification_category") or "result"))
  81. return
  82. def _adopt(cli, state, adopted, text):
  83. from cli import _SeededQueryMessage
  84. state.missed.discard(adopted["admission_id"])
  85. state.adopted[adopted["admission_id"]] = adopted
  86. _put_first(cli._pending_input, _SeededQueryMessage(text, admission_id=adopted["admission_id"]))
  87. def _adoption_waiting(cli, state) -> bool:
  88. """An adopted item is still in ``_pending_input`` (one ``/queue clear`` dropped is forgotten)."""
  89. if not state.adopted:
  90. return False
  91. pending = getattr(cli, "_pending_input", None)
  92. if pending is None:
  93. return False
  94. with pending.mutex:
  95. waiting = {getattr(item, "admission_id", None) for item in pending.queue}
  96. waiting.add(state.armed_admission) # taken by the input loop, its run_conversation not started yet
  97. for admission in [a for a in state.adopted if a not in waiting]:
  98. state.adopted.pop(admission, None)
  99. return bool(state.adopted)
  100. _NEVER_RAN = {"cancelled", "rejected"}
  101. def recover_queued(cli, store) -> None:
  102. """Idle tick, after the stream had no ``message.start`` for them: this viewer's ``/queue``
  103. admissions the owner started or settled without this viewer painting the turn (a local turn's
  104. drain or pump dropped the start, or it ran across a reconnect, which replays no frames). Read on
  105. a new published pending list or a dropped start only, never every tick.
  106. Still running: adopted and pumped live, its reply completed from the receipt (``recovered``).
  107. Settled: adopted with its saved result, painted without a pump. One that never ran (cancelled,
  108. rejected) is forgotten: the only point where ``queued_inputs`` drops a row unpainted.
  109. """
  110. from hermes_cli.gateway_client import GatewayClientError
  111. state = store.connection.state(getattr(cli, "session_id", None))
  112. if not state.queued_inputs or (state.recovered_seq == state.pending_seq and not state.missed):
  113. return
  114. state.recovered_seq = state.pending_seq
  115. listed = {row.get("admission_id"): row.get("status") for row in state.pending or () if isinstance(row, dict)}
  116. for admission, text in list(state.queued_inputs.items()):
  117. if admission in state.adopted or (admission not in state.missed and listed.get(admission) == "queued"):
  118. continue
  119. try:
  120. receipt = store.call("prompt.receipt", session_id=cli.session_id, admission_id=admission,
  121. include_result=True) or {}
  122. except GatewayClientError:
  123. state.recovered_seq = -1 # unread: try again on the next tick
  124. continue
  125. status = receipt.get("status")
  126. if status == "queued":
  127. state.missed.discard(admission)
  128. continue
  129. if status == "terminal" and receipt.get("outcome") in _NEVER_RAN:
  130. state.queued_inputs.pop(admission, None)
  131. state.missed.discard(admission)
  132. continue
  133. if status not in ("started", "terminal"):
  134. # unknown: the owner resolves it (``/discard`` or recovery settles it terminal, and the next
  135. # read forgets or paints it). Until then it stays here and is re-read once per new pending
  136. # list, never every tick; the row is rare and the read is one receipt.
  137. continue
  138. adopted = {"admission_id": admission, "execution_generation": receipt.get("execution_generation"),
  139. "recovered": True}
  140. if status == "terminal":
  141. adopted["receipt"] = receipt
  142. _adopt(cli, state, adopted, text)
  143. return # one turn at a time: the next one is recovered after this one ran
  144. def missed_start(state, params) -> None:
  145. """A frame dropped outside its turn: a ``/queue`` admission's ``message.start`` is remembered so
  146. the idle tick paints that turn from its receipt instead of losing it."""
  147. if params.get("type") == "message.start" and params.get("admission_id") in state.queued_inputs:
  148. state.missed.add(params["admission_id"])
  149. def _put_first(pending, item):
  150. """Ahead of anything already queued: the gateway is running this turn NOW (typed inputs the
  151. post-turn path re-queued are admitted behind it), and a typed turn run first would drop it."""
  152. with pending.mutex:
  153. pending.queue.appendleft(item)
  154. pending.unfinished_tasks += 1
  155. pending.not_empty.notify()
  156. def arm_adopted(cli, item) -> None:
  157. """The input loop took ``item``: when it is an adopted ``/queue`` admission, the next
  158. ``run_conversation`` pumps exactly that admission (``take_admitted_turn``)."""
  159. admission = getattr(item, "admission_id", None)
  160. store = gateway_store(cli) if admission else None
  161. if store is not None:
  162. store.connection.state(getattr(cli, "session_id", None)).armed_admission = admission
  163. def take_admitted_turn(agent, text):
  164. """The admitted turn ``run_conversation(text)`` replays instead of submitting; None (and the
  165. stale notification adoption dropped) for any other turn. A ``/queue`` admission is matched by the
  166. id ``arm_adopted`` recorded, a notification turn by its model text."""
  167. state = agent._state()
  168. armed, state.armed_admission = getattr(state, "armed_admission", None), None
  169. adopted = state.adopted.pop(armed, None) if armed else None
  170. if adopted is not None:
  171. state.queued_inputs.pop(armed, None) # painted now: the admission's turn is this one
  172. return adopted
  173. adopted = getattr(state, "admitted_turn", None)
  174. state.admitted_turn = None
  175. if not adopted or adopted.get("text") != text:
  176. return None
  177. return adopted