ftui-runtime 0.10.0

Elm-style runtime loop and subscriptions for FrankenTUI.
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
// SPDX-License-Identifier: Apache-2.0
//! Retry policies and timeout-enforced task helpers.
//!
//! Provides [`RetryPolicy`] for configurable retry-with-backoff and
//! [`task_with_timeout`] / [`task_with_retry`] constructors that wrap
//! [`Cmd::Task`](crate::Cmd) with deterministic lifecycle guarantees.
//!
//! # Migration rationale
//!
//! Source frameworks often have retry/timeout baked into effect middleware.
//! These helpers give the migration code emitter explicit, testable primitives
//! to target instead of ad-hoc retry loops.
//!
//! # Determinism
//!
//! Backoff delays use fixed formulas (no jitter/randomness) so that
//! replay-based determinism tests can reproduce exact timing sequences.
//!
//! # Example
//!
//! ```
//! use ftui_runtime::retry::{RetryPolicy, BackoffStrategy};
//! use std::time::Duration;
//!
//! let policy = RetryPolicy::new(3, BackoffStrategy::Exponential {
//!     base_ms: 100,
//!     max_ms: 5000,
//! });
//!
//! assert_eq!(policy.delay(0), Duration::from_millis(100));
//! assert_eq!(policy.delay(1), Duration::from_millis(200));
//! assert_eq!(policy.delay(2), Duration::from_millis(400));
//! ```

#![forbid(unsafe_code)]

use std::sync::mpsc;

use crate::cancellation::{CancellationSource, CancellationToken};
use crate::program::{Cmd, TaskSpec};
use web_time::Duration;

const TASK_THREAD_JOIN_TIMEOUT: Duration = Duration::from_millis(250);
const TASK_THREAD_JOIN_POLL: Duration = Duration::from_millis(5);

fn duration_from_millis_saturating(millis: u128) -> Duration {
    if millis >= Duration::MAX.as_millis() {
        Duration::MAX
    } else {
        let seconds = millis / 1_000;
        let subsecond_millis = millis % 1_000;
        let Ok(seconds) = u64::try_from(seconds) else {
            return Duration::MAX;
        };
        let Ok(nanos) = u32::try_from(subsecond_millis.saturating_mul(1_000_000)) else {
            return Duration::MAX;
        };
        Duration::new(seconds, nanos)
    }
}

fn add_millis_saturating(total: &mut u128, millis: u128) {
    *total = total.saturating_add(millis).min(Duration::MAX.as_millis());
}

fn join_task_thread(handle: std::thread::JoinHandle<()>) {
    let _ = handle.join();
}

/// Run `f` on a worker thread; `rx` delivers its result or its panic.
///
/// The worker is where the caller's code runs, so it gets what the executor
/// gives a plain task: the panic is caught, with terminal cleanup hooks
/// suppressed. The executor's suppression is per thread and does not reach
/// this one. A panicking worker used to fire the terminal teardown mid-run,
/// then read as a timeout, because its dropped sender looked like one to
/// `recv_timeout`.
fn spawn_worker<T, F>(
    f: F,
) -> (
    std::thread::JoinHandle<()>,
    mpsc::Receiver<std::thread::Result<T>>,
)
where
    T: Send + 'static,
    F: FnOnce() -> T + Send + 'static,
{
    let (tx, rx) = mpsc::channel();
    let handle = std::thread::spawn(move || {
        let result = ftui_core::with_panic_cleanup_suppressed(|| {
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(f))
        });
        let _ = tx.send(result);
    });
    (handle, rx)
}

fn join_task_thread_bounded(handle: std::thread::JoinHandle<()>, task_name: &'static str) {
    let start = web_time::Instant::now();
    while !handle.is_finished() {
        if start.elapsed() >= TASK_THREAD_JOIN_TIMEOUT {
            tracing::warn!(
                target: crate::telemetry_schema::TARGET_EFFECT,
                task = task_name,
                timeout_ms = TASK_THREAD_JOIN_TIMEOUT.as_millis() as u64,
                "Timed-out worker thread did not exit within the cancellation join timeout; detaching"
            );
            return;
        }
        std::thread::sleep(TASK_THREAD_JOIN_POLL);
    }
    join_task_thread(handle);
}

