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
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
//! Promise combinator handlers for the AWS Durable Execution SDK.
//!
//! This module implements promise combinators (all, all_settled, race, any)
//! that coordinate multiple durable promises using familiar patterns.
//!
//! All combinators are implemented within a STEP operation to ensure durability.
//!
//! # Requirements
//!
//! - 20.1: THE Promise_Combinators SHALL provide an `all` method that waits for all promises to complete successfully
//! - 20.2: THE Promise_Combinators SHALL provide an `all_settled` method that waits for all promises to settle
//! - 20.3: THE Promise_Combinators SHALL provide a `race` method that returns the first promise to settle
//! - 20.4: THE Promise_Combinators SHALL provide an `any` method that returns the first promise to succeed
//! - 20.5: THE Promise_Combinators SHALL be implemented within a STEP operation to ensure durability

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

use serde::{de::DeserializeOwned, Serialize};
use tokio::sync::Mutex;

use crate::concurrency::{BatchItem, BatchResult, CompletionReason};
use crate::context::{LogInfo, Logger, OperationIdentifier};
use crate::error::{DurableError, ErrorObject};
use crate::operation::OperationType;
use crate::serdes::{JsonSerDes, SerDes, SerDesContext};
use crate::state::ExecutionState;

/// Result of a promise combinator operation stored in checkpoint.
#[derive(Debug, Clone, Serialize, serde::Deserialize)]
pub struct PromiseCombinatorResult<T> {
    /// The results from each promise
    pub results: Vec<PromiseOutcome<T>>,
    /// The index of the winning promise (for race/any)
    pub winner_index: Option<usize>,
    /// The completion reason
    pub completion_reason: PromiseCompletionReason,
}

/// Outcome of a single promise in a combinator.
#[derive(Debug, Clone, Serialize, serde::Deserialize)]
pub struct PromiseOutcome<T> {
    /// Index of this promise
    pub index: usize,
    /// Whether the promise succeeded
    pub succeeded: bool,
    /// The result value if succeeded
    pub result: Option<T>,
    /// The error if failed
    pub error: Option<ErrorObject>,
}

/// Reason why a promise combinator completed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, serde::Deserialize)]
pub enum PromiseCompletionReason {
    /// All promises completed successfully (for `all`)
    AllSucceeded,
    /// All promises settled (for `all_settled`)
    AllSettled,
    /// First promise settled (for `race`)
    FirstSettled,
    /// First promise succeeded (for `any`)
    FirstSucceeded,
    /// All promises failed (for `any` when none succeed)
    AllFailed,
    /// A promise failed (for `all` when one fails)
    OneFailed,
}

impl<T> PromiseOutcome<T> {
    /// Creates a successful outcome.
    pub fn success(index: usize, result: T) -> Self {
        Self {
            index,
            succeeded: true,
            result: Some(result),
            error: None,
        }
    }

    /// Creates a failed outcome.
    pub fn failure(index: usize, error: ErrorObject) -> Self {
        Self {
            index,
            succeeded: false,
            result: None,
            error: Some(error),
        }
    }
}

