date: 2026-11-08

title: "Idempotent Email Archives and Reply Flows in Agent Pipelines"

author: Brian Taylor


Idempotent Email Archives and Reply Flows in Agent Pipelines

Most pipeline dupes are not logic bugs. Email is delivered at least once at every layer, and cron recovery replays look like new work.

Dedupe on the right identity

  • event_id stops duplicate deliveries at the handler
  • inboxid+messageid (hashed) guards the reply and survives catch-up replay; Message-ID text alone is insufficient
  • One email to multiple inboxes are separate events, so never key replies on bare message IDs

Record before acting

  • If the handler waits for the model before replying, webhook retries start; persist event rows first and return 2xx, then process asynchronously
  • Unique constraints dedupe same-timestamp deliveries
  • Return 400 signature errors, 500 when nothing was recorded (so retry can recover)

Send once

  • Idempotency key derived from inboxid+messageid; store reply text before send, so re-runs send same text rather than changing keys and 409-ing
  • Provider key expires; after ~24 hours check thread history for a prior sent reply with matching In-Reply-To before resending
  • Separate transactionally: queue row, reply row, send

Operational checklist

  1. Use at-least-once semantics, do not assume exactly-once
  2. Validate webhook signatures over raw bytes; record event_id quickly
  3. Decide dedupe scope per business action: delivery vs side-effect
  4. Treat archive indexing similarly: key by message/id mailbox, not timestamp/content; persist before indexing
  5. Handle retries as changes in state (sentmessageid checkpoint), not as replays of intent
  6. Reconcile old missing replies by querying thread state rather than blindly resending

Why this matters here

Brian’s email archiving belongs to the same idempotency class as the Drive sync: a cron/agent retries, network and timeout happen, and absent identity tracking the same job can run twice. The storage-level fix is to move idempotency into the stable event identity and persist the outcome before the side effect.

Sources