shrike 0.1.5

AT Protocol library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
use std::sync::Arc;
use std::sync::atomic::{AtomicI64, Ordering};
use std::time::Duration;

use futures::StreamExt;
use futures::stream::Stream;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::http::HeaderValue;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};

use crate::streaming::StreamError;
use crate::streaming::event::Event;
use crate::streaming::jetstream::JetstreamEvent;
use crate::streaming::reconnect::BackoffPolicy;

// ---------------------------------------------------------------------------
// Internal types
// ---------------------------------------------------------------------------

type WsStream =
    futures::stream::SplitStream<WebSocketStream<MaybeTlsStream<tokio::net::TcpStream>>>;

/// Batching state shared by both firehose and Jetstream streams.
struct BatchState<E> {
    ws: Option<WsStream>,
    attempt: u32,
    batch: Vec<E>,
    pending_error: Option<StreamError>,
    deadline: Option<tokio::time::Instant>,
    /// Highest seq of a frame the verifier silently dropped (rev replay, queued
    /// resync, sync no-op) since the last cursor advance. Folded into the
    /// cursor on flush so a run of drops doesn't pin the checkpoint. Unused on
    /// the Jetstream path.
    dropped_watermark: i64,
    #[cfg(feature = "sync")]
    resync_events: Option<tokio::sync::mpsc::Receiver<crate::sync::ResyncEvent>>,
    #[cfg(feature = "sync")]
    async_errors: Option<tokio::sync::mpsc::Receiver<crate::sync::VerifierError>>,
}

impl<E> BatchState<E> {
    fn new(capacity: usize) -> Self {
        BatchState {
            ws: None,
            attempt: 0,
            batch: Vec::with_capacity(capacity),
            pending_error: None,
            deadline: None,
            dropped_watermark: 0,
            #[cfg(feature = "sync")]
            resync_events: None,
            #[cfg(feature = "sync")]
            async_errors: None,
        }
    }
}

// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------

/// Configuration for a streaming client.
///
/// Only [`url`](Config::url) is required. All other fields default to `None`,
/// which means "use the built-in default" (see each field's doc comment).
///
/// ```
/// use shrike::streaming::{Client, Config};
///
/// let _client = Client::new(Config {
///     url: "wss://bsky.network/xrpc/com.atproto.sync.subscribeRepos".into(),
///     cursor: Some(12345),
///     ..Config::default()
/// });
/// ```
#[derive(Default)]
pub struct Config {
    /// WebSocket URL (e.g., `"wss://bsky.network/xrpc/com.atproto.sync.subscribeRepos"`).
    pub url: String,
    /// Starting cursor (sequence number for firehose, `time_us` for Jetstream).
    pub cursor: Option<i64>,
    /// Backoff policy for reconnection. None uses sensible defaults
    /// (1s initial, 30s max, full jitter).
    pub backoff: Option<BackoffPolicy>,
    /// Maximum WebSocket message size. None means 2 MB.
    pub max_message_size: Option<usize>,
    /// User-Agent header for outbound WebSocket connections. None means
    /// [`crate::USER_AGENT`].
    pub user_agent: Option<String>,
    /// For Jetstream: filter by collections.
    pub collections: Option<Vec<String>>,
    /// For Jetstream: filter by DIDs.
    pub dids: Option<Vec<String>>,
    /// Maximum number of events per batch. None means 50.
    pub batch_size: Option<usize>,
    /// Maximum time to wait for a full batch before flushing. None means 500ms.
    pub batch_timeout: Option<Duration>,
    /// Optional Sync 1.1 verifier for firehose events.
    #[cfg(feature = "sync")]
    pub verifier: Option<Arc<crate::sync::Verifier>>,
    /// Verifier parallelism. The serial streaming path uses 1.
    #[cfg(feature = "sync")]
    pub parallelism: Option<usize>,
    /// Per-DID queue bound for future parallel verifier integration.
    #[cfg(feature = "sync")]
    pub per_did_queue: Option<usize>,
}

// ---------------------------------------------------------------------------
// Client
// ---------------------------------------------------------------------------

/// Client for consuming AT Protocol event streams (firehose or Jetstream).
///
/// Events are delivered in batches for efficient bulk processing. The
/// [`Config::batch_size`] and [`Config::batch_timeout`] fields control
/// batching behavior (defaults: 50 events, 500ms). Each yield from
/// [`Client::subscribe`] or [`Client::jetstream`] delivers a `Vec` of 1 to
/// `batch_size` events. Batches flush when full, when the timeout elapses,
/// or when an error (decode error, connection loss) is encountered — in
/// which case the partial batch is yielded first, followed by the error.
///
/// The WebSocket connection is established lazily when [`Client::subscribe`]
/// or [`Client::jetstream`] is called.
pub struct Client {
    url: String,
    collections: Option<Vec<String>>,
    dids: Option<Vec<String>>,
    backoff: BackoffPolicy,
    batch_size: usize,
    batch_timeout: Duration,
    user_agent: String,
    cursor: Arc<AtomicI64>,
    #[cfg(feature = "sync")]
    verifier: Option<Arc<crate::sync::Verifier>>,
    #[cfg(feature = "sync")]
    parallelism: usize,
    #[cfg(feature = "sync")]
    per_did_queue: usize,
}

impl Client {
    /// Create a new client with the given configuration.
    pub fn new(config: Config) -> Self {
        let cursor_val = config.cursor.unwrap_or(-1);
        Client {
            url: config.url,
            collections: config.collections,
            dids: config.dids,
            backoff: config.backoff.unwrap_or_default(),
            batch_size: config.batch_size.unwrap_or(50),
            batch_timeout: config.batch_timeout.unwrap_or(Duration::from_millis(500)),
            user_agent: config
                .user_agent
                .unwrap_or_else(|| crate::USER_AGENT.to_owned()),
            cursor: Arc::new(AtomicI64::new(cursor_val)),
            #[cfg(feature = "sync")]
            verifier: config.verifier,
            #[cfg(feature = "sync")]
            parallelism: config.parallelism.unwrap_or(1).max(1),
            #[cfg(feature = "sync")]
            per_did_queue: config.per_did_queue.unwrap_or(2048).max(1),
        }
    }

    /// Return the current cursor position (for checkpointing).
    ///
    /// Returns `None` if no cursor has been set or observed yet.
    pub fn cursor(&self) -> Option<i64> {
        let val = self.cursor.load(Ordering::SeqCst);
        if val < 0 { None } else { Some(val) }
    }

