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
//! Error types for REGULA framework.
//!
//! This module defines the comprehensive error enum used throughout the framework,
//! covering graph construction, execution, state management, checkpointing, and LLM errors.
use std::time::Duration;
use thiserror::Error;
/// The main error type for REGULA operations.
///
/// This enum covers all possible error conditions that can occur during
/// graph construction, compilation, and execution.
#[derive(Error, Debug)]
pub enum RegulaError {
// =========================================================================
// Graph Construction Errors
// =========================================================================
/// A node with this name already exists in the graph.
#[error("Node '{0}' already exists in graph")]
DuplicateNode(String),
/// The specified node was not found in the graph.
#[error("Node '{0}' not found in graph")]
NodeNotFound(String),
/// An edge references a non-existent source node.
#[error("Invalid edge: source node '{0}' does not exist")]
InvalidEdgeSource(String),
/// An edge references a non-existent target node.
#[error("Invalid edge: target node '{0}' does not exist")]
InvalidEdgeTarget(String),
/// The graph has no entry point (no edge from START).
#[error("Graph has no entry point (no edge from START)")]
NoEntryPoint,
/// The graph contains a cycle without a valid exit condition.
#[error("Graph contains a cycle without exit condition: {0}")]
InvalidCycle(String),
/// A node has no incoming edges and is unreachable.
#[error("Unreachable node: '{0}' has no incoming edges")]
UnreachableNode(String),
/// Multiple edges from the same source without conditional routing.
#[error("Ambiguous routing: node '{0}' has multiple outgoing edges without conditional routing")]
AmbiguousRouting(String),
/// A conditional edge has no valid targets defined.
#[error("Conditional edge from '{0}' has no valid targets")]
NoConditionalTargets(String),
// =========================================================================
// Execution Errors
// =========================================================================
/// A node's execution failed.
#[error("Node '{node}' execution failed: {message}")]
NodeExecution {
/// The name of the node that failed.
node: String,
/// The error message.
message: String,
/// The underlying error, if any.
#[source]
source: Option<Box<dyn std::error::Error + Send + Sync>>,
},
/// A router returned an invalid or unknown target node.
#[error("Router for node '{0}' returned invalid target: '{1}'")]
InvalidRouteTarget(String, String),
/// The graph exceeded the maximum number of iterations.
#[error("Maximum iterations ({0}) exceeded - possible infinite loop")]
MaxIterationsExceeded(usize),
/// Graph execution was interrupted by a breakpoint or interrupt() call.
#[error("Graph execution was interrupted at node '{0}'")]
Interrupted(String),
/// Graph execution was cancelled.
#[error("Graph execution was cancelled")]
Cancelled,
/// The graph has already completed and cannot continue.
#[error("Graph has already completed execution")]
AlreadyCompleted,
/// No pending tasks to execute.
#[error("No pending tasks to execute")]
NoPendingTasks,
// =========================================================================
// State / Channel Errors
// =========================================================================
/// Attempted to read from a channel that has no value.
#[error("Channel '{0}' is empty (no value set)")]
EmptyChannel(String),
/// Type mismatch when reading or writing a channel.
#[error("Channel '{channel}' type mismatch: expected {expected}, got {actual}")]
ChannelTypeMismatch {
/// The channel name.
channel: String,
/// The expected type.
expected: String,
/// The actual type encountered.
actual: String,
},
/// A reducer function failed to combine values.
#[error("Failed to apply reducer on channel '{channel}': {message}")]
ReducerFailed {
/// The channel name.
channel: String,
/// The error message.
message: String,
},
/// State serialization or deserialization failed.
#[error("State serialization failed: {0}")]
StateSerialization(#[from] serde_json::Error),
/// A required state field is missing.
#[error("Required state field '{0}' is missing")]
MissingStateField(String),
/// State update contained an unknown field.
#[error("Unknown field in state update: '{0}'")]
UnknownStateField(String),
// =========================================================================
// Checkpoint Errors
// =========================================================================
/// No checkpoint found for the given thread ID.
#[error("Checkpoint not found for thread '{0}'")]
CheckpointNotFound(String),
/// Checkpoint storage operation failed.
#[error("Checkpoint storage error: {0}")]
CheckpointStorage(String),
/// Failed to deserialize a checkpoint.
#[error("Checkpoint deserialization failed: {0}")]
CheckpointDeserialization(String),
/// Checkpoint version mismatch.
#[error("Checkpoint version mismatch: expected {expected}, got {actual}")]
CheckpointVersionMismatch {
/// The expected version.
expected: String,
/// The actual version.
actual: String,
},
/// No checkpointer configured but checkpoint operation requested.
#[error("No checkpointer configured")]
NoCheckpointer,
// =========================================================================
// LLM Errors
// =========================================================================
/// HTTP request to LLM API failed.
#[error("LLM API request failed: {0}")]
LlmRequest(String),
/// LLM API returned an error response.
#[error("LLM API error (status {status}): {message}")]
LlmApiError {
/// HTTP status code.
status: u16,
/// Error message from the API.
message: String,
},
/// Failed to parse LLM response.
#[error("LLM response parsing failed: {0}")]
LlmResponseParse(String),
/// LLM API rate limit exceeded.
#[error("LLM rate limited, retry after {retry_after:?}")]
LlmRateLimited {
/// Suggested retry delay, if provided.
retry_after: Option<Duration>,
},
/// Error during LLM streaming.
#[error("LLM streaming error: {0}")]
LlmStreaming(String),
/// LLM API key is missing or invalid.
#[error("LLM API key is missing or invalid")]
LlmApiKeyMissing,
/// LLM request timed out.
#[error("LLM request timed out after {0:?}")]
LlmTimeout(Duration),
// =========================================================================
// Configuration Errors
// =========================================================================
/// Invalid configuration value.
#[error("Invalid configuration: {0}")]
InvalidConfig(String),
/// Required configuration is missing.
#[error("Missing required configuration: {0}")]
MissingConfig(String),
/// Configuration key not found in context.
#[error("Configuration key '{0}' not found in context")]
ConfigKeyNotFound(String),
/// Configuration value type mismatch.
#[error("Configuration key '{key}' type mismatch: expected {expected}")]
ConfigTypeMismatch {
/// The configuration key.
key: String,
/// The expected type.
expected: String,
},
// =========================================================================
// Generic Errors
// =========================================================================
/// Internal framework error.
#[error("Internal error: {0}")]
Internal(String),
/// Lock acquisition failed (concurrency error).
#[error("Failed to acquire lock: {0}")]
LockError(String),
/// Generic I/O error.
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
/// Wrapper for other error types.
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl RegulaError {
/// Create a node execution error with a source error.
pub fn node_execution(
node: impl Into<String>,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
let message = source.to_string();
Self::NodeExecution {
node: node.into(),
message,
source: Some(Box::new(source)),
}
}
/// Create a node execution error with just a message.
pub fn node_execution_msg(node: impl Into<String>, message: impl Into<String>) -> Self {
Self::NodeExecution {
node: node.into(),
message: message.into(),
source: None,
}
}
/// Check if this error is retryable.
pub fn is_retryable(&self) -> bool {
matches!(
self,
Self::LlmRateLimited { .. }
| Self::LlmTimeout(_)
| Self::LlmRequest(_)
| Self::LockError(_)
)
}
/// Check if this error is a cancellation.
pub fn is_cancelled(&self) -> bool {
matches!(self, Self::Cancelled | Self::Interrupted(_))
}
}
/// Result type alias for REGULA operations.
pub type Result<T> = std::result::Result<T, RegulaError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = RegulaError::DuplicateNode("agent".to_string());
assert_eq!(err.to_string(), "Node 'agent' already exists in graph");
}
#[test]
fn test_node_execution_error() {
let err = RegulaError::node_execution_msg("agent", "connection refused");
assert!(err.to_string().contains("agent"));
assert!(err.to_string().contains("connection refused"));
}
#[test]
fn test_is_retryable() {
assert!(RegulaError::LlmRateLimited { retry_after: None }.is_retryable());
assert!(RegulaError::LlmTimeout(Duration::from_secs(30)).is_retryable());
assert!(!RegulaError::Cancelled.is_retryable());
}
}