fix: strip leaked EOS tokens and correct finish_reason for local models (#578)#643
Open
MrRealORG wants to merge 1 commit into
Open
fix: strip leaked EOS tokens and correct finish_reason for local models (#578)#643MrRealORG wants to merge 1 commit into
MrRealORG wants to merge 1 commit into
Conversation
…ls (XiaomiMiMo#578) Three-pronged fix for agent loop terminating after a single tool call when using local OpenAI-compatible providers (Gemma via oMLX, etc.): 1. Strip leaked EOS tokens in classify.ts - Added regex matching common EOS tokens: <eos>, </s>, <end_of_turn>, <|im_end|>, <|end|>, <|eot_id|> - Applied in both text and reasoning content checks so leaked tokens don't count as meaningful content - A step with ONLY leaked EOS tokens now correctly falls through to 'invalid' classification, triggering the autoContinueInvalidOutput nudge instead of terminating 2. Strip leaked EOS tokens in processor.ts (text-end handler) - Cleans stored messages so tokens don't appear in the TUI or session history - Applied after the plugin hook to avoid interfering with plugins that may handle raw content 3. Correct finish_reason when tool_calls are present - Some local providers return finish_reason 'stop' even when the response contains tool_calls - Now overrides to 'tool-calls' when toolCalls were parsed, ensuring classify check XiaomiMiMo#3 (finish === 'tool-calls') returns 'continue' correctly
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.
Fixes #578 — agent loop terminating after a single tool call with local OpenAI-compatible providers (Gemma via oMLX, llama.cpp, etc.).
Root Cause (three interacting issues)
Leaked EOS tokens treated as content: Gemma and similar models emit tokens as literal text. The classify function saw non-empty text and returned final, terminating the loop.
No content cleaning: The leaked tokens were stored as-is in messages, appearing in the TUI and session history.
Wrong finish_reason: Some local providers return finish_reason stop even when tool_calls are present in the response.
Fix (three-pronged)
session/classify.ts — Strip known EOS tokens before content checks. A step with ONLY leaked EOS now falls to invalid classification, triggering the autoContinueInvalidOutput nudge.
session/processor.ts — Strip leaked EOS from stored messages so tokens do not appear in TUI or history.
openai-compatible-chat-language-model.ts — When tool_calls were parsed but finish_reason is not tool-calls, override to tool-calls so classify check 3 correctly returns continue.