fix: prevent time-travel bug in parallel tool calling #11046
Merged
+146
−1
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.
Problem
When multiple tools are called in parallel (e.g.,
update_todo_list+new_task), tool_results could appear in API history BEFORE their corresponding tool_use blocks, causing Anthropic API 400 errors:Example of malformed history:
Should be:
Root Cause
Tools executed during streaming via
presentAssistantMessage()BEFORE the assistant message was saved to history. The sequence was:presentAssistantMessageimmediately executes completed toolsupdate_todo_listexecutes → pushes tool_result touserMessageContentnew_taskexecutes → callsflushPendingToolResultsToHistory()Solution
Added
assistantMessageSavedToHistorycoordination flag to ensure correct message ordering:flushPendingToolResultsToHistory()waits for the flag before flushing (Task.ts:1074-1110)[assistant with tool_uses] → [user with tool_results]Also removed the "CRITICAL: This tool MUST be called alone" restriction from
new_tasktool description to enable parallel tool calling.Changes
assistantMessageSavedToHistoryflag and waiting logic influshPendingToolResultsToHistory()Testing
All 8 tests pass in the new test file, including tests verifying:
All 5228 tests pass across the entire codebase.
Related
This fix enables parallel tool calling (when the
multipleNativeToolCallsexperiment is enabled) by ensuring the assistant message is always saved to history before tool_results are flushed, preventing the time-travel bug.Important
Fixes a bug in
Task.tsby adding a coordination flag to ensure tool results are saved after assistant messages, preventing API errors.assistantMessageSavedToHistoryflag inTask.tsto ensure tool results are saved after assistant messages.flushPendingToolResultsToHistory()now waits for the flag before proceeding.new_tasktool for parallel execution.flushPendingToolResultsToHistory.spec.tsto verify waiting behavior and flag handling.Task.tsto reset and set the flag at appropriate points in the task lifecycle.This description was created by
for bf7a662. You can customize this summary. It will automatically update as commits are pushed.