durable-execution-sdk 0.1.0-alpha3

AWS Durable Execution SDK for Lambda Rust Runtime
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
//! Runtime support for the `#[durable_execution]` macro.
//!
//! This module contains the runtime logic that was previously generated inline
//! by the proc macro. By extracting it into library code, we get:
//!
//! - Unit-testable runtime logic
//! - A single copy in the binary (no per-handler duplication)
//! - Readable error messages pointing to real source files
//! - Bug fixes ship in the SDK crate, not the macro crate
//!
//! Users typically don't interact with this module directly — the
//! `#[durable_execution]` macro calls [`run_durable_handler`] automatically.
//! However, advanced users can call it directly to skip the macro.
//!
//! # Requirements
//!
//! - 15.1: THE Lambda_Integration SHALL provide a `#[durable_execution]` attribute macro for handler functions
//! - 15.3: THE Lambda_Integration SHALL create ExecutionState and DurableContext for the handler

use std::future::Future;
use std::sync::Arc;

use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::client::{LambdaDurableServiceClient, SharedDurableServiceClient};
use crate::context::DurableContext;
use crate::error::{DurableError, ErrorObject};
use crate::lambda::{DurableExecutionInvocationInput, DurableExecutionInvocationOutput};
use crate::operation::OperationType;
use crate::state::{CheckpointBatcherConfig, ExecutionState};
use crate::termination::TerminationManager;

/// SDK name for user-agent identification.
const SDK_NAME: &str = "durable-execution-sdk-rust";

/// SDK version for user-agent identification (from Cargo.toml).
const SDK_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Maximum response payload size (6MB Lambda limit).
const MAX_RESPONSE_SIZE: usize = 6 * 1024 * 1024;

/// Queue buffer size for the checkpoint batcher.
const CHECKPOINT_QUEUE_BUFFER: usize = 100;

/// Timeout in seconds for waiting on the batcher to drain during cleanup.
const BATCHER_DRAIN_TIMEOUT_SECS: u64 = 5;

/// Extracts the user's event from a [`DurableExecutionInvocationInput`].
///
/// Tries these sources in order:
/// 1. Top-level `Input` field (JSON value)
/// 2. `ExecutionDetails.InputPayload` from the EXECUTION operation (JSON string)
/// 3. `null` deserialization (for types with defaults, e.g. `Option<T>` or `()`)
///
/// # Errors
///
/// Returns a [`DurableExecutionInvocationOutput`] with `FAILED` status if
/// deserialization fails from all sources.
pub fn extract_event<E: DeserializeOwned>(
    input: &DurableExecutionInvocationInput,
) -> Result<E, DurableExecutionInvocationOutput> {
    // Try top-level Input first
    if let Some(value) = &input.input {
        return serde_json::from_value(value.clone()).map_err(|e| {
            DurableExecutionInvocationOutput::failed(ErrorObject::new(
                "DeserializationError",
                format!("Failed to deserialize event from Input: {}", e),
            ))
        });
    }

    // Try ExecutionDetails.InputPayload from the EXECUTION operation
    let execution_op = input
        .initial_execution_state
        .operations
        .iter()
        .find(|op| op.operation_type == OperationType::Execution);

    if let Some(op) = execution_op {
        if let Some(details) = &op.execution_details {
            if let Some(payload) = &details.input_payload {
                return serde_json::from_str::<E>(payload).map_err(|e| {
                    DurableExecutionInvocationOutput::failed(ErrorObject::new(
                        "DeserializationError",
                        format!(
                            "Failed to deserialize event from ExecutionDetails.InputPayload: {}",
                            e
                        ),
                    ))
                });
            }
        }
    }

    // Fall back to null deserialization (supports Option<T>, (), etc.)
    serde_json::from_value(serde_json::Value::Null).map_err(|_| {
        DurableExecutionInvocationOutput::failed(ErrorObject::new(
            "DeserializationError",
            "No input provided and event type does not support default",
        ))
    })
}

