Skip to content

Chat turn writes a round's text twice when it stops mid-tool-execution #2962

Closed

Description

@Israeltheminer

What happens

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 settledParts before 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:

const finalParts = [
  ...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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions