Fix infinite loop in fix_message_list for out-of-order tool messages causing indefinite hang#411
Open
cryansky wants to merge 2 commits intoaliasrobotics:mainfrom
Open
Conversation
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.
Fix #410
Summary
fix_message_listcaused by an overly strictis_valid_sequencecheck that only considered the immediate predecessor, not the nearest preceding assistant message.Context
The
is_valid_sequencecheck in the second pass offix_message_listonly looked at the immediate predecessor of a tool message:This is correct for single-tool-call assistants (
assistant → tool), but fails for multi-tool-call assistants where the valid sequence isassistant → (agent-as-a-)tool → (agent-as-a-)tool. The second tool message's predecessor is the first tool message, not the assistant, so it's incorrectly flagged as out-of-sequence.Because of that, the reorder logic pops the tool message and inserts it right after the assistant. But this displaces the other tool message to the same index, which then also fails the same check, gets moved, displaces the first one back, and so on. This creates an infinite swap loop at a fixed index
ithat never increments.Fix
Replace the single-predecessor check with a backward walk that skips past other tool messages to find the nearest assistant message. If that assistant contains the matching
tool_call_id, the sequence is valid and no reordering is needed.