/// Executes the `all` combinator - waits for all futures to complete successfully.
///
/// Returns all results if all futures succeed, or returns the first error encountered.
/// This is implemented within a STEP operation for durability.
///
/// # Arguments
///
/// * `futures` - Vector of futures to execute
/// * `state` - The execution state for checkpointing
/// * `op_id` - The operation identifier
/// * `logger` - Logger for structured logging
///
/// # Returns
///
/// `Ok(Vec<T>)` if all futures succeed, or `Err` with the first error.
pub async fn all_handler<T, Fut>(
    futures: Vec<Fut>,
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
    logger: &Arc<dyn Logger>,
) -> Result<Vec<T>, DurableError>
where
    T: Serialize + DeserializeOwned + Send + Clone + 'static,
    Fut: Future<Output = Result<T, DurableError>> + Send + 'static,
{
    let log_info = create_log_info(state, op_id);
    logger.debug(
        &format!("Starting 'all' combinator with {} futures", futures.len()),
        &log_info,
    );

    // Check for replay
    if let Some(result) = check_replay::<Vec<T>>(state, op_id, logger).await? {
        return Ok(result);
    }

    // Checkpoint START
    checkpoint_start(state, op_id).await?;

    let total = futures.len();
    if total == 0 {
        // Empty case - succeed immediately
        checkpoint_succeed(state, op_id, &Vec::<T>::new()).await?;
        return Ok(Vec::new());
    }

    // Execute all futures concurrently using a channel to collect results
    let (tx, mut rx) = tokio::sync::mpsc::channel::<(usize, Result<T, DurableError>)>(total);

    for (index, future) in futures.into_iter().enumerate() {
        let tx = tx.clone();

        tokio::spawn(async move {
            let result = future.await;
            let _ = tx.send((index, result)).await;
        });
    }
    drop(tx); // Drop the original sender

    // Collect all results
    let mut results: Vec<Option<Result<T, DurableError>>> = (0..total).map(|_| None).collect();
    let mut received = 0;

    while let Some((index, result)) = rx.recv().await {
        results[index] = Some(result);
        received += 1;
        if received == total {
            break;
        }
    }

    // Check results and build final vector
    let mut final_results = Vec::with_capacity(total);

    for (index, result) in results.into_iter().enumerate() {
        match result {
            Some(Ok(value)) => final_results.push(value),
            Some(Err(e)) => {
                // Checkpoint failure and return error
                let error_obj = ErrorObject::from(&e);
                checkpoint_fail(state, op_id, error_obj).await?;
                return Err(DurableError::UserCode {
                    message: format!("Promise at index {} failed: {}", index, e),
                    error_type: "AllCombinatorError".to_string(),
                    stack_trace: None,
                });
            }
            None => {
                // Should not happen
                let error_obj = ErrorObject::new("InternalError", "Promise result missing");
                checkpoint_fail(state, op_id, error_obj).await?;
                return Err(DurableError::execution("Promise result missing"));
            }
        }
    } // All succeeded - checkpoint and return
    checkpoint_succeed(state, op_id, &final_results).await?;
    logger.debug("'all' combinator completed successfully", &log_info);
    Ok(final_results)
}

/// Executes the `all_settled` combinator - waits for all futures to settle.
///
/// Returns a BatchResult containing outcomes for all futures, regardless of success or failure.
/// This is implemented within a STEP operation for durability.
///
/// # Arguments
///
/// * `futures` - Vector of futures to execute
/// * `state` - The execution state for checkpointing
/// * `op_id` - The operation identifier
/// * `logger` - Logger for structured logging
///
/// # Returns
///
/// `BatchResult<T>` containing results for all futures.
pub async fn all_settled_handler<T, Fut>(
    futures: Vec<Fut>,
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
    logger: &Arc<dyn Logger>,
) -> Result<BatchResult<T>, DurableError>
where
    T: Serialize + DeserializeOwned + Send + Clone + 'static,
    Fut: Future<Output = Result<T, DurableError>> + Send + 'static,
{
    let log_info = create_log_info(state, op_id);
    logger.debug(
        &format!(
            "Starting 'all_settled' combinator with {} futures",
            futures.len()
        ),
        &log_info,
    );

    // Check for replay
    if let Some(result) = check_replay_batch::<T>(state, op_id, logger).await? {
        return Ok(result);
    }

    // Checkpoint START
    checkpoint_start(state, op_id).await?;

    let total = futures.len();
    if total == 0 {
        let result = BatchResult::empty();
        checkpoint_succeed_batch(state, op_id, &result).await?;
        return Ok(result);
    }

    // Execute all futures concurrently
    let results: Arc<Mutex<Vec<BatchItem<T>>>> =
        Arc::new(Mutex::new((0..total).map(BatchItem::pending).collect()));

    let mut handles = Vec::with_capacity(total);
    for (index, future) in futures.into_iter().enumerate() {
        let results = results.clone();

        handles.push(tokio::spawn(async move {
            let result = future.await;
            let mut results_guard = results.lock().await;

            match result {
                Ok(value) => {
                    results_guard[index] = BatchItem::succeeded(index, value);
                }
                Err(e) => {
                    let error_obj = ErrorObject::from(&e);
                    results_guard[index] = BatchItem::failed(index, error_obj);
                }
            }
        }));
    }

    // Wait for all to complete
    for handle in handles {
        let _ = handle.await;
    }

    // Collect results
    let items = Arc::try_unwrap(results)
        .map_err(|_| DurableError::execution("Failed to unwrap results"))?
        .into_inner();

    let batch_result = BatchResult::new(items, CompletionReason::AllCompleted);

    // Checkpoint the result
    checkpoint_succeed_batch(state, op_id, &batch_result).await?;
    logger.debug("'all_settled' combinator completed", &log_info);
    Ok(batch_result)
}

