Commit bf7a662
committed
fix: prevent time-travel bug in parallel tool calling
## 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:
"unexpected `tool_use_id` found in `tool_result` blocks"
## Root Cause
Tools executed during streaming via presentAssistantMessage() BEFORE the
assistant message was saved to history. When new_task triggered delegation
and called flushPendingToolResultsToHistory(), it saved a user message
(with tool_results) before the assistant message (with tool_uses) existed.
## Solution
Added assistantMessageSavedToHistory coordination flag to ensure correct
message ordering:
1. Flag is reset at the start of each API request
2. Flag is set to true after assistant message is saved to history
3. flushPendingToolResultsToHistory() waits for the flag before flushing
4. This ensures: [assistant with tool_uses] → [user with tool_results]
Also removed the "CRITICAL: This tool MUST be called alone" restriction
from new_task tool description to enable parallel tool calling.
## Changes
- src/core/task/Task.ts: Added assistantMessageSavedToHistory flag and
waiting logic in flushPendingToolResultsToHistory()
- src/core/prompts/tools/native-tools/new_task.ts: Removed restriction
preventing parallel tool calling
- src/core/task/__tests__/flushPendingToolResultsToHistory.spec.ts: Added
tests for waiting behavior
## Testing
All 8 tests pass, including new tests verifying:
- Skips waiting when flag is already true
- Waits via pWaitFor when flag is false
- Aborts without flushing when task is aborted during wait1 parent e7965d9 commit bf7a662
File tree
2 files changed
+146
-1
lines changed- src/core/task
- __tests__
2 files changed
+146
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
355 | 355 | | |
356 | 356 | | |
357 | 357 | | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
358 | 372 | | |
359 | 373 | | |
360 | 374 | | |
| |||
1063 | 1077 | | |
1064 | 1078 | | |
1065 | 1079 | | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
1066 | 1110 | | |
1067 | 1111 | | |
1068 | 1112 | | |
| |||
2707 | 2751 | | |
2708 | 2752 | | |
2709 | 2753 | | |
| 2754 | + | |
2710 | 2755 | | |
2711 | 2756 | | |
2712 | 2757 | | |
| |||
3488 | 3533 | | |
3489 | 3534 | | |
3490 | 3535 | | |
| 3536 | + | |
3491 | 3537 | | |
3492 | 3538 | | |
3493 | 3539 | | |
| |||
Lines changed: 100 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
41 | 45 | | |
42 | | - | |
| 46 | + | |
43 | 47 | | |
44 | 48 | | |
45 | 49 | | |
| |||
344 | 348 | | |
345 | 349 | | |
346 | 350 | | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
347 | 446 | | |
0 commit comments