1use std::time::Duration;
4
5use crate::interrupt::Interrupt;
6use thiserror::Error;
7
8pub type Result<T> = std::result::Result<T, GraphError>;
10
11#[derive(Error, Debug)]
13#[non_exhaustive]
14pub enum GraphError {
15 #[error("Invalid graph structure: {0}")]
17 InvalidGraph(String),
18
19 #[error("Node not found: {0}")]
21 NodeNotFound(String),
22
23 #[error("Edge target not found: {0}")]
25 EdgeTargetNotFound(String),
26
27 #[error("No entry point defined (missing edge from START)")]
29 NoEntryPoint,
30
31 #[error("Recursion limit exceeded: {0} steps")]
33 RecursionLimitExceeded(usize),
34
35 #[error("Execution interrupted: {0:?}")]
37 Interrupted(Box<InterruptedExecution>),
38
39 #[error("Node '{node}' execution failed: {message}")]
41 NodeExecutionFailed { node: String, message: String },
42
43 #[error("node '{node}' agent failed: {source}")]
49 AgentFailed {
50 node: String,
52 #[source]
54 source: Box<adk_core::AdkError>,
55 },
56
57 #[error("Node '{node}' timed out after {elapsed:?}")]
59 NodeTimedOut { node: String, elapsed: Duration },
60
61 #[error("Fan-in node '{node}' timed out: received {received}/{expected} upstream outputs")]
63 FanInTimedOut { node: String, received: usize, expected: usize },
64
65 #[error(
68 "fan-in node '{node}' needs {required} upstream outputs but received {received}, and no \
69 other predecessor can still run in this pass. Lower `min_predecessors`, or route every \
70 required branch to the node"
71 )]
72 FanInIncomplete {
73 node: String,
75 received: usize,
77 required: usize,
79 },
80
81 #[error("State serialization error: {0}")]
83 SerializationError(String),
84
85 #[error("Checkpoint error: {0}")]
87 CheckpointError(String),
88
89 #[error(
95 "checkpoint version {version} for thread '{thread_id}' conflicts with stored version {head}. \
96 Another writer advanced the thread; reload its newest checkpoint and continue from version {head} + 1"
97 )]
98 CheckpointConflict {
99 thread_id: String,
101 version: u64,
103 head: u64,
105 },
106
107 #[error(
113 "node '{node}' wrote undeclared channel '{channel}'. Declare it on the graph, or drop the write"
114 )]
115 UndeclaredChannel {
116 node: String,
118 channel: String,
120 },
121
122 #[error("subgraph '{subgraph}' maps channel '{channel}', which the {side} does not declare")]
124 SubgraphChannelMismatch {
125 subgraph: String,
127 channel: String,
129 side: String,
131 },
132
133 #[error("Router returned unknown target: {0}")]
135 UnknownRouteTarget(String),
136
137 #[error("IO error: {0}")]
139 IoError(#[from] std::io::Error),
140
141 #[error("JSON error: {0}")]
143 JsonError(#[from] serde_json::Error),
144
145 #[error("workflow suspended at interrupt '{continuation_key}': {message}")]
151 Suspended {
152 continuation_key: String,
154 message: String,
156 },
157
158 #[error("resume answer '{key}' refused: {reason}")]
164 ResumeRejected {
165 key: String,
167 reason: String,
169 },
170
171 #[error("workflow cancelled")]
177 Cancelled,
178
179 #[error("{0}")]
181 Other(String),
182}
183
184#[derive(Debug, Clone)]
186pub struct InterruptedExecution {
187 pub thread_id: String,
189 pub checkpoint_id: String,
191 pub interrupt: Interrupt,
193 pub interrupts: Vec<Interrupt>,
199 pub state: crate::state::State,
201 pub step: usize,
203}
204
205impl InterruptedExecution {
206 pub fn new(
208 thread_id: String,
209 checkpoint_id: String,
210 interrupt: Interrupt,
211 state: crate::state::State,
212 step: usize,
213 ) -> Self {
214 let interrupts = vec![interrupt.clone()];
215 Self { thread_id, checkpoint_id, interrupt, interrupts, state, step }
216 }
217
218 #[must_use]
222 pub fn with_interrupts(mut self, interrupts: Vec<Interrupt>) -> Self {
223 if let Some(first) = interrupts.first() {
224 self.interrupt = first.clone();
225 self.interrupts = interrupts;
226 }
227 self
228 }
229}
230
231impl From<GraphError> for adk_core::AdkError {
238 fn from(err: GraphError) -> Self {
239 use adk_core::{ErrorCategory, ErrorComponent};
240 if let GraphError::AgentFailed { node, source } = err {
241 let mut error = *source;
242 error.message = format!("graph node '{node}': {}", error.message);
243 return error;
244 }
245 let (category, code) = match &err {
246 GraphError::InvalidGraph(_) => (ErrorCategory::InvalidInput, "graph.invalid"),
247 GraphError::NodeNotFound(_) => (ErrorCategory::NotFound, "graph.node_not_found"),
248 GraphError::EdgeTargetNotFound(_) => {
249 (ErrorCategory::NotFound, "graph.edge_target_not_found")
250 }
251 GraphError::NoEntryPoint => (ErrorCategory::InvalidInput, "graph.no_entry_point"),
252 GraphError::RecursionLimitExceeded(_) => {
253 (ErrorCategory::Internal, "graph.recursion_limit")
254 }
255 GraphError::Interrupted(_) => (ErrorCategory::Cancelled, "graph.interrupted"),
256 GraphError::NodeExecutionFailed { .. } => {
257 (ErrorCategory::Internal, "graph.node_execution_failed")
258 }
259 GraphError::AgentFailed { source, .. } => (source.category, source.code),
260 GraphError::NodeTimedOut { .. } => (ErrorCategory::Timeout, "graph.node_timed_out"),
261 GraphError::FanInTimedOut { .. } => (ErrorCategory::Timeout, "graph.fan_in_timed_out"),
262 GraphError::FanInIncomplete { .. } => {
263 (ErrorCategory::Internal, "graph.fan_in_incomplete")
264 }
265 GraphError::SerializationError(_) => (ErrorCategory::Internal, "graph.serialization"),
266 GraphError::CheckpointError(_) => (ErrorCategory::Internal, "graph.checkpoint"),
267 GraphError::CheckpointConflict { .. } => {
268 (ErrorCategory::InvalidInput, "graph.checkpoint.conflict")
269 }
270 GraphError::SubgraphChannelMismatch { .. } => {
271 (ErrorCategory::InvalidInput, "graph.subgraph_channel_mismatch")
272 }
273 GraphError::UndeclaredChannel { .. } => {
274 (ErrorCategory::InvalidInput, "graph.undeclared_channel")
275 }
276 GraphError::UnknownRouteTarget(_) => {
277 (ErrorCategory::NotFound, "graph.unknown_route_target")
278 }
279 GraphError::Suspended { .. } => (ErrorCategory::Cancelled, "graph.suspended"),
280 GraphError::ResumeRejected { .. } => {
281 (ErrorCategory::InvalidInput, "graph.resume_rejected")
282 }
283 GraphError::Cancelled => (ErrorCategory::Cancelled, "graph.cancelled"),
284 GraphError::IoError(_) => (ErrorCategory::Internal, "graph.io"),
285 GraphError::JsonError(_) => (ErrorCategory::Internal, "graph.json"),
286 GraphError::Other(_) => (ErrorCategory::Internal, "graph.other"),
287 };
288 adk_core::AdkError::new(ErrorComponent::Graph, category, code, err.to_string())
289 .with_source(err)
290 }
291}