/// Processes the handler result into a [`DurableExecutionInvocationOutput`].
///
/// Handles three cases:
/// - `Ok(value)` → serialize, checkpoint if >6MB, return `SUCCEEDED`
/// - `Err(Suspend)` → return `PENDING`
/// - `Err(other)` → return `FAILED` with error details
async fn process_result<R: Serialize>(
    result: Result<R, DurableError>,
    state: &Arc<ExecutionState>,
    durable_execution_arn: &str,
) -> DurableExecutionInvocationOutput {
    match result {
        Ok(value) => match serde_json::to_string(&value) {
            Ok(json) => {
                if json.len() > MAX_RESPONSE_SIZE {
                    checkpoint_large_result(&json, state, durable_execution_arn).await
                } else {
                    DurableExecutionInvocationOutput::succeeded(Some(json))
                }
            }
            Err(e) => DurableExecutionInvocationOutput::failed(ErrorObject::new(
                "SerializationError",
                format!("Failed to serialize result: {}", e),
            )),
        },
        Err(DurableError::Suspend { .. }) => DurableExecutionInvocationOutput::pending(),
        Err(error) => DurableExecutionInvocationOutput::failed(ErrorObject::from(&error)),
    }
}

/// Checkpoints a large result (>6MB) and returns a reference to it.
async fn checkpoint_large_result(
    json: &str,
    state: &Arc<ExecutionState>,
    durable_execution_arn: &str,
) -> DurableExecutionInvocationOutput {
    let result_op_id = format!(
        "__result__{}",
        crate::replay_safe::uuid_string_from_operation(durable_execution_arn, 0)
    );

    let update = crate::operation::OperationUpdate::succeed(
        &result_op_id,
        OperationType::Execution,
        Some(json.to_string()),
    );

    match state.create_checkpoint(update, true).await {
        Ok(()) => DurableExecutionInvocationOutput::checkpointed_result(&result_op_id, json.len()),
        Err(e) => DurableExecutionInvocationOutput::failed(ErrorObject::new(
            "CheckpointError",
            format!("Failed to checkpoint large result: {}", e),
        )),
    }
}

