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