Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>) => ({
type: "emit_inbound",
frame: {
kind: "notification",
method: "github.com/copilot/sessionEvent",
params: { sessionId, type: "tool.execution_start", data },
},
});
const update = (sessionUpdate: Record<string, unknown>) =>
claudeFrames.update(sessionId, sessionUpdate);
const session = yield* openClaudeReplaySession(
[
{
type: "expect_outbound",
frame: { kind: "request", method: "initialize", params: "<any>" },
},
{
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: "<any>" },
},
{
type: "emit_inbound",
frame: { kind: "response", method: "session/new", result: { sessionId } },
},
{
type: "expect_outbound",
frame: { kind: "request", method: "session/prompt", params: "<any>" },
},
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" },
Expand Down
53 changes: 50 additions & 3 deletions packages/provider-acp-registry/src/server/copilotAcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const copilotClientCapabilitiesMeta = {
"session.compaction_complete",
"assistant.turn_start",
"assistant.idle",
"tool.execution_start",
],
},
};
Expand Down Expand Up @@ -63,23 +64,44 @@ function record(value: unknown): Record<string, unknown> | 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<string, string>;
readonly toolIdentityByCallId: Map<string, CopilotToolIdentity>;
}

function copilotToolIdentity(data: Record<string, unknown>): 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() {
const sessions = new Map<string, CopilotSessionAttribution>();
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);
}
};
Expand All @@ -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);
Expand Down Expand Up @@ -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"
Expand All @@ -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 };
Expand Down
Loading