/// Runs a durable execution handler within the Lambda runtime.
///
/// This is the core runtime function that the `#[durable_execution]` macro delegates to.
/// It handles the full lifecycle:
///
/// 1. Extract the user's event from the Lambda input
/// 2. Set up `ExecutionState`, checkpoint batcher, and `DurableContext`
/// 3. Call the user's handler
/// 4. Process the result (serialize, checkpoint large results, map errors)
/// 5. Clean up (drain batcher, drop state)
///
/// # Type Parameters
///
/// - `E`: The user's event type (must implement `DeserializeOwned`)
/// - `R`: The user's result type (must implement `Serialize`)
/// - `Fut`: The future returned by the handler
/// - `F`: The handler function
///
/// # Example
///
/// ```rust,ignore
/// use durable_execution_sdk::runtime::run_durable_handler;
///
/// // Called automatically by #[durable_execution], but can be used directly:
/// pub async fn my_handler(
///     event: LambdaEvent<DurableExecutionInvocationInput>,
/// ) -> Result<DurableExecutionInvocationOutput, lambda_runtime::Error> {
///     run_durable_handler(event, |event: MyEvent, ctx| async move {
///         let result = ctx.step(|_| Ok(42), None).await?;
///         Ok(MyResult { value: result })
///     }).await
/// }
/// ```
pub async fn run_durable_handler<E, R, Fut, F>(
    lambda_event: lambda_runtime::LambdaEvent<DurableExecutionInvocationInput>,
    handler: F,
) -> Result<DurableExecutionInvocationOutput, lambda_runtime::Error>
where
    E: DeserializeOwned,
    R: Serialize,
    Fut: Future<Output = Result<R, DurableError>>,
    F: FnOnce(E, DurableContext) -> Fut,
{
    let (durable_input, lambda_context) = lambda_event.into_parts();

    // Extract the user's event
    let user_event: E = match extract_event(&durable_input) {
        Ok(event) => event,
        Err(output) => return Ok(output),
    };

    // Create termination manager from Lambda context
    let termination_mgr = TerminationManager::from_lambda_context(&lambda_context);

    // Create the service client
    let aws_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
    let service_client: SharedDurableServiceClient =
        Arc::new(LambdaDurableServiceClient::from_aws_config_with_user_agent(
            &aws_config,
            SDK_NAME,
            SDK_VERSION,
        )?);

    // Create ExecutionState with batcher
    let batcher_config = CheckpointBatcherConfig::default();
    let (state, mut batcher) = ExecutionState::with_batcher(
        &durable_input.durable_execution_arn,
        &durable_input.checkpoint_token,
        durable_input.initial_execution_state,
        service_client,
        batcher_config,
        CHECKPOINT_QUEUE_BUFFER,
    );
    let state = Arc::new(state);

    // Spawn the checkpoint batcher task
    let batcher_handle = tokio::spawn(async move {
        batcher.run().await;
    });

    // Create DurableContext and call the handler, racing against timeout
    let durable_ctx = DurableContext::from_lambda_context(state.clone(), lambda_context);

    let output = tokio::select! {
        result = handler(user_event, durable_ctx) => {
            // Handler completed normally (Req 5.3)
            process_result(result, &state, &durable_input.durable_execution_arn).await
        }
        _ = termination_mgr.wait_for_timeout() => {
            // Timeout approaching — flush pending checkpoints and return PENDING (Req 5.2)
            DurableExecutionInvocationOutput::pending()
        }
    };

    // Drop the state to close the checkpoint queue and stop the batcher
    drop(state);

    // Wait for batcher to finish (with timeout)
    let _ = tokio::time::timeout(
        std::time::Duration::from_secs(BATCHER_DRAIN_TIMEOUT_SECS),
        batcher_handle,
    )
    .await;

    Ok(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lambda::InitialExecutionState;
    use crate::operation::{ExecutionDetails, Operation};
    use serde::Deserialize;

    #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
    struct TestEvent {
        order_id: String,
        amount: f64,
    }

    fn make_input(
        input: Option<serde_json::Value>,
        operations: Vec<Operation>,
    ) -> DurableExecutionInvocationInput {
        DurableExecutionInvocationInput {
            durable_execution_arn:
                "arn:aws:lambda:us-east-1:123456789012:function:test:durable:abc".to_string(),
            checkpoint_token: "token".to_string(),
            initial_execution_state: InitialExecutionState {
                operations,
                next_marker: None,
            },
            input,
        }
    }

    // ========================================================================
    // extract_event tests
    // ========================================================================

    #[test]
    fn test_extract_event_from_top_level_input() {
        let input = make_input(
            Some(serde_json::json!({"order_id": "ORD-1", "amount": 99.99})),
            vec![],
        );
        let event: TestEvent = extract_event(&input).unwrap();
        assert_eq!(event.order_id, "ORD-1");
        assert_eq!(event.amount, 99.99);
    }

    #[test]
    fn test_extract_event_from_execution_details_payload() {
        let mut op = Operation::new("exec-1", OperationType::Execution);
        op.execution_details = Some(ExecutionDetails {
            input_payload: Some(r#"{"order_id":"ORD-2","amount":50.0}"#.to_string()),
        });
        let input = make_input(None, vec![op]);
        let event: TestEvent = extract_event(&input).unwrap();
        assert_eq!(event.order_id, "ORD-2");
        assert_eq!(event.amount, 50.0);
    }

    #[test]
    fn test_extract_event_falls_back_to_null_for_option() {
        let input = make_input(None, vec![]);
        let event: Option<TestEvent> = extract_event(&input).unwrap();
        assert!(event.is_none());
    }

    #[test]
    fn test_extract_event_fails_when_no_input_and_type_requires_fields() {
        let input = make_input(None, vec![]);
        let result: Result<TestEvent, _> = extract_event(&input);
        assert!(result.is_err());
        let output = result.unwrap_err();
        assert!(output.is_failed());
        assert!(output
            .error
            .unwrap()
            .error_message
            .contains("does not support default"));
    }

    #[test]
    fn test_extract_event_top_level_input_takes_priority() {
        let mut op = Operation::new("exec-1", OperationType::Execution);
        op.execution_details = Some(ExecutionDetails {
            input_payload: Some(r#"{"order_id":"FROM-PAYLOAD","amount":1.0}"#.to_string()),
        });
        let input = make_input(
            Some(serde_json::json!({"order_id": "FROM-INPUT", "amount": 2.0})),
            vec![op],
        );
        let event: TestEvent = extract_event(&input).unwrap();
        assert_eq!(event.order_id, "FROM-INPUT");
    }

    #[test]
    fn test_extract_event_bad_top_level_input_returns_error() {
        let input = make_input(Some(serde_json::json!({"wrong_field": true})), vec![]);
        let result: Result<TestEvent, _> = extract_event(&input);
        assert!(result.is_err());
        let output = result.unwrap_err();
        assert!(output.is_failed());
        assert!(output
            .error
            .unwrap()
            .error_message
            .contains("Failed to deserialize event from Input"));
    }

    #[test]
    fn test_extract_event_bad_payload_returns_error() {
        let mut op = Operation::new("exec-1", OperationType::Execution);
        op.execution_details = Some(ExecutionDetails {
            input_payload: Some("not valid json".to_string()),
        });
        let input = make_input(None, vec![op]);
        let result: Result<TestEvent, _> = extract_event(&input);
        assert!(result.is_err());
        let output = result.unwrap_err();
        assert!(output
            .error
            .unwrap()
            .error_message
            .contains("ExecutionDetails.InputPayload"));
    }

    #[test]
    fn test_extract_event_execution_op_without_details_falls_back() {
        let op = Operation::new("exec-1", OperationType::Execution);
        // No execution_details set
        let input = make_input(None, vec![op]);
        let event: Option<TestEvent> = extract_event(&input).unwrap();
        assert!(event.is_none());
    }

    #[test]
    fn test_extract_event_execution_op_without_payload_falls_back() {
        let mut op = Operation::new("exec-1", OperationType::Execution);
        op.execution_details = Some(ExecutionDetails {
            input_payload: None,
        });
        let input = make_input(None, vec![op]);
        let event: Option<TestEvent> = extract_event(&input).unwrap();
        assert!(event.is_none());
    }

    // ========================================================================
    // process_result tests
    // ========================================================================

    #[tokio::test]
    async fn test_process_result_success() {
        let client = Arc::new(crate::client::MockDurableServiceClient::new());
        let state = Arc::new(ExecutionState::new(
            "arn:aws:lambda:us-east-1:123456789012:function:test:durable:abc",
            "token",
            InitialExecutionState::new(),
            client,
        ));
        let output = process_result(Ok("hello"), &state, "test-arn").await;
        assert!(output.is_succeeded());
        assert_eq!(output.result.unwrap(), "\"hello\"");
    }

    #[tokio::test]
    async fn test_process_result_suspend() {
        let client = Arc::new(crate::client::MockDurableServiceClient::new());
        let state = Arc::new(ExecutionState::new(
            "arn:aws:lambda:us-east-1:123456789012:function:test:durable:abc",
            "token",
            InitialExecutionState::new(),
            client,
        ));
        let result: Result<String, DurableError> = Err(DurableError::suspend());
        let output = process_result(result, &state, "test-arn").await;
        assert!(output.is_pending());
    }

    #[tokio::test]
    async fn test_process_result_error() {
        let client = Arc::new(crate::client::MockDurableServiceClient::new());
        let state = Arc::new(ExecutionState::new(
            "arn:aws:lambda:us-east-1:123456789012:function:test:durable:abc",
            "token",
            InitialExecutionState::new(),
            client,
        ));
        let result: Result<String, DurableError> = Err(DurableError::execution("something broke"));
        let output = process_result(result, &state, "test-arn").await;
        assert!(output.is_failed());
        assert!(output
            .error
            .unwrap()
            .error_message
            .contains("something broke"));
    }
}