subduction_ephemeral 0.11.0

Ephemeral (non-persisted) messaging for Subduction
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
//! Ephemeral message handler.
//!
//! [`EphemeralHandler`] implements the [`Handler`] trait from `subduction_core`,
//! processing [`EphemeralMessage`]s independently of the sync protocol.
//! It manages its own subscription map and fans out ephemeral payloads
//! to authorized subscribers.
//!
//! # Publish API
//!
//! The application holds `Arc<EphemeralHandler>` and calls
//! [`publish()`](EphemeralHandler::publish) directly to send ephemeral
//! messages to subscribers. Inbound messages arrive via the callback
//! channel returned from [`new()`](EphemeralHandler::new).
//!
//! [`Handler`]: subduction_core::handler::Handler
//! [`EphemeralMessage`]: crate::message::EphemeralMessage

use alloc::{sync::Arc, vec::Vec};
use core::sync::atomic::{AtomicUsize, Ordering};

use async_channel::Sender;
use async_lock::Mutex;
use future_form::{FutureForm, Local, Sendable};
use futures::{StreamExt, stream::FuturesUnordered};
use nonempty::NonEmpty;
use sedimentree_core::collections::{Map, Set};
use subduction_core::{
    authenticated::Authenticated, connection::Connection, handler::Handler, peer::id::PeerId,
    spawn::Spawn,
};
use thiserror::Error;
use tracing::{debug, warn};

use crate::{
    clock::Clock,
    config::{EphemeralConfig, EphemeralEvent},
    message::EphemeralMessage,
    nonce_cache::EphemeralNonceCache,
    payload_header::EphemeralPayloadHeader,
    policy::EphemeralPolicy,
    topic::Topic,
};

/// Maximum unfinished fan-out sends per peer before further ephemeral
/// payloads to that peer are dropped.
///
/// Sends to a healthy peer complete as soon as the detached fan-out task is
/// polled, so the in-flight count stays near zero; only a backpressured
/// connection (e.g. a peer that stopped reading its socket) accumulates
/// toward the cap. Dropping is safe — ephemeral messages are
/// fire-and-forget by design, and a subscriber dozens of messages behind
/// has no use for stale payloads anyway.
///
/// This bounds how many detached fan-out sends can park against a wedged
/// connection between keepalive reaps. Sizing: large enough that a burst
/// dispatched before the executor polls the fan-out tasks doesn't shed
/// messages for healthy peers, small enough that a wedged peer holds at
/// most `64 × max_payload_size` (4 MiB at the 64 KiB default) in parked
/// sends.
///
/// This cap governs *parked sends* only. Total per-peer buffering also
/// includes the transport's own outbound queue (e.g. up to 1024 frames on
/// the WebSocket transport), which fills before sends start parking.
pub const MAX_INFLIGHT_EPHEMERAL_SENDS_PER_PEER: usize = 64;

/// Handler for ephemeral (non-persisted) messages.
///
/// Manages ephemeral subscriptions, performs authorization via
/// [`EphemeralPolicy`], verifies signatures on inbound messages,
/// deduplicates by nonce, and fans out payloads to subscribers.
///
/// Inbound relay fan-out runs *off* the per-peer dispatch permit: `handle`
/// spawns the sends via `Sp` so a backpressured subscriber parks a detached
/// task instead of stalling inbound dispatch (see
/// [`MAX_INFLIGHT_EPHEMERAL_SENDS_PER_PEER`] for the parking bound).
///
/// Construct via [`new()`](Self::new), which returns both the handler
/// and a receiver for inbound [`EphemeralEvent`]s.
#[allow(clippy::type_complexity)]
pub struct EphemeralHandler<
    Async: FutureForm,
    Conn: Clone + 'static,
    E: EphemeralPolicy<Async>,
    Clk: Clock,
    Sp,
