context_compressor_tail.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. """Lean-mode stale-tail demotion for ContextCompressor (old tail tool results become one-line recovery stubs), and
  2. the Skill Safety Rule the compaction handoff carries whenever its transcript holds a skill prune marker."""
  3. from __future__ import annotations
  4. import re
  5. from typing import Any
  6. # Demote tool results older than the newest N rounds so the tail budget binds
  7. # (the tool-group alignment floor otherwise keeps ~32K of tool output alive).
  8. _LEAN_TAIL_KEEP_TOOL_ROUNDS = 6
  9. _LEAN_TAIL_DEMOTE_MIN_CHARS = 1_500
  10. # Matches what ``_lean_recovery_stub`` emits, so later passes treat a demoted row as already summarised: with a session
  11. # id and a reload marker the stub can pass the Phase-1 prune floor, and re-summarising it would drop both pointers.
  12. _DEMOTED_STUB_RE = re.compile(r"\[[^\s\]]* output demoted at compaction — [\d,]+ chars preserved in session history\.")
  13. # :func:`lean_tail_budget`: the lean tail never claims more than this share of the trigger. A configured
  14. # ``threshold_tokens`` (or aux summariser ceiling) far below the window left the 10K floor at 83% of a 12K trigger, and
  15. # the 2.5%-of-window term at 216% of it on a 1M window, so head + summary + tail stayed over threshold and every pre-API
  16. # pass refused as "would grow" (the window-side twin is ``TAIL_MAX_CONTEXT_FRACTION``).
  17. TAIL_MAX_THRESHOLD_FRACTION = 0.50
  18. def lean_tail_budget(context_length: int, threshold_tokens: int) -> int:
  19. """Lean-mode tail selection budget: 2.5% of the window clamped to the 10K floor / 25K cap, then held to
  20. :data:`TAIL_MAX_THRESHOLD_FRACTION` of the trigger. At or below ``_MIN_SUMMARY_TOKENS`` the summary alone fills
  21. the trigger, so no tail leaves room under it and only the floor is held: trimming the recency window there
  22. would lose context without ending the loop."""
  23. from agent.context_compressor import _MIN_SUMMARY_TOKENS, LEAN_TAIL_CAP_TOKENS, LEAN_TAIL_FLOOR_TOKENS
  24. floor, window_term = LEAN_TAIL_FLOOR_TOKENS, min(LEAN_TAIL_CAP_TOKENS, int(context_length * 0.025))
  25. if threshold_tokens > 0:
  26. share = int(threshold_tokens * TAIL_MAX_THRESHOLD_FRACTION)
  27. floor = min(floor, share)
  28. if threshold_tokens > _MIN_SUMMARY_TOKENS:
  29. window_term = min(window_term, share)
  30. return max(floor, window_term)
  31. def is_demoted_stub(content: str) -> bool:
  32. """True for a tail tool result already replaced by ``_lean_recovery_stub`` (plus an optional reload marker)."""
  33. return len(content) < 1_000 and _DEMOTED_STUB_RE.match(content) is not None
  34. def _lean_recovery_stub(tool_name: str, content_len: int, session_id: str) -> str:
  35. """One-line replacement for a demoted tail tool result."""
  36. hint = f" Recover with session_search(query=..., session_id='{session_id}')" if session_id else ""
  37. return (
  38. f"[{tool_name or 'tool'} output demoted at compaction — {content_len:,} "
  39. f"chars preserved in session history.{hint}]"
  40. )
  41. def skill_view_names_by_call_id(messages: list[dict[str, Any]]) -> dict[str, str]:
  42. """Map each ``skill_view`` tool-call id to the skill name it loaded."""
  43. from agent.context_compressor import _json_dict, _tc_get
  44. names: dict[str, str] = {}
  45. for msg in messages:
  46. if msg.get("role") != "assistant":
  47. continue
  48. for tc in msg.get("tool_calls") or []:
  49. fn = _tc_get(tc, "function", {})
  50. skill = _json_dict(_tc_get(fn, "arguments")).get("name") if _tc_get(fn, "name") == "skill_view" else None
  51. if isinstance(skill, str) and skill:
  52. names[str(_tc_get(tc, "id") or "")] = skill
  53. return names
  54. def demote_stale_tail_tools(
  55. messages: list[dict[str, Any]], tail_start: int, session_id: str,
  56. ) -> tuple[list[dict[str, Any]], int]:
  57. """Demote tail tool results older than the newest ``_LEAN_TAIL_KEEP_TOOL_ROUNDS`` rounds to recovery stubs;
  58. skill-marker rows untouched. A demoted ``skill_view`` body carries the canonical reload marker, as its
  59. Phase-1 stub does, so the model does not act on instructions it no longer has (#32106). Returns a new list
  60. (untouched rows shared, demoted copied) and the number of rows demoted."""
  61. from agent.context_compressor import (
  62. SKILL_PRUNED_MARKER_PREFIX, _is_summary_stub, _result_failure_suffix, _rewritten, _skill_pruned_marker,
  63. )
  64. rounds_seen = 0
  65. protected: set[int] = set()
  66. prev_idx = None
  67. for i in (i for i in range(len(messages) - 1, tail_start - 1, -1) if messages[i].get("role") == "tool"):
  68. rounds_seen += prev_idx is None or prev_idx - i > 1
  69. prev_idx = i
  70. if rounds_seen > _LEAN_TAIL_KEEP_TOOL_ROUNDS:
  71. break
  72. protected.add(i)
  73. result = list(messages)
  74. demoted = 0
  75. skills: dict[str, str] | None = None
  76. for i in range(tail_start, len(messages)):
  77. msg = messages[i]
  78. content = msg.get("content")
  79. if msg.get("role") != "tool" or i in protected or not isinstance(content, str):
  80. continue
  81. if len(content) < _LEAN_TAIL_DEMOTE_MIN_CHARS or SKILL_PRUNED_MARKER_PREFIX in content or _is_summary_stub(content):
  82. continue
  83. stub = _lean_recovery_stub(msg.get("tool_name") or "", len(content), session_id)
  84. skills = skill_view_names_by_call_id(messages) if skills is None else skills
  85. if (skill := skills.get(str(msg.get("tool_call_id") or ""))) and not _result_failure_suffix(content):
  86. stub += " " + _skill_pruned_marker(skill)
  87. result[i] = _rewritten(msg, stub)
  88. demoted += 1
  89. return result, demoted
  90. # Sent only where ``[SKILL_PRUNED:`` markers exist — after a compaction, riding its handoff — rather than in every
  91. # fresh session's system prompt; adding it to a compacted transcript is the one sanctioned cache break.
  92. SKILL_SAFETY_HEADING = "## Skill Safety Rule"
  93. SKILL_SAFETY_RULE = (
  94. f"{SKILL_SAFETY_HEADING}\n"
  95. "A skill placeholder containing `[SKILL_PRUNED]` lost its content in context compression and is inaccessible — "
  96. "reload it with skill_view(name='...') before acting on anything that depends on it. After reloading, ignore any "
  97. "remaining `[SKILL_PRUNED]` markers for that same skill; they are historical artifacts of earlier compactions."
  98. )
  99. def tail_cut_pricer(compressor: Any, messages: list[dict[str, Any]], head_end: int):
  100. """Tail-cut walk price ``index -> tokens`` in which rows at or before the newest handoff cost nothing.
  101. The handoff scan folds every row up to the newest in-transcript handoff into the summary window (the tail starts
  102. past it), so none of them can stay in the protected tail. Charging them let the handoff row alone break the soft
  103. ceiling: the walk stopped on it, the window held only that handoff, and compress() returned the
  104. ``empty_post_handoff_window`` no-op at every trigger while the session grew past its window."""
  105. price = compressor._tail_row_pricer(messages)
  106. newest = next(
  107. (i for i in range(len(messages) - 1, head_end - 1, -1) if compressor._is_context_summary_message(messages[i])), -1,
  108. )
  109. return price if newest < 0 else (lambda i: 0 if i <= newest else price(i))
  110. def tail_cut_progress_floor(compressor: Any, messages: list[dict[str, Any]], head_end: int, cut_idx: int) -> int:
  111. """Lowest tail cut that leaves the summary window a row the handoff scan keeps.
  112. The scan drops standalone handoff rows from the window, so when every row before the cut (and up to the newest
  113. handoff) is a handoff, the planned window is empty and compress() no-ops with ``empty_post_handoff_window``. That
  114. happens when the cut lands inside the tool group right after the handoff and is aligned back onto the group's
  115. assistant. The floor then moves past that group, unless doing so would take the newest row or the active user
  116. request out of the tail; those cases keep the old ``head_end + 1`` floor. ``_compress_window``'s
  117. assistant/tool bridge before the active request obeys the same floor, so the two never disagree: when the floor
  118. sits on the active request, the summary merges into that row instead of the bridge emptying the window."""
  119. floor = head_end + 1
  120. newest = next(
  121. (i for i in range(len(messages) - 1, head_end - 1, -1) if compressor._is_context_summary_message(messages[i])), -1,
  122. )
  123. if newest < 0 or not all(map(compressor._is_context_summary_message, messages[head_end:max(cut_idx, newest + 1)])):
  124. return floor
  125. raised = compressor._align_boundary_forward(messages, newest + 2)
  126. last_user = compressor._find_last_user_message_idx(messages, newest + 1)
  127. return floor if raised >= len(messages) or 0 <= last_user < raised else raised
  128. def with_skill_safety_rule(
  129. summary: str, head: list[dict[str, Any]], tail: list[dict[str, Any]],
  130. ) -> tuple[str, list[dict[str, Any]], list[dict[str, Any]]]:
  131. """``(summary, head, tail)`` carrying :data:`SKILL_SAFETY_RULE` exactly once when the summary or a kept row holds a
  132. prune marker (summary-side ``## Pruned Skills`` markers, Phase-1 and lean-demoted ``skill_view`` stubs). A kept
  133. prune-only stub's copy (:func:`with_pruned_row_skill_safety_rule`) moves onto the handoff: compaction is the
  134. sanctioned rewrite, and the token-budget tail often keeps a stub the count-protected prune-only tail ruled."""
  135. from agent.context_compressor import SKILL_PRUNED_MARKER_PREFIX, _content_text_for_contains
  136. def unruled(rows):
  137. return [{**m, "content": without_skill_safety_rule(m["content"])}
  138. if isinstance(m, dict) and m.get("role") != "system" and isinstance(m.get("content"), str)
  139. and SKILL_SAFETY_RULE in m["content"] else m for m in rows]
  140. head, tail = unruled(head), unruled(tail)
  141. texts = (summary, *(_content_text_for_contains(m.get("content")) for m in (*head, *tail) if isinstance(m, dict)))
  142. if any(SKILL_SAFETY_RULE in text for text in texts) or not any(SKILL_PRUNED_MARKER_PREFIX in t for t in texts):
  143. return summary, head, tail
  144. return f"{summary}{_SKILL_SAFETY_SUFFIX}", head, tail
  145. _SKILL_SAFETY_SUFFIX = f"\n\n{SKILL_SAFETY_RULE}"
  146. def without_skill_safety_rule(content: str) -> str:
  147. """*content* minus an attached :data:`SKILL_SAFETY_RULE` (a ruled prune stub is still a stub)."""
  148. return content.replace(_SKILL_SAFETY_SUFFIX, "")
  149. def elide_keeping_skill_safety_rule(content: str, limit: int) -> str:
  150. """``elide(content, limit)`` that keeps an attached rule: the head-only cut would drop it from a long handoff
  151. while marker rows beside it survive (anti-growth salvage)."""
  152. from agent.compression_marker import elide
  153. if SKILL_SAFETY_RULE not in content:
  154. return elide(content, limit)
  155. return elide(without_skill_safety_rule(content), limit - len(_SKILL_SAFETY_SUFFIX)) + _SKILL_SAFETY_SUFFIX
  156. def with_pruned_row_skill_safety_rule(original: list[dict[str, Any]], pruned: list[dict[str, Any]]) -> list[dict[str, Any]]:
  157. """*pruned* (a prune-only rewrite of *original*) with the rule on the first row it turned into a prune marker,
  158. when no row carries the rule yet: prune-only has no handoff for :func:`with_skill_safety_rule` to ride."""
  159. from agent.context_compressor import SKILL_PRUNED_MARKER_PREFIX, _content_text_for_contains
  160. if any(SKILL_SAFETY_RULE in _content_text_for_contains(m.get("content")) for m in pruned if isinstance(m, dict)):
  161. return pruned
  162. for i, (before, after) in enumerate(zip(original, pruned)):
  163. content = after.get("content") if isinstance(after, dict) else None
  164. if isinstance(content, str) and SKILL_PRUNED_MARKER_PREFIX in content and content != before.get("content"):
  165. return [*pruned[:i], {**after, "content": content + _SKILL_SAFETY_SUFFIX}, *pruned[i + 1:]]
  166. return pruned