/// Executes the `race` combinator - returns the result of the first future to settle.
///
/// Returns the result (success or failure) of whichever future completes first.
/// This is implemented within a STEP operation for durability.
///
/// # Arguments
///
/// * `futures` - Vector of futures to execute
/// * `state` - The execution state for checkpointing
/// * `op_id` - The operation identifier
/// * `logger` - Logger for structured logging
///
/// # Returns
///
/// The result of the first future to settle.
pub async fn race_handler<T, Fut>(
    futures: Vec<Fut>,
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
    logger: &Arc<dyn Logger>,
) -> Result<T, DurableError>
where
    T: Serialize + DeserializeOwned + Send + Clone + 'static,
    Fut: Future<Output = Result<T, DurableError>> + Send + 'static,
{
    let log_info = create_log_info(state, op_id);
    logger.debug(
        &format!("Starting 'race' combinator with {} futures", futures.len()),
        &log_info,
    );

    // Check for replay
    if let Some(result) = check_replay::<T>(state, op_id, logger).await? {
        return Ok(result);
    }

    // Checkpoint START
    checkpoint_start(state, op_id).await?;

    let total = futures.len();
    if total == 0 {
        let error_obj = ErrorObject::new("ValidationError", "race requires at least one future");
        checkpoint_fail(state, op_id, error_obj).await?;
        return Err(DurableError::validation(
            "race requires at least one future",
        ));
    }

    // Use tokio::select! pattern with spawned tasks
    let (tx, mut rx) = tokio::sync::mpsc::channel::<(usize, Result<T, DurableError>)>(total);

    for (index, future) in futures.into_iter().enumerate() {
        let tx = tx.clone();
        tokio::spawn(async move {
            let result = future.await;
            let _ = tx.send((index, result)).await;
        });
    }
    drop(tx); // Drop the original sender

    // Wait for the first result
    if let Some((index, result)) = rx.recv().await {
        match result {
            Ok(value) => {
                checkpoint_succeed(state, op_id, &value).await?;
                logger.debug(
                    &format!("'race' combinator won by future at index {}", index),
                    &log_info,
                );
                Ok(value)
            }
            Err(e) => {
                let error_obj = ErrorObject::from(&e);
                checkpoint_fail(state, op_id, error_obj).await?;
                Err(e)
            }
        }
    } else {
        let error_obj = ErrorObject::new("InternalError", "No futures completed");
        checkpoint_fail(state, op_id, error_obj).await?;
        Err(DurableError::execution("No futures completed"))
    }
}

/// Executes the `any` combinator - returns the result of the first future to succeed.
///
/// Returns the result of the first future to succeed. If all futures fail,
/// returns an error containing all the failures.
/// This is implemented within a STEP operation for durability.
///
/// # Arguments
///
/// * `futures` - Vector of futures to execute
/// * `state` - The execution state for checkpointing
/// * `op_id` - The operation identifier
/// * `logger` - Logger for structured logging
///
/// # Returns
///
/// The result of the first future to succeed, or an error if all fail.
pub async fn any_handler<T, Fut>(
    futures: Vec<Fut>,
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
    logger: &Arc<dyn Logger>,
) -> Result<T, DurableError>
where
    T: Serialize + DeserializeOwned + Send + Clone + 'static,
    Fut: Future<Output = Result<T, DurableError>> + Send + 'static,
{
    let log_info = create_log_info(state, op_id);
    logger.debug(
        &format!("Starting 'any' combinator with {} futures", futures.len()),
        &log_info,
    );

    // Check for replay
    if let Some(result) = check_replay::<T>(state, op_id, logger).await? {
        return Ok(result);
    }

    // Checkpoint START
    checkpoint_start(state, op_id).await?;

    let total = futures.len();
    if total == 0 {
        let error_obj = ErrorObject::new("ValidationError", "any requires at least one future");
        checkpoint_fail(state, op_id, error_obj).await?;
        return Err(DurableError::validation("any requires at least one future"));
    }

    // Track results
    let (success_tx, mut success_rx) = tokio::sync::mpsc::channel::<(usize, T)>(1);
    let errors: Arc<Mutex<Vec<(usize, DurableError)>>> = Arc::new(Mutex::new(Vec::new()));
    let completed_count: Arc<std::sync::atomic::AtomicUsize> =
        Arc::new(std::sync::atomic::AtomicUsize::new(0));

    for (index, future) in futures.into_iter().enumerate() {
        let success_tx = success_tx.clone();
        let errors = errors.clone();
        let completed_count = completed_count.clone();

        tokio::spawn(async move {
            let result = future.await;
            match result {
                Ok(value) => {
                    // Try to send success - only first one wins
                    let _ = success_tx.send((index, value)).await;
                }
                Err(e) => {
                    let mut errors_guard = errors.lock().await;
                    errors_guard.push((index, e));
                    drop(errors_guard);

                    // Check if all have failed
                    // Relaxed ordering is sufficient: we only need atomic increment.
                    // The count comparison with total is a simple threshold check.
                    // Requirements: 4.1, 4.6
                    let count =
                        completed_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
                    if count == total {
                        // All failed - close the channel to unblock receiver
                        drop(success_tx);
                    }
                }
            }
        });
    }
    drop(success_tx); // Drop the original sender

    // Wait for first success or all failures
    if let Some((index, value)) = success_rx.recv().await {
        checkpoint_succeed(state, op_id, &value).await?;
        logger.debug(
            &format!("'any' combinator succeeded with future at index {}", index),
            &log_info,
        );
        Ok(value)
    } else {
        // All failed
        let errors_guard = errors.lock().await;
        let error_messages: Vec<String> = errors_guard
            .iter()
            .map(|(i, e)| format!("Future {}: {}", i, e))
            .collect();
        let combined_message = format!(
            "All {} futures failed: {}",
            total,
            error_messages.join("; ")
        );

        let error_obj = ErrorObject::new("AnyCombinatorError", &combined_message);
        checkpoint_fail(state, op_id, error_obj).await?;

        logger.debug("'any' combinator failed - all futures failed", &log_info);
        Err(DurableError::UserCode {
            message: combined_message,
            error_type: "AnyCombinatorError".to_string(),
            stack_trace: None,
        })
    }
}

// Helper functions

fn create_log_info(state: &Arc<ExecutionState>, op_id: &OperationIdentifier) -> LogInfo {
    let mut log_info =
        LogInfo::new(state.durable_execution_arn()).with_operation_id(&op_id.operation_id);
    if let Some(ref parent_id) = op_id.parent_id {
        log_info = log_info.with_parent_id(parent_id);
    }
    log_info
}

async fn check_replay<T>(
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
    logger: &Arc<dyn Logger>,
) -> Result<Option<T>, DurableError>
where
    T: Serialize + DeserializeOwned,
{
    let checkpoint_result = state.get_checkpoint_result(&op_id.operation_id).await;

    if !checkpoint_result.is_existent() {
        return Ok(None);
    }

    let log_info = create_log_info(state, op_id);

    // Check for non-deterministic execution
    if let Some(op_type) = checkpoint_result.operation_type() {
        if op_type != OperationType::Step {
            return Err(DurableError::NonDeterministic {
                message: format!(
                    "Expected Step operation but found {:?} at operation_id {}",
                    op_type, op_id.operation_id
                ),
                operation_id: Some(op_id.operation_id.clone()),
            });
        }
    }

    // Handle succeeded checkpoint
    if checkpoint_result.is_succeeded() {
        logger.debug(
            &format!("Replaying succeeded promise combinator: {}", op_id),
            &log_info,
        );
        state.track_replay(&op_id.operation_id).await;

        if let Some(result_str) = checkpoint_result.result() {
            let serdes = JsonSerDes::<T>::new();
            let serdes_ctx = SerDesContext::new(&op_id.operation_id, state.durable_execution_arn());
            let result =
                serdes
                    .deserialize(result_str, &serdes_ctx)
                    .map_err(|e| DurableError::SerDes {
                        message: format!("Failed to deserialize checkpointed result: {}", e),
                    })?;
            return Ok(Some(result));
        }
    }

    // Handle failed checkpoint
    if checkpoint_result.is_failed() {
        logger.debug(
            &format!("Replaying failed promise combinator: {}", op_id),
            &log_info,
        );
        state.track_replay(&op_id.operation_id).await;

        if let Some(error) = checkpoint_result.error() {
            return Err(DurableError::UserCode {
                message: error.error_message.clone(),
                error_type: error.error_type.clone(),
                stack_trace: error.stack_trace.clone(),
            });
        } else {
            return Err(DurableError::execution(
                "Promise combinator failed with unknown error",
            ));
        }
    }

    Ok(None)
}

