Skip to content

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

refactor(tinyagents): extract web_fetch site-status logic into a dedicated module

Steven Enamakelf43c402684e2
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware.rs b/crates/openhuman-core/src/agent/tinyagents/middleware.rs
index 029773ecc9..e3f479a760 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware.rs
@@ -29,6 +29,7 @@ mod cli_rpc_only;
mod cost_budget;
mod credential_scrub;
mod embedder_hooks;
+mod fetched_site;
mod loop_guards;
mod memory_protocol;
mod packed_tool_route;
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/fetched_site.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/fetched_site.rs
new file mode 100644
index 0000000000..afcbe9b8b8
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/fetched_site.rs
@@ -0,0 +1,55 @@
+//! A public website's HTTP status, as `web_fetch` reports it (tinytools#47).
+//!
+//! `web_fetch` renders a 4xx/5xx from the site it fetched as an error result
+//! shaped `HTTP <code> <reason> from <host>; <guidance>`, followed by an
+//! excerpt of the response body. The site refusing a request says nothing about
+//! OpenHuman's own credentials, but the keyword classifier
+//! (`tools::status::classify`) reads `403`/`forbidden` as bad credentials, a
+//! zero-retry class that would pause the whole run on one bot-blocked page.
+//! The breaker therefore reads this shape itself, for `web_fetch` only, and
+//! never sniffs the quoted body.
+
+/// The agent tool that fetches a public URL (`tinytools` `web_fetch`).
+pub(super) const WEB_FETCH_TOOL: &str = "web_fetch";
+
+/// The HTTP status of a `web_fetch` error result. 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)
+}
+
+/// Recovery class and retry budget for a fetched site's `status`.
+///
+/// - 401/403: the site refused us. Another page or source may still work, so
+/// the run continues, but the same host refusing repeatedly stops it.
+/// - 429 and 5xx: transient, with the usual retry headroom.
+/// - anything else (404, 410, 400, ...): an ordinary tool failure with no
+/// classified budget; the exact-repeat guard bounds it.
+pub(super) fn site_status_policy(status: u16) -> Option<(&'static str, usize)> {
+ tracing::debug!(
+ status,
+ "[tinyagents::mw] web_fetch site status — not a credential failure"
+ );
+ match status {
+ 401 | 403 => Some(("site_refused", 2)),
+ 429 | 500..=599 => Some(("transient", 2)),
+ _ => None,
+ }
+}
+
+/// The host of `url`, if it parses.
+pub(super) fn url_host(url: &str) -> Option<String> {
+ url::Url::parse(url)
+ .ok()
+ .and_then(|u| u.host_str().map(str::to_owned))
+}
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 75ee3127ad..c096f94933 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
@@ -19,6 +19,7 @@ use tinyinference_llm::model::ModelRequest;
use tinyinference_llm::tool::ToolCall as TaToolCall;
use tinytools::ToolResult as TaToolResult;
+use super::fetched_site::{fetched_site_status, site_status_policy, url_host, WEB_FETCH_TOOL};
use super::loop_guards::{
is_repeat_call_exempt, RECOVERABLE_NO_PROGRESS_FAILURE_THRESHOLD,
RECOVERABLE_REPEAT_FAILURE_THRESHOLD,
@@ -197,27 +198,6 @@ 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.
@@ -241,10 +221,7 @@ pub(super) fn failure_scope(tool: &str, arguments: &serde_json::Value) -> String
// 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))
- {
+ if let Some(host) = url_host(&value) {
scope.push_str(":host=");
scope.push_str(&host);
continue;
@@ -341,26 +318,10 @@ 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.
+ // OpenHuman's credentials; only `web_fetch` is exempt (see `fetched_site`).
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,
- };
+ return site_status_policy(status);
}
}
// A tool-owned JSON error contract is less ambiguous than rendered prose.
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 7210569fd9..6fce803e82 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
@@ -25,7 +25,8 @@ async fn varied_queries_against_one_forbidden_endpoint_stop_on_first_failure() {
assert_eq!(drain_pause_count(&handle), 1);
let summary = slot.lock().unwrap().clone().unwrap();
assert!(summary.contains("authentication"), "{summary}");
- assert!(summary.contains("example.test/restricted"), "{summary}");
+ // A fetch is scoped by host, never by page or query.
+ assert!(summary.contains("example.test"), "{summary}");
assert!(!summary.contains("query=first"), "{summary}");
}
@@ -556,7 +557,7 @@ fn web_fetch_failure_scope_is_the_host_not_the_page() {
#[test]
fn fetched_site_status_reads_only_the_web_fetch_error_shape() {
- let status = super::super::repeated_failure::fetched_site_status;
+ let status = super::super::fetched_site::fetched_site_status;
assert_eq!(status(FETCH_403), Some(403));
assert_eq!(status(FETCH_429), Some(429));
assert_eq!(status(FETCH_404), Some(404));