Skip to content

Fix message list handling for multiple tool results#449

Open
Ledraw wants to merge 1 commit into
aliasrobotics:mainfrom
Ledraw:fix-message-list-multi-tool-results
Open

Fix message list handling for multiple tool results#449
Ledraw wants to merge 1 commit into
aliasrobotics:mainfrom
Ledraw:fix-message-list-multi-tool-results

Conversation

@Ledraw
Copy link
Copy Markdown

@Ledraw Ledraw commented May 20, 2026

Summary

This PR fixes an issue in fix_message_list() where valid multi-tool-call sequences could be treated as invalid and repeatedly reordered.

When one assistant message contains multiple tool_calls, the valid message sequence is:

assistant(tool_calls=[call_a, call_b, call_c])
tool(call_a)
tool(call_b)
tool(call_c)

The previous logic only considered a tool message valid if the immediately preceding message was an assistant message with the matching tool_call_id. That works for the first tool result, but incorrectly marks the second and third tool
results as invalid because their previous message is another tool result.

This could cause the tool messages to be repeatedly moved after the assistant message and lead to a non-terminating reorder loop.

Changes

  • Validate tool messages against the nearest preceding assistant tool-call block, not only the immediately preceding message.
  • Preserve the old immediate-predecessor logic as a comment for context.
  • When moving an out-of-order tool result, insert it after the existing tool-result block for that assistant.
  • Advance the loop index after moving a tool message to avoid reprocessing the same slot.
  • Add a regression test for one assistant message followed by three tool results.

Test

./cai/.venv/bin/python -m pytest tests/cli/test_cli_streaming.py -k "fix_message_list" -q

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