stillwater 1.0.1

Pragmatic effect composition and validation for Rust - pure core, imperative shell
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
//! Retry support for zero-cost effects.
//!
//! This module provides retry combinators that integrate with the existing
//! `crate::retry::{RetryPolicy, RetryEvent, RetryExhausted, TimeoutError}` types.

use std::time::{Duration, Instant};

use crate::effect::boxed::BoxedEffect;
use crate::effect::ext::EffectExt;
use crate::effect::trait_def::Effect;
use crate::retry::{RetryEvent, RetryExhausted, RetryPolicy, TimeoutError};

/// Retry an effect using a factory function.
///
/// Each retry creates a fresh effect via the factory. This is semantically
/// correct for I/O operations which should be recreated (fresh connections,
/// new request IDs, etc.) rather than "cloned."
///
/// # Why a factory function?
///
/// Effects use `FnOnce` internally and are consumed on execution. Rather
/// than adding complexity to make effects cloneable, we embrace Rust's
/// ownership model: retry means "try this operation again from scratch."
///
/// # Returns
///
/// Returns a `BoxedEffect` because the retry loop creates dynamic control flow
/// that cannot be represented as a zero-cost combinator type.
///
/// # Example
///
/// ```rust,ignore
/// use stillwater::effect::prelude::*;
/// use stillwater::effect::retry::retry;
/// use stillwater::RetryPolicy;
/// use std::time::Duration;
///
/// let effect = retry(
///     || pure::<_, String, ()>(42),
///     RetryPolicy::exponential(Duration::from_millis(100)).with_max_retries(3)
/// );
///
/// let result = effect.execute(&()).await.unwrap();
/// assert_eq!(result.into_value(), 42);
/// ```
#[cfg(feature = "async")]
pub fn retry<T, E, Env, F, Eff>(
    make_effect: F,
    policy: RetryPolicy,
) -> BoxedEffect<RetryExhausted<T>, RetryExhausted<E>, Env>
where
    T: Send + 'static,
    E: Send + 'static,
    Env: Clone + Send + Sync + 'static,
    F: Fn() -> Eff + Send + 'static,
    Eff: Effect<Output = T, Error = E, Env = Env> + 'static,
{
    crate::effect::constructors::from_async(move |env: &Env| {
        let env = env.clone();
        async move {
            let start = Instant::now();
            let mut attempt = 0u32;
            let mut prev_delay: Option<Duration> = None;

            loop {
                let effect = make_effect();
                match effect.run(&env).await {
                    Ok(value) => {
                        return Ok(RetryExhausted::new(value, attempt + 1, start.elapsed()));
                    }
                    Err(error) => {
                        let delay = policy.delay_with_jitter(attempt, prev_delay);

                        match delay {
                            Some(d) => {
                                tokio::time::sleep(d).await;
                                prev_delay = Some(d);
                                attempt += 1;
                            }
                            None => {
                                return Err(RetryExhausted::new(
                                    error,
                                    attempt + 1,
                                    start.elapsed(),
                                ));
                            }
                        }
                    }
                }
            }
        }
    })
    .boxed()
}

/// Retry only when the predicate returns true for the error.
///
/// Non-retryable errors immediately propagate without retry attempts.
/// Useful for distinguishing transient errors (retry) from permanent
/// errors (fail fast).
///
/// # Example
///
/// ```rust,ignore
/// use stillwater::effect::prelude::*;
/// use stillwater::effect::retry::retry_if;
/// use stillwater::RetryPolicy;
/// use std::time::Duration;
///
/// #[derive(Debug, PartialEq, Clone)]
/// enum AppError { Transient, Permanent }
///
/// let effect = retry_if(
///     || fail::<(), _, ()>(AppError::Permanent),
///     RetryPolicy::constant(Duration::from_millis(10)).with_max_retries(3),
///     |err| matches!(err, AppError::Transient)
/// );
///
/// // Permanent errors are not retried
/// let result = effect.execute(&()).await;
/// assert_eq!(result, Err(AppError::Permanent));
/// ```
#[cfg(feature = "async")]
pub fn retry_if<T, E, Env, F, P, Eff>(
    make_effect: F,
    policy: RetryPolicy,
    should_retry: P,
) -> BoxedEffect<T, E, Env>
where
    T: Send + 'static,
    E: Send + 'static,
    Env: Clone + Send + Sync + 'static,
    F: Fn() -> Eff + Send + 'static,
    P: Fn(&E) -> bool + Send + Sync + 'static,
    Eff: Effect<Output = T, Error = E, Env = Env> + 'static,
{
    crate::effect::constructors::from_async(move |env: &Env| {
        let env = env.clone();
        async move {
            let mut attempt = 0u32;
            let mut prev_delay: Option<Duration> = None;

            loop {
                let effect = make_effect();
                match effect.run(&env).await {
                    Ok(value) => return Ok(value),
                    Err(error) => {
                        if !should_retry(&error) {
                            return Err(error);
                        }

                        let delay = policy.delay_with_jitter(attempt, prev_delay);

                        match delay {
                            Some(d) => {
                                tokio::time::sleep(d).await;
                                prev_delay = Some(d);
                                attempt += 1;
                            }
                            None => {
                                return Err(error);
                            }
                        }
                    }
                }
            }
        }
    })
    .boxed()
}

