Skip to content

The agent that grows with you

fix(web): origin error pages keep their body inside the char_limit budget

Teknium2699663a6430
diff --git a/tests/tools/test_web_tools_truncate.py b/tests/tools/test_web_tools_truncate.py
index 9554a3b393b..fd4cf5c02a7 100644
--- a/tests/tools/test_web_tools_truncate.py
+++ b/tests/tools/test_web_tools_truncate.py
@@ -128,3 +128,13 @@ def test_binary_payload_is_refused_but_prose_with_short_signature_prefix_passes(
assert results[2]["content"] == "BMW reviews are fine"
assert results[3]["content"] == "# hi"
+
+
+def test_origin_error_page_keeps_its_error_and_stays_inside_the_budget():
+ body = "\n".join(f"error page line {i}" for i in range(4000))
+ results = [{"url": "https://example.com/forbidden", "content": body, "raw_content": body,
+ "error": "HTTP 403 from origin: Forbidden"}]
+ web_tools_truncate._truncate_results(results, 2000, {"pages_truncated": 0, "truncation_metrics": []})
+ assert results[0]["error"] == "HTTP 403 from origin: Forbidden"
+ assert "error page line 0" in results[0]["content"]
+ assert len(results[0]["content"]) < len(body) // 4
diff --git a/tools/web_tools_truncate.py b/tools/web_tools_truncate.py
index 69317b7db44..d6a965c344e 100644
--- a/tools/web_tools_truncate.py
+++ b/tools/web_tools_truncate.py
@@ -156,7 +156,12 @@ def _truncate_results(results: list[dict], char_limit: int, debug_call_data: dic
for result in results:
url = result.get("url", "")
raw_content = result.get("raw_content", "") or result.get("content", "")
- if result.get("error") or not raw_content:
+ if result.get("error"):
+ # An origin error page keeps its body (a 404 page can help) but stays in the budget.
+ if body := result.get("content"):
+ result["content"], _ = _truncate_with_footer(convert_base64_images_to_links(body), url, char_limit)
+ continue
+ if not raw_content:
continue
binary_kind = _binary_payload_kind(raw_content)
if binary_kind: