Skip to main content

a2a_protocol_client/delegation/
outcome.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! [`Outcome`] and [`Delegated`]: what became of a delegated task.
5
6use a2a_protocol_types::cancel_cause::{CancelCause, cause_of};
7use a2a_protocol_types::failure::{FailureClass, class_of};
8use a2a_protocol_types::{Message, TaskState, TaskStatus};
9
10use crate::error::ClientError;
11
12/// What became of a delegated task.
13#[derive(Debug)]
14#[non_exhaustive]
15pub enum Outcome {
16    /// The child completed.
17    Completed(TaskStatus),
18    /// The child failed or was rejected (`status.state` says which), with
19    /// the class it declared, or [`FailureClass::Internal`] if it declared
20    /// none.
21    Failed {
22        /// The child's final status.
23        status: TaskStatus,
24        /// Its failure class.
25        class: FailureClass,
26    },
27    /// The child reached `canceled`; [`cancel_cause`](Self::cancel_cause)
28    /// says who stopped it, when its server said.
29    Canceled(TaskStatus),
30    /// The child is waiting for the parent: `input-required` or
31    /// `auth-required`. It was not cancelled; continue it by sending a
32    /// message with its task id, or cancel it.
33    Interrupted(TaskStatus),
34    /// The agent answered with a message and created no task.
35    Message(Message),
36    /// The child's stream failed or ended before the child settled, and
37    /// could not be resumed (see [`ResumePolicy`](super::ResumePolicy)). The
38    /// child, if its id was known, was sent `CancelTask`.
39    Lost(Option<ClientError>),
40    /// A cancel was asked for, but the child's id was never learned within
41    /// the wait, so no `CancelTask` could be sent. A child may be running.
42    Unreachable,
43    /// `CancelTask` was accepted, but the task it returned is not yet in a
44    /// terminal state; the agent stops the child asynchronously.
45    CancelRequested(TaskStatus),
46    /// `CancelTask` was sent and refused or failed.
47    CancelFailed(ClientError),
48    /// The supervisor was shut down and handed the child over: the child was
49    /// left running and its journal record left open, at the position the
50    /// stream had reached, for the next supervisor to find with
51    /// `DelegationJournal::open` and [`Delegation::reattach`](super::Delegation::reattach).
52    /// See [`Delegation::wait_or_hand_off`](super::Delegation::wait_or_hand_off).
53    HandedOff,
54}
55
56impl Outcome {
57    pub(super) fn of(status: TaskStatus) -> Option<Self> {
58        Some(match status.state {
59            TaskState::Completed => Self::Completed(status),
60            TaskState::Canceled => Self::Canceled(status),
61            TaskState::Failed | TaskState::Rejected => {
62                let class = status
63                    .message
64                    .as_ref()
65                    .and_then(class_of)
66                    .unwrap_or(FailureClass::Internal);
67                Self::Failed { status, class }
68            }
69            s if s.is_interrupted() => Self::Interrupted(status),
70            _ => return None,
71        })
72    }
73
74    /// Who stopped a [`Canceled`](Self::Canceled) child: a `CancelTask`, an
75    /// operator's halt or a lapsed lease (extension `cancel/v1`). `None` for
76    /// any other outcome, and for a canceled status that names no cause this
77    /// version knows, as one from another SDK does: never guessed.
78    #[must_use]
79    pub fn cancel_cause(&self) -> Option<CancelCause> {
80        match self {
81            Self::Canceled(status) => status.message.as_ref().and_then(cause_of),
82            _ => None,
83        }
84    }
85}
86
87/// A delegation that has ended: the child's id, if it was learned, and its
88/// outcome.
89#[derive(Debug)]
90#[non_exhaustive]
91pub struct Delegated {
92    /// The child's task id; `None` if no event named it.
93    pub task_id: Option<String>,
94    /// What became of it.
95    pub outcome: Outcome,
96}
97
98#[cfg(test)]
99mod tests {
100    use a2a_protocol_types::cancel_cause::{CancelCause, set_cause};
101    use a2a_protocol_types::{Message, TaskState, TaskStatus};
102
103    use super::Outcome;
104
105    /// COR-04: a canceled child says who stopped it when its server wrote a
106    /// cause, and `None` when a peer wrote none.
107    #[test]
108    fn a_canceled_child_says_who_stopped_it_and_a_bare_one_says_nothing() {
109        let bare = Outcome::of(TaskStatus::new(TaskState::Canceled)).unwrap();
110        assert!(matches!(bare, Outcome::Canceled(_)));
111        assert_eq!(bare.cancel_cause(), None);
112        let mut note = Message::agent_text("m", "halted");
113        set_cause(&mut note, CancelCause::Halted);
114        let halted =
115            Outcome::of(TaskStatus::new(TaskState::Canceled).with_message(note.clone())).unwrap();
116        assert_eq!(halted.cancel_cause(), Some(CancelCause::Halted));
117        let failed = Outcome::of(TaskStatus::new(TaskState::Failed).with_message(note)).unwrap();
118        assert_eq!(
119            failed.cancel_cause(),
120            None,
121            "only a canceled child has a cause"
122        );
123    }
124}