> {
    /// Inbound subscriptions: which peers are subscribed to receive ephemeral messages from us.
    ephemeral_subscriptions: Arc<Mutex<Map<Topic, Set<PeerId>>>>,
    /// Outbound subscriptions: sedimentree IDs we want to receive ephemeral messages for.
    outgoing_subscriptions: Arc<Mutex<Set<Topic>>>,
    connections: Arc<Mutex<Map<PeerId, NonEmpty<Authenticated<Conn, Async>>>>>,
    policy: E,
    callback_tx: Sender<EphemeralEvent>,
    max_payload_size: usize,
    max_message_age: core::time::Duration,
    clock: Clk,
    nonce_cache: Arc<Mutex<EphemeralNonceCache>>,
    spawner: Sp,
    /// Unfinished fan-out sends per peer; see
    /// [`MAX_INFLIGHT_EPHEMERAL_SENDS_PER_PEER`].
    ///
    /// The map holds one shared counter per connected peer *generation*:
    /// [`on_peer_disconnect`](Handler::on_peer_disconnect) removes the
    /// entry, so a reconnecting peer gets a fresh counter and stragglers
    /// from the previous generation decrement their own orphaned counter
    /// harmlessly. Increment/decrement pairing is enforced by
    /// [`InflightGuard`] (RAII), so counters cannot leak even if a fan-out
    /// future is cancelled, aborted, or never polled.
    ///
    /// FIXME(core): the proactive disconnect APIs (`disconnect`,
    /// `disconnect_from_peer`, `disconnect_all`) tear the peer down
    /// without invoking `on_peer_disconnect`, so entries evicted via those
    /// paths linger — the same pre-existing gap as ephemeral subscriptions
    /// and the nonce cache. Fix belongs in `subduction_core`'s teardown.
    inflight_sends: Arc<Mutex<Map<PeerId, Arc<AtomicUsize>>>>,
}

impl<
    Async: FutureForm,
    Conn: Clone + 'static,
    E: EphemeralPolicy<Async> + Clone,
    Clk: Clock,
    Sp: Clone,
> Clone for EphemeralHandler<Async, Conn, E, Clk, Sp>
{
    fn clone(&self) -> Self {
        Self {
            ephemeral_subscriptions: self.ephemeral_subscriptions.clone(),
            outgoing_subscriptions: self.outgoing_subscriptions.clone(),
            connections: self.connections.clone(),
            policy: self.policy.clone(),
            callback_tx: self.callback_tx.clone(),
            max_payload_size: self.max_payload_size,
            max_message_age: self.max_message_age,
            clock: self.clock.clone(),
            nonce_cache: self.nonce_cache.clone(),
            spawner: self.spawner.clone(),
            inflight_sends: self.inflight_sends.clone(),
        }
    }
}

impl<Async: FutureForm, Conn: Clone + 'static, E: EphemeralPolicy<Async>, Clk: Clock, Sp>
    core::fmt::Debug for EphemeralHandler<Async, Conn, E, Clk, Sp>
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("EphemeralHandler").finish_non_exhaustive()
    }
}

impl<Async: FutureForm, Conn: Clone + 'static, E: EphemeralPolicy<Async>, Clk: Clock, Sp>
    EphemeralHandler<Async, Conn, E, Clk, Sp>
{
    /// Create a new ephemeral handler.
    ///
    /// Returns the handler and a receiver for inbound [`EphemeralEvent`]s.
    /// The `connections` map is shared with `Subduction` / `SyncHandler`.
    /// `spawner` runs relay fan-out sends detached from inbound dispatch.
    #[allow(clippy::type_complexity)]
    pub fn new(
        connections: Arc<Mutex<Map<PeerId, NonEmpty<Authenticated<Conn, Async>>>>>,
        policy: E,
        config: EphemeralConfig,
        clock: Clk,
        spawner: Sp,
    ) -> (Self, async_channel::Receiver<EphemeralEvent>) {
        let (tx, rx) = async_channel::bounded(config.channel_capacity);

        let handler = Self {
            ephemeral_subscriptions: Arc::new(Mutex::new(Map::new())),
            outgoing_subscriptions: Arc::new(Mutex::new(Set::new())),
            connections,
            policy,
            callback_tx: tx,
            max_payload_size: config.max_payload_size,
            max_message_age: config.max_message_age,
            clock,
            nonce_cache: Arc::new(Mutex::new(EphemeralNonceCache::new(
                config.nonce_window_duration,
            ))),
            spawner,
            inflight_sends: Arc::new(Mutex::new(Map::new())),
        };

        (handler, rx)
    }

    /// Publish a pre-signed ephemeral message to all subscribers.
    ///
    /// Checks payload size, seeds the nonce cache (so bounce-backs via
    /// gossip cycles are detected as duplicates on receive), gathers
    /// subscribers, filters by policy, and fans out.
    ///
    /// See [`design/ephemeral.md#bounce-back-amplification`] for the
    /// rationale behind the cache seed.
    ///
    /// [`design/ephemeral.md#bounce-back-amplification`]: https://github.com/inkandswitch/subduction/blob/main/design/ephemeral.md#bounce-back-amplification
    /// [`Signed::seal`]: subduction_crypto::signed::Signed::seal
    /// [`Handler::handle`]: subduction_core::handler::Handler::handle
    ///
    /// Errors on individual sends are logged but not propagated —
    /// fire-and-forget semantics.
    pub async fn publish(&self, msg: EphemeralMessage)
    where
        Conn: Connection<Async, EphemeralMessage>,
    {
        let EphemeralMessage::Ephemeral(ref signed) = msg else {
            warn!("publish called with non-Ephemeral message, ignoring");
            return;
        };
        // Decode just the header (id / nonce / timestamp / payload_len)
        // — no copy of the payload bytes, since we only need the
        // sizing info and the nonce-cache key.
        let Ok(header) = EphemeralPayloadHeader::try_decode(signed.fields_bytes()) else {
            warn!("publish called with undecodable Signed<EphemeralPayload>, ignoring");
            return;
        };
        let id = header.id;
        let nonce = header.nonce;
        let issuer = PeerId::from(signed.issuer());
        let payload_len = header.payload_len;

        let max_payload = self.max_payload_size;
        if payload_len > max_payload {
            warn!(
                id = %id,
                size = payload_len,
                max = max_payload,
                "ephemeral publish payload too large, dropping"
            );
            return;
        }

        // Seed the nonce cache so any bounce-back via gossip is treated
        // as a duplicate at recv. See design/ephemeral.md#bounce-back-amplification.
        // A `false` return here means this exact triple is already
        // present (replay of our own publish); silent no-op.
        let now = self.clock.now();
        {
            let mut cache = self.nonce_cache.lock().await;
            if !cache.check_and_insert(issuer, id, nonce, now) {
                debug!(
                    issuer = %issuer,
                    id = %id,
                    nonce = nonce,
                    "publish called with already-seen (issuer, topic, nonce); skipping fan-out"
                );
                return;
            }
        }

        // Peers subscribed to us (inbound) — we relay to them directly.
        let mut target_peers: Set<PeerId> = {
            let subs = self.ephemeral_subscriptions.lock().await;
            subs.get(&id)
                .map(|peers| peers.iter().copied().collect())
                .unwrap_or_default()
        };

        // If we have an outgoing subscription for this ID, also send to
        // all connected peers — they're the relays we subscribed to.
        let is_outgoing = self.outgoing_subscriptions.lock().await.contains(&id);
        if is_outgoing {
            let conns = self.connections.lock().await;
            for peer in conns.keys() {
                target_peers.insert(*peer);
            }
        }

        if target_peers.is_empty() {
            return;
        }

        let authorized_peers = self
            .policy
            .filter_authorized_subscribers(id, target_peers.into_iter().collect())
            .await;

        if authorized_peers.is_empty() {
            return;
        }

        // Collect target connections while holding the lock, then drop it
        // before awaiting sends to avoid holding the mutex across .await.
        let targets: Vec<Authenticated<Conn, Async>> = {
            let conns = self.connections.lock().await;
            authorized_peers
                .iter()
                .flat_map(|peer| {
                    conns
                        .get(peer)
                        .into_iter()
                        .flat_map(|peer_conns| peer_conns.iter().cloned())
                })
                .collect()
        };

        // Fan out concurrently so one backpressured peer can't head-of-line
        // block delivery to the others. Peers already at their in-flight cap
        // are dropped (fire-and-forget semantics) instead of parked on.
        //
        // The cap bounds how many sends can be *parked* per peer, not how
        // long this publish may block: `run()` completes only when every
        // admitted send does, so a wedged subscriber parks this call until
        // the transport tears the connection down. Callers needing bounded
        // latency may wrap `publish` in a timeout — cancellation is
        // leak-free (guards release on drop) and abandons in-flight sends,
        // which requires the transport's `send` to be cancel-safe (the
        // bundled transports are).
        if let Some(fan_out) = self.admit_fan_out(msg, targets).await {
            fan_out.run().await;
        }
    }

    /// Subscribe to ephemeral messages for the given topics.
    ///
    /// Sends `Subscribe` to all connected peers and tracks the topics so
    /// that newly connected peers (via [`subscribe_peer`](Self::subscribe_peer))
    /// also receive the subscription request.
    pub async fn subscribe(&self, topics: NonEmpty<Topic>)
    where
        Conn: Connection<Async, EphemeralMessage>,
    {
        {
            let mut outgoing = self.outgoing_subscriptions.lock().await;
            for topic in &topics {
                outgoing.insert(*topic);
            }
        }

        let msg = EphemeralMessage::Subscribe { topics };
        self.send_to_all_peers(&msg).await;
    }

    /// Unsubscribe from ephemeral messages for the given topics.
    ///
    /// Sends `Unsubscribe` to all connected peers and removes the topics
    /// from outgoing subscription tracking.
    pub async fn unsubscribe(&self, topics: NonEmpty<Topic>)
    where
        Conn: Connection<Async, EphemeralMessage>,
    {
        {
            let mut outgoing = self.outgoing_subscriptions.lock().await;
            for topic in &topics {
                outgoing.remove(topic);
            }
        }

        let msg = EphemeralMessage::Unsubscribe { topics };
        self.send_to_all_peers(&msg).await;
    }

    /// Send current outgoing ephemeral subscriptions to a specific peer.
    ///
    /// Call this after a new peer connects so they know to send us
    /// ephemeral messages for our subscribed topics.
    pub async fn subscribe_peer(&self, peer_id: PeerId)
    where
        Conn: Connection<Async, EphemeralMessage>,
    {
        let topics: NonEmpty<Topic> = {
            let outgoing = self.outgoing_subscriptions.lock().await;
            let topics: Vec<Topic> = outgoing.iter().copied().collect();
            let Some(topics) = NonEmpty::from_vec(topics) else {
                return;
            };
            topics
        };

        let msg = EphemeralMessage::Subscribe { topics };

        let targets: Vec<Authenticated<Conn, Async>> = {
            let conns = self.connections.lock().await;
            conns
                .get(&peer_id)
                .into_iter()
                .flat_map(|peer_conns| peer_conns.iter().cloned())
                .collect()
        };

        let msg_ref = &msg;
        let mut sends: FuturesUnordered<_> = targets
            .iter()
            .map(|conn| async move { (conn.peer_id(), conn.send(msg_ref).await) })
            .collect();
        while let Some((peer, result)) = sends.next().await {
            if let Err(e) = result {
                debug!(
                    %peer,
                    error = %e,
                    "ephemeral subscribe_peer send failed"
                );
            }
        }
    }

    async fn send_to_all_peers(&self, msg: &EphemeralMessage)
    where
        Conn: Connection<Async, EphemeralMessage>,
    {
        let targets: Vec<Authenticated<Conn, Async>> = {
            let conns = self.connections.lock().await;
            conns
                .values()
                .flat_map(|peer_conns| peer_conns.iter().cloned())
                .collect()
        };

        let mut sends: FuturesUnordered<_> = targets
            .iter()
            .map(|conn| async move { (conn.peer_id(), conn.send(msg).await) })
            .collect();
        while let Some((peer, result)) = sends.next().await {
            if let Err(e) = result {
                debug!(
                    %peer,
                    error = %e,
                    "ephemeral send failed"
                );
            }
        }
    }
}

