fix(core): ensure single leading system message in CLI requests (fixes #12963)#12988
Open
not-knope wants to merge 2 commits into
Open
fix(core): ensure single leading system message in CLI requests (fixes #12963)#12988not-knope wants to merge 2 commits into
not-knope wants to merge 2 commits into
Conversation
When converting unified chat history into OpenAI-compatible messages, `convertFromUnifiedHistoryWithSystemMessage` injected a system message at the start but also emitted any system messages that already existed inside the history (e.g. notifications added via `addSystemMessage`, compaction notices, or a system message stored at `history[0]` in loaded/remote sessions). This produced a system message at a non-zero index, which strict providers reject with a 400 "System message must be at the beginning" error. This was commonly triggered by the follow-up request sent after cancelling a tool call. Merge all system content into a single system message positioned at index 0 so the request is always valid, and add tests covering the tool-cancellation follow-up scenario. Closes continuedev#12963 Co-authored-by: Cursor <cursor@cursor.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Description
Fixes #12963.
When cancelling/denying a tool call, the follow-up request to the model could fail with:
Root cause:
convertFromUnifiedHistoryWithSystemMessage(incore/util/messageConversion.ts, used by the CLI streaming path inextensions/cli/src/stream/streamChatResponse.ts) injected the system message at index 0, butconvertFromUnifiedHistoryalso emitted any system-role messages that already existed inside the history. Those messages can come from:ChatHistoryService.addSystemMessage(diffs, "No changes to display", remote/serve helpers, etc.),history[0]in loaded/remote sessions.The result was a request with a
systemmessage at a non-zero index, which strict OpenAI-compatible providers (e.g. the reporter's aqueduct proxy) reject with a400 "System message must be at the beginning". It surfaced most often on the follow-up request that is sent right after a tool call is cancelled.The fix merges all system content (the injected system message plus any system messages found in the history) into a single
systemmessage positioned at index 0, so the outgoing request is always valid. Non-system messages keep their original order, so tool-call/tool-result sequences are untouched.Checklist
Tests
Added
describe("convertFromUnifiedHistoryWithSystemMessage", ...)cases inextensions/cli/src/messageConversion.test.tscovering:systemmessage is at index 0 and the tool result is preserved,Note
This PR was developed with AI assistance (Cursor), as reflected in the commit's
Co-authored-bytrailer. All code was reviewed and verified locally before submission.