Skip to content

Fix infinite loop in fix_message_list with multiple tool_calls#424

Open
juliosuas wants to merge 1 commit intoaliasrobotics:mainfrom
juliosuas:fix/infinite-loop-fix-message-list
Open

Fix infinite loop in fix_message_list with multiple tool_calls#424
juliosuas wants to merge 1 commit intoaliasrobotics:mainfrom
juliosuas:fix/infinite-loop-fix-message-list

Conversation

@juliosuas
Copy link
Copy Markdown

Summary

  • Fixes an infinite loop in fix_message_list() (src/cai/util.py) that occurs when an assistant message has multiple tool_calls and the tool responses arrive out of order
  • The validity check now walks backward past sibling tool messages to find the parent assistant, instead of only checking the immediately preceding message

Root Cause

The second pass of fix_message_list ensures tool messages follow their parent assistant message. The check at line 1250 only compared against processed_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 tool messages to locate the nearest assistant message, then check if it owns the current tool response. This correctly handles N tool responses following a single assistant message.

Reproduction

from cai.util import fix_message_list

messages = [
    {"role": "user", "content": "Validate alert"},
    {"role": "assistant", "tool_calls": [
        {"id": "call_1", "type": "function", "function": {"name": "verify_wiz", "arguments": "{}"}},
        {"id": "call_2", "type": "function", "function": {"name": "verify_github", "arguments": "{}"}}
    ]},
    {"role": "tool", "tool_call_id": "call_2", "content": "GitHub result"},
    {"role": "tool", "tool_call_id": "call_1", "content": "Wiz result"},
]

fix_message_list(messages)  # Previously hangs forever, now returns correctly

Fixes #410

🤖 Generated with Claude Code

…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
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.

Infinite Loop Bug in fix_message_list Function when using Nested Agent-as-a-tool caused CAI to Hang Indefinitely

1 participant