zakura-network 2.0.0

Networking code for the Zakura node. Internal crate, published to support cargo install zakura
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
//! The core per-peer pipe vocabulary shared by every Zakura service.
//!
//! A *pipe* is the single async function that drives one peer's ordered stream
//! from connect to disconnect. This module owns the shared abstraction — the
//! per-stage control flow ([`Flow`]), the per-peer context ([`PipeCx`]), the
//! checked-documentation DAG ([`PipeShape`]), the generic inbound runner
//! ([`Pipe`]/[`PipeSink`]), and the panic-containment launcher
//! ([`spawn_supervised_pipe`]). Services depend on this vocabulary but never
//! re-implement it.
//!
//! Anti-framework rule: [`PipeShape`] is documentation that is *checked*, never
//! an interpreter that is *executed*. Stages are plain `fn`s; the hot path is a
//! `match`, not a walk over `PipeShape.edges`.

use std::{
    future::Future,
    sync::{Arc, OnceLock},
};

use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;

use super::{Frame, FramedRecv, SinkReject};
use crate::zakura::transport::guard::{Admit, SessionGuard};
use crate::zakura::ZakuraPeerId;

/// Shared first-cause connection close attribution.
///
/// A Zakura connection can be cancelled from the supervisor, the transport loop,
/// or any service task. The final `closed.neutral` trace row is emitted by the
/// transport loop, so every teardown path records its bounded reason here before
/// cancelling the shared connection token. The first writer wins.
#[derive(Clone, Debug)]
pub(crate) struct CloseCause {
    reason: Arc<OnceLock<&'static str>>,
}

impl CloseCause {
    /// Create an empty close-cause recorder.
    pub(crate) fn new() -> Self {
        Self {
            reason: Arc::new(OnceLock::new()),
        }
    }

    /// Record `reason` if no earlier close cause was recorded.
    pub(crate) fn record(&self, reason: &'static str) {
        let _ = self.reason.set(reason);
    }

    /// Return the recorded reason, or `default` when no local path recorded one.
    pub(crate) fn get_or(&self, default: &'static str) -> &'static str {
        self.reason.get().copied().unwrap_or(default)
    }
}

impl Default for CloseCause {
    fn default() -> Self {
        Self::new()
    }
}

/// Per-stage control flow.
///
/// Continue with a value, finish this frame cleanly (throttled/deduped — *not*
/// an error), or reject the peer.
pub(crate) enum Flow<T> {
    /// Continue the traversal carrying `T`.
    Continue(T),
    /// Finish this frame cleanly without rejecting the peer.
    Done,
    /// Reject the peer; the connection should close.
    Reject(SinkReject),
}

/// Per-peer context handed to every stage by reference.
///
/// Local state is owned here (`S`); shared state is reached through `env`.
pub(crate) struct PipeCx<'a, S, Env> {
    /// Authenticated identity of the peer this pipe drives.
    pub(crate) peer_id: &'a ZakuraPeerId,
    /// The per-peer state machine — no locking.
    pub(crate) local: &'a mut S,
    /// Arc-cloneable shared environment handle.
    pub(crate) env: &'a Env,
}

