Skip to content

fix(aibridge/intercept/messages): record user prompt before trailing system message#26195

Draft
dannykopping wants to merge 1 commit into
mainfrom
fix/aibridge-mid-conversation-system-prompt
Draft

fix(aibridge/intercept/messages): record user prompt before trailing system message#26195
dannykopping wants to merge 1 commit into
mainfrom
fix/aibridge-mid-conversation-system-prompt

Conversation

@dannykopping

Copy link
Copy Markdown
Contributor

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-07 beta. When enabled, the client appends a trailing role: "system" message after the user's text (e.g. an injected skills list):

"messages": [
  { "role": "user",   "content": [ "...system-reminder...", "hey how are ya" ] },
  { "role": "system", "content": "The following skills are available..." }
]

lastUserPrompt only inspected the last element of messages and required role == user. Since the last message is now system, it returned (nil, false) and the prompt was never recorded via RecordPromptUsage.

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 lastUserPrompt cases covering:

  • trailing system message stepping back to string and typed user content
  • trailing system message after a non-user message (records nothing)
  • a lone system message (no out-of-bounds)
  • two trailing system messages (only steps back once)

All TestRequestPayloadLastUserPrompt subtests pass under -race.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant