Skip to main content

agent_base/engine/
recovery.rs

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