Skip to content

The agent that grows with you

fix(compressor): keep the window opening on a user turn after replay dedup

kshitijk4poord20a55cb5c19
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index a48d3d03921..a660cc4cbd1 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -5056,6 +5056,12 @@ Write only the summary body. Do not include any preamble or prefix."""
and msg.get(MESSAGE_UID) == replay_uid
)
]
+ # When the removed row opened the window, the head's tool flow
+ # (assistant tool_calls) would now lead; native Gemini rejects a
+ # leading model functionCall turn. Open the window on the carrier.
+ first = next(i for i, msg in enumerate(compressed) if msg.get("role") != "system")
+ if len(compressed) != before and compressed[first].get("role") != "user":
+ compressed.insert(first, compressed.pop(compressed.index(carrier)))
# The summary role was picked against a head that ended on the row
# just removed: an assistant carrier would now open the visible
# sequence (or follow an assistant). Use the _force_user_leading
diff --git a/tests/agent/test_cron_inflight_prompt_reappend.py b/tests/agent/test_cron_inflight_prompt_reappend.py
index 7c52a31a7ff..ce6acce4a0a 100644
--- a/tests/agent/test_cron_inflight_prompt_reappend.py
+++ b/tests/agent/test_cron_inflight_prompt_reappend.py
@@ -395,6 +395,9 @@ def test_replay_replaces_surviving_user_row_with_same_message_uid():
messages[1] = {**messages[1], "message_uid": uid}
compressed = _compress(messages)
assert len(holders(compressed)) == 1
+ # Raw role: _template_visible_role skips a leading assistant(tool_calls)
+ # row, which native Gemini rejects as the first turn.
+ assert compressed[1]["role"] == "user", [m["role"] for m in compressed]
visible = [r for r in map(_template_visible_role, compressed[1:]) if r is not None]
assert visible[0] == "user", visible
assert all(a != b for a, b in zip(visible, visible[1:])), visible