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
//! Agent error types.
use core::fmt;
/// Errors that can occur during agent execution.
#[derive(Debug, Clone)]
pub enum AgentError {
/// LLM returned an error.
Llm(String),
/// Tool execution failed.
ToolExecution {
/// Name of the tool that failed.
name: String,
/// The underlying error message.
error: String,
},
/// Maximum iterations exceeded without completing the task.
MaxIterations {
/// The iteration limit that was exceeded.
limit: usize,
},
/// A hook rejected the operation.
HookRejected {
/// Name of the hook that rejected.
hook: &'static str,
/// Reason for rejection.
reason: String,
},
/// Tool not found.
ToolNotFound {
/// Name of the missing tool.
name: String,
},
/// Configuration error.
Config(String),
}
impl fmt::Display for AgentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Llm(e) => write!(f, "LLM error: {e}"),
Self::ToolExecution { name, error } => {
write!(f, "tool '{name}' failed: {error}")
}
Self::MaxIterations { limit } => {
write!(f, "exceeded maximum iterations ({limit})")
}
Self::HookRejected { hook, reason } => {
write!(f, "hook '{hook}' rejected: {reason}")
}
Self::ToolNotFound { name } => {
write!(f, "tool '{name}' not found")
}
Self::Config(msg) => write!(f, "configuration error: {msg}"),
}
}
}
impl std::error::Error for AgentError {}
impl AgentError {
/// Returns `true` if this is a provider-level failure worth retrying
/// (e.g. with a different provider in a multi-provider setup).
///
/// Only `Llm` errors are considered retryable — they indicate the
/// LLM provider itself failed (rate limit, timeout, auth, server error, etc.).
/// Other variants (`MaxIterations`, `HookRejected`, `ToolNotFound`, etc.)
/// are not provider-related and retrying would not help.
#[must_use]
pub const fn is_retryable(&self) -> bool {
matches!(self, Self::Llm(_))
}
}
impl From<anyhow::Error> for AgentError {
fn from(error: anyhow::Error) -> Self {
Self::Llm(error.to_string())
}
}
impl From<String> for AgentError {
fn from(error: String) -> Self {
Self::Llm(error)
}
}