# plugin_ntfy — notes ## Protocol notes (measured against ntfy, 2026-11) * Publish: `POST /` with the message as the raw body. Headers: `Title`, `Priority` (1–5), `Tags` (comma list, emoji names), `Authorization: Bearer …` (protected topics), `X-Markdown`. The server answers `200` with `{"id": "...", "time": ..., "expires": ..., ...}` — the `id` is what the anti-self-loop ledger remembers. * Subscribe: `GET //json` returns an NDJSON stream (`Transfer-Encoding: chunked`). First record is `{"event":"open",...}`. Keepalives are comment lines (`### keepalive`) — not JSON — and must not parse as events. Messages carry `id/time/event/topic/message` and the optional `title/user/tags/priority`. * **The server echoes your own publishes back on the subscription of the same topic.** An unguarded bridge republishes its own reply forever. plugin_ntfy defends three ways at once: the sent-id ledger (`sent.json`), a fixed `scorpiox` tag on every publish, and an `X-Source-Id` header. Any single one stops the loop; together they also stop a hand-typed imitation of our replies. * `user` is **not authentication.** It is only populated when the server authenticated the publisher; on a normal topic it is the publisher's own claim, and tags are free text. That is why pairing is default-deny. ## Why the daemon polls the conversation every tick The first version published the agent's reply only after the NDJSON stream broke (read timeout / server close). With a healthy long-lived subscription that never happens — ntfy keepalives arrive every ~20 s, so replies sat unpublished until a reconnect. The reply check now runs on every read tick: `poll_assistant()` advances a cursor into `GET /conversation?id=&after=`, and a reply is published **only when the cursor actually advanced** (the latest-assistant-message rule alone would republish the same answer on every keepalive — caught by the "not republished on later keepalives" assertion in tests/run.sh). ## The HTTP client (src/sx_http.c) Small, mbedTLS-only, no dependencies. Three bugs were caught by the offline suite and are worth remembering because each one made a *whole feature* silently dead while every other path looked healthy: 1. `sx_send_all()` waited on the **read** set before `send()` on a non-blocking socket. Readiness for writing is writability: every plain HTTP request burned its full deadline and died with "timed out sending request". (The connect path already waited on writability — the comment there even says why.) 2. `sx_read_headers()` copied `rbuf` wholesale into its header accumulator. Headers + body usually arrive in one segment, so the body bytes were consumed and lost: `ntfy_publish` got `200` with an empty body, the publish `id` was `""`, the ledger stayed empty, and the `/conversation` poll always saw nothing. Now the cursor is rewound to the header terminator and the body bytes are handed back to `sx_body_byte()`. 3. `sx_conn_open` for a *plain* connection must not leave the half-used `rbuf` cursor in place from a previous request — every request on this transport is its own connection (`Connection: close`), but the header rewind above made cursor hygiene observable. ## Pairing store shape `pending.json` — key (sha256 hex of `v1|topic|user|tags`), label, code (8 chars, from `getrandom`-seeded PRNG), sample (first message, capped), first/last seen, last_challenge_at (challenge throttled to 10 min), count. `approved.json` — key, label, added_at, via (0 = UI, 1 = PAIR code). `sent.json` — the ids we published (bounded ledger). All mode `0600`, all written atomically (tmp + rename). The `key` length is `SX_NTFY_IDENTITY_LEN` = 64; it was 80 once, which made `valid_key()` reject every real key and `action=approve` impossible for a key copied straight out of `?action=pending` (found by the suite). ## Environment variables See README. The only one with security weight is `NTFY_TLS_INSECURE=1`: in-memory only, never persisted, loudly reported by `?action=status` and the UI pill (`tls: verification OFF`).