Skip to main content

offline_protocol/protocol/
session.rs

1//! Session confirmation, welcome lifecycle, and pending session reconciliation.
2
3use super::{
4    classify_transport_send_error, internal_prefixes, lock_shared_state, send_failure_token,
5    OfflineProtocol, PresenceRescueThrottle, PruneAllowance, RestorableRecord, SessionState,
6    WelcomeDeliveryState, WelcomeLifecycleRecord, CONFIRMATION_PROBE_INTERVAL_SECS,
7    CONFIRMATION_RETRY_INTERVAL_SECS, MAX_REKEY_TRACKED_PEERS, RECONCILIATION_THROTTLE_MS,
8    REKEY_INTERVAL_SECS, SEND_FAIL_REASON_CONFIRM_TIMEOUT, WELCOME_INTERNET_CONFIRM_TIMEOUT_SECS,
9    WELCOME_LIFECYCLE_TTL_SECS, WELCOME_MESH_CONFIRM_TIMEOUT_SECS, WELCOME_NO_CARRIER_RETRY_SECS,
10    WELCOME_PRESENCE_RESCUE_BASE_SECS, WELCOME_PRESENCE_RESCUE_MAX_SECS, WELCOME_RETRY_BATCH_SIZE,
11    WELCOME_RETRY_JITTER_RATIO, WELCOME_UNREACHABLE_RETRY_CAP_SECS, WELCOME_WATCHLIST_MAX_AGE_SECS,
12};
13use crate::events::SecurityWarningCode;
14use crate::mls_observability::MlsOperationContext;
15use crate::protocol::reachability::{Claim, FactSource};
16use crate::{Error, EstablishmentState, Event, Result, SessionStateError};
17use chrono::{Duration as ChronoDuration, Utc};
18use offline_protocol_core::{Message, MessagePriority};
19use offline_protocol_mls::{MlsManager, WelcomeMessage};
20use offline_protocol_transport::TransportType;
21use std::collections::HashSet;
22use std::hash::{Hash, Hasher};
23use std::sync::{Arc, RwLock};
24use std::time::{Duration as StdDuration, Instant};
25use tracing::{debug, info, warn};
26
27impl OfflineProtocol {
28    // ========================================================================
29    // SESSION STATE
30    // ========================================================================
31
32    /// Returns true when persisted state marks this peer session as confirmed.
33    ///
34    /// Uses the in-memory `confirmed_sessions` cache as a fast path to avoid
35    /// hitting persistent storage on every message send. The cache is populated
36    /// when sessions are confirmed and cleared when sessions are invalidated.
37    pub(super) fn is_session_confirmed(&mut self, peer_id: &str) -> Result<bool> {
38        // Fast path: check in-memory cache only — no I/O. The cache is kept in
39        // sync by confirm_session_state() and all invalidation paths (blocking,
40        // stale cleanup, etc.). We deliberately skip the has_mls_session() guard
41        // here because it calls load_group() which hits MlsStorage (10-100ms on
42        // mobile Keychain/Keystore). If the session was wiped externally, the
43        // encrypt call will fail and we handle it there.
44        if self.confirmed_sessions.contains(peer_id) {
45            return Ok(true);
46        }
47
48        let persisted = self
49            .load_session_state_entry(peer_id)?
50            .unwrap_or(SessionState::Pending);
51        if matches!(persisted, SessionState::Confirmed) {
52            if !self.has_mls_session(peer_id)? {
53                warn!(
54                    peer_id = %peer_id,
55                    "Persisted confirmed state has no matching MLS session; clearing stale state"
56                );
57                self.confirmed_sessions.remove(peer_id);
58                self.clear_confirmation_recovery_tracking(peer_id);
59                self.welcome_lifecycles.remove(peer_id);
60                // The MLS session is gone, so any both-create owner gate for this
61                // peer is stale; clear it (memory + storage) or it would block
62                // `welcome_received` confirmation on the next re-pairing.
63                self.clear_both_create_awaiting_decrypt(peer_id);
64                if let Err(err) = self.clear_session_state_entry(peer_id) {
65                    warn!(
66                        peer_id = %peer_id,
67                        error = %err,
68                        "Failed to clear stale persisted session state"
69                    );
70                }
71                if let Err(err) = self.clear_welcome_lifecycle_entry(peer_id) {
72                    warn!(
73                        peer_id = %peer_id,
74                        error = %err,
75                        "Failed to clear stale persisted welcome lifecycle"
76                    );
77                }
78                return Ok(false);
79            }
80            self.confirmed_sessions.insert(peer_id.to_string());
81            return Ok(true);
82        }
83        Ok(false)
84    }
85
86    pub(super) fn has_mls_session(&self, peer_id: &str) -> Result<bool> {
87        let Some(mls) = self.mls_manager.clone() else {
88            return Ok(false);
89        };
90
91        let manager = mls
92            .read()
93            .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
94        manager.has_session(peer_id).map_err(Error::Mls)
95    }
96
97    /// Returns the current establishment state for a peer (for API and error reporting).
98    pub(super) fn establishment_state(&self, peer_id: &str) -> Result<EstablishmentState> {
99        if let Some(mls) = &self.mls_manager {
100            let has_session = {
101                let manager = mls
102                    .read()
103                    .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
104                manager.has_session(peer_id).map_err(Error::Mls)?
105            };
106            if has_session {
107                let state = self
108                    .load_session_state_entry(peer_id)?
109                    .unwrap_or(SessionState::Pending);
110                return Ok(if matches!(state, SessionState::Confirmed) {
111                    EstablishmentState::SessionConfirmed
112                } else {
113                    EstablishmentState::SessionPending
114                });
115            }
116        }
117
118        let now_ms = Utc::now().timestamp_millis() as u64;
119        if let Some(pkg) = self.pending_key_packages.get(peer_id) {
120            if now_ms < pkg.local_expires_at_ms {
121                return Ok(EstablishmentState::HaveKeyPackage);
122            }
123        }
124        if self.load_peer_key_package_from_storage(peer_id).is_some() {
125            return Ok(EstablishmentState::HaveKeyPackage);
126        }
127        Ok(EstablishmentState::NoKeyPackage)
128    }
129
130    /// Monotonic state transition helper: Pending -> Confirmed only.
131    pub(super) fn confirm_session_state(
132        &mut self,
133        peer_id: &str,
134        source_event: &str,
135    ) -> Result<bool> {
136        if !self.can_confirm_from_source(peer_id, source_event) {
137            warn!(
138                event = "session_confirmation_blocked",
139                session_or_group_id = %peer_id,
140                source_event = %source_event,
141                "session_confirmation_blocked"
142            );
143            return Ok(false);
144        }
145
146        let previous = self.ensure_session_state_entry(peer_id, source_event)?;
147
148        if matches!(previous, SessionState::Confirmed) {
149            self.confirmed_sessions.insert(peer_id.to_string());
150            self.clear_confirmation_recovery_tracking(peer_id);
151            self.clear_both_create_awaiting_decrypt(peer_id);
152            info!(
153                event = "session_state_transition",
154                session_or_group_id = %peer_id,
155                previous_state = "Confirmed",
156                new_state = "Confirmed",
157                source_event = %source_event,
158                "session_state_transition"
159            );
160            return Ok(false);
161        }
162
163        // Persist first, then publish in-memory view.
164        if let Err(err) = self.persist_session_state(peer_id, SessionState::Confirmed, source_event)
165        {
166            self.schedule_confirmation_retry(peer_id, source_event);
167            return Err(err);
168        }
169
170        self.confirmed_sessions.insert(peer_id.to_string());
171        self.clear_confirmation_recovery_tracking(peer_id);
172        // Group-aware proof has arrived (decrypt), so we no longer need to gate
173        // confirmation on decrypt for this both-create peer.
174        self.clear_both_create_awaiting_decrypt(peer_id);
175        // The peer has proved the session, so the outbound Welcome — kept
176        // non-terminal so a lost fragment keeps being retried — is delivered.
177        // Mark it Sent so process_welcome_retry_queue stops re-sending it. Mesh
178        // has no transport-level delivery ack, so this session proof is the mesh
179        // equivalent of on_transport_send_confirmed.
180        self.mark_welcome_confirmed(peer_id, source_event);
181        if source_event != "welcome_received" && source_event != "decrypt_success" {
182            self.maybe_emit_local_session_established(
183                peer_id,
184                Self::session_ready_context_for_source(source_event),
185            );
186        }
187        info!(
188            event = "session_state_transition",
189            session_or_group_id = %peer_id,
190            previous_state = previous.as_str(),
191            new_state = "Confirmed",
192            source_event = %source_event,
193            "session_state_transition"
194        );
195
196        // Replication starts here rather than at the four callers that reach
197        // this transition, because the four callers are exactly the shape of
198        // bug that keeps happening: one of them forgets, and the feature is
199        // silently absent on whichever confirmation path that caller owned.
200        // This is also the only signal that covers all four — the session
201        // lifecycle event does not fire on the probe and ack paths.
202        #[cfg(feature = "data")]
203        self.kick_data_sync(peer_id, source_event);
204
205        Ok(true)
206    }
207
208    /// Marks a non-terminal outbound welcome lifecycle as `Sent` once the peer
209    /// has proved the session (probe / ack / welcome / decrypt). The mesh
210    /// equivalent of [`Self::on_transport_send_confirmed`], which only fires for
211    /// Internet. No-op when there is no welcome lifecycle for `peer_id` (e.g. a
212    /// confirmation keyed by a group id rather than a 1:1 peer) or it is already
213    /// terminal, so it is safe to call from every confirmation path.
214    fn mark_welcome_confirmed(&mut self, peer_id: &str, source_event: &str) {
215        match self.welcome_lifecycles.get(peer_id).map(|r| r.state) {
216            // In flight / retried — fall through and mark Sent below.
217            Some(WelcomeDeliveryState::SendAttempted) | Some(WelcomeDeliveryState::Failed) => {}
218            // Parked, never actually sent (no carrier at send time) for a peer
219            // that converged over some other path. `Created -> Sent` is not a
220            // legal transition and there is nothing in flight to confirm, so drop
221            // the now-moot lifecycle (memory + storage) rather than leaving it to
222            // linger until the session is deleted.
223            Some(WelcomeDeliveryState::Created) => {
224                self.welcome_lifecycles.remove(peer_id);
225                if let Err(err) = self.clear_welcome_lifecycle_entry(peer_id) {
226                    warn!(
227                        peer_id = %peer_id,
228                        error = %err,
229                        "Failed to clear parked welcome lifecycle on confirmation"
230                    );
231                }
232                return;
233            }
234            // Already terminal (Sent / Expired) or no lifecycle — nothing to do.
235            _ => return,
236        }
237        if let Some(record) = self.welcome_lifecycles.get_mut(peer_id) {
238            record.next_retry_at = None;
239            record.last_reason_code = None;
240            record.last_transport_error = None;
241        }
242        // transition_welcome_state persists the record and logs the transition.
243        if self
244            .transition_welcome_state(peer_id, WelcomeDeliveryState::Sent, source_event)
245            .is_err()
246        {
247            return;
248        }
249        if let Some(snapshot) = self.welcome_lifecycles.get(peer_id).cloned() {
250            if let Ok(state) = lock_shared_state(&self.shared_state) {
251                state.emit_event(Event::welcome_send_succeeded(
252                    peer_id.to_string(),
253                    snapshot.welcome_message.id.as_str().to_string(),
254                    snapshot.group_id,
255                    snapshot.attempt,
256                ));
257            }
258        }
259    }
260
261    /// Ensures a session has an explicit persisted state entry.
262    pub(super) fn ensure_session_state_entry(
263        &self,
264        peer_id: &str,
265        source_event: &str,
266    ) -> Result<SessionState> {
267        let existing = self.load_session_state_entry(peer_id)?;
268        if let Some(state) = existing {
269            return Ok(state);
270        }
271
272        self.persist_session_state(peer_id, SessionState::Pending, source_event)?;
273        info!(
274            event = "session_state_transition",
275            session_or_group_id = %peer_id,
276            previous_state = "Absent",
277            new_state = "Pending",
278            source_event = %source_event,
279            "session_state_transition"
280        );
281        Ok(SessionState::Pending)
282    }
283
284    /// Reconstructs runtime confirmation cache from persisted session states.
285    /// `allowance` is the launch's shared advisory pool — this walk's deletes
286    /// are advisory (a dropped record re-bootstraps to `Pending`, which is the
287    /// safe answer), and they add up against the same allowance as the four
288    /// category walks. See `storage::MAX_RESTORE_PRUNE_DELETES`.
289    pub(super) fn restore_session_states_from_manager(
290        &mut self,
291        mls: Arc<RwLock<MlsManager>>,
292        allowance: &mut PruneAllowance,
293    ) -> Result<()> {
294        self.confirmed_sessions.clear();
295
296        let sessions = {
297            let manager = mls
298                .read()
299                .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
300            manager.list_sessions()?
301        };
302
303        // The session list itself is deliberately not truncated the way a
304        // container-listed walk is: dropping its tail would silently leave
305        // confirmed peers out of `confirmed_sessions`. What needed bounding was
306        // the *deletes* a bad store makes this walk issue — one device barrier
307        // per peer, with nothing counting them until now.
308        let mut budget = allowance.refusing();
309        for peer_id in sessions {
310            // Before anything that can `continue`: an MLS session with this
311            // peer exists, which is the capability fact, and it holds whether
312            // or not the peer's `session_states` record turns out to be
313            // readable. Deriving it from the record instead would reintroduce
314            // exactly the deletable dependency `encryption_capable_peers`
315            // exists to escape.
316            self.mark_encryption_capable(&peer_id);
317
318            // Restore-path read: a record whose bytes will not decode is
319            // dropped and re-bootstrapped as `Pending` rather than failing
320            // initialization forever. The send path deliberately keeps the
321            // strict loader — see `load_restorable_state_record`.
322            let state = match self.load_session_state_for_restore(&peer_id, Some(&mut budget)) {
323                RestorableRecord::Present(state) => state,
324                RestorableRecord::Absent => self.bootstrap_missing_session_state(&peer_id),
325                // A record is still on disk and could not be read *this*
326                // session. Re-bootstrapping would persist `Pending` straight
327                // over one that may say `Confirmed`, and propagating would fail
328                // `initialize_mls` for as long as that single record stays
329                // unreadable — on an install that then cannot send at all.
330                // Skipping is what leaves the record recoverable: the peer is
331                // simply absent from `confirmed_sessions`, and the send path's
332                // own strict loader (`is_session_confirmed`) still fails closed
333                // for it — and re-reads the record, so a transient failure
334                // heals the moment the store answers again.
335                //
336                // Three consequences worth naming rather than rediscovering,
337                // all of them following from the peer being absent from
338                // `confirmed_sessions` for the run:
339                //
340                // 1. The Welcome machinery gates on
341                //    `!confirmed_sessions.contains(peer)` *plus* a live
342                //    `welcome_lifecycles` entry (`welcome_pending_peers`,
343                //    `resend_unconfirmed_sent_welcome`, the retry ladder). So a
344                //    peer whose lifecycle restored as `Sent` while its session
345                //    state did not can draw a redundant Welcome re-send.
346                // 2. `flush_restored_confirmed_pending_messages` will not flush
347                //    this peer's queued pre-session messages this session. They
348                //    stay queued — in memory and on disk — and flush once a
349                //    launch can read the record, or once a live confirmation
350                //    re-adds the peer.
351                // 3. Tier-2 crypto recovery re-seals a resend only for a peer
352                //    in `confirmed_sessions` (`reseal_resend_content`), so a
353                //    resend to this peer replays its original ciphertext for
354                //    the run rather than re-sealing to the current epoch.
355                //
356                // All three are deferrals on an already-degraded store, and all
357                // three are strictly better than the alternative this replaced
358                // — propagating, which failed `initialize_mls` on every launch
359                // for as long as the one record stayed unreadable, on an
360                // install that could then send nothing at all.
361                RestorableRecord::Unavailable => {
362                    warn!(
363                        session_or_group_id = %peer_id,
364                        "Session state could not be read this session; leaving the record in \
365                         place and treating the session as unconfirmed"
366                    );
367                    continue;
368                }
369            };
370            if matches!(state, SessionState::Confirmed) {
371                self.confirmed_sessions.insert(peer_id.clone());
372            }
373            info!(
374                event = "session_state_restored",
375                session_or_group_id = %peer_id,
376                previous_state = "Pending",
377                new_state = %state.as_str(),
378                source_event = "initialize_mls",
379                "session_state_restored"
380            );
381        }
382
383        if budget.exhausted {
384            warn!(
385                deleted = budget.spent,
386                budget = super::storage::MAX_RESTORE_PRUNE_DELETES,
387                "Session state restore hit its share of the launch delete budget; the \
388                 unreadable records left behind are re-bootstrapped as Pending anyway and \
389                 dropped on a later launch"
390            );
391        }
392
393        Ok(())
394    }
395
396    /// The state a session with no readable record starts from.
397    ///
398    /// Infallible: the migration write is best effort, like every other
399    /// persistence call on the restore path. It used to propagate, so one
400    /// `StoreFailed` — a full disk, a container the OS has locked — failed
401    /// `initialize_mls` outright and, with `require_encryption` on by default,
402    /// left the install unable to send. `Pending` is the safe answer in memory
403    /// either way, and a launch that can write re-derives the record.
404    pub(super) fn bootstrap_missing_session_state(&self, peer_id: &str) -> SessionState {
405        // Legacy session records without explicit state are treated as Pending.
406        // Recovery is driven by probe/ack reconciliation, never by implicit inference.
407        let restored_state = SessionState::Pending;
408        if let Err(e) = self.persist_session_state(
409            peer_id,
410            restored_state,
411            "initialize_mls_missing_state_migration",
412        ) {
413            warn!(
414                session_or_group_id = %peer_id,
415                error = %e,
416                "Failed to persist the bootstrapped session state; the session is Pending for \
417                 this run and the record is re-derived on the next launch"
418            );
419        }
420        info!(
421            event = "session_state_transition",
422            session_or_group_id = %peer_id,
423            previous_state = "Absent",
424            new_state = %restored_state.as_str(),
425            source_event = "initialize_mls_missing_state_migration",
426            "session_state_transition"
427        );
428
429        restored_state
430    }
431
432    // ========================================================================
433    // CONFIRMATION RECOVERY
434    // ========================================================================
435
436    pub(super) fn schedule_confirmation_retry(&mut self, peer_id: &str, source_event: &str) {
437        self.confirmation_retry_due_at
438            .insert(peer_id.to_string(), Utc::now());
439        warn!(
440            event = "session_confirmation_retry_scheduled",
441            session_or_group_id = %peer_id,
442            source_event = %source_event,
443            "session_confirmation_retry_scheduled"
444        );
445    }
446
447    pub(super) fn clear_confirmation_recovery_tracking(&mut self, peer_id: &str) {
448        self.confirmation_retry_due_at.remove(peer_id);
449        self.confirmation_probe_due_at.remove(peer_id);
450    }
451
452    pub(super) fn collect_pending_session_peers(&mut self) -> Result<Vec<String>> {
453        let Some(mls) = self.mls_manager.clone() else {
454            return Ok(Vec::new());
455        };
456
457        let sessions = {
458            let manager = mls
459                .read()
460                .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
461            manager.list_sessions()?
462        };
463
464        let mut pending = Vec::new();
465        for peer_id in sessions {
466            if !self.is_session_confirmed(&peer_id)? {
467                pending.push(peer_id);
468            }
469        }
470
471        Ok(pending)
472    }
473
474    pub(super) fn send_session_confirmation_probe(&mut self, peer_id: &str, source_event: &str) {
475        match self.send_internal_message(
476            peer_id,
477            internal_prefixes::SESSION_CONFIRM_PROBE.to_string(),
478            MessagePriority::High,
479        ) {
480            Ok(_) => {
481                info!(
482                    event = "session_confirmation_probe_sent",
483                    session_or_group_id = %peer_id,
484                    source_event = %source_event,
485                    "session_confirmation_probe_sent"
486                );
487            }
488            Err(err) => {
489                warn!(
490                    event = "session_confirmation_probe_failed",
491                    session_or_group_id = %peer_id,
492                    source_event = %source_event,
493                    error = %err,
494                    "session_confirmation_probe_failed"
495                );
496            }
497        }
498    }
499
500    /// Throttled wrapper around session reconciliation. Skips the expensive
501    /// `list_sessions()` storage I/O when there is no pending work or when the
502    /// last scan ran recently (within `RECONCILIATION_THROTTLE_MS`).
503    ///
504    /// This is the entry-point called from `process()` and `receive_message()`
505    /// on every tick/poll. Without throttling, the storage call holds the
506    /// protocol Mutex and blocks `sendMessage()` on mobile platforms.
507    pub(super) fn run_throttled_reconciliation(&mut self, source_event: &str) {
508        // Fast path: nothing pending → skip entirely (no storage I/O)
509        let has_pending_work = !self.pending_encrypted_messages.is_empty()
510            || !self.confirmation_probe_due_at.is_empty()
511            || !self.confirmation_retry_due_at.is_empty();
512
513        if !has_pending_work {
514            return;
515        }
516
517        // Throttle: only run the full scan every RECONCILIATION_THROTTLE_MS
518        let now = Instant::now();
519        let throttle = StdDuration::from_millis(RECONCILIATION_THROTTLE_MS);
520        if let Some(last) = self.last_reconciliation_at {
521            if now.duration_since(last) < throttle {
522                return;
523            }
524        }
525
526        self.last_reconciliation_at = Some(now);
527        self.retry_pending_session_confirmations();
528        self.kick_pending_session_reconciliation(source_event);
529    }
530
531    pub(super) fn kick_pending_session_reconciliation(&mut self, source_event: &str) {
532        let now = Utc::now();
533        let pending_peers = match self.collect_pending_session_peers() {
534            Ok(peers) => peers,
535            Err(err) => {
536                warn!(
537                    event = "session_confirmation_probe_scan_failed",
538                    source_event = %source_event,
539                    error = %err,
540                    "session_confirmation_probe_scan_failed"
541                );
542                return;
543            }
544        };
545
546        let pending_set: HashSet<String> = pending_peers.iter().cloned().collect();
547        self.confirmation_probe_due_at
548            .retain(|peer, _| pending_set.contains(peer));
549
550        for peer_id in pending_peers {
551            let due_at = self
552                .confirmation_probe_due_at
553                .get(&peer_id)
554                .copied()
555                .unwrap_or(now);
556            if due_at > now {
557                continue;
558            }
559
560            self.send_session_confirmation_probe(&peer_id, source_event);
561            self.confirmation_probe_due_at.insert(
562                peer_id,
563                now + ChronoDuration::seconds(CONFIRMATION_PROBE_INTERVAL_SECS),
564            );
565        }
566    }
567
568    /// Triggers a rate-limited re-key of the 1:1 session with `peer_id` after an
569    /// epoch-desync decrypt failure.
570    ///
571    /// The self-healing primitive is a two-part move, matching the unblock
572    /// `session_reset` flow: tear down our *own* stale session **and** advertise
573    /// a `session_reset` key package. The peer, on receiving it, drops its stale
574    /// session, auto-establishes a fresh one from our enclosed key package, and
575    /// Welcomes us back — which we, now session-less, simply *join*.
576    ///
577    /// Tearing down our session alone (without the reset key package) would
578    /// deadlock: the peer's original key package was consumed at first
579    /// establishment, so nothing could rebuild the channel, and the peer (still
580    /// believing its session is healthy) would never re-advertise one. Sending
581    /// the reset key package is what breaks that deadlock.
582    ///
583    /// Deleting the local session is also what makes convergence **symmetric and
584    /// single-round regardless of user-id ordering**. Because we keep no local
585    /// session, the peer's returning Welcome is joined via the fresh-session
586    /// path rather than the greater-id-adopts tiebreaker in
587    /// `handle_welcome_message` — so the lexicographically-smaller detector is no
588    /// longer stranded (the failure mode when the stale session was kept). Both
589    /// orderings are covered end-to-end by
590    /// `test_desync_dm_heals_end_to_end_when_detector_id_is_greater` and
591    /// `test_desync_dm_heals_end_to_end_when_detector_id_is_smaller`.
592    ///
593    /// Rate-limited via `rekey_due_at` to at most one re-key per
594    /// [`REKEY_INTERVAL_SECS`] per peer, so a peer replaying stale-epoch
595    /// ciphertext (or an injected wrong-epoch frame) cannot drive a re-key storm.
596    /// The interval is stamped before the send so a send error still counts
597    /// against the limit, and it is **never reset early** — not even by a
598    /// successful decrypt on the healed session. That is deliberate: a genuine
599    /// re-fork and a replayed old-epoch frame are indistinguishable at this layer
600    /// (both surface as `WrongEpoch`), so clearing the floor on heal would let an
601    /// attacker who lands one legit decrypt between replays defeat the limit and
602    /// force ~one teardown per inbound message. The floor lapses naturally after
603    /// [`REKEY_INTERVAL_SECS`]; during that window Tier 1 (un-ACK + sender
604    /// retries) keeps delivery honest, so the only cost of not resetting is that a
605    /// genuine second desync within the window heals up to one interval later —
606    /// an acceptable trade for a rare event against a bounded-churn guarantee.
607    ///
608    /// **SECURITY — this trigger is unauthenticated, and cannot be made
609    /// otherwise.** `peer_id` is the *wire-claimed* sender. Three facts compose:
610    /// `__MLS_ENC__` is a data-plane prefix, deliberately exempt from the
611    /// Ed25519 + derivation control-message gate (see `DATA_PLANE_PREFIXES`);
612    /// OpenMLS
613    /// validates the framing header — group id, then epoch — *before* any AEAD,
614    /// sender-data decryption or signature check, so `WrongEpoch` /
615    /// `NoPastEpochData` is produced with the sender still entirely unverified;
616    /// and a 1:1 slot id is `session:<a>:<b>` over two public user ids. So the
617    /// classification that lands here is reachable by **anyone who can inject a
618    /// frame** — no key material, no captured ciphertext, no session, no replay
619    /// needed. `test_forged_frame_reaches_session_desync_without_any_key_material`
620    /// builds such a frame from scratch to keep this statement honest. It is
621    /// inherent to MLS framing, not an OpenMLS defect, so no upgrade changes it.
622    ///
623    /// The `SenderIdentityMismatch` gate cannot help: it compares the MLS
624    /// credential to the claimed sender, and that credential only exists once
625    /// `process_message` **succeeds**. Every pre-authentication verdict is
626    /// structurally outside its reach.
627    ///
628    /// Because the trigger cannot be authenticated, the mitigation is that
629    /// acting on it is **harmless**, not that it is trusted:
630    /// - `SessionManager::decrypt_message` requires the envelope to name the
631    ///   slot shared with the claimed sender, so one derivable session id
632    ///   cannot be aimed at arbitrary peers, and `rekey_due_at` cannot be grown
633    ///   with attacker-chosen keys.
634    /// - [`REKEY_INTERVAL_SECS`] bounds this to one re-key per peer per window.
635    /// - The heal destroys nothing: queued plaintext survives a reset (it seals
636    ///   at flush time against the rebuilt session), and Tier 2 re-seals
637    ///   in-flight resends.
638    /// - Each re-key emits `SecurityWarningCode::SessionRekeyTriggered`, so a
639    ///   sustained rate — the signature of injected frames rather than a real
640    ///   fork — is visible to the app.
641    ///
642    /// **Residual:** an injector can still force bounded re-key churn on a pair
643    /// (delayed delivery, never lost). Closing that needs a signed
644    /// epoch-corroboration exchange before teardown; a liveness-only probe does
645    /// not work, since a healthy peer answers and we would tear down anyway.
646    /// Also see `docs/state-machines/session-lifecycle.md` ("Desync and heal")
647    /// and `docs/security/threat-model.md` (residual risk R2).
648    pub(super) fn schedule_session_rekey(&mut self, peer_id: &str) {
649        let now = Utc::now();
650        if let Some(due_at) = self.rekey_due_at.get(peer_id) {
651            if *due_at > now {
652                return;
653            }
654        }
655        // Bounded like every other map keyed by a wire-claimed id: the desync
656        // that gets us here is classified before MLS authenticates anything.
657        if !self.rekey_due_at.contains_key(peer_id)
658            && self.rekey_due_at.len() >= MAX_REKEY_TRACKED_PEERS
659        {
660            self.rekey_due_at.clear();
661        }
662        self.rekey_due_at.insert(
663            peer_id.to_string(),
664            now + ChronoDuration::seconds(REKEY_INTERVAL_SECS),
665        );
666
667        // A re-key is remotely triggerable and, until now, entirely silent to
668        // the app. A genuine fork produces these occasionally; a sustained rate
669        // for one peer is the signature of injected frames, which an operator
670        // cannot otherwise distinguish.
671        self.emit_security_warning(
672            peer_id,
673            SecurityWarningCode::SessionRekeyTriggered,
674            "1:1 session torn down and re-advertised after an epoch desync",
675        );
676        // Tear down our own stale session before advertising the reset key
677        // package, mirroring the unblock `session_reset` flow. This is what makes
678        // convergence symmetric regardless of user-id ordering: with no local
679        // session left, the peer's returning Welcome is *joined* (not gated by
680        // the greater-id-adopts tiebreaker in `handle_welcome_message`), so both
681        // sides rebuild from scratch and converge in a single round. Keeping the
682        // stale session instead strands the smaller-id detector, which the
683        // tiebreaker forbids from adopting. Best-effort: a missing session is a
684        // no-op, and a failed delete still sends the reset (the peer rebuild plus
685        // the auto key-package exchange re-arm establishment either way).
686        if self.has_mls_session(peer_id).unwrap_or(false) {
687            if let Err(err) = self.manual_mls_delete_session(peer_id) {
688                debug!(
689                    peer_id = %peer_id,
690                    error = %err,
691                    "rekey: failed to delete local stale session; sending reset anyway"
692                );
693            }
694        }
695        match self.send_key_package_to(peer_id, true) {
696            Ok(()) => {
697                info!(
698                    event = "session_rekey_triggered",
699                    peer_id = %peer_id,
700                    "Triggered 1:1 session re-key after epoch desync"
701                );
702            }
703            Err(err) => {
704                warn!(
705                    event = "session_rekey_send_failed",
706                    peer_id = %peer_id,
707                    error = %err,
708                    "session_rekey_send_failed"
709                );
710            }
711        }
712    }
713
714    pub(super) fn retry_pending_session_confirmations(&mut self) {
715        let now = Utc::now();
716        let due_peers: Vec<String> = self
717            .confirmation_retry_due_at
718            .iter()
719            .filter_map(|(peer_id, due_at)| {
720                if *due_at <= now {
721                    Some(peer_id.clone())
722                } else {
723                    None
724                }
725            })
726            .collect();
727
728        for peer_id in due_peers {
729            match self.has_mls_session(&peer_id) {
730                Ok(true) => {}
731                Ok(false) => {
732                    self.confirmation_retry_due_at.remove(&peer_id);
733                    continue;
734                }
735                Err(err) => {
736                    warn!(
737                        event = "session_confirmation_retry_scan_failed",
738                        session_or_group_id = %peer_id,
739                        error = %err,
740                        "session_confirmation_retry_scan_failed"
741                    );
742                    self.confirmation_retry_due_at.insert(
743                        peer_id,
744                        now + ChronoDuration::seconds(CONFIRMATION_RETRY_INTERVAL_SECS),
745                    );
746                    continue;
747                }
748            }
749
750            if !self.can_confirm_from_source(&peer_id, "confirmation_retry") {
751                debug!(
752                    peer_id = %peer_id,
753                    "Skipping confirmation retry until welcome send is at least attempted"
754                );
755                continue;
756            }
757
758            match self.confirm_session_state(&peer_id, "confirmation_retry") {
759                Ok(_) => {
760                    let _ = self.flush_pending_messages(&peer_id);
761                    self.process_pending_decryption(&peer_id);
762                }
763                Err(err) => {
764                    warn!(
765                        event = "session_confirmation_retry_failed",
766                        session_or_group_id = %peer_id,
767                        error = %err,
768                        "session_confirmation_retry_failed"
769                    );
770                    self.confirmation_retry_due_at.insert(
771                        peer_id,
772                        now + ChronoDuration::seconds(CONFIRMATION_RETRY_INTERVAL_SECS),
773                    );
774                }
775            }
776        }
777    }
778
779    /// Marks `peer_id` as a both-create owner gate — we kept our own group and
780    /// must observe a group-aware decrypt before confirming — and persists it so
781    /// an owner restart mid-convergence cannot let a stale plaintext probe/ack
782    /// confirm prematurely. Only writes storage on a genuine insert.
783    pub(super) fn mark_both_create_awaiting_decrypt(&mut self, peer_id: &str) {
784        if self
785            .both_create_awaiting_decrypt
786            .insert(peer_id.to_string())
787        {
788            self.persist_both_create_awaiting_decrypt(peer_id);
789        }
790    }
791
792    /// Clears the both-create owner gate for `peer_id` (it has converged) from
793    /// both memory and storage. Only writes storage on a genuine removal.
794    pub(super) fn clear_both_create_awaiting_decrypt(&mut self, peer_id: &str) {
795        if self.both_create_awaiting_decrypt.remove(peer_id) {
796            self.delete_both_create_awaiting_decrypt(peer_id);
797        }
798    }
799
800    pub(super) fn can_confirm_from_source(&self, peer_id: &str, source_event: &str) -> bool {
801        // Both-create owner: we kept our own group and are waiting for the peer to
802        // prove it adopted *our* group. Only a successful decrypt is group-aware
803        // proof of that; a plaintext probe/ack proves only that the peer holds
804        // some session (possibly its own, pre-adoption group), so it must not
805        // confirm us or we would stop retransmitting and strand the peer.
806        if self.both_create_awaiting_decrypt.contains(peer_id) && source_event != "decrypt_success"
807        {
808            return false;
809        }
810
811        // A successful group decrypt is definitive, group-aware proof that the
812        // peer adopted our 1:1 group. `decrypt_success` is raised only inside
813        // `DecryptResult::Success` (see message_dispatch.rs): ciphertext on the
814        // single stored `session:<a>:<b>` group actually decrypted, which is
815        // possible only if the peer is a member. It must therefore confirm the
816        // session regardless of our *local* Welcome's delivery state. Gating it on
817        // a still-active local Welcome (the lifecycle match below) strands a
818        // both-create owner whose own Welcome timed out to `Failed`/`Expired` on a
819        // lossy or asymmetric BLE link (the Android↔iOS case): the owner decrypts
820        // the peer's messages but never confirms, so every outbound send fails
821        // `SessionNotReady` — it can receive but never reply. The both-create gate
822        // above is preserved, so a both-create owner is still restricted to *only*
823        // decrypt_success (a plaintext probe/ack can never confirm it).
824        if source_event == "decrypt_success" {
825            return true;
826        }
827
828        if !matches!(
829            source_event,
830            "confirmation_ack_received" | "confirmation_probe_received" | "confirmation_retry"
831        ) {
832            return true;
833        }
834
835        match self.welcome_lifecycles.get(peer_id) {
836            Some(record) => matches!(
837                record.state,
838                WelcomeDeliveryState::Sent | WelcomeDeliveryState::SendAttempted
839            ),
840            None => matches!(
841                source_event,
842                // Compatibility path for sessions created before welcome lifecycle
843                // persistence existed. A plaintext probe/ack/retry can confirm here
844                // (it is the only evidence such a legacy session has); a group-aware
845                // decrypt already returned true above and never reaches this arm.
846                "confirmation_ack_received" | "confirmation_probe_received" | "confirmation_retry"
847            ),
848        }
849    }
850
851    pub(super) fn abort_pending_session_for_peer(
852        &mut self,
853        peer_id: &str,
854        reason: crate::events::WelcomeReasonCode,
855    ) {
856        self.drop_pending_queue_for_peer(
857            peer_id,
858            &format!("Welcome delivery failed: {}", reason.as_str()),
859        );
860        if let Ok(state) = lock_shared_state(&self.shared_state) {
861            state.emit_event(Event::secure_session_failed(
862                peer_id.to_string(),
863                format!("Welcome delivery failed: {}", reason.as_str()),
864            ));
865        }
866    }
867
868    pub(super) fn maybe_emit_local_session_established(
869        &self,
870        peer_id: &str,
871        context: MlsOperationContext,
872    ) {
873        let Some(record) = self.welcome_lifecycles.get(peer_id) else {
874            return;
875        };
876        if !matches!(record.state, WelcomeDeliveryState::Sent) {
877            return;
878        }
879        self.emit_mls_session_ready(peer_id, &record.group_id, context);
880        if let Ok(state) = lock_shared_state(&self.shared_state) {
881            state.emit_event(Event::secure_session_established(
882                peer_id.to_string(),
883                record.group_id.clone(),
884                record.group_id.starts_with("session:"),
885                true,
886            ));
887        }
888    }
889
890    // ========================================================================
891    // WELCOME LIFECYCLE
892    // ========================================================================
893
894    pub(super) fn send_welcome_message(
895        &mut self,
896        recipient: &str,
897        welcome: &WelcomeMessage,
898    ) -> Result<bool> {
899        let serialized =
900            serde_json::to_string(welcome).map_err(|e| Error::Serialization(e.to_string()))?;
901        let content = format!("{}{}", internal_prefixes::WELCOME, serialized);
902        let mut message =
903            self.create_message(recipient, content, Some(MessagePriority::High), None)?;
904        self.sign_control_message(&mut message)?;
905        let group_id = welcome.group_id.as_str().to_string();
906
907        self.upsert_welcome_lifecycle(recipient, &group_id, message, "welcome_created")?;
908        self.try_send_welcome(recipient, "welcome_initial_send")
909    }
910
911    /// Parks a Welcome that currently has no transport carrier: refresh the
912    /// carrier-relative TTL and schedule a slow re-check, WITHOUT consuming a
913    /// retry attempt, transitioning state, or emitting a `WelcomeSendAttempted`
914    /// event. A no-carrier send is a guaranteed failure, so re-attempting it at
915    /// the data-plane retry rate only churns storage I/O and app events while a
916    /// device is offline. The lifecycle stays in its current non-terminal state
917    /// (Created on the first send, Failed on a later retry) so the retry queue
918    /// re-polls it on [`WELCOME_NO_CARRIER_RETRY_SECS`]; `on_neighbor_discovered`
919    /// re-arms it immediately when a carrier surfaces the peer. Returns
920    /// `Ok(false)` (not sent).
921    ///
922    /// **Invariant: the refreshed TTL always outlives the retry being
923    /// scheduled.** [`Self::try_send_welcome`] checks `expires_at` *before*
924    /// running a due retry, so a record whose TTL lapsed while it waited gets
925    /// expired terminally (`welcome_send_expired` + `secure_session_failed`) by
926    /// the very timer meant to recover it. That is not hypothetical at the far
927    /// end of the unreachable ladder in
928    /// [`Self::apply_recipient_unreachable_failure`]: it reaches 480s at six
929    /// consecutive parks and [`WELCOME_UNREACHABLE_RETRY_CAP_SECS`] (600s)
930    /// beyond that, both above the 300s [`WELCOME_LIFECYCLE_TTL_SECS`]. A full
931    /// extra interval of slack keeps the probe's own outcome — a verdict
932    /// re-park or a confirm timeout — inside the window too. No-op for the 15s
933    /// no-carrier park, whose interval is far below the TTL.
934    fn park_welcome_no_carrier(
935        &mut self,
936        peer_id: &str,
937        reason: crate::events::WelcomeReasonCode,
938        transport_error: Option<&'static str>,
939        retry_in_secs: i64,
940    ) -> Result<bool> {
941        let snapshot = {
942            let Some(record) = self.welcome_lifecycles.get_mut(peer_id) else {
943                return Ok(false);
944            };
945            let now = Utc::now();
946            record.next_retry_at = Some(now + ChronoDuration::seconds(retry_in_secs));
947            record.expires_at = now
948                + ChronoDuration::seconds(
949                    WELCOME_LIFECYCLE_TTL_SECS.max(retry_in_secs.saturating_mul(2)),
950                );
951            record.last_reason_code = Some(reason);
952            record.last_transport_error = transport_error.map(str::to_string);
953            record.clone()
954        };
955        self.persist_welcome_lifecycle_entry(&snapshot)?;
956        debug!(
957            peer_id = %peer_id,
958            state = snapshot.state.as_str(),
959            retry_in_secs,
960            "Welcome parked: no transport carrier, re-checking on slow interval"
961        );
962        Ok(false)
963    }
964
965    pub(super) fn try_send_welcome(&mut self, peer_id: &str, source_event: &str) -> Result<bool> {
966        let now = Utc::now();
967        let mut record = self
968            .welcome_lifecycles
969            .get(peer_id)
970            .cloned()
971            .ok_or_else(|| Error::Other(format!("Missing welcome lifecycle for {}", peer_id)))?;
972
973        if matches!(record.state, WelcomeDeliveryState::Sent) {
974            return Ok(true);
975        }
976        if matches!(record.state, WelcomeDeliveryState::Expired) {
977            return Ok(false);
978        }
979
980        // Only honor the TTL when a carrier actually exists: a Welcome that has
981        // never had a deliverable transport must not age out.
982        let carrier_available = !self.transport_manager.get_available_transports().is_empty();
983        if carrier_available && record.expires_at <= now {
984            self.transition_welcome_state(peer_id, WelcomeDeliveryState::Expired, source_event)?;
985            if let Ok(state) = lock_shared_state(&self.shared_state) {
986                state.emit_event(Event::welcome_send_expired(
987                    peer_id.to_string(),
988                    record.welcome_message.id.as_str().to_string(),
989                    record.attempt,
990                    crate::events::WelcomeReasonCode::RetryExhausted,
991                ));
992            }
993            self.abort_pending_session_for_peer(
994                peer_id,
995                crate::events::WelcomeReasonCode::RetryExhausted,
996            );
997            return Ok(false);
998        }
999
1000        // No carrier at all: a send is guaranteed to fail, so do NOT burn the
1001        // speculative attempt increment, the SendAttempted/Failed transitions
1002        // (two persisted state changes + two info logs), and a
1003        // `WelcomeSendAttempted` event on it every retry tick. Park the
1004        // lifecycle on a slow carrier-relative interval instead and re-check
1005        // later; `on_neighbor_discovered` re-arms it immediately when a carrier
1006        // surfaces the peer. This keeps an offline device quiet rather than
1007        // spinning storage I/O + app events at the data-plane retry rate.
1008        if !carrier_available {
1009            return self.park_welcome_no_carrier(
1010                peer_id,
1011                crate::events::WelcomeReasonCode::TransportUnavailable,
1012                None,
1013                WELCOME_NO_CARRIER_RETRY_SECS,
1014            );
1015        }
1016
1017        record.attempt = record.attempt.saturating_add(1);
1018        self.welcome_lifecycles
1019            .insert(peer_id.to_string(), record.clone());
1020        self.persist_welcome_lifecycle_entry(&record)?;
1021        self.transition_welcome_state(peer_id, WelcomeDeliveryState::SendAttempted, source_event)?;
1022
1023        if let Ok(state) = lock_shared_state(&self.shared_state) {
1024            state.emit_event(Event::welcome_send_attempted(
1025                peer_id.to_string(),
1026                record.welcome_message.id.as_str().to_string(),
1027                record.group_id.clone(),
1028                record.attempt,
1029            ));
1030        }
1031
1032        match self.transport_manager.send(&record.welcome_message) {
1033            Ok(()) => {
1034                let transport_used = self.transport_manager.current_transport();
1035                let mut updated =
1036                    self.welcome_lifecycles
1037                        .get(peer_id)
1038                        .cloned()
1039                        .ok_or_else(|| {
1040                            Error::Other(format!("Missing welcome lifecycle for {}", peer_id))
1041                        })?;
1042
1043                // No transport's send() proves the peer reassembled and joined
1044                // the group, so keep the lifecycle NON-TERMINAL until an explicit
1045                // confirmation arrives and let the retry queue re-send on timeout.
1046                //
1047                //   - Internet send() only enqueues for platform polling; the
1048                //     confirmation is on_transport_send_confirmed.
1049                //   - A BLE / WiFi-Direct GATT WRITE_TYPE_NO_RESPONSE returning Ok
1050                //     only means the local stack accepted the bytes. A single lost
1051                //     fragment of the multi-fragment Welcome would otherwise leave
1052                //     the sender believing the session exists while the peer holds
1053                //     undecryptable ciphertext, with NO retry — permanently
1054                //     bricking the pair. The mesh confirmation is the peer proving
1055                //     the session (its probe / ack / welcome / decrypt), which marks
1056                //     the welcome Sent via confirm_session_state.
1057                let confirm_timeout_secs =
1058                    if matches!(transport_used, Some(TransportType::Internet)) {
1059                        WELCOME_INTERNET_CONFIRM_TIMEOUT_SECS
1060                    } else {
1061                        WELCOME_MESH_CONFIRM_TIMEOUT_SECS
1062                    };
1063                updated.next_retry_at =
1064                    Some(Utc::now() + ChronoDuration::seconds(confirm_timeout_secs));
1065                updated.last_reason_code = None;
1066                updated.last_transport_error = None;
1067                self.welcome_lifecycles
1068                    .insert(peer_id.to_string(), updated.clone());
1069                self.persist_welcome_lifecycle_entry(&updated)?;
1070
1071                // Seed the confirmation-probe scheduler for this pending peer so
1072                // the SENDER actively reconciles instead of waiting passively.
1073                //
1074                // The Welcome is now in flight but unconfirmed. The sender's only
1075                // built-in convergence path would otherwise be the receiver's
1076                // single proactive encrypted confirm on first Welcome receipt — a
1077                // single point of failure with no retry (a lost fragment, a
1078                // not-yet-ready encryptor, or the retransmit/owner_keep path on
1079                // the receiver all strand it). By marking a probe due now,
1080                // `run_throttled_reconciliation` (whose `has_pending_work` gate is
1081                // otherwise false on the sender, since it has no pending encrypted
1082                // messages) starts emitting `SESSION_CONFIRM_PROBE`s. The peer —
1083                // which holds the session — replies with `SESSION_CONFIRM_ACK`,
1084                // confirming us and marking this Welcome `Sent`. The entry is
1085                // dropped once confirmed (`clear_confirmation_recovery_tracking`)
1086                // or once the peer is no longer pending (`kick`'s retain), so this
1087                // adds no steady-state work after convergence.
1088                self.confirmation_probe_due_at
1089                    .insert(peer_id.to_string(), Utc::now());
1090
1091                Ok(false)
1092            }
1093            Err(err) => {
1094                let no_carrier = Self::is_no_carrier_error(&err);
1095                let reason = Self::map_welcome_reason_code(&err);
1096                // Classified, not rendered: the transport layer interpolates
1097                // the peer into `PeerNotReachable`, and this value is both
1098                // persisted with the lifecycle record and emitted on
1099                // `WelcomeSendFailed.transport_error`, where the scrubber
1100                // hashes `peer_id` beside it. The full error is logged above.
1101                self.apply_welcome_send_failure(
1102                    peer_id,
1103                    reason,
1104                    Some(send_failure_token(&err)),
1105                    no_carrier,
1106                    source_event,
1107                )
1108            }
1109        }
1110    }
1111
1112    /// Re-arms a stalled or expired outbound Welcome when a peer becomes
1113    /// reachable again, so a session that never converged while the peer was
1114    /// offline gets a fresh delivery attempt over the now-available carrier.
1115    ///
1116    /// No-op when the peer's session is already confirmed, when there is no
1117    /// Welcome lifecycle, or when the Welcome is already `Sent` or still
1118    /// in-flight (`SendAttempted`). An `Expired` lifecycle is rebuilt from its
1119    /// retained Welcome message — the MLS group is not torn down on expiry, so
1120    /// the stored Welcome is still valid to re-send — while a `Created`/`Failed`
1121    /// lifecycle has its budget and TTL reset and is retried immediately.
1122    pub(super) fn rearm_welcome_for_peer(&mut self, peer_id: &str, source_event: &str) {
1123        if self.confirmed_sessions.contains(peer_id) {
1124            return;
1125        }
1126        let Some(record) = self.welcome_lifecycles.get(peer_id).cloned() else {
1127            return;
1128        };
1129        match record.state {
1130            WelcomeDeliveryState::Sent | WelcomeDeliveryState::SendAttempted => {}
1131            WelcomeDeliveryState::Created | WelcomeDeliveryState::Failed => {
1132                if let Some(entry) = self.welcome_lifecycles.get_mut(peer_id) {
1133                    entry.attempt = 0;
1134                    // A reachability edge resets the unreachable-park
1135                    // escalation: the next relay verdict starts the interval
1136                    // ladder over from its base.
1137                    entry.unreachable_parks = 0;
1138                    entry.next_retry_at = Some(Utc::now());
1139                    entry.last_reason_code = None;
1140                    entry.last_transport_error = None;
1141                    entry.expires_at =
1142                        Utc::now() + ChronoDuration::seconds(WELCOME_LIFECYCLE_TTL_SECS);
1143                }
1144                if let Err(err) = self.try_send_welcome(peer_id, source_event) {
1145                    warn!(
1146                        peer_id = %peer_id,
1147                        error = %err,
1148                        "Failed to re-arm welcome on peer reachability"
1149                    );
1150                }
1151            }
1152            WelcomeDeliveryState::Expired => {
1153                // Expired is terminal in the lifecycle state machine, so rebuild
1154                // from the retained Welcome message (upsert permits overwrite
1155                // from Expired) before re-sending.
1156                let welcome_message = record.welcome_message.clone();
1157                let group_id = record.group_id.clone();
1158                if let Err(err) =
1159                    self.upsert_welcome_lifecycle(peer_id, &group_id, welcome_message, source_event)
1160                {
1161                    warn!(
1162                        peer_id = %peer_id,
1163                        error = %err,
1164                        "Failed to rebuild expired welcome on peer reachability"
1165                    );
1166                    return;
1167                }
1168                if let Err(err) = self.try_send_welcome(peer_id, source_event) {
1169                    warn!(
1170                        peer_id = %peer_id,
1171                        error = %err,
1172                        "Failed to re-arm expired welcome on peer reachability"
1173                    );
1174                }
1175            }
1176        }
1177    }
1178
1179    /// Ingests an authoritative per-peer presence signal (the internet relay's
1180    /// `PresenceStatusWithLastSeen` answer, bridged by the platform layer).
1181    ///
1182    /// `online` drives the same reachability machinery as a transport-level
1183    /// discovery — outbox flush, welcome re-arm, auto key exchange — plus a
1184    /// rescue for the store-less-relay hole: a welcome whose wire send was
1185    /// confirmed (`Sent`) while the peer was actually offline was dropped by
1186    /// the relay (it forwards or pushes, never stores), and
1187    /// `Self::rearm_welcome_for_peer` deliberately never touches `Sent`.
1188    /// The peer being provably online is the safe moment to rebuild and
1189    /// re-send it: if the original did land, the receiver dedups by message
1190    /// id and the confirmation probe resolves the lifecycle.
1191    ///
1192    /// `offline` parks retry-pending welcomes pending a reachability edge,
1193    /// without burning budget. In-flight records (`SendAttempted`) are left
1194    /// to their confirm deadline — a `DeliveryError`-driven failure or the
1195    /// confirm timeout resolves them, after which the next offline tick parks.
1196    ///
1197    /// The welcome re-arm/re-send on the online edge is throttled per peer
1198    /// with exponential backoff (`WELCOME_PRESENCE_RESCUE_BASE_SECS` ..
1199    /// `WELCOME_PRESENCE_RESCUE_MAX_SECS`): presence answers arrive on the
1200    /// platform's ~20s watch tick, and a peer that is online but can never
1201    /// prove the session (stale key package after a reinstall, incompatible
1202    /// version) must not be re-sent the multi-frame MLS welcome on every
1203    /// tick forever. A throttled online tick still flushes queued data-plane
1204    /// traffic for the peer.
1205    ///
1206    /// Emits `presence_updated` with `source: Internet` — this function is
1207    /// the relay-sourced half of the unified stream and must only be fed
1208    /// relay-observed signals (bridged via `internet_peer_presence`);
1209    /// peer-sent `__PRESENCE__` self-reports are handled in
1210    /// `message_dispatch` and emit `source: Peer`. Self, blocked, or empty
1211    /// peer ids are dropped entirely: an app waiting on `presence_updated`
1212    /// for a blocked peer will never see it.
1213    pub fn on_peer_presence(&mut self, peer_id: &str, online: bool, last_seen_ms: Option<i64>) {
1214        if peer_id.is_empty() || peer_id == self.local_id || self.is_user_blocked(peer_id) {
1215            return;
1216        }
1217        // A presence answer is a claim about this recipient on the carrier
1218        // that answered, so it is recorded as one. It supersedes an earlier
1219        // verdict for that carrier: both answer the same question and this is
1220        // the later answer. Presence facts decay faster than verdicts, since
1221        // this is a third party's report about someone else's connection.
1222        self.reachability.record(
1223            peer_id,
1224            TransportType::Internet,
1225            if online {
1226                Claim::Reachable
1227            } else {
1228                Claim::Unreachable
1229            },
1230            FactSource::GatewayPresence,
1231            std::time::Instant::now(),
1232        );
1233        if online {
1234            // Relay-scoped reachability proof: un-park and re-drive the
1235            // peer's DMs over the internet transport before anything else
1236            // (see `flush_outbox_for_peer_via` — this also cancels
1237            // unanswerable mesh-probe ACKs that would otherwise hold the
1238            // messages hostage past this edge). The rescue branch's inner
1239            // flush then finds successfully re-driven entries awaiting
1240            // fresh ACKs and leaves them alone; entries whose forced send
1241            // *failed* are picked up again, so the inner flush keeps the
1242            // internet override — DORS must not re-route them into the
1243            // mesh void with the park counter already cleared.
1244            self.flush_outbox_for_peer_via(peer_id, Some(TransportType::Internet));
1245            if self.welcome_rescue_permitted(peer_id) {
1246                self.on_neighbor_discovered_via(peer_id, Some(TransportType::Internet));
1247                self.resend_unconfirmed_sent_welcome(peer_id, "peer_presence_online");
1248                self.note_welcome_rescue_attempt(peer_id);
1249            }
1250        } else {
1251            self.park_welcome_peer_unreachable(peer_id);
1252        }
1253
1254        let status = if online {
1255            crate::events::PresenceStatus::Online
1256        } else {
1257            crate::events::PresenceStatus::Offline
1258        };
1259        self.emit_event(Event::presence_updated_with_last_seen(
1260            peer_id.to_string(),
1261            status,
1262            Utc::now().timestamp_millis(),
1263            last_seen_ms,
1264        ));
1265    }
1266
1267    /// Peers with a welcome lifecycle whose session has not been proven —
1268    /// the internet presence watch set. Includes `Sent` records (wire-
1269    /// confirmed but never session-confirmed): only a presence signal can
1270    /// rescue those over a store-less relay.
1271    /// Lifecycles older than `WELCOME_WATCHLIST_MAX_AGE_SECS` are excluded:
1272    /// a permanently-dead peer must not occupy watch-rotation slots (and keep
1273    /// its parked record alive via `expires_at` pushes) forever.
1274    pub fn welcome_pending_peers(&self) -> Vec<String> {
1275        let watch_cutoff = Utc::now() - ChronoDuration::seconds(WELCOME_WATCHLIST_MAX_AGE_SECS);
1276        self.welcome_lifecycles
1277            .iter()
1278            .filter(|(peer_id, record)| {
1279                !self.confirmed_sessions.contains(*peer_id) && record.created_at > watch_cutoff
1280            })
1281            .map(|(peer_id, _)| peer_id.clone())
1282            .collect()
1283    }
1284
1285    /// Rebuilds and re-sends a `Sent`-but-session-unconfirmed welcome (see
1286    /// [`Self::on_peer_presence`]). No-op for any other state.
1287    fn resend_unconfirmed_sent_welcome(&mut self, peer_id: &str, source_event: &str) {
1288        if self.confirmed_sessions.contains(peer_id) {
1289            return;
1290        }
1291        let Some(record) = self.welcome_lifecycles.get(peer_id) else {
1292            return;
1293        };
1294        if !matches!(record.state, WelcomeDeliveryState::Sent) {
1295            return;
1296        }
1297        let welcome_message = record.welcome_message.clone();
1298        let group_id = record.group_id.clone();
1299        if let Err(err) =
1300            self.upsert_welcome_lifecycle(peer_id, &group_id, welcome_message, source_event)
1301        {
1302            warn!(
1303                peer_id = %peer_id,
1304                error = %err,
1305                "Failed to rebuild unconfirmed sent welcome on presence"
1306            );
1307            return;
1308        }
1309        if let Err(err) = self.try_send_welcome(peer_id, source_event) {
1310            warn!(
1311                peer_id = %peer_id,
1312                error = %err,
1313                "Failed to re-send unconfirmed welcome on presence"
1314            );
1315        }
1316    }
1317
1318    /// True when a presence-online rescue for this peer is currently allowed
1319    /// by the per-peer backoff. Trivially true (and the throttle entry is
1320    /// dropped) once the peer has no unconfirmed welcome — there is nothing
1321    /// left to throttle.
1322    fn welcome_rescue_permitted(&mut self, peer_id: &str) -> bool {
1323        let pending = self.welcome_lifecycles.contains_key(peer_id)
1324            && !self.confirmed_sessions.contains(peer_id);
1325        if !pending {
1326            self.welcome_presence_rescue.remove(peer_id);
1327            return true;
1328        }
1329        self.welcome_presence_rescue
1330            .get(peer_id)
1331            .is_none_or(|throttle| throttle.next_allowed_at <= Utc::now())
1332    }
1333
1334    /// Records that a presence-online tick ran the rescue actions for a peer
1335    /// that still has an unconfirmed welcome, doubling the wait before the
1336    /// next one (base 40s, capped at 10 minutes). The counter is per
1337    /// convergence attempt: the entry is dropped (via
1338    /// [`Self::welcome_rescue_permitted`]) as soon as the session confirms
1339    /// or the lifecycle goes away.
1340    fn note_welcome_rescue_attempt(&mut self, peer_id: &str) {
1341        let pending = self.welcome_lifecycles.contains_key(peer_id)
1342            && !self.confirmed_sessions.contains(peer_id);
1343        if !pending {
1344            self.welcome_presence_rescue.remove(peer_id);
1345            return;
1346        }
1347        let throttle = self
1348            .welcome_presence_rescue
1349            .entry(peer_id.to_string())
1350            .or_insert(PresenceRescueThrottle {
1351                next_allowed_at: Utc::now(),
1352                rescues: 0,
1353            });
1354        // Shift is clamped to 8, so 40 << 8 = 10240 max before the cap —
1355        // no overflow risk.
1356        let backoff_secs = (WELCOME_PRESENCE_RESCUE_BASE_SECS << throttle.rescues.min(8))
1357            .min(WELCOME_PRESENCE_RESCUE_MAX_SECS);
1358        throttle.next_allowed_at = Utc::now() + ChronoDuration::seconds(backoff_secs);
1359        throttle.rescues = throttle.rescues.saturating_add(1);
1360    }
1361
1362    /// Parks a welcome pending a peer-reachability edge: reason
1363    /// `PeerUnreachable`, **no timed retry**. The carrier (relay socket) is
1364    /// healthy — only this peer is unreachable on it — so the *data-plane*
1365    /// retry this cancels would re-send the welcome into another
1366    /// `DeliveryError` while burning the real-delivery budget that a
1367    /// carrier-backed failure already charged. Recovery is edge-driven
1368    /// instead: `on_peer_presence(online)` and `on_neighbor_discovered` re-arm
1369    /// via `rearm_welcome_for_peer`, and the peer stays on the presence
1370    /// watchlist (`welcome_pending_peers`) so the platform keeps polling for
1371    /// that edge. The TTL is pushed like a no-carrier park: an unreachable
1372    /// peer must not age the welcome out.
1373    ///
1374    /// Its caller is responsible for not applying this to a record already
1375    /// holding an escalating unreachable probe — see
1376    /// [`Self::park_welcome_peer_unreachable`], which is what distinguishes
1377    /// the budget-burning data-plane retry cancelled here from the
1378    /// budget-refunded probe that must survive.
1379    fn park_welcome_awaiting_peer(&mut self, peer_id: &str) -> Result<()> {
1380        let snapshot = {
1381            let Some(record) = self.welcome_lifecycles.get_mut(peer_id) else {
1382                return Ok(());
1383            };
1384            record.next_retry_at = None;
1385            record.expires_at = Utc::now() + ChronoDuration::seconds(WELCOME_LIFECYCLE_TTL_SECS);
1386            record.last_reason_code = Some(crate::events::WelcomeReasonCode::PeerUnreachable);
1387            record.clone()
1388        };
1389        self.persist_welcome_lifecycle_entry(&snapshot)?;
1390        debug!(
1391            peer_id = %peer_id,
1392            state = snapshot.state.as_str(),
1393            "Welcome parked: peer unreachable on carrier, awaiting reachability edge"
1394        );
1395        Ok(())
1396    }
1397
1398    /// Parks a retry-pending welcome for a peer the relay reports offline.
1399    /// Only `Created`/`Failed` records park: `SendAttempted` keeps its
1400    /// confirm deadline, and `Sent`/`Expired` are handled on the online edge
1401    /// (or corrected by an attributed `DeliveryError`). Skipped entirely when
1402    /// a mesh carrier is also available — relay presence is only
1403    /// authoritative for the internet path, and the data-plane retry track
1404    /// may still deliver over BLE / WiFi-Direct.
1405    ///
1406    /// Never cancels an escalating unreachable probe: a record that already
1407    /// holds one was parked by [`Self::apply_recipient_unreachable_failure`],
1408    /// which already set the same reason code and pushed the same TTL — so this
1409    /// adds nothing and would only downgrade the probe to edge-only. That
1410    /// matters because on an internet-only device this runs once per presence
1411    /// rotation for as long as the peer is down, which would otherwise
1412    /// re-strand the welcome after every single verdict. The distinction is
1413    /// budget: the data-plane retry this *does* cancel was charged an attempt
1414    /// by a carrier-backed failure, while the probe refunds its attempt on
1415    /// every verdict.
1416    ///
1417    /// `last_reason_code` — not `unreachable_parks` alone — is what encodes
1418    /// that distinction. The counter is sticky (only `rearm_welcome_for_peer`
1419    /// clears it), so gating on it by itself would keep shielding the record
1420    /// after the probe track has stopped protecting it: past
1421    /// [`WELCOME_WATCHLIST_MAX_AGE_SECS`] a confirm timeout no longer re-parks
1422    /// (see [`Self::welcome_probe_repark_permitted`]) and instead charges an
1423    /// attempt and arms a plain data-plane retry — precisely the budget-burner
1424    /// this park exists to cancel, and left running it expires the welcome at
1425    /// `max_retries`. Note `park_welcome_awaiting_peer` also stamps
1426    /// `PeerUnreachable` but leaves `next_retry_at = None`, so the
1427    /// `next_retry_at.is_some()` conjunct below is load-bearing, not
1428    /// redundant: the reason code *and* a scheduled retry together are what
1429    /// identify a live probe. A carrier-backed failure stamps
1430    /// `Timeout`/`SendFailed` and a successful send clears the code entirely.
1431    fn park_welcome_peer_unreachable(&mut self, peer_id: &str) {
1432        if self.confirmed_sessions.contains(peer_id) {
1433            return;
1434        }
1435        let Some(record) = self.welcome_lifecycles.get(peer_id) else {
1436            return;
1437        };
1438        if !matches!(
1439            record.state,
1440            WelcomeDeliveryState::Created | WelcomeDeliveryState::Failed
1441        ) {
1442            return;
1443        }
1444        if record.last_reason_code == Some(crate::events::WelcomeReasonCode::PeerUnreachable)
1445            && record.unreachable_parks > 0
1446            && record.next_retry_at.is_some()
1447        {
1448            return;
1449        }
1450        // Local mesh carriers only (BLE / WiFi-Direct): Nostr and Reticulum
1451        // are internet-dependent, so their availability must not veto the
1452        // park — mirrors the carrier guard in
1453        // `apply_recipient_unreachable_failure`.
1454        if self
1455            .transport_manager
1456            .get_available_transports()
1457            .keys()
1458            .any(|transport| matches!(transport, TransportType::BLE | TransportType::WiFiDirect))
1459        {
1460            return;
1461        }
1462        let _ = self.park_welcome_awaiting_peer(peer_id);
1463    }
1464
1465    /// True when an unconfirmed welcome send should resolve as another
1466    /// unreachable verdict ([`Self::apply_recipient_unreachable_failure`])
1467    /// rather than as a carrier-backed failure that ages the record.
1468    ///
1469    /// A live `unreachable_parks` counter means the relay declared the peer
1470    /// offline and no reachability edge has fired since (every edge clears it
1471    /// via `rearm_welcome_for_peer`), so the send that just went unconfirmed
1472    /// was the escalating probe, not a delivery attempt. Without this the
1473    /// probe destroys the very welcome it exists to recover: the relay
1474    /// accepts the frame whenever its push fallback succeeds, which returns
1475    /// *no* `DeliveryError` at all, so the probe resolves at the 10s confirm
1476    /// timeout instead. Treated as carrier-backed that charges the attempt
1477    /// and arms a plain data-plane retry, and since nothing in that ladder
1478    /// pushes `expires_at`, the record walks into `should_expire` within one
1479    /// TTL window — terminal `welcome_send_expired` + `secure_session_failed`
1480    /// for a peer that is merely offline. Re-parking instead refunds the
1481    /// attempt, escalates the interval and pushes the TTL, mirroring the DM
1482    /// path's [`Self::try_repark_exhausted_dm`]: settlement stays reserved
1483    /// for delivery or the record genuinely ageing out below.
1484    ///
1485    /// Only the *presence-offline* answer cancelled that retry before, which
1486    /// is not a defense at all for the case this whole probe exists to serve
1487    /// — a headless consumer that never polls presence.
1488    ///
1489    /// Bounded by [`WELCOME_WATCHLIST_MAX_AGE_SECS`] measured from
1490    /// `created_at`, the same threshold that stops watching a peer as
1491    /// permanently dead: past it the counter no longer shields the record and
1492    /// normal ageing expires it. This is the welcome's twin of the DM probe's
1493    /// absolute bound (`first_sent_at` vs the outbox absolute lifetime) —
1494    /// without it a peer that never returns keeps a welcome probing at the
1495    /// 600s cap forever.
1496    ///
1497    /// Deliberately narrow to `Timeout`: a `SendFailed`/`TransportUnavailable`
1498    /// failure is evidence about the *carrier*, not about this peer, and
1499    /// keeps ageing the record normally.
1500    fn welcome_probe_repark_permitted(&self, peer_id: &str) -> bool {
1501        let Some(record) = self.welcome_lifecycles.get(peer_id) else {
1502            return false;
1503        };
1504        record.unreachable_parks > 0
1505            && record.created_at
1506                > Utc::now() - ChronoDuration::seconds(WELCOME_WATCHLIST_MAX_AGE_SECS)
1507    }
1508
1509    /// Applies the relay's authoritative "recipient offline" verdict
1510    /// (a `recipient_unreachable`-tagged transport failure) to a welcome
1511    /// lifecycle. Also reached *without* a relay verdict when a probe's
1512    /// confirm timeout resolves while the park counter is still live — the
1513    /// same conclusion inferred rather than relayed, see
1514    /// [`Self::welcome_probe_repark_permitted`].
1515    ///
1516    /// The bridge wire-confirms on socket-write success, so by the time the
1517    /// relay's `DeliveryError` arrives the record is normally already `Sent`
1518    /// — a *false* Sent: the store-less relay dropped the content. Correct
1519    /// it back to `Failed` (emitting `welcome_send_failed` so the app's
1520    /// earlier `welcome_send_succeeded` is superseded), refund the attempt
1521    /// (the peer never saw the frame), and park pending a reachability edge.
1522    /// `SendAttempted` (the failure raced ahead of the wire confirm) gets
1523    /// the same treatment; `Created`/`Failed` records just park. Terminal
1524    /// `Expired` records are left to the online edge, which rebuilds them.
1525    ///
1526    /// The record always keeps a timed retry whose interval escalates per
1527    /// consecutive park. Relay presence is authoritative only for the
1528    /// internet path — with a mesh carrier live the peer may still be
1529    /// reachable over BLE / WiFi-Direct (DORS can pick internet even with
1530    /// mesh present) — but an internet-only device needs the timed track just
1531    /// as much: parking it edge-only left the welcome dependent solely on a
1532    /// presence-online answer, i.e. on the platform's presence-polling
1533    /// cadence, which is the same dead end that stalled parked DMs (see
1534    /// [`Self::park_unreachable_dm`]). The escalation (15s → 600s cap) is
1535    /// what bounds the retry: DORS may keep routing to the internet path,
1536    /// where each round trips another budget-refunded `DeliveryError`, and
1537    /// without escalation that would be an unbounded resend loop into the
1538    /// relay. Every verdict refunds the attempt and pushes the TTL — past the
1539    /// scheduled probe, see the invariant on [`Self::park_welcome_no_carrier`]
1540    /// — so the probes never age the welcome out or burn its real-delivery
1541    /// budget, and the counter resets on any reachability edge
1542    /// (`rearm_welcome_for_peer`).
1543    pub(super) fn apply_recipient_unreachable_failure(
1544        &mut self,
1545        peer_id: &str,
1546        transport_error: Option<&'static str>,
1547    ) -> Result<()> {
1548        // A late relay verdict for a session that has since been proven (the
1549        // welcome was rescued over another path and the peer confirmed) must
1550        // not corrupt the converged lifecycle: without this guard the stale
1551        // DeliveryError flips the record back to Failed, refunds an attempt,
1552        // persists the stale state, and emits welcome_send_failed AFTER the
1553        // app already saw secure_session_established.
1554        if self.confirmed_sessions.contains(peer_id) {
1555            return Ok(());
1556        }
1557        let Some(state) = self.welcome_lifecycles.get(peer_id).map(|r| r.state) else {
1558            return Ok(());
1559        };
1560        match state {
1561            WelcomeDeliveryState::Sent | WelcomeDeliveryState::SendAttempted => {
1562                self.transition_welcome_state(
1563                    peer_id,
1564                    WelcomeDeliveryState::Failed,
1565                    "recipient_unreachable",
1566                )?;
1567                if let Some(record) = self.welcome_lifecycles.get_mut(peer_id) {
1568                    record.attempt = record.attempt.saturating_sub(1);
1569                    record.last_transport_error = transport_error.map(str::to_string);
1570                }
1571            }
1572            WelcomeDeliveryState::Created | WelcomeDeliveryState::Failed => {
1573                if let Some(record) = self.welcome_lifecycles.get_mut(peer_id) {
1574                    record.last_transport_error = transport_error.map(str::to_string);
1575                }
1576            }
1577            WelcomeDeliveryState::Expired => return Ok(()),
1578        }
1579        // Escalating timed retry on every carrier (see the doc comment): the
1580        // ladder, not a carrier guard, is what bounds resends into the relay.
1581        let parks = {
1582            let Some(record) = self.welcome_lifecycles.get_mut(peer_id) else {
1583                return Ok(());
1584            };
1585            record.unreachable_parks = record.unreachable_parks.saturating_add(1);
1586            record.unreachable_parks
1587        };
1588        // parks >= 1 here; shift clamped so 15 << 6 = 960 is the largest
1589        // pre-cap value — no overflow risk.
1590        let retry_in_secs = (WELCOME_NO_CARRIER_RETRY_SECS << (parks - 1).min(6))
1591            .min(WELCOME_UNREACHABLE_RETRY_CAP_SECS);
1592        self.park_welcome_no_carrier(
1593            peer_id,
1594            crate::events::WelcomeReasonCode::PeerUnreachable,
1595            transport_error,
1596            retry_in_secs,
1597        )?;
1598
1599        let Some(snapshot) = self.welcome_lifecycles.get(peer_id).cloned() else {
1600            return Ok(());
1601        };
1602        if let Ok(shared) = lock_shared_state(&self.shared_state) {
1603            shared.emit_event(Event::welcome_send_failed(
1604                peer_id.to_string(),
1605                snapshot.welcome_message.id.as_str().to_string(),
1606                snapshot.group_id.clone(),
1607                snapshot.attempt,
1608                crate::events::WelcomeReasonCode::PeerUnreachable,
1609                snapshot
1610                    .last_transport_error
1611                    .as_deref()
1612                    .map(classify_transport_send_error),
1613                // Retryable, and now always with a scheduled time: the park
1614                // keeps an escalating timed probe on every carrier. The
1615                // reachability edges (presence online / peer discovery) still
1616                // re-arm it sooner when they fire.
1617                true,
1618                snapshot.next_retry_at.map(|at| at.timestamp_millis()),
1619            ));
1620        }
1621        Ok(())
1622    }
1623
1624    /// Records a Welcome send failure and decides whether to retry or expire.
1625    ///
1626    /// `no_carrier` is `true` when the failure was simply that no transport
1627    /// carrier exists yet (vs. a present carrier that failed mid-send). A
1628    /// no-carrier failure must NOT age the Welcome: it neither counts toward
1629    /// the retry budget nor lets the TTL expire it, because the peer is merely
1630    /// unreachable for now and will be retried once a carrier appears. The
1631    /// speculative attempt increment from [`Self::try_send_welcome`] is rolled
1632    /// back so a long offline period leaves the real-delivery budget intact.
1633    pub(super) fn apply_welcome_send_failure(
1634        &mut self,
1635        peer_id: &str,
1636        reason: crate::events::WelcomeReasonCode,
1637        transport_error: Option<&'static str>,
1638        no_carrier: bool,
1639        source_event: &str,
1640    ) -> Result<bool> {
1641        let mut updated = self
1642            .welcome_lifecycles
1643            .get(peer_id)
1644            .cloned()
1645            .ok_or_else(|| Error::Other(format!("Missing welcome lifecycle for {}", peer_id)))?;
1646
1647        if matches!(
1648            updated.state,
1649            WelcomeDeliveryState::Sent | WelcomeDeliveryState::Expired
1650        ) {
1651            return Ok(matches!(updated.state, WelcomeDeliveryState::Sent));
1652        }
1653
1654        // A confirm timeout on a welcome that still holds a live
1655        // unreachable-park counter is a *probe* resolving, not a delivery
1656        // failing — re-park it as another unreachable verdict instead of
1657        // ageing it (see [`Self::welcome_probe_repark_permitted`]).
1658        if !no_carrier
1659            && matches!(reason, crate::events::WelcomeReasonCode::Timeout)
1660            && self.welcome_probe_repark_permitted(peer_id)
1661        {
1662            self.apply_recipient_unreachable_failure(peer_id, transport_error)?;
1663            return Ok(false);
1664        }
1665
1666        let max_attempts = self.config.reliability.retry.max_retries.max(1);
1667        // A no-carrier failure never expires the Welcome: neither the retry
1668        // budget nor the TTL applies while the peer is simply unreachable. Only
1669        // a carrier-backed failure (SendFailed / Timeout) ages it toward expiry.
1670        let should_expire =
1671            !no_carrier && (updated.attempt >= max_attempts || updated.expires_at <= Utc::now());
1672        if should_expire {
1673            let terminal_reason = crate::events::WelcomeReasonCode::RetryExhausted;
1674            {
1675                let record = self.welcome_lifecycles.get_mut(peer_id).ok_or_else(|| {
1676                    Error::Other(format!("Missing welcome lifecycle for {}", peer_id))
1677                })?;
1678                record.last_reason_code = Some(terminal_reason);
1679                record.last_transport_error = transport_error.map(str::to_string);
1680                record.next_retry_at = None;
1681            }
1682
1683            if !matches!(updated.state, WelcomeDeliveryState::Failed) {
1684                self.transition_welcome_state(peer_id, WelcomeDeliveryState::Failed, source_event)?;
1685            }
1686            self.transition_welcome_state(
1687                peer_id,
1688                WelcomeDeliveryState::Expired,
1689                "welcome_retry_exhausted",
1690            )?;
1691
1692            let expired_snapshot =
1693                self.welcome_lifecycles
1694                    .get(peer_id)
1695                    .cloned()
1696                    .ok_or_else(|| {
1697                        Error::Other(format!("Missing welcome lifecycle for {}", peer_id))
1698                    })?;
1699            if let Ok(state) = lock_shared_state(&self.shared_state) {
1700                state.emit_event(Event::welcome_send_failed(
1701                    peer_id.to_string(),
1702                    expired_snapshot.welcome_message.id.as_str().to_string(),
1703                    expired_snapshot.group_id.clone(),
1704                    expired_snapshot.attempt,
1705                    terminal_reason,
1706                    expired_snapshot
1707                        .last_transport_error
1708                        .as_deref()
1709                        .map(classify_transport_send_error),
1710                    false,
1711                    None,
1712                ));
1713                state.emit_event(Event::welcome_send_expired(
1714                    peer_id.to_string(),
1715                    expired_snapshot.welcome_message.id.as_str().to_string(),
1716                    expired_snapshot.attempt,
1717                    terminal_reason,
1718                ));
1719            }
1720            self.abort_pending_session_for_peer(peer_id, terminal_reason);
1721            return Ok(false);
1722        }
1723
1724        // A no-carrier failure (the carrier raced away after the pre-send check,
1725        // or an async transport-failed callback with no carrier) re-checks on the
1726        // slow no-carrier interval rather than the data-plane backoff: there is
1727        // nothing to deliver over until a carrier returns, and `try_send_welcome`
1728        // parks subsequent no-carrier ticks cheaply anyway.
1729        let retry_at = if no_carrier {
1730            Utc::now() + ChronoDuration::seconds(WELCOME_NO_CARRIER_RETRY_SECS)
1731        } else {
1732            let delay_ms = self.compute_welcome_retry_delay_ms(peer_id, updated.attempt);
1733            Utc::now() + ChronoDuration::milliseconds(delay_ms as i64)
1734        };
1735
1736        {
1737            let record = self.welcome_lifecycles.get_mut(peer_id).ok_or_else(|| {
1738                Error::Other(format!("Missing welcome lifecycle for {}", peer_id))
1739            })?;
1740            // Roll back the speculative attempt increment from try_send_welcome
1741            // for a no-carrier failure so the retry budget stays reserved for
1742            // real delivery attempts over an available carrier, and push the TTL
1743            // forward so a never-deliverable Welcome does not age out — the TTL
1744            // clock is carrier-relative, not creation-relative.
1745            if no_carrier {
1746                record.attempt = record.attempt.saturating_sub(1);
1747                record.expires_at =
1748                    Utc::now() + ChronoDuration::seconds(WELCOME_LIFECYCLE_TTL_SECS);
1749            }
1750            record.last_reason_code = Some(reason);
1751            record.last_transport_error = transport_error.map(str::to_string);
1752            record.next_retry_at = Some(retry_at);
1753        }
1754
1755        if !matches!(updated.state, WelcomeDeliveryState::Failed) {
1756            self.transition_welcome_state(peer_id, WelcomeDeliveryState::Failed, source_event)?;
1757        } else if let Some(record) = self.welcome_lifecycles.get(peer_id).cloned() {
1758            self.persist_welcome_lifecycle_entry(&record)?;
1759        }
1760
1761        updated = self
1762            .welcome_lifecycles
1763            .get(peer_id)
1764            .cloned()
1765            .ok_or_else(|| Error::Other(format!("Missing welcome lifecycle for {}", peer_id)))?;
1766        if let Ok(state) = lock_shared_state(&self.shared_state) {
1767            state.emit_event(Event::welcome_send_failed(
1768                peer_id.to_string(),
1769                updated.welcome_message.id.as_str().to_string(),
1770                updated.group_id,
1771                updated.attempt,
1772                reason,
1773                updated
1774                    .last_transport_error
1775                    .as_deref()
1776                    .map(classify_transport_send_error),
1777                true,
1778                Some(retry_at.timestamp_millis()),
1779            ));
1780        }
1781        Ok(false)
1782    }
1783
1784    pub(super) fn compute_welcome_retry_delay_ms(&self, peer_id: &str, attempt: u32) -> u64 {
1785        let config = &self.config.reliability.retry;
1786        let capped_attempt = attempt.saturating_sub(1);
1787        let base_ms = if capped_attempt == 0 {
1788            config.initial_delay_ms
1789        } else {
1790            let multiplier = config.backoff_multiplier.powi(capped_attempt as i32);
1791            (config.initial_delay_ms as f64 * multiplier as f64) as u64
1792        }
1793        .min(config.max_delay_ms);
1794
1795        let mut hasher = std::collections::hash_map::DefaultHasher::new();
1796        peer_id.hash(&mut hasher);
1797        attempt.hash(&mut hasher);
1798        Utc::now().timestamp_millis().hash(&mut hasher);
1799        let bucket = (hasher.finish() % 10_000) as f64 / 10_000.0;
1800        let jitter_factor = 1.0 + ((bucket * 2.0 - 1.0) * WELCOME_RETRY_JITTER_RATIO);
1801        let jittered = (base_ms as f64 * jitter_factor).round() as i64;
1802        jittered.max(1) as u64
1803    }
1804
1805    pub(super) fn can_transition_welcome_state(
1806        current: WelcomeDeliveryState,
1807        next: WelcomeDeliveryState,
1808    ) -> bool {
1809        matches!(
1810            (current, next),
1811            (
1812                WelcomeDeliveryState::Created,
1813                WelcomeDeliveryState::SendAttempted
1814            ) | (WelcomeDeliveryState::Created, WelcomeDeliveryState::Expired)
1815                | (
1816                    WelcomeDeliveryState::SendAttempted,
1817                    WelcomeDeliveryState::Sent
1818                )
1819                | (
1820                    WelcomeDeliveryState::SendAttempted,
1821                    WelcomeDeliveryState::Failed
1822                )
1823                | (
1824                    WelcomeDeliveryState::Failed,
1825                    WelcomeDeliveryState::SendAttempted
1826                )
1827                | (WelcomeDeliveryState::Failed, WelcomeDeliveryState::Sent)
1828                | (WelcomeDeliveryState::Failed, WelcomeDeliveryState::Expired)
1829                // Sent is normally a sink, but the relay's DeliveryError is
1830                // authoritative proof that a wire-confirmed frame was dropped
1831                // (store-less relay, recipient offline). That correction is
1832                // the one legal way back out of Sent — see
1833                // `on_transport_send_failed`.
1834                | (WelcomeDeliveryState::Sent, WelcomeDeliveryState::Failed)
1835        )
1836    }
1837
1838    pub(super) fn transition_welcome_state(
1839        &mut self,
1840        peer_id: &str,
1841        next_state: WelcomeDeliveryState,
1842        source_event: &str,
1843    ) -> Result<()> {
1844        let (previous_state, record_snapshot) = {
1845            let record = self.welcome_lifecycles.get_mut(peer_id).ok_or_else(|| {
1846                Error::Other(format!(
1847                    "Missing welcome lifecycle for transition: {}",
1848                    peer_id
1849                ))
1850            })?;
1851
1852            if record.state == next_state {
1853                return Ok(());
1854            }
1855
1856            if !Self::can_transition_welcome_state(record.state, next_state) {
1857                return Err(Error::Other(format!(
1858                    "Illegal welcome lifecycle transition for {}: {} -> {}",
1859                    peer_id,
1860                    record.state.as_str(),
1861                    next_state.as_str()
1862                )));
1863            }
1864
1865            let previous = record.state;
1866            record.state = next_state;
1867            if matches!(
1868                next_state,
1869                WelcomeDeliveryState::Sent | WelcomeDeliveryState::Expired
1870            ) {
1871                record.next_retry_at = None;
1872            }
1873            (previous, record.clone())
1874        };
1875
1876        self.persist_welcome_lifecycle_entry(&record_snapshot)?;
1877        info!(
1878            event = "welcome_lifecycle_transition",
1879            session_or_group_id = %peer_id,
1880            previous_state = previous_state.as_str(),
1881            new_state = next_state.as_str(),
1882            source_event = %source_event,
1883            attempt = record_snapshot.attempt,
1884            "welcome_lifecycle_transition"
1885        );
1886        Ok(())
1887    }
1888
1889    pub(super) fn upsert_welcome_lifecycle(
1890        &mut self,
1891        peer_id: &str,
1892        group_id: &str,
1893        welcome_message: Message,
1894        source_event: &str,
1895    ) -> Result<()> {
1896        if let Some(existing) = self.welcome_lifecycles.get(peer_id) {
1897            if !matches!(
1898                existing.state,
1899                WelcomeDeliveryState::Sent | WelcomeDeliveryState::Expired
1900            ) {
1901                return Err(Error::Other(format!(
1902                    "Refusing to overwrite active welcome lifecycle for {} in state {}",
1903                    peer_id,
1904                    existing.state.as_str()
1905                )));
1906            }
1907        }
1908
1909        let now = Utc::now();
1910        let record = WelcomeLifecycleRecord {
1911            peer_id: peer_id.to_string(),
1912            group_id: group_id.to_string(),
1913            state: WelcomeDeliveryState::Created,
1914            attempt: 0,
1915            unreachable_parks: 0,
1916            welcome_message,
1917            next_retry_at: None,
1918            last_reason_code: None,
1919            last_transport_error: None,
1920            created_at: now,
1921            expires_at: now + ChronoDuration::seconds(WELCOME_LIFECYCLE_TTL_SECS),
1922        };
1923        self.welcome_lifecycles
1924            .insert(peer_id.to_string(), record.clone());
1925        self.persist_welcome_lifecycle_entry(&record)?;
1926        info!(
1927            event = "welcome_lifecycle_transition",
1928            session_or_group_id = %peer_id,
1929            previous_state = "Absent",
1930            new_state = WelcomeDeliveryState::Created.as_str(),
1931            source_event = %source_event,
1932            attempt = 0,
1933            "welcome_lifecycle_transition"
1934        );
1935        Ok(())
1936    }
1937
1938    pub(super) fn map_welcome_reason_code(error: &Error) -> crate::events::WelcomeReasonCode {
1939        SessionStateError::classify(error).to_welcome_reason_code()
1940    }
1941
1942    /// True when a send failed because no transport carrier exists yet, as
1943    /// opposed to a present-but-unhealthy carrier (`SendFailed`) or a
1944    /// sent-but-unconfirmed timeout. A no-carrier failure means the peer is
1945    /// simply unreachable right now, so the Welcome must be kept alive and
1946    /// retried rather than counted against its budget — see
1947    /// [`Self::apply_welcome_send_failure`].
1948    pub(super) fn is_no_carrier_error(error: &Error) -> bool {
1949        matches!(
1950            error,
1951            Error::Transport(offline_protocol_transport::Error::TransportNotAvailable(_))
1952        )
1953    }
1954
1955    pub(super) fn session_ready_context_for_source(source_event: &str) -> MlsOperationContext {
1956        match source_event {
1957            "confirmation_ack_received" | "confirmation_probe_received" | "decrypt_success" => {
1958                MlsOperationContext::Receive
1959            }
1960            "welcome_received" => MlsOperationContext::Welcome,
1961            _ => MlsOperationContext::Send,
1962        }
1963    }
1964
1965    pub(super) fn find_welcome_peer_by_message_id(&self, message_id: &str) -> Option<String> {
1966        self.welcome_lifecycles
1967            .iter()
1968            .find_map(|(peer_id, record)| {
1969                if record.welcome_message.id.as_str() == message_id {
1970                    return Some(peer_id.clone());
1971                }
1972                None
1973            })
1974    }
1975
1976    pub(super) fn process_welcome_retry_queue(&mut self) -> Result<()> {
1977        let now = Utc::now();
1978        let timed_out_attempts: Vec<String> = self
1979            .welcome_lifecycles
1980            .iter()
1981            .filter_map(|(peer_id, record)| {
1982                // Skip peers whose session is already confirmed: the welcome is
1983                // delivered, mark_welcome_confirmed should have marked it Sent,
1984                // and re-sending would be wasted bandwidth (defensive against any
1985                // keying mismatch that left the lifecycle non-terminal).
1986                if self.confirmed_sessions.contains(peer_id) {
1987                    return None;
1988                }
1989                if matches!(record.state, WelcomeDeliveryState::SendAttempted)
1990                    && record.next_retry_at.is_some_and(|retry_at| retry_at <= now)
1991                {
1992                    return Some(peer_id.clone());
1993                }
1994                None
1995            })
1996            .take(WELCOME_RETRY_BATCH_SIZE)
1997            .collect();
1998
1999        for peer_id in timed_out_attempts {
2000            let _ = self.apply_welcome_send_failure(
2001                &peer_id,
2002                crate::events::WelcomeReasonCode::Timeout,
2003                Some(SEND_FAIL_REASON_CONFIRM_TIMEOUT),
2004                // A confirm timeout means the Welcome was sent over a carrier;
2005                // this is not a no-carrier failure, so it ages normally.
2006                false,
2007                "welcome_confirm_timeout",
2008            )?;
2009        }
2010
2011        let due_peers: Vec<String> = self
2012            .welcome_lifecycles
2013            .iter()
2014            .filter_map(|(peer_id, record)| {
2015                if self.confirmed_sessions.contains(peer_id) {
2016                    return None;
2017                }
2018                // Failed = a normal retry-due Welcome. Created with a due
2019                // next_retry_at = a no-carrier-parked Welcome (see
2020                // `park_welcome_no_carrier`): a fresh Created entry has
2021                // next_retry_at == None, so only parked ones match here.
2022                if matches!(
2023                    record.state,
2024                    WelcomeDeliveryState::Failed | WelcomeDeliveryState::Created
2025                ) && record.next_retry_at.is_some_and(|retry_at| retry_at <= now)
2026                {
2027                    return Some(peer_id.clone());
2028                }
2029                None
2030            })
2031            .take(WELCOME_RETRY_BATCH_SIZE)
2032            .collect();
2033
2034        for peer_id in due_peers {
2035            if let Err(err) = self.try_send_welcome(&peer_id, "welcome_retry") {
2036                warn!(
2037                    peer_id = %peer_id,
2038                    error = %err,
2039                    "Failed to process welcome retry"
2040                );
2041            }
2042        }
2043
2044        Ok(())
2045    }
2046
2047    pub(super) fn has_terminal_welcome_failure(&self, peer_id: &str) -> bool {
2048        self.welcome_lifecycles
2049            .get(peer_id)
2050            .is_some_and(|record| matches!(record.state, WelcomeDeliveryState::Expired))
2051    }
2052}