/// Backoff strategy for retry delays.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "state-persistence",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum BackoffStrategy {
    /// Fixed delay between retries.
    Fixed {
        /// Delay in milliseconds.
        delay_ms: u64,
    },
    /// Exponential backoff: `base_ms * 2^attempt`, capped at `max_ms`.
    Exponential {
        /// Base delay in milliseconds.
        base_ms: u64,
        /// Maximum delay cap in milliseconds.
        max_ms: u64,
    },
    /// Linear backoff: `base_ms * (attempt + 1)`, capped at `max_ms`.
    Linear {
        /// Base delay in milliseconds.
        base_ms: u64,
        /// Maximum delay cap in milliseconds.
        max_ms: u64,
    },
}

/// A retry policy with configurable attempts and backoff.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "state-persistence",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct RetryPolicy {
    /// Maximum number of retry attempts (0 = no retries, just the initial attempt).
    pub max_retries: u32,
    /// Backoff strategy between retries.
    pub backoff: BackoffStrategy,
}

impl RetryPolicy {
    /// Create a new retry policy.
    pub fn new(max_retries: u32, backoff: BackoffStrategy) -> Self {
        Self {
            max_retries,
            backoff,
        }
    }

    /// No retries — execute once.
    pub fn no_retry() -> Self {
        Self {
            max_retries: 0,
            backoff: BackoffStrategy::Fixed { delay_ms: 0 },
        }
    }

    /// Compute the delay before the given attempt (0-indexed).
    pub fn delay(&self, attempt: u32) -> Duration {
        match &self.backoff {
            BackoffStrategy::Fixed { delay_ms } => Duration::from_millis(*delay_ms),
            BackoffStrategy::Exponential { base_ms, max_ms } => {
                let multiplier = 1u64.checked_shl(attempt).unwrap_or(u64::MAX);
                let delay = base_ms.saturating_mul(multiplier);
                Duration::from_millis(delay.min(*max_ms))
            }
            BackoffStrategy::Linear { base_ms, max_ms } => {
                let delay = base_ms.saturating_mul(u64::from(attempt) + 1);
                Duration::from_millis(delay.min(*max_ms))
            }
        }
    }

    /// Total maximum delay across all retries (for timeout budgeting).
    pub fn total_max_delay(&self) -> Duration {
        let retry_count = u128::from(self.max_retries);
        let max_duration_millis = Duration::MAX.as_millis();
        match &self.backoff {
            BackoffStrategy::Fixed { delay_ms } => {
                duration_from_millis_saturating(u128::from(*delay_ms).saturating_mul(retry_count))
            }
            BackoffStrategy::Linear { base_ms, max_ms } => {
                if self.max_retries == 0 || *base_ms == 0 || *max_ms == 0 {
                    return Duration::ZERO;
                }

                let uncapped_terms = retry_count.min(u128::from(*max_ms / *base_ms));
                let arithmetic_sum =
                    uncapped_terms.saturating_mul(uncapped_terms.saturating_add(1)) / 2;
                let mut total_millis = u128::from(*base_ms)
                    .saturating_mul(arithmetic_sum)
                    .min(max_duration_millis);

                let capped_terms = retry_count.saturating_sub(uncapped_terms);
                add_millis_saturating(
                    &mut total_millis,
                    u128::from(*max_ms).saturating_mul(capped_terms),
                );
                duration_from_millis_saturating(total_millis)
            }
            BackoffStrategy::Exponential { base_ms, max_ms } => {
                if self.max_retries == 0 || *base_ms == 0 || *max_ms == 0 {
                    return Duration::ZERO;
                }

                let mut total_millis = 0_u128;
                let mut attempt = 0_u32;
                while attempt < self.max_retries {
                    let delay_millis = match 1_u64.checked_shl(attempt) {
                        Some(multiplier) => base_ms.saturating_mul(multiplier).min(*max_ms),
                        None => *max_ms,
                    };
                    add_millis_saturating(&mut total_millis, u128::from(delay_millis));
                    attempt = attempt.saturating_add(1);

                    if delay_millis == *max_ms {
                        let remaining = u128::from(self.max_retries.saturating_sub(attempt));
                        add_millis_saturating(
                            &mut total_millis,
                            u128::from(*max_ms).saturating_mul(remaining),
                        );
                        break;
                    }
                }
                duration_from_millis_saturating(total_millis)
            }
        }
    }
}

