cano 0.14.0

High-performance orchestration engine for building resilient, self-healing systems in Rust. Uses Finite State Machines (FSM) for strict, type-safe transitions.
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
//! # PollTask — Wait-Until Processing Model
//!
//! A [`PollTask`] repeatedly calls `poll()` until it returns [`PollOutcome::Ready`] or until an
//! optional wall-clock timeout is exceeded. This "wait-until" pattern is the canonical way
//! to poll an external system for completion without blocking a thread.
//!
//! ## When to use `PollTask`
//!
//! - **Waiting for external jobs**: poll a job queue or long-running API until it finishes.
//! - **Adaptive backoff without retries**: the loop *is* the resilience mechanism, so the
//!   default `TaskConfig` is [`TaskConfig::minimal()`] (no outer retry wrapping).
//! - **Condition checks**: loop until a database row exists, a flag flips, etc.
//!
//! Every [`PollTask`] automatically implements [`Task`](crate::task::Task) via a
//! per-impl-site companion `impl Task<S> for T` emitted by the `#[task::poll]` macro.
//! This means you can register a `PollTask` with
//! [`Workflow::register`](crate::workflow::Workflow::register) exactly like any other task.
//!
//! ## Quick Start
//!
//! ```rust
//! use cano::prelude::*;
//! use std::sync::atomic::{AtomicU32, Ordering};
//! use std::sync::Arc;
//!
//! #[derive(Debug, Clone, PartialEq, Eq, Hash)]
//! enum Step { Wait, Done }
//!
//! struct Counter(Arc<AtomicU32>);
//!
//! #[task::poll(state = Step)]
//! impl Counter {
//!     async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
//!         let n = self.0.fetch_add(1, Ordering::Relaxed);
//!         if n >= 2 {
//!             Ok(PollOutcome::Ready(TaskResult::Single(Step::Done)))
//!         } else {
//!             Ok(PollOutcome::Pending { delay_ms: 0 })
//!         }
//!     }
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), CanoError> {
//! let counter = Counter(Arc::new(AtomicU32::new(0)));
//! let workflow = Workflow::bare()
//!     .register(Step::Wait, counter)
//!     .add_exit_state(Step::Done);
//!
//! let result = workflow.orchestrate(Step::Wait).await?;
//! assert_eq!(result, Step::Done);
//! # Ok(())
//! # }
//! ```
//!
//! ## Bounding the poll loop by wall-clock time
//!
//! There is no built-in poll-count limit. To cap total wall-clock time, return a
//! [`TaskConfig`] with an `attempt_timeout` from [`PollTask::config`]:
//!
//! ```rust
//! use cano::prelude::*;
//! use std::time::Duration;
//!
//! #[derive(Debug, Clone, PartialEq, Eq, Hash)]
//! enum Step { Wait, Done }
//!
//! struct BoundedPoller;
//!
//! #[task::poll(state = Step)]
//! impl BoundedPoller {
//!     fn config(&self) -> TaskConfig {
//!         TaskConfig::minimal().with_attempt_timeout(Duration::from_secs(30))
//!     }
//!
//!     async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
//!         Ok(PollOutcome::Pending { delay_ms: 100 })
//!     }
//! }
//! ```
//!
//! The timeout is enforced by the workflow engine when the task is dispatched via
//! [`Workflow::orchestrate`](crate::workflow::Workflow::orchestrate). If the entire
//! poll loop does not complete within the timeout, the workflow receives
//! [`CanoError::Timeout`].
//!
//! ## The trait-impl form
//!
//! You can also write the full `impl PollTask<S> for T` header explicitly:
//!
//! ```rust
//! use cano::prelude::*;
//!
//! #[derive(Debug, Clone, PartialEq, Eq, Hash)]
//! enum Step { Poll, Done }
//!
//! struct TraitPoller;
//!
//! #[task::poll]
//! impl PollTask<Step> for TraitPoller {
//!     async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
//!         Ok(PollOutcome::Ready(TaskResult::Single(Step::Done)))
//!     }
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), CanoError> {
//! let workflow = Workflow::bare()
//!     .register(Step::Poll, TraitPoller)
//!     .add_exit_state(Step::Done);
//!
//! let result = workflow.orchestrate(Step::Poll).await?;
//! assert_eq!(result, Step::Done);
//! # Ok(())
//! # }
//! ```

use crate::error::CanoError;
use crate::resource::Resources;
use crate::task::{TaskConfig, TaskResult};
use std::borrow::Cow;
use std::fmt;
use std::hash::Hash;

// ---------------------------------------------------------------------------
// PollOutcome<TState> — the outcome of a single poll() call
// ---------------------------------------------------------------------------

/// The outcome returned by a single [`PollTask::poll`] call.
///
/// - [`PollOutcome::Ready`] — the condition is satisfied; carry the `TaskResult` forward to the FSM.
/// - [`PollOutcome::Pending`] — not yet ready; sleep for `delay_ms` milliseconds then poll again.
///
/// This mirrors `std::task::Poll` conceptually but carries workflow-specific data and an
/// explicit sleep duration to decouple the poller from async executor wakeup mechanics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PollOutcome<TState> {
    /// The poll condition is satisfied. The inner [`TaskResult`] is forwarded to the FSM.
    Ready(TaskResult<TState>),
    /// Not yet ready. Sleep `delay_ms` milliseconds before the next poll.
    Pending {
        /// Milliseconds to sleep before the next poll call. Use `0` for no delay.
        delay_ms: u64,
    },
}

// ---------------------------------------------------------------------------
// PollErrorPolicy — what to do when poll() returns Err
// ---------------------------------------------------------------------------

/// Controls how the poll loop responds when [`PollTask::poll`] returns an [`Err`].
///
/// Return a non-default policy from [`PollTask::on_poll_error`] to override the default
/// fail-fast behaviour.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum PollErrorPolicy {
    /// Propagate the error immediately — the loop stops and the error is returned.
    #[default]
    FailFast,
    /// Log and retry up to `max_errors` consecutive errors before failing.
    RetryOnError {
        /// Maximum number of consecutive errors before the loop fails.
        max_errors: u32,
    },
}

// ---------------------------------------------------------------------------
// PollTask trait
// ---------------------------------------------------------------------------

/// A "wait-until" processing model that repeatedly calls [`poll`](PollTask::poll) until
/// it returns [`PollOutcome::Ready`].
///
/// # Generic Types
///
/// - **`TState`**: The state enum used by the workflow (`Clone + Debug + Send + Sync`).
/// - **`TResourceKey`**: The key type used to look up resources (defaults to
///   [`Cow<'static, str>`]).
///
/// # Default config
///
/// Unlike [`RouterTask`](crate::task::router::RouterTask), `PollTask` defaults to
/// [`TaskConfig::minimal()`] (no retries). The poll loop itself is the resilience
/// mechanism; outer retry wrapping rarely makes sense here.
///
/// # Implementing PollTask
///
/// Prefer the inherent `#[task::poll(state = S)]` form:
///
/// ```rust
/// use cano::prelude::*;
///
/// #[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// enum Step { Wait, Done }
///
/// struct MyPoller;
///
/// #[task::poll(state = Step)]
/// impl MyPoller {
///     async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
///         Ok(PollOutcome::Ready(TaskResult::Single(Step::Done)))
///     }
/// }
/// ```
#[crate::task::poll]
pub trait PollTask<TState, TResourceKey = Cow<'static, str>>: Send + Sync
where
    TState: Clone + fmt::Debug + Send + Sync + 'static,
    TResourceKey: Hash + Eq + Send + Sync + 'static,
{
    /// Get the task configuration that controls execution behaviour.
    ///
    /// Defaults to [`TaskConfig::minimal()`] (no retries, no timeout). Override to
    /// attach a circuit-breaker or per-attempt timeout if needed.
    fn config(&self) -> TaskConfig {
        TaskConfig::minimal()
    }

    /// Human-readable identifier for this poller, reported to [`WorkflowObserver`] hooks.
    ///
    /// Defaults to [`std::any::type_name`] of the implementing type. Override for a
    /// stable, friendly name.
    ///
    /// [`WorkflowObserver`]: crate::observer::WorkflowObserver
    fn name(&self) -> Cow<'static, str> {
        Cow::Borrowed(std::any::type_name::<Self>())
    }

    /// Policy for handling errors returned by [`poll`](PollTask::poll).
    ///
    /// Defaults to [`PollErrorPolicy::FailFast`] — the first error stops the loop.
    /// Override to tolerate transient errors:
    ///
    /// ```rust
    /// use cano::prelude::*;
    ///
    /// #[derive(Debug, Clone, PartialEq, Eq, Hash)]
    /// enum Step { Wait, Done }
    ///
    /// struct ResilientPoller;
    ///
    /// #[task::poll]
    /// impl PollTask<Step> for ResilientPoller {
    ///     fn on_poll_error(&self) -> PollErrorPolicy {
    ///         PollErrorPolicy::RetryOnError { max_errors: 3 }
    ///     }
    ///     async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
    ///         Ok(PollOutcome::Ready(TaskResult::Single(Step::Done)))
    ///     }
    /// }
    /// ```
    fn on_poll_error(&self) -> PollErrorPolicy {
        PollErrorPolicy::FailFast
    }

    /// Call once per poll iteration.
    ///
    /// Return [`PollOutcome::Ready`] with the next [`TaskResult`] when the condition is met,
    /// or [`PollOutcome::Pending`] with a sleep duration when it is not yet ready.
    ///
    /// # Errors
    ///
    /// Returns [`CanoError`] on a non-recoverable error. The loop's response to the
    /// error is governed by [`on_poll_error`](PollTask::on_poll_error).
    async fn poll(&self, res: &Resources<TResourceKey>) -> Result<PollOutcome<TState>, CanoError>;
}