/// Errors from the ephemeral handler.
#[derive(Debug, Error)]
pub enum EphemeralHandlerError<SendErr: core::error::Error> {
    /// A send to a peer failed.
    #[error("ephemeral send failed: {0}")]
    Send(SendErr),
}

#[future_form::future_form(
    Sendable where
        Conn: Connection<Sendable, EphemeralMessage>
            + Clone + Send + Sync + 'static,
        E: EphemeralPolicy<Sendable> + Send + Sync,
        E::SubscribeDisallowed: Send + 'static,
        E::PublishDisallowed: Send + 'static,
        Conn::SendError: Send + 'static,
        Clk: Clock + Send + Sync,
        Sp: Spawn<Sendable> + Send + Sync + 'static,
    Local where
        Conn: Connection<Local, EphemeralMessage>
            + Clone + 'static,
        E: EphemeralPolicy<Local>,
        Clk: Clock,
        Sp: Spawn<Local> + 'static
)]
impl<Async: FutureForm, Conn, E, Clk, Sp> Handler<Async, Conn>
    for EphemeralHandler<Async, Conn, E, Clk, Sp>
{
    type Message = EphemeralMessage;
    type HandlerError = EphemeralHandlerError<Conn::SendError>;

    fn handle<'a>(
        &'a self,
        conn: &'a Authenticated<Conn, Async>,
        message: EphemeralMessage,
    ) -> Async::Future<'a, Result<(), Self::HandlerError>> {
        Async::from_future(async move {
            // The relay fan-out is spawned OFF the per-peer dispatch permit
            // so a backpressured subscriber parks a detached task instead of
            // stalling inbound dispatch for this peer (and, transitively,
            // every publisher sharing a topic with the slow subscriber).
            if let Some(fan_out) = self.dispatch(conn, message).await? {
                self.spawner.spawn(Async::from_future(fan_out.run()));
            }
            Ok(())
        })
    }

    fn on_peer_disconnect(&self, peer: PeerId) -> Async::Future<'_, ()> {
        Async::from_future(async move {
            let mut subs = self.ephemeral_subscriptions.lock().await;
            subs.retain(|_id, peers| {
                peers.remove(&peer);
                !peers.is_empty()
            });

            self.nonce_cache.lock().await.remove_peer(peer);
            self.inflight_sends.lock().await.remove(&peer);

            debug!(
                peer = %peer,
                "cleaned ephemeral subscriptions, nonce cache, and in-flight accounting on disconnect"
            );
        })
    }
}

