Skip to content

The agent that grows with you

fix(web): an origin HTTP 4xx/5xx never triggers the keyless extract rescue

Teknium76911b77f21f
diff --git a/plugins/web/_common.py b/plugins/web/_common.py
index 091e58efd6d..042564d2415 100644
--- a/plugins/web/_common.py
+++ b/plugins/web/_common.py
@@ -8,6 +8,7 @@ slots) lazily at call time so monkeypatching the source module keeps working.
from __future__ import annotations
import logging
+import re
from typing import Any, Awaitable, Callable, Dict, List, Optional
import httpx
@@ -71,7 +72,16 @@ def extract_fail(urls: list[str], error: str) -> list[dict[str, Any]]:
# Scraping vendors report the target's own status inside a successful API reply (Firecrawl
# ``metadata.statusCode``, Parallel ``errors[].http_status_code``, Exa ``statuses[].error``).
# Unchecked, a 403/404 page reads as a successful (often empty) extraction: the agent can't tell
-# "blocked" from "blank", the keyless rescue never runs, and the error page is cached as content.
+# "blocked" from "blank" and the error page is cached as content. The backend did its job, so an
+# origin error never triggers the keyless rescue (``is_origin_http_error``).
+_ORIGIN_HTTP_ERROR_RE = re.compile(r"HTTP \d{3} from origin(?::|$)")
+
+
+def is_origin_http_error(error: Any) -> bool:
+ """True for an ``error`` string built by ``origin_http_error`` (the target page's own answer)."""
+ return isinstance(error, str) and _ORIGIN_HTTP_ERROR_RE.match(error) is not None
+
+
def origin_http_error(status: Any, reason: Any = None) -> Optional[str]:
"""``HTTP <status> from origin[: reason]`` when the target answered >= 400, else None."""
if isinstance(status, bool):
diff --git a/tests/plugins/web/test_extract_origin_status.py b/tests/plugins/web/test_extract_origin_status.py
index 3e376ec3522..c6e4c37ade9 100644
--- a/tests/plugins/web/test_extract_origin_status.py
+++ b/tests/plugins/web/test_extract_origin_status.py
@@ -92,3 +92,20 @@ def test_exa_sdk_reports_failed_urls_from_statuses():
out = exa_provider.ExaWebSearchProvider().extract(["https://t.test/ok", "https://t.test/x"])
assert [r.get("error") for r in out] == [None, "HTTP 403 from origin"]
assert out[1]["url"] == "https://t.test/x"
+
+
[email protected]("errors,rescued", [
+ (["HTTP 404 from origin", "HTTP 410 from origin: Gone"], False), # the pages answered; no outage
+ (["HTTP 500 upstream exploded", "HTTP 500 upstream exploded"], True), # backend failure: rescue
+])
+def test_keyless_rescue_runs_for_backend_failures_not_origin_errors(errors, rescued):
+ from tools import web_tools_extract
+ urls = ["https://t.test/a", "https://t.test/b"]
+ provider = SimpleNamespace(name="keenable", extract=lambda batch, **_kw: [
+ {"url": u, "title": "", "content": "", "error": e} for u, e in zip(batch, errors)])
+ with patch.object(web_tools_extract, "_rescue_eligible", return_value=True), \
+ patch.object(web_tools_extract, "_rescue_extract", return_value=[]) as rescue:
+ out = asyncio.run(web_tools_extract._dispatch_extract(provider, urls, None))
+ assert rescue.called is rescued
+ if not rescued:
+ assert [r["error"] for r in out] == errors
diff --git a/tools/web_tools_extract.py b/tools/web_tools_extract.py
index 5070f00ec7c..a1d9ceeda91 100644
--- a/tools/web_tools_extract.py
+++ b/tools/web_tools_extract.py
@@ -146,11 +146,18 @@ def _extract_timeout_seconds() -> float:
return _DEFAULT_EXTRACT_TIMEOUT_S
+def _backend_failed_whole_batch(results: list[dict]) -> bool:
+ from plugins.web._common import is_origin_http_error
+ errors = [r.get("error") for r in results]
+ return bool(errors) and all(errors) and not any(is_origin_http_error(e) for e in errors)
+
+
async def _dispatch_extract(provider, fetch_urls: list[str], format: Optional[str]) -> list[dict]:
"""Call ``provider.extract`` (async or sync-in-thread), with one-shot keyless rescue.
Rescue fires on a raised exception — including a dispatch timeout — or when the WHOLE batch
- failed (backend outage, not per-page problems). Rescued batches are never cached.
+ failed (backend outage, not per-page problems): an origin HTTP 4xx/5xx is the page's own answer,
+ so a batch with one never triggers it. Rescued batches are never cached.
"""
import inspect
from tools.web_result_cache import extract_cache_put
@@ -177,7 +184,7 @@ async def _dispatch_extract(provider, fetch_urls: list[str], format: Optional[st
raise
failed = [_result_entry(u, str(exc)) for u in fetch_urls]
return await asyncio.to_thread(_rescue_extract, provider.name, fetch_urls, failed)
- if results and all(r.get("error") for r in results) and _rescue_eligible(provider):
+ if _backend_failed_whole_batch(results) and _rescue_eligible(provider):
return await asyncio.to_thread(_rescue_extract, provider.name, fetch_urls, results)
# Cache each successful fetch under the REQUESTED url it reports as its own — never by list