Skip to main content

melin_server_runtime/replication/
mod.rs

1//! Replication — synchronous input-stream streaming from primary to replica.
2//!
3//! The primary's JournalStage encodes each `InputSlot` it just durably
4//! journaled into a wire-ready `InputBatch` frame in the replication ring
5//! (separately from the journal-codec bytes it writes to disk). The
6//! `ReplicationSender` thread forwards those frames as-is over TCP/DPDK
7//! — no decode + re-encode on the hot path. The replica decodes the
8//! frames straight back into `InputSlot`s, publishes them to its local
9//! input disruptor with the primary's pre-assigned sequences and
10//! timestamps, and the replica's JournalStage re-encodes them through
11//! its own writer for byte-exact-on-replay durability.
12//!
13//! ## Wire Protocol
14//!
15//! Length-prefixed frames, little-endian, over a dedicated TCP connection
16//! (or DPDK pipe). The full `InputBatch` payload layout lives in
17//! `melin_transport_core::replication::protocol` /
18//! `melin_transport_core::replication_wire`.
19//!
20//! ### Auth (before handshake)
21//! - **Challenge** (Primary → Replica): `[len:u32][0x03][nonce:[u8;32]]`
22//! - **ChallengeResponse** (Replica → Primary): `[len:u32][0x04][signature:[u8;64]][pubkey:[u8;32]]`
23//! - **AuthOk** (Primary → Replica): `[len:u32][0x05]`
24//! - **AuthFailed** (Primary → Replica): `[len:u32][0x06]`
25//!
26//! ### Replica → Primary
27//! - **Handshake**: `[len:u32][0x01][last_sequence:u64][chain_hash:[u8;32]]`
28//! - **Ack**: `[len:u32][0x02][acked_sequence:u64][in_memory_sequence:u64]`
29//!
30//! ### Primary → Replica
31//! - **StreamStart**: `[len:u32][0x10][start_sequence:u64][segment_start_sequence:u64][anchor_hash:[u8;32]]`
32//!   — the segment header identity a fresh replica creates its journal
33//!   with (lineage origin for full catch-up, the seeded segment's
34//!   identity after a snapshot transfer)
35//! - **NeedSnapshot**: `[len:u32][0x11]`
36//! - **HashMismatch**: `[len:u32][0x12]` — divergent replica journal;
37//!   the replica archives its lineage, then the snapshot flow follows
38//! - **SnapshotBegin**: `[len:u32][0x13][snapshot_len:u64][snap_sequence:u64][snap_chain_hash:[u8;32]]`
39//! - **SnapshotChunk**: `[len:u32][0x14][data...]`
40//! - **SnapshotEnd**: `[len:u32][0x15][crc32c:u32]`
41//! - **Rotate**: `[len:u32][0x16][boundary_seq:u64][tail_hash:[u8;32]]`
42//!   — primary-driven rotation, verified + adopted by the replica
43//! - **ChainCheck**: `[len:u32][0x17][sequence:u64][chain_hash:[u8;32]]`
44//!   — periodic live-stream chain validation
45//! - **SegmentSeedBegin**: `[len:u32][0x18][seed_len:u64]` — raw byte
46//!   prefix of the primary's segment containing the snapshot boundary;
47//!   body rides SnapshotChunk frames, ends with SnapshotEnd
48//! - **InputBatch**: `[len:u32][0x21][count:u16][slot...]` — see
49//!   `transport-core::replication_wire` for the per-slot layout
50//! - **Heartbeat**: `[len:u32][0x30][sequence:u64]`
51//!
52//! ## Limitations
53//!
54//! - Dual replication (up to 2 replicas in parallel)
55//!
56//! See `docs/replication.md` for the full design document and limitation details.
57
58use std::sync::Arc;
59#[cfg(test)]
60use std::sync::atomic::AtomicU64;
61use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
62
63use melin_journal::{BufferedWriter, JournalWrite};
64
65use melin_app::Application;
66use melin_transport_core::pipeline::{InputSlot, OutputSlot};
67use melin_transport_core::replication::archive::{ArchiveReason, archive_local_lineage};
68use melin_transport_core::replication::protocol::{MAX_CONTROL_FRAME, decode_primary_message};
69
70mod auth;
71#[cfg(feature = "dpdk")]
72mod dpdk;
73mod receiver_transport;
74mod tcp_receiver;
75mod tcp_sender;
76// Only the DPDK sender uses the worker, but it is pure thread/channel
77// plumbing with no libdpdk dependency — so it is also compiled under
78// `test`, keeping its mechanics (and the affinity trap it exists to
79// avoid) testable on a build without the `dpdk` feature.
80#[cfg(any(feature = "dpdk", test))]
81mod validation_worker;
82
83use receiver_transport::{ControlFrameSource, SessionExit, StreamingResult, receive_chunked_body};
84
85/// Writer-side view of the trading-halt gate (the `replicas_connected`
86/// counter). The matching stage refuses new orders while the count is zero, so
87/// the counter must reflect the number of replicas that have **authenticated**
88/// — a bare connection must not lift the halt. Both senders (kernel-TCP and
89/// DPDK) lift/lower the gate through this view so the policy, the memory
90/// orderings, and the "trading halted" warning live in one place and cannot
91/// drift apart.
92///
93/// Deliberately a *borrowed view*, not an owner: `melin_transport_core` owns
94/// the `Arc<AtomicU32>` and reads it on the matching hot path and for the
95/// `melin_replicas_connected` gauge. This type is only the senders' write
96/// surface, so centralizing it costs nothing on the read side.
97pub(crate) struct ReplicaGate<'a> {
98    count: &'a AtomicU32,
99}
100
101impl<'a> ReplicaGate<'a> {
102    pub(crate) fn new(count: &'a AtomicU32) -> Self {
103        Self { count }
104    }
105
106    /// A replica has authenticated — lift the halt by one. `Release` so a peer
107    /// that observes the connect also observes everything that preceded it.
108    pub(crate) fn lift(&self) {
109        self.count.fetch_add(1, Ordering::Release);
110    }
111
112    /// A replica left — lower the halt by one. Returns `true` if it was the
113    /// last one (trading is now unprotected), emitting the halt warning here so
114    /// both senders share the wording. `fetch_sub` returns the *prior* count,
115    /// so `== 1` means this call took it to zero; deriving "last one" from the
116    /// returned value rather than a follow-up load avoids a TOCTOU race with a
117    /// concurrent reconnect's `lift`.
118    pub(crate) fn lower(&self) -> bool {
119        let was_last = self.count.fetch_sub(1, Ordering::Release) == 1;
120        if was_last {
121            tracing::warn!("all replicas disconnected — trading halted");
122        }
123        was_last
124    }
125}
126
127// Wire-protocol types, auth, catch-up, ack queueing, dual-track
128// cursor management, and per-replica metrics now live in
129// `melin_transport_core::replication`. Re-export the public types
130// here so the module's public API surface (e.g.
131// `melin_server_runtime::replication::Ack` / `::ReplicationMetrics`) is
132// unchanged for downstream consumers and tests.
133pub use melin_transport_core::replication::ack_queue::{
134    PendingAck, PendingAckQueue, try_flush_dual_track, wait_for_journal_cursor,
135};
136pub use melin_transport_core::replication::protocol::{
137    Ack, Handshake, PrimaryMessage, ReplicaMessage,
138};
139pub use melin_transport_core::replication::{ReplicaCursors, ReplicationMetrics, SentHighWater};
140
141#[cfg(feature = "dpdk")]
142pub(crate) use dpdk::REPL_DISPATCH_BURST;
143#[cfg(feature = "dpdk")]
144pub use dpdk::{DpdkReplicationDriver, run_receiver_dpdk};
145pub use tcp_receiver::{ReceiverResult, run_receiver};
146pub use tcp_sender::{ReplicationListener, Sender, run_sender};
147
148/// The handles a replica's receive loop shares with the rest of the
149/// node — the admin endpoint and the control-plane raft driver.
150/// Bundled (rather than five positional parameters) because they
151/// always travel together through the kernel-TCP and DPDK receiver
152/// signatures, and a transposition between same-typed flags is exactly
153/// the bug a bundle prevents.
154#[derive(Clone)]
155pub struct ReplicaControlPlane {
156    /// Promotion request: polled by the receive loop; filed by the
157    /// admin `PROMOTE` command or the raft driver (auto-promotion).
158    pub promote: crate::promotion::PromotionRequest,
159    /// Flipped `true` once journal recovery has seeded the fence epoch,
160    /// so the raft driver can trust this node's advertised tip — see
161    /// `melin_raft::recency::TipSource`.
162    pub tip_ready: std::sync::Arc<AtomicBool>,
163    /// Sequence half of the advertised journal tip — see
164    /// [`melin_transport_core::AdvertisedJournalTip`].
165    pub journal_tip: melin_transport_core::AdvertisedJournalTip,
166    /// `true` while this replica holds an authenticated replication
167    /// connection to its primary. The raft driver refuses to
168    /// auto-promote while set: a live link means the primary is
169    /// demonstrably alive, and winning a control-plane election (e.g.
170    /// because a *different* node's raft died) must not depose a
171    /// healthy primary.
172    pub primary_link_up: std::sync::Arc<AtomicBool>,
173    /// The ack policy last advertised by the primary
174    /// (`AckPolicy::as_u8`; `ACK_POLICY_UNKNOWN` until first
175    /// contact), refreshed by `StreamStart` and every `Heartbeat`. The
176    /// raft driver's auto-promotion refusal reads this — the policy the
177    /// *primary* acked under decides whether an election win proves
178    /// this replica holds every acked order, not this node's own
179    /// (possibly pre-staged) configuration. Falls back to this node's
180    /// own policy while unknown, which is exactly the pre-propagation
181    /// behavior.
182    pub primary_ack_policy: std::sync::Arc<std::sync::atomic::AtomicU8>,
183    /// Whether the replica's pipeline is healthy — fed to the replica's
184    /// minimal health endpoint (`/health` OK/ERR and the
185    /// `melin_pipeline_healthy` gauge). Process-lifetime (the per-session
186    /// pipeline is torn down and rebuilt across resyncs, so its own
187    /// `journal_failed` latch cannot be handed to the endpoint):
188    /// `true` from boot, latched `false` by the journal stage's failure
189    /// wrapper, reset `true` when a fresh pipeline is built.
190    pub pipeline_healthy: std::sync::Arc<AtomicBool>,
191}
192
193impl ReplicaControlPlane {
194    /// Fresh handles for a replica boot: nothing requested, tip not
195    /// trustworthy yet, tip sequence 0, primary link down, pipeline
196    /// healthy (nothing has failed yet).
197    pub fn new() -> Self {
198        Self {
199            promote: crate::promotion::PromotionRequest::new(),
200            tip_ready: std::sync::Arc::new(AtomicBool::new(false)),
201            journal_tip: melin_transport_core::AdvertisedJournalTip::new(
202                melin_transport_core::WireSeq::new(0),
203            ),
204            primary_link_up: std::sync::Arc::new(AtomicBool::new(false)),
205            primary_ack_policy: std::sync::Arc::new(std::sync::atomic::AtomicU8::new(
206                crate::ack_policy::ACK_POLICY_UNKNOWN,
207            )),
208            pipeline_healthy: std::sync::Arc::new(AtomicBool::new(true)),
209        }
210    }
211}
212
213impl Default for ReplicaControlPlane {
214    fn default() -> Self {
215        Self::new()
216    }
217}
218
219/// Diagnostic: emit a `tcp_info` span at debug level describing the
220/// kernel's view of the socket (rtt, cwnd, retrans, unacked, rcv_space,
221/// rto). Guarded internally with `tracing::enabled!` so the
222/// `getsockopt(TCP_INFO)` syscall is skipped when debug logging is off —
223/// the call is free at runtime unless `RUST_LOG=debug` (or a more
224/// specific filter) is active. Used to distinguish user-space stalls
225/// from TCP-level congestion collapse when diagnosing replication
226/// slowdowns.
227pub(super) fn log_tcp_info(fd: std::os::unix::io::RawFd, tag: &str, slot: usize) {
228    if !tracing::enabled!(tracing::Level::DEBUG) {
229        return;
230    }
231    // SAFETY: all-zero pattern is a valid `tcp_info` (all numeric fields).
232    // The kernel fills in whatever the running version supports and
233    // returns the written length in `len`.
234    let mut info: libc::tcp_info = unsafe { std::mem::zeroed() };
235    let mut len = std::mem::size_of::<libc::tcp_info>() as libc::socklen_t;
236    let rc = unsafe {
237        libc::getsockopt(
238            fd,
239            libc::IPPROTO_TCP,
240            libc::TCP_INFO,
241            &mut info as *mut _ as *mut libc::c_void,
242            &mut len,
243        )
244    };
245    if rc != 0 {
246        tracing::debug!(
247            slot,
248            tag,
249            errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0),
250            "tcp_info getsockopt failed"
251        );
252        return;
253    }
254    tracing::debug!(
255        slot,
256        tag,
257        state = info.tcpi_state,
258        ca_state = info.tcpi_ca_state,
259        rtt_us = info.tcpi_rtt,
260        rttvar_us = info.tcpi_rttvar,
261        snd_cwnd = info.tcpi_snd_cwnd,
262        snd_ssthresh = info.tcpi_snd_ssthresh,
263        snd_mss = info.tcpi_snd_mss,
264        rcv_mss = info.tcpi_rcv_mss,
265        unacked = info.tcpi_unacked,
266        retrans = info.tcpi_retrans,
267        total_retrans = info.tcpi_total_retrans,
268        lost = info.tcpi_lost,
269        rcv_space = info.tcpi_rcv_space,
270        rto_us = info.tcpi_rto,
271        "tcp_info"
272    );
273}
274
275/// Sleep for the given duration in 100ms increments, checking the shutdown
276/// flag and the promotion request between increments. Returns early if
277/// either is set.
278pub(super) fn sleep_checking_flags(
279    duration: std::time::Duration,
280    shutdown: &AtomicBool,
281    promote: &crate::promotion::PromotionRequest,
282) {
283    let deadline = std::time::Instant::now() + duration;
284    while std::time::Instant::now() < deadline {
285        if shutdown.load(Ordering::Relaxed) || promote.is_requested() {
286            return;
287        }
288        std::thread::sleep(std::time::Duration::from_millis(100));
289    }
290}
291
292/// Wait out one retry delay: sleep for the current `backoff` (checking
293/// the shutdown and promotion flags — see [`sleep_checking_flags`]),
294/// then double it, capped at [`MAX_BACKOFF`], for the next attempt.
295/// Sleep-then-double, so the first retry waits the base delay rather
296/// than twice it. Every retry arm in both receivers goes through this —
297/// don't hand-roll the idiom, the copies drift.
298pub(super) fn sleep_then_double_backoff(
299    backoff: &mut std::time::Duration,
300    shutdown: &AtomicBool,
301    promote: &crate::promotion::PromotionRequest,
302) {
303    sleep_checking_flags(*backoff, shutdown, promote);
304    *backoff = (*backoff * 2).min(MAX_BACKOFF);
305}
306
307/// Outcome of shutting down a replica pipeline. All stage threads are
308/// joined in every variant — by the time the caller sees this, no
309/// pipeline thread can still touch the journal or snapshot files.
310pub(super) enum TeardownOutcome<A, W> {
311    /// Clean exit: both stages returned their state, reusable for the
312    /// next pipeline build (post-snapshot) or promotion.
313    Clean(A, W),
314    /// The journal stage exited with an error — its writer is gone and
315    /// the matching stage's state is unusable (it may have applied
316    /// events the journal never persisted). Carries the error so the
317    /// orchestrator can distinguish repairable chain divergence
318    /// (in-process resync) from journal I/O death (exit).
319    JournalFailed(melin_journal::JournalError),
320    /// A stage thread panicked; no state survives.
321    Panicked,
322}
323
324/// Shut down the replica pipeline and extract Exchange + journal writer
325/// from the stage threads.
326///
327/// Relies on the caller having published a `JournalEvent::Shutdown`
328/// sentinel to the input ring before invoking this — the journal and
329/// matching stages exit when they consume the sentinel via the normal
330/// event-processing path. We deliberately don't flip `shutdown_flag`
331/// here: doing so could cause a stage to take its emergency-abort
332/// branch *before* consuming the sentinel, hitting the
333/// drain-vs-cursor race that the sentinel design exists to avoid.
334///
335/// `drain_handle` and `shadow_handle` still observe `shutdown_flag`
336/// because they don't process the input ring (no sentinel reaches
337/// them); set the flag for them only.
338pub(super) fn shutdown_pipeline<A: Send + 'static, W: Send + 'static>(
339    shutdown_flag: &AtomicBool,
340    journal_handle: std::thread::JoinHandle<Result<W, melin_journal::JournalError>>,
341    matching_handle: std::thread::JoinHandle<A>,
342    drain_handle: std::thread::JoinHandle<()>,
343    shadow_handle: Option<std::thread::JoinHandle<()>>,
344) -> TeardownOutcome<A, W> {
345    // Defense-in-depth: set the flag before joining. The sentinel was
346    // already published by `teardown_replica_pipeline` before this call,
347    // so no further events can arrive in the input ring — setting the
348    // flag here cannot race with new publishes. The flag is the fallback
349    // exit signal for paths that don't observe the sentinel: `run_sync`
350    // in `no-persist` builds, the drain consumer, the shadow stage, and
351    // any case where the receiver thread panicked before publishing the
352    // sentinel.
353    //
354    // The flag is also what makes these joins safe when the journal
355    // stage is already dead: the matching stage's gate on the (frozen)
356    // journal cursor is a non-blocking spin that re-checks the flag
357    // every iteration, and its shutdown drain is `try_consume`-bounded.
358    shutdown_flag.store(true, Ordering::Release);
359    // Join EVERY thread before reporting the outcome, even on failure:
360    // the in-process divergence-resync path archives the journal and
361    // snapshot right after this returns, and a still-running shadow
362    // thread could be mid-snapshot-write during those renames.
363    let journal_result = journal_handle.join();
364    let matching_result = matching_handle.join();
365    let _ = drain_handle.join();
366    if let Some(h) = shadow_handle {
367        let _ = h.join();
368    }
369    let writer = match journal_result {
370        Ok(Ok(w)) => w,
371        // Already error!-logged by the journal thread's spawn wrapper.
372        Ok(Err(e)) => return TeardownOutcome::JournalFailed(e),
373        Err(_) => return TeardownOutcome::Panicked,
374    };
375    match matching_result {
376        Ok(exchange) => TeardownOutcome::Clean(exchange, writer),
377        Err(_) => TeardownOutcome::Panicked,
378    }
379}
380
381/// Live replica pipeline — built once on first connect (or after a snapshot
382/// transfer), persists across `Disconnected` reconnects so the orchestrator
383/// doesn't pay the journal-recover + thread-spawn cost on every drop.
384///
385/// Shared between the kernel-TCP and DPDK receivers; both build pipelines
386/// with the same shape (journal / matching / drain / optional shadow), and
387/// both refresh handshake state from `last_seq` + `chain_hash_lock` at
388/// reconnect time.
389pub(super) struct ReplicaPipelineHandles<A: Application, W: Send + 'static> {
390    pub(super) input_producer: melin_pipeline::ring::Producer<InputSlot<A::Event>>,
391    pub(super) journal_cursor: Arc<melin_pipeline::padding::Sequence>,
392    /// Highest wire seq durably persisted, published by JournalStage after
393    /// each fsync. Read by the orchestrator to fill in the reconnect
394    /// handshake without owning the writer. Typed so the handshake's resume
395    /// point can never be sourced from a ring-space counter (the adjacent
396    /// `journal_cursor` resets to ~0 every process start).
397    pub(super) last_seq: melin_transport_core::DurableWireSeqCursor,
398    /// Seqlock-published fsync state (chain hash + journal seq + ring
399    /// cursor), read half. Option to mirror the primary-side pattern;
400    /// always Some on replicas now.
401    pub(super) chain_hash_lock:
402        Option<melin_pipeline::seqlock::SeqLockReader<melin_transport_core::pipeline::FsyncState>>,
403    /// Primary-announced rotation hand-off: the receiver thread pushes
404    /// `Rotate` boundaries here (in stream order), the journal stage
405    /// pops and rotates at exactly those sequences. Replicas have no
406    /// local rotation triggers.
407    pub(super) stream_marks: melin_transport_core::pipeline::StreamMarkQueue,
408    /// Latched by the journal thread's spawn wrapper when the stage
409    /// exits with an error (chain divergence, journal I/O failure).
410    /// The streaming receiver checks it and tears the session down —
411    /// without this, a dead journal stage freezes the journal cursor
412    /// and the receiver wedges forever on ring backpressure or the
413    /// ack-durability wait.
414    pub(super) journal_failed: Arc<AtomicBool>,
415    /// Per-pipeline shutdown flag — flipped only on a controlled teardown
416    /// (Promote/Shutdown/Fatal/Snapshot). NOT flipped on `Disconnected`.
417    pub(super) pipeline_shutdown: Arc<AtomicBool>,
418    pub(super) journal_handle: std::thread::JoinHandle<Result<W, melin_journal::JournalError>>,
419    pub(super) matching_handle: std::thread::JoinHandle<A>,
420    pub(super) drain_handle: std::thread::JoinHandle<()>,
421    pub(super) shadow_handle: Option<std::thread::JoinHandle<()>>,
422}
423
424/// [`ReplicaPipelineHandles`] over the journal's writer — the only
425/// instantiation that exists. The struct keeps its writer parameter
426/// because the teardown helpers are writer-agnostic plumbing; this
427/// alias spares every signature from spelling the concrete type out.
428pub(super) type ReplicaHandles<A> =
429    ReplicaPipelineHandles<A, BufferedWriter<<A as Application>::Event>>;
430
431/// Build the replica pipeline and spawn its stage threads on the configured
432/// cores. Returns the bundle of state the orchestrator keeps across
433/// `Disconnected` reconnects.
434#[allow(clippy::too_many_arguments)]
435pub(super) fn build_replica_pipeline_with_threads<A>(
436    exchange: A,
437    writer: BufferedWriter<A::Event>,
438    cores: crate::server::PipelineCores,
439    snapshot_interval_ms: u64,
440    snapshot_path: std::path::PathBuf,
441    group_commit_delay: std::time::Duration,
442    busy_spin: bool,
443    fence_state: Arc<melin_transport_core::fence::FenceState>,
444    // Process-lifetime health mirror for the replica health endpoint —
445    // see `ReplicaControlPlane::pipeline_healthy`. Reset `true` here (a
446    // fresh pipeline is healthy) and latched `false` by the journal
447    // thread's failure wrapper, alongside the per-pipeline
448    // `journal_failed` latch.
449    pipeline_healthy: Arc<AtomicBool>,
450) -> Result<ReplicaHandles<A>, Box<dyn std::error::Error>>
451where
452    A: Application + Send + 'static,
453    A::Event: Send + Sync + 'static,
454    A::Report: Send + 'static,
455    A::QueryResponse: Send + 'static,
456{
457    let shadow_exchange = <A as Application>::clone_via_snapshot(&exchange)?;
458
459    let enable_shadow = snapshot_interval_ms > 0;
460    // Shadow snapshot seeds its epoch from the fence state's current value
461    // (set from the replica's recovered journal before this builder runs).
462    let shadow_initial_epoch = fence_state.epoch();
463    let pipeline = melin_transport_core::pipeline::build_replica_pipeline(
464        exchange,
465        writer,
466        4096, // max_journal_batch
467        group_commit_delay,
468        busy_spin,
469        enable_shadow,
470        fence_state,
471    );
472
473    let pipeline_shutdown = Arc::new(AtomicBool::new(false));
474
475    let ps = Arc::clone(&pipeline_shutdown);
476    let journal_core = cores.journal;
477    let mut journal_stage = pipeline.journal_stage;
478    // Replicas never rotate on local triggers (size or operator
479    // command) — they adopt the boundaries the primary announces over
480    // the stream, which keeps segment boundaries (and with them chain
481    // values and journal bytes) identical across nodes.
482    let stream_marks: melin_transport_core::pipeline::StreamMarkQueue =
483        Arc::new(std::sync::Mutex::new(std::collections::VecDeque::new()));
484    journal_stage.set_stream_marks(Arc::clone(&stream_marks));
485    journal_stage.set_preparer_core(cores.journal_prep);
486    journal_stage.set_disk_core(cores.journal_disk);
487    let journal_failed = Arc::new(AtomicBool::new(false));
488    let journal_failed_latch = Arc::clone(&journal_failed);
489    // A fresh pipeline is healthy — this also clears the latch after a
490    // successful in-process resync rebuild.
491    pipeline_healthy.store(true, Ordering::Release);
492    let journal_handle = std::thread::Builder::new()
493        .name("journal".into())
494        .spawn(move || {
495            melin_app::affinity::pin_thread("journal", journal_core);
496            let result = journal_stage.run(&ps);
497            if let Err(ref e) = result {
498                // Latch before logging so the receiver reacts even if
499                // logging stalls. A dead journal stage freezes the
500                // journal cursor; every downstream wait on it (ring
501                // backpressure, ack durability) would spin forever —
502                // the streaming loop polls this latch and tears the
503                // session down instead.
504                journal_failed_latch.store(true, Ordering::Release);
505                // Mirror into the process-lifetime gauge the replica
506                // health endpoint serves.
507                pipeline_healthy.store(false, Ordering::Release);
508                tracing::error!(error = %e, "replica journal stage failed — session teardown");
509            }
510            result
511        })
512        .expect("spawn journal thread");
513
514    let ps = Arc::clone(&pipeline_shutdown);
515    let matching_core = cores.matching;
516    let matching_stage = pipeline.matching_stage;
517    let matching_handle = std::thread::Builder::new()
518        .name("matching".into())
519        .spawn(move || {
520            melin_app::affinity::pin_thread("matching", matching_core);
521            matching_stage.run(&ps)
522        })
523        .expect("spawn matching thread");
524
525    // Drain thread uses the response core — replicas have no response stage,
526    // but the consumer needs to be drained so the output ring doesn't fill.
527    let ps = Arc::clone(&pipeline_shutdown);
528    let drain_core = cores.response;
529    let drain_consumer = pipeline.drain_consumer;
530    let drain_handle = std::thread::Builder::new()
531        .name("drain".into())
532        .spawn(move || {
533            melin_app::affinity::pin_thread("drain", drain_core);
534            let mut consumer = drain_consumer;
535            let mut batch = vec![OutputSlot::<A::Report, A::QueryResponse>::default(); 256];
536            loop {
537                if ps.load(Ordering::Relaxed) {
538                    return;
539                }
540                let count = consumer.consume_batch(&mut batch, 256);
541                if count == 0 {
542                    if busy_spin {
543                        std::hint::spin_loop();
544                    } else {
545                        std::thread::yield_now();
546                    }
547                }
548            }
549        })
550        .expect("spawn drain thread");
551
552    let shadow_handle = if let Some(shadow_cons) = pipeline.shadow_consumer {
553        let snap_path = snapshot_path;
554        let chain_lock = pipeline
555            .chain_hash_lock
556            .as_ref()
557            .expect("chain hash lock with shadow")
558            .clone();
559        let ps = Arc::clone(&pipeline_shutdown);
560        let shadow_core = cores.shadow;
561        Some(
562            std::thread::Builder::new()
563                .name("replica-shadow".into())
564                .spawn(move || {
565                    melin_app::affinity::pin_thread("replica-shadow", shadow_core);
566                    melin_transport_core::shadow::run(
567                        shadow_cons,
568                        shadow_exchange,
569                        snap_path,
570                        std::time::Duration::from_millis(snapshot_interval_ms),
571                        chain_lock,
572                        &ps,
573                        false,
574                        shadow_initial_epoch,
575                    );
576                })
577                .expect("spawn shadow thread"),
578        )
579    } else {
580        None
581    };
582
583    Ok(ReplicaPipelineHandles {
584        input_producer: pipeline.input_producer,
585        journal_cursor: pipeline.cursors.journal_ring_arc(),
586        last_seq: pipeline.cursors.durable_wire_seq(),
587        chain_hash_lock: pipeline.chain_hash_lock,
588        stream_marks,
589        journal_failed,
590        pipeline_shutdown,
591        journal_handle,
592        matching_handle,
593        drain_handle,
594        shadow_handle,
595    })
596}
597
598/// Tear down the pipeline: publish the shutdown sentinel, join all
599/// threads, return the recovered (App, journal writer) so the orchestrator
600/// can use them for the next pipeline build (e.g., post-snapshot) or
601/// pass them up on promotion.
602pub(super) fn teardown_replica_pipeline<A: Application + Send + 'static, W: Send + 'static>(
603    mut handles: ReplicaPipelineHandles<A, W>,
604) -> TeardownOutcome<A, W> {
605    // The sentinel is published here, not by callers, so no teardown
606    // path can forget it — the journal and matching stages exit by
607    // consuming it through the normal event path, draining anything
608    // received-but-not-yet-journaled (see `shutdown_pipeline` for why
609    // the flag alone is only the emergency fallback). Bounded retry
610    // rather than the blocking `publish`: if the journal stage dies
611    // with the ring full, its gate cursor freezes and a blocking
612    // publish would spin forever — re-check the failure latch between
613    // attempts and fall through to the flag-only teardown once it
614    // trips (the sentinel has no reader then anyway: the matching
615    // stage is gated behind the frozen journal cursor).
616    while !handles.journal_failed.load(Ordering::Acquire) {
617        match handles
618            .input_producer
619            .try_publish(InputSlot::<A::Event>::shutdown_sentinel())
620        {
621            Ok(_) => break,
622            // Ring full — consumers need to make progress first.
623            Err(_) => std::thread::yield_now(),
624        }
625    }
626    shutdown_pipeline::<A, W>(
627        &handles.pipeline_shutdown,
628        handles.journal_handle,
629        handles.matching_handle,
630        handles.drain_handle,
631        handles.shadow_handle,
632    )
633}
634
635/// How many mid-stream divergence resyncs the receiver attempts
636/// in-process (per process lifetime) before giving up. Mid-stream
637/// divergence is never expected in a healthy cluster — it means
638/// corruption or a serious bug somewhere — so the budget is exactly
639/// one: the first occurrence repairs automatically (archive the local
640/// lineage as `.divergent.<n>`, re-seed from the primary) and pages
641/// the operator via `melin_replica_divergence_total`; a second in the
642/// same process lifetime is systematic, and continuing to repair
643/// would fill the disk with archives while masking the underlying
644/// fault. Exit hard instead.
645pub(super) const MAX_INPROCESS_DIVERGENCE_RESYNCS: u32 = 1;
646
647/// Recover replica boot state from disk.
648///
649/// Recovers whenever any journal segment survives — live OR archived
650/// (a crash between rotation's rename and the new live file's creation
651/// leaves archives with no live segment, and recovery handles that
652/// layout; treating it as a fresh replica would discard local durable
653/// history and then fail `create_new` against the surviving lineage).
654/// Returns `(None, None, 0, zeros)` for a genuinely fresh replica.
655/// Also seeds the fencing epoch from the recovered journal.
656///
657/// Called at receiver startup, and again after a mid-stream chain
658/// divergence tears the pipeline down: the on-disk journal is
659/// self-consistent (merely forked from the primary's history), so
660/// recovery re-derives a truthful handshake pair `(last_sequence,
661/// chain_hash)` and the next connection takes the primary's
662/// HashMismatch → archive → reseed path in-process.
663#[allow(clippy::type_complexity)]
664pub(super) fn recover_replica_state<A, W>(
665    journal_path: &std::path::Path,
666    snapshot_path: &std::path::Path,
667    factory: &dyn melin_app::app_factory::AppFactory<App = A>,
668    fence_state: &melin_transport_core::fence::FenceState,
669) -> Result<(Option<A>, Option<W>, u64, [u8; 32]), Box<dyn std::error::Error>>
670where
671    A: Application,
672    W: melin_journal::JournalWrite<A::Event>,
673{
674    let lineage_exists =
675        journal_path.exists() || !melin_journal::segment::list_archives(journal_path)?.is_empty();
676    if !lineage_exists {
677        return Ok((None, None, 0u64, [0u8; 32]));
678    }
679    let engine = if snapshot_path.exists() {
680        tracing::info!("recovering replica from snapshot + journal");
681        melin_transport_core::JournaledApp::<A, W>::recover_from_snapshot(
682            snapshot_path,
683            journal_path,
684        )?
685    } else {
686        melin_transport_core::JournaledApp::<A, W>::recover(factory.empty(), journal_path)?
687    };
688    let next = engine.next_sequence();
689    let last = next.saturating_sub(1);
690    let hash = engine.chain_hash().unwrap_or([0u8; 32]);
691    // Seed the observed epoch from the replica's own recovered journal.
692    // Streaming `EpochBump`s and the snapshot-resync path raise it later.
693    fence_state.observe_epoch(engine.recovered_epoch());
694    let (mut exchange, writer) = engine.into_parts();
695    factory.apply_operator_policy(&mut exchange);
696    Ok((Some(exchange), Some(writer), last, hash))
697}
698
699/// Reconnect backoff cap shared by both receivers — exponential from
700/// 1 s, clamped here so a long outage settles to one attempt every 30 s
701/// without hammering a flapping primary. Reset to 1 s only when a
702/// session heard the primary speak (data or heartbeat) — see
703/// [`handle_session_exit`].
704pub(super) const MAX_BACKOFF: std::time::Duration = std::time::Duration::from_secs(30);
705
706/// What the receiver's reconnect loop does after a streaming session
707/// ends — the transport-independent half of the exit dispatch, returned
708/// by [`handle_session_exit`].
709pub(in crate::replication) enum AfterSession<A, W> {
710    /// Terminal: `run_receiver*` returns this verbatim — clean shutdown
711    /// (`Ok(None)`), a promotion hand-off (`Ok(Some(state))`), or a
712    /// fatal error.
713    Return(ReceiverResult<A, W>),
714    /// A mid-stream divergence was repaired in-process: the recovered
715    /// on-disk state is the new handshake position. The caller adopts it
716    /// and reconnects; the primary's `HashMismatch` verdict then routes
717    /// the replica through archive + re-seed.
718    Resync {
719        exchange: Option<A>,
720        journal_writer: Option<W>,
721        last_sequence: u64,
722        chain_hash: [u8; 32],
723    },
724    /// A plain disconnect or a stream contiguity break — backoff has
725    /// already been applied (and the flags checked); the caller
726    /// reconnects, reusing the still-live pipeline.
727    Reconnect,
728}
729
730/// Dispatch a finished streaming session — shared by the kernel-TCP and
731/// DPDK receivers.
732///
733/// Folds the behaviours that were previously copied between the two
734/// receiver loops (and had drifted — the copies reset the post-resync
735/// writer differently, a latent corruption bug): the
736/// `Shutdown`/`Promote`/`Fatal` teardown (including the once-per-process
737/// in-process divergence-resync policy) and the `Disconnected` reconnect
738/// backoff. The shutdown-sentinel publish lives in
739/// [`teardown_replica_pipeline`].
740///
741/// `close` runs any transport-specific teardown that must precede a
742/// reconnect — the DPDK receiver closes its smoltcp socket so the
743/// primary's slot and the local socket-set entry are freed; the
744/// kernel-TCP receiver passes a no-op (its `TcpStream` is dropped by the
745/// caller on the next loop turn). It is invoked only on the
746/// reconnecting paths (in-process resync, plain disconnect, stream
747/// gap), never on a terminal return.
748// Twelve arguments is a lot, but each is a distinct piece of the
749// receiver loop's state; bundling them would only move the noise.
750#[allow(clippy::too_many_arguments)]
751pub(in crate::replication) fn handle_session_exit<A, W>(
752    result: StreamingResult,
753    pipeline: &mut Option<ReplicaPipelineHandles<A, W>>,
754    divergence_resyncs: &mut u32,
755    backoff: &mut std::time::Duration,
756    last_sequence: u64,
757    journal_path: &std::path::Path,
758    snapshot_path: &std::path::Path,
759    factory: &dyn melin_app::app_factory::AppFactory<App = A>,
760    fence_state: &melin_transport_core::fence::FenceState,
761    shutdown: &AtomicBool,
762    promote: &crate::promotion::PromotionRequest,
763    mut close: impl FnMut(),
764) -> AfterSession<A, W>
765where
766    A: Application + Send + 'static,
767    W: JournalWrite<A::Event> + Send + 'static,
768{
769    let StreamingResult {
770        exit,
771        heard_from_primary,
772    } = result;
773
774    match exit {
775        SessionExit::Shutdown => {
776            if let Some(p) = pipeline.take() {
777                let _ = teardown_replica_pipeline::<A, W>(p);
778            }
779            AfterSession::Return(Ok(None))
780        }
781
782        SessionExit::Promote => AfterSession::Return(match pipeline.take() {
783            Some(p) => match teardown_replica_pipeline::<A, W>(p) {
784                TeardownOutcome::Clean(ex, wr) => Ok(Some((ex, wr))),
785                _ => Err("pipeline failed during promotion".into()),
786            },
787            None => Err("pipeline missing on promote".into()),
788        }),
789
790        SessionExit::Fatal(e) => {
791            let outcome = match pipeline.take() {
792                Some(p) => teardown_replica_pipeline::<A, W>(p),
793                // Fatal implies a streaming session, which implies a
794                // pipeline — but don't turn a missing one into a resync.
795                None => return AfterSession::Return(Err(e)),
796            };
797            // Mid-stream chain divergence is repairable in-process: the
798            // on-disk journal is self-consistent (merely forked from the
799            // primary's history), so re-derive the handshake state from
800            // disk and reconnect — the primary judges the recovered
801            // position divergent and the next session takes the
802            // HashMismatch → archive → reseed path, no restart needed.
803            // Every other fatal exits as before: protocol violations and
804            // journal I/O death (ENOSPC, RO-FS) would fail the same way
805            // after a resync.
806            let TeardownOutcome::JournalFailed(
807                je @ melin_journal::JournalError::ReplicaChainDivergence { .. },
808            ) = outcome
809            else {
810                return AfterSession::Return(Err(e));
811            };
812
813            *divergence_resyncs += 1;
814            let attempt = *divergence_resyncs;
815            if attempt > MAX_INPROCESS_DIVERGENCE_RESYNCS {
816                return AfterSession::Return(Err(format!(
817                    "mid-stream chain divergence recurred {attempt} times — giving up on \
818                     in-process resync (each cycle archives the local journal and re-seeds \
819                     from the primary; recurrence at this rate means the primary keeps \
820                     streaming history that forks from what it announces): {je}"
821                )
822                .into()));
823            }
824            tracing::warn!(
825                error = %je,
826                attempt,
827                max_attempts = MAX_INPROCESS_DIVERGENCE_RESYNCS,
828                "mid-stream chain divergence — re-deriving local state for in-process resync"
829            );
830            // Transport-specific teardown before reconnecting.
831            close();
832            match recover_replica_state::<A, W>(journal_path, snapshot_path, factory, fence_state) {
833                Ok((exchange, journal_writer, seq, hash)) => AfterSession::Resync {
834                    exchange,
835                    journal_writer,
836                    last_sequence: seq,
837                    chain_hash: hash,
838                },
839                Err(e) => AfterSession::Return(Err(e)),
840            }
841        }
842
843        SessionExit::StreamGap(e) => {
844            // The primary's stream skipped past what we hold; the
845            // contiguous prefix is committed and the journal stage keeps
846            // flushing it, so re-handshaking at the durable position
847            // (re-read from `FsyncState` at the top of the reconnect
848            // loop, after the backoff) lets the primary re-stream the
849            // hole. The primary evidently spoke, so the backoff resets
850            // exactly as a heard-from disconnect does. Not a resync:
851            // nothing on disk is wrong.
852            close();
853            *backoff = std::time::Duration::from_secs(1);
854            tracing::warn!(
855                error = %e,
856                last_sequence,
857                "replication stream broke contiguity — reconnecting from the durable position"
858            );
859            sleep_then_double_backoff(backoff, shutdown, promote);
860            AfterSession::Reconnect
861        }
862
863        SessionExit::Disconnected => {
864            // Transport-specific teardown before reconnecting (smoltcp
865            // socket reclaim on DPDK; no-op on kernel TCP).
866            close();
867            // A session in which the primary spoke — data or heartbeat
868            // (heartbeats flow even on a quiet system) — proves it
869            // alive and serving: treat the drop as transient and reset
870            // the backoff. A session with no word from the primary
871            // keeps escalating: that covers instant-drop flaps and
872            // synthetic results from sessions that never started
873            // streaming (a persistent local transport failure must not
874            // redial at 1 Hz forever).
875            if heard_from_primary {
876                *backoff = std::time::Duration::from_secs(1);
877            }
878            tracing::warn!(
879                last_sequence,
880                heard_from_primary,
881                backoff_secs = backoff.as_secs(),
882                "reconnecting to primary"
883            );
884            sleep_then_double_backoff(backoff, shutdown, promote);
885            AfterSession::Reconnect
886        }
887    }
888}
889
890/// Tear the live pipeline down for a promotion that fired while the
891/// receiver was disconnected — at the top of the reconnect loop or
892/// during reconnect backoff — and return the warm Exchange + writer for
893/// the promoted primary. Shared by both receivers.
894///
895/// A clean teardown hands back the warm state; if there is no pipeline
896/// (promotion before the first connect, or after a resync that left the
897/// state in the receiver's locals) those locals carry it. A missing
898/// pair is a hard error — a promote with nothing to promote.
899pub(in crate::replication) fn take_pipeline_for_promotion<A, W>(
900    pipeline: &mut Option<ReplicaPipelineHandles<A, W>>,
901    exchange: &mut Option<A>,
902    journal_writer: &mut Option<W>,
903) -> ReceiverResult<A, W>
904where
905    A: Application + Send + 'static,
906    W: JournalWrite<A::Event> + Send + 'static,
907{
908    if let Some(p) = pipeline.take()
909        && let TeardownOutcome::Clean(e, w) = teardown_replica_pipeline::<A, W>(p)
910    {
911        *exchange = Some(e);
912        *journal_writer = Some(w);
913    }
914    match (exchange.take(), journal_writer.take()) {
915        (Some(e), Some(w)) => Ok(Some((e, w))),
916        _ => Err("promotion requested but no local state available".into()),
917    }
918}
919
920/// The four facts a successful snapshot + segment-seed transfer yields:
921/// `(snapshot App, snapshot sequence, snapshot chain hash, seed length)`.
922type ResyncTransfer<A> = (A, u64, [u8; 32], u64);
923
924/// What [`handle_resync_verdict`] resolved a `NeedSnapshot` /
925/// `HashMismatch` verdict to.
926pub(in crate::replication) enum ResyncDecision {
927    /// Resync complete — resume streaming from `resume_sequence` on the
928    /// re-seeded lineage `(segment_start_sequence, anchor_hash)`. The
929    /// recovered App + writer are left in the receiver's `exchange` /
930    /// `journal_writer` locals.
931    Ready {
932        segment_start_sequence: u64,
933        anchor_hash: [u8; 32],
934        resume_sequence: u64,
935    },
936    /// The transfer failed network-shaped (drop / restart mid-body); the
937    /// caller backs off and reconnects. The pre-resync lineage is already
938    /// archived and half-applied transfer state has been cleaned up.
939    Retry,
940}
941
942/// Receive the snapshot + segment-seed transfer that follows a resync
943/// verdict, installing both (snapshot → `snapshot_path`, seed →
944/// `journal_path`). Returns the snapshot App, its sequence + chain hash,
945/// and the seed length (the journal's `valid_end`). Shared by both
946/// receivers via [`ControlFrameSource`].
947///
948/// Errors here are network-shaped — the caller retries (see
949/// [`ResyncDecision::Retry`]); the post-transfer install in
950/// [`handle_resync_verdict`] is what's fatal.
951fn receive_resync_transfer<A, S>(
952    source: &mut S,
953    snapshot_path: &std::path::Path,
954    journal_path: &std::path::Path,
955    fence_state: &melin_transport_core::fence::FenceState,
956) -> Result<ResyncTransfer<A>, Box<dyn std::error::Error + Send + Sync>>
957where
958    A: Application,
959    S: ControlFrameSource,
960{
961    let (snap_len, snap_sequence, snap_chain_hash) =
962        match decode_primary_message(&source.next_frame(MAX_CONTROL_FRAME)?)? {
963            PrimaryMessage::SnapshotBegin {
964                snapshot_len,
965                snap_sequence,
966                snap_chain_hash,
967            } => (snapshot_len, snap_sequence, snap_chain_hash),
968            other => return Err(format!("expected SnapshotBegin, got {other:?}").into()),
969        };
970
971    tracing::info!(snap_sequence, snap_len, "receiving snapshot");
972    let tmp_path = snapshot_path.with_extension("snapshot.tmp");
973    receive_chunked_body(source, &tmp_path, snap_len, "snapshot")?;
974    std::fs::rename(&tmp_path, snapshot_path)?;
975    tracing::info!(snap_sequence, snap_len, "snapshot received and verified");
976
977    let (snap_exchange, _snap_seq, snap_hash, snap_epoch) =
978        melin_transport_core::snapshot::load::<A>(snapshot_path)?;
979    if snap_hash != snap_chain_hash {
980        return Err(format!(
981            "snapshot chain hash mismatch: primary sent {snap_chain_hash:02x?}, \
982             loaded snapshot has {snap_hash:02x?}"
983        )
984        .into());
985    }
986    // Adopt the primary's snapshot epoch — the resync rebases this replica
987    // onto the primary's lineage, including its epoch.
988    fence_state.observe_epoch(snap_epoch);
989
990    // Segment seed: the raw byte prefix of the primary's segment
991    // containing `snap_sequence`. Written verbatim as our live segment, it
992    // makes our segmentation a byte-copy of the primary's from birth —
993    // chain values comparable and `Rotate` verification valid immediately.
994    let seed_len = match decode_primary_message(&source.next_frame(MAX_CONTROL_FRAME)?)? {
995        PrimaryMessage::SegmentSeedBegin { seed_len } => seed_len,
996        other => {
997            return Err(format!("expected SegmentSeedBegin after snapshot, got {other:?}").into());
998        }
999    };
1000    let seed_tmp = journal_path.with_extension("seed.tmp");
1001    receive_chunked_body(source, &seed_tmp, seed_len, "segment seed")?;
1002    // Structural check before installing: the CRC only proves transport
1003    // integrity, not that the primary sent a well-formed prefix ending at
1004    // the snapshot sequence. (With hash-chain on, the chain cross-check in
1005    // `handle_resync_verdict` subsumes this; without it, this is the only
1006    // guard.)
1007    if let Err(e) =
1008        melin_journal::segment::verify_segment_prefix(&seed_tmp, snap_sequence, seed_len)
1009    {
1010        let _ = std::fs::remove_file(&seed_tmp);
1011        return Err(format!("segment seed failed structural verification: {e}").into());
1012    }
1013    std::fs::rename(&seed_tmp, journal_path)?;
1014    melin_journal::segment::fsync_parent_dir(journal_path)?;
1015    Ok((snap_exchange, snap_sequence, snap_chain_hash, seed_len))
1016}
1017
1018/// Handle a `NeedSnapshot` / `HashMismatch` resync verdict — shared by
1019/// both receivers. Tears the pipeline down, archives the local lineage
1020/// (never deleted), resets the handshake position, receives + installs
1021/// the snapshot and segment seed via `source`, opens the re-seeded
1022/// segment and ties its chain to the snapshot, then validates the
1023/// post-snapshot `StreamStart` inline before resuming.
1024///
1025/// On success the recovered App + writer are left in `exchange` /
1026/// `journal_writer` and [`ResyncDecision::Ready`] carries the resume
1027/// lineage. A network-shaped transfer failure yields
1028/// [`ResyncDecision::Retry`] (the caller backs off and reconnects). An
1029/// inconsistent primary or a local install failure is fatal (`Err`).
1030#[allow(clippy::too_many_arguments)]
1031pub(in crate::replication) fn handle_resync_verdict<A, W, S>(
1032    divergent: bool,
1033    source: &mut S,
1034    pipeline: &mut Option<ReplicaPipelineHandles<A, W>>,
1035    exchange: &mut Option<A>,
1036    journal_writer: &mut Option<W>,
1037    journal_path: &std::path::Path,
1038    snapshot_path: &std::path::Path,
1039    fence_state: &melin_transport_core::fence::FenceState,
1040    control: &ReplicaControlPlane,
1041    last_sequence: &mut u64,
1042    chain_hash: &mut [u8; 32],
1043) -> Result<ResyncDecision, Box<dyn std::error::Error + Send + Sync>>
1044where
1045    A: Application + Send + 'static,
1046    W: JournalWrite<A::Event> + Send + 'static,
1047    S: ControlFrameSource,
1048{
1049    // HashMismatch is NeedSnapshot plus the verdict that our local journal
1050    // holds divergent history (forked from the primary's — e.g. an
1051    // ex-primary rejoining after failover with an acked-but-unreplicated
1052    // suffix).
1053    if divergent {
1054        tracing::warn!(
1055            last_sequence = *last_sequence,
1056            "primary reports chain divergence — archiving local journal, resyncing from snapshot"
1057        );
1058    } else {
1059        tracing::info!("primary requires snapshot transfer — receiving snapshot");
1060    }
1061
1062    if let Some(p) = pipeline.take() {
1063        let _ = teardown_replica_pipeline::<A, W>(p);
1064    }
1065
1066    // Invalidate the in-memory App + writer before moving their backing
1067    // files aside. On the in-process divergence repair path these still
1068    // hold the recovered handles; a transfer failure returns `Retry`, and
1069    // without this reset the stale writer — now pointing at an
1070    // archived-away journal — would survive the fresh-replica create gate
1071    // and get rebuilt into the next pipeline.
1072    *exchange = None;
1073    *journal_writer = None;
1074
1075    // Move the local lineage aside — never delete. Divergent journals are
1076    // audit-trail material; stale ones may be the last copy of pruned
1077    // history.
1078    let reason = if divergent {
1079        ArchiveReason::Divergent
1080    } else {
1081        ArchiveReason::Resync
1082    };
1083    archive_local_lineage(journal_path, snapshot_path, reason)?;
1084    // Archived — a retried handshake must present as a fresh replica.
1085    *last_sequence = 0;
1086    *chain_hash = [0u8; 32];
1087    // The advertised control-plane tip must shrink with the holdings:
1088    // the archived suffix is either divergent (never acked) or being
1089    // replaced wholesale, so this is the one legitimate tip regression
1090    // (see `AdvertisedJournalTip::reset`).
1091    control
1092        .journal_tip
1093        .reset(melin_transport_core::WireSeq::new(0));
1094
1095    let (snap_exchange, snap_sequence, snap_chain_hash, seed_len) =
1096        match receive_resync_transfer::<A, S>(source, snapshot_path, journal_path, fence_state) {
1097            Ok(v) => v,
1098            Err(e) => {
1099                tracing::warn!(error = %e, "snapshot transfer failed — retrying");
1100                // Half-applied resync state, not audit material — drop it
1101                // so the retry starts clean (the pre-resync lineage is
1102                // already archived; this is not it).
1103                let _ = std::fs::remove_file(snapshot_path);
1104                return Ok(ResyncDecision::Retry);
1105            }
1106        };
1107    *exchange = Some(snap_exchange);
1108
1109    // Open the seeded segment for appending at the snapshot position —
1110    // recovery's resume path: the chain rebuilds from the seeded bytes and
1111    // its value at `snap_sequence` must equal the (verified) snapshot's
1112    // chain hash. `chain_hash()` is `None` only with `hash-chain` disabled
1113    // (nothing to tie); an all-zeros snapshot hash means the primary runs
1114    // without `hash-chain` (also nothing to tie).
1115    let writer = W::open_append(journal_path, snap_sequence, seed_len)?;
1116    let seeded_chain = writer.chain_hash().unwrap_or(snap_chain_hash);
1117    if snap_chain_hash != [0u8; 32] && seeded_chain != snap_chain_hash {
1118        return Err(format!(
1119            "segment seed chain at {snap_sequence} disagrees with the transferred snapshot's \
1120             hash — inconsistent primary"
1121        )
1122        .into());
1123    }
1124    let seeded_info = melin_journal::segment::read_header_info(journal_path)?;
1125    *journal_writer = Some(writer);
1126
1127    // Validate the post-snapshot StreamStart inline before resuming — its
1128    // lineage must agree with the seed the primary just transferred.
1129    match decode_primary_message(&source.next_frame(MAX_CONTROL_FRAME)?)? {
1130        PrimaryMessage::StreamStart {
1131            start_sequence,
1132            segment_start_sequence,
1133            anchor_hash,
1134            epoch,
1135            ack_policy,
1136        } => {
1137            if segment_start_sequence != seeded_info.starting_sequence
1138                || anchor_hash != seeded_info.anchor_hash
1139            {
1140                return Err(format!(
1141                    "post-snapshot StreamStart lineage (start {segment_start_sequence}) \
1142                     disagrees with the transferred segment seed (start {}) — inconsistent \
1143                     primary",
1144                    seeded_info.starting_sequence
1145                )
1146                .into());
1147            }
1148            // We just rebased onto this primary's snapshot, so adopt its
1149            // epoch wholesale (no stale-primary refusal — our prior state
1150            // was discarded).
1151            fence_state.observe_epoch(epoch);
1152            control
1153                .primary_ack_policy
1154                .store(ack_policy, Ordering::Release);
1155            tracing::info!(
1156                start_sequence,
1157                epoch,
1158                ack_policy,
1159                "streaming resumed after snapshot transfer"
1160            );
1161            // The transfer is installed and validated: the node's holdings
1162            // are exactly the snapshot position again.
1163            control
1164                .journal_tip
1165                .reset(melin_transport_core::WireSeq::new(snap_sequence));
1166            Ok(ResyncDecision::Ready {
1167                segment_start_sequence,
1168                anchor_hash,
1169                resume_sequence: snap_sequence,
1170            })
1171        }
1172        other => Err(format!("expected StreamStart after snapshot, got {other:?}").into()),
1173    }
1174}
1175
1176#[cfg(test)]
1177mod tests {
1178    use std::io::Write;
1179    use std::sync::Arc;
1180
1181    use super::auth::{authenticate_replica, authenticate_with_primary};
1182    use super::*;
1183    // Any `AppEvent` works as the pipeline's event type here — these
1184    // protocol-level tests never construct a real app event (the slot's
1185    // event is always `JournalEvent::Tick`), so the counter example's
1186    // event type stands in for an exchange event and keeps the runtime's
1187    // test deps free of exchange crates.
1188    use counter_server::CounterEvent;
1189    type InputSlot = melin_transport_core::pipeline::InputSlot<CounterEvent>;
1190    use melin_transport_core::replication::protocol::{
1191        MAX_CONTROL_FRAME, MAX_DATA_FRAME, MSG_AUTH_OK, MSG_CHALLENGE_RESPONSE, MSG_SNAPSHOT_BEGIN,
1192        MSG_SNAPSHOT_CHUNK, MSG_SNAPSHOT_END, decode_auth_result, decode_challenge,
1193        decode_challenge_response, decode_primary_message, decode_replica_message, encode_ack,
1194        encode_auth_failed, encode_auth_ok, encode_chain_check, encode_challenge,
1195        encode_challenge_response, encode_handshake, encode_hash_mismatch, encode_heartbeat,
1196        encode_input_batch, encode_need_snapshot, encode_rotate, encode_segment_seed_begin,
1197        encode_snapshot_begin, encode_snapshot_chunk, encode_snapshot_end, encode_stream_start,
1198        read_frame, try_decode_input_batch,
1199    };
1200
1201    /// Build a wire-ready `InputBatch` frame containing a single `Tick`
1202    /// slot at the given sequence — the protocol-level tests don't need
1203    /// real journal payloads, just something with a known max sequence.
1204    fn encode_input_batch_with_seq(end_sequence: u64, buf: &mut Vec<u8>) {
1205        let slot = InputSlot {
1206            connection_id: 0,
1207            key_hash: 0,
1208            request_seq: 0,
1209            sequence: end_sequence,
1210            timestamp_ns: 0,
1211            event: melin_journal::JournalEvent::Tick { now_ns: 0 },
1212            publish_ts: Default::default(),
1213            recv_ts: Default::default(),
1214        };
1215        encode_input_batch(&[slot], buf);
1216    }
1217
1218    #[test]
1219    fn handshake_encode_decode_round_trip() {
1220        let handshake = Handshake {
1221            last_sequence: 42,
1222            chain_hash: [0xAB; 32],
1223            // Non-zero so a dropped/zeroed epoch field is caught.
1224            epoch: 9,
1225        };
1226        let mut buf = Vec::new();
1227        encode_handshake(&handshake, &mut buf);
1228
1229        // Read frame: skip 4-byte length prefix.
1230        let payload = &buf[4..];
1231        let msg = decode_replica_message(payload).unwrap();
1232        match msg {
1233            ReplicaMessage::Handshake(h) => {
1234                assert_eq!(h.last_sequence, 42);
1235                assert_eq!(h.chain_hash, [0xAB; 32]);
1236                assert_eq!(h.epoch, 9);
1237            }
1238            _ => panic!("expected Handshake"),
1239        }
1240    }
1241
1242    #[test]
1243    fn ack_encode_decode_round_trip() {
1244        let ack = Ack {
1245            acked_sequence: 1000,
1246            in_memory_sequence: 1024,
1247        };
1248        let mut buf = Vec::new();
1249        encode_ack(&ack, &mut buf);
1250
1251        let payload = &buf[4..];
1252        let msg = decode_replica_message(payload).unwrap();
1253        match msg {
1254            ReplicaMessage::Ack(a) => {
1255                assert_eq!(a.acked_sequence, 1000);
1256                assert_eq!(a.in_memory_sequence, 1024);
1257            }
1258            _ => panic!("expected Ack"),
1259        }
1260    }
1261
1262    /// Pin the exact on-the-wire byte layout of an Ack frame. A future
1263    /// `repr(C)` field reorder, alignment change, or accidental
1264    /// big-endian wrapper substitution would silently break replica/
1265    /// primary compatibility — the const `size_of` assert in
1266    /// `protocol.rs` only catches size changes, not layout changes.
1267    /// This test pins the expected bytes so any such break shows up
1268    /// loudly on the next CI run.
1269    #[test]
1270    fn ack_wire_byte_pattern() {
1271        let ack = Ack {
1272            acked_sequence: 0xDEAD_BEEF_CAFE_F00D,
1273            in_memory_sequence: 0x1122_3344_5566_7788,
1274        };
1275        let mut buf = Vec::new();
1276        encode_ack(&ack, &mut buf);
1277        // [length:u32 LE = 17][tag:u8 = MSG_ACK = 0x02]
1278        // [acked_sequence:u64 LE][in_memory_sequence:u64 LE]
1279        let expected: &[u8] = &[
1280            0x11, 0x00, 0x00, 0x00, // length = 17 LE
1281            0x02, // MSG_ACK
1282            0x0D, 0xF0, 0xFE, 0xCA, 0xEF, 0xBE, 0xAD, 0xDE, // acked_sequence LE
1283            0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, // in_memory_sequence LE
1284        ];
1285        assert_eq!(buf.as_slice(), expected, "Ack wire layout drifted");
1286    }
1287
1288    #[test]
1289    fn stream_start_encode_decode_round_trip() {
1290        let mut buf = Vec::new();
1291        // Non-zero epoch/policy so a dropped/zeroed field is caught.
1292        encode_stream_start(99, 42, [0xAA; 32], 5, 2, &mut buf);
1293
1294        let payload = &buf[4..];
1295        let msg = decode_primary_message(payload).unwrap();
1296        match msg {
1297            PrimaryMessage::StreamStart {
1298                start_sequence,
1299                segment_start_sequence,
1300                anchor_hash,
1301                epoch,
1302                ack_policy,
1303            } => {
1304                assert_eq!(start_sequence, 99);
1305                assert_eq!(segment_start_sequence, 42);
1306                assert_eq!(anchor_hash, [0xAA; 32]);
1307                assert_eq!(ack_policy, 2);
1308                assert_eq!(epoch, 5);
1309            }
1310            _ => panic!("expected StreamStart"),
1311        }
1312    }
1313
1314    #[test]
1315    fn heartbeat_encode_decode_round_trip() {
1316        let mut buf = Vec::new();
1317        encode_heartbeat(123, 2, &mut buf);
1318
1319        let payload = &buf[4..];
1320        let msg = decode_primary_message(payload).unwrap();
1321        match msg {
1322            PrimaryMessage::Heartbeat {
1323                sequence,
1324                ack_policy,
1325            } => {
1326                assert_eq!(sequence, 123);
1327                assert_eq!(ack_policy, 2);
1328            }
1329            _ => panic!("expected Heartbeat"),
1330        }
1331    }
1332
1333    #[test]
1334    fn need_snapshot_encode_decode_round_trip() {
1335        let mut buf = Vec::new();
1336        encode_need_snapshot(&mut buf);
1337
1338        let payload = &buf[4..];
1339        let msg = decode_primary_message(payload).unwrap();
1340        assert!(matches!(msg, PrimaryMessage::NeedSnapshot));
1341    }
1342
1343    #[test]
1344    fn hash_mismatch_encode_decode_round_trip() {
1345        let mut buf = Vec::new();
1346        encode_hash_mismatch(&mut buf);
1347
1348        let payload = &buf[4..];
1349        let msg = decode_primary_message(payload).unwrap();
1350        assert!(matches!(msg, PrimaryMessage::HashMismatch));
1351    }
1352
1353    #[test]
1354    fn rotate_encode_decode_round_trip() {
1355        let mut buf = Vec::new();
1356        encode_rotate(7_000_000, &[0xBB; 32], &mut buf);
1357
1358        let payload = &buf[4..];
1359        let msg = decode_primary_message(payload).unwrap();
1360        match msg {
1361            PrimaryMessage::Rotate {
1362                boundary_seq,
1363                tail_hash,
1364            } => {
1365                assert_eq!(boundary_seq, 7_000_000);
1366                assert_eq!(tail_hash, [0xBB; 32]);
1367            }
1368            _ => panic!("expected Rotate"),
1369        }
1370    }
1371
1372    #[test]
1373    fn chain_check_encode_decode_round_trip() {
1374        let mut buf = Vec::new();
1375        encode_chain_check(555, &[0xCC; 32], &mut buf);
1376
1377        let payload = &buf[4..];
1378        let msg = decode_primary_message(payload).unwrap();
1379        match msg {
1380            PrimaryMessage::ChainCheck {
1381                sequence,
1382                chain_hash,
1383            } => {
1384                assert_eq!(sequence, 555);
1385                assert_eq!(chain_hash, [0xCC; 32]);
1386            }
1387            _ => panic!("expected ChainCheck"),
1388        }
1389    }
1390
1391    /// Pin the wire layout of the shared `(sequence, hash)` frame body —
1392    /// same rationale as `ack_wire_byte_pattern`. Rotate and ChainCheck
1393    /// share the layout; only the tag differs.
1394    #[test]
1395    fn rotate_wire_byte_pattern() {
1396        let mut hash = [0u8; 32];
1397        for (i, b) in hash.iter_mut().enumerate() {
1398            *b = i as u8;
1399        }
1400        let mut buf = Vec::new();
1401        encode_rotate(0xDEAD_BEEF_CAFE_F00D, &hash, &mut buf);
1402        // [length:u32 LE = 41][tag:u8 = MSG_ROTATE = 0x16]
1403        // [boundary_seq:u64 LE][tail_hash:32 bytes verbatim]
1404        let mut expected = vec![
1405            0x29, 0x00, 0x00, 0x00, // length = 41 LE
1406            0x16, // MSG_ROTATE
1407            0x0D, 0xF0, 0xFE, 0xCA, 0xEF, 0xBE, 0xAD, 0xDE, // boundary_seq LE
1408        ];
1409        expected.extend_from_slice(&hash);
1410        assert_eq!(
1411            buf.as_slice(),
1412            expected.as_slice(),
1413            "Rotate wire layout drifted"
1414        );
1415    }
1416
1417    #[test]
1418    fn segment_seed_begin_encode_decode_round_trip() {
1419        let mut buf = Vec::new();
1420        encode_segment_seed_begin(123_456, &mut buf);
1421
1422        let payload = &buf[4..];
1423        let msg = decode_primary_message(payload).unwrap();
1424        match msg {
1425            PrimaryMessage::SegmentSeedBegin { seed_len } => {
1426                assert_eq!(seed_len, 123_456);
1427            }
1428            _ => panic!("expected SegmentSeedBegin"),
1429        }
1430    }
1431
1432    #[test]
1433    fn unknown_replica_message_type_is_error() {
1434        let payload = [0xFF, 0, 0, 0, 0, 0, 0, 0, 0];
1435        let result = decode_replica_message(&payload);
1436        assert!(result.is_err());
1437    }
1438
1439    #[test]
1440    fn unknown_primary_message_type_is_error() {
1441        let payload = [0xFF, 0, 0, 0, 0, 0, 0, 0, 0];
1442        let result = decode_primary_message(&payload);
1443        assert!(result.is_err());
1444    }
1445
1446    #[test]
1447    fn read_frame_enforces_max_size() {
1448        // Create a buffer with a length prefix claiming 1000 bytes.
1449        let mut buf = Vec::new();
1450        buf.extend_from_slice(&1000u32.to_le_bytes());
1451        buf.extend_from_slice(&[0u8; 100]); // not enough data, but max_size check comes first
1452
1453        let mut cursor = std::io::Cursor::new(buf);
1454        let result = read_frame(&mut cursor, 64);
1455        assert!(result.is_err());
1456    }
1457
1458    #[test]
1459    fn challenge_encode_decode_round_trip() {
1460        let nonce = [0x42; 32];
1461        let mut buf = Vec::new();
1462        encode_challenge(&nonce, &mut buf);
1463
1464        let payload = &buf[4..];
1465        let decoded = decode_challenge(payload).unwrap();
1466        assert_eq!(decoded, nonce);
1467    }
1468
1469    #[test]
1470    fn challenge_response_encode_decode_round_trip() {
1471        let sig = [0xAA; 64];
1472        let pubkey = [0xBB; 32];
1473        let mut buf = Vec::new();
1474        encode_challenge_response(&sig, &pubkey, &mut buf);
1475
1476        let payload = &buf[4..];
1477        let (decoded_sig, decoded_pubkey) = decode_challenge_response(payload).unwrap();
1478        assert_eq!(decoded_sig, sig);
1479        assert_eq!(decoded_pubkey, pubkey);
1480    }
1481
1482    #[test]
1483    fn auth_ok_encode_decode_round_trip() {
1484        let mut buf = Vec::new();
1485        encode_auth_ok(&mut buf);
1486
1487        let payload = &buf[4..];
1488        assert!(decode_auth_result(payload).unwrap());
1489    }
1490
1491    #[test]
1492    fn auth_failed_encode_decode_round_trip() {
1493        let mut buf = Vec::new();
1494        encode_auth_failed(&mut buf);
1495
1496        let payload = &buf[4..];
1497        assert!(!decode_auth_result(payload).unwrap());
1498    }
1499
1500    #[test]
1501    fn decode_challenge_rejects_wrong_tag() {
1502        let mut payload = [0u8; 33];
1503        payload[0] = MSG_AUTH_OK;
1504        assert!(decode_challenge(&payload).is_err());
1505    }
1506
1507    #[test]
1508    fn decode_challenge_response_rejects_short_payload() {
1509        let payload = [MSG_CHALLENGE_RESPONSE; 10]; // too short
1510        assert!(decode_challenge_response(&payload).is_err());
1511    }
1512
1513    #[test]
1514    fn auth_round_trip_valid_key() {
1515        use ed25519_dalek::SigningKey;
1516        use std::os::unix::net::UnixStream;
1517
1518        let repl_key = SigningKey::from_bytes(&[0xFC; 32]);
1519        let pub_b64 = base64::Engine::encode(
1520            &base64::engine::general_purpose::STANDARD,
1521            repl_key.verifying_key().to_bytes(),
1522        );
1523        let keys_content = format!("replication {pub_b64} test-replica\n");
1524        let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
1525
1526        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1527        primary_stream
1528            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1529            .unwrap();
1530        replica_stream
1531            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1532            .unwrap();
1533
1534        let repl_key_clone = SigningKey::from_bytes(&[0xFC; 32]);
1535        let replica_handle = std::thread::spawn(move || {
1536            let mut conn = replica_stream;
1537            authenticate_with_primary(&mut conn, &repl_key_clone)
1538        });
1539
1540        let mut conn = primary_stream;
1541        authenticate_replica(&mut conn, &authorized_keys).unwrap();
1542
1543        replica_handle.join().unwrap().unwrap();
1544    }
1545
1546    #[test]
1547    fn auth_rejects_unknown_key() {
1548        use ed25519_dalek::SigningKey;
1549        use std::os::unix::net::UnixStream;
1550
1551        // authorized_keys has one key, but the replica uses a different one.
1552        let authorized_key = SigningKey::from_bytes(&[0xAA; 32]);
1553        let rogue_key = SigningKey::from_bytes(&[0xBB; 32]);
1554        let pub_b64 = base64::Engine::encode(
1555            &base64::engine::general_purpose::STANDARD,
1556            authorized_key.verifying_key().to_bytes(),
1557        );
1558        let keys_content = format!("replication {pub_b64} authorized-replica\n");
1559        let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
1560
1561        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1562        primary_stream
1563            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1564            .unwrap();
1565        replica_stream
1566            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1567            .unwrap();
1568
1569        let replica_handle = std::thread::spawn(move || {
1570            let mut conn = replica_stream;
1571            authenticate_with_primary(&mut conn, &rogue_key)
1572        });
1573
1574        let mut conn = primary_stream;
1575        let result = authenticate_replica(&mut conn, &authorized_keys);
1576        assert!(result.is_err());
1577        assert!(result.unwrap_err().to_string().contains("unknown"));
1578
1579        // Replica should also get a rejection.
1580        let replica_result = replica_handle.join().unwrap();
1581        assert!(replica_result.is_err());
1582    }
1583
1584    #[test]
1585    fn auth_rejects_wrong_permission() {
1586        use ed25519_dalek::SigningKey;
1587        use std::os::unix::net::UnixStream;
1588
1589        // Key exists but has Trader permission, not Replication.
1590        let key = SigningKey::from_bytes(&[0xCC; 32]);
1591        let pub_b64 = base64::Engine::encode(
1592            &base64::engine::general_purpose::STANDARD,
1593            key.verifying_key().to_bytes(),
1594        );
1595        let keys_content = format!("trader {pub_b64} wrong-role\n");
1596        let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
1597
1598        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1599        primary_stream
1600            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1601            .unwrap();
1602        replica_stream
1603            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1604            .unwrap();
1605
1606        let replica_handle = std::thread::spawn(move || {
1607            let mut conn = replica_stream;
1608            authenticate_with_primary(&mut conn, &key)
1609        });
1610
1611        let mut conn = primary_stream;
1612        let result = authenticate_replica(&mut conn, &authorized_keys);
1613        assert!(result.is_err());
1614        assert!(result.unwrap_err().to_string().contains("Replication"));
1615
1616        let replica_result = replica_handle.join().unwrap();
1617        assert!(replica_result.is_err());
1618    }
1619
1620    /// A replica that sends a validly-formatted but tampered signature
1621    /// (correct public key, wrong signature bytes) is rejected.
1622    #[test]
1623    fn auth_rejects_invalid_signature() {
1624        use ed25519_dalek::SigningKey;
1625        use std::os::unix::net::UnixStream;
1626
1627        // Register the correct key.
1628        let correct_key = SigningKey::from_bytes(&[0xDD; 32]);
1629        let pub_b64 = base64::Engine::encode(
1630            &base64::engine::general_purpose::STANDARD,
1631            correct_key.verifying_key().to_bytes(),
1632        );
1633        let keys_content = format!("replication {pub_b64} test-replica\n");
1634        let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
1635
1636        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1637        primary_stream
1638            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1639            .unwrap();
1640        replica_stream
1641            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1642            .unwrap();
1643
1644        // Replica side: read challenge, but sign with a DIFFERENT key,
1645        // then send the response with the correct public key (spoofing).
1646        let replica_handle = std::thread::spawn(move || {
1647            use melin_transport_core::replication::protocol::*;
1648
1649            let mut reader = replica_stream.try_clone().unwrap();
1650            let mut writer = replica_stream;
1651
1652            // Read the challenge.
1653            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1654            let nonce = decode_challenge(&frame).unwrap();
1655
1656            // Sign with a WRONG key but send the CORRECT public key.
1657            let wrong_key = SigningKey::from_bytes(&[0xEE; 32]);
1658            let bad_signature = ed25519_dalek::Signer::sign(&wrong_key, &nonce);
1659            let correct_pubkey = correct_key.verifying_key();
1660
1661            let mut buf = Vec::with_capacity(128);
1662            encode_challenge_response(
1663                &bad_signature.to_bytes(),
1664                correct_pubkey.as_bytes(),
1665                &mut buf,
1666            );
1667            writer.write_all(&buf).unwrap();
1668            writer.flush().unwrap();
1669
1670            // Should receive AuthFailed.
1671            let result_frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1672            let ok = decode_auth_result(&result_frame).unwrap();
1673            assert!(!ok, "should receive auth failure");
1674        });
1675
1676        let mut conn = primary_stream;
1677        let result = authenticate_replica(&mut conn, &authorized_keys);
1678        assert!(result.is_err());
1679        assert!(
1680            result
1681                .unwrap_err()
1682                .to_string()
1683                .contains("signature verification failed"),
1684            "should fail on signature verification"
1685        );
1686
1687        replica_handle.join().unwrap();
1688    }
1689
1690    #[test]
1691    fn sender_receiver_end_to_end() {
1692        use std::os::unix::net::UnixStream;
1693
1694        // Create a mock connection.
1695        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1696
1697        // Spawn a thread simulating the replica side.
1698        let replica_handle = std::thread::spawn(move || {
1699            let mut reader = replica_stream.try_clone().unwrap();
1700            let mut writer = replica_stream;
1701
1702            // Send handshake.
1703            let mut buf = Vec::new();
1704            let handshake = Handshake {
1705                last_sequence: 0,
1706                chain_hash: [0u8; 32],
1707                epoch: 0,
1708            };
1709            encode_handshake(&handshake, &mut buf);
1710            writer.write_all(&buf).unwrap();
1711            writer.flush().unwrap();
1712            buf.clear();
1713
1714            // Read StreamStart.
1715            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1716            let msg = decode_primary_message(&frame).unwrap();
1717            assert!(matches!(msg, PrimaryMessage::StreamStart { .. }));
1718
1719            // Read InputBatch.
1720            let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
1721            let slots: Vec<InputSlot> = try_decode_input_batch(&frame).expect("decode InputBatch");
1722            let end_seq = slots
1723                .last()
1724                .map(|s| s.sequence)
1725                .expect("InputBatch carried at least one slot");
1726
1727            // Send ack.
1728            let ack = Ack {
1729                acked_sequence: end_seq,
1730                in_memory_sequence: end_seq,
1731            };
1732            encode_ack(&ack, &mut buf);
1733            writer.write_all(&buf).unwrap();
1734            writer.flush().unwrap();
1735
1736            end_seq
1737        });
1738
1739        // Primary side: simulate handle_replica_connection partially.
1740        let mut p_reader = primary_stream.try_clone().unwrap();
1741        let mut p_writer = primary_stream;
1742
1743        // Read handshake.
1744        let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1745        let handshake = match decode_replica_message(&frame).unwrap() {
1746            ReplicaMessage::Handshake(h) => h,
1747            _ => panic!("expected Handshake"),
1748        };
1749        assert_eq!(handshake.last_sequence, 0);
1750
1751        // Send StreamStart.
1752        let mut buf = Vec::new();
1753        encode_stream_start(0, 1, [0u8; 32], 0, 1, &mut buf); // fake lineage for test
1754        p_writer.write_all(&buf).unwrap();
1755        p_writer.flush().unwrap();
1756        buf.clear();
1757
1758        // Send an InputBatch with a single Tick slot at seq 42.
1759        encode_input_batch_with_seq(42, &mut buf);
1760        p_writer.write_all(&buf).unwrap();
1761        p_writer.flush().unwrap();
1762        buf.clear();
1763
1764        // Read ack.
1765        let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1766        let ack = match decode_replica_message(&frame).unwrap() {
1767            ReplicaMessage::Ack(a) => a,
1768            _ => panic!("expected Ack"),
1769        };
1770        assert_eq!(ack.acked_sequence, 42);
1771
1772        // Join replica thread.
1773        let end_seq = replica_handle.join().unwrap();
1774        assert_eq!(end_seq, 42);
1775    }
1776
1777    #[test]
1778    fn multiple_data_batches_acked_in_order() {
1779        // Send multiple InputBatch frames, verify replica acks each one
1780        // and the cursor advances correctly.
1781        use std::os::unix::net::UnixStream;
1782
1783        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1784
1785        let replica_handle = std::thread::spawn(move || {
1786            let mut reader = replica_stream.try_clone().unwrap();
1787            let mut writer = replica_stream;
1788            let mut buf = Vec::new();
1789
1790            // Send handshake.
1791            encode_handshake(
1792                &Handshake {
1793                    last_sequence: 0,
1794                    chain_hash: [0u8; 32],
1795                    epoch: 0,
1796                },
1797                &mut buf,
1798            );
1799            writer.write_all(&buf).unwrap();
1800            writer.flush().unwrap();
1801            buf.clear();
1802
1803            // Read StreamStart.
1804            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1805            assert!(matches!(
1806                decode_primary_message(&frame).unwrap(),
1807                PrimaryMessage::StreamStart { .. }
1808            ));
1809
1810            // Read and ack 3 InputBatches.
1811            let mut acked_seqs = Vec::new();
1812            for _ in 0..3 {
1813                let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
1814                let slots: Vec<InputSlot> =
1815                    try_decode_input_batch(&frame).expect("decode InputBatch");
1816                let end_seq = slots
1817                    .last()
1818                    .map(|s| s.sequence)
1819                    .expect("InputBatch carried at least one slot");
1820                acked_seqs.push(end_seq);
1821
1822                encode_ack(
1823                    &Ack {
1824                        acked_sequence: end_seq,
1825                        in_memory_sequence: end_seq,
1826                    },
1827                    &mut buf,
1828                );
1829                writer.write_all(&buf).unwrap();
1830                writer.flush().unwrap();
1831                buf.clear();
1832            }
1833
1834            acked_seqs
1835        });
1836
1837        // Primary side.
1838        let mut p_reader = primary_stream.try_clone().unwrap();
1839        let mut p_writer = primary_stream;
1840        let mut buf = Vec::new();
1841
1842        // Read handshake.
1843        let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1844        assert!(matches!(
1845            decode_replica_message(&frame).unwrap(),
1846            ReplicaMessage::Handshake(_)
1847        ));
1848
1849        // Send StreamStart.
1850        encode_stream_start(0, 1, [0u8; 32], 0, 1, &mut buf);
1851        p_writer.write_all(&buf).unwrap();
1852        p_writer.flush().unwrap();
1853        buf.clear();
1854
1855        // Send 3 InputBatches with increasing sequence numbers.
1856        for seq in [10u64, 20, 30] {
1857            encode_input_batch_with_seq(seq, &mut buf);
1858            p_writer.write_all(&buf).unwrap();
1859            p_writer.flush().unwrap();
1860            buf.clear();
1861        }
1862
1863        // Read 3 acks.
1864        for expected_seq in [10u64, 20, 30] {
1865            let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1866            let ack = match decode_replica_message(&frame).unwrap() {
1867                ReplicaMessage::Ack(a) => a,
1868                other => panic!("expected Ack, got {other:?}"),
1869            };
1870            assert_eq!(ack.acked_sequence, expected_seq);
1871        }
1872
1873        let acked = replica_handle.join().unwrap();
1874        assert_eq!(acked, vec![10, 20, 30]);
1875    }
1876
1877    #[test]
1878    fn heartbeat_encode_contains_sequence() {
1879        // Heartbeat messages carry the last known sequence so the replica
1880        // can verify it hasn't missed any data.
1881        let mut buf = Vec::new();
1882        encode_heartbeat(999, 1, &mut buf);
1883
1884        let payload = &buf[4..];
1885        match decode_primary_message(payload).unwrap() {
1886            PrimaryMessage::Heartbeat { sequence, .. } => {
1887                assert_eq!(sequence, 999);
1888            }
1889            other => panic!("expected Heartbeat, got {other:?}"),
1890        }
1891    }
1892
1893    #[test]
1894    fn replica_mid_stream_handshake_with_nonzero_sequence() {
1895        // A replica that already has some data sends a non-zero last_sequence
1896        // in its handshake. The primary should respond with StreamStart
1897        // containing that sequence, and the replica should only receive
1898        // events after that point.
1899        use std::os::unix::net::UnixStream;
1900
1901        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1902
1903        let replica_handle = std::thread::spawn(move || {
1904            let mut reader = replica_stream.try_clone().unwrap();
1905            let mut writer = replica_stream;
1906            let mut buf = Vec::new();
1907
1908            // Replica already has events up to sequence 100.
1909            encode_handshake(
1910                &Handshake {
1911                    last_sequence: 100,
1912                    chain_hash: [0xBB; 32],
1913                    epoch: 0,
1914                },
1915                &mut buf,
1916            );
1917            writer.write_all(&buf).unwrap();
1918            writer.flush().unwrap();
1919            buf.clear();
1920
1921            // Read StreamStart — should echo back our last_sequence.
1922            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1923            match decode_primary_message(&frame).unwrap() {
1924                PrimaryMessage::StreamStart { start_sequence, .. } => {
1925                    assert_eq!(
1926                        start_sequence, 100,
1927                        "StreamStart should echo replica's last_sequence"
1928                    );
1929                }
1930                other => panic!("expected StreamStart, got {other:?}"),
1931            }
1932
1933            // Read an InputBatch — should be for events AFTER 100.
1934            let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
1935            let slots: Vec<InputSlot> = try_decode_input_batch(&frame).expect("decode InputBatch");
1936            let end_sequence = slots
1937                .last()
1938                .map(|s| s.sequence)
1939                .expect("InputBatch carried at least one slot");
1940            assert!(
1941                end_sequence > 100,
1942                "InputBatch should be after replica's last_sequence"
1943            );
1944        });
1945
1946        // Primary side.
1947        let mut p_reader = primary_stream.try_clone().unwrap();
1948        let mut p_writer = primary_stream;
1949        let mut buf = Vec::new();
1950
1951        // Read handshake.
1952        let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1953        let handshake = match decode_replica_message(&frame).unwrap() {
1954            ReplicaMessage::Handshake(h) => h,
1955            _ => panic!("expected Handshake"),
1956        };
1957        assert_eq!(handshake.last_sequence, 100);
1958        assert_eq!(handshake.chain_hash, [0xBB; 32]);
1959
1960        // Send StreamStart echoing the replica's sequence.
1961        encode_stream_start(handshake.last_sequence, 1, [0u8; 32], 0, 1, &mut buf);
1962        p_writer.write_all(&buf).unwrap();
1963        p_writer.flush().unwrap();
1964        buf.clear();
1965
1966        // Send InputBatch with sequence 150 (after replica's 100).
1967        encode_input_batch_with_seq(150, &mut buf);
1968        p_writer.write_all(&buf).unwrap();
1969        p_writer.flush().unwrap();
1970
1971        replica_handle.join().unwrap();
1972    }
1973
1974    /// The receiver's stale-primary refusal: a replica that has observed
1975    /// epoch 5 must refuse a `StreamStart` advertising a *lower* epoch
1976    /// rather than follow that (divergent) lineage on top of its newer
1977    /// state. This reproduces the exact decision `run_receiver` makes on
1978    /// the normal-resume `StreamStart` — decode the frame off the wire,
1979    /// then `fence_state.refuses_primary(epoch)` — over a real socket so
1980    /// the epoch wire field and the policy are exercised together.
1981    ///
1982    /// In a current-build cluster this branch is a *second* line of
1983    /// defense: a primary reads the replica's higher-epoch handshake and
1984    /// self-demotes before it ever sends a `StreamStart` (see
1985    /// `tcp_sender::handle_replica_connection`). The receiver check guards
1986    /// the case where the primary does *not* fence — a non-fencing or
1987    /// older build — which is exactly what the mock primary below is.
1988    #[test]
1989    fn receiver_refuses_stream_start_from_stale_primary() {
1990        use std::os::unix::net::UnixStream;
1991
1992        use melin_transport_core::fence::FenceState;
1993
1994        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1995
1996        // Mock primary that — unlike a current build — does *not* fence on
1997        // the handshake and streams its stale lineage anyway. It advertises
1998        // epoch 3 on the StreamStart.
1999        const STALE_EPOCH: u64 = 3;
2000        let primary_handle = std::thread::spawn(move || {
2001            let mut reader = primary_stream.try_clone().unwrap();
2002            let mut writer = primary_stream;
2003
2004            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2005            let handshake = match decode_replica_message(&frame).unwrap() {
2006                ReplicaMessage::Handshake(h) => h,
2007                _ => panic!("expected Handshake"),
2008            };
2009            // The replica truthfully advertises its higher epoch; a correct
2010            // primary would fence here. This mock deliberately doesn't.
2011            assert_eq!(handshake.epoch, 5, "replica advertises its real epoch");
2012
2013            let mut buf = Vec::new();
2014            encode_stream_start(
2015                handshake.last_sequence,
2016                1,
2017                [0u8; 32],
2018                STALE_EPOCH,
2019                1,
2020                &mut buf,
2021            );
2022            writer.write_all(&buf).unwrap();
2023            writer.flush().unwrap();
2024        });
2025
2026        // Replica side: epoch 5, mirroring `run_receiver`'s handshake +
2027        // StreamStart handling.
2028        let fence_state = Arc::new(FenceState::new(5));
2029        let mut r_reader = replica_stream.try_clone().unwrap();
2030        let mut r_writer = replica_stream;
2031
2032        let mut buf = Vec::new();
2033        encode_handshake(
2034            &Handshake {
2035                last_sequence: 0,
2036                chain_hash: [0u8; 32],
2037                epoch: fence_state.epoch(),
2038            },
2039            &mut buf,
2040        );
2041        r_writer.write_all(&buf).unwrap();
2042        r_writer.flush().unwrap();
2043
2044        let frame = read_frame(&mut r_reader, MAX_CONTROL_FRAME).unwrap();
2045        let stream_epoch = match decode_primary_message(&frame).unwrap() {
2046            PrimaryMessage::StreamStart { epoch, .. } => epoch,
2047            other => panic!("expected StreamStart, got {other:?}"),
2048        };
2049        assert_eq!(stream_epoch, STALE_EPOCH, "epoch must survive the wire");
2050
2051        // This is the receiver's refusal decision. A stale primary must be
2052        // refused; the replica's own epoch must not have been lowered.
2053        assert!(
2054            fence_state.refuses_primary(stream_epoch),
2055            "replica at epoch 5 must refuse a StreamStart from epoch {STALE_EPOCH}"
2056        );
2057        assert_eq!(fence_state.epoch(), 5, "refusal must not lower our epoch");
2058
2059        // Sanity: an equal or newer epoch is followed, not refused (the
2060        // ex-primary-rejoin path). Guards against an inverted comparison.
2061        assert!(!fence_state.refuses_primary(5), "same tenure is followed");
2062        assert!(!fence_state.refuses_primary(6), "newer primary is followed");
2063
2064        primary_handle.join().unwrap();
2065    }
2066
2067    #[test]
2068    fn snapshot_begin_encode_decode_round_trip() {
2069        let mut buf = Vec::new();
2070        encode_snapshot_begin(1_000_000, 42, &[0xAB; 32], &mut buf);
2071
2072        let payload = &buf[4..];
2073        let msg = decode_primary_message(payload).unwrap();
2074        match msg {
2075            PrimaryMessage::SnapshotBegin {
2076                snapshot_len,
2077                snap_sequence,
2078                snap_chain_hash,
2079            } => {
2080                assert_eq!(snapshot_len, 1_000_000);
2081                assert_eq!(snap_sequence, 42);
2082                assert_eq!(snap_chain_hash, [0xAB; 32]);
2083            }
2084            _ => panic!("expected SnapshotBegin"),
2085        }
2086    }
2087
2088    #[test]
2089    fn snapshot_chunk_encode_decode_round_trip() {
2090        let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
2091        let mut buf = Vec::new();
2092        encode_snapshot_chunk(&data, &mut buf);
2093
2094        let payload = &buf[4..];
2095        let msg = decode_primary_message(payload).unwrap();
2096        match msg {
2097            PrimaryMessage::SnapshotChunk(chunk) => {
2098                assert_eq!(chunk, data);
2099            }
2100            _ => panic!("expected SnapshotChunk"),
2101        }
2102    }
2103
2104    #[test]
2105    fn snapshot_end_encode_decode_round_trip() {
2106        let mut buf = Vec::new();
2107        encode_snapshot_end(0xDEADBEEF, &mut buf);
2108
2109        let payload = &buf[4..];
2110        let msg = decode_primary_message(payload).unwrap();
2111        match msg {
2112            PrimaryMessage::SnapshotEnd { crc32c } => {
2113                assert_eq!(crc32c, 0xDEADBEEF);
2114            }
2115            _ => panic!("expected SnapshotEnd"),
2116        }
2117    }
2118
2119    /// Simulate the receiver side of a snapshot transfer where the
2120    /// advertised snap_len doesn't match the actual bytes sent.
2121    /// The receiver must detect this and return an error.
2122    #[test]
2123    fn snapshot_receiver_detects_length_mismatch() {
2124        use std::os::unix::net::UnixStream;
2125
2126        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
2127
2128        // Receiver thread — reads NeedSnapshot, then the snapshot transfer.
2129        let receiver = std::thread::spawn(move || -> String {
2130            let mut reader = replica_stream.try_clone().unwrap();
2131
2132            // Read NeedSnapshot.
2133            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2134            assert!(matches!(
2135                decode_primary_message(&frame).unwrap(),
2136                PrimaryMessage::NeedSnapshot,
2137            ));
2138
2139            // Read SnapshotBegin.
2140            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2141            let (snap_len, _snap_sequence, _snap_chain_hash) =
2142                match decode_primary_message(&frame).unwrap() {
2143                    PrimaryMessage::SnapshotBegin {
2144                        snapshot_len,
2145                        snap_sequence,
2146                        snap_chain_hash,
2147                    } => (snapshot_len, snap_sequence, snap_chain_hash),
2148                    other => panic!("expected SnapshotBegin, got {other:?}"),
2149                };
2150
2151            // Receive chunks and check length at SnapshotEnd.
2152            let mut received: u64 = 0;
2153            loop {
2154                let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
2155                match decode_primary_message(&frame).unwrap() {
2156                    PrimaryMessage::SnapshotChunk(data) => {
2157                        received += data.len() as u64;
2158                    }
2159                    PrimaryMessage::SnapshotEnd { .. } => {
2160                        if received != snap_len {
2161                            return format!(
2162                                "snapshot length mismatch: expected {snap_len} bytes, got {received}"
2163                            );
2164                        }
2165                        return String::new(); // no error
2166                    }
2167                    other => panic!("unexpected message: {other:?}"),
2168                }
2169            }
2170        });
2171
2172        // Primary side — send snapshot with wrong advertised length.
2173        let mut writer = primary_stream;
2174        let mut buf = Vec::new();
2175
2176        let actual_data = vec![0xAA; 100];
2177        let wrong_len = 999u64; // advertise 999 bytes, send only 100
2178
2179        encode_need_snapshot(&mut buf);
2180        std::io::Write::write_all(&mut writer, &buf).unwrap();
2181        buf.clear();
2182
2183        encode_snapshot_begin(wrong_len, 42, &[0xBB; 32], &mut buf);
2184        std::io::Write::write_all(&mut writer, &buf).unwrap();
2185        buf.clear();
2186
2187        encode_snapshot_chunk(&actual_data, &mut buf);
2188        std::io::Write::write_all(&mut writer, &buf).unwrap();
2189        buf.clear();
2190
2191        let crc = crc32c::crc32c(&actual_data);
2192        encode_snapshot_end(crc, &mut buf);
2193        std::io::Write::write_all(&mut writer, &buf).unwrap();
2194        std::io::Write::flush(&mut writer).unwrap();
2195
2196        let error_msg = receiver.join().unwrap();
2197        assert!(
2198            error_msg.contains("length mismatch"),
2199            "expected length mismatch error, got: {error_msg:?}"
2200        );
2201    }
2202
2203    /// Simulate the receiver side of a snapshot transfer where the CRC
2204    /// in SnapshotEnd doesn't match the actual data. The receiver must
2205    /// detect and reject the transfer.
2206    #[test]
2207    fn snapshot_receiver_detects_crc_mismatch() {
2208        use std::os::unix::net::UnixStream;
2209
2210        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
2211
2212        let receiver = std::thread::spawn(move || -> String {
2213            let mut reader = replica_stream.try_clone().unwrap();
2214
2215            // Read NeedSnapshot.
2216            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2217            assert!(matches!(
2218                decode_primary_message(&frame).unwrap(),
2219                PrimaryMessage::NeedSnapshot,
2220            ));
2221
2222            // Read SnapshotBegin.
2223            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2224            let snap_len = match decode_primary_message(&frame).unwrap() {
2225                PrimaryMessage::SnapshotBegin { snapshot_len, .. } => snapshot_len,
2226                other => panic!("expected SnapshotBegin, got {other:?}"),
2227            };
2228
2229            // Receive chunks, verify CRC at SnapshotEnd.
2230            let mut received_data = Vec::new();
2231            let mut received: u64 = 0;
2232            loop {
2233                let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
2234                match decode_primary_message(&frame).unwrap() {
2235                    PrimaryMessage::SnapshotChunk(data) => {
2236                        received += data.len() as u64;
2237                        received_data.extend_from_slice(&data);
2238                    }
2239                    PrimaryMessage::SnapshotEnd {
2240                        crc32c: expected_crc,
2241                    } => {
2242                        if received != snap_len {
2243                            return format!("length mismatch: {snap_len} vs {received}");
2244                        }
2245                        let actual_crc = crc32c::crc32c(&received_data);
2246                        if actual_crc != expected_crc {
2247                            return format!(
2248                                "CRC mismatch: expected {expected_crc:#x}, got {actual_crc:#x}"
2249                            );
2250                        }
2251                        return String::new();
2252                    }
2253                    other => panic!("unexpected message: {other:?}"),
2254                }
2255            }
2256        });
2257
2258        // Primary side — send correct length but wrong CRC.
2259        let mut writer = primary_stream;
2260        let mut buf = Vec::new();
2261
2262        let data = vec![0xAA; 100];
2263
2264        encode_need_snapshot(&mut buf);
2265        std::io::Write::write_all(&mut writer, &buf).unwrap();
2266        buf.clear();
2267
2268        encode_snapshot_begin(data.len() as u64, 42, &[0xBB; 32], &mut buf);
2269        std::io::Write::write_all(&mut writer, &buf).unwrap();
2270        buf.clear();
2271
2272        encode_snapshot_chunk(&data, &mut buf);
2273        std::io::Write::write_all(&mut writer, &buf).unwrap();
2274        buf.clear();
2275
2276        // Send a wrong CRC (flip bits).
2277        let wrong_crc = !crc32c::crc32c(&data);
2278        encode_snapshot_end(wrong_crc, &mut buf);
2279        std::io::Write::write_all(&mut writer, &buf).unwrap();
2280        std::io::Write::flush(&mut writer).unwrap();
2281
2282        let error_msg = receiver.join().unwrap();
2283        assert!(
2284            error_msg.contains("CRC mismatch"),
2285            "expected CRC mismatch error, got: {error_msg:?}"
2286        );
2287    }
2288
2289    /// The receiver verifies the chain hash from the loaded snapshot
2290    /// matches the one advertised in SnapshotBegin. Simulate a mismatch.
2291    #[test]
2292    fn snapshot_receiver_detects_chain_hash_mismatch() {
2293        use std::os::unix::net::UnixStream;
2294
2295        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
2296
2297        let receiver = std::thread::spawn(move || -> String {
2298            let mut reader = replica_stream.try_clone().unwrap();
2299
2300            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2301            assert!(matches!(
2302                decode_primary_message(&frame).unwrap(),
2303                PrimaryMessage::NeedSnapshot,
2304            ));
2305
2306            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2307            let (snap_len, _snap_sequence, snap_chain_hash) =
2308                match decode_primary_message(&frame).unwrap() {
2309                    PrimaryMessage::SnapshotBegin {
2310                        snapshot_len,
2311                        snap_sequence,
2312                        snap_chain_hash,
2313                    } => (snapshot_len, snap_sequence, snap_chain_hash),
2314                    other => panic!("expected SnapshotBegin, got {other:?}"),
2315                };
2316
2317            // Receive the snapshot data.
2318            let mut received_data = Vec::new();
2319            let mut received: u64 = 0;
2320            loop {
2321                let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
2322                match decode_primary_message(&frame).unwrap() {
2323                    PrimaryMessage::SnapshotChunk(data) => {
2324                        received += data.len() as u64;
2325                        received_data.extend_from_slice(&data);
2326                    }
2327                    PrimaryMessage::SnapshotEnd {
2328                        crc32c: expected_crc,
2329                    } => {
2330                        assert_eq!(received, snap_len, "length should match");
2331                        let actual_crc = crc32c::crc32c(&received_data);
2332                        assert_eq!(actual_crc, expected_crc, "CRC should match");
2333                        break;
2334                    }
2335                    other => panic!("unexpected message: {other:?}"),
2336                }
2337            }
2338
2339            // Simulate chain hash verification: the loaded snapshot would
2340            // have a different chain hash than what SnapshotBegin advertised.
2341            let loaded_hash = [0xFF; 32]; // different from snap_chain_hash
2342            if loaded_hash != snap_chain_hash {
2343                return format!(
2344                    "snapshot chain hash mismatch: primary sent {snap_chain_hash:02x?}, \
2345                     loaded snapshot has {loaded_hash:02x?}"
2346                );
2347            }
2348            String::new()
2349        });
2350
2351        // Primary side — send valid snapshot but with a chain hash in
2352        // SnapshotBegin that won't match what the replica "loads".
2353        let mut writer = primary_stream;
2354        let mut buf = Vec::new();
2355
2356        let data = vec![0xAA; 64];
2357        // Advertise chain hash [0xBB; 32] — receiver will "load" [0xFF; 32].
2358        let advertised_hash = [0xBB; 32];
2359
2360        encode_need_snapshot(&mut buf);
2361        std::io::Write::write_all(&mut writer, &buf).unwrap();
2362        buf.clear();
2363
2364        encode_snapshot_begin(data.len() as u64, 10, &advertised_hash, &mut buf);
2365        std::io::Write::write_all(&mut writer, &buf).unwrap();
2366        buf.clear();
2367
2368        encode_snapshot_chunk(&data, &mut buf);
2369        std::io::Write::write_all(&mut writer, &buf).unwrap();
2370        buf.clear();
2371
2372        let crc = crc32c::crc32c(&data);
2373        encode_snapshot_end(crc, &mut buf);
2374        std::io::Write::write_all(&mut writer, &buf).unwrap();
2375        std::io::Write::flush(&mut writer).unwrap();
2376
2377        let error_msg = receiver.join().unwrap();
2378        assert!(
2379            error_msg.contains("chain hash mismatch"),
2380            "expected chain hash mismatch error, got: {error_msg:?}"
2381        );
2382    }
2383
2384    /// Primary-side magic validation: a file without the SNAP magic
2385    /// (0x534E4150) must be rejected before transfer.
2386    #[test]
2387    fn primary_rejects_snapshot_with_invalid_magic() {
2388        let tmp = tempfile::tempdir().unwrap();
2389        let snap_path = tmp.path().join("test.snapshot");
2390
2391        // Write a file with wrong magic but enough bytes for a header.
2392        let mut bad_snap = vec![0u8; 64];
2393        bad_snap[0..4].copy_from_slice(&0xDEAD_BEEFu32.to_le_bytes()); // wrong magic
2394
2395        std::fs::write(&snap_path, &bad_snap).unwrap();
2396
2397        // Replicate the primary's validation logic.
2398        let snap_data = std::fs::read(&snap_path).unwrap();
2399        assert!(
2400            snap_data.len() >= 48,
2401            "file should be big enough for header"
2402        );
2403
2404        let magic = u32::from_le_bytes(snap_data[0..4].try_into().unwrap());
2405        assert_ne!(magic, 0x534E_4150);
2406        assert_eq!(magic, 0xDEAD_BEEF);
2407    }
2408
2409    /// Primary-side: a snapshot file smaller than the 48-byte header
2410    /// must be rejected.
2411    #[test]
2412    fn primary_rejects_snapshot_too_small_for_header() {
2413        let tmp = tempfile::tempdir().unwrap();
2414        let snap_path = tmp.path().join("test.snapshot");
2415
2416        // Write a file smaller than the 48-byte header.
2417        std::fs::write(&snap_path, [0u8; 20]).unwrap();
2418
2419        let snap_data = std::fs::read(&snap_path).unwrap();
2420        assert!(
2421            snap_data.len() < 48,
2422            "file must be too small for header validation"
2423        );
2424    }
2425
2426    #[test]
2427    fn decode_snapshot_begin_too_short() {
2428        // SnapshotBegin needs type(1) + snapshot_len(8) + snap_sequence(8) + chain_hash(32) = 49.
2429        // Send only the type byte + a few extra bytes.
2430        let payload = [MSG_SNAPSHOT_BEGIN, 0x01, 0x02, 0x03];
2431        let err = decode_primary_message(&payload).unwrap_err();
2432        assert!(
2433            err.to_string().contains("SnapshotBegin too short"),
2434            "expected 'SnapshotBegin too short', got: {err}"
2435        );
2436    }
2437
2438    #[test]
2439    fn decode_snapshot_end_too_short() {
2440        // SnapshotEnd needs type(1) + crc32c(4) = 5. Send only the type byte.
2441        let payload = [MSG_SNAPSHOT_END];
2442        let err = decode_primary_message(&payload).unwrap_err();
2443        assert!(
2444            err.to_string().contains("SnapshotEnd too short"),
2445            "expected 'SnapshotEnd too short', got: {err}"
2446        );
2447    }
2448
2449    #[test]
2450    fn decode_snapshot_chunk_empty_data() {
2451        // SnapshotChunk with just the type byte — valid but empty payload.
2452        let payload = [MSG_SNAPSHOT_CHUNK];
2453        let msg = decode_primary_message(&payload).unwrap();
2454        match msg {
2455            PrimaryMessage::SnapshotChunk(data) => {
2456                assert!(data.is_empty());
2457            }
2458            _ => panic!("expected SnapshotChunk"),
2459        }
2460    }
2461
2462    // --- PendingAckQueue tests ---
2463
2464    fn make_journal_cursor(val: u64) -> melin_pipeline::padding::Sequence {
2465        melin_pipeline::padding::Sequence::new(AtomicU64::new(val))
2466    }
2467
2468    #[test]
2469    fn pending_ack_queue_push_and_pop_ready() {
2470        let mut q = PendingAckQueue::new(8);
2471        assert!(q.is_empty());
2472        assert!(!q.is_full());
2473
2474        q.push(10, 100);
2475        q.push(20, 200);
2476        assert!(!q.is_empty());
2477
2478        // Cursor at 5 — neither ready.
2479        let cursor = make_journal_cursor(5);
2480        assert!(q.pop_ready(&cursor).is_none());
2481
2482        // Cursor at 15 — first ready, second not.
2483        cursor.get().store(15, Ordering::Relaxed);
2484        assert_eq!(q.pop_ready(&cursor), Some(100));
2485        // Only one popped — second still pending.
2486        assert!(!q.is_empty());
2487
2488        // Cursor at 25 — second now ready.
2489        cursor.get().store(25, Ordering::Relaxed);
2490        assert_eq!(q.pop_ready(&cursor), Some(200));
2491        assert!(q.is_empty());
2492    }
2493
2494    #[test]
2495    fn pending_ack_queue_pop_ready_returns_highest_sequence() {
2496        // When multiple acks become ready simultaneously, pop_ready
2497        // returns the highest acked_sequence (ack semantics are
2498        // cumulative — "everything up to this sequence is durable").
2499        let mut q = PendingAckQueue::new(8);
2500        q.push(10, 100);
2501        q.push(20, 200);
2502        q.push(30, 300);
2503
2504        let cursor = make_journal_cursor(30);
2505        assert_eq!(q.pop_ready(&cursor), Some(300));
2506        assert!(q.is_empty());
2507    }
2508
2509    #[test]
2510    fn pending_ack_queue_capacity_and_full() {
2511        let mut q = PendingAckQueue::new(8);
2512        for i in 0..8 {
2513            assert!(!q.is_full());
2514            q.push(i as u64 + 1, (i + 1) as u64 * 100);
2515        }
2516        assert!(q.is_full());
2517    }
2518
2519    /// A full queue must absorb the push by merging it into the newest
2520    /// entry — never block, never drop, and never release a sequence
2521    /// before its own journal target. The merge is a *coarsening*: the
2522    /// entry it absorbed is acked later than it would have been.
2523    #[test]
2524    fn pending_ack_queue_merges_into_the_newest_entry_when_full() {
2525        let mut q = PendingAckQueue::new(4);
2526        for i in 1..=4u64 {
2527            q.push(i * 10, i * 100);
2528        }
2529        assert!(q.is_full());
2530        assert_eq!(q.merged(), 0);
2531
2532        // Fifth batch: target 50, sequence 500. Merges into (40, 400).
2533        q.push(50, 500);
2534        assert_eq!(q.merged(), 1);
2535        assert_eq!(q.len(), 4, "merge must not grow the queue");
2536
2537        // Cursor at 40 would have released 400 before the merge; now the
2538        // newest entry needs 50, so 300 is the highest safe ack.
2539        let cursor = make_journal_cursor(40);
2540        assert_eq!(q.pop_ready(&cursor), Some(300));
2541        assert!(!q.is_empty(), "merged entry is not durable yet");
2542
2543        // Cursor reaches the merged target — the merged entry releases
2544        // the newer sequence, subsuming the one it absorbed.
2545        cursor.get().store(50, Ordering::Relaxed);
2546        assert_eq!(q.pop_ready(&cursor), Some(500));
2547        assert!(q.is_empty());
2548    }
2549
2550    /// Repeated merges keep collapsing onto the same slot, so a journal
2551    /// stall of any length costs granularity and nothing else.
2552    #[test]
2553    fn pending_ack_queue_merges_repeatedly_without_growing() {
2554        let mut q = PendingAckQueue::new(2);
2555        for i in 1..=100u64 {
2556            q.push(i, i * 10);
2557        }
2558        assert_eq!(q.len(), 2);
2559        assert_eq!(q.merged(), 98);
2560
2561        // Nothing releases before its target...
2562        let cursor = make_journal_cursor(99);
2563        assert_eq!(q.pop_ready(&cursor), Some(10), "only the first entry");
2564        // ...and the merged tail carries the newest pair.
2565        cursor.get().store(100, Ordering::Relaxed);
2566        assert_eq!(q.pop_ready(&cursor), Some(1000));
2567    }
2568
2569    #[test]
2570    fn pending_ack_queue_pop_oldest_blocking() {
2571        let mut q = PendingAckQueue::new(8);
2572        q.push(10, 100);
2573        q.push(20, 200);
2574
2575        // Cursor already past both targets — pop_oldest_blocking
2576        // returns immediately.
2577        let cursor = make_journal_cursor(25);
2578        let seq = q.pop_oldest_blocking(&cursor, true, &AtomicBool::new(false));
2579        // Should pop both (oldest + any others that became ready).
2580        assert_eq!(seq, Some(200));
2581        assert!(q.is_empty());
2582    }
2583
2584    /// A dead journal stage (abort latch set) must abort the blocking
2585    /// wait instead of spinning forever on the frozen cursor.
2586    #[test]
2587    fn pending_ack_queue_blocking_wait_aborts_on_journal_failure() {
2588        let mut q = PendingAckQueue::new(8);
2589        q.push(10, 100);
2590
2591        // Cursor frozen BELOW the target; abort pre-latched.
2592        let cursor = make_journal_cursor(5);
2593        let abort = AtomicBool::new(true);
2594        assert_eq!(q.pop_oldest_blocking(&cursor, true, &abort), None);
2595        assert!(!q.is_empty(), "aborted wait must not pop the entry");
2596        assert_eq!(q.pop_all_blocking(&cursor, true, &abort), None);
2597    }
2598
2599    /// `shutdown_pipeline` must surface the journal stage's error kind —
2600    /// the orchestrator routes `ReplicaChainDivergence` into in-process
2601    /// resync and everything else into process exit, so collapsing the
2602    /// error into a None (the old behavior) breaks that dispatch. It
2603    /// must also join EVERY thread in the failure arms: the resync path
2604    /// archives journal + snapshot right after teardown, and a
2605    /// still-running shadow thread mid-snapshot-write would race the
2606    /// renames.
2607    #[test]
2608    fn shutdown_pipeline_surfaces_journal_error_kind() {
2609        let flag = AtomicBool::new(false);
2610        let matching_joined = Arc::new(AtomicBool::new(false));
2611        let mj = Arc::clone(&matching_joined);
2612
2613        let journal = std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> {
2614            Err(melin_journal::JournalError::ReplicaChainDivergence {
2615                sequence: 42,
2616                expected: [1u8; 32],
2617                actual: [2u8; 32],
2618            })
2619        });
2620        let matching = std::thread::spawn(move || {
2621            mj.store(true, Ordering::Release);
2622            7u64
2623        });
2624        let drain = std::thread::spawn(|| {});
2625
2626        let outcome = shutdown_pipeline::<u64, u32>(&flag, journal, matching, drain, None);
2627        assert!(flag.load(Ordering::Acquire), "shutdown flag must be set");
2628        assert!(
2629            matching_joined.load(Ordering::Acquire),
2630            "matching thread must be joined even when the journal failed"
2631        );
2632        match outcome {
2633            TeardownOutcome::JournalFailed(
2634                melin_journal::JournalError::ReplicaChainDivergence { sequence, .. },
2635            ) => assert_eq!(sequence, 42),
2636            _ => panic!("expected JournalFailed(ReplicaChainDivergence)"),
2637        }
2638    }
2639
2640    #[test]
2641    fn shutdown_pipeline_clean_returns_both_states() {
2642        let flag = AtomicBool::new(false);
2643        let journal =
2644            std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> { Ok(11u32) });
2645        let matching = std::thread::spawn(|| 7u64);
2646        let drain = std::thread::spawn(|| {});
2647
2648        match shutdown_pipeline::<u64, u32>(&flag, journal, matching, drain, None) {
2649            TeardownOutcome::Clean(app, writer) => {
2650                assert_eq!(app, 7);
2651                assert_eq!(writer, 11);
2652            }
2653            _ => panic!("expected Clean"),
2654        }
2655    }
2656
2657    #[test]
2658    fn shutdown_pipeline_panicked_stage_reports_panicked() {
2659        let flag = AtomicBool::new(false);
2660        let journal =
2661            std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> { Ok(11u32) });
2662        let matching = std::thread::spawn(|| -> u64 { panic!("matching stage died") });
2663        let drain = std::thread::spawn(|| {});
2664
2665        assert!(matches!(
2666            shutdown_pipeline::<u64, u32>(&flag, journal, matching, drain, None),
2667            TeardownOutcome::Panicked
2668        ));
2669    }
2670
2671    /// `ReplicaPipelineHandles` over a real input ring (one gate
2672    /// consumer) with immediately-returning stage threads — just enough
2673    /// structure for `teardown_replica_pipeline` to run for real. The
2674    /// returned consumer observes what teardown published.
2675    fn teardown_fixture(
2676        capacity: usize,
2677    ) -> (
2678        ReplicaPipelineHandles<counter_server::Counter, u32>,
2679        melin_pipeline::ring::Consumer<InputSlot>,
2680    ) {
2681        use melin_app::app_factory::AppFactory;
2682        let (input_producer, mut consumers) =
2683            melin_pipeline::ring::DisruptorBuilder::<InputSlot>::new(capacity)
2684                .add_consumer()
2685                .build();
2686        let consumer = consumers.pop().expect("one consumer");
2687        let handles = ReplicaPipelineHandles {
2688            input_producer,
2689            journal_cursor: Arc::new(make_journal_cursor(0)),
2690            last_seq: melin_transport_core::DurableWireSeqCursor::detached(
2691                melin_transport_core::WireSeq::new(0),
2692            ),
2693            chain_hash_lock: None,
2694            stream_marks: Arc::new(std::sync::Mutex::new(std::collections::VecDeque::new())),
2695            journal_failed: Arc::new(AtomicBool::new(false)),
2696            pipeline_shutdown: Arc::new(AtomicBool::new(false)),
2697            journal_handle: std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> {
2698                Ok(11)
2699            }),
2700            matching_handle: std::thread::spawn(|| counter_server::CounterFactory.empty()),
2701            drain_handle: std::thread::spawn(|| {}),
2702            shadow_handle: None,
2703        };
2704        (handles, consumer)
2705    }
2706
2707    /// The invariant behind moving the sentinel publish into
2708    /// `teardown_replica_pipeline`: a healthy teardown always puts the
2709    /// shutdown sentinel on the ring, so the stages exit by consuming it
2710    /// through the normal event path (draining anything ahead of it),
2711    /// not via the flag's emergency-abort branch.
2712    #[test]
2713    fn teardown_publishes_shutdown_sentinel() {
2714        let (handles, mut consumer) = teardown_fixture(8);
2715        assert!(matches!(
2716            teardown_replica_pipeline::<counter_server::Counter, u32>(handles),
2717            TeardownOutcome::Clean(..)
2718        ));
2719        let (_, slot) = consumer.try_consume().expect("sentinel on the ring");
2720        assert!(
2721            slot.event.is_shutdown(),
2722            "published slot must be the sentinel"
2723        );
2724        assert!(consumer.try_consume().is_none(), "exactly one sentinel");
2725    }
2726
2727    /// Drive `handle_session_exit` through one `Disconnected` exit with
2728    /// the given liveness flag and a backoff pre-escalated to
2729    /// `MAX_BACKOFF`, returning the post-exit backoff. Shutdown is
2730    /// latched so the backoff sleep returns immediately (the receiver's
2731    /// loop top would handle it on the next turn); the journal, factory
2732    /// and fence arguments are inert on the `Disconnected` path.
2733    fn backoff_after_disconnect(heard_from_primary: bool) -> std::time::Duration {
2734        let dir = tempfile::tempdir().expect("tempdir");
2735        type Writer = melin_journal::BufferedWriter<CounterEvent>;
2736        let mut pipeline: Option<ReplicaPipelineHandles<counter_server::Counter, Writer>> = None;
2737        let mut divergence_resyncs = 0u32;
2738        let mut backoff = MAX_BACKOFF;
2739        let shutdown = AtomicBool::new(true);
2740        let promote = crate::promotion::PromotionRequest::new();
2741        let after = handle_session_exit::<counter_server::Counter, Writer>(
2742            StreamingResult {
2743                exit: SessionExit::Disconnected,
2744                heard_from_primary,
2745            },
2746            &mut pipeline,
2747            &mut divergence_resyncs,
2748            &mut backoff,
2749            0,
2750            &dir.path().join("r.journal"),
2751            &dir.path().join("r.snapshot"),
2752            &counter_server::CounterFactory,
2753            &melin_transport_core::fence::FenceState::new(0),
2754            &shutdown,
2755            &promote,
2756            || {},
2757        );
2758        assert!(matches!(after, AfterSession::Reconnect));
2759        backoff
2760    }
2761
2762    /// A session in which the primary spoke — even heartbeat-only on a
2763    /// quiet system — must reset the disconnect backoff. A stale
2764    /// escalated backoff delays primary-link recovery, and with it the
2765    /// auto-promotion veto, by up to MAX_BACKOFF.
2766    #[test]
2767    fn disconnect_resets_backoff_when_primary_spoke() {
2768        // Reset to 1s, then doubled for the next attempt by the shared
2769        // helper — NOT stuck at MAX_BACKOFF.
2770        assert_eq!(
2771            backoff_after_disconnect(true),
2772            std::time::Duration::from_secs(2)
2773        );
2774    }
2775
2776    /// A session with no word from the primary — an instant-drop flap,
2777    /// or the synthetic result of a session that never started
2778    /// streaming (local transport init failure) — must keep its
2779    /// escalated backoff rather than redialing at 1 Hz forever.
2780    #[test]
2781    fn disconnect_keeps_escalated_backoff_when_primary_silent() {
2782        assert_eq!(backoff_after_disconnect(false), MAX_BACKOFF);
2783    }
2784
2785    /// A stream contiguity break is a reconnect, never a process exit:
2786    /// the contiguous prefix is committed and the journal is intact, so
2787    /// the receiver re-handshakes from its durable position and the
2788    /// primary re-streams the hole. It used to fall through the `Fatal`
2789    /// arm and take the replica down for a documented-benign primary
2790    /// handoff corner. The pipeline must be left standing (it is what
2791    /// the reconnect resumes into) and the backoff reset — the primary
2792    /// was evidently speaking.
2793    #[test]
2794    fn stream_gap_reconnects_and_keeps_pipeline() {
2795        use melin_app::app_factory::AppFactory;
2796        let dir = tempfile::tempdir().expect("tempdir");
2797        type Writer = melin_journal::BufferedWriter<CounterEvent>;
2798        // Same shape as `teardown_fixture`, but with a real writer type:
2799        // `handle_session_exit` is bounded on `JournalWrite` for the
2800        // resync arm even though this path never touches it.
2801        let (input_producer, mut consumers) =
2802            melin_pipeline::ring::DisruptorBuilder::<InputSlot>::new(4)
2803                .add_consumer()
2804                .build();
2805        let _consumer = consumers.pop().expect("one consumer");
2806        let writer_path = dir.path().join("w.journal");
2807        let handles = ReplicaPipelineHandles {
2808            input_producer,
2809            journal_cursor: Arc::new(make_journal_cursor(0)),
2810            last_seq: melin_transport_core::DurableWireSeqCursor::detached(
2811                melin_transport_core::WireSeq::new(0),
2812            ),
2813            chain_hash_lock: None,
2814            stream_marks: Arc::new(std::sync::Mutex::new(std::collections::VecDeque::new())),
2815            journal_failed: Arc::new(AtomicBool::new(false)),
2816            pipeline_shutdown: Arc::new(AtomicBool::new(false)),
2817            journal_handle: std::thread::spawn(move || Writer::create(&writer_path)),
2818            matching_handle: std::thread::spawn(|| counter_server::CounterFactory.empty()),
2819            drain_handle: std::thread::spawn(|| {}),
2820            shadow_handle: None,
2821        };
2822        let mut pipeline: Option<ReplicaPipelineHandles<counter_server::Counter, Writer>> =
2823            Some(handles);
2824        let mut divergence_resyncs = 0u32;
2825        let mut backoff = MAX_BACKOFF;
2826        let shutdown = AtomicBool::new(true);
2827        let promote = crate::promotion::PromotionRequest::new();
2828        let mut closed = false;
2829        let after = handle_session_exit::<counter_server::Counter, Writer>(
2830            StreamingResult {
2831                exit: SessionExit::StreamGap("sequence gap: expected 3, got 5".into()),
2832                heard_from_primary: true,
2833            },
2834            &mut pipeline,
2835            &mut divergence_resyncs,
2836            &mut backoff,
2837            2,
2838            &dir.path().join("r.journal"),
2839            &dir.path().join("r.snapshot"),
2840            &counter_server::CounterFactory,
2841            &melin_transport_core::fence::FenceState::new(0),
2842            &shutdown,
2843            &promote,
2844            || closed = true,
2845        );
2846        assert!(
2847            matches!(after, AfterSession::Reconnect),
2848            "a stream gap must reconnect, not return an error"
2849        );
2850        assert!(
2851            pipeline.is_some(),
2852            "the pipeline must survive for the reconnect"
2853        );
2854        assert!(closed, "the transport must be closed before redialing");
2855        assert_eq!(
2856            backoff,
2857            std::time::Duration::from_secs(2),
2858            "backoff reset to 1s then doubled by the shared helper"
2859        );
2860        assert_eq!(divergence_resyncs, 0, "not a resync");
2861        if let Some(p) = pipeline.take() {
2862            let _ = teardown_replica_pipeline::<counter_server::Counter, Writer>(p);
2863        }
2864    }
2865
2866    /// With the journal-failure latch set and the ring full — the state
2867    /// a dead journal stage leaves behind (its frozen gate cursor means
2868    /// the ring never drains) — teardown must skip the sentinel and
2869    /// return, instead of spinning forever on ring backpressure.
2870    #[test]
2871    fn teardown_skips_sentinel_when_journal_failed_and_ring_full() {
2872        let (mut handles, mut consumer) = teardown_fixture(4);
2873        for seq in 0..4 {
2874            assert!(
2875                handles
2876                    .input_producer
2877                    .try_publish(InputSlot {
2878                        sequence: seq,
2879                        ..InputSlot::default()
2880                    })
2881                    .is_ok(),
2882                "ring must have space while filling"
2883            );
2884        }
2885        assert!(
2886            handles
2887                .input_producer
2888                .try_publish(InputSlot::default())
2889                .is_err(),
2890            "ring must be full"
2891        );
2892        handles.journal_failed.store(true, Ordering::Release);
2893        // Would spin forever on the frozen gate without the latch check.
2894        assert!(matches!(
2895            teardown_replica_pipeline::<counter_server::Counter, u32>(handles),
2896            TeardownOutcome::Clean(..)
2897        ));
2898        while let Some((_, slot)) = consumer.try_consume() {
2899            assert!(!slot.event.is_shutdown(), "no sentinel may be published");
2900        }
2901    }
2902
2903    #[test]
2904    fn pending_ack_queue_wraps_around() {
2905        let mut q = PendingAckQueue::new(8);
2906        let cursor = make_journal_cursor(100);
2907
2908        // Fill and drain multiple times to exercise circular buffer wrap.
2909        for round in 0..3 {
2910            for i in 0..8 {
2911                let target = (round * 8 + i) as u64 + 1;
2912                q.push(target, target * 10);
2913            }
2914            assert!(q.is_full());
2915            let seq = q.pop_ready(&cursor).expect("should be ready");
2916            assert_eq!(seq, (round * 8 + 8) as u64 * 10);
2917            assert!(q.is_empty());
2918        }
2919    }
2920
2921    #[test]
2922    fn pending_ack_queue_pop_all_blocking_empty() {
2923        let mut q = PendingAckQueue::new(8);
2924        let cursor = make_journal_cursor(0);
2925        assert!(
2926            q.pop_all_blocking(&cursor, true, &AtomicBool::new(false))
2927                .is_none()
2928        );
2929    }
2930
2931    // --- try_flush_dual_track tests ---
2932
2933    #[test]
2934    fn dual_track_returns_none_when_both_tracks_idle() {
2935        let mut q = PendingAckQueue::new(8);
2936        let cursor = make_journal_cursor(0);
2937        assert!(
2938            try_flush_dual_track(&mut q, &cursor, 0, 0, 0).is_none(),
2939            "no advance on either track → no ack"
2940        );
2941    }
2942
2943    #[test]
2944    fn dual_track_fires_on_persisted_advance_only() {
2945        // Persisted track moves: pop_ready returns 100; in-memory
2946        // stayed at 50 (last_sent). Expect ack with acked=100,
2947        // in_memory=50 (no regression — the wire field carries the
2948        // current sample, not a "no change" marker).
2949        let mut q = PendingAckQueue::new(8);
2950        q.push(10, 100);
2951        let cursor = make_journal_cursor(20);
2952        let ack = try_flush_dual_track(&mut q, &cursor, 50, 0, 50).expect("persisted advanced");
2953        assert_eq!(ack.acked_sequence, 100);
2954        assert_eq!(ack.in_memory_sequence, 50);
2955    }
2956
2957    #[test]
2958    fn dual_track_fires_on_in_memory_advance_only() {
2959        // Persisted is idle (queue empty): unwrap_or keeps the
2960        // last-sent acked at 100. In-memory bumped from 100 to 200.
2961        let mut q = PendingAckQueue::new(8);
2962        let cursor = make_journal_cursor(0);
2963        let ack = try_flush_dual_track(&mut q, &cursor, 200, 100, 100).expect("in-memory advanced");
2964        assert_eq!(ack.acked_sequence, 100);
2965        assert_eq!(ack.in_memory_sequence, 200);
2966    }
2967
2968    #[test]
2969    fn dual_track_coalesces_until_caller_updates_trackers() {
2970        // Caller did not advance trackers between calls. The queue
2971        // popped on call 1; on call 2 it's empty so persisted stays
2972        // at 100 (unwrap_or). In-memory advanced 50 → 80 between
2973        // calls. Second call must still fire because tracker is
2974        // still 50.
2975        let mut q = PendingAckQueue::new(8);
2976        q.push(10, 100);
2977        let cursor = make_journal_cursor(20);
2978        let ack1 = try_flush_dual_track(&mut q, &cursor, 50, 0, 50).expect("call 1 fires");
2979        assert_eq!(ack1.acked_sequence, 100);
2980        // Caller "forgot" to update trackers (simulates send failure).
2981        let ack2 = try_flush_dual_track(&mut q, &cursor, 80, 0, 50)
2982            .expect("call 2 fires on in-memory advance");
2983        assert_eq!(
2984            ack2.acked_sequence, 0,
2985            "no new persisted pop → unwrap_or(last_sent)"
2986        );
2987        assert_eq!(ack2.in_memory_sequence, 80);
2988    }
2989
2990    #[test]
2991    fn dual_track_no_duplicate_ack_after_backpressure_drain() {
2992        // Models the backpressure-drain → resume-normal-flush path
2993        // taken by every receiver when `PendingAckQueue` fills up.
2994        // The receiver must drain the queue (via the
2995        // pop_oldest_blocking path in production; pop_ready here),
2996        // update *both* trackers from the drained ack, and only
2997        // then resume the normal cursor-driven flush. The bug
2998        // classes this pins are:
2999        //   (a) emitting a follow-up ack whose `acked_sequence`
3000        //       regresses below what the drain already sent
3001        //       (caught by the debug_assert! in
3002        //       `try_flush_dual_track`, also asserted at value
3003        //       level here);
3004        //   (b) emitting a duplicate ack carrying the same
3005        //       cursors as the drain when neither track actually
3006        //       advanced — the leak class fixed by ensuring every
3007        //       send site updates both `last_sent_acked_seq` and
3008        //       `last_sent_in_memory_seq`.
3009        let mut q = PendingAckQueue::new(4);
3010        // Fill the queue: four pending acks at primary seqs
3011        // 100, 200, 300, 400 with journal targets 10..=40.
3012        for i in 1..=4u64 {
3013            q.push(i * 10, i * 100);
3014        }
3015        assert!(q.is_full());
3016
3017        // Backpressure path: caller drains all ready entries
3018        // before pushing more. Highest acked seen = 400.
3019        let cursor = make_journal_cursor(40);
3020        let drained = q.pop_ready(&cursor).expect("all entries durable");
3021        assert_eq!(drained, 400);
3022        assert!(q.is_empty());
3023
3024        // Caller updates BOTH trackers from the drain. in_memory
3025        // at the time of the drained batch was 450.
3026        let mut last_sent_acked = drained;
3027        let mut last_sent_in_mem = 450u64;
3028
3029        // Quiescent immediately after drain: no fresh push, no
3030        // in-memory advance → try_flush must return None.
3031        // Failing here means we'd emit a duplicate ack carrying
3032        // the same (400, 450) the backpressure drain already sent.
3033        assert!(
3034            try_flush_dual_track(
3035                &mut q,
3036                &cursor,
3037                last_sent_in_mem,
3038                last_sent_acked,
3039                last_sent_in_mem,
3040            )
3041            .is_none(),
3042            "no advance on either track after backpressure drain → no duplicate ack",
3043        );
3044
3045        // Push a fresh batch (primary seq 500, journal target 50),
3046        // cursor catches up, in_memory advances to 500.
3047        q.push(50, 500);
3048        let cursor2 = make_journal_cursor(50);
3049        let ack = try_flush_dual_track(&mut q, &cursor2, 500, last_sent_acked, last_sent_in_mem)
3050            .expect("fresh batch after drain fires a new ack");
3051        assert!(
3052            ack.acked_sequence >= last_sent_acked,
3053            "regression: drain sent {last_sent_acked} but next ack carries {}",
3054            ack.acked_sequence,
3055        );
3056        assert_eq!(ack.acked_sequence, 500);
3057        assert_eq!(ack.in_memory_sequence, 500);
3058
3059        last_sent_acked = ack.acked_sequence;
3060        last_sent_in_mem = ack.in_memory_sequence;
3061
3062        // Post-resume quiescent: confirms idempotency past the
3063        // resume point — the second flush sees neither track
3064        // advance and must stay silent.
3065        assert!(
3066            try_flush_dual_track(
3067                &mut q,
3068                &cursor2,
3069                last_sent_in_mem,
3070                last_sent_acked,
3071                last_sent_in_mem,
3072            )
3073            .is_none(),
3074            "post-resume idle → no further ack",
3075        );
3076    }
3077}