Commit 463fad6
authored
🤖 fix: prevent queued message amnesia by flushing partial before tool-call-end (#807)
## Problem
When a queued message is sent after `tool-call-end`, the model doesn't
see the complete assistant response including tool results. This causes
"amnesia" where the model starts tasks from scratch.
### Root Cause
Race condition in partial write timing:
```
Tool completes → emit(tool-call-end) → schedulePartialWrite (fire-and-forget)
↓
sendQueuedMessages() → commitToHistory() → reads STALE partial.json
```
The `schedulePartialWrite` was called with `void` (fire-and-forget).
When `sendQueuedMessages` triggers `commitToHistory`, the partial file
may not contain the latest tool results.
## Solution
Await the partial flush **before** emitting `tool-call-end`:
```
Tool completes → flushPartialWrite (await) → emit(tool-call-end)
↓
sendQueuedMessages() → commitToHistory() → reads COMPLETE partial
```
## Changes
- Make `completeToolCall` async
- Await `flushPartialWrite` before emitting `tool-call-end`
- Update callers to await the async method
---
_Generated with `mux`_1 parent 96732fd commit 463fad6
1 file changed
+16
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
566 | 566 | | |
567 | 567 | | |
568 | 568 | | |
569 | | - | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
570 | 572 | | |
571 | | - | |
| 573 | + | |
572 | 574 | | |
573 | 575 | | |
574 | 576 | | |
| |||
578 | 580 | | |
579 | 581 | | |
580 | 582 | | |
581 | | - | |
| 583 | + | |
582 | 584 | | |
583 | 585 | | |
584 | 586 | | |
| |||
609 | 611 | | |
610 | 612 | | |
611 | 613 | | |
612 | | - | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
613 | 621 | | |
614 | 622 | | |
615 | 623 | | |
| |||
618 | 626 | | |
619 | 627 | | |
620 | 628 | | |
621 | | - | |
622 | | - | |
623 | | - | |
624 | 629 | | |
625 | 630 | | |
626 | 631 | | |
| |||
762 | 767 | | |
763 | 768 | | |
764 | 769 | | |
765 | | - | |
766 | | - | |
| 770 | + | |
| 771 | + | |
767 | 772 | | |
768 | 773 | | |
769 | 774 | | |
| |||
799 | 804 | | |
800 | 805 | | |
801 | 806 | | |
802 | | - | |
803 | | - | |
| 807 | + | |
| 808 | + | |
804 | 809 | | |
805 | 810 | | |
806 | 811 | | |
| |||
0 commit comments