fix: disable assistant prefill for Claude 4.6 models#14772
fix: disable assistant prefill for Claude 4.6 models#14772hsuanguo wants to merge 7 commits intoanomalyco:devfrom
Conversation
Claude Opus 4.6 and Sonnet 4.6 models don't support assistant message prefill across all providers (Anthropic, GitHub Copilot, OpenRouter, etc), causing errors when OpenCode reaches max steps. This fix detects Claude 4.6 models and uses a user message instead of assistant prefill for max steps handling, while preserving prefill for models that support it. Changes: - Add supportsAssistantPrefill() to detect Claude 4.6 models - Update max steps handling to conditionally use user/assistant role - Add comprehensive tests for various providers and model versions Fixes anomalyco#13768
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
…co#13768) The original fix only covered the isLastStep/MAX_STEPS prefill case. Logs revealed the error also occurs in normal conversation flow when any assistant message ends up last in the message array (e.g. from an aborted or interrupted turn). Broader fix: add stripTrailingAssistant() inside normalizeMessages() which is the single choke point for all message processing. This strips any trailing assistant messages for models that don't support prefill (Claude opus-4.6 and sonnet-4.6 across all providers), covering all code paths rather than just the max steps case. Also reverts the prompt.ts role switch since normalizeMessages handles it more correctly at the transport layer.
|
@hsuanguo Has this been fixed yet? This feature is very important to me. |
I haven't seen the error in the last couple of days so I assume it fixes it. |
|
I validated PR #14772 locally against issue #13768 by testing it side-by-side with other candidate fixes and replaying the real failing session exports shared in the issue discussion. To make sure the result was not specific to one path, I used separate worktrees for each approach: the PR branch itself ( For replay validation, I used the two exported sessions attached in issue comments by The alternatives were informative but incomplete: both the loop-guard variant and the Copilot-adapter-only variant had passing tests, but replay still showed a trailing Based on these comparisons, PR #14772 is the correct fix for #13768. It addresses the issue at the transport normalization layer ( |
|
nobody merge this? |
|
I tried this, it also fixed the issue for me ( both Claude Opus 4.6 on Bedrock and on Github Copilot ) |
|
Please prioritize merging this PR. This bug makes Claude 4.6 models (Opus and Sonnet) essentially unreliable on AWS Bedrock, which is the primary provider for enterprise users. Impact
Why this PR specifically
Our situationWe run OpenCode headless on a server with Bedrock Opus 4.6, accessed via web UI, Desktop app, and Slack. This bug forced us to build from this PR branch ourselves. We'd much rather track stable releases. |
…aude 4.x prefill error (GH#17790) Adds prefill-guard.mjs to opencode-aidevops plugin and composes it into the experimental.chat.messages.transform hook after the existing TTSR hook. The guard strips trailing assistant messages from the outgoing LLM payload when safe — preserving messages with finish=tool-calls or active tool parts so legitimate tool-call flows are untouched. Session DB is never modified. Fixes the mobile webui error 'This model does not support assistant message prefill' on Claude Opus/Sonnet 4.x. Mirrors upstream PRs anomalyco/opencode#14772, marcusquinn#16921, and #18091 which add the same logic in provider/transform.ts but are not yet merged in opencode v1.3.17 / v1.4.0. Fixes marcusquinn#17790
…aude 4.x prefill error (GH#17790) Adds prefill-guard.mjs to opencode-aidevops plugin and composes it into the experimental.chat.messages.transform hook after the existing TTSR hook. The guard strips trailing assistant messages from the outgoing LLM payload when safe — preserving messages with finish=tool-calls or active tool parts so legitimate tool-call flows are untouched. Session DB is never modified. Fixes the mobile webui error 'This model does not support assistant message prefill' on Claude Opus/Sonnet 4.x. Mirrors upstream PRs anomalyco/opencode#14772, marcusquinn#16921, and #18091 which add the same logic in provider/transform.ts but are not yet merged in opencode v1.3.17 / v1.4.0. Fixes marcusquinn#17790
Issue for this PR
Closes #13768
Type of change
What does this PR do?
Problem:
Claude Opus 4.6 and Sonnet 4.6 models reject requests where the last message in the conversation is an assistant message, returning: "This model does not support assistant message prefill. The conversation must end with a user message."
This happens in at least two scenarios:
The issue affects these models across all providers — native Anthropic API, GitHub Copilot, OpenRouter, etc.
Fix:
Added
stripTrailingAssistant()insidenormalizeMessages()inprovider/transform.ts. SincenormalizeMessagesis called from the singleProviderTransform.message()choke point used for all outgoing requests, this covers every code path rather than patching individual call sites.For models that don't support prefill (detected via
supportsAssistantPrefill()), any trailing assistant messages are stripped before the request is sent. For all other models, nothing changes.How did you verify your code works?
Confirmed the root cause using logs from a live session that reproduced the error:
providerID=anthropic,modelID=claude-opus-4-6, URLhttps://api.anthropic.com/v1/messagesrole: assistantfrom a prior interrupted turn — not a max steps prefillAdded integration tests in
ProviderTransform.message - strip trailing assistant for Claude 4.6that verify:Screenshots / recordings
Not applicable - backend logic fix
Checklist