Skip to content

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

fix(middleware): handle repeated failure classification correctly

Steven Enamakel19d71943db7d
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 4243199771..175d59c6ff 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
@@ -197,6 +197,25 @@ fn args_fingerprint(arguments: &serde_json::Value) -> String {
format!("{:x}", hasher.finish())
}
+/// The agent tool that fetches a public URL (`tinytools` `web_fetch`).
+const WEB_FETCH_TOOL: &str = "web_fetch";
+
+/// The HTTP status of a `web_fetch` error result, read from the
+/// `HTTP <code> <reason> from <host>; <guidance>` line `tinytools` renders for
+/// a 4xx/5xx from the fetched site (tinytools#47). Anchored to the start of the
+/// text, so a status quoted later (a response excerpt, another tool's output)
+/// is not read, and to the whole shape, so a bare `HTTP 403` or `403 Forbidden`
+/// from some other source is not either.
+pub(super) fn fetched_site_status(text: &str) -> Option<u16> {
+ let rest = text.trim_start().strip_prefix("HTTP ")?;
+ let code = rest.get(..3).filter(|c| c.bytes().all(|b| b.is_ascii_digit()))?;
+ let rest = rest[3..].strip_prefix(' ')?;
+ let status: u16 = code.parse().ok().filter(|s| (400..=599).contains(s))?;
+ let (_reason, after_from) = rest.lines().next()?.split_once(" from ")?;
+ let (host, _guidance) = after_from.split_once(';')?;
+ (!host.is_empty() && !host.contains(char::is_whitespace)).then_some(status)
+}
+
/// Stable resource identity supplied by the call, excluding free-form queries,
/// prompts, credentials, and URL query parameters. An absent target remains
/// scoped to the operation, never to the changing argument fingerprint.
@@ -216,6 +235,19 @@ pub(super) fn failure_scope(tool: &str, arguments: &serde_json::Value) -> String
Some(serde_json::Value::Number(value)) => value.to_string(),
_ => continue,
};
+ // A site decides whether to serve us per host, not per page: scope a
+ // fetch by host so a model walking a blocked site's pages shares one
+ // budget instead of getting a fresh one for every URL.
+ if tool == WEB_FETCH_TOOL && field == "url" {
+ if let Some(host) = url::Url::parse(&value)
+ .ok()
+ .and_then(|u| u.host_str().map(str::to_owned))
+ {
+ scope.push_str(":host=");
+ scope.push_str(&host);
+ continue;
+ }
+ }
scope.push(':');
scope.push_str(field);
scope.push('=');
@@ -306,6 +338,29 @@ fn classified_recovery_policy(
{
return Some(("unavailable", 1));
}
+ // A public website refusing or failing a fetch says nothing about
+ // OpenHuman's own credentials, whatever status it used, and the excerpt of
+ // its response body that follows must not be sniffed for keywords either.
+ // Only `web_fetch` is exempt: the same words from an account-bound tool
+ // still mean its credentials failed.
+ if tool == WEB_FETCH_TOOL {
+ if let Some(status) = fetched_site_status(error) {
+ tracing::debug!(
+ tool,
+ status,
+ "[tinyagents::mw] web_fetch site status — not a credential failure"
+ );
+ return match status {
+ // Another page or source may still work; the same host
+ // refusing repeatedly is the signal to stop.
+ 401 | 403 => Some(("site_refused", 2)),
+ 429 | 500..=599 => Some(("transient", 2)),
+ // A missing page or a rejected request is an ordinary tool
+ // failure; the exact-repeat guard bounds it.
+ _ => None,
+ };
+ }
+ }
// A tool-owned JSON error contract is less ambiguous than rendered prose.
// Read only explicit status/code fields; arbitrary response data is not a
// failure signal (this function is called only for `is_error` results).
@@ -474,6 +529,7 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
for class in [
"permission",
"authentication",
+ "site_refused",
"policy",
"unsupported",
"missing_window",
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_classified_failure_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_classified_failure_tests.rs
index e1b7d725db..b4ceccdcf5 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_classified_failure_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_classified_failure_tests.rs
@@ -545,3 +545,31 @@ fn web_fetch_failure_scope_is_the_host_not_the_page() {
scope("web_fetch", &fetch_args("https://other.test/a"))
);
}
+
+#[test]
+fn fetched_site_status_reads_only_the_web_fetch_error_shape() {
+ let status = super::super::repeated_failure::fetched_site_status;
+ assert_eq!(status(FETCH_403), Some(403));
+ assert_eq!(status(FETCH_429), Some(429));
+ assert_eq!(status(FETCH_404), Some(404));
+ assert_eq!(status(FETCH_503), Some(503));
+ assert_eq!(
+ status(" HTTP 403 Forbidden from 127.0.0.1; the site refused it."),
+ Some(403)
+ );
+ // Bare statuses, other tools' wording, success codes, and the shape
+ // buried mid-text are not the shape.
+ for text in [
+ "HTTP 403 Forbidden",
+ "HTTP 403",
+ "403 Forbidden",
+ "Gmail API error: 403 insufficient scopes",
+ "Command failed (exit 1)\nHTTP 403 Forbidden from example.test; x",
+ "HTTP 2000 Weird from example.test; x",
+ "HTTP 200 OK from example.test; x",
+ "HTTP 403 Forbidden from ; x",
+ "HTTP 403 Forbidden from example.test no semicolon",
+ ] {
+ assert_eq!(status(text), None, "{text}");
+ }
+}
diff --git a/crates/openhuman-core/src/tools/status/mod.rs b/crates/openhuman-core/src/tools/status/mod.rs
index e166705f7f..76bc7a363e 100644
--- a/crates/openhuman-core/src/tools/status/mod.rs
+++ b/crates/openhuman-core/src/tools/status/mod.rs
@@ -16,7 +16,7 @@
mod ops;
mod types;
-pub use ops::{classify, describe, fetched_site_status, tool_execution_error};
+pub use ops::{classify, describe, tool_execution_error};
pub use types::{
ClassifiedFailure, FailureCategory, ToolFailureClass, ToolLifecycleState, MODULE_FAULT_MARKER,
NOT_FOUND_MARKER, UNSUPPORTED_MARKER,
diff --git a/crates/openhuman-core/src/tools/status/ops.rs b/crates/openhuman-core/src/tools/status/ops.rs
index 36d315da55..400940346d 100644
--- a/crates/openhuman-core/src/tools/status/ops.rs
+++ b/crates/openhuman-core/src/tools/status/ops.rs
@@ -25,11 +25,6 @@ pub fn classify(error_text: &str, timed_out: bool) -> ClassifiedFailure {
describe(class)
}
-/// Stub (RED step).
-pub fn fetched_site_status(_text: &str) -> Option<u16> {
- None
-}
-
/// The prefix the agent tool adapter puts on an error a tool returned as `Err`.
/// [`tool_execution_error`] is its only producer, so the classifier and the
/// adapter cannot drift apart.
diff --git a/crates/openhuman-core/src/tools/status/ops_tests.rs b/crates/openhuman-core/src/tools/status/ops_tests.rs
index 22aff800e5..58f7508b2a 100644
--- a/crates/openhuman-core/src/tools/status/ops_tests.rs
+++ b/crates/openhuman-core/src/tools/status/ops_tests.rs
@@ -260,69 +260,3 @@ fn recoverable_flag_matches_category() {
assert!(!classify("permission denied (os error 13)", false).recoverable);
assert!(!classify("blocked by policy", false).recoverable);
}
-
-// ── A public site's HTTP status is not OpenHuman's credential (tinytools#47) ──
-//
-// `web_fetch` reports a 4xx/5xx from the site it fetched as
-// `HTTP <code> <reason> from <host>; <guidance>`. The site refusing a request
-// says nothing about OpenHuman's own sign-in, so the bare-status keyword rules
-// must not claim it.
-
-const FETCH_403: &str =
- "HTTP 403 Forbidden from example.test; the site refused the request. Try another source.";
-const FETCH_429: &str = "HTTP 429 Too Many Requests from example.test; the site is rate limiting requests. Retry-After: 30. Try another source, or retry later.";
-const FETCH_404: &str = "HTTP 404 Not Found from example.test; the page does not exist at this URL. Check the URL or try another source.";
-const FETCH_503: &str = "HTTP 503 Service Unavailable from example.test; the server failed to handle the request. Retry later or try another source.";
-
-#[test]
-fn fetched_site_status_reads_the_web_fetch_error_shape() {
- assert_eq!(fetched_site_status(FETCH_403), Some(403));
- assert_eq!(fetched_site_status(FETCH_429), Some(429));
- assert_eq!(fetched_site_status(FETCH_404), Some(404));
- assert_eq!(fetched_site_status(FETCH_503), Some(503));
- assert_eq!(
- fetched_site_status(" HTTP 403 Forbidden from 127.0.0.1; the site refused it."),
- Some(403)
- );
-}
-
-#[test]
-fn fetched_site_status_ignores_text_that_only_resembles_the_shape() {
- // Bare statuses, other tools' wording, and the shape buried mid-text.
- assert_eq!(fetched_site_status("HTTP 403 Forbidden"), None);
- assert_eq!(fetched_site_status("HTTP 403"), None);
- assert_eq!(fetched_site_status("403 Forbidden"), None);
- assert_eq!(
- fetched_site_status("Gmail API error: 403 insufficient scopes"),
- None
- );
- assert_eq!(
- fetched_site_status("Command failed (exit 1)\nHTTP 403 Forbidden from example.test; x"),
- None
- );
- assert_eq!(fetched_site_status("HTTP 2000 Weird from example.test; x"), None);
- assert_eq!(fetched_site_status("HTTP 403 Forbidden from ; x"), None);
-}
-
-#[test]
-fn a_fetched_sites_403_is_not_a_credential_failure() {
- assert_ne!(class_of(FETCH_403), ToolFailureClass::BadCredentials);
- assert_ne!(class_of(FETCH_403), ToolFailureClass::BlockedByPolicy);
- // Body excerpts quoted back by the tool must not steer the class either.
- let with_excerpt = format!("{FETCH_403}\nResponse excerpt: unauthorized, invalid api key");
- assert_ne!(class_of(&with_excerpt), ToolFailureClass::BadCredentials);
-}
-
-#[test]
-fn a_fetched_sites_other_statuses_keep_their_ordinary_classes() {
- assert_eq!(class_of(FETCH_429), ToolFailureClass::ServiceUnavailable);
- assert_eq!(class_of(FETCH_503), ToolFailureClass::ServiceUnavailable);
- assert_eq!(class_of(FETCH_404), ToolFailureClass::NotFound);
-}
-
-#[test]
-fn bare_status_text_still_means_credentials() {
- // The credentialed-API protection: no `from <host>;` shape, no exemption.
- assert_eq!(class_of("HTTP 403 Forbidden"), ToolFailureClass::BadCredentials);
- assert_eq!(class_of("403 Forbidden"), ToolFailureClass::BadCredentials);
-}