Skip to content

The agent that grows with you

fix: web_extract cache digest matches CRLF and BOM pages as read back

Teknium74195d368078
diff --git a/tests/tools/test_web_extract_cache_final_url.py b/tests/tools/test_web_extract_cache_final_url.py
index 26ded7a8f75..e43a3c98156 100644
--- a/tests/tools/test_web_extract_cache_final_url.py
+++ b/tests/tools/test_web_extract_cache_final_url.py
@@ -100,6 +100,25 @@ def test_an_uncached_refetch_never_retargets_the_old_entry(monkeypatch):
assert later.calls == 1 and not result.get("cached"), "the old blocked page was served from cache"
+def test_crlf_page_round_trips_through_the_cache(monkeypatch):
+ """A page with CRLF line endings (Tavily passes text through untouched) must hit on the next
+ call, not miss forever and re-buy the vendor fetch."""
+ check, safe = _policy()
+ monkeypatch.setattr("tools.website_policy.check_website_access", check)
+ monkeypatch.setattr("tools.url_safety.async_is_safe_url", safe)
+
+ class _Crlf(_RedirectingProvider):
+ async def extract(self, urls, format=None):
+ self.calls += 1
+ body = "line one\r\nline two\r\n"
+ return [{"url": TARGET, "content": body, "raw_content": body, "metadata": {"sourceURL": urls[0]}}]
+
+ provider = _Crlf()
+ _extract(provider)
+ assert _extract(provider)[0].get("cached") is True
+ assert provider.calls == 1
+
+
def test_interleaved_writers_never_pair_one_body_with_anothers_final_url(monkeypatch):
"""Two fetches of one key: A writes its body, B writes its body and final URL, then A's late
final-URL write lands. B's body must stay paired with B's destination, so blocking B's host
diff --git a/tools/web_result_cache.py b/tools/web_result_cache.py
index 7453d1be3c6..a16bd2a9236 100644
--- a/tools/web_result_cache.py
+++ b/tools/web_result_cache.py
@@ -284,7 +284,10 @@ def extract_cache_get(url: str, format: Optional[str] = None, provider: str = ""
def _content_sha(content: str) -> str:
- return hashlib.sha256(content.encode("utf-8")).hexdigest()
+ # Hash the text as ``extract_cache_get`` reads it back: text-mode reads translate CRLF/CR to LF
+ # and ``utf-8-sig`` drops a leading BOM, so a raw-text digest would never match those pages.
+ normalized = content.removeprefix("\ufeff").replace("\r\n", "\n").replace("\r", "\n")
+ return hashlib.sha256(normalized.encode("utf-8")).hexdigest()
def extract_cache_record_final_url(