context_compressor_prune.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. """The proactive tool-result prune: deterministic passes plus the in-place row commit (#124102).
  2. Split out of :mod:`agent.context_compressor` (FILE_LINES ratchet): the prune is a self-contained
  3. reclamation feature. The origin module keeps the ``ContextCompressor`` class; this sibling owns
  4. ``prune_tool_results_only`` and its in-place commit helper. Log name parity with the origin
  5. module, and every origin-module symbol is read through ``_cc()`` at call time so tests that patch
  6. ``agent.context_compressor.X`` stay effective.
  7. """
  8. from __future__ import annotations
  9. import logging
  10. from typing import Any, Optional
  11. logger = logging.getLogger("agent.context_compressor")
  12. def _cc():
  13. """The origin module, resolved lazily: avoids the import cycle and keeps tests that patch
  14. ``agent.context_compressor.X`` effective (attributes are read at call time)."""
  15. from agent import context_compressor
  16. return context_compressor
  17. class ProactivePruneMixin:
  18. """``ContextCompressor`` mixin: the deterministic, no-LLM tool-result prune and its commit."""
  19. def prune_tool_results_only(
  20. self, messages: list[dict[str, Any]], current_tokens: int | None = None,
  21. ) -> tuple[list[dict[str, Any]], int]:
  22. """Deterministic, no-LLM tool-result prune gated on ``proactive_prune_tokens``.
  23. Protects the tail by message COUNT only. A commit breaks the prompt cache, so it requires
  24. ``proactive_prune_min_reclaim_tokens`` and a full regrowth runway; otherwise returns the INPUT
  25. object as ``(messages, 0)``. The rearm gate is measured on message bodies only, so it is
  26. bypassed (never the reclaim gate) when a provider-billed ``current_tokens`` reading already
  27. puts the request over ``threshold_tokens`` (#101889); every no-op taken while over threshold
  28. is logged once per distinct reason.
  29. ``_prune_old_tool_results`` runs all deterministic passes: (1) dedup byte-identical tool results —
  30. keeps the newest full copy and back-references older exact duplicates ANYWHERE in the list
  31. (including the protected tail), so no unique content is ever lost; (2) summarize non-tail tool
  32. results larger than ``min_prune_chars``; (3) truncate oversized tool_call arguments on non-tail
  33. assistant messages; (3.5) retire image payloads on all but the newest ``_MAX_KEEP_TOOL_IMAGES``
  34. image-bearing tool results, except a pending round that fits the hard share (#92699). Only pass (2)'s floor is
  35. raised by ``proactive_prune_min_result_chars``; passes (1) and (3) keep their own fixed floors. The
  36. recent-tail protection applies to passes (2) and (3); pass (1) is tail-agnostic by design because
  37. dedup is lossless.
  38. """
  39. if self.proactive_prune_tokens <= 0 or (
  40. current_tokens is not None and current_tokens < self.proactive_prune_tokens
  41. ):
  42. return messages, 0
  43. if len(messages) <= self.protect_last_n + self._protect_head_size(messages) + 1:
  44. self._warn_reclamation_no_op("prune:tail_only", current_tokens)
  45. return messages, 0
  46. _estimate_msg_budget_tokens = _cc()._estimate_msg_budget_tokens
  47. before = sum(_estimate_msg_budget_tokens(m) for m in messages)
  48. # Under-threshold runway skip is ordinary hysteresis (silent); above it the lockout is the bug.
  49. if before < self._proactive_prune_rearm_tokens and not self._billed_basis_over_threshold(current_tokens):
  50. return messages, 0
  51. # Capability gate first: a store without archive_and_compact makes every prune a no-op.
  52. session_db = getattr(self, "_session_db", None)
  53. session_id = getattr(self, "_session_id", "")
  54. if session_db and session_id and not callable(getattr(session_db, "archive_and_compact", None)):
  55. self._warn_reclamation_no_op("prune:store_cannot_persist", current_tokens)
  56. return messages, 0
  57. pruned_msgs, pruned_count = self._prune_old_tool_results(
  58. messages, protect_tail_count=self.protect_last_n, protect_tail_tokens=None, min_prune_chars=self.proactive_prune_min_result_chars,
  59. )
  60. from agent.context_compressor_tail import with_pruned_row_skill_safety_rule
  61. pruned_msgs = with_pruned_row_skill_safety_rule(messages, pruned_msgs)
  62. if not pruned_count:
  63. # No-op contract: return the INPUT object so callers can gate on `result is not input`.
  64. self._warn_reclamation_no_op("prune:nothing_eligible", current_tokens)
  65. return messages, 0
  66. # Prompt-cache hysteresis: commit only when the reclaim is meaningful.
  67. after = sum(_estimate_msg_budget_tokens(m) for m in pruned_msgs)
  68. reclaimed = max(0, before - after)
  69. if reclaimed < self.proactive_prune_min_reclaim_tokens:
  70. self._warn_reclamation_no_op("prune:reclaim_below_minimum", current_tokens, before=before)
  71. return messages, 0
  72. # Require a full trigger-sized regrowth before the next cache-breaking rewrite.
  73. runway = max(reclaimed, self.proactive_prune_tokens, self.proactive_prune_min_reclaim_tokens)
  74. next_rearm_tokens = after + runway
  75. if session_db and session_id:
  76. _cc_ = _cc()
  77. from hermes_state_errors import HeldRowRewrittenError
  78. model_config_patch = {_cc_.PROACTIVE_PRUNE_REARM_MODEL_CONFIG_KEY: next_rearm_tokens}
  79. StaleHeldHistory = _cc_.StaleHeldHistory
  80. _DB_PERSISTED_MARKER = _cc_._DB_PERSISTED_MARKER
  81. rewritten = None
  82. try:
  83. # Raises StaleHeldHistory when another compaction already won, for both commit paths.
  84. watermark = _cc_._archive_watermark_for(session_db, session_id, messages)
  85. rewritten = self._rewrite_pruned_rows_in_place(
  86. session_db, session_id, messages, pruned_msgs, model_config_patch)
  87. if rewritten is None:
  88. from agent.conversation_compression_archive import coverage_for_commit
  89. covered_ids, unresolved_held = coverage_for_commit(session_db, session_id, messages)
  90. session_db.archive_and_compact(
  91. session_id, pruned_msgs,
  92. model_config_patch=model_config_patch,
  93. watermark=watermark,
  94. covered_ids=covered_ids, unresolved_held=unresolved_held,
  95. held_for_cas=messages)
  96. except StaleHeldHistory:
  97. # Another compaction already committed this session's history; a lease-less prune of the
  98. # generation this process holds would publish beside the winner. Leave the input alone.
  99. logger.info("Proactive tool-result prune skipped: another compaction already committed this session")
  100. self._warn_reclamation_no_op("prune:stale_generation", current_tokens, before=before)
  101. return messages, 0
  102. except HeldRowRewrittenError:
  103. # The full writer's in-transaction CAS fence tripped: a held row changed between the
  104. # precheck and the commit. Same disposition as StaleHeldHistory — nothing was written.
  105. logger.info("Proactive tool-result prune skipped: a held row was rewritten in place before the commit")
  106. self._warn_reclamation_no_op("prune:stale_generation", current_tokens, before=before)
  107. return messages, 0
  108. except Exception as exc: # health: allow BLE001 -- the commit boundary keeps the original transcript on any store failure (#124102)
  109. logger.warning("Proactive tool-result prune DB commit failed; keeping the original transcript: %s", exc)
  110. return messages, 0
  111. # Shared post-commit stamp site with the in-place commit and micro-compaction sync.
  112. # See #98450. The in-place rewrite wrote no new rows, so only already-durable messages
  113. # carry the marker; unwritten ones reach the DB through the next flush.
  114. stamp_db_persisted_markers = _cc_.stamp_db_persisted_markers
  115. if rewritten is None:
  116. stamp_db_persisted_markers(pruned_msgs)
  117. else:
  118. stamp_db_persisted_markers([
  119. replacement for original, replacement in zip(messages, pruned_msgs)
  120. if original.get(_DB_PERSISTED_MARKER)])
  121. self._proactive_prune_rearm_tokens = next_rearm_tokens
  122. # Reclamation just ran: let a future lockout warn again.
  123. self._last_reclaim_block_warn = None
  124. return pruned_msgs, pruned_count
  125. def _rewrite_pruned_rows_in_place(
  126. self, session_db: Any, session_id: str, messages: list[dict[str, Any]],
  127. pruned_msgs: list[dict[str, Any]], model_config_patch: dict[str, Any],
  128. ) -> Optional[int]:
  129. """Commit a prune by rewriting only its changed rows (#124102); ``None`` means use the full rewrite.
  130. A prune edits the content of a few existing messages and never adds, drops or reorders one, so the
  131. durable rows it touched can be updated where they are. That needs a store with
  132. ``rewrite_pruned_rows`` and unwritten messages only at the end (the next flush appends those in
  133. order; one sitting before durable rows would land out of order). A row the store cannot name
  134. exactly also falls back: ``archive_and_compact`` handles every shape. A row it *can* name whose
  135. stored body changed is not ambiguity but a lost race, and raises ``StaleHeldHistory`` so the
  136. caller skips the pass rather than republishing the held generation.
  137. """
  138. rewrite = getattr(session_db, "rewrite_pruned_rows", None)
  139. if not callable(rewrite) or len(messages) != len(pruned_msgs):
  140. return None
  141. from hermes_state_errors import PruneRowStaleError, PruneRowUnresolvedError
  142. _DB_PERSISTED_MARKER = _cc()._DB_PERSISTED_MARKER
  143. ignored = {_DB_PERSISTED_MARKER, "_row_id"}
  144. changes: list[tuple] = []
  145. unwritten_seen = False
  146. for original, replacement in zip(messages, pruned_msgs):
  147. if not isinstance(original, dict) or not isinstance(replacement, dict):
  148. return None
  149. if not original.get(_DB_PERSISTED_MARKER):
  150. unwritten_seen = True
  151. continue
  152. if unwritten_seen:
  153. return None
  154. if ({k: v for k, v in original.items() if k not in ignored}
  155. != {k: v for k, v in replacement.items() if k not in ignored}):
  156. changes.append((original, replacement))
  157. try:
  158. return rewrite(session_id, changes, model_config_patch=model_config_patch)
  159. except PruneRowStaleError as exc:
  160. # A named row whose stored body changed is another writer's generation, which is exactly
  161. # what StaleHeldHistory exists to catch — the in-place prune preserved the row ids, so
  162. # _archive_watermark_for could not see it. Falling back would hand this stale transcript to
  163. # archive_and_compact, which archives rows it never compared and republishes the held text
  164. # over the newer one. Fail closed; the caller skips the pass.
  165. raise _cc().StaleHeldHistory(str(exc)) from exc
  166. except PruneRowUnresolvedError as exc:
  167. logger.info("Proactive prune: in-place rewrite refused (%s); using the full rewrite", exc)
  168. return None