An autonomous agent is only as reliable as the pipe it speaks through. This work
was about the unglamorous layer beneath the model: getting messages out through
providers with different failure semantics, without sending twice,
getting rate-limited, or permanently burning a phone number over a temporary cap.
The agent reached people over WhatsApp, voice and SMS. Each provider reports
delivery differently, and each failure means something different: an invalid
recipient is permanent, an opt-out is a compliance event, a marketing-window cap
is temporary, and a locked sender is an emergency. Early code flattened all of
these into "failed" and scattered the handling across the codebase.
Duplicate messages under concurrency. Provider status webhooks
(queued → sent → failed) arrive in parallel. Concurrent callbacks could pass a
check-then-act and create the same outbound twice — the person received the
same template back to back.
Burned numbers on temporary caps. A marketing-window rejection was being
treated like a permanent invalid number, disabling a perfectly good channel
forever.
Rate-limit bursts. Dispatch went out in bursts that exceeded the
provider's ceiling and throttled the sender.
These were not independent defects. A retry caused by throttling could meet a
concurrent webhook, duplicate an effect and then update the contact with the
wrong permanent state. The solution therefore had to treat delivery as one
stateful pipeline.
I replaced the scattered handling with one error-policy table: each provider
status code maps to a final outcome and an action. Invalid recipients are marked
permanently; opt-outs suppress the contact (a compliance requirement); a
marketing cap sets a temporary block with jitter so the same number is retried
later instead of disabled; a locked sender trips a circuit breaker that pauses
the channel and alerts a human.
Error policy
One table maps provider codes to a final outcome and an action — instead of the same logic copy-pasted across the codebase.
The model treats a phone number as stateful rather than boolean. A temporary
block can expire instead of being treated as a terminal channel failure.
Phone number states
A marketing cap is temporary and can return a number to valid; only real failures are terminal.
The duplicate-send race was the highest-severity bug. The fix was an
idempotency guard scoped to the contact and the day, plus orphan cleanup: if the
send throws after the record is created, the half-created attempt is closed out
rather than left dangling. A concurrent-callback test verifies idempotency under
parallel webhooks.
Dispatch uses a semaphore with jittered spacing to keep a sender within the
provider's rate policy. A short lease also makes ownership explicit when a worker
stops mid-batch, so recovery does not depend on a long-running lock.
The model was never the bottleneck for reliability here. Delivery semantics,
concurrency and provider quirks were. Treating the messaging layer as a real
distributed-systems problem — idempotency, explicit state, backpressure — makes
delivery behavior explainable and reviewable.