Skip to content

The agent that grows with you

refactor(agent): extract the compaction window plan from compress()

Hugo Alves Pereiraee5479836002
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index fa26f20e2db..3fbf176fd5c 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -5216,6 +5216,16 @@ Write only the summary body. Do not include any preamble or prefix."""
compress_end = bridge_idx
return compress_start, compress_end
+ def _plan_compaction_window(self, messages: List[Dict[str, Any]]) -> tuple[List[Dict[str, Any]], int, int, int]:
+ """Phases 1-2 of compress(): ``(working, pruned_count, compress_start, compress_end)``.
+ ``working`` is a pruned copy without blank echoes; the boundaries index into it."""
+ working, pruned_count = self._prune_old_tool_results(
+ messages, protect_tail_count=self.protect_last_n, protect_tail_tokens=self.tail_token_budget,
+ )
+ working = self._drop_blank_echoes(working)
+ compress_start, compress_end = self._compress_window(working)
+ return working, pruned_count, compress_start, compress_end
+
def _log_compression_start(
self, display_tokens: int, compress_start: int, compress_end: int, n_turns: int, tail_msgs: int,
) -> None:
@@ -5480,16 +5490,12 @@ Write only the summary body. Do not include any preamble or prefix."""
# Unpruned copy for no-op/abort returns (history stays lossless) and finalize; head/tail are
# assembled and measured from the pruned copy (#61932).
canonical_messages = self._drop_blank_echoes(messages)
- # Phase 1: Prune old tool results (cheap, no LLM call)
- messages, pruned_count = self._prune_old_tool_results(
- messages, protect_tail_count=self.protect_last_n, protect_tail_tokens=self.tail_token_budget,
- )
+ # Phases 1-2: prune (cheap, no LLM call), drop blank echoes, determine boundaries on the pruned
+ # copy so a pressure-demoted tail can compress (#61932)
+ messages, pruned_count, compress_start, compress_end = self._plan_compaction_window(messages)
if pruned_count and not self.quiet_mode:
logger.info("Pre-compression: pruned %d old tool result(s)", pruned_count)
- messages = self._drop_blank_echoes(messages)
n_messages = len(messages)
- # Phase 2: Determine boundaries (on the pruned copy so a pressure-demoted tail can compress, #61932)
- compress_start, compress_end = self._compress_window(messages)
if compress_start >= compress_end:
self._record_compression_regions(
head_messages=messages[:compress_start], middle_messages=[], tail_messages=messages[compress_end:],