Skip to content

Test-only delivery handoff foundation

This internal foundation exercises delivery handoff semantics with an in-memory SMS test adapter. It does not create live dispatches, connect to a provider, enqueue Sidekiq jobs, or process Visitor simulations. The database explicitly constrains every handoff to mode = test. An adapter other than the in-memory test adapter is rejected. Test acceptance is a fake observation, not a provider acceptance or delivery.

The trusted caller authorizes the tenant, enters TenantContext, and calls DeliveryTesting::DeliveryHandoffs.prepare_test! with a tenant-scoped request key, E.164-shaped destination and message. A tenant row lock and unique index make concurrent reuse safe; the same key and payload return the existing ID, while a different payload raises a conflict. The transaction must commit before its result is considered durable. Creation and its initial event commit together. This is not yet a production dispatch API or proof of a valid phone number.

A trusted test caller may then invoke attempt_test! outside any transaction. It claims a pending or due retryable record under a row lock, commits the submitting state and event, then calls the test adapter outside a transaction. A second transaction records its observation. A rejected or accepted result is terminal in this slice. A simulated confirmed safe pre-acceptance temporary failure may be retried after a delay, at most three times. Exhaustion is not a provider rejection. Exceptions and unrecognized results are indeterminate and are never retried automatically. An abandoned submission can be marked indeterminate, never silently reset to pending; an in-flight result racing this recovery cannot overwrite the indeterminate state. Events preserve the states observed here, but database-enforced append-only semantics are not implemented.

An opt-in test-only loop in the Sidekiq worker polls committed work for an operator-configured allowlist, NOTIFI_TEST_HANDOFF_TENANT_IDS (comma-separated positive integer tenant IDs). The default is empty, so polling is disabled. The worker enters a separate tenant transaction for each configured tenant, checks its existence, scans bounded batches of pending and due retryable test records, and uses the same locking and outcome rules described above. Old submitting records are moved to indeterminate rather than resent. PostgreSQL retains the work across a worker or Redis restart; the loop rediscovers it on its next pass. Multiple workers can scan the same records because claims are locked. The allowlist is trusted operator configuration, not user input or a job argument. Do not add production tenants to it as a production delivery mechanism.

This test loop does not cover scalable tenant onboarding or authoritative cross-tenant discovery. The runtime role is subject to tenant RLS, and an arbitrary tenant ID is not authorization. There is no general durable cross-tenant discovery mechanism: the runtime role is subject to tenant RLS, and an arbitrary tenant ID is not authorization. Before real dispatches are possible we still need an authorized real-dispatch transaction that creates frozen recipients and one delivery per destination together, an audited tenant work-discovery design, actual worker scheduling/recovery, a real provider contract with provider-specific guarantees, reconciliation of indeterminate results, callback authentication and deduplication, delivery-status ledger, retention, privacy rules, monitoring, and an explicit activation path. Never reinterpret existing simulations as authorization to send.