impl<
    Async: FutureForm,
    Conn: Connection<Async, EphemeralMessage> + Clone + 'static,
    E: EphemeralPolicy<Async>,
    Clk: Clock,
    Sp,
> EphemeralHandler<Async, Conn, E, Clk, Sp>
{
    async fn dispatch(
        &self,
        conn: &Authenticated<Conn, Async>,
        message: EphemeralMessage,
    ) -> Result<Option<EphemeralFanOut<Conn, Async>>, EphemeralHandlerError<Conn::SendError>> {
        match message {
            EphemeralMessage::Ephemeral { .. } => {
                return Ok(self.recv_ephemeral(conn, message).await);
            }
            EphemeralMessage::Subscribe { topics } => {
                self.recv_subscribe(conn, topics).await;
            }
            EphemeralMessage::Unsubscribe { topics } => {
                self.recv_unsubscribe(conn, topics).await;
            }
            EphemeralMessage::SubscribeRejected { .. } => {
                // Informational — nothing to do on the handler side.
                debug!("received SubscribeRejected (informational)");
            }
        }
        Ok(None)
    }

    /// Admit `targets` against the per-peer in-flight cap, incrementing the
    /// counter for each admitted connection.
    ///
    /// Connections whose peer is already at
    /// [`MAX_INFLIGHT_EPHEMERAL_SENDS_PER_PEER`] are dropped from the
    /// fan-out — fire-and-forget semantics make dropping safe, and it stops
    /// a wedged connection from accumulating parked send tasks. Returns
    /// `None` when nothing was admitted.
    async fn admit_fan_out(
        &self,
        message: EphemeralMessage,
        targets: Vec<Authenticated<Conn, Async>>,
    ) -> Option<EphemeralFanOut<Conn, Async>> {
        let admitted: Vec<(Authenticated<Conn, Async>, InflightGuard)> = {
            let mut inflight = self.inflight_sends.lock().await;
            targets
                .into_iter()
                .filter_map(|conn| {
                    let peer = conn.peer_id();
                    let counter = inflight
                        .entry(peer)
                        .or_insert_with(|| Arc::new(AtomicUsize::new(0)));

                    let admitted = InflightGuard::admit(counter);
                    if admitted.is_none() {
                        debug!(
                            peer = %peer,
                            cap = MAX_INFLIGHT_EPHEMERAL_SENDS_PER_PEER,
                            "ephemeral send dropped: peer at in-flight cap"
                        );
                    }
                    admitted.map(|guard| (conn, guard))
                })
                .collect()
        };

        if admitted.is_empty() {
            return None;
        }

        Some(EphemeralFanOut {
            message,
            targets: admitted,
        })
    }

    /// Handle an inbound signed ephemeral message from a peer.
    ///
    /// Step order: decode-unverified → size → age → cache `contains`
    /// (read-only) → verify → cache `check_and_insert` → authorise →
    /// deliver → fan out. The cache probe before verify is the
    /// cross-edge fast path; the post-verify `check_and_insert` is the
    /// only place cache state is written and is gated behind a
    /// successful signature check.
    ///
    /// See [`design/ephemeral.md`] for the full rationale and threat
    /// model.
    ///
    /// [`design/ephemeral.md`]: https://github.com/inkandswitch/subduction/blob/main/design/ephemeral.md#recv-ephemeralhandlerrecv_ephemeral
    #[allow(clippy::too_many_lines)]
    async fn recv_ephemeral(
        &self,
        conn: &Authenticated<Conn, Async>,
        message: EphemeralMessage,
    ) -> Option<EphemeralFanOut<Conn, Async>> {
        let EphemeralMessage::Ephemeral(ref signed) = message else {
            return None;
        };

        let relay = conn.peer_id();
        let sender = PeerId::from(signed.issuer());

        // 1. Decode the header fields (id / nonce / timestamp /
        //    payload_len) without verifying and without copying the
        //    payload bytes. These values are UNTRUSTED until step 3
        //    succeeds — used only for read-only checks below. The full
        //    payload is materialised once, post-verify, via
        //    `try_verify`.
        let header = match EphemeralPayloadHeader::try_decode(signed.fields_bytes()) {
            Ok(h) => h,
            Err(e) => {
                warn!(
                    relay = %relay,
                    error = %e,
                    "ephemeral payload undecodable, dropping"
                );
                return None;
            }
        };

        let untrusted_id = header.id;
        let untrusted_nonce = header.nonce;
        let untrusted_timestamp = header.timestamp;
        let payload_len = header.payload_len;

        // 2a. Payload size (signature-independent).
        let max_payload = self.max_payload_size;
        if payload_len > max_payload {
            warn!(
                originator = %sender,
                relay = %relay,
                id = %untrusted_id,
                size = payload_len,
                max = max_payload,
                "ephemeral payload too large, dropping"
            );
            return None;
        }

        // 2b. Message age (signature-independent).
        let now = self.clock.now();
        let max_age = self.max_message_age;
        {
            let age = now.abs_diff(untrusted_timestamp);
            if age > max_age {
                debug!(
                    originator = %sender,
                    relay = %relay,
                    id = %untrusted_id,
                    timestamp_secs = untrusted_timestamp.as_secs(),
                    now_secs = now.as_secs(),
                    age_secs = age.as_secs(),
                    max_age_secs = max_age.as_secs(),
                    "ephemeral message too old or too far in the future, dropping"
                );
                return None;
            }
        }

        // 2c. Read-only cache probe. Hit ⇒ drop before paying for Ed25519 verify.
        {
            let cache = self.nonce_cache.lock().await;
            if cache.contains(sender, untrusted_id, untrusted_nonce) {
                debug!(
                    originator = %sender,
                    relay = %relay,
                    id = %untrusted_id,
                    nonce = untrusted_nonce,
                    "duplicate ephemeral nonce (pre-verify fast path), dropping"
                );
                return None;
            }
        }

        // 3. Verify signature. `sender` is trusted from here on.
        let verified = match signed.try_verify() {
            Ok(v) => v,
            Err(e) => {
                warn!(
                    originator = %sender,
                    relay = %relay,
                    error = %e,
                    "ephemeral signature verification failed, dropping"
                );
                return None;
            }
        };

        let ep = verified.payload();
        let id = ep.id;
        let nonce = ep.nonce;

        // 4. Post-verify insert. The only place cache state is written.
        //    `false` here means a concurrent duplicate raced past 2c
        //    and inserted first; drop.
        {
            let mut cache = self.nonce_cache.lock().await;
            if !cache.check_and_insert(sender, id, nonce, now) {
                debug!(
                    originator = %sender,
                    relay = %relay,
                    id = %id,
                    nonce = nonce,
                    "duplicate ephemeral nonce (post-verify race), dropping"
                );
                return None;
            }
        }

        // 5. Check publish authorization (using verified originator).
        if let Err(e) = self.policy.authorize_publish(sender, id).await {
            debug!(
                originator = %sender,
                relay = %relay,
                id = %id,
                error = %e,
                "ephemeral publish unauthorized"
            );
            return None;
        }

        // 6. Deliver to local callback channel.
        let event = EphemeralEvent {
            id,
            sender,
            nonce,
            payload: ep.payload.clone(),
        };
        if self.callback_tx.try_send(event).is_err() {
            warn!("ephemeral callback channel full, dropping event");
        }

        // 7. Fan out to other subscribers, excluding:
        //    - the relay that forwarded the message to us
        //    - the originator (they already have it — they wrote it)
        let subscriber_peers: Vec<PeerId> = {
            let subs = self.ephemeral_subscriptions.lock().await;
            subs.get(&id)
                .map(|peers| {
                    peers
                        .iter()
                        .copied()
                        .filter(|p| *p != relay && *p != sender)
                        .collect()
                })
                .unwrap_or_default()
        };

        if subscriber_peers.is_empty() {
            return None;
        }

        let authorized_peers = self
            .policy
            .filter_authorized_subscribers(id, subscriber_peers)
            .await;

        // Collect target connections while holding the lock, then drop it
        // before awaiting sends to avoid holding the mutex across .await.
        let targets: Vec<Authenticated<Conn, Async>> = {
            let conns = self.connections.lock().await;
            authorized_peers
                .iter()
                .flat_map(|peer| {
                    conns
                        .get(peer)
                        .into_iter()
                        .flat_map(|peer_conns| peer_conns.iter().cloned())
                })
                .collect()
        };

        // Forward the original signed message as-is (preserving sender +
        // signature). The returned fan-out is spawned by `handle` OFF this
        // peer's dispatch permit; peers at their in-flight cap were dropped
        // by `admit_fan_out`.
        self.admit_fan_out(message, targets).await
    }

    /// Handle a subscribe request from a peer.
    ///
    /// Policy checks are batched first (no lock held), then all
    /// authorized topics are inserted under a single lock acquisition.
    async fn recv_subscribe(&self, conn: &Authenticated<Conn, Async>, topics: NonEmpty<Topic>) {
        let peer = conn.peer_id();
        let mut authorized = Vec::new();
        let mut rejected = Vec::new();

        // 1. Batch policy checks (no subscription lock held).
        for topic in &topics {
            if let Err(e) = self.policy.authorize_subscribe(peer, *topic).await {
                debug!(
                    peer = %peer,
                    topic = %topic,
                    error = %e,
                    "ephemeral subscribe rejected"
                );
                rejected.push(*topic);
            } else {
                authorized.push(*topic);
            }
        }

        // 2. Insert authorized topics under a single lock acquisition.
        if !authorized.is_empty() {
            let mut subs = self.ephemeral_subscriptions.lock().await;
            for topic in &authorized {
                subs.entry(*topic).or_default().insert(peer);
            }
        }

        if let Some(rejected) = NonEmpty::from_vec(rejected) {
            let msg = EphemeralMessage::SubscribeRejected { topics: rejected };
            if let Err(e) = conn.send(&msg).await {
                debug!(
                    peer = %peer,
                    error = %e,
                    "failed to send SubscribeRejected"
                );
            }
        }
    }

    /// Handle an unsubscribe request from a peer.
    async fn recv_unsubscribe(&self, conn: &Authenticated<Conn, Async>, topics: NonEmpty<Topic>) {
        let peer = conn.peer_id();
        let mut subs = self.ephemeral_subscriptions.lock().await;

        for topic in &topics {
            if let Some(peers) = subs.get_mut(topic) {
                peers.remove(&peer);
                if peers.is_empty() {
                    subs.remove(topic);
                }
            }
        }
    }
}

/// Deferred fan-out of an ephemeral payload to subscriber connections.
///
/// Built on the dispatch path but **run off it**: [`EphemeralHandler`]'s
/// `handle` spawns [`run`](Self::run) via the handler's spawner, so a
/// backpressured subscriber parks a detached task instead of holding the
/// originating peer's dispatch permit. `publish` runs it inline (the
/// application controls its own concurrency there).
///
/// Each target carries the [`InflightGuard`] claimed for it during
/// admission, so its slot in the per-peer budget is released exactly when
/// its send completes — or when the fan-out future is dropped without
/// running (cancellation, abort, a spawner that never polls). Counters
/// cannot leak.
struct EphemeralFanOut<Conn: Clone + 'static, Async: FutureForm> {
    message: EphemeralMessage,
    targets: Vec<(Authenticated<Conn, Async>, InflightGuard)>,
}

impl<Conn, Async> EphemeralFanOut<Conn, Async>
where
    Conn: Connection<Async, EphemeralMessage> + Clone + 'static,
    Async: FutureForm,
{
    /// Send the payload to every admitted target concurrently.
    ///
    /// Sends run via `FuturesUnordered` so one slow target can't
    /// head-of-line block the others. Failures are logged and dropped
    /// (fire-and-forget). Each target's [`InflightGuard`] is dropped the
    /// moment its own send completes, re-admitting that peer for future
    /// fan-outs — a wedged sibling target does not pin it.
    async fn run(self) {
        let Self { message, targets } = self;
        let msg = &message;

        let mut sends: FuturesUnordered<_> = targets
            .into_iter()
            .map(|(conn, guard)| async move {
                let result = conn.send(msg).await;
                // Release this peer's budget slot as soon as *its* send
                // finishes, independent of the other targets.
                drop(guard);
                (conn.peer_id(), result)
            })
            .collect();

        while let Some((peer, result)) = sends.next().await {
            if let Err(e) = result {
                debug!(
                    %peer,
                    error = %e,
                    "ephemeral fan-out send failed"
                );
            }
        }
    }
}

