Skip to content

The agent that grows with you

fix(agent): keep canonical history intact during image-corrupt retry

Sora-blueskya3177d0570f2
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py
index 344f23f976d..7520a1fcc43 100644
--- a/agent/conversation_loop.py
+++ b/agent/conversation_loop.py
@@ -4893,13 +4893,20 @@ def run_conversation(
# image parts and retry once instead of routing through the
# shrink path above. See issue #69078.
if classified.reason == FailoverReason.image_corrupt:
- _imgs_removed = _strip_images_from_messages(messages)
+ # Strip ONLY the per-call payload copy. api_messages rows
+ # are shallow copies of canonical history, and the strip
+ # replaces msg["content"] rather than mutating the shared
+ # parts list — so canonical messages keep their images.
+ # A transient provider rejection must not permanently
+ # erase history (#69104 sweeper review; the copy-on-write
+ # contract from e762a5a473).
+ _imgs_removed = False
if isinstance(api_messages, list):
- _imgs_removed = _strip_images_from_messages(api_messages) or _imgs_removed
+ _imgs_removed = _strip_images_from_messages(api_messages)
if _imgs_removed:
agent._vprint(
f"{agent.log_prefix}⚠️ Provider rejected a corrupted image — "
- f"stripped images from history and retrying...",
+ f"stripped images from the retry payload and retrying...",
force=True,
)
continue
diff --git a/tests/run_agent/test_69078_image_corrupt_recovery.py b/tests/run_agent/test_69078_image_corrupt_recovery.py
index a58448a89aa..e3726f433ae 100644
--- a/tests/run_agent/test_69078_image_corrupt_recovery.py
+++ b/tests/run_agent/test_69078_image_corrupt_recovery.py
@@ -367,3 +367,55 @@ class TestRunConversationRecoversFromCorruptImage400:
"reverted generic fallback appears to have been reintroduced: "
f"{only_call_msgs!r}"
)
+
+
+# ─── History isolation: strip only the per-call payload copy (#69104) ────────
+
+
+class TestCanonicalHistoryIsolation:
+ def test_payload_copy_strip_preserves_canonical_history(self):
+ """api_messages rows are shallow copies of canonical history
+ (``msg.copy()`` in the build loop). The strip must replace the
+ copy's ``content`` instead of mutating the shared parts list, so
+ a transient provider rejection never erases the stored image."""
+ image_part = {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,AA=="},
+ }
+ text_part = {"type": "text", "text": "look at this"}
+ canonical = [{"role": "user", "content": [text_part, image_part]}]
+ api_messages = [m.copy() for m in canonical] # build-loop aliasing
+
+ removed = _strip_images_from_messages(api_messages)
+
+ assert removed is True
+ assert api_messages[0]["content"] == [text_part]
+ assert canonical[0]["content"] == [text_part, image_part], (
+ "canonical history lost its image through shallow aliasing"
+ )
+
+ def test_image_corrupt_branch_strips_only_payload_copy(self):
+ """Contract check on the recovery branch itself: the image_corrupt
+ path must NOT call _strip_images_from_messages(messages) — only the
+ api_messages per-call copy. Stripping canonical history permanently
+ erased images on a transient provider error (#69104 sweeper review,
+ copy-on-write contract from e762a5a473)."""
+ import inspect
+ import re as _re
+
+ import agent.conversation_loop as loop_mod
+
+ src = inspect.getsource(loop_mod)
+ # Locate the image_corrupt recovery block and inspect its calls.
+ block = _re.search(
+ r"image_corrupt:\n(.*?)\n\s*(?:continue|else)", src, _re.S
+ )
+ assert block is not None, "image_corrupt recovery branch not found"
+ body = block.group(1)
+ assert "_strip_images_from_messages(api_messages)" in body, (
+ "recovery must strip the per-call api_messages copy"
+ )
+ assert "_strip_images_from_messages(messages)" not in body, (
+ "recovery must NOT strip canonical messages — that permanently "
+ "erases history on a transient provider rejection"
+ )