    /// Connect to a firehose or label stream (CBOR protocol).
    ///
    /// Returns an async stream of event batches. Events are accumulated up to
    /// [`Config::batch_size`] (default 50) or until [`Config::batch_timeout`]
    /// (default 500ms) elapses, whichever comes first. Partial batches are
    /// flushed before errors or connection loss.
    ///
    /// The stream reconnects automatically on connection failure with
    /// exponential backoff + jitter. Info and sync frames are silently
    /// skipped. All other parse/connection errors are yielded as `Err`
    /// items without terminating the stream.
    #[cfg(feature = "sync")]
    pub fn subscribe(&self) -> impl Stream<Item = Result<Vec<Event>, StreamError>> + '_ {
        // Parallel path: a verifier configured with parallelism > 1 verifies
        // different DIDs concurrently while preserving same-DID FIFO. The
        // serial path (default) is used otherwise and stays byte-for-byte
        // compatible with the no-verifier behavior.
        match self.verifier.clone() {
            Some(verifier) if self.parallelism > 1 => {
                self.subscribe_parallel(verifier).left_stream()
            }
            _ => self.subscribe_serial().right_stream(),
        }
    }

    /// Connect to a firehose or label stream (CBOR protocol).
    #[cfg(not(feature = "sync"))]
    pub fn subscribe(&self) -> impl Stream<Item = Result<Vec<Event>, StreamError>> + '_ {
        self.subscribe_serial()
    }

    fn subscribe_serial(&self) -> impl Stream<Item = Result<Vec<Event>, StreamError>> + '_ {
        let cursor = Arc::clone(&self.cursor);
        let batch_size = self.batch_size;
        let batch_timeout = self.batch_timeout;
        #[cfg(feature = "sync")]
        let verifier = self.verifier.clone();

        #[cfg_attr(not(feature = "sync"), allow(unused_mut))]
        let mut initial_state = BatchState::<Event>::new(batch_size);
        #[cfg(feature = "sync")]
        if let Some(verifier) = verifier.as_ref() {
            initial_state.resync_events = Some(verifier.resync_events());
            initial_state.async_errors = Some(verifier.async_errors());
        }

        futures::stream::unfold(initial_state, move |mut state| {
            let cursor = Arc::clone(&cursor);
            #[cfg(feature = "sync")]
            let verifier = verifier.clone();
            async move {
                // Yield any pending error from a previous partial-batch flush.
                if let Some(err) = state.pending_error.take() {
                    return Some((Err(err), state));
                }

                loop {
                    #[cfg(feature = "sync")]
                    {
                        if let Some(err) = take_async_error(&mut state) {
                            if !state.batch.is_empty() {
                                state.pending_error = Some(err);
                                state.deadline = None;
                                let batch = std::mem::take(&mut state.batch);
                                update_firehose_cursor(
                                    &cursor,
                                    &batch,
                                    &mut state.dropped_watermark,
                                );
                                return Some((Ok(batch), state));
                            }
                            state.deadline = None;
                            return Some((Err(err), state));
                        }
                        match take_resync_event(&mut state) {
                            Ok(Some(event)) => {
                                state.batch.push(event);
                                if state.batch.len() >= batch_size {
                                    state.deadline = None;
                                    let batch = std::mem::take(&mut state.batch);
                                    update_firehose_cursor(
                                        &cursor,
                                        &batch,
                                        &mut state.dropped_watermark,
                                    );
                                    return Some((Ok(batch), state));
                                }
                            }
                            Ok(None) => {}
                            Err(err) => {
                                if !state.batch.is_empty() {
                                    state.pending_error = Some(err);
                                    state.deadline = None;
                                    let batch = std::mem::take(&mut state.batch);
                                    update_firehose_cursor(
                                        &cursor,
                                        &batch,
                                        &mut state.dropped_watermark,
                                    );
                                    return Some((Ok(batch), state));
                                }
                                state.deadline = None;
                                return Some((Err(err), state));
                            }
                        }
                    }

                    // Establish a connection if we don't have one.
                    if state.ws.is_none() {
                        match connect_ws(
                            &self.url,
                            cursor.load(Ordering::SeqCst),
                            &self.collections,
                            &self.dids,
                            &self.user_agent,
                        )
                        .await
                        {
                            Ok(ws) => {
                                state.ws = Some(ws);
                                state.attempt = 0;
                            }
                            Err(e) => {
                                // Flush partial batch before yielding connection error.
                                if !state.batch.is_empty() {
                                    state.pending_error = Some(e);
                                    state.deadline = None;
                                    let batch = std::mem::take(&mut state.batch);
                                    update_firehose_cursor(
                                        &cursor,
                                        &batch,
                                        &mut state.dropped_watermark,
                                    );
                                    return Some((Ok(batch), state));
                                }
                                let delay = self.backoff.delay(state.attempt);
                                state.attempt = state.attempt.saturating_add(1);
                                tokio::time::sleep(delay).await;
                                return Some((Err(e), state));
                            }
                        }
                    }

                    let deadline = *state
                        .deadline
                        .get_or_insert_with(|| tokio::time::Instant::now() + batch_timeout);

                    // Take ws out of state to avoid borrow conflicts in select!.
                    let Some(mut ws) = state.ws.take() else {
                        continue;
                    };

                    tokio::select! {
                        msg = ws.next() => {
                            match msg {
                                Some(Ok(Message::Binary(data))) => {
                                    state.ws = Some(ws);
                                    let parsed = parse_firehose_outcome(
                                        #[cfg(feature = "sync")]
                                        verifier.as_ref(),
                                        &data,
                                    )
                                    .await;
                                    match parsed {
                                        Ok(VerifyOutcome { event: Some(event), .. }) => {
                                            state.batch.push(event);
                                            if state.batch.len() >= batch_size {
                                                state.deadline = None;
                                                let batch = std::mem::take(&mut state.batch);
                                                update_firehose_cursor(&cursor, &batch, &mut state.dropped_watermark);
                                                return Some((Ok(batch), state));
                                            }
                                        }
                                        // Silent drop (rev replay, queued resync, sync
                                        // no-op, control frame): no event to deliver, but
                                        // the seq must still advance the checkpoint.
                                        Ok(VerifyOutcome { event: None, seq }) => {
                                            state.dropped_watermark = state.dropped_watermark.max(seq);
                                            continue;
                                        }
                                        // Info/sync frames return UnknownType — skip.
                                        Err(StreamError::UnknownType(_)) => continue,
                                        Err(e) => {
                                            if !state.batch.is_empty() {
                                                state.pending_error = Some(e);
                                                state.deadline = None;
                                                let batch = std::mem::take(&mut state.batch);
                                                update_firehose_cursor(&cursor, &batch, &mut state.dropped_watermark);
                                                return Some((Ok(batch), state));
                                            }
                                            state.deadline = None;
                                            return Some((Err(e), state));
                                        }
                                    }
                                }
                                Some(Ok(Message::Close(_))) | None => {
                                    // Connection closed — flush partial batch,
                                    // then reconnect on next iteration.
                                    drop(ws);
                                    if !state.batch.is_empty() {
                                        state.deadline = None;
                                        let batch = std::mem::take(&mut state.batch);
                                        update_firehose_cursor(&cursor, &batch, &mut state.dropped_watermark);
                                        return Some((Ok(batch), state));
                                    }
                                    // No batch to flush, but advance the cursor
                                    // past any frames dropped before the close so
                                    // the reconnect resumes past them.
                                    if state.dropped_watermark > 0 {
                                        update_firehose_cursor(&cursor, &[], &mut state.dropped_watermark);
                                    }
                                    let delay = self.backoff.delay(state.attempt);
                                    state.attempt = state.attempt.saturating_add(1);
                                    tokio::time::sleep(delay).await;
                                    continue;
                                }
                                Some(Ok(_)) => {
                                    state.ws = Some(ws);
                                    continue; // ping/pong/text — skip
                                }
                                Some(Err(e)) => {
                                    // WebSocket error — flush partial batch,
                                    // then reconnect on next iteration.
                                    drop(ws);
                                    let err = StreamError::WebSocket(e.to_string());
                                    if !state.batch.is_empty() {
                                        state.pending_error = Some(err);
                                        state.deadline = None;
                                        let batch = std::mem::take(&mut state.batch);
                                        update_firehose_cursor(&cursor, &batch, &mut state.dropped_watermark);
                                        return Some((Ok(batch), state));
                                    }
                                    if state.dropped_watermark > 0 {
                                        update_firehose_cursor(&cursor, &[], &mut state.dropped_watermark);
                                    }
                                    let delay = self.backoff.delay(state.attempt);
                                    state.attempt = state.attempt.saturating_add(1);
                                    tokio::time::sleep(delay).await;
                                    return Some((Err(err), state));
                                }
                            }
                        }
                        _ = tokio::time::sleep_until(deadline) => {
                            state.ws = Some(ws);
                            if !state.batch.is_empty() {
                                state.deadline = None;
                                let batch = std::mem::take(&mut state.batch);
                                update_firehose_cursor(&cursor, &batch, &mut state.dropped_watermark);
                                return Some((Ok(batch), state));
                            }
                            // Empty batch — but if frames were silently dropped
                            // since the last flush, advance the checkpoint past
                            // them so an idle run of drops doesn't pin the cursor.
                            if state.dropped_watermark > 0 {
                                update_firehose_cursor(&cursor, &[], &mut state.dropped_watermark);
                            }
                            // Reset deadline and keep waiting.
                            state.deadline = Some(
                                tokio::time::Instant::now() + batch_timeout,
                            );
                        }
                    }
                }
            }
        })
    }

    /// Verifier-aware parallel firehose path: different DIDs verify
    /// concurrently across a worker pool while same-DID events keep FIFO order.
    /// The cursor advances by watermark (`min(inflight) - 1`), so a restart
    /// after parallel verification never skips an in-flight lower-seq event.
    #[cfg(feature = "sync")]
    fn subscribe_parallel(
        &self,
        verifier: Arc<crate::sync::Verifier>,
    ) -> impl Stream<Item = Result<Vec<Event>, StreamError>> + '_ {
        use crate::streaming::parallel::{InflightSeqs, Scheduler};

        let cursor = Arc::clone(&self.cursor);
        let batch_size = self.batch_size;
        let batch_timeout = self.batch_timeout;
        let parallelism = self.parallelism;
        let per_did_queue = self.per_did_queue;

        // Shared in-flight seq set: the dispatcher adds on submit, the collector
        // removes on result. Drives the watermark cursor.
        let inflight = Arc::new(std::sync::Mutex::new(InflightSeqs::new()));

        // Scheduler verifies each dispatched frame keyed by DID.
        let verifier_for_run = verifier.clone();
        let (scheduler, result_rx) = Scheduler::<crate::syntax::Did, ParallelJob>::new(
            parallelism,
            per_did_queue,
            batch_size.max(1) * 2,
            move |job: ParallelJob| {
                let verifier = verifier_for_run.clone();
                async move {
                    let outcome = verify_raw_event(&verifier, job.raw).await;
                    ParallelResult {
                        seq: job.seq,
                        outcome,
                    }
                }
            },
        );
        let scheduler = Arc::new(scheduler);

        // Dispatcher task: owns the websocket + reconnection, parses frames,
        // tracks inflight, and feeds the scheduler. Overflow + parse/connection
        // errors flow to the collector through `ctrl_tx`.
        let (ctrl_tx, ctrl_rx) = tokio::sync::mpsc::channel::<StreamError>(parallelism.max(1) * 4);
        let dispatch = {
            let url = self.url.clone();
            let collections = self.collections.clone();
            let dids = self.dids.clone();
            let user_agent = self.user_agent.clone();
            let backoff = self.backoff;
            let cursor = Arc::clone(&cursor);
            let inflight = Arc::clone(&inflight);
            let scheduler = Arc::clone(&scheduler);
            tokio::spawn(async move {
                dispatch_loop(
                    url,
                    collections,
                    dids,
                    user_agent,
                    backoff,
                    cursor,
                    inflight,
                    scheduler,
                    ctrl_tx,
                )
                .await;
            })
        };

        let resync_events = verifier.resync_events();
        let async_errors = verifier.async_errors();

        let state = ParallelState {
            cursor,
            batch_size,
            batch_timeout,
            inflight,
            scheduler,
            result_rx,
            ctrl_rx,
            resync_events,
            async_errors,
            dispatch: Some(dispatch),
            batch: Vec::with_capacity(batch_size),
            pending_error: None,
            deadline: None,
            _marker: std::marker::PhantomData,
        };

        futures::stream::unfold(state, move |mut state| async move {
            if let Some(err) = state.pending_error.take() {
                return Some((Err(err), state));
            }
            let deadline = *state
                .deadline
                .get_or_insert_with(|| tokio::time::Instant::now() + state.batch_timeout);

            loop {
                tokio::select! {
                    biased;

                    // Async verifier errors (resync failures, overflow, etc.).
                    Some(err) = state.async_errors.recv() => {
                        return parallel_yield_error(state, err.into());
                    }
                    // Dispatcher control errors (parse/connection/overflow).
                    Some(err) = state.ctrl_rx.recv() => {
                        return parallel_yield_error(state, err);
                    }
                    // Resync ops produced by async workers.
                    Some(event) = state.resync_events.recv() => {
                        match event_from_resync_event(event) {
                            Ok(event) => {
                                state.batch.push(event);
                                if state.batch.len() >= state.batch_size {
                                    return Some((parallel_flush(&mut state), state));
                                }
                            }
                            Err(err) => return parallel_yield_error(state, err),
                        }
                    }
                    // Verified results from the scheduler.
                    result = state.result_rx.recv() => {
                        let Some(result) = result else {
                            // Scheduler closed: dispatcher exited (consumer drop
                            // or fatal). Flush any partial batch, then end.
                            if !state.batch.is_empty() {
                                return Some((parallel_flush(&mut state), state));
                            }
                            return None;
                        };
                        lock_inflight(&state.inflight).remove(result.seq);
                        match result.outcome {
                            Ok(VerifyOutcome { event: Some(event), .. }) => {
                                state.batch.push(event);
                                if state.batch.len() >= state.batch_size {
                                    return Some((parallel_flush(&mut state), state));
                                }
                            }
                            // Silent drop: seq already removed from inflight, so
                            // the watermark advances past it on the next flush.
                            Ok(VerifyOutcome { event: None, .. }) => {}
                            Err(StreamError::UnknownType(_)) => {}
                            Err(err) => {
                                if !state.batch.is_empty() {
                                    state.pending_error = Some(err);
                                    return Some((parallel_flush(&mut state), state));
                                }
                                state.deadline = None;
                                return Some((Err(err), state));
                            }
                        }
                    }
                    _ = tokio::time::sleep_until(deadline) => {
                        if !state.batch.is_empty() {
                            return Some((parallel_flush(&mut state), state));
                        }
                        // Idle: advance the cursor to the current watermark so a
                        // run of in-flight/dropped events doesn't pin it.
                        parallel_advance_cursor(&state, &[]);
                        state.deadline =
                            Some(tokio::time::Instant::now() + state.batch_timeout);
                    }
                }
            }
        })
    }

    /// Connect to a Jetstream endpoint (JSON protocol).
    ///
    /// Returns an async stream of event batches. Batching behavior is
    /// identical to [`Client::subscribe`] — see its documentation for
    /// details on batch size, timeout, and partial-batch flushing.
    ///
    /// The stream reconnects automatically on connection failure with
    /// exponential backoff + jitter.
    pub fn jetstream(&self) -> impl Stream<Item = Result<Vec<JetstreamEvent>, StreamError>> + '_ {
        let cursor = Arc::clone(&self.cursor);
        let batch_size = self.batch_size;
        let batch_timeout = self.batch_timeout;

        futures::stream::unfold(
            BatchState::<JetstreamEvent>::new(batch_size),
            move |mut state| {
                let cursor = Arc::clone(&cursor);
                async move {
                    if let Some(err) = state.pending_error.take() {
                        return Some((Err(err), state));
                    }

                    loop {
                        if state.ws.is_none() {
                            match connect_ws(
                                &self.url,
                                cursor.load(Ordering::SeqCst),
                                &self.collections,
                                &self.dids,
                                &self.user_agent,
                            )
                            .await
                            {
                                Ok(ws) => {
                                    state.ws = Some(ws);
                                    state.attempt = 0;
                                }
                                Err(e) => {
                                    if !state.batch.is_empty() {
                                        state.pending_error = Some(e);
                                        state.deadline = None;
                                        let batch = std::mem::take(&mut state.batch);
                                        update_jetstream_cursor(&cursor, &batch);
                                        return Some((Ok(batch), state));
                                    }
                                    let delay = self.backoff.delay(state.attempt);
                                    state.attempt = state.attempt.saturating_add(1);
                                    tokio::time::sleep(delay).await;
                                    return Some((Err(e), state));
                                }
                            }
                        }

                        let deadline = *state
                            .deadline
                            .get_or_insert_with(|| tokio::time::Instant::now() + batch_timeout);

                        let Some(mut ws) = state.ws.take() else {
                            continue;
                        };

                        tokio::select! {
                            msg = ws.next() => {
                                match msg {
                                    Some(Ok(Message::Text(text))) => {
                                        state.ws = Some(ws);
                                        match crate::streaming::jetstream::parse_jetstream_message(&text) {
                                            Ok(event) => {
                                                state.batch.push(event);
                                                if state.batch.len() >= batch_size {
                                                    state.deadline = None;
                                                    let batch = std::mem::take(&mut state.batch);
                                                    update_jetstream_cursor(&cursor, &batch);
                                                    return Some((Ok(batch), state));
                                                }
                                            }
                                            Err(e) => {
                                                if !state.batch.is_empty() {
                                                    state.pending_error = Some(e);
                                                    state.deadline = None;
                                                    let batch = std::mem::take(&mut state.batch);
                                                    update_jetstream_cursor(&cursor, &batch);
                                                    return Some((Ok(batch), state));
                                                }
                                                state.deadline = None;
                                                return Some((Err(e), state));
                                            }
                                        }
                                    }
                                    Some(Ok(Message::Close(_))) | None => {
                                        drop(ws);
                                        if !state.batch.is_empty() {
                                            state.deadline = None;
                                            let batch = std::mem::take(&mut state.batch);
                                            update_jetstream_cursor(&cursor, &batch);
                                            return Some((Ok(batch), state));
                                        }
                                        let delay = self.backoff.delay(state.attempt);
                                        state.attempt = state.attempt.saturating_add(1);
                                        tokio::time::sleep(delay).await;
                                        continue;
                                    }
                                    Some(Ok(_)) => {
                                        state.ws = Some(ws);
                                        continue;
                                    }
                                    Some(Err(e)) => {
                                        drop(ws);
                                        let err = StreamError::WebSocket(e.to_string());
                                        if !state.batch.is_empty() {
                                            state.pending_error = Some(err);
                                            state.deadline = None;
                                            let batch = std::mem::take(&mut state.batch);
                                            update_jetstream_cursor(&cursor, &batch);
                                            return Some((Ok(batch), state));
                                        }
                                        let delay = self.backoff.delay(state.attempt);
                                        state.attempt = state.attempt.saturating_add(1);
                                        tokio::time::sleep(delay).await;
                                        return Some((Err(err), state));
                                    }
                                }
                            }
                            _ = tokio::time::sleep_until(deadline) => {
                                state.ws = Some(ws);
                                if !state.batch.is_empty() {
                                    state.deadline = None;
                                    let batch = std::mem::take(&mut state.batch);
                                    update_jetstream_cursor(&cursor, &batch);
                                    return Some((Ok(batch), state));
                                }
                                state.deadline = Some(
                                    tokio::time::Instant::now() + batch_timeout,
                                );
                            }
                        }
                    }
                }
            },
        )
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Build a WebSocket URL with cursor and filter query params, then connect.
async fn connect_ws(
    base_url: &str,
    cursor: i64,
    collections: &Option<Vec<String>>,
    dids: &Option<Vec<String>>,
    user_agent: &str,
) -> Result<WsStream, StreamError> {
    let mut url = url::Url::parse(base_url)
        .map_err(|e| StreamError::WebSocket(format!("invalid URL: {e}")))?;

    if cursor > 0 {
        url.query_pairs_mut()
            .append_pair("cursor", &cursor.to_string());
    }
    if let Some(cols) = collections {
        for col in cols {
            url.query_pairs_mut().append_pair("wantedCollections", col);
        }
    }
    if let Some(ds) = dids {
        for d in ds {
            url.query_pairs_mut().append_pair("wantedDids", d);
        }
    }

    let request = websocket_request(url.as_str(), user_agent)?;

    let (ws_stream, _response) = tokio_tungstenite::connect_async(request)
        .await
        .map_err(|e| StreamError::WebSocket(format!("connection failed: {e}")))?;

    let (_write, read) = ws_stream.split();
    Ok(read)
}

