The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor: dedupe the corrupt-image recovery and split the phrase lists
kshitijk4poor565b2ce2065a
diff --git a/agent/message_sanitization.py b/agent/message_sanitization.pyindex c035686349a..c9b0ca6363a 100644--- a/agent/message_sanitization.py+++ b/agent/message_sanitization.py@@ -393,10 +393,9 @@ _IMAGE_REJECTION_PHRASES = ()# Provider error bodies meaning "this particular image payload is bad" — the model CAN see, it-# just could not decode what it was sent. Matched by ``_looks_like_image_content_rejection`` too-# (the turn recovers the same way: strip and retry), but the recovery must NOT remember the-# model as image-rejecting for the session on their account: the next request with a good image-# would be needlessly stripped for the rest of the session.+# just could not decode what it was sent. Disjoint from ``_IMAGE_REJECTION_PHRASES``: the turn+# recovers the same way (strip and retry) but must NOT remember the model as image-rejecting,+# or the next request with a good image would be needlessly stripped for the rest of the session._IMAGE_CORRUPT_PHRASES = (# ChatGPT-account Codex backend's wording for corrupt/unsupported native image payloads."image data you provided does not represent a valid image",@@ -410,9 +409,6 @@ _IMAGE_CORRUPT_PHRASES = ("failed to decode image",)-_IMAGE_REJECTION_PHRASES = _IMAGE_REJECTION_PHRASES + _IMAGE_CORRUPT_PHRASES--def strip_images_for_rejecting_model(agent: Any, api_messages: Any) -> bool:"""Send-path image strip for a model that rejected image content (see turn_recovery).diff --git a/agent/turn_recovery.py b/agent/turn_recovery.pyindex 3163061ff69..3dd48ee67a3 100644--- a/agent/turn_recovery.py+++ b/agent/turn_recovery.py@@ -220,6 +220,17 @@ def _recover_unicode_encode_error(return True, active_system_prompt+def _strip_request_images_and_retry(agent: Any, api_messages: Any) -> bool:+ """Strip image parts from the per-call ``api_messages`` copy; True if anything was removed.++ Shared by the corrupt-image recoveries: a bad payload says nothing about the model, so it+ is stripped for this attempt only and the model is never recorded as image-rejecting."""+ if isinstance(api_messages, list) and _strip_images_from_messages(api_messages):+ _vlines(agent, "⚠️ Provider rejected a corrupted image — stripped images from the retry payload and retrying...")+ return True+ return False++def recover_before_classification(agent: Any, api_error: Exception, *, messages: List[Dict[str, Any]], api_messages: Any,api_kwargs: Any, active_system_prompt: Any,@@ -249,18 +260,18 @@ def recover_before_classification(# images too, and a turn-wide flag would skip its recovery and fail the turn._model_key = _provider_model_key(agent)_rejected = agent._image_rejecting_models- if _model_key not in _rejected and _looks_like_image_content_rejection(_err_body) and _status_ok:+ _corrupt = _looks_like_corrupt_image_rejection(_err_body)+ if _status_ok and (_corrupt or (_model_key not in _rejected and _looks_like_image_content_rejection(_err_body))):# Send-path only. A rejection says what THIS model accepts, not what the conversation# holds: stripping ``messages`` (canonical history) and forcing a flush deleted every# image — and every image-only message — from state.db for good, so a later switch to a# vision model found them gone. Same failure as the ASCII strip in #117802.- if _looks_like_corrupt_image_rejection(_err_body):+ if _corrupt:# A bad payload says nothing about the model's capability: strip this attempt only# (like the image_corrupt branch below) and leave the model unmarked so a later good# image still reaches it. Retry only if something was stripped, or a text-only# request would loop on the same error.- if isinstance(api_messages, list) and _strip_images_from_messages(api_messages):- _vlines(agent, "⚠️ Provider rejected a corrupted image — stripped images from the retry payload and retrying...")+ if _strip_request_images_and_retry(agent, api_messages):return True, active_system_promptelse:# Record the model; the retry re-enters build_api_request with the same@@ -709,8 +720,7 @@ def recover_after_classification(# Strip ONLY the per-call copy: replacing msg["content"] on the shallow api_messages# rows keeps canonical history's images (transient rejection must not erase history).if classified.reason == FailoverReason.image_corrupt:- if isinstance(api_messages, list) and _strip_images_from_messages(api_messages):- _vlines(agent, "⚠️ Provider rejected a corrupted image — stripped images from the retry payload and retrying...")+ if _strip_request_images_and_retry(agent, api_messages):return True, recovered_with_poollogger.info("image-corrupt recovery: no image parts found to strip; surfacing original error.")diff --git a/tests/agent/test_image_corrupt_recovery.py b/tests/agent/test_image_corrupt_recovery.pyindex 400b0aab0b3..d7e93c83abc 100644--- a/tests/agent/test_image_corrupt_recovery.py+++ b/tests/agent/test_image_corrupt_recovery.py@@ -456,7 +456,7 @@ class TestCanonicalHistoryIsolation:)assert block is not None, "image_corrupt recovery branch not found"body = block.group(1)- assert "_strip_images_from_messages(api_messages)" in body, (+ assert "_strip_request_images_and_retry(agent, api_messages)" in body, ("recovery must strip the per-call api_messages copy")assert "_strip_images_from_messages(messages)" not in body, (diff --git a/tests/agent/test_image_rejection_fallback.py b/tests/agent/test_image_rejection_fallback.pyindex be7155d815c..d56047620a3 100644--- a/tests/agent/test_image_rejection_fallback.py+++ b/tests/agent/test_image_rejection_fallback.py@@ -7,7 +7,8 @@ require, and that the phrase detector fires on the expected error bodies."""from agent.message_sanitization import (- _looks_like_image_content_rejection, _strip_images_from_messages, strip_images_for_rejecting_model,+ _looks_like_corrupt_image_rejection, _looks_like_image_content_rejection, _strip_images_from_messages,+ strip_images_for_rejecting_model,)@@ -171,7 +172,9 @@ class TestImageRejectionPhraseIsolation:"""def _matches(self, body: str) -> bool:- return _looks_like_image_content_rejection(body)+ # The two phrase lists are disjoint (corrupt payload vs. text-only model) but both+ # trip the strip-and-retry recovery; this asks the same question the recovery does.+ return _looks_like_corrupt_image_rejection(body) or _looks_like_image_content_rejection(body)def test_kimi_truncated_image_trips_recovery(self):# Kimi/Moonshot reject truncated image bytes with this 400; the