{{ message }}
fix(copilot): treat synthetic user messages as agent for x-initiator billing#15821
Closed
Juhwa-Lee1023 wants to merge 1 commit into
Closed
fix(copilot): treat synthetic user messages as agent for x-initiator billing#15821Juhwa-Lee1023 wants to merge 1 commit into
Juhwa-Lee1023 wants to merge 1 commit into
Conversation
…billing Tool results, image attachments, and other agent-produced content arrive as messages with role 'user'. Previously only the Anthropic Messages API path checked for this; the Completions and Responses API paths performed a bare role check and mis-classified these messages as user-initiated, causing premium Copilot requests to be billed against the user quota. Introduce a shared isSyntheticUserMessage() helper that returns true when every content part of a 'user' message is an agent-produced type (tool_result, attachment, input_image, image_url, image). Apply it consistently across all three API paths. The Messages API path previously used a different (inverted) formulation; it is now aligned with the other two for consistency. Fixes: anomalyco#8030
Contributor
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Author
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.

Issue for this PR
Closes #15824
Type of change
What does this PR do?
When GitHub Copilot receives a request, it uses the
x-initiatorheader to decide whether to bill the request against the user's premium quota (user) or the agent budget (agent). opencode sets this header based on theroleof the last message in the request body.The problem: tool results, image attachments, and other agent-produced content travel as messages with
role: "user"in all three API flavours (Completions, Responses, Anthropic Messages). The Completions and Responses API paths only checkedlast?.role !== "user", so every agentic turn containing tool results was mis-classified as user-initiated and incorrectly billed against the user's Copilot premium quota.The Anthropic Messages API path had a partial fix (
hasNonToolCalls), but it was inconsistent with the other two paths and only coveredtool_resultparts.Fix: introduce a shared
isSyntheticUserMessage()helper that returnstruewhen every content part of arole: "user"message is an agent-produced type (tool_result,attachment,input_image,image_url,image). Apply it consistently across all three API paths.How did you verify your code works?
Added 13 unit tests in
packages/opencode/test/provider/copilot/x-initiator.test.tscovering genuine human messages (→false) and all synthetic agent-produced message shapes (→true).Screenshots / recordings
N/A — logic-only change, no UI.
Checklist