Skip to content

Commit 3d15106

Browse files
fix(opencode): settle T3's MCP tools when a reconnect lands during the add
A server that read connected before the add could still be reconnected by it, so the turn skipped the tool settle. Seeing it pending after the add, or a reconnect that forgot it, now counts as a join too. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 270b791 commit 3d15106

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3417,6 +3417,18 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
34173417
...connected,
34183418
mcpStatusChanged(server),
34193419
...turn,
3420+
// Connected before the add but pending after it: OpenCode reconnected it in between.
3421+
...connected,
3422+
add,
3423+
reply("mcp.add", null),
3424+
out("mcp.list", { "location[directory]": WORK }),
3425+
reply("mcp.list", {
3426+
location: { directory: WORK },
3427+
data: [{ name: server, status: { status: "pending" } }],
3428+
}),
3429+
mcpStatusChanged(server),
3430+
...connected,
3431+
...turn,
34203432
out("mcp.remove", { server, "location[directory]": WORK }),
34213433
reply("mcp.remove", null),
34223434
]);
@@ -3430,6 +3442,9 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
34303442
const third = yield* terminalOf(runtime).pipe(Effect.forkScoped);
34313443
yield* startedAfterSettle(runtime, thread, settles.started);
34323444
assert.equal((yield* Fiber.join(third))?.status, "completed");
3445+
const fourth = yield* terminalOf(runtime).pipe(Effect.forkScoped);
3446+
yield* startedAfterSettle(runtime, thread, settles.started);
3447+
assert.equal((yield* Fiber.join(fourth))?.status, "completed");
34333448
}),
34343449
).pipe(Effect.scoped),
34353450
);

‎packages/provider-opencode/src/server/v2/adapter.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,21 +3325,22 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
33253325

33263326
// An add to a fresh Location returns while OpenCode is still connecting.
33273327
const connectedMcpStatus = Effect.fnUntraced(function* (server: string, directory: string) {
3328-
for (;;) {
3328+
for (let waited = false; ; waited = true) {
33293329
// Registered before the list so a change landing mid-call is not lost.
33303330
const changed = yield* Deferred.make<void>();
33313331
mcpStatusChanges.set(server, changed);
33323332
const status = yield* mcpStatusOf(server, directory);
3333-
if (status !== "pending") return status;
3333+
if (status !== "pending") return { status, waited };
33343334
yield* Deferred.await(changed);
33353335
}
33363336
});
33373337

33383338
const addMcp = Effect.fnUntraced(
33393339
function* (state: ThreadState, wanted: NonNullable<ThreadState["mcp"]>, endpoint: string) {
33403340
// Idle Location eviction drops the server without an event, and a reconnect's status
3341-
// event can trail the list reply, so only the state before the add tells them apart.
3342-
const joining =
3341+
// event can trail the list reply, so the state before the add tells them apart. A server
3342+
// seen pending, or a reconnect that forgets it, joins during this add instead.
3343+
let joining =
33433344
state.mcp === undefined ||
33443345
(yield* mcpStatusOf(wanted.name, wanted.directory).pipe(
33453346
Effect.timeout(INVENTORY_TIMEOUT),
@@ -3359,14 +3360,19 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
33593360
.pipe(
33603361
Effect.andThen(
33613362
Effect.suspend(() => {
3363+
if (state.mcp === undefined) joining = true;
33623364
state.mcp = wanted;
33633365
return connectedMcpStatus(wanted.name, wanted.directory);
33643366
}),
33653367
),
33663368
Effect.timeout(INVENTORY_TIMEOUT),
3367-
Effect.flatMap((status) =>
3369+
Effect.flatMap(({ status, waited }) =>
33683370
status === "connected"
3369-
? Effect.succeed(joining ? ("joining" as const) : ("connected" as const))
3371+
? Effect.succeed(
3372+
joining || waited || state.mcp === undefined
3373+
? ("joining" as const)
3374+
: ("connected" as const),
3375+
)
33703376
: Effect.logWarning("OpenCode did not connect T3 Code's MCP server.", {
33713377
status,
33723378
}).pipe(Effect.as("unavailable" as const)),

0 commit comments

Comments
 (0)