fix(opencode): strip trailing assistant messages to prevent prefill 400#51
Merged
Merged
Conversation
rewriteRequestBody() never removed trailing assistant-role messages, so a resumed or compacted session that ends on an assistant turn (e.g. after a failed tool round) reached Anthropic with assistant-message prefill. Claude Code OAuth models reject this with "This model does not support assistant message prefill. The conversation must end with a user message." (HTTP 400). Add stripTrailingAssistantMessages() and call it right after JSON.parse so trailing assistant messages are popped before billing/signing. Mirrors the behavior of the built-in @ai-sdk/anthropic provider and packages/pi. Adds 5 tests; updates existing fixtures that ended on assistant turns.
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
rewriteRequestBody()never removed trailing assistant-role messages. A resumed or compacted session that ends on an assistant turn (e.g. after a failed tool round, or a session restored mid-response) reached Anthropic with assistant-message prefill. Claude Code OAuth models reject this:This recurs in practice on session resume/compaction and forces the user to manually send another message to recover.
Fix
Add
stripTrailingAssistantMessages()and call it right afterJSON.parse(body), so trailing assistant messages are popped before billing-header computation and signing. This mirrors the behavior of the built-in@ai-sdk/anthropicprovider and thepackages/piconversion path, which already strip trailing assistant messages.Only messages after the last user message are removed — the active user turn and any assistant message followed by a user message are always preserved. The alternative (no strip) is a hard 400 that returns nothing.
Tests
Adds 5 tests:
tool_usefollowed by usertool_resulttool_result+ assistantUpdates existing fixtures that previously ended on assistant turns to end on user messages. All opencode tests pass (264), typecheck clean, biome clean.
Need help on this PR? Tag
@codesmithwith what you need. Autofix is disabled.Summary by cubic
Strips trailing assistant messages during request rewrite so conversations end with a user message, preventing Anthropic Claude Code OAuth 400 “assistant message prefill” errors. Aligns behavior with
@ai-sdk/anthropicandpackages/pi.stripTrailingAssistantMessages()and called it inrewriteRequestBody()right afterJSON.parse.tool_use→tool_resultflows.Written for commit ca249f9. Summary will update on new commits.