Skip to content

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

fix(web-search): restrict quota-error detection to provider envelope

Steven Enamakele466d129fcf0
diff --git a/src/openhuman/search/tools/exa.rs b/src/openhuman/search/tools/exa.rs
index d4ca349e1e..8d98bab3f4 100644
--- a/src/openhuman/search/tools/exa.rs
+++ b/src/openhuman/search/tools/exa.rs
@@ -249,7 +249,7 @@ impl ExaClient {
if status == reqwest::StatusCode::TOO_MANY_REQUESTS {
anyhow::bail!(
"Exa rate limit exceeded (HTTP 429). \
- Please wait a moment before searching again."
+ Please wait a moment before retrying."
);
}
anyhow::bail!("Exa returned non-2xx status {status}");
diff --git a/src/openhuman/search/tools/web_search.rs b/src/openhuman/search/tools/web_search.rs
index eeafb3503f..43c42955b7 100644
--- a/src/openhuman/search/tools/web_search.rs
+++ b/src/openhuman/search/tools/web_search.rs
@@ -37,11 +37,24 @@ const MANAGED_DEFAULT_PROVIDER: &str = "Exa";
fn managed_search_quota_error(message: &str) -> Option<&'static str> {
let lowered = message.to_ascii_lowercase();
- // Exa tags credit exhaustion explicitly; the numeric form covers providers
- // that only report the status. `credit` is required alongside the bare
- // status so an unrelated 402 in a URL or id cannot trip this.
- let credits_exhausted = lowered.contains("no_more_credits")
- || (lowered.contains("402") && lowered.contains("credit"));
+ // Only inspect the structured provider envelope before any echoed JSON
+ // body. The body may contain the submitted query, so searching all of it
+ // would let an unrelated query such as "HTTP 429" trigger this mapping.
+ let envelope = message.split_once('{').map_or(message, |(prefix, _)| prefix);
+ let envelope_lowered = envelope.to_ascii_lowercase();
+ let provider_json = message
+ .find('{')
+ .and_then(|start| serde_json::from_str::<Value>(&message[start..]).ok());
+
+ // Exa tags credit exhaustion explicitly; the numeric form covers a
+ // provider status in its API-error envelope. A parsed tag avoids matching
+ // the same text when it appears in an echoed query or error string.
+ let credits_exhausted = provider_json
+ .as_ref()
+ .and_then(|body| body.get("tag"))
+ .and_then(Value::as_str)
+ .is_some_and(|tag| tag.eq_ignore_ascii_case("NO_MORE_CREDITS"))
+ || envelope_lowered.contains("api error (402)");
if credits_exhausted {
return Some(
"Managed web search is temporarily unavailable: the shared search credit pool is \
@@ -50,7 +63,17 @@ fn managed_search_quota_error(message: &str) -> Option<&'static str> {
);
}
- if lowered.contains("429") || lowered.contains("rate limit") {
+ // Accept only an explicit status in the outer/provider envelope. In
+ // particular, do not classify prose from the response body: providers
+ // commonly echo the query there verbatim.
+ if envelope_lowered.contains("api error (429)")
+ || envelope_lowered.contains("backend returned 429 ")
+ || provider_json
+ .as_ref()
+ .and_then(|body| body.get("status"))
+ .and_then(Value::as_u64)
+ == Some(429)
+ {
return Some(
"Managed web search is rate limited upstream. Please wait a moment before searching \
again, or configure your own search API key under Connections > Search engine.",
diff --git a/src/openhuman/search/tools/web_search_tests.rs b/src/openhuman/search/tools/web_search_tests.rs
index 0dff06f09f..d4de4a0cee 100644
--- a/src/openhuman/search/tools/web_search_tests.rs
+++ b/src/openhuman/search/tools/web_search_tests.rs
@@ -632,7 +632,7 @@ fn managed_quota_classifier_matches_the_tag_alone() {
fn managed_quota_classifier_matches_rate_limits() {
for message in [
"Backend returned 429 Too Many Requests for POST /agent-integrations/parallel/search: slow down",
- "Backend returned 400 Bad Request for POST /x: provider rate limit exceeded",
+ "Exa API error (429): {\"error\":\"temporarily unavailable\"}",
] {
let classified =
managed_search_quota_error(message).expect("rate-limit fault must classify");
@@ -654,6 +654,11 @@ fn managed_quota_classifier_leaves_unrelated_failures_alone() {
// on its own — the classifier requires the word `credit` alongside it.
"POST http://127.0.0.1:40200/agent-integrations/parallel/search failed: timed out",
"Web search unavailable: no backend session token.",
+ // Quota-looking text in an echoed query must not replace the real
+ // failure classification.
+ "Backend returned 500 Internal Server Error for POST /x: query=NO_MORE_CREDITS",
+ "Backend returned 500 Internal Server Error for POST /x: query=what is HTTP 429",
+ "Backend returned 400 Bad Request for POST /x: {\"error\":\"query says rate limit\"}",
] {
assert!(
managed_search_quota_error(message).is_none(),