swiftide-tasks 0.34.0

Composable LLM agents, typed task graphs, and streaming RAG pipelines in Rust
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
//! Transition values describe how task execution should continue after a node finishes.
//!
//! This module contains the public building blocks for linear transitions, fan-out, join
//! configuration, and join payload inspection.
//!
//! # Examples
//!
//! ```no_run
//! use swiftide_tasks::{JoinInput, NodeError, Task, Transition};
//!
//! let mut task = Task::<i32, i32>::new();
//! let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
//! let left = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
//! let right = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 2) });
//! let join = task.register_node_fn(|input: &JoinInput<i32>| -> Result<i32, NodeError> {
//!     Ok(input.iter().copied().sum())
//! });
//!
//! task.starts_with(start);
//! task.register_transition(start, move |value| {
//!     // `join_with` defines the branch group and the join node that waits for it.
//!     Transition::fan_out(&left, value)
//!         .and(&right, value)
//!         .join_with(join.join())
//! })?;
//! // Branches still decide where their own output goes before the join can run. Fan-out branches
//! // cannot finish independently; use a join node as the single place that finishes the task.
//! task.register_transition(left, join.join())?;
//! task.register_transition(right, join.join())?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
use std::{any::Any, marker::PhantomData, sync::Arc};

use super::{
    errors::TaskError,
    node::NodeId,
    traits::{NodeArg, TaskNode},
};

/// Identifies a concrete runtime branch within a task run.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct BranchId(pub(crate) usize);

/// Controls whether newly scheduled work should run one branch at a time or concurrently.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub enum ConcurrencyModel {
    /// Run one branch to completion before starting the next runnable branch.
    #[default]
    Sequential,
    /// Allow multiple runnable branches to execute at the same time.
    Parallel,
}

#[derive(Debug, Clone)]
pub(crate) struct NextNode {
    pub(crate) node_id: usize,
    pub(crate) context: Arc<dyn Any + Send + Sync>,
}

impl NextNode {
    pub(crate) fn new<T: TaskNode + ?Sized>(node_id: NodeId<T>, context: T::Input) -> Self
    where
        <T as TaskNode>::Input: 'static,
    {
        Self {
            node_id: node_id.id(),
            context: Arc::new(context) as Arc<dyn Any + Send + Sync>,
        }
    }
}

type ErasedPayload = Arc<dyn Any + Send + Sync>;

#[derive(Clone, Copy)]
enum JoinInputFactory {
    Fallible(fn(Vec<ErasedPayload>) -> Result<ErasedPayload, TaskError>),
    Infallible(fn(Vec<ErasedPayload>) -> ErasedPayload),
}

impl JoinInputFactory {
    fn build(self, payloads: Vec<ErasedPayload>) -> Result<ErasedPayload, TaskError> {
        match self {
            JoinInputFactory::Fallible(factory) => factory(payloads),
            JoinInputFactory::Infallible(factory) => Ok(factory(payloads)),
        }
    }
}

#[doc(hidden)]
#[derive(Clone, Copy)]
pub struct JoinDefinition {
    pub(crate) join_node_id: usize,
    input_factory: JoinInputFactory,
}

impl std::fmt::Debug for JoinDefinition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JoinDefinition")
            .field("join_node_id", &self.join_node_id)
            .finish_non_exhaustive()
    }
}

impl JoinDefinition {
    fn typed<Payload: NodeArg>(join_node_id: usize) -> Self {
        Self {
            join_node_id,
            input_factory: JoinInputFactory::Fallible(typed_join_input::<Payload>),
        }
    }

    fn any(join_node_id: usize) -> Self {
        Self {
            join_node_id,
            input_factory: JoinInputFactory::Infallible(any_join_input),
        }
    }

    pub(crate) fn into_input(
        self,
        payloads: Vec<ErasedPayload>,
    ) -> Result<ErasedPayload, TaskError> {
        self.input_factory.build(payloads)
    }
}

fn typed_join_input<Payload: NodeArg>(
    payloads: Vec<ErasedPayload>,
) -> Result<ErasedPayload, TaskError> {
    let branches = payloads
        .into_iter()
        .map(|payload| {
            payload.downcast::<Payload>().map_err(|_| {
                TaskError::invalid_state(format!(
                    "Join payload expected type {}",
                    std::any::type_name::<Payload>()
                ))
            })
        })
        .collect::<Result<Vec<_>, _>>()?;

    Ok(Arc::new(JoinInput::<Payload>::new(branches)) as ErasedPayload)
}