fn websocket_request(
    url: &str,
    user_agent: &str,
) -> Result<tokio_tungstenite::tungstenite::http::Request<()>, StreamError> {
    let mut request = url
        .into_client_request()
        .map_err(|e| StreamError::WebSocket(format!("invalid request: {e}")))?;
    let user_agent = HeaderValue::from_str(user_agent)
        .map_err(|e| StreamError::WebSocket(format!("invalid User-Agent header: {e}")))?;
    request.headers_mut().insert(
        tokio_tungstenite::tungstenite::http::header::USER_AGENT,
        user_agent,
    );
    Ok(request)
}

#[cfg(feature = "sync")]
fn take_async_error(state: &mut BatchState<Event>) -> Option<StreamError> {
    let rx = state.async_errors.as_mut()?;
    match rx.try_recv() {
        Ok(err) => Some(err.into()),
        Err(tokio::sync::mpsc::error::TryRecvError::Empty) => None,
        Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => {
            state.async_errors = None;
            None
        }
    }
}

#[cfg(feature = "sync")]
fn take_resync_event(state: &mut BatchState<Event>) -> Result<Option<Event>, StreamError> {
    let Some(rx) = state.resync_events.as_mut() else {
        return Ok(None);
    };
    match rx.try_recv() {
        Ok(event) => event_from_resync_event(event).map(Some),
        Err(tokio::sync::mpsc::error::TryRecvError::Empty) => Ok(None),
        Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => {
            state.resync_events = None;
            Ok(None)
        }
    }
}

