Skip to content

feat(modules): add the AhaSend email provider - #345

Open
jeroenrinzema wants to merge 8 commits into
mainfrom
jeroenrinzema/ahasend-provider
Open

jeroenrinzema wants to merge 8 commits into
mainfrom
jeroenrinzema/ahasend-provider

Conversation

@jeroenrinzema

@jeroenrinzema jeroenrinzema commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Why: Lunogram has no AhaSend integration. AhaSend is a transactional email service that keeps message data in the EU.

How: A new modules/ahasend WASM provider, built the same way as Mailgun.

  • Send: POST /v2/accounts/{account_id}/messages/conversation with a Bearer API key. The conversation endpoint is used instead of Create Message because it supports cc and bcc and runs no substitutions over content Lunogram has already rendered. The inbox message ID is sent as Idempotency-Key, so a redelivered send replays AhaSend's stored result instead of sending again. 409, 429 and 5xx responses are retried; other 4xx responses fail permanently.
  • Webhooks: message.* events map to provider events: reception → sent, transient_error → deferred, bounced and failed → bounced, suppressed → dropped, plus delivered, opened, clicked, complained and unsubscribed. When webhookSecret is set, the Standard Webhooks HMAC is checked with the secret's literal bytes, as AhaSend requires. The user registers the webhook in the AhaSend dashboard, so the API key only needs messages:send:{domain}.
  • Exports use //go:wasmexport. Under //go:export, TinyGo runs exports without a current goroutine, and crypto/sha256 nil-panics on its FIPS indicator. Because of that, main.go is //go:build tinygo, and the rest of the module tests under plain go test.

Scope: modules/ahasend, a new integration docs page, and the provider lists. It also includes the platform changes AhaSend webhooks need (below). Non-goal: registering the webhook through AhaSend's API, which would need the webhooks:write scope.

Verification

  • go test ./... in modules/ahasend covers request mapping, cc copies in the response, config validation, status classification, every event mapping, and signature acceptance and rejection for wrong secret, tampered body, stale timestamp and missing headers.
  • Built with TinyGo 0.40.1 and loaded through the host registry: validation, webhook parsing and verification work. A send to api.ahasend.com with an invalid key returned status=401 {"message":"invalid API key"} and was classified as permanent. No send was made with a real account.

Provider webhook pipeline

AhaSend webhooks can't echo the inbox message ID back: they have no tags or custom headers, and AhaSend regenerates Message-ID. On top of that, no JetStream stream covered providers.webhooks.*, so on main every correlated provider webhook event, Resend's and Twilio's included, failed to publish (nats: no response from stream) and the endpoint answered 500.

sequenceDiagram
    participant D as inbox dispatch
    participant S as providers stream
    participant M as providers-messages
    participant W as webhook endpoint
    participant H as providers-webhooks
    participant E as user/org events
    D->>S: ProviderMessageSent (provider msg ID to inbox msg)
    S->>M: record in provider_messages
    W->>S: ProviderWebhookEvent (inbox ID or provider msg ID)
    S->>H: resolve via provider_messages
    H-->>S: not recorded yet: NakWithDelay (DefaultBackOff, 6 deliveries)
    H->>E: provider.delivered, provider.bounced, ... with provider_data
Loading
  • provider_messages (subjects DB) maps (project, provider, provider message ID) to a user or organization inbox message. Rows cascade with the message.
  • Dispatch queues the mapping only for modules with a webhook and never fails over it, so a send is not repeated for correlation.
  • WithDelayedRetry / RetryAfter: the two provider consumers back off between retries instead of getting the router's immediate NAK.
  • Deduplication: each webhook event gets a content-hash ID, published as Nats-Msg-Id (1h duplicates window on the providers stream), so a webhook the provider retries is recorded once. The user/org event reuses that ID as its Msg-Id, which covers JetStream redeliveries.
  • Time: UserEvent/OrganizationEvent take an optional OccurredAt, stored as created_at. Provider events use the provider's timestamp; a missing, unparsable or future timestamp falls back to now.
  • Events that still can't be correlated after ~13 minutes (e.g. mail sent by another app on the same AhaSend account) are dropped with a log. The endpoint keeps answering 200, because AhaSend disables a webhook after 100 consecutive failures.

Evidence

  • TestProviderMessages (store): resolves user/org mappings, a replayed ID keeps its first mapping, lookups are scoped by project and provider, and mappings cascade on user delete.
  • TestProviderWebhooks (consumer): resolves a recorded ID, uses an echoed inbox ID, delays unresolved events by DefaultBackOff[n-1] and drops them on the last delivery, rejects cross-project IDs, and keeps provider_data from overwriting message_id.
  • TestProviderWebhookEventID (retries keep their ID, distinct events don't share one) and TestInsertUserEventOccurredAt; TestProviderWebhooks also checks that the provider timestamp is kept and that unusable timestamps fall back to now.
  • TestWithDelayedRetry, plus TestBootstrapCreatesAllConsumers for the new consumers.

Merge Danger

Door: two-way, apart from the new provider_messages table (migration 1784800000, with a down migration).

Blast Radius: provider webhooks. Correlated Resend/Twilio events now get published and become provider.* user/org events (they used to 500), so rules and journeys can start seeing them.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

AhaSend events lack InboxMessageID, causing even correctly signed callbacks to be acknowledged with HTTP 200 but silently skipped instead of published.

1 open issue
What changed in this PR

Adds AhaSend as a WASM email provider, extending Lunogram’s integrations with transactional email sending and webhook handling. Signature verification depends on the raw-body preservation change in #344.

Changes:

  • Implements email request mapping, idempotent sends, retry classification, and signed webhook parsing.
  • Adds provider tests, build configuration, setup documentation, and discovery links.
  • Removes the root Makefile’s forced GOGC=off setting.
File Description
modules/​README.md Lists AhaSend among available providers.
modules/​ahasend/​README.md Documents configuration and development.
modules/​ahasend/​Makefile Adds the WASM build target.
modules/​ahasend/​main.go Exports provider configuration, sending, validation, and webhook handling.
modules/​ahasend/​main_test.go Tests request mapping, responses, validation, events, and signatures.
modules/​ahasend/​go.sum Records dependency checksums.
modules/​ahasend/​go.mod Defines module dependencies and the local SDK reference.
modules/​ahasend/​ahasend.go Implements request mapping and response classification.
modules/​ahasend/​ahasend_webhook.go Maps webhook events and verifies signatures.
Makefile Stops disabling garbage collection for Go commands.
docs/​content/​docs/​integrations/​smtp.mdx Adds AhaSend as an HTTP-provider alternative.
docs/​content/​docs/​integrations/​meta.json Adds AhaSend to documentation navigation.
docs/​content/​docs/​integrations/​index.mdx Adds the AhaSend integration card.
docs/​content/​docs/​integrations/​ahasend.mdx Documents setup, webhooks, and troubleshooting.

Review effort: Balanced

Comment thread modules/ahasend/ahasend_webhook.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Webhook events can be permanently lost because callbacks arriving before correlation records exist are acknowledged and discarded, and transient failures to persist those records have no durable retry.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment thread internal/pubsub/consumer/inbox.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Webhook retry handling can drop events, provider payloads are discarded, and correlation persistence failures can trigger duplicate emails.

3 open issues
2 resolved since last review

Review effort: Balanced

Comment thread internal/pubsub/consumer/inbox.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Storage failures bypass configured retry backoff, risking exhaustion of all 20 mapping deliveries during a brief database outage and leaving accepted sends without automatically recoverable correlation rows.

1 open issue
3 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed to evaluate redelivery risks that can duplicate event counts and journey triggers, as well as the loss of original occurrence timestamps.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

- Sends through AhaSend's Create Conversation Message endpoint, which carries cc and bcc and runs no substitutions over rendered content.
- Passes the inbox message ID as the Idempotency-Key so a redelivered send replays the stored result.
- Maps AhaSend message webhooks to provider events and verifies their Standard Webhooks signatures.
- Exports use //go:wasmexport: under //go:export, crypto/sha256 panics in TinyGo for lack of a current goroutine.
- Documents setup in docs/content/docs/integrations/ahasend.mdx.
- AhaSend webhooks carry neither tags nor custom headers and AhaSend regenerates Message-ID, so they cannot echo the inbox message ID back; every event was acknowledged and then dropped for lacking one.
- Sends through modules that accept webhooks now record the provider's message ID against the user or organization inbox message in provider_messages. Mappings cascade with the message.
- The webhook handler resolves an event's MessageID through that mapping when the module returned no InboxMessageID.
- Recording failures are logged, not returned: the message has already been sent and redelivering the dispatch would send it again.
- No stream covered providers.webhooks.*, so every correlated webhook event failed to publish ('no response from stream') and the endpoint answered 500.
- A providers stream and a providers-webhooks consumer now take those events. The consumer resolves MessageID through provider_messages when the module returned no inbox message ID, loads the inbox message, and publishes the provider event (provider.delivered, provider.bounced, ...) as an event of its user or organization.
- A webhook can arrive before the send recorded its message ID, so the webhook handler publishes unresolved events too and the consumer redelivers them along DefaultBackOff (about 13 minutes) before dropping them.
- A failure to record the provider message ID now fails the dispatch so it is redelivered, as a failure to mark the message sent does. AhaSend replays the original send for the repeated idempotency key.
… webhooks

- Dispatch no longer records the provider message ID inline. It publishes a ProviderMessageSent onto the providers stream, and the providers-messages consumer records it with JetStream retries. A failed record no longer redelivers the dispatch, which for a message without external_id created a new inbox row and with it a new idempotency key.
- Handlers can return RetryAfter to have the router NAK with a delay. The providers-webhooks consumer uses it to wait for an unresolved provider message ID along DefaultBackOff, independent of how the server treats a plain NAK.
- Provider webhook data is forwarded under provider_data, as the WebhookEvent contract promises, without overwriting the inbox message fields.
- WithDelayedRetry redelivers a transient failure after the DefaultBackOff delay for its delivery count, instead of the router's immediate NAK. Both provider consumers use it, so a short database outage cannot use up their deliveries. Permanent and rate-limited errors keep their own handling.
@jeroenrinzema
jeroenrinzema force-pushed the jeroenrinzema/ahasend-provider branch from 3407839 to 289c1bb Compare November 30, 2026 12:30
- The webhook endpoint gives each event a content-derived ID and publishes it with that Msg-Id; the providers stream keeps a 1h duplicates window, covering AhaSend's ~16 minute retry schedule. A provider retrying a webhook is recorded once.
- The providers-webhooks consumer publishes the user or organization event with a Msg-Id derived from the same ID, so a redelivery after the publish is not recorded twice.
- UserEvent and OrganizationEvent take an optional OccurredAt, stored as created_at. Provider events use the provider's timestamp; a missing, unparsable or future one records the event as now.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Twilio's synthetic timestamp changes across retries, bypassing both deduplication stages and allowing duplicate provider events that may retrigger journeys.

2 open issues

Review effort: Balanced

Comment thread internal/pubsub/schemas/events.go Resolved
…he AhaSend webhook URL

- The Twilio module stamped each event with its receipt time, so a retried callback hashed to a new ID and was recorded again. It now leaves Timestamp empty and the host records the event at receipt; the WebhookEvent contract says so.
- The console shows no provider webhook URL, so the AhaSend docs give its format and where to find the project and provider IDs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed to judge whether omitted provider event flows and retry/drop behavior in the architecture documentation materially hinder webhook troubleshooting.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The webhook-driven rules/journeys and durable correlation pipeline still require real-account sending and end-to-end webhook validation beyond the provided helper and bootstrap tests.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

No deployments
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.

2 participants