fix(aibridge/intercept/messages): record user prompt before trailing system message#26195
Draft
dannykopping wants to merge 1 commit into
Draft
fix(aibridge/intercept/messages): record user prompt before trailing system message#26195dannykopping wants to merge 1 commit into
dannykopping wants to merge 1 commit into
Conversation
…system message Clients using the mid-conversation system beta (e.g. Claude Code with anthropic-beta: mid-conversation-system-*) append a trailing role=system message after the user's prompt, such as an injected skills list. The last message was then role=system rather than role=user, so lastUserPrompt returned nothing and the conversation's prompt was never recorded. Step back exactly one message when the last message is role=system so the user's prompt is recorded. Stepping back only past a single trailing system message avoids re-recording a stale prompt from an earlier turn that contained no new user input.
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.
Problem
For a single user turn, Claude Code can issue two API calls. One is a side call to generate a session title; the other is the real conversation. We observed that the real-conversation thread had no recorded prompt.
The cause is the
mid-conversation-system-2026-04-07beta. When enabled, the client appends a trailingrole: "system"message after the user's text (e.g. an injected skills list):lastUserPromptonly inspected the last element ofmessagesand requiredrole == user. Since the last message is nowsystem, it returned(nil, false)and the prompt was never recorded viaRecordPromptUsage.Fix
When the last message is
role: system, step back exactly one message to find the user's prompt. Stepping back only past a single trailing system message means requests that contain no new user prompt (two trailing system messages, or a system message after an assistant turn) still record nothing, avoiding re-recording a stale prompt from an earlier turn.Tests
Added
lastUserPromptcases covering:All
TestRequestPayloadLastUserPromptsubtests pass under-race.