From 245bb21da5dfec3c5496cc8eb9f0f6ad11eceba5 Mon Sep 17 00:00:00 2001 From: "exe.dev user" Date: Thu, 5 Nov 2026 18:04:18 +0000 Subject: [PATCH] fix(core): mark Copilot steps with tool images as agent-initiated --- .../src/plugin/provider/github-copilot.ts | 4 ++- .../plugin/provider-github-copilot.test.ts | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/core/src/plugin/provider/github-copilot.ts b/packages/core/src/plugin/provider/github-copilot.ts index 1eb7ad17321..7e9cda9ef5d 100644 --- a/packages/core/src/plugin/provider/github-copilot.ts +++ b/packages/core/src/plugin/provider/github-copilot.ts @@ -478,8 +478,10 @@ function requestMetadata(url: string, body: unknown) { if (!Array.isArray(body.messages)) return { agent: false, vision: false } const last = body.messages.at(-1) if (url.includes("completions")) { + const previous = body.messages.at(-2) return { - agent: !isRecord(last) || last.role !== "user", + // Chat Completions moves tool-result images into a user message after the tool messages. + agent: !isRecord(last) || last.role !== "user" || (isRecord(previous) && previous.role === "tool"), vision: body.messages.some( (message) => isRecord(message) && diff --git a/packages/core/test/plugin/provider-github-copilot.test.ts b/packages/core/test/plugin/provider-github-copilot.test.ts index 0f3fc1fb556..1dea21abc2b 100644 --- a/packages/core/test/plugin/provider-github-copilot.test.ts +++ b/packages/core/test/plugin/provider-github-copilot.test.ts @@ -462,6 +462,32 @@ describe("GithubCopilotPlugin", () => { }), ) + it.effect("treats tool images moved into a Chat Completions user message as agent-initiated", () => + Effect.gen(function* () { + yield* addPlugin() + const event = yield* httpRequest( + new Request("https://api.githubcopilot.com/chat/completions", { + method: "POST", + headers: { authorization: "Bearer token" }, + body: JSON.stringify({ + messages: [ + { role: "user", content: "Take a screenshot." }, + { + role: "assistant", + content: null, + tool_calls: [{ id: "call_1", type: "function", function: { name: "screenshot", arguments: "{}" } }], + }, + { role: "tool", tool_call_id: "call_1", content: "" }, + { role: "user", content: [{ type: "image_url", image_url: { url: "data:image/png;base64,AAAA" } }] }, + ], + }), + }), + ) + expect(event.request.headers.get("x-initiator")).toBe("agent") + expect(event.request.headers.get("copilot-vision-request")).toBe("true") + }), + ) + it.effect("keeps a declared agent initiator when the body looks user-initiated", () => Effect.gen(function* () { yield* addPlugin()