fix: sanitize pending/running tool parts to prevent orphaned tool_use#54
Merged
fix: sanitize pending/running tool parts to prevent orphaned tool_use#54
Conversation
When a session errors mid-tool-execution (e.g. context overflow), the tool part stays in pending/running state. On the next transform, the SDK generates a tool_use block for this part but has no output to generate a matching tool_result, causing Anthropic to reject with: tool_use ids were found without tool_result blocks immediately after Add sanitizeToolParts() in gradient.ts that converts pending/running tool parts to error state after transformInner(). Error state generates both tool_use + tool_result(is_error=true), eliminating the orphan while preserving conversation history. The function is a no-op (returns same array reference) when all tools are already in terminal state.
62a44f9 to
b3e149a
Compare
2 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.
Problem
When a session errors mid-tool-execution (e.g. context overflow), the tool part stays in
pending/runningstate. On the nexttransform(), the OpenCode SDK generates atool_useblock for this part but has no output to generate a matchingtool_result, causing Anthropic to reject with:Root Cause
In the SDK's
ToolStatediscriminated union:ToolStatePending/ToolStateRunning→ only generatestool_use(no output → notool_result)ToolStateCompleted/ToolStateError→ generates bothtool_use+tool_resultLore's gradient transform had no sanitization step for incomplete tool parts. The trailing drop loop preserved messages with ANY tool part regardless of status, which was correct for preventing the prefill error but didn't address the orphaned
tool_useissue.Fix
Add
sanitizeToolParts()ingradient.tsthat convertspending/runningtool parts toerrorstate aftertransformInner(). This:tool_use+tool_result(is_error=true), eliminating the orphanChanges
src/gradient.ts: AddsanitizeToolParts()function and integrate intotransform()src/index.ts: Update trailing drop loop comment to note sanitization happens upstreamtest/gradient.test.ts: 6 new test cases covering pending/running/mixed/no-op scenarios