Skip to content

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

refactor(tinyagents): delegate repeated-failure tracking to harness ladder

Steven Enamakela3e428be1077
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 dfb4cd9414..e1f63428b7 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
@@ -2,7 +2,7 @@
//! keep failing with no progress, driving the crate no-progress ladder plus
//! OpenHuman's recoverable-failure headroom and terminal-inference fast-halt.
-use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
+use std::sync::atomic::{AtomicUsize, Ordering};
use async_trait::async_trait;
@@ -10,11 +10,11 @@ use tinyagents_harness::context::RunContext;
use tinyagents_harness::error::Result as TaResult;
use tinyagents_harness::middleware::{
call_effect, dispatch_target, is_repeat_call_exempt, repeat_guard_marker, CallEffect, Middleware,
- StopLatch, ToolFactsLookup, ToolInvocationIdentity, RECOVERABLE_NO_PROGRESS_FAILURE_THRESHOLD,
- RECOVERABLE_REPEAT_FAILURE_THRESHOLD,
+ StopLatch, ToolFactsLookup, ToolInvocationIdentity,
};
use tinyagents_harness::no_progress::{
- ClassifiedFailure, ClassifiedFailureTracker, NoProgress, NoProgressTracker, ToolAttempt,
+ classified_step, clear_recovered, ClassifiedFailureTracker, ClassifiedStep, FailurePolicy,
+ NoProgress, NoProgressTracker, RecoverableFailureLadder, ToolAttempt, RECOVERY_CLASSES,
};
use tinyagents_harness::steering::SteeringHandle;
use tinyinference_llm::tool::ToolCall as TaToolCall;
@@ -80,11 +80,9 @@ pub(crate) struct RepeatedToolFailureMiddleware {
/// ([`RECOVERABLE_REPEAT_FAILURE_THRESHOLD`] identical /
/// [`RECOVERABLE_NO_PROGRESS_FAILURE_THRESHOLD`] consecutive) rather than the
/// crate's fixed 3/6, which is right only for deterministic failures.
- /// `tool\u{1f}args` → identical-failure count; persists across the turn.
- recoverable_sig_counts: std::sync::Mutex<std::collections::HashMap<String, u32>>,
- /// Consecutive recoverable-looking failures with no success in between. Reset
- /// on any success or non-recoverable failure (mirrors the legacy guard).
- recoverable_consecutive: AtomicU32,
+ /// Counting lives in the harness ladder; the halt wording is
+ /// [`HaltCopy`].
+ recoverable: RecoverableFailureLadder,
/// Corrective nudges for the next model request, drained by
/// [`PendingNudgeInjector`]. Never sent through steering: an injected
/// message joins the working transcript and is committed into durable
@@ -121,8 +119,7 @@ impl RepeatedToolFailureMiddleware {
step: AtomicUsize::new(0),
arg_sigs: std::sync::Mutex::new(std::collections::HashMap::new()),
target_scopes: std::sync::Mutex::new(std::collections::HashMap::new()),
- recoverable_sig_counts: std::sync::Mutex::new(std::collections::HashMap::new()),
- recoverable_consecutive: AtomicU32::new(0),
+ recoverable: RecoverableFailureLadder::default(),
pending_nudges: EphemeralInstructionQueue::new(),
call_effects: std::sync::Mutex::new(std::collections::HashMap::new()),
tool_facts: None,
@@ -150,76 +147,28 @@ impl RepeatedToolFailureMiddleware {
self.pending_nudges.drain_all()
}
- /// Clear the consecutive recoverable-failure streak. Called on any success or
- /// non-recoverable failure (the per-signature identical counts persist across
- /// the turn, matching the legacy guard). Idempotent.
- fn reset_recoverable_streak(&self) {
- self.recoverable_consecutive.store(0, Ordering::SeqCst);
+}
+
+/// The halt wording for the recoverable ladder: OpenHuman's own copy.
+struct HaltCopy;
+
+impl FailurePolicy for HaltCopy {
+ fn identical_halt_summary(&self, tool: &str, count: u32, failure_text: &str) -> String {
+ recoverable_identical_halt_summary(tool, count, failure_text)
}
- /// Record one recoverable failure and return a root-cause halt summary once
- /// its extended headroom is exhausted (identical `>=` [`RECOVERABLE_REPEAT_FAILURE_THRESHOLD`]
- /// or consecutive `>=` [`RECOVERABLE_NO_PROGRESS_FAILURE_THRESHOLD`]).
- fn record_recoverable(&self, tool: &str, arg_fp: &str, failure_text: &str) -> Option<String> {
- let key = format!("{tool}\u{1f}{arg_fp}");
- let count = self
- .recoverable_sig_counts
- .lock()
- .ok()
- .map(|mut counts| {
- let c = counts.entry(key).or_insert(0);
- *c += 1;
- *c
- })
- .unwrap_or(0);
- let consecutive = self.recoverable_consecutive.fetch_add(1, Ordering::SeqCst) + 1;
- tracing::debug!(
- tool,
- count,
- consecutive,
- "[tinyagents::mw] recoverable tool failure recorded with extended circuit-breaker headroom"
- );
- if count >= RECOVERABLE_REPEAT_FAILURE_THRESHOLD {
- return Some(recoverable_identical_halt_summary(
- tool,
- count,
- failure_text,
- ));
- }
- if consecutive >= RECOVERABLE_NO_PROGRESS_FAILURE_THRESHOLD {
- return Some(recoverable_no_progress_halt_summary(
- consecutive,
- tool,
- failure_text,
- ));
- }
- None
+ fn no_progress_halt_summary(&self, consecutive: u32, tool: &str, failure_text: &str) -> String {
+ recoverable_no_progress_halt_summary(consecutive, tool, failure_text)
}
}
-/// The first line of a failure, plus the first stderr line when the text is a
-/// command exit report -- that is where a program's own reason tends to be.
+/// The first line of a failure (see the harness `first_error_line`), scrubbed
+/// first: the line is read by the model and persisted with the session in a
+/// halt summary, and a command can print a token or a user's own words on stderr.
fn first_error_line(text: &str) -> String {
- // The line is read by the model and persisted with the session in a halt
- // summary, so it is scrubbed first: a command can print a token or a
- // user's own words on stderr.
- let scrubbed = crate::security::scrub::sanitize_text(text).value;
- let text = scrubbed.as_str();
- let first = text
- .lines()
- .find(|l| !l.trim().is_empty())
- .unwrap_or("")
- .trim();
- let stderr = text
- .split_once("[stderr]\n")
- .and_then(|(_, tail)| tail.lines().find(|l| !l.trim().is_empty()))
- .map(str::trim)
- .filter(|l| !l.is_empty() && *l != first);
- let line = match stderr {
- Some(err) => format!("{first} — {err}"),
- None => first.to_owned(),
- };
- line.chars().take(200).collect()
+ tinyagents_harness::no_progress::first_error_line(
+ &crate::security::scrub::sanitize_text(text).value,
+ )
}
/// A stable, bounded fingerprint of a tool call's arguments for the identical-
@@ -455,94 +404,55 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
self.stop.record_success();
// Only a successful observation against this operation and scope
// demonstrates that its blocker changed. Unrelated successes do not.
- for class in [
- "permission",
- "authentication",
- "site_refused",
- "policy",
- "blocked_by_policy",
- "task_failed",
- "unsupported",
- "missing_window",
- "missing_app",
- "not_found",
- "transient",
- "uncertain_side_effect",
- "validation",
- "unavailable",
- "service_refused",
- "invalid_arguments",
- ] {
- self.classified
- .clear(&ClassifiedFailure::new(class, tool_name, &scope));
- }
+ clear_recovered(&self.classified, tool_name, &scope);
} else if !is_repeat_call_exempt(tool_name) {
if let Some((class, budget)) =
recovery_policy_with_effect(tool_name, &failure_text, body_level_failure, effect)
{
- let key = ClassifiedFailure::new(class, tool_name, &scope);
- if let NoProgress::Halt(mut summary) = self.classified.record(&key, budget) {
- tracing::warn!(
- tool = tool_name,
- class,
- budget,
- "[tinyagents::mw] classified failure budget exhausted"
- );
- if class == "uncertain_side_effect" {
- summary.push_str(" The action may already have happened; reconcile its external state before any retry.");
- }
- // An identical re-issue made while the repeat guard
- // refuses the calls around it is a repeat loop.
- self.stop
- .halt_classified(summary, class, tool_name, repeated_failure, || {
- first_error_line(&failure_text)
- });
- self.tracker.reset();
- return Ok(());
- }
- if matches!(
+ match classified_step(
+ &self.classified,
class,
- "missing_window"
- | "missing_app"
- | "validation"
- | "invalid_arguments"
- | "uncertain_side_effect"
- | "unavailable"
- | "service_refused"
- | "blocked_by_policy"
- | "task_failed"
+ tool_name,
+ &scope,
+ budget,
+ &sanitized_failure(&failure_text),
) {
- let instruction = match class {
- "service_refused" => format!(
- "The `{tool_name}` tool cannot be used in this session: the service refused the request ({}). Do not call `{tool_name}` again; continue with your other tools.",
- first_error_line(&failure_text)
- ),
- "blocked_by_policy" => format!(
- "The `{tool_name}` call was blocked by policy and did not run ({}). Try one narrower, permitted alternative (a scoped path, a bounded command, a read instead of a write) or continue with other tools; do not resend it unchanged. Another refusal of this operation ends the turn.",
- first_error_line(&failure_text)
- ),
- "task_failed" => format!(
- "The `{tool_name}` task ended without finishing ({}). Follow its hint once: change the goal, inputs or starting point it names, or continue with other tools. Do not rerun it unchanged.",
- first_error_line(&failure_text)
- ),
- "validation" => "The last call failed validation. Correct its schema or arguments once before trying again.".to_owned(),
- "invalid_arguments" => format!(
- "The `{tool_name}` call was rejected before it ran: its arguments did not match the tool's schema ({}). Read the tool's parameters and correct the call; do not resend it unchanged.",
- first_error_line(&failure_text)
- ),
- "uncertain_side_effect" => "The last command timed out and was killed; it may have partly run. Check its effect before repeating anything, then retry at most once as a smaller, bounded step (fewer items per call, a per-item timeout such as `timeout 5`, or background it and poll).".to_owned(),
- "unavailable" => format!("The `{tool_name}` tool is unavailable for the rest of this run: a module it needs failed to load and will not recover until the app restarts. Do not call `{tool_name}` again; continue with your other tools."),
- "missing_app" if tool_name == "shell" => {
+ ClassifiedStep::Halt(summary) => {
+ tracing::warn!(
+ tool = tool_name,
+ class,
+ budget,
+ "[tinyagents::mw] classified failure budget exhausted"
+ );
+ // An identical re-issue made while the repeat guard
+ // refuses the calls around it is a repeat loop.
+ self.stop.halt_classified(
+ summary,
+ class,
+ tool_name,
+ repeated_failure,
+ || first_error_line(&failure_text),
+ );
+ self.tracker.reset();
+ return Ok(());
+ }
+ ClassifiedStep::Nudge(generic) => {
+ // The harness wording for `missing_app` is the
+ // desktop-target one; a shell command names this
+ // host's OS and shell instead.
+ let instruction = if class == "missing_app" && tool_name == "shell" {
missing_program_nudge(tool_name, &first_error_line(&failure_text))
- }
- _ => "The desktop target was not found. Rediscover the current app and window once before trying again.".to_owned(),
- };
- tracing::debug!(
- tool = tool_name,
- class,
- "[tinyagents::mw] classified failure within budget — nudging recovery"
- );
- self.pending_nudges.push(ctx, instruction);
+ } else {
+ generic
+ };
+ tracing::debug!(
+ tool = tool_name,
+ class,
+ "[tinyagents::mw] classified failure within budget — nudging recovery"
+ );
+ self.pending_nudges.push(ctx, instruction);
+ }
+ ClassifiedStep::Within => {}
}
// The classified budget owns this known blocker. In particular,
// a different query must not reset its count or trigger a
@@ -575,7 +485,7 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
&failure_text,
));
self.tracker.reset();
- self.reset_recoverable_streak();
+ self.recoverable.reset_streak();
return Ok(());
}
}
@@ -626,13 +536,13 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
if is_repeat_call_exempt(tool_name) {
return Ok(());
}
- if let Some(summary) = self.record_recoverable(tool_name, &arg_fp, &failure_text) {
+ if let Some(summary) = self.recoverable.record(&HaltCopy, tool_name, &arg_fp, &failure_text) {
tracing::warn!(
tool = tool_name,
"[tinyagents::mw] recoverable-failure headroom exhausted — halting run so the root cause surfaces"
);
self.stop.halt(summary);
- self.reset_recoverable_streak();
+ self.recoverable.reset_streak();
}
// Recoverable failures never feed the crate tracker — its fixed 3/6
// backstop would halt them before the extended headroom is spent.
@@ -641,7 +551,7 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
// Success or non-recoverable failure: clear the recoverable streak (its
// per-signature counts persist across the turn) before the crate tracker
// handles the deterministic 3/6 + hard-reject-2 path below.
- self.reset_recoverable_streak();
+ self.recoverable.reset_streak();
// Union the body-level `ok:false` signal with the existing `error.is_some()`
// predicate so the crate tracker (which reads `attempt.error` as its sole