← Back to PRs

#5681: fix(gateway): flush text buffer before tool events in webchat

by MaudeBot open 2026-01-31 20:46 View on GitHub →
gateway
## Problem Tool events were racing text deltas in webchat streaming. Text deltas are throttled (150ms) while tool events broadcast immediately, causing clients to receive tool events before the text that precedes them. **Example:** When the assistant writes `"Here's what I found" + [tool_use]`, the client might receive: 1. Tool event (immediately) 2. Text delta (after throttle) This caused text to appear truncated or in the wrong position relative to tool cards. ## Solution Force-flush any buffered text before broadcasting tool `start` events: 1. Added `force` parameter to `emitChatDelta()` to bypass the 150ms throttle 2. Before broadcasting a tool event, check for buffered text and emit it immediately ## Testing Tested with multiple sequential tool calls with text between them: - Text → Tool → Text → Tool → Text - All text appeared in correct positions with no truncation ## Changes - `src/gateway/server-chat.ts`: +20 lines, -2 lines <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR adds an explicit flush path for throttled webchat text deltas to prevent tool `start` events from arriving before the preceding assistant text. It does so by adding a `force` flag to `emitChatDelta()` to bypass the 150ms throttle, and by checking for buffered text immediately before broadcasting tool start events. This fits into the gateway’s streaming pipeline in `src/gateway/server-chat.ts`, where assistant text deltas are buffered/throttled for webchat while tool events are broadcast immediately to both the global `broadcast("agent")` stream and per-session `nodeSendToSession` stream. <h3>Confidence Score: 3/5</h3> - This PR is likely safe to merge, but the forced flush’s sequence numbering could cause subtle ordering/seq-gap issues in some clients. - The change is small and targeted, but it introduces a new code path that emits chat deltas using the tool event sequence number and doesn’t clearly delineate buffer semantics on forced flush. If downstream consumers assume per-stream sequencing, this may lead to hard-to-debug UI ordering issues. - src/gateway/server-chat.ts (sequence semantics for forced flush) <!-- greptile_other_comments_section --> <sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub> **Context used:** - Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8)) - Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13)) <!-- /greptile_comment -->

Most Similar PRs