manual_compression_feedback.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """User-facing summaries for manual compression commands."""
  2. from __future__ import annotations
  3. from typing import Any, Sequence
  4. from agent.redact import redact_sensitive_text
  5. def describe_compression_lock_skip(lock_signal: Any) -> str:
  6. """User-facing text for a manual /compress skipped by the compression lock.
  7. ``lock_signal`` is a holder string when another compressor CONFIRMED holds
  8. the lock, else ``True``/``None``. A failed acquire is NOT proof another
  9. compression is running (``try_acquire_compression_lock`` swallows
  10. ``sqlite3.Error``), so the two cases are worded differently.
  11. """
  12. if isinstance(lock_signal, str) and lock_signal.strip():
  13. return (
  14. f"⏳ Compression already in progress for this session "
  15. f"(holder: {lock_signal}). Please wait for it to finish."
  16. )
  17. return (
  18. "⏳ Compression skipped: could not acquire this session's compression lock. Another compression may "
  19. "still be running, or the lock check failed — try again shortly."
  20. )
  21. def summarize_manual_compression(
  22. before_messages: Sequence[dict[str, Any]],
  23. after_messages: Sequence[dict[str, Any]],
  24. before_tokens: int,
  25. after_tokens: int,
  26. *,
  27. compression_state: Any = None,
  28. ) -> dict[str, Any]:
  29. """Consistent user-facing feedback (headline, token line, optional note) for manual compression."""
  30. before_count = len(before_messages)
  31. after_count = len(after_messages)
  32. noop = list(after_messages) == list(before_messages)
  33. def flag(name: str) -> bool:
  34. return getattr(compression_state, name, False) is True
  35. aborted = flag("_last_compress_aborted")
  36. refused_would_grow = flag("_last_compress_refused_would_grow")
  37. fallback_used = flag("_last_summary_fallback_used")
  38. failure_reason = getattr(compression_state, "_last_summary_error", None)
  39. if not isinstance(failure_reason, str) or not failure_reason.strip():
  40. failure_reason = None
  41. note = None
  42. if refused_would_grow:
  43. headline = f"Compression refused (summary would grow the conversation): {before_count} messages preserved"
  44. note = "The generated summary was larger than what it would replace; no messages were removed."
  45. elif aborted:
  46. headline = f"Compression aborted: {before_count} messages preserved"
  47. note = "Summary generation failed; no messages were removed."
  48. elif fallback_used:
  49. headline = f"Compressed with fallback: {before_count} → {after_count} messages"
  50. dropped_count = getattr(compression_state, "_last_summary_dropped_count", None)
  51. if not isinstance(dropped_count, int) or isinstance(dropped_count, bool):
  52. dropped_count = max(before_count - after_count, 0)
  53. note = (
  54. "Summary generation failed; Hermes used limited fallback context "
  55. f"and removed {dropped_count} message(s)."
  56. )
  57. elif noop:
  58. headline = f"No changes from compression: {before_count} messages"
  59. else:
  60. headline = f"Compressed: {before_count} → {after_count} messages"
  61. if after_count < before_count and after_tokens > before_tokens:
  62. note = (
  63. "Note: fewer messages can still raise this estimate when "
  64. "compression rewrites the transcript into denser summaries."
  65. )
  66. if (noop and after_tokens == before_tokens) or refused_would_grow:
  67. token_line = f"Approx request size: ~{before_tokens:,} tokens (unchanged)"
  68. else:
  69. token_line = f"Approx request size: ~{before_tokens:,} → ~{after_tokens:,} tokens"
  70. if failure_reason and (aborted or fallback_used):
  71. # Crosses a user-facing UI boundary: never let a disabled global redaction
  72. # preference expose credentials embedded in provider exception text.
  73. note = f"{note} Reason: {redact_sensitive_text(failure_reason.strip(), force=True)}"
  74. return {
  75. "noop": noop,
  76. "aborted": aborted,
  77. "refused_would_grow": refused_would_grow,
  78. "fallback_used": fallback_used,
  79. "headline": headline,
  80. "token_line": token_line,
  81. "note": note,
  82. }