durable-lambda-core 1.2.0

Core replay engine, types, and operation logic for AWS Lambda durable execution 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
//! Replay engine — operation-keyed state with visited tracking.
//!
//! Implement FR1-FR5: history loading, replay/execute mode detection,
//! cached result return, checkpoint execution, and replay status transitions.
//!
//! The replay engine uses a `HashMap<String, Operation>` keyed by operation ID
//! (matching the Python SDK's approach) and tracks which operations have been
//! visited. The replay status transitions from `Replaying` to `Executing` when
//! all completed operations in history have been visited.

use std::collections::{HashMap, HashSet};

use aws_sdk_lambda::types::{Operation, OperationStatus};

use crate::operation_id::OperationIdGenerator;
use crate::types::ExecutionMode;

/// Manage replay state for a durable execution.
///
/// The engine holds the complete operation state loaded from AWS, tracks which
/// operations have been visited during the current invocation, and determines
/// whether the execution is replaying cached results or executing new work.
///
/// # Replay Status Transitions
///
/// - Starts in [`ExecutionMode::Replaying`] if completed operations exist in history.
/// - Starts in [`ExecutionMode::Executing`] if history is empty or has no completed operations.
/// - Transitions from `Replaying` to `Executing` when all completed operations
///   have been visited via [`track_replay`](Self::track_replay).
///
/// # Examples
///
/// ```
/// use durable_lambda_core::replay::ReplayEngine;
/// use durable_lambda_core::types::ExecutionMode;
/// use std::collections::HashMap;
///
/// // Empty history → starts in Executing mode.
/// let engine = ReplayEngine::new(HashMap::new(), None);
/// assert_eq!(engine.execution_mode(), ExecutionMode::Executing);
/// ```
pub struct ReplayEngine {
    /// All operations from the durable execution state, keyed by operation ID.
    operations: HashMap<String, Operation>,
    /// Operation IDs that have been visited during the current invocation.
    visited: HashSet<String>,
    /// IDs of operations with completed statuses (cached at init for perf).
    completed_ids: HashSet<String>,
    /// Current replay/execute mode.
    mode: ExecutionMode,
    /// Deterministic operation ID generator.
    id_generator: OperationIdGenerator,
}

/// Check whether an operation status represents a completed state.
///
/// Completed statuses: `Succeeded`, `Failed`, `Cancelled`, `TimedOut`, `Stopped`.
fn is_completed_status(status: &OperationStatus) -> bool {
    matches!(
        status,
        OperationStatus::Succeeded
            | OperationStatus::Failed
            | OperationStatus::Cancelled
            | OperationStatus::TimedOut
            | OperationStatus::Stopped
    )
}

impl ReplayEngine {
    /// Create a new replay engine from loaded operations.
    ///
    /// Sets the initial [`ExecutionMode`] based on whether completed operations
    /// exist in the history. Operations with type `Execution` are excluded from
    /// replay tracking (they represent the root invocation, not user operations).
    ///
    /// # Arguments
    ///
    /// * `operations` — All operations from the durable execution state, keyed by ID.
    /// * `parent_id` — Parent operation ID for child context scoping (`None` for root).
    ///
    /// # Examples
    ///
    /// ```
    /// use durable_lambda_core::replay::ReplayEngine;
    /// use durable_lambda_core::types::ExecutionMode;
    /// use std::collections::HashMap;
    ///
    /// let engine = ReplayEngine::new(HashMap::new(), None);
    /// assert_eq!(engine.execution_mode(), ExecutionMode::Executing);
    /// ```
    pub fn new(operations: HashMap<String, Operation>, parent_id: Option<String>) -> Self {
        let completed_ids: HashSet<String> = operations
            .iter()
            .filter(|(_, op)| {
                is_completed_status(&op.status)
                    && op.r#type != aws_sdk_lambda::types::OperationType::Execution
            })
            .map(|(id, _)| id.clone())
            .collect();

        let mode = if completed_ids.is_empty() {
            ExecutionMode::Executing
        } else {
            ExecutionMode::Replaying
        };

        Self {
            operations,
            visited: HashSet::new(),
            completed_ids,
            mode,
            id_generator: OperationIdGenerator::new(parent_id),
        }
    }

    /// Look up an operation by ID, returning it if it exists with a completed status.
    ///
    /// Returns `None` if the operation doesn't exist or is not in a completed state.
    ///
    /// # Examples
    ///
    /// ```
    /// use durable_lambda_core::replay::ReplayEngine;
    /// use std::collections::HashMap;
    ///
    /// let engine = ReplayEngine::new(HashMap::new(), None);
    /// assert!(engine.check_result("nonexistent").is_none());
    /// ```
    pub fn check_result(&self, operation_id: &str) -> Option<&Operation> {
        self.operations
            .get(operation_id)
            .filter(|op| is_completed_status(&op.status))
    }

    /// Mark an operation as visited and update replay status.
    ///
    /// After visiting, checks whether all completed operations have been visited.
    /// If so, transitions the mode from [`ExecutionMode::Replaying`] to
    /// [`ExecutionMode::Executing`].
    ///
    /// # Examples
    ///
    /// ```
    /// use durable_lambda_core::replay::ReplayEngine;
    /// use std::collections::HashMap;
    ///
    /// let mut engine = ReplayEngine::new(HashMap::new(), None);
    /// engine.track_replay("some-op-id");
    /// ```
    pub fn track_replay(&mut self, operation_id: &str) {
        self.visited.insert(operation_id.to_string());

        if self.mode == ExecutionMode::Replaying && self.completed_ids.is_subset(&self.visited) {
            self.mode = ExecutionMode::Executing;
        }
    }

    /// Return whether the engine is currently in replay mode.
    ///
    /// # Examples
    ///
    /// ```
    /// use durable_lambda_core::replay::ReplayEngine;
    /// use std::collections::HashMap;
    ///
    /// let engine = ReplayEngine::new(HashMap::new(), None);
    /// assert!(!engine.is_replaying());
    /// ```
    pub fn is_replaying(&self) -> bool {
        self.mode == ExecutionMode::Replaying
    }

    /// Return the current execution mode.
    ///
    /// # Examples
    ///
    /// ```
    /// use durable_lambda_core::replay::ReplayEngine;
    /// use durable_lambda_core::types::ExecutionMode;
    /// use std::collections::HashMap;
    ///
    /// let engine = ReplayEngine::new(HashMap::new(), None);
    /// assert_eq!(engine.execution_mode(), ExecutionMode::Executing);
    /// ```
    pub fn execution_mode(&self) -> ExecutionMode {
        self.mode.clone()
    }

    /// Generate the next deterministic operation ID.
    ///
    /// Delegates to the internal [`OperationIdGenerator`].
    ///
    /// # Examples
    ///
    /// ```
    /// use durable_lambda_core::replay::ReplayEngine;
    /// use std::collections::HashMap;
    ///
    /// let mut engine = ReplayEngine::new(HashMap::new(), None);
    /// let id = engine.generate_operation_id();
    /// assert_eq!(id.len(), 64);
    /// ```
    pub fn generate_operation_id(&mut self) -> String {
        self.id_generator.next_id()
    }

    /// Look up an operation by ID, returning it regardless of status.
    ///
    /// Unlike [`check_result`](Self::check_result) which only returns
    /// operations in a completed status, this returns the operation in
    /// any status (Started, Pending, Succeeded, etc.). Used by callback
    /// operations that need to extract the server-generated `callback_id`
    /// from operations that may still be in a non-completed state.
    ///
    /// # Examples
    ///
    /// ```
    /// use durable_lambda_core::replay::ReplayEngine;
    /// use std::collections::HashMap;
    ///
    /// let engine = ReplayEngine::new(HashMap::new(), None);
    /// assert!(engine.get_operation("nonexistent").is_none());
    /// ```
    pub fn get_operation(&self, operation_id: &str) -> Option<&Operation> {
        self.operations.get(operation_id)
    }

    /// Return a reference to the operations map.
    ///
    /// # Examples
    ///
    /// ```
    /// use durable_lambda_core::replay::ReplayEngine;
    /// use std::collections::HashMap;
    ///
    /// let engine = ReplayEngine::new(HashMap::new(), None);
    /// assert!(engine.operations().is_empty());
    /// ```
    pub fn operations(&self) -> &HashMap<String, Operation> {
        &self.operations
    }

