conversation_compression_manual.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """Manual ``/compress`` core shared by the CLI, gateway, TUI and ACP surfaces.
  2. Manual compression is the ONE sanctioned history mutation (prompt-cache invariant): each surface parses
  3. its own flags and renders its own text, but the sequence — split for ``here [N]``, estimate, run
  4. ``agent._compress_context(force=True)``, detect a lock-skip, rejoin the verbatim tail, summarize — lives
  5. here so ``--preview`` / ``--aggressive`` and the lock-skip wording cannot drift per surface again.
  6. """
  7. from __future__ import annotations
  8. from dataclasses import dataclass, field
  9. from typing import Any, Callable, Dict, List, Optional, Sequence
  10. #: Every surface renders the same refusal; hard truncation has no persistence path outside the guarded
  11. #: ``_compress_context`` rotation, so ``--aggressive`` is refused rather than mis-parsed as a focus topic.
  12. AGGRESSIVE_UNSUPPORTED = (
  13. "--aggressive is not supported; use '/compress here [N]' to keep only recent exchanges, "
  14. "or /undo to drop turns.")
  15. MIN_MESSAGES = 4
  16. @dataclass
  17. class CompressRequest:
  18. """Parsed ``/compress`` arguments (``extract_compress_flags`` + ``parse_partial_compress_args``)."""
  19. preview: bool = False
  20. aggressive: bool = False
  21. partial: bool = False
  22. keep_last: int = 2
  23. focus_topic: Optional[str] = None
  24. @dataclass
  25. class CompressResult:
  26. status: str # "preview" | "compressed" | "lock_skipped" | "nothing_to_do"
  27. before_messages: list[dict[str, Any]]
  28. after_messages: list[dict[str, Any]]
  29. before_tokens: int
  30. after_tokens: int
  31. request: CompressRequest
  32. lines: list[str] = field(default_factory=list) # preview report lines (status == "preview")
  33. lock_holder: Any = None
  34. summary: Optional[dict[str, Any]] = None # ``summarize_manual_compression`` payload when compressed
  35. @property
  36. def removed(self) -> int:
  37. return len(self.before_messages) - len(self.after_messages)
  38. def parse_compress_args(raw_args: str) -> CompressRequest:
  39. """One parser for every surface: flags anywhere, then the boundary-aware / focus positional forms."""
  40. from hermes_cli.partial_compress import extract_compress_flags, parse_partial_compress_args
  41. rest, preview, aggressive = extract_compress_flags((raw_args or "").strip())
  42. partial, keep_last, focus_topic = parse_partial_compress_args(rest)
  43. return CompressRequest(preview=preview, aggressive=aggressive, partial=partial, keep_last=keep_last,
  44. focus_topic=focus_topic or None)
  45. def estimate_request_tokens(agent: Any, messages: Sequence[dict[str, Any]]) -> int:
  46. """Transcript + system prompt + tool schemas: a transcript-only figure understates real request pressure
  47. and can even appear to grow after a dense handoff summary replaces many short turns (#6217)."""
  48. from agent.model_metadata import estimate_request_tokens_rough
  49. if not messages:
  50. return 0
  51. return estimate_request_tokens_rough(
  52. list(messages), system_prompt=getattr(agent, "_cached_system_prompt", "") or "",
  53. tools=getattr(agent, "tools", None) or None)
  54. def compress_now(
  55. agent: Any, history: Sequence[dict[str, Any]], request: CompressRequest, *,
  56. system_message: Any = None, task_id: str = "default", skip_without_window: bool = False,
  57. snapshot_is_current: Optional[Callable[[], bool]] = None,
  58. ) -> CompressResult:
  59. """Run one manual compression of ``history`` on ``agent`` and return the outcome; the caller installs
  60. ``after_messages`` (and re-anchors session ids) — history is never mutated here.
  61. ``snapshot_is_current`` validates the host's snapshot after durable lease admission, before the
  62. summarizer runs; the normal timeout/fallback pipeline remains in charge of the attempt.
  63. ``preview=True`` performs no compression and leaves ``agent`` untouched. A held compression lock
  64. yields ``lock_skipped`` with the agent's signal cleared and the deferred context-engine notification
  65. discarded; otherwise the caller must call ``finalize_context_engine_compression_notification(agent,
  66. committed=True)`` once its own history transaction commits (``committed=False`` on failure).
  67. ``system_message=None`` makes ``_compress_context`` rebuild the prompt; passing the cached prompt
  68. duplicated the identity block (#15281). ``skip_without_window`` (gateway) answers ``nothing_to_do``
  69. when the local compressor sees no summarizable middle; the in-process surfaces leave it off because
  70. ``_compress_context`` still does useful work there — codex_app_server native compaction, and the
  71. phase-1 tool-result prune / blank-echo drop that ``ContextCompressor.compress`` commits even when no
  72. summary window exists."""
  73. from agent.context_compressor import _DB_PERSISTED_MARKER, _fresh_compaction_message_copy
  74. from agent.conversation_compression import finalize_context_engine_compression_notification
  75. from agent.manual_compression_feedback import summarize_manual_compression
  76. from hermes_cli.partial_compress import (
  77. rejoin_compressed_head_and_tail, split_history_for_partial_compress, summarize_compress_preview)
  78. before = list(history)
  79. before_tokens = estimate_request_tokens(agent, before)
  80. head, tail = before, []
  81. if request.partial:
  82. head, tail = split_history_for_partial_compress(before, request.keep_last)
  83. if not tail: # degenerate split: nothing to keep verbatim → full compression
  84. head = before
  85. if request.preview:
  86. report = summarize_compress_preview(before, request.partial, request.keep_last, request.focus_topic, before_tokens)
  87. return CompressResult("preview", before, before, before_tokens, before_tokens, request, lines=report["lines"])
  88. compressor = getattr(agent, "context_compressor", None)
  89. has_content = getattr(compressor, "has_content_to_compress", None)
  90. if skip_without_window and callable(has_content) and has_content(head) is False:
  91. return CompressResult("nothing_to_do", before, before, before_tokens, before_tokens, request)
  92. # An in-place commit archives every durable row under the lease watermark, the kept tail's included, so
  93. # it must store the tail again itself. It gets copies because the insert writes row ids onto them. A copy
  94. # keeps its source's row id only while the source is unchanged since it was loaded (marker present): a
  95. # resume merge rewrote the row and dropped the marker, and its id no longer bounds what the row holds.
  96. tail_rows = []
  97. for m in tail:
  98. row = _fresh_compaction_message_copy(m)
  99. if not m.get(_DB_PERSISTED_MARKER):
  100. # A rewritten row's id does not bound the archive, but it and any row a merge
  101. # folded into it were still in the compressor's input. Keep them named so the
  102. # commit archives those originals instead of cloning them beside the tail.
  103. dropped = row.pop("_row_id", None)
  104. if isinstance(dropped, int) and not isinstance(dropped, bool) and dropped > 0:
  105. absorbed = [*(row.get("_absorbed_row_ids") or ())]
  106. if dropped not in absorbed:
  107. absorbed.append(dropped)
  108. row["_absorbed_row_ids"] = absorbed
  109. tail_rows.append(row)
  110. try:
  111. compressed, _ = agent._compress_context(
  112. head, system_message, approx_tokens=before_tokens, focus_topic=request.focus_topic, force=True,
  113. defer_context_engine_notification=True, **({"task_id": task_id} if task_id != "default" else {}),
  114. **({"snapshot_is_current": snapshot_is_current} if snapshot_is_current is not None else {}),
  115. **({"verbatim_tail": tail_rows} if tail_rows else {}))
  116. except Exception:
  117. finalize_context_engine_compression_notification(agent, committed=False)
  118. raise
  119. # Type-pinned (is True / str): bare truthiness is fooled by MagicMock auto-attributes on test doubles.
  120. lock_signal = getattr(agent, "_compression_skipped_due_to_lock", None)
  121. if lock_signal is True or isinstance(lock_signal, str):
  122. agent._compression_skipped_due_to_lock = None
  123. finalize_context_engine_compression_notification(agent, committed=False)
  124. return CompressResult("lock_skipped", before, before, before_tokens, before_tokens, request,
  125. lock_holder=lock_signal if isinstance(lock_signal, str) else None)
  126. # Stamped copies mean the commit (in place or rotated) stored the tail and already returned head + tail.
  127. # A no-op or a rolled-back commit leaves them unstamped, and the tail is then only in the caller's dicts.
  128. if tail and not all(row.get(_DB_PERSISTED_MARKER) is True for row in tail_rows):
  129. compressed = rejoin_compressed_head_and_tail(compressed, tail)
  130. after_tokens = estimate_request_tokens(agent, compressed)
  131. # A gateway-backed agent compresses on its owner, whose compressor holds the abort/refusal/reason
  132. # flags: the owner's report is the result. Type-pinned (dict) for the MagicMock reason above.
  133. summary = getattr(agent, "compression_summary", None)
  134. if not isinstance(summary, dict):
  135. summary = summarize_manual_compression(before, compressed, before_tokens, after_tokens,
  136. compression_state=compressor)
  137. return CompressResult("compressed", before, list(compressed), before_tokens, after_tokens, request, summary=summary)
  138. def render_compress_result(result: CompressResult, *, prefix: str = "") -> list[str]:
  139. """Surface-neutral text lines for a result (each surface may add its own icon/prefix)."""
  140. if result.status == "preview":
  141. return [f"{prefix}{line}" for line in result.lines]
  142. if result.status == "lock_skipped":
  143. from agent.manual_compression_feedback import describe_compression_lock_skip
  144. return [f"{prefix}{describe_compression_lock_skip(result.lock_holder or True)}"]
  145. if result.status == "nothing_to_do":
  146. return [f"{prefix}Nothing to compress yet."]
  147. summary = result.summary or {}
  148. return [f"{prefix}{line}" for line in (summary.get("headline"), summary.get("token_line"), summary.get("note")) if line]