Skip to content

The agent that grows with you

fix(compression): the active-request bridge obeys the post-handoff progress floor

Teknium882482b58a3f
diff --git a/agent/context_compressor_tail.py b/agent/context_compressor_tail.py
index af35d5ed1c2..cafc9b24706 100644
--- a/agent/context_compressor_tail.py
+++ b/agent/context_compressor_tail.py
@@ -139,7 +139,9 @@ def tail_cut_progress_floor(compressor: Any, messages: list[dict[str, Any]], hea
handoff) is a handoff, the planned window is empty and compress() no-ops with ``empty_post_handoff_window``. That
happens when the cut lands inside the tool group right after the handoff and is aligned back onto the group's
assistant. The floor then moves past that group, unless doing so would take the newest row or the active user
- request out of the tail; those cases keep the old ``head_end + 1`` floor."""
+ request out of the tail; those cases keep the old ``head_end + 1`` floor. ``_compress_window``'s
+ assistant/tool bridge before the active request obeys the same floor, so the two never disagree: when the floor
+ sits on the active request, the summary merges into that row instead of the bridge emptying the window."""
floor = head_end + 1
newest = next(
(i for i in range(len(messages) - 1, head_end - 1, -1) if compressor._is_context_summary_message(messages[i])), -1,
diff --git a/agent/context_compressor_window.py b/agent/context_compressor_window.py
index c984124c456..8eeb61628dd 100644
--- a/agent/context_compressor_window.py
+++ b/agent/context_compressor_window.py
@@ -6,6 +6,7 @@ from __future__ import annotations
from typing import Any, Optional
+from agent.context_compressor_tail import tail_cut_progress_floor
from agent.prepared_compaction import PreparedCompaction, PreparedSummary
@@ -20,7 +21,9 @@ class CompactionWindowMixin:
compress_start = self._align_boundary_forward(messages, self._protect_head_size(messages))
compress_end = self._find_tail_cut_by_tokens(messages, compress_start)
# A role collision can merge the summary into the first tail row; keep an actionable user
- # event out of that slot by retaining an older assistant/tool bridge.
+ # event out of that slot by retaining an older assistant/tool bridge. The bridge never moves
+ # the cut below the progress floor: a bridge that leaves only handoff rows in the window
+ # would no-op with ``empty_post_handoff_window`` at every trigger.
latest_actionable_idx = self._find_last_user_message_idx(messages, 0)
if compress_end == latest_actionable_idx:
bridge_idx = latest_actionable_idx - 1
@@ -29,7 +32,7 @@ class CompactionWindowMixin:
bridge_idx = self._align_boundary_backward(messages, latest_actionable_idx)
elif bridge_role != "assistant":
bridge_idx = -1
- if bridge_idx > compress_start:
+ if bridge_idx >= tail_cut_progress_floor(self, messages, compress_start, bridge_idx):
compress_end = bridge_idx
return compress_start, compress_end
diff --git a/tests/agent/test_compressor_handoff_tool_group_floor.py b/tests/agent/test_compressor_handoff_tool_group_floor.py
index ccec8795c64..db62064c13d 100644
--- a/tests/agent/test_compressor_handoff_tool_group_floor.py
+++ b/tests/agent/test_compressor_handoff_tool_group_floor.py
@@ -58,3 +58,24 @@ def test_cut_inside_the_first_tool_group_still_compacts_past_the_handoff():
# The active ask and everything after it stay verbatim in the tail.
assert [m.get("content") for m in result[-5:]] == [m.get("content") for m in messages[-5:]]
+
+def test_first_tool_group_right_before_the_active_request_still_compacts():
+ """The floor lands on the active request; the bridge rule must not pull the cut back onto the handoff."""
+ cc = _compressor()
+ ask = "please read the next file and report"
+ messages = [
+ _transcript()[0],
+ _calls("a", "b"), _result("a", 1_200), _result("b", 1_300),
+ {"role": "user", "content": ask},
+ _calls("f"), _result("f", 1_770), _calls("g"), _result("g", 1_670), _calls("h"), _result("h", 1_500),
+ ]
+ with patch.object(cc, "_generate_summary", return_value="NEW SUMMARY BODY"):
+ result = cc.compress(messages, current_tokens=12_800)
+
+ assert (cc._last_compression_telemetry or {}).get("failure_class") is None
+ assert cc._last_compression_made_progress is True
+ assert len(result) < len(messages)
+ assert sum(1 for m in result if cc._is_context_summary_message(m)) == 1
+ # The active ask survives verbatim (after the summary's end marker) and the rounds after it stay in the tail.
+ assert result[0]["content"].endswith(ask)
+ assert [m.get("content") for m in result[1:]] == [m.get("content") for m in messages[5:]]