Skip to content

feat(daemon): webhook notifications for health alerts (#1150) - #1159

Merged
Sinity merged 1 commit into
masterfrom
feature/feat/daemon-webhook-notifications
May 17, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/feat/daemon-webhook-notifications

Conversation

@Sinity

@Sinity Sinity commented May 17, 2026

Copy link
Copy Markdown
Owner

Summary

Add a WebhookNotificationBackend alongside the existing
LogNotificationBackend so daemon health alerts have a real, non-log
egress route. Selected via notification_backend = "webhook" plus
notification_webhook_url = "https://..." in polylogue.toml.

Problem

polylogue/daemon/notifications.py shipped only
LogNotificationBackend, and _resolve_backend() rejected any name
other than "log" with ValueError. The periodic health loop in
polylogue/daemon/cli.py therefore had no way to deliver alerts off
the 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.py
    • WebhookNotificationBackend POSTs a typed JSON envelope
      {alerts, emitted_at, daemon_version} to a configured URL with a
      5 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 existing
      except Exception boundary in the periodic health loop catches
      it. No buffering, no queue.
    • HTTP client is injected via a structural _HTTPPoster Protocol so
      tests can supply a lightweight stub without depending on the full
      httpx.Client surface; production uses httpx.Client directly.
    • _resolve_backend("webhook", config=...) reads
      notification_webhook_url from the resolved config dict and
      raises a typed WebhookConfigError if it is missing/empty.
      _resolve_backend("log") and the unknown-name ValueError path
      are preserved.
  • polylogue/config.py
    • New notification_webhook_url flat key in defaults, exposed as
      PolylogueConfig.notification_webhook_url, mapped from the
      [notifications].webhook_url TOML key, overridable via
      POLYLOGUE_NOTIFICATION_WEBHOOK_URL, and emitted by
      format_config_toml.
  • tests/unit/daemon/test_notification_webhook.py (new) covers
    envelope shape (URL, method, timeout, payload), 5xx retry,
    network-error retry, persistent failure raising, 4xx no-retry,
    missing-URL config error at construction and _resolve_backend
    time, send_notifications end-to-end dispatch, and the preserved
    unknown-name ValueError.
  • docs/daemon.md extended with the webhook config example, env-var
    alternative, envelope shape, and retry/timeout semantics.

Rejected alternative: a generic plugin registry. Two backends do not
yet justify the machinery; _resolve_backend still dispatches on the
name 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.57s
  • nix 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_path
    and ::test_every_owner_appears_in_files) reproduce on
    origin/master with this branch's files reverted and are unrelated
    to webhook work.
  • Manual roundtrip: [notifications] backend = "webhook" /
    webhook_url = "https://x/y" in polylogue.toml loads, env override
    POLYLOGUE_NOTIFICATION_WEBHOOK_URL wins, and
    _resolve_backend("webhook", config=cfg.raw) returns a
    WebhookNotificationBackend.

Closes #1150

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
@coderabbitai

coderabbitai Bot commented May 17, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@Sinity has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 45 minutes and 50 seconds before requesting another review.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 103ba7ae-7171-412b-8d91-1de5c7472e1e

📥 Commits

Reviewing files that changed from the base of the PR and between 0208595 and c6ae6da.

📒 Files selected for processing (4)
  • docs/daemon.md
  • polylogue/config.py
  • polylogue/daemon/notifications.py
  • tests/unit/daemon/test_notification_webhook.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/feat/daemon-webhook-notifications

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Sinity
Sinity merged commit 96c6b99 into master May 17, 2026
3 checks passed
@Sinity
Sinity deleted the feature/feat/daemon-webhook-notifications branch May 17, 2026 04:49

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +176 to +177
with httpx.Client(timeout=self._timeout) as client:
return client.post(self._url, json=envelope)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(daemon): webhook notification backend for health alerts

1 participant