Skip to content

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

refactor(inference): move halt copy into the loop-guard middleware

Steven Enamakel493cf8635691
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
index 81437377b5..c16b596725 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
@@ -49,7 +49,7 @@ impl CopyProvider for OpenHumanHaltCopy {
}
}
-fn user_actionable_escalation(tool: &str, error: &str) -> Option<String> {
+pub(crate) fn user_actionable_escalation(tool: &str, error: &str) -> Option<String> {
let lower = error.to_lowercase();
let permission_or_scope_failure = lower.contains("[composio:error:insufficient_scope]")
|| lower.contains("[composio:error:trigger_permission]")
@@ -78,11 +78,11 @@ fn user_actionable_escalation(tool: &str, error: &str) -> Option<String> {
})
}
-fn terminal_inference_failure_kind(result: &str) -> Option<TerminalInferenceFailureKind> {
+pub(crate) fn terminal_inference_failure_kind(result: &str) -> Option<TerminalInferenceFailureKind> {
tinyagents_hooks::stop::terminal_inference_failure_kind(result)
}
-fn terminal_inference_halt_summary(kind: TerminalInferenceFailureKind, tool: &str, error: &str) -> String {
+pub(crate) fn terminal_inference_halt_summary(kind: TerminalInferenceFailureKind, tool: &str, error: &str) -> String {
match kind {
TerminalInferenceFailureKind::BudgetExhausted => {
stop::halt_summary::budget(&OpenHumanHaltCopy, tool, error).summary
@@ -93,11 +93,11 @@ fn terminal_inference_halt_summary(kind: TerminalInferenceFailureKind, tool: &st
}
}
-fn recoverable_identical_halt_summary(tool: &str, count: u32, error: &str) -> String {
+pub(crate) fn recoverable_identical_halt_summary(tool: &str, count: u32, error: &str) -> String {
stop::halt_summary::retry_exhausted(&OpenHumanHaltCopy, tool, count, error).summary
}
-fn recoverable_no_progress_halt_summary(consecutive: u32, tool: &str, error: &str) -> String {
+pub(crate) fn recoverable_no_progress_halt_summary(consecutive: u32, tool: &str, error: &str) -> String {
stop::halt_summary::no_progress(&OpenHumanHaltCopy, consecutive, tool, error).summary
}
diff --git a/crates/openhuman-core/src/inference/failure_copy/halt.rs b/crates/openhuman-core/src/inference/failure_copy/halt.rs
deleted file mode 100644
index 5316fecd3b..0000000000
--- a/crates/openhuman-core/src/inference/failure_copy/halt.rs
+++ /dev/null
@@ -1,176 +0,0 @@
-//! Loop-guard halt copy: the root-cause summaries a circuit breaker records
-//! when a turn is stopped for repeated tool failures, plus the recogniser for a
-//! permanent delegated-inference failure that picks between the two terminal
-//! summaries. Thresholds and the repeat-call exemption stay with the guards in
-//! `agent::tinyagents::middleware::loop_guards`.
-
-use tinyinference_providers::{
- is_budget_message, is_provider_config_rejection_message, BudgetMatch,
-};
-
-/// Clamp the last-error text embedded in a circuit-breaker halt summary so a huge
-/// tool error (already capped at 1MB upstream) can't blow up the agent's result.
-/// Mirrors the legacy `tool_loop::truncate_for_halt`.
-pub(crate) fn truncate_for_halt(s: &str) -> String {
- const MAX: usize = 600;
- if s.chars().count() <= MAX {
- return s.to_string();
- }
- let head: String = s.chars().take(MAX).collect();
- format!("{head}\n… [truncated]")
-}
-
-/// A permanent, non-retryable inference failure surfaced by a delegated
-/// sub-agent's tool result. Unlike a transient error, re-issuing the call cannot
-/// succeed even under a *different* delegation tool or varied args: the budget is
-/// account-wide and the model/provider configuration is shared by every
-/// (sub-)agent. See [`terminal_inference_failure_kind`] (#3104).
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub(crate) enum TerminalInferenceFailure {
- /// Out of inference budget / credits — every retry hits the same wall.
- BudgetExhausted,
- /// The configured model/provider rejected the request for a reason the user
- /// must fix (unknown model, non-chat/embedding model, missing credential,
- /// region block, …).
- ProviderConfig,
-}
-
-/// Inference/delegation **envelope** markers that prove a tool result came from a
-/// delegated inference call (a sub-agent / provider round-trip) rather than from
-/// arbitrary tool stderr. Every marker here is harness-generated (our own
-/// reliable-chain rollup or sub-agent dispatch wrapper), NOT a provider HTTP body
-/// that arbitrary tool stderr could forge. Ported from legacy `tool_loop`.
-const INFERENCE_FAILURE_ENVELOPE_MARKERS: &[&str] = &[
- // Reliable-chain exhaustion rollup (reliable.rs::format_failure_aggregate).
- "all providers/models failed",
- "may not be available on your provider",
- // Sub-agent delegation failure wrapper (dispatch.rs::format_subagent_failure).
- "failed and did not complete",
-];
-
-/// True if `result` carries one of the inference/delegation envelope markers —
-/// i.e. the failure demonstrably came from a delegated provider round-trip, not
-/// arbitrary tool stderr. See [`INFERENCE_FAILURE_ENVELOPE_MARKERS`].
-fn has_inference_failure_envelope(result: &str) -> bool {
- let lower = result.to_ascii_lowercase();
- INFERENCE_FAILURE_ENVELOPE_MARKERS
- .iter()
- .any(|marker| lower.contains(marker))
-}
-
-/// Recognize a permanent (non-retryable) delegated-inference failure from a tool
-/// result. Two-stage gate so a *recoverable* tool failure can't be misclassified:
-/// (1) the result must carry a delegated-inference envelope
-/// ([`has_inference_failure_envelope`]); (2) the trusted body is matched against
-/// the two tight provider classifiers. Budget takes precedence if both match.
-/// Ported from legacy `tool_loop::terminal_inference_failure_kind` (#3104).
-pub(crate) fn terminal_inference_failure_kind(result: &str) -> Option<TerminalInferenceFailure> {
- if !has_inference_failure_envelope(result) {
- return None;
- }
- if is_budget_message(result, BudgetMatch::Billing) {
- Some(TerminalInferenceFailure::BudgetExhausted)
- } else if is_provider_config_rejection_message(result) {
- Some(TerminalInferenceFailure::ProviderConfig)
- } else {
- None
- }
-}
-
-/// The actionable root-cause halt summary for a terminal delegated-inference
-/// failure. Ported verbatim from the legacy loop.
-pub(crate) fn terminal_inference_halt_summary(
- kind: TerminalInferenceFailure,
- tool: &str,
- result: &str,
-) -> String {
- match kind {
- TerminalInferenceFailure::BudgetExhausted => format!(
- "Stopping: the `{tool}` step failed because the account is out of inference \
- budget/credits — every retry hits the same wall. Add credits to your account \
- (or, when using a custom/BYO provider, top up that provider's own account) and try \
- again. Details:\n{}",
- truncate_for_halt(result),
- ),
- TerminalInferenceFailure::ProviderConfig => format!(
- "Stopping: the `{tool}` step failed because the configured model/provider rejected the \
- request (e.g. an unknown model, a non-chat/embedding model, a missing credential, or \
- a region block) — retrying will not help. Fix the model or API key in Connections → API keys → LLM. \
- Details:\n{}",
- truncate_for_halt(result),
- ),
- }
-}
-
-/// Halt summary when a single recoverable `(tool, args)` call exhausts its
-/// extended identical-retry headroom. Ported from the legacy loop.
-pub(crate) fn recoverable_identical_halt_summary(tool: &str, count: u32, result: &str) -> String {
- format!(
- "Stopping: the `{tool}` call was retried {count} times with identical arguments and kept \
- failing — repeating it will not help. Last error:\n{}\n\nThis looked recoverable at \
- first, but the same call exhausted the extended transient-failure headroom. Report this \
- back instead of retrying.",
- truncate_for_halt(result),
- )
-}
-
-/// Halt summary when many recoverable-looking failures pile up with no progress.
-/// Ported from the legacy loop.
-pub(crate) fn recoverable_no_progress_halt_summary(
- consecutive: u32,
- tool: &str,
- result: &str,
-) -> String {
- format!(
- "Stopping: {consecutive} recoverable-looking tool failures happened in a row with no \
- successful progress. Last error (from `{tool}`):\n{}\n\nThe turn is still bounded by the \
- iteration/cost limits, but this many consecutive transient failures means the goal is not \
- currently reachable. Report this back instead of retrying.",
- truncate_for_halt(result),
- )
-}
-
-/// Recognise a **user-actionable** blocker in a failing tool result — one only
-/// the user can clear — and phrase the halt as a direct ask instead of the
-/// crate's generic "the goal looks unreachable in this environment, report this
-/// back" summary (issue #4092). Today that's a missing service connection (the
-/// issue's canonical example: acting on a service that isn't connected). Such a
-/// failure will never self-resolve by retrying, and the fix is the user's, so
-/// escalate with a concrete next step instead of looping or reporting a generic
-/// dead-end. Returns `None` for failures that are not user-actionable, leaving
-/// the crate's summary in place.
-pub(crate) fn user_actionable_escalation(tool: &str, error: &str) -> Option<String> {
- let lower = error.to_lowercase();
- let permission_or_scope_failure = lower.contains("[composio:error:insufficient_scope]")
- || lower.contains("[composio:error:trigger_permission]")
- || lower.contains("insufficient scope")
- || lower.contains("insufficient authentication scopes")
- || lower.contains("insufficient permissions")
- || lower.contains("missing required permissions")
- || lower.contains("permission to manage triggers");
- if permission_or_scope_failure {
- return None;
- }
- // Keep this narrow: some scope/permission failures legitimately tell the
- // user to reconnect in Connections, but they are not missing connections.
- let missing_connection = lower.contains("[composio:error:composio_platform]")
- || lower.contains("not connected")
- || lower.contains("isn't connected")
- || lower.contains("is not connected")
- || lower.contains("not enabled")
- || lower.contains("token revoked")
- || lower.contains("connection error, try to authenticate");
- if !missing_connection {
- return None;
- }
- Some(format!(
- "I can't continue without your input: the `{tool}` action needs a service that isn't \
- connected. {}\n\nConnect it (Connections), then tell me to retry — or \
- tell me how you'd like to proceed instead.",
- crate::util::truncate_with_ellipsis(error, 400),
- ))
-}
-
-#[cfg(test)]
-#[path = "halt_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/inference/failure_copy/halt_tests.rs b/crates/openhuman-core/src/inference/failure_copy/halt_tests.rs
deleted file mode 100644
index ba90a82cdc..0000000000
--- a/crates/openhuman-core/src/inference/failure_copy/halt_tests.rs
+++ /dev/null
@@ -1,70 +0,0 @@
-use super::*;
-
-#[test]
-fn identical_halt_summary_text_is_pinned() {
- assert_eq!(
- recoverable_identical_halt_summary("web_fetch", 5, "503 upstream"),
- "Stopping: the `web_fetch` call was retried 5 times with identical arguments and kept \
- failing — repeating it will not help. Last error:\n503 upstream\n\nThis looked \
- recoverable at first, but the same call exhausted the extended transient-failure \
- headroom. Report this back instead of retrying."
- );
-}
-
-#[test]
-fn no_progress_halt_summary_text_is_pinned() {
- assert_eq!(
- recoverable_no_progress_halt_summary(4, "shell", "boom"),
- "Stopping: 4 recoverable-looking tool failures happened in a row with no successful \
- progress. Last error (from `shell`):\nboom\n\nThe turn is still bounded by the \
- iteration/cost limits, but this many consecutive transient failures means the goal is \
- not currently reachable. Report this back instead of retrying."
- );
-}
-
-#[test]
-fn terminal_budget_summary_text_is_pinned() {
- assert_eq!(
- terminal_inference_halt_summary(TerminalInferenceFailure::BudgetExhausted, "spawn", "x"),
- "Stopping: the `spawn` step failed because the account is out of inference \
- budget/credits — every retry hits the same wall. Add credits to your account (or, \
- when using a custom/BYO provider, top up that provider's own account) and try again. \
- Details:\nx"
- );
-}
-
-#[test]
-fn terminal_provider_config_summary_text_is_pinned() {
- assert_eq!(
- terminal_inference_halt_summary(TerminalInferenceFailure::ProviderConfig, "spawn", "x"),
- "Stopping: the `spawn` step failed because the configured model/provider rejected the \
- request (e.g. an unknown model, a non-chat/embedding model, a missing credential, or a \
- region block) — retrying will not help. Fix the model or API key in Connections → API \
- keys → LLM. Details:\nx"
- );
-}
-
-#[test]
-fn halt_text_clamps_the_embedded_error_at_600_chars() {
- let long = "e".repeat(700);
- let out = truncate_for_halt(&long);
- assert_eq!(out, format!("{}\n… [truncated]", "e".repeat(600)));
- assert_eq!(truncate_for_halt(&"e".repeat(600)), "e".repeat(600));
-}
-
-#[test]
-fn user_actionable_escalation_text_is_pinned() {
- assert_eq!(
- user_actionable_escalation("gmail_send", "Gmail is not connected").as_deref(),
- Some(
- "I can't continue without your input: the `gmail_send` action needs a service that \
- isn't connected. Gmail is not connected\n\nConnect it (Connections), then tell me \
- to retry — or tell me how you'd like to proceed instead."
- )
- );
- assert_eq!(
- user_actionable_escalation("gmail_send", "insufficient scope; not connected"),
- None
- );
- assert_eq!(user_actionable_escalation("shell", "exit 1"), None);
-}
diff --git a/crates/openhuman-core/src/inference/failure_copy/mod.rs b/crates/openhuman-core/src/inference/failure_copy/mod.rs
index afef20f8f7..ab914ceafe 100644
--- a/crates/openhuman-core/src/inference/failure_copy/mod.rs
+++ b/crates/openhuman-core/src/inference/failure_copy/mod.rs
@@ -3,21 +3,8 @@
//! - [`table`]: failure class -> (`error_type` wire token, `source`,
//! `retryable`, user copy), consumed by `web_chat::web_errors` to build the
//! `chat_error` envelope.
-//! - [`halt`]: the loop-guard halt summaries and the delegated-inference
-//! recogniser that picks between them.
-//!
-//! Provider-text matching (including the one budget-phrase matcher,
-//! `tinyinference_providers::is_budget_message`) lives upstream in
-//! `tinyinference`; deciding *which class an error is* stays in the callers.
+//! Provider-neutral and host-specific error copy used by the chat error surface.
-mod halt;
-mod stop_summary;
mod table;
-pub(crate) use halt::TerminalInferenceFailure;
-pub(crate) use halt::{
- recoverable_identical_halt_summary, recoverable_no_progress_halt_summary,
- terminal_inference_failure_kind, terminal_inference_halt_summary, user_actionable_escalation,
-};
-pub(crate) use stop_summary::stop_summary_copy;
pub(crate) use table::{failure_copy, FailureClass};
diff --git a/crates/openhuman-core/src/inference/failure_copy/stop_summary.rs b/crates/openhuman-core/src/inference/failure_copy/stop_summary.rs
deleted file mode 100644
index 3d1a228cce..0000000000
--- a/crates/openhuman-core/src/inference/failure_copy/stop_summary.rs
+++ /dev/null
@@ -1,37 +0,0 @@
-//! Stable English fallback copy for terminal turn-stop reasons.
-
-/// The user-facing text for one `turn_stop.*` localization key.
-pub(crate) fn stop_summary_copy(key: &str) -> &'static str {
- match key {
- "turn_stop.transient" => {
- "a service I needed kept timing out or was temporarily unavailable"
- }
- "turn_stop.uncertain_side_effect" => {
- "an action timed out and I could not confirm whether it went through, so I did not repeat it"
- }
- "turn_stop.authentication" => "a service rejected my credentials",
- "turn_stop.permission" => "I don't have permission for a step this needs",
- "turn_stop.policy" => "a step this needs is blocked by your security settings",
- "turn_stop.missing_program" => {
- "a program or feature this needs is not available on this device"
- }
- "turn_stop.not_found" => "something this needs could not be found",
- "turn_stop.invalid_calls" => "a tool kept rejecting my requests as invalid",
- "turn_stop.unavailable" => "a tool this needs is not available right now",
- "turn_stop.site_refused" => "a website kept refusing my requests",
- "turn_stop.repeated_failure" => "the same step kept failing",
- "turn_stop.varied_failures" => "several different tool calls in a row failed",
- "turn_stop.repeating_step" => "I kept repeating the same step without getting anything new",
- "turn_stop.out_of_credits" => {
- "the account is out of inference credits; add credits, then ask me to try again"
- }
- "turn_stop.provider_config" => {
- "the configured model or provider rejected the request; check the model and API key in Connections"
- }
- "turn_stop.missing_connection" => {
- "a service this needs isn't connected; connect it in Connections, then ask me to try again"
- }
- "turn_stop.generic" => "my tool calls were not getting anywhere",
- _ => "my tool calls were not getting anywhere",
- }
-}
diff --git a/crates/openhuman-core/src/web_chat/web_tests_error_copy_fixture_tests.rs b/crates/openhuman-core/src/web_chat/web_tests_error_copy_fixture_tests.rs
index 5608778d64..f996e37863 100644
--- a/crates/openhuman-core/src/web_chat/web_tests_error_copy_fixture_tests.rs
+++ b/crates/openhuman-core/src/web_chat/web_tests_error_copy_fixture_tests.rs
@@ -255,8 +255,8 @@ fn shared_budget_matcher_reproduces_each_legacy_matcher_on_the_corpus() {
);
let delegated = format!("All providers/models failed: {message}");
assert_eq!(
- crate::inference::failure_copy::terminal_inference_failure_kind(&delegated)
- == Some(crate::inference::failure_copy::TerminalInferenceFailure::BudgetExhausted),
+ crate::agent::tinyagents::middleware::repeated_failure::terminal_inference_failure_kind(&delegated)
+ == Some(tinyagents_hooks::stop::TerminalInferenceFailureKind::BudgetExhausted),
legacy_billing_matcher(message),
"loop-guard matcher drifted on {message:?}"
);
@@ -272,10 +272,11 @@ fn shared_budget_matcher_reproduces_each_legacy_matcher_on_the_corpus() {
#[test]
fn loop_guard_halt_summaries_are_byte_identical() {
- use crate::inference::failure_copy::{
+ use crate::agent::tinyagents::middleware::repeated_failure::{
recoverable_identical_halt_summary, recoverable_no_progress_halt_summary,
- terminal_inference_halt_summary, user_actionable_escalation, TerminalInferenceFailure,
+ terminal_inference_halt_summary, user_actionable_escalation,
};
+ use tinyagents_hooks::stop::TerminalInferenceFailureKind as TerminalInferenceFailure;
assert_eq!(
terminal_inference_halt_summary(TerminalInferenceFailure::BudgetExhausted, "delegate", "boom"),
"Stopping: the `delegate` step failed because the account is out of inference budget/credits \u{2014} every retry hits the same wall. Add credits to your account (or, when using a custom/BYO provider, top up that provider's own account) and try again. Details:\nboom"