eventfold-es 0.2.0

Embedded event-sourcing framework built on eventfold
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
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
//! Top-level entry point that composes storage layout, actor spawning, and
//! handle caching into a single `AggregateStore` type.

use std::any::Any;
use std::collections::{HashMap, HashSet};
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::RwLock;

use crate::actor::{ActorConfig, AggregateHandle, spawn_actor_with_config};
use crate::aggregate::Aggregate;
use crate::process_manager::{
    AggregateDispatcher, ProcessManagerCatchUp, ProcessManagerReport, ProcessManagerRunner,
    TypedDispatcher, append_dead_letter,
};
use crate::projection::{Projection, ProjectionRunner};
use crate::storage::StreamLayout;

/// Type-erased handle cache keyed by `(aggregate_type, instance_id)`.
///
/// `Box<dyn Any + Send + Sync>` lets a single map hold `AggregateHandle<A>`
/// for any concrete `A`. Downcasting recovers the typed handle.
type HandleCache = HashMap<(String, String), Box<dyn Any + Send + Sync>>;

/// Type-erased projection map keyed by projection name.
///
/// Each value is a `std::sync::Mutex<ProjectionRunner<P>>` erased to `dyn Any`.
/// We use `std::sync::Mutex` (not `tokio::sync::Mutex`) because the lock is
/// held briefly and `catch_up` does blocking I/O that should not hold an async
/// mutex across an `.await` point.
type ProjectionMap = HashMap<String, Box<dyn Any + Send + Sync>>;

/// Type-erased list of process manager runners.
///
/// Each runner is wrapped in `std::sync::Mutex` because `catch_up` does
/// blocking file I/O that must not hold an async mutex.
type ProcessManagerList = Vec<std::sync::Mutex<Box<dyn ProcessManagerCatchUp>>>;

/// Type-erased dispatcher map keyed by aggregate type name.
type DispatcherMap = HashMap<String, Box<dyn AggregateDispatcher>>;

/// Type-erased catch-up list for projections.
///
/// Mirrors the `ProcessManagerList` pattern: each entry wraps a
/// `ProjectionRunner<P>` behind a `std::sync::Mutex` so that
/// [`inject_event`](AggregateStore::inject_event) can trigger catch-up on
/// all projections without knowing the concrete `P` type.
type ProjectionCatchUpList = Vec<std::sync::Mutex<Box<dyn ProjectionCatchUpFn>>>;

/// Type-erased projection catch-up interface.
///
/// Implemented by [`ProjectionRunner<P>`](crate::projection::ProjectionRunner)
/// wrapper so that the store can catch up all projections without knowing
/// the concrete `P` type parameter.
trait ProjectionCatchUpFn: Send + Sync {
    /// Catch up on all subscribed streams and save the checkpoint.
    fn catch_up(&mut self) -> io::Result<()>;
}

/// Wrapper that implements [`ProjectionCatchUpFn`] by delegating to a
/// shared `Arc<Mutex<ProjectionRunner<P>>>`. This allows the type-erased
/// catch-up list and the typed projection map to share the same runner.
struct SharedProjectionCatchUp<P: Projection> {
    inner: Arc<std::sync::Mutex<ProjectionRunner<P>>>,
}

impl<P: Projection> ProjectionCatchUpFn for SharedProjectionCatchUp<P> {
    fn catch_up(&mut self) -> io::Result<()> {
        let mut runner = self
            .inner
            .lock()
            .map_err(|e| io::Error::other(e.to_string()))?;
        runner.catch_up()
    }
}

/// Default idle timeout for actors: 5 minutes.
const DEFAULT_IDLE_TIMEOUT: Duration = Duration::from_secs(300);

/// Options controlling the behaviour of [`AggregateStore::inject_event`].
///
/// # Examples
///
/// ```
/// use eventfold_es::InjectOptions;
///
/// // Default: do not run process managers after injection.
/// let opts = InjectOptions::default();
/// assert!(!opts.run_process_managers);
///
/// // Opt in to process manager triggering.
/// let opts = InjectOptions { run_process_managers: true };
/// assert!(opts.run_process_managers);
/// ```
#[derive(Debug, Clone, Default)]
pub struct InjectOptions {
    /// When `true`, call [`AggregateStore::run_process_managers`] after
    /// appending the event. Defaults to `false`.
    pub run_process_managers: bool,
}

/// Central registry that manages aggregate instance lifecycles.
///
/// The store handles directory creation, actor spawning, and handle caching.
/// It is `Clone + Send + Sync` -- cloning shares the underlying cache.
///
/// # Examples
///
/// ```no_run
/// use eventfold_es::{AggregateStore, CommandContext};
///
/// # async fn example() -> std::io::Result<()> {
/// let store = AggregateStore::open("/tmp/my-app").await?;
/// // Use store.get::<MyAggregate>("instance-id") to get handles
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct AggregateStore {
    layout: StreamLayout,
    cache: Arc<RwLock<HandleCache>>,
    projections: Arc<std::sync::RwLock<ProjectionMap>>,
    /// Type-erased projection catch-up runners for [`inject_event`].
    projection_catch_ups: Arc<std::sync::RwLock<ProjectionCatchUpList>>,
    process_managers: Arc<std::sync::RwLock<ProcessManagerList>>,
    dispatchers: Arc<DispatcherMap>,
    /// In-memory set of event IDs already injected, for deduplication.
    /// Shared across clones via `Arc`.
    seen_ids: Arc<std::sync::Mutex<HashSet<String>>>,
    idle_timeout: Duration,
}

// Manual `Debug` because `dyn Any` is not `Debug` and we don't want to
// expose cache internals.
impl std::fmt::Debug for AggregateStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AggregateStore")
            .field("base_dir", &self.layout.base_dir())
            .finish()
    }
}

impl AggregateStore {
    /// Open or create a store rooted at `base_dir`.
    ///
    /// Creates the metadata directory if it does not exist.
    ///
    /// # Arguments
    ///
    /// * `base_dir` - Root directory for all event store data.
    ///
    /// # Returns
    ///
    /// A new `AggregateStore` ready to spawn aggregate actors.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if the metadata directory cannot be created.
    pub async fn open(base_dir: impl AsRef<Path>) -> io::Result<Self> {
        let layout = StreamLayout::new(base_dir.as_ref());
        // Create meta dir using blocking I/O wrapped in spawn_blocking
        // to avoid blocking the tokio reactor.
        let meta_dir = layout.meta_dir();
        tokio::task::spawn_blocking(move || std::fs::create_dir_all(meta_dir))
            .await
            .map_err(io::Error::other)??;
        Ok(Self {
            layout,
            cache: Arc::new(RwLock::new(HashMap::new())),
            projections: Arc::new(std::sync::RwLock::new(HashMap::new())),
            projection_catch_ups: Arc::new(std::sync::RwLock::new(Vec::new())),
            process_managers: Arc::new(std::sync::RwLock::new(Vec::new())),
            dispatchers: Arc::new(HashMap::new()),
            seen_ids: Arc::new(std::sync::Mutex::new(HashSet::new())),
            idle_timeout: DEFAULT_IDLE_TIMEOUT,
        })
    }

    /// Get a handle to an aggregate instance, spawning its actor if needed.
    ///
    /// If the actor is already running (cached), returns a clone of the
    /// existing handle. Otherwise, creates the stream directory on disk and
    /// spawns a new actor thread.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique instance identifier within the aggregate type.
    ///
    /// # Returns
    ///
    /// An [`AggregateHandle`] for sending commands and reading state.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if directory creation or event log opening fails.
    pub async fn get<A: Aggregate>(&self, id: &str) -> io::Result<AggregateHandle<A>> {
        let key = (A::AGGREGATE_TYPE.to_owned(), id.to_owned());

        // Fast path: check cache with read lock.
        {
            let cache = self.cache.read().await;
            if let Some(boxed) = cache.get(&key)
                && let Some(handle) = boxed.downcast_ref::<AggregateHandle<A>>()
                && handle.is_alive()
            {
                return Ok(handle.clone());
            }
        }

        // If we get here, either the handle is missing or the actor has
        // exited (e.g. idle timeout). Evict any stale entry and re-spawn.
        {
            let mut cache = self.cache.write().await;
            cache.remove(&key);
        }

        // Slow path: create stream directory and spawn actor.
        let layout = self.layout.clone();
        let agg_type = A::AGGREGATE_TYPE.to_owned();
        let inst_id = id.to_owned();
        let stream_dir =
            tokio::task::spawn_blocking(move || layout.ensure_stream(&agg_type, &inst_id))
                .await
                .map_err(io::Error::other)??;

        tracing::debug!(
            aggregate_type = A::AGGREGATE_TYPE,
            instance_id = %id,
            "spawning actor"
        );

        let config = ActorConfig {
            idle_timeout: self.idle_timeout,
        };
        let handle = spawn_actor_with_config::<A>(&stream_dir, config)?;

        let mut cache = self.cache.write().await;
        cache.insert(key, Box::new(handle.clone()));
        Ok(handle)
    }

    /// List all instance IDs for a given aggregate type.
    ///
    /// # Returns
    ///
    /// A sorted `Vec<String>` of instance IDs. Returns an empty vector
    /// if no instances of the given aggregate type exist.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if reading the directory fails.
    pub async fn list<A: Aggregate>(&self) -> io::Result<Vec<String>> {
        let layout = self.layout.clone();
        let agg_type = A::AGGREGATE_TYPE.to_owned();
        tokio::task::spawn_blocking(move || layout.list_streams(&agg_type))
            .await
            .map_err(io::Error::other)?
    }

