warning_notifications.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """Delivery policy for engine diagnostics, never assistant or command responses."""
  2. from gateway.display_config import resolve_display_setting
  3. class DiagnosticText(str):
  4. """Producer-owned classification on the legacy two-argument status callback.
  5. String behavior and event kind stay unchanged for existing plugin renderers.
  6. Durable carriers must serialize their own category, not this in-memory marker.
  7. """
  8. def is_warning_status(event_type: str, message: str) -> bool:
  9. return event_type == "warn" or isinstance(message, DiagnosticText)
  10. def is_diagnostic_notice(notice) -> bool:
  11. """Out-of-band ``AgentNotice`` classification shared by the gateway, TUI and CLI sinks.
  12. Credit-service notices are automatic diagnostics at every level (usage bands are ``info``,
  13. ``credits.restored`` is ``success``); everything else is diagnostic only when it warns.
  14. """
  15. return (getattr(notice, "level", None) in {"warn", "error"}
  16. or str(getattr(notice, "key", "") or "").startswith("credits."))
  17. def diagnostic_metadata(event) -> dict:
  18. """``{"notification_category": "diagnostic"}`` for a trusted diagnostic-only wake, else ``{}``.
  19. Only internal wakes may classify a turn; human content never does.
  20. """
  21. if getattr(event, "internal", False) and (getattr(event, "metadata", None) or {}).get(
  22. "notification_category") == "diagnostic":
  23. return {"notification_category": "diagnostic"}
  24. return {}
  25. def effective_user_config() -> dict:
  26. """The active profile's effective config, or ``{}`` when it cannot be read (presentation fails open)."""
  27. from hermes_cli.config_effective import load_user_config_effective
  28. try:
  29. config = load_user_config_effective()
  30. except Exception:
  31. return {}
  32. return config if isinstance(config, dict) else {}
  33. def diagnostic_turn_muted(display_metadata, platform, user_config=None) -> bool:
  34. """One admission rule for every surface: a diagnostic-category wake mutes its turn's
  35. presentation only when the owning policy hides diagnostics. Human content never mutes."""
  36. return ((display_metadata or {}).get("notification_category") == "diagnostic"
  37. and not warning_notifications_enabled(platform, user_config))
  38. def diagnostic_wake_muted(event, user_config=None) -> bool:
  39. """Only trusted diagnostic-only wakes can mute a turn, never human content."""
  40. snapshot = getattr(event, "_notification_reply_muted", None)
  41. if isinstance(snapshot, bool):
  42. return snapshot
  43. return bool(getattr(event, "internal", False)) and diagnostic_turn_muted(
  44. getattr(event, "metadata", None), event.source.platform, user_config)
  45. def render_notification(render, *, platform, diagnostic=True, user_config=None) -> bool:
  46. """Invoke a synchronous UI renderer only when its classified content is visible.
  47. Return whether the renderer ran, not whether a transport delivered anything.
  48. Call only at a presentation sink, never around producer callbacks or persistence.
  49. The caller supplies the owning scope/turn snapshot; exceptions remain its policy.
  50. """
  51. if diagnostic and not warning_notifications_enabled(platform, user_config):
  52. return False
  53. render()
  54. return True
  55. async def present_notification(present, *, platform, diagnostic=True, user_config=None) -> bool:
  56. """Async twin of :func:`render_notification` for lifecycle emitters that await a send.
  57. Return whether the presenter ran; its own receipt/exception is the caller's to interpret.
  58. The caller binds the owning profile scope BEFORE calling (watchers/shutdown fan-outs).
  59. """
  60. if diagnostic and not warning_notifications_enabled(platform, user_config):
  61. return False
  62. await present()
  63. return True
  64. def warning_notifications_enabled(platform, user_config=None) -> bool:
  65. """Use the turn snapshot when supplied, otherwise the active profile's effective config.
  66. No surface exemption: direct command/API outcomes are not notifications.
  67. Unknown values never opt in; null inherits via the canonical display resolver.
  68. """
  69. if user_config is None:
  70. user_config = effective_user_config()
  71. elif not isinstance(user_config, dict):
  72. user_config = {}
  73. platform_key = getattr(platform, "value", platform)
  74. return not resolve_display_setting(user_config, platform_key, "suppress_warning_notifications", False)