diff --git a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts index ee82cc06de83..495f16646a61 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts @@ -1997,6 +1997,109 @@ describe("AcpRegistryAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + // Copilot flattens MCP titles and reports kind "read" for MCP calls and glob; its native + // tool.execution_start event carries the real identity. + it.effect("keeps Copilot's MCP and search tool identity instead of calling them file reads", () => + Effect.gen(function* () { + const sessionId = "copilot-session"; + const executionStart = (data: Record) => ({ + type: "emit_inbound", + frame: { + kind: "notification", + method: "github.com/copilot/sessionEvent", + params: { sessionId, type: "tool.execution_start", data }, + }, + }); + const update = (sessionUpdate: Record) => + claudeFrames.update(sessionId, sessionUpdate); + const session = yield* openClaudeReplaySession( + [ + { + type: "expect_outbound", + frame: { kind: "request", method: "initialize", params: "" }, + }, + { + type: "emit_inbound", + frame: { + kind: "response", + method: "initialize", + result: { + protocolVersion: 1, + agentInfo: { name: "Copilot", version: "1.0.112" }, + agentCapabilities: { loadSession: true }, + authMethods: [], + }, + }, + }, + { + type: "expect_outbound", + frame: { kind: "request", method: "session/new", params: "" }, + }, + { + type: "emit_inbound", + frame: { kind: "response", method: "session/new", result: { sessionId } }, + }, + { + type: "expect_outbound", + frame: { kind: "request", method: "session/prompt", params: "" }, + }, + executionStart({ + toolCallId: "mcp-call", + mcpServerName: "demo-library", + mcpToolName: "search_books", + }), + update({ + sessionUpdate: "tool_call", + toolCallId: "mcp-call", + title: "demo-library-search_books", + kind: "read", + status: "completed", + rawInput: { arguments: { query: "paper airplanes" }, limit: 2 }, + }), + executionStart({ + toolCallId: "native-search", + toolName: "glob", + toolTitle: "Finding files", + arguments: { pattern: "*.ts", paths: ["/workspace"] }, + }), + update({ + sessionUpdate: "tool_call", + toolCallId: "native-search", + title: "Finding files matching *.ts", + kind: "read", + status: "completed", + rawInput: { pattern: "*.ts", paths: ["/workspace"] }, + locations: [{ path: "/workspace" }], + }), + update({ sessionUpdate: "session_info_update", title: "after tools" }), + ], + undefined, + false, + "github-copilot-cli", + ); + yield* session.runtime.startTurn(session.turnInput(1)); + const events = yield* session.throughMarker("after tools"); + const lastItem = (nativeId: string) => + events + .flatMap((event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === nativeId + ? [event.turnItem] + : [], + ) + .at(-1); + const mcp = lastItem("mcp-call"); + const search = lastItem("native-search"); + assert.deepEqual( + { + mcp: mcp?.type === "dynamic_tool" ? mcp.toolName : mcp?.type, + search: search?.type === "dynamic_tool" ? search.toolName : search?.type, + }, + { mcp: "demo-library.search_books", search: "file_search" }, + ); + }).pipe(Effect.provide(layerTest)), + ); + it.effect.each([ { kind: "approval", stopReason: "end_turn", status: "completed" }, { kind: "question", stopReason: "error", status: "failed" }, diff --git a/packages/provider-acp-registry/src/server/copilotAcp.ts b/packages/provider-acp-registry/src/server/copilotAcp.ts index 848288689298..7b5027147c07 100644 --- a/packages/provider-acp-registry/src/server/copilotAcp.ts +++ b/packages/provider-acp-registry/src/server/copilotAcp.ts @@ -24,6 +24,7 @@ export const copilotClientCapabilitiesMeta = { "session.compaction_complete", "assistant.turn_start", "assistant.idle", + "tool.execution_start", ], }, }; @@ -63,9 +64,26 @@ function record(value: unknown): Record | undefined { const childSessionId = (taskToolCallId: string) => `copilot-task:${taskToolCallId}`; +// Copilot's tool_call reports MCP calls and glob/grep as kind "read" under a flattened title. +type CopilotToolIdentity = + | { readonly kind: "mcp"; readonly server: string; readonly tool: string } + | { readonly kind: "search" }; + +const COPILOT_SEARCH_TOOLS = new Set(["glob", "grep"]); + interface CopilotSessionAttribution { delta: { readonly kind: string; readonly text: string; readonly task?: string } | undefined; readonly taskByAgentId: Map; + readonly toolIdentityByCallId: Map; +} + +function copilotToolIdentity(data: Record): CopilotToolIdentity | undefined { + const server = typeof data.mcpServerName === "string" ? data.mcpServerName.trim() : ""; + const tool = typeof data.mcpToolName === "string" ? data.mcpToolName.trim() : ""; + if (server.length > 0 && tool.length > 0) return { kind: "mcp", server, tool }; + return typeof data.toolName === "string" && COPILOT_SEARCH_TOOLS.has(data.toolName) + ? { kind: "search" } + : undefined; } export function makeCopilotSubagentRouting() { @@ -73,13 +91,17 @@ export function makeCopilotSubagentRouting() { const session = (sessionId: string) => { let current = sessions.get(sessionId); if (current === undefined) { - current = { delta: undefined, taskByAgentId: new Map() }; + current = { delta: undefined, taskByAgentId: new Map(), toolIdentityByCallId: new Map() }; sessions.set(sessionId, current); } return current; }; const release = (sessionId: string, current: CopilotSessionAttribution) => { - if (current.delta === undefined && current.taskByAgentId.size === 0) { + if ( + current.delta === undefined && + current.taskByAgentId.size === 0 && + current.toolIdentityByCallId.size === 0 + ) { sessions.delete(sessionId); } }; @@ -105,6 +127,13 @@ export function makeCopilotSubagentRouting() { } return Effect.void; } + case "tool.execution_start": { + const identity = copilotToolIdentity(data); + if (identity !== undefined && typeof data.toolCallId === "string") { + session(event.sessionId).toolIdentityByCallId.set(data.toolCallId, identity); + } + return Effect.void; + } case "subagent.completed": case "subagent.failed": { const current = sessions.get(event.sessionId); @@ -154,6 +183,7 @@ export function makeCopilotSubagentRouting() { const current = sessions.get(notification.sessionId); if (current === undefined) return notification; let task: string | undefined; + let normalized = notification; if ( update.sessionUpdate === "agent_message_chunk" || update.sessionUpdate === "agent_thought_chunk" @@ -178,8 +208,25 @@ export function makeCopilotSubagentRouting() { ) { const agentId = record(record(update._meta)?.[COPILOT_META_KEY])?.agentId; task = typeof agentId === "string" ? current.taskByAgentId.get(agentId) : undefined; + const identity = current.toolIdentityByCallId.get(update.toolCallId); + if (identity !== undefined) { + if (update.status === "completed" || update.status === "failed") { + current.toolIdentityByCallId.delete(update.toolCallId); + release(notification.sessionId, current); + } + normalized = { + ...notification, + update: + identity.kind === "search" + ? { ...update, kind: "search" } + : { + ...update, + _meta: { ...update._meta, serverId: identity.server, toolName: identity.tool }, + }, + }; + } } - return task === undefined ? notification : { ...notification, sessionId: childSessionId(task) }; + return task === undefined ? normalized : { ...normalized, sessionId: childSessionId(task) }; }; return { registerExtensions, normalizeSessionUpdate };