Skip to content

The agent that grows with you

fix(compression): keep the pending tool round when two steers follow it

MongLong021465f185c59452
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 2f3abf6dab6..abb4baaa9be 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -1379,10 +1379,11 @@ def _last_assistant_index(messages: "List[Dict[str, Any]]") -> int:
def _pending_tool_round(messages: "List[Dict[str, Any]]") -> range:
"""Indices of the tool results the transcript ends with — a round the model has not answered yet; empty when
- the transcript ends in any other row. One /steer row after the round does not answer it: the steer is
- delivered after the newest tool result before the next API call, so preflight compaction sees it last."""
+ the transcript ends in any other row. /steer rows after the round do not answer it: a steer is delivered
+ after the newest tool result before the next API call, and two can land in one iteration (one when the tool
+ batch ends, one before the request), so every contiguous trailing steer row is skipped."""
end = len(messages)
- if end and messages[end - 1].get("display_kind") == STEER_DISPLAY_KIND:
+ while end and messages[end - 1].get("display_kind") == STEER_DISPLAY_KIND:
end -= 1
start = end
while start > 0 and messages[start - 1].get("role") == "tool":
diff --git a/tests/agent/test_protected_tail_pressure.py b/tests/agent/test_protected_tail_pressure.py
index 79b45fb0b0d..e87bc6b6b90 100644
--- a/tests/agent/test_protected_tail_pressure.py
+++ b/tests/agent/test_protected_tail_pressure.py
@@ -225,15 +225,17 @@ def _terminal_result(call_id: str, tag: str, lines: int) -> dict:
return {"role": "tool", "tool_call_id": call_id, "content": json.dumps({"output": output, "exit_code": 0})}
[email protected]("steered", [False, True], ids=["round_last", "steer_after_round"])
-def test_mid_turn_compaction_keeps_the_pending_tool_round_verbatim(steered):
[email protected]("steers", [0, 1, 2], ids=["round_last", "steer_after_round", "two_steers_after_round"])
+def test_mid_turn_compaction_keeps_the_pending_tool_round_verbatim(steers):
"""Regression: compaction after a tool round stubbed the output the model had just asked for.
Lean mode's 10K tail budget puts pass 4's soft ceiling at 15K tokens. Long user messages (no pass
may shrink them) filled it, so the last resort stubbed the pending round. The model has not read
that round and would re-run the command or answer blind, so it must survive verbatim while older
rounds still give way to the budget. A /steer sent during the tools lands as a user row after the
- round before preflight compaction runs; the round is still unread then.
+ round before preflight compaction runs; the round is still unread then. Two steers can land in one
+ iteration: one drained when the tool batch ends, another drained before the next request and
+ inserted right after the newest tool result, so the round is followed by two steer rows.
"""
ctx = 272_000
with patch("agent.context_compressor.get_model_context_length", return_value=ctx):
@@ -262,8 +264,8 @@ def test_mid_turn_compaction_keeps_the_pending_tool_round_verbatim(steered):
]
pending = {m["tool_call_id"]: m["content"] for m in msgs[-2:]}
previous = msgs[-6]
- if steered:
- msgs.append(steer_user_row("Also flag any node above 90% cpu."))
+ for text in ["Also flag any node above 90% cpu.", "And list the racks they sit in."][:steers]:
+ msgs.append(steer_user_row(text))
assert previous["tool_call_id"] == "old_12"
out = c.compress(list(msgs), current_tokens=estimate_messages_tokens_rough(msgs))
diff --git a/website/docs/developer-guide/context-compression-and-caching.md b/website/docs/developer-guide/context-compression-and-caching.md
index 6bf5ecb0bb5..a6155211e25 100644
--- a/website/docs/developer-guide/context-compression-and-caching.md
+++ b/website/docs/developer-guide/context-compression-and-caching.md
@@ -504,7 +504,7 @@ This is a cheap pre-pass that saves significant tokens from verbose tool
outputs (file contents, terminal output, search results).
A tool round the model has not answered yet (compaction fired right after it ran, with or
-without a `/steer` delivered after it) keeps its text results verbatim in the tail, so the model
+without `/steer` messages delivered after it) keeps its text results verbatim in the tail, so the model
does not re-run the call or answer without its output. The one exception is a round that alone
exceeds 20% of the input budget (the context window minus the output reservation): it is
summarized so compaction can still make room.