Repository navigation
fix(server): close the streamed vs non-streamed parity gaps from #2458 - #2500
Conversation
PR #2458 left eight cases where a client saw different content or reasoning depending on whether it streamed. The non-streamed cleaner is the reference except where both paths change. - StreamFilter holds trailing content whitespace until visible text decides it, so whitespace before a final tool call (`<tool_call>`, `<|python_tag|>`) never streams and nothing is retracted; whitespace after a leading call is dropped as leading whitespace. - Harmony bodies are trimmed and joined with a newline in the stream, for content and `analysis` reasoning, as try_harmony joins them; reasoning_tokens stay equal on Responses. - A `to=` recipient in the Harmony role header is a tool call in try_harmony and in the filter, via a shared `harmony_role_suffix`. - Kimi-VL `◁think▷` / `◁/think▷` route to reasoning on both paths; a MiniMax M3 block leaves no namespace token in no-tools content. - Under `--reasoning-format none`, thoughts reach content only inside a delimited block (`ThinkingDelimiterEcho::thoughts_in_content`, used by routes/chat.rs and both router_front paths), so gpt-oss returns the answer only, matching the non-streamed reply. Each item has a parity test in stream_parity_tests.rs across all chunkings; all eight fail with the main sources. Ten filter unit tests now pin the held trailing space. Refs #2464
The #2464 trailing-whitespace hold dropped whitespace that followed a thinking close marker when no answer came after it, so under `--reasoning-format none` a reply such as `<think>r</think> \n` streamed as `<think>r</think>` while the non-streamed reply kept the trailing run. The same loss hit `</think> <tool_call>...</tool_call>B`, where the space before the call was dropped as leading whitespace. StreamFilter now records which part of the held whitespace directly follows a thinking close, bounded by the next delimiter, and reports it as `post_thinking_gap` when that whitespace is dropped at end of stream or after a tool call, matching `post_close_whitespace`. Content output is unchanged. A route-level parity test covers spaces after the close, mixed newlines and spaces, an answer after the gap, and the tool-call cases, fed at every fragment count. It fails without the fix. The tool_calls, routes, responses, router_front, muse_atem and reasoning_echo suites, clippy and fmt pass. Refs #2464
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
|
|
Security and performance pass on the #2464 parity fixes, with model output treated as untrusted. No CRITICAL or HIGH findings, so no commits were added. MEDIUM: streamed tool-call arguments leak into LOW: role-header lookback window vs Measured, no finding:
Pre-existing, not from this PR: Gates: |
Security review of #2500: the stream held only the exact Muse `<|start|>assistant to=` prefix, so a role header with two spaces, a tab or a newline before `to=` drained as content before its `<|channel|>` arrived, and the call arguments then streamed as content too. `unterminated_header_start` (and through it `safe_emit_length`) now also holds `<|start|>assistant` followed by whitespace and no `<|`, optionally ending in a partial `<|channel|>`, the shape `harmony_role_suffix` accepts. The role suffix is now capped at `HARMONY_MAX_ROLE_SUFFIX` (256 bytes) in `harmony_role_suffix`, the same bound the stream holds and looks back, so `try_harmony` and the filter accept the same role headers. New parity test `harmony_role_header_with_unusual_whitespace_is_held_until_it_resolves` covers double space, tab and newline across all chunkings; it failed before the fix with the arguments in streamed content. Refs #2464
… notes Add the Unreleased/Fixed entry for the eight streamed vs non-streamed gaps. Extend the #2464 paragraph in docs/supported-models.md with the Muse to=self rule under --reasoning-format none, end-of-stream whitespace trimming, and the router_front non-streamed none output now carrying the thinking markers. Validation: tool_calls, routes, responses and router_front lib tests pass with MLXCEL_BUILD_METAL=0; clippy --lib --tests -D warnings and cargo fmt --check are clean. Refs #2464
Summary
Closes the eight streamed vs non-streamed parity gaps #2458 left (issue items 1-6 plus 7 and 8 from the comment). The non-streamed cleaner is the reference; items 2, 5 and 6 change both paths. The fixes sit in
StreamFilter, the parsers, and the live routes (routes/chat.rs, bothrouter_front.rspaths).Item 1 decision: Harmony has no canonical thinking-marker pair, so under
--reasoning-format nonegpt-oss returns the answer only, streamed or not, as the non-streamed reply already did. The newThinkingDelimiterEcho::thoughts_in_contentputs thoughts in content only inside a delimited block (an echoed open marker). Museto=selfreasoning, which also has no pair, now follows the same rule.Behavior changes a client can see
analysisbodies are trimmed and joined with\nin the stream. Responsesreasoning_tokensstay equal.to=functions.Xrecipient in the Harmony role header is a tool call on both paths (sharedharmony_role_suffix).]<]minimax[>[residue in no-tools content. Kimi-VL◁think▷/◁/think▷go to reasoning.nonenow carries the markers and the post-close gap, where it used to append bare reasoning.Changes during review
</think>undernone(1edd9916). The item 8 hold dropped whitespace after a thinking close when no answer followed (<think>r</think> \n), and dropped the space in</think> <tool_call>..</tool_call>B. A newpost_close_wsspan inStreamFilterreports that whitespace aspost_thinking_gapwhen it is dropped, matching the non-streamedpost_close_whitespace. Test:whitespace_after_the_close_marker_with_no_answer_survives.8adab292). With two spaces, a tab or a newline beforeto=, the stream drained<|start|>assistant to=functions.fas content and leaked the call arguments.pending_harmony_role_headernow holds<|start|>assistantplus whitespace (no<|) up to 256 bytes.formats::HARMONY_MAX_ROLE_SUFFIXcaps the role suffix for bothtry_harmonyand the stream, so both accept the same headers. Test:harmony_role_header_with_unusual_whitespace_is_held_until_it_resolves.2dfca808):CHANGELOG.mdanddocs/supported-models.md.Measured (security review): the whitespace hold is linear (1 MiB of spaces fed per byte costs the same as 1 MiB of letters).
held_wshas no cap, but the routes already keep the full raw text. 50k Harmony messages stream in 0.47 s. 20k random multibyte inputs produced no panics.Known, not in this PR:
try_harmonyis quadratic in message count (pre-existing; about 40 ms at 32k tokens). Whitespace at the very start of a generation still streams. A prompt that already ends with<|start|>assistantleaves a bareto=functions.Xthat neither path reads. A primed Kimi-VL◁think▷close is not recognized byprimed_open_thinking_close_marker.Test plan
stream_parity_tests.rs, acrosschunkings(). All eight fail when run against the main sources: source hunks stashed, test file kept, plus a temporary main-equivalentthoughts_in_contentfor item 1.--libtargets:server::tool_calls::(468),server::routes::(461),server::responses,server::router_front,server::muse_atem,server::reasoning_echo,server::in_process,server::chat_request,server::anthropic. All pass on a CPU build.cargo clippy -p mlxcel --lib --tests -- -D warningsand rustfmt on touched files are clean.Resolves #2464