/// RAII claim on one slot of a peer's in-flight fan-out budget.
///
/// Claimed via [`admit`](Self::admit) under the cap check; released on
/// `Drop`. Tying the release to `Drop` (rather than an explicit decrement
/// after each send) makes the accounting immune to cancelled `publish`
/// futures, aborted spawns, and spawners that drop tasks without polling
/// them — none of which can leak a slot and starve a healthy peer.
///
/// Holds its own [`Arc`] to the counter, so a guard from a previous
/// connection generation (the peer disconnected and its map entry was
/// replaced) decrements the orphaned counter, never the fresh one.
struct InflightGuard(Arc<AtomicUsize>);

impl InflightGuard {
    /// Claim a slot against [`MAX_INFLIGHT_EPHEMERAL_SENDS_PER_PEER`].
    ///
    /// Returns `None` when the peer is at its cap.
    fn admit(counter: &Arc<AtomicUsize>) -> Option<Self> {
        counter
            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| {
                (n < MAX_INFLIGHT_EPHEMERAL_SENDS_PER_PEER).then_some(n + 1)
            })
            .ok()
            .map(|_| Self(Arc::clone(counter)))
    }
}

impl Drop for InflightGuard {
    fn drop(&mut self) {
        self.0.fetch_sub(1, Ordering::Relaxed);
    }
}