Repository navigation
Reply plugins: keep each choice of an n > 1 answer apart - #342
Merged
Merged
Conversation
Chat Completions with n > 1 and Gemini with candidateCount > 1 stream several choices (candidates) in one answer, told apart by the `index` field of each choice. The reply plugin chain read only the first element of `choices` / `candidates` and sent all text through one lane, so: - text of interleaved choices went into one buffer: a block-mode plugin got "hel" + "wor" + "lo" + "ld" as one block, and half a secret held back for one choice was handed out at the head of the other choice; - Chat tool call fragments were keyed by the tool call index only, so call 0 of choice 0 and call 0 of choice 1 were glued into one call; - choices after the first in a frame, and in a whole body, never went through the plugins at all. Each choice now has its own state, keyed by its `index`: its own text lane (the lane number is the choice index), its own tool call buffer and numbering, and a `finish_reason` ends only that choice. `[DONE]`, error frames and the usage chunk still close every choice. Whole bodies run every choice through the plugins. Gemini candidates get the same treatment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fylorn
force-pushed
the
fix/plugin-reply-choices
branch
from
October 16, 2026 09:28
4b45ba5 to
226d9b5
Compare
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Problem
Chat Completions with
n > 1and Gemini withcandidateCount > 1carry several choices (candidates) in one answer, told apart by each choice'sindex. The reply plugin chain only looked at the first element ofchoices/candidatesand sent all text through a single lane:hel+wor+lo+ldas one block and wrote it back into choice 0 (HELWORLOLD).indexonly, so call 0 of choice 0 and call 0 of choice 1 were glued into one call (unparseable arguments), and choice 1 lost its call.Fix
plugin/reply/chat.rsandplugin/reply/gemini.rsnow keep state per choice, keyed by the choice'sindexfield (falling back to the array position when it is missing):finish_reasonrewrite; afinish_reasonends only that choice, while[DONE], error frames and the usage chunk still close every choice;index;reply/mod.rsis unchanged.Tests
New tests in
plugin/reply/tests(all four fail onmain):chat_choices_are_kept_apart_in_a_stream: two interleaved choices, each with text and tool calls; a block-mode text plugin plus a tool call rewriter. Each choice gets its own text and its own rewritten calls.chat_choices_hold_back_apart_in_stream_mode: a stream-mode plugin with a known secret split across two frames of choice 0 and choice 1 text in between. The held half stays with choice 0.gemini_candidates_are_kept_apart_in_a_stream: interleaved candidates, with a final frame carrying both candidates (including a function call in the second one).whole_bodies_run_every_choice_through_the_plugins: Chat and Gemini whole bodies with two choices.cargo fmt --all -- --check,cargo clippy --workspace --all-targets -- -D warningsandcargo test --workspacepass locally.Not covered here: a reply plugin instance is still one per answer and
onReplyText/onReplyTextEnddo not say which choice a piece belongs to, so a stream-mode plugin that buffers text in its own state still sees the choices interleaved.🤖 Generated with Claude Code