The best way to control Claude Code, Codex and more from any computer or phone
fix(server): OpenCode 2 threads keep T3's tools after an idle project is evicted (#16796)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Lakshmi Tanmay committed
Oct 19, 2026 at 10:28 UTC
a5307c44f5de5933ad4a515d57d1460e7d097091
2 files changed
+62
-4
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+55
@@ -35,6 +35,7 @@ import { describe } from "vite-plus/test";
35
36
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
37
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
38
+import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeInstructions";
39
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
40
import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
41
import {
@@ -3243,6 +3244,60 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
3244
}).pipe(Effect.scoped),
3245
);
3246
3247
+ it.effect("re-registers T3 MCP each turn and drops its instructions when adding fails", () =>
3248
+ Effect.gen(function* () {
3249
+ const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
3250
+ yield* mcpSessions.set({
3251
+ environmentId: EnvironmentId.make("environment:opencode2-adapter"),
3252
+ threadId,
3253
+ providerSessionId: "mcp:opencode2-adapter",
3254
+ providerInstanceId: instanceId,
3255
+ endpoint: "http://127.0.0.1:3773/mcp",
3256
+ authorizationHeader: "Bearer thread-credential",
3257
+ browserToolsAvailable: false,
3258
+ });
3259
+ yield* Effect.addFinalizer(() => mcpSessions.clear(threadId));
3260
+ const server = "t3-code-thread_opencode2-adapter";
3261
+ const add = out("mcp.add", {
3262
+ server,
3263
+ "location[directory]": WORK,
3264
+ config: {
3265
+ type: "remote",
3266
+ url: "http://127.0.0.1:3773/mcp",
3267
+ headers: { Authorization: "Bearer thread-credential" },
3268
+ oauth: false,
3269
+ },
3270
+ });
3271
+ const turn = [
3272
+ out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3273
+ promptAccepted,
3274
+ event("session.execution.succeeded", { sessionID: SESSION }),
3275
+ ];
3276
+ // Idle Location eviction drops OpenCode's runtime MCP servers without a reconnect.
3277
+ const { runtime, thread } = yield* resumed([
3278
+ add,
3279
+ reply("mcp.add", null),
3280
+ ...turn,
3281
+ add,
3282
+ reply("mcp.add", { status: 500, body: { message: "MCP unavailable" } }),
3283
+ out("session.instructions.entry.put", {
3284
+ sessionID: SESSION,
3285
+ key: "t3-code",
3286
+ value: buildRuntimeInstructions({ harness: "OpenCode", model: bigPickle.model }),
3287
+ }),
3288
+ reply("session.instructions.entry.put", null),
3289
+ ...turn,
3290
+ out("mcp.remove", { server, "location[directory]": WORK }),
3291
+ reply("mcp.remove", null),
3292
+ ]);
3293
+ for (let index = 0; index < 2; index++) {
3294
+ const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
3295
+ yield* runtime.startTurn(turnInput(thread));
3296
+ assert.equal((yield* Fiber.join(terminal))?.status, "completed");
3297
+ }
3298
+ }).pipe(Effect.scoped),
3299
+ );
3300
+
3301
it.effect("registers a long thread id's MCP server under a name OpenCode accepts", () =>
3302
Effect.gen(function* () {
3303
const child = ThreadId.make(
packages/provider-opencode/src/server/v2/adapter.ts
+7
-4
@@ -2955,8 +2955,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2955
yield* removeMcp(state.mcp);
2956
state.mcp = undefined;
2957
}
2958
- if (wanted !== undefined && state.mcp === undefined) {
2959
- const added = yield* client.mcp
2958
+ // Idle Location eviction drops runtime MCP servers without clearing T3's cache.
2959
+ // Re-adding an unchanged config keeps its existing connection.
2960
+ let mcpAvailable = false;
2961
+ if (wanted !== undefined) {
2962
+ mcpAvailable = yield* client.mcp
2963
.add({
2964
server: name,
2965
location: { directory },
@@ -2976,11 +2979,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2979
),
2980
),
2981
);
2979
- if (added) state.mcp = wanted;
2982
+ if (mcpAvailable) state.mcp = wanted;
2983
}
2984
const instructions = [
2985
buildRuntimeInstructions({ harness: "OpenCode", model: turnInput.modelSelection.model }),
2983
- t3OrchestrationSystemPrompt(state.mcp !== undefined),
2986
+ t3OrchestrationSystemPrompt(mcpAvailable),
2987
]
2988
.filter((part) => part !== undefined && part.length > 0)
2989
.join("\n\n");