Repository navigation
feat(daemon): webhook notifications for health alerts (#1150) - #1159
Conversation
Problem: polylogue/daemon/notifications.py shipped only LogNotificationBackend, and _resolve_backend() rejected any name other than "log" with ValueError. The periodic health loop's alerts therefore never left the daemon log, leaving the tiered health work shipped via #830 with no operator-visible egress (open Scope item on #999). What changed: - Add WebhookNotificationBackend that POSTs a typed JSON envelope {alerts, emitted_at, daemon_version} to a configured URL with a 5s per-attempt timeout and a single retry on transient failure (network error or HTTP 5xx). 4xx responses are not retried. - Extend _resolve_backend("webhook") to construct the backend from notification_webhook_url in the resolved config dict; missing/invalid URL raises a typed WebhookConfigError (no silent fallback to log). - Extend polylogue/config.py with notification_webhook_url across the defaults, [notifications].webhook_url TOML key, env override POLYLOGUE_NOTIFICATION_WEBHOOK_URL, format_config_toml roundtrip, and PolylogueConfig.notification_webhook_url accessor. - Add tests/unit/daemon/test_notification_webhook.py covering envelope shape, URL+timeout, single-retry behaviour for 5xx and network errors, persistent-failure raise, no retry on 4xx, missing-URL config error at construction and resolve time, send_notifications end-to-end dispatch. - Update docs/daemon.md notification backend section with the webhook config example and envelope shape. Verification: - nix develop -c pytest tests/unit/daemon/test_notification_webhook.py tests/unit/daemon/test_notifications.py -q -> 16 passed - nix develop -c devtools verify --quick -> exit_code 0 - Two pre-existing failures in tests/unit/devtools/test_topology_projection_witness.py reproduce on origin/master and are unrelated to this change. Ref #1150
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (4)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c6ae6da40f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| with httpx.Client(timeout=self._timeout) as client: | ||
| return client.post(self._url, json=envelope) |
There was a problem hiding this comment.
Offload webhook POST from the asyncio event loop
This synchronous httpx.Client.post(...) call runs inside send_notifications(), which is invoked by _periodic_health_check() (an asyncio task in polylogue/daemon/cli.py). When the webhook endpoint is slow or unreachable, this blocks the entire event loop for the request timeout (and again on retry), delaying unrelated daemon tasks like convergence, heartbeat, and watcher handling. Because this backend is intended for production alerting, the HTTP call (and retry backoff) should be moved to non-blocking async I/O or executed via asyncio.to_thread.
Useful? React with 👍 / 👎.
Summary
Add a
WebhookNotificationBackendalongside the existingLogNotificationBackendso daemon health alerts have a real, non-logegress route. Selected via
notification_backend = "webhook"plusnotification_webhook_url = "https://..."inpolylogue.toml.Problem
polylogue/daemon/notifications.pyshipped onlyLogNotificationBackend, and_resolve_backend()rejected any nameother than
"log"withValueError. The periodic health loop inpolylogue/daemon/cli.pytherefore had no way to deliver alerts offthe daemon log, leaving the tiered health work shipped via #830 with
no operator-visible egress. This is the open Scope item on #999:
"At least one non-log notification backend if product scope still
requires notifications."
Webhook is the most generic local-first choice: no provider
credentials, no desktop dependency, slots into self-hosted notifiers
(ntfy, gotify, Discord/Slack webhooks).
Solution
polylogue/daemon/notifications.pyWebhookNotificationBackendPOSTs a typed JSON envelope{alerts, emitted_at, daemon_version}to a configured URL with a5 s per-attempt timeout. On transient failure (network error or
HTTP 5xx) it retries exactly once after a short backoff. HTTP 4xx
is treated as a permanent client error and is not retried.
Persistent failure raises
httpx.HTTPError; the existingexcept Exceptionboundary in the periodic health loop catchesit. No buffering, no queue.
_HTTPPosterProtocol sotests can supply a lightweight stub without depending on the full
httpx.Clientsurface; production useshttpx.Clientdirectly._resolve_backend("webhook", config=...)readsnotification_webhook_urlfrom the resolved config dict andraises a typed
WebhookConfigErrorif it is missing/empty._resolve_backend("log")and the unknown-nameValueErrorpathare preserved.
polylogue/config.pynotification_webhook_urlflat key in defaults, exposed asPolylogueConfig.notification_webhook_url, mapped from the[notifications].webhook_urlTOML key, overridable viaPOLYLOGUE_NOTIFICATION_WEBHOOK_URL, and emitted byformat_config_toml.tests/unit/daemon/test_notification_webhook.py(new) coversenvelope shape (URL, method, timeout, payload), 5xx retry,
network-error retry, persistent failure raising, 4xx no-retry,
missing-URL config error at construction and
_resolve_backendtime,
send_notificationsend-to-end dispatch, and the preservedunknown-name
ValueError.docs/daemon.mdextended with the webhook config example, env-varalternative, envelope shape, and retry/timeout semantics.
Rejected alternative: a generic plugin registry. Two backends do not
yet justify the machinery;
_resolve_backendstill dispatches on thename string per the issue.
Verification
nix develop -c pytest tests/unit/daemon/test_notification_webhook.py tests/unit/daemon/test_notifications.py -q->
16 passed in 12.57snix develop -c devtools verify --quick->exit_code: 0(
total_duration_s: 25.79)nix develop -c pytest -q --ignore=tests/integration->
2 failed, 7355 passed, 2 xfailed in 100.12s. The two failures(
tests/unit/devtools/test_topology_projection_witness.py::test_every_cell_has_target_pathand
::test_every_owner_appears_in_files) reproduce onorigin/masterwith this branch's files reverted and are unrelatedto webhook work.
[notifications] backend = "webhook"/webhook_url = "https://x/y"inpolylogue.tomlloads, env overridePOLYLOGUE_NOTIFICATION_WEBHOOK_URLwins, and_resolve_backend("webhook", config=cfg.raw)returns aWebhookNotificationBackend.Closes #1150