/// Create a [`Cmd::Task`] that enforces a cooperative timeout.
///
/// The worker closure receives a [`CancellationToken`] and must honor it for
/// timely timeout teardown. On timeout, the runtime requests cancellation and
/// returns `on_timeout`; any late worker result is discarded.
pub fn task_with_timeout<M, F>(timeout: Duration, f: F, on_timeout: M) -> Cmd<M>
where
    M: Send + 'static,
    F: FnOnce(CancellationToken) -> M + Send + 'static,
{
    Cmd::task(move || {
        let source = CancellationSource::new();
        let token = source.token();
        let (handle, rx) = spawn_worker(move || f(token));
        match rx.recv_timeout(timeout) {
            Ok(result) => {
                join_task_thread(handle);
                // A panic is the task's, as it would be run inline.
                result.unwrap_or_else(|payload| std::panic::resume_unwind(payload))
            }
            Err(_) => {
                source.cancel();
                join_task_thread_bounded(handle, "task_with_timeout");
                on_timeout
            }
        }
    })
}

/// Create a [`Cmd::Task`] with a named spec and cooperative timeout.
pub fn task_with_timeout_named<M, F>(
    name: impl Into<String>,
    timeout: Duration,
    f: F,
    on_timeout: M,
) -> Cmd<M>
where
    M: Send + 'static,
    F: FnOnce(CancellationToken) -> M + Send + 'static,
{
    Cmd::task_with_spec(TaskSpec::default().with_name(name), move || {
        let source = CancellationSource::new();
        let token = source.token();
        let (handle, rx) = spawn_worker(move || f(token));
        match rx.recv_timeout(timeout) {
            Ok(result) => {
                join_task_thread(handle);
                result.unwrap_or_else(|payload| std::panic::resume_unwind(payload))
            }
            Err(_) => {
                source.cancel();
                join_task_thread_bounded(handle, "task_with_timeout_named");
                on_timeout
            }
        }
    })
}

/// Create a [`Cmd::Task`] that retries on failure with the given policy.
///
/// The `f` closure returns `Result<M, String>`. On `Ok`, the message is
/// returned immediately. On `Err`, the task retries according to the policy,
/// sleeping between attempts. After all retries are exhausted, `on_exhaust`
/// is called with the last error to produce a fallback message.
pub fn task_with_retry<M, F>(policy: RetryPolicy, f: F, on_exhaust: fn(String) -> M) -> Cmd<M>
where
    M: Send + 'static,
    F: Fn() -> Result<M, String> + Send + 'static,
{
    Cmd::task(move || {
        let mut last_err = String::new();
        for attempt in 0..=policy.max_retries {
            match f() {
                Ok(msg) => return msg,
                Err(e) => {
                    last_err = e;
                    if attempt < policy.max_retries {
                        std::thread::sleep(policy.delay(attempt));
                    }
                }
            }
        }
        on_exhaust(last_err)
    })
}

/// Create a [`Cmd::Task`] with both retry and timeout.
///
/// Each individual attempt is bounded by `per_attempt_timeout`. The worker
/// receives a [`CancellationToken`] and must honor it for timely timeout
/// teardown. The total number of attempts is governed by the retry policy.
pub fn task_with_retry_and_timeout<M, F>(
    policy: RetryPolicy,
    per_attempt_timeout: Duration,
    f: F,
    on_exhaust: fn(String) -> M,
) -> Cmd<M>
where
    M: Send + 'static,
    F: Fn(CancellationToken) -> Result<M, String> + Send + Sync + 'static,
{
    Cmd::task(move || {
        let f = std::sync::Arc::new(f);
        let mut last_err = String::new();
        for attempt in 0..=policy.max_retries {
            let source = CancellationSource::new();
            let token = source.token();
            let f_clone = std::sync::Arc::clone(&f);
            let (handle, rx) = spawn_worker(move || f_clone(token));
            match rx.recv_timeout(per_attempt_timeout) {
                Ok(Ok(Ok(msg))) => {
                    join_task_thread(handle);
                    return msg;
                }
                Ok(Ok(Err(e))) => {
                    join_task_thread(handle);
                    last_err = e;
                }
                // A panic is not a failed attempt to retry: it ends the task,
                // as it does `task_with_retry`, which runs `f` inline.
                Ok(Err(payload)) => {
                    join_task_thread(handle);
                    std::panic::resume_unwind(payload);
                }
                Err(_) => {
                    source.cancel();
                    join_task_thread_bounded(handle, "task_with_retry_and_timeout");
                    last_err = "timeout".into();
                }
            }
            if attempt < policy.max_retries {
                std::thread::sleep(policy.delay(attempt));
            }
        }
        on_exhaust(last_err)
    })
}

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

    #[test]
    fn fixed_backoff_constant_delay() {
        let policy = RetryPolicy::new(3, BackoffStrategy::Fixed { delay_ms: 100 });
        assert_eq!(policy.delay(0), Duration::from_millis(100));
        assert_eq!(policy.delay(1), Duration::from_millis(100));
        assert_eq!(policy.delay(2), Duration::from_millis(100));
    }

    #[test]
    fn exponential_backoff_doubles() {
        let policy = RetryPolicy::new(
            5,
            BackoffStrategy::Exponential {
                base_ms: 100,
                max_ms: 5000,
            },
        );
        assert_eq!(policy.delay(0), Duration::from_millis(100));
        assert_eq!(policy.delay(1), Duration::from_millis(200));
        assert_eq!(policy.delay(2), Duration::from_millis(400));
        assert_eq!(policy.delay(3), Duration::from_millis(800));
    }

    #[test]
    fn exponential_backoff_caps_at_max() {
        let policy = RetryPolicy::new(
            5,
            BackoffStrategy::Exponential {
                base_ms: 1000,
                max_ms: 3000,
            },
        );
        assert_eq!(policy.delay(0), Duration::from_millis(1000));
        assert_eq!(policy.delay(1), Duration::from_millis(2000));
        assert_eq!(policy.delay(2), Duration::from_millis(3000)); // capped
        assert_eq!(policy.delay(3), Duration::from_millis(3000)); // capped
    }

    #[test]
    fn linear_backoff_increments() {
        let policy = RetryPolicy::new(
            4,
            BackoffStrategy::Linear {
                base_ms: 100,
                max_ms: 500,
            },
        );
        assert_eq!(policy.delay(0), Duration::from_millis(100));
        assert_eq!(policy.delay(1), Duration::from_millis(200));
        assert_eq!(policy.delay(2), Duration::from_millis(300));
        assert_eq!(policy.delay(3), Duration::from_millis(400));
        assert_eq!(policy.delay(4), Duration::from_millis(500)); // capped
    }

    #[test]
    fn linear_backoff_caps_at_max() {
        let policy = RetryPolicy::new(
            4,
            BackoffStrategy::Linear {
                base_ms: 200,
                max_ms: 500,
            },
        );
        assert_eq!(policy.delay(2), Duration::from_millis(500)); // 200*3 = 600, capped at 500
    }

    #[test]
    fn no_retry_policy() {
        let policy = RetryPolicy::no_retry();
        assert_eq!(policy.max_retries, 0);
    }

    #[test]
    fn total_max_delay_fixed() {
        let policy = RetryPolicy::new(3, BackoffStrategy::Fixed { delay_ms: 100 });
        assert_eq!(policy.total_max_delay(), Duration::from_millis(300));
    }

    #[test]
    fn total_max_delay_exponential() {
        let policy = RetryPolicy::new(
            3,
            BackoffStrategy::Exponential {
                base_ms: 100,
                max_ms: 10000,
            },
        );
        // Delays: 100 + 200 + 400 = 700
        assert_eq!(policy.total_max_delay(), Duration::from_millis(700));
    }

    #[test]
    fn total_max_delay_zero_retries() {
        let policy = RetryPolicy::no_retry();
        assert_eq!(policy.total_max_delay(), Duration::ZERO);
    }

    #[test]
    fn total_max_delay_fixed_saturates_without_iterating() {
        let policy = RetryPolicy::new(u32::MAX, BackoffStrategy::Fixed { delay_ms: u64::MAX });
        assert_eq!(policy.total_max_delay(), Duration::MAX);
    }

    #[test]
    fn total_max_delay_linear_handles_large_retry_counts() {
        let policy = RetryPolicy::new(
            u32::MAX,
            BackoffStrategy::Linear {
                base_ms: 1,
                max_ms: 10,
            },
        );
        assert_eq!(
            policy.total_max_delay(),
            Duration::from_millis(10_u64.saturating_mul(u64::from(u32::MAX)) - 45)
        );
    }

    #[test]
    fn total_max_delay_exponential_saturates_after_cap() {
        let policy = RetryPolicy::new(
            6,
            BackoffStrategy::Exponential {
                base_ms: 10,
                max_ms: 35,
            },
        );
        assert_eq!(
            policy.total_max_delay(),
            Duration::from_millis(10 + 20 + 35 * 4)
        );
    }

    #[test]
    fn total_max_delay_matches_delay_sequence_for_representative_policies() {
        let policies = [
            RetryPolicy::new(5, BackoffStrategy::Fixed { delay_ms: 7 }),
            RetryPolicy::new(
                6,
                BackoffStrategy::Linear {
                    base_ms: 3,
                    max_ms: 10,
                },
            ),
            RetryPolicy::new(
                4,
                BackoffStrategy::Linear {
                    base_ms: 10,
                    max_ms: 3,
                },
            ),
            RetryPolicy::new(
                6,
                BackoffStrategy::Exponential {
                    base_ms: 2,
                    max_ms: 9,
                },
            ),
        ];

        for policy in policies {
            let expected_millis = (0..policy.max_retries)
                .map(|attempt| policy.delay(attempt).as_millis())
                .sum::<u128>();
            assert_eq!(policy.total_max_delay().as_millis(), expected_millis);
        }
    }

    #[test]
    fn exponential_backoff_overflow_saturates() {
        let policy = RetryPolicy::new(
            1,
            BackoffStrategy::Exponential {
                base_ms: u64::MAX / 2,
                max_ms: u64::MAX,
            },
        );
        // Should not panic on overflow
        let _ = policy.delay(30);
    }

    #[test]
    fn linear_backoff_overflow_saturates() {
        let policy = RetryPolicy::new(
            1,
            BackoffStrategy::Linear {
                base_ms: u64::MAX / 2,
                max_ms: u64::MAX,
            },
        );
        let _ = policy.delay(30);
    }

    #[test]
    fn retry_policy_clone_eq() {
        let policy = RetryPolicy::new(
            3,
            BackoffStrategy::Exponential {
                base_ms: 100,
                max_ms: 5000,
            },
        );
        let cloned = policy.clone();
        assert_eq!(policy, cloned);
    }

    #[test]
    fn task_with_retry_succeeds_first_try() {
        #[derive(Debug, PartialEq)]
        enum Msg {
            Ok(i32),
            Err(String),
        }

        let policy = RetryPolicy::new(3, BackoffStrategy::Fixed { delay_ms: 1 });
        let cmd = task_with_retry(policy, || Ok(Msg::Ok(42)), Msg::Err);

        // Verify it produces a Task variant
        assert_eq!(cmd.type_name(), "Task");
    }

    #[test]
    fn task_with_timeout_produces_task() {
        #[derive(Debug)]
        #[allow(dead_code)]
        enum Msg {
            Result(i32),
            Timeout,
        }

        let cmd = task_with_timeout(
            Duration::from_secs(1),
            |_token| Msg::Result(42),
            Msg::Timeout,
        );
        assert_eq!(cmd.type_name(), "Task");
    }

    #[test]
    fn task_with_timeout_requests_cancellation_on_timeout() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicBool, Ordering};

        #[derive(Debug, PartialEq)]
        enum Msg {
            Finished,
            Timeout,
        }

        let cancelled = Arc::new(AtomicBool::new(false));
        let worker_exited = Arc::new(AtomicBool::new(false));
        let cancelled_flag = Arc::clone(&cancelled);
        let exited_flag = Arc::clone(&worker_exited);

        let cmd = task_with_timeout(
            Duration::from_millis(10),
            move |token| {
                cancelled_flag.store(token.wait_timeout(Duration::from_secs(1)), Ordering::SeqCst);
                exited_flag.store(true, Ordering::SeqCst);
                Msg::Finished
            },
            Msg::Timeout,
        );

        let result = match cmd {
            Cmd::Task(_, task) => task(),
            other => panic!("expected Task, got {other:?}"),
        };

        assert_eq!(result, Msg::Timeout);
        std::thread::sleep(Duration::from_millis(50));
        assert!(cancelled.load(Ordering::SeqCst));
        assert!(worker_exited.load(Ordering::SeqCst));
    }

    #[cfg(not(panic = "abort"))]
    #[test]
    fn a_panicking_worker_is_the_tasks_panic_and_spares_the_terminal() {
        use std::cell::Cell;
        use std::sync::atomic::{AtomicUsize, Ordering};

        // Only panics on a worker this test marked count, so panics in
        // parallel tests cannot interfere.
        thread_local! {
            static MARKED_WORKER: Cell<bool> = const { Cell::new(false) };
        }
        static CLEANUPS: AtomicUsize = AtomicUsize::new(0);
        fn count_cleanup() {
            if MARKED_WORKER.with(Cell::get) {
                CLEANUPS.fetch_add(1, Ordering::SeqCst);
            }
        }
        ftui_core::session_teardown::install_chained_panic_hook(
            "retry-worker-panic-test",
            count_cleanup,
            || false,
        );

        let cmd = task_with_timeout(
            Duration::from_secs(5),
            |_token| -> u8 {
                MARKED_WORKER.with(|marked| marked.set(true));
                panic!("worker bug")
            },
            0,
        );
        let Cmd::Task(_, task) = cmd else {
            panic!("expected a Task");
        };
        let start = std::time::Instant::now();
        let payload = std::panic::catch_unwind(std::panic::AssertUnwindSafe(task))
            .expect_err("the worker's panic should be the task's, not a timeout message");

        assert_eq!(payload.downcast_ref::<&str>(), Some(&"worker bug"));
        assert!(start.elapsed() < std::time::Duration::from_secs(5));
        assert_eq!(
            CLEANUPS.load(Ordering::SeqCst),
            0,
            "the worker's panic ran terminal cleanup"
        );
    }

    #[cfg(not(panic = "abort"))]
    #[test]
    fn a_panic_is_not_a_failed_attempt_to_retry() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        let attempts = Arc::new(AtomicUsize::new(0));
        let attempts_seen = Arc::clone(&attempts);
        let cmd = task_with_retry_and_timeout(
            RetryPolicy::new(3, BackoffStrategy::Fixed { delay_ms: 0 }),
            Duration::from_secs(5),
            move |_token| -> Result<u8, String> {
                attempts_seen.fetch_add(1, Ordering::SeqCst);
                panic!("worker bug")
            },
            |_err| 0,
        );
        let Cmd::Task(_, task) = cmd else {
            panic!("expected a Task");
        };

        assert!(std::panic::catch_unwind(std::panic::AssertUnwindSafe(task)).is_err());
        assert_eq!(attempts.load(Ordering::SeqCst), 1, "a panic was retried");
    }

    #[test]
    fn task_with_retry_and_timeout_cancels_each_timed_out_attempt() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        #[derive(Debug, PartialEq)]
        enum Msg {
            Exhausted(String),
        }

        fn on_exhaust(err: String) -> Msg {
            Msg::Exhausted(err)
        }

        let attempts = Arc::new(AtomicUsize::new(0));
        let cancelled = Arc::new(AtomicUsize::new(0));
        let attempts_flag = Arc::clone(&attempts);
        let cancelled_flag = Arc::clone(&cancelled);
        let policy = RetryPolicy::new(1, BackoffStrategy::Fixed { delay_ms: 0 });

        let cmd = task_with_retry_and_timeout(
            policy,
            Duration::from_millis(10),
            move |token| {
                attempts_flag.fetch_add(1, Ordering::SeqCst);
                if token.wait_timeout(Duration::from_secs(1)) {
                    cancelled_flag.fetch_add(1, Ordering::SeqCst);
                }
                Err("cancelled".to_owned())
            },
            on_exhaust,
        );

        let result = match cmd {
            Cmd::Task(_, task) => task(),
            other => panic!("expected Task, got {other:?}"),
        };

        assert_eq!(result, Msg::Exhausted("timeout".to_owned()));
        std::thread::sleep(Duration::from_millis(50));
        assert_eq!(attempts.load(Ordering::SeqCst), 2);
        assert_eq!(cancelled.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn backoff_strategy_variants_debug() {
        let fixed = BackoffStrategy::Fixed { delay_ms: 100 };
        let exp = BackoffStrategy::Exponential {
            base_ms: 100,
            max_ms: 5000,
        };
        let linear = BackoffStrategy::Linear {
            base_ms: 100,
            max_ms: 500,
        };
        // Just verify Debug doesn't panic
        let _ = format!("{fixed:?}");
        let _ = format!("{exp:?}");
        let _ = format!("{linear:?}");
    }
}