fix(web): sanitize — repair dangling tool calls on older assistant messages#32018
Merged
Conversation
…ssages Occasionally a 'tool_result' SSE event is lost between the daemon and the client (network drop, reconnect race, server-side fanout glitch). The tool call stays 'status: running' forever in the client's DisplayMessage[], even though the assistant clearly continued — there is a subsequent assistant message in the transcript, which is only possible if the LLM provider received the tool result on the server side. The render layer shows these stuck calls as a permanent spinner on an older message bubble, which is misleading: the tool DID complete, the client just never saw the result. This adds 'repairDanglingToolCalls' as Hack #4 in the sanitize pipeline. For every assistant message that is NOT the last assistant in the transcript, any tool call with 'status: running' is rewritten to: status: 'error' isError: true result: '<SYNTHETIC_DANGLING_RESULT>' (explains the client-side data loss for diagnosis) Pipeline placement: AFTER 'removeDuplicateTrailingAssistant' so the dedup filter's pairwise 'result' equality check sees the original (still undefined) values and can correctly identify the duplicate. Why the last assistant is never patched: its dangling tools might still resolve via in-flight stream events. Why a subsequent USER message doesn't qualify as proof: only a later ASSISTANT message proves the LLM provider received the result and continued. A trailing user message could just be a queued send. Tests: 11 new cases under 'sanitizeDisplayMessages · repair dangling tool calls' covering the happy path, last-assistant guard, user-only trailing, sibling tool calls, multiple older assistants, identity preservation, empty input. Integration test extended to assert the patched tool call's result contains 'client-side data loss'. 35/35 tests pass. Lint + audit clean. Typecheck matches the main baseline (10 pre-existing errors in unrelated files).
dvargasfuertes
approved these changes
May 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds Hack #4 —
repairDanglingToolCallstosanitizeDisplayMessages.Occasionally a
tool_resultSSE event is lost between the daemon and the client (network drop, reconnect race, server-side fanout glitch). The tool call staysstatus: "running"forever in the client'sDisplayMessage[], even though the assistant clearly continued — there is a subsequent assistant message in the transcript, which is only possible if the LLM provider received the tool result on the server side.The render layer shows these stuck calls as a permanent spinner on an older message bubble — misleading: the tool DID complete, the client just never saw the result.
How
For every assistant message that is not the last assistant in the transcript, any tool call with
status === "running"is rewritten via COW to:Predicate (must ALL hold)
role: "assistant",status === "running"(matches the UI's canonicalisRunningpredicate fromtool-call-chip.tsx:261).Why a subsequent USER message doesn't qualify
Only a later assistant message proves the LLM provider received the result and continued generating. A trailing user message could just be a queued send.
Pipeline placement
Runs after
removeDuplicateTrailingAssistantso the dedup filter's pairwiseresultequality check sees the original (still undefined) values and can correctly identify the duplicate. If both duplicate trailing assistants carry the same dangling tools, dedup drops one and the remaining one becomes the last assistant — at which point this step conservatively skips it.Tests
11 new cases under
sanitizeDisplayMessages · repair dangling tool calls:status: "completed"tool calls alonestatus: "error"tool calls aloneIntegration test strengthened to assert the patched tool's result contains
"client-side data loss".35/35 tests pass · 54 expect() calls · lint clean · audit clean · typecheck matches main baseline (10 pre-existing errors in unrelated files).
SHORT TERM until
The assistant backend reliably delivers
tool_resultSSE events (or the reconcile pass closes the gap by treating dangling tools as authoritative client-side state to repair against/v1/history).