/// Outcome of handling one firehose frame.
///
/// `seq` is the frame's firehose sequence number (0 for control frames that
/// carry none). It is reported even when `event` is `None` — a silent drop
/// (rev replay, queued resync, sync no-op) must still advance the cursor
/// watermark, otherwise a long run of drops would pin the checkpoint and force
/// the whole run to be reprocessed after a restart.
struct VerifyOutcome {
    event: Option<Event>,
    seq: i64,
}

/// Parse (and, when a verifier is configured, verify) a single firehose frame
/// into a [`VerifyOutcome`]. Without the `sync` feature, or with no verifier,
/// this is a plain parse that always yields the parsed event.
async fn parse_firehose_outcome(
    #[cfg(feature = "sync")] verifier: Option<&Arc<crate::sync::Verifier>>,
    data: &[u8],
) -> Result<VerifyOutcome, StreamError> {
    #[cfg(feature = "sync")]
    if let Some(verifier) = verifier {
        return verify_firehose_frame(verifier, data).await;
    }
    let event = crate::streaming::parse_firehose_frame(data)?;
    let seq = event_seq(&event);
    Ok(VerifyOutcome {
        event: Some(event),
        seq,
    })
}

#[cfg(feature = "sync")]
async fn verify_firehose_frame(
    verifier: &crate::sync::Verifier,
    data: &[u8],
) -> Result<VerifyOutcome, StreamError> {
    let raw = crate::streaming::parse_raw_sync_frame(data)
        .map_err(|err| StreamError::ParseCbor(err.to_string()))?;
    verify_raw_event(verifier, raw).await
}

/// The DID a raw event serializes on. Events for the same DID must verify in
/// arrival order (chain-state advances race otherwise); `None` events (control
/// frames) have no ordering constraint.
#[cfg(feature = "sync")]
fn frame_key(raw: &crate::sync::RawSyncEvent) -> Option<crate::syntax::Did> {
    match raw {
        crate::sync::RawSyncEvent::Commit(c) => Some(c.repo.clone()),
        crate::sync::RawSyncEvent::Sync(s) => Some(s.did.clone()),
        crate::sync::RawSyncEvent::Account(a) => Some(a.did.clone()),
        crate::sync::RawSyncEvent::Identity(i) => Some(i.did.clone()),
        crate::sync::RawSyncEvent::Info => None,
    }
}

#[cfg(feature = "sync")]
async fn verify_raw_event(
    verifier: &crate::sync::Verifier,
    raw: crate::sync::RawSyncEvent,
) -> Result<VerifyOutcome, StreamError> {
    match raw {
        crate::sync::RawSyncEvent::Commit(raw) => {
            let did = raw.repo.clone();
            let rev = raw.rev;
            let seq = raw.seq;
            let event = verifier
                .verify_commit(&raw)
                .await?
                .map(|ops| event_from_verifier_ops(did, rev, seq, ops))
                .transpose()?;
            Ok(VerifyOutcome { event, seq })
        }
        crate::sync::RawSyncEvent::Sync(raw) => {
            let did = raw.did.clone();
            let rev = crate::syntax::Tid::try_from(raw.rev.as_str())
                .map_err(|err| StreamError::ParseCbor(format!("invalid sync rev: {err}")))?;
            let seq = raw.seq;
            let event = verifier
                .verify_sync(&raw)
                .await?
                .map(|ops| event_from_verifier_ops(did, rev, seq, ops))
                .transpose()?;
            Ok(VerifyOutcome { event, seq })
        }
        crate::sync::RawSyncEvent::Account(raw) => {
            // A persistence failure must not suppress delivery of the account
            // event: the consumer still needs to see the hosting-status change.
            // Surface the bookkeeping error on the async-error channel instead
            // of dropping the event.
            if let Err(err) = verifier.on_account_event(&raw).await {
                verifier.report_async_error(err).await;
            }
            let seq = raw.seq;
            Ok(VerifyOutcome {
                event: Some(Event::Account {
                    did: raw.did,
                    seq,
                    active: raw.active,
                }),
                seq,
            })
        }
        crate::sync::RawSyncEvent::Identity(raw) => {
            let seq = raw.seq;
            Ok(VerifyOutcome {
                event: Some(Event::Identity {
                    did: raw.did,
                    seq,
                    handle: raw.handle,
                }),
                seq,
            })
        }
        crate::sync::RawSyncEvent::Info => Ok(VerifyOutcome {
            event: None,
            seq: 0,
        }),
    }
}

