Skip to content

Commit 2f7f794

Browse files
voltcrashclaude
andcommitted
fix(server): OpenCode 2 threads keep T3's tools after an idle project is evicted
OpenCode 2 keeps MCP servers added at runtime in the project Location and drops them when it evicts a Location after 60 idle minutes. The adapter added T3's MCP server once and cached that in `state.mcp`, so later turns ran without T3's tools while the instructions still said they were there. Add the server on every turn (OpenCode leaves an unchanged config connected) and tell the model the tools are available only when this turn's add worked. Fixes #16768 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent cd41c4a commit 2f7f794

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,58 @@ describe("OpenCode2 adapter", () => {
27222722
}).pipe(Effect.scoped),
27232723
);
27242724

2725+
it.effect("adds T3's MCP server again on every turn, in case OpenCode dropped it", () =>
2726+
Effect.gen(function* () {
2727+
McpProviderSession.setMcpProviderSession({
2728+
environmentId: EnvironmentId.make("environment:opencode2-adapter"),
2729+
threadId,
2730+
providerSessionId: "mcp:opencode2-adapter",
2731+
providerInstanceId: instanceId,
2732+
endpoint: "http://127.0.0.1:3773/mcp",
2733+
authorizationHeader: "Bearer thread-credential",
2734+
browserToolsAvailable: false,
2735+
});
2736+
yield* Effect.addFinalizer(() =>
2737+
Effect.sync(() => McpProviderSession.clearMcpProviderSession(threadId)),
2738+
);
2739+
const server = "t3-code-thread_opencode2-adapter";
2740+
const add = [
2741+
out("mcp.add", {
2742+
server,
2743+
"location[directory]": WORK,
2744+
config: {
2745+
type: "remote",
2746+
url: "http://127.0.0.1:3773/mcp",
2747+
headers: { Authorization: "Bearer thread-credential" },
2748+
oauth: false,
2749+
},
2750+
}),
2751+
reply("mcp.add", null),
2752+
];
2753+
const turn = [
2754+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2755+
promptAccepted,
2756+
event("session.execution.succeeded", { sessionID: SESSION }),
2757+
];
2758+
// OpenCode keeps runtime-added servers with the Location and forgets them
2759+
// when it evicts an idle one, without a reconnect T3 could notice.
2760+
const { runtime, thread } = yield* resumed([
2761+
...add,
2762+
...turn,
2763+
...add,
2764+
...turn,
2765+
// Removed when the session closes.
2766+
out("mcp.remove", { server, "location[directory]": WORK }),
2767+
reply("mcp.remove", null),
2768+
]);
2769+
for (let index = 0; index < 2; index++) {
2770+
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
2771+
yield* runtime.startTurn(turnInput(thread));
2772+
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
2773+
}
2774+
}).pipe(Effect.scoped),
2775+
);
2776+
27252777
it.effect("registers a long thread id's MCP server under a name OpenCode accepts", () =>
27262778
Effect.gen(function* () {
27272779
// Spelled out, this delegated thread's server name would be 120 characters.

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ interface ThreadState {
396396
* inbox, where the next prompt would deliver them first, so it cancels them.
397397
*/
398398
readonly strandedSteers: Set<string>;
399-
/** T3's MCP server as registered for this thread, and the instructions entry sent with it. */
399+
/** T3's MCP server as last registered for this thread, and the instructions entry sent with it. */
400400
mcp:
401401
| { readonly name: string; readonly directory: string; readonly credential: string }
402402
| undefined;
@@ -3260,9 +3260,12 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
32603260
yield* removeMcp(state.mcp);
32613261
state.mcp = undefined;
32623262
}
3263-
// T3's tools are an addition: a server that cannot add them still runs the turn.
3264-
if (wanted !== undefined && state.mcp === undefined) {
3265-
const added = yield* client.mcp
3263+
// Added on every turn: OpenCode drops runtime-added servers when it evicts
3264+
// an idle Location, and leaves an unchanged one connected. T3's tools are
3265+
// an addition: a server that cannot add them still runs the turn.
3266+
let mcpAvailable = false;
3267+
if (wanted !== undefined) {
3268+
mcpAvailable = yield* client.mcp
32663269
.add({
32673270
server: name,
32683271
location: { directory },
@@ -3282,11 +3285,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
32823285
),
32833286
),
32843287
);
3285-
if (added) state.mcp = wanted;
3288+
if (mcpAvailable) state.mcp = wanted;
32863289
}
32873290
const instructions = [
32883291
buildRuntimeInstructions({ harness: "OpenCode", model: turnInput.modelSelection.model }),
3289-
t3OrchestrationSystemPrompt(state.mcp !== undefined),
3292+
t3OrchestrationSystemPrompt(mcpAvailable),
32903293
]
32913294
.filter((part) => part !== undefined && part.length > 0)
32923295
.join("\n\n");

0 commit comments

Comments
 (0)