// ---------------------------------------------------------------------------
// run_poll_loop — the loop body called by the macro-synthesised Task::run
// ---------------------------------------------------------------------------

/// Run the [`PollTask`] poll loop for `p`.
///
/// The synthesised `Task::run` method emitted by `#[task::poll]` delegates here so
/// that the loop body (which needs `tokio::time::sleep`) lives in a single place that
/// can reference `tokio` directly.
///
/// The loop calls `p.poll(res)` repeatedly:
///
/// - `PollOutcome::Ready(result)` → return `Ok(result)`.
/// - `PollOutcome::Pending { delay_ms }` → reset the consecutive-error counter, sleep
///   `delay_ms` ms, then poll again.
/// - `Err(e)` → consult [`PollTask::on_poll_error`]:
///   - [`PollErrorPolicy::FailFast`] → return `Err(e)` immediately.
///   - [`PollErrorPolicy::RetryOnError { max_errors }`] → increment the consecutive-error
///     counter; if it exceeds `max_errors`, return `Err(e)`; otherwise continue.
///
/// There is no built-in iteration limit. To cap total wall-clock time, return
/// `TaskConfig::minimal().with_attempt_timeout(dur)` from [`PollTask::config`] and
/// dispatch the task via [`Workflow::orchestrate`](crate::workflow::Workflow::orchestrate).
pub async fn run_poll_loop<P, S, K>(p: &P, res: &Resources<K>) -> Result<TaskResult<S>, CanoError>
where
    P: PollTask<S, K> + ?Sized,
    S: Clone + fmt::Debug + Send + Sync + 'static,
    K: Hash + Eq + Send + Sync + 'static,
{
    let policy = p.on_poll_error();
    let mut consecutive_errors: u32 = 0;

    loop {
        match p.poll(res).await {
            Ok(PollOutcome::Ready(result)) => {
                #[cfg(feature = "metrics")]
                crate::metrics::poll_iteration(true);
                return Ok(result);
            }
            Ok(PollOutcome::Pending { delay_ms }) => {
                #[cfg(feature = "metrics")]
                crate::metrics::poll_iteration(false);
                consecutive_errors = 0;
                if delay_ms > 0 {
                    tokio::time::sleep(std::time::Duration::from_millis(delay_ms)).await;
                }
            }
            Err(e) => match &policy {
                PollErrorPolicy::FailFast => return Err(e),
                PollErrorPolicy::RetryOnError { max_errors } => {
                    consecutive_errors += 1;
                    if consecutive_errors > *max_errors {
                        return Err(e);
                    }
                }
            },
        }
    }
}

// ---------------------------------------------------------------------------
// Type aliases
// ---------------------------------------------------------------------------

/// Type alias for a dynamic [`PollTask`] trait object.
pub type DynPollTask<TState, TResourceKey = Cow<'static, str>> =
    dyn PollTask<TState, TResourceKey> + Send + Sync;

/// Type alias for an `Arc`-wrapped dynamic [`PollTask`] trait object.
pub type PollTaskObject<TState, TResourceKey = Cow<'static, str>> =
    std::sync::Arc<DynPollTask<TState, TResourceKey>>;

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::resource::Resources;
    use crate::task;
    use crate::task::Task;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU32, Ordering};

    // Note: all `#[task::poll]` usages inside the `cano` crate itself use the
    // trait-impl form (`impl PollTask<S> for T`), because the inherent form emits
    // `::cano::PollTask<...>` paths that don't resolve inside this crate. The
    // inherent form is tested in `cano-macros/tests/poll_task_impl.rs` where
    // `::cano` resolves correctly.

    #[derive(Debug, Clone, PartialEq, Eq, Hash)]
    enum Step {
        Wait,
        Done,
        Next,
    }

    // ---------------------------------------------------------------------------
    // (a) PollTask that becomes Ready on the first call
    // ---------------------------------------------------------------------------

    struct ImmediatePoller;

    #[task::poll]
    impl PollTask<Step> for ImmediatePoller {
        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
            Ok(PollOutcome::Ready(TaskResult::Single(Step::Done)))
        }
    }

    #[tokio::test]
    async fn test_poll_task_immediate_via_poll() {
        let poller = ImmediatePoller;
        let res = Resources::new();
        let result = PollTask::poll(&poller, &res).await.unwrap();
        assert_eq!(result, PollOutcome::Ready(TaskResult::Single(Step::Done)));
    }

    #[tokio::test]
    async fn test_poll_task_immediate_via_task_run() {
        let poller = ImmediatePoller;
        let res = Resources::new();
        let result = Task::run(&poller, &res).await.unwrap();
        assert_eq!(result, TaskResult::Single(Step::Done));
    }

    // ---------------------------------------------------------------------------
    // (b) PollTask that takes multiple polls before becoming Ready
    // ---------------------------------------------------------------------------

    struct CountingPoller {
        target: u32,
        count: AtomicU32,
    }

    impl CountingPoller {
        fn new(target: u32) -> Self {
            Self {
                target,
                count: AtomicU32::new(0),
            }
        }
    }

    #[task::poll]
    impl PollTask<Step> for CountingPoller {
        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
            let n = self.count.fetch_add(1, Ordering::Relaxed) + 1;
            if n >= self.target {
                Ok(PollOutcome::Ready(TaskResult::Single(Step::Done)))
            } else {
                Ok(PollOutcome::Pending { delay_ms: 0 })
            }
        }
    }

    #[tokio::test]
    async fn test_poll_task_multiple_polls() {
        let poller = CountingPoller::new(3);
        let res = Resources::new();
        let result = Task::run(&poller, &res).await.unwrap();
        assert_eq!(result, TaskResult::Single(Step::Done));
        assert_eq!(poller.count.load(Ordering::Relaxed), 3);
    }

    // ---------------------------------------------------------------------------
    // (c) Poll error propagates immediately
    // ---------------------------------------------------------------------------

    struct ErrorPoller;

    #[task::poll]
    impl PollTask<Step> for ErrorPoller {
        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
            Err(CanoError::task_execution("poll failed"))
        }
    }

    #[tokio::test]
    async fn test_poll_task_error_propagates() {
        let poller = ErrorPoller;
        let res = Resources::new();
        let err = Task::run(&poller, &res).await.unwrap_err();
        assert!(matches!(err, CanoError::TaskExecution(_)));
    }

    // ---------------------------------------------------------------------------
    // (d) PollTask returning Split
    // ---------------------------------------------------------------------------

    struct SplitPoller;

    #[task::poll]
    impl PollTask<Step> for SplitPoller {
        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
            Ok(PollOutcome::Ready(TaskResult::Split(vec![
                Step::Wait,
                Step::Next,
            ])))
        }
    }

    #[tokio::test]
    async fn test_poll_task_split() {
        let poller = SplitPoller;
        let res = Resources::new();
        let result = Task::run(&poller, &res).await.unwrap();
        assert_eq!(result, TaskResult::Split(vec![Step::Wait, Step::Next]));
    }

    // ---------------------------------------------------------------------------
    // (e) Config + name overrides
    // ---------------------------------------------------------------------------

    struct CustomPoller;

    #[task::poll]
    impl PollTask<Step> for CustomPoller {
        fn config(&self) -> TaskConfig {
            TaskConfig::minimal()
        }
        fn name(&self) -> Cow<'static, str> {
            Cow::Borrowed("my-custom-poller")
        }
        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
            Ok(PollOutcome::Ready(TaskResult::Single(Step::Done)))
        }
    }

    #[test]
    fn test_poll_task_config_override() {
        let poller = CustomPoller;
        assert_eq!(
            PollTask::<Step>::config(&poller).retry_mode.max_attempts(),
            1
        );
    }

    #[test]
    fn test_poll_task_name_override() {
        let poller = CustomPoller;
        assert_eq!(PollTask::<Step>::name(&poller), "my-custom-poller");
    }

    #[test]
    fn test_companion_task_forwards_config_and_name() {
        let poller = CustomPoller;
        assert_eq!(Task::config(&poller).retry_mode.max_attempts(), 1);
        assert_eq!(Task::name(&poller), "my-custom-poller");
    }

    // ---------------------------------------------------------------------------
    // (f) Default config is minimal (no retries)
    // ---------------------------------------------------------------------------

    struct DefaultConfigPoller;

    #[task::poll]
    impl PollTask<Step> for DefaultConfigPoller {
        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
            Ok(PollOutcome::Ready(TaskResult::Single(Step::Done)))
        }
    }

    #[test]
    fn test_poll_task_default_config_is_minimal() {
        let poller = DefaultConfigPoller;
        assert_eq!(
            PollTask::<Step>::config(&poller).retry_mode.max_attempts(),
            1,
            "PollTask default config must be minimal (no retries)"
        );
    }

    #[test]
    fn test_poll_task_default_name_contains_type_name() {
        let poller = DefaultConfigPoller;
        let name = PollTask::<Step>::name(&poller);
        assert!(
            name.contains("DefaultConfigPoller"),
            "default name should contain the type name, got: {name}",
        );
    }

    // ---------------------------------------------------------------------------
    // (g) Dynamic dispatch: Arc<dyn Task<S>>
    // ---------------------------------------------------------------------------

    #[tokio::test]
    async fn test_poll_task_as_dyn_task() {
        let poller: Arc<dyn Task<Step>> = Arc::new(ImmediatePoller);
        let res = Resources::new();
        let result = Task::run(poller.as_ref(), &res).await.unwrap();
        assert_eq!(result, TaskResult::Single(Step::Done));
    }

    // ---------------------------------------------------------------------------
    // (h) Workflow integration
    // ---------------------------------------------------------------------------

    #[tokio::test]
    async fn test_poll_task_in_workflow() {
        use crate::workflow::Workflow;

        struct NextTask;

        #[task]
        impl Task<Step> for NextTask {
            async fn run_bare(&self) -> Result<TaskResult<Step>, CanoError> {
                Ok(TaskResult::Single(Step::Done))
            }
        }

        let poller = CountingPoller::new(2);

        let workflow = Workflow::bare()
            .register(Step::Wait, poller)
            .register(Step::Next, NextTask)
            .add_exit_state(Step::Done);

        // CountingPoller::new(2) -> first poll returns Pending, second returns Ready(Next)
        // But wait: poll 1 => count becomes 1, 1 < 2 => Pending; poll 2 => count becomes 2, 2 >= 2 => Ready(Done)
        // But we registered Step::Done as exit state so Done is the final state
        // Actually CountingPoller returns Single(Step::Done) when ready, so we skip Next entirely
        let result = workflow.orchestrate(Step::Wait).await.unwrap();
        assert_eq!(result, Step::Done);
    }

    // ---------------------------------------------------------------------------
    // (i) run_poll_loop with dyn PollTask (?Sized)
    // ---------------------------------------------------------------------------

    #[tokio::test]
    async fn test_run_poll_loop_dyn_dispatch() {
        let poller: &dyn PollTask<Step> = &ImmediatePoller;
        let res = Resources::new();
        let result = run_poll_loop(poller, &res).await.unwrap();
        assert_eq!(result, TaskResult::Single(Step::Done));
    }

    // ---------------------------------------------------------------------------
    // (j) PollErrorPolicy — RetryOnError succeeds when errors < max_errors
    // ---------------------------------------------------------------------------

    // Emits `count` errors then PollOutcome::Ready.
    struct ErrorThenReadyPoller {
        errors_before_ready: u32,
        count: AtomicU32,
    }

    impl ErrorThenReadyPoller {
        fn new(errors_before_ready: u32) -> Self {
            Self {
                errors_before_ready,
                count: AtomicU32::new(0),
            }
        }
    }

    #[task::poll]
    impl PollTask<Step> for ErrorThenReadyPoller {
        fn on_poll_error(&self) -> PollErrorPolicy {
            PollErrorPolicy::RetryOnError { max_errors: 2 }
        }

        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
            let n = self.count.fetch_add(1, Ordering::Relaxed);
            if n < self.errors_before_ready {
                Err(CanoError::task_execution("transient"))
            } else {
                Ok(PollOutcome::Ready(TaskResult::Single(Step::Done)))
            }
        }
    }

    #[tokio::test]
    async fn test_retry_on_error_within_budget_succeeds() {
        // 2 errors then Ready — max_errors = 2 so both errors are tolerated.
        let poller = ErrorThenReadyPoller::new(2);
        let res = Resources::new();
        let result = Task::run(&poller, &res).await.unwrap();
        assert_eq!(result, TaskResult::Single(Step::Done));
        assert_eq!(poller.count.load(Ordering::Relaxed), 3); // 2 errors + 1 Ready
    }

    // ---------------------------------------------------------------------------
    // (k) PollErrorPolicy — RetryOnError fails when consecutive errors exceed max
    // ---------------------------------------------------------------------------

    #[tokio::test]
    async fn test_retry_on_error_exceeds_budget_fails() {
        // 3 consecutive errors with max_errors = 2 → should fail on the 3rd error.
        let poller = ErrorThenReadyPoller::new(3);
        let res = Resources::new();
        let err = Task::run(&poller, &res).await.unwrap_err();
        assert!(matches!(err, CanoError::TaskExecution(_)));
        // poll called exactly 3 times: errors 1, 2, 3 (third exceeds max_errors=2)
        assert_eq!(poller.count.load(Ordering::Relaxed), 3);
    }

    // ---------------------------------------------------------------------------
    // (l) PollErrorPolicy — Pending resets the consecutive-error counter
    // ---------------------------------------------------------------------------

    // Sequence: error → Pending → error → Ready (Pending resets counter)
    struct ErrorPendingErrorReadyPoller {
        count: AtomicU32,
    }

    impl ErrorPendingErrorReadyPoller {
        fn new() -> Self {
            Self {
                count: AtomicU32::new(0),
            }
        }
    }

    #[task::poll]
    impl PollTask<Step> for ErrorPendingErrorReadyPoller {
        fn on_poll_error(&self) -> PollErrorPolicy {
            PollErrorPolicy::RetryOnError { max_errors: 1 }
        }

        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
            match self.count.fetch_add(1, Ordering::Relaxed) {
                0 => Err(CanoError::task_execution("first error")),
                1 => Ok(PollOutcome::Pending { delay_ms: 0 }),
                2 => Err(CanoError::task_execution("second error after reset")),
                _ => Ok(PollOutcome::Ready(TaskResult::Single(Step::Done))),
            }
        }
    }

    #[tokio::test]
    async fn test_retry_on_error_pending_resets_counter() {
        // error(1) → Pending resets → error(1) → Ready: should succeed, 4 polls
        let poller = ErrorPendingErrorReadyPoller::new();
        let res = Resources::new();
        let result = Task::run(&poller, &res).await.unwrap();
        assert_eq!(result, TaskResult::Single(Step::Done));
        assert_eq!(poller.count.load(Ordering::Relaxed), 4);
    }

    // ---------------------------------------------------------------------------
    // (m) Default on_poll_error is FailFast
    // ---------------------------------------------------------------------------

    #[tokio::test]
    async fn test_default_on_poll_error_is_fail_fast() {
        // ErrorPoller has no on_poll_error override — default is FailFast.
        // First error should propagate immediately (1 poll call).
        let poller = ErrorPoller;
        let res = Resources::new();
        let err = Task::run(&poller, &res).await.unwrap_err();
        assert!(matches!(err, CanoError::TaskExecution(_)));
        // Verify the default method itself
        assert_eq!(
            PollTask::<Step>::on_poll_error(&poller),
            PollErrorPolicy::FailFast
        );
    }

    // ---------------------------------------------------------------------------
    // (n) attempt_timeout bounds the poll loop when dispatched via Workflow::orchestrate
    // ---------------------------------------------------------------------------

    struct InfinitePendingPoller;

    #[task::poll]
    impl PollTask<Step> for InfinitePendingPoller {
        fn config(&self) -> TaskConfig {
            TaskConfig::minimal().with_attempt_timeout(std::time::Duration::from_millis(20))
        }

        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<Step>, CanoError> {
            Ok(PollOutcome::Pending { delay_ms: 1 })
        }
    }

    #[tokio::test]
    async fn test_attempt_timeout_bounds_infinite_pending_loop() {
        use crate::workflow::Workflow;

        let workflow = Workflow::bare()
            .register(Step::Wait, InfinitePendingPoller)
            .add_exit_state(Step::Done);

        let start = std::time::Instant::now();
        let err = workflow.orchestrate(Step::Wait).await.unwrap_err();
        let elapsed = start.elapsed();

        // The FSM wraps the failure with state context; `.inner()` peels one layer.
        assert!(
            matches!(err.inner(), CanoError::Timeout(_)),
            "expected CanoError::Timeout, got: {err:?}"
        );
        // Should complete well within 500ms even with scheduling jitter
        assert!(
            elapsed < std::time::Duration::from_millis(500),
            "timeout took too long: {elapsed:?}"
        );
    }
}

