Skip to content

Commit 58c9948

Browse files
t3dotggclaude
andcommitted
fix: address computer access review findings on the port
- The proxy closes the control connection only after ending the session, and T3 waits for that, so a new turn cannot reach the old driver. - A driver that refuses the replayed handshake fails the queued calls instead of receiving them. - OpenCode names only the servers it accepted in the prompt. - The setup dialog cancels only a permission request it started. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 8dbc443 commit 58c9948

4 files changed

Lines changed: 58 additions & 21 deletions

File tree

‎apps/server/src/mcp/CuaMcpProxy.test.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { endCuaSession, ensureCuaMcpProxy } from "./CuaMcpProxy.ts";
1212

1313
// Stands in for `cua-driver mcp`: answers `initialize`, reports its pid and
1414
// whether the handshake finished, and answers "hang" only as it is stopped.
15+
// With REFUSE_REPLAY set, it refuses the handshake the proxy replays.
1516
const FAKE_DRIVER = `
1617
let initialized = false;
1718
let hanging = null;
@@ -23,7 +24,11 @@ process.stdin.on("data", (chunk) => {
2324
input = lines.pop();
2425
for (const line of lines) {
2526
const message = JSON.parse(line);
26-
if (message.method === "initialize") reply({ id: message.id, result: { protocolVersion: "2025-06-18" } });
27+
if (message.method === "initialize" && process.env.REFUSE_REPLAY && String(message.id).startsWith("t3-replay")) {
28+
reply({ id: message.id, error: { code: -32603, message: "refused" } });
29+
} else if (message.method === "initialize") {
30+
reply({ id: message.id, result: { protocolVersion: "2025-06-18" } });
31+
}
2732
if (message.method === "notifications/initialized") initialized = true;
2833
if (message.method === "tools/call" && message.params.name === "hang") hanging = message.id;
2934
if (message.method === "tools/call" && message.params.name === "whoami") {
@@ -42,7 +47,7 @@ afterEach(() => {
4247
for (const dir of temporaryDirs.splice(0)) NodeFS.rmSync(dir, { recursive: true, force: true });
4348
});
4449

45-
const startProxy = Effect.fn(function* () {
50+
const startProxy = Effect.fn(function* (driverEnv: Record<string, string> = {}) {
4651
const dir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-cua-proxy-"));
4752
temporaryDirs.push(dir);
4853
const driverScript = NodePath.join(dir, "fake-driver.mjs");
@@ -58,6 +63,7 @@ const startProxy = Effect.fn(function* () {
5863
T3_CUA_DRIVER: driver,
5964
T3_CUA_CONTROL: control,
6065
T3_SERVER_PID: String(process.pid),
66+
...driverEnv,
6167
},
6268
stdio: ["pipe", "pipe", "inherit"],
6369
});
@@ -129,4 +135,20 @@ describe("Cua MCP proxy", () => {
129135
yield* Effect.promise(() => exited);
130136
}).pipe(Effect.provide(NodeServices.layer)),
131137
);
138+
139+
it.effect("fails queued calls when a new driver refuses the replayed handshake", () =>
140+
Effect.gen(function* () {
141+
const { proxy, control, request } = yield* startProxy({ REFUSE_REPLAY: "1" });
142+
143+
yield* endCuaSession(control);
144+
const refused = yield* Effect.promise(() =>
145+
request(2, "tools/call", { name: "whoami", arguments: {} }),
146+
);
147+
expect(refused.error?.message).toContain("Computer use stopped");
148+
149+
const exited = new Promise((resolve) => proxy.once("exit", resolve));
150+
proxy.stdin.end();
151+
yield* Effect.promise(() => exited);
152+
}).pipe(Effect.provide(NodeServices.layer)),
153+
);
132154
});

‎apps/server/src/mcp/CuaMcpProxy.ts‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,19 @@ function start() {
7878
if (!line.trim()) continue;
7979
const message = parse(line);
8080
if (replay !== null && message?.id === replay) {
81+
replay = null;
82+
// A driver that refuses the replayed handshake cannot serve the
83+
// queued calls; they fail, and the next call starts a new driver.
84+
if (message.error !== undefined) {
85+
child = null;
86+
current.kill();
87+
failPending();
88+
return;
89+
}
8190
if (initialized) {
8291
current.stdin.write(JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" }) + "\\n");
8392
}
8493
// Only the first match is the replayed handshake; later ones are the client's.
85-
replay = null;
8694
ready = true;
8795
flush();
8896
continue;
@@ -137,11 +145,12 @@ function shutdown() {
137145
138146
// A newer proxy for the thread takes the path over from an older one.
139147
if (!process.platform.startsWith("win")) rmSync(controlPath, { force: true });
140-
// A connection is the whole message: end the session. Without the socket the
148+
// A connection is the whole message: end the session, then close the
149+
// connection, which tells T3 the session has ended. Without the socket the
141150
// session still ends when this proxy exits, so a failed listen is not fatal.
142151
createServer((socket) => {
143-
socket.destroy();
144152
end();
153+
socket.destroy();
145154
})
146155
.on("error", (error) => console.error("cua-mcp-proxy: control socket failed:", error.message))
147156
.listen(controlPath);
@@ -195,15 +204,16 @@ export const cuaControlPath = Effect.fn("CuaMcpProxy.controlPath")(function* (
195204
return platform === "win32" ? `\\\\.\\pipe\\${name}` : path.join(NodeOS.tmpdir(), `${name}.sock`);
196205
});
197206

198-
/** Asks a proxy to end its Cua session. A proxy that is not running means there is nothing to end. */
207+
/**
208+
* Asks a proxy to end its Cua session and waits until it has: the proxy closes
209+
* the connection after ending it. A proxy that is not running means there is
210+
* nothing to end, and a stuck one is not waited on for long.
211+
*/
199212
export const endCuaSession = (controlPath: string) =>
200213
Effect.callback<void>((resume) => {
201214
const socket = NodeNet.connect(controlPath);
202-
const done = () => {
203-
socket.destroy();
204-
resume(Effect.void);
205-
};
206-
socket.once("connect", done);
207-
socket.once("error", done);
215+
socket.once("close", () => resume(Effect.void));
216+
// An error is followed by `close`.
217+
socket.once("error", () => {});
208218
return Effect.sync(() => socket.destroy());
209-
});
219+
}).pipe(Effect.timeout("2 seconds"), Effect.ignore);

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import * as Stream from "effect/Stream";
5656

5757
import { resolveAttachmentPath } from "../../attachmentStore.ts";
5858
import * as ServerConfig from "../../config.ts";
59+
import type { LocalMcpServer } from "../../mcp/ComputerAccess.ts";
5960
import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
6061
import type { EventNdjsonLogger } from "../../provider/Layers/EventNdjsonLogger.ts";
6162
import * as ProviderEventLoggers from "../../provider/Layers/ProviderEventLoggers.ts";
@@ -971,8 +972,9 @@ export function makeOpenCodeAdapterV2(
971972
const mcpSession = McpProviderSession.readMcpProviderSession(input.threadId);
972973
const hasT3Mcp = mcpSession !== undefined && !connection.external;
973974
const orchestrationSystemPrompt = t3OrchestrationSystemPrompt(hasT3Mcp);
974-
// An external server is shared, so T3 adds its servers only to one it spawned.
975-
const localMcpServers = hasT3Mcp ? (mcpSession.localMcpServers ?? []) : [];
975+
// An external server is shared, so T3 adds its servers only to one it
976+
// spawned. Only the servers OpenCode accepted are named in the prompt.
977+
const localMcpServers: Array<LocalMcpServer> = [];
976978
if (hasT3Mcp) {
977979
yield* OpenCodeRuntime.runOpenCodeSdk("mcp.add", () =>
978980
client.mcp.add({
@@ -986,7 +988,7 @@ export function makeOpenCodeAdapterV2(
986988
}),
987989
);
988990
// Computer access is an addition: a server that cannot start it still runs.
989-
for (const local of localMcpServers) {
991+
for (const local of mcpSession.localMcpServers ?? []) {
990992
yield* OpenCodeRuntime.runOpenCodeSdk("mcp.add", () =>
991993
client.mcp.add({
992994
name: local.name,
@@ -997,6 +999,7 @@ export function makeOpenCodeAdapterV2(
997999
},
9981000
}),
9991001
).pipe(
1002+
Effect.andThen(Effect.sync(() => localMcpServers.push(local))),
10001003
Effect.catch((cause) =>
10011004
Effect.logWarning("Could not add a computer access MCP server to OpenCode.", {
10021005
server: local.name,

‎apps/web/src/components/settings/ComputerAccessSetupDialog.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ export function ComputerAccessSetupDialog({
9191
};
9292
}, [requesting]);
9393

94-
// Set as soon as Start is pressed, since the status that reports the
95-
// request can arrive after the dialog closes.
94+
// Whether this dialog started a permission request. Set as soon as Start is
95+
// pressed, since the status that reports the request can arrive after the
96+
// dialog closes.
9697
const requestStarted = useRef(false);
9798
const runAction = async (action: ServerComputerAccessAction) => {
9899
if (action === "request-cua-permissions") requestStarted.current = true;
@@ -104,10 +105,11 @@ export function ComputerAccessSetupDialog({
104105
onRefresh();
105106
return result._tag !== "Failure";
106107
};
107-
// A request left running would keep waiting on the user for minutes, so
108-
// the dialog cancels it when it closes for any reason.
108+
// A request left running would keep waiting on the user for minutes, so the
109+
// dialog cancels its own request when it closes for any reason. Another
110+
// client's request is left alone.
109111
const cancelRequest = useEffectEvent(() => {
110-
if (requesting || requestStarted.current) {
112+
if (requestStarted.current) {
111113
void run({ environmentId, input: { action: "cancel-cua-permissions" } });
112114
}
113115
});

0 commit comments

Comments
 (0)