Skip to content

fix: prevent infinite loops and 400 errors in message history management#41

Merged
bigboateng merged 2 commits intomainfrom
fix/extended-thinking-400-error
Dec 15, 2025
Merged

fix: prevent infinite loops and 400 errors in message history management#41
bigboateng merged 2 commits intomainfrom
fix/extended-thinking-400-error

Conversation

@bigboateng
Copy link
Contributor

@bigboateng bigboateng commented Dec 15, 2025

Important

Fixes infinite loops and 400 errors by ensuring thinking block presence and tool use/result pairing in message history management.

  • Behavior:
    • Fixes infinite loops by adding placeholder thinking blocks when Claude doesn't emit them, using ensureThinkingBlockForResponse() in loop.ts.
    • Prevents 400 errors by ensuring every assistant message starts with a thinking block when thinkingBudget is enabled.
    • Ensures tool use/result pairing by rewriting cleanMessageHistory() in utils/message-processing.ts.
  • Functions:
    • Adds ensureThinkingBlockForResponse() in utils/message-processing.ts to handle missing thinking blocks.
    • Updates cleanMessageHistory() to validate tool use/result pairing on a per-message basis.
  • Testing:
    • Extended thinking test passes with multiple tool uses in 27.5s without loops.
    • Properly handles missing thinking blocks by adding placeholders.

This description was created by Ellipsis for 5e7c22c. You can customize this summary. It will automatically update as commits are pushed.

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.
@ellipsis-dev ellipsis-dev bot changed the title ... fix: prevent infinite loops and 400 errors in message history management Dec 15, 2025
Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

Looks good to me! 👍

Reviewed everything up to 5e7c22c in 58 seconds. Click for details.
  • Reviewed 167 lines of code in 3 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft 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% <= threshold 50% 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% <= threshold 50% 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% <= threshold 50% None

Workflow ID: wflow_Q8sIP977rldlW1SW

You can customize Ellipsis 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
Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bigboateng bigboateng merged commit 1aeb2c9 into main Dec 15, 2025
1 check passed
@bigboateng bigboateng deleted the fix/extended-thinking-400-error branch December 15, 2025 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments