fix(#62): consolidate mid-conversation system messages for strict chat templates#65
Merged
Merged
Conversation
…plates Qwen3/Qwen3.5 chat templates under llama.cpp --jinja raise "System message must be at the beginning." and llama.cpp 400s when a system-role message appears at any index but 0 — but only when tools are present (that's when it compiles the template to build a tool-call grammar). SmallCode injects system content mid-conversation (clarifier, plan request, planner injection, path-validation warnings, skill activation, compaction), so the messages array routinely had system entries past index 0. New src/session/message_normalizer.js#consolidateSystemMessages() collapses all system-role messages into a single leading one (order preserved, identical blocks de-duplicated) and keeps only non-system turns after it. Applied in both request builders (bin/smallcode.js and bin/model_client.js chatCompletion) right before the body is sent. Verified E2E against a Qwen3 model: every tool-bearing request now carries exactly one system message at index 0. +9 tests; full suite 157 passing.
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 #62.
Qwen3 / Qwen3.5 chat templates under llama.cpp
--jinjaraiseSystem message must be at the beginning.and llama.cpp returns HTTP 400 — but only whentoolsare present, since that's when it compiles the template into a tool-call grammar.SmallCode injects
role: systemcontent mid-conversation in several places (clarifier, plan request, planner injection, path-validation warnings, skill activation, compaction summaries), so the outgoingmessagesarray routinely hadsystementries at positions other than 0.Fix
src/session/message_normalizer.js#consolidateSystemMessages()collapses every system-role message into a single leading system message (order preserved, identical blocks de-duplicated) and keeps only non-system turns after it.bin/smallcode.jsandbin/model_client.jschatCompletion) immediately before the request body is built, so it catches stray system messages regardless of which path injected them.Verification
test/message_normalizer.test.js(merge, ordering, dedup, multimodal passthrough, idempotency, edge cases).npm run buildpasses.