Routing and guard rails

Tested with test connectors and stubbed provider answers; no message has gone through a real provider with these rules yet (see the module's status).

A send request names the channels to try, in order ("channels": ["whatsapp", "sms"]). Karta works through them one step at a time and decides from facts. A plug or an agent only reports (permanent, retry, unknown_recipient, not_linked, rate_limited, unknown_outcome, unsupported, too_large, expired); only Karta retries or falls back.

When Karta moves on, and when it does not

What happened at the stepWhat Karta doesCan the customer get it twice?
The WhatsApp agent is not connectedwaits up to policy.wait_for_connector_s (default 120 seconds; 0 does not wait), then goes to the next channelno: nothing was sent
The number is not on the channel (a cached check says no, or the send answered unknown_recipient)goes to the next channel at onceno
not_linked, permanent, unsupported, too_largegoes to the next channelno
retry, rate_limited, expired (nothing was sent)tries the same channel again, three attempts in all (after the delay the class needs, Retry-After for a rate limit), then the next channelno
unknown_outcome: nobody can tell whether it went outstops. The request is unknown, the message is flagged duplicate_possible, nothing else is sent; the application decidesyes, if it were sent again
Sent, but no delivery receipt yetnothing, unless the request sets policy.fallback_after_s (300 seconds or more)
Sent, no receipt, and fallback_after_s passedsends on the next channel and flags both attempts duplicate_possibleyes, and it is said so
The provider reports a failure after it accepted the message (an SMS the carrier gave up on)goes to the next channel; never tries the same channel again

A step is also skipped, with the reason in the answer's skipped list, when no connector serves the channel (no_connector), when the channel costs money and is not allowed (not_allowed) or the daily cap is reached (paid_cap), when the agent stayed away (offline) and when a cached check says the address is not there (unknown_recipient). A request with nothing left ends failed.

Moving on because a receipt did not come is off by default because it can send the same message twice (a phone that was off comes back and shows both). Ask for it only for messages where a duplicate is harmless, such as order updates. It is refused for a sign-in code (authentication), and below 300 seconds.

Every attempt is a message of its own, in the conversation of its own connector, so the archive shows the WhatsApp attempt that failed and the SMS that followed. The answer of POST /messages (chosen_channel, attempts, skipped), GET /messages/:id and the messaging.message.status events always say the channel used, the attempt_no, fallback_from (the channel before it), the cost where the provider reported one, and duplicate_possible. final is true when Karta will make no further attempt for this request after this one.

Cost, and who may spend

SMS costs money. It is used only when

  1. the request names it in channels, and
  2. the server may use it (Messaging settings, server.sms_allowed, off until someone switches it on), and
  3. the request allows paying (policy.allow_paid: true) or the organisation does by default (paid_by_default), and
  4. the destination is in a country the organisation allows SMS to (sms_countries; by default Bonaire, Curaçao, Aruba, Sint Maarten and the Netherlands; the world is an opt-in), and
  5. the organisation's daily cap of SMS segments is not reached (paid_daily_cap; when none is set the cap is 50 segments a day, never unlimited; 0 allows none; a long text counts for each of its segments; shared by all the organisation's servers).

A request whose channels are all paid and not allowed is refused at the door (422 paid_channel_not_allowed, or 422 sms_destination_not_allowed for a country that is not on the list). As a later step it is skipped: the request fails instead of costing money. The answer carries an estimate of the segments an SMS will take (gsm7 or ucs2, 160 or 70 characters in one segment, 153 or 67 in a longer text); it is the worst case, not the invoice. The real cost is kept on the attempt when the provider reports it.

First contact

Karta does not start a WhatsApp conversation with someone without a reason. A request that names whatsapp is refused (422 first_contact_requires_reason) when the address has never written to this server on WhatsApp and the request has no consent ({"basis": "order", "ref": "1043"}: why this person may be written to, set by the application). A conversation the customer began, or a stated reason, is enough. SMS-only requests are not subject to the rule.

Quiet hours and limits

Messaging settings can switch on quiet hours: a window in the organisation's local time (a fixed offset from UTC, Bonaire is -240; it may cross midnight, 21:00 to 08:00). A request made inside it waits (state: waiting) and goes when the window ends. A sign-in code (authentication) is never held.

Each agent connector sends at most 10 messages a minute and 300 a day by default (its limits, else the organisation's send_per_minute and send_per_day). Karta counts them across all replicas (Karta.Limits); the request that would pass the limit waits a few seconds and goes later. The agent adds its own gap and jitter between sends. An SMS provider limits itself and answers 429, which Karta honours.

Waiting

Waiting is bounded. A sign-in code (authentication) never waits for an agent more than two minutes, and is not held by quiet hours; a limit that would hold it, or any request, past its deadline (two minutes for a code, twelve hours otherwise; max_wait_s) skips the step (wait_expired) and the next channel is tried, or the request fails. At most 500 requests wait on one connector (max_waiting); the next one skips the step (offline or waiting_full) instead of queueing behind them. A request that a crash left behind (queued, waiting or sending with no attempt, and no job to look at it again, for two minutes) is moved on by the housekeeping job; a request with a job, however far off, is left alone. A late report about an earlier attempt takes the request's row lock and moves nothing unless it is about the attempt the request is on; a waiting request has at most one job.

A request that waits (waiting) has no attempt yet: for the agent to return, for the end of quiet hours or for room under a limit. An Oban job looks again: every few seconds while it waits for an agent, once at the end of quiet hours or a limit. Nothing is written to an agent that is not there, so a command never sits in an outbox for nobody.