Skip to content

fix: resolve Claude 4.6 assistant prefill compatibility issues#16883

Closed
hsuanguo wants to merge 1 commit intoanomalyco:devfrom
hsuanguo:fix/claude-4.6-prefill-proper-fixes
Closed

fix: resolve Claude 4.6 assistant prefill compatibility issues#16883
hsuanguo wants to merge 1 commit intoanomalyco:devfrom
hsuanguo:fix/claude-4.6-prefill-proper-fixes

Conversation

@hsuanguo
Copy link
Copy Markdown

Summary

Fixes #13768 - Resolves the "This model does not support assistant message prefill" error that occurs with Claude Opus 4.6 and Sonnet 4.6 models across all providers (Anthropic, GitHub Copilot, OpenRouter, etc.).

This PR addresses two distinct root causes with targeted, surgical fixes:

1. Max Steps Message Role (prompt.ts:670)

Problem: When the maximum step limit is reached, opencode appends an assistant message containing MAX_STEPS instructions. Claude 4.6 rejects any conversation ending with an assistant message.

Fix: Changed the role from assistant to user for the MAX_STEPS instruction.

  • ✅ Maintains instruction delivery to the model
  • ✅ Compatible with Claude 4.6's requirements
  • ✅ No behavioral changes to the agent

2. Agentic Loop Exit Condition (prompt.ts:322)

Problem: The loop exit condition used timestamp-based ID comparison (lastUser.id < lastAssistant.id) to determine if the assistant had responded to the user. When client clocks run ahead of server clocks, this comparison fails, causing the loop to create an extra assistant message that becomes the final message in the array.

Fix: Replaced ID comparison with explicit parent-child relationship check (lastAssistant.parentID === lastUser.id).

  • ✅ Clock-skew resistant
  • ✅ Uses existing parentID field that tracks which user message an assistant is responding to
  • ✅ More semantically correct

Changes

  • packages/opencode/src/session/prompt.ts (2 lines changed)

Testing

  • ✅ All existing session tests pass (110 pass, 4 skip, 0 fail)
  • ✅ Prompt-specific tests verified (4 pass)
  • ✅ No new test coverage needed as these are targeted fixes to existing logic

Context

Previous PR #14772 attempted to fix this with a safety net approach (stripTrailingAssistant()) but was correctly identified as hiding the real bugs rather than fixing them. This PR implements proper root-cause fixes instead.

Credit to @nguquen for identifying the clock-skew issue in this comment.

This commit addresses two root causes of the 'This model does not support assistant message prefill' error that occurs with Claude Opus 4.6 and Sonnet 4.6 models:

1. Max steps message role (prompt.ts:670)
   - Changed from assistant to user role for MAX_STEPS instruction
   - Claude 4.6 rejects conversations ending with assistant messages
   - User role maintains instruction delivery while ensuring compatibility

2. Agentic loop exit condition (prompt.ts:322)
   - Replaced timestamp-based ID comparison with explicit parentID check
   - Previous: lastUser.id < lastAssistant.id (vulnerable to clock skew)
   - Fixed: lastAssistant.parentID === lastUser.id (clock-skew resistant)
   - Prevents extra assistant messages when client/server clocks differ

Fixes anomalyco#13768
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 10, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

I found related PRs that are relevant to the current PR:

Related PRs:

  1. fix: disable assistant prefill for Claude 4.6 models #14772 - "fix: disable assistant prefill for Claude 4.6 models"

  2. fix: use parentID matching instead of ID ordering for prompt loop exit and message rendering #14307 - "fix: use parentID matching instead of ID ordering for prompt loop exit and message rendering"

    • Related because this directly addresses one of the root causes fixed in the current PR - it implements the parentID matching logic for the agentic loop exit condition instead of relying on ID comparison.
  3. fix: server-generated message IDs to prevent client/server clock skew #11869 - "fix: server-generated message IDs to prevent client/server clock skew"

    • Related because this addresses the underlying clock skew issue that motivated the need for the parentID fix in the current PR.

@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 10, 2026
@github-actions github-actions bot closed this Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

This model does not support assistant message prefill / Github Copilot with Opus 4.6

1 participant