fix(summarization): prevent context overflow and orphaned tool messages#228
Draft
christian-bromann wants to merge 1 commit intomainfrom
Draft
fix(summarization): prevent context overflow and orphaned tool messages#228christian-bromann wants to merge 1 commit intomainfrom
christian-bromann wants to merge 1 commit intomainfrom
Conversation
Three issues with the summarization middleware caused agent crashes: 1. No post-summarization safety check: after cutting messages, a single oversized tool result (e.g. a 668K-char glob) in the preserved window could still exceed the model limit. Added emergency truncation both when summarization is not triggered and after summarization completes. 2. Orphaned ToolMessages: determineCutoffIndex could split an AIMessage from its ToolMessages, leaving tool_result blocks without a preceding tool_use. Added adjustCutoffForToolMessages to advance the cutoff past any leading ToolMessages, applied at summarization time and defensively in getEffectiveMessages for backward compatibility. 3. Token estimation included system message and tool schema overhead in the count (matching Python's approach), and the trigger was lowered from 170K to 130K after removing the arbitrary 1.25x safety factor from the proactive trigger path. The safety factor is now only used for hard-limit checks (emergency truncation). Also cleaned up verbose per-call debug logging. Co-authored-by: Cursor <cursoragent@cursor.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three issues with the summarization middleware caused agent crashes:
No post-summarization safety check: after cutting messages, a single oversized tool result (e.g. a 668K-char glob) in the preserved window could still exceed the model limit. Added emergency truncation both when summarization is not triggered and after summarization completes.
Orphaned ToolMessages: determineCutoffIndex could split an AIMessage from its ToolMessages, leaving tool_result blocks without a preceding tool_use. Added adjustCutoffForToolMessages to advance the cutoff past any leading ToolMessages, applied at summarization time and defensively in getEffectiveMessages for backward compatibility.
Token estimation included system message and tool schema overhead in the count (matching Python's approach), and the trigger was lowered from 170K to 130K after removing the arbitrary 1.25x safety factor from the proactive trigger path. The safety factor is now only used for hard-limit checks (emergency truncation).