partial_compress.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """Boundary-aware partial compression — "summarize up to here".
  2. * **Role alternation.** The compressed head ends with summary/handoff content (assistant- or user-
  3. role, possibly a trailing todo snapshot). The verbatim tail must begin with a ``user`` message so
  4. the rejoined history keeps the user↔assistant alternation that providers validate.
  5. * **No silent context mutation.** Manual, user-invoked; rotates the session exactly like
  6. ``/compress`` (via the caller), so the prompt-cache reset is explicit and expected.
  7. """
  8. from __future__ import annotations
  9. from typing import Any, Dict, List, Optional, Tuple
  10. #: Recent exchanges preserved verbatim when ``/compress here`` has no explicit count.
  11. DEFAULT_KEEP_LAST = 2
  12. #: Hard ceiling so a fat-fingered ``/compress here 9999`` clamps instead of no-op'ing.
  13. MAX_KEEP_LAST = 100
  14. def parse_partial_compress_args(raw_args: str) -> tuple[bool, int, Optional[str]]:
  15. """Parse the argument string after ``/compress`` into ``(partial, keep_last, focus_topic)``.
  16. ``here [N]`` / ``up to here [N]`` / ``--keep N`` / ``-k N`` / ``--keep=N`` select the
  17. boundary-aware form; anything else is a focus topic for full ``/compress <focus>``.
  18. """
  19. text = (raw_args or "").strip()
  20. if not text:
  21. return False, DEFAULT_KEEP_LAST, None
  22. lowered = text.lower()
  23. if lowered.startswith("up to here"):
  24. lowered = lowered[len("up to ") :]
  25. text = text[len("up to ") :]
  26. tokens = lowered.split()
  27. head = tokens[0] if tokens else ""
  28. if head == "here":
  29. keep = _coerce_keep(tokens[1]) if len(tokens) >= 2 else DEFAULT_KEEP_LAST
  30. return True, keep, None
  31. if head in ("--keep", "-k") and len(tokens) >= 2:
  32. return True, _coerce_keep(tokens[1]), None
  33. if head.startswith("--keep="):
  34. return True, _coerce_keep(head.split("=", 1)[1]), None
  35. return False, DEFAULT_KEEP_LAST, text or None
  36. def extract_compress_flags(raw_args: str) -> tuple[str, bool, bool]:
  37. """Strip ``--preview``/``--dry-run``/``--aggressive`` (anywhere in the string) and return
  38. ``(remaining_args, preview, aggressive_requested)``.
  39. ``preview`` means report what WOULD be compressed and change nothing. No surface implements an
  40. LLM-free hard-truncate path, so callers surface "not supported" for ``--aggressive`` instead of
  41. treating it as a focus topic.
  42. """
  43. preview = False
  44. aggressive = False
  45. kept: list[str] = []
  46. for tok in (raw_args or "").split():
  47. low = tok.lower()
  48. if low in ("--preview", "--dry-run", "--dryrun"):
  49. preview = True
  50. elif low == "--aggressive":
  51. aggressive = True
  52. else:
  53. kept.append(tok)
  54. return " ".join(kept), preview, aggressive
  55. def summarize_compress_preview(
  56. history: list[dict[str, Any]],
  57. partial: bool,
  58. keep_last: int,
  59. focus_topic: Optional[str],
  60. approx_tokens: int,
  61. ) -> dict[str, Any]:
  62. """Build the ``/compress --preview`` report — pure, no side effects.
  63. Shared by the CLI and the gateway slash handler so both report the numbers the real run would
  64. use. Returns ``head_count``/``tail_count``/``total``/``partial``/``lines`` (ready to print).
  65. """
  66. total = len(history)
  67. head, tail = list(history), []
  68. effective_partial = False
  69. if partial:
  70. head, tail = split_history_for_partial_compress(history, keep_last)
  71. # Same degenerate-split fallback the real run applies.
  72. effective_partial = bool(tail)
  73. lines = [
  74. "Preview — no changes made.",
  75. f"Would compress {len(head)} of {total} message(s) "
  76. f"(~{approx_tokens:,} tokens currently in context).",
  77. ]
  78. if effective_partial:
  79. lines.append(
  80. f"Boundary: keeping the last {keep_last} exchange(s) "
  81. f"({len(tail)} message(s)) verbatim."
  82. )
  83. elif partial:
  84. lines.append("Boundary: 'here' split would keep everything — falling back to full compression.")
  85. if focus_topic:
  86. lines.append(f'Focus topic: "{focus_topic}"')
  87. lines.append("Run the command again without --preview to apply.")
  88. return {
  89. "head_count": len(head),
  90. "tail_count": len(tail),
  91. "total": total,
  92. "partial": effective_partial,
  93. "lines": lines,
  94. }
  95. def _coerce_keep(value: str) -> int:
  96. """Parse a keep-count token, clamping to [1, MAX_KEEP_LAST]."""
  97. try:
  98. n = int(value)
  99. except (TypeError, ValueError):
  100. return DEFAULT_KEEP_LAST
  101. return max(1, min(n, MAX_KEEP_LAST))
  102. def split_history_for_partial_compress(
  103. history: list[dict[str, Any]],
  104. keep_last: int,
  105. ) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
  106. """Split ``history`` into ``(head, tail)``: head is summarized, tail is the last ``keep_last``
  107. exchanges kept verbatim.
  108. Exchanges are counted by ``user`` messages so the tail always starts on a user turn and
  109. ``compressed_head + tail`` keeps alternation valid. Returns ``(history, [])`` when the head
  110. would be empty (or there are no user turns), signaling the caller to fall back to full
  111. compression rather than rotating the session for a no-op.
  112. """
  113. keep_last = max(keep_last, 1)
  114. if not history:
  115. return [], []
  116. # Walk backwards to the earliest of the most recent `keep_last` user-message starts.
  117. boundary = None
  118. seen = 0
  119. for idx in range(len(history) - 1, -1, -1):
  120. if history[idx].get("role") == "user":
  121. boundary = idx
  122. seen += 1
  123. if seen >= keep_last:
  124. break
  125. if not boundary: # no user turns, or everything is in the tail
  126. return list(history), []
  127. return history[:boundary], history[boundary:]
  128. def rejoin_compressed_head_and_tail(
  129. compressed_head: list[dict[str, Any]],
  130. tail: list[dict[str, Any]],
  131. ) -> list[dict[str, Any]]:
  132. """Concatenate a compressed head with the verbatim tail, defending the seam's role alternation.
  133. The head compressor's output shape isn't contractually guaranteed (a plugin engine could end on
  134. a user turn). If the last head message and first tail message share a user/assistant role, the
  135. tail's first string content is folded onto the head's last message; non-string (multimodal)
  136. content gets a minimal bridging turn instead so nothing is lost. ``tool`` messages are left
  137. alone — consecutive tool entries are the one legal repetition (parallel results).
  138. """
  139. if not tail:
  140. return list(compressed_head)
  141. if not compressed_head:
  142. return list(tail)
  143. head = list(compressed_head)
  144. rest = list(tail)
  145. last, first = head[-1], rest[0]
  146. last_role, first_role = last.get("role"), first.get("role")
  147. if last_role == first_role and last_role in ("user", "assistant"):
  148. last_content = last.get("content")
  149. first_content = first.get("content")
  150. if isinstance(last_content, str) and isinstance(first_content, str):
  151. head[-1] = {**last, "content": f"{last_content}\n\n{first_content}"}
  152. rest = rest[1:]
  153. else:
  154. head.append({"role": "assistant" if first_role == "user" else "user", "content": ""})
  155. return head + rest