fix: prevent infinite loops and 400 errors in message history management#41
Merged
bigboateng merged 2 commits intomainfrom Dec 15, 2025
Merged
fix: prevent infinite loops and 400 errors in message history management#41bigboateng merged 2 commits intomainfrom
bigboateng merged 2 commits intomainfrom
Conversation
Changed approach to handle missing thinking blocks in extended thinking mode: OLD APPROACH (caused loops): - Removed assistant messages without thinking blocks from history - Caused excessive context loss - Agent would forget recent actions and repeat them NEW APPROACH (prevents loops): - Add placeholder thinking block to NEW response if Claude doesn't emit one - Reuse the most recent thinking block from history as placeholder - Preserves all context in message history - Prevents 400 errors while maintaining agent memory This matches the pattern from production systems and prevents infinite loop issues where agents repeatedly call the same action (e.g., goto). The API error refers to the NEW message being added, not old messages in history. We fix the new response, not remove old context. Tested: Extended thinking example completes successfully in 27.5s without loops.
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed everything up to 5e7c22c in 58 seconds. Click for details.
- Reviewed
167lines of code in3files - Skipped
0files when reviewing. - Skipped posting
3draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. .changeset/fix-extended-thinking-blocks.md:5
- Draft comment:
The release note now clearly lists the three major fixes (infinite loop prevention, extended thinking block validation, and tool use/result pairing) and matches the implementation changes. Make sure this documentation remains in sync with future changes. - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
2. loop.ts:257
- Draft comment:
The new call to ensureThinkingBlockForResponse properly injects a placeholder thinking block when missing. Using '!!thinkingBudget' to toggle this behavior is clear. Consider if deep cloning might be needed if the thinking block structure becomes more complex. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
3. utils/message-processing.ts:296
- Draft comment:
In ensureThinkingBlockForResponse, the placeholder thinking block is cloned using a shallow spread. This is acceptable for now, but consider a deep clone if nested properties might be modified in the future. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
Workflow ID: wflow_Q8sIP977rldlW1SW
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
Replaced old changeset (already consumed) with new one covering: - Infinite loop prevention with placeholder thinking blocks - Extended thinking 400 error fixes - Tool use/result pairing validation
There was a problem hiding this comment.
Skipped PR review on 58a2632 because no changed files had a supported extension. If you think this was in error, please contact us and we'll fix it right away.
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.
Important
Fixes infinite loops and 400 errors by ensuring thinking block presence and tool use/result pairing in message history management.
ensureThinkingBlockForResponse()inloop.ts.thinkingBudgetis enabled.cleanMessageHistory()inutils/message-processing.ts.ensureThinkingBlockForResponse()inutils/message-processing.tsto handle missing thinking blocks.cleanMessageHistory()to validate tool use/result pairing on a per-message basis.This description was created by
for 5e7c22c. You can customize this summary. It will automatically update as commits are pushed.