Skip to main content

agent_base/engine/
recovery.rs

1use crate::types::{AgentError, AgentResult, SessionId};
2
3/// Action taken by the runtime after a tool execution failure
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub enum ToolErrorAction {
6    /// Stop the current run with a failed outcome
7    Stop,
8    /// Feed the error back to the LLM and continue reasoning
9    Retry,
10}
11
12/// Recovery strategy after a tool execution failure
13///
14/// Defaults to [`StopOnError`], following the lightweight kernel design of
15/// conservative defaults and strategy injection.
16/// Upper-layer agents can inject custom strategies such as [`RetryOnError`].
17pub trait ToolErrorRecovery: Send + Sync {
18    fn on_error(
19        &self,
20        _session_id: &SessionId,
21        _tool_names: &[String],
22        _error: &AgentError,
23    ) -> AgentResult<ToolErrorAction>;
24}
25
26/// Default strategy: stop on tool failure.
27///
28/// This is the most conservative strategy. The kernel only reports the fact
29/// without making business recovery decisions for the upper layer.
30pub struct StopOnError;
31
32impl ToolErrorRecovery for StopOnError {
33    fn on_error(
34        &self,
35        _session_id: &SessionId,
36        _tool_names: &[String],
37        _error: &AgentError,
38    ) -> AgentResult<ToolErrorAction> {
39        Ok(ToolErrorAction::Stop)
40    }
41}
42
43/// Continue on tool failure, feeding the error back to the model
44///
45/// Suitable for scenarios where model self-healing is desired (e.g. code-agent, browser-agent).
46pub struct RetryOnError;
47
48impl ToolErrorRecovery for RetryOnError {
49    fn on_error(
50        &self,
51        _session_id: &SessionId,
52        _tool_names: &[String],
53        _error: &AgentError,
54    ) -> AgentResult<ToolErrorAction> {
55        Ok(ToolErrorAction::Retry)
56    }
57}