Skip to content

The agent that grows with you

test(context): pin byte-exact old tool-call args with one red-on-base test

kshitijk4poor697f07a56fea
diff --git a/tests/agent/test_context_compression_canonical_state.py b/tests/agent/test_context_compression_canonical_state.py
deleted file mode 100644
index 65373e3b12d..00000000000
--- a/tests/agent/test_context_compression_canonical_state.py
+++ /dev/null
@@ -1,87 +0,0 @@
-"""Regression coverage for #122559 canonical compression state."""
-
-from __future__ import annotations
-
-import json
-from unittest.mock import patch
-
-from agent.compression_marker import _COMPRESSION_MARKER_PREFIX
-from agent.context_compressor import ContextCompressor
-
-
-def _call(call_id: str, payload: str) -> dict:
- return {
- "role": "assistant",
- "content": "",
- "tool_calls": [{
- "id": call_id,
- "type": "function",
- "function": {
- "name": "write_file",
- "arguments": json.dumps({"path": "/tmp/x.py", "content": payload}),
- },
- }],
- }
-
-
-def _messages() -> list[dict]:
- return [
- {"role": "user", "content": "head request"},
- _call("head_call", "A" * 4_000),
- {"role": "tool", "tool_call_id": "head_call", "content": "R" * 10_000},
- {"role": "user", "content": "middle 1"},
- {"role": "assistant", "content": "middle answer 1"},
- {"role": "user", "content": "middle 2"},
- {"role": "assistant", "content": "middle answer 2"},
- {"role": "user", "content": "tail request"},
- {"role": "assistant", "content": "tail answer"},
- ]
-
-
-def _compressor() -> ContextCompressor:
- return ContextCompressor(
- model="test/model",
- config_context_length=200_000,
- protect_first_n=3,
- protect_last_n=2,
- quiet_mode=True,
- tail_mode="legacy",
- )
-
-
-def test_structural_noop_returns_canonical_tool_arguments() -> None:
- compressor = _compressor()
- messages = _messages()
- original_args = messages[1]["tool_calls"][0]["function"]["arguments"]
-
- with patch.object(compressor, "_compress_window", return_value=(3, 3)):
- result = compressor.compress(messages, current_tokens=100_000)
-
- assert result[1]["tool_calls"][0]["function"]["arguments"] == original_args
- assert _COMPRESSION_MARKER_PREFIX not in json.dumps(result, ensure_ascii=False)
-
-
-def test_successful_compaction_carries_canonical_head_not_pruned_working_copy() -> None:
- compressor = _compressor()
- messages = _messages()
- original_args = messages[1]["tool_calls"][0]["function"]["arguments"]
- original_result = messages[2]["content"]
-
- with (
- patch.object(compressor, "_compress_window", return_value=(3, 7)),
- patch.object(compressor, "_generate_summary", return_value="Summary of middle turns."),
- ):
- result = compressor.compress(messages, current_tokens=100_000, force=True)
-
- head_call = next(
- message for message in result
- if message.get("role") == "assistant"
- and any(call.get("id") == "head_call" for call in message.get("tool_calls", []))
- )
- head_tool = next(
- message for message in result
- if message.get("role") == "tool" and message.get("tool_call_id") == "head_call"
- )
- assert head_call["tool_calls"][0]["function"]["arguments"] == original_args
- assert head_tool["content"] == original_result
- assert _COMPRESSION_MARKER_PREFIX not in head_call["tool_calls"][0]["function"]["arguments"]
diff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.py
index ace2fd9d3b8..00f4de68719 100644
--- a/tests/agent/test_context_compressor.py
+++ b/tests/agent/test_context_compressor.py
@@ -2291,79 +2291,35 @@ class TestThresholdTokensCap:
class TestHistoricalToolCallArgumentsStayCanonical:
- """Regression coverage for #122559: compression may summarize results, never executable args."""
+ """#122559: pruning may summarize tool results but never rewrites old tool-call arguments."""
- @staticmethod
- def _compressor():
- return ContextCompressor(
- model="test/model",
- config_context_length=100_000,
- threshold_percent=0.85,
- protect_first_n=1,
- protect_last_n=2,
- quiet_mode=True,
- )
-
- @staticmethod
- def _write_call(call_id: str, arguments: str) -> dict:
- return {
- "role": "assistant",
- "content": None,
- "tool_calls": [{
- "id": call_id,
- "type": "function",
- "function": {"name": "write_file", "arguments": arguments},
- }],
- }
-
- def test_ordinary_prune_keeps_effectful_arguments_byte_exact(self):
- c = self._compressor()
- arguments = json.dumps(
- {"path": "/tmp/app.py", "content": "x" * 4_000},
- separators=(",", ":"),
- )
- messages = [
- {"role": "user", "content": "write it"},
- self._write_call("call_1", arguments),
- {"role": "tool", "tool_call_id": "call_1", "content": "R" * 10_000},
- {"role": "user", "content": "continue"},
- {"role": "assistant", "content": "done"},
- ]
-
- result, pruned = c._prune_old_tool_results(
- messages, protect_tail_count=2, min_prune_chars=1_000,
- )
-
- assert pruned >= 1
- assert result[1]["tool_calls"][0]["function"]["arguments"] == arguments
- assert "HERMES-CONTEXT-COMPRESSION" not in json.dumps(result, ensure_ascii=False)
-
- def test_pressure_demotion_keeps_protected_arguments_byte_exact(self):
- c = self._compressor()
- arguments = json.dumps(
- {"path": "/tmp/app.py", "content": "y" * 8_000},
- separators=(",", ":"),
+ def test_old_large_arguments_survive_prune_byte_exact(self):
+ c = ContextCompressor(
+ model="test/model", config_context_length=200_000, protect_first_n=1,
+ protect_last_n=2, quiet_mode=True, tail_mode="legacy",
)
- messages = [
- {"role": "user", "content": "write it"},
- self._write_call("call_1", arguments),
- {"role": "tool", "tool_call_id": "call_1", "content": "R" * 12_000},
- {"role": "user", "content": "follow up"},
- {"role": "assistant", "content": "ok"},
- {"role": "user", "content": "again"},
- {"role": "assistant", "content": "done"},
- ]
+ messages = [{"role": "user", "content": "go"}]
+ originals = {}
+ for k in range(6):
+ args = json.dumps({"path": "a.py", "content": "X" * 3000})
+ originals[f"c{k}"] = args
+ messages += [
+ {"role": "assistant", "content": "", "tool_calls": [{
+ "id": f"c{k}", "type": "function",
+ "function": {"name": "write_file", "arguments": args},
+ }]},
+ {"role": "tool", "tool_call_id": f"c{k}", "content": "ok"},
+ {"role": "user", "content": f"u{k}"},
+ ]
+ messages.append({"role": "assistant", "content": "done"})
- result, pruned = c._prune_old_tool_results(
- messages,
- protect_tail_count=6,
- protect_tail_tokens=100,
- min_prune_chars=1_000,
- )
+ result, _ = c._prune_old_tool_results(messages, protect_tail_count=2)
- assert pruned >= 1
- assert result[1]["tool_calls"][0]["function"]["arguments"] == arguments
- assert "HERMES-CONTEXT-COMPRESSION" not in json.dumps(result, ensure_ascii=False)
+ seen = {
+ m["tool_calls"][0]["id"]: m["tool_calls"][0]["function"]["arguments"]
+ for m in result if m.get("tool_calls")
+ }
+ assert seen == originals
class TestLazyContextResolution: