fix(opencode): recover from truncated tool calls instead of failing silently#21688
Open
nickveenhof wants to merge 2 commits intoanomalyco:devfrom
Open
fix(opencode): recover from truncated tool calls instead of failing silently#21688nickveenhof wants to merge 2 commits intoanomalyco:devfrom
nickveenhof wants to merge 2 commits intoanomalyco:devfrom
Conversation
…ilently When a model's output hits the token limit mid-tool-call, the JSON arguments are truncated and tool parsing fails with 'expected string, received undefined'. Two fixes: 1. experimental_repairToolCall now detects truncation: when the tool name is a valid registered tool but args failed to parse, return an actionable error telling the model to split its operation into smaller pieces. Previously all parse failures were routed to a generic 'invalid tool' handler with no recovery guidance. 2. The session loop now handles finishReason 'length': instead of silently exiting when the model is cut off by the token limit, inject a synthetic continuation message so the model can resume where it left off. Fixes anomalyco#18108 Fixes anomalyco#17471 Refs anomalyco#14519, anomalyco#13102
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
6 tasks
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 #18108
Closes #17471
Refs #14519, #13102, #18151, #18131
Type of change
What does this PR do?
When a model's output hits
max_tokensmid-tool-call, the streamed JSON arguments get truncated. The tool parser receivesundefinedfor required fields, producing:Two fixes:
1. Truncation detection in
experimental_repairToolCall(llm.ts)repairToolCallcurrently only handles tool name case-sensitivity. All other parse failures route to the genericinvalidtool. But whentoolNameis a valid registered tool and the args failed to parse, the cause is truncation, not an invalid tool. This check distinguishes the two cases and returns an actionable error telling the model to split its operation into smaller pieces.2. Auto-continue on
finishReason: "length"(prompt.ts)The session loop exits when
finishReasonis anything other than"tool-calls". This means"length"(token limit hit) causes the session to stop silently. Now the loop detects"length", injects a synthetic continuation message, and keeps going so the model can resume.How did you verify your code works?
bun run --cwd packages/opencode tsc --noEmitpasses with no new type errorsOUTPUT_TOKEN_MAXand verifying the model receives the truncation error message and retries with smaller operationsScreenshots / recordings
N/A - no UI changes
Checklist