fix(session): recover when models emit tool calls as plain text#30633
Open
pebeto wants to merge 1 commit into
Open
fix(session): recover when models emit tool calls as plain text#30633pebeto wants to merge 1 commit into
pebeto wants to merge 1 commit into
Conversation
6 tasks
6d83e04 to
7678d43
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 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.
Issue for this PR
Closes #24316
Type of change
What does this PR do?
Models served through vLLM/llama.cpp sometimes write their tool call as plain text instead of a structured call. I captured this live with Qwen3.6 27B: about 50K tokens into a session the model emitted
<function_bash>instead of<function=bash>, the server-side parser couldn't match it, the whole call leaked into the message text, and the turn finishedstopwith zero tool calls. The session loop saw a "finished" turn and halted mid-task.I first tested the tag-stripping approach from #27984, but it didn't hold up. It patches
packages/llm, which the CLI doesn't import (OpenAI-compatible providers go through the AI SDK path), and stripping text can't un-halt the session anyway since there's still no tool call to execute.This PR adds a small detector (
tool-call-leak.ts) that recognizes a leaked tool-call block at the end of the assistant text (Qwen XML, drifted variants, hermes JSON). When the session loop is about to exit on such a turn, it instead injects a hidden corrective message and lets the model retry, capped at 2 attempts per user prompt. Past the cap it stops with a visible error instead of a silent halt. Nothing is ever parsed or executed from the leaked text; the retry goes through the normal tool-calling path, so permissions and tool resolution are untouched. This works because the leak is intermittent format drift: given a correction, the model re-issues the call properly on the next try.How did you verify your code works?
bun testinpackages/opencode: 3000 pass, 0 fail;bun run typecheckcleanChecklist