The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): keep the pending round's tool-call args verbatim too
kshitijk4poor3231237448e2
diff --git a/agent/context_compressor.py b/agent/context_compressor.pyindex 383f762f816..b7a09342fb2 100644--- a/agent/context_compressor.py+++ b/agent/context_compressor.py@@ -3223,8 +3223,11 @@ class ContextCompressor(SummaryDispatchMixin, MicroCompactionMixin, ContextEnginreturn demoteddef _spared_pending_tool_round(self, messages: List[Dict[str, Any]]) -> range:- """Return the pending round only when it fits the input window's hard share."""+ """Return the pending round, owning assistant(tool_calls) row included, only when it fits the input window's+ hard share. The call's args are part of the unread round: the model reads its result next to them."""pending = _pending_tool_round(messages)+ if pending and pending.start > 0 and messages[pending.start - 1].get("tool_calls"):+ pending = range(pending.start - 1, pending.stop)input_window = self._effective_input_window(getattr(self, "context_length", 0) or 0, getattr(self, "max_tokens", None))hard_share = int(input_window * TAIL_MAX_CONTEXT_FRACTION)@@ -3242,9 +3245,9 @@ class ContextCompressor(SummaryDispatchMixin, MicroCompactionMixin, ContextEngincall_id_to_tool = _tool_calls_by_id(result)prune_boundary = self._prune_boundary(result, protect_tail_count, protect_tail_tokens)# The pending tool round is output the model asked for and has not read yet: a stub makes it re-run the- # call (side effects included) or answer blind. Passes 2 and 4 spare it unless it alone exceeds the tail's- # hard share of the input budget the threshold is computed from (the output reservation is not room the- # round can keep) — the #61932 single-200KB-read case, which must still give way.+ # call (side effects included) or answer blind. Passes 2-4 spare it, call args included, unless it alone+ # exceeds the tail's hard share of the input budget the threshold is computed from (the output reservation+ # is not room the round can keep) — the #61932 single-200KB-read case, which must still give way.spared = self._spared_pending_tool_round(result)prune_boundary = min(prune_boundary, spared.start) if spared else prune_boundarypruned = self._dedupe_tool_results(result)diff --git a/tests/agent/test_protected_tail_pressure.py b/tests/agent/test_protected_tail_pressure.pyindex 2daa7dc10ed..a86f0d7af71 100644--- a/tests/agent/test_protected_tail_pressure.py+++ b/tests/agent/test_protected_tail_pressure.py@@ -273,11 +273,12 @@ def test_mid_turn_compaction_keeps_the_pending_tool_round_verbatim(steers):_terminal_result(f"old_{t}", f"o{t}", 70),{"role": "assistant", "content": f"node-o{t}-000 is the hottest."},]+ # The pending call's own args (well over pass 3's 500-char floor) belong to the unread round too.+ long_command = "python3 probe.py lyra 1 " + " ".join(f"--node node-nb-{i:03d}" for i in range(120))+ pending_calls = [_terminal_call("new_a", "python3 probe.py indus 3"), _terminal_call("new_b", long_command)]msgs += [{"role": "user", "content": "Run both probes and compare them."},- {"role": "assistant", "content": None, "tool_calls": [- _terminal_call("new_a", "python3 probe.py indus 3"), _terminal_call("new_b", "python3 probe.py lyra 1"),- ]},+ {"role": "assistant", "content": None, "tool_calls": pending_calls},_terminal_result("new_a", "na", 70),# Last and bigger than the soft ceiling on its own, still well inside the hard window share._terminal_result("new_b", "nb", 420),@@ -293,6 +294,8 @@ def test_mid_turn_compaction_keeps_the_pending_tool_round_verbatim(steers):by_id = {m.get("tool_call_id"): m.get("content") for m in out if m.get("role") == "tool"}for call_id, content in pending.items():assert by_id.get(call_id) == content, f"pending result {call_id} was not kept verbatim: {by_id.get(call_id)!r:.120}"+ owning = [m for m in out if m.get("role") == "assistant" and m.get("tool_calls")]+ assert owning[-1]["tool_calls"] == pending_calls, "the pending round's tool-call args were truncated"# The budget still binds older rounds: the previous turn's result does not survive verbatim.assert by_id.get("old_12") != previous["content"]