Skip to main content

offline_protocol/
transport_manager.rs

1//! Transport management for multi-transport protocol.
2//!
3//! This module provides the TransportManager which manages multiple transports
4//! and uses DORS (Dynamic Offline Relay Switch) to select the optimal transport
5//! for each message.
6
7use crate::constants::{HOP_COUNT_EMA_ALPHA, LATENCY_EMA_ALPHA, OBSERVED_STATS_COMPACT_THRESHOLD};
8use crate::events::{DorsEscalationPhase, DorsEscalationReasonCode, DorsReasonCode, Event};
9use crate::telemetry::routing::{RoutingDecision, RoutingPhase, RoutingReasonCode};
10use crate::{Error, Result};
11use chrono::Utc;
12use offline_protocol_core::{Message, Username, WireCodec};
13use offline_protocol_router::{
14    display_routing_score, DorsConfig, EscalationTriggerReason, TransportScore, TransportSelector,
15};
16use offline_protocol_transport::{
17    nostr::ResolveRequest, Error as TransportError, Transport, TransportMetrics, TransportStatus,
18    TransportType,
19};
20use std::collections::{HashMap, HashSet};
21use std::sync::Arc;
22use std::time::Instant;
23use tracing::{debug, warn};
24
25/// Manages multiple transports and handles transport selection.
26pub struct TransportManager {
27    /// Available transports mapped by type.
28    ///
29    /// Transports are fully interior-mutable (every `Transport` method takes
30    /// `&self`), so they are shared as plain `Arc`s — no outer mutex
31    /// serializing sends against status/metrics reads.
32    transports: HashMap<TransportType, Arc<dyn Transport>>,
33
34    /// Current active transport.
35    current_transport: Option<TransportType>,
36
37    /// Transport selector (DORS).
38    selector: TransportSelector,
39
40    /// Locally observed delivery outcomes used to enrich transport metrics.
41    observations: HashMap<TransportType, ObservedStats>,
42
43    /// Optional callback for DORS lifecycle/decision events (OFF-258).
44    dors_event_callback: Option<Arc<dyn Fn(Event) + Send + Sync>>,
45
46    /// Optional callback for structured routing decisions. Fires alongside
47    /// every legacy `dors_event_callback` emission so the telemetry sink
48    /// receives the richer `RoutingDecision` shape while `EventCallback`
49    /// consumers continue to see the flattened `Event::Dors*` variants.
50    routing_decision_callback: Option<Arc<dyn Fn(RoutingDecision) + Send + Sync>>,
51
52    /// When `true`, every emitted [`RoutingDecision`] carries the full
53    /// per-transport [`TransportScore`] breakdown in its `scores` field.
54    /// Mirrors `TelemetryConfig::routing_diagnostic` and is toggled by
55    /// `OfflineProtocol::install_telemetry_sink`.
56    routing_diagnostic: bool,
57
58    /// Last escalation trigger event emitted (reason, time) for dedupe window.
59    last_escalation_trigger_emitted: Option<(DorsEscalationReasonCode, std::time::Instant)>,
60
61    /// Peers known to decode the binary wire codec (from their signed key
62    /// package). Messages addressed to a listed peer are stamped
63    /// [`WireCodec::BinaryV1`] at send; all others stay JSON.
64    peer_binary_wire: HashSet<String>,
65
66    /// Host-reported battery level for this device, if the platform publishes
67    /// one. Merged into every transport's metrics that does not report its own
68    /// — see [`TransportManager::set_device_battery`].
69    device_battery: Option<u8>,
70
71    /// Host-reported charging state, applied alongside [`Self::device_battery`].
72    device_is_charging: bool,
73}
74
75/// Carriers whose links are peer-to-peer, in the order a forwarding caller
76/// should prefer them.
77///
78/// These are the only transports that can carry a *mesh hop*: a frame handed
79/// straight to a neighbor's radio. Wi-Fi Direct leads because a link that
80/// exists at all is the higher-bandwidth one; BLE is the carrier that is
81/// almost always present. Everything else reaches peers through
82/// infrastructure, which is not a hop — it is an exit from the mesh.
83const MESH_TRANSPORTS: &[TransportType] = &[TransportType::WiFiDirect, TransportType::BLE];
84
85/// A peer reachable over a direct mesh link.
86#[derive(Debug, Clone, PartialEq, Eq)]
87pub struct MeshNeighbor {
88    /// User-level id of the neighbor.
89    pub peer_id: String,
90    /// Signal strength for the link, when the carrier measures one.
91    pub rssi: Option<i16>,
92    /// The carrier holding the link.
93    pub transport: TransportType,
94}
95
96impl MeshNeighbor {
97    /// How good this link is, on a 0–100 scale.
98    ///
99    /// Derived from signal strength where the carrier reports it, and a
100    /// mid-scale value where it does not — an unmeasured link should not
101    /// outrank a measured good one, nor be discarded like a measured bad one.
102    pub fn link_quality(&self) -> u8 {
103        match self.rssi {
104            Some(rssi) => offline_protocol_transport::LinkQuality::from_rssi(rssi).value(),
105            None => offline_protocol_transport::DEFAULT_LINK_QUALITY_WITHOUT_RSSI,
106        }
107    }
108}
109
110/// Dedupe window: don't re-emit same escalation trigger reason within this duration.
111const ESCALATION_TRIGGER_DEDUPE_SECS: u64 = 30;
112
113/// Cap on the per-peer binary-wire capability set. Keyed by the wire-claimed
114/// peer id, so — like `key_package_sent_to` — it is cleared at capacity to bound
115/// a forged-sender flood; the only cost of forgetting a peer is re-learning its
116/// capability from the next key package (one JSON send in the meantime).
117const MAX_PEER_BINARY_WIRE_ENTRIES: usize = 4096;
118
119#[derive(Debug, Default, Clone)]
120struct ObservedStats {
121    success_count: u32,
122    failure_count: u32,
123    latency_ema: Option<f32>,
124    hop_ema: Option<f32>,
125    last_success_at: Option<Instant>,
126}
127
128impl ObservedStats {
129    fn record_success(&mut self, latency_ms: u32, hop_count: u8) {
130        self.success_count = self.success_count.saturating_add(1);
131        self.latency_ema = Some(update_ema(
132            self.latency_ema,
133            latency_ms as f32,
134            LATENCY_EMA_ALPHA,
135        ));
136        self.hop_ema = Some(update_ema(
137            self.hop_ema,
138            hop_count as f32,
139            HOP_COUNT_EMA_ALPHA,
140        ));
141        self.last_success_at = Some(Instant::now());
142        self.compact();
143    }
144
145    fn record_failure(&mut self) {
146        self.failure_count = self.failure_count.saturating_add(1);
147        self.compact();
148    }
149
150    fn compact(&mut self) {
151        let total = self.success_count.saturating_add(self.failure_count);
152        if total > OBSERVED_STATS_COMPACT_THRESHOLD {
153            self.success_count /= 2;
154            self.failure_count /= 2;
155        }
156    }
157
158    fn delivery_ratio(&self) -> Option<f32> {
159        let total = self.success_count + self.failure_count;
160        if total == 0 {
161            None
162        } else {
163            Some(self.success_count as f32 / total as f32)
164        }
165    }
166
167    fn drop_ratio(&self) -> Option<f32> {
168        self.delivery_ratio()
169            .map(|ratio| (1.0 - ratio).clamp(0.0, 1.0))
170    }
171
172    fn apply_to_metrics(&self, metrics: &mut TransportMetrics) {
173        if self.success_count + self.failure_count > 0 {
174            // Only override delivery counts when the transport itself has not
175            // reported any.  Internet transport tracks wire-level outcomes via
176            // the confirmation loop; overwriting those would discard real data.
177            //
178            // NOTE: This check is value-based, not transport-type-based.  If
179            // BLE or WiFi Direct ever start reporting their own success/failure
180            // counts from the native layer (the way Internet does via the
181            // confirmation loop), the condition below will silently skip the
182            // local observations for those transports too.  At that point,
183            // consider switching to an explicit per-transport-type check.
184            if metrics.success_count + metrics.failure_count == 0 {
185                metrics.success_count = self.success_count;
186                metrics.failure_count = self.failure_count;
187                metrics.delivery_ratio = self.delivery_ratio();
188                metrics.drop_rate = self.drop_ratio();
189            }
190        }
191
192        if let Some(latency) = self.latency_ema {
193            metrics.latency_ms = Some(latency.round().clamp(0.0, u32::MAX as f32) as u32);
194        }
195
196        if let Some(hop) = self.hop_ema {
197            metrics.average_hop_count = Some(hop);
198        }
199    }
200}
201
202fn update_ema(current: Option<f32>, new_value: f32, alpha: f32) -> f32 {
203    let alpha = alpha.clamp(0.0, 1.0);
204    match current {
205        Some(existing) => existing * (1.0 - alpha) + new_value * alpha,
206        None => new_value,
207    }
208}
209
210impl TransportManager {
211    /// Creates a new transport manager with default configuration.
212    pub fn new(selector: TransportSelector) -> Self {
213        Self {
214            transports: HashMap::new(),
215            current_transport: None,
216            selector,
217            observations: HashMap::new(),
218            dors_event_callback: None,
219            routing_decision_callback: None,
220            routing_diagnostic: false,
221            last_escalation_trigger_emitted: None,
222            peer_binary_wire: HashSet::new(),
223            device_battery: None,
224            device_is_charging: false,
225        }
226    }
227
228    /// Records the host's battery reading for this device.
229    ///
230    /// Battery is a property of the *device*, not of any one radio, but every
231    /// consumer inside the SDK reads it from the per-transport
232    /// [`TransportMetrics`] map: DORS energy scoring, the relay
233    /// promote/demote policy, the forwarding battery floor, and the telemetry
234    /// device-capability snapshot all resolve it through
235    /// `device_battery_from_available`. This setter is therefore the single
236    /// host-facing feed, and it is merged into the two snapshot loops that
237    /// build that map.
238    ///
239    /// `level` is clamped to 0-100. A transport that reports its own battery
240    /// level keeps it: the merge fills the field, never overwrites it.
241    pub fn set_device_battery(&mut self, level: u8, is_charging: bool) {
242        self.device_battery = Some(level.min(100));
243        self.device_is_charging = is_charging;
244    }
245
246    /// Returns the host-reported `(battery_level, is_charging)` pair, if the
247    /// platform has published one.
248    pub fn device_battery(&self) -> (Option<u8>, bool) {
249        (self.device_battery, self.device_is_charging)
250    }
251
252    /// Fills the device-level battery fields on one transport's metrics.
253    ///
254    /// **Fill-if-absent, never overwrite.** A transport that publishes its own
255    /// `battery_level` is reporting something the host feed cannot know better
256    /// (a peripheral's own cell, say), so its reading wins; the host value is
257    /// the fallback for the ordinary case where no transport reports one at
258    /// all. `is_charging` is only applied alongside a battery level that came
259    /// from the host, so a transport's own (level, charging) pair is never
260    /// split across two sources — the same coherence rule
261    /// `device_battery_from_available` applies when choosing between
262    /// transports.
263    fn apply_device_battery(&self, metrics: &mut TransportMetrics) {
264        let Some(level) = self.device_battery else {
265            return;
266        };
267        if metrics.battery_level.is_none() {
268            metrics.battery_level = Some(level);
269            metrics.is_charging = self.device_is_charging;
270        }
271    }
272
273    /// Returns whether a peer is recorded as able to decode the binary wire
274    /// codec (and would therefore be sent binary frames).
275    pub fn peer_supports_binary_wire(&self, peer_id: &str) -> bool {
276        self.peer_binary_wire.contains(peer_id)
277    }
278
279    /// Records whether a peer can decode our binary wire frames, learned from
280    /// its signed key package. Messages addressed to a capable peer are stamped
281    /// [`WireCodec::BinaryV1`] at send; all others stay JSON.
282    pub fn mark_peer_binary_wire(&mut self, peer_id: &str, supported: bool) {
283        if supported {
284            // Bound the wire-keyed set: clear at capacity, then insert.
285            if !self.peer_binary_wire.contains(peer_id)
286                && self.peer_binary_wire.len() >= MAX_PEER_BINARY_WIRE_ENTRIES
287            {
288                self.peer_binary_wire.clear();
289            }
290            self.peer_binary_wire.insert(peer_id.to_string());
291        } else {
292            self.peer_binary_wire.remove(peer_id);
293        }
294    }
295
296    /// Forwards a peer's advertised Nostr public key to the Nostr transport, so
297    /// gift wraps addressed to them are sealed to a key only their install
298    /// holds instead of to the publicly computable bootstrap key.
299    ///
300    /// `None` means "this peer advertises no Nostr key", which **forgets** any
301    /// key already held rather than leaving it in place. That is what keeps the
302    /// in-memory copy consistent with the durable record, whose downgrade
303    /// semantics delete on an empty advertisement: retaining it here instead
304    /// would keep sealing to a key the peer has stopped claiming, and survive
305    /// only until the next restart — a divergence with no symptom.
306    ///
307    /// A no-op when the Nostr transport is not installed (the common case —
308    /// `nostr_enabled` defaults off). The bound on the underlying map lives in
309    /// the transport.
310    pub fn mark_peer_nostr_pubkey(&mut self, peer_id: &str, pubkey_hex: Option<&str>) {
311        let Some(transport) = self.transports.get(&TransportType::Nostr) else {
312            return;
313        };
314        if let Some(nostr) = transport
315            .as_any()
316            .downcast_ref::<offline_protocol_transport::nostr::NostrTransport>()
317        {
318            match pubkey_hex {
319                Some(pubkey_hex) => nostr.set_peer_nostr_pubkey(peer_id, pubkey_hex),
320                None => nostr.forget_peer_nostr_pubkey(peer_id),
321            }
322        }
323    }
324
325    /// Forgets a peer's Nostr public key, so frames to them revert to the
326    /// publicly computable bootstrap key.
327    ///
328    /// Used by the unblock clean slate, which declares everything learned about
329    /// a peer stale. Reverting is the right fallback rather than refusing to
330    /// send: the bootstrap key is derived from the peer's user id, so it works
331    /// whatever install they are on now.
332    pub fn clear_peer_nostr_pubkey(&mut self, peer_id: &str) {
333        if let Some(nostr) = self.transports.get(&TransportType::Nostr).and_then(|t| {
334            t.as_any()
335                .downcast_ref::<offline_protocol_transport::nostr::NostrTransport>()
336        }) {
337            nostr.forget_peer_nostr_pubkey(peer_id);
338        }
339    }
340
341    /// This install's Nostr public key, for advertising in outgoing key
342    /// packages. `None` when the Nostr transport is not installed.
343    pub fn nostr_public_key(&self) -> Option<String> {
344        self.transports
345            .get(&TransportType::Nostr)?
346            .as_any()
347            .downcast_ref::<offline_protocol_transport::nostr::NostrTransport>()
348            .map(|nostr| nostr.public_key_hex())
349    }
350
351    /// Applies the sealed-envelope kill switch to the Nostr transport.
352    pub fn set_nostr_sealing_enabled(&mut self, enabled: bool) {
353        if let Some(nostr) = self.transports.get(&TransportType::Nostr).and_then(|t| {
354            t.as_any()
355                .downcast_ref::<offline_protocol_transport::nostr::NostrTransport>()
356        }) {
357            nostr.set_sealing_enabled(enabled);
358        }
359    }
360
361    /// Applies the cold-contact kill switch to the Nostr transport.
362    pub fn set_nostr_cold_contact_enabled(&mut self, enabled: bool) {
363        if let Some(nostr) = self.nostr_transport() {
364            nostr.set_cold_contact_enabled(enabled);
365        }
366    }
367
368    /// Queues a key-package record for publication at `slot_id`.
369    ///
370    /// A no-op when the Nostr transport is not installed, which is the common
371    /// case: publication is driven from the engine's tick, and that tick runs
372    /// whether or not Nostr is enabled.
373    ///
374    /// Serializes through the transport's own chokepoint, which yields JSON for
375    /// an unstamped message — and a published record must never be anything
376    /// else. The binary codec is negotiated per peer from that peer's key
377    /// package, and the reader of this record is by definition a stranger whose
378    /// key package we have not seen. Emitting binary here would make the record
379    /// unreadable by exactly the audience it exists for.
380    pub fn publish_nostr_key_package(&mut self, slot_id: &str, message: &Message) -> Result<()> {
381        let Some(nostr) = self.nostr_transport() else {
382            return Ok(());
383        };
384        let payload = nostr.serialize_message(message)?;
385        nostr.publish_key_package(slot_id, payload);
386        Ok(())
387    }
388
389    /// Drains the slot ids whose publication never reached a relay.
390    ///
391    /// Empty when Nostr is not installed, which keeps the caller's tick free of
392    /// a transport check it would otherwise need.
393    pub fn take_failed_nostr_publications(&self) -> Vec<String> {
394        self.nostr_transport()
395            .map(|nostr| nostr.take_failed_publications())
396            .unwrap_or_default()
397    }
398
399    /// Whether the Nostr transport is installed and publishing records.
400    pub fn nostr_cold_contact_active(&self) -> bool {
401        self.nostr_transport()
402            .map(|nostr| nostr.cold_contact_enabled())
403            .unwrap_or(false)
404    }
405
406    /// Enables or disables username discovery on the Nostr transport.
407    pub fn set_nostr_username_discovery_enabled(&mut self, enabled: bool) {
408        if let Some(nostr) = self.nostr_transport() {
409            nostr.set_discovery_enabled(enabled);
410        }
411    }
412
413    /// Whether the Nostr transport is installed and username discovery is on.
414    ///
415    /// Already accounts for the cold-contact coupling: the transport reports
416    /// false when either switch is off.
417    pub fn nostr_discovery_active(&self) -> bool {
418        self.nostr_transport()
419            .map(|nostr| nostr.discovery_enabled())
420            .unwrap_or(false)
421    }
422
423    /// Queues this install's username claim for publication.
424    pub fn publish_nostr_discovery_record(&mut self, username: Username, payload: Vec<u8>) {
425        if let Some(nostr) = self.nostr_transport() {
426            nostr.publish_discovery_record(username, payload);
427        }
428    }
429
430    /// Queues a retraction of this install's username claim.
431    pub fn retract_nostr_discovery_record(&mut self, username: Username, payload: Vec<u8>) {
432        if let Some(nostr) = self.nostr_transport() {
433            nostr.retract_discovery_record(username, payload);
434        }
435    }
436
437    /// Drains the discovery tags whose publication never reached a relay.
438    pub fn take_failed_nostr_discovery_publications(&self) -> Vec<String> {
439        self.nostr_transport()
440            .map(|nostr| nostr.take_failed_discovery_publications())
441            .unwrap_or_default()
442    }
443
444    /// Drains the discovery tags whose publication a relay acknowledged.
445    pub fn take_confirmed_nostr_discovery_publications(&self) -> Vec<String> {
446        self.nostr_transport()
447            .map(|nostr| nostr.take_confirmed_discovery_publications())
448            .unwrap_or_default()
449    }
450
451    /// Whether a Nostr transport is installed at all.
452    ///
453    /// Distinct from [`Self::nostr_discovery_active`]: a retraction is owed
454    /// whether or not discovery is still switched on, so the engine has to be
455    /// able to tell "the feature is off" from "there is nothing here that could
456    /// carry a tombstone". Without the second, queueing a retraction silently
457    /// does nothing while the engine records it as done.
458    pub fn has_nostr_transport(&self) -> bool {
459        self.nostr_transport().is_some()
460    }
461
462    /// Requests resolution of the devices claiming `username`.
463    ///
464    /// A missing Nostr transport reports `Disabled`, which is what it is from
465    /// the caller's side: no query was sent and no event will follow.
466    pub fn resolve_nostr_username(&self, username: Username) -> ResolveRequest {
467        self.nostr_transport()
468            .map(|nostr| nostr.resolve_username(username))
469            .unwrap_or(ResolveRequest::Disabled)
470    }
471
472    /// Withdraws a queued username lookup that was never issued to a relay.
473    ///
474    /// Returns whether one was removed. Called when the engine gives up on a
475    /// lookup, so the name does not sit in the queue making every later request
476    /// for it return "already queued" without ever answering.
477    pub fn cancel_nostr_username_resolution(&self, username: &Username) -> bool {
478        self.nostr_transport()
479            .map(|nostr| nostr.cancel_username_resolution(username))
480            .unwrap_or(false)
481    }
482
483    fn nostr_transport(&self) -> Option<&offline_protocol_transport::nostr::NostrTransport> {
484        self.transports.get(&TransportType::Nostr).and_then(|t| {
485            t.as_any()
486                .downcast_ref::<offline_protocol_transport::nostr::NostrTransport>()
487        })
488    }
489
490    /// Sets the callback for DORS decision events (dors_score_updated, dors_transport_selected,
491    pub fn set_dors_event_callback(&mut self, callback: Option<Arc<dyn Fn(Event) + Send + Sync>>) {
492        self.dors_event_callback = callback;
493    }
494
495    /// Sets the callback that receives structured
496    /// [`crate::telemetry::RoutingDecision`] records.
497    ///
498    /// Fires at the same sites as `dors_event_callback`, carrying a richer
499    /// shape suitable for the unified telemetry sink.
500    ///
501    /// Crate-private: apps must drive routing telemetry through
502    /// [`crate::OfflineProtocol::install_telemetry_sink`] rather than wiring
503    /// the callback directly. This keeps [`crate::telemetry::TelemetryConfig`]
504    /// the single source of truth for sink configuration.
505    pub(crate) fn set_routing_decision_callback(
506        &mut self,
507        callback: Option<Arc<dyn Fn(RoutingDecision) + Send + Sync>>,
508    ) {
509        self.routing_decision_callback = callback;
510    }
511
512    /// Enables or disables the routing-diagnostic detail level.
513    ///
514    /// When `true`, [`RoutingDecision::scores`] is populated with the full
515    /// seven-factor [`TransportScore`] for every ranked transport on every
516    /// emission. When `false` (default), the vector is empty and the hot
517    /// path avoids the per-emission allocation. Mirrors
518    /// `TelemetryConfig::routing_diagnostic`.
519    ///
520    /// Crate-private: see [`Self::set_routing_decision_callback`].
521    pub(crate) fn set_routing_diagnostic(&mut self, enabled: bool) {
522        self.routing_diagnostic = enabled;
523    }
524
525    fn emit_dors_event(&self, event: Event) {
526        if let Some(ref cb) = self.dors_event_callback {
527            cb(event);
528        }
529    }
530
531    /// Lazy-emit a [`RoutingDecision`]. The `build` closure only runs when a
532    /// callback is actually installed, so the no-sink path pays only an
533    /// `Option::is_some` check — not a `Utc::now()` call and struct build.
534    fn emit_routing_decision<F: FnOnce() -> RoutingDecision>(&self, build: F) {
535        if let Some(ref cb) = self.routing_decision_callback {
536            cb(build());
537        }
538    }
539
540    fn escalation_trigger_reason_to_code(r: EscalationTriggerReason) -> DorsEscalationReasonCode {
541        match r {
542            EscalationTriggerReason::RetryThreshold => DorsEscalationReasonCode::RetryThreshold,
543            EscalationTriggerReason::PoorSignal => DorsEscalationReasonCode::PoorSignal,
544            EscalationTriggerReason::Congestion => DorsEscalationReasonCode::Congestion,
545            EscalationTriggerReason::LowTtl => DorsEscalationReasonCode::LowTtl,
546            EscalationTriggerReason::LowSuccessRate => DorsEscalationReasonCode::LowSuccessRate,
547        }
548    }
549
550    fn dors_reason_code_to_routing(code: DorsReasonCode) -> RoutingReasonCode {
551        match code {
552            DorsReasonCode::InitialSelection => RoutingReasonCode::InitialSelection,
553            DorsReasonCode::PrimarySelected => RoutingReasonCode::PrimarySelected,
554            DorsReasonCode::PrimarySuccess => RoutingReasonCode::PrimarySuccess,
555            DorsReasonCode::FallbackSuccess => RoutingReasonCode::FallbackSuccess,
556            DorsReasonCode::EscalationApplied => RoutingReasonCode::EscalationApplied,
557            DorsReasonCode::CurrentUnavailable => RoutingReasonCode::CurrentUnavailable,
558        }
559    }
560
561    fn escalation_reason_code_to_routing(code: DorsEscalationReasonCode) -> RoutingReasonCode {
562        match code {
563            DorsEscalationReasonCode::FallbackSuccess => RoutingReasonCode::FallbackSuccess,
564            DorsEscalationReasonCode::RetryThreshold => RoutingReasonCode::RetryThreshold,
565            DorsEscalationReasonCode::PoorSignal => RoutingReasonCode::PoorSignal,
566            DorsEscalationReasonCode::Congestion => RoutingReasonCode::Congestion,
567            DorsEscalationReasonCode::LowTtl => RoutingReasonCode::LowTtl,
568            DorsEscalationReasonCode::LowSuccessRate => RoutingReasonCode::LowSuccessRate,
569        }
570    }
571
572    /// Builds a [`RoutingDecision`]. The `detailed_scores` slice is cloned
573    /// into `scores` when `routing_diagnostic` is enabled on the manager;
574    /// otherwise the vector stays empty (allocation-free).
575    ///
576    /// Takes `now_ms` by argument rather than calling `Utc::now()` per
577    /// build so every decision emitted from a single `send()` cycle shares
578    /// one timestamp — easier to correlate downstream and saves three to
579    /// four syscalls on the send hot path.
580    #[allow(clippy::too_many_arguments)]
581    fn build_routing_decision(
582        &self,
583        now_ms: i64,
584        phase: RoutingPhase,
585        from: Option<TransportType>,
586        to: Option<TransportType>,
587        winning_score: Option<f32>,
588        reason_code: Option<RoutingReasonCode>,
589        detailed_scores: Option<&[(TransportType, TransportScore)]>,
590    ) -> RoutingDecision {
591        // Sanitize the internal fallback-demotion sentinel out of every score
592        // that leaves the engine. DORS keeps Internet's `total` negative to rank
593        // it last; telemetry/UniFFI consumers must see the real 0–125 quality
594        // score instead. This is the single chokepoint for all RoutingDecision
595        // emitters, so `winning_score` is also sanitized here defensively
596        // (idempotent for an already-positive value).
597        let scores = match (self.routing_diagnostic, detailed_scores) {
598            (true, Some(detailed)) => detailed
599                .iter()
600                .map(|(t, s)| {
601                    let mut s = s.clone();
602                    s.total = display_routing_score(s.total);
603                    (*t, s)
604                })
605                .collect(),
606            _ => Vec::new(),
607        };
608        RoutingDecision {
609            timestamp_ms: now_ms,
610            phase,
611            from,
612            to,
613            winning_score: winning_score.map(display_routing_score),
614            reason_code,
615            scores,
616        }
617    }
618
619    /// Emit dors_escalation_triggered at trigger boundary (typed reason), deduped by reason + time window.
620    fn emit_escalation_trigger_if_deduped(
621        &mut self,
622        now_ms: i64,
623        reason_code: DorsEscalationReasonCode,
624        from: TransportType,
625        to: TransportType,
626        reason_detail: Option<String>,
627        detailed_scores: Option<&[(TransportType, TransportScore)]>,
628    ) {
629        let now = std::time::Instant::now();
630        let emit = match &self.last_escalation_trigger_emitted {
631            Some((last_code, last_at)) => {
632                *last_code != reason_code
633                    || last_at.elapsed().as_secs() >= ESCALATION_TRIGGER_DEDUPE_SECS
634            }
635            None => true,
636        };
637        if emit {
638            self.last_escalation_trigger_emitted = Some((reason_code, now));
639            self.emit_dors_event(Event::dors_escalation_triggered(
640                DorsEscalationPhase::Triggered,
641                from.to_string(),
642                to.to_string(),
643                reason_code,
644                reason_detail,
645            ));
646            self.emit_routing_decision(|| {
647                self.build_routing_decision(
648                    now_ms,
649                    RoutingPhase::Escalated,
650                    Some(from),
651                    Some(to),
652                    None,
653                    Some(Self::escalation_reason_code_to_routing(reason_code)),
654                    detailed_scores,
655                )
656            });
657        }
658    }
659
660    /// Adds a transport to the manager.
661    ///
662    /// # Arguments
663    ///
664    /// * `transport_type` - Type of transport to add
665    /// * `transport` - The transport implementation
666    pub fn add_transport(&mut self, transport_type: TransportType, transport: Box<dyn Transport>) {
667        self.transports.insert(transport_type, Arc::from(transport));
668    }
669
670    /// Sends a message through the best available transport, with fallback.
671    ///
672    /// DORS selects a primary transport (applying hysteresis, cooldown, and
673    /// stability checks). If the primary's `send()` fails synchronously, the
674    /// remaining transports are tried in descending score order before
675    /// returning an error. Each synchronous failure is recorded via
676    /// `record_retry_failure` so DORS can adjust future scoring.
677    ///
678    /// # Internet transport — fallback does NOT apply
679    ///
680    /// Internet `send()` enqueues the message and returns `Ok(())`
681    /// immediately. The actual wire-level outcome is reported asynchronously
682    /// via the `confirm_sent` / `report_send_failure` confirmation loop on
683    /// `InternetTransport`. Because `send()` never fails synchronously, the
684    /// fallback loop below will **not** trigger for Internet. If the
685    /// WebSocket is silently broken (status still `Available` but sends
686    /// fail), messages will be enqueued, detected as failures by the
687    /// confirmation-loop timeout, and counted in DORS metrics — but they
688    /// are **not** retried on an alternative transport by this method.
689    /// Retry/re-routing for those messages is handled by the higher-level
690    /// outbox retry mechanism.
691    pub fn send(&mut self, message: &Message) -> Result<()> {
692        // Stamp the negotiated wire codec for this recipient. The clone happens
693        // only on the binary path (peers known to support it); JSON and unknown
694        // peers reuse the borrowed message. The internet transport ignores the
695        // stamp (its relay bridge is JSON-only), and mesh transports only ever
696        // emit to a recipient that is a directly connected peer, so `recipient`
697        // is the physical peer whose capability was recorded — keying by it
698        // matches the same peer-string the MTU path already keys by.
699        //
700        // The codec travels on the message, and both `Transport::send` and this
701        // method take `&Message` as public API, so carrying the stamp across
702        // that boundary needs an owned message — hence this clone. Note it is a
703        // full-message clone: it copies `binary_content` (up to the max message
704        // size), not just the envelope, so for media-sized payloads sent to a
705        // binary-capable peer it is a real payload memcpy on the hot path. It is
706        // a deliberate tradeoff: threading the codec as a separate argument would
707        // shave the clone but is a breaking change to the public `Transport`
708        // trait every downstream implementor relies on. The transport's own
709        // queue-clone is unaffected either way. If profiling ever flags this,
710        // stamp via a lightweight wrapper rather than cloning the payload.
711        let stamped;
712        let message = if message.wire_codec() != WireCodec::BinaryV1
713            && self.peer_binary_wire.contains(message.recipient.as_str())
714        {
715            let mut m = message.clone();
716            m.set_wire_codec(WireCodec::BinaryV1);
717            stamped = m;
718            &stamped
719        } else {
720            message
721        };
722
723        let available = self.get_available_transports();
724        if available.is_empty() {
725            return Err(Error::Transport(TransportError::TransportNotAvailable(
726                "No available transport".to_string(),
727            )));
728        }
729
730        // Single timestamp shared by every RoutingDecision emitted from this
731        // send cycle — cheaper than per-emit `Utc::now()` and lets downstream
732        // consumers group decisions by timestamp.
733        let now_ms = Utc::now().timestamp_millis();
734
735        let previous = self.current_transport;
736
737        // DORS selects the primary transport (applies hysteresis/cooldown/stability).
738        let primary = self
739            .selector
740            .select_transport(message, &available)
741            .ok_or_else(|| {
742                Error::Transport(TransportError::TransportNotAvailable(
743                    "No suitable transport selected from available transports".to_string(),
744                ))
745            })?;
746
747        // Emit DORS observability: scores and selection (before send attempt).
748        // At the default (non-diagnostic) tier we project the detailed
749        // breakdown down to `(transport, total)` — a single scoring pass
750        // feeds both the legacy event and the routing record.
751        let detailed_scores: Option<Vec<(TransportType, TransportScore)>> =
752            if self.routing_diagnostic && self.routing_decision_callback.is_some() {
753                Some(self.selector.score_and_rank_detailed(message, &available))
754            } else {
755                None
756            };
757        let detailed_slice = detailed_scores.as_deref();
758        let scored: Vec<(TransportType, f32)> = match detailed_slice {
759            Some(d) => d.iter().map(|(t, s)| (*t, s.total)).collect(),
760            None => self.selector.score_and_rank(message, &available),
761        };
762        // Sanitize the fallback-demotion sentinel out of the emitted scores (the
763        // `scored` vec itself keeps the raw totals — the fallback loop below
764        // relies on Internet ranking last).
765        let scores: Vec<(String, f32)> = scored
766            .iter()
767            .map(|(t, s)| (t.to_string(), display_routing_score(*s)))
768            .collect();
769        self.emit_dors_event(Event::dors_score_updated(scores));
770
771        self.emit_routing_decision(|| {
772            self.build_routing_decision(
773                now_ms,
774                RoutingPhase::ScoreUpdated,
775                previous,
776                None,
777                None,
778                None,
779                detailed_slice,
780            )
781        });
782        // Telemetry-only; sanitize the fallback-demotion sentinel so the emitted
783        // selection score is the real 0–125 quality value, not the negative rank.
784        let primary_score = scored
785            .iter()
786            .find(|(t, _)| *t == primary)
787            .map(|(_, s)| display_routing_score(*s))
788            .unwrap_or(0.0);
789        let selection_reason = if previous.is_none() {
790            DorsReasonCode::InitialSelection
791        } else {
792            DorsReasonCode::PrimarySelected
793        };
794        self.emit_dors_event(Event::dors_transport_selected(
795            previous.map(|t| t.to_string()),
796            primary.to_string(),
797            selection_reason,
798            primary_score,
799        ));
800        self.emit_routing_decision(|| {
801            self.build_routing_decision(
802                now_ms,
803                RoutingPhase::Selected,
804                previous,
805                Some(primary),
806                Some(primary_score),
807                Some(Self::dors_reason_code_to_routing(selection_reason)),
808                detailed_slice,
809            )
810        });
811
812        // Try the primary transport first.
813        let primary_result = {
814            let transport = self
815                .transports
816                .get(&primary)
817                .ok_or_else(|| Error::Other(format!("Transport {:?} not found", primary)))?;
818            transport.send(message)
819        };
820
821        match primary_result {
822            Ok(()) => {
823                self.current_transport = Some(primary);
824                if previous != Some(primary) {
825                    let reason_code = if previous.is_some_and(|p| !available.contains_key(&p)) {
826                        DorsReasonCode::CurrentUnavailable
827                    } else {
828                        DorsReasonCode::PrimarySuccess
829                    };
830                    self.emit_dors_event(Event::dors_transport_switched(
831                        previous.map(|t| t.to_string()),
832                        primary.to_string(),
833                        reason_code,
834                        None,
835                    ));
836                    self.emit_routing_decision(|| {
837                        self.build_routing_decision(
838                            now_ms,
839                            RoutingPhase::Switched,
840                            previous,
841                            Some(primary),
842                            Some(primary_score),
843                            Some(Self::dors_reason_code_to_routing(reason_code)),
844                            detailed_slice,
845                        )
846                    });
847                }
848                return Ok(());
849            }
850            Err(e) => {
851                let is_peer_routing = matches!(e, TransportError::PeerNotReachable(_));
852                if is_peer_routing {
853                    debug!(
854                        transport = ?primary,
855                        error = %e,
856                        "Peer not reachable via primary transport, trying fallback"
857                    );
858                } else {
859                    warn!(
860                        transport = ?primary,
861                        error = %e,
862                        "Primary transport send failed, trying fallback"
863                    );
864                    self.selector.record_retry_failure(primary);
865                }
866            }
867        }
868
869        // Primary failed — try remaining transports in score order.
870        // Reuse the scored ranking computed above; record_retry_failure only
871        // mutates retry_counts which score_and_rank does not read.
872        let mut last_error = None;
873        let mut attempted: Vec<TransportType> = vec![primary];
874
875        for (transport_type, _score) in &scored {
876            if *transport_type == primary {
877                continue;
878            }
879
880            // Emit escalation trigger at DORS boundary (typed reason), deduped.
881            if primary == TransportType::BLE && *transport_type == TransportType::WiFiDirect {
882                if let Some(trigger_reason) = self.selector.escalation_trigger_reason() {
883                    let reason_code = Self::escalation_trigger_reason_to_code(trigger_reason);
884                    self.emit_escalation_trigger_if_deduped(
885                        now_ms,
886                        reason_code,
887                        primary,
888                        *transport_type,
889                        None,
890                        detailed_slice,
891                    );
892                }
893            }
894
895            let transport = match self.transports.get(transport_type) {
896                Some(t) => t,
897                None => continue,
898            };
899
900            attempted.push(*transport_type);
901
902            match transport.send(message) {
903                Ok(()) => {
904                    self.current_transport = Some(*transport_type);
905                    self.selector.set_current_transport(*transport_type);
906                    // Emit switch only when active transport actually changed (fallback success).
907                    if previous != Some(*transport_type) {
908                        let reason_code = if primary == TransportType::BLE
909                            && *transport_type == TransportType::WiFiDirect
910                        {
911                            DorsReasonCode::EscalationApplied
912                        } else {
913                            DorsReasonCode::FallbackSuccess
914                        };
915                        self.emit_dors_event(Event::dors_transport_switched(
916                            previous.map(|t| t.to_string()),
917                            transport_type.to_string(),
918                            reason_code,
919                            Some("primary send failed, fallback succeeded".to_string()),
920                        ));
921                        let fallback = *transport_type;
922                        self.emit_routing_decision(|| {
923                            self.build_routing_decision(
924                                now_ms,
925                                RoutingPhase::Switched,
926                                previous,
927                                Some(fallback),
928                                None,
929                                Some(Self::dors_reason_code_to_routing(reason_code)),
930                                detailed_slice,
931                            )
932                        });
933                    }
934                    // Escalation applied only when BLE→WiFi fallback actually succeeded.
935                    if primary == TransportType::BLE && *transport_type == TransportType::WiFiDirect
936                    {
937                        self.emit_dors_event(Event::dors_escalation_triggered(
938                            DorsEscalationPhase::Applied,
939                            primary.to_string(),
940                            transport_type.to_string(),
941                            DorsEscalationReasonCode::FallbackSuccess,
942                            Some("primary BLE send failed, fallback to WiFi succeeded".to_string()),
943                        ));
944                        let escalated = *transport_type;
945                        self.emit_routing_decision(|| {
946                            self.build_routing_decision(
947                                now_ms,
948                                RoutingPhase::Escalated,
949                                Some(primary),
950                                Some(escalated),
951                                None,
952                                Some(Self::escalation_reason_code_to_routing(
953                                    DorsEscalationReasonCode::FallbackSuccess,
954                                )),
955                                detailed_slice,
956                            )
957                        });
958                    }
959                    return Ok(());
960                }
961                Err(e) => {
962                    if !matches!(e, TransportError::PeerNotReachable(_)) {
963                        warn!(
964                            transport = ?transport_type,
965                            error = %e,
966                            "Fallback transport send failed, trying next"
967                        );
968                        self.selector.record_retry_failure(*transport_type);
969                    }
970                    last_error = Some(e);
971                }
972            }
973        }
974
975        let terminal_error = last_error.unwrap_or_else(|| {
976            TransportError::SendFailed(format!("All transports failed (tried {:?})", attempted))
977        });
978        Err(Error::Transport(terminal_error))
979    }
980
981    /// Sends a message via a specific transport, bypassing DORS selection.
982    ///
983    /// # Arguments
984    ///
985    /// * `message` - The message to send
986    /// * `transport_type` - The transport to use
987    ///
988    /// # Returns
989    ///
990    /// Returns Ok(()) if sent successfully, Err otherwise.
991    pub fn send_via_transport(
992        &mut self,
993        message: &Message,
994        transport_type: TransportType,
995    ) -> Result<()> {
996        let transport = self
997            .transports
998            .get(&transport_type)
999            .ok_or_else(|| Error::Other(format!("Transport {:?} not found", transport_type)))?;
1000
1001        if transport.status() != TransportStatus::Available {
1002            return Err(Error::Other(format!(
1003                "Transport {:?} is not available",
1004                transport_type
1005            )));
1006        }
1007
1008        // Preserved as `Error::Transport`, not flattened into `Error::Other`.
1009        // Flattening discarded the variant every classifier keys on —
1010        // `SessionStateError::classify` and `Protocol::is_no_carrier_error`
1011        // both answer on `Error::Transport(_)`, so on this path they saw
1012        // `Unknown`/`false` and the rendered string was the only surviving
1013        // signal. That is exactly the string that must stop being read.
1014        transport.send(message).map_err(Error::Transport)?;
1015
1016        // Only update current transport after successful send
1017        self.current_transport = Some(transport_type);
1018
1019        Ok(())
1020    }
1021
1022    /// Lists every peer reachable over a direct mesh link, with the transport
1023    /// that link runs on.
1024    ///
1025    /// Only the peer-to-peer carriers are consulted: a mesh neighbor is one we
1026    /// can hand a frame to over our own radio, which is what makes it a
1027    /// candidate next hop. Infrastructure carriers (Internet, Nostr, Reticulum)
1028    /// reach peers through something else, so they report no links and appear
1029    /// here as nothing — a message routed through them is not taking a mesh
1030    /// hop, it is leaving the mesh.
1031    ///
1032    /// A peer visible on more than one carrier is reported once, on the first
1033    /// carrier that claims it; [`Self::send_to_neighbor`] resolves the same way,
1034    /// so the pair stay consistent.
1035    pub fn mesh_neighbors(&self) -> Vec<MeshNeighbor> {
1036        let mut neighbors: Vec<MeshNeighbor> = Vec::new();
1037
1038        for transport_type in MESH_TRANSPORTS {
1039            let Some(transport) = self.transports.get(transport_type) else {
1040                continue;
1041            };
1042            for link in transport.connected_peers() {
1043                if neighbors.iter().any(|n| n.peer_id == link.peer_id) {
1044                    continue;
1045                }
1046                neighbors.push(MeshNeighbor {
1047                    peer_id: link.peer_id,
1048                    rssi: link.rssi,
1049                    transport: *transport_type,
1050                });
1051            }
1052        }
1053
1054        neighbors
1055    }
1056
1057    /// Whether a frame for `peer_id` can plausibly arrive without being carried
1058    /// by other devices.
1059    ///
1060    /// This exists because **a send returning `Ok` is not evidence of
1061    /// reachability**. Only BLE refuses a recipient it holds no link to; Wi-Fi
1062    /// Direct and Reticulum enqueue for any recipient and report success, so a
1063    /// frame handed to them for someone out of range is queued for a link that
1064    /// will never drain — reported as sent, silently swallowed. Anything that
1065    /// decides "the mesh has to carry this" from a send failure therefore never
1066    /// fires on a device where one of those carriers is up. Asking this instead
1067    /// keeps that decision on facts we can check.
1068    ///
1069    /// Two ways the answer is yes:
1070    ///
1071    /// - An **infrastructure** carrier is available. Internet, Nostr and
1072    ///   Reticulum do their own routing and reach peers we hold no radio link
1073    ///   to, so a direct send is a real delivery attempt. (Reticulum counts
1074    ///   here because it is a routing network in its own right — its `Ok` means
1075    ///   "accepted for routing", which is as much as any relay-style carrier
1076    ///   promises.)
1077    /// - The peer is one of our own **mesh neighbors**, so a mesh carrier can
1078    ///   hand the frame straight over.
1079    ///
1080    /// Checked in that order so the common online case costs a status scan and
1081    /// never enumerates links.
1082    ///
1083    /// **This answer is deliberately peer-blind on its first arm**, and that is
1084    /// a real limit rather than an oversight: an infrastructure carrier being up
1085    /// says nothing about whether *this* recipient is on it. Callers that hold a
1086    /// peer-specific fact must not settle for this answer — the relay's own
1087    /// `recipient_unreachable` verdict is exactly such a fact, and the park it
1088    /// drives offers the frame to the mesh regardless of what this returns (see
1089    /// `park_unreachable_dm`).
1090    pub fn can_reach_without_carrying(&self, peer_id: &str) -> bool {
1091        self.has_infrastructure_carrier() || self.mesh_can_address(peer_id)
1092    }
1093
1094    /// Whether any carrier that does its own routing is available.
1095    ///
1096    /// Peer-blind by nature: these carriers reach peers we hold no radio link
1097    /// to, so there is no per-peer link to consult. Note the status is
1098    /// bridge-reported and means "the platform says this carrier is up", not
1099    /// "the relay connection is authenticated" — a distinction that matters
1100    /// wherever this stands in for reachability.
1101    pub fn has_infrastructure_carrier(&self) -> bool {
1102        self.transports.iter().any(|(transport_type, transport)| {
1103            !MESH_TRANSPORTS.contains(transport_type)
1104                && transport.status() == TransportStatus::Available
1105        })
1106    }
1107
1108    /// Whether a mesh carrier holds a live link straight to `peer_id`.
1109    ///
1110    /// The peer-specific half of [`Self::can_reach_without_carrying`], split out
1111    /// because callers that already know infrastructure is up still need to ask
1112    /// whether the mesh can hand a frame over without anyone carrying it.
1113    pub fn mesh_can_address(&self, peer_id: &str) -> bool {
1114        MESH_TRANSPORTS.iter().any(|transport_type| {
1115            self.transports
1116                .get(transport_type)
1117                .filter(|transport| transport.status() == TransportStatus::Available)
1118                .is_some_and(|transport| {
1119                    transport
1120                        .connected_peers()
1121                        .iter()
1122                        .any(|link| link.peer_id == peer_id)
1123                })
1124        })
1125    }
1126
1127    /// Whether `transport_type` is a short-range carrier that can only address
1128    /// peers it holds a live link to — as opposed to an infrastructure carrier
1129    /// that does its own routing.
1130    ///
1131    /// A frame that arrived over one of these travelled either from a neighbor
1132    /// or through them, which is what makes it evidence about the route back.
1133    pub fn is_mesh_transport(transport_type: TransportType) -> bool {
1134        MESH_TRANSPORTS.contains(&transport_type)
1135    }
1136
1137    /// Whether `transport_type` can actually put a frame in front of `peer_id`.
1138    ///
1139    /// The narrower question [`Self::can_reach_without_carrying`] answers across
1140    /// every carrier, asked of one. It exists for the same reason: a transport
1141    /// returning `Ok` is not evidence the frame can arrive. A **mesh** carrier
1142    /// can only address a peer it holds a live link to, and Wi-Fi Direct
1143    /// enqueues for any recipient regardless — so a frame handed to it for
1144    /// someone several hops away is queued for a link that never drains.
1145    /// **Infrastructure** carriers do their own routing, so for them the answer
1146    /// is yes whenever they are available.
1147    ///
1148    /// Callers that would otherwise trust a preferred transport — replying on
1149    /// the link a message arrived over, above all — must ask this first, or the
1150    /// reply dies on exactly the multi-hop path that made forwarding necessary.
1151    pub fn can_address_via(&self, transport_type: TransportType, peer_id: &str) -> bool {
1152        let Some(transport) = self.transports.get(&transport_type) else {
1153            return false;
1154        };
1155        if transport.status() != TransportStatus::Available {
1156            return false;
1157        }
1158        if !MESH_TRANSPORTS.contains(&transport_type) {
1159            return true;
1160        }
1161        transport
1162            .connected_peers()
1163            .iter()
1164            .any(|link| link.peer_id == peer_id)
1165    }
1166
1167    /// Hands `message` to a specific mesh neighbor, whatever the message's own
1168    /// recipient is.
1169    ///
1170    /// This is the forwarding primitive: the frame crosses one link, to the
1171    /// peer named by `peer_id`. Because that peer is the physical target, the
1172    /// binary wire codec is negotiated against **its** capabilities rather than
1173    /// the recipient's — stamping a forwarded frame binary because the distant
1174    /// recipient supports it could put bytes a neighbor cannot decode onto that
1175    /// neighbor's link.
1176    ///
1177    /// Returns the transport the frame went out on.
1178    pub fn send_to_neighbor(&self, peer_id: &str, message: &Message) -> Result<TransportType> {
1179        let stamped;
1180        let message = if message.wire_codec() != WireCodec::BinaryV1
1181            && self.peer_binary_wire.contains(peer_id)
1182        {
1183            let mut m = message.clone();
1184            m.set_wire_codec(WireCodec::BinaryV1);
1185            stamped = m;
1186            &stamped
1187        } else if message.wire_codec() == WireCodec::BinaryV1
1188            && !self.peer_binary_wire.contains(peer_id)
1189        {
1190            // The frame arrived stamped binary (or was stamped for a different
1191            // peer) but this hop cannot decode it. Fall back to the JSON floor
1192            // rather than writing bytes the neighbor will drop.
1193            let mut m = message.clone();
1194            m.set_wire_codec(WireCodec::Json);
1195            stamped = m;
1196            &stamped
1197        } else {
1198            message
1199        };
1200
1201        let mut last_error: Option<String> = None;
1202
1203        for transport_type in MESH_TRANSPORTS {
1204            let Some(transport) = self.transports.get(transport_type) else {
1205                continue;
1206            };
1207            if transport.status() != TransportStatus::Available {
1208                continue;
1209            }
1210            match transport.send_to_peer(peer_id, message) {
1211                Ok(()) => return Ok(*transport_type),
1212                Err(e) => last_error = Some(e.to_string()),
1213            }
1214        }
1215
1216        Err(Error::Transport(TransportError::PeerNotReachable(format!(
1217            "no mesh transport holds a link to {}{}",
1218            peer_id,
1219            last_error.map(|e| format!(" ({})", e)).unwrap_or_default()
1220        ))))
1221    }
1222
1223    /// Attempts to receive a message from any transport.
1224    ///
1225    /// # Returns
1226    ///
1227    /// Returns Ok(Some((TransportType, Message))) if a message was received, Ok(None) if no message available.
1228    pub fn receive(&self) -> Result<Option<(TransportType, Message)>> {
1229        // Check all transports for messages
1230        for (transport_type, transport) in &self.transports {
1231            match transport.receive() {
1232                Ok(Some(message)) => {
1233                    debug!(
1234                        transport = ?transport_type,
1235                        sender = %message.sender,
1236                        recipient = %message.recipient,
1237                        "TransportManager found message"
1238                    );
1239                    return Ok(Some((*transport_type, message)));
1240                }
1241                Ok(None) => {
1242                    // No message from this transport, continue checking others
1243                }
1244                Err(e) => {
1245                    warn!(
1246                        transport = ?transport_type,
1247                        error = %e,
1248                        "Transport receive error"
1249                    );
1250                }
1251            }
1252        }
1253        Ok(None)
1254    }
1255
1256    /// Gets metrics for all available transports.
1257    ///
1258    /// # Returns
1259    ///
1260    /// Returns a map of transport type to metrics.
1261    pub fn get_available_transports(&self) -> HashMap<TransportType, TransportMetrics> {
1262        let mut available = HashMap::new();
1263
1264        for (transport_type, transport) in &self.transports {
1265            let maybe_metrics = {
1266                let status = transport.status();
1267                if status == TransportStatus::Available {
1268                    Some(transport.metrics())
1269                } else {
1270                    debug!(
1271                        transport = ?transport_type,
1272                        status = ?status,
1273                        "Transport excluded from available set"
1274                    );
1275                    None
1276                }
1277            };
1278
1279            if let Some(mut metrics) = maybe_metrics {
1280                if let Some(stats) = self.observations.get(transport_type) {
1281                    stats.apply_to_metrics(&mut metrics);
1282                }
1283                self.apply_device_battery(&mut metrics);
1284                available.insert(*transport_type, metrics);
1285            }
1286        }
1287
1288        available
1289    }
1290
1291    /// Gets the current active transport type.
1292    pub fn current_transport(&self) -> Option<TransportType> {
1293        self.current_transport
1294    }
1295
1296    /// Returns the current status of every registered transport, including
1297    /// those not currently [`TransportStatus::Available`].
1298    ///
1299    /// Used by the telemetry aggregator to detect state transitions; unlike
1300    /// [`TransportManager::get_available_transports`], this only reads each
1301    /// transport's status with no metrics work.
1302    pub fn get_all_transport_statuses(&self) -> HashMap<TransportType, TransportStatus> {
1303        let mut out = HashMap::with_capacity(self.transports.len());
1304        for (transport_type, transport) in &self.transports {
1305            out.insert(*transport_type, transport.status());
1306        }
1307        out
1308    }
1309
1310    /// Returns the per-transport status map AND the available-only metrics
1311    /// map in a single pass.
1312    ///
1313    /// Equivalent to calling [`Self::get_all_transport_statuses`] and
1314    /// [`Self::get_available_transports`] back-to-back, but reads each
1315    /// transport's status once instead of twice. Used by the per-tick
1316    /// telemetry aggregator.
1317    pub fn snapshot_status_and_available(
1318        &self,
1319    ) -> (
1320        HashMap<TransportType, TransportStatus>,
1321        HashMap<TransportType, TransportMetrics>,
1322    ) {
1323        let mut statuses = HashMap::with_capacity(self.transports.len());
1324        let mut available = HashMap::new();
1325
1326        for (transport_type, transport) in &self.transports {
1327            let status = transport.status();
1328            let metrics = if status == TransportStatus::Available {
1329                Some(transport.metrics())
1330            } else {
1331                None
1332            };
1333            statuses.insert(*transport_type, status);
1334            if let Some(mut metrics) = metrics {
1335                if let Some(stats) = self.observations.get(transport_type) {
1336                    stats.apply_to_metrics(&mut metrics);
1337                }
1338                self.apply_device_battery(&mut metrics);
1339                available.insert(*transport_type, metrics);
1340            }
1341        }
1342
1343        (statuses, available)
1344    }
1345
1346    /// Starts all transports.
1347    pub fn start(&mut self) -> Result<()> {
1348        for (transport_type, transport) in &self.transports {
1349            transport.start().map_err(|e| {
1350                Error::Other(format!(
1351                    "Failed to start transport {:?}: {}",
1352                    transport_type, e
1353                ))
1354            })?;
1355        }
1356        Ok(())
1357    }
1358
1359    /// Stops all transports.
1360    pub fn stop(&mut self) -> Result<()> {
1361        for (transport_type, transport) in &self.transports {
1362            transport.stop().map_err(|e| {
1363                Error::Other(format!(
1364                    "Failed to stop transport {:?}: {}",
1365                    transport_type, e
1366                ))
1367            })?;
1368        }
1369        Ok(())
1370    }
1371
1372    /// Gets a shared handle to a specific transport.
1373    pub fn get_transport(&self, transport_type: TransportType) -> Option<Arc<dyn Transport>> {
1374        self.transports.get(&transport_type).cloned()
1375    }
1376
1377    /// Removes a transport from the manager.
1378    ///
1379    /// # Arguments
1380    ///
1381    /// * `transport_type` - Type of transport to remove
1382    pub fn remove_transport(&mut self, transport_type: TransportType) {
1383        self.transports.remove(&transport_type);
1384
1385        // Clear current transport if it was the one removed
1386        if self.current_transport == Some(transport_type) {
1387            self.current_transport = None;
1388        }
1389
1390        self.observations.remove(&transport_type);
1391    }
1392
1393    /// Gets a list of all active transport types.
1394    ///
1395    /// # Returns
1396    ///
1397    /// Returns a vector of transport types that are currently added to the manager.
1398    pub fn get_active_transports(&self) -> Vec<TransportType> {
1399        self.transports.keys().copied().collect()
1400    }
1401
1402    /// Checks if DORS suggests escalating from BLE to WiFi Direct.
1403    pub fn should_escalate_to_wifi(&self) -> bool {
1404        self.selector.should_escalate_to_wifi()
1405    }
1406
1407    /// Records a retry failure for the given transport.
1408    pub fn record_retry_failure(&mut self, transport_type: TransportType) {
1409        self.selector.record_retry_failure(transport_type);
1410    }
1411
1412    /// Resets retry count for the given transport after successful delivery.
1413    pub fn reset_retry_count(&mut self, transport_type: TransportType) {
1414        self.selector.reset_retry_count(transport_type);
1415    }
1416
1417    /// Records a successful end-to-end delivery for the given transport.
1418    pub fn record_delivery_success(
1419        &mut self,
1420        transport_type: TransportType,
1421        latency_ms: u32,
1422        hop_count: u8,
1423    ) {
1424        let stats = self.observations.entry(transport_type).or_default();
1425        stats.record_success(latency_ms, hop_count);
1426    }
1427
1428    /// Records a delivery failure (after exhausting retries) for the given transport.
1429    pub fn record_delivery_failure(&mut self, transport_type: TransportType) {
1430        let stats = self.observations.entry(transport_type).or_default();
1431        stats.record_failure();
1432    }
1433
1434    /// Updates the DORS selector configuration at runtime, preserving
1435    /// accumulated state (transport history, retry counts, signal tracking).
1436    pub fn update_selector_config(&mut self, config: DorsConfig) {
1437        self.selector.update_config(config);
1438    }
1439
1440    /// Returns the DORS configuration transport scoring runs against.
1441    pub fn selector_config(&self) -> &DorsConfig {
1442        self.selector.config()
1443    }
1444}
1445
1446#[cfg(test)]
1447mod tests {
1448    use super::*;
1449    use offline_protocol_core::{AppId, UserId};
1450    use offline_protocol_router::DorsConfig;
1451    use offline_protocol_transport::{mock::MockTransport, TransportType};
1452    use std::sync::Mutex;
1453
1454    fn create_test_message() -> Message {
1455        Message::new(
1456            UserId::new("alice").unwrap(),
1457            UserId::new("bob").unwrap(),
1458            AppId::new("test").unwrap(),
1459            "Test message",
1460        )
1461    }
1462
1463    #[test]
1464    fn test_transport_manager_creation() {
1465        let selector = TransportSelector::with_config(DorsConfig::default());
1466        let manager = TransportManager::new(selector);
1467        assert_eq!(manager.transports.len(), 0);
1468        assert!(manager.current_transport().is_none());
1469    }
1470
1471    #[test]
1472    fn test_add_transport() {
1473        let selector = TransportSelector::with_config(DorsConfig::default());
1474        let mut manager = TransportManager::new(selector);
1475
1476        let transport = Box::new(MockTransport::new(TransportType::BLE));
1477        manager.add_transport(TransportType::BLE, transport);
1478
1479        assert_eq!(manager.transports.len(), 1);
1480    }
1481
1482    #[test]
1483    fn test_start_stop_through_shared_transport_handle() {
1484        let selector = TransportSelector::with_config(DorsConfig::default());
1485        let mut manager = TransportManager::new(selector);
1486        manager.add_transport(
1487            TransportType::BLE,
1488            Box::new(MockTransport::new(TransportType::BLE)),
1489        );
1490
1491        // The handle shares the same transport the manager drives: start()
1492        // and stop() go through &self, no per-transport mutex involved.
1493        let handle = manager
1494            .get_transport(TransportType::BLE)
1495            .expect("transport registered");
1496        assert_eq!(handle.status(), TransportStatus::Unavailable);
1497
1498        manager.start().unwrap();
1499        assert_eq!(handle.status(), TransportStatus::Available);
1500
1501        manager.stop().unwrap();
1502        assert_eq!(handle.status(), TransportStatus::Disconnected);
1503    }
1504
1505    #[test]
1506    fn test_send_message() {
1507        let selector = TransportSelector::with_config(DorsConfig::default());
1508        let mut manager = TransportManager::new(selector);
1509
1510        let transport = MockTransport::new(TransportType::BLE);
1511        transport.start().unwrap();
1512        manager.add_transport(TransportType::BLE, Box::new(transport));
1513
1514        let message = create_test_message();
1515        let result = manager.send(&message);
1516        assert!(result.is_ok());
1517    }
1518
1519    #[test]
1520    fn send_stamps_binary_only_for_capable_peer() {
1521        let selector = TransportSelector::with_config(DorsConfig::default());
1522        let mut manager = TransportManager::new(selector);
1523        let transport = MockTransport::new(TransportType::BLE);
1524        transport.start().unwrap();
1525        manager.add_transport(TransportType::BLE, Box::new(transport));
1526
1527        // Unknown peer -> JSON (the default).
1528        manager.send(&create_test_message()).unwrap();
1529        // Peer recorded as binary-capable -> the send is stamped binary.
1530        manager.mark_peer_binary_wire("bob", true);
1531        manager.send(&create_test_message()).unwrap();
1532
1533        let mock = manager
1534            .transports
1535            .get(&TransportType::BLE)
1536            .unwrap()
1537            .as_any()
1538            .downcast_ref::<MockTransport>()
1539            .unwrap();
1540        let sent = mock.sent_messages();
1541        assert_eq!(sent.len(), 2);
1542        assert_eq!(sent[0].wire_codec(), WireCodec::Json);
1543        assert_eq!(sent[1].wire_codec(), WireCodec::BinaryV1);
1544    }
1545
1546    #[test]
1547    fn send_reverts_to_json_after_peer_downgrades() {
1548        let selector = TransportSelector::with_config(DorsConfig::default());
1549        let mut manager = TransportManager::new(selector);
1550        let transport = MockTransport::new(TransportType::BLE);
1551        transport.start().unwrap();
1552        manager.add_transport(TransportType::BLE, Box::new(transport));
1553
1554        // Learn the peer is binary-capable -> the next send is stamped binary.
1555        manager.mark_peer_binary_wire("bob", true);
1556        assert!(manager.peer_supports_binary_wire("bob"));
1557        manager.send(&create_test_message()).unwrap();
1558
1559        // A later key package that no longer advertises support downgrades the
1560        // peer (e.g. after a session reset, or the codec being turned off on
1561        // their side); subsequent sends must fall back to JSON, not keep
1562        // emitting binary a legacy build could not decode.
1563        manager.mark_peer_binary_wire("bob", false);
1564        assert!(!manager.peer_supports_binary_wire("bob"));
1565        manager.send(&create_test_message()).unwrap();
1566
1567        let mock = manager
1568            .transports
1569            .get(&TransportType::BLE)
1570            .unwrap()
1571            .as_any()
1572            .downcast_ref::<MockTransport>()
1573            .unwrap();
1574        let sent = mock.sent_messages();
1575        assert_eq!(sent.len(), 2);
1576        assert_eq!(sent[0].wire_codec(), WireCodec::BinaryV1);
1577        assert_eq!(sent[1].wire_codec(), WireCodec::Json);
1578    }
1579
1580    #[test]
1581    fn test_dors_events_emitted_when_callback_set() {
1582        let selector = TransportSelector::with_config(DorsConfig::default());
1583        let mut manager = TransportManager::new(selector);
1584
1585        let transport = MockTransport::new(TransportType::BLE);
1586        transport.start().unwrap();
1587        manager.add_transport(TransportType::BLE, Box::new(transport));
1588
1589        let events: std::sync::Arc<Mutex<Vec<Event>>> = std::sync::Arc::new(Mutex::new(Vec::new()));
1590        let events_clone = events.clone();
1591        manager.set_dors_event_callback(Some(Arc::new(move |e| {
1592            events_clone.lock().unwrap().push(e);
1593        })));
1594
1595        let message = create_test_message();
1596        let _ = manager.send(&message);
1597
1598        let captured = events.lock().unwrap();
1599        assert!(
1600            captured
1601                .iter()
1602                .any(|e| matches!(e, crate::events::Event::DorsScoreUpdated { .. })),
1603            "expected dors_score_updated"
1604        );
1605        assert!(
1606            captured
1607                .iter()
1608                .any(|e| { matches!(e, crate::events::Event::DorsTransportSelected { .. }) }),
1609            "expected dors_transport_selected"
1610        );
1611    }
1612
1613    #[test]
1614    fn test_receive_message() {
1615        let selector = TransportSelector::with_config(DorsConfig::default());
1616        let mut manager = TransportManager::new(selector);
1617
1618        let transport = MockTransport::new(TransportType::BLE);
1619        transport.start().unwrap();
1620        let message = create_test_message();
1621        transport.queue_message(message.clone());
1622
1623        manager.add_transport(TransportType::BLE, Box::new(transport));
1624
1625        let received = manager.receive().unwrap();
1626        assert!(matches!(received, Some((TransportType::BLE, _))));
1627    }
1628
1629    #[test]
1630    fn test_record_delivery_success_enriches_metrics() {
1631        let selector = TransportSelector::with_config(DorsConfig::default());
1632        let mut manager = TransportManager::new(selector);
1633
1634        let transport = MockTransport::new(TransportType::BLE);
1635        transport.start().unwrap();
1636        manager.add_transport(TransportType::BLE, Box::new(transport));
1637
1638        manager.record_delivery_success(TransportType::BLE, 150, 2);
1639
1640        let metrics = manager.get_available_transports();
1641        let ble_metrics = metrics.get(&TransportType::BLE).expect("metrics");
1642
1643        assert_eq!(ble_metrics.success_count, 1);
1644        assert_eq!(ble_metrics.failure_count, 0);
1645        assert!(ble_metrics.delivery_ratio.expect("delivery ratio") > 0.99);
1646        assert_eq!(ble_metrics.average_hop_count, Some(2.0));
1647        assert_eq!(ble_metrics.latency_ms, Some(150));
1648    }
1649
1650    #[test]
1651    fn test_record_delivery_failure_enriches_metrics() {
1652        let selector = TransportSelector::with_config(DorsConfig::default());
1653        let mut manager = TransportManager::new(selector);
1654
1655        let transport = MockTransport::new(TransportType::BLE);
1656        transport.start().unwrap();
1657        manager.add_transport(TransportType::BLE, Box::new(transport));
1658
1659        manager.record_delivery_failure(TransportType::BLE);
1660        let metrics = manager.get_available_transports();
1661        let ble_metrics = metrics.get(&TransportType::BLE).expect("metrics");
1662
1663        assert_eq!(ble_metrics.success_count, 0);
1664        assert_eq!(ble_metrics.failure_count, 1);
1665        assert!(ble_metrics.drop_rate.expect("drop ratio") > 0.99);
1666    }
1667
1668    /// Two transports, neither reporting a battery level of its own — the
1669    /// ordinary case, since no platform transport publishes one. Both must
1670    /// come back carrying the host reading, because every downstream consumer
1671    /// (DORS energy scoring, the relay role, the forwarding floor) resolves
1672    /// battery through this map and would otherwise take its unknown branch.
1673    #[test]
1674    fn test_device_battery_fills_transports_that_report_none() {
1675        let selector = TransportSelector::with_config(DorsConfig::default());
1676        let mut manager = TransportManager::new(selector);
1677
1678        for transport_type in [TransportType::BLE, TransportType::WiFiDirect] {
1679            let transport = MockTransport::new(transport_type);
1680            transport.start().unwrap();
1681            manager.add_transport(transport_type, Box::new(transport));
1682        }
1683
1684        assert_eq!(manager.device_battery(), (None, false));
1685        manager.set_device_battery(42, true);
1686        assert_eq!(manager.device_battery(), (Some(42), true));
1687
1688        let metrics = manager.get_available_transports();
1689        for transport_type in [TransportType::BLE, TransportType::WiFiDirect] {
1690            let m = metrics.get(&transport_type).expect("metrics");
1691            assert_eq!(m.battery_level, Some(42));
1692            assert!(m.is_charging);
1693        }
1694
1695        // The one-pass snapshot builds the same map and must agree with it —
1696        // the telemetry aggregator and the relay-role evaluation read this one.
1697        let (_statuses, available) = manager.snapshot_status_and_available();
1698        let m = available.get(&TransportType::BLE).expect("metrics");
1699        assert_eq!(m.battery_level, Some(42));
1700        assert!(m.is_charging);
1701    }
1702
1703    /// A transport that publishes its own battery reading keeps it, and keeps
1704    /// its own charging state with it: mixing a level from one source with a
1705    /// charging flag from another is what produces the spurious
1706    /// charging-state flips `device_battery_from_available` documents.
1707    #[test]
1708    fn test_transport_reported_battery_wins_over_device_feed() {
1709        let selector = TransportSelector::with_config(DorsConfig::default());
1710        let mut manager = TransportManager::new(selector);
1711
1712        let transport = MockTransport::new(TransportType::BLE);
1713        transport.start().unwrap();
1714        transport.set_metrics(TransportMetrics {
1715            battery_level: Some(7),
1716            is_charging: false,
1717            ..Default::default()
1718        });
1719        manager.add_transport(TransportType::BLE, Box::new(transport));
1720
1721        manager.set_device_battery(90, true);
1722
1723        let metrics = manager.get_available_transports();
1724        let m = metrics.get(&TransportType::BLE).expect("metrics");
1725        assert_eq!(m.battery_level, Some(7));
1726        assert!(!m.is_charging);
1727    }
1728
1729    #[test]
1730    fn test_device_battery_is_clamped() {
1731        let selector = TransportSelector::with_config(DorsConfig::default());
1732        let mut manager = TransportManager::new(selector);
1733        manager.set_device_battery(240, false);
1734        assert_eq!(manager.device_battery(), (Some(100), false));
1735    }
1736}