Skip to content

The agent that grows with you

fix(agent): decode sentinel-encoded multimodal turns before the alternation repair (#125299)

PRATHAMESH759d7b78f0c691
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index 65d47d29335..61bd294f252 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -663,17 +663,57 @@ _SEQUENCE_REPAIR_PASSES = (
_merge_consecutive_users,
)
+# Mirrors ``SessionDB._CONTENT_JSON_PREFIX``: the NUL-prefixed sentinel marking structured
+# (multimodal list/dict) content that was serialized to a scalar for persistence. Kept as a literal so
+# the pre-call repair layer need not import the persistence class; the NUL byte cannot occur in real text.
+_JSON_CONTENT_SENTINEL = "\x00json:"
+
+
+def _decode_sentinel_content(content: Any) -> Any:
+ """Reverse the persistence sentinel for an in-memory row. A multimodal turn (text + image) can
+ re-enter the working set as its encoded ``\\x00json:[…]`` string — e.g. after a proactive prune
+ re-inserts history (#124102) — and the alternation repair would then glue the base64 image onto a
+ neighbouring text turn as plain text (#125299). Decoding restores the structured content so the
+ repair treats it as multimodal. A body that no longer parses (already merged, corrupted) is left
+ untouched."""
+ if isinstance(content, str) and content.startswith(_JSON_CONTENT_SENTINEL):
+ try:
+ return json.loads(content[len(_JSON_CONTENT_SENTINEL):])
+ except (ValueError, TypeError):
+ return content
+ return content
+
+
+def _normalize_sentinel_encoded_content(messages: List[Dict]) -> int:
+ """Decode any sentinel-encoded row content in place before the alternation passes run, so an
+ image-bearing turn is never merged as text. Returns the number of rows restored."""
+ restored = 0
+ for msg in messages:
+ if not isinstance(msg, dict):
+ continue
+ content = msg.get("content")
+ decoded = _decode_sentinel_content(content)
+ if decoded is not content:
+ msg["content"] = decoded
+ restored += 1
+ return restored
+
def repair_message_sequence(agent, messages: List[Dict]) -> int:
"""Collapse malformed role-alternation left in the live history; returns repair count.
Providers require strict alternation after the system message (violations: silent empty
responses or 400s); this is the pre-call belt for host-fed, resumed or replayed histories.
- Passes in order: merge consecutive assistant turns (BEFORE orphan detection so the merged
- tool_call-id union is known); drop stray tool results; prune unanswered tool_calls; merge
- consecutive user turns. A user turn directly after an assistant turn is valid and left alone.
+ Passes in order: decode any sentinel-encoded multimodal rows (so an image is not merged as text);
+ merge consecutive assistant turns (BEFORE orphan detection so the merged tool_call-id union is
+ known); drop stray tool results; prune unanswered tool_calls; merge consecutive user turns. A user
+ turn directly after an assistant turn is valid and left alone.
"""
if not messages:
return 0
+ # A re-inserted multimodal turn can arrive as its ``\x00json:`` string; decode before the passes so
+ # the base64 image is not concatenated onto an adjacent text turn (#125299). In-place mutation on the
+ # shared row dicts is visible to the passes and to callers regardless of the alternation count below.
+ _normalize_sentinel_encoded_content(messages)
repairs = 0
current = messages
for repair_pass in _SEQUENCE_REPAIR_PASSES:
diff --git a/tests/agent/test_message_sequence_repair.py b/tests/agent/test_message_sequence_repair.py
index 3c36d331ef9..0ef057d1ad8 100644
--- a/tests/agent/test_message_sequence_repair.py
+++ b/tests/agent/test_message_sequence_repair.py
@@ -1579,3 +1579,45 @@ def test_repair_cursor_invalidates_scan_prefix_when_stamped_dict_dirtied():
assert repairs == 1
assert _DB_PERSISTED_MARKER not in messages[0]
assert agent._db_flush_scan_prefix is None
+
+
+# ── sentinel-encoded multimodal content (#125299) ──────────────────────────
+
+def test_repair_decodes_sentinel_multimodal_and_skips_text_merge():
+ """A multimodal turn re-inserted as its ``\x00json:`` string (e.g. after a proactive prune
+ re-inserts history) must be decoded back to structured content, not glued onto an adjacent
+ text turn as a giant base64 blob (#125299)."""
+ import json
+ from agent.agent_runtime_helpers import repair_message_sequence, _JSON_CONTENT_SENTINEL
+
+ parts = [
+ {"type": "text", "text": "look at this"},
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64," + "A" * 4000}},
+ ]
+ encoded = _JSON_CONTENT_SENTINEL + json.dumps(parts)
+ messages = [
+ {"role": "user", "content": encoded},
+ {"role": "user", "content": "any follow-up thoughts?"},
+ ]
+
+ repair_message_sequence(_bare_agent(), messages)
+
+ # The encoded turn is restored to structured content and left un-merged; the base64 image
+ # never leaks into the neighbouring text turn.
+ assert len(messages) == 2
+ assert messages[0]["content"] == parts
+ assert messages[1]["content"] == "any follow-up thoughts?"
+ assert "base64" not in messages[1]["content"]
+
+
+def test_repair_leaves_corrupted_sentinel_content_untouched():
+ """A sentinel body that no longer parses (already merged / truncated) is left as-is rather
+ than crashing the pre-call repair (#125299)."""
+ from agent.agent_runtime_helpers import repair_message_sequence, _JSON_CONTENT_SENTINEL
+
+ corrupt = _JSON_CONTENT_SENTINEL + '[{"type": "text"} EXTRA garbage'
+ messages = [{"role": "user", "content": corrupt}]
+
+ repair_message_sequence(_bare_agent(), messages)
+
+ assert messages[0]["content"] == corrupt