fn any_join_input(payloads: Vec<ErasedPayload>) -> ErasedPayload {
    Arc::new(AnyJoinInput::new(payloads)) as ErasedPayload
}

/// A configured join destination for branches that should converge into a join node.
///
/// Build a `JoinTarget` from a join node with [`NodeId::join`](crate::NodeId::join),
/// and then register it through
/// [`Task::register_transition`](crate::Task::register_transition).
/// Users normally do not construct this type directly.
#[must_use]
pub struct JoinTarget<T: TaskNode<Input = JoinInput<Payload>> + ?Sized, Payload: NodeArg> {
    pub(crate) definition: JoinDefinition,
    _marker: PhantomData<(*const T, Payload)>,
}

/// A join target with a synchronous payload mapping step.
///
/// This is returned by [`JoinTarget::map`] and registered with
/// [`Task::register_transition`](crate::Task::register_transition).
#[must_use]
pub struct MappedJoinTarget<T: TaskNode<Input = JoinInput<Payload>> + ?Sized, Payload: NodeArg, F> {
    pub(crate) join_target: JoinTarget<T, Payload>,
    pub(crate) map: F,
}

/// A configured join destination for branches with mixed payload types.
///
/// Build an `AnyJoinTarget` from an [`AnyJoinInput`] node with
/// [`NodeId::join_any`](crate::NodeId::join_any).
#[must_use]
pub struct AnyJoinTarget<T: TaskNode<Input = AnyJoinInput> + ?Sized> {
    pub(crate) definition: JoinDefinition,
    _marker: PhantomData<T>,
}

/// A value accepted by [`FanOutTransition::join_with`].
#[doc(hidden)]
pub trait JoinDestination {
    #[doc(hidden)]
    fn into_definition(self) -> JoinDefinition;
}

impl<T, Payload> JoinDestination for JoinTarget<T, Payload>
where
    T: TaskNode<Input = JoinInput<Payload>> + ?Sized,
    Payload: NodeArg,
{
    fn into_definition(self) -> JoinDefinition {
        self.definition
    }
}

impl<T> JoinDestination for AnyJoinTarget<T>
where
    T: TaskNode<Input = AnyJoinInput> + ?Sized,
{
    fn into_definition(self) -> JoinDefinition {
        self.definition
    }
}

impl<T, Payload> JoinTarget<T, Payload>
where
    T: TaskNode<Input = JoinInput<Payload>> + ?Sized,
    Payload: NodeArg,
{
    pub(crate) fn new(node_id: NodeId<T>) -> Self {
        Self {
            definition: JoinDefinition::typed::<Payload>(node_id.id()),
            _marker: PhantomData,
        }
    }

    /// Maps each joining branch output into the payload stored for the join node.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use swiftide_tasks::{JoinInput, NodeError, Task, Transition};
    ///
    /// let mut task = Task::<i32, i32>::new();
    /// let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
    /// let branch = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
    /// let join = task.register_node_fn(|input: &JoinInput<i32>| -> Result<i32, NodeError> {
    ///     Ok(input.iter().copied().sum())
    /// });
    ///
    /// task.starts_with(start);
    /// task.register_transition(start, move |value| {
    ///     Transition::fan_out(&branch, value)
    ///         .join_with(join.join())
    /// })?;
    /// task.register_transition(branch, join.join().map(|value| value * 2))?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn map<F>(self, map: F) -> MappedJoinTarget<T, Payload, F>
    where
        F: Send + Sync + 'static,
    {
        MappedJoinTarget {
            join_target: self,
            map,
        }
    }

    /// Maps each joining branch output asynchronously before storing it for the join node.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use swiftide_tasks::{JoinInput, NodeError, Task, Transition};
    ///
    /// let mut task = Task::<i32, i32>::new();
    /// let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
    /// let branch = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
    /// let join = task.register_node_fn(|input: &JoinInput<i32>| -> Result<i32, NodeError> {
    ///     Ok(input.iter().copied().sum())
    /// });
    ///
    /// task.starts_with(start);
    /// task.register_transition(start, move |value| {
    ///     Transition::fan_out(&branch, value)
    ///         .join_with(join.join())
    /// })?;
    /// task.register_transition_async(branch, join.join().map_async(|value| async move { value * 2 }))?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn map_async<F>(self, map: F) -> MappedJoinTarget<T, Payload, F>
    where
        F: Send + Sync + 'static,
    {
        MappedJoinTarget {
            join_target: self,
            map,
        }
    }
}

impl<T: TaskNode<Input = AnyJoinInput> + ?Sized> AnyJoinTarget<T> {
    pub(crate) fn new(node_id: NodeId<T>) -> Self {
        Self {
            definition: JoinDefinition::any(node_id.id()),
            _marker: PhantomData,
        }
    }
}

/// Describes how task execution should continue after a node completes.
///
/// Most transitions are created either through [`NodeId::transitions_with`] for the linear case or
/// [`Transition::fan_out`] plus [`FanOutTransition::join_with`] for branching. Use
/// [`Transition::pause`] and [`Transition::error`] when a transition closure needs to control task
/// execution directly.
///
/// # Examples
///
/// ```no_run
/// use swiftide_tasks::{NodeError, Task, TaskRunOutcome, Transition};
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut task = Task::<i32, i32>::new();
///
/// let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
/// let left = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
/// let right = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 2) });
/// let join = task.register_node_fn(
///     |input: &swiftide_tasks::JoinInput<i32>| -> Result<i32, NodeError> {
///         Ok(input.iter().copied().sum())
///     },
/// );
///
/// task.starts_with(start);
/// task.register_transition(start, move |value| {
///     Transition::fan_out(&left, value)
///         .and(&right, value)
///         .join_with(join.join())
///         .concurrency_model(swiftide_tasks::ConcurrencyModel::Parallel)
/// })?;
/// task.register_transition(left, join.join())?;
/// task.register_transition(right, join.join())?;
/// task.register_transition(join, task.transitions_to_finish())?;
///
/// assert_eq!(task.run(1).await?, TaskRunOutcome::Completed(5));
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
#[must_use]
pub struct Transition {
    pub(crate) action: TransitionAction,
    pub(crate) concurrency_model: Option<ConcurrencyModel>,
}

/// A linear transition that preserves the destination node type.
///
/// This is returned by [`Transition::next`] and [`NodeId::transitions_with`], then accepted by
/// [`Task::register_transition`](crate::Task::register_transition) closures.
#[must_use]
pub struct MarkedTransition<To: TaskNode + ?Sized>(Transition, PhantomData<To>);

impl<To: TaskNode + ?Sized> MarkedTransition<To> {
    pub(crate) fn new(transition: Transition) -> Self {
        Self(transition, PhantomData)
    }

    pub(crate) fn into_inner(self) -> Transition {
        self.0
    }

    /// Overrides the concurrency model for work scheduled by this transition.
    pub fn concurrency_model(self, concurrency_model: ConcurrencyModel) -> Self {
        Self::new(self.0.concurrency_model(concurrency_model))
    }
}

impl<To: TaskNode + ?Sized> From<MarkedTransition<To>> for Transition {
    fn from(marked_transition: MarkedTransition<To>) -> Self {
        marked_transition.into_inner()
    }
}

/// A typed fan-out transition builder that must be connected to a join before it can run.
///
/// Build one with [`Transition::fan_out`] and add additional branch targets with
/// [`FanOutTransition::and`]. Each branch input is type-checked before the branch target is erased
/// for runtime scheduling.
///
/// Fan-out always represents a joined branch group. True unjoined fan-out with multiple
/// independent task endings is not supported; model that shape by sending each branch to a join
/// node and finishing from the join node.
#[derive(Debug)]
#[must_use]
pub struct FanOutTransition {
    targets: Vec<NextNode>,
    concurrency_model: Option<ConcurrencyModel>,
}

impl FanOutTransition {
    /// Adds another typed branch target to this fan-out.
    ///
    /// The branch input must convert into the target node's input type.
    pub fn and<T, Input>(mut self, node_id: &NodeId<T>, context: Input) -> Self
    where
        T: TaskNode + ?Sized,
        Input: Into<T::Input>,
    {
        self.targets.push(NextNode::new(*node_id, context.into()));
        self
    }

