cli_process_notifications.py 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """CLI notification ownership, structured queueing and last-moment consumption."""
  2. class CLIProcessNotificationsMixin:
  3. def _owns_process_notification(self, event: dict) -> bool:
  4. """Whether this session owns a delegation event (pre-compression keys resolve to their continuation; fail closed)."""
  5. event_key = str(event.get("session_key") or "")
  6. current_key = str(getattr(self, "session_id", "") or "")
  7. if not event_key or not current_key:
  8. return False
  9. if event_key == current_key:
  10. return True
  11. try:
  12. session_db = getattr(self, "_session_db", None)
  13. resolved_key = (
  14. session_db.resolve_resume_session_id(event_key) if session_db is not None else event_key
  15. ) or event_key
  16. except Exception:
  17. resolved_key = event_key
  18. return str(resolved_key) == current_key
  19. def _background_notifications_suppressed(self) -> bool:
  20. """Whether ``display.background_process_notifications`` is ``off`` for this CLI session.
  21. The key gates the gateway's completion injection (#9290) but the CLI drain never consulted
  22. it, so the documented ``off`` escape hatch silently did nothing here (#123114). Mirrors the
  23. gateway semantics: events are still drained, claimed and acknowledged — only the
  24. turn-starting injection is suppressed."""
  25. try:
  26. from cli import CLI_CONFIG
  27. mode = str((CLI_CONFIG.get("display") or {}).get("background_process_notifications") or "").strip().lower()
  28. except Exception:
  29. return False
  30. return mode == "off"
  31. def _drain_process_notifications(self, consumer: str) -> None:
  32. from hermes_cli.gateway_tools_notify import gateway_store, queue_admitted_turn
  33. store = gateway_store(self)
  34. if store is not None:
  35. # The process registry and its delivery claims live in the gateway, which admits the
  36. # notification turn itself; this viewer only paints it (IPS-086).
  37. queue_admitted_turn(self, store)
  38. return
  39. from tools.process_registry import process_registry
  40. from tools.async_delegation import claim_event_delivery, complete_event_delivery
  41. from tools.process_registry_notifications import (
  42. HEARTBEAT_DISPLAY_KIND, ProcessNotificationBatch, TimelineNotification, group_process_notifications,
  43. heartbeat_display_text)
  44. claimed = []
  45. for event, text in process_registry.drain_notifications(
  46. session_key=getattr(self, "session_id", "") or "", owns_event=self._owns_process_notification,
  47. ):
  48. claim = claim_event_delivery(event, consumer)
  49. if claim is None:
  50. continue
  51. claimed.append((event, text))
  52. complete_event_delivery(event, claim)
  53. if self._background_notifications_suppressed():
  54. # Subagent results are not process notifications: they still land.
  55. claimed = [(event, text) for event, text in claimed if event.get("type") == "async_delegation"]
  56. for notifications in group_process_notifications(claimed):
  57. event, text = notifications[0]
  58. evt_type = event.get("type", "completion")
  59. if evt_type == "completion":
  60. pending = ProcessNotificationBatch(notifications)
  61. elif evt_type == "heartbeat":
  62. pending = TimelineNotification(text, heartbeat_display_text(event), HEARTBEAT_DISPLAY_KIND)
  63. else:
  64. pending = TimelineNotification.for_delegation(text, event) if evt_type == "async_delegation" else text
  65. from agent.notification_presentation import diagnostic_process_event
  66. if diagnostic_process_event(event) and not isinstance(pending, TimelineNotification):
  67. pending = TimelineNotification(text, text, "internal_notification", "diagnostic")
  68. self._pending_input.put(pending)
  69. def _tui_unwrap_input(self, user_input):
  70. """Unwrap ``_VoiceInputMessage`` / ``_SeededQueryMessage`` -> ``(text_or_tuple, is_voice_input, is_seeded_query)``."""
  71. from cli import _VoiceInputMessage, _SeededQueryMessage
  72. from tools.process_registry import process_registry
  73. from tools.process_registry_notifications import (
  74. PROCESS_COMPLETE_DISPLAY_KIND, ProcessNotificationBatch, TimelineNotification)
  75. if isinstance(user_input, ProcessNotificationBatch):
  76. rendered = user_input.render(process_registry)
  77. user_input = rendered and TimelineNotification(
  78. rendered, user_input.display_text(process_registry), PROCESS_COMPLETE_DISPLAY_KIND)
  79. # Voice-transcribed messages arrive wrapped in a sentinel so only genuine STT output gets the voice
  80. # prefix (#65827).
  81. is_voice_input = isinstance(user_input, _VoiceInputMessage)
  82. if is_voice_input:
  83. user_input = user_input.text
  84. is_seeded_query = isinstance(user_input, _SeededQueryMessage)
  85. if is_seeded_query:
  86. from hermes_cli.gateway_tools_notify import arm_adopted
  87. arm_adopted(self, user_input)
  88. user_input = (user_input.text, user_input.images) if user_input.images else user_input.text
  89. return user_input, is_voice_input, is_seeded_query