Skip to content

fix: strip leaked EOS tokens and correct finish_reason for local models (#578)#643

Open
MrRealORG wants to merge 1 commit into
XiaomiMiMo:mainfrom
MrRealORG:fix/auto-202606150510-eos-token-strip
Open

fix: strip leaked EOS tokens and correct finish_reason for local models (#578)#643
MrRealORG wants to merge 1 commit into
XiaomiMiMo:mainfrom
MrRealORG:fix/auto-202606150510-eos-token-strip

Conversation

@MrRealORG

Copy link
Copy Markdown

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)

  1. 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.

  2. No content cleaning: The leaked tokens were stored as-is in messages, appearing in the TUI and session history.

  3. Wrong finish_reason: Some local providers return finish_reason stop even when tool_calls are present in the response.

Fix (three-pronged)

  1. 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.

  2. session/processor.ts — Strip leaked EOS from stored messages so tokens do not appear in TUI or history.

  3. 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.

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent loop stops after the first tool call with local OpenAI-compatible models (Gemma via oMLX) — provider returns valid follow-up tool_calls

1 participant