Skip to content

Commit 463a097

Browse files
fix(opencode): a failed MCP refresh keeps the thread's claim on t3-code
AI-assisted; verified with the tests and tools listed above.
1 parent 7588d14 commit 463a097

2 files changed

Lines changed: 98 additions & 21 deletions

File tree

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

Lines changed: 96 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,30 +2830,43 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
28302830
}).pipe(Effect.scoped),
28312831
);
28322832

2833+
// regression: #33542 a failed refresh released t3-code while the thread kept using it
28332834
it.effect("re-registers T3 MCP each turn and drops its instructions when adding fails", () =>
28342835
Effect.gen(function* () {
2836+
const OTHER = "ses_f148ca2deffeOtherSession000";
2837+
const child = ThreadId.make(
2838+
"thread:delegated-task:command%3Amcp%3A48bef2bf-6d0e-4f7a-9c3b-2e5d8a1f7c40%3Adelegate-task%3Asubproject-b-round1",
2839+
);
2840+
const fallback = "t3-code-aa73fa1e03099934";
28352841
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
2836-
yield* mcpSessions.set({
2837-
environmentId: EnvironmentId.make("environment:opencode2-adapter"),
2838-
threadId,
2839-
providerSessionId: "mcp:opencode2-adapter",
2840-
providerInstanceId: instanceId,
2841-
endpoint: "http://127.0.0.1:3773/mcp",
2842-
authorizationHeader: "Bearer thread-credential",
2843-
browserToolsAvailable: false,
2844-
});
2845-
yield* Effect.addFinalizer(() => mcpSessions.clear(threadId));
2842+
for (const [id, credential] of [
2843+
[threadId, "Bearer thread-credential"],
2844+
[child, "Bearer other-credential"],
2845+
] as const) {
2846+
yield* mcpSessions.set({
2847+
environmentId: EnvironmentId.make("environment:opencode2-adapter"),
2848+
threadId: id,
2849+
providerSessionId: "mcp:opencode2-adapter",
2850+
providerInstanceId: instanceId,
2851+
endpoint: "http://127.0.0.1:3773/mcp",
2852+
authorizationHeader: credential,
2853+
browserToolsAvailable: false,
2854+
});
2855+
yield* Effect.addFinalizer(() => mcpSessions.clear(id));
2856+
}
28462857
const server = "t3-code";
2847-
const add = out("mcp.add", {
2848-
server,
2849-
"location[directory]": WORK,
2850-
config: {
2851-
type: "remote",
2852-
url: "http://127.0.0.1:3773/mcp",
2853-
headers: { Authorization: "Bearer thread-credential" },
2854-
oauth: false,
2855-
},
2856-
});
2858+
const addAs = (name: string, credential: string) =>
2859+
out("mcp.add", {
2860+
server: name,
2861+
"location[directory]": WORK,
2862+
config: {
2863+
type: "remote",
2864+
url: "http://127.0.0.1:3773/mcp",
2865+
headers: { Authorization: credential },
2866+
oauth: false,
2867+
},
2868+
});
2869+
const add = addAs(server, "Bearer thread-credential");
28572870
const turn = [
28582871
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
28592872
promptAccepted,
@@ -2873,6 +2886,51 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
28732886
}),
28742887
reply("session.instructions.entry.put", null),
28752888
...turn,
2889+
// The failed refresh kept A's t3-code registration, so B must not take that name.
2890+
out("session.get", { sessionID: OTHER }),
2891+
replyData("session.get", sessionInfo({ id: OTHER })),
2892+
out("permission.list", { sessionID: OTHER }),
2893+
replyData("permission.list", []),
2894+
out("session.form.list", { sessionID: OTHER }),
2895+
replyData("session.form.list", []),
2896+
out("session.update", {
2897+
sessionID: OTHER,
2898+
permissions: [
2899+
{ action: "*", resource: "*", effect: "allow" },
2900+
{ action: "t3-code*", resource: "*", effect: "deny" },
2901+
{ action: `${fallback}_*`, resource: "*", effect: "allow" },
2902+
],
2903+
}),
2904+
reply("session.update", null),
2905+
addAs(fallback, "Bearer other-credential"),
2906+
reply("mcp.add", null),
2907+
out("session.instructions.entry.put", {
2908+
sessionID: OTHER,
2909+
key: "t3-code",
2910+
value: "<any>",
2911+
}),
2912+
reply("session.instructions.entry.put", null),
2913+
out("session.prompt", { sessionID: OTHER, text: "<any>" }),
2914+
replyData("session.prompt", {
2915+
id: "msg_recorded_turn_b",
2916+
sessionID: OTHER,
2917+
time: { created: 1790656601410 },
2918+
type: "user",
2919+
payload: { text: "hi" },
2920+
delivery: "steer",
2921+
}),
2922+
event("session.execution.succeeded", { sessionID: OTHER }),
2923+
add,
2924+
reply("mcp.add", null),
2925+
out("session.instructions.entry.put", {
2926+
sessionID: SESSION,
2927+
key: "t3-code",
2928+
value: "<any>",
2929+
}),
2930+
reply("session.instructions.entry.put", null),
2931+
...turn,
2932+
out("mcp.remove", { server: fallback, "location[directory]": WORK }),
2933+
reply("mcp.remove", null),
28762934
out("mcp.remove", { server, "location[directory]": WORK }),
28772935
reply("mcp.remove", null),
28782936
]);
@@ -2881,6 +2939,24 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
28812939
yield* runtime.startTurn(turnInput(thread));
28822940
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
28832941
}
2942+
const other = yield* runtime.resumeThread({
2943+
providerThread: {
2944+
...providerThread(yield* DateTime.now),
2945+
id: ProviderThreadId.make("provider-thread:opencode2-adapter:other"),
2946+
appThreadId: child,
2947+
nativeThreadRef: { driver: OPENCODE_PROVIDER, nativeId: OTHER, strength: "strong" },
2948+
},
2949+
threadId: child,
2950+
modelSelection: bigPickle,
2951+
runtimePolicy: policy(),
2952+
});
2953+
const otherEnded = yield* terminalOf(runtime).pipe(Effect.forkScoped);
2954+
yield* runtime.startTurn({ ...turnInput(other), threadId: child });
2955+
assert.equal((yield* Fiber.join(otherEnded))?.status, "completed");
2956+
const again = yield* terminalOf(runtime).pipe(Effect.forkScoped);
2957+
yield* runtime.startTurn(turnInput(thread));
2958+
assert.equal((yield* Fiber.join(again))?.status, "completed");
2959+
yield* runtime.unloadThread!({ providerThread: other });
28842960
}).pipe(Effect.scoped),
28852961
);
28862962

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3178,7 +3178,8 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
31783178
),
31793179
);
31803180
if (mcpAvailable) state.mcp = { name, directory, credential };
3181-
else releaseSharedMcp(state, directory);
3181+
// A failed refresh keeps the cached registration, which still owns its name.
3182+
else if (state.mcp === undefined) releaseSharedMcp(state, directory);
31823183
// Rules written before this turn assumed a name; a concurrent claim may have changed it.
31833184
yield* writeRules(state, state.policy);
31843185
}

0 commit comments

Comments
 (0)