Skip to content

Commit 7f3e86e

Browse files
committed
fix(server): bound MCP status lookups and retry failures
1 parent 80dbf88 commit 7f3e86e

2 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.test.ts‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,94 @@ describe("OpenCodeAdapterV2", () => {
10401040
}).pipe(Effect.provide(IdAllocator.layer), Effect.scoped),
10411041
);
10421042

1043+
it.effect.each(["failure", "timeout"] as const)("retries MCP status after %s", (failure) =>
1044+
Effect.gen(function* () {
1045+
const nativeEvents = asyncEventStream();
1046+
const called = promiseGate<void>();
1047+
const sessionId = `mcp-status-${failure}`;
1048+
let statusReads = 0;
1049+
let signal: AbortSignal | undefined;
1050+
const harness = yield* makeOpenCodeRuntimeHarness(sessionId, sessionId, {
1051+
event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
1052+
session: {
1053+
create: async () => ({ data: { id: sessionId, time: { created: 1, updated: 1 } } }),
1054+
promptAsync: async () => ({ data: true }),
1055+
},
1056+
mcp: {
1057+
status: async (_input: unknown, options: { signal?: AbortSignal }) => {
1058+
statusReads++;
1059+
if (statusReads === 1) {
1060+
signal = options?.signal;
1061+
called.resolve();
1062+
if (failure === "timeout") return new Promise(() => {});
1063+
throw new Error("MCP status unavailable");
1064+
}
1065+
return { data: { weather: { status: "connected" } } };
1066+
},
1067+
},
1068+
});
1069+
yield* harness.startTurn();
1070+
const received = yield* harness.runtime.events.pipe(
1071+
Stream.takeUntil(
1072+
(event) => event.type === "turn_item.updated" && event.turnItem.type === "compaction",
1073+
),
1074+
Stream.runCollect,
1075+
Effect.forkScoped,
1076+
);
1077+
for (const status of ["running", "completed"] as const) {
1078+
const emitted = yield* Effect.promise(() =>
1079+
nativeEvents.push({
1080+
type: "message.part.updated",
1081+
properties: {
1082+
sessionID: sessionId,
1083+
part: {
1084+
id: "weather-part",
1085+
sessionID: sessionId,
1086+
messageID: "weather-message",
1087+
type: "tool",
1088+
callID: "weather-call",
1089+
tool: "weather_edit_document",
1090+
state: {
1091+
status,
1092+
input: { document: "remote-doc" },
1093+
title: "Edit remote document",
1094+
metadata: {},
1095+
output: "saved",
1096+
time: { start: 1, end: 2 },
1097+
},
1098+
},
1099+
},
1100+
}),
1101+
).pipe(Effect.forkScoped);
1102+
if (status === "running" && failure === "timeout") {
1103+
yield* Effect.promise(() => called.promise);
1104+
yield* TestClock.adjust("1 second");
1105+
}
1106+
yield* Fiber.join(emitted);
1107+
}
1108+
yield* Effect.promise(() =>
1109+
nativeEvents.push({ type: "session.compacted", properties: { sessionID: sessionId } }),
1110+
);
1111+
const completed = (yield* Fiber.join(received)).find(
1112+
(event) =>
1113+
event.type === "turn_item.updated" &&
1114+
event.turnItem.type === "dynamic_tool" &&
1115+
event.turnItem.status === "completed",
1116+
);
1117+
assert.equal(statusReads, 2);
1118+
assert.equal(
1119+
completed?.type === "turn_item.updated" && completed.turnItem.title,
1120+
"Edit remote document",
1121+
);
1122+
assert.deepEqual(completed?.type === "turn_item.updated" && completed.turnItem.toolSource, {
1123+
key: "mcp:weather",
1124+
name: "weather",
1125+
kind: "integration",
1126+
});
1127+
if (failure === "timeout") assert.isTrue(signal?.aborted);
1128+
}).pipe(Effect.provide(IdAllocator.layer), Effect.scoped),
1129+
);
1130+
10431131
it.effect("admits a native command on its user receipt before generation completes", () =>
10441132
Effect.gen(function* () {
10451133
const nativeEvents = asyncEventStream();

‎apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,19 +1597,20 @@ export function makeOpenCodeAdapterV2(
15971597
>;
15981598
const input = toolInput(part);
15991599
const output = toolOutput(part);
1600-
if (part.tool.includes("_") && turn.mcpServerNames === undefined) {
1601-
turn.mcpServerNames = yield* OpenCodeRuntime.runOpenCodeSdk("mcp.status", () =>
1602-
client.mcp.status(),
1600+
const isNativeTool = part.tool === "code_search" || part.tool === "apply_patch";
1601+
if (!isNativeTool && part.tool.includes("_") && turn.mcpServerNames === undefined) {
1602+
const serverNames = yield* OpenCodeRuntime.runOpenCodeSdk("mcp.status", (signal) =>
1603+
client.mcp.status(undefined, { signal, throwOnError: true }),
16031604
).pipe(
1605+
Effect.timeout("1 second"),
16041606
Effect.map((response) => Object.keys(response.data ?? {})),
1605-
Effect.catch(() => Effect.succeed([])),
1607+
Effect.catch(() => Effect.succeed(undefined)),
16061608
);
1609+
if (serverNames !== undefined) turn.mcpServerNames = serverNames;
16071610
}
16081611
const matchingServers = turn.mcpServerNames?.filter(
16091612
(name) =>
1610-
part.tool !== "code_search" &&
1611-
part.tool !== "apply_patch" &&
1612-
part.tool.startsWith(`${name.replace(/[^a-zA-Z0-9_-]/g, "_")}_`),
1613+
!isNativeTool && part.tool.startsWith(`${name.replace(/[^a-zA-Z0-9_-]/g, "_")}_`),
16131614
);
16141615
const serverName = matchingServers?.length === 1 ? matchingServers[0] : undefined;
16151616
const presentation =

0 commit comments

Comments
 (0)