Skip to content

Commit 144bc1d

Browse files
fix(opencode): OpenCode 2 threads see T3 Code's MCP tools on their first turn
OpenCode before 2.2.6 loads a newly connected MCP server's tools into its tool registry 100 ms after the connection, and its pre-request flush does not wait for that reload. T3 adds the per-thread MCP server right before the first prompt, so that prompt was built without T3's tools. After an add that newly registers the server, wait past that reload on those versions before prompting. Fixes #22945 AI-assisted; verified with the tests and tools listed above.
1 parent b68aedf commit 144bc1d

2 files changed

Lines changed: 39 additions & 18 deletions

File tree

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

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4150,26 +4150,37 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
41504150
promptAccepted,
41514151
event("session.execution.succeeded", { sessionID: SESSION }),
41524152
];
4153+
const prompts: Array<unknown> = [];
41534154
// Idle Location eviction drops OpenCode's runtime MCP servers without a reconnect.
4154-
const { runtime, thread } = yield* resumed([
4155-
add,
4156-
reply("mcp.add", null),
4157-
...turn,
4158-
add,
4159-
reply("mcp.add", { status: 500, body: { message: "MCP unavailable" } }),
4160-
out("session.instructions.entry.put", {
4161-
sessionID: SESSION,
4162-
key: "t3-code",
4163-
value: buildRuntimeInstructions({ harness: "OpenCode", model: bigPickle.model }),
4164-
}),
4165-
reply("session.instructions.entry.put", null),
4166-
...turn,
4167-
out("mcp.remove", { server, "location[directory]": WORK }),
4168-
reply("mcp.remove", null),
4169-
]);
4155+
const { runtime, thread } = yield* resumed(
4156+
[
4157+
add,
4158+
reply("mcp.add", null),
4159+
...turn,
4160+
add,
4161+
reply("mcp.add", { status: 500, body: { message: "MCP unavailable" } }),
4162+
out("session.instructions.entry.put", {
4163+
sessionID: SESSION,
4164+
key: "t3-code",
4165+
value: buildRuntimeInstructions({ harness: "OpenCode", model: bigPickle.model }),
4166+
}),
4167+
reply("session.instructions.entry.put", null),
4168+
...turn,
4169+
out("mcp.remove", { server, "location[directory]": WORK }),
4170+
reply("mcp.remove", null),
4171+
],
4172+
{ onPrompt: (input) => prompts.push(input) },
4173+
);
41704174
for (let index = 0; index < 2; index++) {
41714175
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
4172-
yield* runtime.startTurn(turnInput(thread));
4176+
const started = yield* runtime.startTurn(turnInput(thread)).pipe(Effect.forkScoped);
4177+
// OpenCode before 2.2.6 loads a newly added server's tools after a debounce.
4178+
if (index === 0) {
4179+
yield* TestClock.adjust("249 millis");
4180+
assert.lengthOf(prompts, 0, "prompted before OpenCode loaded the new MCP server's tools");
4181+
yield* TestClock.adjust("1 millis");
4182+
}
4183+
yield* Fiber.join(started);
41734184
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
41744185
}
41754186
}).pipe(Effect.scoped),
@@ -4230,7 +4241,11 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
42304241
runtimePolicy: policy(),
42314242
});
42324243
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
4233-
yield* runtime.startTurn({ ...turnInput(thread), threadId: child });
4244+
const started = yield* runtime
4245+
.startTurn({ ...turnInput(thread), threadId: child })
4246+
.pipe(Effect.forkScoped);
4247+
yield* TestClock.adjust("250 millis");
4248+
yield* Fiber.join(started);
42344249
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
42354250
yield* runtime.unloadThread!({ providerThread: thread });
42364251
}).pipe(Effect.scoped),

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ const RECONNECT_WAIT = "30 seconds";
592592
const LOST_BACKGROUND =
593593
"T3 Code lost its connection to OpenCode while this subagent ran, so its result is not shown.";
594594
const INVENTORY_TIMEOUT = "5 seconds";
595+
const MCP_TOOL_RELOAD_SETTLE = "250 millis";
595596
const ACTIVE_CHECK_TIMEOUT = "5 seconds";
596597
// Other failures may still have admitted the prompt.
597598
const CLEAR_PROMPT_REJECTIONS: ReadonlySet<string> = new Set([
@@ -4060,6 +4061,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
40604061
// Re-adding an unchanged config keeps its existing connection.
40614062
let mcpAvailable = false;
40624063
if (wanted !== undefined) {
4064+
const fresh = state.mcp === undefined;
40634065
mcpAvailable = yield* client.mcp
40644066
.add({
40654067
server: name,
@@ -4081,6 +4083,10 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
40814083
),
40824084
);
40834085
if (mcpAvailable) state.mcp = wanted;
4086+
// Before 2.2.6, OpenCode loads a new server's tools 100 ms after it connects, and a prompt sent sooner lacks them.
4087+
if (mcpAvailable && fresh && compareSemverVersions(connection.version, "2.2.6") < 0) {
4088+
yield* Effect.sleep(MCP_TOOL_RELOAD_SETTLE);
4089+
}
40844090
}
40854091
const instructions = [
40864092
buildRuntimeInstructions({ harness: "OpenCode", model: turnInput.modelSelection.model }),

0 commit comments

Comments
 (0)