Skip to content

Commit c458c0a

Browse files
narcisoafclaude
andcommitted
fix(mcp): keep watching a late external OpenCode add past the wait limit
After two minutes the background follow-up disconnects best-effort and keeps awaiting the add, disconnecting again once it settles, so an add that lands even later can't leave a connected registration behind. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent fcc58d5 commit c458c0a

2 files changed

Lines changed: 85 additions & 8 deletions

File tree

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,77 @@ describe("OpenCodeAdapterV2", () => {
676676
}).pipe(Effect.provide(IdAllocator.layer), Effect.scoped),
677677
);
678678

679+
it.effect(
680+
"disconnects again once a shared MCP server add lands after the background wait gave up",
681+
() =>
682+
Effect.gen(function* () {
683+
const scope = yield* Scope.make();
684+
const threadId = ThreadId.make("thread-opencode-latest-add");
685+
McpProviderSession.setMcpProviderSession({
686+
environmentId: EnvironmentId.make("environment-opencode-latest-add"),
687+
threadId,
688+
providerSessionId: "mcp-session-opencode-latest-add",
689+
providerInstanceId: ProviderInstanceId.make("opencode-latest-add"),
690+
endpoint: "http://127.0.0.1:43123/mcp",
691+
authorizationHeader: "Bearer thread-credential",
692+
browserToolsAvailable: false,
693+
sharedServers: [
694+
{ name: "gateway", url: "http://127.0.0.1:3050/mcp", enabled: true, headers: {} },
695+
],
696+
});
697+
yield* Effect.addFinalizer(() =>
698+
Effect.sync(() => McpProviderSession.clearMcpProviderSession(threadId)),
699+
);
700+
const calls: string[] = [];
701+
const addAnswered = promiseGate<void>();
702+
const firstDisconnect = promiseGate<void>();
703+
const secondDisconnect = promiseGate<void>();
704+
const opened = yield* makeOpenCodeRuntimeHarness("latest-add", "root", {
705+
event: { subscribe: async () => ({ stream: asyncEventStream().stream }) },
706+
session: {
707+
create: async () => ({ data: { id: "root", time: { created: 1, updated: 1 } } }),
708+
abort: async () => ({ data: true }),
709+
children: async () => ({ data: [] }),
710+
},
711+
mcp: {
712+
add: async ({ name }: { name: string }) => {
713+
calls.push(`add:${name}`);
714+
await addAnswered.promise;
715+
calls.push(`added:${name}`);
716+
return { data: {} };
717+
},
718+
disconnect: async ({ name }: { name: string }) => {
719+
calls.push(`disconnect:${name}`);
720+
(calls.filter((call) => call.startsWith("disconnect:")).length === 1
721+
? firstDisconnect
722+
: secondDisconnect
723+
).resolve();
724+
return { data: true };
725+
},
726+
},
727+
}).pipe(Effect.provideService(Scope.Scope, scope), Effect.forkChild);
728+
yield* TestClock.adjust("5 seconds");
729+
yield* Fiber.join(opened);
730+
const closed = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild);
731+
yield* TestClock.adjust("5 seconds");
732+
yield* Fiber.join(closed);
733+
// The background wait gives up and disconnects best-effort.
734+
yield* TestClock.adjust("2 minutes");
735+
yield* Effect.promise(() => firstDisconnect.promise);
736+
// OpenCode lands the add later still; it is disconnected again.
737+
addAnswered.resolve();
738+
yield* Effect.promise(() => secondDisconnect.promise);
739+
740+
const gateway = sharedMcpServerName(threadId, "gateway");
741+
assert.deepEqual(calls, [
742+
`add:${gateway}`,
743+
`disconnect:${gateway}`,
744+
`added:${gateway}`,
745+
`disconnect:${gateway}`,
746+
]);
747+
}).pipe(Effect.provide(IdAllocator.layer), Effect.scoped),
748+
);
749+
679750
it.effect.each(["enumeration", "abort", "not-found", "timeout"] as const)(
680751
"reports descendant cleanup %s",
681752
(failure) =>

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export { openCodeToolProjectionKind } from "./OpenCodeToolItems.ts";
9292
* server name) is replaced by a digest of it.
9393
*/
9494
const SHARED_MCP_ADD_TIMEOUT = "5 seconds";
95-
/** How long a late add on an external server is followed before it is disconnected regardless. */
95+
/** How long a late add on an external server is awaited before a first, best-effort disconnect. */
9696
const SHARED_MCP_LATE_ADD_LIMIT = "2 minutes";
9797

9898
export const sharedMcpServerPrefix = (threadId: string) => {
@@ -1072,13 +1072,19 @@ export function makeOpenCodeAdapterV2(
10721072
Effect.matchEffect({
10731073
onSuccess: () => disconnect(name),
10741074
onFailure: () =>
1075-
Fiber.await(fiber).pipe(
1076-
Effect.timeout(SHARED_MCP_LATE_ADD_LIMIT),
1077-
Effect.ignore,
1078-
Effect.andThen(disconnect(name)),
1079-
Effect.forkDetach,
1080-
Effect.asVoid,
1081-
),
1075+
Effect.gen(function* () {
1076+
const settled = yield* Fiber.await(fiber).pipe(
1077+
Effect.timeout(SHARED_MCP_LATE_ADD_LIMIT),
1078+
Effect.option,
1079+
);
1080+
yield* disconnect(name);
1081+
// Disconnected at the limit regardless, then again
1082+
// once the add settles, should it land after all.
1083+
if (Option.isNone(settled)) {
1084+
yield* Fiber.await(fiber);
1085+
yield* disconnect(name);
1086+
}
1087+
}).pipe(Effect.forkDetach, Effect.asVoid),
10821088
}),
10831089
),
10841090
// Side by side, so several slow adds cost one wait, not one each.

0 commit comments

Comments
 (0)