    /// Attaches every branch from this fan-out to the provided join target.
    ///
    /// This defines the branch group and the join node that waits for every branch in that group.
    /// Each branch still needs its own registered transition to the same join target when it is
    /// ready to contribute its output. If branches are conceptually terminal, register each branch
    /// to transition to this join target and finish from the join node.
    pub fn join_with<J>(self, join_target: J) -> Transition
    where
        J: JoinDestination,
    {
        Transition {
            action: TransitionAction::FanOut {
                targets: self.targets,
                join: join_target.into_definition(),
            },
            concurrency_model: self.concurrency_model,
        }
    }

    /// Overrides the concurrency model for work scheduled by this fan-out.
    pub fn concurrency_model(mut self, concurrency_model: ConcurrencyModel) -> Self {
        self.concurrency_model = Some(concurrency_model);
        self
    }
}

#[derive(Debug)]
pub(crate) enum TransitionAction {
    Next(NextNode),
    FanOut {
        targets: Vec<NextNode>,
        join: JoinDefinition,
    },
    Pause,
    Error(Box<dyn std::error::Error + Send + Sync>),
    Finish(Arc<dyn Any + Send + Sync>),
}

impl Transition {
    pub(crate) fn from_next_node(next_node: NextNode) -> Self {
        Self {
            action: TransitionAction::Next(next_node),
            concurrency_model: None,
        }
    }

    /// Continues execution at `node_id` with the provided input.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use swiftide_tasks::{NodeError, Task, Transition};
    ///
    /// let mut task = Task::<i32, i32>::new();
    /// let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
    /// let finish = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input * 2) });
    ///
    /// task.starts_with(start);
    /// task.register_transition(start, move |value| Transition::next(&finish, value))?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn next<T: TaskNode + ?Sized>(
        node_id: &NodeId<T>,
        context: T::Input,
    ) -> MarkedTransition<T> {
        MarkedTransition::new(Self::from_next_node(NextNode::new(*node_id, context)))
    }

    /// Builds a fan-out that schedules one or more branches from the current node output.
    ///
    /// The returned [`FanOutTransition`] must attach an explicit join with
    /// [`FanOutTransition::join_with`] before it can be returned from a task transition.
    /// Branches in that fan-out are joined branches, not independent task endings. To express
    /// multiple logical endings, transition each branch to a join node and finish the task from the
    /// join node.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use swiftide_tasks::{NodeError, Task, Transition};
    ///
    /// let mut task = Task::<i32, i32>::new();
    /// let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
    /// let left = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
    /// let right = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 2) });
    ///
    /// task.starts_with(start);
    /// let join = task.register_node_fn(
    ///     |input: &swiftide_tasks::JoinInput<i32>| -> Result<i32, NodeError> {
    ///         Ok(input.iter().copied().sum())
    ///     },
    /// );
    ///
    /// task.register_transition(start, move |value| {
    ///     Transition::fan_out(&left, value)
    ///         .and(&right, value)
    ///         .join_with(join.join())
    /// })?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn fan_out<T, Input>(node_id: &NodeId<T>, context: Input) -> FanOutTransition
    where
        T: TaskNode + ?Sized,
        Input: Into<T::Input>,
    {
        FanOutTransition {
            targets: vec![NextNode::new(*node_id, context.into())],
            concurrency_model: None,
        }
    }

    /// Pauses the current branch.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use swiftide_tasks::{NodeError, Task, TaskRunOutcome, Transition};
    ///
    /// # #[tokio::main(flavor = "current_thread")]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut task = Task::<i32, ()>::new();
    /// let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
    ///
    /// task.starts_with(start);
    /// task.register_transition(start, |_value| Transition::pause())?;
    ///
    /// assert_eq!(task.run(1).await?, TaskRunOutcome::Paused);
    /// # Ok(())
    /// # }
    /// ```
    pub fn pause() -> Self {
        Self {
            action: TransitionAction::Pause,
            concurrency_model: None,
        }
    }

    /// Fails the current branch with the provided error.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::Error;
    ///
    /// use swiftide_tasks::{NodeError, Task, Transition};
    ///
    /// # #[tokio::main(flavor = "current_thread")]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut task = Task::<i32, i32>::new();
    /// let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
    ///
    /// task.starts_with(start);
    /// task.register_transition(start, |_value| Transition::error(Error::other("boom")))?;
    ///
    /// assert!(task.run(1).await.is_err());
    /// # Ok(())
    /// # }
    /// ```
    pub fn error(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self {
        Self {
            action: TransitionAction::Error(error.into()),
            concurrency_model: None,
        }
    }

    pub(crate) fn finish<T: NodeArg>(output: T) -> Self {
        Self {
            action: TransitionAction::Finish(Arc::new(output) as Arc<dyn Any + Send + Sync>),
            concurrency_model: None,
        }
    }

    /// Overrides the concurrency model for work scheduled by this transition.
    pub fn concurrency_model(mut self, concurrency_model: ConcurrencyModel) -> Self {
        self.concurrency_model = Some(concurrency_model);
        self
    }
}