#[cfg(feature = "sync")]
fn event_from_resync_event(event: crate::sync::ResyncEvent) -> Result<Event, StreamError> {
    let rev = crate::syntax::Tid::try_from(event.new_rev.as_str())
        .map_err(|err| StreamError::ParseCbor(format!("invalid resync rev: {err}")))?;
    event_from_verifier_ops(event.did, rev, 0, event.ops)
}

// ---------------------------------------------------------------------------
// Parallel firehose path
// ---------------------------------------------------------------------------

/// A frame dispatched to the parallel verify scheduler.
#[cfg(feature = "sync")]
struct ParallelJob {
    raw: crate::sync::RawSyncEvent,
    seq: i64,
}

/// A scheduler result: the verify outcome plus the frame's seq (so the
/// collector can release it from the in-flight watermark set).
#[cfg(feature = "sync")]
struct ParallelResult {
    seq: i64,
    outcome: Result<VerifyOutcome, StreamError>,
}

#[cfg(feature = "sync")]
type SharedInflight = Arc<std::sync::Mutex<crate::streaming::parallel::InflightSeqs>>;

/// Lock the in-flight set, recovering the guard if a holder panicked. The set
/// is plain bookkeeping (sorted seqs); a poisoned lock carries no torn
/// invariant, so recovering is safe and preferable to propagating a panic into
/// the firehose loop.
#[cfg(feature = "sync")]
fn lock_inflight(
    inflight: &std::sync::Mutex<crate::streaming::parallel::InflightSeqs>,
) -> std::sync::MutexGuard<'_, crate::streaming::parallel::InflightSeqs> {
    inflight
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

#[cfg(feature = "sync")]
struct ParallelState<'a> {
    cursor: Arc<AtomicI64>,
    batch_size: usize,
    batch_timeout: Duration,
    inflight: SharedInflight,
    #[allow(dead_code)]
    scheduler: Arc<crate::streaming::parallel::Scheduler<crate::syntax::Did, ParallelJob>>,
    result_rx: tokio::sync::mpsc::Receiver<ParallelResult>,
    ctrl_rx: tokio::sync::mpsc::Receiver<StreamError>,
    resync_events: tokio::sync::mpsc::Receiver<crate::sync::ResyncEvent>,
    async_errors: tokio::sync::mpsc::Receiver<crate::sync::VerifierError>,
    dispatch: Option<tokio::task::JoinHandle<()>>,
    batch: Vec<Event>,
    pending_error: Option<StreamError>,
    deadline: Option<tokio::time::Instant>,
    _marker: std::marker::PhantomData<&'a ()>,
}

#[cfg(feature = "sync")]
impl Drop for ParallelState<'_> {
    fn drop(&mut self) {
        if let Some(handle) = self.dispatch.take() {
            handle.abort();
        }
    }
}

/// Advance the cursor to the parallel watermark: the highest seq guaranteed
/// fully processed. With work in flight that is `min(inflight) - 1`; when the
/// in-flight set is empty it is the highest seq in the just-flushed batch.
/// Monotonic — never rewinds a consumer's checkpoint.
#[cfg(feature = "sync")]
fn parallel_advance_cursor(state: &ParallelState<'_>, batch: &[Event]) {
    let watermark = lock_inflight(&state.inflight).min();
    let target = if watermark > 0 {
        watermark - 1
    } else {
        batch
            .iter()
            .map(event_seq)
            .filter(|&s| s > 0)
            .max()
            .unwrap_or(0)
    };
    if target > 0 {
        state.cursor.fetch_max(target, Ordering::SeqCst);
    }
}

/// Take the current batch, advance the cursor to the watermark, and return it.
#[cfg(feature = "sync")]
fn parallel_flush(state: &mut ParallelState<'_>) -> Result<Vec<Event>, StreamError> {
    state.deadline = None;
    let batch = std::mem::take(&mut state.batch);
    parallel_advance_cursor(state, &batch);
    Ok(batch)
}

/// Flush any partial batch before an error (stashing the error to yield next),
/// or yield the error directly when the batch is empty.
#[cfg(feature = "sync")]
fn parallel_yield_error(
    mut state: ParallelState<'_>,
    err: StreamError,
) -> Option<(Result<Vec<Event>, StreamError>, ParallelState<'_>)> {
    if !state.batch.is_empty() {
        state.pending_error = Some(err);
        let batch = parallel_flush(&mut state);
        return Some((batch, state));
    }
    state.deadline = None;
    Some((Err(err), state))
}

/// The parallel dispatcher: reads frames, parses them to extract the per-DID
/// key and seq, tracks in-flight seqs, and feeds the scheduler. Reconnects with
/// backoff. Control frames and parse/connection errors flow to `ctrl_tx`;
/// queue overflow drops the oldest in-flight unit and reports it.
#[cfg(feature = "sync")]
#[allow(clippy::too_many_arguments)]
async fn dispatch_loop(
    url: String,
    collections: Option<Vec<String>>,
    dids: Option<Vec<String>>,
    user_agent: String,
    backoff: BackoffPolicy,
    cursor: Arc<AtomicI64>,
    inflight: SharedInflight,
    scheduler: Arc<crate::streaming::parallel::Scheduler<crate::syntax::Did, ParallelJob>>,
    ctrl_tx: tokio::sync::mpsc::Sender<StreamError>,
) {
    use crate::streaming::parallel::AddOutcome;

    let mut attempt: u32 = 0;
    loop {
        let mut ws = match connect_ws(
            &url,
            cursor.load(Ordering::SeqCst),
            &collections,
            &dids,
            &user_agent,
        )
        .await
        {
            Ok(ws) => {
                attempt = 0;
                ws
            }
            Err(e) => {
                if ctrl_tx.send(e).await.is_err() {
                    return;
                }
                let delay = backoff.delay(attempt);
                attempt = attempt.saturating_add(1);
                tokio::time::sleep(delay).await;
                continue;
            }
        };

        loop {
            match ws.next().await {
                Some(Ok(Message::Binary(data))) => {
                    let raw = match crate::streaming::parse_raw_sync_frame(&data) {
                        Ok(raw) => raw,
                        Err(err) => {
                            // Mirror serial behavior: info/sync frames surface
                            // as UnknownType and are skipped by the collector.
                            if ctrl_tx
                                .send(StreamError::ParseCbor(err.to_string()))
                                .await
                                .is_err()
                            {
                                return;
                            }
                            continue;
                        }
                    };
                    let seq = raw_event_seq(&raw);
                    let Some(key) = frame_key(&raw) else {
                        // Control frame (#info): nothing to verify or order.
                        continue;
                    };
                    // Reserve the seq before dispatch so the watermark cannot
                    // advance past an event still queued for verification.
                    lock_inflight(&inflight).add(seq);
                    match scheduler
                        .add_work(key.clone(), ParallelJob { raw, seq })
                        .await
                    {
                        AddOutcome::Queued => {}
                        AddOutcome::Dropped(job) => {
                            lock_inflight(&inflight).remove(job.seq);
                            let err = StreamError::QueueOverflow {
                                did: key.as_str().to_owned(),
                                seq: job.seq,
                            };
                            if ctrl_tx.send(err).await.is_err() {
                                return;
                            }
                        }
                        AddOutcome::ShuttingDown(job) => {
                            lock_inflight(&inflight).remove(job.seq);
                            return;
                        }
                    }
                }
                Some(Ok(Message::Close(_))) | None => {
                    let delay = backoff.delay(attempt);
                    attempt = attempt.saturating_add(1);
                    tokio::time::sleep(delay).await;
                    break;
                }
                Some(Ok(_)) => continue, // ping/pong/text
                Some(Err(e)) => {
                    if ctrl_tx
                        .send(StreamError::WebSocket(e.to_string()))
                        .await
                        .is_err()
                    {
                        return;
                    }
                    let delay = backoff.delay(attempt);
                    attempt = attempt.saturating_add(1);
                    tokio::time::sleep(delay).await;
                    break;
                }
            }
        }
    }
}

