Files
hermes-agent/agent/compression_marker.py
kshitijk4poor 159f716313 refactor(agent): derive the pruned-marker matcher from the compressor template
The dispatch-boundary detector re-typed the marker's wording next to the
producer's template and hid the import behind functools.lru_cache, citing a
context_compressor -> prompt_builder -> tool_dispatch_helpers cycle that does
not exist (prompt_builder only mentions this module in a comment; both import
orders succeed). Two copies of one string means a template edit silently
disables the guard.

Move the prefix/template into a dependency-free leaf, agent/compression_marker,
and build the regex from the template's first sentence with the count
placeholders swapped for \d[\d,]*. context_compressor re-exports the names, so
its callers and tests are unchanged. The matcher keeps the intended behaviour:
prefix-only mentions do not match; a minted marker, flat or nested, does. The
old \s+ tolerance for multiple spaces is dropped because the producer never
emits one, so only a template-shaped copy counts.

tool_dispatch_helpers stays light: importing it alone still does not load
context_compressor (auxiliary_client, context_engine, ...).
2026-09-24 16:29:19 +05:30

32 lines
1.5 KiB
Python

"""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,]*")
)