    /// Insert or update an operation in the state.
    ///
    /// If the operation has a completed status (and is not the root `Execution`
    /// type), it is added to the completed set for replay tracking.
    ///
    /// # Examples
    ///
    /// ```
    /// use durable_lambda_core::replay::ReplayEngine;
    /// use aws_sdk_lambda::types::{Operation, OperationType, OperationStatus};
    /// use std::collections::HashMap;
    ///
    /// let mut engine = ReplayEngine::new(HashMap::new(), None);
    /// assert!(engine.operations().is_empty());
    ///
    /// let op = Operation::builder()
    ///     .id("op-1")
    ///     .r#type(OperationType::Step)
    ///     .status(OperationStatus::Succeeded)
    ///     .start_timestamp(aws_smithy_types::DateTime::from_secs(0))
    ///     .build()
    ///     .unwrap();
    /// engine.insert_operation("op-1".to_string(), op);
    /// assert_eq!(engine.operations().len(), 1);
    /// ```
    pub fn insert_operation(&mut self, id: String, operation: Operation) {
        if is_completed_status(&operation.status)
            && operation.r#type != aws_sdk_lambda::types::OperationType::Execution
        {
            self.completed_ids.insert(id.clone());
        }
        self.operations.insert(id, operation);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use aws_sdk_lambda::types::{Operation, OperationStatus, OperationType};
    fn make_operation(id: &str, status: OperationStatus, op_type: OperationType) -> Operation {
        Operation::builder()
            .id(id)
            .r#type(op_type)
            .status(status)
            .start_timestamp(aws_smithy_types::DateTime::from_secs(0))
            .build()
            .unwrap()
    }

    #[test]
    fn empty_history_starts_executing() {
        let engine = ReplayEngine::new(HashMap::new(), None);
        assert_eq!(engine.execution_mode(), ExecutionMode::Executing);
        assert!(!engine.is_replaying());
    }

    #[test]
    fn completed_operations_start_replaying() {
        let mut ops = HashMap::new();
        ops.insert(
            "op1".to_string(),
            make_operation("op1", OperationStatus::Succeeded, OperationType::Step),
        );

        let engine = ReplayEngine::new(ops, None);
        assert_eq!(engine.execution_mode(), ExecutionMode::Replaying);
        assert!(engine.is_replaying());
    }

    #[test]
    fn only_pending_operations_start_executing() {
        let mut ops = HashMap::new();
        ops.insert(
            "op1".to_string(),
            make_operation("op1", OperationStatus::Pending, OperationType::Step),
        );

        let engine = ReplayEngine::new(ops, None);
        assert_eq!(engine.execution_mode(), ExecutionMode::Executing);
    }

    #[test]
    fn execution_type_excluded_from_replay_tracking() {
        let mut ops = HashMap::new();
        // Only an EXECUTION-type completed op — should NOT count for replay.
        ops.insert(
            "exec".to_string(),
            make_operation("exec", OperationStatus::Succeeded, OperationType::Execution),
        );

        let engine = ReplayEngine::new(ops, None);
        assert_eq!(engine.execution_mode(), ExecutionMode::Executing);
    }

    #[test]
    fn transitions_to_executing_after_all_visited() {
        let mut ops = HashMap::new();
        ops.insert(
            "op1".to_string(),
            make_operation("op1", OperationStatus::Succeeded, OperationType::Step),
        );
        ops.insert(
            "op2".to_string(),
            make_operation("op2", OperationStatus::Failed, OperationType::Step),
        );

        let mut engine = ReplayEngine::new(ops, None);
        assert!(engine.is_replaying());

        engine.track_replay("op1");
        assert!(engine.is_replaying()); // Still replaying — op2 not visited.

        engine.track_replay("op2");
        assert!(!engine.is_replaying()); // All completed ops visited → Executing.
        assert_eq!(engine.execution_mode(), ExecutionMode::Executing);
    }

    #[test]
    fn check_result_returns_completed_operations() {
        let mut ops = HashMap::new();
        ops.insert(
            "op1".to_string(),
            make_operation("op1", OperationStatus::Succeeded, OperationType::Step),
        );
        ops.insert(
            "op2".to_string(),
            make_operation("op2", OperationStatus::Pending, OperationType::Step),
        );

        let engine = ReplayEngine::new(ops, None);
        assert!(engine.check_result("op1").is_some());
        assert!(engine.check_result("op2").is_none()); // Pending, not completed.
        assert!(engine.check_result("op3").is_none()); // Doesn't exist.
    }

    #[test]
    fn generate_operation_id_is_deterministic() {
        let mut engine1 = ReplayEngine::new(HashMap::new(), None);
        let mut engine2 = ReplayEngine::new(HashMap::new(), None);

        let id1a = engine1.generate_operation_id();
        let id1b = engine2.generate_operation_id();
        assert_eq!(id1a, id1b);

        let id2a = engine1.generate_operation_id();
        let id2b = engine2.generate_operation_id();
        assert_eq!(id2a, id2b);
        assert_ne!(id1a, id2a);
    }

    #[test]
    fn mixed_statuses_only_track_completed() {
        let mut ops = HashMap::new();
        ops.insert(
            "done".to_string(),
            make_operation("done", OperationStatus::Succeeded, OperationType::Step),
        );
        ops.insert(
            "pending".to_string(),
            make_operation("pending", OperationStatus::Pending, OperationType::Wait),
        );
        ops.insert(
            "started".to_string(),
            make_operation("started", OperationStatus::Started, OperationType::Step),
        );

        let mut engine = ReplayEngine::new(ops, None);
        assert!(engine.is_replaying());

        // Only need to visit the one completed op to transition.
        engine.track_replay("done");
        assert!(!engine.is_replaying());
    }

    #[test]
    fn all_completed_statuses_are_tracked() {
        for status in [
            OperationStatus::Succeeded,
            OperationStatus::Failed,
            OperationStatus::Cancelled,
            OperationStatus::TimedOut,
            OperationStatus::Stopped,
        ] {
            let mut ops = HashMap::new();
            ops.insert(
                "op".to_string(),
                make_operation("op", status, OperationType::Step),
            );
            let engine = ReplayEngine::new(ops, None);
            assert!(engine.is_replaying(), "Should replay for completed status");
        }
    }

    #[test]
    fn insert_operation_updates_state() {
        let mut engine = ReplayEngine::new(HashMap::new(), None);
        assert!(!engine.is_replaying());

        let op = make_operation("new_op", OperationStatus::Succeeded, OperationType::Step);
        engine.insert_operation("new_op".to_string(), op);

        assert!(engine.check_result("new_op").is_some());
    }
}