/// One inbound frame's whole traversal — the only fn-ptr indirection per frame.
pub(crate) type PipeEntry<S, Env> = fn(&mut PipeCx<'_, S, Env>, Frame) -> Flow<()>;

/// A node kind in a [`PipeShape`] DAG.
///
/// `Validate`/`Outcome`-style effect nodes and the `Stage`/`branch!`/`Outcome`
/// scaffolding for them are deliberately absent: today every service forwards a
/// decoded `WireMessage` to its compatibility reactor, so the live shapes only
/// use these variants. The effect-returning `Core` migration (see the pipelines
/// plan) reintroduces exactly what it needs when it lands.
#[derive(Copy, Clone, Debug)]
pub(crate) enum NodeKind {
    /// The shared protection stage.
    Guard,
    /// `Frame` to typed message.
    Decode,
    /// The single `match` on the typed message.
    Branch,
    /// Local/shared state mutation producing an effect.
    Mutate,
    /// Effect execution (the async sends/actions/fanout).
    Emit,
}

/// One node in a [`PipeShape`] DAG.
#[derive(Copy, Clone, Debug)]
// `PIPE_SHAPE` constructs these nodes in non-test lib code, but the fields are
// only *read* by the per-service `pipe_shape_matches_runtime` drift test, so the
// lib build still sees them as unread.
#[allow(dead_code)]
pub(crate) struct Node {
    /// Stable node identifier, referenced by [`Edge`]s.
    pub(crate) id: &'static str,
    /// The kind of work this node performs.
    pub(crate) kind: NodeKind,
}

/// One directed edge in a [`PipeShape`] DAG.
#[derive(Copy, Clone, Debug)]
// Read only by the per-service drift test (see [`Node`]).
#[allow(dead_code)]
pub(crate) struct Edge {
    /// Source [`Node::id`].
    pub(crate) from: &'static str,
    /// Destination [`Node::id`].
    pub(crate) to: &'static str,
    /// The condition (branch arm or stage result) this edge is taken on.
    pub(crate) on: &'static str,
}

/// The inspectable DAG for one service's per-peer pipe.
///
/// This is checked documentation: a per-service drift test asserts the runtime
/// `match` matches this shape. It is never walked to decide control flow.
#[derive(Copy, Clone, Debug)]
// Read only by the per-service drift test (see [`Node`]).
#[allow(dead_code)]
pub(crate) struct PipeShape {
    /// Stable service name for diagnostics.
    pub(crate) service: &'static str,
    /// Every node in the DAG.
    pub(crate) nodes: &'static [Node],
    /// Every directed edge in the DAG.
    pub(crate) edges: &'static [Edge],
}

impl PipeShape {
    /// Validate that every edge endpoint names a real node.
    ///
    /// This is the reusable half of the per-service drift test; the
    /// service-specific half (matching the runtime `match` arms) lands with each
    /// service migration. Called only from those `#[cfg(test)]` drift tests.
    #[allow(dead_code)] // called only from per-service drift tests
    pub(crate) fn validate(&self) -> Result<(), String> {
        for edge in self.edges {
            if !self.nodes.iter().any(|node| node.id == edge.from) {
                return Err(format!(
                    "{}: edge from unknown node {:?}",
                    self.service, edge.from
                ));
            }
            if !self.nodes.iter().any(|node| node.id == edge.to) {
                return Err(format!(
                    "{}: edge to unknown node {:?}",
                    self.service, edge.to
                ));
            }
        }
        Ok(())
    }
}

/// A per-peer pipe, built once per connected peer and consumed by the runner.
pub(crate) struct Pipe<S, Env> {
    peer_id: ZakuraPeerId,
    local: S,
    env: Env,
    guard: SessionGuard,
    entry: PipeEntry<S, Env>,
    // The declared shape is retained for per-service drift diagnostics; it is
    // checked documentation, never walked at runtime, so the runner never reads
    // it. The per-service drift test reads the same `&'static PipeShape`.
    #[allow(dead_code)] // retained for diagnostics; the runner never reads it
    shape: &'static PipeShape,
}

impl<S, Env> Pipe<S, Env> {
    /// Build a per-peer pipe from its local state, environment, guard, entry,
    /// and declared shape.
    pub(crate) fn new(
        peer_id: ZakuraPeerId,
        local: S,
        env: Env,
        guard: SessionGuard,
        entry: PipeEntry<S, Env>,
        shape: &'static PipeShape,
    ) -> Self {
        Self {
            peer_id,
            local,
            env,
            guard,
            entry,
            shape,
        }
    }

    /// Run one inbound frame: admit through the guard, then the entry traversal.
    ///
    /// This is the single inbound traversal used by both the production sink and
    /// the test/recorder path, so the two can never disagree.
    pub(crate) fn run_one(&mut self, frame: Frame) -> Flow<()> {
        match self.guard.admit(&frame) {
            Admit::Throttle => Flow::Done,
            Admit::Reject(reason) => Flow::Reject(SinkReject::protocol(reason)),
            Admit::Pass => {
                let mut cx = PipeCx {
                    peer_id: &self.peer_id,
                    local: &mut self.local,
                    env: &self.env,
                };
                (self.entry)(&mut cx, frame)
            }
        }
    }

    /// Borrow the peer-local state for service-specific side channels.
    pub(crate) fn local_mut(&mut self) -> &mut S {
        &mut self.local
    }
}

/// The generic inbound runner for a per-peer pipe.
///
/// `PipeSink::run` is the single inbound loop: `recv → guard.admit → entry →
/// map Flow`. block_sync drives its stream-6 inbound path through it directly;
/// header_sync and discovery fork their own loops (command draining / async
/// handler handoff) that are genuinely different shapes.
#[allow(dead_code)]
pub(crate) struct PipeSink<S, Env> {
    pipe: Pipe<S, Env>,
    recv: FramedRecv,
    cancel: CancellationToken,
}

#[allow(dead_code)]
impl<S, Env> PipeSink<S, Env>
where
    S: Send + 'static,
    Env: Send + Sync + 'static,
{
    /// Build the inbound runner around a pipe and its receive half.
    pub(crate) fn new(pipe: Pipe<S, Env>, recv: FramedRecv, cancel: CancellationToken) -> Self {
        Self { pipe, recv, cancel }
    }

    /// Run the inbound loop until the stream closes or the peer is cancelled.
    ///
    /// `Throttle` continues the loop, `Reject` returns `Err(SinkReject)`, and
    /// stream end or cancellation returns `Ok(())`.
    pub(crate) async fn run(mut self) -> Result<(), SinkReject> {
        loop {
            let frame = tokio::select! {
                () = self.cancel.cancelled() => return Ok(()),
                frame = self.recv.recv() => frame,
            };
            let Some(frame) = frame else {
                return Ok(());
            };
            match self.pipe.run_one(frame) {
                Flow::Continue(()) | Flow::Done => continue,
                Flow::Reject(reject) => return Err(reject),
            }
        }
    }
}

/// Cleanup that runs when a supervised pipe task ends, on every exit path.
///
/// Living in `Drop`, the teardown runs whether the pipe future returns normally,
/// returns after a reject, or unwinds on a panic — all from within the single
/// spawned pipe task, with no second `tokio::spawn`. This depends on the build
/// unwinding rather than aborting: under `panic = "unwind"` the `Drop` runs as
/// the panic unwinds the task and tokio catches the panic at the task boundary,
/// so the blast radius is exactly one peer (security_requirements.md SR-1). The
/// `#[cfg(panic = "abort")] compile_error!` above [`spawn_supervised_pipe`]
/// refuses to build the node with abort, where this `Drop` could not run and a
/// single peer panic would kill the whole node.
struct PipeTeardown<F: FnOnce(), P: FnOnce()> {
    peer_id: ZakuraPeerId,
    cancel: CancellationToken,
    on_teardown: Option<F>,
    on_panic: Option<P>,
}

impl<F: FnOnce(), P: FnOnce()> Drop for PipeTeardown<F, P> {
    fn drop(&mut self) {
        if std::thread::panicking() {
            metrics::counter!("zakura.pipe.panic").increment(1);
            tracing::error!(
                peer_id = ?self.peer_id,
                "Zakura peer pipe panicked; disconnecting peer only"
            );
            if let Some(on_panic) = self.on_panic.take() {
                on_panic();
            }
        }
        self.cancel.cancel();
        if let Some(on_teardown) = self.on_teardown.take() {
            on_teardown();
        }
    }
}

// Peer-pipe panic containment (security_requirements.md SR-1) depends on
// unwinding: `PipeTeardown`'s `Drop` runs during the unwind to disconnect and
// clean up the panicked peer, and tokio catches the task panic so the blast
// radius is one peer. Under `panic = "abort"` none of that can happen — a single
// peer panic aborts the whole node — so refuse to build that way. The workspace
// [profile.*] tables set `panic = "unwind"`; this guard catches a silent
// regression back to abort.
#[cfg(panic = "abort")]
compile_error!(
    "Zakura peer-pipe panic containment requires `panic = \"unwind\"` \
     (security_requirements.md SR-1); this build sets `panic = \"abort\"`. \
     Set panic = \"unwind\" in the workspace [profile.dev] and [profile.release]."
);

/// Launch a per-peer pipe in its own supervised task.
///
/// This is the single way pipes are launched. The pipe runs inside one spawned
/// task guarded by a [`PipeTeardown`], which cancels the caller-supplied `cancel`
/// token and runs `on_teardown` (which must be idempotent) on every exit path —
/// normal return, reject, or panic. On panic only, it also runs `on_panic`.
/// Callers pass the token whose cancellation is safe on *every* exit (e.g. a
/// per-service token, not a shared connection token that other services ride
/// on); connection-level teardown that must only happen on a fatal reject
/// belongs inside the `pipe` future (see [`handle_pipe_exit`]), and
/// connection-level teardown for panic belongs in `on_panic`. A panicking peer is
/// contained to its own task without a nested `tokio::spawn`.
///
/// Returns the task's [`JoinHandle`]: services let it drop to detach the task (it
/// self-reaps; the `PipeTeardown` still runs on every exit), while the
/// panic-containment test awaits it to observe the contained panic.
pub(crate) fn spawn_supervised_pipe(
    peer_id: ZakuraPeerId,
    cancel: CancellationToken,
    on_teardown: impl FnOnce() + Send + 'static,
    on_panic: impl FnOnce() + Send + 'static,
    pipe: impl Future<Output = ()> + Send + 'static,
) -> JoinHandle<()> {
    tokio::spawn(async move {
        let _teardown = PipeTeardown {
            peer_id,
            cancel,
            on_teardown: Some(on_teardown),
            on_panic: Some(on_panic),
        };
        pipe.await;
    })
}

/// Cleanup that runs when a supervised *non-pipe* peer task ends, on every exit
/// path.
///
/// This is the task-level sibling of [`PipeTeardown`] for the peer-influenced
/// service tasks that are not the generic inbound [`Pipe`] runner — e.g. the
/// discovery source/admission helpers and (once adopted) the legacy gossip
/// replay/receive loops. Unlike [`PipeTeardown`] it owns no [`CancellationToken`]
/// of its own: those tasks decide which token(s) a panic must cancel from inside
/// their `on_panic` hook, because some of them ride directly on the shared
/// connection token, which must *not* be cancelled on a normal exit (a clean
/// stream-end of one service must not tear the whole connection down). On panic
/// it runs `on_panic` during the unwind; on every exit it runs `on_teardown`.
struct PeerTaskTeardown<F: FnOnce(), P: FnOnce()> {
    peer_id: ZakuraPeerId,
    on_teardown: Option<F>,
    on_panic: Option<P>,
}

impl<F: FnOnce(), P: FnOnce()> Drop for PeerTaskTeardown<F, P> {
    fn drop(&mut self) {
        if std::thread::panicking() {
            metrics::counter!("zakura.pipe.panic").increment(1);
            tracing::error!(
                peer_id = ?self.peer_id,
                "Zakura peer task panicked; disconnecting peer only"
            );
            if let Some(on_panic) = self.on_panic.take() {
                on_panic();
            }
        }
        if let Some(on_teardown) = self.on_teardown.take() {
            on_teardown();
        }
    }
}

/// Launch a peer-influenced, non-pipe service task in its own supervised task.
///
/// This is the task-level counterpart to [`spawn_supervised_pipe`] for the
/// peer-driven helper tasks that are *not* the generic inbound pipe runner
/// (discovery source/admission, legacy gossip replay/receive). The task runs
/// inside one spawned task guarded by a [`PeerTaskTeardown`], which runs
/// `on_teardown` (which must be idempotent) on every exit path — normal return
/// or panic — and `on_panic` on panic only. Callers put whatever connection /
/// service cancellation a panic requires inside `on_panic`, so a buggy or hostile
/// peer that panics one of these tasks still disconnects *that one peer* and runs
/// its cleanup instead of leaving stale service state behind a half-live
/// connection (security_requirements.md SR-1). Like [`spawn_supervised_pipe`]
/// this depends on the build unwinding rather than aborting; the
/// `#[cfg(panic = "abort")] compile_error!` above guards that for both wrappers.
///
/// Returns the task's [`JoinHandle`]: services let it drop to detach the task (it
/// self-reaps; the `PeerTaskTeardown` still runs on every exit), while the
/// panic-containment test awaits it to observe the contained panic.
pub(crate) fn spawn_supervised_peer_task(
    peer_id: ZakuraPeerId,
    on_teardown: impl FnOnce() + Send + 'static,
    on_panic: impl FnOnce() + Send + 'static,
    task: impl Future<Output = ()> + Send + 'static,
) -> JoinHandle<()> {
    tokio::spawn(async move {
        let _teardown = PeerTaskTeardown {
            peer_id,
            on_teardown: Some(on_teardown),
            on_panic: Some(on_panic),
        };
        task.await;
    })
}

/// Map a finished pipe run to its connection-teardown effect — the single place
/// the "is this exit fatal to the whole connection?" decision lives.
///
/// A protocol reject is fatal: it cancels the shared `connection_cancel` token so
/// the whole connection tears down. A local reject (e.g. a closed service queue)
/// tears down only this stream — the per-service token is already cancelled by
/// the [`PipeTeardown`] — so it is logged and the connection is left for other
/// services. `Ok` is a normal/parked exit and does nothing here. Panic-path
/// connection teardown is separate (`on_panic`), because a panic never returns a
/// `Result` to inspect.
pub(crate) fn handle_pipe_exit(
    service: &'static str,
    connection_cancel: &CancellationToken,
    close_cause: &CloseCause,
    result: Result<(), SinkReject>,
) {
    match result {
        Ok(()) => {}
        Err(SinkReject::Protocol(error)) => {
            tracing::debug!(
                ?error,
                service,
                "Zakura stream rejected protocol-invalid frame"
            );
            close_cause.record("service_protocol_reject");
            connection_cancel.cancel();
        }
        Err(SinkReject::Local(error)) => {
            tracing::debug!(?error, service, "Zakura stream stopped on local error");
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    use super::*;
    use crate::zakura::transport::framed_channel;
    use crate::zakura::transport::guard::ByteBudget;

    const SHAPE: PipeShape = PipeShape {
        service: "test",
        nodes: &[
            Node {
                id: "guard",
                kind: NodeKind::Guard,
            },
            Node {
                id: "decode",
                kind: NodeKind::Decode,
            },
        ],
        edges: &[Edge {
            from: "guard",
            to: "decode",
            on: "Pass",
        }],
    };

    const DANGLING_SHAPE: PipeShape = PipeShape {
        service: "test-dangling",
        nodes: &[Node {
            id: "guard",
            kind: NodeKind::Guard,
        }],
        edges: &[Edge {
            from: "guard",
            to: "missing",
            on: "Pass",
        }],
    };

    fn peer_id() -> ZakuraPeerId {
        ZakuraPeerId::new(vec![1, 2, 3]).expect("3-byte id is within the node-id bound")
    }

    fn noop_entry(_cx: &mut PipeCx<'_, (), ()>, _frame: Frame) -> Flow<()> {
        Flow::Continue(())
    }

    fn pass_guard() -> SessionGuard {
        // Allow message type 1, generous size cap, no byte budget.
        SessionGuard::new(&[1], 1_024, None)
    }

    fn frame(message_type: u16) -> Frame {
        Frame {
            message_type,
            flags: 0,
            payload: Vec::new(),
        }
    }

    #[test]
    fn pipe_shape_validate_accepts_consistent_graph() {
        assert!(SHAPE.validate().is_ok());
    }

    #[test]
    fn pipe_shape_validate_rejects_dangling_edge() {
        let err = DANGLING_SHAPE
            .validate()
            .expect_err("dangling edge target must fail validation");
        assert!(err.contains("missing"), "error names the bad node: {err}");
    }

    #[test]
    fn run_one_passes_admitted_frame_to_entry() {
        let mut pipe = Pipe::new(peer_id(), (), (), pass_guard(), noop_entry, &SHAPE);
        assert!(matches!(pipe.run_one(frame(1)), Flow::Continue(())));
    }

    #[test]
    fn run_one_throttles_when_budget_exhausted() {
        // A budget that admits one 4-byte payload then throttles the next.
        let guard = SessionGuard::new(&[1], 1_024, Some(ByteBudget::new(4)));
        let mut pipe = Pipe::new(peer_id(), (), (), guard, noop_entry, &SHAPE);
        let mut framed = frame(1);
        framed.payload = vec![0u8; 4];
        assert!(matches!(pipe.run_one(framed.clone()), Flow::Continue(())));
        assert!(matches!(pipe.run_one(framed), Flow::Done));
    }

    #[test]
    fn run_one_rejects_disallowed_type() {
        let mut pipe = Pipe::new(peer_id(), (), (), pass_guard(), noop_entry, &SHAPE);
        assert!(matches!(pipe.run_one(frame(2)), Flow::Reject(_)));
    }

    #[test]
    fn close_cause_keeps_first_recorded_reason() {
        let cause = CloseCause::new();

        cause.record("first");
        cause.record("second");

        assert_eq!(cause.get_or("fallback"), "first");
    }

    #[test]
    fn close_cause_uses_default_without_recorded_reason() {
        let cause = CloseCause::new();

        assert_eq!(cause.get_or("fallback"), "fallback");
    }

    #[test]
    fn protocol_pipe_exit_records_close_cause_and_cancels_connection() {
        let cancel = CancellationToken::new();
        let cause = CloseCause::new();

        handle_pipe_exit(
            "test",
            &cancel,
            &cause,
            Err(SinkReject::protocol("bad frame")),
        );

        assert!(cancel.is_cancelled());
        assert_eq!(cause.get_or("fallback"), "service_protocol_reject");
    }

    #[tokio::test]
    async fn pipe_sink_run_returns_ok_on_stream_end() {
        let (send, recv) = framed_channel(4);
        let cancel = CancellationToken::new();
        let pipe = Pipe::new(peer_id(), (), (), pass_guard(), noop_entry, &SHAPE);
        let sink = PipeSink::new(pipe, recv, cancel);
        // One admitted frame, then close the stream by dropping the sender.
        send.send(frame(1)).await.expect("channel has capacity");
        drop(send);
        assert!(sink.run().await.is_ok());
    }

    #[tokio::test]
    async fn pipe_sink_run_returns_err_on_reject() {
        let (send, recv) = framed_channel(4);
        let cancel = CancellationToken::new();
        let pipe = Pipe::new(peer_id(), (), (), pass_guard(), noop_entry, &SHAPE);
        let sink = PipeSink::new(pipe, recv, cancel);
        // A disallowed type rejects the peer.
        send.send(frame(2)).await.expect("channel has capacity");
        assert!(sink.run().await.is_err());
    }

    #[tokio::test]
    async fn supervised_pipe_runs_teardown_on_panic() {
        let cancel = CancellationToken::new();
        let torn_down = Arc::new(AtomicBool::new(false));
        let flag = torn_down.clone();
        let panic_disconnected = Arc::new(AtomicBool::new(false));
        let panic_flag = panic_disconnected.clone();

        // Production drops this handle to detach the task; here we await it to
        // observe the contained panic.
        let handle = spawn_supervised_pipe(
            peer_id(),
            cancel.clone(),
            move || flag.store(true, Ordering::SeqCst),
            move || panic_flag.store(true, Ordering::SeqCst),
            async {
                panic!("peer pipe panics");
            },
        );

        // The pipe runs in a single task with no nested unwind-isolation spawn,
        // so the task itself surfaces the panic to its `JoinHandle`. The
        // `Drop`-based teardown still runs during the unwind, so cleanup is
        // guaranteed and the panic is contained to this one task.
        let join_error = handle
            .await
            .expect_err("a panicking pipe surfaces a join error");
        assert!(
            join_error.is_panic(),
            "the pipe panic is reported as a panic, not a cancellation"
        );

        assert!(
            torn_down.load(Ordering::SeqCst),
            "teardown runs even when the pipe panics"
        );
        assert!(
            panic_disconnected.load(Ordering::SeqCst),
            "panic-only disconnect hook runs when the pipe panics"
        );
        assert!(cancel.is_cancelled(), "the peer connection is cancelled");
    }

    #[tokio::test]
    async fn supervised_peer_task_runs_teardown_and_disconnect_on_panic() {
        // The surprising input: a peer-influenced helper task (e.g. discovery
        // source/admission, legacy gossip recv loop) panics *after* its per-peer
        // service state is registered, before its normal-path cleanup runs.
        let torn_down = Arc::new(AtomicBool::new(false));
        let teardown_flag = torn_down.clone();
        let disconnected = Arc::new(AtomicBool::new(false));
        let disconnect_flag = disconnected.clone();

        // Production drops this handle to detach the task; here we await it to
        // observe the contained panic.
        let handle = spawn_supervised_peer_task(
            peer_id(),
            move || teardown_flag.store(true, Ordering::SeqCst),
            move || disconnect_flag.store(true, Ordering::SeqCst),
            async {
                panic!("peer task panics after state registration");
            },
        );

        let join_error = handle
            .await
            .expect_err("a panicking peer task surfaces a join error");
        assert!(
            join_error.is_panic(),
            "the task panic is reported as a panic, not a cancellation"
        );
        // The safe expectation (SR-1): the panic still ran cleanup and the
        // peer-disconnect hook, so no stale service state survives behind a
        // half-live connection.
        assert!(
            torn_down.load(Ordering::SeqCst),
            "teardown runs even when the peer task panics"
        );
        assert!(
            disconnected.load(Ordering::SeqCst),
            "panic-only disconnect hook runs when the peer task panics"
        );
    }

    #[tokio::test]
    async fn supervised_peer_task_skips_disconnect_on_normal_exit() {
        let torn_down = Arc::new(AtomicBool::new(false));
        let teardown_flag = torn_down.clone();
        let disconnected = Arc::new(AtomicBool::new(false));
        let disconnect_flag = disconnected.clone();

        let handle = spawn_supervised_peer_task(
            peer_id(),
            move || teardown_flag.store(true, Ordering::SeqCst),
            move || disconnect_flag.store(true, Ordering::SeqCst),
            async {},
        );

        handle
            .await
            .expect("a normal peer task exit does not panic");
        assert!(
            torn_down.load(Ordering::SeqCst),
            "teardown runs on a normal exit"
        );
        // A clean exit (e.g. one service's stream ends) must NOT trip the
        // panic-only disconnect — that would tear down peers that rode on the
        // same connection.
        assert!(
            !disconnected.load(Ordering::SeqCst),
            "the panic-only disconnect hook must not fire on a normal exit"
        );
    }
}