async fn check_replay_batch<T>(
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
    logger: &Arc<dyn Logger>,
) -> Result<Option<BatchResult<T>>, DurableError>
where
    T: Serialize + DeserializeOwned,
{
    check_replay::<BatchResult<T>>(state, op_id, logger).await
}

async fn checkpoint_start(
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
) -> Result<(), DurableError> {
    use crate::operation::OperationUpdate;

    let mut update = OperationUpdate::start(&op_id.operation_id, OperationType::Step);
    if let Some(ref parent_id) = op_id.parent_id {
        update = update.with_parent_id(parent_id);
    }
    if let Some(ref name) = op_id.name {
        update = update.with_name(name);
    }
    update = update.with_sub_type("PromiseCombinator");

    state.create_checkpoint(update, true).await
}

async fn checkpoint_succeed<T>(
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
    result: &T,
) -> Result<(), DurableError>
where
    T: Serialize + DeserializeOwned,
{
    use crate::operation::OperationUpdate;

    let serdes = JsonSerDes::<T>::new();
    let serdes_ctx = SerDesContext::new(&op_id.operation_id, state.durable_execution_arn());
    let serialized = serdes
        .serialize(result, &serdes_ctx)
        .map_err(|e| DurableError::SerDes {
            message: format!("Failed to serialize result: {}", e),
        })?;

    let mut update =
        OperationUpdate::succeed(&op_id.operation_id, OperationType::Step, Some(serialized));
    if let Some(ref parent_id) = op_id.parent_id {
        update = update.with_parent_id(parent_id);
    }
    if let Some(ref name) = op_id.name {
        update = update.with_name(name);
    }

    state.create_checkpoint(update, true).await
}

async fn checkpoint_succeed_batch<T>(
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
    result: &BatchResult<T>,
) -> Result<(), DurableError>
where
    T: Serialize + DeserializeOwned,
{
    checkpoint_succeed(state, op_id, result).await
}

async fn checkpoint_fail(
    state: &Arc<ExecutionState>,
    op_id: &OperationIdentifier,
    error: ErrorObject,
) -> Result<(), DurableError> {
    use crate::operation::OperationUpdate;

    let mut update = OperationUpdate::fail(&op_id.operation_id, OperationType::Step, error);
    if let Some(ref parent_id) = op_id.parent_id {
        update = update.with_parent_id(parent_id);
    }
    if let Some(ref name) = op_id.name {
        update = update.with_name(name);
    }

    state.create_checkpoint(update, true).await
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::{CheckpointResponse, MockDurableServiceClient, SharedDurableServiceClient};
    use crate::context::TracingLogger;
    use crate::lambda::InitialExecutionState;
    use std::pin::Pin;

    type DurableFuture<T> = Pin<Box<dyn Future<Output = Result<T, DurableError>> + Send>>;

    fn create_mock_client() -> SharedDurableServiceClient {
        Arc::new(
            MockDurableServiceClient::new()
                .with_checkpoint_response(Ok(CheckpointResponse::new("token-1")))
                .with_checkpoint_response(Ok(CheckpointResponse::new("token-2")))
                .with_checkpoint_response(Ok(CheckpointResponse::new("token-3"))),
        )
    }

    fn create_test_state(client: SharedDurableServiceClient) -> Arc<ExecutionState> {
        Arc::new(ExecutionState::new(
            "arn:aws:lambda:us-east-1:123456789012:function:test:durable:abc123",
            "initial-token",
            InitialExecutionState::new(),
            client,
        ))
    }

    fn create_test_op_id(name: &str) -> OperationIdentifier {
        OperationIdentifier::new(format!("test-op-{}", name), None, Some(name.to_string()))
    }

    fn create_test_logger() -> Arc<dyn Logger> {
        Arc::new(TracingLogger)
    }

    #[tokio::test]
    async fn test_all_handler_success() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("all-success");
        let logger = create_test_logger();

        let futures = vec![
            Box::pin(async { Ok::<_, DurableError>(1) })
                as Pin<Box<dyn Future<Output = Result<i32, DurableError>> + Send>>,
            Box::pin(async { Ok(2) }),
            Box::pin(async { Ok(3) }),
        ];

        let result = all_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), vec![1, 2, 3]);
    }

    #[tokio::test]
    async fn test_all_handler_failure() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("all-failure");
        let logger = create_test_logger();

        let futures = vec![
            Box::pin(async { Ok::<_, DurableError>(1) })
                as Pin<Box<dyn Future<Output = Result<i32, DurableError>> + Send>>,
            Box::pin(async { Err(DurableError::execution("test error")) }),
            Box::pin(async { Ok(3) }),
        ];

        let result = all_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_all_handler_empty() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("all-empty");
        let logger = create_test_logger();

        let futures: Vec<DurableFuture<i32>> = vec![];

        let result = all_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_ok());
        assert!(result.unwrap().is_empty());
    }

    #[tokio::test]
    async fn test_all_settled_handler_mixed() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("all-settled-mixed");
        let logger = create_test_logger();

        let futures = vec![
            Box::pin(async { Ok::<_, DurableError>(1) })
                as Pin<Box<dyn Future<Output = Result<i32, DurableError>> + Send>>,
            Box::pin(async { Err(DurableError::execution("test error")) }),
            Box::pin(async { Ok(3) }),
        ];

        let result = all_settled_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_ok());
        let batch = result.unwrap();
        assert_eq!(batch.items.len(), 3);
        assert_eq!(batch.success_count(), 2);
        assert_eq!(batch.failure_count(), 1);
    }

    #[tokio::test]
    async fn test_all_settled_handler_all_success() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("all-settled-success");
        let logger = create_test_logger();

        let futures = vec![
            Box::pin(async { Ok::<_, DurableError>(1) })
                as Pin<Box<dyn Future<Output = Result<i32, DurableError>> + Send>>,
            Box::pin(async { Ok(2) }),
        ];

        let result = all_settled_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_ok());
        let batch = result.unwrap();
        assert!(batch.all_succeeded());
    }

    #[tokio::test]
    async fn test_race_handler_first_wins() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("race-first");
        let logger = create_test_logger();

        // First future completes immediately
        let futures = vec![
            Box::pin(async { Ok::<_, DurableError>(1) })
                as Pin<Box<dyn Future<Output = Result<i32, DurableError>> + Send>>,
            Box::pin(async {
                tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                Ok(2)
            }),
        ];

        let result = race_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_ok());
        // First one should win
        assert_eq!(result.unwrap(), 1);
    }

    #[tokio::test]
    async fn test_race_handler_error_wins() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("race-error");
        let logger = create_test_logger();

        // Error completes first
        let futures = vec![
            Box::pin(async { Err::<i32, _>(DurableError::execution("fast error")) })
                as Pin<Box<dyn Future<Output = Result<i32, DurableError>> + Send>>,
            Box::pin(async {
                tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                Ok(2)
            }),
        ];

        let result = race_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_race_handler_empty() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("race-empty");
        let logger = create_test_logger();

        let futures: Vec<DurableFuture<i32>> = vec![];

        let result = race_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_any_handler_first_success() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("any-first");
        let logger = create_test_logger();

        let futures = vec![
            Box::pin(async { Ok::<_, DurableError>(1) })
                as Pin<Box<dyn Future<Output = Result<i32, DurableError>> + Send>>,
            Box::pin(async { Err(DurableError::execution("error")) }),
            Box::pin(async { Ok(3) }),
        ];

        let result = any_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_ok());
        // Either 1 or 3 should win (first success)
        let value = result.unwrap();
        assert!(value == 1 || value == 3);
    }

    #[tokio::test]
    async fn test_any_handler_all_fail() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("any-all-fail");
        let logger = create_test_logger();

        let futures = vec![
            Box::pin(async { Err::<i32, _>(DurableError::execution("error 1")) })
                as Pin<Box<dyn Future<Output = Result<i32, DurableError>> + Send>>,
            Box::pin(async { Err(DurableError::execution("error 2")) }),
        ];

        let result = any_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_err());
        if let Err(DurableError::UserCode { message, .. }) = result {
            assert!(message.contains("All 2 futures failed"));
        } else {
            panic!("Expected UserCode error");
        }
    }

    #[tokio::test]
    async fn test_any_handler_empty() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("any-empty");
        let logger = create_test_logger();

        let futures: Vec<DurableFuture<i32>> = vec![];

        let result = any_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_any_handler_success_after_failures() {
        let client = create_mock_client();
        let state = create_test_state(client);
        let op_id = create_test_op_id("any-success-after-fail");
        let logger = create_test_logger();

        // First two fail immediately, third succeeds after delay
        let futures = vec![
            Box::pin(async { Err::<i32, _>(DurableError::execution("error 1")) })
                as Pin<Box<dyn Future<Output = Result<i32, DurableError>> + Send>>,
            Box::pin(async { Err(DurableError::execution("error 2")) }),
            Box::pin(async {
                tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
                Ok(42)
            }),
        ];

        let result = any_handler(futures, &state, &op_id, &logger).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 42);
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use crate::client::{CheckpointResponse, MockDurableServiceClient, SharedDurableServiceClient};
    use crate::context::TracingLogger;
    use crate::lambda::InitialExecutionState;
    use proptest::prelude::*;
    use std::pin::Pin;

    type DurableFuture<T> = Pin<Box<dyn Future<Output = Result<T, DurableError>> + Send>>;

    fn create_mock_client_with_responses(count: usize) -> SharedDurableServiceClient {
        let mut client = MockDurableServiceClient::new();
        for i in 0..count {
            client = client
                .with_checkpoint_response(Ok(CheckpointResponse::new(format!("token-{}", i))));
        }
        Arc::new(client)
    }

    fn create_test_state(client: SharedDurableServiceClient) -> Arc<ExecutionState> {
        Arc::new(ExecutionState::new(
            "arn:aws:lambda:us-east-1:123456789012:function:test:durable:abc123",
            "initial-token",
            InitialExecutionState::new(),
            client,
        ))
    }

    fn create_test_op_id(name: &str) -> OperationIdentifier {
        OperationIdentifier::new(format!("test-op-{}", name), None, Some(name.to_string()))
    }

    fn create_test_logger() -> Arc<dyn Logger> {
        Arc::new(TracingLogger)
    }

    /// **Feature: durable-execution-rust-sdk, Property 13: Promise Combinator Correctness**
    /// **Validates: Requirements 20.1, 20.2, 20.3, 20.4**
    ///
    /// For any set of futures passed to promise combinators:
    /// - `all` SHALL return all results only when all futures succeed, or fail on first error
    /// - `all_settled` SHALL return results for all futures regardless of success/failure
    /// - `race` SHALL return the result of the first future to settle
    /// - `any` SHALL return the result of the first future to succeed
    mod promise_combinator_tests {
        use super::*;

        proptest! {
            #![proptest_config(ProptestConfig::with_cases(100))]

            /// Property test: `all` returns all results when all futures succeed
            /// For any number of successful futures, `all` returns a vector with all results.
            #[test]
            fn prop_all_returns_all_results_on_success(
                values in prop::collection::vec(0i32..1000, 1..10),
            ) {
                let rt = tokio::runtime::Runtime::new().unwrap();
                rt.block_on(async {
                    let client = create_mock_client_with_responses(10);
                    let state = create_test_state(client);
                    let op_id = create_test_op_id(&format!("all-prop-{}", values.len()));
                    let logger = create_test_logger();

                    let expected = values.clone();
                    let futures: Vec<DurableFuture<i32>> =
                        values.into_iter()
                            .map(|v| Box::pin(async move { Ok(v) }) as DurableFuture<i32>)
                            .collect();

                    let result = all_handler(futures, &state, &op_id, &logger).await;

                    prop_assert!(result.is_ok(), "all should succeed when all futures succeed");
                    prop_assert_eq!(result.unwrap(), expected, "all should return all results in order");
                    Ok(())
                })?;
            }

            /// Property test: `all` fails on first error
            /// For any set of futures where at least one fails, `all` returns an error.
            #[test]
            fn prop_all_fails_on_any_error(
                success_count in 0usize..5,
                error_index in 0usize..10,
            ) {
                let rt = tokio::runtime::Runtime::new().unwrap();
                rt.block_on(async {
                    let total = success_count + 1; // At least one error
                    let error_index = error_index % total;

                    let client = create_mock_client_with_responses(10);
                    let state = create_test_state(client);
                    let op_id = create_test_op_id(&format!("all-fail-prop-{}", total));
                    let logger = create_test_logger();

                    let futures: Vec<DurableFuture<i32>> =
                        (0..total)
                            .map(|i| {
                                if i == error_index {
                                    Box::pin(async move { Err(DurableError::execution("test error")) }) as DurableFuture<i32>
                                } else {
                                    Box::pin(async move { Ok(i as i32) }) as DurableFuture<i32>
                                }
                            })
                            .collect();

                    let result = all_handler(futures, &state, &op_id, &logger).await;

                    prop_assert!(result.is_err(), "all should fail when any future fails");
                    Ok(())
                })?;
            }

            /// Property test: `all_settled` returns results for all futures
            /// For any mix of successes and failures, `all_settled` returns a BatchResult
            /// with the correct count of successes and failures.
            #[test]
            fn prop_all_settled_returns_all_outcomes(
                success_indices in prop::collection::vec(0usize..10, 0..10),
                total in 1usize..10,
            ) {
                let rt = tokio::runtime::Runtime::new().unwrap();
                rt.block_on(async {
                    let client = create_mock_client_with_responses(10);
                    let state = create_test_state(client);
                    let op_id = create_test_op_id(&format!("all-settled-prop-{}", total));
                    let logger = create_test_logger();

                    let success_set: std::collections::HashSet<usize> =
                        success_indices.into_iter().filter(|&i| i < total).collect();
                    let expected_successes = success_set.len();
                    let expected_failures = total - expected_successes;

                    let futures: Vec<DurableFuture<i32>> =
                        (0..total)
                            .map(|i| {
                                if success_set.contains(&i) {
                                    Box::pin(async move { Ok(i as i32) }) as DurableFuture<i32>
                                } else {
                                    Box::pin(async move { Err(DurableError::execution("test error")) }) as DurableFuture<i32>
                                }
                            })
                            .collect();

                    let result = all_settled_handler(futures, &state, &op_id, &logger).await;

                    prop_assert!(result.is_ok(), "all_settled should always succeed");
                    let batch = result.unwrap();
                    prop_assert_eq!(batch.total_count(), total, "all_settled should return all items");
                    prop_assert_eq!(batch.success_count(), expected_successes, "success count should match");
                    prop_assert_eq!(batch.failure_count(), expected_failures, "failure count should match");
                    Ok(())
                })?;
            }

            /// Property test: `any` succeeds if at least one future succeeds
            /// For any set of futures where at least one succeeds, `any` returns a success.
            #[test]
            fn prop_any_succeeds_if_one_succeeds(
                failure_count in 0usize..5,
                success_value in 0i32..1000,
            ) {
                let rt = tokio::runtime::Runtime::new().unwrap();
                rt.block_on(async {
                    let client = create_mock_client_with_responses(10);
                    let state = create_test_state(client);
                    let op_id = create_test_op_id(&format!("any-prop-{}", failure_count));
                    let logger = create_test_logger();

                    // Create futures: some failures followed by one success
                    let mut futures: Vec<DurableFuture<i32>> =
                        (0..failure_count)
                            .map(|_| Box::pin(async { Err(DurableError::execution("test error")) }) as DurableFuture<i32>)
                            .collect();

                    // Add the success
                    futures.push(Box::pin(async move { Ok(success_value) }));

                    let result = any_handler(futures, &state, &op_id, &logger).await;

                    prop_assert!(result.is_ok(), "any should succeed when at least one future succeeds");
                    prop_assert_eq!(result.unwrap(), success_value, "any should return the successful value");
                    Ok(())
                })?;
            }

            /// Property test: `any` fails only when all futures fail
            /// For any set of futures where all fail, `any` returns an error.
            #[test]
            fn prop_any_fails_when_all_fail(
                failure_count in 1usize..10,
            ) {
                let rt = tokio::runtime::Runtime::new().unwrap();
                rt.block_on(async {
                    let client = create_mock_client_with_responses(10);
                    let state = create_test_state(client);
                    let op_id = create_test_op_id(&format!("any-all-fail-prop-{}", failure_count));
                    let logger = create_test_logger();

                    let futures: Vec<DurableFuture<i32>> =
                        (0..failure_count)
                            .map(|i| Box::pin(async move { Err(DurableError::execution(format!("error {}", i))) }) as DurableFuture<i32>)
                            .collect();

                    let result = any_handler(futures, &state, &op_id, &logger).await;

                    prop_assert!(result.is_err(), "any should fail when all futures fail");
                    if let Err(DurableError::UserCode { message, .. }) = result {
                        prop_assert!(message.contains(&format!("All {} futures failed", failure_count)),
                            "Error message should indicate all futures failed");
                    }
                    Ok(())
                })?;
            }
        }
    }
}