Skip to content

The agent that grows with you

fix(compression): retain non-text active requests when splitting turns

ppazosp96f087eef518
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 8968b7f08d0..8f192bee92b 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -1412,6 +1412,21 @@ def _content_text_for_contains(content: Any) -> str:
return "" if content is None else content if isinstance(content, str) else str(content)
+def _is_text_only_content(content: Any) -> bool:
+ """Whether the active request can be restated without losing a content part."""
+ if isinstance(content, str):
+ return True
+ return isinstance(content, list) and all(
+ isinstance(part, str)
+ or (
+ isinstance(part, dict)
+ and part.get("type") in {"text", "input_text"}
+ and isinstance(part.get("text"), str)
+ )
+ for part in content
+ )
+
+
def _append_text_to_content(content: Any, text: str, *, prepend: bool = False) -> Any:
"""Append or prepend plain text to message content (string or multimodal list)."""
if content is None:
@@ -5017,6 +5032,9 @@ Write only the summary body. Do not include any preamble or prefix."""
if (
allow_split_turn
and user_anchored_cut < cut_idx
+ # The handoff can restate text, but cannot replace an audio, image,
+ # or unknown structured input with its text-only projection.
+ and _is_text_only_content(messages[last_user_idx].get("content"))
# A single oversized user message is indivisible and must stay verbatim in the tail; this
# exception is only for aggregate turn growth after a normally sized opening request.
and _estimate_msg_budget_tokens(messages[last_user_idx]) <= soft_ceiling
diff --git a/tests/agent/test_split_turn_compaction.py b/tests/agent/test_split_turn_compaction.py
index fbc45793483..70e7b3aae27 100644
--- a/tests/agent/test_split_turn_compaction.py
+++ b/tests/agent/test_split_turn_compaction.py
@@ -260,3 +260,30 @@ def test_active_request_survives_repeated_compaction_and_restart(
messages = db.get_messages_as_conversation(session_id)
finally:
db.close()
+
+
+ "payload, can_split",
+ [
+ ([{"type": "audio", "source": {"data": "AA=="}}], False),
+ ([{"type": "input_audio", "input_audio": {"data": "AA=="}}], False),
+ ([{"type": "future_input", "payload": {"value": 7}}], False),
+ ([{"type": "text", "text": _ACTIVE_REQUEST}, {"type": "audio", "source": {"data": "AA=="}}], False),
+ ([{"type": "text", "text": _ACTIVE_REQUEST}], True),
+ ],
+ ids=["audio", "input-audio", "future-input", "text-and-audio", "text-parts"],
+)
+def test_split_requires_a_request_that_can_be_restated_as_text(payload, can_split):
+ compressor = _make_compressor()
+ messages = _oversized_active_turn()
+ messages[3]["content"] = payload
+ cut = compressor._find_tail_cut_by_tokens(messages, compressor._protect_head_size(messages))
+ assert (cut > 3) is can_split
+ with patch.object(compressor, "_generate_summary", return_value=None):
+ compressed = compressor.compress(messages, current_tokens=90_000, force=True)
+ if can_split:
+ assert len(compressed) < len(messages)
+ assert any(_ACTIVE_REQUEST in str(m.get("content")) for m in compressed)
+ else:
+ assert any(m.get("content") == payload for m in compressed)
+ _assert_tool_pairs_are_complete(compressed)