dactor 0.2.0

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

use crate::actor::Actor;

/// Uniquely identifies a persistent actor's journal/snapshot in storage.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PersistenceId(pub String);

impl PersistenceId {
    /// Create a persistence ID from entity type and entity ID (joined by `:`).
    pub fn new(entity_type: &str, entity_id: &str) -> Self {
        Self(format!("{entity_type}:{entity_id}"))
    }
}

impl std::fmt::Display for PersistenceId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Monotonically increasing event sequence number.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SequenceId(pub i64);

impl SequenceId {
    /// Return the next sequence ID.
    pub fn next(self) -> Self {
        Self(self.0 + 1)
    }
}

/// A single journal entry (event).
#[derive(Debug, Clone)]
pub struct JournalEntry {
    /// Sequence number of this event.
    pub sequence_id: SequenceId,
    /// Type name of the event (for deserialization dispatch).
    pub event_type: String,
    /// Serialized event payload.
    pub payload: Vec<u8>,
}

/// A single snapshot entry.
#[derive(Debug, Clone)]
pub struct SnapshotEntry {
    /// Sequence number at which the snapshot was taken.
    pub sequence_id: SequenceId,
    /// Serialized snapshot payload.
    pub payload: Vec<u8>,
}

/// Errors from persistence operations.
#[derive(Debug)]
pub enum PersistError {
    /// The backing store is unreachable or returned an error.
    StorageUnavailable(String),
    /// Serialization or deserialization of the payload failed.
    SerializationFailed(String),
    /// A stored entry is corrupt or cannot be decoded.
    CorruptEntry {
        /// Sequence number of the corrupt entry.
        sequence_id: SequenceId,
        /// Description of the corruption.
        detail: String,
    },
    /// No storage provider has been configured.
    NotConfigured,
}

impl std::fmt::Display for PersistError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::StorageUnavailable(msg) => write!(f, "storage unavailable: {}", msg),
            Self::SerializationFailed(msg) => write!(f, "serialization failed: {}", msg),
            Self::CorruptEntry {
                sequence_id,
                detail,
            } => write!(f, "corrupt entry at {:?}: {}", sequence_id, detail),
            Self::NotConfigured => write!(f, "no storage provider configured"),
        }
    }
}

impl std::error::Error for PersistError {}

/// What to do when recovery fails.
#[derive(Debug, Clone, Default)]
pub enum RecoveryFailurePolicy {
    /// Stop the actor on recovery failure (default).
    #[default]
    Stop,
    /// Retry recovery with backoff.
    Retry {
        /// Maximum number of retry attempts (None = unlimited).
        max_attempts: Option<u32>,
        /// Initial delay before the first retry.
        initial_delay: Duration,
    },
    /// Skip recovery and start with default state.
    SkipAndStart,
}

/// What to do when a persist operation fails.
#[derive(Debug, Clone, Default)]
pub enum PersistFailurePolicy {
    /// Stop the actor on persist failure (default).
    #[default]
    Stop,
    /// Return the error to the caller.
    ReturnError,
    /// Retry the persist operation with backoff.
    Retry {
        /// Maximum number of retry attempts (None = unlimited).
        max_attempts: Option<u32>,
        /// Initial delay before the first retry.
        initial_delay: Duration,
    },
}

/// Controls automatic snapshotting for event-sourced actors.
#[derive(Debug, Clone, Default)]
pub struct SnapshotConfig {
    /// Take a snapshot every N events (None = disabled).
    pub every_n_events: Option<u64>,
    /// Take a snapshot at this interval (None = disabled).
    pub interval: Option<Duration>,
    /// Keep at most N snapshots (None = keep all).
    pub retention_count: Option<u32>,
    /// Whether to delete journal events after a successful snapshot.
    pub delete_events_on_snapshot: bool,
}

/// Controls automatic state saving for durable state actors.
#[derive(Debug, Clone, Default)]
pub struct SaveConfig {
    /// Save state every N messages (None = disabled).
    pub every_n_messages: Option<u64>,
    /// Save state at this interval (None = disabled).
    pub interval: Option<Duration>,
}

// ── Storage Traits ──────────────────────────────────

/// Pluggable journal storage for event-sourced actors.
#[async_trait::async_trait]
pub trait JournalStorage: Send + Sync + 'static {
    /// Append a single event to the journal.
    async fn write_event(
        &self,
        persistence_id: &PersistenceId,
        sequence_id: SequenceId,
        event_type: &str,
        payload: &[u8],
    ) -> Result<(), PersistError>;

    /// Append a batch of events to the journal atomically.
    async fn write_event_batch(
        &self,
        persistence_id: &PersistenceId,
        entries: &[(SequenceId, &str, &[u8])],
    ) -> Result<(), PersistError>;

    /// Read all events starting from `from_sequence`.
    async fn read_events(
        &self,
        persistence_id: &PersistenceId,
        from_sequence: SequenceId,
    ) -> Result<Vec<JournalEntry>, PersistError>;

    /// Read the highest sequence number in the journal.
    async fn read_highest_sequence(
        &self,
        persistence_id: &PersistenceId,
    ) -> Result<Option<SequenceId>, PersistError>;

    /// Delete all events up to and including `to_sequence`.
    async fn delete_events_to(
        &self,
        persistence_id: &PersistenceId,
        to_sequence: SequenceId,
    ) -> Result<(), PersistError>;
}

/// Pluggable snapshot storage.
#[async_trait::async_trait]
pub trait SnapshotStorage: Send + Sync + 'static {
    /// Save a snapshot at the given sequence number.
    async fn save_snapshot(
        &self,
        persistence_id: &PersistenceId,
        sequence_id: SequenceId,
        payload: &[u8],
    ) -> Result<(), PersistError>;

    /// Load the most recent snapshot.
    async fn load_latest_snapshot(
        &self,
        persistence_id: &PersistenceId,
    ) -> Result<Option<SnapshotEntry>, PersistError>;

    /// Delete all snapshots before the given sequence number.
    async fn delete_snapshots_before(
        &self,
        persistence_id: &PersistenceId,
        sequence_id: SequenceId,
    ) -> Result<(), PersistError>;
}

/// Pluggable state store for durable state actors.
#[async_trait::async_trait]
pub trait StateStorage: Send + Sync + 'static {
    /// Save the current state.
    async fn save_state(
        &self,
        persistence_id: &PersistenceId,
        payload: &[u8],
    ) -> Result<(), PersistError>;

    /// Load the previously saved state, if any.
    async fn load_state(
        &self,
        persistence_id: &PersistenceId,
    ) -> Result<Option<Vec<u8>>, PersistError>;

    /// Delete the saved state.
    async fn delete_state(&self, persistence_id: &PersistenceId) -> Result<(), PersistError>;
}

// ── In-Memory Storage (for testing) ─────────────────

/// In-memory storage implementation for testing.
/// Stores journal events, snapshots, and state in HashMaps.
///
/// **Note:** Uses `std::sync::Mutex` (not `tokio::sync::Mutex`) intentionally.
/// All lock-guarded operations are immediate HashMap lookups with no `.await`
/// points while the lock is held. Per tokio docs, `std::sync::Mutex` is
/// preferred for short, non-async critical sections.
pub struct InMemoryStorage {
    journals: Mutex<HashMap<String, Vec<JournalEntry>>>,
    snapshots: Mutex<HashMap<String, Vec<SnapshotEntry>>>,
    states: Mutex<HashMap<String, Vec<u8>>>,
}

impl InMemoryStorage {
    /// Create a new empty in-memory storage.
    pub fn new() -> Self {
        Self {
            journals: Mutex::new(HashMap::new()),
            snapshots: Mutex::new(HashMap::new()),
            states: Mutex::new(HashMap::new()),
        }
    }
}

impl Default for InMemoryStorage {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl JournalStorage for InMemoryStorage {
    async fn write_event(
        &self,
        persistence_id: &PersistenceId,
        sequence_id: SequenceId,
        event_type: &str,
        payload: &[u8],
    ) -> Result<(), PersistError> {
        let mut journals = self.journals.lock().unwrap();
        let entries = journals.entry(persistence_id.0.clone()).or_default();
        entries.push(JournalEntry {
            sequence_id,
            event_type: event_type.to_string(),
            payload: payload.to_vec(),
        });
        Ok(())
    }

    async fn write_event_batch(
        &self,
        persistence_id: &PersistenceId,
        batch: &[(SequenceId, &str, &[u8])],
    ) -> Result<(), PersistError> {
        let mut journals = self.journals.lock().unwrap();
        let entries = journals.entry(persistence_id.0.clone()).or_default();
        for (seq, event_type, payload) in batch {
            entries.push(JournalEntry {
                sequence_id: *seq,
                event_type: event_type.to_string(),
                payload: payload.to_vec(),
            });
        }
        Ok(())
    }

    async fn read_events(
        &self,
        persistence_id: &PersistenceId,
        from_sequence: SequenceId,
    ) -> Result<Vec<JournalEntry>, PersistError> {
        let journals = self.journals.lock().unwrap();
        Ok(journals
            .get(&persistence_id.0)
            .map(|entries| {
                entries
                    .iter()
                    .filter(|e| e.sequence_id >= from_sequence)
                    .cloned()
                    .collect()
            })
            .unwrap_or_default())
    }

    async fn read_highest_sequence(
        &self,
        persistence_id: &PersistenceId,
    ) -> Result<Option<SequenceId>, PersistError> {
        let journals = self.journals.lock().unwrap();
        Ok(journals
            .get(&persistence_id.0)
            .and_then(|entries| entries.last().map(|e| e.sequence_id)))
    }

    async fn delete_events_to(
        &self,
        persistence_id: &PersistenceId,
        to_sequence: SequenceId,
    ) -> Result<(), PersistError> {
        let mut journals = self.journals.lock().unwrap();
        if let Some(entries) = journals.get_mut(&persistence_id.0) {
            entries.retain(|e| e.sequence_id > to_sequence);
        }
        Ok(())
    }
}

#[async_trait::async_trait]
impl SnapshotStorage for InMemoryStorage {
    async fn save_snapshot(
        &self,
        persistence_id: &PersistenceId,
        sequence_id: SequenceId,
        payload: &[u8],
    ) -> Result<(), PersistError> {
        let mut snapshots = self.snapshots.lock().unwrap();
        let entries = snapshots.entry(persistence_id.0.clone()).or_default();
        entries.push(SnapshotEntry {
            sequence_id,
            payload: payload.to_vec(),
        });
        Ok(())
    }

    async fn load_latest_snapshot(
        &self,
        persistence_id: &PersistenceId,
    ) -> Result<Option<SnapshotEntry>, PersistError> {
        let snapshots = self.snapshots.lock().unwrap();
        Ok(snapshots
            .get(&persistence_id.0)
            .and_then(|entries| entries.last().cloned()))
    }

    async fn delete_snapshots_before(
        &self,
        persistence_id: &PersistenceId,
        sequence_id: SequenceId,
    ) -> Result<(), PersistError> {
        let mut snapshots = self.snapshots.lock().unwrap();
        if let Some(entries) = snapshots.get_mut(&persistence_id.0) {
            entries.retain(|e| e.sequence_id >= sequence_id);
        }
        Ok(())
    }
}

#[async_trait::async_trait]
impl StateStorage for InMemoryStorage {
    async fn save_state(
        &self,
        persistence_id: &PersistenceId,
        payload: &[u8],
    ) -> Result<(), PersistError> {
        let mut states = self.states.lock().unwrap();
        states.insert(persistence_id.0.clone(), payload.to_vec());
        Ok(())
    }

    async fn load_state(
        &self,
        persistence_id: &PersistenceId,
    ) -> Result<Option<Vec<u8>>, PersistError> {
        let states = self.states.lock().unwrap();
        Ok(states.get(&persistence_id.0).cloned())
    }

    async fn delete_state(&self, persistence_id: &PersistenceId) -> Result<(), PersistError> {
        let mut states = self.states.lock().unwrap();
        states.remove(&persistence_id.0);
        Ok(())
    }
}

// ── Actor-facing Persistence Traits ──────────────────

/// Marker trait for actors that persist their state.
/// Provides the persistence identity and lifecycle hooks for recovery.
pub trait PersistentActor: Actor {
    /// Unique persistence identity for this actor's journal/snapshots.
    fn persistence_id(&self) -> PersistenceId;

    /// Called before recovery starts. Default: no-op.
    fn pre_recovery(&mut self) {}

    /// Called after recovery completes successfully. Default: no-op.
    fn post_recovery(&mut self) {}

    /// Policy when recovery fails. Default: Stop.
    fn recovery_failure_policy(&self) -> RecoveryFailurePolicy {
        RecoveryFailurePolicy::Stop
    }

    /// Policy when a persist operation fails. Default: Stop.
    fn persist_failure_policy(&self) -> PersistFailurePolicy {
        PersistFailurePolicy::Stop
    }
}

/// Event-sourced actor that rebuilds state by replaying events.
///
/// The actor's state is derived entirely from a sequence of events.
/// `apply()` is called for each event during recovery (replay) and
/// during normal operation (after persist). It must be deterministic.
#[async_trait::async_trait]
pub trait EventSourced: PersistentActor {
    /// The event type for this actor. Must be serializable for storage.
    type Event: Send + Sync + 'static;

    /// Apply an event to the actor's state. Must be **deterministic** and
    /// **infallible** — the same sequence of events must always produce the
    /// same state. Panicking in `apply()` is a programming error.
    fn apply(&mut self, event: &Self::Event);

    /// Deserialize an event from raw bytes (used during replay).
    fn deserialize_event(&self, payload: &[u8]) -> Result<Self::Event, PersistError>;

    /// Serialize an event to raw bytes (used when persisting).
    fn serialize_event(&self, event: &Self::Event) -> Result<Vec<u8>, PersistError>;

    /// Persist an event to the journal, then apply it to the state.
    /// The event is first written to storage, then applied via `apply()`.
    /// Returns Err on storage/serialization failure; the caller (runtime)
    /// should consult `persist_failure_policy()` to decide next action.
    async fn persist(
        &mut self,
        event: Self::Event,
        journal: &dyn JournalStorage,
    ) -> Result<SequenceId, PersistError> {
        let next_seq = self.last_sequence_id().next();
        let payload = self.serialize_event(&event)?;
        let event_type = std::any::type_name::<Self::Event>();
        journal
            .write_event(&self.persistence_id(), next_seq, event_type, &payload)
            .await?;
        self.apply(&event);
        self.set_last_sequence_id(next_seq);
        Ok(next_seq)
    }

    /// Persist multiple events atomically, then apply them in order.
    async fn persist_batch(
        &mut self,
        events: Vec<Self::Event>,
        journal: &dyn JournalStorage,
    ) -> Result<SequenceId, PersistError> {
        if events.is_empty() {
            return Ok(self.last_sequence_id());
        }
        let pid = self.persistence_id();
        let event_type = std::any::type_name::<Self::Event>();
        let mut seq = self.last_sequence_id();
        let mut batch: Vec<(SequenceId, &str, Vec<u8>)> = Vec::with_capacity(events.len());
        for event in &events {
            seq = seq.next();
            let payload = self.serialize_event(event)?;
            batch.push((seq, event_type, payload));
        }
        let refs: Vec<(SequenceId, &str, &[u8])> = batch
            .iter()
            .map(|(s, t, p)| (*s, *t, p.as_slice()))
            .collect();
        journal.write_event_batch(&pid, &refs).await?;
        for event in &events {
            self.apply(event);
        }
        self.set_last_sequence_id(seq);
        Ok(seq)
    }

    /// Create a snapshot of the current state.
    async fn snapshot(&self, snapshots: &dyn SnapshotStorage) -> Result<(), PersistError> {
        let payload = self.snapshot_payload()?;
        snapshots
            .save_snapshot(&self.persistence_id(), self.last_sequence_id(), &payload)
            .await
    }

    /// Restore state from a snapshot. Called during recovery before
    /// replaying events that occurred after the snapshot.
    fn restore_snapshot(&mut self, payload: Vec<u8>) -> Result<(), PersistError>;

    /// Serialize the current state for snapshotting.
    fn snapshot_payload(&self) -> Result<Vec<u8>, PersistError>;

    /// Optional snapshot configuration for automatic snapshots.
    fn snapshot_config(&self) -> Option<SnapshotConfig> {
        None
    }

    /// The last applied sequence ID. Used to determine where to start
    /// replaying events during recovery.
    fn last_sequence_id(&self) -> SequenceId;

    /// Set the last sequence ID (called during recovery).
    fn set_last_sequence_id(&mut self, seq: SequenceId);
}

/// Durable state actor that persists the entire state as a snapshot.
///
/// Unlike EventSourced, this stores the full state each time rather
/// than individual events. Simpler but less efficient for large states
/// with small changes.
#[async_trait::async_trait]
pub trait DurableState: PersistentActor {
    /// Serialize the current state for storage.
    fn serialize_state(&self) -> Result<Vec<u8>, PersistError>;

    /// Restore state from stored bytes. Called during recovery.
    fn restore_state(&mut self, payload: Vec<u8>) -> Result<(), PersistError>;

    /// Save the current state to storage.
    async fn save_state(&self, storage: &dyn StateStorage) -> Result<(), PersistError> {
        let payload = self.serialize_state()?;
        storage.save_state(&self.persistence_id(), &payload).await
    }

    /// Optional save configuration for automatic saves.
    fn save_config(&self) -> Option<SaveConfig> {
        None
    }
}

/// Provides storage instances for persistent actors.
/// Register on the runtime to enable persistence.
pub trait StorageProvider: Send + Sync + 'static {
    /// Get a journal storage instance for the given persistence ID.
    fn journal(&self, id: &PersistenceId) -> Option<Arc<dyn JournalStorage>>;

    /// Get a snapshot storage instance for the given persistence ID.
    fn snapshots(&self, id: &PersistenceId) -> Option<Arc<dyn SnapshotStorage>>;

    /// Get a state storage instance for the given persistence ID.
    fn state(&self, id: &PersistenceId) -> Option<Arc<dyn StateStorage>>;
}

/// In-memory storage provider for testing. Wraps a single [`InMemoryStorage`]
/// and returns it for every persistence ID.
pub struct InMemoryStorageProvider {
    storage: Arc<InMemoryStorage>,
}

impl InMemoryStorageProvider {
    /// Create a new in-memory storage provider.
    pub fn new() -> Self {
        Self {
            storage: Arc::new(InMemoryStorage::new()),
        }
    }

    /// Create from an existing `Arc<InMemoryStorage>` for shared access.
    pub fn from_storage(storage: Arc<InMemoryStorage>) -> Self {
        Self { storage }
    }
}

impl Default for InMemoryStorageProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl StorageProvider for InMemoryStorageProvider {
    fn journal(&self, _id: &PersistenceId) -> Option<Arc<dyn JournalStorage>> {
        Some(self.storage.clone())
    }

    fn snapshots(&self, _id: &PersistenceId) -> Option<Arc<dyn SnapshotStorage>> {
        Some(self.storage.clone())
    }

    fn state(&self, _id: &PersistenceId) -> Option<Arc<dyn StateStorage>> {
        Some(self.storage.clone())
    }
}

