run_turn_followup_ack.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Processing-lifecycle hooks for runner-drained queued follow-ups.
  2. A message that arrives mid-turn is parked in the adapter's pending slot and drained in-band by
  3. ``TurnRunner._run_agent_queued_followup``, never by ``BasePlatformAdapter._process_message_background``
  4. — the only other call site for ``on_processing_start`` / ``on_processing_complete``. Without firing
  5. them here every adapter that renders a read-receipt reaction from the hooks silently skips queued,
  6. interrupting and steer-demoted messages (#72502, salvage #72503).
  7. """
  8. from __future__ import annotations
  9. import asyncio
  10. from gateway.platforms.base import BasePlatformAdapter, MessageEvent, ProcessingOutcome
  11. def _followup_processing_hooks_apply(adapter, event: MessageEvent | None) -> bool:
  12. """Both conditions are necessary: a real inbound platform message to acknowledge (adapters key their
  13. marker off ``message_id`` — Slack/Telegram/Feishu/Matrix/Photon — or off ``raw_message`` — Signal,
  14. Discord; synthetic drains such as ``/goal`` continuations, wake-ups and startup auto-resume carry
  15. neither and must stay silent), and an adapter that overrides ``on_processing_start``. We bracket, so
  16. both halves must belong to us: a complete-only adapter (Google Chat reaps its typing card there,
  17. webhook ends its per-delivery session) would otherwise be handed a completion for a turn whose reply
  18. is delivered only after the drain chain unwinds back into ``_process_message_background``."""
  19. if adapter is None or event is None:
  20. return False
  21. if not (getattr(event, "message_id", None) or getattr(event, "raw_message", None)):
  22. return False
  23. start_hook = getattr(type(adapter), "on_processing_start", None)
  24. return start_hook is not None and start_hook is not BasePlatformAdapter.on_processing_start
  25. def _followup_cancel_outcome(adapter) -> ProcessingOutcome:
  26. """Classify a cancelled follow-up exactly as ``_process_message_background`` does: only cancels the
  27. adapter itself routed (``/stop``, ``/new``, ``/reset``, cleanup) are CANCELLED, anything else is a
  28. failure. Signal and Matrix leave the in-progress marker in place on CANCELLED, so reporting an
  29. unexpected cancellation as CANCELLED would strand it."""
  30. expected = getattr(adapter, "_expected_cancelled_tasks", None)
  31. if expected is None:
  32. return ProcessingOutcome.FAILURE
  33. try:
  34. current = asyncio.current_task()
  35. except RuntimeError:
  36. current = None
  37. if current is None:
  38. return ProcessingOutcome.FAILURE
  39. try:
  40. return ProcessingOutcome.CANCELLED if current in expected else ProcessingOutcome.FAILURE
  41. except TypeError:
  42. return ProcessingOutcome.FAILURE
  43. async def _run_followup_processing_hook(adapter, event: MessageEvent | None, hook_name: str, *args) -> None:
  44. """Fire one lifecycle hook for a runner-drained follow-up; no-op per ``_followup_processing_hooks_apply``."""
  45. if not _followup_processing_hooks_apply(adapter, event):
  46. return
  47. run_hook = getattr(adapter, "_run_processing_hook", None)
  48. if not callable(run_hook):
  49. return
  50. await run_hook(hook_name, event, *args)