    /// Create a builder for configuring projections and other options.
    ///
    /// # Arguments
    ///
    /// * `base_dir` - Root directory for all event store data.
    ///
    /// # Returns
    ///
    /// An [`AggregateStoreBuilder`] that can register projections before opening.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use eventfold_es::AggregateStore;
    ///
    /// # async fn example() -> std::io::Result<()> {
    /// let store = AggregateStore::builder("/tmp/my-app")
    ///     // .projection::<MyProjection>()
    ///     .open()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder(base_dir: impl AsRef<Path>) -> AggregateStoreBuilder {
        AggregateStoreBuilder {
            base_dir: base_dir.as_ref().to_owned(),
            projection_factories: Vec::new(),
            process_manager_factories: Vec::new(),
            dispatcher_factories: Vec::new(),
            idle_timeout: DEFAULT_IDLE_TIMEOUT,
        }
    }

    /// Catch up and return the current state of a registered projection.
    ///
    /// Triggers a lazy catch-up before returning: reads any new events
    /// from subscribed streams. Returns a clone of the projection state.
    ///
    /// This method is synchronous (not async). It uses `std::sync` locks
    /// and blocking I/O internally. For embedded use, `catch_up` is fast
    /// for incremental updates. Callers that need an async boundary can
    /// wrap this in `tokio::task::spawn_blocking`.
    ///
    /// # Returns
    ///
    /// A clone of the projection's current state after catching up.
    ///
    /// # Errors
    ///
    /// Returns `io::ErrorKind::NotFound` if the projection is not registered.
    /// Returns `io::Error` if catching up on events fails.
    pub fn projection<P: Projection>(&self) -> io::Result<P> {
        let projections = self
            .projections
            .read()
            .map_err(|e| io::Error::other(e.to_string()))?;
        let runner_any = projections.get(P::NAME).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::NotFound,
                format!("projection '{}' not registered", P::NAME),
            )
        })?;
        // Downcast the type-erased `Box<dyn Any>` back to the concrete
        // `Arc<Mutex<ProjectionRunner<P>>>`. This is safe because
        // `projection()` on the builder registered the runner under the
        // same `P::NAME` key.
        let runner_arc = runner_any
            .downcast_ref::<Arc<std::sync::Mutex<ProjectionRunner<P>>>>()
            .ok_or_else(|| io::Error::other("projection type mismatch"))?;
        let mut runner = runner_arc
            .lock()
            .map_err(|e| io::Error::other(e.to_string()))?;
        runner.catch_up()?;
        Ok(runner.state().clone())
    }

    /// Delete the checkpoint for projection `P` and replay all events from scratch.
    ///
    /// This acquires the projections read-lock, downcasts to the concrete
    /// `ProjectionRunner<P>`, and calls `rebuild()` which:
    /// 1. Deletes the checkpoint file
    /// 2. Resets internal state to `ProjectionCheckpoint::default()`
    /// 3. Calls `catch_up()` to replay all events from offset 0
    /// 4. Saves the new checkpoint
    ///
    /// **Blocking I/O** -- if called from an async context,
    /// wrap this in `tokio::task::spawn_blocking`.
    ///
    /// # Errors
    ///
    /// Returns `io::ErrorKind::NotFound` if the projection is not registered.
    /// Returns `io::Error` if deleting the checkpoint or catching up fails.
    pub fn rebuild_projection<P: Projection>(&self) -> io::Result<()> {
        let projections = self
            .projections
            .read()
            .map_err(|e| io::Error::other(e.to_string()))?;
        let runner_any = projections.get(P::NAME).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::NotFound,
                format!("projection '{}' not registered", P::NAME),
            )
        })?;
        let runner_arc = runner_any
            .downcast_ref::<Arc<std::sync::Mutex<ProjectionRunner<P>>>>()
            .ok_or_else(|| io::Error::other("projection type mismatch"))?;
        let mut runner = runner_arc
            .lock()
            .map_err(|e| io::Error::other(e.to_string()))?;
        runner.rebuild()
    }

    /// Run all registered process managers through a catch-up pass.
    ///
    /// For each process manager:
    /// 1. Catch up on subscribed streams, collecting command envelopes.
    /// 2. Dispatch each envelope to the target aggregate via the type registry.
    /// 3. Write failed dispatches to the per-PM dead-letter log.
    /// 4. Save the process manager checkpoint after all envelopes are handled.
    ///
    /// # Returns
    ///
    /// A [`ProcessManagerReport`] summarizing how many envelopes were
    /// dispatched and how many were dead-lettered.
    ///
    /// # Errors
    ///
    /// Returns `io::Error` if catching up or saving checkpoints fails.
    pub async fn run_process_managers(&self) -> io::Result<ProcessManagerReport> {
        // Collect envelopes and dead-letter paths from each PM under the
        // std::sync::Mutex. The lock is held only for catch_up (blocking I/O).
        let mut all_work: Vec<(Vec<crate::command::CommandEnvelope>, std::path::PathBuf)> =
            Vec::new();

        {
            let pms = self
                .process_managers
                .read()
                .map_err(|e| io::Error::other(e.to_string()))?;
            for pm_mutex in pms.iter() {
                let mut pm = pm_mutex
                    .lock()
                    .map_err(|e| io::Error::other(e.to_string()))?;
                let envelopes = pm.catch_up()?;
                let dead_letter_path = pm.dead_letter_path();
                all_work.push((envelopes, dead_letter_path));
            }
        }

        // Dispatch envelopes asynchronously.
        let mut report = ProcessManagerReport::default();
        for (envelopes, dead_letter_path) in &all_work {
            for envelope in envelopes {
                let agg_type = &envelope.aggregate_type;
                match self.dispatchers.get(agg_type) {
                    Some(dispatcher) => match dispatcher.dispatch(self, envelope.clone()).await {
                        Ok(()) => {
                            tracing::info!(
                                target_type = %agg_type,
                                target_id = %envelope.instance_id,
                                "dispatching command"
                            );
                            report.dispatched += 1;
                        }
                        Err(e) => {
                            tracing::error!(
                                aggregate_type = %agg_type,
                                instance_id = %envelope.instance_id,
                                error = %e,
                                "process manager dispatch failed, dead-lettering"
                            );
                            append_dead_letter(dead_letter_path, envelope.clone(), &e.to_string())?;
                            report.dead_lettered += 1;
                        }
                    },
                    None => {
                        let err_msg = format!("unknown aggregate type: {agg_type}");
                        tracing::error!(
                            aggregate_type = %agg_type,
                            "no dispatcher registered, dead-lettering"
                        );
                        append_dead_letter(dead_letter_path, envelope.clone(), &err_msg)?;
                        report.dead_lettered += 1;
                    }
                }
            }
        }

        // Save all PM checkpoints after dispatch is complete.
        {
            let pms = self
                .process_managers
                .read()
                .map_err(|e| io::Error::other(e.to_string()))?;
            for pm_mutex in pms.iter() {
                let pm = pm_mutex
                    .lock()
                    .map_err(|e| io::Error::other(e.to_string()))?;
                pm.save()?;
            }
        }

        Ok(report)
    }

    /// Returns a reference to the underlying storage layout.
    pub fn layout(&self) -> &StreamLayout {
        &self.layout
    }

    /// List all known `(aggregate_type, instance_id)` pairs.
    ///
    /// When `aggregate_type` is `Some`, returns only streams for that type.
    /// When `None`, returns streams across all aggregate types. Results are
    /// sorted by aggregate type then instance ID.
    ///
    /// # Arguments
    ///
    /// * `aggregate_type` - Optional filter. When `Some`, only streams for
    ///   that aggregate type are returned. When `None`, all streams are
    ///   returned.
    ///
    /// # Returns
    ///
    /// A sorted `Vec<(String, String)>` of `(aggregate_type, instance_id)`
    /// pairs. Returns an empty vector if no matching streams exist.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if reading the directory fails.
    pub async fn list_streams(
        &self,
        aggregate_type: Option<&str>,
    ) -> io::Result<Vec<(String, String)>> {
        let layout = self.layout.clone();
        match aggregate_type {
            Some(agg_type) => {
                let agg_type = agg_type.to_owned();
                tokio::task::spawn_blocking(move || {
                    let ids = layout.list_streams(&agg_type)?;
                    Ok(ids.into_iter().map(|id| (agg_type.clone(), id)).collect())
                })
                .await
                .map_err(io::Error::other)?
            }
            None => tokio::task::spawn_blocking(move || {
                let types = layout.list_aggregate_types()?;
                let mut pairs = Vec::new();
                for agg_type in types {
                    let ids = layout.list_streams(&agg_type)?;
                    pairs.extend(ids.into_iter().map(|id| (agg_type.clone(), id)));
                }
                Ok(pairs)
            })
            .await
            .map_err(io::Error::other)?,
        }
    }

    /// Read all raw events from a stream identified by aggregate type and
    /// instance ID.
    ///
    /// Returns the events in the order they were appended. Does not spawn
    /// an actor or acquire a write lock on the stream.
    ///
    /// # Arguments
    ///
    /// * `aggregate_type` - The aggregate type name (e.g. `"counter"`).
    /// * `instance_id` - The unique instance identifier within that type.
    ///
    /// # Returns
    ///
    /// A `Vec<eventfold::Event>` containing all events in the stream.
    /// Returns `Ok(vec![])` if the stream directory exists but no events
    /// have been written yet.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` with `ErrorKind::NotFound` if the stream
    /// directory does not exist (i.e. the stream was never created).
    /// Returns `std::io::Error` for other I/O failures during reading.
    pub async fn read_events(
        &self,
        aggregate_type: &str,
        instance_id: &str,
    ) -> io::Result<Vec<eventfold::Event>> {
        let layout = self.layout.clone();
        let agg_type = aggregate_type.to_owned();
        let inst_id = instance_id.to_owned();
        tokio::task::spawn_blocking(move || {
            let stream_dir = layout.stream_dir(&agg_type, &inst_id);

            // If the stream directory itself does not exist, return NotFound.
            if !stream_dir.is_dir() {
                return Err(io::Error::new(
                    io::ErrorKind::NotFound,
                    format!("stream directory not found: {}", stream_dir.display()),
                ));
            }

            let reader = eventfold::EventReader::new(&stream_dir);
            // read_from(0) returns NotFound when app.jsonl doesn't exist yet
            // (stream dir created but no events written). Map that to empty vec.
            let iter = match reader.read_from(0) {
                Ok(iter) => iter,
                Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
                Err(e) => return Err(e),
            };

            let mut events = Vec::new();
            for result in iter {
                let (event, _next_offset, _line_hash) = result?;
                events.push(event);
            }
            Ok(events)
        })
        .await
        .map_err(io::Error::other)?
    }

    /// Append a pre-validated event directly to a stream, bypassing command
    /// validation.
    ///
    /// This is the primary entry point for relay-sync scenarios where events
    /// have already been validated on the originating client. The event is
    /// written as-is to the stream's JSONL log, projections are caught up,
    /// and process managers are optionally triggered.
    ///
    /// # Deduplication
    ///
    /// If `event.id` is `Some(id)` and that ID has already been seen by this
    /// store instance, the call returns `Ok(())` immediately without writing.
    /// Events with `event.id = None` are never deduplicated.
    ///
    /// # Actor interaction
    ///
    /// If a live actor exists for the target stream, the event is injected
    /// through the actor's channel (preserving the actor's exclusive writer
    /// ownership). Otherwise, a temporary `EventWriter` is opened directly.
    ///
    /// # Arguments
    ///
    /// * `instance_id` - Unique instance identifier within the aggregate type.
    /// * `event` - A pre-validated `eventfold::Event` to append as-is.
    /// * `opts` - Controls whether process managers run after injection.
    ///
    /// # Returns
    ///
    /// `Ok(())` on success (including dedup no-ops).
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if directory creation, event writing, or
    /// projection catch-up fails.
    pub async fn inject_event<A: Aggregate>(
        &self,
        instance_id: &str,
        event: eventfold::Event,
        opts: InjectOptions,
    ) -> io::Result<()> {
        // 1. Dedup check: if the event has an ID already seen, no-op.
        let event_id = event.id.clone();
        if let Some(ref id) = event_id {
            let seen = self
                .seen_ids
                .lock()
                .map_err(|e| io::Error::other(e.to_string()))?;
            if seen.contains(id) {
                return Ok(());
            }
        }

        // 2. Ensure stream directory exists.
        let layout = self.layout.clone();
        let agg_type = A::AGGREGATE_TYPE.to_owned();
        let inst_id = instance_id.to_owned();
        let stream_dir =
            tokio::task::spawn_blocking(move || layout.ensure_stream(&agg_type, &inst_id))
                .await
                .map_err(io::Error::other)??;

        // 3. Append the event: route through the actor if one is alive,
        //    otherwise open a temporary writer directly.
        let key = (A::AGGREGATE_TYPE.to_owned(), instance_id.to_owned());
        let injected_via_actor = {
            let cache = self.cache.read().await;
            if let Some(boxed) = cache.get(&key)
                && let Some(handle) = boxed.downcast_ref::<AggregateHandle<A>>()
                && handle.is_alive()
            {
                handle.inject_via_actor(event.clone()).await?;
                true
            } else {
                false
            }
        };

        if !injected_via_actor {
            let ev = event;
            tokio::task::spawn_blocking(move || {
                let mut writer = eventfold::EventWriter::open(&stream_dir)?;
                writer.append(&ev).map(|_| ())
            })
            .await
            .map_err(io::Error::other)??;
        }

        // 4. Register event ID in seen_ids after successful append.
        if let Some(id) = event_id {
            let mut seen = self
                .seen_ids
                .lock()
                .map_err(|e| io::Error::other(e.to_string()))?;
            seen.insert(id);
        }

        // 5. Catch up all registered projections via the type-erased list.
        {
            let catch_ups = self
                .projection_catch_ups
                .read()
                .map_err(|e| io::Error::other(e.to_string()))?;
            for catch_up_mutex in catch_ups.iter() {
                let mut catch_up = catch_up_mutex
                    .lock()
                    .map_err(|e| io::Error::other(e.to_string()))?;
                catch_up.catch_up()?;
            }
        }

        // 6. Optionally trigger process managers.
        if opts.run_process_managers {
            self.run_process_managers().await?;
        }

        Ok(())
    }
}