/// The typed aggregated view of branches that have reached a join node.
///
/// `JoinInput<T>` is the normal join input. It is only available when every branch contributes a
/// `T`, or maps its output into `T` before joining. For mixed payload types, use [`AnyJoinInput`].
#[derive(Clone)]
pub struct JoinInput<T: NodeArg> {
    branches: Vec<Arc<T>>,
}

impl<T: NodeArg> std::fmt::Debug for JoinInput<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JoinInput")
            .field("payload_type", &std::any::type_name::<T>())
            .field("len", &self.branches.len())
            .finish()
    }
}

impl<T: NodeArg> JoinInput<T> {
    pub(crate) fn new(branches: Vec<Arc<T>>) -> Self {
        Self { branches }
    }

    /// Iterates over every ready branch payload in stable branch creation order.
    ///
    /// This is the usual way to read join payloads.
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.branches.iter().map(Arc::as_ref)
    }

    /// Returns the number of branch payloads in this join.
    pub fn len(&self) -> usize {
        self.branches.len()
    }

    /// Returns `true` when this join has no branch payloads.
    pub fn is_empty(&self) -> bool {
        self.branches.is_empty()
    }
}

/// The type-erased aggregated view of branches that have reached a join node.
///
/// Use this for joins that intentionally collect mixed branch payload types. Homogeneous joins
/// should prefer [`JoinInput<T>`](JoinInput), which is checked when transitions are registered.
#[derive(Clone)]
pub struct AnyJoinInput {
    branches: Vec<ErasedPayload>,
}

impl std::fmt::Debug for AnyJoinInput {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AnyJoinInput")
            .field("len", &self.branches.len())
            .finish()
    }
}

impl AnyJoinInput {
    pub(crate) fn new(branches: Vec<ErasedPayload>) -> Self {
        Self { branches }
    }

    /// Iterates over every type-erased branch payload in stable branch creation order.
    pub fn iter_any(&self) -> impl Iterator<Item = &(dyn Any + Send + Sync)> {
        self.branches.iter().map(Arc::as_ref)
    }

    /// Iterates over every ready branch payload that can be downcast to `T`.
    ///
    /// Values are yielded in stable branch creation order. This is the usual way to read join
    /// payloads from a mixed join when you only need one payload type.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use swiftide_tasks::{AnyJoinInput, NodeError, Task, Transition};
    ///
    /// let mut task = Task::<i32, i32>::new();
    /// let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
    /// let left = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
    /// let right = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 2) });
    /// let join = task.register_node_fn(|input: &AnyJoinInput| -> Result<i32, NodeError> {
    ///     Ok(input.iter::<i32>().copied().sum())
    /// });
    ///
    /// task.starts_with(start);
    /// task.register_transition(start, move |value| {
    ///     Transition::fan_out(&left, value)
    ///         .and(&right, value)
    ///         .join_with(join.join_any())
    /// })?;
    /// task.register_transition(left, join.join_any())?;
    /// task.register_transition(right, join.join_any())?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn iter<T: NodeArg>(&self) -> impl Iterator<Item = &T> {
        self.iter_any()
            .filter_map(|value| value.downcast_ref::<T>())
    }

    /// Returns the number of branch payloads in this join.
    pub fn len(&self) -> usize {
        self.branches.len()
    }

    /// Returns `true` when this join has no branch payloads.
    pub fn is_empty(&self) -> bool {
        self.branches.is_empty()
    }
}