/// Retry with hooks for observability.
///
/// The `on_retry` callback is invoked before each retry attempt,
/// receiving information about the failed attempt. The callback
/// is synchronous and should not block; use it for logging/metrics.
///
/// # Example
///
/// ```rust,ignore
/// use stillwater::effect::prelude::*;
/// use stillwater::effect::retry::retry_with_hooks;
/// use stillwater::{RetryPolicy, RetryEvent};
/// use std::time::Duration;
///
/// let effect = retry_with_hooks(
///     || pure::<_, String, ()>(42),
///     RetryPolicy::exponential(Duration::from_millis(10)).with_max_retries(3),
///     |event: &RetryEvent<'_, String>| {
///         println!(
///             "Attempt {} failed: {:?}, next delay: {:?}",
///             event.attempt, event.error, event.next_delay
///         );
///     }
/// );
/// ```
#[cfg(feature = "async")]
pub fn retry_with_hooks<T, E, Env, F, H, Eff>(
    make_effect: F,
    policy: RetryPolicy,
    on_retry: H,
) -> BoxedEffect<RetryExhausted<T>, RetryExhausted<E>, Env>
where
    T: Send + 'static,
    E: Send + 'static,
    Env: Clone + Send + Sync + 'static,
    F: Fn() -> Eff + Send + 'static,
    H: Fn(&RetryEvent<'_, E>) + Send + Sync + 'static,
    Eff: Effect<Output = T, Error = E, Env = Env> + 'static,
{
    crate::effect::constructors::from_async(move |env: &Env| {
        let env = env.clone();
        async move {
            let start = Instant::now();
            let mut attempt = 0u32;
            let mut prev_delay: Option<Duration> = None;

            loop {
                let effect = make_effect();
                match effect.run(&env).await {
                    Ok(value) => {
                        return Ok(RetryExhausted::new(value, attempt + 1, start.elapsed()));
                    }
                    Err(error) => {
                        let delay = policy.delay_with_jitter(attempt, prev_delay);

                        // Call the hook before retrying
                        {
                            let event = RetryEvent {
                                attempt: attempt + 1,
                                error: &error,
                                next_delay: delay,
                                elapsed: start.elapsed(),
                            };
                            on_retry(&event);
                        }

                        match delay {
                            Some(d) => {
                                tokio::time::sleep(d).await;
                                prev_delay = Some(d);
                                attempt += 1;
                            }
                            None => {
                                return Err(RetryExhausted::new(
                                    error,
                                    attempt + 1,
                                    start.elapsed(),
                                ));
                            }
                        }
                    }
                }
            }
        }
    })
    .boxed()
}

/// Add a timeout to an effect.
///
/// If the effect doesn't complete within the duration, it fails
/// with a timeout error.
///
/// # Example
///
/// ```rust,ignore
/// use stillwater::effect::prelude::*;
/// use stillwater::effect::retry::with_timeout;
/// use stillwater::TimeoutError;
/// use std::time::Duration;
///
/// let effect = with_timeout(
///     from_async(|_: &()| async {
///         tokio::time::sleep(Duration::from_secs(10)).await;
///         Ok::<_, String>(42)
///     }),
///     Duration::from_millis(10)
/// );
///
/// match effect.execute(&()).await {
///     Err(TimeoutError::Timeout { duration }) => {
///         assert_eq!(duration, Duration::from_millis(10));
///     }
///     _ => panic!("Expected timeout"),
/// }
/// ```
#[cfg(feature = "async")]
pub fn with_timeout<T, E, Env, Eff>(
    effect: Eff,
    duration: Duration,
) -> BoxedEffect<T, TimeoutError<E>, Env>
where
    T: Send + 'static,
    E: Send + 'static,
    Env: Clone + Send + Sync + 'static,
    Eff: Effect<Output = T, Error = E, Env = Env> + 'static,
{
    crate::effect::constructors::from_async(move |env: &Env| {
        let env = env.clone();
        async move {
            match tokio::time::timeout(duration, effect.run(&env)).await {
                Ok(Ok(value)) => Ok(value),
                Ok(Err(e)) => Err(TimeoutError::Inner(e)),
                Err(_) => Err(TimeoutError::Timeout { duration }),
            }
        }
    })
    .boxed()
}

#[cfg(all(test, feature = "async"))]
mod tests {
    use super::*;
    use crate::effect::constructors::{fail, from_async, from_fn, pure};
    use std::sync::atomic::{AtomicU32, Ordering};
    use std::sync::Arc;

    // ==========================================================================
    // Helper functions for creating test effects
    // ==========================================================================

    /// Creates an effect that fails `fail_count` times before succeeding with `success_value`.
    fn flaky_effect<E: Clone + Send + 'static>(
        attempt_counter: Arc<AtomicU32>,
        fail_until: u32,
        success_value: i32,
        error: E,
    ) -> impl Effect<Output = i32, Error = E, Env = ()> {
        let error_clone = error.clone();
        from_fn(move |_: &()| {
            let current = attempt_counter.fetch_add(1, Ordering::SeqCst);
            if current < fail_until {
                Err(error_clone.clone())
            } else {
                Ok(success_value)
            }
        })
    }

    // ==========================================================================
    // Tests for retry() function
    // ==========================================================================

    #[tokio::test]
    async fn test_retry_success_first_attempt() {
        // Test: Operation succeeds on the first attempt (no retries needed)
        let effect = retry(
            || pure::<_, String, ()>(42),
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(3),
        );

        let result = effect.execute(&()).await.unwrap();
        assert_eq!(result.final_error, 42); // Note: final_error holds success value in Ok case
        assert_eq!(result.attempts, 1);
        assert!(result.total_duration < Duration::from_millis(100)); // Should be nearly instant
    }

    #[tokio::test]
    async fn test_retry_success_after_multiple_retries() {
        // Test: Operation fails 2 times, then succeeds on 3rd attempt
        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry(
            move || flaky_effect(counter_clone.clone(), 2, 42, "transient error".to_string()),
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(5),
        );

        let result = effect.execute(&()).await.unwrap();
        assert_eq!(result.final_error, 42);
        assert_eq!(result.attempts, 3); // 2 failures + 1 success
        assert_eq!(attempt_counter.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_retry_exhaustion_all_failures() {
        // Test: All attempts fail, retry exhausted
        let effect = retry(
            || fail::<i32, _, ()>("always fails".to_string()),
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(2),
        );

        let result = effect.execute(&()).await.unwrap_err();
        assert_eq!(result.final_error, "always fails".to_string());
        assert_eq!(result.attempts, 3); // 1 initial + 2 retries
    }

    #[tokio::test]
    async fn test_retry_attempt_count_accuracy() {
        // Test: Verify exact attempt counting with different max_retries settings
        for max_retries in [0, 1, 3, 5] {
            let effect = retry(
                || fail::<i32, _, ()>("error".to_string()),
                RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(max_retries),
            );

            let result = effect.execute(&()).await.unwrap_err();
            assert_eq!(
                result.attempts,
                max_retries + 1,
                "Expected {} attempts for max_retries={}, got {}",
                max_retries + 1,
                max_retries,
                result.attempts
            );
        }
    }

    #[tokio::test]
    async fn test_retry_elapsed_time_tracking() {
        // Test: Verify elapsed time is tracked correctly
        let effect = retry(
            || fail::<i32, _, ()>("error".to_string()),
            RetryPolicy::constant(Duration::from_millis(10)).with_max_retries(2),
        );

        let result = effect.execute(&()).await.unwrap_err();
        // Should have waited at least 20ms (2 retries * 10ms each)
        assert!(
            result.total_duration >= Duration::from_millis(15),
            "Expected total_duration >= 15ms, got {:?}",
            result.total_duration
        );
    }

    #[tokio::test]
    async fn test_retry_with_exponential_policy() {
        // Test: Retry with exponential backoff policy
        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry(
            move || flaky_effect(counter_clone.clone(), 2, 100, "error".to_string()),
            RetryPolicy::exponential(Duration::from_millis(1)).with_max_retries(5),
        );

        let result = effect.execute(&()).await.unwrap();
        assert_eq!(result.final_error, 100);
        assert_eq!(result.attempts, 3);
    }

    #[tokio::test]
    async fn test_retry_with_linear_policy() {
        // Test: Retry with linear backoff policy
        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry(
            move || flaky_effect(counter_clone.clone(), 1, 50, "error".to_string()),
            RetryPolicy::linear(Duration::from_millis(1)).with_max_retries(3),
        );

        let result = effect.execute(&()).await.unwrap();
        assert_eq!(result.final_error, 50);
        assert_eq!(result.attempts, 2);
    }

    #[tokio::test]
    async fn test_retry_zero_max_retries() {
        // Test: With max_retries=0, only the initial attempt is made
        let effect = retry(
            || fail::<i32, _, ()>("error".to_string()),
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(0),
        );

        let result = effect.execute(&()).await.unwrap_err();
        assert_eq!(result.attempts, 1);
    }

    #[tokio::test]
    async fn test_retry_factory_creates_fresh_effect() {
        // Test: Each retry creates a fresh effect via the factory
        let call_count = Arc::new(AtomicU32::new(0));
        let call_count_clone = call_count.clone();

        let effect = retry(
            move || {
                // Increment counter each time factory is called
                call_count_clone.fetch_add(1, Ordering::SeqCst);
                fail::<i32, _, ()>("error".to_string())
            },
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(3),
        );

        let _ = effect.execute(&()).await;
        // Factory should be called 4 times (1 initial + 3 retries)
        assert_eq!(call_count.load(Ordering::SeqCst), 4);
    }

    // ==========================================================================
    // Tests for retry_if() function
    // ==========================================================================

    #[tokio::test]
    async fn test_retry_if_retryable_errors_are_retried() {
        // Test: Retryable errors trigger retry attempts
        #[derive(Debug, PartialEq, Clone)]
        enum TestError {
            Transient(u32),
            #[allow(dead_code)]
            Permanent,
        }

        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry_if(
            move || {
                let counter = counter_clone.clone();
                from_fn(move |_: &()| {
                    let count = counter.fetch_add(1, Ordering::SeqCst);
                    if count < 2 {
                        Err(TestError::Transient(count))
                    } else {
                        Ok(42)
                    }
                })
            },
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(5),
            |err| matches!(err, TestError::Transient(_)),
        );

        let result = effect.execute(&()).await;
        assert_eq!(result, Ok(42));
        assert_eq!(attempt_counter.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_retry_if_non_retryable_errors_fail_immediately() {
        // Test: Non-retryable errors fail immediately without retry
        #[derive(Debug, PartialEq, Clone)]
        #[allow(dead_code)]
        enum TestError {
            Transient,
            Permanent,
        }

        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry_if(
            move || {
                counter_clone.fetch_add(1, Ordering::SeqCst);
                fail::<(), _, ()>(TestError::Permanent)
            },
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(5),
            |err| matches!(err, TestError::Transient),
        );

        let result = effect.execute(&()).await;
        assert_eq!(result, Err(TestError::Permanent));
        // Only one attempt should be made
        assert_eq!(attempt_counter.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn test_retry_if_mixed_errors() {
        // Test: Mix of transient errors followed by permanent error
        #[derive(Debug, PartialEq, Clone)]
        enum TestError {
            Transient,
            Permanent,
        }

        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry_if(
            move || {
                let count = counter_clone.fetch_add(1, Ordering::SeqCst);
                if count < 2 {
                    fail::<i32, _, ()>(TestError::Transient)
                } else {
                    fail::<i32, _, ()>(TestError::Permanent)
                }
            },
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(5),
            |err| matches!(err, TestError::Transient),
        );

        let result = effect.execute(&()).await;
        assert_eq!(result, Err(TestError::Permanent));
        // 3 attempts: 2 transient + 1 permanent
        assert_eq!(attempt_counter.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_retry_if_predicate_receives_error() {
        // Test: Predicate receives the correct error reference
        #[derive(Debug, PartialEq, Clone)]
        struct ErrorWithCode(u32);

        let error_codes_seen = Arc::new(std::sync::Mutex::new(Vec::new()));
        let codes_clone = error_codes_seen.clone();

        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry_if(
            move || {
                let count = counter_clone.fetch_add(1, Ordering::SeqCst);
                fail::<i32, _, ()>(ErrorWithCode(count))
            },
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(3),
            move |err: &ErrorWithCode| {
                codes_clone.lock().unwrap().push(err.0);
                err.0 < 2 // Retry for codes 0, 1; fail on 2+
            },
        );

        let _ = effect.execute(&()).await;
        let codes = error_codes_seen.lock().unwrap().clone();
        // Predicate should have seen errors with codes 0, 1, 2
        assert_eq!(codes, vec![0, 1, 2]);
    }

    #[tokio::test]
    async fn test_retry_if_exhaustion_for_retryable_errors() {
        // Test: Retryable errors eventually exhaust retries
        #[derive(Debug, PartialEq, Clone)]
        struct RetryableError;

        let effect = retry_if(
            || fail::<i32, _, ()>(RetryableError),
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(2),
            |_: &RetryableError| true, // Always retryable
        );

        let result = effect.execute(&()).await;
        assert_eq!(result, Err(RetryableError));
    }

    #[tokio::test]
    async fn test_retry_if_success_on_first_attempt() {
        // Test: Success on first attempt, predicate never called
        let predicate_called = Arc::new(AtomicU32::new(0));
        let pred_clone = predicate_called.clone();

        let effect = retry_if(
            || pure::<_, String, ()>(42),
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(3),
            move |_: &String| {
                pred_clone.fetch_add(1, Ordering::SeqCst);
                true
            },
        );

        let result = effect.execute(&()).await;
        assert_eq!(result, Ok(42));
        assert_eq!(predicate_called.load(Ordering::SeqCst), 0);
    }

    // ==========================================================================
    // Tests for retry_with_hooks() function
    // ==========================================================================

    #[tokio::test]
    async fn test_retry_with_hooks_on_retry_called() {
        // Test: on_retry hook is called for each retry attempt
        let hook_calls = Arc::new(AtomicU32::new(0));
        let hook_clone = hook_calls.clone();

        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry_with_hooks(
            move || flaky_effect(counter_clone.clone(), 2, 42, "error".to_string()),
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(5),
            move |_event: &RetryEvent<'_, String>| {
                hook_clone.fetch_add(1, Ordering::SeqCst);
            },
        );

        let result = effect.execute(&()).await.unwrap();
        assert_eq!(result.final_error, 42);
        // Hook should be called twice (for the 2 failures)
        assert_eq!(hook_calls.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn test_retry_with_hooks_event_data() {
        // Test: Hook receives correct RetryEvent information
        let events = Arc::new(std::sync::Mutex::new(Vec::<(u32, String, bool)>::new()));
        let events_clone = events.clone();

        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry_with_hooks(
            move || {
                let count = counter_clone.fetch_add(1, Ordering::SeqCst);
                fail::<i32, _, ()>(format!("error_{}", count))
            },
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(2),
            move |event: &RetryEvent<'_, String>| {
                events_clone.lock().unwrap().push((
                    event.attempt,
                    event.error.clone(),
                    event.next_delay.is_some(),
                ));
            },
        );

        let _ = effect.execute(&()).await;
        let recorded_events = events.lock().unwrap().clone();

        // Should have 3 events (for attempts 1, 2, 3)
        assert_eq!(recorded_events.len(), 3);

        // First failure (attempt 1)
        assert_eq!(recorded_events[0].0, 1);
        assert_eq!(recorded_events[0].1, "error_0");
        assert!(recorded_events[0].2); // has next_delay

        // Second failure (attempt 2)
        assert_eq!(recorded_events[1].0, 2);
        assert_eq!(recorded_events[1].1, "error_1");
        assert!(recorded_events[1].2); // has next_delay

        // Third failure (attempt 3) - exhausted
        assert_eq!(recorded_events[2].0, 3);
        assert_eq!(recorded_events[2].1, "error_2");
        assert!(!recorded_events[2].2); // no next_delay (exhausted)
    }

    #[tokio::test]
    async fn test_retry_with_hooks_not_called_on_success() {
        // Test: Hook is not called when first attempt succeeds
        let hook_called = Arc::new(AtomicU32::new(0));
        let hook_clone = hook_called.clone();

        let effect = retry_with_hooks(
            || pure::<_, String, ()>(42),
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(3),
            move |_event: &RetryEvent<'_, String>| {
                hook_clone.fetch_add(1, Ordering::SeqCst);
            },
        );

        let result = effect.execute(&()).await.unwrap();
        assert_eq!(result.final_error, 42);
        assert_eq!(hook_called.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn test_retry_with_hooks_elapsed_time_in_event() {
        // Test: RetryEvent contains increasing elapsed time
        let elapsed_times = Arc::new(std::sync::Mutex::new(Vec::<Duration>::new()));
        let times_clone = elapsed_times.clone();

        let effect = retry_with_hooks(
            || fail::<i32, _, ()>("error".to_string()),
            RetryPolicy::constant(Duration::from_millis(5)).with_max_retries(2),
            move |event: &RetryEvent<'_, String>| {
                times_clone.lock().unwrap().push(event.elapsed);
            },
        );

        let _ = effect.execute(&()).await;
        let times = elapsed_times.lock().unwrap().clone();

        // Each elapsed time should be greater than the previous
        for window in times.windows(2) {
            assert!(
                window[1] >= window[0],
                "Elapsed times should be non-decreasing: {:?} vs {:?}",
                window[0],
                window[1]
            );
        }
    }

    #[tokio::test]
    async fn test_retry_with_hooks_success_after_retries() {
        // Test: Hook is called for failures, not for the final success
        let hook_calls = Arc::new(AtomicU32::new(0));
        let hook_clone = hook_calls.clone();

        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry_with_hooks(
            move || flaky_effect(counter_clone.clone(), 3, 99, "error".to_string()),
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(5),
            move |_: &RetryEvent<'_, String>| {
                hook_clone.fetch_add(1, Ordering::SeqCst);
            },
        );

        let result = effect.execute(&()).await.unwrap();
        assert_eq!(result.final_error, 99);
        assert_eq!(result.attempts, 4);
        // Hook called 3 times for the 3 failures
        assert_eq!(hook_calls.load(Ordering::SeqCst), 3);
    }

    // ==========================================================================
    // Tests for with_timeout() function
    // ==========================================================================

    #[tokio::test]
    async fn test_with_timeout_completes_before_timeout() {
        // Test: Operation completes successfully before timeout
        let effect = with_timeout(pure::<_, String, ()>(42), Duration::from_secs(1));

        let result = effect.execute(&()).await;
        assert_eq!(result, Ok(42));
    }

    #[tokio::test]
    async fn test_with_timeout_operation_times_out() {
        // Test: Operation times out correctly
        let effect = with_timeout(
            from_async(|_: &()| async {
                tokio::time::sleep(Duration::from_secs(10)).await;
                Ok::<_, String>(42)
            }),
            Duration::from_millis(10),
        );

        let result = effect.execute(&()).await;
        match result {
            Err(TimeoutError::Timeout { duration }) => {
                assert_eq!(duration, Duration::from_millis(10));
            }
            _ => panic!("Expected TimeoutError::Timeout, got {:?}", result),
        }
    }

    #[tokio::test]
    async fn test_with_timeout_inner_error_propagated() {
        // Test: Inner error is propagated with TimeoutError::Inner variant
        let effect = with_timeout(
            fail::<i32, _, ()>("inner error".to_string()),
            Duration::from_secs(1),
        );

        let result = effect.execute(&()).await;
        match result {
            Err(TimeoutError::Inner(e)) => {
                assert_eq!(e, "inner error");
            }
            _ => panic!("Expected TimeoutError::Inner, got {:?}", result),
        }
    }

    #[tokio::test]
    async fn test_with_timeout_zero_duration() {
        // Test: Zero duration timeout (edge case)
        let effect = with_timeout(
            from_async(|_: &()| async {
                // Even a minimal operation might timeout with zero duration
                Ok::<_, String>(42)
            }),
            Duration::ZERO,
        );

        let result = effect.execute(&()).await;
        // With zero duration, the result depends on scheduling - could be either
        // success or timeout. Just verify it returns a valid result.
        assert!(result.is_ok() || matches!(result, Err(TimeoutError::Timeout { .. })));
    }

    #[tokio::test]
    async fn test_with_timeout_async_operation_success() {
        // Test: Async operation that takes some time but completes before timeout
        let effect = with_timeout(
            from_async(|_: &()| async {
                tokio::time::sleep(Duration::from_millis(5)).await;
                Ok::<_, String>(100)
            }),
            Duration::from_millis(100),
        );

        let result = effect.execute(&()).await;
        assert_eq!(result, Ok(100));
    }

    #[tokio::test]
    async fn test_with_timeout_async_operation_failure() {
        // Test: Async operation that fails (not times out)
        let effect = with_timeout(
            from_async(|_: &()| async {
                tokio::time::sleep(Duration::from_millis(5)).await;
                Err::<i32, _>("async error".to_string())
            }),
            Duration::from_millis(100),
        );

        let result = effect.execute(&()).await;
        assert_eq!(result, Err(TimeoutError::Inner("async error".to_string())));
    }

    #[tokio::test]
    async fn test_with_timeout_preserves_value_type() {
        // Test: Timeout wrapper preserves the value type correctly
        #[derive(Debug, PartialEq)]
        struct CustomValue {
            x: i32,
            y: String,
        }

        let effect = with_timeout(
            pure::<_, String, ()>(CustomValue {
                x: 42,
                y: "test".to_string(),
            }),
            Duration::from_secs(1),
        );

        let result = effect.execute(&()).await;
        assert_eq!(
            result,
            Ok(CustomValue {
                x: 42,
                y: "test".to_string()
            })
        );
    }

    #[tokio::test]
    async fn test_with_timeout_preserves_error_type() {
        // Test: Timeout wrapper preserves the error type correctly
        #[derive(Debug, PartialEq)]
        struct CustomError {
            code: u32,
            message: String,
        }

        let effect = with_timeout(
            fail::<i32, _, ()>(CustomError {
                code: 500,
                message: "internal error".to_string(),
            }),
            Duration::from_secs(1),
        );

        let result = effect.execute(&()).await;
        assert_eq!(
            result,
            Err(TimeoutError::Inner(CustomError {
                code: 500,
                message: "internal error".to_string()
            }))
        );
    }

    // ==========================================================================
    // Additional edge case and integration tests
    // ==========================================================================

    #[tokio::test]
    async fn test_retry_with_environment() {
        // Test: Retry works correctly with a non-unit environment
        #[derive(Clone)]
        struct Config {
            multiplier: i32,
        }

        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry(
            move || {
                let counter = counter_clone.clone();
                from_fn(move |env: &Config| {
                    let count = counter.fetch_add(1, Ordering::SeqCst);
                    if count < 2 {
                        Err::<i32, _>("not ready".to_string())
                    } else {
                        Ok(env.multiplier * 10)
                    }
                })
            },
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(5),
        );

        let result = effect.execute(&Config { multiplier: 5 }).await.unwrap();
        assert_eq!(result.final_error, 50);
    }

    #[tokio::test]
    async fn test_retry_if_with_environment() {
        // Test: retry_if works correctly with a custom environment
        #[derive(Clone)]
        struct AppConfig {
            threshold: u32,
        }

        #[derive(Debug, PartialEq, Clone)]
        #[allow(dead_code)]
        enum AppError {
            BelowThreshold(u32),
            AboveThreshold,
        }

        let attempt_counter = Arc::new(AtomicU32::new(0));
        let counter_clone = attempt_counter.clone();

        let effect = retry_if(
            move || {
                let counter = counter_clone.clone();
                from_fn(move |env: &AppConfig| {
                    let count = counter.fetch_add(1, Ordering::SeqCst);
                    if count < env.threshold {
                        Err::<i32, _>(AppError::BelowThreshold(count))
                    } else {
                        Ok((count + 1) as i32)
                    }
                })
            },
            RetryPolicy::constant(Duration::from_millis(1)).with_max_retries(10),
            |err| matches!(err, AppError::BelowThreshold(_)),
        );

        let result = effect.execute(&AppConfig { threshold: 3 }).await;
        assert_eq!(result, Ok(4));
    }
}