eventuary-core 0.2.0

Core event model and async IO traits for eventuary
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
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
//! CheckpointReader: composes an inner reader and a checkpoint store.
//!
//! On `read`, loads all persisted cursors for the requested scope
//! (one per `CursorId`) and hands them to the inner subscription via
//! `StartableSubscription::with_starts` as a `Vec<StartFrom::After(c)>`.
//! The inner reader decides which row to resume from — the default
//! impl picks the smallest `After(c)`; wrappers like `PartitionedReader`
//! filter by compatibility first. Records each delivered cursor
//! in contiguous delivered order per `CursorId`. On downstream `ack`,
//! calls the inner ack first, then buffers the cursor progress and
//! commits to the store according to the configured
//! [`CheckpointFlushPolicy`] (immediate flush by default). Store commit
//! errors propagate to the caller and to consumers of the stream.
//!
//! When `max_pending_interval > 0`, a background timer task flushes
//! expired entries even if no further acks arrive. The returned
//! [`CheckpointStream`] also exposes [`CheckpointStream::flush`] for
//! explicit on-demand drain, which graceful-shutdown paths should call
//! before dropping the stream.
//!
//! The checkpoint store persists the same cursor type the inner reader
//! emits. `CheckpointReader` uses `Cursor::id()` only to build the
//! checkpoint key; it does not transform the cursor before storing or
//! resuming. For partitioned composition (`PartitionedReader<R>`), the
//! persisted value is `PartitionedCursor<R::Cursor>`.

use std::collections::HashMap;
use std::collections::VecDeque;
use std::future::Future;
use std::num::NonZeroUsize;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;

use futures::Stream;
use futures::StreamExt;
use tokio::sync::Mutex;
use tokio::sync::Notify;
use tokio::sync::mpsc;
use tokio::time::Instant;

use crate::error::{Error, Result};
use crate::io::ConsumerGroupId;
use crate::io::acker::NackContext;
use crate::io::position::{StartFrom, StartableSubscription};
use crate::io::stream_id::StreamId;
use crate::io::{Acker, Cursor, CursorId, Message, Reader};

pub trait CheckpointStore<C>: Clone + Send + Sync + 'static {
    fn load<'a>(
        &'a self,
        key: &'a CheckpointKey,
    ) -> impl Future<Output = Result<Option<C>>> + Send + 'a;

    fn load_scope<'a>(
        &'a self,
        scope: &'a CheckpointScope,
    ) -> impl Future<Output = Result<Vec<(CursorId, C)>>> + Send + 'a;

    fn commit<'a>(
        &'a self,
        key: &'a CheckpointKey,
        cursor: C,
    ) -> impl Future<Output = Result<()>> + Send + 'a;
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct CheckpointScope {
    pub consumer_group_id: ConsumerGroupId,
    pub stream_id: StreamId,
}

impl CheckpointScope {
    pub fn new(consumer_group_id: ConsumerGroupId, stream_id: StreamId) -> Self {
        Self {
            consumer_group_id,
            stream_id,
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct CheckpointKey {
    pub scope: CheckpointScope,
    pub cursor_id: CursorId,
}

impl CheckpointKey {
    pub fn new(scope: CheckpointScope, cursor_id: CursorId) -> Self {
        Self { scope, cursor_id }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub enum MissingCheckpointPolicy {
    #[default]
    UseInitialStart,
    Error,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub enum InvalidCursorPolicy {
    #[default]
    UseInitialStart,
    Error,
}

/// Buffering policy for per-CursorId checkpoint flushes.
///
/// `CheckpointReader` collapses repeated `store.commit(...)` calls per
/// `CursorId` into a single flush, controlled by either a count or a time
/// threshold. The default policy (`max_pending_acks = 1`,
/// `max_pending_interval = ZERO`) flushes on every ack, matching the
/// historical behavior.
///
/// Events acked between flushes will be redelivered on crash. Handlers
/// must already be idempotent for redelivery.
#[derive(Debug, Clone, Copy)]
pub struct CheckpointFlushPolicy {
    pub max_pending_acks: NonZeroUsize,
    pub max_pending_interval: Duration,
}

impl Default for CheckpointFlushPolicy {
    fn default() -> Self {
        Self {
            max_pending_acks: NonZeroUsize::new(1).unwrap(),
            max_pending_interval: Duration::ZERO,
        }
    }
}

pub struct CheckpointReaderConfig {
    pub max_pending_per_key: usize,
    pub flush_policy: CheckpointFlushPolicy,
}

impl Default for CheckpointReaderConfig {
    fn default() -> Self {
        Self {
            max_pending_per_key: 1024,
            flush_policy: CheckpointFlushPolicy::default(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct CheckpointSubscription<S> {
    pub inner: S,
    pub scope: CheckpointScope,
    pub on_missing: MissingCheckpointPolicy,
    pub on_invalid: InvalidCursorPolicy,
}

impl<S> CheckpointSubscription<S> {
    pub fn new(inner: S, scope: CheckpointScope) -> Self {
        Self {
            inner,
            scope,
            on_missing: MissingCheckpointPolicy::UseInitialStart,
            on_invalid: InvalidCursorPolicy::UseInitialStart,
        }
    }

    pub fn on_missing(mut self, policy: MissingCheckpointPolicy) -> Self {
        self.on_missing = policy;
        self
    }

    pub fn on_invalid(mut self, policy: InvalidCursorPolicy) -> Self {
        self.on_invalid = policy;
        self
    }
}

struct Pending<C> {
    cursor: C,
    completed: bool,
}

/// Per-`CursorId` state combining contiguous-ack tracking and flush
/// accounting under a single lock.
struct PerCursorState<C> {
    queue: VecDeque<Pending<C>>,
    offset: usize,
    pending_cursor: Option<C>,
    pending_count: usize,
    first_pending_at: Option<Instant>,
}

impl<C: Clone> PerCursorState<C> {
    fn new() -> Self {
        Self {
            queue: VecDeque::new(),
            offset: 0,
            pending_cursor: None,
            pending_count: 0,
            first_pending_at: None,
        }
    }

    fn record(&mut self, cursor: C) -> usize {
        let idx = self.offset + self.queue.len();
        self.queue.push_back(Pending {
            cursor,
            completed: false,
        });
        idx
    }

    fn complete(&mut self, idx: usize) -> Option<C> {
        let adjusted = idx.wrapping_sub(self.offset);
        self.queue[adjusted].completed = true;
        let mut latest: Option<C> = None;
        while self.queue.front().map(|p| p.completed).unwrap_or(false) {
            latest = Some(self.queue.pop_front().unwrap().cursor);
            self.offset += 1;
        }
        latest
    }

    fn pending_queue_count(&self) -> usize {
        self.queue.len()
    }

    /// Advance the buffered flush cursor and return it if a flush
    /// threshold has been crossed. Caller should commit the returned
    /// cursor outside the lock.
    fn advance_and_check_flush(
        &mut self,
        cursor: C,
        policy: &CheckpointFlushPolicy,
        now: Instant,
    ) -> Option<C> {
        if self.first_pending_at.is_none() {
            self.first_pending_at = Some(now);
        }
        self.pending_cursor = Some(cursor);
        self.pending_count += 1;

        let count_hit = self.pending_count >= policy.max_pending_acks.get();
        let interval_hit = !policy.max_pending_interval.is_zero()
            && self
                .first_pending_at
                .map(|t| now.duration_since(t) >= policy.max_pending_interval)
                .unwrap_or(false);

        if count_hit || interval_hit {
            self.take_pending()
        } else {
            None
        }
    }

    /// Take the buffered flush cursor and reset flush accounting.
    fn take_pending(&mut self) -> Option<C> {
        self.pending_count = 0;
        self.first_pending_at = None;
        self.pending_cursor.take()
    }

    fn is_empty(&self) -> bool {
        self.queue.is_empty() && self.pending_cursor.is_none()
    }

    fn flush_deadline(&self, policy: &CheckpointFlushPolicy) -> Option<Instant> {
        if policy.max_pending_interval.is_zero() {
            return None;
        }
        self.first_pending_at
            .map(|t| t + policy.max_pending_interval)
    }
}

type StateMap<C> = Arc<Mutex<HashMap<CursorId, PerCursorState<C>>>>;

/// Notifies the background flush timer that pending state changed
/// (new entry, threshold crossing, drop). Atomic flag avoids busy-wait
/// when the timer is parked between deadlines.
#[derive(Clone)]
struct FlushSignal {
    notify: Arc<Notify>,
}

impl FlushSignal {
    fn new() -> Self {
        Self {
            notify: Arc::new(Notify::new()),
        }
    }

    fn wake(&self) {
        self.notify.notify_one();
    }

    async fn wait(&self) {
        self.notify.notified().await;
    }
}

pub struct CheckpointAcker<A: Acker, C, S: CheckpointStore<C>> {
    inner: A,
    scope: CheckpointScope,
    cursor_id: CursorId,
    index: usize,
    state: StateMap<C>,
    store: S,
    flush_policy: CheckpointFlushPolicy,
    flush_signal: FlushSignal,
}

impl<A, C, S> Acker for CheckpointAcker<A, C, S>
where
    A: Acker + Send + Sync + 'static,
    C: Clone + Send + Sync + 'static,
    S: CheckpointStore<C>,
{
    async fn ack(&self) -> Result<()> {
        self.inner.ack().await?;
        let to_commit = {
            let mut state = self.state.lock().await;
            let entry = state
                .entry(self.cursor_id.clone())
                .or_insert_with(PerCursorState::new);
            let advanced = entry.complete(self.index);
            let commit = match advanced {
                Some(cursor) => {
                    entry.advance_and_check_flush(cursor, &self.flush_policy, Instant::now())
                }
                None => None,
            };
            if entry.is_empty() {
                state.remove(&self.cursor_id);
            }
            commit
        };
        self.flush_signal.wake();
        if let Some(cursor) = to_commit {
            let key = CheckpointKey {
                scope: self.scope.clone(),
                cursor_id: self.cursor_id.clone(),
            };
            self.store.commit(&key, cursor).await?;
        }
        Ok(())
    }

    async fn nack(&self) -> Result<()> {
        self.inner.nack().await
    }

    async fn nack_with(&self, context: NackContext) -> Result<()> {
        self.inner.nack_with(context).await
    }
}

type CheckpointStreamRx<A, C, S, P> =
    mpsc::Receiver<Result<Message<CheckpointAcker<A, C, S>, C, P>>>;

/// Stream returned by [`CheckpointReader::read`]. Delegates [`Stream`]
/// to an internal mpsc receiver and exposes [`flush`](Self::flush) for
/// on-demand drain of buffered checkpoints.
pub struct CheckpointStream<A, C, S, P = crate::payload::Payload>
where
    A: Acker + Send + Sync + 'static,
    C: Clone + Send + Sync + 'static,
    S: CheckpointStore<C>,
    P: Send + Sync + 'static,
{
    rx: CheckpointStreamRx<A, C, S, P>,
    controller: Arc<FlushController<C, S>>,
    producer: Option<tokio::task::JoinHandle<()>>,
    timer: Option<tokio::task::JoinHandle<()>>,
}

impl<A, C, S, P> CheckpointStream<A, C, S, P>
where
    A: Acker + Send + Sync + 'static,
    C: Clone + Send + Sync + 'static,
    S: CheckpointStore<C>,
    P: Send + Sync + 'static,
{
    /// Commits all buffered checkpoints synchronously. Use on graceful
    /// shutdown before dropping the stream. Errors from the store
    /// surface as `Error::Store`.
    pub async fn flush(&self) -> Result<()> {
        self.controller.flush().await
    }
}

impl<A, C, S, P> Stream for CheckpointStream<A, C, S, P>
where
    A: Acker + Send + Sync + 'static,
    C: Clone + Send + Sync + 'static,
    S: CheckpointStore<C>,
    P: Send + Sync + 'static,
{
    type Item = Result<Message<CheckpointAcker<A, C, S>, C, P>>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.rx.poll_recv(cx)
    }
}

impl<A, C, S, P> Drop for CheckpointStream<A, C, S, P>
where
    A: Acker + Send + Sync + 'static,
    C: Clone + Send + Sync + 'static,
    S: CheckpointStore<C>,
    P: Send + Sync + 'static,
{
    fn drop(&mut self) {
        // Close receiver so producer notices via tx.closed() and drains.
        self.rx.close();
        // Wake timer so it observes shutdown signal and exits.
        self.controller.shutdown();
        // Detach handles; tasks self-terminate via signals above.
        drop(self.producer.take());
        drop(self.timer.take());
    }
}

/// Shared flush state visible to acker, producer loop, timer task, and
/// the public `flush()` API. Holds the per-`CursorId` state map, scope,
/// store handle, and shutdown signal.
struct FlushController<C, S: CheckpointStore<C>> {
    scope: CheckpointScope,
    state: StateMap<C>,
    store: S,
    flush_policy: CheckpointFlushPolicy,
    flush_signal: FlushSignal,
    shutdown: Arc<tokio::sync::Notify>,
    shutdown_flag: Arc<std::sync::atomic::AtomicBool>,
}

impl<C, S> FlushController<C, S>
where
    C: Clone + Send + Sync + 'static,
    S: CheckpointStore<C>,
{
    fn shutdown(&self) {
        self.shutdown_flag
            .store(true, std::sync::atomic::Ordering::Release);
        self.shutdown.notify_waiters();
        self.flush_signal.wake();
    }

    fn is_shutting_down(&self) -> bool {
        self.shutdown_flag
            .load(std::sync::atomic::Ordering::Acquire)
    }

    /// Drain all pending flush cursors and commit them. Aggregates the
    /// first store error and continues so all cursors are attempted.
    async fn flush(&self) -> Result<()> {
        let drained: Vec<(CursorId, C)> = {
            let mut state = self.state.lock().await;
            let mut out = Vec::new();
            let mut empty_ids = Vec::new();
            for (id, entry) in state.iter_mut() {
                if let Some(cursor) = entry.take_pending() {
                    out.push((id.clone(), cursor));
                }
                if entry.is_empty() {
                    empty_ids.push(id.clone());
                }
            }
            for id in empty_ids {
                state.remove(&id);
            }
            out
        };
        let mut first_err: Option<Error> = None;
        for (cursor_id, cursor) in drained {
            let key = CheckpointKey {
                scope: self.scope.clone(),
                cursor_id: cursor_id.clone(),
            };
            if let Err(e) = self.store.commit(&key, cursor).await
                && first_err.is_none()
            {
                tracing::warn!("checkpoint reader: explicit flush failed for {cursor_id:?}: {e}");
                first_err = Some(e);
            }
        }
        match first_err {
            Some(e) => Err(e),
            None => Ok(()),
        }
    }

    /// Best-effort drain used on producer-task exit (stream end or
    /// consumer drop). Only commits already-buffered flushes; does not
    /// clear in-flight queue entries because outstanding acks from the
    /// consumer side still need to advance them. Errors are logged but
    /// not propagated since no caller is awaiting a result.
    async fn drain_best_effort(&self, source: &'static str) {
        let drained: Vec<(CursorId, C)> = {
            let mut state = self.state.lock().await;
            let mut out = Vec::new();
            let mut empty_ids = Vec::new();
            for (id, entry) in state.iter_mut() {
                if let Some(cursor) = entry.take_pending() {
                    out.push((id.clone(), cursor));
                }
                if entry.is_empty() {
                    empty_ids.push(id.clone());
                }
            }
            for id in empty_ids {
                state.remove(&id);
            }
            out
        };
        for (cursor_id, cursor) in drained {
            let key = CheckpointKey {
                scope: self.scope.clone(),
                cursor_id: cursor_id.clone(),
            };
            if let Err(e) = self.store.commit(&key, cursor).await {
                tracing::warn!("checkpoint reader: {source} drain failed for {cursor_id:?}: {e}");
            }
        }
    }
}

/// Wraps a `Reader` with durable consumer progress backed by a
/// `CheckpointStore`.
///
/// # Cursor bounds
///
/// The inner reader's cursor must implement
/// `Cursor + Clone + Ord + Send + Sync + 'static`:
/// - [`Cursor`]: provides the `CursorId` used as the checkpoint key.
/// - `Ord`: used to skip-already-stored cursors on resume and to track
///   contiguous in-order progress per cursor id.
/// - `Clone + Send + Sync + 'static`: required to persist the cursor
///   across acks and to share it between the intake task and the
///   per-message [`CheckpointAcker`].
///
/// Backend cursors (`PgCursor`, `SqliteCursor`) and the
/// [`PartitionedCursor`](super::partitioned::PartitionedCursor) wrapper
/// satisfy these bounds out of the box.
pub struct CheckpointReader<R, S> {
    inner: R,
    store: S,
    config: CheckpointReaderConfig,
}

impl<R, S> CheckpointReader<R, S> {
    pub fn new(inner: R, store: S) -> Self {
        Self {
            inner,
            store,
            config: CheckpointReaderConfig::default(),
        }
    }

    pub fn with_config(inner: R, store: S, config: CheckpointReaderConfig) -> Self {
        Self {
            inner,
            store,
            config,
        }
    }
}

impl<R, S, P> Reader<P> for CheckpointReader<R, S>
where
    R: Reader<P> + Send + Sync + 'static,
    R::Cursor: Cursor + Clone + Ord + Send + Sync + 'static,
    R::Subscription: StartableSubscription<R::Cursor>,
    R::Acker: Acker + Send + Sync + 'static,
    R::Stream: Send + 'static,
    S: CheckpointStore<R::Cursor>,
    P: Send + Sync + 'static,
{
    type Subscription = CheckpointSubscription<R::Subscription>;
    type Acker = CheckpointAcker<R::Acker, R::Cursor, S>;
    type Cursor = R::Cursor;
    type Stream = CheckpointStream<R::Acker, R::Cursor, S, P>;

    async fn read(&self, subscription: Self::Subscription) -> Result<Self::Stream> {
        let scope = subscription.scope.clone();
        let store = self.store.clone();
        let stored = store.load_scope(&scope).await?;

        if stored.is_empty() && matches!(subscription.on_missing, MissingCheckpointPolicy::Error) {
            return Err(Error::InvalidCursor(format!(
                "checkpoint reader: no checkpoint found for consumer group `{}` and stream `{}`",
                scope.consumer_group_id.as_str(),
                scope.stream_id.as_str()
            )));
        }

        let inner_subscription = subscription.inner.clone().with_starts(
            stored
                .iter()
                .map(|(_, c)| StartFrom::After(c.clone()))
                .collect(),
        );

        let (inner_stream, known) = match self.inner.read(inner_subscription).await {
            Ok(stream) => {
                let known: HashMap<CursorId, R::Cursor> = stored.into_iter().collect();
                (stream, known)
            }
            Err(Error::InvalidCursor(reason))
                if matches!(
                    subscription.on_invalid,
                    InvalidCursorPolicy::UseInitialStart
                ) =>
            {
                tracing::warn!(
                    reason = %reason,
                    "checkpoint reader falling back to initial start after invalid checkpoint cursor"
                );
                let stream = self.inner.read(subscription.inner.clone()).await?;
                (stream, HashMap::new())
            }
            Err(e) => return Err(e),
        };

        let state: StateMap<R::Cursor> = Arc::new(Mutex::new(HashMap::new()));
        let max_pending = self.config.max_pending_per_key;
        let flush_policy = self.config.flush_policy;
        let flush_signal = FlushSignal::new();
        let shutdown = Arc::new(tokio::sync::Notify::new());
        let shutdown_flag = Arc::new(std::sync::atomic::AtomicBool::new(false));

        let controller = Arc::new(FlushController {
            scope: scope.clone(),
            state: Arc::clone(&state),
            store: store.clone(),
            flush_policy,
            flush_signal: flush_signal.clone(),
            shutdown: Arc::clone(&shutdown),
            shutdown_flag: Arc::clone(&shutdown_flag),
        });

        let (tx, rx) = mpsc::channel::<
            Result<Message<CheckpointAcker<R::Acker, R::Cursor, S>, R::Cursor, P>>,
        >(64);
        let known = Arc::new(known);

        // Cancel-safe producer loop. Selects between inner-stream items
        // and tx.closed() so consumer drop terminates the task even if
        // the inner reader is blocked in next().
        let producer_controller = Arc::clone(&controller);
        let producer_state = Arc::clone(&state);
        let producer_scope = scope.clone();
        let producer_known = Arc::clone(&known);
        let producer_signal = flush_signal.clone();
        let producer_tx = tx.clone();
        let producer = tokio::spawn(async move {
            let mut inner_stream = Box::pin(inner_stream);
            loop {
                let item = tokio::select! {
                    biased;
                    _ = producer_tx.closed() => break,
                    item = inner_stream.next() => item,
                };
                let Some(item) = item else { break };
                let msg = match item {
                    Ok(m) => m,
                    Err(e) => {
                        if producer_tx.send(Err(e)).await.is_err() {
                            break;
                        }
                        continue;
                    }
                };
                let cursor_id: CursorId = msg.cursor().id();
                let cursor = msg.cursor().clone();
                if let Some(stored_cursor) = producer_known.get(&cursor_id)
                    && cursor <= *stored_cursor
                {
                    let _ = msg.ack().await;
                    continue;
                }
                let (event, inner_acker, _cursor) = msg.into_parts();
                let index = {
                    let mut state_guard = producer_state.lock().await;
                    let pending_now = state_guard
                        .get(&cursor_id)
                        .map(|s| s.pending_queue_count())
                        .unwrap_or(0);
                    if pending_now >= max_pending {
                        let _ = producer_tx
                            .send(Err(Error::Store(format!(
                                "checkpoint reader: max_pending_per_key ({max_pending}) reached for cursor id {cursor_id:?}"
                            ))))
                            .await;
                        break;
                    }
                    let entry = state_guard
                        .entry(cursor_id.clone())
                        .or_insert_with(PerCursorState::new);
                    entry.record(cursor.clone())
                };
                let acker: CheckpointAcker<R::Acker, R::Cursor, S> = CheckpointAcker {
                    inner: inner_acker,
                    scope: producer_scope.clone(),
                    cursor_id: cursor_id.clone(),
                    index,
                    state: Arc::clone(&producer_state),
                    store: store.clone(),
                    flush_policy,
                    flush_signal: producer_signal.clone(),
                };
                let out = Message::new(event, acker, cursor);
                if producer_tx.send(Ok(out)).await.is_err() {
                    break;
                }
            }
            producer_controller.drain_best_effort("producer exit").await;
        });

        // Background flush timer. Wakes on flush_signal (any ack /
        // shutdown), recomputes earliest deadline across the state
        // map, parks until that deadline or another wakeup, then
        // flushes expired entries.
        let timer_controller = Arc::clone(&controller);
        let timer = tokio::spawn(async move {
            if timer_controller.flush_policy.max_pending_interval.is_zero() {
                // Pure count-based policy — no timer needed.
                timer_controller.shutdown.notified().await;
                return;
            }
            loop {
                if timer_controller.is_shutting_down() {
                    break;
                }
                let next_deadline = {
                    let state = timer_controller.state.lock().await;
                    let mut earliest: Option<Instant> = None;
                    for entry in state.values() {
                        if let Some(deadline) = entry.flush_deadline(&timer_controller.flush_policy)
                            && earliest.map(|e| deadline < e).unwrap_or(true)
                        {
                            earliest = Some(deadline);
                        }
                    }
                    earliest
                };
                match next_deadline {
                    None => {
                        tokio::select! {
                            biased;
                            _ = timer_controller.shutdown.notified() => break,
                            _ = timer_controller.flush_signal.wait() => {}
                        }
                    }
                    Some(deadline) => {
                        let sleep = tokio::time::sleep_until(deadline);
                        tokio::pin!(sleep);
                        tokio::select! {
                            biased;
                            _ = timer_controller.shutdown.notified() => break,
                            _ = &mut sleep => {
                                timer_controller.flush_expired().await;
                            }
                            _ = timer_controller.flush_signal.wait() => {}
                        }
                    }
                }
            }
        });

        Ok(CheckpointStream {
            rx,
            controller,
            producer: Some(producer),
            timer: Some(timer),
        })
    }
}

impl<C, S> FlushController<C, S>
where
    C: Clone + Send + Sync + 'static,
    S: CheckpointStore<C>,
{
    /// Flush all entries whose `first_pending_at + max_pending_interval`
    /// has already elapsed. Used by the background timer.
    async fn flush_expired(&self) {
        let now = Instant::now();
        let drained: Vec<(CursorId, C)> = {
            let mut state = self.state.lock().await;
            let mut out = Vec::new();
            let mut empty_ids = Vec::new();
            for (id, entry) in state.iter_mut() {
                let expired = entry
                    .flush_deadline(&self.flush_policy)
                    .map(|d| d <= now)
                    .unwrap_or(false);
                if expired && let Some(cursor) = entry.take_pending() {
                    out.push((id.clone(), cursor));
                }
                if entry.is_empty() {
                    empty_ids.push(id.clone());
                }
            }
            for id in empty_ids {
                state.remove(&id);
            }
            out
        };
        for (cursor_id, cursor) in drained {
            let key = CheckpointKey {
                scope: self.scope.clone(),
                cursor_id: cursor_id.clone(),
            };
            if let Err(e) = self.store.commit(&key, cursor).await {
                tracing::warn!("checkpoint reader: timer flush failed for {cursor_id:?}: {e}");
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::Event;
    use crate::io::Message;
    use crate::io::StreamId;
    use crate::io::acker::NoopAcker;
    use crate::io::position::{StartFrom, StartableSubscription};
    use crate::payload::Payload;
    use futures::{Stream, StreamExt};
    use std::pin::Pin;
    use std::task::{Context, Poll};
    use tokio::sync::Mutex as TokioMutex;

    struct MpscStream<T> {
        rx: tokio::sync::mpsc::Receiver<T>,
    }

    impl<T> Stream for MpscStream<T> {
        type Item = T;

        fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> {
            self.rx.poll_recv(cx)
        }
    }

    type InnerRx = tokio::sync::mpsc::Receiver<Result<Message<NoopAcker, TestCursor>>>;

    struct ChannelReader {
        rx: std::sync::Mutex<Option<InnerRx>>,
    }

    impl Reader for ChannelReader {
        type Subscription = TestSub;
        type Acker = NoopAcker;
        type Cursor = TestCursor;
        type Stream = Pin<Box<dyn Stream<Item = Result<Message<NoopAcker, TestCursor>>> + Send>>;

        async fn read(&self, _: TestSub) -> Result<Self::Stream> {
            let rx = self.rx.lock().unwrap().take().unwrap();
            Ok(Box::pin(MpscStream { rx }))
        }
    }

    #[test]
    fn pending_state_advances_contiguously_in_order() {
        let mut s: PerCursorState<i64> = PerCursorState::new();
        let i0 = s.record(10);
        let i1 = s.record(20);
        assert_eq!(s.complete(i0), Some(10));
        assert_eq!(s.complete(i1), Some(20));
        assert_eq!(s.pending_queue_count(), 0);
    }

    #[test]
    fn pending_state_does_not_advance_past_unacked_predecessor() {
        let mut s: PerCursorState<i64> = PerCursorState::new();
        let i0 = s.record(10);
        let i1 = s.record(20);
        let i2 = s.record(30);
        assert_eq!(s.complete(i1), None, "i0 unacked, can't advance");
        assert_eq!(s.complete(i2), None, "i0 still unacked");
        assert_eq!(
            s.complete(i0),
            Some(30),
            "completes whole prefix to highest"
        );
        assert_eq!(s.pending_queue_count(), 0);
    }

    #[test]
    fn pending_state_returns_highest_in_prefix() {
        let mut s: PerCursorState<i64> = PerCursorState::new();
        let i0 = s.record(10);
        let i1 = s.record(20);
        let _i2 = s.record(30);
        assert_eq!(s.complete(i0), Some(10));
        assert_eq!(s.complete(i1), Some(20));
        assert_eq!(s.pending_queue_count(), 1);
    }

    #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
    struct TestCursor(i64);

    impl Cursor for TestCursor {}

    #[derive(Debug, Clone, Default)]
    struct TestSub;
    impl StartableSubscription<TestCursor> for TestSub {
        fn with_start(self, _: StartFrom<TestCursor>) -> Self {
            self
        }
    }

    #[derive(Debug, Clone)]
    struct SeedingSub {
        start: StartFrom<TestCursor>,
    }

    impl Default for SeedingSub {
        fn default() -> Self {
            Self {
                start: StartFrom::Earliest,
            }
        }
    }

    impl StartableSubscription<TestCursor> for SeedingSub {
        fn with_start(mut self, start: StartFrom<TestCursor>) -> Self {
            self.start = start;
            self
        }
    }

    struct SeedingSubReader {
        observed: std::sync::Arc<TokioMutex<Option<SeedingSub>>>,
    }

    impl Reader for SeedingSubReader {
        type Subscription = SeedingSub;
        type Acker = NoopAcker;
        type Cursor = TestCursor;
        type Stream = Pin<Box<dyn Stream<Item = Result<Message<NoopAcker, TestCursor>>> + Send>>;

        async fn read(&self, subscription: SeedingSub) -> Result<Self::Stream> {
            *self.observed.lock().await = Some(subscription);
            Ok(Box::pin(futures::stream::empty()))
        }
    }

    #[derive(Clone)]
    struct PreloadedStore<C> {
        rows: std::sync::Arc<TokioMutex<Vec<(CursorId, C)>>>,
    }

    impl<C> Default for PreloadedStore<C> {
        fn default() -> Self {
            Self {
                rows: std::sync::Arc::new(TokioMutex::new(Vec::new())),
            }
        }
    }

    impl<C> CheckpointStore<C> for PreloadedStore<C>
    where
        C: Clone + Send + Sync + 'static,
    {
        async fn load(&self, _: &CheckpointKey) -> Result<Option<C>> {
            Ok(None)
        }

        async fn load_scope(&self, _: &CheckpointScope) -> Result<Vec<(CursorId, C)>> {
            Ok(self.rows.lock().await.clone())
        }

        async fn commit(&self, _: &CheckpointKey, _: C) -> Result<()> {
            Ok(())
        }
    }

    struct VecReader {
        events: std::sync::Mutex<Option<Vec<Event>>>,
    }

    impl Reader for VecReader {
        type Subscription = TestSub;
        type Acker = NoopAcker;
        type Cursor = TestCursor;
        type Stream = Pin<Box<dyn Stream<Item = Result<Message<NoopAcker, TestCursor>>> + Send>>;

        async fn read(&self, _: TestSub) -> Result<Self::Stream> {
            let events = self.events.lock().unwrap().take().unwrap();
            let iter = events
                .into_iter()
                .enumerate()
                .map(|(i, e)| Ok(Message::new(e, NoopAcker, TestCursor(i as i64 + 1))));
            Ok(Box::pin(futures::stream::iter(iter)))
        }
    }

    #[derive(Clone)]
    struct MemStore {
        rows: Arc<TokioMutex<Vec<(CheckpointKey, TestCursor)>>>,
        committed: Arc<std::sync::Mutex<Vec<(CursorId, TestCursor)>>>,
    }

    impl Default for MemStore {
        fn default() -> Self {
            Self {
                rows: Arc::new(TokioMutex::new(Vec::new())),
                committed: Arc::new(std::sync::Mutex::new(Vec::new())),
            }
        }
    }

    impl MemStore {
        fn committed(&self) -> Vec<(CursorId, TestCursor)> {
            self.committed.lock().unwrap().clone()
        }
    }

    impl CheckpointStore<TestCursor> for MemStore {
        async fn load(&self, _: &CheckpointKey) -> Result<Option<TestCursor>> {
            Ok(None)
        }
        async fn load_scope(&self, _: &CheckpointScope) -> Result<Vec<(CursorId, TestCursor)>> {
            Ok(vec![])
        }
        async fn commit(&self, key: &CheckpointKey, cursor: TestCursor) -> Result<()> {
            self.rows.lock().await.push((key.clone(), cursor));
            self.committed
                .lock()
                .unwrap()
                .push((key.cursor_id.clone(), cursor));
            Ok(())
        }
    }

    fn test_scope() -> CheckpointScope {
        CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        )
    }

    fn ev(key: &str) -> Event {
        Event::builder(
            "acme",
            "/x",
            "thing.happened",
            key,
            Payload::from_string("p"),
        )
        .unwrap()
        .build()
        .expect("valid event")
    }

    #[tokio::test]
    async fn checkpoint_reader_errors_when_per_key_max_pending_exceeded() {
        use futures::StreamExt;
        let events: Vec<Event> = (0..5).map(|i| ev(&format!("k{i}"))).collect();
        let reader = VecReader {
            events: std::sync::Mutex::new(Some(events)),
        };
        let store = MemStore::default();
        let cr = CheckpointReader::with_config(
            reader,
            store,
            CheckpointReaderConfig {
                max_pending_per_key: 2,
                ..Default::default()
            },
        );
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );
        let mut stream = cr
            .read(CheckpointSubscription::new(TestSub, scope))
            .await
            .unwrap();
        // Pull messages without acking — all 5 share partition=None, so
        // pending grows. After max_pending_per_key=2 is exceeded, the
        // stream must yield an error.
        let mut held = Vec::new();
        let mut saw_error = false;
        for _ in 0..10 {
            match tokio::time::timeout(std::time::Duration::from_millis(500), stream.next()).await {
                Ok(Some(Ok(m))) => held.push(m),
                Ok(Some(Err(_))) => {
                    saw_error = true;
                    break;
                }
                Ok(None) => break,
                Err(_) => break,
            }
        }
        assert!(
            saw_error,
            "expected stream error after per-key max_pending exceeded, held {} messages",
            held.len()
        );
    }

    #[tokio::test]
    async fn checkpoint_reader_errors_when_required_checkpoint_is_missing() {
        let events: Vec<Event> = vec![ev("k0")];
        let reader = VecReader {
            events: std::sync::Mutex::new(Some(events)),
        };
        let store = MemStore::default();
        let cr = CheckpointReader::new(reader, store);
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );

        let subscription =
            CheckpointSubscription::new(TestSub, scope).on_missing(MissingCheckpointPolicy::Error);

        let err = match cr.read(subscription).await {
            Ok(_) => panic!("expected missing checkpoint error"),
            Err(e) => e,
        };

        assert!(matches!(err, Error::InvalidCursor(_)));
        assert!(err.to_string().contains("no checkpoint found"));
    }

    struct InvalidCursorReader {
        calls: std::sync::Arc<std::sync::atomic::AtomicUsize>,
    }

    impl Reader for InvalidCursorReader {
        type Subscription = TestSub;
        type Acker = NoopAcker;
        type Cursor = TestCursor;
        type Stream = Pin<Box<dyn Stream<Item = Result<Message<NoopAcker, TestCursor>>> + Send>>;

        async fn read(&self, _: TestSub) -> Result<Self::Stream> {
            let call = self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            if call == 0 {
                return Err(Error::InvalidCursor(
                    "checkpoint partition count changed".to_owned(),
                ));
            }
            Ok(Box::pin(futures::stream::empty()))
        }
    }

    #[tokio::test]
    async fn checkpoint_reader_retries_with_initial_start_when_checkpoint_cursor_is_invalid() {
        let store = PreloadedStore::default();
        *store.rows.lock().await = vec![(CursorId::global(), TestCursor(10))];
        let calls = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let reader = InvalidCursorReader {
            calls: std::sync::Arc::clone(&calls),
        };
        let cr = CheckpointReader::new(reader, store);
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );

        let _stream = cr
            .read(CheckpointSubscription::new(TestSub, scope))
            .await
            .unwrap();

        assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn checkpoint_reader_returns_invalid_cursor_when_policy_is_error() {
        let store = PreloadedStore::default();
        *store.rows.lock().await = vec![(CursorId::global(), TestCursor(10))];
        let calls = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let reader = InvalidCursorReader {
            calls: std::sync::Arc::clone(&calls),
        };
        let cr = CheckpointReader::new(reader, store);
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );

        let err = match cr
            .read(
                CheckpointSubscription::new(TestSub, scope).on_invalid(InvalidCursorPolicy::Error),
            )
            .await
        {
            Ok(_) => panic!("expected invalid cursor error"),
            Err(e) => e,
        };

        assert!(matches!(err, Error::InvalidCursor(_)));
        assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn checkpoint_reader_over_mixed_partitions_resumes_from_current_only() {
        // Stored cursor < VecReader's emitted TestCursor(1) so the
        // CheckpointReader skip-already-stored guard does not swallow
        // the resumed event if partition assignment happens to match.
        use crate::io::reader::{
            PartitionedCursor, PartitionedReader, PartitionedReaderConfig, PartitionedSubscription,
        };
        use crate::partition::Partition;
        use futures::StreamExt;
        use std::num::NonZeroU32;

        let old_partition = Partition::new(0, NonZeroU32::new(8).unwrap()).unwrap();
        let current_partition = Partition::new(2, NonZeroU32::new(4).unwrap()).unwrap();

        let store = PreloadedStore::<PartitionedCursor<TestCursor>>::default();
        *store.rows.lock().await = vec![
            (
                CursorId::partition(old_partition),
                PartitionedCursor::new(TestCursor(0), old_partition),
            ),
            (
                CursorId::partition(current_partition),
                PartitionedCursor::new(TestCursor(0), current_partition),
            ),
        ];

        let inner_reader = VecReader {
            events: std::sync::Mutex::new(Some(vec![ev("k0")])),
        };
        let partitioned = PartitionedReader::source(
            inner_reader,
            PartitionedReaderConfig {
                partition_count: NonZeroU32::new(4).unwrap(),
                ..PartitionedReaderConfig::default()
            },
        );
        let checkpointed = CheckpointReader::new(partitioned, store);
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );

        let mut stream = checkpointed
            .read(CheckpointSubscription::new(
                PartitionedSubscription::new(TestSub),
                scope,
            ))
            .await
            .unwrap();

        let msg = tokio::time::timeout(std::time::Duration::from_secs(2), stream.next())
            .await
            .unwrap()
            .unwrap()
            .unwrap();
        assert_eq!(msg.event().key().as_str(), "k0");
    }

    #[tokio::test]
    async fn checkpoint_reader_over_old_partitions_only_falls_back() {
        // Stored row is incompatible (partition_count=8 vs configured=4);
        // test exercises the fallback path.
        use crate::io::reader::{
            PartitionedCursor, PartitionedReader, PartitionedReaderConfig, PartitionedSubscription,
        };
        use crate::partition::Partition;
        use futures::StreamExt;
        use std::num::NonZeroU32;

        let old_partition = Partition::new(0, NonZeroU32::new(8).unwrap()).unwrap();
        let store = PreloadedStore::<PartitionedCursor<TestCursor>>::default();
        *store.rows.lock().await = vec![(
            CursorId::partition(old_partition),
            PartitionedCursor::new(TestCursor(10), old_partition),
        )];

        let inner_reader = VecReader {
            events: std::sync::Mutex::new(Some(vec![ev("k0")])),
        };
        let partitioned = PartitionedReader::source(
            inner_reader,
            PartitionedReaderConfig {
                partition_count: NonZeroU32::new(4).unwrap(),
                ..PartitionedReaderConfig::default()
            },
        );
        let checkpointed = CheckpointReader::new(partitioned, store);
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );

        let mut stream = checkpointed
            .read(CheckpointSubscription::new(
                PartitionedSubscription::new(TestSub),
                scope,
            ))
            .await
            .unwrap();

        let msg = tokio::time::timeout(std::time::Duration::from_secs(2), stream.next())
            .await
            .unwrap()
            .unwrap()
            .unwrap();
        assert_eq!(msg.event().key().as_str(), "k0");
    }

    #[tokio::test]
    async fn checkpoint_reader_seeds_inner_with_min_cursor() {
        let cursor_id = CursorId::partition(
            crate::partition::Partition::new(2, std::num::NonZeroU32::new(4).unwrap()).unwrap(),
        );
        let store = PreloadedStore::default();
        *store.rows.lock().await = vec![(cursor_id, TestCursor(10))];
        let observed = std::sync::Arc::new(TokioMutex::new(None));
        let reader = SeedingSubReader {
            observed: std::sync::Arc::clone(&observed),
        };
        let cr = CheckpointReader::new(reader, store);
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );

        let _stream = cr
            .read(CheckpointSubscription::new(SeedingSub::default(), scope))
            .await
            .unwrap();

        let observed = observed.lock().await.clone().unwrap();
        assert_eq!(observed.start, StartFrom::After(TestCursor(10)));
    }

    #[test]
    fn missing_checkpoint_policy_defaults_to_use_initial_start() {
        assert_eq!(
            MissingCheckpointPolicy::default(),
            MissingCheckpointPolicy::UseInitialStart
        );
    }

    #[test]
    fn invalid_cursor_policy_defaults_to_use_initial_start() {
        assert_eq!(
            InvalidCursorPolicy::default(),
            InvalidCursorPolicy::UseInitialStart
        );
    }

    #[test]
    fn checkpoint_key_with_global_cursor_id_has_no_partition_subkey() {
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );
        let key = CheckpointKey::new(scope, CursorId::global());
        assert_eq!(key.cursor_id, CursorId::global());
    }

    #[test]
    fn checkpoint_key_with_named_cursor_id_preserves_value() {
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );
        let id = CursorId::partition(
            crate::partition::Partition::new(17, std::num::NonZeroU32::new(100).unwrap()).unwrap(),
        );
        let key = CheckpointKey::new(scope, id.clone());
        assert_eq!(key.cursor_id, id);
    }

    #[tokio::test]
    async fn checkpoint_stream_drop_aborts_forwarder_when_inner_stream_is_pending() {
        struct PendingReader;
        impl Reader for PendingReader {
            type Subscription = TestSub;
            type Acker = NoopAcker;
            type Cursor = TestCursor;
            type Stream =
                Pin<Box<dyn Stream<Item = Result<Message<NoopAcker, TestCursor>>> + Send>>;

            async fn read(&self, _: TestSub) -> Result<Self::Stream> {
                Ok(Box::pin(futures::stream::pending()))
            }
        }

        let store = PreloadedStore::<TestCursor>::default();
        let cr = CheckpointReader::new(PendingReader, store);
        let scope = CheckpointScope::new(
            ConsumerGroupId::new("g").unwrap(),
            StreamId::new("s").unwrap(),
        );

        let stream = cr
            .read(CheckpointSubscription::new(TestSub, scope))
            .await
            .unwrap();
        drop(stream);
    }

    #[tokio::test]
    async fn flush_policy_count_triggers_after_n_acks() {
        let store = MemStore::default();
        let reader = VecReader {
            events: std::sync::Mutex::new(Some(vec![ev("a"), ev("b"), ev("c")])),
        };
        let config = CheckpointReaderConfig {
            flush_policy: CheckpointFlushPolicy {
                max_pending_acks: NonZeroUsize::new(2).unwrap(),
                max_pending_interval: Duration::ZERO,
            },
            ..Default::default()
        };
        let cr = CheckpointReader::with_config(reader, store.clone(), config);
        let sub = CheckpointSubscription::new(TestSub, test_scope());

        let mut stream = cr.read(sub).await.unwrap();

        let msg1 = stream.next().await.unwrap().unwrap();
        msg1.ack().await.unwrap();
        assert!(
            store.committed().is_empty(),
            "first ack should buffer, not flush"
        );

        let msg2 = stream.next().await.unwrap().unwrap();
        msg2.ack().await.unwrap();
        assert_eq!(
            store.committed().len(),
            1,
            "second ack should trigger flush"
        );
    }

    #[tokio::test]
    async fn flush_policy_interval_triggers_after_duration() {
        let store = MemStore::default();
        let reader = VecReader {
            events: std::sync::Mutex::new(Some(vec![ev("a"), ev("b")])),
        };
        let config = CheckpointReaderConfig {
            flush_policy: CheckpointFlushPolicy {
                max_pending_acks: NonZeroUsize::new(100).unwrap(),
                max_pending_interval: Duration::from_millis(50),
            },
            ..Default::default()
        };
        let cr = CheckpointReader::with_config(reader, store.clone(), config);
        let sub = CheckpointSubscription::new(TestSub, test_scope());

        let mut stream = cr.read(sub).await.unwrap();

        let msg1 = stream.next().await.unwrap().unwrap();
        msg1.ack().await.unwrap();
        assert!(
            store.committed().is_empty(),
            "should buffer, high count threshold not hit"
        );

        tokio::time::sleep(Duration::from_millis(100)).await;

        let msg2 = stream.next().await.unwrap().unwrap();
        msg2.ack().await.unwrap();
        assert_eq!(
            store.committed().len(),
            1,
            "interval should trigger flush on next ack"
        );
    }

    #[tokio::test]
    async fn default_flush_policy_commits_on_every_ack() {
        let store = MemStore::default();
        let reader = VecReader {
            events: std::sync::Mutex::new(Some(vec![ev("a"), ev("b")])),
        };
        let cr = CheckpointReader::new(reader, store.clone());
        let sub = CheckpointSubscription::new(TestSub, test_scope());

        let mut stream = cr.read(sub).await.unwrap();

        let msg1 = stream.next().await.unwrap().unwrap();
        msg1.ack().await.unwrap();
        assert_eq!(store.committed().len(), 1, "default flushes on first ack");

        let msg2 = stream.next().await.unwrap().unwrap();
        msg2.ack().await.unwrap();
        assert_eq!(store.committed().len(), 2, "default flushes on second ack");
    }

    #[tokio::test]
    async fn flush_on_stream_drop_drains_buffered_cursors() {
        let (tx, rx) = tokio::sync::mpsc::channel::<Result<Message<NoopAcker, TestCursor>>>(1);

        let reader = ChannelReader {
            rx: std::sync::Mutex::new(Some(rx)),
        };
        let store = MemStore::default();
        let config = CheckpointReaderConfig {
            flush_policy: CheckpointFlushPolicy {
                max_pending_acks: NonZeroUsize::new(10).unwrap(),
                max_pending_interval: Duration::from_secs(3600),
            },
            ..Default::default()
        };
        let cr = CheckpointReader::with_config(reader, store.clone(), config);
        let sub = CheckpointSubscription::new(TestSub, test_scope());

        let mut stream = cr.read(sub).await.unwrap();

        tx.send(Ok(Message::new(ev("a"), NoopAcker, TestCursor(1))))
            .await
            .unwrap();

        let msg1 = stream.next().await.unwrap().unwrap();
        msg1.ack().await.unwrap();
        assert!(
            store.committed().is_empty(),
            "should buffer under high flush threshold"
        );

        drop(stream);
        drop(tx);

        tokio::time::sleep(Duration::from_millis(50)).await;

        assert_eq!(
            store.committed().len(),
            1,
            "drop should drain buffered cursor"
        );
    }

    #[tokio::test]
    async fn post_loop_drain_commits_buffered_cursors_on_natural_end() {
        let (tx, rx) = tokio::sync::mpsc::channel::<Result<Message<NoopAcker, TestCursor>>>(1);

        let reader = ChannelReader {
            rx: std::sync::Mutex::new(Some(rx)),
        };
        let store = MemStore::default();
        let config = CheckpointReaderConfig {
            flush_policy: CheckpointFlushPolicy {
                max_pending_acks: NonZeroUsize::new(10).unwrap(),
                max_pending_interval: Duration::from_secs(3600),
            },
            ..Default::default()
        };
        let cr = CheckpointReader::with_config(reader, store.clone(), config);
        let sub = CheckpointSubscription::new(TestSub, test_scope());

        let mut stream = cr.read(sub).await.unwrap();

        tx.send(Ok(Message::new(ev("a"), NoopAcker, TestCursor(1))))
            .await
            .unwrap();

        let msg1 = stream.next().await.unwrap().unwrap();
        msg1.ack().await.unwrap();
        assert!(
            store.committed().is_empty(),
            "should buffer under high flush threshold"
        );

        drop(tx);

        assert!(stream.next().await.is_none(), "stream should end naturally");

        assert_eq!(
            store.committed().len(),
            1,
            "natural end should drain buffered cursor"
        );
    }

    #[tokio::test]
    async fn background_timer_flushes_without_further_acks() {
        let (tx, rx) = tokio::sync::mpsc::channel::<Result<Message<NoopAcker, TestCursor>>>(1);
        let reader = ChannelReader {
            rx: std::sync::Mutex::new(Some(rx)),
        };
        let store = MemStore::default();
        let config = CheckpointReaderConfig {
            flush_policy: CheckpointFlushPolicy {
                max_pending_acks: NonZeroUsize::new(100).unwrap(),
                max_pending_interval: Duration::from_millis(50),
            },
            ..Default::default()
        };
        let cr = CheckpointReader::with_config(reader, store.clone(), config);
        let sub = CheckpointSubscription::new(TestSub, test_scope());
        let mut stream = cr.read(sub).await.unwrap();

        tx.send(Ok(Message::new(ev("a"), NoopAcker, TestCursor(1))))
            .await
            .unwrap();
        let msg = stream.next().await.unwrap().unwrap();
        msg.ack().await.unwrap();

        assert!(
            store.committed().is_empty(),
            "below count threshold, interval not yet elapsed"
        );

        // Advance time past the interval — background timer must flush
        // even though no further acks arrive.
        tokio::time::sleep(Duration::from_millis(75)).await;

        assert_eq!(
            store.committed().len(),
            1,
            "background timer should flush expired entry without further acks"
        );
    }

    /// Producer task remains blocked on inner stream forever. Consumer
    /// drops the stream — producer must exit instead of leaking.
    #[tokio::test]
    async fn drop_aborts_producer_blocked_on_inner_stream() {
        let (_tx, rx) = tokio::sync::mpsc::channel::<Result<Message<NoopAcker, TestCursor>>>(1);
        let reader = ChannelReader {
            rx: std::sync::Mutex::new(Some(rx)),
        };
        let store = MemStore::default();
        let cr = CheckpointReader::new(reader, store.clone());
        let sub = CheckpointSubscription::new(TestSub, test_scope());

        let stream = cr.read(sub).await.unwrap();
        // Producer is awaiting inner_stream.next(). Dropping stream
        // must signal producer via tx.closed() so it exits.
        drop(stream);

        // Give the runtime a tick to schedule the producer's select! arm.
        tokio::time::sleep(Duration::from_millis(10)).await;
        // If the producer were leaked, this test would still pass — the
        // real assertion is that no panic / hang occurs and the runtime
        // shuts down cleanly when the test ends. The previous version
        // before cancel-safety would leak the producer task.
    }

    #[tokio::test]
    async fn explicit_flush_drains_buffered_cursors_synchronously() {
        let (tx, rx) = tokio::sync::mpsc::channel::<Result<Message<NoopAcker, TestCursor>>>(2);
        let reader = ChannelReader {
            rx: std::sync::Mutex::new(Some(rx)),
        };
        let store = MemStore::default();
        let config = CheckpointReaderConfig {
            flush_policy: CheckpointFlushPolicy {
                max_pending_acks: NonZeroUsize::new(100).unwrap(),
                max_pending_interval: Duration::from_secs(3600),
            },
            ..Default::default()
        };
        let cr = CheckpointReader::with_config(reader, store.clone(), config);
        let sub = CheckpointSubscription::new(TestSub, test_scope());
        let mut stream = cr.read(sub).await.unwrap();

        tx.send(Ok(Message::new(ev("a"), NoopAcker, TestCursor(1))))
            .await
            .unwrap();
        let msg = stream.next().await.unwrap().unwrap();
        msg.ack().await.unwrap();

        assert!(
            store.committed().is_empty(),
            "buffered, neither threshold hit yet"
        );

        // Explicit flush — must commit synchronously.
        stream.flush().await.unwrap();

        assert_eq!(
            store.committed().len(),
            1,
            "explicit flush should drain immediately"
        );

        // Second flush with no pending state is a no-op.
        stream.flush().await.unwrap();
        assert_eq!(store.committed().len(), 1);
    }

    #[tokio::test]
    async fn background_timer_does_not_spin_when_interval_is_zero() {
        // Sanity: with default policy (interval=0), the timer task
        // parks on shutdown notify and never wakes for sleeps. Just
        // confirm no panics / hangs when default config runs against
        // an empty channel and is dropped.
        let (_tx, rx) = tokio::sync::mpsc::channel::<Result<Message<NoopAcker, TestCursor>>>(1);
        let reader = ChannelReader {
            rx: std::sync::Mutex::new(Some(rx)),
        };
        let store = MemStore::default();
        let cr = CheckpointReader::new(reader, store.clone());
        let sub = CheckpointSubscription::new(TestSub, test_scope());
        let stream = cr.read(sub).await.unwrap();
        tokio::time::sleep(Duration::from_millis(10)).await;
        drop(stream);
    }
}