fix(copilot): treat synthetic user messages as agent for x-initiator billing#15821
fix(copilot): treat synthetic user messages as agent for x-initiator billing#15821Juhwa-Lee1023 wants to merge 1 commit intoanomalyco:devfrom
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
|
The following comment was made by an LLM, it may be inaccurate: I found a potentially related PR: Related PR:
This PR (15821) may be a more comprehensive fix or refactor of the issue originally addressed in #8721. You should verify whether #8721 was closed in favor of this new PR or if they address different aspects of the same problem. |
|
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. |
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
|
Closing in favour of #8721 which provides a more complete fix using history-based inference (matching VSCode Copilot's |
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