fix: track real message count since last summary - #5642
Conversation
The message-count trigger excludes the leading system message and the summary injected by the last applied summary. Both are derived from the context at check time, with the summary matched by identity, so replacing the messages resets the count instead of leaving a stale offset.
|
Thanks for the fix. I pushed a commit on top (bc49297) that keeps your approach but changes when the count is computed. Your version cached the number of excluded leading messages in The commit replaces the cached int with a reference to the summary message the summarizer injected ( I also added three tests in |
markbackman
left a comment
There was a problem hiding this comment.
LGTM! Thanks for the contribution.
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
Fixes #5504
Summary
LLMContextSummarizer._should_summarize()was calculating unsummarized messages usinglen(context.messages) - 1. The issue was that this calculation never reset after running a summary, and it blindly assumed a leading system message was always present.Because of this,
max_unsummarized_messageswas effectively evaluating total context size rather than new messages. It also caused context summarization to loop continuously, as the newly injected summary message was counted as a fresh turn and immediately triggered another summary.Fix
Following the fix suggested in #5504:
- 1with a_messages_since_summary_baselinemarker to track state accurately._apply_summary()to reset the baseline tonum_system_preserved + 1, accounting for both the preserved system message and the newly added summary message._should_summarize()to computemessages_since_summaryagainst this new baseline instead oflen(messages) - 1.Note: This PR only addresses the message-count trigger. The related token-trigger issue (#5505) is intentionally left out and will be handled separately.
Test plan
uv run pytest tests/test_llm_context_summarizer.py tests/test_context_summarization.py