Repository navigation
fix(ai): place Anthropic system updates before the next assistant turn - #52568
Merged
Merged
Conversation
# Conflicts: # packages/ai/src/protocols/anthropic-messages.ts
rekram1-node
enabled auto-merge (squash)
October 5, 2026 04:17
rekram1-node
disabled auto-merge
October 5, 2026 04:17
Ichinose-Kazuki
pushed a commit
to Ichinose-Kazuki/opencode
that referenced
this pull request
Oct 7, 2026
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.
Issue for this PR
No linked issue.
Type of change
What does this PR do?
Anthropic accepts a mid-conversation
systemmessage only right after a user turn (tool results count) and right before an assistant turn or at the end of the request. Consecutive system messages are accepted and judged together. The converter checked each update against its immediate neighbors, which lost real system messages in two common cases:assistant → update → user). An update after an assistant turn can't be native, so it was sent as wrapped<system-update>user text.The converter now holds text updates and releases them together just before the next assistant turn, or at the end of the request. If the turn before that spot is a user turn, tool results, or a paused server-tool turn, they go out as native system messages; otherwise they are wrapped as before.
Unchanged:
The order depends only on the stored history, so later requests keep the same prefix and prompt caching is unaffected. A session that already has these updates in history changes order once.
This also removes the Vertex-only rule from #43498 that wrapped any update directly after local tool results. Vertex returned 404 for that shape on Claude Opus 4.8 in August; it no longer does. On
opencode-production(usandglobal,rawPredictandstreamRawPredict), a native system message after tool results, at the end or followed by more turns, returned 200 with a complete stream and was followed on claude-opus-4-8, claude-opus-4-8@default, claude-opus-5-5, and claude-sonnet-5. Vertex now lowers updates exactly like the direct API.How did you verify your code works?
bun testandbun typecheckinpackages/ai,test/session-runner.test.tsinpackages/core, rootbun run check.usandglobal: same six shapes plus an update after tool results. All 200.Screenshots / recordings
Not a UI change.
Checklist