You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Chat turn writes a round's text twice when it stops mid-tool-execution #2962
Stopping a chat turn while its tools are running duplicates the model's pre-tool text in the settled message. The reader sees the same paragraph twice.
Why
lib/chat/turn.ts settles a round's reasoning, text and tool calls into settledPartsbefore running that round's tools. Normally the loop then streams again, so by the time the turn finalizes, streamed holds the next round's output and the settled text and the final text never overlap.
A round that breaks out of the loop after that settle block leaves streamed still pointing at what was just settled — and the finalize appends it a second time:
constfinalParts=[
...settledParts,
...(streamed.reasoning ... ),// already in settledParts
...(streamed.text ... ),// already in settledParts];
Two paths do exactly that, both after the settle:
the boundary-flush cancel check (turn.ts ~L1009) — a Stop that landed while the round streamed its tool calls
the per-call cancel check between tool executions (~L1065)
Reproducing
Send a message that triggers a tool call where the model writes something before calling (common), then press Stop while the tool is executing. The settled assistant message carries the pre-tool text twice.
Fix
Track whether the current round already settled its text/reasoning, and have the finalize skip what it would otherwise repeat. Reset the flag at the top of each loop iteration so it only ever describes the round that is about to run its tools.
A verified fix and a regression test exist on the closed branch feat/chat-clarifying-questions (see #2961) — the test streams intro text before a tool call and asserts exactly one text part. Worth lifting rather than rewriting.
Note for whoever picks this up: a fake model that streams no text before its tool call passes straight through the bug, which is why it went unnoticed.
Activity