Skip to content

Conversation

@ethanndickson
Copy link
Member

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

…-call-end

When a queued message is sent after tool-call-end, the model needs to see
the complete assistant response including tool results. Previously, there
was a race condition where tool-call-end was emitted before the partial
file was written, causing commitToHistory to read stale data.

The fix ensures partial.json is flushed to disk BEFORE emitting the
tool-call-end event, so listeners (like sendQueuedMessages) see the
complete tool result when they read via commitToHistory.

Changes:
- Make completeToolCall async
- Await flushPartialWrite before emitting tool-call-end
- Update callers to await the async method

_Generated with `mux`_
@ethanndickson
Copy link
Member Author

ethanndickson commented Dec 1, 2025

We still buffer partial writes for stream-delta, but I don't think it makes sense to buffer them for tool-call-end, given they're nowhere near as frequent.

@ethanndickson ethanndickson added this pull request to the merge queue Dec 1, 2025
Merged via the queue into main with commit 463fad6 Dec 1, 2025
13 checks passed
@ethanndickson ethanndickson deleted the fix-queued-message-amnesia branch December 1, 2025 01:13
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