The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(context-compressor): the marked-leaf guard must match the whole tail
kshitijk4poor39abfdf4ecd4
diff --git a/agent/context_compressor.py b/agent/context_compressor.pyindex d2872e1fea6..2ac79d2d29a 100644--- a/agent/context_compressor.py+++ b/agent/context_compressor.py@@ -1377,10 +1377,11 @@ def _truncate_tool_call_args_json(args: str, head_chars: int = 200) -> str:def _shrink(obj: Any) -> Any:nonlocal changedif isinstance(obj, str):- # Already marked: the compressor always writes the marker at ``head_chars``, so test- # that exact shape — a leaf that merely mentions the marker must still shrink. Never- # re-apply it (the counts are the marker's anti-imitation value, see header).- if len(obj) <= head_chars or obj.startswith(_COMPRESSION_MARKER_PREFIX, head_chars):+ # Already marked: the compressor writes the head and the marker as the whole tail, so+ # key on that shape. A substring/prefix test alone would exempt a leaf that merely+ # quotes the marker — including the imitation #83714 is about — from shrinking forever.+ marked = obj.startswith(_COMPRESSION_MARKER_PREFIX, head_chars) and obj.endswith("⟫")+ if len(obj) <= head_chars or marked:return objmarker = _COMPRESSION_MARKER_TEMPLATE.format(omitted=len(obj) - head_chars, total=len(obj)@@ -1403,7 +1404,8 @@ def _truncate_tool_call_args_json(args: str, head_chars: int = 200) -> str:if not changed:return args# ensure_ascii=False keeps CJK/emoji from bloating into \uXXXX- return json.dumps(shrunken, ensure_ascii=False)+ out = json.dumps(shrunken, ensure_ascii=False)+ return out if len(out) < len(args) else args_IMAGE_PART_TYPES = frozenset({"image_url", "input_image", "image"})diff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.pyindex a50cf31946d..ebc3eef1eb7 100644--- a/tests/agent/test_context_compressor.py+++ b/tests/agent/test_context_compressor.py@@ -2456,10 +2456,10 @@ class TestTruncationMarkerNotImitable:shrunk = json.loads(_truncate_tool_call_args_json(payload))["new_string"]assert "...[truncated]" not in shrunk# ...and the leaf really was shrunk, so a no-op helper cannot pass this.- assert shrunk == "y" * 200 + _COMPRESSION_MARKER_TEMPLATE.format(omitted=400, total=600)+ assert len(shrunk) < 600 and shrunk.startswith("y" * 200)def test_args_without_a_net_gain_leaf_are_left_byte_identical(self):- """A leaf the marker would not shrink, and a leaf that merely mentions the marker.+ """Leaves the marker would not shrink, and leaves that merely quote the marker.Below the break-even (``head_chars`` + marker) replacing a leaf would grow the payload, andre-serialising alone would rewrite compact wire JSON — both read as "this changed" upstream@@ -2469,10 +2469,22 @@ class TestTruncationMarkerNotImitable:assert _truncate_tool_call_args_json(tiny) == tinycompact = json.dumps({"new_string": "y" * 201, "pad": "z" * 320}, separators=(",", ":"))assert _truncate_tool_call_args_json(compact) == compact+ # Separator whitespace added by the re-serialise can exceed a single leaf's saving.+ many_keys = json.dumps(+ {**{f"k{i}": i for i in range(300)}, "big": "y" * 426}, separators=(",", ":")+ )+ assert _truncate_tool_call_args_json(many_keys) == many_keys- # The guard keys on the marker's POSITION, so an imitated marker mid-leaf still shrinks.- imitated = json.dumps({"new_string": "x" * 1000 + _COMPRESSION_MARKER_PREFIX + " 5 of 9⟫" + "y" * 500})- assert len(_truncate_tool_call_args_json(imitated)) < len(imitated)+ # The guard keys on the marker being the whole tail, so the imitation shape #83714+ # describes — replayed head+marker followed by new content — is still shrinkable.+ for leaf in (+ "x" * 1000 + _COMPRESSION_MARKER_PREFIX + " 5 of 9⟫" + "y" * 500,+ "x" * 200 + _COMPRESSION_MARKER_PREFIX + " 5 of 9 chars omitted⟫" + "y" * 5000,+ ):+ out = _truncate_tool_call_args_json(json.dumps({"new_string": leaf}))+ assert json.loads(out)["new_string"] == "x" * 200 + _COMPRESSION_MARKER_TEMPLATE.format(+ omitted=len(leaf) - 200, total=len(leaf)+ )def test_shrunken_leaf_is_head_plus_marker_and_a_fixed_point(self):"""Re-shrinking must be a no-op: the marker's counts are its anti-imitation value."""