Skip to main content

Notifications architecture

:::caution Routed; the NotifyStack is opt-in The 008 HTTP routes are registered in lib/routes.ts (stack cse-http-be-<stage>-api-notify; G03 via POST /v1/{collection} in ApiStack); the web team observed them answering on dev on 2026-10-28. The NotifyStack (cse-http-be-<stage>-notify: notify-recompute and digest-jobs queues, NotifyRecomputeFn, DigestFn, weekly reconcile schedule) is built but exists only with -c busEnabled=true. Large audience changes are queued to notify-recompute when busEnabled is on (Q36). Channel flags come from SSM (Q37), the org directory (ORGS#<kind>) is written on org create/rename and needs a one-off backfill for older orgs (Q38) and the email consumer is off by default. The notify.*.v1 events (notify.user.v1, notify.audience.v1, notify.org-settings.v1) are written to the outbox on deploy but reach TCP only when busEnabled is on. Nothing on this page has run against any stage. :::

The v2 split​

SideOwns
HTTP Server (this repo)Push tokens, notification rules, org notification settings, email delivery (immediate + digest), the HTTP in-app inbox for HTTP-owned events (GET /v1/me/notifications)
TCP ServerAlert detection, rule matching, grouping, push sending, delivery log, its own device-alert inbox

The HTTP side never calls a push provider. It publishes projections to TCP through the integration bus; TCP decides and sends.

Events​

DirectionEvent
Emittednotify.audience.v1, notify.user.v1 (pushEligible; since ADR 0031 endpoints for everyone), notify.user.deleted.v1, notify.org-settings.v1, notify.push.requested.v1 (digest fallback; work-order, report-ready and membership-notice inbox items, with link/tag/platforms/eligibilityExempt), notify.reconciliation.completed.v1
Consumednotify.endpoint.invalid.v1 (delete the token), notify.email.requested.v1 (send or hold for digest)

Push: End Users only, every platform (ADR 0025, generalising ADR 0022)​

Web push was committed 2026-11-06 (ADR 0022). The all-platform rule was written 2026-11-13 (ADR 0025). Neither is deployed. Push on any platform (ANDROID, IOS, WEB) goes to End Users only.

  • The rule is per person, because a push token belongs to a person, not to a membership. It is in packages/core/src/notify/push-eligibility.ts.
  • notify.user.v1 carries pushEligible. It is false while the person holds any active staff membership. The TCP Server then skips every push for them, on HTTP endpoints and on the tokens it holds itself (ws-gateway registrations). The one exception is a membership notice (ADR 0031, below). Since ADR 0031 the endpoints are sent whatever the flag says.
  • Membership changes republish. Every membership writer (suspend, restore, remove, role or scope change, bulk scope, invitation accept) republishes the person's notify.user.v1, token or not:
    • inline, in the same transaction, for ≤ 25 users;
    • otherwise right after the commit, from committed state;
    • also right after an invitation accept.
  • Some callers skip the rebuild with eligibility: false: rules, shares, technician teams and role-permission edits. None of them can change eligibility.
  • The push toggle (PATCH /v1/me/preferences notifications.channels.push) republishes notify.user.v1 in the same transaction as the save. pushEnabled: false blocks all push, CRITICAL included.
  • Reconciliation re-applies the rule. It also gives a first projection to never-projected members of PLATFORM, OPERATOR and PARTNER orgs (below).

HTTP inbox items for work orders and report ready also write notify.push.requested.v1 (with link, tag, platforms: ["WEB"]) in the inbox item's own transaction. See Web push.

Membership notices (ADR 0031, 2026-11-26, not deployed). JOIN_REQUEST_RECEIVED (an Installer asked to join → the Business's team ≥ MANAGE members), INVITATION_RECEIVED (a Business invited an Installer or End User) and ADDED_TO_BUSINESS (a plant transfer added an End User into a Business) push to ANDROID, IOS and WEB with eligibilityExempt: true. These are the only pushes staff can receive. Waiting on the TCP Server to honour the flag. See Membership notices.

Revocation bound​

When a membership or role changes, the audience is recomputed and a new notify.audience.v1 is published. Target: a removed user stops receiving push within ≤ 2 minutes. Writers include role-permission edits, technician PUT and invitation accept (response shapes unchanged). Changes affecting ≤ 25 users write the new audience items in the same transaction as the change (or, when the caller's transaction has no item budget left, recompute synchronously right after commit). Since owner decision Q36 (2026-10-27), changes affecting more than 25 users (revocations and widenings alike) send USERS recompute requests — at most 1000 users each — to the notify-recompute queue after commit; the NotifyRecomputeFn consumer recomputes from committed state in 25-user transactions. The writers that follow this rule: member role/scope/suspend/ remove, role-permission edits, technician and team changes, shares, and invitation accept / share activation.

  • No queue configured (busEnabled off, so no NOTIFY_RECOMPUTE_QUEUE_URL): the overflow is recomputed synchronously, chunked, before the response (the pre-Q36 behaviour). Nothing is dropped.
  • Enqueue failure: every chunk not yet queued falls back to the synchronous recompute and the API logs WARN notify_recompute_enqueue_failed (reason, user count, queued count, error name; no ids). The request is not failed, because the change has already committed.
  • User deletion: the hook planUserDeletionNotify(sub) publishes notify.user.deleted.v1 and a NONE audience tombstone per operator in the deletion's own transaction (Q36 overflow rules apply), but no user-deletion route or script calls it yet. See Known issues.

Reconciliation​

A weekly job (packages/core/src/notify/reconcile-service.ts) republishes the full notify.audience.v1, notify.user.v1 and notify.org-settings.v1 state, each stamped with a new generation, so TCP can prune keys not refreshed for two generations. Three passes, no Scan:

  1. Audience shards — every published audience (GSI2 NAUD#0…15), recomputed from source; republished or tombstoned.
  2. Orgs — every directory org's settings; for an operator org, its own members with no audience state get their first publish; for a platform, operator or partner org, members with no notify.user.v1 yet get their first one (ADR 0025), so every staff member ends up with pushEligible: false.
  3. User shards — every published user (GSI2 NUSER#0…15), rebuilt.

The position is saved in the cursor item RECONCILE#<stage>/CURSOR after every page, so any invocation resumes from the saved cursor; one message processes several pages. Events go through a ≤ 50 events/s limiter. The run ends with notify.reconciliation.completed.v1 (seq = generation). The weekly schedule cse-http-be-<stage>-notify-reconcile-start (cron(0 21 ? * SUN,WED *), Sunday and Wednesday UTC) sends {"kind":"RECONCILE_PAGE","reason":"WEEKLY"} to the notify-recompute queue; NotifyRecomputeFn runs pages until < 60 s of its 300 s remain, then re-enqueues. The cursor carries a step counter: every save is conditioned on the (generation, step) it read, so if two chains walk the same run (duplicate delivery, concurrent consumers) the loser gets ReconcileCursorConflict and its message is dropped. Alarm NotifyReconcileStale fires when a 7-day period has no completed run. Built, not deployed.

Digests​

DigestFn consumes the digest-jobs queue: one job per {orgId, periodKey} sends one digest per recipient (email, or push fallback). It creates one-time schedules in group cse-http-be-<stage>-digests through the scheduler role. Email sending stays off while SES is in sandbox.

Conformance vectors​

packages/core/src/notify/policy-reference.ts holds the reference decision function and shared test vectors; TCP runs the same vectors so both sides agree on who gets notified.

Cost​

ItemCost driverNote
Projection writesDynamoDB on-demand writes + SQS FIFO messagesSkipped when the content hash is unchanged
EmailSES per messageOff by default (SES sandbox)
Pushone bus message per pushed inbox item (work orders, report ready)TCP pays FCM (free); staff recipients have no web endpoint, so TCP drops theirs
Reconcileweekly; several pages per SQS message; every projection republished (generation stamp)≤ 50 events/s

The TCP side of this design is documented in the TCP Server docs.