close
Skip to content

bug(copilot): tool results and attachments incorrectly billed as user-initiated (x-initiator header) #15824

Description

@Juhwa-Lee1023

Summary

When opencode makes requests to the GitHub Copilot API, it sets the x-initiator header based on the role of the last message. However, tool results, image attachments, and other agent-produced content travel as messages with role: "user" in all three API flavours (Completions, Responses, Anthropic Messages).

This causes every agentic turn containing tool results or attachments to be mis-classified as user-initiated, burning the user's Copilot premium quota instead of the agent budget.

Affected code

packages/opencode/src/plugin/copilot.tsisAgent calculation inside the custom fetch wrapper:

// Completions API — bare role check, misses synthetic messages
isAgent: last?.role !== "user",

// Responses API — same problem
isAgent: last?.role !== "user",

// Messages API — partial fix only, covers tool_result but not attachments/images
isAgent: !(last?.role === "user" && hasNonToolCalls),

Expected behaviour

Messages whose content consists entirely of agent-produced parts (tool results, attachments, images) should set x-initiator: agent.

Related

Originally reported upstream at #8030.

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions