Merge pull request #1953 from maximharizanov/fix/copilot-initiator-attribution

fix(copilot): mark internal hook injections as agent-initiated
This commit is contained in:
YeonGyu-Kim
2026-02-20 11:54:01 +09:00
committed by GitHub
19 changed files with 292 additions and 22 deletions
+109
View File
@@ -0,0 +1,109 @@
import { describe, expect, test } from "bun:test"
import { OMO_INTERNAL_INITIATOR_MARKER } from "../shared"
import { createChatHeadersHandler } from "./chat-headers"
describe("createChatHeadersHandler", () => {
test("sets x-initiator=agent for Copilot internal marker messages", async () => {
const handler = createChatHeadersHandler({
ctx: {
client: {
session: {
message: async () => ({
data: {
parts: [
{
type: "text",
text: `notification\n${OMO_INTERNAL_INITIATOR_MARKER}`,
},
],
},
}),
},
},
} as never,
})
const output: { headers: Record<string, string> } = { headers: {} }
await handler(
{
sessionID: "ses_1",
provider: { id: "github-copilot" },
message: {
id: "msg_1",
role: "user",
},
},
output,
)
expect(output.headers["x-initiator"]).toBe("agent")
})
test("does not override non-copilot providers", async () => {
const handler = createChatHeadersHandler({
ctx: {
client: {
session: {
message: async () => ({
data: {
parts: [
{
type: "text",
text: `notification\n${OMO_INTERNAL_INITIATOR_MARKER}`,
},
],
},
}),
},
},
} as never,
})
const output: { headers: Record<string, string> } = { headers: {} }
await handler(
{
sessionID: "ses_1",
provider: { id: "openai" },
message: {
id: "msg_1",
role: "user",
},
},
output,
)
expect(output.headers["x-initiator"]).toBeUndefined()
})
test("does not override regular user messages", async () => {
const handler = createChatHeadersHandler({
ctx: {
client: {
session: {
message: async () => ({
data: {
parts: [{ type: "text", text: "normal user message" }],
},
}),
},
},
} as never,
})
const output: { headers: Record<string, string> } = { headers: {} }
await handler(
{
sessionID: "ses_1",
provider: { id: "github-copilot" },
message: {
id: "msg_1",
role: "user",
},
},
output,
)
expect(output.headers["x-initiator"]).toBeUndefined()
})
})
+104
View File
@@ -0,0 +1,104 @@
import { OMO_INTERNAL_INITIATOR_MARKER } from "../shared"
import type { PluginContext } from "./types"
type ChatHeadersInput = {
sessionID: string
provider: { id: string }
message: {
id?: string
role?: string
}
}
type ChatHeadersOutput = {
headers: Record<string, string>
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null
}
function buildChatHeadersInput(raw: unknown): ChatHeadersInput | null {
if (!isRecord(raw)) return null
const sessionID = raw.sessionID
const provider = raw.provider
const message = raw.message
if (typeof sessionID !== "string") return null
if (!isRecord(provider) || typeof provider.id !== "string") return null
if (!isRecord(message)) return null
return {
sessionID,
provider: { id: provider.id },
message: {
id: typeof message.id === "string" ? message.id : undefined,
role: typeof message.role === "string" ? message.role : undefined,
},
}
}
function isChatHeadersOutput(raw: unknown): raw is ChatHeadersOutput {
if (!isRecord(raw)) return false
if (!isRecord(raw.headers)) {
raw.headers = {}
}
return isRecord(raw.headers)
}
function isCopilotProvider(providerID: string): boolean {
return providerID === "github-copilot" || providerID === "github-copilot-enterprise"
}
async function hasInternalMarker(
client: PluginContext["client"],
sessionID: string,
messageID: string,
): Promise<boolean> {
try {
const response = await client.session.message({
path: { id: sessionID, messageID },
})
const data = response.data
if (!isRecord(data) || !Array.isArray(data.parts)) return false
return data.parts.some((part) => {
if (!isRecord(part) || part.type !== "text" || typeof part.text !== "string") {
return false
}
return part.text.includes(OMO_INTERNAL_INITIATOR_MARKER)
})
} catch {
return false
}
}
async function isOmoInternalMessage(input: ChatHeadersInput, client: PluginContext["client"]): Promise<boolean> {
if (input.message.role !== "user") {
return false
}
if (!input.message.id) {
return false
}
return hasInternalMarker(client, input.sessionID, input.message.id)
}
export function createChatHeadersHandler(args: { ctx: PluginContext }): (input: unknown, output: unknown) => Promise<void> {
const { ctx } = args
return async (input, output): Promise<void> => {
const normalizedInput = buildChatHeadersInput(input)
if (!normalizedInput) return
if (!isChatHeadersOutput(output)) return
if (!isCopilotProvider(normalizedInput.provider.id)) return
if (!(await isOmoInternalMessage(normalizedInput, ctx.client))) return
output.headers["x-initiator"] = "agent"
}
}
+11 -1
View File
@@ -2,7 +2,17 @@ import type { Plugin, ToolDefinition } from "@opencode-ai/plugin"
export type PluginContext = Parameters<Plugin>[0]
export type PluginInstance = Awaited<ReturnType<Plugin>>
export type PluginInterface = Omit<PluginInstance, "experimental.session.compacting">
type ChatHeadersHook = PluginInstance extends { "chat.headers"?: infer T }
? T
: (input: unknown, output: unknown) => Promise<void>
export type PluginInterface = Omit<
PluginInstance,
"experimental.session.compacting" | "chat.headers"
> & {
"chat.headers"?: ChatHeadersHook
}
export type ToolsRecord = Record<string, ToolDefinition>