Skip to content

Flush pending chat delta before emitting tool events in server-chat.ts #39082

@jakepresent

Description

@jakepresent

Problem

When the model produces text followed by a tool call, the Control UI receives the tool event before the final text delta has been delivered. This causes text preceding a tool card to appear truncated during streaming.

Root Cause

In server-chat.ts, emitChatDelta throttles text broadcasts at 150ms intervals (line 374). When a tool event fires, it is broadcast immediately via emitAgentEvent / broadcastToConnIds. There is no flush of the pending chat delta buffer before the tool event is sent.

The server already flushes buffered text before emitChatFinal (around line 425) to ensure the client has the complete text before the final event. The same pattern should apply before tool events.

Expected Behavior

All accumulated text deltas should be flushed to WebSocket clients before the corresponding tool start event is broadcast. This ensures the UI can display the complete text above the tool card without relying on client-side timing heuristics.

Current Workaround

PR #39036 adds chatStreamSegments to commit whatever text has been received at the time a tool card renders. This works but results in truncated text fragments when the tool event arrives before the throttled delta. History reload shows the correct final state.

Proposed Fix

In the tool event emission path (likely where emitAgentEvent is called with stream: "tool" and phase: "start"), flush any pending chat delta for the same runId/sessionKey before broadcasting the tool event. This mirrors the existing flush-before-final pattern.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions