Skip to content
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The HTTP API clients and UIs use, and how it and the engine's own calls to HFS a
| `GET /api/records?source=&status=&file_id=` | Records; `status` is `received`, `pending`, `sent` or `rejected` |
| `GET /api/records/{id}` | One record: file, line, original and corrected row, resources sent, HFS response |
| `POST /api/records/{id}/reprocess` | Runs a record again; body `{"raw": {...}}` replaces the row and keeps the original |
| `GET /api/alerts?source=&open=true&since=&limit=100&cursor=` | Alerts, newest first, each with its rule, recipients, channels, when and why it cleared, and `webhook`: the delivery of its latest webhook event (`event`, `event_id`, `state`, `attempts`, `last_error`, `last_status`, `delivered_at`; null for alerts from before events were recorded); `open=true` lists only those not cleared. `since` and `cursor` read a period page by page (see [Reading alerts by period](#reading-alerts-by-period)). Kept for `retention.history` (see [Alerts and retention](#alerts-and-retention)). Also `webhook: {configured, signed}`, this instance's webhook settings (see [the alert webhook](sources.md#the-alert-webhook)) |
| `GET /api/alerts?source=&open=true&since=&limit=100&cursor=` | Alerts, newest first, each with its rule, recipients, channels, when and why it cleared, and `webhook`: the delivery of its latest webhook event (`event`, `event_id`, `state`, `attempts`, `last_error`, `last_status`, `delivered_at`), and in `webhook.events` every event of the alert with the same fields, oldest first (a `fired` still parked or failed stays visible after its `cleared` is delivered); null for alerts from before events were recorded; `open=true` lists only those not cleared. `since` and `cursor` read a period page by page (see [Reading alerts by period](#reading-alerts-by-period)). Kept for `retention.history` (see [Alerts and retention](#alerts-and-retention)). Also `webhook: {configured, signed}`, this instance's webhook settings (see [the alert webhook](sources.md#the-alert-webhook)) |
| `GET /api/events?source=&kind=&since=&cursor=&limit=` | The event log, oldest first: each record's attempts and outcomes, each load's steps, each submission's steps (see [Events](#events)) |
| `GET /api/alert-rules?source=` | Every rule, each with its `created_at`; `POST` creates one (`400` names what is wrong with it). `created_at` is set by the engine, never by the body. A rule id is never given again, like alert and event ids. A `missing_by` rule may name a `tz` (see [sources](sources.md#alerts-and-rules)) |
| `GET` / `PUT` / `DELETE /api/alert-rules/{id}` | One rule: read, replace, remove. A `PUT` that turns the rule off (`enabled: false`) or changes its `condition`, `threshold`, `tz` or `source_id`, and a `DELETE`, clear the rule's open alerts in the same transaction, each with its webhook `cleared` event; a change of `notify` or `channels` alone does not |
Expand Down
12 changes: 7 additions & 5 deletions docs/decisions.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,24 +480,26 @@ Several engine instances may share one PostgreSQL. A source is run by the instan
- `/api/alerts` keeps its typed parameters, so a value of the wrong type or given twice is refused without `field`, and an unknown parameter is ignored, unlike `/api/events`, which names the field of every refusal. `/api/records` and the per-source loads list keep the extractor's plain-text `400`; `/api/loads` (42) answers `invalid_query` as `/api/alerts` does.
## 40. The alert webhook is a signed, retried and recorded contract

**Chosen:** Every change of an alert, `fired` and `cleared`, is an event with a random `event_id`, written with its exact body in the same transaction as the alert, in the store's `alert_webhooks`. The body is `{event_id, event, alert: {id, rule_id, kind, message, notify, created_at, cleared_at, cleared_reason}, source: {id, name}, text}`. With `alerts.webhook_secret` (or `HIE_ALERT_WEBHOOK_SECRET`) set, `X-HIE-Signature: sha256=<hex>` carries the HMAC-SHA256 of the raw body. A sender task per instance posts waiting events oldest first: a 2xx delivers; anything else, a 3xx included (redirects are not followed), is retried up to five times after `alerts.webhook_backoff_ms` (10 s), doubling, then the event is `failed`. Each attempt is claimed with one conditional update, for a lease of 60 s, and its outcome is stored only while the claim is still that attempt's; a last attempt whose outcome was never stored ends `failed`. A `cleared` event waits for its `fired` event for the same URL to be delivered or given up on. Each event belongs to the URL configured when it was written and waits for that URL. `GET /api/alerts` shows, per alert, the delivery of its latest event (`event`, `state`, `attempts`, `last_error`, `last_status`, `delivered_at`), and `webhook: {configured, signed}` for the instance. Events not requested by the rule's channels, or written with no URL configured, are recorded as `not_requested` and `not_configured` and never sent.
**Chosen:** Every change of an alert, `fired` and `cleared`, is an event with a random `event_id`, written with its exact body in the same transaction as the alert, in the store's `alert_webhooks`. The body is `{event_id, event, alert: {id, rule_id, kind, message, notify, created_at, cleared_at, cleared_reason}, source: {id, name}, text}`. With `alerts.webhook_secret` (or `HIE_ALERT_WEBHOOK_SECRET`) set, `X-HIE-Signature: sha256=<hex>` carries the HMAC-SHA256 of the raw body (amended below: of a timestamp and the body). A sender task per instance posts waiting events oldest first: a 2xx delivers; anything else, a 3xx included (redirects are not followed), is retried up to five times after `alerts.webhook_backoff_ms` (10 s), doubling, then the event is `failed`. Each attempt is claimed with one conditional update, for a lease of 60 s, and its outcome is stored only while the claim is still that attempt's; a last attempt whose outcome was never stored ends `failed`. A `cleared` event waits for its `fired` event for the same URL to be delivered or given up on. Each event belongs to the URL configured when it was written and waits for that URL. `GET /api/alerts` shows, per alert, the delivery of its latest event (`event`, `state`, `attempts`, `last_error`, `last_status`, `delivered_at`; amended below: and of every event), and `webhook: {configured, signed}` for the instance. Events not requested by the rule's channels, or written with no URL configured, are recorded as `not_requested` and `not_configured` and never sent.

**Why:**
- External monitoring and paging tools are meant to receive the engine's alerts through its webhook, so it needs a stable shape, a way to prove the sender, and delivery that survives a receiver's outage and the engine's restarts. A host application shows whether an alert reached them, and needs to tell "no webhook configured" from "not delivered yet".
- Writing the event with the alert, in one transaction, means no alert change is lost between the two; storing the exact body means every retry is the same bytes, so a receiver de-duplicates by `event_id` and a signature never depends on when it was computed.
- A body-only HMAC (the shape GitHub's webhooks use) is the simplest scheme receivers verify. A replayed request carries an `event_id` the receiver has seen; an old event names an old alert.
- Writing the event with the alert, in one transaction, means no alert change is lost between the two; storing the exact body means every retry is the same bytes, so a receiver de-duplicates by `event_id` and a signature never depends on when it was computed (until the amendment below).
- A body-only HMAC (the shape GitHub's webhooks use) is the simplest scheme receivers verify. A replayed request carries an `event_id` the receiver has seen; an old event names an old alert. (The amendment below signs a timestamp with the body, so that a receiver need not remember every `event_id` for ever.)
- Claiming by `event_id` with a conditional update, and fencing the outcome by the attempt number, lets several instances on one store send without sending one attempt twice, on SQLite and PostgreSQL alike, and keeps a stalled instance from overwriting a newer outcome.
- Binding an event to its URL keeps alert text and recipients from going to a receiver configured later for something else; waiting for that URL keeps a brief misconfiguration from losing them.
- Redirects are refused because a redirected POST becomes a GET without the body, and would carry the signature to another host.

**Cost / not yet:**
- The body changed: the old `feed`, `kind`, `alert_id`, `notify` and `cleared` keys are gone (`text` stays), and a clear is now posted only when the channels recorded on the alert when it fired include the webhook (a later edit of the rule does not change that; a rule turned off or deleted left its open alert open, with no clear, until decision 43 made it clear the alert). `alerts:` refuses unknown keys, so a misspelt `webhook_secret` cannot send unsigned; a file with stray keys there no longer starts. While instances of an earlier version share the store, those still post the old body, unsigned. In the library, an `Alerter` now only records events: they are sent by the task `Alerter::start` returns, which `build` keeps with the source manager.
- A receiver can still get an event twice, and a late `fired` after its `cleared`; it must de-duplicate. `event_id` covers retries of one event, not a second alert raised for one condition. Receivers match the two events of an alert by its `id`, which is never given again (decision 38).
- No timestamp is signed: a receiver that forgets the `event_id`s it has seen accepts a replay of an old event. Use HTTPS.
- No timestamp was signed: a receiver that forgot the `event_id`s it had seen accepted a replay of an old event (amended below, before any release carried the signature). Use HTTPS.
- Sends are sequential, one per instance at a time: with a receiver that times out, waiting events queue behind each other. Claims and due times use each instance's clock, as source leases do: instances on one store need synchronised clocks. All instances on a store need the same `alerts:` settings.
- The API shows the latest event only: a `fired` that failed is shown until its `cleared` is written. A monitor tracking delivery re-reads the alerts it still follows; delivery changes do not move a cursor. Events are kept with their alert and removed with it when `retention.history` purges it (decision 38), waiting ones included: by then only an event parked for an earlier URL, or never sent, can still be waiting.
- The API showed the latest event only (until the amendment below added every event): a `fired` that failed was shown until its `cleared` was written. A monitor tracking delivery re-reads the alerts it still follows; delivery changes do not move a cursor. Events are kept with their alert and removed with it when `retention.history` purges it (decision 38), waiting ones included: by then only an event parked for an earlier URL, or never sent, can still be waiting.
- No mail sink, per-rule URLs, redelivery endpoint or delivery metrics.

*Amended 2026-11-24:* every attempt carries `X-HIE-Timestamp`, the Unix seconds when it was sent, and `X-HIE-Signature` is the HMAC-SHA256 of `{timestamp}.{raw body}` instead of the body alone, as Stripe-style webhooks do, in HIE's own headers. Receivers refuse a timestamp outside a tolerance they enforce, 5 minutes by default, and keep the `event_id`s they have acted on for at least twice that long. Why: with the body alone, anyone who captured one delivery could replay it later to a receiver that had pruned its `event_id`s, or never kept them (a `fired` that pages someone again); adding a timestamp after receivers verify the body-only signature would break every one of them. The stored body is still the same bytes on every attempt; the timestamp and the signature are computed per attempt when it leaves, so a retry is never stale, and the receiver's window need not cover the backoff. No release signed the body alone (0.2.0 sent unsigned events), so there is no compatibility mode. Cost: a receiver whose clock is off by more than its tolerance refuses every event, and gets them again on each retry until the five retries are spent; instances need synchronised clocks already. Each attempt's signature depends on when it is sent. Also: the secret is trimmed of leading and trailing whitespace when read, from `hie.yaml` or the environment, so a secret file ending in a newline signs with the secret it holds; a secret meant to begin or end with whitespace now signs without it. And `GET /api/alerts` adds `webhook.events`, every event of each alert, read with the page in one statement: showing the latest event only could hide a `fired` parked for an earlier URL behind a `cleared` delivered to the current one.

## 41. Check now has an id and an outcome

**Chosen:** `POST /api/sources/{id}/check-now` records a check in the store and answers its `check_id`; `GET /api/checks/{id}` answers `pending`, `found`, `nothing_new` or `failed`, with when it was asked for and answered. A check is recorded with a baseline, the source's newest load at that moment, and is answered by the first scan of the source that starts after it was recorded, on the instance holding the source: each pass reads the source's pending checks just before its scan and answers exactly those when the scan ends, before delivering. `found` lists the source's loads after the baseline up to the end of that scan, read with their current status when asked; `nothing_new` when there are none; `failed` when the source is paused, its credential is not loaded, the scan failed, or the source was deleted (unless another instance held it then), with one of a fixed set of reasons and the loads taken before the failure. The first answer stays. Checks are purged with events and alerts after `retention.history` (decision 38), counted from when they were asked for, answered or not. The route keeps its name; nothing new is needed to ask for a check.
Expand Down
Loading