conversation_compression_reply_anchor.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. """Keep the just-delivered assistant reply live across a compaction commit (#118900).
  2. Sibling of ``agent/conversation_compression.py`` (the facade). The facade
  3. late-imports this module at its single call site in ``compress_context``; this
  4. module must never import the facade at module level (import cycle).
  5. """
  6. from __future__ import annotations
  7. import logging
  8. from typing import Any, Optional
  9. from agent.message_sanitization import coalesce_tool_call_id
  10. from agent.model_metadata import estimate_messages_tokens_rough
  11. logger = logging.getLogger(__name__)
  12. def _ensure_compressed_keeps_last_assistant_reply(
  13. original_messages: list, compressed: list, *, session_id: Optional[str] = None,
  14. ) -> Optional[dict]:
  15. """Keep the latest visible assistant reply live across compaction (#118900).
  16. A reply that just finished streaming is the row the user is reading; when an
  17. engine's fold drops it into the summary region, the commit archives its row
  18. (active=0) and surfaces that render it from the active set drop it on the
  19. next refresh — while the content sits intact on disk. The built-in
  20. compressor keeps this row in the tail (``_ensure_last_assistant_message_in_tail``),
  21. but plugin engines implement their own ``compress()`` without that guard, so
  22. it is enforced here, next to ``_ensure_compressed_has_user_turn``, where
  23. every engine and every path (threshold preflight, engine maintenance,
  24. manual /compress; in-place and rotation) routes through.
  25. Runs FIRST at the commit boundary — before the todo-snapshot fold and the
  26. user-turn anchor — so the follower rows still read as the engine returned
  27. them and the later passes see the reply as the tail they place around
  28. (``[summary, reply, U_new]`` / ``[..., reply, TODO-row]``). Placement is
  29. chronological (see ``_reply_insertion_index``); when the only slot would
  30. sit assistant-adjacent to a non-twin row the reinsertion is skipped, never
  31. forced: strict alternation beats recovery. Empty (reasoning-only) and
  32. tool-call rows are out of scope: the former is a different bug family, the
  33. latter must keep its atomic tool group.
  34. Returns the exact dict placed into ``compressed`` (the caller names it as a
  35. carried row at commit), or ``None`` when nothing was reinserted — every skip
  36. that is not "the reply is already there" is logged here with its reason.
  37. """
  38. from agent.context_compressor import (
  39. _DB_PERSISTED_MARKER, _fresh_compaction_message_copy, is_compaction_summary_message,
  40. )
  41. from agent.conversation_compression import _message_text
  42. reply, reply_text = None, ""
  43. for message in reversed(original_messages):
  44. if not isinstance(message, dict) or message.get("role") != "assistant":
  45. continue
  46. if message.get("tool_calls") or is_compaction_summary_message(message):
  47. continue
  48. reply_text = _message_text(message).strip()
  49. if not reply_text:
  50. continue
  51. reply = message
  52. break
  53. if reply is None:
  54. return None
  55. def _is_reply_twin(message: Any) -> bool:
  56. if (
  57. not isinstance(message, dict)
  58. or message.get("role") != "assistant"
  59. or message.get("tool_calls")
  60. ):
  61. return False
  62. return _same_visible_content(message, reply, reply_text)
  63. if any(_is_reply_twin(message) for message in compressed):
  64. return None
  65. anchor = _fresh_compaction_message_copy(reply)
  66. # Identity scan, not list.index(): duplicate rows with identical content
  67. # exist in the wild, and == would resolve to the older twin (#118900).
  68. reply_pos = next(i for i, m in enumerate(original_messages) if m is reply)
  69. index = _reply_insertion_index(
  70. original_messages[reply_pos + 1:], compressed, reused_ids=_reused_tool_call_ids(original_messages),
  71. )
  72. if index is None:
  73. logger.warning(
  74. "Compression: engine folded away the just-delivered assistant reply and the surviving tool "
  75. "rounds reuse tool-call ids, so its slot cannot be located; not reinserting it (session=%s).",
  76. session_id or "none",
  77. )
  78. return None
  79. # Either neighbour already being a non-tool assistant twin means the reply is
  80. # effectively present at this seam; inserting would create assistant;assistant.
  81. neighbours = [compressed[i] for i in (index - 1, index) if 0 <= i < len(compressed)]
  82. if any(_is_reply_twin(message) for message in neighbours):
  83. return None
  84. # A non-twin assistant neighbour (an older kept reply, the engine's own paraphrase of
  85. # this one, or a tool-call row) means the slot cannot take the row without
  86. # assistant;assistant adjacency, and sliding past it would only move the seam while
  87. # putting the newest reply before an older one. Strict alternation is the invariant
  88. # every provider path relies on; the reply stays readable on disk, so skip the recovery
  89. # rather than break the shape. Tool-call rows are NOT exempt on either side: to the left
  90. # the row would separate a call from its result; to the right the user-turn anchor is
  91. # only best-effort (it yields to a merged in-flight replay, a surviving real user row or
  92. # a busy steer), so the pair would reach the durable active set and the pre-call belt
  93. # would then merge the reply INTO the tool-call row, diverging live from DB.
  94. if any(isinstance(m, dict) and m.get("role") == "assistant" for m in neighbours):
  95. logger.warning(
  96. "Compression: engine folded away the just-delivered assistant reply and left a non-matching "
  97. "assistant row at its slot; not reinserting it to keep strict role alternation (session=%s).",
  98. session_id or "none",
  99. )
  100. return None
  101. # The built-in compressor deliberately lets a reply that alone dominates the window fold
  102. # (``_ensure_last_assistant_message_in_tail`` yields to ``head_end + 1``) so compaction can
  103. # make progress; forcing it back would make the candidate no smaller than the input, and
  104. # the commit-site no-growth guard would then refuse the attempt and add an ineffective
  105. # strike — a session that can never compact is worse than a folded reply. Measure with
  106. # the same rough estimate that guard uses and mirror its comparison (it refuses only on
  107. # `>`, so exact break-even is still accepted). The estimate is a per-row sum, so the
  108. # anchor's cost is added rather than re-estimating the whole candidate. The `<` pre-check
  109. # limits the yield to compactions that ARE shrinking: a candidate that already fails to
  110. # shrink is refused by the guard (or repaired by salvage) whether or not the reply is in
  111. # it, so dropping the reply there would buy nothing.
  112. rough_in = estimate_messages_tokens_rough(original_messages)
  113. rough_out = estimate_messages_tokens_rough(compressed)
  114. if rough_out < rough_in < rough_out + estimate_messages_tokens_rough([anchor]):
  115. logger.info(
  116. "Compression: not reinserting the just-delivered assistant reply; it would stop this "
  117. "compaction from shrinking the transcript (session=%s).", session_id or "none",
  118. )
  119. return None
  120. # Post-commit contract (#98450, mirrors _insert_real_user_anchor._place):
  121. # archive_and_compact durably writes every dict in `compressed` as the new
  122. # active set, so stamp the copy or the next flush re-INSERTs it as a duplicate.
  123. anchor[_DB_PERSISTED_MARKER] = True
  124. compressed.insert(index, anchor)
  125. return anchor
  126. def _same_visible_content(left: dict, right: dict, right_text: str) -> bool:
  127. """Visible-content equality: normalized text when either side has any, else raw ``content``.
  128. A conforming engine may hand a row back whitespace-stripped or with parts-list
  129. content re-rendered as a string; raw ``==`` would treat that as a different row
  130. (the twin check would insert a duplicate, the follower scan would miss its slot).
  131. ``left`` is the per-candidate row and is flattened here; ``right_text`` is the
  132. caller's row flattened once per scan rather than once per candidate.
  133. """
  134. from agent.conversation_compression import _message_text
  135. left_text = _message_text(left).strip()
  136. if left_text or right_text:
  137. return left_text == right_text
  138. return left.get("content") == right.get("content")
  139. def _tool_call_ids(message: dict) -> frozenset:
  140. """Pairing ids of an assistant row's tool calls, keyed the way the pre-call sanitizer
  141. keys them (``call_id`` before ``id``, composite ``call|item`` split): Codex rows carry
  142. ``id=fc_…`` AND ``call_id=call_…`` and their results pair on the latter."""
  143. return frozenset(filter(None, map(coalesce_tool_call_id, message.get("tool_calls") or ())))
  144. def _tool_result_id(message: dict) -> str:
  145. """The ``tool_call_id`` of a ``tool`` row under the same normalisation as ``_tool_call_ids``."""
  146. return coalesce_tool_call_id({"id": message.get("tool_call_id")})
  147. def _reused_tool_call_ids(messages: list) -> frozenset:
  148. """Tool-call ids that more than one assistant row in ``messages`` uses.
  149. ``tool_call_id`` is not unique in practice: llama.cpp emits one constant id for
  150. every call it ever returns, and other providers reuse ``call_0`` per turn (see
  151. ``_dedupe_tool_call_ids``). Such ids cannot locate a specific row.
  152. """
  153. seen: dict = {}
  154. for message in messages:
  155. if isinstance(message, dict) and message.get("role") == "assistant":
  156. for call_id in _tool_call_ids(message):
  157. seen[call_id] = seen.get(call_id, 0) + 1
  158. return frozenset(call_id for call_id, count in seen.items() if count > 1)
  159. def _reply_insertion_index(followers: list, compressed: list, *, reused_ids: frozenset = frozenset()) -> Optional[int]:
  160. """Chronologically correct slot for the dropped reply inside ``compressed``.
  161. ``followers`` are the ORIGINAL rows after the reply (the next user turn, its
  162. tool rounds, ...). The slot is just before the surviving row that originally
  163. followed it: the LAST match in ``compressed`` on role + normalized text
  164. (a first-match scan binds to an older identical "ok"/"thanks" twin and
  165. resurfaces the reply mid-history; raw ``==`` misses a follower the engine
  166. re-rendered from a parts list). When no follower survives but the original
  167. had a real user turn after the reply, the reply must still never land after
  168. a trailing REAL user row — that is the turn the loop is about to answer, and
  169. an ``assistant`` tail would present the old reply as its answer — so it goes
  170. right before that row instead (user-role scaffolding such as a todo snapshot
  171. or handoff row is not that turn; the reply goes after it and the user-turn
  172. anchor places the real turn behind the reply). No followers at all
  173. (idle/maintenance compaction right after the reply) → the reply is genuinely
  174. the tail.
  175. Tool rounds whose ids the transcript reuses (``reused_ids``) are not usable
  176. anchors: the id would bind to whichever round happens to sit last and put the
  177. reply mid-chain. Such followers are passed over in favour of the remaining
  178. real followers; when none locates the slot and a reused-id round survives in
  179. ``compressed``, the placement is unknown and ``None`` is returned (skip).
  180. """
  181. from agent.conversation_compression import _is_real_user_message, _message_text
  182. def _row_ids(message: dict) -> frozenset:
  183. return _tool_call_ids(message) | frozenset(filter(None, (_tool_result_id(message),)))
  184. followers = [f for f in followers if isinstance(f, dict)]
  185. ambiguous = False
  186. for follower in followers:
  187. follower_role, follower_text = follower.get("role"), _message_text(follower).strip()
  188. follower_call_ids = _tool_call_ids(follower)
  189. if _row_ids(follower) & reused_ids:
  190. ambiguous = ambiguous or any(
  191. isinstance(m, dict) and _row_ids(m) & reused_ids for m in compressed
  192. )
  193. continue
  194. for pos in range(len(compressed) - 1, -1, -1):
  195. message = compressed[pos]
  196. if not isinstance(message, dict) or message.get("role") != follower_role:
  197. continue
  198. # Tool rounds carry no visible text: a tool-call assistant has ``content None``
  199. # and tool results repeat ("ok", "{}"), so content equality binds to whichever
  200. # such row sits LAST (mid-turn compaction keeps the whole current tool chain).
  201. # The call ids are unique per round; match on those instead.
  202. message_call_ids = _tool_call_ids(message)
  203. if follower_call_ids or message_call_ids:
  204. if message_call_ids == follower_call_ids:
  205. return pos
  206. continue
  207. if follower_role == "tool" and (_tool_result_id(follower) or _tool_result_id(message)):
  208. if _tool_result_id(message) == _tool_result_id(follower):
  209. return pos
  210. continue
  211. if _same_visible_content(message, follower, follower_text):
  212. return pos
  213. if ambiguous:
  214. return None
  215. if (
  216. followers
  217. and any(_is_real_user_message(f) for f in followers)
  218. and compressed
  219. and _is_real_user_message(compressed[-1])
  220. ):
  221. return len(compressed) - 1
  222. return len(compressed)