fix: don't insert relocated system text before tool_result blocks - #257
Draft
darknessest wants to merge 1 commit into
Draft
fix: don't insert relocated system text before tool_result blocks#257darknessest wants to merge 1 commit into
darknessest wants to merge 1 commit into
Conversation
Anthropic requires tool_result blocks to be the leading content of the message that follows a tool_use. Unshifting the relocated system prompt text in front of them makes the API reject the request with "tool_use ids were found without tool_result blocks immediately after". Compaction requests are the common victim: their first user message is the tool_result carrier for a leading assistant tool_use turn.
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.
While chasing recurring 400s from Anthropic during opencode's session compaction, I traced it back to the system-prompt relocation in
transformBody.What happens: when the request's first user message carries
tool_resultblocks (which is the norm for compaction requests — their history often starts with an assistanttool_useturn), the relocated system text getsunshifted in front of the results. Anthropic wants tool_results to be the leading blocks of the message that follows atool_use, so it rejects the whole request:Regular chat requests never hit this (their first user message is plain text), which makes it look like a mysterious compaction-only failure. It also keeps re-failing on every auto-compaction retry until the tail boundary happens to shift, so sessions get stuck in an error loop near the context limit.
Repro/verification: captured a failing compaction request from a live session, replayed it against the API — 400 with the exact error above. Moved the injected text block after the tool_results, replayed again — 200. That one-block reorder is this patch.
Fix: insert the relocated text after any leading
tool_resultblocks instead of always at index 0. Added a regression test.Marking as draft since I only exercised the compaction path against the real API — happy to adjust if you'd rather relocate into a different message entirely.