/// The firehose seq carried by a raw event (0 for control frames).
#[cfg(feature = "sync")]
fn raw_event_seq(raw: &crate::sync::RawSyncEvent) -> i64 {
    match raw {
        crate::sync::RawSyncEvent::Commit(c) => c.seq,
        crate::sync::RawSyncEvent::Sync(s) => s.seq,
        crate::sync::RawSyncEvent::Account(a) => a.seq,
        crate::sync::RawSyncEvent::Identity(i) => i.seq,
        crate::sync::RawSyncEvent::Info => 0,
    }
}

#[cfg(feature = "sync")]
fn event_from_verifier_ops(
    did: crate::syntax::Did,
    rev: crate::syntax::Tid,
    seq: i64,
    ops: Vec<crate::sync::VerifierOp>,
) -> Result<Event, StreamError> {
    let operations = ops
        .into_iter()
        .map(operation_from_verifier_op)
        .collect::<Result<Vec<_>, _>>()?;
    Ok(Event::Commit {
        did,
        rev,
        seq,
        operations,
    })
}

#[cfg(feature = "sync")]
fn operation_from_verifier_op(
    op: crate::sync::VerifierOp,
) -> Result<crate::streaming::Operation, StreamError> {
    use crate::streaming::Operation;
    use crate::syntax::{Nsid, RecordKey};

    let (collection, rkey) = op
        .path
        .split_once('/')
        .ok_or_else(|| StreamError::ParseCbor(format!("op path missing '/': {:?}", op.path)))?;
    let collection = Nsid::try_from(collection)
        .map_err(|err| StreamError::ParseCbor(format!("invalid op collection: {err}")))?;
    let rkey = RecordKey::try_from(rkey)
        .map_err(|err| StreamError::ParseCbor(format!("invalid op rkey: {err}")))?;

    match op.action.as_str() {
        "create" => Ok(Operation::Create {
            collection,
            rkey,
            cid: op
                .cid
                .ok_or_else(|| StreamError::ParseCbor("create op missing cid".to_owned()))?,
            record: op.record,
        }),
        "update" => Ok(Operation::Update {
            collection,
            rkey,
            cid: op
                .cid
                .ok_or_else(|| StreamError::ParseCbor("update op missing cid".to_owned()))?,
            record: op.record,
        }),
        "delete" => Ok(Operation::Delete { collection, rkey }),
        "resync" => Ok(Operation::Resync {
            collection,
            rkey,
            cid: op.cid,
            record: op.record,
        }),
        action => Err(StreamError::ParseCbor(format!(
            "unknown verified op action: {action}"
        ))),
    }
}

pub(crate) fn event_seq(event: &Event) -> i64 {
    match event {
        Event::Commit { seq, .. }
        | Event::Identity { seq, .. }
        | Event::Account { seq, .. }
        | Event::Labels { seq, .. } => *seq,
    }
}

pub(crate) fn jetstream_time_us(event: &JetstreamEvent) -> i64 {
    match event {
        JetstreamEvent::Commit { time_us, .. }
        | JetstreamEvent::Identity { time_us, .. }
        | JetstreamEvent::Account { time_us, .. } => *time_us,
    }
}

/// Advance the firehose cursor to the highest seq processed so far, then clear
/// the silent-drop watermark.
///
/// `watermark` is the high-water mark of frames the verifier silently dropped
/// (which never reach the batch but must still advance the checkpoint). The
/// cursor moves to the max of the highest delivered event seq, the watermark,
/// and the current value — it never moves backward, so a stale resync event
/// (seq 0) or an out-of-order flush cannot rewind a consumer's checkpoint.
fn update_firehose_cursor(cursor: &AtomicI64, batch: &[Event], watermark: &mut i64) {
    let batch_max = batch
        .iter()
        .map(event_seq)
        .filter(|&s| s > 0)
        .max()
        .unwrap_or(0);
    let target = batch_max.max(*watermark);
    *watermark = 0;
    if target > 0 {
        cursor.fetch_max(target, Ordering::SeqCst);
    }
}

/// Update the cursor from the last event in a Jetstream batch.
fn update_jetstream_cursor(cursor: &AtomicI64, batch: &[JetstreamEvent]) {
    if let Some(t) = batch.iter().rev().map(jetstream_time_us).find(|&t| t > 0) {
        cursor.store(t, Ordering::SeqCst);
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::unreachable
)]
mod tests {
    use super::*;

    #[test]
    fn config_defaults() {
        let cfg = Config::default();
        assert!(cfg.url.is_empty());
        assert!(cfg.cursor.is_none());
        assert!(cfg.user_agent.is_none());
        assert!(cfg.max_message_size.is_none());
        assert!(cfg.batch_size.is_none());
        assert!(cfg.batch_timeout.is_none());
        assert!(cfg.backoff.is_none());
        assert!(cfg.collections.is_none());
        assert!(cfg.dids.is_none());
    }

    #[test]
    fn config_struct_literal() {
        let cfg = Config {
            url: "wss://example.com".into(),
            cursor: Some(12345),
            batch_size: Some(100),
            batch_timeout: Some(Duration::from_secs(2)),
            collections: Some(vec!["app.bsky.feed.post".into()]),
            dids: Some(vec!["did:plc:test123456789abcdefghij".into()]),
            user_agent: Some("my-stream-consumer/1.0".into()),
            ..Config::default()
        };
        assert_eq!(cfg.url, "wss://example.com");
        assert_eq!(cfg.cursor, Some(12345));
        assert_eq!(cfg.batch_size, Some(100));
        assert_eq!(cfg.batch_timeout, Some(Duration::from_secs(2)));
        assert_eq!(cfg.collections.as_ref().unwrap().len(), 1);
        assert_eq!(cfg.dids.as_ref().unwrap().len(), 1);
        assert_eq!(cfg.user_agent.as_deref(), Some("my-stream-consumer/1.0"));
    }

    #[test]
    fn client_resolves_defaults() {
        let client = Client::new(Config {
            url: "wss://example.com".into(),
            ..Config::default()
        });
        assert_eq!(client.cursor(), None);
        assert_eq!(client.user_agent, crate::USER_AGENT);
        assert_eq!(client.batch_size, 50);
        assert_eq!(client.batch_timeout, Duration::from_millis(500));
    }

    #[test]
    fn client_overrides_user_agent() {
        let client = Client::new(Config {
            url: "wss://example.com".into(),
            user_agent: Some("custom-client/2.3".into()),
            ..Config::default()
        });
        assert_eq!(client.user_agent, "custom-client/2.3");
    }

    #[test]
    fn client_cursor_from_config() {
        let client = Client::new(Config {
            url: "wss://example.com".into(),
            cursor: Some(42),
            ..Config::default()
        });
        assert_eq!(client.cursor(), Some(42));
    }

    #[test]
    fn client_overrides_batch_size() {
        let client = Client::new(Config {
            url: "wss://example.com".into(),
            batch_size: Some(200),
            ..Config::default()
        });
        assert_eq!(client.batch_size, 200);
    }

    #[test]
    fn event_seq_extraction() {
        let event = Event::Commit {
            did: crate::syntax::Did::default(),
            rev: crate::syntax::Tid::new(0, 0),
            seq: 999,
            operations: vec![],
        };
        assert_eq!(event_seq(&event), 999);
    }

    #[test]
    fn event_seq_identity() {
        let event = Event::Identity {
            did: crate::syntax::Did::default(),
            seq: 123,
            handle: None,
        };
        assert_eq!(event_seq(&event), 123);
    }

    #[test]
    fn event_seq_account() {
        let event = Event::Account {
            did: crate::syntax::Did::default(),
            seq: 456,
            active: true,
        };
        assert_eq!(event_seq(&event), 456);
    }

    #[test]
    fn event_seq_labels() {
        let event = Event::Labels {
            seq: 789,
            labels: vec![],
        };
        assert_eq!(event_seq(&event), 789);
    }

    #[test]
    fn jetstream_time_us_extraction() {
        let event = JetstreamEvent::Identity {
            did: crate::syntax::Did::default(),
            time_us: 1_700_000_000_000_000,
        };
        assert_eq!(jetstream_time_us(&event), 1_700_000_000_000_000);
    }

    #[test]
    fn jetstream_time_us_commit() {
        let event = JetstreamEvent::Commit {
            did: crate::syntax::Did::default(),
            time_us: 42,
            collection: crate::syntax::Nsid::default(),
            rkey: crate::syntax::RecordKey::default(),
            operation: crate::streaming::jetstream::JetstreamCommit::Delete,
        };
        assert_eq!(jetstream_time_us(&event), 42);
    }

    #[test]
    fn jetstream_time_us_account() {
        let event = JetstreamEvent::Account {
            did: crate::syntax::Did::default(),
            time_us: 99,
            active: false,
        };
        assert_eq!(jetstream_time_us(&event), 99);
    }

    #[test]
    fn update_firehose_cursor_finds_last_seq() {
        let cursor = AtomicI64::new(-1);
        let batch = vec![
            Event::Identity {
                did: crate::syntax::Did::default(),
                seq: 10,
                handle: None,
            },
            Event::Identity {
                did: crate::syntax::Did::default(),
                seq: 20,
                handle: None,
            },
        ];
        let mut watermark = 0;
        update_firehose_cursor(&cursor, &batch, &mut watermark);
        assert_eq!(cursor.load(Ordering::SeqCst), 20);
    }

    #[test]
    fn update_firehose_cursor_advances_past_silent_drops() {
        // A run of silently-dropped frames (empty batch, high watermark) must
        // still advance the checkpoint, and the watermark is cleared after.
        let cursor = AtomicI64::new(5);
        let mut watermark = 42;
        update_firehose_cursor(&cursor, &[], &mut watermark);
        assert_eq!(cursor.load(Ordering::SeqCst), 42);
        assert_eq!(watermark, 0);
    }

    #[test]
    fn update_firehose_cursor_never_moves_backward() {
        // Already ahead of both the batch and the watermark: no rewind. Guards
        // against stale resync events (seq 0) or out-of-order flushes.
        let cursor = AtomicI64::new(100);
        let batch = vec![Event::Identity {
            did: crate::syntax::Did::default(),
            seq: 20,
            handle: None,
        }];
        let mut watermark = 10;
        update_firehose_cursor(&cursor, &batch, &mut watermark);
        assert_eq!(cursor.load(Ordering::SeqCst), 100);
    }

    #[test]
    fn update_jetstream_cursor_finds_last_time_us() {
        let cursor = AtomicI64::new(-1);
        let batch = vec![
            JetstreamEvent::Identity {
                did: crate::syntax::Did::default(),
                time_us: 100,
            },
            JetstreamEvent::Identity {
                did: crate::syntax::Did::default(),
                time_us: 200,
            },
        ];
        update_jetstream_cursor(&cursor, &batch);
        assert_eq!(cursor.load(Ordering::SeqCst), 200);
    }

    #[test]
    fn websocket_request_sets_shrike_user_agent() {
        let request = websocket_request(
            "wss://example.com/xrpc/com.atproto.sync.subscribeRepos",
            crate::USER_AGENT,
        )
        .unwrap();
        assert_eq!(
            request
                .headers()
                .get(tokio_tungstenite::tungstenite::http::header::USER_AGENT)
                .and_then(|value| value.to_str().ok()),
            Some(crate::USER_AGENT)
        );
    }

    #[test]
    fn websocket_request_sets_custom_user_agent() {
        let request = websocket_request(
            "wss://example.com/xrpc/com.atproto.sync.subscribeRepos",
            "custom-client/2.3",
        )
        .unwrap();
        assert_eq!(
            request
                .headers()
                .get(tokio_tungstenite::tungstenite::http::header::USER_AGENT)
                .and_then(|value| value.to_str().ok()),
            Some("custom-client/2.3")
        );
    }

    #[test]
    fn websocket_request_rejects_invalid_user_agent() {
        let err = websocket_request(
            "wss://example.com/xrpc/com.atproto.sync.subscribeRepos",
            "bad\nagent",
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("invalid User-Agent"),
            "unexpected error: {err}"
        );
    }
}