The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(context-compressor): match the marker by position, and leave args byte-identical
kshitijk4poora5381a7dca7c
diff --git a/agent/context_compressor.py b/agent/context_compressor.pyindex 425aa316f95..d2872e1fea6 100644--- a/agent/context_compressor.py+++ b/agent/context_compressor.py@@ -1362,23 +1362,35 @@ _COMPRESSION_MARKER_TEMPLATE = (def _truncate_tool_call_args_json(args: str, head_chars: int = 200) -> str:- """Shrink long string leaves in a tool-call arguments JSON blob, keeping it valid (providers 400 on malformed args)."""+ """Shrink long string leaves in a tool-call arguments JSON blob, keeping it valid (providers 400 on malformed args).++ Only leaves where the replacement is a net reduction are changed (``head_chars`` plus the+ marker, ~420 chars); the input string is returned unchanged when nothing was replaced.+ """try:parsed = json.loads(args)except (ValueError, TypeError):return args+ changed = False+def _shrink(obj: Any) -> Any:+ nonlocal changedif isinstance(obj, str):- if len(obj) <= head_chars or _COMPRESSION_MARKER_PREFIX in obj:+ # 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):return objmarker = _COMPRESSION_MARKER_TEMPLATE.format(omitted=len(obj) - head_chars, total=len(obj))- # Only replace when it actually reclaims bytes: for a leaf just over the cap the marker- # is longer than what it replaces, and re-shrinking an already-marked leaf would rewrite- # its counts (which the marker's anti-imitation value depends on) on every compaction.- return obj[:head_chars] + marker if head_chars + len(marker) < len(obj) else obj+ # Only replace when it reclaims bytes: for a leaf just over the cap the marker is+ # longer than what it replaces.+ if head_chars + len(marker) >= len(obj):+ return obj+ changed = True+ return obj[:head_chars] + markerif isinstance(obj, dict):return {k: _shrink(v) for k, v in obj.items()}if isinstance(obj, list):@@ -1386,6 +1398,10 @@ def _truncate_tool_call_args_json(args: str, head_chars: int = 200) -> str:return objshrunken = _shrink(parsed)+ # Re-serialising alone would rewrite the caller's bytes (compact wire JSON gains spaces),+ # which the callers read as "this message changed" and count as reclaimed pressure.+ if not changed:+ return args# ensure_ascii=False keeps CJK/emoji from bloating into \uXXXXreturn json.dumps(shrunken, ensure_ascii=False)diff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.pyindex 638c67a53be..a50cf31946d 100644--- a/tests/agent/test_context_compressor.py+++ b/tests/agent/test_context_compressor.py@@ -12,6 +12,7 @@ from agent.context_compressor import (HISTORICAL_TASK_HEADING,SUMMARY_PREFIX,COMPRESSED_SUMMARY_METADATA_KEY,+ _COMPRESSION_MARKER_PREFIX,_COMPRESSION_MARKER_TEMPLATE,_PRUNE_MIN_CHARS,_summarize_tool_result,@@ -2364,17 +2365,18 @@ class TestTruncateToolCallArgsJson:def test_shrunken_args_remain_valid_json(self):import json as _jsonshrink = self._helper()+ content = "# Shopping Browser Setup Notes\n\n" + "abc " * 400original = _json.dumps({"path": "~/.hermes/skills/shopping/browser-setup-notes.md",- "content": "# Shopping Browser Setup Notes\n\n" + "abc " * 400,+ "content": content,})assert len(original) > 500shrunk = shrink(original)parsed = _json.loads(shrunk) # must not raiseassert parsed["path"] == "~/.hermes/skills/shopping/browser-setup-notes.md"- # Head preserved, marker appended (not substituted for the leaf's own text).- assert parsed["content"].startswith("# Shopping Browser Setup Notes\n\n"[:200])- assert parsed["content"].endswith("⟫")+ # Head preserved, marker appended at the cut (not substituted for the leaf's own text).+ assert parsed["content"].startswith(content[:200])+ assert parsed["content"][200:].startswith(_COMPRESSION_MARKER_PREFIX)assert len(shrunk) < len(original)@@ -2396,7 +2398,7 @@ class TestTruncateToolCallArgsJson:assert parsed["timeout"] is Noneassert parsed["items"] == [1, 2, 3]assert parsed["note"].startswith("z" * 200)- assert parsed["note"].endswith("⟫")+ assert parsed["note"][200:].startswith(_COMPRESSION_MARKER_PREFIX)@@ -2435,7 +2437,7 @@ class TestTruncateToolCallArgsJson:parsed = _json.loads(shrunk)assert parsed["path"] == "~/.hermes/skills/shopping/browser-setup-notes.md"assert parsed["content"].startswith(huge_content[:200])- assert parsed["content"].endswith("⟫")+ assert parsed["content"][200:].startswith(_COMPRESSION_MARKER_PREFIX)class TestTruncationMarkerNotImitable:@@ -2449,23 +2451,31 @@ class TestTruncationMarkerNotImitable:"""def test_old_bare_marker_no_longer_produced(self):- """The exact literal that caused #83714 must never come out of the shrink helper again."""+ """The literal that caused #83714 must never come out of the shrink helper again."""payload = json.dumps({"path": "/f.py", "new_string": "y" * 600})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)- def test_shrink_only_replaces_when_it_reclaims_and_never_re_shrinks(self):- """Shrinking is a one-way, size-reducing rewrite of a leaf.+ 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.- Every replayed leaf must lose bytes (a leaf just over the cap used to- *grow*, because the marker is longer than the text it replaces), keep a- 200-char head plus the marker with true counts, and be a fixed point: a- second pass over already-shrunk args must not rewrite the counts.+ Below the break-even (``head_chars`` + marker) replacing a leaf would grow the payload, and+ re-serialising alone would rewrite compact wire JSON — both read as "this changed" upstream+ and are counted as reclaimed pressure."""- # Just over the cap: replacing here would be a net gain of characters.tiny = json.dumps({"new_string": "y" * 201, "pad": "z" * 320})assert _truncate_tool_call_args_json(tiny) == tiny+ compact = json.dumps({"new_string": "y" * 201, "pad": "z" * 320}, separators=(",", ":"))+ assert _truncate_tool_call_args_json(compact) == compact+ # 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)++ 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."""payload = json.dumps({"content": "x" * 2000})once = _truncate_tool_call_args_json(payload)assert len(once) < len(payload)