/// Factory function type for creating a type-erased projection runner.
///
/// Each closure captures the concrete `P: Projection` type, creates a
/// `ProjectionRunner<P>`, and returns both:
/// - A `Box<dyn Any + Send + Sync>` (the `Arc<Mutex<ProjectionRunner<P>>>`) for
///   the typed projection map (used by `projection::<P>()`).
/// - A `Mutex<Box<dyn ProjectionCatchUpFn>>` for the type-erased catch-up list
///   (used by `inject_event`).
type ProjectionFactory = Box<
    dyn FnOnce(
        StreamLayout,
    ) -> io::Result<(
        Box<dyn Any + Send + Sync>,
        std::sync::Mutex<Box<dyn ProjectionCatchUpFn>>,
    )>,
>;

/// Factory function type for creating a type-erased process manager runner.
type ProcessManagerFactory =
    Box<dyn FnOnce(StreamLayout) -> io::Result<std::sync::Mutex<Box<dyn ProcessManagerCatchUp>>>>;

/// Factory function type for creating a type-erased aggregate dispatcher.
type DispatcherFactory = Box<dyn FnOnce() -> Box<dyn AggregateDispatcher>>;

/// Builder for configuring an [`AggregateStore`] with projections and
/// process managers.
///
/// Created via [`AggregateStore::builder`]. Register projections with
/// [`projection`](AggregateStoreBuilder::projection), process managers with
/// [`process_manager`](AggregateStoreBuilder::process_manager), and dispatch
/// targets with [`aggregate_type`](AggregateStoreBuilder::aggregate_type),
/// then call [`open`](AggregateStoreBuilder::open) to finalize.
///
/// # Examples
///
/// ```no_run
/// use eventfold_es::AggregateStore;
///
/// # async fn example() -> std::io::Result<()> {
/// let store = AggregateStore::builder("/tmp/my-app")
///     // .projection::<MyProjection>()
///     // .process_manager::<MySaga>()
///     // .aggregate_type::<MyAggregate>()
///     .open()
///     .await?;
/// # Ok(())
/// # }
/// ```
pub struct AggregateStoreBuilder {
    base_dir: PathBuf,
    projection_factories: Vec<(String, ProjectionFactory)>,
    process_manager_factories: Vec<(String, ProcessManagerFactory)>,
    dispatcher_factories: Vec<(String, DispatcherFactory)>,
    idle_timeout: Duration,
}

impl AggregateStoreBuilder {
    /// Register a projection type to be managed by this store.
    ///
    /// The projection will be initialized (loading any existing checkpoint)
    /// when [`open`](AggregateStoreBuilder::open) is called.
    ///
    /// # Type Parameters
    ///
    /// * `P` - A type implementing [`Projection`].
    ///
    /// # Returns
    ///
    /// `self` for method chaining.
    pub fn projection<P: Projection>(mut self) -> Self {
        self.projection_factories.push((
            P::NAME.to_owned(),
            Box::new(|layout| {
                let runner = ProjectionRunner::<P>::new(layout)?;
                let shared = Arc::new(std::sync::Mutex::new(runner));
                // Store the Arc in the typed projection map for downcasting
                // by `AggregateStore::projection::<P>()`.
                let any_box: Box<dyn Any + Send + Sync> = Box::new(shared.clone());
                // Create a type-erased catch-up wrapper sharing the same runner.
                let catch_up: std::sync::Mutex<Box<dyn ProjectionCatchUpFn>> =
                    std::sync::Mutex::new(Box::new(SharedProjectionCatchUp { inner: shared }));
                Ok((any_box, catch_up))
            }),
        ));
        self
    }

    /// Register a process manager type to be managed by this store.
    ///
    /// The process manager will be initialized (loading any existing
    /// checkpoint) when [`open`](AggregateStoreBuilder::open) is called.
    /// Use [`run_process_managers`](AggregateStore::run_process_managers)
    /// to trigger catch-up and dispatch.
    ///
    /// # Type Parameters
    ///
    /// * `PM` - A type implementing [`ProcessManager`](crate::process_manager::ProcessManager).
    ///
    /// # Returns
    ///
    /// `self` for method chaining.
    pub fn process_manager<PM>(mut self) -> Self
    where
        PM: crate::process_manager::ProcessManager,
    {
        self.process_manager_factories.push((
            PM::NAME.to_owned(),
            Box::new(|layout| {
                let runner = ProcessManagerRunner::<PM>::new(layout)?;
                Ok(std::sync::Mutex::new(
                    Box::new(runner) as Box<dyn ProcessManagerCatchUp>
                ))
            }),
        ));
        self
    }

