Skip to content

The agent that grows with you

fix(compression): split oversized turns whose request is long

Brian Le41c67294217f
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index b43f907c73a..e56929cdd6f 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -4563,7 +4563,8 @@ Write only the summary body. Do not include any preamble or prefix."""
text = _redact_compaction_text(_authored_request_text(msg.get("content"))) or text
text = re.sub(r"\s+", " ", text)
# Elide AFTER repr: repr would escape the marker's "Hermes's" and hide a copy from the
- # guard. Authored text within the cap stays whole (the split-turn path relies on that).
+ # guard. Authored text within the cap stays whole; a longer request split out of an
+ # oversized turn is restated verbatim by _reappend_inflight_user_task, not by this snapshot.
text = repr(text) if len(text) <= _ACTIVE_TASK_MAX_CHARS else elide(repr(text), _ACTIVE_TASK_MAX_CHARS)
return (
f"User asked (deterministic, from compacted turns): {text}\n"
@@ -5227,9 +5228,8 @@ Write only the summary body. Do not include any preamble or prefix."""
# 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
- # Measure what the user wrote: a gateway reply pointer quotes another message and
- # would otherwise disable the split for a short reply to a long answer.
- and len(_authored_request_text(messages[last_user_idx].get("content"))) <= _ACTIVE_TASK_MAX_CHARS
+ # The token ceiling is the only size guard: the request is restated verbatim after the
+ # handoff, so a character cap only pinned long requests (e.g. /goal prompts) in place.
# Only split when there is real turn body to summarize: if the oversized weight is the
# active turn's own newest group, the pre-anchor cut retains it anyway, so taking the
# active request out of the tail buys no reclaim and loses the #10896 anchor.
diff --git a/tests/agent/test_split_turn_compaction.py b/tests/agent/test_split_turn_compaction.py
index 7c2ff1f2d84..c2d66fd53ba 100644
--- a/tests/agent/test_split_turn_compaction.py
+++ b/tests/agent/test_split_turn_compaction.py
@@ -359,6 +359,34 @@ def test_restated_reply_keeps_splitting_on_later_compactions() -> None:
messages.extend(_tool_group(100 * (cycle + 1) + index))
+def test_a_long_active_request_still_splits_and_survives_verbatim() -> None:
+ """A long but token-bounded request (e.g. a /goal continuation prompt) must not pin the turn.
+
+ The row-size guard is the token soft ceiling; a character cap on the request made every
+ long-goal session uncompressible (empty window, structural backoff, context overflow).
+ """
+ compressor = _make_compressor()
+ compressor.tail_token_budget = 1_000 # soft ceiling must hold the long request row itself
+ long_request = " ".join(f"step-{i}" for i in range(400))
+ assert len(long_request) > _ACTIVE_TASK_MAX_CHARS
+ messages = _oversized_active_turn()
+ messages[3]["content"] = long_request
+ for index in range(10, 40):
+ messages.extend(_tool_group(index))
+
+ cut = compressor._find_tail_cut_by_tokens(messages, compressor._protect_head_size(messages))
+ assert cut > 3
+
+ with patch.object(compressor, "_generate_summary", return_value=None):
+ compressed = compressor.compress(messages, current_tokens=90_000, force=True)
+ assert len(compressed) < len(messages)
+ _assert_tool_pairs_are_complete(compressed)
+ live = "\n".join(
+ str(m.get("content")).rsplit(_SUMMARY_END_MARKER, 1)[-1] for m in compressed if m["role"] == "user"
+ )
+ assert live.count(long_request) == 1
+
+
@pytest.mark.parametrize(
"payload, can_split",
[