@devries-cameron20 / t3code Mirror · read-only / commits / f6d196463c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 09:45 UTC

fix(shared): stopping an MCP call cancels its stalled SSE response (#37872)

Julius Marminge committed Nov 16, 2026 at 20:17 UTC f6d196463cf74f1d776e23578f4e920af470e7f7
5 files changed +210 -10
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+123
@@ -11,6 +11,7 @@ import { AcpRegistryCatalog } from "@t3tools/provider-acp-registry/server/AcpReg
11 import { decodeAcpReplayTranscript, makeAcpReplayRuntime } from "./AcpAdapterV2.testkit.ts";
12 // @effect-diagnostics nodeBuiltinImport:off
13 import * as NodeFS from "node:fs";
14 +import * as NodeHttp from "node:http";
15 import * as NodePath from "node:path";
16
17 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
@@ -5796,6 +5797,128 @@ describe("AcpAdapterV2", () => {
5797 }).pipe(Effect.provide(layerTest)),
5798 );
5799
5800 + it.live("Stop closes an MCP-over-ACP call stalled on its SSE response", () =>
5801 + Effect.gen(function* () {
5802 + const responseStarted = yield* Deferred.make<void>();
5803 + const responseClosed = yield* Deferred.make<void>();
5804 + const server = yield* Effect.acquireRelease(
5805 + Effect.promise(
5806 + () =>
5807 + new Promise<NodeHttp.Server>((resolve) => {
5808 + const server = NodeHttp.createServer((_request, response) => {
5809 + response.on("close", () => Deferred.doneUnsafe(responseClosed, Effect.void));
5810 + response.writeHead(200, { "content-type": "text/event-stream" });
5811 + response.write('data: {"jsonrpc":"2.0","method":"notifications/progress"}\n\n');
5812 + Deferred.doneUnsafe(responseStarted, Effect.void);
5813 + });
5814 + server.listen(0, "127.0.0.1", () => resolve(server));
5815 + }),
5816 + ),
5817 + (server) =>
5818 + Effect.sync(() => {
5819 + server.closeAllConnections();
5820 + server.close();
5821 + }),
5822 + );
5823 + const address = server.address();
5824 + if (address === null || typeof address === "string") {
5825 + return yield* Effect.die("Expected a TCP address");
5826 + }
5827 + type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
5828 + let mcpConnect: Parameters<RuntimeService["handleMcpConnect"]>[0] | undefined;
5829 + let mcpMessage: Parameters<RuntimeService["handleMcpMessage"]>[0] | undefined;
5830 + const instanceId = ProviderInstanceId.make("acp-test-stop-mcp-call");
5831 + const threadId = ThreadId.make("thread-acp-stop-mcp-call");
5832 + const idAllocator = yield* IdAllocator.IdAllocatorV2;
5833 + yield* (yield* McpProviderSessions.McpProviderSessions).set({
5834 + environmentId: EnvironmentId.make("environment-acp-stop-mcp-call"),
5835 + threadId,
5836 + providerSessionId: "mcp-session-acp-stop-mcp-call",
5837 + providerInstanceId: instanceId,
5838 + endpoint: `http://127.0.0.1:${address.port}/mcp`,
5839 + authorizationHeader: "Bearer stop-mcp-call-token",
5840 + browserToolsAvailable: false,
5841 + });
5842 + const adapter = yield* makeAcpAdapterV2({
5843 + instanceId,
5844 + flavor: {
5845 + driver: ACP_TEST_DRIVER,
5846 + capabilities: AcpProviderCapabilitiesV2,
5847 + interruptPromptOnCancel: true,
5848 + makeRuntime: makeMockRuntime({
5849 + childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
5850 + mockAgentPath: yield* bundledMockAgentPath,
5851 + environment: { T3_ACP_HANG_PROMPT_FOREVER: "1" },
5852 + wrapRuntime: (runtime) => ({
5853 + ...runtime,
5854 + handleMcpConnect: (handler) =>
5855 + Effect.sync(() => {
5856 + mcpConnect = handler;
5857 + }).pipe(Effect.andThen(runtime.handleMcpConnect(handler))),
5858 + handleMcpMessage: (handler) =>
5859 + Effect.sync(() => {
5860 + mcpMessage = handler;
5861 + }).pipe(Effect.andThen(runtime.handleMcpMessage(handler))),
5862 + }),
5863 + }),
5864 + },
5865 + selfInvocation: yield* resolveSelfInvocation(),
5866 + clientTerminals: {},
5867 + });
5868 + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
5869 + runtimeMode: "full-access",
5870 + interactionMode: "default",
5871 + cwd: process.cwd(),
5872 + });
5873 + const modelSelection = { instanceId, model: "default" } as const;
5874 + const runtime = yield* adapter.openSession({
5875 + threadId,
5876 + providerSessionId: ProviderSessionId.make("provider-session-acp-stop-mcp-call"),
5877 + modelSelection,
5878 + runtimePolicy,
5879 + });
5880 + const providerThread = yield* runtime.ensureThread({
5881 + threadId,
5882 + modelSelection,
5883 + runtimePolicy,
5884 + });
5885 + const turnFiber = yield* runtime
5886 + .startTurn(
5887 + makeTurnInput({
5888 + threadId,
5889 + providerThread,
5890 + instanceId,
5891 + runtimePolicy,
5892 + now: yield* DateTime.now,
5893 + }),
5894 + )
5895 + .pipe(Effect.forkScoped);
5896 + if (mcpConnect === undefined || mcpMessage === undefined) {
5897 + return yield* Effect.die("ACP runtime must register MCP-over-ACP handlers");
5898 + }
5899 + const { connectionId } = yield* mcpConnect(
5900 + { serverId: "t3-code" },
5901 + { requestId: "stop-mcp-connect", method: "mcp/connect" },
5902 + );
5903 + const call = yield* mcpMessage(
5904 + { connectionId, method: "tools/call", params: { name: "task_status", arguments: {} } },
5905 + { requestId: "stop-mcp-call", method: "mcp/message" },
5906 + ).pipe(Effect.exit, Effect.forkScoped);
5907 + yield* Deferred.await(responseStarted);
5908 +
5909 + yield* runtime.interruptTurn({
5910 + providerThread,
5911 + providerTurnId: idAllocator.derive.providerTurn({
5912 + driver: ACP_TEST_DRIVER,
5913 + nativeTurnId: acpScopedNativeId(instanceId, "mock-session-1:turn:1"),
5914 + }),
5915 + });
5916 + assert.isTrue(Exit.isFailure(yield* Fiber.join(call)));
5917 + yield* Deferred.await(responseClosed);
5918 + yield* Fiber.join(turnFiber);
5919 + }).pipe(Effect.provide(layerTest)),
5920 + );
5921 +
5922 it.effect.each([
5923 {
5924 label: "grants an ID-only execute approval its tracked command",
packages/provider-acp/src/server/adapter.ts
+9 -2
@@ -6243,10 +6243,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
6243 ),
6244 );
6245
6246 - const cancelPendingRuntimeRequests = Effect.fnUntraced(function* () {
6246 + const cancelPendingRuntimeRequests = Effect.fnUntraced(function* (
6247 + options: { readonly stopMcpCalls: boolean } = { stopMcpCalls: true },
6248 + ) {
6249 const waiting = turnAttached;
6250 turnAttached = yield* Deferred.make<boolean>();
6251 yield* Deferred.succeed(waiting, false);
6252 + // session/cancel does not end the agent's MCP calls; T3 runs them, so it stops them.
6253 + if (options.stopMcpCalls && runtimeMcpBridge !== undefined) {
6254 + yield* runtimeMcpBridge.stopCalls;
6255 + }
6256 const requests = yield* Ref.modify(pendingRuntimeRequests, (current) => [
6257 [...current.values()],
6258 new Map<string, PendingRuntimeRequest>(),
@@ -9622,7 +9628,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
9628 if (hardRestart || turnInput.requestRuntimeRestart === true) {
9629 yield* quarantineStoppedRun();
9630 }
9625 - yield* cancelPendingRuntimeRequests();
9631 + // A settled soft interrupt keeps background subagents, and with them their MCP calls.
9632 + yield* cancelPendingRuntimeRequests({ stopMcpCalls: !settledSoftInterrupt });
9633 // Finalize before killing so projection catches up and concurrent interrupts see activeTurn cleared.
9634 if (
9635 (hardRestart || context.promptSettled || settledSoftInterrupt) &&
packages/provider-acp/src/server/mcpOverAcpBridge.ts
+25 -1
@@ -1,4 +1,5 @@
1 // @effect-diagnostics globalJsonInEffect:off
2 +import * as Deferred from "effect/Deferred";
3 import * as Effect from "effect/Effect";
4 import * as Schema from "effect/Schema";
5 import * as Semaphore from "effect/Semaphore";
@@ -81,6 +82,8 @@ export interface AcpMcpOverAcpBridge {
82 readonly disconnect: (
83 request: AcpSchema.DisconnectMcpRequest,
84 ) => Effect.Effect<AcpSchema.DisconnectMcpResponse, AcpMcpOverAcpError>;
85 + /** Fails every call in flight, so Stop releases their HTTP responses. */
86 + readonly stopCalls: Effect.Effect<void>;
87 readonly dispose: Effect.Effect<void>;
88 }
89
@@ -89,6 +92,7 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
92 ): Effect.fn.Return<AcpMcpOverAcpBridge> {
93 const fetchImplementation = options.fetchImplementation ?? fetch;
94 const connections = yield* Effect.sync(() => new Map<string, Connection>());
95 + let callsStopped = yield* Deferred.make<void>();
96
97 const connectionFor = (connectionId: string): Effect.Effect<Connection, AcpMcpOverAcpError> =>
98 Effect.suspend(() => {
@@ -108,7 +112,7 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
112 connection.protocolVersion === null ? connection.mutex.withPermits(1)(effect) : effect,
113 );
114
111 - const send = (
115 + const sendNow = (
116 connection: Connection,
117 message: unknown,
118 ): Effect.Effect<ReadonlyArray<unknown>, AcpMcpOverAcpError> =>
@@ -156,6 +160,21 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
160 }).pipe(Effect.mapError(bridgeError)),
161 );
162
163 + const send = (
164 + connection: Connection,
165 + message: unknown,
166 + ): Effect.Effect<ReadonlyArray<unknown>, AcpMcpOverAcpError> =>
167 + Effect.suspend(() =>
168 + Effect.raceFirst(
169 + sendNow(connection, message),
170 + Deferred.await(callsStopped).pipe(
171 + Effect.andThen(
172 + Effect.fail(new AcpMcpOverAcpError({ message: "The MCP call was stopped." })),
173 + ),
174 + ),
175 + ),
176 + );
177 +
178 const disconnect = Effect.fn("mcpOverAcpBridge.disconnect")(function* (
179 request: AcpSchema.DisconnectMcpRequest,
180 ): Effect.fn.Return<AcpSchema.DisconnectMcpResponse, AcpMcpOverAcpError> {
@@ -244,6 +263,11 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
263 });
264 }),
265 disconnect,
266 + stopCalls: Effect.suspend(() => {
267 + const stopped = callsStopped;
268 + callsStopped = Deferred.makeUnsafe();
269 + return Deferred.succeed(stopped, undefined);
270 + }).pipe(Effect.asVoid),
271 dispose: Effect.suspend(() =>
272 Effect.forEach(
273 [...connections.keys()],
packages/shared/src/mcpResponsePayloads.test.ts
+30
@@ -1,5 +1,6 @@
1 import { expect, it } from "@effect/vitest";
2 import * as Effect from "effect/Effect";
3 +import * as Fiber from "effect/Fiber";
4 import * as Stream from "effect/Stream";
5
6 import { responsePayloads } from "./mcpResponsePayloads.ts";
@@ -91,3 +92,32 @@ it.effect("picks the SSE reader from the media type alone, whatever its case", (
92 expect(Array.from(jsonPayloads)).toEqual([{ jsonrpc: "2.0", id: 1, result: {} }]);
93 }),
94 );
95 +
96 +it.effect("releases a stalled SSE response body when its consumer is interrupted", () =>
97 + Effect.gen(function* () {
98 + const reading = Promise.withResolvers<void>();
99 + let controller!: ReadableStreamDefaultController<Uint8Array>;
100 + const body = new ReadableStream<Uint8Array>(
101 + {
102 + start(value) {
103 + controller = value;
104 + },
105 + pull() {
106 + reading.resolve();
107 + },
108 + },
109 + { highWaterMark: 0 },
110 + );
111 + const response = new Response(body, { headers: { "content-type": "text/event-stream" } });
112 + const consumer = yield* Stream.runDrain(responsePayloads(response, String)).pipe(
113 + Effect.forkChild,
114 + );
115 + yield* Effect.promise(() => reading.promise);
116 + yield* Fiber.interrupt(consumer);
117 + try {
118 + expect(body.locked, "interrupted SSE response must release its body").toBe(false);
119 + } finally {
120 + if (body.locked) controller.close();
121 + }
122 + }),
123 +);
packages/shared/src/mcpResponsePayloads.ts
+23 -7
@@ -4,12 +4,15 @@ import * as Stream from "effect/Stream";
4
5 const SSE_EVENT_SEPARATOR = /(?:\r\n|\r(?!\n)|\n){2}/u;
6
7 -async function* sseDataLines(response: Response): AsyncGenerator<string> {
8 - if (response.body === null) return;
7 +async function* sseDataLines(
8 + reader: ReadableStreamDefaultReader<Uint8Array>,
9 +): AsyncGenerator<string> {
10 const decoder = new TextDecoder();
11 let buffered = "";
11 - for await (const chunk of response.body) {
12 - buffered += decoder.decode(chunk as Uint8Array, { stream: true });
12 + while (true) {
13 + const { done, value } = await reader.read();
14 + if (done) return;
15 + buffered += decoder.decode(value, { stream: true });
16 let separator = SSE_EVENT_SEPARATOR.exec(buffered);
17 while (separator !== null) {
18 const rawEvent = buffered.slice(0, separator.index);
@@ -37,9 +40,22 @@ export function responsePayloads<E>(
40 }
41 const mediaType = (response.headers.get("content-type") ?? "").split(";", 1)[0]!.trim();
42 if (mediaType.toLowerCase() === "text/event-stream") {
40 - return Stream.fromAsyncIterable(sseDataLines(response), onError).pipe(
41 - Stream.mapEffect((data) => Effect.try({ try: () => JSON.parse(data), catch: onError })),
42 - );
43 + const body = response.body;
44 + if (body === null) return Stream.empty;
45 + // Interrupting fromAsyncIterable cannot return a generator parked on read(), so the
46 + // reader is cancelled from a finalizer instead; that settles the read and closes the body.
47 + return Stream.unwrap(
48 + Effect.acquireRelease(
49 + Effect.sync(() => body.getReader()),
50 + (reader) =>
51 + Effect.promise(() =>
52 + reader
53 + .cancel()
54 + .catch(() => undefined)
55 + .then(() => reader.releaseLock()),
56 + ),
57 + ).pipe(Effect.map((reader) => Stream.fromAsyncIterable(sseDataLines(reader), onError))),
58 + ).pipe(Stream.mapEffect((data) => Effect.try({ try: () => JSON.parse(data), catch: onError })));
59 }
60 return Stream.unwrap(
61 Effect.tryPromise({ try: () => response.text(), catch: onError }).pipe(