    /// Register an aggregate type as a dispatch target for process managers.
    ///
    /// This allows [`CommandEnvelope`](crate::command::CommandEnvelope)s
    /// targeting this aggregate type to be deserialized and routed. The
    /// aggregate's `Command` type must implement `DeserializeOwned`.
    ///
    /// # Type Parameters
    ///
    /// * `A` - A type implementing [`Aggregate`] with `Command: DeserializeOwned`.
    ///
    /// # Returns
    ///
    /// `self` for method chaining.
    /// Set the idle timeout for actor eviction.
    ///
    /// Actors that receive no messages for this duration will shut down,
    /// releasing their file lock. The next [`get`](AggregateStore::get) call
    /// transparently re-spawns the actor and recovers state from disk.
    ///
    /// Defaults to 5 minutes. Pass `Duration::from_secs(u64::MAX / 2)` to
    /// effectively disable idle eviction.
    ///
    /// # Arguments
    ///
    /// * `timeout` - How long an idle actor waits before shutting down.
    ///
    /// # Returns
    ///
    /// `self` for method chaining.
    pub fn idle_timeout(mut self, timeout: Duration) -> Self {
        self.idle_timeout = timeout;
        self
    }

    /// Register an aggregate type as a dispatch target for process managers.
    ///
    /// This allows [`CommandEnvelope`](crate::command::CommandEnvelope)s
    /// targeting this aggregate type to be deserialized and routed. The
    /// aggregate's `Command` type must implement `DeserializeOwned`.
    ///
    /// # Type Parameters
    ///
    /// * `A` - A type implementing [`Aggregate`] with `Command: DeserializeOwned`.
    ///
    /// # Returns
    ///
    /// `self` for method chaining.
    pub fn aggregate_type<A>(mut self) -> Self
    where
        A: Aggregate,
        A::Command: serde::de::DeserializeOwned,
    {
        self.dispatcher_factories.push((
            A::AGGREGATE_TYPE.to_owned(),
            Box::new(|| Box::new(TypedDispatcher::<A>::new()) as Box<dyn AggregateDispatcher>),
        ));
        self
    }

    /// Build and open the store, initializing all registered projections
    /// and process managers.
    ///
    /// Creates the metadata directory on disk and instantiates each
    /// projection runner and process manager runner (loading persisted
    /// checkpoints if available).
    ///
    /// # Returns
    ///
    /// A fully initialized [`AggregateStore`].
    ///
    /// # Errors
    ///
    /// Returns `io::Error` if directory creation or initialization fails.
    pub async fn open(self) -> io::Result<AggregateStore> {
        let layout = StreamLayout::new(&self.base_dir);
        let meta_dir = layout.meta_dir();
        tokio::task::spawn_blocking(move || std::fs::create_dir_all(meta_dir))
            .await
            .map_err(io::Error::other)??;

        let mut projections = HashMap::new();
        let mut projection_catch_ups: ProjectionCatchUpList = Vec::new();
        for (name, factory) in self.projection_factories {
            let (any_runner, catch_up) = factory(layout.clone())?;
            projections.insert(name, any_runner);
            projection_catch_ups.push(catch_up);
        }

        let mut process_managers = Vec::new();
        for (_name, factory) in self.process_manager_factories {
            let runner = factory(layout.clone())?;
            process_managers.push(runner);
        }

        let mut dispatchers: HashMap<String, Box<dyn AggregateDispatcher>> = HashMap::new();
        for (name, factory) in self.dispatcher_factories {
            dispatchers.insert(name, factory());
        }

        Ok(AggregateStore {
            layout,
            cache: Arc::new(RwLock::new(HashMap::new())),
            projections: Arc::new(std::sync::RwLock::new(projections)),
            projection_catch_ups: Arc::new(std::sync::RwLock::new(projection_catch_ups)),
            process_managers: Arc::new(std::sync::RwLock::new(process_managers)),
            dispatchers: Arc::new(dispatchers),
            seen_ids: Arc::new(std::sync::Mutex::new(HashSet::new())),
            idle_timeout: self.idle_timeout,
        })
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use tempfile::TempDir;

    use super::*;
    use crate::aggregate::test_fixtures::{Counter, CounterCommand};
    use crate::command::CommandContext;

    #[tokio::test]
    async fn full_roundtrip() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let handle = store
            .get::<Counter>("c-1")
            .await
            .expect("get should succeed");

        let ctx = CommandContext::default();
        handle
            .execute(CounterCommand::Increment, ctx.clone())
            .await
            .expect("first increment should succeed");
        handle
            .execute(CounterCommand::Increment, ctx)
            .await
            .expect("second increment should succeed");

        let state = handle.state().await.expect("state should succeed");
        assert_eq!(state.value, 2);
    }

    #[tokio::test]
    async fn list_empty_initially() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let ids = store.list::<Counter>().await.expect("list should succeed");
        assert!(ids.is_empty());
    }

    #[tokio::test]
    async fn list_after_commands() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let ctx = CommandContext::default();

        let h1 = store
            .get::<Counter>("c-1")
            .await
            .expect("get c-1 should succeed");
        h1.execute(CounterCommand::Increment, ctx.clone())
            .await
            .expect("c-1 increment should succeed");

        let h2 = store
            .get::<Counter>("c-2")
            .await
            .expect("get c-2 should succeed");
        h2.execute(CounterCommand::Increment, ctx)
            .await
            .expect("c-2 increment should succeed");

        let mut ids = store.list::<Counter>().await.expect("list should succeed");
        ids.sort();
        assert_eq!(ids, vec!["c-1", "c-2"]);
    }

    #[tokio::test]
    async fn same_id_returns_shared_handle() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let h1 = store
            .get::<Counter>("c-1")
            .await
            .expect("first get should succeed");
        let h2 = store
            .get::<Counter>("c-1")
            .await
            .expect("second get should succeed");

        h1.execute(CounterCommand::Increment, CommandContext::default())
            .await
            .expect("increment via h1 should succeed");

        let state = h2.state().await.expect("state via h2 should succeed");
        assert_eq!(state.value, 1);
    }

    #[tokio::test]
    async fn state_survives_store_reopen() {
        let tmp = TempDir::new().expect("failed to create temp dir");

        // First store: increment 3 times, then drop everything.
        {
            let store = AggregateStore::open(tmp.path())
                .await
                .expect("open should succeed");
            let handle = store
                .get::<Counter>("c-1")
                .await
                .expect("get should succeed");
            let ctx = CommandContext::default();
            for _ in 0..3 {
                handle
                    .execute(CounterCommand::Increment, ctx.clone())
                    .await
                    .expect("increment should succeed");
            }
        }

        // Brief sleep so the actor thread exits and releases the flock.
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Second store on the same directory should recover persisted state.
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("reopen should succeed");
        let handle = store
            .get::<Counter>("c-1")
            .await
            .expect("get after reopen should succeed");
        let state = handle.state().await.expect("state should succeed");
        assert_eq!(state.value, 3);
    }

    #[tokio::test]
    async fn two_aggregate_types_coexist() {
        // A minimal second aggregate type for testing type coexistence.
        #[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
        struct Toggle {
            pub on: bool,
        }

        #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
        #[serde(tag = "type", content = "data")]
        enum ToggleEvent {
            Toggled,
        }

        #[derive(Debug, thiserror::Error)]
        enum ToggleError {}

        impl Aggregate for Toggle {
            const AGGREGATE_TYPE: &'static str = "toggle";
            type Command = ();
            type DomainEvent = ToggleEvent;
            type Error = ToggleError;

            fn handle(&self, _cmd: ()) -> Result<Vec<ToggleEvent>, ToggleError> {
                Ok(vec![ToggleEvent::Toggled])
            }

            fn apply(mut self, _event: &ToggleEvent) -> Self {
                self.on = !self.on;
                self
            }
        }

        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        // Interact with the Counter aggregate.
        let counter_handle = store
            .get::<Counter>("c-1")
            .await
            .expect("get counter should succeed");
        counter_handle
            .execute(CounterCommand::Increment, CommandContext::default())
            .await
            .expect("counter increment should succeed");

        // Interact with the Toggle aggregate.
        let toggle_handle = store
            .get::<Toggle>("t-1")
            .await
            .expect("get toggle should succeed");
        toggle_handle
            .execute((), CommandContext::default())
            .await
            .expect("toggle should succeed");

        // Verify states.
        let counter_state = counter_handle
            .state()
            .await
            .expect("counter state should succeed");
        assert_eq!(counter_state.value, 1);

        let toggle_state = toggle_handle
            .state()
            .await
            .expect("toggle state should succeed");
        assert!(toggle_state.on);

        // Verify listing is type-scoped.
        let counter_ids = store
            .list::<Counter>()
            .await
            .expect("list counters should succeed");
        assert_eq!(counter_ids, vec!["c-1"]);

        let toggle_ids = store
            .list::<Toggle>()
            .await
            .expect("list toggles should succeed");
        assert_eq!(toggle_ids, vec!["t-1"]);
    }

    // --- AggregateStoreBuilder + projection integration tests ---

    use crate::projection::test_fixtures::EventCounter;

    /// Helper: execute a single `Increment` command on the given instance.
    async fn increment(store: &AggregateStore, id: &str) {
        let handle = store.get::<Counter>(id).await.expect("get should succeed");
        handle
            .execute(CounterCommand::Increment, CommandContext::default())
            .await
            .expect("increment should succeed");
    }

    #[tokio::test]
    async fn builder_with_projection_roundtrip() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::builder(tmp.path())
            .projection::<EventCounter>()
            .open()
            .await
            .expect("builder open should succeed");

        let handle = store
            .get::<Counter>("c-1")
            .await
            .expect("get should succeed");
        let ctx = CommandContext::default();
        for _ in 0..3 {
            handle
                .execute(CounterCommand::Increment, ctx.clone())
                .await
                .expect("increment should succeed");
        }

        let counter = store
            .projection::<EventCounter>()
            .expect("projection query should succeed");
        assert_eq!(counter.count, 3);
    }

    #[tokio::test]
    async fn projection_sees_multiple_instances() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::builder(tmp.path())
            .projection::<EventCounter>()
            .open()
            .await
            .expect("builder open should succeed");

        increment(&store, "c-1").await;
        increment(&store, "c-2").await;

        let counter = store
            .projection::<EventCounter>()
            .expect("projection query should succeed");
        assert_eq!(counter.count, 2);
    }

    #[tokio::test]
    async fn projection_persists_across_restart() {
        let tmp = TempDir::new().expect("failed to create temp dir");

        // First store: execute events and query the projection (triggers save).
        {
            let store = AggregateStore::builder(tmp.path())
                .projection::<EventCounter>()
                .open()
                .await
                .expect("builder open should succeed");

            increment(&store, "c-1").await;
            increment(&store, "c-1").await;
            increment(&store, "c-2").await;

            let counter = store
                .projection::<EventCounter>()
                .expect("projection query should succeed");
            assert_eq!(counter.count, 3);
        }

        // Brief sleep so actor threads exit and release flocks.
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Second store on the same directory: projection should restore
        // from checkpoint without replaying events.
        let store = AggregateStore::builder(tmp.path())
            .projection::<EventCounter>()
            .open()
            .await
            .expect("reopen should succeed");

        let counter = store
            .projection::<EventCounter>()
            .expect("projection query after reopen should succeed");
        assert_eq!(counter.count, 3);
    }

    #[tokio::test]
    async fn projection_without_registration_returns_error() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let result = store.projection::<EventCounter>();
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
    }

    #[tokio::test]
    async fn open_convenience_still_works() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let handle = store
            .get::<Counter>("c-1")
            .await
            .expect("get should succeed");
        handle
            .execute(CounterCommand::Increment, CommandContext::default())
            .await
            .expect("increment should succeed");

        let state = handle.state().await.expect("state should succeed");
        assert_eq!(state.value, 1);
    }

    // --- Process manager integration tests ---
    //
    // These tests use a "Receiver" aggregate that accepts deserializable
    // commands, plus a "ForwardSaga" process manager that reacts to Counter
    // events and dispatches commands to the Receiver.

    /// A minimal aggregate that accepts JSON-deserializable commands.
    /// Used as a dispatch target for process manager tests.
    #[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
    struct Receiver {
        pub received_count: u64,
    }

    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
    #[serde(tag = "type", content = "data")]
    enum ReceiverCommand {
        Accept,
    }

    #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
    #[serde(tag = "type", content = "data")]
    enum ReceiverEvent {
        Accepted,
    }

    #[derive(Debug, thiserror::Error)]
    enum ReceiverError {}

    impl Aggregate for Receiver {
        const AGGREGATE_TYPE: &'static str = "receiver";
        type Command = ReceiverCommand;
        type DomainEvent = ReceiverEvent;
        type Error = ReceiverError;

        fn handle(&self, _cmd: ReceiverCommand) -> Result<Vec<ReceiverEvent>, ReceiverError> {
            Ok(vec![ReceiverEvent::Accepted])
        }

        fn apply(mut self, _event: &ReceiverEvent) -> Self {
            self.received_count += 1;
            self
        }
    }

    /// A process manager that reacts to counter events by dispatching
    /// `Accept` commands to the Receiver aggregate.
    #[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
    struct ForwardSaga {
        pub forwarded: u64,
    }

    impl crate::process_manager::ProcessManager for ForwardSaga {
        const NAME: &'static str = "forward-saga";

        fn subscriptions(&self) -> &'static [&'static str] {
            &["counter"]
        }

        fn react(
            &mut self,
            _aggregate_type: &str,
            stream_id: &str,
            _event: &eventfold::Event,
        ) -> Vec<crate::command::CommandEnvelope> {
            self.forwarded += 1;
            vec![crate::command::CommandEnvelope {
                aggregate_type: "receiver".to_string(),
                instance_id: stream_id.to_string(),
                command: serde_json::json!({"type": "Accept"}),
                context: CommandContext::default(),
            }]
        }
    }

    #[tokio::test]
    async fn end_to_end_process_manager_dispatch() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::builder(tmp.path())
            .process_manager::<ForwardSaga>()
            .aggregate_type::<Receiver>()
            .open()
            .await
            .expect("builder open should succeed");

        // Produce events in the Counter aggregate.
        increment(&store, "c-1").await;
        increment(&store, "c-1").await;

        // Run process managers: should catch up, dispatch to Receiver.
        let report = store
            .run_process_managers()
            .await
            .expect("run_process_managers should succeed");

        assert_eq!(report.dispatched, 2);
        assert_eq!(report.dead_lettered, 0);

        // Verify the Receiver aggregate received the dispatched commands.
        let receiver_handle = store
            .get::<Receiver>("c-1")
            .await
            .expect("get receiver should succeed");
        let receiver_state = receiver_handle
            .state()
            .await
            .expect("receiver state should succeed");
        assert_eq!(receiver_state.received_count, 2);
    }

    #[tokio::test]
    async fn process_manager_dead_letters_unknown_type() {
        /// A process manager that emits commands to a non-existent aggregate.
        #[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
        struct BadTargetSaga {
            seen: u64,
        }

        impl crate::process_manager::ProcessManager for BadTargetSaga {
            const NAME: &'static str = "bad-target-saga";

            fn subscriptions(&self) -> &'static [&'static str] {
                &["counter"]
            }

            fn react(
                &mut self,
                _aggregate_type: &str,
                _stream_id: &str,
                _event: &eventfold::Event,
            ) -> Vec<crate::command::CommandEnvelope> {
                self.seen += 1;
                vec![crate::command::CommandEnvelope {
                    aggregate_type: "nonexistent".to_string(),
                    instance_id: "x".to_string(),
                    command: serde_json::json!({}),
                    context: CommandContext::default(),
                }]
            }
        }

        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::builder(tmp.path())
            .process_manager::<BadTargetSaga>()
            .open()
            .await
            .expect("builder open should succeed");

        increment(&store, "c-1").await;

        let report = store
            .run_process_managers()
            .await
            .expect("run_process_managers should succeed");

        assert_eq!(report.dispatched, 0);
        assert_eq!(report.dead_lettered, 1);

        // Verify dead-letter file is readable JSONL.
        let dl_path = tmp
            .path()
            .join("process_managers/bad-target-saga/dead_letters.jsonl");
        let contents = std::fs::read_to_string(&dl_path).expect("dead-letter file should exist");
        let entry: serde_json::Value =
            serde_json::from_str(contents.trim()).expect("dead-letter entry should be valid JSON");
        assert!(
            entry["error"]
                .as_str()
                .expect("error field should be a string")
                .contains("nonexistent")
        );
    }

    #[tokio::test]
    async fn run_process_managers_idempotent() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::builder(tmp.path())
            .process_manager::<ForwardSaga>()
            .aggregate_type::<Receiver>()
            .open()
            .await
            .expect("builder open should succeed");

        increment(&store, "c-1").await;

        // First run: dispatches 1 envelope.
        let first = store
            .run_process_managers()
            .await
            .expect("first run should succeed");
        assert_eq!(first.dispatched, 1);

        // Second run with no new events: should be a no-op.
        let second = store
            .run_process_managers()
            .await
            .expect("second run should succeed");
        assert_eq!(second.dispatched, 0);
        assert_eq!(second.dead_lettered, 0);
    }

    #[tokio::test]
    async fn process_manager_recovers_after_restart() {
        let tmp = TempDir::new().expect("failed to create temp dir");

        // First store: process 2 events.
        {
            let store = AggregateStore::builder(tmp.path())
                .process_manager::<ForwardSaga>()
                .aggregate_type::<Receiver>()
                .open()
                .await
                .expect("builder open should succeed");

            increment(&store, "c-1").await;
            increment(&store, "c-2").await;

            let report = store
                .run_process_managers()
                .await
                .expect("run should succeed");
            assert_eq!(report.dispatched, 2);
        }

        // Brief sleep so actor threads exit.
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Second store: add 1 more event, run PM again.
        let store = AggregateStore::builder(tmp.path())
            .process_manager::<ForwardSaga>()
            .aggregate_type::<Receiver>()
            .open()
            .await
            .expect("reopen should succeed");

        increment(&store, "c-1").await;

        let report = store
            .run_process_managers()
            .await
            .expect("run after restart should succeed");

        // Should only dispatch the 1 new event, not replay old ones.
        assert_eq!(report.dispatched, 1);
        assert_eq!(report.dead_lettered, 0);
    }

    // --- Idle eviction tests ---

    #[tokio::test]
    async fn idle_actor_evicted_and_respawned() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::builder(tmp.path())
            .idle_timeout(Duration::from_millis(200))
            .open()
            .await
            .expect("builder open should succeed");

        // Execute a command.
        let handle = store
            .get::<Counter>("c-1")
            .await
            .expect("get should succeed");
        handle
            .execute(CounterCommand::Increment, CommandContext::default())
            .await
            .expect("increment should succeed");

        // Wait for the actor to idle out.
        tokio::time::sleep(Duration::from_millis(400)).await;
        assert!(
            !handle.is_alive(),
            "actor should be dead after idle timeout"
        );

        // A new `get` should transparently re-spawn.
        let handle2 = store
            .get::<Counter>("c-1")
            .await
            .expect("get after eviction should succeed");
        let state = handle2.state().await.expect("state should succeed");
        assert_eq!(state.value, 1, "state should reflect persisted events");
    }

    #[tokio::test]
    async fn rapid_commands_keep_actor_alive() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::builder(tmp.path())
            .idle_timeout(Duration::from_millis(300))
            .open()
            .await
            .expect("builder open should succeed");

        let handle = store
            .get::<Counter>("c-1")
            .await
            .expect("get should succeed");

        let ctx = CommandContext::default();
        for _ in 0..5 {
            handle
                .execute(CounterCommand::Increment, ctx.clone())
                .await
                .expect("execute should succeed");
            tokio::time::sleep(Duration::from_millis(100)).await;
        }

        assert!(
            handle.is_alive(),
            "actor should remain alive during activity"
        );
        let state = handle.state().await.expect("state should succeed");
        assert_eq!(state.value, 5);
    }

    // --- inject_event tests ---

    /// Helper: build an event matching Counter's "Incremented" variant.
    fn incremented_event() -> eventfold::Event {
        eventfold::Event::new("Incremented", serde_json::Value::Null)
    }

    #[tokio::test]
    async fn inject_event_appends_to_stream() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        store
            .inject_event::<Counter>("c-1", incremented_event(), InjectOptions::default())
            .await
            .expect("inject_event should succeed");

        // Verify the event appears in the JSONL file.
        let jsonl_path = tmp.path().join("streams/counter/c-1/app.jsonl");
        let contents = std::fs::read_to_string(&jsonl_path).expect("app.jsonl should exist");
        assert_eq!(
            contents.lines().count(),
            1,
            "should have exactly one event line"
        );
    }

    #[tokio::test]
    async fn inject_event_projections_reflect_event() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::builder(tmp.path())
            .projection::<EventCounter>()
            .open()
            .await
            .expect("builder open should succeed");

        store
            .inject_event::<Counter>("c-1", incremented_event(), InjectOptions::default())
            .await
            .expect("inject_event should succeed");

        let counter = store
            .projection::<EventCounter>()
            .expect("projection query should succeed");
        assert_eq!(counter.count, 1);
    }

    #[tokio::test]
    async fn inject_event_dedup_by_id() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let event = incremented_event().with_id("ev-1".to_string());

        // First injection should succeed and write.
        store
            .inject_event::<Counter>("c-1", event.clone(), InjectOptions::default())
            .await
            .expect("first inject should succeed");

        // Second injection with the same ID should be a no-op.
        store
            .inject_event::<Counter>("c-1", event, InjectOptions::default())
            .await
            .expect("second inject should succeed (no-op)");

        // Verify only one event in the JSONL.
        let jsonl_path = tmp.path().join("streams/counter/c-1/app.jsonl");
        let contents = std::fs::read_to_string(&jsonl_path).expect("app.jsonl should exist");
        assert_eq!(
            contents.lines().count(),
            1,
            "dedup should prevent second write"
        );
    }

    #[tokio::test]
    async fn inject_event_no_dedup_for_none_id() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        // Events with id=None should never be deduplicated.
        let event = incremented_event();
        assert!(event.id.is_none(), "precondition: id is None");

        store
            .inject_event::<Counter>("c-1", event.clone(), InjectOptions::default())
            .await
            .expect("first inject should succeed");

        store
            .inject_event::<Counter>("c-1", event, InjectOptions::default())
            .await
            .expect("second inject should succeed");

        let jsonl_path = tmp.path().join("streams/counter/c-1/app.jsonl");
        let contents = std::fs::read_to_string(&jsonl_path).expect("app.jsonl should exist");
        assert_eq!(contents.lines().count(), 2, "both events should be written");
    }

    #[tokio::test]
    async fn inject_options_default_does_not_run_process_managers() {
        let opts = InjectOptions::default();
        assert!(!opts.run_process_managers);
    }

    #[tokio::test]
    async fn inject_event_with_process_managers() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::builder(tmp.path())
            .process_manager::<ForwardSaga>()
            .aggregate_type::<Receiver>()
            .open()
            .await
            .expect("builder open should succeed");

        store
            .inject_event::<Counter>(
                "c-1",
                incremented_event(),
                InjectOptions {
                    run_process_managers: true,
                },
            )
            .await
            .expect("inject_event should succeed");

        // The ForwardSaga should have dispatched a command to Receiver.
        let receiver_handle = store
            .get::<Receiver>("c-1")
            .await
            .expect("get receiver should succeed");
        let receiver_state = receiver_handle
            .state()
            .await
            .expect("receiver state should succeed");
        assert_eq!(
            receiver_state.received_count, 1,
            "process manager should have dispatched"
        );
    }

    #[tokio::test]
    async fn inject_event_with_live_actor() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        // Spawn an actor for c-1 by getting a handle.
        let handle = store
            .get::<Counter>("c-1")
            .await
            .expect("get should succeed");
        assert!(handle.is_alive(), "actor should be alive");

        // Inject an event -- should route through the live actor.
        store
            .inject_event::<Counter>("c-1", incremented_event(), InjectOptions::default())
            .await
            .expect("inject_event with live actor should succeed");

        // The actor's view should reflect the injected event.
        let state = handle.state().await.expect("state should succeed");
        assert_eq!(state.value, 1, "actor should see the injected event");
    }

    #[tokio::test]
    async fn inject_event_creates_new_stream() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        // Inject into a stream that doesn't exist yet.
        store
            .inject_event::<Counter>(
                "new-instance",
                incremented_event(),
                InjectOptions::default(),
            )
            .await
            .expect("inject_event should create stream");

        // Verify the directory was created.
        let stream_dir = tmp.path().join("streams/counter/new-instance");
        assert!(stream_dir.is_dir(), "stream directory should exist");

        // Verify state via a fresh actor.
        let handle = store
            .get::<Counter>("new-instance")
            .await
            .expect("get should succeed after inject");
        let state = handle.state().await.expect("state should succeed");
        assert_eq!(state.value, 1, "actor should replay the injected event");
    }

    // --- list_streams / read_events tests ---

    // Minimal second aggregate type shared by list_streams tests.
    #[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
    struct Toggle {
        pub on: bool,
    }

    #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
    #[serde(tag = "type", content = "data")]
    enum ToggleEvent {
        Toggled,
    }

    #[derive(Debug, thiserror::Error)]
    enum ToggleError {}

    impl Aggregate for Toggle {
        const AGGREGATE_TYPE: &'static str = "toggle";
        type Command = ();
        type DomainEvent = ToggleEvent;
        type Error = ToggleError;

        fn handle(&self, _cmd: ()) -> Result<Vec<ToggleEvent>, ToggleError> {
            Ok(vec![ToggleEvent::Toggled])
        }

        fn apply(mut self, _event: &ToggleEvent) -> Self {
            self.on = !self.on;
            self
        }
    }

    /// Helper: execute a single Toggle command on the given instance.
    async fn toggle(store: &AggregateStore, id: &str) {
        let handle = store
            .get::<Toggle>(id)
            .await
            .expect("get toggle should succeed");
        handle
            .execute((), CommandContext::default())
            .await
            .expect("toggle should succeed");
    }

    #[tokio::test]
    async fn list_streams_none_returns_all_sorted() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        // Create counter instances c-1, c-2 and toggle instance t-1.
        increment(&store, "c-1").await;
        increment(&store, "c-2").await;
        toggle(&store, "t-1").await;

        let pairs = store
            .list_streams(None)
            .await
            .expect("list_streams(None) should succeed");

        assert_eq!(
            pairs,
            vec![
                ("counter".to_owned(), "c-1".to_owned()),
                ("counter".to_owned(), "c-2".to_owned()),
                ("toggle".to_owned(), "t-1".to_owned()),
            ]
        );
    }

    #[tokio::test]
    async fn list_streams_some_filters_by_type() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        increment(&store, "c-1").await;
        increment(&store, "c-2").await;
        toggle(&store, "t-1").await;

        let pairs = store
            .list_streams(Some("counter"))
            .await
            .expect("list_streams(Some) should succeed");

        assert_eq!(
            pairs,
            vec![
                ("counter".to_owned(), "c-1".to_owned()),
                ("counter".to_owned(), "c-2".to_owned()),
            ]
        );
    }

    #[tokio::test]
    async fn list_streams_none_empty_store() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let pairs = store
            .list_streams(None)
            .await
            .expect("list_streams(None) on empty store should succeed");

        assert!(pairs.is_empty());
    }

    #[tokio::test]
    async fn list_streams_some_nonexistent_type() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let pairs = store
            .list_streams(Some("nonexistent"))
            .await
            .expect("list_streams(Some(nonexistent)) should succeed");

        assert!(pairs.is_empty());
    }

    #[tokio::test]
    async fn read_events_returns_all_events() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        increment(&store, "c-1").await;
        increment(&store, "c-1").await;

        let events = store
            .read_events("counter", "c-1")
            .await
            .expect("read_events should succeed");

        assert_eq!(events.len(), 2);
        assert_eq!(events[0].event_type, "Incremented");
        assert_eq!(events[1].event_type, "Incremented");
    }

    #[tokio::test]
    async fn read_events_empty_stream_returns_empty_vec() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        // Create the stream directory without executing any commands.
        let _handle = store
            .get::<Counter>("c-1")
            .await
            .expect("get should succeed");

        // Drop the handle's actor so the flock is released, then read.
        // The stream directory exists but app.jsonl may or may not exist.
        // In either case read_events should return Ok(vec![]).
        let events = store
            .read_events("counter", "c-1")
            .await
            .expect("read_events on empty stream should succeed");

        assert!(events.is_empty());
    }

    #[tokio::test]
    async fn read_events_nonexistent_stream_returns_not_found() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let store = AggregateStore::open(tmp.path())
            .await
            .expect("open should succeed");

        let result = store.read_events("nonexistent", "x").await;

        assert!(result.is_err());
        assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
    }
}