#5681: fix(gateway): flush text buffer before tool events in webchat
gateway
Cluster:
Tool Result Handling Improvements
## 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
#22994: fix(gateway): flush text buffer before tool events to prevent data ...
by youngkent · 2026-02-21
81.2%
#8353: fix(ui): display tool calls during webchat streaming
by MarvinDontPanic · 2026-02-03
79.4%
#9248: Fix: Webchat UI goes grey/unresponsive after Slack message tool calls
by vishaltandale00 · 2026-02-05
76.3%
#15996: fix(agents): messages arrive out of order — tool output beats narra...
by yinghaosang · 2026-02-14
76.2%
#14946: fix(webchat): accumulate text across blocks in streaming buffer
by mcaxtr · 2026-02-12
75.2%
#22886: fix: await onBlockReplyFlush before tool execution to preserve mess...
by botverse · 2026-02-21
75.0%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
75.0%
#7760: fix(agents): resolve message ordering conflict during tool execution
by aryan877 · 2026-02-03
74.6%
#14309: fix(ui): resolve chat event session key mismatch
by justonlyforyou · 2026-02-11
74.3%
#14811: feat(gateway): route chat/agent events per-connection instead of glob…
by jiangjin11 · 2026-02-12
74.3%