//! The outbox: messages written while the phone could not reach its object//! wait as rows (`send_state` queued) and go in the order they were written.//!//! **Oldest first in each conversation, and one stuck message holds up//! nothing for long** (20 Nov 2026). Conversations go side//! by side: a message waiting in one never holds another. Within one, the//! oldest waiting message goes first and a younger one does not overtake it,//! so the other side reads them in order, until it has been tried//! [`STUCK_AFTER`] times; from then on it is stuck, keeps its own retries,//! and the ones behind it go. A failed attempt waits before the next,//! doubling from two seconds to five minutes; a refusal the object meant (a//! bad number, too many messages) fails the row, which the person sees and//! may retry, rather than retrying it forever./// One waiting message as the outbox reads it.#[derive(Debug, Clone, PartialEq, Eq)]pubstructWaiting {pub id: String,/// Its conversation: the order holds within one, never across them.pub thread: String,/// When it was written here, ms: the order it goes in.pub written_at: i64,pub attempts: u32,/// The last attempt, ms, if any.pub tried_at: Option<i64>,}/// Attempts after which a message stops retrying and fails.pubconst ATTEMPTS_MAX: u32 = 50;/// The wait after `attempts` failed attempts, ms.pubfn backoff_ms(attempts: u32) -> i64 {if attempts == 0 {return0; }let seconds = 2i64.saturating_pow(attempts.min(9)); seconds.min(300) * 1000}/// Tries after which a waiting message stops holding up the ones written after it in its conversation.pubconst STUCK_AFTER: u32 = 3;fn ready_at(w: &Waiting) -> i64 { w.tried_at.map_or(0, |at| at + backoff_ms(w.attempts))}/// Each conversation's waiting messages that may go when ready, oldest first:/// its oldest, and past it each one behind a stuck message.fn eligible(waiting: &[Waiting]) -> Vec<&Waiting> {letmut by_thread: std::collections::BTreeMap<&str, Vec<&Waiting>> = std::collections::BTreeMap::new();for w in waiting { by_thread.entry(w.thread.as_str()).or_default().push(w); }letmut out = Vec::new();for (_, mut list) in by_thread { list.sort_by(|a, b| (a.written_at, &a.id).cmp(&(b.written_at, &b.id)));for w in list { out.push(w);if w.attempts < STUCK_AFTER {break; } } } out.sort_by(|a, b| (a.written_at, &a.id).cmp(&(b.written_at, &b.id))); out}/// The message due now, if any: the oldest of those that may go, once its wait is over.pubfn due(waiting: &[Waiting], now: i64) -> Option<&Waiting> { eligible(waiting).into_iter().find(|w| ready_at(w) <= now)}/// When the next attempt may be made, ms (the shell's timer), if anything waits.pubfn next_at(waiting: &[Waiting]) -> Option<i64> { eligible(waiting).into_iter().map(ready_at).min()}/// What an attempt's answer means for the row.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pubenumOutcome {/// The object took it: sent.Taken,/// No answer, or one that may change: try again later.Retry,/// A refusal that will not change: failed, said in words.Failed,}/// An HTTP status's meaning for an attempt (0: no answer at all).pubfn outcome(status: u16, attempts: u32) -> Outcome {match status {200..=299 => Outcome::Taken,400 | 403 | 404 | 409 | 413 => Outcome::Failed, _ if attempts + 1 >= ATTEMPTS_MAX => Outcome::Failed, _ => Outcome::Retry, }}/// The rank of a sent message's state: a receipt only ever moves it forward.pubfn rank(state: &str) -> u8 {match state {"queued" => 1,"sending" => 2,"sent" => 3,"delivered" => 4,"read" => 5, _ => 0, }}/// The state a receipt leaves a message in: the further of the two.pubfn advanced<'a>(current: &'a str, receipt: &'a str) -> &'a str {if current == "failed" || rank(receipt) <= rank(current) { current } else { receipt }}#[cfg(test)]#[path = "outbox_tests.rs"]mod tests;