/// Recover an event-sourced actor by loading the latest snapshot and
/// replaying journal events that occurred after it.
///
/// Events are deserialized in full before any are applied. This ensures
/// that a deserialization failure on event N doesn't leave the actor in
/// a partially-recovered state (livelock on retry).
///
/// Consults `actor.recovery_failure_policy()` on error:
/// - `Stop` — propagate the error (default).
/// - `Retry { max_attempts, initial_delay }` — retry with backoff.
/// - `SkipAndStart` — skip recovery and start fresh.
pub async fn recover_event_sourced<A: EventSourced>(
    actor: &mut A,
    journal: &dyn JournalStorage,
    snapshots: &dyn SnapshotStorage,
) -> Result<(), PersistError> {
    let policy = actor.recovery_failure_policy();

    match recover_event_sourced_inner(actor, journal, snapshots).await {
        Ok(()) => Ok(()),
        Err(e) => match policy {
            RecoveryFailurePolicy::Stop => Err(e),
            RecoveryFailurePolicy::Retry {
                max_attempts,
                initial_delay,
            } => {
                let attempts = max_attempts.unwrap_or(3);
                let mut delay = initial_delay;
                for _ in 1..attempts {
                    tokio::time::sleep(delay).await;
                    delay = std::time::Duration::from_millis(
                        (delay.as_millis() as u64).saturating_mul(2).min(30_000),
                    );
                    match recover_event_sourced_inner(actor, journal, snapshots).await {
                        Ok(()) => return Ok(()),
                        Err(_) => continue,
                    }
                }
                Err(e)
            }
            RecoveryFailurePolicy::SkipAndStart => {
                actor.post_recovery();
                Ok(())
            }
        },
    }
}

async fn recover_event_sourced_inner<A: EventSourced>(
    actor: &mut A,
    journal: &dyn JournalStorage,
    snapshots: &dyn SnapshotStorage,
) -> Result<(), PersistError> {
    actor.pre_recovery();

    // Load latest snapshot
    let pid = actor.persistence_id();
    if let Some(snap) = snapshots.load_latest_snapshot(&pid).await? {
        actor.restore_snapshot(snap.payload)?;
        actor.set_last_sequence_id(snap.sequence_id);
    }

    // Replay events after the snapshot.
    // Deserialize ALL events first to avoid partial apply on failure.
    let start = actor.last_sequence_id().next();
    let entries = journal.read_events(&pid, start).await?;
    let deserialized: Vec<(SequenceId, A::Event)> = entries
        .iter()
        .map(|entry| {
            let event = actor.deserialize_event(&entry.payload)?;
            Ok((entry.sequence_id, event))
        })
        .collect::<Result<Vec<_>, PersistError>>()?;

    // All deserialized successfully — now apply. apply() is infallible.
    for (seq, event) in &deserialized {
        actor.apply(event);
        actor.set_last_sequence_id(*seq);
    }

    actor.post_recovery();
    Ok(())
}

/// Recover a durable-state actor by loading its stored state.
///
/// Consults `actor.recovery_failure_policy()` on error:
/// - `Stop` — propagate the error (default).
/// - `Retry { max_attempts, initial_delay }` — retry with backoff.
/// - `SkipAndStart` — skip recovery and start fresh.
pub async fn recover_durable_state<A: DurableState>(
    actor: &mut A,
    storage: &dyn StateStorage,
) -> Result<(), PersistError> {
    let policy = actor.recovery_failure_policy();

    match recover_durable_state_inner(actor, storage).await {
        Ok(()) => Ok(()),
        Err(e) => match policy {
            RecoveryFailurePolicy::Stop => Err(e),
            RecoveryFailurePolicy::Retry {
                max_attempts,
                initial_delay,
            } => {
                let attempts = max_attempts.unwrap_or(3);
                let mut delay = initial_delay;
                for _ in 1..attempts {
                    tokio::time::sleep(delay).await;
                    delay = std::time::Duration::from_millis(
                        (delay.as_millis() as u64).saturating_mul(2).min(30_000),
                    );
                    match recover_durable_state_inner(actor, storage).await {
                        Ok(()) => return Ok(()),
                        Err(_) => continue,
                    }
                }
                Err(e)
            }
            RecoveryFailurePolicy::SkipAndStart => {
                actor.post_recovery();
                Ok(())
            }
        },
    }
}

