nautilus-coinapi 0.67.0

CoinAPI market data integration adapter for the Nautilus trading engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Adapter-local order book synchronization state for CoinAPI.
//!
//! [`BookSyncTracker`] applies the CoinAPI sequence rule to the shared per-book [`BookSync`]
//! lifecycle and converts accepted messages into deltas. A snapshot lists the whole book, so it is
//! accepted at any sequence; an update is accepted only at the sequence after the last accepted
//! message. A book's position is that last sequence.
//!
//! CoinAPI restarts a pair's sequence at one whenever it restarts the pair or the connection, and
//! a managed `OrderBook` keeps its sequence as a high-water mark across snapshots. Emitted deltas
//! therefore carry the venue sequence plus a per-book offset, set at each accepted snapshot so the
//! emitted sequence keeps rising across restarts, unsubscribes, and reconnects. Gap detection uses
//! the venue sequence.
//!
//! Conversion happens under the tracker's lock, so an L3 book's `OrderIndex` changes only with
//! its accepted position. A message that fails conversion leaves the book out of sync, as a gap
//! does. This module performs state transitions; `super::recovery` runs the asynchronous
//! scope changes and retries.

use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};

use ahash::AHashMap;
use nautilus_common::live::dst::time::Instant;
use nautilus_core::AtomicMap;
use nautilus_live::book::{
    BookSequenceOutcome, BookSyncSignal, BookSyncSignalKind,
    snapshot::SnapshotGate,
    sync::{BookPhase, BookSync},
};
use nautilus_model::{
    data::OrderBookDeltas, identifiers::InstrumentId, instruments::InstrumentAny,
};
use parking_lot::Mutex;
use tokio_util::sync::CancellationToken;

use super::{CoinApiBookError, orders::OrderIndex, recovery::BookRecovery};
use crate::websocket::{
    messages::{Book, BookL3, DataType, Subscription},
    parse::parse_book_deltas,
};

/// A `book` or `book_l3` message routed to its book.
#[derive(Debug, Clone, Copy)]
pub enum BookFrame<'a> {
    Levels(&'a Book),
    Orders(&'a BookL3),
}

impl BookFrame<'_> {
    fn sequence(self) -> u64 {
        match self {
            Self::Levels(book) => book.sequence,
            Self::Orders(book) => book.sequence,
        }
    }

    fn is_snapshot(self) -> bool {
        match self {
            Self::Levels(book) => book.is_snapshot == Some(true),
            Self::Orders(book) => book.is_snapshot,
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct BookSubscription {
    pub(crate) data_type: DataType,
    pub(crate) depth: Option<usize>,
    pub(crate) active: Arc<AtomicBool>,
}

// Registration and retirement share one writer lock; market-data reads use immutable snapshots
#[derive(Debug, Default)]
pub(crate) struct BookSubscriptions {
    pub(crate) entries: AtomicMap<InstrumentId, BookSubscription>,
    pub(crate) writes: Mutex<()>,
    pub(crate) sync: BookSyncTracker,
}

impl BookSubscriptions {
    pub(crate) fn clear(&self) {
        let _write = self.writes.lock();
        self.sync.clear();
        self.entries.store(AHashMap::new());
    }

    pub(crate) fn retire(&self) {
        let _write = self.writes.lock();
        let mut entries = (**self.entries.load()).clone();
        entries.retain(|instrument_id, registration| {
            if registration.active.load(Ordering::Relaxed) {
                self.sync.remove(*instrument_id);
                false
            } else {
                true
            }
        });

        self.entries.store(entries);
    }
}

#[derive(Debug)]
struct BookState {
    subscription: Subscription,
    sync: BookSync<CoinApiBookError, u64>,
    orders: OrderIndex,
    // Added to the venue sequence of each delta emitted in the current snapshot episode
    offset: u64,
}

/// Sequence, snapshot, and recovery state of each subscribed book.
///
/// A book accepts messages only after [`Self::record_subscription`] registers it; [`Self::apply`]
/// suppresses messages for any other book.
#[derive(Debug, Clone, Default)]
pub struct BookSyncTracker {
    books: Arc<Mutex<AHashMap<InstrumentId, BookState>>>,
    // Last emitted sequence of each book, kept across subscriptions and connections
    emitted: Arc<Mutex<AHashMap<InstrumentId, u64>>>,
    // Last venue and emitted sequence of each depth stream
    depths: Arc<Mutex<AHashMap<InstrumentId, (u64, u64)>>>,
}

impl BookSyncTracker {
    /// Replaces the book with one waiting for its first snapshot behind `gate`.
    pub fn record_subscription(
        &self,
        instrument_id: InstrumentId,
        subscription: Subscription,
        now: Instant,
        gate: SnapshotGate,
    ) -> CancellationToken {
        let mut sync = BookSync::new(now);
        let cancel = sync.expect_snapshot(gate);

        let state = BookState {
            subscription,
            sync,
            orders: OrderIndex::default(),
            offset: 0,
        };

        self.books.lock().insert(instrument_id, state);
        cancel
    }

    pub(crate) fn remove(&self, instrument_id: InstrumentId) {
        self.books.lock().remove(&instrument_id);
    }

    pub(crate) fn clear(&self) {
        self.books.lock().clear();
    }

    /// Validates a message and converts it once accepted.
    ///
    /// Returns [`BookSequenceOutcome::Recover`] when the caller must start a recovery, and deltas
    /// only for an accepted message that changes the book.
    pub fn apply(
        &self,
        instrument_id: InstrumentId,
        frame: BookFrame<'_>,
        instrument: &InstrumentAny,
        now: Instant,
    ) -> (BookSequenceOutcome, Option<OrderBookDeltas>) {
        let mut books = self.books.lock();

        let Some(state) = books.get_mut(&instrument_id) else {
            return (BookSequenceOutcome::Suppress, None);
        };

        // Frames before the subscribing write belong to a scope that did not include the book
        if state.sync.is_send_pending() {
            return (BookSequenceOutcome::Suppress, None);
        }

        let sequence = frame.sequence();
        if frame.is_snapshot() {
            let converted = match frame {
                BookFrame::Levels(book) => parse_book_deltas(book, instrument).map(|d| (None, d)),
                BookFrame::Orders(book) => OrderIndex::snapshot(book, instrument)
                    .map(|(orders, deltas)| (Some(orders), Some(deltas))),
            };

            let (orders, deltas) = match converted {
                Ok(converted) => converted,
                Err(e) => return (invalid(state, instrument_id, sequence, &e), None),
            };

            if !state.sync.accept_snapshot(sequence, now) {
                return (BookSequenceOutcome::Suppress, None);
            }

            if let Some(orders) = orders {
                state.orders = orders;
            }

            // The snapshot's emitted sequence follows the book's last one, even after a restart
            let mut emitted = self.emitted.lock();
            let last = emitted.get(&instrument_id).copied().unwrap_or(0);
            state.offset = (last + 1).saturating_sub(sequence);
            emitted.insert(instrument_id, state.offset + sequence);
            return (
                BookSequenceOutcome::Accept,
                deltas.map(|deltas| offset(deltas, state.offset)),
            );
        }

        // An out-of-sync book suppresses updates while an owner restores it, and asks for one
        // otherwise
        match *state.sync.phase() {
            BookPhase::Synced(position) if position.checked_add(1) == Some(sequence) => {}
            BookPhase::Synced(_) | BookPhase::Waiting | BookPhase::Recovering => {
                return (gap(state, instrument_id, sequence), None);
            }
        }

        let converted = match frame {
            BookFrame::Levels(book) => parse_book_deltas(book, instrument),
            BookFrame::Orders(book) => state.orders.update(book, instrument),
        };

        match converted {
            Ok(deltas) => {
                state.sync.advance(sequence, now);
                self.emitted
                    .lock()
                    .insert(instrument_id, state.offset + sequence);
                (
                    BookSequenceOutcome::Accept,
                    deltas.map(|deltas| offset(deltas, state.offset)),
                )
            }
            Err(e) => (invalid(state, instrument_id, sequence, &e), None),
        }
    }

    /// Returns the emitted sequence of a depth snapshot at venue `sequence`.
    ///
    /// Each depth message lists complete top levels, so the stream advances by the venue step and
    /// continues from its last emitted sequence when CoinAPI restarts the venue sequence.
    pub fn depth_sequence(&self, instrument_id: InstrumentId, sequence: u64) -> u64 {
        let mut depths = self.depths.lock();

        let emitted = match depths.get(&instrument_id) {
            Some(&(venue, emitted)) if sequence > venue => emitted + (sequence - venue),
            Some(&(_, emitted)) => emitted + 1,
            None => sequence,
        };

        depths.insert(instrument_id, (sequence, emitted));
        emitted
    }

    /// Restarts every book after its connection reconnects, arming a snapshot `deadline` when
    /// given, and returns how many deadlines were armed.
    pub fn reset_on_reconnect(&self, deadline: Option<Instant>) -> usize {
        let mut armed = 0;

        for state in self.books.lock().values_mut() {
            state.sync.reset_on_reconnect();

            if deadline.is_some_and(|deadline| state.sync.arm_deadline(deadline)) {
                armed += 1;
            }
        }

        armed
    }

    pub(crate) fn take_expired_snapshots(&self, now: Instant) -> Vec<BookSyncSignal> {
        let mut expired = self
            .books
            .lock()
            .iter_mut()
            .filter_map(|(instrument_id, state)| {
                state.sync.take_expired(now).then_some(instrument_id)
            })
            .map(|instrument_id| BookSyncSignal {
                instrument_id: *instrument_id,
                kind: BookSyncSignalKind::SnapshotMissing,
            })
            .collect::<Vec<_>>();

        // Sort by instrument; the book map iterates in per-process hash order
        expired.sort_by_key(|signal| signal.instrument_id);
        expired
    }

    /// Claims a recovery for a subscription whose initial snapshot did not arrive, unless the
    /// subscription was replaced or removed.
    pub(crate) fn claim_subscription_recovery(
        &self,
        instrument_id: InstrumentId,
        cancel: &CancellationToken,
    ) -> Option<(Arc<BookRecovery>, Subscription)> {
        let mut books = self.books.lock();

        if cancel.is_cancelled() {
            return None;
        }

        let state = books.get_mut(&instrument_id)?;
        let recovery = state.sync.claim()?;
        Some((recovery, state.subscription))
    }

    pub(crate) fn claim_recovery(
        &self,
        instrument_id: InstrumentId,
    ) -> Option<(Arc<BookRecovery>, Subscription)> {
        let mut books = self.books.lock();
        let state = books
            .get_mut(&instrument_id)
            .filter(|state| !state.sync.is_send_pending())?;
        let recovery = state.sync.claim()?;
        Some((recovery, state.subscription))
    }
}

fn offset(mut deltas: OrderBookDeltas, offset: u64) -> OrderBookDeltas {
    for delta in &mut deltas.deltas {
        delta.sequence += offset;
    }

    deltas.sequence += offset;
    deltas
}

fn gap(state: &mut BookState, instrument_id: InstrumentId, sequence: u64) -> BookSequenceOutcome {
    let position = state.sync.position().copied();
    let outcome = state.sync.gap();

    if outcome == BookSequenceOutcome::Recover {
        log::warn!(
            "Book sequence gap for {instrument_id}: last sequence {position:?}, received \
             {sequence}; requesting a fresh snapshot"
        );
    }

    outcome
}

fn invalid(
    state: &mut BookState,
    instrument_id: InstrumentId,
    sequence: u64,
    error: &anyhow::Error,
) -> BookSequenceOutcome {
    log::warn!("Invalid book message for {instrument_id} at sequence {sequence}: {error:#}");
    state.sync.gap()
}

#[cfg(test)]
mod tests {
    use nautilus_common::live::dst::time::Duration;
    use nautilus_core::UnixNanos;
    use nautilus_model::{
        enums::BookAction,
        identifiers::InstrumentId,
        instruments::CurrencyPair,
        types::{Currency, Price, Quantity},
    };
    use rstest::rstest;
    use ustr::Ustr;

    use super::*;
    use crate::websocket::messages::{Channel, DataType};

    fn instrument_id() -> InstrumentId {
        InstrumentId::from("BTC-USD.COINBASE")
    }

    fn instrument() -> InstrumentAny {
        InstrumentAny::CurrencyPair(
            CurrencyPair::builder()
                .instrument_id(instrument_id())
                .raw_symbol("BTC-USD".into())
                .base_currency(Currency::BTC())
                .quote_currency(Currency::USD())
                .price_precision(2)
                .size_precision(8)
                .price_increment(Price::from("0.01"))
                .size_increment(Quantity::from("0.00000001"))
                .ts_event(UnixNanos::default())
                .ts_init(UnixNanos::default())
                .build()
                .unwrap(),
        )
    }

    fn subscription(data_type: DataType) -> Subscription {
        Subscription {
            channel: Channel {
                data_type,
                period_id: None,
            },
            symbol_id: Ustr::from("COINBASE_SPOT_BTC_USD"),
        }
    }

    fn levels(sequence: u64, is_snapshot: bool) -> Book {
        let mut book: Book =
            serde_json::from_str(include_str!("../../test_data/ws_book.json")).unwrap();
        book.sequence = sequence;
        book.is_snapshot = Some(is_snapshot);
        book
    }

    fn orders(sequence: u64, is_snapshot: bool) -> BookL3 {
        let mut book: BookL3 =
            serde_json::from_str(include_str!("../../test_data/ws_book_l3.json")).unwrap();
        book.sequence = sequence;
        book.is_snapshot = is_snapshot;
        book
    }

    // A tracker whose book's subscribing write has completed
    fn subscribed(data_type: DataType, now: Instant) -> BookSyncTracker {
        let tracker = BookSyncTracker::default();
        tracker.record_subscription(
            instrument_id(),
            subscription(data_type),
            now,
            SnapshotGate::default(),
        );
        tracker
    }

    fn apply(
        tracker: &BookSyncTracker,
        book: &Book,
        now: Instant,
    ) -> (BookSequenceOutcome, Option<Vec<BookAction>>) {
        let (outcome, deltas) =
            tracker.apply(instrument_id(), BookFrame::Levels(book), &instrument(), now);
        let actions = deltas.map(|deltas| deltas.deltas.iter().map(|d| d.action).collect());
        (outcome, actions)
    }

    #[rstest]
    #[case(true)]
    #[case(false)]
    fn retirement_removes_active_and_preserves_pending(#[case] active: bool) {
        let subscriptions = BookSubscriptions::default();
        subscriptions.entries.insert(
            instrument_id(),
            BookSubscription {
                data_type: DataType::Book,
                depth: None,
                active: Arc::new(AtomicBool::new(active)),
            },
        );

        let cancel = subscriptions.sync.record_subscription(
            instrument_id(),
            subscription(DataType::Book),
            Instant::now(),
            SnapshotGate::default(),
        );
        subscriptions.retire();

        assert_eq!(
            subscriptions.entries.contains_key(&instrument_id()),
            !active
        );
        assert_eq!(cancel.is_cancelled(), active);
    }

    #[rstest]
    fn retirement_keeps_replacement_registration_when_old_command_commits() {
        let subscriptions = BookSubscriptions::default();
        let old = Arc::new(AtomicBool::new(false));
        let replacement = Arc::new(AtomicBool::new(false));
        subscriptions.entries.insert(
            instrument_id(),
            BookSubscription {
                data_type: DataType::Book20,
                depth: Some(10),
                active: Arc::clone(&replacement),
            },
        );

        old.store(true, Ordering::Relaxed);
        subscriptions.retire();

        let registration = subscriptions.entries.get_cloned(&instrument_id()).unwrap();
        assert_eq!(registration.data_type, DataType::Book20);
        assert_eq!(registration.depth, Some(10));
        assert!(!registration.active.load(Ordering::Relaxed));
        assert!(Arc::ptr_eq(&registration.active, &replacement));
    }

    #[rstest]
    fn snapshot_then_linked_updates_are_accepted() {
        let now = Instant::now();
        let tracker = subscribed(DataType::Book, now);

        let snapshot = apply(&tracker, &levels(1, true), now);
        let update = apply(&tracker, &levels(2, false), now);

        assert_eq!(
            snapshot,
            (
                BookSequenceOutcome::Accept,
                Some(vec![
                    BookAction::Clear,
                    BookAction::Add,
                    BookAction::Add,
                    BookAction::Add,
                    BookAction::Add,
                ])
            )
        );
        assert_eq!(
            update,
            (
                BookSequenceOutcome::Accept,
                Some(vec![BookAction::Update; 4])
            )
        );
    }

    // A snapshot lists the whole book, so it resynchronizes at any sequence, including after a skip
    #[rstest]
    fn gap_recovers_once_claimed_and_snapshot_completes_recovery() {
        let now = Instant::now();
        let tracker = subscribed(DataType::Book, now);
        apply(&tracker, &levels(1, true), now);

        let gap = apply(&tracker, &levels(3, false), now);
        let unowned = apply(&tracker, &levels(4, false), now);
        let (recovery, claimed) = tracker.claim_recovery(instrument_id()).unwrap();
        assert!(recovery.begin_replacement());
        let owned = apply(&tracker, &levels(5, false), now);
        let during_write = apply(&tracker, &levels(1, true), now);
        recovery.gate.open();
        let restarted = apply(&tracker, &levels(1, true), now);
        let linked = apply(&tracker, &levels(2, false), now);

        assert_eq!(gap, (BookSequenceOutcome::Recover, None));
        assert_eq!(unowned, (BookSequenceOutcome::Recover, None));
        assert_eq!(claimed, subscription(DataType::Book));
        assert_eq!(owned, (BookSequenceOutcome::Suppress, None));
        assert_eq!(during_write, (BookSequenceOutcome::Suppress, None));
        assert_eq!(restarted.0, BookSequenceOutcome::Accept);
        assert!(recovery.is_accepted());
        assert_eq!(linked.0, BookSequenceOutcome::Accept);
    }

    // CoinAPI restarts sequences at one, so emitted sequences continue from the last emitted one,
    // including after the book is subscribed again
    #[rstest]
    fn emitted_sequences_keep_rising_across_restarts_and_resubscribes() {
        let now = Instant::now();
        let tracker = subscribed(DataType::Book, now);
        let emitted = |frame: &Book| {
            tracker
                .apply(
                    instrument_id(),
                    BookFrame::Levels(frame),
                    &instrument(),
                    now,
                )
                .1
                .map(|deltas| {
                    let sequences: Vec<u64> = deltas.deltas.iter().map(|d| d.sequence).collect();
                    assert!(
                        sequences
                            .iter()
                            .all(|sequence| *sequence == deltas.sequence)
                    );
                    deltas.sequence
                })
        };

        let first = emitted(&levels(1, true));
        let second = emitted(&levels(2, false));
        let doubled = emitted(&levels(2, true));
        let restarted = emitted(&levels(1, true));
        let linked = emitted(&levels(2, false));
        tracker.record_subscription(
            instrument_id(),
            subscription(DataType::Book),
            now,
            SnapshotGate::default(),
        );
        let resubscribed = emitted(&levels(1, true));

        assert_eq!(
            [first, second, doubled, restarted, linked, resubscribed],
            [Some(1), Some(2), Some(3), Some(4), Some(5), Some(6)]
        );
    }

    #[rstest]
    fn depth_sequences_keep_rising_when_the_venue_restarts() {
        let tracker = BookSyncTracker::default();
        let emitted: Vec<u64> = [7, 9, 1, 2, 5]
            .into_iter()
            .map(|sequence| tracker.depth_sequence(instrument_id(), sequence))
            .collect();

        assert_eq!(emitted, [7, 9, 10, 11, 14]);
    }

    #[rstest]
    fn update_before_snapshot_requests_recovery() {
        let now = Instant::now();
        let tracker = subscribed(DataType::Book, now);

        let result = apply(&tracker, &levels(7, false), now);

        assert_eq!(result, (BookSequenceOutcome::Recover, None));
    }

    #[rstest]
    fn frames_wait_for_subscribing_write() {
        let now = Instant::now();
        let tracker = BookSyncTracker::default();
        let gate = SnapshotGate::default();
        gate.lock().close();
        let cancel = tracker.record_subscription(
            instrument_id(),
            subscription(DataType::Book),
            now,
            gate.clone(),
        );

        let update = apply(&tracker, &levels(7, false), now);
        let snapshot = apply(&tracker, &levels(1, true), now);
        let refused = tracker.claim_recovery(instrument_id());
        gate.open();
        let accepted = apply(&tracker, &levels(1, true), now);

        assert_eq!(update, (BookSequenceOutcome::Suppress, None));
        assert_eq!(snapshot, (BookSequenceOutcome::Suppress, None));
        assert!(refused.is_none());
        assert_eq!(accepted.0, BookSequenceOutcome::Accept);
        assert!(cancel.is_cancelled());
    }

    #[rstest]
    fn invalid_messages_request_recovery_without_output() {
        let now = Instant::now();
        let tracker = subscribed(DataType::BookL3, now);
        let mut aggregated = orders(1, true);
        aggregated.bids[1].update_type = None;
        aggregated
            .asks
            .iter_mut()
            .for_each(|entry| entry.update_type = None);

        let (snapshot, snapshot_deltas) = tracker.apply(
            instrument_id(),
            BookFrame::Orders(&aggregated),
            &instrument(),
            now,
        );

        assert_eq!(snapshot, BookSequenceOutcome::Recover);
        assert_eq!(snapshot_deltas, None);
    }

    // The index follows the accepted snapshot, so a later ADD for a listed order is inconsistent
    #[rstest]
    fn l3_updates_apply_to_orders_from_snapshot() {
        let now = Instant::now();
        let tracker = subscribed(DataType::BookL3, now);
        let mut snapshot = orders(1, true);
        snapshot.bids.truncate(1);
        let mut delete = orders(2, false);
        delete.asks.remove(0);
        delete.bids.clear();
        let mut add_listed = orders(3, false);
        add_listed.asks.truncate(1);
        add_listed.bids.clear();

        let apply = |book: &BookL3| {
            let (outcome, deltas) =
                tracker.apply(instrument_id(), BookFrame::Orders(book), &instrument(), now);
            let actions: Option<Vec<BookAction>> =
                deltas.map(|deltas| deltas.deltas.iter().map(|d| d.action).collect());
            (outcome, actions)
        };

        let accepted = apply(&snapshot);
        let deleted = apply(&delete);
        let inconsistent = apply(&add_listed);

        assert_eq!(
            accepted,
            (
                BookSequenceOutcome::Accept,
                Some(vec![
                    BookAction::Clear,
                    BookAction::Add,
                    BookAction::Add,
                    BookAction::Add
                ])
            )
        );
        assert_eq!(
            deleted,
            (BookSequenceOutcome::Accept, Some(vec![BookAction::Delete]))
        );
        assert_eq!(inconsistent, (BookSequenceOutcome::Recover, None));
    }

    #[rstest]
    fn reconnect_waits_for_replayed_snapshot_until_deadline() {
        let now = Instant::now();
        let tracker = subscribed(DataType::Book, now);
        apply(&tracker, &levels(1, true), now);
        apply(&tracker, &levels(2, false), now);

        let armed = tracker.reset_on_reconnect(Some(now + Duration::from_secs(10)));
        let stale = apply(&tracker, &levels(3, false), now);
        let early = tracker.take_expired_snapshots(now + Duration::from_secs(9));
        let expired = tracker.take_expired_snapshots(now + Duration::from_secs(10));
        let unowned = apply(&tracker, &levels(3, false), now);

        assert_eq!(armed, 1);
        assert_eq!(stale, (BookSequenceOutcome::Suppress, None));
        assert!(early.is_empty());
        assert_eq!(
            expired,
            vec![BookSyncSignal {
                instrument_id: instrument_id(),
                kind: BookSyncSignalKind::SnapshotMissing,
            }]
        );
        assert_eq!(unowned, (BookSequenceOutcome::Recover, None));
    }

    #[rstest]
    fn removed_book_suppresses_frames_and_cancels_recovery() {
        let now = Instant::now();
        let tracker = subscribed(DataType::Book, now);
        let (recovery, _) = tracker.claim_recovery(instrument_id()).unwrap();

        tracker.remove(instrument_id());
        let result = apply(&tracker, &levels(1, true), now);

        assert!(recovery.cancellation.is_cancelled());
        assert_eq!(result, (BookSequenceOutcome::Suppress, None));
    }
}