aidaemon_core/outcome.rs
1use serde::{Deserialize, Serialize};
2
3use crate::limits::LimitKind;
4use crate::provider::Usage;
5use crate::receipt::Receipt;
6use crate::state::{Interruption, RunState};
7use crate::stuck::StuckSignal;
8
9/// Why a run ended. Every exit path maps to exactly one variant, so callers
10/// never have to parse prose to learn what happened.
11#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12#[serde(rename_all = "snake_case", tag = "status")]
13#[non_exhaustive]
14#[allow(clippy::large_enum_variant)] // boxing `Interrupted` would break every match on it
15pub enum RunStatus {
16 /// The model gave a final answer and every stop hook accepted it.
17 Completed,
18 /// Paused before a tool call; resume with [`crate::Agent::resume`].
19 Interrupted {
20 interruption: Interruption,
21 },
22 LimitReached {
23 limit: LimitKind,
24 },
25 /// A custom [`crate::StopCondition`] ended the run.
26 Stopped {
27 reason: String,
28 },
29 Stuck {
30 signal: StuckSignal,
31 },
32 Cancelled,
33 /// The provider failed in a way the provider layer could not recover.
34 ProviderFailed {
35 error: String,
36 },
37 /// A tool ended the run with [`crate::ToolOutput::finish`]; `tool` is
38 /// the name of the tool whose finish won. A `Succeeded` finish in a run
39 /// with declined effects ([`crate::RunState::declined`]) is reported as
40 /// `Partial`.
41 Finished {
42 outcome: FinishOutcome,
43 tool: String,
44 },
45}
46
47/// How a tool-driven finish judged the work.
48#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
49#[serde(rename_all = "snake_case")]
50#[non_exhaustive]
51pub enum FinishOutcome {
52 Succeeded,
53 Partial,
54 Blocked,
55}
56
57/// The typed result of [`crate::Agent::run`] or [`crate::Agent::resume`].
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
59pub struct RunOutcome {
60 pub status: RunStatus,
61 /// Text of the last assistant message, when the run completed.
62 pub final_text: Option<String>,
63 /// Full state, ready to persist, resume, or continue with
64 /// [`RunState::with_user_message`].
65 pub state: RunState,
66}
67
68impl RunOutcome {
69 /// The run stopped because the model answered and every stop hook let
70 /// it stop. That is not the same as the work being done: see
71 /// [`RunOutcome::is_succeeded`].
72 pub fn is_completed(&self) -> bool {
73 matches!(self.status, RunStatus::Completed)
74 }
75
76 /// The run's objective was met, as far as the kernel and its hooks can
77 /// tell: it completed with no unmet requirement and nothing the owner
78 /// declined, or a tool finished it as `Succeeded` (the kernel already
79 /// reports a finish in a run with declines as `Partial`).
80 pub fn is_succeeded(&self) -> bool {
81 match &self.status {
82 RunStatus::Completed => self.state.unmet.is_empty() && self.state.declined.is_empty(),
83 RunStatus::Finished { outcome, .. } => *outcome == FinishOutcome::Succeeded,
84 _ => false,
85 }
86 }
87
88 /// Requirements a stop hook reported as unmet when the run stopped.
89 pub fn unmet(&self) -> &[String] {
90 &self.state.unmet
91 }
92
93 /// Usage of the whole session.
94 pub fn usage(&self) -> Usage {
95 self.state.usage
96 }
97
98 /// Usage of this run only.
99 pub fn run_usage(&self) -> Usage {
100 self.state.run_usage
101 }
102
103 pub fn receipts(&self) -> &[Receipt] {
104 &self.state.receipts
105 }
106
107 pub fn turns(&self) -> u32 {
108 self.state.turn
109 }
110
111 /// How long the run took, summed across resumes, on the runtime's
112 /// clock (Tokio's, so a test with paused time measures paused time).
113 pub fn elapsed(&self) -> std::time::Duration {
114 std::time::Duration::from_millis(self.state.elapsed_ms)
115 }
116
117 /// Each tool call of this run with the result the model was given, in
118 /// call order: the receipt (tool, arguments, status, details) and the
119 /// text of the tool message that answered it. A sub-agent's answer
120 /// reaches its parent this way. `text` is `None` when no message
121 /// answers the call any more (a hook edited the transcript).
122 pub fn tool_results(&self) -> Vec<CallResult<'_>> {
123 self.state
124 .run_receipts()
125 .map(|receipt| {
126 let message = self.state.messages.iter().rev().find(|m| {
127 m.role == crate::Role::Tool
128 && m.tool_call_id.as_deref() == Some(receipt.call_id.as_str())
129 });
130 CallResult {
131 receipt,
132 text: message.map(|m| m.content.as_str()),
133 }
134 })
135 .collect()
136 }
137}
138
139/// One tool call of a run and what the model was told about it (see
140/// [`RunOutcome::tool_results`]).
141#[derive(Debug, Clone, Copy, PartialEq)]
142pub struct CallResult<'a> {
143 pub receipt: &'a Receipt,
144 /// The tool message the model saw for this call.
145 pub text: Option<&'a str>,
146}
147
148impl CallResult<'_> {
149 /// The call ran and succeeded.
150 pub fn succeeded(&self) -> bool {
151 self.receipt.succeeded()
152 }
153}