Skip to content

Commit a5307c4

Browse files
voltcrashclaudejuliusmarminge
authored
fix(server): OpenCode 2 threads keep T3's tools after an idle project is evicted (pingdotgg#16796)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent b6295dd commit a5307c4

2 files changed

Lines changed: 62 additions & 4 deletions

File tree

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { describe } from "vite-plus/test";
3535

3636
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
3737
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
38+
import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeInstructions";
3839
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
3940
import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
4041
import {
@@ -3243,6 +3244,60 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
32433244
}).pipe(Effect.scoped),
32443245
);
32453246

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+
32463301
it.effect("registers a long thread id's MCP server under a name OpenCode accepts", () =>
32473302
Effect.gen(function* () {
32483303
const child = ThreadId.make(

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,8 +2955,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
29552955
yield* removeMcp(state.mcp);
29562956
state.mcp = undefined;
29572957
}
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
29602963
.add({
29612964
server: name,
29622965
location: { directory },
@@ -2976,11 +2979,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
29762979
),
29772980
),
29782981
);
2979-
if (added) state.mcp = wanted;
2982+
if (mcpAvailable) state.mcp = wanted;
29802983
}
29812984
const instructions = [
29822985
buildRuntimeInstructions({ harness: "OpenCode", model: turnInput.modelSelection.model }),
2983-
t3OrchestrationSystemPrompt(state.mcp !== undefined),
2986+
t3OrchestrationSystemPrompt(mcpAvailable),
29842987
]
29852988
.filter((part) => part !== undefined && part.length > 0)
29862989
.join("\n\n");

0 commit comments

Comments
 (0)