compression_marker.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """The model-visible compression/elision marker and its matcher.
  2. Tool-call arguments are no longer rewritten by the compressor, but legacy sessions may still
  3. carry this marker inside replayed tool-call arguments. Dependency-free leaf shared by the
  4. elision renderers (``agent.context_compressor``) and the dispatch-boundary detector
  5. (``agent.tool_dispatch_helpers``), so the matcher is derived from the template instead of
  6. re-typing its wording.
  7. """
  8. from __future__ import annotations
  9. import re
  10. # #83714 — this text lands inside the model's OWN replayed tool call, so it must not read like
  11. # something the model would write itself: the bare "...[truncated]" it replaced was imitated into
  12. # new calls and written to disk. Non-prose delimiters, an explicit "not original content"
  13. # disclaimer, and per-instance counts keep a copied marker visibly wrong; the counts also make a
  14. # verbatim copy stale, which is why the marker must never be re-applied.
  15. _COMPRESSION_MARKER_PREFIX = "⟪HERMES-CONTEXT-COMPRESSION:"
  16. _COMPRESSION_MARKER_TEMPLATE = (
  17. _COMPRESSION_MARKER_PREFIX
  18. + " {omitted:,} of {total:,} chars omitted here by Hermes's context compressor. "
  19. "This is NOT part of the original tool call and must never be reproduced in new "
  20. "output — always write full, untruncated content.⟫"
  21. )
  22. # A rendered omitted/total count inside a minted marker.
  23. _MARKER_COUNT_RE = r"\d[\d,]*"
  24. # Full rendered first sentence of a minted marker; renderer tests assert this shape
  25. # (the dispatch guard uses _COMPRESSION_MARKER_ARTIFACT_RE below).
  26. _COMPRESSION_MARKER_RE = re.compile(
  27. re.escape(_COMPRESSION_MARKER_TEMPLATE.split(". ", 1)[0] + ".")
  28. .replace(re.escape("{omitted:,}"), _MARKER_COUNT_RE)
  29. .replace(re.escape("{total:,}"), _MARKER_COUNT_RE)
  30. )
  31. # A marker cut by a later boundary may never reach the fixed sentence above. The
  32. # dispatch guard therefore recognizes a minted marker as soon as its first numeric
  33. # count is present. Requiring that rendered count keeps the bare prefix/template
  34. # editable in source and documentation.
  35. _COMPRESSION_MARKER_ARTIFACT_RE = re.compile(
  36. re.escape(_COMPRESSION_MARKER_PREFIX) + r"\s+" + _MARKER_COUNT_RE
  37. )
  38. # #121548 — every OTHER model-visible elision (turn text, summaries, skill bodies, diagnostics)
  39. # mints the args marker's first sentence only (so the dispatch-boundary artifact
  40. # matcher catches a copy from any renderer) and fits small caps like clarify's 199.
  41. _ELISION_MARKER_TEMPLATE = _COMPRESSION_MARKER_TEMPLATE.split(". ", 1)[0] + ".⟫"
  42. def _elision_marker(omitted: int, total: int) -> str:
  43. """Render the non-imitable elision marker with per-instance byte counts."""
  44. return _ELISION_MARKER_TEMPLATE.format(omitted=omitted, total=total)
  45. # Widest marker for any text under ~1 TB: callers with a small leftover budget skip the item
  46. # when the budget cannot hold the marker plus content, instead of emitting a marker-only line.
  47. ELISION_MARKER_MAX_LEN = len(_elision_marker(omitted=10**12 - 1, total=10**12 - 1))
  48. def elide(text: str, limit: int) -> str:
  49. """Cap ``text`` at ``limit`` chars, marking the elision with ``_elision_marker``.
  50. Every model-visible renderer must truncate through here (#121548). ``text`` is
  51. returned unchanged when it already fits. Otherwise the result is ``head + marker``
  52. with accurate omitted/total counts and never exceeds ``limit`` (sized against the
  53. widest rendering the counts can take), unless ``limit`` cannot hold the marker at
  54. all, in which case the marker alone is returned.
  55. """
  56. if len(text) <= limit:
  57. return text
  58. total = len(text)
  59. head_len = limit - len(_elision_marker(omitted=total, total=total))
  60. if head_len <= 0:
  61. return _elision_marker(omitted=total, total=total)
  62. kept = text[:head_len].rstrip()
  63. return kept + _elision_marker(omitted=total - len(kept), total=total)
  64. def elide_middle(text: str, head: int, tail: int) -> str:
  65. """Keep ``head`` chars from the start and ``tail`` from the end, eliding the middle."""
  66. if len(text) <= head + tail:
  67. return text
  68. marker = _elision_marker(omitted=len(text) - head - tail, total=len(text))
  69. return text[:head] + marker + text[len(text) - tail:]