fix(provider): consolidate leading system messages to prevent Jinja crash on compaction - #12
Open
adi-IL wants to merge 1 commit into
Open
fix(provider): consolidate leading system messages to prevent Jinja crash on compaction#12adi-IL wants to merge 1 commit into
adi-IL wants to merge 1 commit into
Conversation
…rash on compaction
Consolidate multiple contiguous leading system messages into a single system
message for OpenAI-compatible, Ollama, and local model providers, and lower
non-leading system messages in conversation history into user-compatible
<system-update> blocks.
Prevents HTTP 500 Jinja template errors ("System message must be at the
beginning.") when auto-compaction and per-turn engagement state are lowered
across OpenAI-compatible chat completion endpoints (such as Qwen, Ollama,
vLLM, and M.O.G.-SEC models), while preserving distinct leading system
blocks on Anthropic for prompt caching.
Fixes s0ld13rr#11
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.
Fixes #11
What was wrong
When running with OpenAI-compatible endpoints or local models (Ollama, vLLM, Qwen, M.O.G.-SEC, Mistral) and reaching auto-compaction or turns with volatile engagement state, the session failed permanently with an HTTP 500 Jinja template error:
Root Cause
packages/opencode/src/session/llm/request.ts,prepareappends volatile engagement context as a secondary system entry (system.push(input.volatileSystem)). Whenmessagesis constructed via...system.map((x) => ({ role: "system", content: x })), multiple discrete{ role: "system" }messages are emitted at the head of the message array.messagesand enforce thatrole == 'system'is only valid onloop.first(messages[0]). When the loop evaluatesmessages[1](role == 'system'),not loop.firstevaluates to true, raisingSystem message must be at the beginning.What changed
System Message Normalization (
packages/opencode/src/provider/transform.ts):normalizeMessages, all contiguous leadingrole: "system"messages are consolidated into a single leadingrole: "system"message (joining contents with\n\n) for OpenAI-compatible, Ollama, and non-Anthropic providers.@ai-sdk/anthropic,@ai-sdk/google-vertex/anthropic), distinct leading system blocks are preserved so prompt caching continues to hit.role: "system"messages appearing after user/assistant turns in history are lowered into user-compatible<system-update>\n...\n</system-update>blocks, preserving chronological context without violating chat template role ordering.Middleware Transform Support (
packages/opencode/src/session/llm.ts):transformParamsinwrapLanguageModelto runProviderTransform.messagefor bothstreamandgeneraterequest types.Unit Tests (
packages/opencode/test/provider/transform.test.ts):<system-update>blocks.Verification
bun test test/provider/transform.test.ts: 295 passed (all new tests passing).bun test test/session/compaction.test.ts: 52 passed.bun turbo typecheck: 19/19 packages passed cleanly with zero type errors.