Fix infinite loop in fix_message_list with multiple tool_calls#424
Open
juliosuas wants to merge 1 commit intoaliasrobotics:mainfrom
Open
Fix infinite loop in fix_message_list with multiple tool_calls#424juliosuas wants to merge 1 commit intoaliasrobotics:mainfrom
juliosuas wants to merge 1 commit intoaliasrobotics:mainfrom
Conversation
…l_calls When an assistant message has multiple tool_calls and the tool responses arrive out of order, the second pass of fix_message_list enters an infinite loop. This happens because the validity check only looks at the immediately preceding message (which may be a sibling tool response) rather than walking backward past sibling tool messages to find the parent assistant message. The fix walks backward past sibling tool messages to locate the nearest assistant message before checking whether the sequence is valid. This correctly handles the case where multiple tool responses follow the same assistant message. Fixes aliasrobotics#410
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.
Summary
fix_message_list()(src/cai/util.py) that occurs when an assistant message has multipletool_callsand the tool responses arrive out of orderRoot Cause
The second pass of
fix_message_listensures tool messages follow their parent assistant message. The check at line 1250 only compared againstprocessed_messages[i - 1], but when an assistant has two tool calls (call_1,call_2), the tool response at index 2 sees a sibling tool response at index 1 — not the assistant — and incorrectly moves it. This creates an infinite ping-pong between the two tool responses.Fix
Walk backward from the current position past any sibling
toolmessages to locate the nearestassistantmessage, then check if it owns the current tool response. This correctly handles N tool responses following a single assistant message.Reproduction
Fixes #410
🤖 Generated with Claude Code