session_producer_lifecycle.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """The adapter processing lifecycle of a producer-admitted turn (relay, webhook: ``admit_producer``).
  2. A chat message an adapter hands to ``handle_message`` is bracketed by
  3. ``BasePlatformAdapter._process_message_background``: the typing refresh, ``on_processing_start``,
  4. and ``on_processing_complete`` once its reply was sent. A producer delivery is only committed at
  5. ingress (off the transport reader, acknowledged on the commit) and the authority's drain runs it, so
  6. no adapter task brackets it. The executor does, for those admissions only (a messaging admission's
  7. own adapter task already brackets it):
  8. - opened when the admission starts executing, not when it commits: a delivery queued behind a
  9. running turn is acknowledged when it runs, as the in-band queue's are (``run_turn_followup_ack``);
  10. - typing lives as long as execution, so it stops before the reply is sent (#117300 pauses it there);
  11. - the outcome is reported once: an interrupted turn as CANCELLED and a failed one as FAILURE when
  12. execution ends; a completed one after its settlement, by the reply's send (``deliver_settled``),
  13. or as FAILURE when settlement dropped the reply (``drop_producer_turn``).
  14. """
  15. import asyncio
  16. from contextlib import asynccontextmanager
  17. from gateway.platforms.event import ProcessingOutcome
  18. def _turns(authority):
  19. """admission id -> None (committed by a producer, not started) or its open ``_Bracket``."""
  20. turns = getattr(authority, 'producer_turns', None)
  21. if turns is None:
  22. turns = authority.producer_turns = {}
  23. return turns
  24. def mark_producer_admission(authority, event, row):
  25. """``SessionAuthority._admitted``, in the commit's own tracked task: a producer's freshly queued
  26. row is the executor's to bracket. A re-send of a row already running or settled is not marked."""
  27. if getattr(event, '_producer_delivery', False) and row['status'] == 'queued':
  28. _turns(authority).setdefault(row['admission_id'], None)
  29. class _Bracket:
  30. def __init__(self, adapter, event, hooks):
  31. from gateway.platforms.base import _reply_anchor_for_event, _thread_metadata_for_event
  32. from gateway.platforms.helpers import typing_metadata_for_turn
  33. self.adapter, self.event, self.hooks = adapter, event, hooks
  34. # The status anchor is this turn's: a message that reaches the chat later must not move it.
  35. self.metadata = typing_metadata_for_turn(
  36. adapter, event.source.chat_id, _reply_anchor_for_event(event), _thread_metadata_for_event(event))
  37. self.stop = asyncio.Event()
  38. self.typing = None
  39. self.closed = False
  40. async def stop_typing(self):
  41. task, self.typing = self.typing, None
  42. if task is not None:
  43. self.stop.set()
  44. await self.adapter._stop_typing_refresh(self.event.source.chat_id, task, metadata=self.metadata)
  45. async def close(self, outcome):
  46. if self.closed:
  47. return
  48. self.closed = True
  49. await self.stop_typing()
  50. if self.hooks:
  51. await self.adapter._run_processing_hook('on_processing_complete', self.event, outcome)
  52. async def _open(authority, row, event):
  53. """The bracket of a producer-admitted row starting to execute, else None: an adapter that
  54. renders neither typing nor the processing hooks (a webhook) is left alone."""
  55. turns = _turns(authority)
  56. if row['admission_id'] not in turns:
  57. return None
  58. from gateway.platforms.base import BasePlatformAdapter
  59. from gateway.run_turn_followup_ack import _followup_processing_hooks_apply
  60. adapter = authority.runner._adapter_for_source(event.source)
  61. typing = adapter is not None and type(adapter).send_typing is not BasePlatformAdapter.send_typing
  62. hooks = _followup_processing_hooks_apply(adapter, event)
  63. if not (typing or hooks):
  64. turns.pop(row['admission_id'], None)
  65. return None
  66. bracket = turns[row['admission_id']] = _Bracket(adapter, event, hooks)
  67. if typing:
  68. # None for a heartbeat or ``typing_indicator: false``, as on the adapter path.
  69. bracket.typing = adapter._start_typing_refresh(event, bracket.stop, bracket.metadata)
  70. if hooks:
  71. try:
  72. await adapter._run_processing_hook('on_processing_start', event)
  73. except BaseException:
  74. # Cancelled before ``producer_turn``'s finally exists: release what was opened here.
  75. bracket.stop.set()
  76. if bracket.typing is not None:
  77. bracket.typing.cancel()
  78. turns.pop(row['admission_id'], None)
  79. raise
  80. return bracket
  81. def _executed_outcome(captured):
  82. """The outcome settlement will commit (``session_results.prepare_result``), as a hook outcome."""
  83. result = captured.get('terminal_result', captured.get('result')) or {}
  84. if result.get('interrupted'):
  85. return ProcessingOutcome.CANCELLED
  86. if result.get('failed') or result.get('error'):
  87. return ProcessingOutcome.FAILURE
  88. return ProcessingOutcome.SUCCESS
  89. @asynccontextmanager
  90. async def producer_turn(authority, row, event, captured):
  91. """Around ``execute_admission``'s run of *row*: ``captured`` is its ``execution_result``."""
  92. bracket = await _open(authority, row, event)
  93. if bracket is None:
  94. yield
  95. return
  96. outcome = ProcessingOutcome.FAILURE
  97. try:
  98. yield
  99. outcome = _executed_outcome(captured)
  100. finally:
  101. await bracket.stop_typing()
  102. if outcome is not ProcessingOutcome.SUCCESS:
  103. await close_producer_turn(authority, row['admission_id'], outcome)
  104. async def close_producer_turn(authority, admission_id, outcome):
  105. """Report a settled producer turn's outcome (once; a no-op for any other admission)."""
  106. bracket = _turns(authority).pop(admission_id, None)
  107. if bracket is not None:
  108. await bracket.close(outcome)
  109. def drop_producer_turn(authority, admission_id):
  110. """The admission settled without its reply (unknown, discarded) or was cancelled before it ran:
  111. a started turn reports FAILURE, off the caller's path."""
  112. bracket = _turns(authority).pop(admission_id, None)
  113. if bracket is None or bracket.closed:
  114. return
  115. task = asyncio.get_running_loop().create_task(bracket.close(ProcessingOutcome.FAILURE))
  116. bracket.adapter._background_tasks.add(task)
  117. task.add_done_callback(bracket.adapter._background_tasks.discard)