Skip to content

The agent that grows with you

refactor(agent): derive the pruned-marker matcher from the compressor template

kshitijk4poor159f71631358
diff --git a/agent/compression_marker.py b/agent/compression_marker.py
new file mode 100644
index 00000000000..b2a4a292790
--- /dev/null
+++ b/agent/compression_marker.py
@@ -0,0 +1,31 @@
+"""The model-visible marker the context compressor leaves in pruned tool-call arguments.
+
+Dependency-free leaf shared by the producer (``agent.context_compressor``) and the
+dispatch-boundary detector (``agent.tool_dispatch_helpers``), so the matcher is derived
+from the template instead of re-typing its wording.
+"""
+
+from __future__ import annotations
+
+import re
+
+# #83714 — this text lands inside the model's OWN replayed tool call, so it must not read like
+# something the model would write itself: the bare "...[truncated]" it replaced was imitated into
+# new calls and written to disk. Non-prose delimiters, an explicit "not original content"
+# disclaimer, and per-instance counts keep a copied marker visibly wrong; the counts also make a
+# verbatim copy stale, which is why the marker must never be re-applied (see ``_shrink``).
+_COMPRESSION_MARKER_PREFIX = "⟪HERMES-CONTEXT-COMPRESSION:"
+_COMPRESSION_MARKER_TEMPLATE = (
+ _COMPRESSION_MARKER_PREFIX
+ + " {omitted:,} of {total:,} chars omitted here by Hermes's context compressor. "
+ "This is NOT part of the original tool call and must never be reproduced in new "
+ "output — always write full, untruncated content.⟫"
+)
+
+# A minted marker (prefix + rendered counts through the first sentence). The prefix alone
+# does not match, so source/docs that mention the constant can still be edited.
+_COMPRESSION_MARKER_RE = re.compile(
+ re.escape(_COMPRESSION_MARKER_TEMPLATE.split(". ", 1)[0] + ".")
+ .replace(re.escape("{omitted:,}"), r"\d[\d,]*")
+ .replace(re.escape("{total:,}"), r"\d[\d,]*")
+)
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 99add9a9ae3..17c64cf3000 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -15,6 +15,7 @@ from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Sequence, Tuple
from agent.image_eviction_policy import outbound_image_retire_count
+from agent.compression_marker import _COMPRESSION_MARKER_PREFIX, _COMPRESSION_MARKER_TEMPLATE
from agent.auxiliary_client import (
AuxiliaryExplicitCancellation,
_coerce_llm_message,
@@ -1479,20 +1480,6 @@ def evict_stale_outbound_tool_images(api_messages: List[Dict[str, Any]]) -> int:
return pruned
-# #83714 — this text lands inside the model's OWN replayed tool call, so it must not read like
-# something the model would write itself: the bare "...[truncated]" it replaced was imitated into
-# new calls and written to disk. Non-prose delimiters, an explicit "not original content"
-# disclaimer, and per-instance counts keep a copied marker visibly wrong; the counts also make a
-# verbatim copy stale, which is why the marker must never be re-applied (see ``_shrink``).
-_COMPRESSION_MARKER_PREFIX = "⟪HERMES-CONTEXT-COMPRESSION:"
-_COMPRESSION_MARKER_TEMPLATE = (
- _COMPRESSION_MARKER_PREFIX
- + " {omitted:,} of {total:,} chars omitted here by Hermes's context compressor. "
- "This is NOT part of the original tool call and must never be reproduced in new "
- "output — always write full, untruncated content.⟫"
-)
-
-
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).
diff --git a/agent/tool_dispatch_helpers.py b/agent/tool_dispatch_helpers.py
index 3221e6f390f..d93c969e1bb 100644
--- a/agent/tool_dispatch_helpers.py
+++ b/agent/tool_dispatch_helpers.py
@@ -9,7 +9,6 @@ normalisation, and the tool-result message constructor with untrusted-content wr
from __future__ import annotations
-import functools
import json
import logging
import os
@@ -17,6 +16,7 @@ import re
from pathlib import Path
from typing import Any, Dict, List, Optional
+from agent.compression_marker import _COMPRESSION_MARKER_RE
from agent.message_metadata import stamp_message_timestamp
from agent.tool_result_classification import (
FILE_MUTATING_TOOL_NAMES as _FILE_MUTATING_TOOLS,
@@ -69,21 +69,6 @@ _DESTRUCTIVE_PATTERNS = re.compile(
# Output redirects that overwrite files (> but not >>)
_REDIRECT_OVERWRITE = re.compile(r'[^>]>[^>]|^>[^>]')
[email protected]_cache(maxsize=1)
-def _context_pruned_marker_re() -> re.Pattern[str]:
- """Producer-shaped regex for the compressor's model-visible prune marker.
-
- Imported lazily: ``agent.context_compressor`` -> ``agent.prompt_builder`` ->
- this module, so a module-level import would be circular. Compiled once.
- """
- from agent.context_compressor import _COMPRESSION_MARKER_PREFIX
-
- return re.compile(
- re.escape(_COMPRESSION_MARKER_PREFIX)
- + r"\s+\d[\d,]* of \d[\d,]* chars omitted here by Hermes's context compressor\."
- )
-
-
def _context_pruned_argument_paths(tool_name: str, args: Any) -> list[str]:
"""Paths whose values contain model-visible context-compression artifacts.
@@ -96,12 +81,11 @@ def _context_pruned_argument_paths(tool_name: str, args: Any) -> list[str]:
if not tool_may_have_side_effect(tool_name):
return []
- marker_re = _context_pruned_marker_re()
found: list[str] = []
def _walk(value: Any, path: str) -> None:
if isinstance(value, str):
- if marker_re.search(value):
+ if _COMPRESSION_MARKER_RE.search(value):
found.append(path)
return
if isinstance(value, dict):