diff --git a/packages/ai/src/tools/communication-tools/done-tool/done-tool-execute.ts b/packages/ai/src/tools/communication-tools/done-tool/done-tool-execute.ts index 9df74d9d6..228c20dd0 100644 --- a/packages/ai/src/tools/communication-tools/done-tool/done-tool-execute.ts +++ b/packages/ai/src/tools/communication-tools/done-tool/done-tool-execute.ts @@ -75,11 +75,13 @@ export function createDoneToolExecute(context: DoneToolContext, state: DoneToolS throw new Error('Tool call ID is required'); } - const result = await processDone(state, state.toolCallId, context.messageId, context, input); - - // Wait for all pending updates from delta/finish to complete before returning + // CRITICAL: Wait for ALL pending updates from delta/finish to complete FIRST + // This ensures execute's update is always the last one in the queue await waitForPendingUpdates(context.messageId); + // Now do the final authoritative update with the complete input + const result = await processDone(state, state.toolCallId, context.messageId, context, input); + cleanupState(state); return result; },