fix(opencode): ignore tool calls emitted inside reasoning blocks#30277
Open
danmaxis wants to merge 1 commit into
Open
fix(opencode): ignore tool calls emitted inside reasoning blocks#30277danmaxis wants to merge 1 commit into
danmaxis wants to merge 1 commit into
Conversation
Some reasoning models (Qwen, Kimi K2, GLM, ...) occasionally emit tool-call markup inside their <think> reasoning block while still thinking. The inference server promotes that to a structured tool call, which streamText then executes prematurely — running a side effect and ending the turn before the model produces its real answer. Add a language-model middleware that runs over the provider stream before tool calls are interpreted. It drops any tool call that begins while a reasoning block is still open (and that call's input/result parts) and downgrades a resulting tool-calls finish reason to stop. Tool calls emitted after reasoning-end pass through untouched, and the transform is a no-op for streams that never emit reasoning parts. Opt out per model with options.suppressToolCallsInReasoning: false. Refs: anomalyco#8851, anomalyco#6708, anomalyco#10996
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
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 #6708
(Same underlying behavior was also reported in #8851 and #10996, now closed.)
Type of change
What does this PR do?
Reasoning models (Qwen, Kimi K2, GLM, ...) sometimes emit tool-call markup inside their
<think>reasoning while still thinking. The inference server promotes that to a structured tool call, and since opencode's tools carry anexecute,streamTextruns it — firing a side effect and ending the turn before the model produces its real answer.By the time the assistant message exists, the tool call is already a structured
tool_callsentry with no trace of where it came from. The ordering is still visible at the provider stream level, so the fix is a smallLanguageModelV3wrapStreammiddleware (reasoning-tool-guard.ts) added next to the existingtransformParamsmiddleware insession/llm.ts. It runs over the stream beforestreamTextinterprets tool calls and:tool-input-*/tool-resultparts), andtool-callsfinish reason tostop, so the session loop doesn't wait on a tool that never runs.It only touches calls that begin before
reasoning-end. Calls emitted after reasoning closes pass through, a mixed turn keeps itstool-callsfinish reason, and it's a no-op for any stream without reasoning parts. Opt out per model withoptions.suppressToolCallsInReasoning: false.How did you verify your code works?
Added
test/session/reasoning-tool-guard.test.ts(5 cases: in-reasoning call suppressed + finish downgraded; post-reasoning call preserved; mixed turn keepstool-calls; plain text passes through; suppressed even when the model stops mid-<think>).bun test,oxlint,prettier --check, andtsgo --noEmitpass; the existingllm.test.tsis unchanged.Screenshots / recordings
N/A — not a UI change.
Checklist
Thanks for taking a look — happy to adjust the gating, naming, or placement if you'd prefer it done differently.