Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(tinyagents): extract failure policy middleware

Steven Enamakelc8935d673c9f
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/failure_policy.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/failure_policy.rs
index 011e169a2b..87953e7b32 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/failure_policy.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/failure_policy.rs
@@ -2,17 +2,38 @@
//! ([`recovery_policy`]), split out of `repeated_failure.rs` so the breaker
//! stays readable: the ladder driver lives there, the classification here.
+use super::call_effect::{call_effect, CallEffect};
use super::fetched_site::fetched_site_policy;
use tinyinference_llm::failure::is_recoverable_failure_text as is_recoverable_tool_failure;
/// Explicit recovery policy. Only recognised failures enter the classified
/// ledger; unknown prose continues through the established exact-repeat guard.
+///
+/// Judges the call's side effect from `tool`'s name alone; the breaker, which
+/// sees the call's arguments and the registered tools, uses
+/// [`recovery_policy_with_effect`].
pub(super) fn recovery_policy(
tool: &str,
error: &str,
body_level_failure: bool,
) -> Option<(&'static str, usize)> {
- let (class, budget) = classified_recovery_policy(tool, error, body_level_failure)?;
+ recovery_policy_with_effect(tool, error, body_level_failure, CallEffect::Unknown)
+}
+
+/// [`recovery_policy`] for a call whose side effect is already known
+/// ([`super::call_effect::call_effect`]). `CallEffect::Unknown` falls back to
+/// reading `tool`'s name.
+pub(super) fn recovery_policy_with_effect(
+ tool: &str,
+ error: &str,
+ body_level_failure: bool,
+ effect: CallEffect,
+) -> Option<(&'static str, usize)> {
+ let effect = match effect {
+ CallEffect::Unknown => call_effect(None, tool, &serde_json::Value::Null),
+ known => known,
+ };
+ let (class, budget) = classified_recovery_policy(tool, error, body_level_failure, effect)?;
// A connector, the hosted backend or the memory store refusing the
// request (401/403, a missing or invalid key) says that service is not
// available in this session. That is a tool to stop using, not a reason
@@ -84,10 +105,67 @@ pub(super) fn is_optional_service(tool: &str) -> bool {
)
}
+/// Tools whose timeout has always been transient, kept as a floor under the
+/// side-effect reading: a `web_fetch` declaring network access must not become
+/// an uncertain action because its policy does not say `read_only`.
+const ALWAYS_RETRYABLE_ON_TIMEOUT: &[&str] = &[
+ "web_search",
+ "web_fetch",
+ "file_read",
+ "list_files",
+ "desktop_list_windows",
+];
+
+/// Whether `error` reports a failure from before the tool did anything: its
+/// runtime could not be resolved or built, or its process never spawned.
+/// Nothing ran, so there is nothing to reconcile, whatever the tool does.
+/// Conservative on purpose: only the tools' own pre-execution wording.
+pub(super) fn is_pre_execution_failure(error: &str) -> bool {
+ let lower = error.to_ascii_lowercase();
+ [
+ "runtime unavailable",
+ "runtime resolution",
+ "failed to build runtime command",
+ "failed to spawn",
+ ]
+ .iter()
+ .any(|marker| lower.contains(marker))
+}
+
+/// Whether `error` is the tool rejecting its arguments before running.
+fn is_argument_rejection(error: &str) -> bool {
+ let lower = error.to_ascii_lowercase();
+ lower.contains("schema validation") || lower.contains("invalid arguments")
+}
+
+/// The shell agent commands run under on this host, for a nudge that has to
+/// say which commands exist.
+fn host_shell_description() -> String {
+ let shell = if cfg!(windows) {
+ "cmd.exe"
+ } else {
+ crate::agent::platform_shell::bash_path().unwrap_or("sh")
+ };
+ format!("{} (`{shell}`)", std::env::consts::OS)
+}
+
+/// The corrective nudge for a `shell` command that called a program this host
+/// does not have, naming the host OS and shell so the model can switch to a
+/// command that exists there (a POSIX tool on Windows is the usual case).
+pub(super) fn missing_program_nudge(tool: &str, detail: &str) -> String {
+ format!(
+ "The `{tool}` command failed because a program it calls is not available on this host \
+ ({detail}). Commands run on {}. Retry once with a command that exists there, or check \
+ first (`where` on Windows, `command -v` elsewhere); do not resend it unchanged.",
+ host_shell_description()
+ )
+}
+
pub(super) fn classified_recovery_policy(
tool: &str,
error: &str,
body_level_failure: bool,
+ effect: CallEffect,
) -> Option<(&'static str, usize)> {
use crate::tools::status::ToolFailureClass as Class;
if body_level_failure {
@@ -159,6 +237,10 @@ pub(super) fn classified_recovery_policy(
Class::MissingPermission => ("permission", 0),
Class::BadCredentials => ("authentication", 0),
Class::BlockedByPolicy | Class::Denied | Class::ApprovalExpired => ("policy", 0),
+ // A shell command calling a program this host lacks (often a POSIX
+ // tool on Windows) is a call the model can correct: one retry, with a
+ // nudge naming the host OS and shell. Other tools keep zero.
+ Class::MissingApp if tool == "shell" => ("missing_app", 1),
Class::Unsupported | Class::MissingApp => ("unsupported", 0),
Class::NotFound
if tool.contains("desktop") && error.to_ascii_lowercase().contains("window") =>
@@ -167,11 +249,20 @@ pub(super) fn classified_recovery_policy(
}
Class::NotFound => ("not_found", 1),
Class::ServiceUnavailable | Class::ModelConnection => ("transient", 2),
+ // A schema rejection that merely names a `timeout_secs` field reads as
+ // a timeout to the keyword classifier; it is a wrong call.
+ Class::Timeout if is_argument_rejection(error) => {
+ ("invalid_arguments", ARGUMENT_SCHEMA_RECOVERY)
+ }
+ // A timeout is uncertain only when the call could have changed
+ // something. One that happened before the tool ran (runtime
+ // resolution, spawn), or on a read (`use_skill` → `composio_list_tools`
+ // halted turns on its first timeout), is retried like any transient
+ // failure.
Class::Timeout
- if matches!(
- tool,
- "web_search" | "web_fetch" | "file_read" | "list_files" | "desktop_list_windows"
- ) =>
+ if ALWAYS_RETRYABLE_ON_TIMEOUT.contains(&tool)
+ || is_pre_execution_failure(error)
+ || effect == CallEffect::ReadOnly =>
{
("transient", 2)
}