Skip to main content

offline_protocol/protocol/
send.rs

1//! Send pipeline, outbox management, and delivery tracking.
2
3use super::{
4    base64_encode, internal_prefixes, lifetime_expired, lock_shared_state,
5    ConnectionAcceptedPayload, ConnectionRequestPayload, KeyPackagePayload, MediaSendOptions,
6    MediaTransferDescriptor, OfflineProtocol, OutboundMediaTransfer, OutboundSendPreparation,
7    OutboxEntry, OutboxReseal, PendingConnectionRequest, PendingMessage, PendingProvenance,
8    PresencePayload, ProtocolState, ReadReceiptPayload, RichPayloadV1, RichSendExtras,
9    SendMessageOptions, TypingIndicatorPayload, WelcomeDeliveryState, MAX_INITIAL_MESSAGE_BYTES,
10    MAX_KEY_PACKAGE_SENT_TO, MAX_MESSAGE_CONTENT_BYTES, MAX_PENDING_CONNECTION_REQUESTS,
11    MAX_PENDING_EXPIRIES_PER_PASS, MAX_PENDING_MESSAGES_GLOBAL, MAX_PENDING_MESSAGES_PER_PEER,
12    MAX_PENDING_MESSAGE_BYTES_GLOBAL, MAX_PENDING_MESSAGE_BYTES_PER_PEER, MAX_READ_RECEIPT_IDS,
13    MAX_RICH_EXTRAS_BYTES, MLS_ENVELOPE_COMPACT_V1, PENDING_CONNECTION_REQUEST_TTL,
14    RICH_PAYLOAD_V1, SEND_FAIL_REASON_RECIPIENT_UNREACHABLE, SEND_FAIL_REASON_TRANSPORT,
15    WELCOME_NO_CARRIER_RETRY_SECS, WELCOME_UNREACHABLE_RETRY_CAP_SECS,
16};
17use super::{classify_transport_send_error, send_failure_token};
18use crate::constants::{
19    ACK_FOR_KEY, ACK_HOP_COUNT_KEY, ACK_TRANSPORT_KEY, MAX_FORWARD_COUNT, MAX_OUTBOX_ENTRIES,
20};
21use crate::events::{DecryptionFailureCode, Event, PresenceStatus};
22use crate::file_transfer::{FileChunk, OutboundTransferState};
23use crate::media_envelope::{encode_media_envelope, MediaChunkPlaintext, MediaRichExtras};
24use crate::mls_observability::{DecryptionFailureKind, MlsErrorCategory, MlsOperationContext};
25use crate::protocol::reachability::{Claim, FactSource};
26use crate::transport_manager::TransportManager;
27use crate::{Error, Result};
28use chrono::{DateTime, Utc};
29use offline_protocol_core::{
30    AppId, ContentType, ForwardInfo, MediaMetadata, Message, MessageId, MessagePriority, UserId,
31    TTL,
32};
33use offline_protocol_mls::{EncryptedMessage, MlsManager};
34use offline_protocol_transport::TransportType;
35use std::collections::HashSet;
36use std::sync::{Arc, RwLock};
37use std::time::{Duration as StdDuration, Instant};
38use tracing::{debug, error, info, warn};
39
40impl OfflineProtocol {
41    // ========================================================================
42    // CORE SEND
43    // ========================================================================
44
45    /// Sends a message.
46    ///
47    /// # Arguments
48    ///
49    /// * `recipient` - Recipient's user ID
50    /// * `content` - Message content
51    /// * `priority` - Message priority (optional, defaults to Medium)
52    /// * `reply_to_msg` - ID of the message this is replying to (optional)
53    ///
54    /// # Returns
55    ///
56    /// Returns the message ID if successful.
57    ///
58    /// # Auto-Encryption
59    ///
60    /// When encryption is enabled and MLS is initialized, messages are automatically
61    /// encrypted before sending. If no session exists with the recipient but we have
62    /// their key package, a session is created automatically. If no key package is
63    /// available and `store_pending` is enabled, the message is queued until a key
64    /// package is received.
65    pub fn send_message(
66        &mut self,
67        recipient: impl Into<String>,
68        content: impl Into<String>,
69        priority: Option<MessagePriority>,
70        reply_to_msg: Option<impl Into<String>>,
71    ) -> Result<MessageId> {
72        self.send_message_with(
73            recipient,
74            content,
75            SendMessageOptions {
76                priority,
77                reply_to_msg: reply_to_msg.map(Into::into),
78                ..Default::default()
79            },
80        )
81    }
82
83    /// Sends a message with rich options: priority and reply threading (as
84    /// on [`Self::send_message`]), plus quoted-reply context, rich media
85    /// metadata, and forward attribution.
86    ///
87    /// The rich fields only ever travel inside the MLS-sealed `__RICH_V1__`
88    /// body, and only toward recipients whose key package advertised
89    /// `rich_versions` support (gated by
90    /// `EncryptionConfig::rich_payload_enabled`). Toward anyone else they
91    /// are silently dropped — never sent cleartext — so the message degrades
92    /// to plain text with `reply_to_msg` threading intact.
93    ///
94    /// Returns `InvalidArgument` for `ContentType::FileChunk` (an internal
95    /// transport content type — the receiver would swallow the message into
96    /// its file-transfer manager) and for rich extras whose serialized size
97    /// exceeds 32 KiB (oversized bodies inflate the MLS plaintext into heavy
98    /// transport fragmentation).
99    pub fn send_message_with(
100        &mut self,
101        recipient: impl Into<String>,
102        content: impl Into<String>,
103        options: SendMessageOptions,
104    ) -> Result<MessageId> {
105        // Check if protocol is running
106        {
107            let state = lock_shared_state(&self.shared_state)?;
108            if state.state != ProtocolState::Running {
109                return Err(Error::NotStarted);
110            }
111        }
112
113        let recipient_str: String = recipient.into();
114        let content_str: String = content.into();
115        let priority = options.priority.unwrap_or(MessagePriority::Medium);
116        Self::validate_outbound_recipient(&recipient_str)?;
117
118        // Prevent sending messages to blocked users. Blocking is bidirectional:
119        // we neither receive from nor send to a blocked peer.
120        if self.is_user_blocked(&recipient_str) {
121            return Err(Error::UserBlocked(recipient_str));
122        }
123
124        Self::check_content_size(&content_str)?;
125
126        // Reject content that starts with an internal control prefix to prevent
127        // injection of protocol-level messages through the public API.
128        if Self::is_internal_prefix(&content_str) {
129            return Err(Error::InvalidArgument(
130                "Message content must not start with a reserved internal prefix".to_string(),
131            ));
132        }
133
134        // FileChunk is an internal transport content type: the receiver
135        // routes a message stamped with it into handle_incoming_file_chunk
136        // (after ACKing delivery) where a non-chunk fails to parse and is
137        // dropped without surfacing. Reject rather than silently lose it.
138        if options.content_type == Some(ContentType::FileChunk) {
139            return Err(Error::InvalidArgument(
140                "FileChunk is an internal content type and cannot be sent directly".to_string(),
141            ));
142        }
143
144        // Parse reply_to_msg if provided
145        let reply_to_msg_id = options
146            .reply_to_msg
147            .as_deref()
148            .map(MessageId::from_str)
149            .transpose()
150            .map_err(|e| Error::InvalidArgument(format!("Invalid reply_to_msg: {}", e)))?;
151
152        let rich = RichSendExtras {
153            reply_context: options.reply_context,
154            media_metadata: options.media_metadata,
155            forward_info: options.forward_info,
156        };
157
158        // Bound the extras here at the boundary — not at seal time — so the
159        // pending queue only ever holds extras that are known to seal: the
160        // flush path re-seals against current capability, and a seal-time
161        // failure there would re-queue the message forever. The cap keeps an
162        // oversized quote or thumbnail from inflating the MLS plaintext into
163        // heavy transport fragmentation.
164        rich.check_size()?;
165
166        let final_content = match self.prepare_outbound_content(
167            &recipient_str,
168            &content_str,
169            priority,
170            reply_to_msg_id.clone(),
171            None,
172            options.content_type.unwrap_or_default(),
173            None,
174            Some(&rich),
175            None,
176            "send_message_session_pending",
177        )? {
178            OutboundSendPreparation::Ready(content) => content,
179            OutboundSendPreparation::Queued(message_id) => return Ok(message_id),
180        };
181
182        // Create message with potentially encrypted content. Rich extras are
183        // deliberately NOT copied onto the outer message — they are either
184        // inside the sealed body by now or dropped; only the coarse
185        // content_type rendering hint rides outer.
186        let reseal_reply_to = reply_to_msg_id.clone();
187        let mut message = self.create_message(
188            &recipient_str,
189            final_content,
190            Some(priority),
191            reply_to_msg_id,
192        )?;
193        if let Some(content_type) = options.content_type {
194            message.content_type = content_type;
195        }
196
197        // Tier 2: if the send was encrypted, keep the plaintext provenance so
198        // each resend re-seals against the peer's current session instead of
199        // replaying dead-epoch ciphertext (see `OutboxReseal`).
200        if message.content.starts_with(internal_prefixes::ENCRYPTED) {
201            self.stage_outbox_reseal(
202                &message.id,
203                OutboxReseal {
204                    content: content_str,
205                    priority,
206                    reply_to_msg: reseal_reply_to,
207                    forwarded_from: None,
208                    content_type: message.content_type,
209                    media_metadata: None,
210                    rich: Some(rich).filter(|r| r.is_any()),
211                },
212            );
213        }
214
215        match options.via_transport {
216            Some(transport) => self.dispatch_prepared_message_via(message, transport),
217            None => self.dispatch_prepared_message(message),
218        }
219    }
220
221    /// Forwards a message to a new recipient with attribution to the original sender.
222    ///
223    /// Creates a new message with the original content and attaches `ForwardInfo`
224    /// tracking the original sender, message ID, timestamp, and forward count.
225    /// If the message was already forwarded, the original attribution is preserved
226    /// and `forward_count` is incremented.
227    ///
228    /// Toward a recipient that advertised the sealed rich payload, the
229    /// attribution and the original `media_metadata` — including any
230    /// cloud-media `encryption_key`/`iv` secrets — travel inside the MLS-sealed
231    /// `__RICH_V1__` body, so forwarded cloud media stays openable. Toward
232    /// anyone else they ride the legacy cleartext outer fields, from which
233    /// the transport chokepoint strips the secrets — attribution and display
234    /// hints survive, but the media key cannot be delivered.
235    ///
236    /// # Arguments
237    ///
238    /// * `original_message` - The message to forward
239    /// * `new_recipient` - Recipient's user ID
240    /// * `priority` - Message priority (optional, defaults to Medium)
241    ///
242    /// # Returns
243    ///
244    /// Returns the new message ID if successful.
245    pub fn forward_message(
246        &mut self,
247        original_message: &Message,
248        new_recipient: impl Into<String>,
249        priority: Option<MessagePriority>,
250    ) -> Result<MessageId> {
251        {
252            let state = lock_shared_state(&self.shared_state)?;
253            if state.state != ProtocolState::Running {
254                return Err(Error::NotStarted);
255            }
256        }
257
258        let recipient_str: String = new_recipient.into();
259        let priority = priority.unwrap_or(MessagePriority::Medium);
260        Self::validate_outbound_recipient(&recipient_str)?;
261
262        if self.is_user_blocked(&recipient_str) {
263            return Err(Error::UserBlocked(recipient_str));
264        }
265
266        // Reject content that starts with an internal control prefix to prevent
267        // injection of protocol-level messages through the forwarding API.
268        if Self::is_internal_prefix(&original_message.content) {
269            return Err(Error::InvalidArgument(
270                "Cannot forward a message with reserved internal prefix content".to_string(),
271            ));
272        }
273
274        // FileChunk is an internal transport content type (see
275        // `send_message_with`): a forwarded message stamped with it would be
276        // ACKed and then dropped by the receiver's file-transfer manager.
277        if original_message.content_type == ContentType::FileChunk {
278            return Err(Error::InvalidArgument(
279                "FileChunk is an internal content type and cannot be forwarded".to_string(),
280            ));
281        }
282
283        // Build ForwardInfo: preserve original attribution, increment count
284        let forward_info = ForwardInfo::from_message(original_message);
285
286        if forward_info.forward_count > MAX_FORWARD_COUNT {
287            return Err(Error::InvalidArgument(format!(
288                "Forward count {} exceeds maximum of {}",
289                forward_info.forward_count, MAX_FORWARD_COUNT,
290            )));
291        }
292
293        // Sealed-path extras: toward a rich-capable recipient the attribution
294        // and the original media metadata (including cloud-media key/iv
295        // secrets, which only the sealed body may carry — the wire chokepoint
296        // strips them from the cleartext outer field) travel inside the
297        // `__RICH_V1__` body. The outer copies set below remain the fallback
298        // for non-capable recipients; the receiver's sealed-body restore is
299        // authoritative and overwrites them when the seal happened.
300        let rich = RichSendExtras {
301            reply_context: None,
302            media_metadata: original_message.media_metadata.clone(),
303            forward_info: Some(forward_info.clone()),
304        };
305        // Same boundary caps as `send_message_with` — enforced here, not at
306        // seal time, so a forward queued behind session establishment is
307        // always known to seal at flush and always fits the queue's budget.
308        Self::check_content_size(&original_message.content)?;
309        rich.check_size()?;
310
311        // Prepare content (may encrypt). ForwardInfo is threaded through so
312        // it survives the pending-message queue if the session isn't ready.
313        let final_content = match self.prepare_outbound_content(
314            &recipient_str,
315            &original_message.content,
316            priority,
317            None,
318            Some(forward_info.clone()),
319            original_message.content_type,
320            original_message.media_metadata.clone(),
321            Some(&rich),
322            None,
323            "forward_message_session_pending",
324        )? {
325            OutboundSendPreparation::Ready(content) => content,
326            OutboundSendPreparation::Queued(message_id) => return Ok(message_id),
327        };
328
329        // Create and dispatch the forwarded message
330        let mut message =
331            self.create_message(&recipient_str, final_content, Some(priority), None)?;
332        message.forwarded_from = Some(forward_info);
333        message.content_type = original_message.content_type;
334        message.media_metadata = original_message.media_metadata.clone();
335
336        // Tier 2 provenance (see `send_message_with`): re-seal forwarded encrypted
337        // DMs against the peer's current session on resend.
338        if message.content.starts_with(internal_prefixes::ENCRYPTED) {
339            self.stage_outbox_reseal(
340                &message.id,
341                OutboxReseal {
342                    content: original_message.content.clone(),
343                    priority,
344                    reply_to_msg: None,
345                    forwarded_from: message.forwarded_from.clone(),
346                    content_type: message.content_type,
347                    media_metadata: message.media_metadata.clone(),
348                    rich: Some(rich).filter(|r| r.is_any()),
349                },
350            );
351        }
352
353        self.dispatch_prepared_message(message)
354    }
355
356    /// Shared send path: dedup, transport send, success/failure handling, event emission.
357    ///
358    /// Used by `forward_message`, `flush_pending_messages` (for forwarded messages),
359    /// and any path that has a fully-constructed `Message` ready to send.
360    fn dispatch_prepared_message(&mut self, message: Message) -> Result<MessageId> {
361        let message_id = message.id.clone();
362
363        if self.deduplicator.is_duplicate(&message_id) {
364            return Err(crate::Error::Other("Duplicate message".to_string()));
365        }
366
367        self.deduplicator.mark_seen(message_id.clone());
368
369        let previous_transport = self.transport_manager.current_transport();
370
371        // A recipient this device holds a live mesh link to is reached over
372        // that link, even with infrastructure up. Without this the online
373        // sender standing next to an offline recipient routes through the
374        // relay, which answers `recipient_unreachable`, and delivery waits for
375        // the park machinery to offer the frame to the very neighbour that was
376        // addressable all along. DORS cannot make this call: it scores
377        // carriers, and the fact that decides it is about the recipient.
378        //
379        // The link is ground truth, not a stored claim, so there is nothing to
380        // go stale. A refusal here is not terminal: the frame falls through to
381        // selection exactly as if this had never been tried, which keeps a
382        // marginal radio link from stranding a message another carrier could
383        // take.
384        let direct_mesh = self
385            .transport_manager
386            .mesh_transport_for(message.recipient.as_str());
387        let mut sent_via_mesh = None;
388        if let Some(transport) = direct_mesh {
389            match self
390                .transport_manager
391                .send_via_transport(&message, transport)
392            {
393                Ok(()) => sent_via_mesh = Some(transport),
394                Err(err) => {
395                    self.transport_manager.record_retry_failure(transport);
396                    debug!(
397                        message_id = %message.id,
398                        transport = ?transport,
399                        error = %err,
400                        "Direct mesh link refused the frame; falling back to transport selection"
401                    );
402                }
403            }
404        }
405
406        let (send_result, current_transport) = match sent_via_mesh {
407            Some(transport) => (Ok(()), Some(transport)),
408            None => {
409                let result = self.transport_manager.send(&message);
410                (result, self.transport_manager.current_transport())
411            }
412        };
413
414        match send_result {
415            Ok(()) => {
416                self.handle_send_success(&message, current_transport)?;
417                self.emit_transport_switch_event(previous_transport, current_transport)?;
418                self.emit_message_sent_event(&message)?;
419                Ok(message_id)
420            }
421            Err(err) => {
422                let next_retry_at =
423                    self.handle_send_failure(&message, current_transport.or(previous_transport))?;
424                warn!(
425                    message_id = %message.id,
426                    error = %err,
427                    "Send failed, message deferred"
428                );
429                self.emit_event(Event::message_deferred(
430                    message.id.clone(),
431                    message.recipient.as_str().to_string(),
432                    send_failure_token(&err),
433                    0,
434                    next_retry_at.map(|at| at.timestamp_millis()),
435                ));
436                Ok(message_id)
437            }
438        }
439    }
440
441    /// [`Self::dispatch_prepared_message`] variant that sends via a specific
442    /// transport (bypassing DORS) — the shared tail for
443    /// `send_message_via_transport` and rich sends carrying `via_transport`.
444    /// Unlike `TransportManager::send`, `send_via_transport` does not record
445    /// retry failures internally, so the failure arm records one explicitly.
446    fn dispatch_prepared_message_via(
447        &mut self,
448        message: Message,
449        transport: TransportType,
450    ) -> Result<MessageId> {
451        let message_id = message.id.clone();
452
453        if self.deduplicator.is_duplicate(&message_id) {
454            return Err(crate::Error::Other("Duplicate message".to_string()));
455        }
456
457        self.deduplicator.mark_seen(message_id.clone());
458
459        let previous_transport = self.transport_manager.current_transport();
460        let send_result = self
461            .transport_manager
462            .send_via_transport(&message, transport);
463        let current_transport = Some(transport);
464
465        match send_result {
466            Ok(()) => {
467                self.handle_send_success(&message, current_transport)?;
468                self.emit_transport_switch_event(previous_transport, current_transport)?;
469                self.emit_message_sent_event(&message)?;
470                Ok(message_id)
471            }
472            Err(err) => {
473                let next_retry_at =
474                    self.handle_send_failure(&message, current_transport.or(previous_transport))?;
475                // send_via_transport does not record retry failures internally
476                // (unlike TransportManager::send), so record explicitly here.
477                self.transport_manager.record_retry_failure(transport);
478                warn!(
479                    message_id = %message.id,
480                    transport = ?transport,
481                    error = %err,
482                    "Send via forced transport failed, message deferred"
483                );
484                // The forced transport is in the `warn!` above, not on the
485                // event: it is a routing detail, and the token names the
486                // failure class either way.
487                self.emit_event(Event::message_deferred(
488                    message.id.clone(),
489                    message.recipient.as_str().to_string(),
490                    send_failure_token(&err),
491                    0,
492                    next_retry_at.map(|at| at.timestamp_millis()),
493                ));
494                Ok(message_id)
495            }
496        }
497    }
498
499    /// Sends an internal protocol message (connection requests, etc.) via DORS.
500    ///
501    /// Handles the full send orchestration: state check, deduplication, transport send,
502    /// success/failure handling, and transport switch events. Does NOT emit a
503    /// `MessageSent` event — internal messages are not user-visible content.
504    pub(crate) fn send_internal_message(
505        &mut self,
506        recipient: &str,
507        content: String,
508        priority: MessagePriority,
509    ) -> Result<MessageId> {
510        {
511            let state = lock_shared_state(&self.shared_state)?;
512            if state.state != ProtocolState::Running {
513                return Err(Error::NotStarted);
514            }
515        }
516
517        let mut message = self.create_message(recipient, content, Some(priority), None)?;
518        self.sign_control_message(&mut message)?;
519        let message_id = message.id.clone();
520
521        if self.deduplicator.is_duplicate(&message_id) {
522            return Err(crate::Error::Other("Duplicate message".to_string()));
523        }
524
525        self.deduplicator.mark_seen(message_id.clone());
526
527        let previous_transport = self.transport_manager.current_transport();
528        let send_result = self.transport_manager.send(&message);
529        let current_transport = self.transport_manager.current_transport();
530
531        match send_result {
532            Ok(()) => {
533                self.handle_send_success(&message, current_transport)?;
534            }
535            Err(err) => {
536                let _ =
537                    self.handle_send_failure(&message, current_transport.or(previous_transport))?;
538                warn!(
539                    message_id = %message.id,
540                    recipient = %recipient,
541                    error = %err,
542                    "Internal message send failed, message deferred"
543                );
544            }
545        }
546
547        self.emit_transport_switch_event(previous_transport, current_transport)?;
548        Ok(message_id)
549    }
550
551    /// Sends a self-addressed relay *hint* frame — `__GRP_RELAY_REG__` or
552    /// `__GRP_RELAY_BCAST__` — pinned to the Internet transport, without the
553    /// ACK ladder.
554    ///
555    /// A hint frame is not traffic. The platform bridge recognizes it via
556    /// [`Self::internet_control_op`] and **replaces** it with a relay-native
557    /// frame (`CreateGroup` + member deltas, or `SendGroupMessage`), so the
558    /// frame itself never reaches a peer and no delivery ACK can ever come
559    /// back for it. Two properties follow, and both are load-bearing:
560    ///
561    /// 1. **`requires_ack = false`.** On the ordinary ladder an un-ACKable
562    ///    frame is retransmitted `DEFAULT_MAX_RETRIES` times over ~800s, each
563    ///    resend producing another full relay fan-out (under a fresh
564    ///    relay-minted id, so receiver dedup misses it), and terminates in a
565    ///    `MessageFailed` for an id the app was never told about plus a
566    ///    `record_delivery_failure` that degrades DORS' score for the very
567    ///    transport the frame requires. Opting out of the ladder is what makes
568    ///    these frames honest one-shots; retry policy for them lives at the
569    ///    application layer instead (`RelayRegisterPending` for registration,
570    ///    the caller's per-member fallback for broadcast).
571    /// 2. **Pinned to [`TransportType::Internet`].** These frames are
572    ///    self-addressed, and DORS demotes Internet below every mesh transport
573    ///    (`INTERNET_FALLBACK_DEMOTION`), so ordinary routing hands them to
574    ///    BLE/Wi-Fi Direct first. BLE fails closed (self is never a connected
575    ///    peer), but Wi-Fi Direct and Reticulum enqueue unconditionally and
576    ///    return `Ok` — swallowing the frame while reporting success, which on
577    ///    the broadcast path means the group message is delivered to nobody.
578    ///
579    /// Errors propagate to the caller rather than routing through
580    /// [`Self::handle_send_failure`]: a hint frame has no outbox entry to
581    /// defer into, and its callers already have a better recovery than a
582    /// retry (fall back to per-member fan-out; leave the group unregistered
583    /// for the next sync tick). The failure is also deliberately *not*
584    /// recorded against Internet's DORS metrics — "the relay bridge isn't up"
585    /// is not evidence about the transport's delivery quality.
586    ///
587    /// No `TransportChanged` event is emitted, matching
588    /// [`Self::send_delivery_ack`] — the other fire-and-forget control frame
589    /// sent over a forced transport. A hint frame is not user traffic, and
590    /// announcing a switch for one would report a transport change the app's
591    /// messages did not take.
592    pub(crate) fn send_relay_hint_message(
593        &mut self,
594        content: String,
595        priority: MessagePriority,
596    ) -> Result<MessageId> {
597        {
598            let state = lock_shared_state(&self.shared_state)?;
599            if state.state != ProtocolState::Running {
600                return Err(Error::NotStarted);
601            }
602        }
603
604        let self_id = self.local_id.clone();
605        let mut message = self.create_message(&self_id, content, Some(priority), None)?;
606        // Set before signing for clarity; the canonical payload covers only
607        // sender/id/recipient/content, so ordering is not security-relevant.
608        message.requires_ack = false;
609        self.sign_control_message(&mut message)?;
610        let message_id = message.id.clone();
611
612        if self.deduplicator.is_duplicate(&message_id) {
613            return Err(crate::Error::Other("Duplicate message".to_string()));
614        }
615
616        // Mark seen so a bridge that passes the frame through verbatim (an
617        // adapter without a translator) cannot have the relay echo it back
618        // into our own receive path.
619        self.deduplicator.mark_seen(message_id.clone());
620
621        self.transport_manager
622            .send_via_transport(&message, TransportType::Internet)?;
623
624        Ok(message_id)
625    }
626
627    /// Sends one MLS-encrypted session-confirm marker to `peer_id`.
628    ///
629    /// Used by the Welcome-adopt path: after we adopt the peer's `session:` group
630    /// (and confirm our own side), the peer may be the both-create "owner" that
631    /// kept its own group and confirms ONLY on a group-aware decrypt from us. A
632    /// plaintext probe/ack does not count, and we may have no user traffic to
633    /// send — so without this the owner stays Pending forever and the 1:1
634    /// connection never completes. The marker is `SESSION_CONFIRM_ENCRYPTED`
635    /// wrapped in the normal `ENCRYPTED` envelope; the peer consumes it on decrypt
636    /// (never surfaced). Mirrors [`Self::send_internal_message`] but encrypts
637    /// (data-plane, so NOT signed) and never propagates errors — it is re-sent on
638    /// every received Welcome while still adopting, so a lost confirm is retried
639    /// in lockstep with the owner's Welcome retransmission.
640    pub(super) fn send_session_confirm_encrypted(&mut self, peer_id: &str) {
641        let encrypted = match self.encrypt_content_for_recipient(
642            peer_id,
643            internal_prefixes::SESSION_CONFIRM_ENCRYPTED,
644            MessagePriority::High,
645        ) {
646            Ok(content) => content,
647            Err(err) => {
648                debug!(
649                    peer = %peer_id,
650                    error = %err,
651                    "Skipped proactive session-confirm (encryption not ready); welcome retry remains"
652                );
653                return;
654            }
655        };
656
657        let message =
658            match self.create_message(peer_id, encrypted, Some(MessagePriority::High), None) {
659                Ok(message) => message,
660                Err(err) => {
661                    warn!(peer = %peer_id, error = %err, "Failed to build session-confirm message");
662                    return;
663                }
664            };
665
666        if self.deduplicator.is_duplicate(&message.id) {
667            return;
668        }
669        self.deduplicator.mark_seen(message.id.clone());
670
671        let previous_transport = self.transport_manager.current_transport();
672        match self.transport_manager.send(&message) {
673            Ok(()) => {
674                let current_transport = self.transport_manager.current_transport();
675                let _ = self.handle_send_success(&message, current_transport);
676                info!(peer = %peer_id, "Sent proactive encrypted session-confirm to owner");
677            }
678            Err(err) => {
679                let current_transport = self.transport_manager.current_transport();
680                let _ =
681                    self.handle_send_failure(&message, current_transport.or(previous_transport));
682                debug!(
683                    peer = %peer_id,
684                    error = %err,
685                    "Proactive session-confirm deferred (will retry with welcome)"
686                );
687            }
688        }
689    }
690
691    /// Sends a message via a specific transport type (bypassing DORS).
692    pub fn send_message_via_transport(
693        &mut self,
694        recipient: impl Into<String>,
695        content: impl Into<String>,
696        priority: Option<MessagePriority>,
697        transport: TransportType,
698        reply_to_msg: Option<impl Into<String>>,
699    ) -> Result<MessageId> {
700        self.send_message_with(
701            recipient,
702            content,
703            SendMessageOptions {
704                priority,
705                reply_to_msg: reply_to_msg.map(Into::into),
706                via_transport: Some(transport),
707                ..Default::default()
708            },
709        )
710    }
711
712    /// Rejects a recipient token that is not a well-formed [`UserId`].
713    ///
714    /// Enforced at every outbound boundary so an app-owned placeholder (an
715    /// `unresolved:token`, an empty string) cannot become durable protocol
716    /// state — an outbox entry or pending-queue record that is retried, stored,
717    /// and restored forever against an address no transport can ever resolve.
718    pub(crate) fn validate_outbound_recipient(recipient: &str) -> Result<()> {
719        UserId::new(recipient)
720            .map(|_| ())
721            .map_err(|error| Error::InvalidArgument(format!("Invalid recipient user ID: {error}")))
722    }
723
724    /// Creates a new message from the given parameters.
725    pub(super) fn create_message(
726        &mut self,
727        recipient: impl Into<String>,
728        content: impl Into<String>,
729        priority: Option<MessagePriority>,
730        reply_to_msg: Option<MessageId>,
731    ) -> Result<Message> {
732        let sender = UserId::new(&self.local_id)?;
733        let recipient = UserId::new(recipient)?;
734        let app_id = AppId::new(&self.config.app_id)?;
735
736        let clock_value = self.lamport_clock.tick();
737        self.persist_lamport_clock();
738
739        let mut builder = Message::builder(sender, recipient, app_id)
740            .content(content)
741            .priority(priority.unwrap_or(MessagePriority::Medium))
742            .ttl(TTL::new(self.config.initial_ttl)?)
743            .lamport_clock(clock_value);
744
745        if let Some(reply_to) = reply_to_msg {
746            builder = builder.reply_to_msg(reply_to);
747        }
748
749        Ok(builder.build())
750    }
751
752    // ========================================================================
753    // ENCRYPTION
754    // ========================================================================
755
756    pub(super) fn encrypt_content_for_recipient(
757        &mut self,
758        recipient: &str,
759        content: &str,
760        _priority: MessagePriority,
761    ) -> Result<String> {
762        let encrypted = self.encrypt_bytes_for_recipient(recipient, content.as_bytes())?;
763        self.seal_encrypted_content(recipient, &encrypted)
764    }
765
766    /// Bytes-level core of [`Self::encrypt_content_for_recipient`]: initiates
767    /// session establishment when a key package is available and encrypts once
768    /// the session is confirmed. Text content and media chunk plaintexts share
769    /// this path.
770    pub(super) fn encrypt_bytes_for_recipient(
771        &mut self,
772        recipient: &str,
773        plaintext: &[u8],
774    ) -> Result<EncryptedMessage> {
775        // Clone the Arc to avoid borrow issues
776        let mls = self.mls_manager.clone().ok_or_else(|| {
777            self.emit_mls_session_missing(
778                Some(recipient),
779                None,
780                MlsOperationContext::SessionLookup,
781                MlsErrorCategory::NotInitialized,
782            );
783            Error::MlsNotInitialized
784        })?;
785
786        // Fast path: if the session is already confirmed (in-memory cache hit),
787        // skip the has_session() storage call — we know the session exists.
788        if self.confirmed_sessions.contains(recipient) {
789            return self.encrypt_bytes_confirmed_session(&mls, recipient, plaintext);
790        }
791
792        self.ensure_session_establishment(&mls, recipient)?;
793
794        // Only encrypt if session is confirmed (Welcome processed or successful decrypt).
795        // Confirmation truth comes from persisted session state.
796        if !self.is_session_confirmed(recipient)? {
797            debug!(recipient = %recipient, "Session exists but not confirmed, queuing message");
798            return Err(Error::SessionNotReady(self.establishment_state(recipient)?));
799        }
800
801        // Encrypt the message
802        let encrypted = {
803            let manager = mls
804                .read()
805                .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
806            manager
807                .encrypt_for_user(recipient, plaintext)
808                .map_err(|_| Error::EncryptFailed("encryption operation failed".to_string()))?
809        };
810
811        self.emit_mls_encryption_used(recipient);
812        Ok(encrypted)
813    }
814
815    /// Ensures MLS session establishment with `recipient` has at least been
816    /// initiated: when no session exists in storage, imports a stored/pending
817    /// key package, creates the session, and sends the Welcome.
818    ///
819    /// Returns `Ok(())` when a session already exists (possibly unconfirmed —
820    /// callers must still gate on `is_session_confirmed`) or when no usable
821    /// key package remains (expired). Returns `Err(SessionNotReady)` when no
822    /// key package is available, or right after creating a session with
823    /// `store_pending` enabled (the session cannot be confirmed yet).
824    pub(super) fn ensure_session_establishment(
825        &mut self,
826        mls: &Arc<RwLock<MlsManager>>,
827        recipient: &str,
828    ) -> Result<()> {
829        // Check for existing session (requires storage I/O via load_group)
830        let has_session = {
831            let manager = mls
832                .read()
833                .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
834            manager.has_session(recipient)?
835        };
836
837        if !has_session {
838            // Try loading key package from storage (e.g. after restart) then create session from memory
839            self.try_load_key_package_from_storage_into_memory(recipient);
840            // Try to create session from stored key package
841            // Clone first, only remove after all operations succeed to avoid losing the key package on failure
842            if let Some(received_pkg) = self.pending_key_packages.get(recipient).cloned() {
843                // Check if key package has expired (using local clock)
844                let now_ms = Utc::now().timestamp_millis() as u64;
845                if now_ms >= received_pkg.local_expires_at_ms {
846                    warn!(recipient = %recipient, "Received key package has expired, discarding");
847                    self.pending_key_packages.remove(recipient);
848                    self.delete_peer_key_package_from_storage(recipient);
849                } else {
850                    {
851                        let manager = mls
852                            .read()
853                            .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
854                        manager.import_key_package(recipient, &received_pkg.key_package_data)?;
855                    }
856
857                    // Create session and send welcome message
858                    let welcome = {
859                        let manager = mls
860                            .read()
861                            .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
862                        manager.create_session(recipient)?
863                    };
864                    self.mark_encryption_capable(recipient);
865
866                    // All operations succeeded, now safe to remove the key package
867                    self.pending_key_packages.remove(recipient);
868                    self.delete_peer_key_package_from_storage(recipient);
869
870                    let group_id = welcome.group_id.as_str().to_string();
871                    let is_session = group_id.starts_with("session:");
872
873                    if let Err(err) =
874                        self.ensure_session_state_entry(recipient, "session_created_local")
875                    {
876                        warn!(
877                            recipient = %recipient,
878                            error = %err,
879                            "Failed to persist pending session state"
880                        );
881                    }
882
883                    let welcome_sent = self.send_welcome_message(recipient, &welcome)?;
884
885                    debug!(
886                        recipient = %recipient,
887                        group_id = %group_id,
888                        welcome_sent = welcome_sent,
889                        "Created MLS session and scheduled welcome lifecycle"
890                    );
891
892                    if welcome_sent {
893                        debug!(recipient = %recipient, group_id = %group_id, is_session, "Welcome synchronously sent");
894                    }
895
896                    // Don't encrypt immediately after creating session.
897                    // Queue message until session is confirmed (peer processes our Welcome
898                    // and we successfully decrypt their first message, or we receive their Welcome).
899                    // This avoids race conditions where both peers create sessions.
900                    if self.config.encryption.store_pending {
901                        return Err(Error::SessionNotReady(self.establishment_state(recipient)?));
902                    }
903                }
904            } else {
905                // No key package available (memory nor storage)
906                self.emit_mls_session_missing(
907                    Some(recipient),
908                    None,
909                    MlsOperationContext::SessionLookup,
910                    MlsErrorCategory::SessionStateMissing,
911                );
912                return Err(Error::SessionNotReady(self.establishment_state(recipient)?));
913            }
914        }
915
916        Ok(())
917    }
918
919    /// Serializes an [`EncryptedMessage`] into the prefixed string form used
920    /// by the text path.
921    ///
922    /// Recipients that advertised [`MLS_ENVELOPE_COMPACT_V1`] in their key
923    /// package (with our own `compact_envelope_enabled` on) get the compact
924    /// envelope — `__MLS_ENC__` + base64 of [`EncryptedMessage::to_bytes`] —
925    /// roughly 2.7x smaller than the legacy JSON form, whose `ciphertext`
926    /// field renders as an integer array. Everyone else gets `__MLS_ENC__` +
927    /// JSON, the permanent floor. Receivers sniff the byte after the prefix
928    /// (`{` = JSON), so no per-message signaling is needed, and the choice is
929    /// per-recipient end-to-end state, valid however the message is later
930    /// routed or retried.
931    pub(super) fn seal_encrypted_content(
932        &self,
933        recipient: &str,
934        encrypted: &EncryptedMessage,
935    ) -> Result<String> {
936        if self.config.encryption.compact_envelope_enabled
937            && self.peer_compact_envelope.contains(recipient)
938        {
939            return Ok(format!(
940                "{}{}",
941                internal_prefixes::ENCRYPTED,
942                base64_encode(&encrypted.to_bytes())
943            ));
944        }
945        let serialized =
946            serde_json::to_string(encrypted).map_err(|e| Error::Serialization(e.to_string()))?;
947        Ok(format!("{}{}", internal_prefixes::ENCRYPTED, serialized))
948    }
949
950    /// The sync versions this device advertises.
951    ///
952    /// Empty unless the data layer is both compiled in and switched on. A
953    /// capability advertised with nothing behind it is worse than silence:
954    /// the peer starts sending frames and waiting for answers that never
955    /// come, and nothing in that exchange looks like an error to either
956    /// side.
957    pub(crate) fn advertised_data_versions(&self) -> Vec<u8> {
958        #[cfg(feature = "data")]
959        {
960            if self.config.data.enabled {
961                return vec![
962                    super::types::DATA_SYNC_V1,
963                    super::types::DATA_GROUP_V1,
964                    super::types::DATA_MEDIA_V1,
965                ];
966            }
967        }
968        Vec::new()
969    }
970
971    /// Whether document sync is live toward `recipient`: the layer is
972    /// compiled in and enabled here, the peer is not blocked, and they
973    /// advertised [`DATA_SYNC_V1`] in their key package.
974    ///
975    /// Consulted per send rather than cached at session establishment, like
976    /// the two capability gates below it: a peer that downgrades stops
977    /// receiving frames on their next key package, not at their next
978    /// session.
979    ///
980    /// The block check is here rather than at the call sites because this is
981    /// the only gate they share, and because replication is not reached
982    /// through a send API: every public send surface checks blocking for
983    /// itself, while these frames are triggered by a commit or a discovery.
984    /// A blocked peer still holds a live session, so nothing further down
985    /// would have refused the frame. Inbound needs no equivalent: the
986    /// receive path drops a blocked sender before any dispatch.
987    #[cfg_attr(not(feature = "data"), allow(dead_code))]
988    pub(super) fn data_sync_active(&self, recipient: &str) -> bool {
989        #[cfg(feature = "data")]
990        {
991            self.config.data.enabled
992                && self.peer_data_sync.contains(recipient)
993                && !self.is_user_blocked(recipient)
994        }
995        #[cfg(not(feature = "data"))]
996        {
997            let _ = recipient;
998            false
999        }
1000    }
1001
1002    /// Whether blobs may travel to `recipient`: 1:1 replication is live with
1003    /// them and they advertised [`DATA_MEDIA_V1`], so they understand the
1004    /// fetch frames and route a data-purposed media transfer into the data
1005    /// layer rather than to their user.
1006    ///
1007    /// Strictly narrower than [`Self::data_sync_active`] and checked in
1008    /// addition to it, never instead: every blob exchange is part of a
1009    /// replication that the first gate already had to allow, and a peer who
1010    /// stops replicating stops receiving blobs at the same moment.
1011    ///
1012    /// [`DATA_MEDIA_V1`]: crate::protocol::types::DATA_MEDIA_V1
1013    #[cfg_attr(not(feature = "data"), allow(dead_code))]
1014    pub(super) fn data_media_active(&self, recipient: &str) -> bool {
1015        #[cfg(feature = "data")]
1016        {
1017            self.data_sync_active(recipient) && self.peer_data_media.contains(recipient)
1018        }
1019        #[cfg(not(feature = "data"))]
1020        {
1021            let _ = recipient;
1022            false
1023        }
1024    }
1025
1026    /// Whether rich extras seal for `recipient`: our own kill switch is on
1027    /// and the peer advertised [`RICH_PAYLOAD_V1`] in their key package.
1028    pub(super) fn rich_seal_active(&self, recipient: &str) -> bool {
1029        self.config.encryption.rich_payload_enabled && self.peer_rich_payload.contains(recipient)
1030    }
1031
1032    /// Whether rich extras may seal into a group message: our own kill
1033    /// switch is on and every non-self member is known to parse
1034    /// [`RICH_PAYLOAD_V1`] — either self-advertised in a directly received
1035    /// key package (`peer_rich_payload`) or attested by a group inviter on
1036    /// the Add commit / Welcome (`peer_rich_attested`; members added by
1037    /// someone else never exchange key packages with us directly). Group
1038    /// MLS encryption produces a single ciphertext for all members, so one
1039    /// non-capable or capability-unknown member forces the extras to drop
1040    /// for the whole group. Conservative by design: an unknown member must
1041    /// never receive a sealed body their SDK would render as literal
1042    /// `__RICH_V1__` JSON.
1043    ///
1044    /// Attestation is third-party and may be stale or forged by a rogue
1045    /// admin; the worst case is exactly the degraded display below, never a
1046    /// leak, and direct contact evicts the attested entry. Best-effort
1047    /// beyond that: callers pass the `group_mesh.members` fan-out cache,
1048    /// which trails MLS membership until the add/remove notification
1049    /// lands. In that window a just-added non-capable member can receive
1050    /// one sealed body and render it as literal JSON — degraded display,
1051    /// not a leak (every group member is entitled to the sealed contents).
1052    pub(crate) fn group_rich_seal_active(&self, members: &[String]) -> bool {
1053        self.config.encryption.rich_payload_enabled
1054            && members
1055                .iter()
1056                .filter(|m| m.as_str() != self.local_id)
1057                .all(|m| {
1058                    self.peer_rich_payload.contains(m.as_str())
1059                        || self.peer_rich_attested.contains(m.as_str())
1060                })
1061    }
1062
1063    /// Group members (non-self) not known to parse the sealed rich payload
1064    /// — neither directly advertised nor inviter-attested. These are the
1065    /// members that hold `group_rich_seal_active` closed; the set absence
1066    /// cannot distinguish "never heard from" from "known not capable".
1067    pub(crate) fn group_rich_unknown_members(&self, members: &[String]) -> Vec<String> {
1068        members
1069            .iter()
1070            .filter(|m| {
1071                m.as_str() != self.local_id
1072                    && !self.peer_rich_payload.contains(m.as_str())
1073                    && !self.peer_rich_attested.contains(m.as_str())
1074            })
1075            .cloned()
1076            .collect()
1077    }
1078
1079    /// The rich-payload versions we can attest for a peer when inviting it
1080    /// into (or welcoming it to) a group: [`RICH_PAYLOAD_V1`] when the peer
1081    /// is known capable (directly or itself attested — attestation chains
1082    /// transitively, which is how knowledge reaches members several adds
1083    /// removed from any direct exchange), `None` when unknown. `None` is
1084    /// deliberately not a downgrade signal: absence of knowledge must never
1085    /// evict what another member learned first-hand.
1086    pub(crate) fn attestable_rich_versions(&self, peer_id: &str) -> Option<Vec<u8>> {
1087        (self.peer_rich_payload.contains(peer_id) || self.peer_rich_attested.contains(peer_id))
1088            .then(|| vec![RICH_PAYLOAD_V1])
1089    }
1090
1091    /// Whether documents may replicate into a group: the layer is enabled
1092    /// here and every non-self member is known to intercept group sync
1093    /// frames — either self-advertised in a directly received key package
1094    /// (`peer_data_group`) or attested by a group inviter on the Add commit
1095    /// / Welcome (`peer_data_group_attested`).
1096    ///
1097    /// All-members rather than per-member, and that is not a convenience.
1098    /// Group MLS encryption produces one ciphertext for everyone, so a
1099    /// frame sent for the capable members is also delivered to the rest;
1100    /// one member that cannot intercept it renders it to its user as
1101    /// literal `__DATA_V1__` text. The gate is therefore closed by a single
1102    /// unknown member, which is the same conservatism
1103    /// [`Self::group_rich_seal_active`] applies to sealed rich bodies, for
1104    /// the same reason and with a worse symptom if skipped.
1105    ///
1106    /// Blocking is deliberately not consulted here. A blocked *peer* is a
1107    /// 1:1 relationship; a group send is one ciphertext to a roster, and
1108    /// dropping the whole group's replication because one member is blocked
1109    /// would be a surprising amount of collateral. The 1:1 gate keeps its
1110    /// block check because there the recipient and the relationship are the
1111    /// same thing.
1112    #[cfg_attr(not(feature = "data"), allow(dead_code))]
1113    pub(crate) fn group_data_sync_active(&self, members: &[String]) -> bool {
1114        #[cfg(feature = "data")]
1115        {
1116            self.config.data.enabled
1117                && members
1118                    .iter()
1119                    .filter(|m| m.as_str() != self.local_id)
1120                    .all(|m| {
1121                        self.peer_data_group.contains(m.as_str())
1122                            || self.peer_data_group_attested.contains(m.as_str())
1123                    })
1124        }
1125        #[cfg(not(feature = "data"))]
1126        {
1127            let _ = members;
1128            false
1129        }
1130    }
1131
1132    /// Group members (non-self) not known to intercept group sync frames.
1133    ///
1134    /// These are the members holding [`Self::group_data_sync_active`]
1135    /// closed. Handed to [`Self::backfill_group_rich_capabilities`], whose
1136    /// probe is capability-agnostic: it sends our key package so the peer's
1137    /// reply teaches us everything they advertise at once.
1138    #[cfg_attr(not(feature = "data"), allow(dead_code))]
1139    pub(crate) fn group_data_unknown_members(&self, members: &[String]) -> Vec<String> {
1140        #[cfg(feature = "data")]
1141        {
1142            members
1143                .iter()
1144                .filter(|m| {
1145                    m.as_str() != self.local_id
1146                        && !self.peer_data_group.contains(m.as_str())
1147                        && !self.peer_data_group_attested.contains(m.as_str())
1148                })
1149                .cloned()
1150                .collect()
1151        }
1152        #[cfg(not(feature = "data"))]
1153        {
1154            let _ = members;
1155            Vec::new()
1156        }
1157    }
1158
1159    /// The sync versions we can attest for a peer when inviting it into (or
1160    /// welcoming it to) a group.
1161    ///
1162    /// [`DATA_GROUP_V1`] when the peer is known group-capable, directly or
1163    /// itself attested — attestation chains transitively, which is how
1164    /// knowledge reaches members several adds removed from any direct
1165    /// exchange. `None` when unknown, and `None` is deliberately not a
1166    /// downgrade signal: absence of knowledge must never evict what another
1167    /// member learned first-hand.
1168    ///
1169    /// [`DATA_GROUP_V1`]: crate::protocol::types::DATA_GROUP_V1
1170    #[cfg_attr(not(feature = "data"), allow(dead_code))]
1171    pub(crate) fn attestable_data_versions(&self, peer_id: &str) -> Option<Vec<u8>> {
1172        #[cfg(feature = "data")]
1173        {
1174            (self.peer_data_group.contains(peer_id)
1175                || self.peer_data_group_attested.contains(peer_id))
1176            .then(|| vec![super::types::DATA_GROUP_V1])
1177        }
1178        #[cfg(not(feature = "data"))]
1179        {
1180            let _ = peer_id;
1181            None
1182        }
1183    }
1184
1185    /// Best-effort capability backfill for group members whose rich support
1186    /// is unknown (pre-attestation groups, or an attestation chain broken by
1187    /// an old-SDK inviter): send them our key package so their
1188    /// `auto_key_exchange` reply teaches us theirs. Guarded by
1189    /// `key_package_sent_to`, so repeated gate-failing group sends don't
1190    /// re-probe the same peer; a peer that never replies is either
1191    /// unreachable, old-SDK, or opted out — all of which correctly leave
1192    /// the gate closed.
1193    pub(crate) fn backfill_group_rich_capabilities(&mut self, unknown_members: &[String]) {
1194        for member in unknown_members {
1195            if self.key_package_sent_to.contains(member.as_str()) {
1196                continue;
1197            }
1198            if let Err(e) = self.send_key_package_to(member, false) {
1199                debug!(
1200                    member = %member,
1201                    error = %e,
1202                    "Rich-capability backfill key package deferred (peer unreachable)"
1203                );
1204            }
1205        }
1206    }
1207
1208    /// Wraps plaintext content and rich extras into the sealed
1209    /// `__RICH_V1__`-prefixed JSON body ([`RichPayloadV1`]). Callers must
1210    /// only invoke this on a path that MLS-encrypts the result — the sealed
1211    /// body is the sole carrier for reply context and media secrets and must
1212    /// never leave as cleartext.
1213    ///
1214    /// The outer `content_type` hint is always copied into the body (not
1215    /// just when non-Text) so the receiver can treat the sealed copy as
1216    /// authoritative and a relay cannot rewrite the hint in either
1217    /// direction on a rich message.
1218    pub(crate) fn seal_rich_payload(
1219        content: &str,
1220        extras: &RichSendExtras,
1221        content_type: ContentType,
1222    ) -> Result<String> {
1223        let payload = RichPayloadV1 {
1224            text: content.to_string(),
1225            reply_context: extras.reply_context.clone(),
1226            media_metadata: extras.media_metadata.clone(),
1227            forward_info: extras.forward_info.clone(),
1228            content_type: Some(content_type),
1229        };
1230        let serialized =
1231            serde_json::to_string(&payload).map_err(|e| Error::Serialization(e.to_string()))?;
1232        Ok(format!("{}{}", internal_prefixes::RICH_V1, serialized))
1233    }
1234
1235    /// Encrypts a plaintext for a recipient whose session is known to be
1236    /// confirmed (in-memory cache hit). Uses `encrypt_for_existing_session` to
1237    /// skip both the external `has_session()` and the internal one inside
1238    /// `encrypt_for_user`, reducing storage I/O from 2 round-trips to 1
1239    /// (`load_group` for encrypt).
1240    ///
1241    /// Only evicts the cache on `SessionNotFound` (session deleted externally).
1242    /// Transient errors (crypto, storage I/O) propagate without cache eviction
1243    /// so the fast path is preserved for the next attempt.
1244    pub(super) fn encrypt_bytes_confirmed_session(
1245        &mut self,
1246        mls: &Arc<RwLock<MlsManager>>,
1247        recipient: &str,
1248        plaintext: &[u8],
1249    ) -> Result<EncryptedMessage> {
1250        let encrypt_result = {
1251            let manager = mls
1252                .read()
1253                .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
1254            manager.encrypt_for_existing_session(recipient, plaintext)
1255        };
1256
1257        let encrypted = match encrypt_result {
1258            Ok(enc) => enc,
1259            Err(offline_protocol_mls::MlsError::SessionNotFound(_)) => {
1260                // Session was deleted externally — evict stale cache entry and
1261                // return SessionNotReady so the send pipeline can queue the
1262                // message (when store_pending is enabled) rather than dropping it.
1263                warn!(
1264                    recipient = %recipient,
1265                    "Confirmed session missing from MLS storage, evicting cache"
1266                );
1267                self.confirmed_sessions.remove(recipient);
1268                let state = self
1269                    .establishment_state(recipient)
1270                    .unwrap_or(crate::EstablishmentState::NoKeyPackage);
1271                return Err(Error::SessionNotReady(state));
1272            }
1273            Err(e) => {
1274                // Transient error (crypto, storage I/O, etc.) — do NOT evict
1275                // cache, the session likely still exists.
1276                return Err(Error::EncryptFailed(format!(
1277                    "encryption operation failed: {}",
1278                    e
1279                )));
1280            }
1281        };
1282
1283        self.emit_mls_encryption_used(recipient);
1284        Ok(encrypted)
1285    }
1286
1287    pub(super) fn encrypt_content_for_recipient_strict(
1288        &mut self,
1289        recipient: &str,
1290        content: &str,
1291    ) -> Result<String> {
1292        let encrypted = self.encrypt_bytes_for_recipient_strict(recipient, content.as_bytes())?;
1293        self.seal_encrypted_content(recipient, &encrypted)
1294    }
1295
1296    /// Bytes-level core of [`Self::encrypt_content_for_recipient_strict`]:
1297    /// requires an existing confirmed session and never initiates
1298    /// establishment.
1299    pub(super) fn encrypt_bytes_for_recipient_strict(
1300        &mut self,
1301        recipient: &str,
1302        plaintext: &[u8],
1303    ) -> Result<EncryptedMessage> {
1304        let mls = self.mls_manager.clone().ok_or_else(|| {
1305            self.emit_mls_session_missing(
1306                Some(recipient),
1307                None,
1308                MlsOperationContext::SessionLookup,
1309                MlsErrorCategory::NotInitialized,
1310            );
1311            Error::MlsNotInitialized
1312        })?;
1313
1314        // Fast path: if session is confirmed (in-memory cache), skip has_session() I/O
1315        if self.confirmed_sessions.contains(recipient) {
1316            return self.encrypt_bytes_confirmed_session(&mls, recipient, plaintext);
1317        }
1318
1319        let has_session = {
1320            let manager = mls
1321                .read()
1322                .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
1323            manager.has_session(recipient)?
1324        };
1325
1326        if !has_session {
1327            self.try_load_key_package_from_storage_into_memory(recipient);
1328            let now_ms = Utc::now().timestamp_millis() as u64;
1329            let has_valid_key_package = match self.pending_key_packages.get(recipient) {
1330                Some(pkg) if now_ms < pkg.local_expires_at_ms => true,
1331                Some(_) => {
1332                    self.pending_key_packages.remove(recipient);
1333                    self.delete_peer_key_package_from_storage(recipient);
1334                    false
1335                }
1336                None => false,
1337            };
1338
1339            if has_valid_key_package {
1340                return Err(Error::SessionNotReady(self.establishment_state(recipient)?));
1341            }
1342
1343            self.emit_mls_session_missing(
1344                Some(recipient),
1345                None,
1346                MlsOperationContext::SessionLookup,
1347                MlsErrorCategory::SessionStateMissing,
1348            );
1349            return Err(Error::SessionNotReady(self.establishment_state(recipient)?));
1350        }
1351
1352        if !self.is_session_confirmed(recipient)? {
1353            return Err(Error::SessionNotReady(self.establishment_state(recipient)?));
1354        }
1355
1356        let encrypted = {
1357            let manager = mls
1358                .read()
1359                .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
1360            manager
1361                .encrypt_for_user(recipient, plaintext)
1362                .map_err(|_| Error::EncryptFailed("encryption operation failed".to_string()))?
1363        };
1364
1365        self.emit_mls_encryption_used(recipient);
1366        Ok(encrypted)
1367    }
1368
1369    #[allow(clippy::too_many_arguments)]
1370    pub(super) fn prepare_outbound_content(
1371        &mut self,
1372        recipient: &str,
1373        content: &str,
1374        priority: MessagePriority,
1375        reply_to_msg_id: Option<MessageId>,
1376        forwarded_from: Option<ForwardInfo>,
1377        content_type: ContentType,
1378        media_metadata: Option<MediaMetadata>,
1379        rich: Option<&RichSendExtras>,
1380        provenance: Option<PendingProvenance>,
1381        reconciliation_reason: &'static str,
1382    ) -> Result<OutboundSendPreparation> {
1383        if self.should_auto_encrypt() {
1384            // Rich extras travel only inside the sealed body: seal for a
1385            // capable recipient, drop for everyone else — never cleartext.
1386            // The decision is made here, at actual-send time; a message
1387            // queued behind session establishment stores the raw extras and
1388            // re-evaluates capability at flush (the key package that
1389            // confirms the session may be the one that advertised it).
1390            let rich_extras = rich.filter(|extras| extras.is_any());
1391            let sealed_body;
1392            let outbound: &str = match rich_extras {
1393                Some(extras) if self.rich_seal_active(recipient) => {
1394                    sealed_body = Self::seal_rich_payload(content, extras, content_type)?;
1395                    &sealed_body
1396                }
1397                Some(_) => {
1398                    debug!(
1399                        recipient = %recipient,
1400                        "Recipient lacks sealed rich payload capability, dropping rich extras"
1401                    );
1402                    content
1403                }
1404                // No rich extras, but a non-Text content_type toward a
1405                // capable recipient still seals a hint-only body: the outer
1406                // hint is relay-writable, and restamping it FileChunk gets
1407                // the decrypted message ACKed and then dropped by the
1408                // file-transfer manager — worse than a plain relay drop,
1409                // which at least fails the ACK and retries. Only when
1410                // nothing rides outer (`forwarded_from`/`media_metadata`
1411                // both absent): fresh forwards thread their attribution and
1412                // media metadata through `rich` above, so this outer-only
1413                // shape survives solely in messages queued by an older build
1414                // (persisted `PendingMessage`s with no `rich`), where the
1415                // receiver's wholesale sealed-body restore would wipe the
1416                // outer copies a hint-only seal can't replicate. Bare Text
1417                // seals nothing: no hint to protect, and the unsealed form
1418                // keeps the plaintext floor maximal.
1419                None if content_type != ContentType::Text
1420                    && forwarded_from.is_none()
1421                    && media_metadata.is_none()
1422                    && self.rich_seal_active(recipient) =>
1423                {
1424                    sealed_body =
1425                        Self::seal_rich_payload(content, &RichSendExtras::default(), content_type)?;
1426                    &sealed_body
1427                }
1428                None => content,
1429            };
1430            if self.config.encryption.require_encryption {
1431                match self.encrypt_content_for_recipient_strict(recipient, outbound) {
1432                    Ok(encrypted) => return Ok(OutboundSendPreparation::Ready(encrypted)),
1433                    Err(Error::SessionNotReady(_)) if self.config.encryption.store_pending => {
1434                        // Session not ready but store_pending is enabled — queue
1435                        // the message so it gets encrypted and sent once the
1436                        // session is confirmed, rather than dropping it.
1437                        //
1438                        // Kick establishment first (import a stored key package,
1439                        // create the session, send the Welcome), matching the
1440                        // non-strict and media paths: without this a peer whose
1441                        // key package arrived but whose arrival-time
1442                        // auto-establish failed would stall until the peer
1443                        // initiates. SessionNotReady from the kick is the
1444                        // expected "created, awaiting confirmation" outcome;
1445                        // the message queues regardless.
1446                        if let Some(mls) = self.mls_manager.clone() {
1447                            match self.ensure_session_establishment(&mls, recipient) {
1448                                Ok(()) | Err(Error::SessionNotReady(_)) => {}
1449                                Err(err) => {
1450                                    warn!(
1451                                        recipient = %recipient,
1452                                        error = %err,
1453                                        "Session establishment kick failed; message queued anyway"
1454                                    );
1455                                }
1456                            }
1457                        }
1458                        let queued_id = self.queue_message_for_session_establishment(
1459                            recipient,
1460                            content,
1461                            priority,
1462                            reply_to_msg_id,
1463                            forwarded_from,
1464                            content_type,
1465                            media_metadata,
1466                            rich_extras.cloned(),
1467                            provenance,
1468                            reconciliation_reason,
1469                        )?;
1470                        return Ok(OutboundSendPreparation::Queued(queued_id));
1471                    }
1472                    Err(e) => return Err(e),
1473                }
1474            }
1475
1476            match self.encrypt_content_for_recipient(recipient, outbound, priority) {
1477                Ok(encrypted) => Ok(OutboundSendPreparation::Ready(encrypted)),
1478                Err(Error::SessionNotReady(state)) => {
1479                    if !self.config.encryption.store_pending {
1480                        return Err(Error::SessionNotReady(state));
1481                    }
1482
1483                    let queued_id = self.queue_message_for_session_establishment(
1484                        recipient,
1485                        content,
1486                        priority,
1487                        reply_to_msg_id,
1488                        forwarded_from,
1489                        content_type,
1490                        media_metadata,
1491                        rich_extras.cloned(),
1492                        provenance,
1493                        reconciliation_reason,
1494                    )?;
1495                    Ok(OutboundSendPreparation::Queued(queued_id))
1496                }
1497                Err(e) => Err(e),
1498            }
1499        } else if self.config.encryption.require_encryption {
1500            Err(Error::EncryptFailed(
1501                "MLS encryption is required (the default) but MLS is not initialized — \
1502                 call initialize_mls() with secure and protocol-state storage, or explicitly opt out with \
1503                 require_encryption=false to send plaintext"
1504                    .to_string(),
1505            ))
1506        } else {
1507            // Explicit opt-out: require_encryption=false with encryption
1508            // disabled or MLS uninitialized. The message leaves as plaintext;
1509            // surface that loudly (once per peer) instead of a debug log.
1510            // Rich extras are dropped here unconditionally — they only ever
1511            // travel inside the sealed body, never as cleartext.
1512            if rich.is_some_and(|extras| extras.is_any()) {
1513                debug!(
1514                    recipient = %recipient,
1515                    "Plaintext send path, dropping rich extras (sealed-only)"
1516                );
1517            }
1518            self.warn_plaintext_send(recipient);
1519            Ok(OutboundSendPreparation::Ready(content.to_string()))
1520        }
1521    }
1522
1523    // ========================================================================
1524    // PENDING / FLUSH
1525    // ========================================================================
1526
1527    /// Enforces [`MAX_MESSAGE_CONTENT_BYTES`] on application-supplied content.
1528    ///
1529    /// Enforced at the public send boundary rather than at transmit time,
1530    /// because a message that has to wait for MLS session establishment is
1531    /// queued — in memory and on disk — long before it ever reaches the
1532    /// transport's own size check. Without this cap a handful of very large
1533    /// sends could exhaust mobile memory and protocol-state storage while
1534    /// still satisfying the pending queue's entry-count limits.
1535    ///
1536    /// Applies to the plaintext the app hands us. MLS ciphertext, base64, and
1537    /// the JSON wire envelope expand it further, which is why the cap sits
1538    /// well under the transport's 1 MiB ceiling: everything accepted here can
1539    /// actually be delivered.
1540    ///
1541    /// Deliberately *not* applied to the group surface: a group send has no
1542    /// durable pre-session queue behind it (there is no per-recipient MLS
1543    /// session to wait on), so it is bounded by the transport alone — the
1544    /// documented "no send-side limit" behavior that
1545    /// `test_group_mls_send_large_content_no_send_side_limit` pins.
1546    pub(crate) fn check_content_size(content: &str) -> Result<()> {
1547        if content.len() > MAX_MESSAGE_CONTENT_BYTES {
1548            return Err(Error::InvalidArgument(format!(
1549                "Message content too large: {} bytes (max {}); use send_media for large payloads",
1550                content.len(),
1551                MAX_MESSAGE_CONTENT_BYTES
1552            )));
1553        }
1554        Ok(())
1555    }
1556
1557    #[allow(clippy::too_many_arguments)]
1558    pub(super) fn queue_message_for_session_establishment(
1559        &mut self,
1560        recipient: &str,
1561        content: &str,
1562        priority: MessagePriority,
1563        reply_to_msg_id: Option<MessageId>,
1564        forwarded_from: Option<ForwardInfo>,
1565        content_type: ContentType,
1566        media_metadata: Option<MediaMetadata>,
1567        rich: Option<RichSendExtras>,
1568        provenance: Option<PendingProvenance>,
1569        reconciliation_reason: &'static str,
1570    ) -> Result<MessageId> {
1571        // Fresh sends mint an ID without ticking the Lamport clock — the
1572        // real tick happens when flush_pending_messages re-sends after the
1573        // session is established. A flush-time re-queue passes the id the
1574        // caller already holds so Deferred/Sent/Delivered stay correlatable,
1575        // and the timestamp it first entered the queue so the absolute
1576        // pending lifetime is not renewed by the round trip.
1577        let (message_id, first_queued_at) = match provenance {
1578            Some(provenance) => (provenance.message_id, provenance.first_queued_at),
1579            None => (MessageId::default(), None),
1580        };
1581
1582        debug!(
1583            recipient = %recipient,
1584            message_id = %message_id,
1585            "Message queued pending session establishment"
1586        );
1587        self.queue_pending_message_at(
1588            recipient,
1589            content,
1590            priority,
1591            message_id.clone(),
1592            reply_to_msg_id,
1593            forwarded_from,
1594            content_type,
1595            media_metadata,
1596            rich,
1597            first_queued_at.unwrap_or_else(Utc::now),
1598        );
1599        self.kick_pending_session_reconciliation(reconciliation_reason);
1600        if self.has_terminal_welcome_failure(recipient) {
1601            self.abort_pending_session_for_peer(
1602                recipient,
1603                crate::events::WelcomeReasonCode::RetryExhausted,
1604            );
1605            return Err(Error::Other(format!(
1606                "Welcome delivery failed for {}",
1607                recipient
1608            )));
1609        }
1610
1611        Ok(message_id)
1612    }
1613
1614    /// Queues a message with a specific message ID for later sending when
1615    /// session is established, starting a fresh pending lifetime.
1616    ///
1617    /// A re-queue must go through [`Self::queue_pending_message_at`] with the
1618    /// entry's original timestamp instead — which is why production code always
1619    /// calls that one directly (through
1620    /// `queue_message_for_session_establishment`, where the distinction is
1621    /// made) and this convenience exists only for tests.
1622    #[cfg(test)]
1623    #[allow(clippy::too_many_arguments)]
1624    pub(super) fn queue_pending_message(
1625        &mut self,
1626        recipient: &str,
1627        content: &str,
1628        priority: MessagePriority,
1629        message_id: MessageId,
1630        reply_to_msg: Option<MessageId>,
1631        forwarded_from: Option<ForwardInfo>,
1632        content_type: ContentType,
1633        media_metadata: Option<MediaMetadata>,
1634        rich: Option<RichSendExtras>,
1635    ) {
1636        self.queue_pending_message_at(
1637            recipient,
1638            content,
1639            priority,
1640            message_id,
1641            reply_to_msg,
1642            forwarded_from,
1643            content_type,
1644            media_metadata,
1645            rich,
1646            Utc::now(),
1647        );
1648    }
1649
1650    /// Queues a message with an explicit first-queued timestamp.
1651    ///
1652    /// `queued_at` is the instant the message *first* entered the pending
1653    /// queue, which is what `pending_message_max_lifetime_ms` is measured from.
1654    /// Re-queueing an entry (a flush that found the session still unavailable)
1655    /// must pass the original value: stamping `now()` there would hand the
1656    /// message a fresh window on every failed reconciliation, so a message
1657    /// could outlive an "absolute" lifetime indefinitely.
1658    ///
1659    /// Admission is bounded on two axes — entry count and serialized bytes,
1660    /// per peer and globally — because message content is
1661    /// application-supplied and a count alone bounds neither memory nor
1662    /// durable storage. Both axes evict oldest-first and settle each evicted
1663    /// message as failed.
1664    #[allow(clippy::too_many_arguments)]
1665    pub(super) fn queue_pending_message_at(
1666        &mut self,
1667        recipient: &str,
1668        content: &str,
1669        priority: MessagePriority,
1670        message_id: MessageId,
1671        reply_to_msg: Option<MessageId>,
1672        forwarded_from: Option<ForwardInfo>,
1673        content_type: ContentType,
1674        media_metadata: Option<MediaMetadata>,
1675        rich: Option<RichSendExtras>,
1676        queued_at: DateTime<Utc>,
1677    ) {
1678        let message_id_str = message_id.as_str().to_string();
1679        let mut pending = PendingMessage {
1680            content: content.to_string(),
1681            priority,
1682            message_id,
1683            reply_to_msg,
1684            forwarded_from,
1685            content_type,
1686            media_metadata,
1687            rich,
1688            queued_at,
1689            serialized_bytes: 0,
1690        };
1691        pending.measure();
1692        let incoming_bytes = pending.serialized_bytes;
1693
1694        let mut evicted = Vec::new();
1695
1696        while self
1697            .pending_encrypted_messages
1698            .get(recipient)
1699            .is_some_and(|messages| messages.len() >= MAX_PENDING_MESSAGES_PER_PEER)
1700        {
1701            if let Some(message) = self.evict_pending_message_at(recipient, 0) {
1702                evicted.push(message);
1703            } else {
1704                break;
1705            }
1706        }
1707
1708        // The running totals are decremented as entries are evicted rather
1709        // than recomputed each turn, so a queue at capacity does not make
1710        // admission quadratic.
1711        let mut peer_bytes = self.pending_message_bytes_for(recipient);
1712        while peer_bytes.saturating_add(incoming_bytes) > MAX_PENDING_MESSAGE_BYTES_PER_PEER {
1713            let Some(message) = self.evict_pending_message_at(recipient, 0) else {
1714                break;
1715            };
1716            peer_bytes = peer_bytes.saturating_sub(message.serialized_bytes);
1717            evicted.push(message);
1718        }
1719
1720        while self.total_pending_message_count() >= MAX_PENDING_MESSAGES_GLOBAL {
1721            let Some((_, message)) = self.evict_oldest_pending_message() else {
1722                break;
1723            };
1724            evicted.push(message);
1725        }
1726
1727        let mut global_bytes = self.total_pending_message_bytes();
1728        while global_bytes.saturating_add(incoming_bytes) > MAX_PENDING_MESSAGE_BYTES_GLOBAL {
1729            let Some((_, message)) = self.evict_oldest_pending_message() else {
1730                break;
1731            };
1732            global_bytes = global_bytes.saturating_sub(message.serialized_bytes);
1733            evicted.push(message);
1734        }
1735
1736        // Push to in-memory queue first, then persist (the in-memory queue
1737        // is the source of truth; storage is a crash-recovery backup).
1738        //
1739        // One record per message, so this writes the incoming entry and nothing
1740        // else — the queue it joins is already on disk, entry by entry, and the
1741        // entries the caps just evicted are deleted by id rather than rewritten
1742        // around. Which recipients were touched no longer matters.
1743        self.pending_encrypted_messages
1744            .entry(recipient.to_string())
1745            .or_default()
1746            .push(pending);
1747        self.note_pending_message_expiry(queued_at);
1748
1749        if let Some(queued) = self
1750            .pending_encrypted_messages
1751            .get(recipient)
1752            .and_then(|messages| messages.last())
1753        {
1754            self.persist_pending_message(recipient, queued);
1755        }
1756
1757        for message in evicted {
1758            self.delete_pending_message_from_storage(&message.message_id);
1759            self.emit_event(Event::message_failed(
1760                message.message_id,
1761                "Pending session queue capacity exceeded".to_string(),
1762                0,
1763            ));
1764        }
1765
1766        debug!(recipient = %recipient, message_id = %message_id_str, "Queued message pending session establishment");
1767    }
1768
1769    pub(super) fn total_pending_message_count(&self) -> usize {
1770        self.pending_encrypted_messages.values().map(Vec::len).sum()
1771    }
1772
1773    /// Total serialized footprint of the pending-session queue across all
1774    /// peers. Summing precomputed per-entry sizes, so this stays a cheap
1775    /// integer walk rather than re-serializing the queue.
1776    pub(super) fn total_pending_message_bytes(&self) -> usize {
1777        self.pending_encrypted_messages
1778            .values()
1779            .flatten()
1780            .map(|message| message.serialized_bytes)
1781            .sum()
1782    }
1783
1784    /// Serialized footprint of one peer's pending-session queue.
1785    pub(super) fn pending_message_bytes_for(&self, recipient: &str) -> usize {
1786        self.pending_encrypted_messages
1787            .get(recipient)
1788            .map(|messages| {
1789                messages
1790                    .iter()
1791                    .map(|message| message.serialized_bytes)
1792                    .sum()
1793            })
1794            .unwrap_or(0)
1795    }
1796
1797    fn evict_pending_message_at(
1798        &mut self,
1799        recipient: &str,
1800        index: usize,
1801    ) -> Option<PendingMessage> {
1802        let (message, remove_recipient) = {
1803            let messages = self.pending_encrypted_messages.get_mut(recipient)?;
1804            if index >= messages.len() {
1805                return None;
1806            }
1807            let message = messages.remove(index);
1808            (message, messages.is_empty())
1809        };
1810        if remove_recipient {
1811            self.pending_encrypted_messages.remove(recipient);
1812        }
1813        Some(message)
1814    }
1815
1816    pub(super) fn evict_oldest_pending_message(&mut self) -> Option<(String, PendingMessage)> {
1817        let (recipient, index) = self
1818            .pending_encrypted_messages
1819            .iter()
1820            .flat_map(|(recipient, messages)| {
1821                messages
1822                    .iter()
1823                    .enumerate()
1824                    .map(move |(index, message)| (recipient, index, message))
1825            })
1826            .min_by(|left, right| {
1827                left.2
1828                    .queued_at
1829                    .cmp(&right.2.queued_at)
1830                    .then_with(|| left.2.message_id.as_str().cmp(&right.2.message_id.as_str()))
1831            })
1832            .map(|(recipient, index, _)| (recipient.clone(), index))?;
1833        let message = self.evict_pending_message_at(&recipient, index)?;
1834        Some((recipient, message))
1835    }
1836
1837    /// Drops a recipient's whole queue from memory *and* from storage, settling
1838    /// every dropped entry as `MessageFailed`.
1839    ///
1840    /// The storage half is a delete per entry rather than one whole-queue
1841    /// delete, which is the cost side of keying records by message id. Callers
1842    /// are the paths where every queued message for a peer becomes undeliverable
1843    /// at once — blocking, an aborted session.
1844    ///
1845    /// Settlement happens *here* rather than in the callers on purpose. The
1846    /// caller holds ids handed to the app by `send_message*` at queue time, so
1847    /// a silent drop leaves the app waiting forever on ids that will never
1848    /// resolve — the same contract the blocked-recipient branch of
1849    /// [`Self::flush_pending_messages`] and the pending-expiry pass already
1850    /// honour. Returning the messages for the caller to settle is what let all
1851    /// three call sites discard them instead, so there is nothing to discard
1852    /// any more.
1853    pub(super) fn drop_pending_queue_for_peer(&mut self, recipient: &str, reason: &str) -> usize {
1854        let messages = self
1855            .pending_encrypted_messages
1856            .remove(recipient)
1857            .unwrap_or_default();
1858        self.delete_pending_messages_from_storage(messages.iter().map(|m| &m.message_id));
1859        for message in &messages {
1860            self.emit_event(Event::message_failed(
1861                message.message_id.clone(),
1862                reason.to_string(),
1863                0,
1864            ));
1865        }
1866        self.recompute_next_pending_message_expiry();
1867        messages.len()
1868    }
1869
1870    fn pending_message_expiry(queued_at: DateTime<Utc>, lifetime_ms: u64) -> Option<DateTime<Utc>> {
1871        let lifetime_ms = i64::try_from(lifetime_ms).ok()?;
1872        queued_at.checked_add_signed(chrono::Duration::milliseconds(lifetime_ms))
1873    }
1874
1875    fn note_pending_message_expiry(&mut self, queued_at: DateTime<Utc>) {
1876        let Some(expiry) = Self::pending_message_expiry(
1877            queued_at,
1878            self.config
1879                .reliability
1880                .retry
1881                .pending_message_max_lifetime_ms,
1882        ) else {
1883            return;
1884        };
1885        self.next_pending_message_expiry = Some(
1886            self.next_pending_message_expiry
1887                .map_or(expiry, |current| current.min(expiry)),
1888        );
1889    }
1890
1891    pub(super) fn recompute_next_pending_message_expiry(&mut self) {
1892        let lifetime_ms = self
1893            .config
1894            .reliability
1895            .retry
1896            .pending_message_max_lifetime_ms;
1897        self.next_pending_message_expiry = self
1898            .pending_encrypted_messages
1899            .values()
1900            .flat_map(|messages| messages.iter())
1901            .filter_map(|message| Self::pending_message_expiry(message.queued_at, lifetime_ms))
1902            .min();
1903    }
1904
1905    /// Flushes pending messages for a recipient after session is established.
1906    ///
1907    /// Every message flushes through one unified path — prepare (re-making
1908    /// the seal-or-drop decision for rich extras against the recipient's
1909    /// *current* capability; the key package that confirmed this session may
1910    /// have advertised it), create, restore outer fields, then dispatch —
1911    /// and keeps the `message_id` the caller received from `send_message*`
1912    /// at queue time (no event fires on queueing — the returned id is the
1913    /// correlation anchor), so that id matches the eventual
1914    /// `MessageSent`/`MessageDelivered`/`MessageFailed`. A re-queue (session
1915    /// flapped back to not-ready) keeps the id too.
1916    pub(super) fn flush_pending_messages(&mut self, recipient: &str) -> Result<()> {
1917        if let Some(pending) = self.pending_encrypted_messages.remove(recipient) {
1918            // Blocking is bidirectional (we neither receive from nor send
1919            // to): a recipient blocked after messages were queued drops the
1920            // queue outright — retrying would fail with UserBlocked forever.
1921            if self.is_user_blocked(recipient) {
1922                info!(
1923                    recipient = %recipient,
1924                    count = pending.len(),
1925                    "Dropping pending messages for blocked recipient"
1926                );
1927                // The caller holds these ids (returned by `send_message*`
1928                // at queue time) — settle each one so the app isn't left
1929                // waiting on ids that will never resolve.
1930                for msg in &pending {
1931                    self.delete_pending_message_from_storage(&msg.message_id);
1932                    self.emit_event(Event::message_failed(
1933                        msg.message_id.clone(),
1934                        "Recipient blocked".to_string(),
1935                        0,
1936                    ));
1937                }
1938                return Ok(());
1939            }
1940
1941            info!(recipient = %recipient, count = pending.len(), "Flushing pending messages");
1942            let original_order: Vec<String> =
1943                pending.iter().map(|m| m.message_id.as_str()).collect();
1944            let mut remaining = Vec::new();
1945            let now = Utc::now();
1946            let lifetime_ms = self
1947                .config
1948                .reliability
1949                .retry
1950                .pending_message_max_lifetime_ms;
1951
1952            for msg in pending {
1953                // The absolute pending lifetime is enforced here as well as in
1954                // the expiry pass, because that pass is bounded per tick
1955                // (`MAX_PENDING_EXPIRIES_PER_PASS`) — a restore can hand this
1956                // flush entries already past their deadline, and dispatching
1957                // one would settle it `MessageSent` after the lifetime promised
1958                // `message_failed`. Back into `remaining` instead: the entry
1959                // keeps its record, and the next expiry pass settles and
1960                // deletes it as a pair.
1961                if lifetime_expired(now, msg.queued_at, lifetime_ms) {
1962                    remaining.push(msg);
1963                    continue;
1964                }
1965
1966                // With stable ids the deduplicator now guards against a
1967                // double flush (e.g. a stale storage snapshot restored after
1968                // the message already went out): a dedup hit means this id
1969                // was dispatched before — drop it, don't retry forever.
1970                if self.deduplicator.is_duplicate(&msg.message_id) {
1971                    if self.deduplicator.is_exact() {
1972                        // Exact mode: the hit is authoritative — this id was
1973                        // already dispatched and (barring an internal error
1974                        // after the transport accepted it) settled then via
1975                        // MessageSent or MessageDeferred; re-dispatching
1976                        // would duplicate on the wire.
1977                        debug!(
1978                            message_id = %msg.message_id,
1979                            "Pending message already dispatched (dedup hit), dropping"
1980                        );
1981                    } else {
1982                        // Bloom mode: the hit may be a false positive for a
1983                        // message that never went out, and dispatching anyway
1984                        // would error-loop on the same filter. Settle loudly
1985                        // so the app can resend under a fresh id instead of
1986                        // losing the message silently.
1987                        warn!(
1988                            message_id = %msg.message_id,
1989                            "Pending message dropped on probabilistic dedup hit"
1990                        );
1991                        self.emit_event(Event::message_failed(
1992                            msg.message_id.clone(),
1993                            "Duplicate suppressed at flush (probabilistic dedup; possible false positive)".to_string(),
1994                            0,
1995                        ));
1996                    }
1997                    // Leaves the queue for good either way, so its record goes
1998                    // with it. Only the entries that end up back in `remaining`
1999                    // below keep theirs — those were never deleted, and their
2000                    // contents have not changed, so re-queuing them costs no
2001                    // write at all.
2002                    self.delete_pending_message_from_storage(&msg.message_id);
2003                    continue;
2004                }
2005
2006                let final_content = match self.prepare_outbound_content(
2007                    recipient,
2008                    &msg.content,
2009                    msg.priority,
2010                    msg.reply_to_msg.clone(),
2011                    msg.forwarded_from.clone(),
2012                    msg.content_type,
2013                    msg.media_metadata.clone(),
2014                    msg.rich.as_ref(),
2015                    Some(PendingProvenance::requeued(&msg)),
2016                    "flush_pending",
2017                ) {
2018                    Ok(OutboundSendPreparation::Ready(c)) => c,
2019                    Ok(OutboundSendPreparation::Queued(message_id)) => {
2020                        // Re-queued for later (session still not ready),
2021                        // keeping the original id.
2022                        debug!(message_id = %message_id, "Pending message re-queued");
2023                        continue;
2024                    }
2025                    Err(e) => {
2026                        warn!(message_id = %msg.message_id, error = %e, "Failed to prepare pending message");
2027                        remaining.push(msg);
2028                        continue;
2029                    }
2030                };
2031
2032                let message = match self.create_message(
2033                    recipient,
2034                    final_content,
2035                    Some(msg.priority),
2036                    msg.reply_to_msg.clone(),
2037                ) {
2038                    Ok(mut message) => {
2039                        // Restore outer fields: forward attribution and its
2040                        // media_metadata ride outer as the fallback for
2041                        // non-capable recipients (secrets stripped at the
2042                        // wire); when `msg.rich` sealed above, the receiver's
2043                        // sealed-body restore overwrites these wholesale.
2044                        // Plain sends store None for both — their rich
2045                        // copies are sealed or dropped by prepare above.
2046                        message.forwarded_from = msg.forwarded_from.clone();
2047                        message.content_type = msg.content_type;
2048                        message.media_metadata = msg.media_metadata.clone();
2049                        message.id = msg.message_id.clone();
2050                        message
2051                    }
2052                    Err(e) => {
2053                        warn!(message_id = %msg.message_id, error = %e, "Failed to create pending message");
2054                        remaining.push(msg);
2055                        continue;
2056                    }
2057                };
2058
2059                // Tier 2 provenance: a flushed pending message that sealed
2060                // carries its plaintext forward so later resends re-seal against
2061                // the peer's current session (see `send_message_with`).
2062                if message.content.starts_with(internal_prefixes::ENCRYPTED) {
2063                    self.stage_outbox_reseal(
2064                        &message.id,
2065                        OutboxReseal {
2066                            content: msg.content.clone(),
2067                            priority: msg.priority,
2068                            reply_to_msg: msg.reply_to_msg.clone(),
2069                            forwarded_from: msg.forwarded_from.clone(),
2070                            content_type: msg.content_type,
2071                            media_metadata: msg.media_metadata.clone(),
2072                            rich: msg.rich.clone(),
2073                        },
2074                    );
2075                }
2076
2077                match self.dispatch_prepared_message(message) {
2078                    Ok(id) => {
2079                        debug!(message_id = %id, "Sent pending message");
2080                        // Handed off to the outbox, which persists its own
2081                        // record under the same id — so the pending copy has to
2082                        // go, or a restart would restore and re-flush it.
2083                        self.delete_pending_message_from_storage(&id);
2084                    }
2085                    Err(e) => {
2086                        warn!(message_id = %msg.message_id, error = %e, "Failed to send pending message");
2087                        remaining.push(msg);
2088                    }
2089                }
2090            }
2091
2092            // Messages that re-queued above re-inserted themselves into the
2093            // map (and rewrote their own record) — merge failures in without
2094            // clobbering the entry. Nothing to clear when neither survives: each
2095            // entry that left the queue already took its own record with it.
2096            if remaining.is_empty() {
2097                return Ok(());
2098            }
2099            if self.has_terminal_welcome_failure(recipient) {
2100                // A prepare failure above came from a terminal Welcome
2101                // failure: abort_pending_session_for_peer already cleared
2102                // the queue and its storage and settled the peer with
2103                // secure_session_failed — re-inserting `remaining` would
2104                // resurrect messages the abort just settled, on every
2105                // future flush, forever.
2106                //
2107                // The abort could only delete the records of entries still in
2108                // the map, and this flush took the queue out of it at the top,
2109                // so these records are still on disk and have to go here or the
2110                // next launch restores exactly what the abort settled.
2111                debug!(
2112                    recipient = %recipient,
2113                    count = remaining.len(),
2114                    "Dropping flush failures for aborted pending session"
2115                );
2116                for msg in &remaining {
2117                    self.delete_pending_message_from_storage(&msg.message_id);
2118                }
2119            } else {
2120                // Every other path into `pending_encrypted_messages` notes the
2121                // entry's deadline. This one is a *re*-insertion of entries the
2122                // queue already held, so today the cached minimum is still
2123                // correct without it — but only because nothing recomputes that
2124                // cache while a flush is in progress. Noting them keeps
2125                // "`next_pending_message_expiry` is never later than the true
2126                // minimum" an invariant of the code rather than of the call
2127                // graph: if it ever broke, these entries would stop being
2128                // scanned and would never expire, silently, on the one queue the
2129                // absolute pending lifetime exists to bound.
2130                let merged_deadlines: Vec<DateTime<Utc>> =
2131                    remaining.iter().map(|m| m.queued_at).collect();
2132                let queue = self
2133                    .pending_encrypted_messages
2134                    .entry(recipient.to_string())
2135                    .or_default();
2136                queue.extend(remaining);
2137                // A failure that preceded a mid-flush re-queue would land
2138                // behind it — restore the original queue order (stable
2139                // sort; every id here came from this flush's queue).
2140                queue.sort_by_key(|m| {
2141                    original_order
2142                        .iter()
2143                        .position(|id| *id == m.message_id.as_str())
2144                        .unwrap_or(usize::MAX)
2145                });
2146                for queued_at in merged_deadlines {
2147                    self.note_pending_message_expiry(queued_at);
2148                }
2149                // No write: these entries never left the queue durably. Their
2150                // records were written at enqueue, were not deleted above, and
2151                // their contents have not changed — only their position in the
2152                // in-memory queue, which the restore ordering rebuilds from
2153                // `queued_at` rather than from storage order.
2154            }
2155        }
2156        Ok(())
2157    }
2158
2159    pub(super) fn flush_restored_confirmed_pending_messages(&mut self) {
2160        let recipients: Vec<String> = self.pending_encrypted_messages.keys().cloned().collect();
2161
2162        for recipient in recipients {
2163            match self.is_session_confirmed(&recipient) {
2164                Ok(true) => {
2165                    if let Err(err) = self.flush_pending_messages(&recipient) {
2166                        warn!(
2167                            recipient = %recipient,
2168                            error = %err,
2169                            "Failed to flush restored pending messages for confirmed session"
2170                        );
2171                    }
2172                }
2173                Ok(false) => {}
2174                Err(err) => {
2175                    warn!(
2176                        recipient = %recipient,
2177                        error = %err,
2178                        "Failed to read session confirmation state while restoring pending messages"
2179                    );
2180                }
2181            }
2182        }
2183    }
2184
2185    // ========================================================================
2186    // MEDIA
2187    // ========================================================================
2188
2189    /// Sends a media attachment (image, video, audio, file, etc.) to a recipient.
2190    ///
2191    /// The file data is chunked and each chunk is sent as an individual message
2192    /// with `content_type: FileChunk`. The first chunk carries the full
2193    /// `MediaMetadata` so the receiver can display a preview before all chunks
2194    /// arrive. Individual chunk messages require ACKs and participate in retry
2195    /// logic so delivery is tracked and recoverable per chunk. `MediaSent` is
2196    /// emitted only after all chunks are ACKed.
2197    ///
2198    /// # Encryption
2199    ///
2200    /// With auto-encryption active (encryption enabled and MLS initialized),
2201    /// chunk bytes — and the chunk-0 media metadata and original content type —
2202    /// travel inside MLS ciphertext, using the same session the text path uses.
2203    /// The session must already be confirmed: media is never queued pending
2204    /// establishment and never falls back to plaintext. When the session is
2205    /// not ready this returns [`Error::SessionNotReady`] (after kicking
2206    /// establishment if a key package is available); retry after the
2207    /// `secure_session_established` event. When encryption is required but MLS
2208    /// is not initialized this returns [`Error::EncryptFailed`]. Plaintext
2209    /// chunks are sent only when auto-encryption is inactive AND
2210    /// `require_encryption` was explicitly set to `false` (it defaults to
2211    /// `true`): the explicit encryption opt-out, or encryption enabled but
2212    /// MLS never initialized (matching the text path). Every plaintext
2213    /// transfer emits a
2214    /// [`crate::events::SecurityWarningCode::PlaintextSend`] warning, once
2215    /// per peer.
2216    ///
2217    /// Returns a `file_id` that can be used to track progress or cancel.
2218    pub fn send_media(
2219        &mut self,
2220        recipient: impl Into<String>,
2221        file_data: Vec<u8>,
2222        file_name: impl Into<String>,
2223        content_type: ContentType,
2224        media_metadata: Option<MediaMetadata>,
2225    ) -> Result<String> {
2226        self.send_media_with(
2227            recipient,
2228            file_data,
2229            file_name,
2230            content_type,
2231            MediaSendOptions {
2232                media_metadata,
2233                ..Default::default()
2234            },
2235        )
2236    }
2237
2238    /// Sends a media attachment with rich options: the chunk-0 media
2239    /// metadata (as on [`Self::send_media`]), plus caption, reply threading,
2240    /// quoted-reply context, forward attribution, and an optional
2241    /// caller-supplied `file_id`.
2242    ///
2243    /// The rich fields only ever travel inside the MLS-sealed chunk-0
2244    /// plaintext (under the v2 media envelope), and only toward recipients
2245    /// whose key package advertised `rich_versions` support (gated by
2246    /// `EncryptionConfig::rich_payload_enabled`). Toward anyone else —
2247    /// including every plaintext (encryption opt-out) transfer — they are
2248    /// silently dropped, never sent cleartext.
2249    ///
2250    /// Returns `InvalidArgument` for an unparsable `reply_to_msg`, rich
2251    /// extras whose serialized size exceeds 32 KiB, or a `file_id` that is
2252    /// empty, over the wire field bound, or already carried by an active
2253    /// outbound transfer. Encryption preconditions match
2254    /// [`Self::send_media`].
2255    pub fn send_media_with(
2256        &mut self,
2257        recipient: impl Into<String>,
2258        file_data: Vec<u8>,
2259        file_name: impl Into<String>,
2260        content_type: ContentType,
2261        options: MediaSendOptions,
2262    ) -> Result<String> {
2263        // The public surface never carries a data purpose. Every transfer
2264        // reachable from an application is a transfer for its user, and the
2265        // one argument that could change that is not in `MediaSendOptions`
2266        // and must never be added to it: a caller able to mark a send as
2267        // data-purposed could feed bytes of its choosing to a peer's
2268        // document engine while that peer's user saw nothing arrive.
2269        self.send_media_inner(recipient, file_data, file_name, content_type, options, None)
2270    }
2271
2272    /// The body of [`Self::send_media_with`], plus the one argument the
2273    /// public surface may not supply.
2274    ///
2275    /// `data_purpose` marks a transfer as belonging to the replicated-
2276    /// document layer. It is `pub(crate)` and reached only from the data
2277    /// layer's own send seams.
2278    #[cfg_attr(not(feature = "data"), allow(dead_code))]
2279    pub(crate) fn send_media_inner(
2280        &mut self,
2281        recipient: impl Into<String>,
2282        file_data: Vec<u8>,
2283        file_name: impl Into<String>,
2284        content_type: ContentType,
2285        options: MediaSendOptions,
2286        data_purpose: Option<crate::media_envelope::DataPurpose>,
2287    ) -> Result<String> {
2288        {
2289            let state = lock_shared_state(&self.shared_state)?;
2290            if state.state != ProtocolState::Running {
2291                return Err(Error::NotStarted);
2292            }
2293        }
2294
2295        let recipient_str: String = recipient.into();
2296        let file_name_str: String = file_name.into();
2297        let media_metadata = options.media_metadata;
2298        Self::validate_outbound_recipient(&recipient_str)?;
2299
2300        // Validate the reply id like send_message_with does — a malformed id
2301        // fails the call instead of riding sealed to the receiver.
2302        if let Some(reply_to) = options.reply_to_msg.as_deref() {
2303            MessageId::from_str(reply_to)
2304                .map_err(|e| Error::InvalidArgument(format!("Invalid reply_to_msg: {}", e)))?;
2305        }
2306
2307        let rich_extras = MediaRichExtras {
2308            caption: options.caption,
2309            reply_to_msg: options.reply_to_msg,
2310            reply_context: options.reply_context,
2311            forward_info: options.forward_info,
2312        };
2313        // Same boundary cap as send_message_with: an oversized quote or
2314        // caption would inflate the sealed chunk-0 plaintext into heavy
2315        // fragmentation. Enforced before the capability gate so the caller
2316        // hears about it even when the extras would be dropped.
2317        if rich_extras.is_any() {
2318            let extras_len = serde_json::to_vec(&rich_extras)
2319                .map_err(|e| Error::Serialization(e.to_string()))?
2320                .len();
2321            if extras_len > MAX_RICH_EXTRAS_BYTES {
2322                return Err(Error::InvalidArgument(format!(
2323                    "Rich extras too large: {} bytes serialized (max {})",
2324                    extras_len, MAX_RICH_EXTRAS_BYTES
2325                )));
2326            }
2327        }
2328
2329        if let Some(file_id) = options.file_id.as_deref() {
2330            if file_id.is_empty() || file_id.len() > FileChunk::MAX_STRING_FIELD_LEN {
2331                return Err(Error::InvalidArgument(format!(
2332                    "file_id length must be 1..={} bytes",
2333                    FileChunk::MAX_STRING_FIELD_LEN
2334                )));
2335            }
2336            if self.outbound_media_transfers.contains_key(file_id) {
2337                return Err(Error::InvalidArgument(format!(
2338                    "file_id {} already has an active outbound transfer",
2339                    file_id
2340                )));
2341            }
2342            // A restored descriptor under this file_id means this send is
2343            // the app answering MediaResendRequired: it must target the
2344            // original recipient (a redirect would consume the descriptor
2345            // and orphan the interrupted transfer's resend forever) and
2346            // re-supply the original bytes — the receiver may already hold
2347            // chunks of the interrupted attempt, and a silent content swap
2348            // under the same id would fail its integrity check at best.
2349            if let Some(descriptor) = self.restored_media_descriptors.get(file_id) {
2350                if descriptor.recipient != recipient_str {
2351                    return Err(Error::InvalidArgument(format!(
2352                        "file_id {} belongs to an interrupted transfer to a different recipient",
2353                        file_id
2354                    )));
2355                }
2356                use sha2::{Digest, Sha256};
2357                let resupplied_checksum = format!("{:x}", Sha256::digest(&file_data));
2358                if resupplied_checksum != descriptor.file_checksum {
2359                    return Err(Error::InvalidArgument(format!(
2360                        "file_id {} resend bytes do not match the interrupted transfer's checksum",
2361                        file_id
2362                    )));
2363                }
2364            }
2365        }
2366
2367        // Prevent sending media to blocked users. Blocking is bidirectional:
2368        // we neither receive from nor send to a blocked peer.
2369        if self.is_user_blocked(&recipient_str) {
2370            return Err(Error::UserBlocked(recipient_str));
2371        }
2372
2373        // A data-purposed transfer goes only to a peer that said it routes
2374        // one into its document layer. Toward anybody else the bytes would
2375        // be handed to a person as a downloaded file, so this refuses rather
2376        // than degrading: there is no useful weaker form of this send.
2377        if data_purpose.is_some() && !self.data_media_active(&recipient_str) {
2378            return Err(Error::InvalidArgument(format!(
2379                "peer {recipient_str} did not advertise attachment carriage"
2380            )));
2381        }
2382
2383        // And only where it can be sealed. The marking exists nowhere but
2384        // inside the encrypted chunk-0 plaintext, so a transfer leaving by
2385        // the plaintext opt-out path arrives stripped of it: the receiver
2386        // has no way to know what the bytes are and hands its user a
2387        // downloaded file named by a hash, which is the outcome the whole
2388        // marking exists to prevent. Refused rather than degraded, for the
2389        // same reason the capability gate above refuses.
2390        if data_purpose.is_some() && !self.should_auto_encrypt() {
2391            return Err(Error::EncryptFailed(
2392                "a document-layer transfer cannot be sent unsealed: its marking travels \
2393                 inside the encrypted chunk, and a receiver would surface the bytes to its \
2394                 user as an ordinary file"
2395                    .to_string(),
2396            ));
2397        }
2398
2399        // SEC-H1: media rides the same MLS session machinery as text. With
2400        // auto-encryption active the session must already be confirmed —
2401        // files are too large for the pending-message queue, so media is
2402        // never queued pending establishment and never falls back to
2403        // plaintext. Callers should retry after `secure_session_established`.
2404        if self.should_auto_encrypt() {
2405            if !self.is_session_confirmed(&recipient_str)? {
2406                // Kick session establishment exactly like the text path
2407                // (import a stored key package, create the session, send the
2408                // Welcome), then report not-ready: the session cannot be
2409                // confirmed synchronously, so callers retry after
2410                // `secure_session_established`.
2411                if let Some(mls) = self.mls_manager.clone() {
2412                    self.ensure_session_establishment(&mls, &recipient_str)?;
2413                }
2414                return Err(Error::SessionNotReady(
2415                    self.establishment_state(&recipient_str)?,
2416                ));
2417            }
2418
2419            // Encrypted chunks share the recipient's session ratchet with
2420            // text, and the receiver keeps out-of-order message keys for a
2421            // bounded number of generations — cap concurrent transfers so
2422            // the combined in-flight windows cannot push a delayed chunk
2423            // beyond that tolerance and permanently stall it.
2424            use crate::constants::{
2425                MAX_CONCURRENT_INTERNAL_MEDIA_TRANSFERS_PER_PEER,
2426                MAX_CONCURRENT_MEDIA_TRANSFERS_PER_PEER,
2427            };
2428            let active_transfers = self
2429                .outbound_media_transfers
2430                .values()
2431                .filter(|transfer| transfer.recipient == recipient_str)
2432                .count();
2433            if active_transfers >= MAX_CONCURRENT_MEDIA_TRANSFERS_PER_PEER {
2434                return Err(Error::MediaTransferLimit(recipient_str));
2435            }
2436
2437            // An SDK-internal transfer takes at most one of those slots.
2438            // The cap above bounds the ratchet gap and so cannot be raised
2439            // for our own traffic; what is left is to leave the application
2440            // a slot it can account for. The in-flight dedup stops a second
2441            // copy of one transfer, not two different ones, and two
2442            // different ones are ordinary here: a document snapshot and an
2443            // answered blob request are separate errands to the same peer.
2444            // Filling both slots with them fails the app's own send with a
2445            // limit whose cause it cannot see, because what holds the slots
2446            // is invisible to it by design.
2447            if data_purpose.is_some() {
2448                let internal_transfers = self
2449                    .outbound_media_transfers
2450                    .values()
2451                    .filter(|transfer| {
2452                        transfer.recipient == recipient_str && transfer.data_purpose.is_some()
2453                    })
2454                    .count();
2455                if internal_transfers >= MAX_CONCURRENT_INTERNAL_MEDIA_TRANSFERS_PER_PEER {
2456                    return Err(Error::MediaTransferLimit(recipient_str));
2457                }
2458            }
2459        } else if self.config.encryption.require_encryption {
2460            return Err(Error::EncryptFailed(
2461                "MLS encryption is required (the default) but MLS is not initialized — \
2462                 call initialize_mls() with secure and protocol-state storage, or explicitly opt out with \
2463                 require_encryption=false to send plaintext media"
2464                    .to_string(),
2465            ));
2466        } else {
2467            // Explicit opt-out: the whole transfer leaves as legacy plaintext
2468            // chunks. Warn once per peer, not per chunk.
2469            self.warn_plaintext_send(&recipient_str);
2470        }
2471
2472        // Rich extras seal only toward recipients that advertised the rich
2473        // payload, and only on the encrypted path; everyone else — including
2474        // every plaintext (opt-out) transfer — gets the plain transfer.
2475        // Dropped here, never sent cleartext.
2476        let rich_extras = if !rich_extras.is_any() {
2477            None
2478        } else if self.should_auto_encrypt() && self.rich_seal_active(&recipient_str) {
2479            Some(rich_extras)
2480        } else {
2481            debug!(
2482                recipient = %recipient_str,
2483                "Dropping rich media extras: recipient did not advertise sealed rich payload support"
2484            );
2485            None
2486        };
2487
2488        let file_id = options
2489            .file_id
2490            .unwrap_or_else(|| format!("file_{}", MessageId::new().as_str()));
2491        let pinned_transport = self.select_media_transport()?;
2492
2493        let (chunk_size, window_size) = match pinned_transport {
2494            TransportType::BLE => {
2495                use crate::constants::{CHUNK_SIZE_BLE, MEDIA_WINDOW_SIZE_BLE};
2496                (CHUNK_SIZE_BLE, MEDIA_WINDOW_SIZE_BLE)
2497            }
2498            TransportType::Internet => {
2499                use crate::constants::{CHUNK_SIZE_INTERNET, MEDIA_WINDOW_SIZE_INTERNET};
2500                (CHUNK_SIZE_INTERNET, MEDIA_WINDOW_SIZE_INTERNET)
2501            }
2502            TransportType::WiFiDirect => {
2503                use crate::constants::{DEFAULT_CHUNK_SIZE, DEFAULT_MEDIA_WINDOW_SIZE};
2504                (DEFAULT_CHUNK_SIZE, DEFAULT_MEDIA_WINDOW_SIZE)
2505            }
2506            TransportType::Reticulum => {
2507                // Reticulum is low-bandwidth (LoRa); use BLE-like chunk sizes.
2508                use crate::constants::{CHUNK_SIZE_BLE, MEDIA_WINDOW_SIZE_BLE};
2509                (CHUNK_SIZE_BLE, MEDIA_WINDOW_SIZE_BLE)
2510            }
2511            TransportType::Nostr => {
2512                // Nostr relays have decent bandwidth; use Internet-like chunk sizes.
2513                use crate::constants::{CHUNK_SIZE_INTERNET, MEDIA_WINDOW_SIZE_INTERNET};
2514                (CHUNK_SIZE_INTERNET, MEDIA_WINDOW_SIZE_INTERNET)
2515            }
2516        };
2517        let chunks = self.file_transfer_manager.chunk_file(
2518            file_id.clone(),
2519            file_name_str,
2520            file_data,
2521            Some(chunk_size),
2522        )?;
2523
2524        let total_chunks = chunks.len() as u32;
2525        self.outbound_media_transfers.insert(
2526            file_id.clone(),
2527            OutboundMediaTransfer {
2528                content_type,
2529                recipient: recipient_str.clone(),
2530                pinned_transport,
2531                total_chunks,
2532                delivered_chunks: HashSet::new(),
2533                last_updated_at: Instant::now(),
2534                media_metadata: media_metadata.clone(),
2535                rich_extras: rich_extras.clone(),
2536                data_purpose: data_purpose.clone(),
2537            },
2538        );
2539
2540        // Persist the crash-recovery descriptor (no chunk bytes) so a
2541        // restart mid-transfer can tell the app to re-initiate via
2542        // MediaResendRequired. A same-file_id resend consumes the restored
2543        // copy here — its checksum was validated at the boundary above.
2544        if let Some(first_chunk) = chunks.first() {
2545            let descriptor = MediaTransferDescriptor {
2546                file_id: file_id.clone(),
2547                recipient: recipient_str.clone(),
2548                file_name: first_chunk.file_name.clone(),
2549                file_size: first_chunk.file_size,
2550                file_checksum: first_chunk.file_checksum.clone(),
2551                content_type,
2552                queued_at: Utc::now(),
2553                data_purpose: data_purpose.clone(),
2554            };
2555            self.restored_media_descriptors.remove(&file_id);
2556            self.persist_media_descriptor(&descriptor);
2557        }
2558
2559        let mut window_state = OutboundTransferState::new(chunks, window_size);
2560        let initial_batch = window_state.next_chunks_to_send();
2561        self.outbound_media_windows
2562            .insert(file_id.clone(), window_state);
2563
2564        // A data-purposed transfer is invisible to the application on this
2565        // side too. The receiver's user must not be shown a download they did
2566        // not start; the sender's must not be shown an upload they did not
2567        // make, and `MediaSent` for one would put a file in a conversation.
2568        if data_purpose.is_none() {
2569            let state = lock_shared_state(&self.shared_state)?;
2570            state.emit_event(Event::file_progress(file_id.clone(), 0, total_chunks));
2571            drop(state);
2572        }
2573
2574        self.send_media_chunk_batch(
2575            &file_id,
2576            initial_batch,
2577            &recipient_str,
2578            pinned_transport,
2579            content_type,
2580            media_metadata.as_ref(),
2581            rich_extras.as_ref(),
2582            data_purpose.as_ref(),
2583        )?;
2584
2585        Ok(file_id)
2586    }
2587
2588    /// Sends a batch of file chunks, wiring each into the outbox and media tracking.
2589    #[allow(clippy::too_many_arguments)]
2590    pub(super) fn send_media_chunk_batch(
2591        &mut self,
2592        file_id: &str,
2593        chunks: Vec<FileChunk>,
2594        recipient: &str,
2595        pinned_transport: TransportType,
2596        content_type: ContentType,
2597        media_metadata: Option<&MediaMetadata>,
2598        rich_extras: Option<&MediaRichExtras>,
2599        data_purpose: Option<&crate::media_envelope::DataPurpose>,
2600    ) -> Result<()> {
2601        for chunk in chunks {
2602            let chunk_index = chunk.chunk_index;
2603
2604            let meta_for_chunk = if chunk_index == 0 {
2605                media_metadata.cloned()
2606            } else {
2607                None
2608            };
2609
2610            // SEC-H1: with auto-encryption active, chunk bytes AND the chunk-0
2611            // metadata (file name, preview thumbnail, original content type)
2612            // travel inside the MLS ciphertext — the wire Message carries none
2613            // of them. The plaintext fields are only populated when
2614            // auto-encryption is inactive. Rich extras (caption, reply,
2615            // forward) exist only on this sealed path, chunk 0 only; their
2616            // presence bumps the envelope to v2 so a pre-rich receiver
2617            // rejects cleanly instead of misparsing.
2618            let (binary_payload, wire_metadata, wire_original_ct) = if self.should_auto_encrypt() {
2619                let inner = MediaChunkPlaintext {
2620                    chunk_bytes: chunk.to_bytes(),
2621                    media_metadata: meta_for_chunk,
2622                    original_content_type: (chunk_index == 0).then_some(content_type),
2623                    rich_extras: if chunk_index == 0 {
2624                        rich_extras.cloned()
2625                    } else {
2626                        None
2627                    },
2628                    data_purpose: if chunk_index == 0 {
2629                        data_purpose.cloned()
2630                    } else {
2631                        None
2632                    },
2633                };
2634                let envelope_version = inner.envelope_version();
2635                let sealed = inner
2636                    .encode()
2637                    .map_err(Error::Serialization)
2638                    .and_then(|plaintext| {
2639                        self.encrypt_bytes_for_recipient_strict(recipient, &plaintext)
2640                    });
2641                let encrypted = match sealed {
2642                    Ok(encrypted) => encrypted,
2643                    Err(err) => {
2644                        // The chunks this batch popped from the window are
2645                        // marked in-flight with no outbox entry — nothing
2646                        // would ever ACK or retry them, so the transfer can
2647                        // only wedge while holding a per-peer transfer slot.
2648                        // Encryption failure is not per-chunk transient:
2649                        // abort the whole transfer loudly.
2650                        self.abort_outbound_media_transfer(file_id, "chunk encryption failed");
2651                        return Err(err);
2652                    }
2653                };
2654                (
2655                    encode_media_envelope(&encrypted, envelope_version),
2656                    None,
2657                    false,
2658                )
2659            } else {
2660                (chunk.to_bytes(), meta_for_chunk, chunk_index == 0)
2661            };
2662
2663            let mut message = self.create_media_message(
2664                recipient,
2665                String::new(),
2666                ContentType::FileChunk,
2667                wire_metadata,
2668            )?;
2669            message.binary_content = Some(binary_payload);
2670
2671            if wire_original_ct {
2672                use crate::constants::ORIGINAL_CONTENT_TYPE_KEY;
2673                message.metadata.insert(
2674                    ORIGINAL_CONTENT_TYPE_KEY.to_string(),
2675                    content_type.to_string(),
2676                );
2677            }
2678            self.outbound_media_chunks
2679                .insert(message.id.clone(), (file_id.to_string(), chunk_index));
2680
2681            let previous_transport = self.transport_manager.current_transport();
2682            let send_result = self
2683                .transport_manager
2684                .send_via_transport(&message, pinned_transport);
2685            let current_transport = Some(pinned_transport);
2686
2687            match send_result {
2688                Ok(()) => {
2689                    self.handle_send_success(&message, current_transport)?;
2690                    self.emit_transport_switch_event(previous_transport, current_transport)?;
2691                }
2692                Err(err) => {
2693                    let _ = self
2694                        .handle_send_failure(&message, current_transport.or(previous_transport))?;
2695                    // send_via_transport does not record retry failures internally.
2696                    self.transport_manager
2697                        .record_retry_failure(pinned_transport);
2698                    warn!(
2699                        file_id = %file_id,
2700                        chunk_index = chunk_index,
2701                        transport = ?pinned_transport,
2702                        error = %err,
2703                        "File chunk send failed, message deferred"
2704                    );
2705                }
2706            }
2707            if !self.outbound_media_transfers.contains_key(file_id) {
2708                return Err(Error::Other(format!(
2709                    "Media transfer {} could not be scheduled for reliable delivery",
2710                    file_id
2711                )));
2712            }
2713        }
2714        Ok(())
2715    }
2716
2717    /// Pumps all active windowed media transfers, sending the next batch of
2718    /// chunks for any transfer whose window has capacity (previous chunks ACKed).
2719    /// Should be called from the periodic tick/poll loop.
2720    pub(super) fn pump_media_transfers(&mut self) {
2721        let file_ids: Vec<String> = self.outbound_media_windows.keys().cloned().collect();
2722
2723        for file_id in file_ids {
2724            let transfer = match self.outbound_media_transfers.get(&file_id) {
2725                Some(t) => t.clone(),
2726                None => {
2727                    self.outbound_media_windows.remove(&file_id);
2728                    continue;
2729                }
2730            };
2731
2732            let window = match self.outbound_media_windows.get_mut(&file_id) {
2733                Some(w) => w,
2734                None => continue,
2735            };
2736
2737            if !window.has_capacity() {
2738                continue;
2739            }
2740
2741            let batch = window.next_chunks_to_send();
2742            if batch.is_empty() {
2743                continue;
2744            }
2745
2746            if let Err(err) = self.send_media_chunk_batch(
2747                &file_id,
2748                batch,
2749                &transfer.recipient,
2750                transfer.pinned_transport,
2751                transfer.content_type,
2752                transfer.media_metadata.as_ref(),
2753                transfer.rich_extras.as_ref(),
2754                transfer.data_purpose.as_ref(),
2755            ) {
2756                warn!(
2757                    file_id = %file_id,
2758                    error = %err,
2759                    "Failed to pump media transfer chunks"
2760                );
2761            }
2762        }
2763    }
2764
2765    /// Creates a message carrying media content (file chunks, etc.).
2766    ///
2767    /// Like `create_message` but sets `content_type`, `media_metadata`, marks
2768    /// the message as internet-preferred via metadata, and requires per-chunk ACKs.
2769    pub(super) fn create_media_message(
2770        &mut self,
2771        recipient: &str,
2772        content: impl Into<String>,
2773        content_type: ContentType,
2774        media_metadata: Option<MediaMetadata>,
2775    ) -> Result<Message> {
2776        use crate::constants::{TRANSPORT_PREFERENCE_INTERNET, TRANSPORT_PREFERENCE_KEY};
2777
2778        let sender = UserId::new(&self.local_id)?;
2779        let recipient = UserId::new(recipient)?;
2780        let app_id = AppId::new(&self.config.app_id)?;
2781
2782        let clock_value = self.lamport_clock.tick();
2783        self.persist_lamport_clock();
2784
2785        let mut builder = Message::builder(sender, recipient, app_id)
2786            .content(content)
2787            .content_type(content_type)
2788            .priority(MessagePriority::Medium)
2789            .ttl(TTL::new(self.config.initial_ttl)?)
2790            .lamport_clock(clock_value)
2791            .metadata(TRANSPORT_PREFERENCE_KEY, TRANSPORT_PREFERENCE_INTERNET)
2792            .requires_ack(true);
2793
2794        if let Some(meta) = media_metadata {
2795            builder = builder.media_metadata(meta);
2796        }
2797
2798        Ok(builder.build())
2799    }
2800
2801    // ========================================================================
2802    // OUTBOX
2803    // ========================================================================
2804
2805    pub(super) fn ensure_outbox_entry(&mut self, message: &Message) {
2806        if !message.requires_ack {
2807            return;
2808        }
2809
2810        let is_media = Self::is_media_outbox_message(message);
2811        let (outbox, capacity) = if is_media {
2812            use crate::constants::MAX_MEDIA_OUTBOX_ENTRIES;
2813            (&mut self.media_outbox, MAX_MEDIA_OUTBOX_ENTRIES)
2814        } else {
2815            (&mut self.outbox, MAX_OUTBOX_ENTRIES)
2816        };
2817
2818        if !outbox.contains_key(&message.id) && outbox.len() >= capacity {
2819            if let Some((oldest_id, last_transport, attempt_count)) = outbox
2820                .iter()
2821                .min_by_key(|(_, entry)| entry.last_sent_at)
2822                .map(|(id, entry)| (id.clone(), entry.last_transport, entry.attempt_count))
2823            {
2824                if let Some(transport) = last_transport {
2825                    self.transport_manager.record_delivery_failure(transport);
2826                }
2827                let outbox = if is_media {
2828                    &mut self.media_outbox
2829                } else {
2830                    &mut self.outbox
2831                };
2832                outbox.remove(&oldest_id);
2833                if !is_media {
2834                    self.clear_outbox_entry_from_storage(&oldest_id);
2835                }
2836                self.handle_outbound_media_chunk_failed(&oldest_id, "outbox eviction");
2837                if !is_media {
2838                    // Capacity eviction is as terminal as expiry: the entry
2839                    // and its persisted copy are gone, so without an event
2840                    // the app shows the message as pending forever. (Media
2841                    // chunks surface through the transfer abort above.)
2842                    self.emit_event(Event::message_failed(
2843                        oldest_id.clone(),
2844                        "Outbox capacity exceeded".to_string(),
2845                        attempt_count,
2846                    ));
2847                    if let Some(recipient) = self.take_undeliverable_connection_request(&oldest_id)
2848                    {
2849                        warn!(
2850                            recipient = %recipient,
2851                            message_id = %oldest_id,
2852                            "Connection request undeliverable: outbox capacity exceeded"
2853                        );
2854                        self.emit_event(Event::connection_request_undeliverable(
2855                            recipient,
2856                            oldest_id.as_str(),
2857                            "outbox_capacity_exceeded",
2858                        ));
2859                    }
2860                }
2861            }
2862        }
2863
2864        // Consume staged re-seal provenance only when first creating a
2865        // (non-media) entry here (the failure/retry path); later calls find the
2866        // entry already present and leave its `reseal` intact.
2867        let staged_reseal = self.take_staged_reseal(message, is_media);
2868        let outbox = if is_media {
2869            &mut self.media_outbox
2870        } else {
2871            &mut self.outbox
2872        };
2873        let is_new = !outbox.contains_key(&message.id);
2874        outbox
2875            .entry(message.id.clone())
2876            .or_insert_with(|| OutboxEntry {
2877                message: message.clone(),
2878                attempt_count: 0,
2879                first_sent_at: Utc::now(),
2880                last_sent_at: Utc::now(),
2881                last_transport: None,
2882                reseal: staged_reseal,
2883            });
2884        // Persist newly-created main-outbox entries so they survive a restart.
2885        // media entries are intentionally not persisted.
2886        if is_new && !is_media {
2887            if let Some(entry) = self.outbox.get(&message.id) {
2888                self.persist_outbox_entry(entry);
2889            }
2890        }
2891    }
2892
2893    pub(super) fn mark_message_sent(
2894        &mut self,
2895        message: &Message,
2896        transport: Option<TransportType>,
2897        attempt_hint: Option<u32>,
2898    ) {
2899        if !message.requires_ack {
2900            return;
2901        }
2902
2903        let now = Utc::now();
2904        let is_media = Self::is_media_outbox_message(message);
2905        // Consume the staged re-seal provenance only when first creating a
2906        // (non-media) outbox entry; on later attempts the entry already carries
2907        // it. Staging for media is never populated.
2908        let staged_reseal = self.take_staged_reseal(message, is_media);
2909        let outbox = if is_media {
2910            &mut self.media_outbox
2911        } else {
2912            &mut self.outbox
2913        };
2914        let is_new = !outbox.contains_key(&message.id);
2915        let entry = outbox
2916            .entry(message.id.clone())
2917            .or_insert_with(|| OutboxEntry {
2918                message: message.clone(),
2919                attempt_count: 0,
2920                first_sent_at: now,
2921                last_sent_at: now,
2922                last_transport: transport,
2923                reseal: staged_reseal,
2924            });
2925
2926        entry.message = message.clone();
2927        if entry.attempt_count == 0 {
2928            entry.first_sent_at = now;
2929        }
2930        entry.attempt_count = attempt_hint.unwrap_or(entry.attempt_count.saturating_add(1));
2931        entry.last_sent_at = now;
2932        entry.last_transport = transport;
2933
2934        // Persist only when the entry is newly created here. The success path
2935        // never calls ensure_outbox_entry, so this is the one point a
2936        // successfully-sent message first enters the durable outbox before its
2937        // ACK. Later attempts don't re-persist: the durable copy already exists
2938        // and attempt/timestamp churn isn't restore-critical (the TTL is
2939        // refreshed on restore anyway), which keeps secure-storage writes off
2940        // the retry hot path. Media entries are never persisted.
2941        if is_new && !is_media {
2942            if let Some(entry) = self.outbox.get(&message.id) {
2943                self.persist_outbox_entry(entry);
2944            }
2945        }
2946    }
2947
2948    /// Stages re-seal provenance for a message about to be dispatched, so the
2949    /// outbox entry created during dispatch carries it. Callers gate on the
2950    /// content being a sealed encrypted DM (`__MLS_ENC__`); media and plaintext
2951    /// never stage and thus always replay verbatim.
2952    pub(super) fn stage_outbox_reseal(&mut self, id: &MessageId, reseal: OutboxReseal) {
2953        self.pending_reseal.insert(id.clone(), reseal);
2954    }
2955
2956    /// Consumes any staged re-seal provenance for `message`, attaching it to a
2957    /// (non-media) main-outbox entry being created for the first time. On later
2958    /// attempts the entry already exists and carries its `reseal`, so this
2959    /// returns `None`. Media never stages.
2960    ///
2961    /// **Always removes the staged copy** (it does not leave it behind when it
2962    /// returns `None`): the staging map is strictly transient and must never
2963    /// outlive its message, or a message that stages but is then dropped before
2964    /// an outbox entry is created (e.g. `dispatch_prepared_message` early-returns
2965    /// on a duplicate id) would strand plaintext in `pending_reseal` for the
2966    /// process lifetime. Removing unconditionally here — plus the belt-and-braces
2967    /// clear in [`Self::remove_outbox_entry`] — bounds the map to genuinely
2968    /// in-flight sends. Kept as one helper so the two outbox-entry creation sites
2969    /// ([`Self::ensure_outbox_entry`] and [`Self::mark_message_sent`]) stay in
2970    /// lockstep on the consume condition.
2971    fn take_staged_reseal(&mut self, message: &Message, is_media: bool) -> Option<OutboxReseal> {
2972        let staged = self.pending_reseal.remove(&message.id);
2973        if is_media || self.outbox.contains_key(&message.id) {
2974            return None;
2975        }
2976        staged
2977    }
2978
2979    /// Re-seals an outbound encrypted DM against the recipient's *current* MLS
2980    /// session for a resend, returning fresh sealed content, or `None` to replay
2981    /// verbatim. Returns `None` when it is not a re-sealable encrypted DM, when
2982    /// there is no stored provenance (plaintext, media, or a pre-upgrade
2983    /// persisted entry), when the session is not currently confirmed, or when
2984    /// the re-seal produced identical bytes.
2985    ///
2986    /// This is the Tier 2 core: after a desync + re-key the recipient's session
2987    /// is rebuilt at a new epoch, so replaying the ciphertext sealed at the old
2988    /// epoch is undecryptable forever. Re-sealing here rebuilds only the
2989    /// ciphertext `content`; the outer `Message.id` is preserved so the
2990    /// receiver's dedup and ACK correlation are unaffected. Gating on a
2991    /// confirmed session guarantees `prepare_outbound_content` takes the
2992    /// encrypt-now path and never the `store_pending` queueing path.
2993    fn reseal_resend_content(&mut self, message: &Message) -> Option<String> {
2994        if !message.content.starts_with(internal_prefixes::ENCRYPTED) {
2995            return None;
2996        }
2997        let recipient = message.recipient.as_str().to_string();
2998        if !self.confirmed_sessions.contains(&recipient) {
2999            return None;
3000        }
3001        let reseal = self.outbox.get(&message.id)?.reseal.as_ref()?.clone();
3002        match self.prepare_outbound_content(
3003            &recipient,
3004            &reseal.content,
3005            reseal.priority,
3006            reseal.reply_to_msg.clone(),
3007            reseal.forwarded_from.clone(),
3008            reseal.content_type,
3009            reseal.media_metadata.clone(),
3010            reseal.rich.as_ref(),
3011            Some(PendingProvenance::for_id(message.id.clone())),
3012            "resend_reseal",
3013        ) {
3014            Ok(OutboundSendPreparation::Ready(sealed)) if sealed != message.content => Some(sealed),
3015            // Identical bytes, or an error: replay verbatim this round. Tier 1
3016            // keeps the sender retrying until the session heals.
3017            //
3018            // The `Queued` arm is all but unreachable — the `confirmed_sessions`
3019            // gate above runs synchronously with no yield before the encrypt, so
3020            // the session cannot flip to not-ready in between. If it ever does
3021            // (e.g. `confirmed_sessions` outlived an externally-deleted MLS
3022            // session), `prepare_outbound_content` has enqueued a pending message
3023            // as a side effect, keyed by our SAME `existing_id`. That is benign:
3024            // this round still replays the verbatim outbox bytes, and when the
3025            // pending copy later flushes, the sender's own `dispatch_prepared_message`
3026            // dedup drops it as a duplicate id. So no double-delivery, just a
3027            // transient pending entry that self-clears on flush.
3028            _ => None,
3029        }
3030    }
3031
3032    /// Re-seals `message` in place against the current session if it is a
3033    /// re-sealable encrypted DM (see [`Self::reseal_resend_content`]). Used at
3034    /// the resend transmit points so the bytes that go out — and the outbox copy
3035    /// `mark_message_sent` then stores — are freshly sealed.
3036    pub(super) fn reseal_for_resend_in_place(&mut self, message: &mut Message) {
3037        if let Some(fresh) = self.reseal_resend_content(message) {
3038            message.content = fresh;
3039        }
3040    }
3041
3042    pub(super) fn remove_outbox_entry(&mut self, message_id: &MessageId) -> Option<OutboxEntry> {
3043        // Belt-and-braces: drop any still-staged re-seal provenance for this id
3044        // so it never outlives the message (see `take_staged_reseal`). Normally
3045        // staging is consumed at entry creation; this covers the id being torn
3046        // down before that ever happened.
3047        self.pending_reseal.remove(message_id);
3048        if let Some(entry) = self.outbox.remove(message_id) {
3049            self.clear_outbox_entry_from_storage(message_id);
3050            return Some(entry);
3051        }
3052        self.media_outbox.remove(message_id)
3053    }
3054
3055    /// Settles the pending connection-request entry for `message_id` when a
3056    /// terminal drop occurs (max retries, outbox expiry, capacity eviction),
3057    /// returning the recipient the caller should emit
3058    /// `connection_request_undeliverable` for. The TTL gates emission — a
3059    /// signal that stale belongs to a request the app has long stopped
3060    /// waiting on — but the entry is removed either way.
3061    pub(super) fn take_undeliverable_connection_request(
3062        &mut self,
3063        message_id: &MessageId,
3064    ) -> Option<String> {
3065        self.pending_connection_requests
3066            .remove(&message_id.as_str())
3067            .filter(|pending| pending.sent_at.elapsed() <= PENDING_CONNECTION_REQUEST_TTL)
3068            .map(|pending| pending.recipient)
3069    }
3070
3071    /// Removes messages that have waited too long for MLS session
3072    /// establishment. This gives the pre-outbox queue the same bounded
3073    /// lifecycle guarantee as dispatched messages.
3074    pub(super) fn cleanup_expired_pending_messages(&mut self) {
3075        self.cleanup_expired_pending_messages_at(Utc::now());
3076    }
3077
3078    /// Runs pending-message expiry only when the earliest queued deadline is
3079    /// due. Bindings call `process()` every 100 ms, so blindly scanning the
3080    /// entire queue there wastes CPU and battery for almost the whole default
3081    /// seven-day lifetime.
3082    pub(super) fn cleanup_expired_pending_messages_if_due(&mut self) {
3083        let now = Utc::now();
3084        if self
3085            .next_pending_message_expiry
3086            .is_some_and(|expiry| now >= expiry)
3087        {
3088            self.cleanup_expired_pending_messages_at(now);
3089        }
3090    }
3091
3092    fn cleanup_expired_pending_messages_at(&mut self, now: DateTime<Utc>) {
3093        let lifetime_ms = self
3094            .config
3095            .reliability
3096            .retry
3097            .pending_message_max_lifetime_ms;
3098        let mut changed_recipients = Vec::new();
3099        let mut expired_ids = Vec::new();
3100
3101        for (recipient, messages) in &mut self.pending_encrypted_messages {
3102            let previous_len = messages.len();
3103            messages.retain(|pending| {
3104                // Bounded per pass: each expiry costs a durable delete, and a
3105                // burst of entries queued together comes due together. The
3106                // remainder stays queued past its deadline and drains on the
3107                // next tick — see `MAX_PENDING_EXPIRIES_PER_PASS`.
3108                if expired_ids.len() < MAX_PENDING_EXPIRIES_PER_PASS
3109                    && lifetime_expired(now, pending.queued_at, lifetime_ms)
3110                {
3111                    expired_ids.push(pending.message_id.clone());
3112                    false
3113                } else {
3114                    true
3115                }
3116            });
3117            if messages.len() != previous_len {
3118                changed_recipients.push(recipient.clone());
3119            }
3120        }
3121
3122        for recipient in changed_recipients {
3123            if self
3124                .pending_encrypted_messages
3125                .get(&recipient)
3126                .is_some_and(Vec::is_empty)
3127            {
3128                self.pending_encrypted_messages.remove(&recipient);
3129            }
3130        }
3131        // The survivors' records are untouched, so only the expired entries are
3132        // written to storage — one delete each, paired with the settlement
3133        // below.
3134        self.delete_pending_messages_from_storage(expired_ids.iter());
3135        self.recompute_next_pending_message_expiry();
3136
3137        for message_id in expired_ids {
3138            // Reached both from `process()` (emits immediately) and from
3139            // restore, which runs before the event pipeline is live.
3140            self.settle_restored_message_failure(Event::message_failed(
3141                message_id,
3142                "Pending session lifetime exceeded".to_string(),
3143                0,
3144            ));
3145        }
3146    }
3147
3148    pub(super) fn cleanup_outbox(&mut self) {
3149        let now = Utc::now();
3150        let lifetime_ms = self.config.reliability.retry.outbox_max_lifetime_ms;
3151        // In-process twin of the restore path's absolute cap
3152        // (`restore_outbox`): the carrier-relative window slides on every
3153        // send, and an unreachable-parked DM's mesh reachability probe keeps
3154        // sending — without an absolute bound from `first_sent_at`, a
3155        // long-lived process would keep such an entry alive forever.
3156        let absolute_lifetime_ms =
3157            lifetime_ms.saturating_mul(crate::constants::OUTBOX_ABSOLUTE_LIFETIME_FACTOR as u64);
3158
3159        let mut expired_from_outbox = Vec::new();
3160        for (message_id, entry) in &self.outbox {
3161            if !lifetime_expired(now, entry.last_sent_at, lifetime_ms)
3162                && !lifetime_expired(now, entry.first_sent_at, absolute_lifetime_ms)
3163            {
3164                continue;
3165            }
3166            if entry.message.requires_ack && self.ack_manager.is_waiting_for_ack(&entry.message.id)
3167            {
3168                continue;
3169            }
3170            expired_from_outbox.push((
3171                message_id.clone(),
3172                entry.last_transport,
3173                entry.attempt_count,
3174            ));
3175        }
3176        for (message_id, last_transport, attempt_count) in expired_from_outbox {
3177            if let Some(transport) = last_transport {
3178                self.transport_manager.record_delivery_failure(transport);
3179            }
3180            self.retry_queue.remove(&message_id.as_str());
3181            self.outbox.remove(&message_id);
3182            self.clear_outbox_entry_from_storage(&message_id);
3183            self.handle_outbound_media_chunk_failed(&message_id, "outbox lifetime exceeded");
3184
3185            // Expiry is terminal: without an event the app shows the message
3186            // as pending forever. Mirrors handle_max_retries_exceeded, which
3187            // settles the same way when the retry budget runs out.
3188            self.emit_event(Event::message_failed(
3189                message_id.clone(),
3190                "Outbox lifetime exceeded".to_string(),
3191                attempt_count,
3192            ));
3193            if let Some(recipient) = self.take_undeliverable_connection_request(&message_id) {
3194                warn!(
3195                    recipient = %recipient,
3196                    message_id = %message_id,
3197                    "Connection request undeliverable: outbox lifetime exceeded"
3198                );
3199                self.emit_event(Event::connection_request_undeliverable(
3200                    recipient,
3201                    message_id.as_str(),
3202                    "outbox_lifetime_exceeded",
3203                ));
3204            }
3205        }
3206
3207        // Only peers that still hold outbox entries can retain an
3208        // unreachable-park counter (bounds the map at the outbox cap).
3209        if !self.dm_unreachable_parks.is_empty() {
3210            let active: HashSet<&str> = self
3211                .outbox
3212                .values()
3213                .map(|entry| entry.message.recipient.as_str())
3214                .collect();
3215            self.dm_unreachable_parks
3216                .retain(|peer, _| active.contains(peer.as_str()));
3217        }
3218
3219        let mut expired_from_media = Vec::new();
3220        for (message_id, entry) in &self.media_outbox {
3221            if !lifetime_expired(now, entry.last_sent_at, lifetime_ms) {
3222                continue;
3223            }
3224            if entry.message.requires_ack && self.ack_manager.is_waiting_for_ack(&entry.message.id)
3225            {
3226                continue;
3227            }
3228            expired_from_media.push((message_id.clone(), entry.last_transport));
3229        }
3230        for (message_id, last_transport) in expired_from_media {
3231            if let Some(transport) = last_transport {
3232                self.transport_manager.record_delivery_failure(transport);
3233            }
3234            self.retry_queue.remove(&message_id.as_str());
3235            self.media_outbox.remove(&message_id);
3236            self.handle_outbound_media_chunk_failed(&message_id, "outbox lifetime exceeded");
3237        }
3238    }
3239
3240    pub(super) fn ensure_ack_registration(&mut self, message: &Message) -> Result<bool> {
3241        if !message.requires_ack {
3242            return Ok(false);
3243        }
3244
3245        if self.ack_manager.is_waiting_for_ack(&message.id) {
3246            Ok(false)
3247        } else {
3248            if let Some(eviction) = self
3249                .ack_manager
3250                .register_pending_ack(message.id.clone(), None)?
3251            {
3252                self.emit_event(Event::ack_evicted(
3253                    eviction.message_id,
3254                    eviction.priority.as_str(),
3255                    "capacity".to_string(),
3256                ));
3257            }
3258            Ok(true)
3259        }
3260    }
3261
3262    pub(super) fn is_media_outbox_message(message: &Message) -> bool {
3263        message.content_type == ContentType::FileChunk
3264    }
3265
3266    // ========================================================================
3267    // DELIVERY TRACKING
3268    // ========================================================================
3269
3270    /// Handles successful message send.
3271    /// Whether a frame for `peer_id` can arrive without a neighbour carrying
3272    /// it, given what this device knows about *this recipient*.
3273    ///
3274    /// [`TransportManager::can_reach_without_carrying`] answers the same
3275    /// question from carrier status alone, and so says yes for every recipient
3276    /// while any infrastructure carrier is up. This is that answer with the
3277    /// per-recipient facts applied: a carrier that has already told us it
3278    /// cannot reach this recipient stops counting as a way to reach them.
3279    ///
3280    /// Two properties hold by construction:
3281    ///
3282    /// - **A carrier holding no fact is unchanged.** Nostr cannot report
3283    ///   per-recipient delivery at all, so it never produces a fact and keeps
3284    ///   its blanket claim. That is deliberate: the mixed-neighbourhood
3285    ///   residual stays open for Nostr-only devices rather than closing by
3286    ///   accident under a design that never gave them a verdict to act on.
3287    /// - **A live mesh link always wins.** It is ground truth rather than a
3288    ///   claim, and it is checked whether or not any fact exists.
3289    pub(crate) fn can_reach_recipient(&self, peer_id: &str) -> bool {
3290        let now = Instant::now();
3291        let infrastructure_reaches_peer = self
3292            .transport_manager
3293            .available_infrastructure_carriers()
3294            .into_iter()
3295            .any(|carrier| {
3296                !matches!(
3297                    self.reachability.claim_for(peer_id, carrier, now),
3298                    Some(Claim::Unreachable)
3299                )
3300            });
3301        infrastructure_reaches_peer || self.transport_manager.mesh_can_address(peer_id)
3302    }
3303
3304    pub(super) fn handle_send_success(
3305        &mut self,
3306        message: &Message,
3307        transport: Option<TransportType>,
3308    ) -> Result<()> {
3309        self.mark_message_sent(message, transport, Some(1));
3310        self.ensure_ack_registration(message)?;
3311
3312        // A transport reporting success is not always evidence the frame can
3313        // arrive. Wi-Fi Direct and Reticulum accept any recipient and return
3314        // `Ok`, so a send to someone we hold no link to is queued for a link
3315        // that never drains — and because the mesh hand-off used to hang off
3316        // the *failure* path, a device with either carrier up would swallow the
3317        // frame instead of asking its neighbors to carry it. That is precisely
3318        // the out-of-range case forwarding exists for, so the question is asked
3319        // directly rather than inferred from an error that never comes.
3320        //
3321        // Offering here is additive, never a replacement: the outbox entry and
3322        // the ACK registration above stand either way, and if the accepting
3323        // carrier did deliver after all, the recipient's deduplicator absorbs
3324        // the second copy.
3325        if !self.can_reach_recipient(message.recipient.as_str()) {
3326            let handed_to_mesh = self.offer_to_mesh(message);
3327            if handed_to_mesh > 0 {
3328                debug!(
3329                    message_id = %message.id,
3330                    recipient = %message.recipient,
3331                    handed_to_mesh,
3332                    "Transport accepted a recipient it cannot reach; also handed to neighbors"
3333                );
3334            }
3335        }
3336        Ok(())
3337    }
3338
3339    /// Handles failed message send by persisting to outbox and scheduling retry.
3340    ///
3341    /// EDGE CASE HANDLING:
3342    /// - Ensures message is persisted to outbox for recovery
3343    /// - Schedules retry with exponential backoff
3344    /// - Handles case where all transports are unavailable
3345    ///
3346    /// NOTE: This does NOT call `record_retry_failure` — callers that need it
3347    /// (e.g. `send_via_forced_transport`) must record the failure themselves.
3348    /// `TransportManager::send()` already records failures internally, so
3349    /// calling it here would double-count.
3350    /// Returns the absolute time the retry was scheduled for (`None` when the
3351    /// message was already queued for retry), so callers can surface it in
3352    /// `MessageDeferred` instead of guessing.
3353    pub(super) fn handle_send_failure(
3354        &mut self,
3355        message: &Message,
3356        transport: Option<TransportType>,
3357    ) -> Result<Option<DateTime<Utc>>> {
3358        // Ensure message is persisted to outbox for recovery
3359        self.ensure_outbox_entry(message);
3360
3361        // No transport could reach the recipient directly — but a neighbor
3362        // might be able to, or know someone who can. Offer the frame to the
3363        // mesh before settling for a retry: this is the case the whole
3364        // forwarding path exists for, someone out of our range but inside the
3365        // crowd's.
3366        //
3367        // The outbox entry and retry stay in place regardless. Handing a frame
3368        // to neighbors is not proof it arrived, and the acknowledgement coming
3369        // back is what settles it.
3370        let handed_to_mesh = self.offer_to_mesh(message);
3371
3372        // Schedule retry (enqueue is infallible — no attempt limit)
3373        let next_retry_at = self.retry_queue.enqueue(message.clone(), 0);
3374
3375        warn!(
3376            message_id = %message.id,
3377            transport = ?transport,
3378            handed_to_mesh,
3379            "Deferred message due to send error"
3380        );
3381        Ok(next_retry_at)
3382    }
3383
3384    /// Hands a locally-originated frame to nearby devices so it can travel
3385    /// toward a recipient we cannot reach ourselves.
3386    ///
3387    /// Returns how many neighbors took a copy. Zero means we are alone, or
3388    /// every neighbor refused the link — in both cases the retry ladder and the
3389    /// outbox remain the recovery path, exactly as before.
3390    ///
3391    /// Unlike a forwarded frame, this one is not held back: the delay before
3392    /// forwarding exists so neighbors holding the *same* frame do not all
3393    /// transmit at once, and nobody else is holding this one yet.
3394    pub(super) fn offer_to_mesh(&mut self, message: &Message) -> usize {
3395        // Deliberately not gated on `allow_relay`. That setting is about what
3396        // this device does for *other people* — whether it spends its battery
3397        // carrying their traffic. This is the device's own message, and the
3398        // ones it must be able to send include the acknowledgement for a
3399        // message it just received. Gating it here would leave a
3400        // relay-declining device unable to answer anything that reached it
3401        // across the mesh, so its sender would retransmit to exhaustion and
3402        // report a failure for a message that was delivered and read.
3403
3404        // Never hand a self-addressed frame to the mesh. Those are the relay
3405        // hint frames (`__GRP_RELAY_REG__`, `__GRP_RELAY_BCAST__`), which mean
3406        // something only to our own bridge: a neighbor receiving one would see
3407        // a frame addressed to somebody else and carry it further, spending
3408        // airtime to deliver a control op nobody can act on. They are pinned to
3409        // Internet for the same reason.
3410        if message.recipient.as_str() == self.local_id {
3411            return 0;
3412        }
3413
3414        let neighbors = self.transport_manager.mesh_neighbors();
3415        if neighbors.is_empty() {
3416            return 0;
3417        }
3418
3419        // Ours to originate: record it as handled so a copy coming back to us
3420        // through the mesh is recognized and not forwarded again.
3421        self.mesh_relay.mark_handled(&message.id.as_str());
3422
3423        // If the recipient is standing right there, hand it to them. Reached
3424        // here it means the ordinary send could not use that link, but trying
3425        // it costs one call and beats routing around the destination.
3426        let recipient = message.recipient.as_str();
3427        let targets = if neighbors.iter().any(|n| n.peer_id == recipient) {
3428            vec![recipient.to_string()]
3429        } else {
3430            // Full width regardless of this device's battery. Capability bias
3431            // narrows the fan-out for *forwarded* frames, where neighbors hold
3432            // copies and the sender is still retrying so the cost is
3433            // redundancy. This frame is ours and nobody else is holding it, so
3434            // the fan-out is the delivery attempt itself — and since targets
3435            // are chosen stably per message id, a narrowed set would be the
3436            // same narrowed set on every retransmission.
3437            self.mesh_relay.select_origination_targets(
3438                neighbors
3439                    .iter()
3440                    .map(|n| (n.peer_id.as_str(), n.link_quality())),
3441                &[],
3442                &message.id.as_str(),
3443            )
3444        };
3445
3446        let mut handed_to = 0usize;
3447        for target in targets {
3448            // Metered against the same ceiling as carrying other people's
3449            // traffic: it is the same radio. Without this a large transfer,
3450            // which offers every chunk separately, would put an unbounded burst
3451            // on the air in one call.
3452            //
3453            // Own traffic spends from the reserve that forwarding leaves alone,
3454            // so a device carrying the neighborhood at its ceiling can still
3455            // get its own frames out — an acknowledgement above all, whose
3456            // absence makes a delivered message look lost to its sender.
3457            if !self.mesh_relay.take_own_send_token() {
3458                debug!(
3459                    message_id = %message.id,
3460                    "At the forwarding limit; not handing this to further neighbors"
3461                );
3462                break;
3463            }
3464
3465            match self.transport_manager.send_to_neighbor(&target, message) {
3466                Ok(transport) => {
3467                    handed_to += 1;
3468                    debug!(
3469                        message_id = %message.id,
3470                        recipient = %message.recipient,
3471                        next_hop = %target,
3472                        transport = ?transport,
3473                        "Handed message to a neighbor to carry"
3474                    );
3475                }
3476                Err(err) => {
3477                    debug!(
3478                        message_id = %message.id,
3479                        next_hop = %target,
3480                        error = %err,
3481                        "Neighbor could not take the message"
3482                    );
3483                }
3484            }
3485        }
3486
3487        handed_to
3488    }
3489
3490    /// Confirms that a message was successfully sent by the transport layer.
3491    pub fn on_transport_send_confirmed(&mut self, message_id: &str) -> Result<()> {
3492        let Some(peer_id) = self.find_welcome_peer_by_message_id(message_id) else {
3493            return Ok(());
3494        };
3495
3496        let updated = self
3497            .welcome_lifecycles
3498            .get(&peer_id)
3499            .cloned()
3500            .ok_or_else(|| Error::Other(format!("Missing welcome lifecycle for {}", peer_id)))?;
3501
3502        if matches!(
3503            updated.state,
3504            WelcomeDeliveryState::Sent | WelcomeDeliveryState::Expired
3505        ) {
3506            return Ok(());
3507        }
3508
3509        // A relay DeliveryError verdict outranks a late wire confirm: the
3510        // socket write succeeded, but the relay saw the frame and dropped it
3511        // (recipient offline). When the verdict raced ahead of this confirm,
3512        // the record is already parked `Failed`/`PeerUnreachable`; letting
3513        // the confirm proceed would resurrect the false `Sent` the verdict
3514        // just corrected — and emit welcome_send_succeeded *after* the
3515        // corrective welcome_send_failed. Any legitimate new send attempt
3516        // moves the record to `SendAttempted` first, so `Failed` here means
3517        // this confirm belongs to the very send the relay already failed.
3518        if updated.state == WelcomeDeliveryState::Failed
3519            && updated.last_reason_code == Some(crate::events::WelcomeReasonCode::PeerUnreachable)
3520        {
3521            return Ok(());
3522        }
3523
3524        // A wire confirm for a reachability probe must not upgrade the
3525        // record to `Sent`. With a live unreachable-park counter the send in
3526        // flight IS the escalating probe — every reachability edge clears the
3527        // counter before sending anything else — and the socket write
3528        // succeeding adds nothing the park didn't already know. Whenever the
3529        // relay's push fallback succeeds it returns no `DeliveryError` at
3530        // all, so a probe marked `Sent` here would go quiet forever:
3531        // `next_retry_at` cleared below, `Sent` outside every retry scan, and
3532        // `rearm_welcome_for_peer` a no-op on it — the exact edge-only dead
3533        // end the probe exists to close, reachable only through this bridge
3534        // confirm (mesh carriers never wire-confirm). Leaving the record
3535        // `SendAttempted` keeps the confirm deadline armed: a relay verdict
3536        // re-parks it, and an accepted-but-unanswered frame resolves at the
3537        // confirm timeout, which re-parks via
3538        // `welcome_probe_repark_permitted`. Genuine delivery still converges
3539        // regardless — the peer proving the session marks the welcome `Sent`
3540        // through `confirm_session_state`. This also withholds the
3541        // `welcome_send_succeeded` a probe's socket write would otherwise
3542        // emit, which the next verdict or timeout would only have to correct.
3543        if matches!(updated.state, WelcomeDeliveryState::SendAttempted)
3544            && updated.unreachable_parks > 0
3545        {
3546            return Ok(());
3547        }
3548
3549        {
3550            let record = self.welcome_lifecycles.get_mut(&peer_id).ok_or_else(|| {
3551                Error::Other(format!("Missing welcome lifecycle for {}", peer_id))
3552            })?;
3553            record.next_retry_at = None;
3554            record.last_reason_code = None;
3555            record.last_transport_error = None;
3556        }
3557        self.transition_welcome_state(&peer_id, WelcomeDeliveryState::Sent, "transport_confirmed")?;
3558
3559        let sent_snapshot = self
3560            .welcome_lifecycles
3561            .get(&peer_id)
3562            .cloned()
3563            .ok_or_else(|| Error::Other(format!("Missing welcome lifecycle for {}", peer_id)))?;
3564        if let Ok(state) = lock_shared_state(&self.shared_state) {
3565            state.emit_event(Event::welcome_send_succeeded(
3566                peer_id.clone(),
3567                sent_snapshot.welcome_message.id.as_str().to_string(),
3568                sent_snapshot.group_id,
3569                sent_snapshot.attempt,
3570            ));
3571        }
3572
3573        // The welcome is now confirmed as sent. Send a confirmation probe
3574        // immediately so the session can be confirmed without waiting for the
3575        // next process() tick — an optimistic fast-path for the common case
3576        // where the transport confirms promptly.
3577        self.send_session_confirmation_probe(&peer_id, "transport_confirmed");
3578
3579        Ok(())
3580    }
3581
3582    /// Handles asynchronous transport send failures for pending welcome
3583    /// sends and outbound connection requests.
3584    pub fn on_transport_send_failed(
3585        &mut self,
3586        message_id: &str,
3587        transport_error: Option<String>,
3588    ) -> Result<()> {
3589        self.on_transport_send_failed_via(message_id, transport_error, None)
3590    }
3591
3592    /// [`Self::on_transport_send_failed`] with the carrier that reported it.
3593    ///
3594    /// The carrier is what turns a failure into a *fact*: "the recipient is
3595    /// not reachable" is meaningless on its own, since a device may hold
3596    /// several carriers and only one of them is making the claim. Bridges that
3597    /// know which transport answered pass it; the plain form above keeps
3598    /// working for callers that do not, and records no fact rather than
3599    /// guessing one.
3600    pub fn on_transport_send_failed_via(
3601        &mut self,
3602        message_id: &str,
3603        transport_error: Option<String>,
3604        carrier: Option<TransportType>,
3605    ) -> Result<()> {
3606        // Classified at the boundary, once, and never carried past it.
3607        //
3608        // The platform bridges build this string from the relay's own
3609        // `DeliveryError.reason` (`recipient_unreachable: <relay text>`), and
3610        // the Nostr bridge from a relay's `OK` rejection text — remote-chosen,
3611        // unbounded, and downstream of here it reaches three event fields the
3612        // telemetry scrubber ships verbatim, each beside a recipient it
3613        // hashes. Even the honest relay renders the peer into its wording.
3614        //
3615        // The token itself is load-bearing and survives: the two prefix tests
3616        // below and the DM park key on `recipient_unreachable`, so classifying
3617        // to the bare token keeps every protocol decision intact and drops
3618        // only the prose. The raw string stays in this one device log.
3619        if let Some(raw) = transport_error.as_deref() {
3620            debug!(
3621                message_id = %message_id,
3622                transport_error = %raw.chars().take(256).collect::<String>(),
3623                "Platform reported a send failure"
3624            );
3625        }
3626        let transport_error = transport_error
3627            .as_deref()
3628            .map(classify_transport_send_error);
3629        // Connection requests first: the relay's "recipient offline"
3630        // DeliveryError is the only fast, authoritative failure signal a
3631        // request gets (there is no ACK from an offline peer — the app
3632        // would otherwise wait out the full retry budget for a generic
3633        // message_failed). Non-unreachable errors stay with the generic
3634        // retry machinery: the request is queued/retried and may still
3635        // deliver.
3636        if let Some(pending) = self.pending_connection_requests.get(message_id) {
3637            // The TTL must hold at read time too: the insert-path prune only
3638            // runs on the next `send_connection_request`, so an idle map can
3639            // hold an entry far past the correlation window — a failure
3640            // signal that stale belongs to a request the app has long
3641            // stopped waiting on and must not fire the event.
3642            if pending.sent_at.elapsed() > PENDING_CONNECTION_REQUEST_TTL {
3643                self.pending_connection_requests.remove(message_id);
3644            } else {
3645                let unreachable = transport_error == Some(SEND_FAIL_REASON_RECIPIENT_UNREACHABLE);
3646                if unreachable {
3647                    let recipient = pending.recipient.clone();
3648                    self.pending_connection_requests.remove(message_id);
3649                    warn!(
3650                        recipient = %recipient,
3651                        message_id = %message_id,
3652                        "Connection request undeliverable: recipient unreachable"
3653                    );
3654                    if let Ok(state) = lock_shared_state(&self.shared_state) {
3655                        state.emit_event(Event::connection_request_undeliverable(
3656                            recipient,
3657                            message_id.to_string(),
3658                            transport_error.unwrap_or(SEND_FAIL_REASON_TRANSPORT),
3659                        ));
3660                    }
3661                    return Ok(());
3662                }
3663            }
3664        }
3665
3666        let Some(peer_id) = self.find_welcome_peer_by_message_id(message_id) else {
3667            // Not a connection request, not a welcome: a plain DM or media
3668            // chunk. Surface the relay's verdict and park plain DMs so the
3669            // ACK retry budget stops burning against an offline peer.
3670            self.handle_recipient_unreachable_for_message(message_id, transport_error, carrier);
3671            return Ok(());
3672        };
3673        // A reason tagged "recipient_unreachable" (the internet bridge's
3674        // translation of the relay's DeliveryError) is authoritative proof
3675        // the frame was dropped: the carrier is up but *this peer* is not on
3676        // it. It must be handled even for records already wire-confirmed
3677        // (`Sent`) — the bridge confirms on socket-write success, before the
3678        // relay can answer, so the DeliveryError normally arrives when the
3679        // record is already Sent and the plain failure path below would
3680        // no-op on it, stranding a false `Sent`.
3681        let peer_unreachable = transport_error == Some(SEND_FAIL_REASON_RECIPIENT_UNREACHABLE);
3682        if peer_unreachable {
3683            if let Some(carrier) = carrier {
3684                self.reachability.record(
3685                    &peer_id,
3686                    carrier,
3687                    Claim::Unreachable,
3688                    FactSource::GatewayVerdict,
3689                    Instant::now(),
3690                );
3691            }
3692            return self.apply_recipient_unreachable_failure(&peer_id, transport_error);
3693        }
3694        let reason = crate::events::WelcomeReasonCode::TransportUnavailable;
3695        // No raw error is available on this async path, so infer no-carrier
3696        // from live connectivity: with no transport currently available the
3697        // peer is simply unreachable and the Welcome must be kept alive, not
3698        // aged.
3699        let no_carrier = self.transport_manager.get_available_transports().is_empty();
3700        let _ = self.apply_welcome_send_failure(
3701            &peer_id,
3702            reason,
3703            transport_error,
3704            no_carrier,
3705            "transport_failed",
3706        )?;
3707        Ok(())
3708    }
3709
3710    /// Handles a relay `recipient_unreachable` verdict for a plain DM or
3711    /// media chunk: emits the non-terminal [`Event::MessageUndeliverable`],
3712    /// then *parks* plain DMs.
3713    ///
3714    /// Parking drops the pending ACK and the retry-queue entry while the
3715    /// outbox entry stays put — exactly the "stranded" state every re-drive
3716    /// edge already flushes (`flush_outbox_all` on reconnect/start,
3717    /// `flush_outbox_for_peer_via` on discovery and presence-online, the latter
3718    /// fed by [`Self::presence_watch_peers`]). Without the park, the offline
3719    /// peer's missing ACK burns the full `max_retries` budget in minutes
3720    /// (`process_timed_out_acks`) and settles a 7-day outbox message
3721    /// terminally; a re-driven send re-registers a fresh ACK, so the budget
3722    /// normally burns only against a peer believed reachable. The exception
3723    /// is the reachability probe below: its send re-enters the ACK machinery
3724    /// but may never earn a relay verdict (a mesh carrier cannot produce one
3725    /// at all), so exhaustion with a live park counter *re-parks* instead of
3726    /// settling ([`Self::try_repark_exhausted_dm`]). Settlement is reserved
3727    /// for delivery or outbox-lifetime expiry.
3728    ///
3729    /// The park always keeps a timed reachability probe whose interval
3730    /// escalates per consecutive park (15s → 600s cap, per-peer counter reset
3731    /// on any reachability edge). It is deliberately carrier-agnostic: with a
3732    /// local mesh carrier the peer may be a room away — possibly already a
3733    /// discovered neighbor, so no future edge would fire for it — and on an
3734    /// internet-only device the external edges (presence rotation, reconnect)
3735    /// are the *only* other recovery, which leaves delivery hostage to a
3736    /// bridge's polling cadence. See [`Self::park_unreachable_dm`] for why
3737    /// probing over the relay is self-limiting.
3738    ///
3739    /// Media chunks are never parked: their offline story is retry
3740    /// exhaustion → transfer abort → persisted descriptor →
3741    /// `MediaResendRequired` with app-resupplied bytes. Messages without an
3742    /// outbox entry (nothing to attribute a recipient from) are skipped.
3743    fn handle_recipient_unreachable_for_message(
3744        &mut self,
3745        message_id: &str,
3746        transport_error: Option<&'static str>,
3747        carrier: Option<TransportType>,
3748    ) {
3749        let Some(reason) = transport_error.filter(|r| *r == SEND_FAIL_REASON_RECIPIENT_UNREACHABLE)
3750        else {
3751            return;
3752        };
3753        let Ok(parsed_id) = MessageId::from_str(message_id) else {
3754            return;
3755        };
3756        let (entry, is_media) = match self.outbox.get(&parsed_id) {
3757            Some(entry) => (entry, false),
3758            None => match self.media_outbox.get(&parsed_id) {
3759                Some(entry) => (entry, true),
3760                None => {
3761                    debug!(
3762                        message_id = %message_id,
3763                        "Unreachable verdict for message without outbox entry, dropping"
3764                    );
3765                    return;
3766                }
3767            },
3768        };
3769        let recipient = entry.message.recipient.as_str().to_string();
3770        let attempt_count = entry.attempt_count;
3771        // The verdict, recorded as a fact before anything acts on it. Parking
3772        // below reacts to *this* message; the fact is what lets the next send
3773        // to the same recipient skip a carrier that has already said no.
3774        if let Some(carrier) = carrier {
3775            self.reachability.record(
3776                &recipient,
3777                carrier,
3778                Claim::Unreachable,
3779                FactSource::GatewayVerdict,
3780                Instant::now(),
3781            );
3782        }
3783        // Media never parks, so the mesh offer the park performs for a DM has
3784        // to happen here instead. Cloned while the entry is still borrowed;
3785        // only on a verdict, so not a per-chunk cost.
3786        let media_frame = is_media.then(|| entry.message.clone());
3787        let file_id = self
3788            .outbound_media_chunks
3789            .get(&parsed_id)
3790            .map(|(file_id, _)| file_id.clone());
3791        warn!(
3792            message_id = %message_id,
3793            file_id = ?file_id,
3794            parked = !is_media,
3795            "Recipient unreachable for in-flight message (non-terminal)"
3796        );
3797        self.emit_event(Event::message_undeliverable(
3798            parsed_id.clone(),
3799            recipient.clone(),
3800            reason,
3801            file_id,
3802        ));
3803        if let Some(frame) = media_frame {
3804            // The recipient is not on the relay, but they may be a few devices
3805            // away. A chunk keeps its pending ACK (media is never parked), so
3806            // an answer coming back over the mesh settles it through the
3807            // ordinary path — nothing else is needed here.
3808            let handed_to_mesh = self.offer_to_mesh(&frame);
3809            if handed_to_mesh > 0 {
3810                debug!(
3811                    message_id = %message_id,
3812                    recipient = %recipient,
3813                    handed_to_mesh,
3814                    "Relay cannot reach this chunk's recipient; handed it to neighbors"
3815                );
3816            }
3817            return;
3818        }
3819
3820        self.park_unreachable_dm(&parsed_id, &recipient, attempt_count);
3821    }
3822
3823    /// The park action shared by the relay-verdict path
3824    /// ([`Self::handle_recipient_unreachable_for_message`]) and the
3825    /// exhausted-probe path ([`Self::try_repark_exhausted_dm`]): drops the
3826    /// pending ACK and any scheduled retry so nothing burns budget against
3827    /// the offline peer, offers the frame to the mesh, then schedules the
3828    /// escalating reachability probe.
3829    ///
3830    /// The mesh offer is here rather than at either caller so both parks make
3831    /// it, and because this is where the frame is already at hand. Its rationale
3832    /// is inline below; the short version is that a relay verdict is the only
3833    /// per-peer reachability fact this device ever receives, and it says the
3834    /// recipient is somewhere the relay cannot go.
3835    ///
3836    /// The probe is carrier-agnostic — DORS picks its transport like any
3837    /// other send — because an internet-only device is the common
3838    /// configuration, not an exotic one, and parking it edge-only leaves the
3839    /// message with *no* self-recovery at all: no pending ACK to time out, no
3840    /// retry entry, nothing on a timer. Recovery then depends entirely on an
3841    /// external reachability edge (`flush_outbox_all` on reconnect/start, or
3842    /// a presence-online answer), i.e. on a bridge's presence-polling cadence
3843    /// — tens of seconds to minutes, and nothing at all for a headless
3844    /// consumer that never polls presence.
3845    ///
3846    /// Probing over the relay is self-limiting in every outcome, which is why
3847    /// no carrier guard is needed:
3848    /// - the peer is still offline and the relay's push fallback fails → a
3849    ///   fresh `DeliveryError` re-enters this park, escalating the interval
3850    ///   (15s → 600s cap) and re-emitting the non-terminal
3851    ///   [`Event::MessageUndeliverable`];
3852    /// - the relay's push fallback succeeds → no verdict is returned at all
3853    ///   and the probe becomes an ordinary in-flight send on the ACK ladder;
3854    /// - the peer is back → the probe *is* the delivery, which beats waiting
3855    ///   for any presence edge.
3856    ///
3857    /// The counter is per-*peer* while the probes are per-*message*: a burst
3858    /// of N DMs to one offline peer escalates the shared ladder once per
3859    /// park, so no individual message climbs 15s → 600s — the Nth already
3860    /// starts high on it. The compensating edge is delivery: an ACK for any
3861    /// one of the peer's DMs re-drives the rest immediately
3862    /// ([`Self::handle_ack_message`]) instead of leaving them on their
3863    /// escalated timers.
3864    ///
3865    /// Relay traffic is bounded differently in each of those branches, and
3866    /// the difference matters for capacity planning now that the whole
3867    /// internet-only fleet is on this path:
3868    /// - verdict branch: the escalation is the bound — one frame per interval
3869    ///   per parked *message*, settling at one per 600s;
3870    /// - accepted branch: the probe registers a fresh ACK at `retry_count` 0,
3871    ///   so it rides the ordinary ACK ladder (up to `max_retries` sends,
3872    ///   1s → 300s backoff, ~800s cumulative on the defaults) before
3873    ///   `try_repark_exhausted_dm` re-parks it at the escalated interval.
3874    ///
3875    /// The outbox lifetime bounds the entry itself — and note the probe
3876    /// refreshes `last_sent_at` on every send, so the sliding 7-day window
3877    /// stops binding and settlement moves out to the absolute cap
3878    /// ([`crate::constants::OUTBOX_ABSOLUTE_LIFETIME_FACTOR`] × the lifetime).
3879    /// Note also that a probe can be routed onto the relay when a mesh
3880    /// carrier is up — DORS has always been free to choose it — so this path
3881    /// is not new behavior for the relay, only newly reachable on
3882    /// internet-only devices.
3883    ///
3884    /// The counter increment is unconditional for the same reason: it is what
3885    /// arms [`Self::try_repark_exhausted_dm`], so a probe that exhausts its
3886    /// ACK budget re-parks instead of settling terminally.
3887    fn park_unreachable_dm(&mut self, message_id: &MessageId, recipient: &str, attempt_count: u32) {
3888        // Park: no pending ACK to time out, no backoff-scheduled resend.
3889        self.ack_manager.remove_ack(message_id);
3890        self.retry_queue.remove(&message_id.as_str());
3891
3892        let parks = {
3893            let count = self
3894                .dm_unreachable_parks
3895                .entry(recipient.to_string())
3896                .or_insert(0);
3897            *count = count.saturating_add(1);
3898            *count
3899        };
3900        // parks >= 1 here; shift clamped so 15 << 6 = 960 is the largest
3901        // pre-cap value — no overflow risk.
3902        let retry_in_secs = (WELCOME_NO_CARRIER_RETRY_SECS << (parks - 1).min(6))
3903            .min(WELCOME_UNREACHABLE_RETRY_CAP_SECS);
3904        let mut handed_to_mesh = 0usize;
3905        if let Some(entry) = self.outbox.get(message_id) {
3906            let message = entry.message.clone();
3907            // Ask the neighbors. The relay has just told us something no local
3908            // transport status can: that *this peer* is not on it. That is the
3909            // peer-specific fact `can_reach_without_carrying` cannot supply —
3910            // it answers yes for every recipient while an infrastructure
3911            // carrier is up, which is why the send-time offer never fires on an
3912            // online device and why the whole mixed-neighborhood case (an
3913            // online sender, a recipient reachable only across the mesh) used
3914            // to wait here for a peer that was never going to come online.
3915            //
3916            // Additive, exactly like the send-time offer: the outbox entry and
3917            // the probe below stand either way, since a neighbor taking a copy
3918            // is not proof of arrival. What settles it is the acknowledgement
3919            // coming back — over the mesh, for a message the relay could not
3920            // deliver, which is why parked DMs must be settleable without a
3921            // pending ACK (`settle_parked_dm_from_ack`). Without that arm this
3922            // offer would deliver messages the sender never learns about.
3923            //
3924            // Re-offering on every park is cheap by construction: neighbors
3925            // suppress a copy of an id they have already carried for the whole
3926            // `RelaySeenCache` retention window, the escalation (15s → 600s)
3927            // bounds how often we ask, and each offer spends the same own-send
3928            // tokens as any other frame of ours.
3929            handed_to_mesh = self.offer_to_mesh(&message);
3930            let _ = self.retry_queue.enqueue_with_delay(
3931                message,
3932                attempt_count,
3933                (retry_in_secs * 1000) as u64,
3934            );
3935        }
3936        debug!(
3937            message_id = %message_id,
3938            recipient = %recipient,
3939            retry_in_secs = retry_in_secs,
3940            parks = parks,
3941            handed_to_mesh,
3942            "Parked unreachable DM with escalating reachability probe"
3943        );
3944    }
3945
3946    /// Re-parks a plain DM whose ACK retry budget just exhausted while its
3947    /// recipient still holds a live unreachable-park counter, returning
3948    /// `true` when the caller (`handle_max_retries_exceeded`) must skip
3949    /// terminal settlement.
3950    ///
3951    /// A live counter means the relay declared the peer offline and no
3952    /// reachability edge has fired since (every edge clears it), so the
3953    /// exhausted budget was burnt by reachability probes — sends that succeed
3954    /// locally without proving the peer is back — not by a peer believed
3955    /// reachable. Over a mesh carrier such a probe can never earn a relay
3956    /// verdict at all; over the relay a verdict normally re-parks the message
3957    /// (resetting its ACK budget) well before exhaustion, so reaching here on
3958    /// an internet-only device means the probes were accepted for delivery
3959    /// but never answered. Settling terminally here would
3960    /// reintroduce the ~15-minute `message_failed` the park exists to
3961    /// prevent; re-parking keeps settlement reserved for delivery or
3962    /// outbox-lifetime expiry (the escalation cap bounds probe traffic, the
3963    /// outbox lifetime + absolute cap bound the entry itself).
3964    ///
3965    /// Deliberately narrow: pending connection requests keep their typed
3966    /// terminal path, welcomes keep their own lifecycle, and media chunks
3967    /// (never in `outbox`, and never counted) keep retry exhaustion →
3968    /// transfer abort → `MediaResendRequired`.
3969    pub(super) fn try_repark_exhausted_dm(&mut self, message_id: &MessageId) -> bool {
3970        if !self.is_parkable_plain_dm(message_id) {
3971            return false;
3972        }
3973        let Some(entry) = self.outbox.get(message_id) else {
3974            return false;
3975        };
3976        let recipient = entry.message.recipient.as_str();
3977        if !self.dm_unreachable_parks.contains_key(recipient) {
3978            return false;
3979        }
3980        let recipient = recipient.to_string();
3981        let attempt_count = entry.attempt_count;
3982        debug!(
3983            message_id = %message_id,
3984            recipient = %recipient,
3985            "ACK budget exhausted by reachability probes; re-parking instead of settling"
3986        );
3987        self.park_unreachable_dm(message_id, &recipient, attempt_count);
3988        true
3989    }
3990
3991    /// True when `message_id` is a plain DM eligible for the
3992    /// unreachable-park machinery: not a pending connection request (typed
3993    /// terminal path), not an MLS welcome (own lifecycle), and still present
3994    /// in the outbox. Media chunks live in `media_outbox`, so they never
3995    /// pass the outbox check.
3996    pub(super) fn is_parkable_plain_dm(&self, message_id: &MessageId) -> bool {
3997        if self
3998            .pending_connection_requests
3999            .contains_key(&message_id.as_str())
4000        {
4001            return false;
4002        }
4003        if self
4004            .find_welcome_peer_by_message_id(&message_id.as_str())
4005            .is_some()
4006        {
4007            return false;
4008        }
4009        self.outbox.contains_key(message_id)
4010    }
4011
4012    /// Cancels the in-flight probe state (pending ACK + scheduled retry) of
4013    /// every parkable plain DM addressed to `peer_id`, so a
4014    /// reachability-edge flush can re-drive them.
4015    ///
4016    /// Only acts when the peer holds a live unreachable-park counter: a
4017    /// live counter means the relay declared the peer offline and no edge
4018    /// has fired since, so a pending ACK on the peer's DMs belongs to a
4019    /// reachability probe — a send that locally succeeded but has not been
4020    /// answered — not to a delivery in good standing.
4021    /// Without this cancel, the flush edges' awaiting-ACK filters skip
4022    /// exactly the probing messages while still clearing the counter,
4023    /// stranding them on minutes-scale ACK backoff after the relay already
4024    /// reported the peer online — and, with the counter gone, exposing them
4025    /// to terminal settlement on exhaustion (`try_repark_exhausted_dm`
4026    /// finds nothing). Canceling an ACK that was genuinely about to arrive
4027    /// is safe: receivers dedupe and re-ACK duplicates.
4028    ///
4029    /// Does NOT clear the counter itself — the flush edge owns that reset,
4030    /// after this runs.
4031    pub(super) fn cancel_probe_state_for_parked_peer(&mut self, peer_id: &str) {
4032        if !self.dm_unreachable_parks.contains_key(peer_id) {
4033            return;
4034        }
4035        let probing: Vec<MessageId> = self
4036            .outbox
4037            .values()
4038            .filter(|entry| entry.message.recipient.as_str() == peer_id)
4039            .map(|entry| entry.message.id.clone())
4040            .collect();
4041        for message_id in probing {
4042            if !self.is_parkable_plain_dm(&message_id) {
4043                continue;
4044            }
4045            self.ack_manager.remove_ack(&message_id);
4046            self.retry_queue.remove(&message_id.as_str());
4047        }
4048    }
4049
4050    /// Classifies an outbound internet frame as a server-plane control op the
4051    /// platform bridge should translate to (or mirror as) a relay-native
4052    /// message instead of an opaque `SendMessage`.
4053    ///
4054    /// Returns `(op, payload)` where `payload` is the JSON after the prefix
4055    /// (empty string for payload-less ops). `None` means normal traffic —
4056    /// send verbatim.
4057    ///
4058    /// Ops and their bridge semantics:
4059    /// - `group_relay_register` / `group_relay_broadcast` — self-addressed
4060    ///   relay hints (`send_group_message` optimization); REPLACE with
4061    ///   relay-native `CreateGroup`+member deltas / `SendGroupMessage`. The
4062    ///   relay does not intercept content prefixes, so untranslated frames
4063    ///   would be echoed back uselessly.
4064    /// - `group_mls_leave` — TAP, don't replace: the per-member leave
4065    ///   notification must still be delivered verbatim, but the bridge also
4066    ///   sends one relay-native `LeaveGroup` so the relay's group registry
4067    ///   (which feeds invite links and broadcast fan-out) doesn't go stale.
4068    ///
4069    /// Connection ops (`__CONN_REQ__`/`__CONN_ACC__`/`__CONN_REJ__`/
4070    /// `__CONN_CAN__`) deliberately do NOT classify: they ship verbatim as
4071    /// opaque `SendMessage` frames so the Ed25519 control signature in the
4072    /// message metadata survives to the receiver's security gate. The former
4073    /// relay-native translation (`SendConnectionRequest` & co.) rebuilt them
4074    /// unsigned on the receiving bridge, which the gate now rejects
4075    /// unconditionally — control traffic must be signed. Verbatim
4076    /// also gains the relay's push-notification fallback for offline
4077    /// recipients, which the relay-native connection frames never had. The
4078    /// cost is that pre-SDK relay clients (which only speak the relay-native
4079    /// frames) no longer interoperate with connection ops — an accepted
4080    /// trade: the SDK's internet transport is the only supported relay
4081    /// client.
4082    ///
4083    /// Only frames *originated by this device* classify. The mesh relays
4084    /// third-party messages verbatim (`try_relay_message`). The relay-hint
4085    /// ops additionally require the self-addressed recipient (that is how
4086    /// the core marks them as hints rather than traffic), so they classify
4087    /// only when sender AND recipient are both this device.
4088    ///
4089    /// Self-origination is proven by `hop_count == 0`, not by the `sender`
4090    /// field: `sender` is an unauthenticated wire field, so a mesh peer
4091    /// could forge `sender == self` on a frame we then relay — but
4092    /// `try_relay_message` increments the hop before re-sending, while every
4093    /// locally-originated frame leaves `send_internal_message` at hop 0.
4094    /// (Defense in depth: the receive loop also drops inbound frames
4095    /// claiming our own origin before they can reach the relay path.)
4096    pub fn internet_control_op(&self, message: &Message) -> Option<(&'static str, String)> {
4097        if message.hop_count.value() != 0 {
4098            return None;
4099        }
4100        let content = message.content.as_str();
4101        if message.sender.as_str() == self.local_id {
4102            if let Some(payload) = content.strip_prefix(internal_prefixes::GROUP_MLS_LEAVE) {
4103                return Some(("group_mls_leave", payload.to_string()));
4104            }
4105        }
4106        if message.sender.as_str() == self.local_id && message.recipient.as_str() == self.local_id {
4107            if let Some(payload) = content.strip_prefix(internal_prefixes::GROUP_RELAY_REGISTER) {
4108                return Some(("group_relay_register", payload.to_string()));
4109            }
4110            if let Some(payload) = content.strip_prefix(internal_prefixes::GROUP_RELAY_BROADCAST) {
4111                return Some(("group_relay_broadcast", payload.to_string()));
4112            }
4113        }
4114        None
4115    }
4116
4117    pub(super) fn handle_outbound_media_chunk_delivered(&mut self, message_id: &MessageId) {
4118        let Some((file_id, chunk_index)) = self.outbound_media_chunks.remove(message_id) else {
4119            return;
4120        };
4121
4122        if let Some(window) = self.outbound_media_windows.get_mut(&file_id) {
4123            window.on_chunk_ack(chunk_index);
4124        }
4125
4126        let Some(transfer) = self.outbound_media_transfers.get_mut(&file_id) else {
4127            return;
4128        };
4129
4130        transfer.delivered_chunks.insert(chunk_index);
4131        transfer.last_updated_at = Instant::now();
4132
4133        let delivered_chunks = transfer.delivered_chunks.len() as u32;
4134        let total_chunks = transfer.total_chunks;
4135        let content_type = transfer.content_type;
4136        let recipient = transfer.recipient.clone();
4137        let completed = delivered_chunks == total_chunks;
4138        let carries_data = transfer.data_purpose.is_some();
4139
4140        if !carries_data {
4141            if let Ok(state) = lock_shared_state(&self.shared_state) {
4142                state.emit_event(Event::file_progress(
4143                    file_id.clone(),
4144                    delivered_chunks,
4145                    total_chunks,
4146                ));
4147                if completed {
4148                    state.emit_event(Event::media_sent(file_id.clone(), content_type, recipient));
4149                }
4150            }
4151        }
4152
4153        if completed {
4154            self.outbound_media_transfers.remove(&file_id);
4155            self.outbound_media_windows.remove(&file_id);
4156            self.remove_media_descriptor(&file_id);
4157        }
4158    }
4159
4160    pub(super) fn handle_outbound_media_chunk_failed(
4161        &mut self,
4162        message_id: &MessageId,
4163        reason: &str,
4164    ) {
4165        let Some((file_id, _chunk_index)) = self.outbound_media_chunks.remove(message_id) else {
4166            return;
4167        };
4168        self.abort_outbound_media_transfer(&file_id, reason);
4169    }
4170
4171    /// Aborts an active outbound media transfer: removes all transfer
4172    /// tracking (freeing its per-peer transfer slot) and emits
4173    /// [`Event::MediaSendFailed`] so the app learns the transfer will never
4174    /// complete. Idempotent — a second call for the same `file_id` is a no-op.
4175    ///
4176    /// A data-purposed transfer is the one exception to the event: it has no
4177    /// application-facing identity, so the report is withheld rather than
4178    /// naming a `file_id` the app never saw.
4179    pub(super) fn abort_outbound_media_transfer(&mut self, file_id: &str, reason: &str) {
4180        let Some(transfer) = self.outbound_media_transfers.remove(file_id) else {
4181            return;
4182        };
4183        self.outbound_media_chunks
4184            .retain(|_, (candidate_file_id, _)| candidate_file_id.as_str() != file_id);
4185        self.outbound_media_windows.remove(file_id);
4186        // A terminal abort is settled state — the app hears MediaSendFailed
4187        // now, so a MediaResendRequired for it after a restart would be
4188        // stale noise.
4189        self.remove_media_descriptor(file_id);
4190        warn!(
4191            file_id = %file_id,
4192            reason = %reason,
4193            "Aborting outbound media transfer"
4194        );
4195        // A data-purposed transfer has no application-facing identity to
4196        // fail, so reporting one would name a file_id the app has never
4197        // seen. The requester's own fetch expires and can be asked again,
4198        // and a snapshot is re-offered by the next anti-entropy exchange, so
4199        // nothing is stranded by staying quiet here.
4200        if transfer.data_purpose.is_some() {
4201            debug!(
4202                file_id = %file_id,
4203                reason = %reason,
4204                "A data-layer transfer was aborted; the asking side will retry"
4205            );
4206            return;
4207        }
4208        if let Ok(state) = lock_shared_state(&self.shared_state) {
4209            state.emit_event(Event::media_send_failed(
4210                file_id.to_string(),
4211                transfer.recipient,
4212                reason.to_string(),
4213            ));
4214        }
4215    }
4216
4217    pub(super) fn cleanup_stale_media_state(&mut self, max_age: StdDuration) {
4218        let now = Instant::now();
4219
4220        self.pending_media_metadata
4221            .retain(|_, metadata| now.duration_since(metadata.last_updated_at) <= max_age);
4222
4223        let stale_outbound_file_ids: HashSet<String> = self
4224            .outbound_media_transfers
4225            .iter()
4226            .filter_map(|(file_id, transfer)| {
4227                if now.duration_since(transfer.last_updated_at) > max_age {
4228                    return Some(file_id.clone());
4229                }
4230                None
4231            })
4232            .collect();
4233
4234        if stale_outbound_file_ids.is_empty() {
4235            return;
4236        }
4237
4238        self.outbound_media_transfers
4239            .retain(|file_id, _| !stale_outbound_file_ids.contains(file_id));
4240        self.outbound_media_chunks
4241            .retain(|_, (file_id, _)| !stale_outbound_file_ids.contains(file_id));
4242        self.outbound_media_windows
4243            .retain(|file_id, _| !stale_outbound_file_ids.contains(file_id));
4244        for file_id in &stale_outbound_file_ids {
4245            self.remove_media_descriptor(file_id);
4246        }
4247    }
4248
4249    // ========================================================================
4250    // ACK
4251    // ========================================================================
4252
4253    pub(super) fn send_delivery_ack(
4254        &mut self,
4255        message: &Message,
4256        inbound_transport: TransportType,
4257    ) -> Result<()> {
4258        let sender = UserId::new(&self.local_id)?;
4259        let recipient = message.sender.clone();
4260        let app_id = AppId::new(&self.config.app_id)?;
4261        let ttl = TTL::new(self.config.initial_ttl).unwrap_or_else(|_| TTL::default());
4262
4263        let ack_message = Message::builder(sender, recipient, app_id)
4264            .content(String::new())
4265            .priority(MessagePriority::Low)
4266            .ttl(ttl)
4267            .requires_ack(false)
4268            .metadata(ACK_FOR_KEY, message.id.as_str())
4269            .metadata(ACK_HOP_COUNT_KEY, message.hop_count.value().to_string())
4270            .metadata(ACK_TRANSPORT_KEY, Self::transport_label(inbound_transport))
4271            .build();
4272
4273        self.route_ack(&ack_message, inbound_transport)
4274    }
4275
4276    /// Gets an acknowledgement back to whoever sent us the message it answers.
4277    ///
4278    /// The single route-selection point for every delivery ACK, direct or
4279    /// group. Keeping one ladder is deliberate: it has four callers across two
4280    /// builders, and the failure this ordering exists to prevent is silent —
4281    /// a copy of the ladder that drifted would look fine and lose answers.
4282    ///
4283    /// Three routes, in order:
4284    ///
4285    /// 1. **The link it arrived on**, when that carrier can actually address
4286    ///    the sender. Preferred because it is known to work for this peer.
4287    /// 2. **The mesh**, when the sender is not a direct mesh neighbor and
4288    ///    either nothing we hold reaches them directly *or* the message itself
4289    ///    arrived over a mesh carrier: the answer travels back the way the
4290    ///    message came, carried by the devices in between. Without this,
4291    ///    multi-hop delivery looks like failure to a sender who retransmits a
4292    ///    message we already have and read.
4293    /// 3. **DORS**, which picks among whatever else is available.
4294    ///
4295    /// The arrival-over-mesh half of step 2 is what makes this work in a mixed
4296    /// neighborhood. A frame that reached us across the mesh came from someone
4297    /// the mesh can reach and — having failed step 1 — someone we cannot hand it
4298    /// straight back to, which says nothing about whether *our* internet is up.
4299    /// Deciding from local carrier status alone, an online recipient answers a
4300    /// mesh-delivered message over the relay, where an offline sender cannot see
4301    /// it: delivered and read, reported failed. In that case the answer goes to
4302    /// the mesh **and** on to step 3, since an online sender is equally
4303    /// plausible and a duplicate acknowledgement costs one frame the sender's
4304    /// ack handling already absorbs. When nothing reaches the sender directly
4305    /// the mesh remains the whole answer and step 3 is skipped, as before.
4306    ///
4307    /// Step 1 is *gated on addressability* rather than on a send error, and
4308    /// that gate is load-bearing. A transport returning `Ok` is not evidence
4309    /// the frame can arrive: Wi-Fi Direct enqueues for any recipient, and it is
4310    /// the preferred mesh carrier — so on the last hop of a forwarded frame the
4311    /// answer would be queued for a link that never drains, reported as sent,
4312    /// and step 2 would never run. The sender's retransmissions would take the
4313    /// same path every time, ending in a failure report for a delivered
4314    /// message.
4315    fn route_ack(&mut self, ack_message: &Message, inbound_transport: TransportType) -> Result<()> {
4316        let ack_to = ack_message.recipient.as_str().to_string();
4317
4318        if self
4319            .transport_manager
4320            .can_address_via(inbound_transport, &ack_to)
4321            && self
4322                .transport_manager
4323                .send_via_transport(ack_message, inbound_transport)
4324                .is_ok()
4325        {
4326            return Ok(());
4327        }
4328
4329        debug!(
4330            ack_to = %ack_to,
4331            inbound_transport = ?inbound_transport,
4332            "Inbound transport cannot answer this sender; looking for another route"
4333        );
4334
4335        // No route at all without help: the mesh is the entire answer.
4336        let no_direct_route = !self.can_reach_recipient(&ack_to);
4337        // Reached us through other devices, and we cannot hand it straight
4338        // back: the way it came is a route we know exists, whatever our own
4339        // carriers claim.
4340        let carried_to_us = TransportManager::is_mesh_transport(inbound_transport)
4341            && !self.transport_manager.mesh_can_address(&ack_to);
4342
4343        let mut handed_to_mesh = 0usize;
4344        if no_direct_route || carried_to_us {
4345            handed_to_mesh = self.offer_to_mesh(ack_message);
4346            if handed_to_mesh > 0 {
4347                debug!(
4348                    ack_to = %ack_to,
4349                    handed_to_mesh,
4350                    "Handed the acknowledgement to the mesh"
4351                );
4352                if no_direct_route {
4353                    return Ok(());
4354                }
4355            }
4356        }
4357
4358        if self.transport_manager.send(ack_message).is_ok() {
4359            return Ok(());
4360        }
4361
4362        // Neighbors took it, so the answer is on its way even though nothing
4363        // else could carry it.
4364        if handed_to_mesh > 0 {
4365            return Ok(());
4366        }
4367
4368        Err(Error::Other(format!(
4369            "no route back to {} for delivery acknowledgement",
4370            ack_to
4371        )))
4372    }
4373
4374    /// Builds and sends a delivery ACK from the group-message drain, which
4375    /// holds only the acked id and the original wire sender (not the full
4376    /// inbound [`Message`]). Mirrors [`Self::send_delivery_ack`]; the hop count
4377    /// is unknown at drain time and reported as 0 (sender-side metrics only).
4378    pub(crate) fn send_group_delivery_ack(
4379        &mut self,
4380        ack_to: &str,
4381        acked_message_id: &str,
4382        inbound_transport: TransportType,
4383    ) -> Result<()> {
4384        let sender = UserId::new(&self.local_id)?;
4385        let recipient = UserId::new(ack_to)?;
4386        let app_id = AppId::new(&self.config.app_id)?;
4387        let ttl = TTL::new(self.config.initial_ttl).unwrap_or_else(|_| TTL::default());
4388
4389        let ack_message = Message::builder(sender, recipient, app_id)
4390            .content(String::new())
4391            .priority(MessagePriority::Low)
4392            .ttl(ttl)
4393            .requires_ack(false)
4394            .metadata(ACK_FOR_KEY, acked_message_id)
4395            .metadata(ACK_HOP_COUNT_KEY, "0")
4396            .metadata(ACK_TRANSPORT_KEY, Self::transport_label(inbound_transport))
4397            .build();
4398
4399        // Same ladder as a direct-message ACK, mesh fallback included: a group
4400        // frame reaches its members over the same hops a DM does.
4401        self.route_ack(&ack_message, inbound_transport)
4402    }
4403
4404    pub(super) fn handle_ack_message(&mut self, message: &Message) {
4405        if let Some(ack_for) = message.metadata.get(ACK_FOR_KEY) {
4406            if let Ok(message_id) = MessageId::from_str(ack_for) {
4407                // Whoever answers has to be whoever we sent it to. An
4408                // acknowledgement names a message id and nothing else ties it
4409                // to a delivery, so without this any third party that merely
4410                // saw the frame — a mesh carrier that took a copy, the relay —
4411                // settles it: the outbox entry is dropped, the retries stop and
4412                // the app is told `message_delivered` for a message that
4413                // arrived nowhere. Refusing delivery is something those parties
4414                // could always do; a false confirmation is the strictly worse
4415                // one, because it is the version the sender never finds out
4416                // about.
4417                //
4418                // Checked before anything is removed. The settle work below
4419                // starts by taking the pending ACK, and a frame rejected after
4420                // that point would have already cost the message its retry
4421                // record — turning a refused acknowledgement into the silent
4422                // loss this exists to prevent.
4423                //
4424                // Safe on this path — which every delivery passes through —
4425                // because delivery already implies the equality: a device
4426                // processes a frame only when its own `local_id` equals the
4427                // frame's recipient (`receive_message` forwards anything else),
4428                // and the acknowledgement it builds carries that same
4429                // `local_id` as its sender. A genuine confirmation therefore
4430                // matches whatever identifier namespace the app addresses peers
4431                // in — the profile before MLS initialization, the `off1…`
4432                // address after it.
4433                //
4434                // It is **not** authentication and must not be read as one: an
4435                // acknowledgement carries no signature (empty content is not a
4436                // security-gated prefix, and the receive loop handles it before
4437                // the gate would run anyway), so a forger who saw the frame saw
4438                // its recipient too and can simply write that name. What this
4439                // removes is the unattributed forgery, and the divergence
4440                // between the two settle paths below. Binding an
4441                // acknowledgement to its sender for real needs signed acks.
4442                if !self.ack_is_from_the_message_recipient(&message_id, message) {
4443                    debug!(
4444                        message_id = %message_id,
4445                        claimed_sender = %message.sender,
4446                        "Ignoring an acknowledgement from someone this message was not sent to"
4447                    );
4448                    return;
4449                }
4450
4451                // A delivery ack settles an outbound connection request:
4452                // the recipient provably received it, so a later stale
4453                // recipient_unreachable signal must not fire a false
4454                // ConnectionRequestUndeliverable. Outside the ack_manager
4455                // branch — a duplicate ack (second transport) or one
4456                // arriving after retry exhaustion is still proof.
4457                self.pending_connection_requests
4458                    .remove(&message_id.as_str());
4459                if let Some(pending) = self.ack_manager.remove_ack(&message_id) {
4460                    let latency = Utc::now()
4461                        .signed_duration_since(pending.sent_at)
4462                        .num_milliseconds()
4463                        .max(0) as u64;
4464
4465                    let hop_count = message
4466                        .metadata
4467                        .get(ACK_HOP_COUNT_KEY)
4468                        .and_then(|v| v.parse::<u8>().ok())
4469                        .unwrap_or(0);
4470
4471                    let transport = message
4472                        .metadata
4473                        .get(ACK_TRANSPORT_KEY)
4474                        .map(|label| Self::transport_from_label(label))
4475                        .unwrap_or(TransportType::BLE);
4476
4477                    // Emit event if we can lock the state, but don't fail if we can't
4478                    if let Ok(state) = lock_shared_state(&self.shared_state) {
4479                        state.emit_event(Event::message_delivered(
4480                            message_id.clone(),
4481                            latency,
4482                            hop_count,
4483                            transport,
4484                        ));
4485                        drop(state);
4486                    } else {
4487                        error!(
4488                            "Failed to lock shared state for ACK event, skipping event emission"
4489                        );
4490                    }
4491
4492                    self.transport_manager.reset_retry_count(transport);
4493                    self.transport_manager.record_delivery_success(
4494                        transport,
4495                        latency.min(u32::MAX as u64) as u32,
4496                        hop_count,
4497                    );
4498                    self.handle_outbound_media_chunk_delivered(&message_id);
4499                    self.retry_queue.remove(&message_id.as_str());
4500                    if let Some(entry) = self.remove_outbox_entry(&message_id) {
4501                        // Delivery proves reachability, so the escalating
4502                        // unreachable-probe interval starts over. Clearing the
4503                        // counter is not enough on its own: it is per-peer
4504                        // while the probes are per-message, so a burst of DMs
4505                        // to an offline peer escalates the shared ladder once
4506                        // per park and leaves the later ones scheduled minutes
4507                        // out (at the 600s cap after seven). Delivery of any
4508                        // one of them proves the rest can go now, and on a
4509                        // consumer that never polls presence this ACK is the
4510                        // only edge that will say so. Flushing owns the
4511                        // counter reset — including restoring it when every
4512                        // re-drive fails — so this must not pre-clear it.
4513                        //
4514                        // The override is our own record of the transport the
4515                        // delivered send went out on (`last_transport`), which
4516                        // this ACK just proved reaches the peer — genuine
4517                        // per-transport reachability, pinning the re-drive the
4518                        // way presence-online pins Internet. Deliberately NOT
4519                        // the ACK's `transport` above: ACK_TRANSPORT_KEY is
4520                        // peer-supplied, and `from_label` maps any absent,
4521                        // stale, or hostile label to BLE — pinning a burst of
4522                        // sibling sends to a carrier chosen by the peer.
4523                        // Filtered on local availability (the transport may
4524                        // have dropped since the send); `None` falls back to
4525                        // DORS, so a missing carrier costs routing freedom,
4526                        // never the re-drive itself.
4527                        let recipient = entry.message.recipient.as_str().to_string();
4528                        if self.dm_unreachable_parks.contains_key(&recipient) {
4529                            let redrive_via = entry.last_transport.filter(|t| {
4530                                self.transport_manager
4531                                    .get_available_transports()
4532                                    .contains_key(t)
4533                            });
4534                            self.flush_outbox_for_peer_via(&recipient, redrive_via);
4535                        }
4536                    }
4537                } else {
4538                    // No pending ACK. Ordinarily that means a duplicate answer
4539                    // or one arriving after the message already settled — but a
4540                    // *parked* DM has no pending ACK either, because parking
4541                    // removed it, and its delivery is the one this device has
4542                    // no other way to learn about.
4543                    self.settle_parked_dm_from_ack(&message_id, message);
4544                }
4545            }
4546        }
4547    }
4548
4549    /// Whether `ack` comes from the peer the message it answers was addressed
4550    /// to — the one thing about an acknowledgement that is not simply asserted
4551    /// by whoever sent it. See [`Self::handle_ack_message`] for what that does
4552    /// and does not buy.
4553    ///
4554    /// Both outboxes are consulted because both hold ack-bearing sends: plain
4555    /// DMs and control frames in `outbox`, media chunks in `media_outbox`.
4556    ///
4557    /// Answers `true` when neither holds the id, which is the ordinary shape of
4558    /// a duplicate answer arriving after the message already settled — there is
4559    /// no record left to judge against, and inventing a verdict would start
4560    /// dropping the answers this device has always accepted. The fail-open is
4561    /// not reachable by an attacker: it needs our own outbox entry to be gone,
4562    /// which nothing a peer sends can arrange.
4563    fn ack_is_from_the_message_recipient(&self, message_id: &MessageId, ack: &Message) -> bool {
4564        let Some(entry) = self
4565            .outbox
4566            .get(message_id)
4567            .or_else(|| self.media_outbox.get(message_id))
4568        else {
4569            return true;
4570        };
4571        ack.sender.as_str() == entry.message.recipient.as_str()
4572    }
4573
4574    /// Settles a parked DM whose acknowledgement arrived with no pending ACK to
4575    /// match it — the delivery a park can otherwise never learn about.
4576    ///
4577    /// Parking is what makes this necessary: it removes the pending ACK so the
4578    /// retry budget stops burning against a peer the relay says is offline. Once
4579    /// the park also hands the frame to the mesh
4580    /// ([`Self::park_unreachable_dm`]), a neighbor can carry it to a recipient
4581    /// the relay could not reach — and the answer coming back would land here,
4582    /// on the branch that used to drop it. The message would sit in the outbox
4583    /// being probed until its lifetime expired, delivered and read the whole
4584    /// time. The offer and this arm are one change; neither is correct alone.
4585    ///
4586    /// Gated three ways, because an acknowledgement is unauthenticated (it
4587    /// carries no internal prefix, so it never reaches the control gate, and it
4588    /// is handled before the security gate runs on the receive path):
4589    ///
4590    /// 1. the id is a parkable plain DM — welcomes keep their own lifecycle,
4591    ///    and media chunks are never parked, so they keep their pending ACK and
4592    ///    settle on the branch above. This gate does **not** exclude connection
4593    ///    requests, whatever [`Self::is_parkable_plain_dm`] looks like it
4594    ///    promises: the caller retires their typed tracking a few lines earlier
4595    ///    and unconditionally, so by the time that check runs the map no longer
4596    ///    holds the id. The outcome is the wanted one — a request whose typed
4597    ///    window has already lapsed, carried to its recipient and answered, was
4598    ///    delivered, and settling it is the same proof the pending-ACK branch
4599    ///    acts on for an already-untracked request — but it is the caller's
4600    ///    ordering that decides it, not this gate;
4601    /// 2. it is still in the outbox;
4602    /// 3. its recipient holds a live park counter, so the relay did declare this
4603    ///    peer unreachable and no edge has cleared it since.
4604    ///
4605    /// Attribution — that the answer comes from the recipient rather than from
4606    /// any party that handled the frame — is deliberately *not* a fourth gate
4607    /// here. [`Self::handle_ack_message`] now applies it to every
4608    /// acknowledgement it accepts, so both settle paths judge by one rule
4609    /// against one record; a copy of it here would be unreachable, and the kind
4610    /// of unreachable that reads as load-bearing to the next person.
4611    ///
4612    /// What remains after all of them is a forgery by someone who saw the frame
4613    /// *and* knows the relay refused it — the carriers and the relay, parties
4614    /// who can already deny delivery outright. What they gain is making a
4615    /// parked message look delivered rather than staying parked, which is the
4616    /// same class as forging an acknowledgement for an in-flight message.
4617    ///
4618    /// Reports latency end to end (from the first send) rather than per attempt:
4619    /// there is no pending record to measure the last attempt against, and for a
4620    /// message that waited out a park the honest number is how long delivery
4621    /// actually took. For the same reason no transport metric is recorded — the
4622    /// carrier label is peer-supplied and there is no attempt to attribute it
4623    /// to.
4624    fn settle_parked_dm_from_ack(&mut self, message_id: &MessageId, ack: &Message) {
4625        if !self.is_parkable_plain_dm(message_id) {
4626            return;
4627        }
4628        let Some(entry) = self.outbox.get(message_id) else {
4629            return;
4630        };
4631        let recipient = entry.message.recipient.as_str().to_string();
4632        if !self.dm_unreachable_parks.contains_key(&recipient) {
4633            return;
4634        }
4635
4636        let latency = Utc::now()
4637            .signed_duration_since(entry.first_sent_at)
4638            .num_milliseconds()
4639            .max(0) as u64;
4640        let hop_count = ack
4641            .metadata
4642            .get(ACK_HOP_COUNT_KEY)
4643            .and_then(|v| v.parse::<u8>().ok())
4644            .unwrap_or(0);
4645        let transport = ack
4646            .metadata
4647            .get(ACK_TRANSPORT_KEY)
4648            .map(|label| Self::transport_from_label(label))
4649            .unwrap_or(TransportType::BLE);
4650
4651        debug!(
4652            message_id = %message_id,
4653            recipient = %recipient,
4654            latency_ms = latency,
4655            "Parked DM was delivered after all; settling from its acknowledgement"
4656        );
4657
4658        self.retry_queue.remove(&message_id.as_str());
4659        let redrive_via = self.remove_outbox_entry(message_id).and_then(|entry| {
4660            // Same reasoning as the pending-ACK path: our own record of where
4661            // the delivered send went, never the peer-supplied label, and only
4662            // while that carrier is still up. The park counter stays for the
4663            // flush to reset, so a re-drive that fails everywhere restores it.
4664            entry.last_transport.filter(|t| {
4665                self.transport_manager
4666                    .get_available_transports()
4667                    .contains_key(t)
4668            })
4669        });
4670        if let Ok(state) = lock_shared_state(&self.shared_state) {
4671            state.emit_event(Event::message_delivered(
4672                message_id.clone(),
4673                latency,
4674                hop_count,
4675                transport,
4676            ));
4677            drop(state);
4678        } else {
4679            error!("Failed to lock shared state for parked-ACK event, skipping event emission");
4680        }
4681        // The peer answered, so the rest of their parked traffic can go now
4682        // rather than waiting out its own escalated probe timers.
4683        self.flush_outbox_for_peer_via(&recipient, redrive_via);
4684    }
4685
4686    pub(super) fn handle_missing_outbox_entry(
4687        &mut self,
4688        message_id: &MessageId,
4689        retry_count: u32,
4690    ) -> Result<()> {
4691        let state = lock_shared_state(&self.shared_state).map_err(|e| {
4692            error!(
4693                "Failed to lock shared state for missing outbox entry event: {}",
4694                e
4695            );
4696            e
4697        })?;
4698        state.emit_event(Event::message_failed(
4699            message_id.clone(),
4700            "Message missing from outbox (cannot retry)".to_string(),
4701            retry_count,
4702        ));
4703        drop(state);
4704
4705        self.handle_outbound_media_chunk_failed(message_id, "missing outbox entry");
4706        self.ack_manager.remove_ack(message_id);
4707        Ok(())
4708    }
4709
4710    // ========================================================================
4711    // EVENTS
4712    // ========================================================================
4713
4714    /// Emits a message sent event.
4715    pub(super) fn emit_message_sent_event(&self, message: &Message) -> Result<()> {
4716        let state = lock_shared_state(&self.shared_state).map_err(|e| {
4717            error!("Failed to lock shared state for message sent event: {}", e);
4718            e
4719        })?;
4720        state.emit_event(Event::message_sent(message));
4721        drop(state);
4722        Ok(())
4723    }
4724
4725    /// Emits a transport switched event if the transport changed.
4726    pub(super) fn emit_transport_switch_event(
4727        &self,
4728        previous_transport: Option<TransportType>,
4729        current_transport: Option<TransportType>,
4730    ) -> Result<()> {
4731        if current_transport != previous_transport {
4732            if let Some(new_transport) = current_transport {
4733                let state = lock_shared_state(&self.shared_state).map_err(|e| {
4734                    error!(
4735                        "Failed to lock shared state for transport switch event: {}",
4736                        e
4737                    );
4738                    e
4739                })?;
4740                state.emit_event(Event::transport_switched(
4741                    previous_transport,
4742                    new_transport,
4743                    "DORS selected better transport".to_string(),
4744                ));
4745                drop(state);
4746            }
4747        }
4748        Ok(())
4749    }
4750
4751    // ========================================================================
4752    // SOCIAL
4753    // ========================================================================
4754
4755    /// Sends a connection request to another user via any available transport.
4756    ///
4757    /// The request is routed through DORS, so it works over Internet, BLE, or WiFi Direct.
4758    ///
4759    /// # Arguments
4760    ///
4761    /// * `recipient` - The user ID of the recipient
4762    /// * `sender_name` - Display name of the sender
4763    /// * `key_package` - Optional MLS key package for encrypted session setup
4764    /// * `initial_message` - Optional first message shown with the request
4765    ///   (surfaced verbatim in the recipient's `ConnectionRequestReceived`
4766    ///   event; apps typically seed the conversation with it on accept).
4767    ///   At most 4096 UTF-8 bytes (`MAX_INITIAL_MESSAGE_BYTES`, crate-private)
4768    ///   — longer input is rejected with `Error::InvalidArgument`.
4769    ///
4770    /// # Encryption
4771    ///
4772    /// Connection requests are internal control messages sent in plaintext,
4773    /// exempt from `require_encryption` (same as key packages and welcome
4774    /// messages) — an `initial_message` is therefore NOT end-to-end
4775    /// encrypted and should be treated like the sender display name.
4776    pub fn send_connection_request(
4777        &mut self,
4778        recipient: &str,
4779        sender_name: &str,
4780        key_package: Option<Vec<u8>>,
4781        initial_message: Option<String>,
4782    ) -> Result<MessageId> {
4783        Self::validate_outbound_recipient(recipient)?;
4784        if let Some(msg) = initial_message.as_deref() {
4785            if msg.len() > MAX_INITIAL_MESSAGE_BYTES {
4786                return Err(Error::InvalidArgument(format!(
4787                    "initial_message exceeds {} bytes (got {})",
4788                    MAX_INITIAL_MESSAGE_BYTES,
4789                    msg.len()
4790                )));
4791            }
4792        }
4793
4794        // Connection requests are internal control messages (not user content),
4795        // so they are exempt from require_encryption — same as key packages.
4796        if self.is_user_blocked(recipient) {
4797            return Err(Error::UserBlocked(recipient.to_string()));
4798        }
4799
4800        let payload = ConnectionRequestPayload {
4801            sender_name: sender_name.to_string(),
4802            timestamp_ms: Utc::now().timestamp_millis(),
4803            key_package,
4804            initial_message,
4805        };
4806
4807        let serialized =
4808            serde_json::to_string(&payload).map_err(|e| Error::Serialization(e.to_string()))?;
4809        let content = format!("{}{}", internal_prefixes::CONN_REQUEST, serialized);
4810
4811        let message_id = self.send_internal_message(recipient, content, MessagePriority::High)?;
4812        self.track_pending_connection_request(&message_id, recipient);
4813        info!(recipient = %recipient, "Sent connection request");
4814        Ok(message_id)
4815    }
4816
4817    /// Records an outbound connection request so a later transport-level
4818    /// "recipient offline" verdict can be surfaced as a typed event
4819    /// (see `on_transport_send_failed`). TTL-prunes and caps the map here,
4820    /// on the write path, so it needs no periodic sweep.
4821    fn track_pending_connection_request(&mut self, message_id: &MessageId, recipient: &str) {
4822        let now = std::time::Instant::now();
4823        self.pending_connection_requests
4824            .retain(|_, p| now.duration_since(p.sent_at) <= PENDING_CONNECTION_REQUEST_TTL);
4825        if self.pending_connection_requests.len() >= MAX_PENDING_CONNECTION_REQUESTS {
4826            if let Some(oldest) = self
4827                .pending_connection_requests
4828                .iter()
4829                .min_by_key(|(_, p)| p.sent_at)
4830                .map(|(id, _)| id.clone())
4831            {
4832                self.pending_connection_requests.remove(&oldest);
4833            }
4834        }
4835        self.pending_connection_requests.insert(
4836            message_id.as_str(),
4837            PendingConnectionRequest {
4838                recipient: recipient.to_string(),
4839                sent_at: now,
4840            },
4841        );
4842    }
4843
4844    /// Accepts a connection request from another user via any available transport.
4845    ///
4846    /// The response is routed through DORS, so it works over Internet, BLE, or WiFi Direct.
4847    ///
4848    /// # Arguments
4849    ///
4850    /// * `recipient` - The user ID of the original requester
4851    /// * `accepter_name` - Display name of the accepting party
4852    /// * `key_package` - Optional MLS key package for encrypted session setup
4853    ///
4854    /// # Encryption
4855    ///
4856    /// Connection accepts are internal control messages sent in plaintext,
4857    /// exempt from `require_encryption` (same as key packages and welcome messages).
4858    pub fn accept_connection_request(
4859        &mut self,
4860        recipient: &str,
4861        accepter_name: &str,
4862        key_package: Option<Vec<u8>>,
4863    ) -> Result<MessageId> {
4864        Self::validate_outbound_recipient(recipient)?;
4865        // Accept messages are internal control messages (not user content),
4866        // so they are exempt from require_encryption — same as key packages.
4867        if self.is_user_blocked(recipient) {
4868            return Err(Error::UserBlocked(recipient.to_string()));
4869        }
4870
4871        let payload = ConnectionAcceptedPayload {
4872            accepted_by_name: accepter_name.to_string(),
4873            timestamp_ms: Utc::now().timestamp_millis(),
4874            key_package,
4875        };
4876
4877        let serialized =
4878            serde_json::to_string(&payload).map_err(|e| Error::Serialization(e.to_string()))?;
4879        let content = format!("{}{}", internal_prefixes::CONN_ACCEPT, serialized);
4880
4881        let message_id = self.send_internal_message(recipient, content, MessagePriority::High)?;
4882        info!(recipient = %recipient, "Accepted connection request");
4883        Ok(message_id)
4884    }
4885
4886    /// Rejects a connection request from another user via any available transport.
4887    ///
4888    /// The response is routed through DORS, so it works over Internet, BLE, or WiFi Direct.
4889    ///
4890    /// # Arguments
4891    ///
4892    /// * `recipient` - The user ID of the original requester
4893    ///
4894    /// # Encryption
4895    ///
4896    /// Connection rejects are internal control messages sent in plaintext,
4897    /// exempt from `require_encryption` (same as key packages and welcome messages).
4898    pub fn reject_connection_request(&mut self, recipient: &str) -> Result<MessageId> {
4899        Self::validate_outbound_recipient(recipient)?;
4900        // Reject messages are internal control messages (not user content),
4901        // so they are exempt from require_encryption — same as key packages.
4902        if self.is_user_blocked(recipient) {
4903            return Err(Error::UserBlocked(recipient.to_string()));
4904        }
4905
4906        let content = internal_prefixes::CONN_REJECT.to_string();
4907
4908        let message_id = self.send_internal_message(recipient, content, MessagePriority::High)?;
4909        info!(recipient = %recipient, "Rejected connection request");
4910        Ok(message_id)
4911    }
4912
4913    /// Cancels a previously sent connection request via any available transport.
4914    ///
4915    /// The cancellation is routed through DORS, so it works over Internet, BLE, or WiFi Direct.
4916    ///
4917    /// # Arguments
4918    ///
4919    /// * `recipient` - The user ID of the original request recipient
4920    ///
4921    /// # Encryption
4922    ///
4923    /// Connection cancellations are internal control messages sent in plaintext,
4924    /// exempt from `require_encryption` (same as key packages and welcome messages).
4925    pub fn cancel_connection_request(&mut self, recipient: &str) -> Result<MessageId> {
4926        Self::validate_outbound_recipient(recipient)?;
4927        if self.is_user_blocked(recipient) {
4928            return Err(Error::UserBlocked(recipient.to_string()));
4929        }
4930
4931        let content = internal_prefixes::CONN_CANCEL.to_string();
4932
4933        let message_id = self.send_internal_message(recipient, content, MessagePriority::High)?;
4934        info!(recipient = %recipient, "Cancelled connection request");
4935        Ok(message_id)
4936    }
4937
4938    /// Sends a presence update to a single peer (unicast).
4939    ///
4940    /// This sends to one recipient at a time. To broadcast presence to multiple
4941    /// peers, the caller must invoke this method once per peer.
4942    ///
4943    /// # Arguments
4944    ///
4945    /// * `recipient` - The user ID of the peer to notify
4946    /// * `status` - Presence status
4947    pub fn send_presence_update(
4948        &mut self,
4949        recipient: &str,
4950        status: PresenceStatus,
4951    ) -> Result<MessageId> {
4952        Self::validate_outbound_recipient(recipient)?;
4953        if self.is_user_blocked(recipient) {
4954            return Err(Error::UserBlocked(recipient.to_string()));
4955        }
4956
4957        let payload = PresencePayload {
4958            status,
4959            timestamp_ms: Utc::now().timestamp_millis(),
4960        };
4961
4962        let serialized =
4963            serde_json::to_string(&payload).map_err(|e| Error::Serialization(e.to_string()))?;
4964        let content = format!("{}{}", internal_prefixes::PRESENCE, serialized);
4965
4966        let message_id = self.send_internal_message(recipient, content, MessagePriority::Low)?;
4967        debug!(recipient = %recipient, status = ?status, "Sent presence update");
4968        Ok(message_id)
4969    }
4970
4971    /// Sends a typing indicator to a peer.
4972    ///
4973    /// # Arguments
4974    ///
4975    /// * `recipient` - The user ID of the peer to notify
4976    /// * `conversation_id` - Opaque conversation key, carried to the peer and
4977    ///   echoed back on its event. Never parsed or routed on. Conventionally
4978    ///   the peer's address for a DM and the group id for a group; any stable
4979    ///   string works, but a display name does not (those change).
4980    /// * `is_typing` - Whether the user started or stopped typing
4981    pub fn send_typing_indicator(
4982        &mut self,
4983        recipient: &str,
4984        conversation_id: &str,
4985        is_typing: bool,
4986    ) -> Result<MessageId> {
4987        Self::validate_outbound_recipient(recipient)?;
4988        if self.is_user_blocked(recipient) {
4989            return Err(Error::UserBlocked(recipient.to_string()));
4990        }
4991        if conversation_id.is_empty() {
4992            return Err(Error::InvalidArgument(
4993                "conversation_id must not be empty".to_string(),
4994            ));
4995        }
4996
4997        let payload = TypingIndicatorPayload {
4998            conversation_id: conversation_id.to_string(),
4999            is_typing,
5000            timestamp_ms: Utc::now().timestamp_millis(),
5001        };
5002
5003        let serialized =
5004            serde_json::to_string(&payload).map_err(|e| Error::Serialization(e.to_string()))?;
5005        let content = format!("{}{}", internal_prefixes::TYPING_INDICATOR, serialized);
5006
5007        let message_id = self.send_internal_message(recipient, content, MessagePriority::Low)?;
5008        debug!(recipient = %recipient, is_typing = %is_typing, "Sent typing indicator");
5009        Ok(message_id)
5010    }
5011
5012    /// Sends a read receipt to a peer, indicating that the given messages have been read.
5013    ///
5014    /// # Arguments
5015    ///
5016    /// * `recipient` - The user ID of the peer who sent the messages
5017    /// * `message_ids` - IDs of the messages that were read (max 256)
5018    pub fn send_read_receipt(
5019        &mut self,
5020        recipient: &str,
5021        message_ids: Vec<String>,
5022    ) -> Result<MessageId> {
5023        Self::validate_outbound_recipient(recipient)?;
5024        if self.is_user_blocked(recipient) {
5025            return Err(Error::UserBlocked(recipient.to_string()));
5026        }
5027        if message_ids.is_empty() {
5028            return Err(Error::InvalidArgument(
5029                "message_ids must not be empty".to_string(),
5030            ));
5031        }
5032        if message_ids.len() > MAX_READ_RECEIPT_IDS {
5033            return Err(Error::InvalidArgument(format!(
5034                "message_ids exceeds maximum of {MAX_READ_RECEIPT_IDS}"
5035            )));
5036        }
5037
5038        let payload = ReadReceiptPayload {
5039            message_ids,
5040            timestamp_ms: Utc::now().timestamp_millis(),
5041        };
5042
5043        let serialized =
5044            serde_json::to_string(&payload).map_err(|e| Error::Serialization(e.to_string()))?;
5045        let content = format!("{}{}", internal_prefixes::READ_RECEIPT, serialized);
5046
5047        let message_id = self.send_internal_message(recipient, content, MessagePriority::Low)?;
5048        debug!(recipient = %recipient, "Sent read receipt");
5049        Ok(message_id)
5050    }
5051
5052    /// Builds the advertisement body carried by a key package, whether it is
5053    /// pushed to a peer or published for a stranger to fetch.
5054    ///
5055    /// Shared so the two paths cannot drift: a published record that advertised
5056    /// a different capability set from a pushed one would make a peer's
5057    /// behaviour depend on which way they first met us.
5058    pub(crate) fn build_key_package_payload(
5059        &self,
5060        key_pkg: &offline_protocol_mls::KeyPackageBundle,
5061        session_reset: bool,
5062    ) -> KeyPackagePayload {
5063        KeyPackagePayload {
5064            user_id: self.local_id.clone(),
5065            key_package_data: key_pkg.key_package_data.clone(),
5066            remaining_lifetime_ms: key_pkg.remaining_lifetime_ms(),
5067            timestamp_ms: Utc::now().timestamp_millis() as u64,
5068            session_reset,
5069            wire_versions: if self.config.transport.binary_wire_enabled {
5070                vec![offline_protocol_core::WIRE_VERSION_V1]
5071            } else {
5072                Vec::new()
5073            },
5074            env_versions: if self.config.encryption.compact_envelope_enabled {
5075                vec![MLS_ENVELOPE_COMPACT_V1]
5076            } else {
5077                Vec::new()
5078            },
5079            rich_versions: if self.config.encryption.rich_payload_enabled {
5080                vec![RICH_PAYLOAD_V1]
5081            } else {
5082                Vec::new()
5083            },
5084            data_versions: self.advertised_data_versions(),
5085            // Present only when the Nostr transport is installed. Advertised
5086            // regardless of the sealing kill switch, which gates what *we*
5087            // publish, not what a peer may seal to us: withholding it would
5088            // downgrade the peer's traffic to the bootstrap key as a side
5089            // effect of a local setting.
5090            nostr_pubkey: self.transport_manager.nostr_public_key(),
5091        }
5092    }
5093
5094    /// Advertises this device's key package to `peer_id`.
5095    ///
5096    /// The package comes from the peer-keyed pool
5097    /// ([`MlsManager::take_push_key_package`]): `peer_id` gets its own init key,
5098    /// re-handed on every push until a Welcome consumes it. Before that, this
5099    /// path handed the *same* package to every peer until somebody used it,
5100    /// which both weakened forward secrecy at session establishment (one
5101    /// compromised init key opens every Welcome built against it) and made the
5102    /// second peer's Welcome permanently unprocessable.
5103    pub(crate) fn send_key_package_to(&mut self, peer_id: &str, session_reset: bool) -> Result<()> {
5104        let mls = self.mls_manager.as_ref().ok_or(Error::MlsNotInitialized)?;
5105
5106        let push = {
5107            let manager = mls
5108                .read()
5109                .map_err(|_| Error::Other("MLS lock poisoned".to_string()))?;
5110            manager.take_push_key_package(peer_id)?
5111        };
5112
5113        if push.pool_exhausted {
5114            self.warn_push_key_package_pool_exhausted(peer_id);
5115        }
5116
5117        let key_pkg = push.bundle;
5118        let payload = self.build_key_package_payload(&key_pkg, session_reset);
5119
5120        let serialized =
5121            serde_json::to_string(&payload).map_err(|e| Error::Serialization(e.to_string()))?;
5122        let content = format!("{}{}", internal_prefixes::KEY_PACKAGE, serialized);
5123
5124        let mut message =
5125            self.create_message(peer_id, content, Some(MessagePriority::Low), None)?;
5126        self.sign_control_message(&mut message)?;
5127
5128        match self.transport_manager.send(&message) {
5129            Ok(()) => {
5130                // SECURITY (resource exhaustion): `key_package_sent_to` is keyed
5131                // by the wire-claimed peer id, so a forged-sender key-package
5132                // flood (each reply routes through here) would grow it without
5133                // bound. Reset at capacity like `plaintext_receive_warned` — the
5134                // only cost of forgetting a peer is one idempotent re-send.
5135                if !self.key_package_sent_to.contains(peer_id)
5136                    && self.key_package_sent_to.len() >= MAX_KEY_PACKAGE_SENT_TO
5137                {
5138                    self.key_package_sent_to.clear();
5139                }
5140                self.key_package_sent_to.insert(peer_id.to_string());
5141                debug!(peer_id = %peer_id, message_id = %message.id, "Sent key package");
5142                Ok(())
5143            }
5144            Err(err) => {
5145                // Don't mark as sent and don't enqueue for retry -- if the peer is
5146                // unreachable now, on_neighbor_discovered will fire again when they
5147                // reconnect, generating a fresh exchange.
5148                debug!(peer_id = %peer_id, error = %err, "Key package send deferred");
5149                Err(err)
5150            }
5151        }
5152    }
5153
5154    // ========================================================================
5155    // TRANSPORT HELPERS
5156    // ========================================================================
5157
5158    pub(super) fn transport_from_label(label: &str) -> TransportType {
5159        TransportType::from_label(label)
5160    }
5161
5162    pub(super) fn transport_label(transport: TransportType) -> &'static str {
5163        transport.label()
5164    }
5165
5166    pub(super) fn select_media_transport(&self) -> Result<TransportType> {
5167        let available = self.transport_manager.get_available_transports();
5168
5169        if let Some(current) = self.transport_manager.current_transport() {
5170            if available.contains_key(&current) {
5171                return Ok(current);
5172            }
5173        }
5174
5175        // Reticulum excluded: LoRa bandwidth (~0.7 KB/s typical) is unsuitable for media transfer.
5176        for preferred in [
5177            TransportType::Internet,
5178            TransportType::WiFiDirect,
5179            TransportType::BLE,
5180        ] {
5181            if available.contains_key(&preferred) {
5182                return Ok(preferred);
5183            }
5184        }
5185
5186        Err(Error::Other(
5187            "No available transport for media transfer".to_string(),
5188        ))
5189    }
5190
5191    pub(super) fn pinned_media_transport_for_message(
5192        &self,
5193        message_id: &MessageId,
5194    ) -> Option<TransportType> {
5195        let (file_id, _) = self.outbound_media_chunks.get(message_id)?;
5196        self.outbound_media_transfers
5197            .get(file_id)
5198            .map(|transfer| transfer.pinned_transport)
5199    }
5200
5201    pub(super) fn decryption_failure_code_from_kind(
5202        kind: DecryptionFailureKind,
5203    ) -> DecryptionFailureCode {
5204        match kind {
5205            DecryptionFailureKind::NotInitialized => DecryptionFailureCode::NotInitialized,
5206            DecryptionFailureKind::InvalidCiphertext => DecryptionFailureCode::InvalidCiphertext,
5207            DecryptionFailureKind::IdentityMismatch => DecryptionFailureCode::IdentityMismatch,
5208            DecryptionFailureKind::CryptoFailure => DecryptionFailureCode::CryptoFailure,
5209            DecryptionFailureKind::SessionNotFound | DecryptionFailureKind::Unknown => {
5210                DecryptionFailureCode::Unknown
5211            }
5212        }
5213    }
5214}