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
//! Error types for adk-graph
use std::time::Duration;
use crate::interrupt::Interrupt;
use thiserror::Error;
/// Result type for graph operations
pub type Result<T> = std::result::Result<T, GraphError>;
/// Errors that can occur during graph operations
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum GraphError {
/// Graph structure is invalid
#[error("Invalid graph structure: {0}")]
InvalidGraph(String),
/// Node not found
#[error("Node not found: {0}")]
NodeNotFound(String),
/// Edge target not found
#[error("Edge target not found: {0}")]
EdgeTargetNotFound(String),
/// No entry point defined
#[error("No entry point defined (missing edge from START)")]
NoEntryPoint,
/// Recursion limit exceeded
#[error("Recursion limit exceeded: {0} steps")]
RecursionLimitExceeded(usize),
/// Execution was interrupted
#[error("Execution interrupted: {0:?}")]
Interrupted(Box<InterruptedExecution>),
/// Node execution failed
#[error("Node '{node}' execution failed: {message}")]
NodeExecutionFailed { node: String, message: String },
/// The agent an [`AgentNode`](crate::node::AgentNode) runs failed.
///
/// Carries the agent's own error, so its category, code, retry hint, and
/// upstream status reach the caller: a rate-limited model is still
/// `RateLimited` once the graph reports it.
#[error("node '{node}' agent failed: {source}")]
AgentFailed {
/// The node whose agent failed.
node: String,
/// The error the agent returned or yielded.
#[source]
source: Box<adk_core::AdkError>,
},
/// Node timed out
#[error("Node '{node}' timed out after {elapsed:?}")]
NodeTimedOut { node: String, elapsed: Duration },
/// Fan-in node timed out waiting for upstream paths
#[error("Fan-in node '{node}' timed out: received {received}/{expected} upstream outputs")]
FanInTimedOut { node: String, received: usize, expected: usize },
/// A fan-in node's remaining predecessors can no longer run, and fewer than
/// its `min_predecessors` arrived.
#[error(
"fan-in node '{node}' needs {required} upstream outputs but received {received}, and no \
other predecessor can still run in this pass. Lower `min_predecessors`, or route every \
required branch to the node"
)]
FanInIncomplete {
/// The fan-in node.
node: String,
/// How many predecessors arrived.
received: usize,
/// The `min_predecessors` it was configured with.
required: usize,
},
/// State serialization error
#[error("State serialization error: {0}")]
SerializationError(String),
/// Checkpoint error, including a failure of the checkpointer's database.
#[error("Checkpoint error: {0}")]
CheckpointError(String),
/// A checkpoint was refused because its thread already holds a checkpoint with an
/// equal or higher version.
///
/// Raised when two writers continue one thread: the writer that falls behind is
/// refused instead of interleaving its checkpoints with the other's.
#[error(
"checkpoint version {version} for thread '{thread_id}' conflicts with stored version {head}. \
Another writer advanced the thread; reload its newest checkpoint and continue from version {head} + 1"
)]
CheckpointConflict {
/// The thread the checkpoint belongs to.
thread_id: String,
/// The version the writer tried to store.
version: u64,
/// The highest version already stored for the thread.
head: u64,
},
/// A node wrote a channel the state schema does not declare.
///
/// Only raised when channel enforcement is on. An undeclared channel
/// otherwise takes the overwrite reducer, which silently discards the
/// appends a list channel was meant to collect.
#[error(
"node '{node}' wrote undeclared channel '{channel}'. Declare it on the graph, or drop the write"
)]
UndeclaredChannel {
/// The node that produced the update.
node: String,
/// The channel name that is not declared.
channel: String,
},
/// A subgraph mapping names a channel the relevant side does not declare.
#[error("subgraph '{subgraph}' maps channel '{channel}', which the {side} does not declare")]
SubgraphChannelMismatch {
/// The subgraph node's name.
subgraph: String,
/// The channel that is not declared.
channel: String,
/// Which side is missing it, the parent or the subgraph.
side: String,
},
/// Router returned unknown target
#[error("Router returned unknown target: {0}")]
UnknownRouteTarget(String),
/// IO error
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
/// JSON error
#[error("JSON error: {0}")]
JsonError(#[from] serde_json::Error),
/// A functional-API workflow suspended at an interrupt and needs an answer.
///
/// Supply the answer under `continuation_key` with
/// [`ExecutionConfig::with_resume_value`](crate::node::ExecutionConfig::with_resume_value)
/// and invoke the entrypoint again. A `#[task]` retry policy does not retry it.
#[error("workflow suspended at interrupt '{continuation_key}': {message}")]
Suspended {
/// Key identifying the interrupt site within the thread.
continuation_key: String,
/// The message passed to the interrupt.
message: String,
},
/// A resume answer was refused before any node ran: it does not satisfy the
/// pause's `response_schema`, or, under
/// [`ExecutionConfig::strict_resume`](crate::node::ExecutionConfig::strict_resume),
/// its key is not the id of a pause pending in the thread. The checkpoint is
/// unchanged, so the run can be resumed again with a valid answer.
#[error("resume answer '{key}' refused: {reason}")]
ResumeRejected {
/// The key the answer was supplied under: an interrupt id or a node name.
key: String,
/// Why it was refused.
reason: String,
},
/// The caller cancelled a functional-API run.
///
/// Raised when the run's
/// [`ExecutionConfig::cancellation_token`](crate::node::ExecutionConfig) is
/// cancelled. A `#[task]` retry policy does not retry it.
#[error("workflow cancelled")]
Cancelled,
/// Other error (used by extensions like the functional API)
#[error("{0}")]
Other(String),
}
/// Information about an interrupted execution
#[derive(Debug, Clone)]
pub struct InterruptedExecution {
/// Thread ID for resumption
pub thread_id: String,
/// Checkpoint ID for resumption
pub checkpoint_id: String,
/// The interrupt that occurred: the first of [`Self::interrupts`].
pub interrupt: Interrupt,
/// Every pause of the step, in node-name order.
///
/// Nodes of one super-step run together, so several can pause at once, such
/// as two agent nodes each asking to confirm a tool call. A static gate is a
/// single pause.
pub interrupts: Vec<Interrupt>,
/// Current state at interruption
pub state: crate::state::State,
/// Step number when interrupted
pub step: usize,
}
impl InterruptedExecution {
/// Create a new interrupted execution with a single pause.
pub fn new(
thread_id: String,
checkpoint_id: String,
interrupt: Interrupt,
state: crate::state::State,
step: usize,
) -> Self {
let interrupts = vec![interrupt.clone()];
Self { thread_id, checkpoint_id, interrupt, interrupts, state, step }
}
/// Records every pause of the step; the first stays [`Self::interrupt`].
///
/// An empty list leaves the single pause given to [`Self::new`].
#[must_use]
pub fn with_interrupts(mut self, interrupts: Vec<Interrupt>) -> Self {
if let Some(first) = interrupts.first() {
self.interrupt = first.clone();
self.interrupts = interrupts;
}
self
}
}
/// Converts a graph error into the structured ADK error.
///
/// A [`GraphError::AgentFailed`] becomes the agent's own error, with the node
/// named in its message, so its category, code, and retry hint are unchanged.
/// Every other variant maps to an `ErrorComponent::Graph` error that carries the
/// graph error as its source.
impl From<GraphError> for adk_core::AdkError {
fn from(err: GraphError) -> Self {
use adk_core::{ErrorCategory, ErrorComponent};
if let GraphError::AgentFailed { node, source } = err {
let mut error = *source;
error.message = format!("graph node '{node}': {}", error.message);
return error;
}
let (category, code) = match &err {
GraphError::InvalidGraph(_) => (ErrorCategory::InvalidInput, "graph.invalid"),
GraphError::NodeNotFound(_) => (ErrorCategory::NotFound, "graph.node_not_found"),
GraphError::EdgeTargetNotFound(_) => {
(ErrorCategory::NotFound, "graph.edge_target_not_found")
}
GraphError::NoEntryPoint => (ErrorCategory::InvalidInput, "graph.no_entry_point"),
GraphError::RecursionLimitExceeded(_) => {
(ErrorCategory::Internal, "graph.recursion_limit")
}
GraphError::Interrupted(_) => (ErrorCategory::Cancelled, "graph.interrupted"),
GraphError::NodeExecutionFailed { .. } => {
(ErrorCategory::Internal, "graph.node_execution_failed")
}
GraphError::AgentFailed { source, .. } => (source.category, source.code),
GraphError::NodeTimedOut { .. } => (ErrorCategory::Timeout, "graph.node_timed_out"),
GraphError::FanInTimedOut { .. } => (ErrorCategory::Timeout, "graph.fan_in_timed_out"),
GraphError::FanInIncomplete { .. } => {
(ErrorCategory::Internal, "graph.fan_in_incomplete")
}
GraphError::SerializationError(_) => (ErrorCategory::Internal, "graph.serialization"),
GraphError::CheckpointError(_) => (ErrorCategory::Internal, "graph.checkpoint"),
GraphError::CheckpointConflict { .. } => {
(ErrorCategory::InvalidInput, "graph.checkpoint.conflict")
}
GraphError::SubgraphChannelMismatch { .. } => {
(ErrorCategory::InvalidInput, "graph.subgraph_channel_mismatch")
}
GraphError::UndeclaredChannel { .. } => {
(ErrorCategory::InvalidInput, "graph.undeclared_channel")
}
GraphError::UnknownRouteTarget(_) => {
(ErrorCategory::NotFound, "graph.unknown_route_target")
}
GraphError::Suspended { .. } => (ErrorCategory::Cancelled, "graph.suspended"),
GraphError::ResumeRejected { .. } => {
(ErrorCategory::InvalidInput, "graph.resume_rejected")
}
GraphError::Cancelled => (ErrorCategory::Cancelled, "graph.cancelled"),
GraphError::IoError(_) => (ErrorCategory::Internal, "graph.io"),
GraphError::JsonError(_) => (ErrorCategory::Internal, "graph.json"),
GraphError::Other(_) => (ErrorCategory::Internal, "graph.other"),
};
adk_core::AdkError::new(ErrorComponent::Graph, category, code, err.to_string())
.with_source(err)
}
}