async fn recover_durable_state_inner<A: DurableState>(
    actor: &mut A,
    storage: &dyn StateStorage,
) -> Result<(), PersistError> {
    actor.pre_recovery();

    let pid = actor.persistence_id();
    if let Some(payload) = storage.load_state(&pid).await? {
        actor.restore_state(payload)?;
    }

    actor.post_recovery();
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_persistence_id() {
        let id = PersistenceId::new("BankAccount", "acct-123");
        assert_eq!(id.0, "BankAccount:acct-123");
        assert_eq!(format!("{}", id), "BankAccount:acct-123");
    }

    #[test]
    fn test_sequence_id_ordering() {
        let s1 = SequenceId(1);
        let s2 = SequenceId(2);
        assert!(s1 < s2);
        assert_eq!(s1.next(), s2);
    }

    #[tokio::test]
    async fn test_journal_write_read() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        storage
            .write_event(&pid, SequenceId(1), "Created", b"data1")
            .await
            .unwrap();
        storage
            .write_event(&pid, SequenceId(2), "Updated", b"data2")
            .await
            .unwrap();

        let events = storage.read_events(&pid, SequenceId(1)).await.unwrap();
        assert_eq!(events.len(), 2);
        assert_eq!(events[0].event_type, "Created");
        assert_eq!(events[1].payload, b"data2");
    }

    #[tokio::test]
    async fn test_journal_read_from_sequence() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        for i in 1..=5 {
            storage
                .write_event(&pid, SequenceId(i), "Event", &[i as u8])
                .await
                .unwrap();
        }
        let events = storage.read_events(&pid, SequenceId(3)).await.unwrap();
        assert_eq!(events.len(), 3); // seq 3, 4, 5
    }

    #[tokio::test]
    async fn test_journal_highest_sequence() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        assert_eq!(storage.read_highest_sequence(&pid).await.unwrap(), None);
        storage
            .write_event(&pid, SequenceId(1), "E", b"")
            .await
            .unwrap();
        storage
            .write_event(&pid, SequenceId(5), "E", b"")
            .await
            .unwrap();
        assert_eq!(
            storage.read_highest_sequence(&pid).await.unwrap(),
            Some(SequenceId(5))
        );
    }

    #[tokio::test]
    async fn test_journal_delete_events() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        for i in 1..=5 {
            storage
                .write_event(&pid, SequenceId(i), "E", &[i as u8])
                .await
                .unwrap();
        }
        storage.delete_events_to(&pid, SequenceId(3)).await.unwrap();
        let events = storage.read_events(&pid, SequenceId(1)).await.unwrap();
        assert_eq!(events.len(), 2); // seq 4, 5
    }

    #[tokio::test]
    async fn test_journal_batch_write() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        let batch = vec![
            (SequenceId(1), "A", b"a" as &[u8]),
            (SequenceId(2), "B", b"b"),
            (SequenceId(3), "C", b"c"),
        ];
        storage.write_event_batch(&pid, &batch).await.unwrap();
        let events = storage.read_events(&pid, SequenceId(1)).await.unwrap();
        assert_eq!(events.len(), 3);
    }

    #[tokio::test]
    async fn test_snapshot_save_load() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        storage
            .save_snapshot(&pid, SequenceId(10), b"state-at-10")
            .await
            .unwrap();
        let snap = storage.load_latest_snapshot(&pid).await.unwrap().unwrap();
        assert_eq!(snap.sequence_id, SequenceId(10));
        assert_eq!(snap.payload, b"state-at-10");
    }

    #[tokio::test]
    async fn test_snapshot_latest() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        storage
            .save_snapshot(&pid, SequenceId(5), b"old")
            .await
            .unwrap();
        storage
            .save_snapshot(&pid, SequenceId(10), b"new")
            .await
            .unwrap();
        let snap = storage.load_latest_snapshot(&pid).await.unwrap().unwrap();
        assert_eq!(snap.payload, b"new");
    }

    #[tokio::test]
    async fn test_snapshot_delete_before() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        storage
            .save_snapshot(&pid, SequenceId(5), b"s1")
            .await
            .unwrap();
        storage
            .save_snapshot(&pid, SequenceId(10), b"s2")
            .await
            .unwrap();
        storage
            .delete_snapshots_before(&pid, SequenceId(10))
            .await
            .unwrap();
        let snap = storage.load_latest_snapshot(&pid).await.unwrap().unwrap();
        assert_eq!(snap.payload, b"s2");
    }

    #[tokio::test]
    async fn test_state_save_load() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        storage.save_state(&pid, b"my-state").await.unwrap();
        let state = storage.load_state(&pid).await.unwrap().unwrap();
        assert_eq!(state, b"my-state");
    }

    #[tokio::test]
    async fn test_state_overwrite() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        storage.save_state(&pid, b"v1").await.unwrap();
        storage.save_state(&pid, b"v2").await.unwrap();
        let state = storage.load_state(&pid).await.unwrap().unwrap();
        assert_eq!(state, b"v2");
    }

    #[tokio::test]
    async fn test_state_delete() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "1");
        storage.save_state(&pid, b"data").await.unwrap();
        storage.delete_state(&pid).await.unwrap();
        assert!(storage.load_state(&pid).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_empty_reads() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Test", "nonexistent");
        assert!(storage
            .read_events(&pid, SequenceId(1))
            .await
            .unwrap()
            .is_empty());
        assert!(storage.load_latest_snapshot(&pid).await.unwrap().is_none());
        assert!(storage.load_state(&pid).await.unwrap().is_none());
    }

    #[test]
    fn test_persist_error_display() {
        let err = PersistError::StorageUnavailable("db down".into());
        assert!(format!("{}", err).contains("db down"));
    }

    #[test]
    fn test_recovery_failure_policy_default() {
        assert!(matches!(
            RecoveryFailurePolicy::default(),
            RecoveryFailurePolicy::Stop
        ));
    }

    #[test]
    fn test_snapshot_config_default() {
        let cfg = SnapshotConfig::default();
        assert!(cfg.every_n_events.is_none());
        assert!(!cfg.delete_events_on_snapshot);
    }

    // ── Mock actor for trait tests ──────────────────────

    use crate::actor::Actor;

    /// Minimal event-sourced counter used to test the persistence traits.
    struct CounterActor {
        id: String,
        value: i64,
        last_seq: SequenceId,
        recovered: bool,
    }

    impl CounterActor {
        fn new(id: &str) -> Self {
            Self {
                id: id.to_string(),
                value: 0,
                last_seq: SequenceId(0),
                recovered: false,
            }
        }
    }

    impl Actor for CounterActor {
        type Args = ();
        type Deps = ();
        fn create(_args: (), _deps: ()) -> Self {
            CounterActor::new("default")
        }
    }

    impl PersistentActor for CounterActor {
        fn persistence_id(&self) -> PersistenceId {
            PersistenceId::new("Counter", &self.id)
        }

        fn post_recovery(&mut self) {
            self.recovered = true;
        }
    }

    /// Simple i64 delta event.
    #[derive(Debug, Clone)]
    struct CounterEvent(i64);

    #[async_trait::async_trait]
    impl EventSourced for CounterActor {
        type Event = CounterEvent;

        fn apply(&mut self, event: &CounterEvent) {
            self.value += event.0;
        }

        fn deserialize_event(&self, payload: &[u8]) -> Result<CounterEvent, PersistError> {
            if payload.len() != 8 {
                return Err(PersistError::SerializationFailed("bad length".into()));
            }
            let bytes: [u8; 8] = payload.try_into().unwrap();
            Ok(CounterEvent(i64::from_le_bytes(bytes)))
        }

        fn serialize_event(&self, event: &CounterEvent) -> Result<Vec<u8>, PersistError> {
            Ok(event.0.to_le_bytes().to_vec())
        }

        fn restore_snapshot(&mut self, payload: Vec<u8>) -> Result<(), PersistError> {
            if payload.len() != 8 {
                return Err(PersistError::SerializationFailed("bad snapshot".into()));
            }
            let bytes: [u8; 8] = payload.try_into().unwrap();
            self.value = i64::from_le_bytes(bytes);
            Ok(())
        }

        fn snapshot_payload(&self) -> Result<Vec<u8>, PersistError> {
            Ok(self.value.to_le_bytes().to_vec())
        }

        fn last_sequence_id(&self) -> SequenceId {
            self.last_seq
        }

        fn set_last_sequence_id(&mut self, seq: SequenceId) {
            self.last_seq = seq;
        }
    }

    #[tokio::test]
    async fn test_event_sourced_persist_and_apply() {
        let storage = InMemoryStorage::new();
        let mut actor = CounterActor::new("1");

        let seq = actor.persist(CounterEvent(10), &storage).await.unwrap();
        assert_eq!(seq, SequenceId(1));
        assert_eq!(actor.value, 10);

        let seq = actor.persist(CounterEvent(-3), &storage).await.unwrap();
        assert_eq!(seq, SequenceId(2));
        assert_eq!(actor.value, 7);

        // Verify journal entries were written
        let pid = actor.persistence_id();
        let entries = storage.read_events(&pid, SequenceId(1)).await.unwrap();
        assert_eq!(entries.len(), 2);
    }

    #[tokio::test]
    async fn test_event_sourced_persist_batch() {
        let storage = InMemoryStorage::new();
        let mut actor = CounterActor::new("batch");

        let events = vec![CounterEvent(5), CounterEvent(3), CounterEvent(2)];
        let seq = actor.persist_batch(events, &storage).await.unwrap();
        assert_eq!(seq, SequenceId(3));
        assert_eq!(actor.value, 10);

        let pid = actor.persistence_id();
        let entries = storage.read_events(&pid, SequenceId(1)).await.unwrap();
        assert_eq!(entries.len(), 3);
    }

    #[tokio::test]
    async fn test_event_sourced_persist_batch_empty() {
        let storage = InMemoryStorage::new();
        let mut actor = CounterActor::new("empty-batch");

        let seq = actor.persist_batch(vec![], &storage).await.unwrap();
        assert_eq!(seq, SequenceId(0));
        assert_eq!(actor.value, 0);
    }

    #[tokio::test]
    async fn test_event_sourced_snapshot() {
        let storage = InMemoryStorage::new();
        let mut actor = CounterActor::new("snap");

        actor.persist(CounterEvent(42), &storage).await.unwrap();
        actor.snapshot(&storage).await.unwrap();

        let pid = actor.persistence_id();
        let snap = storage.load_latest_snapshot(&pid).await.unwrap().unwrap();
        assert_eq!(snap.sequence_id, SequenceId(1));
        let bytes: [u8; 8] = snap.payload.try_into().unwrap();
        assert_eq!(i64::from_le_bytes(bytes), 42);
    }

    #[tokio::test]
    async fn test_recover_event_sourced_events_only() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Counter", "recover");

        // Pre-populate journal
        storage
            .write_event(&pid, SequenceId(1), "CounterEvent", &10i64.to_le_bytes())
            .await
            .unwrap();
        storage
            .write_event(&pid, SequenceId(2), "CounterEvent", &5i64.to_le_bytes())
            .await
            .unwrap();

        let mut actor = CounterActor::new("recover");
        recover_event_sourced(&mut actor, &storage, &storage)
            .await
            .unwrap();

        assert_eq!(actor.value, 15);
        assert_eq!(actor.last_seq, SequenceId(2));
        assert!(actor.recovered);
    }

    #[tokio::test]
    async fn test_recover_event_sourced_with_snapshot() {
        let storage = InMemoryStorage::new();
        let pid = PersistenceId::new("Counter", "snap-recover");

        // Snapshot at seq 2 with value 15
        storage
            .save_snapshot(&pid, SequenceId(2), &15i64.to_le_bytes())
            .await
            .unwrap();
        // Event after snapshot
        storage
            .write_event(&pid, SequenceId(3), "CounterEvent", &7i64.to_le_bytes())
            .await
            .unwrap();

        let mut actor = CounterActor::new("snap-recover");
        recover_event_sourced(&mut actor, &storage, &storage)
            .await
            .unwrap();

        assert_eq!(actor.value, 22); // 15 + 7
        assert_eq!(actor.last_seq, SequenceId(3));
        assert!(actor.recovered);
    }

    #[tokio::test]
    async fn test_recover_event_sourced_empty() {
        let storage = InMemoryStorage::new();
        let mut actor = CounterActor::new("empty");

        recover_event_sourced(&mut actor, &storage, &storage)
            .await
            .unwrap();

        assert_eq!(actor.value, 0);
        assert_eq!(actor.last_seq, SequenceId(0));
        assert!(actor.recovered);
    }

    // ── Durable state mock ──────────────────────────────

    struct ConfigActor {
        id: String,
        data: String,
        recovered: bool,
    }

    impl ConfigActor {
        fn new(id: &str) -> Self {
            Self {
                id: id.to_string(),
                data: String::new(),
                recovered: false,
            }
        }
    }

    impl Actor for ConfigActor {
        type Args = ();
        type Deps = ();
        fn create(_args: (), _deps: ()) -> Self {
            ConfigActor::new("default")
        }
    }

    impl PersistentActor for ConfigActor {
        fn persistence_id(&self) -> PersistenceId {
            PersistenceId::new("Config", &self.id)
        }

        fn post_recovery(&mut self) {
            self.recovered = true;
        }
    }

    #[async_trait::async_trait]
    impl DurableState for ConfigActor {
        fn serialize_state(&self) -> Result<Vec<u8>, PersistError> {
            Ok(self.data.as_bytes().to_vec())
        }

        fn restore_state(&mut self, payload: Vec<u8>) -> Result<(), PersistError> {
            self.data = String::from_utf8(payload)
                .map_err(|e| PersistError::SerializationFailed(e.to_string()))?;
            Ok(())
        }
    }

    #[tokio::test]
    async fn test_durable_state_save_and_recover() {
        let storage = InMemoryStorage::new();
        let _pid = PersistenceId::new("Config", "ds");

        // Save state via trait default impl
        let mut actor = ConfigActor::new("ds");
        actor.data = "hello world".to_string();
        DurableState::save_state(&actor, &storage as &dyn StateStorage)
            .await
            .unwrap();

        // Recover into a fresh actor
        let mut actor2 = ConfigActor::new("ds");
        recover_durable_state(&mut actor2, &storage).await.unwrap();
        assert_eq!(actor2.data, "hello world");
        assert!(actor2.recovered);
    }

    #[tokio::test]
    async fn test_recover_durable_state_empty() {
        let storage = InMemoryStorage::new();
        let mut actor = ConfigActor::new("empty");

        recover_durable_state(&mut actor, &storage).await.unwrap();
        assert_eq!(actor.data, "");
        assert!(actor.recovered);
    }

    // ── StorageProvider tests ───────────────────────────

    #[test]
    fn test_in_memory_storage_provider() {
        let provider = InMemoryStorageProvider::new();
        let pid = PersistenceId::new("Test", "1");

        assert!(provider.journal(&pid).is_some());
        assert!(provider.snapshots(&pid).is_some());
        assert!(provider.state(&pid).is_some());
    }

    #[tokio::test]
    async fn test_in_memory_storage_provider_shared() {
        let raw = Arc::new(InMemoryStorage::new());
        let provider = InMemoryStorageProvider::from_storage(raw.clone());
        let pid = PersistenceId::new("Test", "shared");

        // Write via provider, read via raw storage
        let journal = provider.journal(&pid).unwrap();
        journal
            .write_event(&pid, SequenceId(1), "E", b"data")
            .await
            .unwrap();
        let events = raw.read_events(&pid, SequenceId(1)).await.unwrap();
        assert_eq!(events.len(), 1);
    }

    #[test]
    fn test_persistent_actor_default_policies() {
        let actor = CounterActor::new("policy");
        assert!(matches!(
            actor.recovery_failure_policy(),
            RecoveryFailurePolicy::Stop
        ));
        assert!(matches!(
            actor.persist_failure_policy(),
            PersistFailurePolicy::Stop
        ));
    }
}