micro_compaction.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. """Micro-compaction mixin for ContextCompressor.
  2. Rolling per-exchange summarization that folds old user/assistant exchanges into a single
  3. summary marker between turns. OFF by default: every pass rewrites the prompt prefix and
  4. breaks the provider prompt cache.
  5. """
  6. from __future__ import annotations
  7. import json
  8. import logging
  9. import time
  10. from typing import Any, Dict, List, Optional
  11. from agent.compaction_events import publish_micro
  12. from agent.context_compressor_telemetry import _safe_int
  13. from agent.message_metadata import record_absorbed_message
  14. from agent.model_metadata import estimate_messages_tokens_rough, estimate_tokens_rough
  15. # Log name parity with the origin module.
  16. logger = logging.getLogger("agent.context_compressor")
  17. def _cc():
  18. """The origin module, resolved lazily: avoids the import cycle and keeps tests that patch
  19. ``agent.context_compressor.X`` effective (attributes are read at call time)."""
  20. from agent import context_compressor
  21. return context_compressor
  22. def _is_summary_marker(entry: Any) -> bool:
  23. return isinstance(entry, dict) and bool(entry.get(_cc().COMPRESSED_SUMMARY_METADATA_KEY))
  24. def _is_micro_marker(entry: Any) -> bool:
  25. """True for a summary marker provably absorbed into the rolling summary (micro, not batch)."""
  26. return _is_summary_marker(entry) and bool(entry.get(_cc().MICRO_COMPACT_MARKER_KEY))
  27. class MicroCompactionMixin:
  28. """Rolling micro-compaction; host must be a ``ContextCompressor``."""
  29. def _resolve_compact_cursor(self, messages: list[dict[str, Any]], head_end: int, tail_start: int) -> int:
  30. """Index of the first message not yet absorbed into the rolling summary: the in-memory
  31. cursor when valid, else just past the last summary marker."""
  32. if head_end < self._micro_compact_cursor < tail_start:
  33. return self._micro_compact_cursor
  34. summaries = (i for i in range(head_end, tail_start) if self._is_context_summary_message(messages[i]))
  35. last = max(summaries, default=-1)
  36. cursor = last + 1 if last >= head_end else head_end
  37. if last >= head_end:
  38. # Resumed session: rehydrate the rolling summary from the surviving marker so the next
  39. # pass merges, not replaces.
  40. recovered = "" if self._micro_compact_rolling_summary.strip() else (
  41. self._rolling_summary_from_marker(messages[last].get("content"))
  42. )
  43. if recovered:
  44. self._micro_compact_rolling_summary = recovered
  45. # Rehydration proves containment: this marker (batch or micro) becomes
  46. # supersede/defrag-eligible; unabsorbed markers never get the key.
  47. messages[last][_cc().MICRO_COMPACT_MARKER_KEY] = True
  48. logger.info("Micro-compaction: recovered rolling summary from transcript (%d chars)", len(recovered))
  49. self._micro_compact_cursor = cursor
  50. return cursor
  51. def _find_one_exchange(
  52. self, messages: list[dict[str, Any]], start: int, tail_start: int,
  53. ) -> Optional[tuple[int, int]]:
  54. """Find the next complete exchange (full agent turn) starting at *start*; returns
  55. ``(exchange_start, exchange_end)`` or ``None``. Spans assistant+tool rows up to the next
  56. user message; user turns are never absorbed (alternation safety, verbatim user text)."""
  57. limit = min(tail_start, len(messages))
  58. def _turn_row(idx: int, roles: tuple) -> bool:
  59. return messages[idx].get("role") in roles and not self._is_context_summary_message(messages[idx])
  60. # Skip user messages and (assistant-role) summary markers to reach a real assistant message;
  61. # otherwise a rehydrated cursor could absorb the marker itself.
  62. idx = start
  63. while idx < limit and not _turn_row(idx, ("assistant",)):
  64. idx += 1
  65. if idx >= limit:
  66. return None
  67. exchange_start = idx
  68. idx += 1
  69. while idx < limit and _turn_row(idx, ("assistant", "tool")):
  70. idx += 1
  71. # Boundary must close the turn: a mid-turn stop at tail_start would put the assistant marker
  72. # beside assistant/tool rows. Any other role is a safe splice (avoids wedging the cursor).
  73. boundary = messages[idx] if idx < len(messages) else None
  74. if not isinstance(boundary, dict) or boundary.get("role") in ("assistant", "tool"):
  75. return None
  76. return (exchange_start, idx)
  77. def _build_micro_summary_prompt(self, existing_summary: str, exchange_text: str) -> list[dict[str, str]]:
  78. """Build the prompt messages for a single-exchange micro-summary."""
  79. summary_block = existing_summary if existing_summary.strip() else "(No previous summary yet.)"
  80. user_prompt = (
  81. "You are a summarization agent creating a compact record of an "
  82. "ongoing conversation. You are given a running summary and the "
  83. "next exchange from the conversation. Merge the exchange's key "
  84. "decisions, requirements, file paths, and open questions into the "
  85. "summary. Preserve the summary's structure. Drop resolved details "
  86. "that are no longer relevant. Add new decisions, file paths, and "
  87. "open questions.\n\n"
  88. "NEVER include API keys, tokens, passwords, secrets, credentials, "
  89. "or connection strings in the summary \u2014 replace any that appear "
  90. f"with [REDACTED].\n\n"
  91. f"## Current Running Summary\n{summary_block}\n\n"
  92. f"## Next Exchange to Merge\n{exchange_text}\n\n"
  93. "Return ONLY the updated summary text, no preamble or explanation. "
  94. "Do not include this instruction block in your output."
  95. )
  96. return [
  97. {"role": "system", "content": "You are a conversation summarization assistant."},
  98. {"role": "user", "content": user_prompt},
  99. ]
  100. def _micro_summarize_one(self, exchange_text: str) -> Optional[str]:
  101. """Micro-summarize one exchange into the rolling summary via the aux LLM (None on failure)."""
  102. from agent.auxiliary_client import aux_interrupt_protection, call_llm
  103. call_kwargs = {
  104. "task": "compression",
  105. "messages": self._build_micro_summary_prompt(self._micro_compact_rolling_summary, exchange_text),
  106. "max_tokens": min(1500, self.max_summary_tokens or 1500),
  107. "temperature": 0.1,
  108. }
  109. self._apply_summary_route(call_kwargs)
  110. if self.model:
  111. call_kwargs.setdefault("main_runtime", {
  112. "model": self.model, "provider": self.provider or "", "base_url": self.base_url or "",
  113. "api_key": self.api_key or "", "api_mode": getattr(self, "api_mode", "") or "",
  114. })
  115. try:
  116. with aux_interrupt_protection():
  117. response = call_llm(**call_kwargs)
  118. except Exception as exc:
  119. logger.info("micro-summarization call failed: %s", exc)
  120. return None
  121. # A length stop means a partial merge; leave the exchange unabsorbed so a later pass retries.
  122. if _cc()._response_finish_reason(response) == "length":
  123. logger.warning(
  124. "micro-summarization output hit the token cap (finish_reason=length) — discarding partial summary",
  125. )
  126. return None
  127. message = response.choices[0].message
  128. content = message.get("content") if isinstance(message, dict) else getattr(message, "content", message)
  129. content = (content if isinstance(content, str) else str(content) if content else "").strip()
  130. from agent.agent_runtime_helpers import strip_think_blocks
  131. content = strip_think_blocks(None, content).strip()
  132. if not content:
  133. logger.info("micro-summarization returned empty content")
  134. return None
  135. if _cc()._is_refusal_response(response, content):
  136. logger.warning("micro-summarization returned refusal content — discarding unusable summary")
  137. return None
  138. return content
  139. def _needs_defrag(self) -> bool:
  140. """Return True when the rolling summary is large enough to defrag."""
  141. return estimate_tokens_rough(self._micro_compact_rolling_summary) >= self._micro_compact_defrag_threshold_tokens
  142. def _defrag_rolling_summary(self, messages: list[dict[str, Any]]) -> bool:
  143. """Re-summarize the rolling summary text and rewrite the marker in place.
  144. Transcript-shape-neutral (no splice, no cursor move). Returns True when it rewrote."""
  145. old_summary = self._micro_compact_rolling_summary
  146. if not old_summary.strip():
  147. return False
  148. # Empty base turns the merge prompt into a rewrite-compactly instruction.
  149. self._micro_compact_rolling_summary = ""
  150. fresh_summary = self._micro_summarize_one(old_summary)
  151. self._micro_compact_rolling_summary = fresh_summary or old_summary
  152. if not fresh_summary:
  153. return False
  154. # Rewrite only the newest MICRO marker (resume rehydrates from it); a batch marker holds
  155. # history we lack.
  156. entry = next((e for e in reversed(messages) if _is_micro_marker(e)), None)
  157. if entry is not None:
  158. entry["content"] = self._render_micro_marker_content(fresh_summary)
  159. # Content changed: clear the persisted stamp so the DB sync rewrites the row. An
  160. # in-place pop on a live dict would be identity-skipped by the bounded flush scan;
  161. # flag the finalizer.
  162. entry.pop(_cc()._DB_PERSISTED_MARKER, None)
  163. # Sibling of the finalize_turn pop site (#75170): this pop also strips the marker from a LIVE
  164. # dict in place, so the bounded flush-scan cursor would identity-skip the rewritten marker and
  165. # the defragged summary would never reach state.db. The compressor holds no agent reference, so
  166. # raise a flag the finalizer consumes to invalidate agent._db_flush_scan_prefix. (The pop sites
  167. # at module scope — fresh copies in strip-marker helpers — break identity and need no flag.)
  168. self._flush_scan_cursor_invalidated = True
  169. logger.info(
  170. "Micro-compaction defrag: rolling summary re-summarized (%d -> %d chars)",
  171. len(old_summary), len(fresh_summary),
  172. )
  173. return True
  174. def _reset_micro_failure_tracking(self) -> None:
  175. self._micro_compact_consecutive_failures = 0
  176. self._micro_compact_last_failure_cursor = -1
  177. def _micro_compact(
  178. self, messages: list[dict[str, Any]], *, turn_session_id: Optional[str] = None,
  179. ) -> list[dict[str, Any]]:
  180. """Run one round of micro-compaction (entry point from ``finalize_turn()``). Returns the
  181. (possibly modified) list and syncs the session DB via ``archive_and_compact`` (the
  182. append-only flush alone would double-load on resume). ``turn_session_id`` is the session the
  183. caller's turn started in; it parents the pass's Relay mark when this session has no scope yet."""
  184. if not self._micro_compact_enabled:
  185. return messages
  186. # Cadence gate: each pass breaks prompt cache once. Counted per invocation so a no-op turn
  187. # can't wedge it.
  188. every_n = max(1, int(self._micro_compact_every_n_turns or 1))
  189. if every_n > 1:
  190. self._micro_compact_turns_since_pass += 1
  191. if self._micro_compact_turns_since_pass < every_n:
  192. return messages
  193. self._micro_compact_turns_since_pass = 0
  194. n_messages = len(messages)
  195. exchange = self._next_exchange(messages) if n_messages >= 4 else None
  196. if exchange is None:
  197. return messages
  198. exchange_start, exchange_end = exchange
  199. # Telemetry baseline; taken only once an exchange exists so no-op turns don't pay.
  200. _started_at = time.monotonic()
  201. _tokens_before = estimate_messages_tokens_rough(messages)
  202. # The history this pass rewrites, and the store's watermark, before the slow summary call: the commit
  203. # keeps what another surface or a concurrent write added instead of archiving it unseen. A watermark
  204. # the store could not answer for leaves the commit unbounded, so the pass does not run at all.
  205. _held = list(messages)
  206. _skip, _start_watermark = self._micro_start_watermark(_held)
  207. def _telemetry(outcome: str, result: list[dict[str, Any]], **extra: Any) -> None:
  208. self._emit_micro_compaction_telemetry(
  209. outcome=outcome, messages_before=n_messages, messages_after=len(result),
  210. tokens_before=_tokens_before, duration_ms=int((time.monotonic() - _started_at) * 1000),
  211. turn_session_id=turn_session_id, **extra,
  212. )
  213. if _skip: # unanswerable watermark (unbounded archive) or a stale generation (no lease): skip
  214. _telemetry(_skip, messages, tokens_after=_tokens_before)
  215. return messages
  216. # Pre-pass state, restored when the commit finds a compaction landed during the summary call.
  217. _pre_cursor, _pre_summary = self._micro_compact_cursor, self._micro_compact_rolling_summary
  218. # Defrag rewrites summary text/marker in place (no splice, no cursor move) instead of
  219. # absorbing this turn.
  220. if self._needs_defrag():
  221. _marker = next((e for e in reversed(messages) if _is_micro_marker(e)), None)
  222. _pre_marker = dict(_marker) if _marker is not None else None
  223. defragged = self._defrag_rolling_summary(messages)
  224. if defragged and not self._sync_micro_compact_to_db(
  225. messages, held=_held, start_watermark=_start_watermark,
  226. ):
  227. # Stale generation: undo the in-place rewrite so the finalizer never persists it.
  228. self._micro_compact_rolling_summary = _pre_summary
  229. if _marker is not None:
  230. _marker.clear()
  231. _marker.update(_pre_marker)
  232. _telemetry("stale_generation", messages, tokens_after=_tokens_before)
  233. return messages
  234. if defragged:
  235. self._reset_micro_failure_tracking()
  236. outcome = "defrag" if defragged else "defrag_failed"
  237. _telemetry(outcome, messages, tokens_after=estimate_messages_tokens_rough(messages))
  238. return messages
  239. # Cumulative iff it subsumes an earlier marker; captured before summarizing.
  240. _cumulative = bool(self._micro_compact_rolling_summary.strip())
  241. exchange_text = self._serialize_for_summary(messages[exchange_start:exchange_end])
  242. _exchange_tokens = estimate_tokens_rough(exchange_text)
  243. updated_summary = self._micro_summarize_one(exchange_text)
  244. if updated_summary is None:
  245. _outcome = self._record_micro_failure(exchange_start, exchange_end)
  246. _telemetry(_outcome, messages, tokens_after=_tokens_before, exchange_tokens=_exchange_tokens)
  247. return messages
  248. self._micro_compact_rolling_summary = updated_summary
  249. self._micro_compact_cursor = exchange_end
  250. self._reset_micro_failure_tracking()
  251. result = self._splice_micro_compact_result(messages, exchange_start, exchange_end, supersede=_cumulative)
  252. self._micro_compact_cursor = self._cursor_after_splice(result, exchange_start + 1)
  253. if not self._sync_micro_compact_to_db(result, held=_held, start_watermark=_start_watermark):
  254. # Another compaction committed during the summary call. A true no-op: returning the spliced
  255. # list would let finalize_turn persist its unmarked summary row beside the winning generation.
  256. self._micro_compact_cursor, self._micro_compact_rolling_summary = _pre_cursor, _pre_summary
  257. _telemetry("stale_generation", messages, tokens_after=_tokens_before)
  258. return messages
  259. _telemetry(
  260. "absorbed", result, tokens_after=estimate_messages_tokens_rough(result), exchange_tokens=_exchange_tokens,
  261. )
  262. return result
  263. def _record_micro_failure(self, exchange_start: int, exchange_end: int) -> str:
  264. """Count a summarize failure at this cursor; skip the exchange after too many in a row."""
  265. # Track consecutive failures at the same cursor to avoid busy-looping every turn.
  266. same_cursor = exchange_start == self._micro_compact_last_failure_cursor
  267. self._micro_compact_consecutive_failures = self._micro_compact_consecutive_failures + 1 if same_cursor else 1
  268. self._micro_compact_last_failure_cursor = exchange_start
  269. if self._micro_compact_consecutive_failures < _cc()._MICRO_COMPACT_MAX_CONSECUTIVE_FAILURES:
  270. return "summarize_failed"
  271. logger.info(
  272. "Micro-compaction: skipping exchange at cursor %d after %d consecutive failures",
  273. exchange_start, self._micro_compact_consecutive_failures,
  274. )
  275. # Skip the stuck exchange; it stays in the transcript for batch compression/defrag.
  276. self._micro_compact_cursor = exchange_end
  277. self._reset_micro_failure_tracking()
  278. return "exchange_skipped"
  279. def _next_exchange(self, messages: list[dict[str, Any]]) -> Optional[tuple[int, int]]:
  280. """The next un-absorbed exchange inside the compressible window, or None."""
  281. compress_start = self._align_boundary_forward(messages, self._protect_head_size(messages))
  282. compress_end = self._find_tail_cut_by_tokens(messages, compress_start, allow_split_turn=False)
  283. if compress_start >= compress_end:
  284. return None
  285. cursor = self._resolve_compact_cursor(messages, compress_start, compress_end)
  286. return None if cursor >= compress_end else self._find_one_exchange(messages, cursor, compress_end)
  287. @staticmethod
  288. def _rolling_summary_from_marker(content: Any) -> str:
  289. """Recover the rolling-summary text from a summary marker (resume rehydration)."""
  290. cc = _cc()
  291. if not isinstance(content, str) or not content.strip():
  292. return ""
  293. # rfind: SUMMARY_PREFIX itself mentions the heading, so the first hit is in the preamble.
  294. idx = content.rfind(cc.HISTORICAL_TASK_HEADING)
  295. body = content[idx + len(cc.HISTORICAL_TASK_HEADING):] if idx != -1 else content
  296. end = body.find(cc._SUMMARY_END_MARKER)
  297. return (body[:end] if end != -1 else body).strip()
  298. def _cursor_after_splice(self, result: list[dict[str, Any]], fallback: int) -> int:
  299. """Cursor position just past the summary marker in *result*. Must derive from the SPLICED
  300. list: a splice collapses several rows into one marker (and may drop a superseded one), so
  301. pre-splice indices land inside a later exchange and silently skip it."""
  302. return next((idx + 1 for idx in range(len(result) - 1, -1, -1) if _is_summary_marker(result[idx])), fallback)
  303. def _emit_micro_compaction_telemetry(
  304. self, *, outcome: str, messages_before: int, messages_after: int, tokens_before: int | None,
  305. tokens_after: int | None, exchange_tokens: int | None = None, duration_ms: int | None = None,
  306. turn_session_id: str | None = None,
  307. ) -> None:
  308. """Emit one content-free JSON log line for a micro-compaction pass.
  309. ``tokens_delta`` < 0 means the pass shrank the transcript; ``*_total`` fields accumulate."""
  310. try:
  311. delta = tokens_after - tokens_before if tokens_before is not None and tokens_after is not None else None
  312. self._micro_compact_tokens_saved_total -= delta or 0
  313. self._micro_compact_passes += 1
  314. # Cached reads only: the lazy properties can fire a synchronous /models probe.
  315. # The ``threshold_tokens`` / ``context_length`` properties resolve lazily and can fire a
  316. # synchronous /models probe on first access (#32221) — telemetry must never be the thing that
  317. # blocks a turn. Unresolved simply reports null.
  318. threshold = self._threshold_tokens
  319. has_occupancy = threshold and tokens_after is not None and threshold > 0
  320. occupancy = round(tokens_after / threshold * 100, 1) if has_occupancy else None
  321. payload = {
  322. "event": "micro_compaction", "session_id": getattr(self, "_session_id", "") or "", "outcome": outcome,
  323. "messages_before": messages_before, "messages_after": messages_after,
  324. "tokens_before": _safe_int(tokens_before), "tokens_after": _safe_int(tokens_after),
  325. "tokens_delta": _safe_int(delta), "exchange_tokens": _safe_int(exchange_tokens),
  326. "rolling_summary_tokens": estimate_tokens_rough(self._micro_compact_rolling_summary),
  327. "cursor": _safe_int(self._micro_compact_cursor), "passes_total": self._micro_compact_passes,
  328. "tokens_saved_total": self._micro_compact_tokens_saved_total, "duration_ms": _safe_int(duration_ms),
  329. # Headroom: how full the window is being kept.
  330. "threshold_tokens": _safe_int(threshold), "context_limit": _safe_int(self._resolved_context_length),
  331. "occupancy_pct": occupancy, "main_model": self.model or "", "aux_model": self.summary_model or "",
  332. }
  333. logger.info("micro compaction telemetry: %s", json.dumps(payload, sort_keys=True, separators=(",", ":")))
  334. publish_micro(payload, turn_session_id=turn_session_id)
  335. except Exception as exc:
  336. logger.debug("failed to emit micro-compaction telemetry: %s", exc)
  337. def _micro_start_watermark(self, held: list[dict[str, Any]]) -> tuple[Optional[str], Optional[int]]:
  338. """``(skip_outcome, watermark)`` taken before a pass's slow step; *skip_outcome* is the telemetry
  339. label of a pass that must not run, or None.
  340. A store with no watermark API runs the pass with ``(None, None)``: the commit keeps today's
  341. archive-everything behaviour. ``watermark_unavailable`` when the read itself raised — that must
  342. NOT collapse into the same None, because None means "archive every active row", the very loss
  343. this watermark exists to prevent. ``stale_generation`` when *held* is no longer the session's live
  344. generation (another compaction already committed): this pass holds no compression lease, so
  345. publishing would leave two generations live. The commit re-checks, since a compaction can also
  346. land during the summary call.
  347. """
  348. session_db, session_id = getattr(self, "_session_db", None), getattr(self, "_session_id", "")
  349. watermark_of = getattr(session_db, "get_active_message_watermark", None)
  350. if not session_id or not callable(watermark_of):
  351. return None, None
  352. try:
  353. watermark = watermark_of(session_id)
  354. _cc()._archive_watermark_for(session_db, session_id, held, watermark)
  355. except _cc().StaleHeldHistory as exc:
  356. logger.info("micro-compaction: skipping this pass, %s", exc)
  357. return "stale_generation", None
  358. except Exception as exc:
  359. logger.info("micro-compaction: watermark read failed, skipping this pass: %s", exc)
  360. return "watermark_unavailable", None
  361. return None, watermark
  362. def _sync_micro_compact_to_db(
  363. self, compacted_messages: list[dict[str, Any]], *, held: Optional[list[dict[str, Any]]] = None,
  364. start_watermark: Optional[int] = None,
  365. ) -> bool:
  366. """Persist the micro-compacted set to the session DB atomically and stamp rows persisted.
  367. Without this the old exchange rows stay ``active=1`` and a resume double-loads both the
  368. summary and the originals. Returns False only when *held* turned out to be a stale generation
  369. (nothing written); the caller must then discard the pass. Any other outcome returns True."""
  370. session_db, session_id = getattr(self, "_session_db", None), getattr(self, "_session_id", "")
  371. if not session_db or not session_id:
  372. return True
  373. from hermes_state_errors import HeldRowRewrittenError
  374. try:
  375. # Micro-compaction is prefix + marker + suffix, not a contiguous tail. Identify the exact
  376. # byte-identical originals by their persistence marker; the state transaction resolves each
  377. # one by row id or durable identity+timestamp. In-place mutations deliberately pop
  378. # _DB_PERSISTED_MARKER, and the fresh summary marker never has one. A positional tail_count
  379. # can otherwise classify the summarized assistant/tool rows as rewind-only (#118481).
  380. carried_messages = [
  381. message for message in compacted_messages
  382. if isinstance(message, dict) and message.get(_cc()._DB_PERSISTED_MARKER)
  383. ]
  384. watermark = None
  385. covered_ids = unresolved_held = None
  386. if held is not None and start_watermark is not None:
  387. watermark = _cc()._archive_watermark_for(session_db, session_id, held, start_watermark)
  388. from agent.conversation_compression_archive import coverage_for_commit
  389. covered_ids, unresolved_held = coverage_for_commit(session_db, session_id, held)
  390. session_db.archive_and_compact(
  391. session_id, compacted_messages, carried_messages=carried_messages, watermark=watermark,
  392. covered_ids=covered_ids, unresolved_held=unresolved_held,
  393. held_for_cas=held)
  394. # Shared post-commit stamp site with batch commit and proactive prune.
  395. # See #98450.
  396. _cc().stamp_db_persisted_markers(compacted_messages)
  397. except _cc().StaleHeldHistory as exc:
  398. # Another compaction committed during the summary call. Nothing is written: the store already
  399. # holds the winning generation. The caller discards the pass (original list, pre-pass cursor
  400. # and summary); otherwise finalize_turn would append the unmarked summary row beside the winner.
  401. logger.info("Micro-compaction commit skipped, %s", exc)
  402. return False
  403. except HeldRowRewrittenError as exc:
  404. # The in-transaction CAS fence tripped: a held row changed between the precheck and the
  405. # commit. Same disposition as a stale generation — nothing written, the pass is discarded.
  406. logger.info("Micro-compaction commit skipped, %s", exc)
  407. return False
  408. except Exception:
  409. logger.info(
  410. "Micro-compaction DB sync failed — resume will double-load "
  411. "compacted messages until the next batch compression"
  412. )
  413. return True
  414. def _splice_micro_compact_result(
  415. self, messages: list[dict[str, Any]], splice_start: int, splice_end: int, supersede: bool = True,
  416. ) -> list[dict[str, Any]]:
  417. """Replace *messages[splice_start:splice_end]* with an assistant-role summary marker.
  418. Merges user turns left adjacent by a superseded marker so the result is alternation-valid."""
  419. cc = _cc()
  420. summary_text = self._micro_compact_rolling_summary
  421. if not summary_text.strip():
  422. return messages
  423. summary_msg = {
  424. "role": "assistant", "content": self._render_micro_marker_content(summary_text),
  425. cc.COMPRESSED_SUMMARY_METADATA_KEY: True,
  426. # Micro marker: eligible for supersede/defrag; batch markers never carry this key.
  427. cc.MICRO_COMPACT_MARKER_KEY: True,
  428. # Micro markers absorb only assistant/tool content; user turns stay in the transcript.
  429. cc.COMPRESSED_SUMMARY_HAS_USER_TURN_KEY: False,
  430. }
  431. result = messages[:splice_start] + [summary_msg] + messages[splice_end:]
  432. # Cumulative summary: keep only the newest marker. Drop an older one only if supersede AND
  433. # it has MICRO_COMPACT_MARKER_KEY (provably absorbed); a batch marker holds MORE history.
  434. stale = [i for i, m in enumerate(result) if _is_micro_marker(m)][:-1] if supersede else []
  435. if stale:
  436. result = self._merge_adjacent_user_turns([m for i, m in enumerate(result) if i not in stale])
  437. # Deliberately no _strip_persistence_markers: micro archives in place under the same session
  438. # id, so stamps stay accurate and a failed archive keeps the append-only flush idempotent.
  439. return result
  440. @staticmethod
  441. def _render_micro_marker_content(summary_text: str) -> str:
  442. """Assemble the marker content wrapper around *summary_text*."""
  443. cc = _cc()
  444. return f"{cc.SUMMARY_PREFIX}\n\n{cc.HISTORICAL_TASK_HEADING}\n{summary_text.strip()}\n\n{cc._SUMMARY_END_MARKER}"
  445. def _merge_adjacent_user_turns(self, result: list[dict[str, Any]]) -> list[dict[str, Any]]:
  446. """Merge consecutive plain-text real user turns left by a supersede. Same ``\\n\\n`` join as
  447. ``repair_message_sequence`` pass 2, done here so the marker and cursor are never collateral
  448. damage of the downstream repair. Lists untouched."""
  449. from agent.turn_context import drop_stale_api_content
  450. def _plain_user(m: Any) -> bool:
  451. return (
  452. isinstance(m, dict) and m.get("role") == "user" and not _is_summary_marker(m)
  453. and isinstance(m.get("content"), str)
  454. )
  455. merged: list[dict[str, Any]] = []
  456. for msg in result:
  457. prev = merged[-1] if merged else None
  458. if _plain_user(msg) and _plain_user(prev):
  459. prev["content"] = "\n\n".join(c for c in (prev["content"], msg["content"]) if c)
  460. drop_stale_api_content(prev) # merged content invalidates the api_content sidecar
  461. # The originals stay in display history as compacted rows; showing the join too
  462. # would paint every merged input twice on resume.
  463. prev["display_metadata"] = {**(prev.get("display_metadata") or {}),
  464. _cc().MODEL_ONLY_DISPLAY_METADATA_KEY: True}
  465. # The merge rewrites a live dict that may carry _db_persisted: pop the stamp
  466. # and flag the finalizer to invalidate the bounded flush-scan cursor, or the
  467. # merged text is identity-skipped and never reaches state.db. Same contract
  468. # as the defrag rewrite site above.
  469. prev.pop(_cc()._DB_PERSISTED_MARKER, None)
  470. self._flush_scan_cursor_invalidated = True
  471. record_absorbed_message(prev, msg) # merge witness: prev keeps its uid, records msg's
  472. else:
  473. merged.append(msg)
  474. return merged