#[cfg(all(test, feature = "metrics"))]
mod metrics_tests {
    use super::*;
    use crate::metrics::test_support::*;
    use crate::task::Task;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU32, Ordering};

    #[derive(Debug, Clone, PartialEq, Eq, Hash)]
    enum St {
        Done,
    }

    // PollTask returning Pending twice then Ready(Single(Done))
    struct TwicePendingPoller {
        count: Arc<AtomicU32>,
    }

    // Use the trait-impl form: inside cano the inherent form emits ::cano:: paths
    // that don't resolve; trait-impl form works everywhere.
    #[crate::task::poll]
    impl PollTask<St> for TwicePendingPoller {
        async fn poll(&self, _res: &Resources) -> Result<PollOutcome<St>, CanoError> {
            let n = self.count.fetch_add(1, Ordering::Relaxed);
            if n < 2 {
                Ok(PollOutcome::Pending { delay_ms: 0 })
            } else {
                Ok(PollOutcome::Ready(TaskResult::Single(St::Done)))
            }
        }
    }

    /// Test via run_poll_loop (the free fn used when registered via Workflow::register
    /// with the inherent macro form, and directly exercising the loop body in poll.rs).
    ///
    /// Note: inside `cano`, the trait-impl form of `#[task::poll]` inlines the loop body
    /// rather than delegating to `run_poll_loop`; so we call `run_poll_loop` directly here.
    /// External callers using the inherent `#[task::poll(state = S)]` form always
    /// delegate to `run_poll_loop`.
    #[test]
    fn poll_iterations_counted_pending_twice_ready_once() {
        let (result, rows) = run_with_recorder(|| async {
            let poller = TwicePendingPoller {
                count: Arc::new(AtomicU32::new(0)),
            };
            let res = crate::resource::Resources::new();
            run_poll_loop(&poller, &res).await
        });
        assert!(result.is_ok(), "poll loop should succeed: {result:?}");
        assert_eq!(
            counter(
                &rows,
                "cano_poll_iterations_total",
                &[("outcome", "pending")]
            ),
            2,
            "expected 2 pending iterations"
        );
        assert_eq!(
            counter(&rows, "cano_poll_iterations_total", &[("outcome", "ready")]),
            1,
            "expected 1 ready iteration"
        );
    }
}