fix(domain): collapse system messages into a single one (#2894)#3436
Open
ImBIOS wants to merge 3 commits into
Open
fix(domain): collapse system messages into a single one (#2894)#3436ImBIOS wants to merge 3 commits into
ImBIOS wants to merge 3 commits into
Conversation
) `Context::set_system_messages` previously inserted each entry in `content` as a separate `ContextMessage::system(...)`, producing N system messages in the resulting chat. Chat templates such as Qwen3.5/3.6 (used by llama.cpp and vLLM) only allow a single system message at `messages[0]` and raise `raise_exception('System message must be at the beginning.')` for any additional one, breaking those providers entirely. Join all entries into a single system message (separated by `\n\n`) and insert it once at position 0. Empty payloads are filtered out and an all-empty `content` vector leaves the context untouched. Fixes tailcallhq#2894, also resolves the duplicate tailcallhq#2586 (qwen3.5-27b llama.cpp). Co-Authored-By: ForgeCode <noreply@forgecode.dev>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates Context::set_system_messages to ensure compatibility with chat templates that require exactly one system message at messages[0].
Changes:
- Replace any existing system messages and collapse all provided entries into a single system message block.
- Filter out empty system-message entries and avoid inserting a system message when the combined payload is empty.
- Add regression tests covering multi-entry collapse and replacement of pre-existing system messages.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Applied suggestions from the Copilot review on PR tailcallhq#3436: 1. Doc comments no longer wrap the Jinja exception `raise_exception('System message must be at the beginning.')` across a line break inside a single inline code span — both occurrences now use a fenced `text` code block so rustdoc/CommonMark render as intended. 2. The semantic of an empty `content` payload is now explicitly part of the public contract of `set_system_messages`: it clears every existing system message and inserts no replacement. This matches the pre-tailcallhq#2894 behavior on the non-empty branch, but it was previously undocumented. Added two regression tests (`..._with_empty_payload_clears_existing` and `..._with_only_empty_strings_clears_existing`) to lock it in. 3. The combined system message is now built in a single `String` allocation (push_str + manual separator insertion) instead of the previous `collect::<Vec<_>>().join(...)` approach, which materialised an intermediate `Vec<String>` for no benefit. Co-Authored-By: ForgeCode <noreply@forgecode.dev>
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.
Summary
Context::set_system_messagespreviously inserted each entry incontentas a separate
ContextMessage::system(...), producing N system messagesin the resulting chat. Chat templates such as Qwen3.5/3.6 (used by
llama.cpp and vLLM) only allow a single system message at
messages[0]and raise:
for any additional system message, breaking those providers entirely with
a 400/500 error from the upstream inference server.
Fix: join all entries into a single system message (separated by
\n\n)and insert it once at position 0. Empty payloads are filtered out, and an
all-empty
contentvector leaves the context untouched.Changes
crates/forge_domain/src/context.rsContext::set_system_messagesto:\n\n-joined string (filteringout empty entries).
ContextMessage::system(combined)at position 0.motivates the change.
test_set_system_messages_collapses_into_single_message— asserts thecontext contains exactly one system message and that its content is the
\n\n-joined payload.test_set_system_messages_replaces_existing_single_system_message—asserts a pre-existing system message is replaced, not retained or
duplicated.
Why this is safe
Context::set_system_messagesis unchanged.pass a
Vecof length 1 (still produces a single system message atposition 0).
forge_app/src/system_prompt.rs:136alreadypasses exactly two entries (
static_block,non_static_block), so thisis the path actually exercised in production.
Verification
cargo check -p forge_domain --tests: cleancargo check -p forge_app --tests: cleancargo clippy -p forge_domain --tests --no-deps: cleancargo test -p forge_domain --lib: 596 passed, 0 failed (incl. 2 newregression tests)
cargo test -p forge_app --lib system_prompt: 7 passed, 0 failedRelated issues
System message must be at the beginning.Jinja error on Qwen3.5-27B / llama.cpp) - it's a duplicate issueHow to test locally
cargo build -p forge_domain(or run the two new tests directly).or vLLM should no longer reject requests with the
System message must be at the beginning.Jinja exception.