From 9ee9f64bc216934e73b3851d40d6332215d7dd23 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:55:40 +0100 Subject: [PATCH 1/5] test(shared): a multi-megabyte SSE event in small chunks parses within the test timeout Co-authored-by: Claude Opus 5.5 --- .../shared/src/mcpResponsePayloads.test.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/shared/src/mcpResponsePayloads.test.ts b/packages/shared/src/mcpResponsePayloads.test.ts index d3366d671586e..d77eadcd34c50 100644 --- a/packages/shared/src/mcpResponsePayloads.test.ts +++ b/packages/shared/src/mcpResponsePayloads.test.ts @@ -71,6 +71,30 @@ it.effect("reads SSE line endings across byte-chunk boundaries without splitting }), ); +it.effect("reads a multi-megabyte SSE event delivered in small chunks", () => + Effect.gen(function* () { + const text = "x".repeat(8 * 1024 * 1024); + const bytes = new TextEncoder().encode( + `data: ${JSON.stringify({ id: 1, result: { text } })}\n\n`, + ); + let offset = 0; + const response = new Response( + new ReadableStream({ + pull(controller) { + if (offset === bytes.length) return controller.close(); + controller.enqueue(bytes.subarray(offset, offset + 256)); + offset = Math.min(offset + 256, bytes.length); + }, + }), + { headers: { "content-type": "text/event-stream" } }, + ); + const payloads = yield* Stream.runCollect( + responsePayloads(response, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })), + ); + expect(Array.from(payloads)).toEqual([{ id: 1, result: { text } }]); + }), +); + it.effect("picks the SSE reader from the media type alone, whatever its case", () => Effect.gen(function* () { const response = new Response('data: {"id":1}\n\n', { From 85634a9c5ca51a4d281c67723a830659d28dc658 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:56:11 +0100 Subject: [PATCH 2/5] fix(shared): large MCP SSE results in small chunks parse in linear time Co-authored-by: Claude Opus 5.5 --- packages/shared/src/mcpResponsePayloads.ts | 34 +++++++++++++++------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/shared/src/mcpResponsePayloads.ts b/packages/shared/src/mcpResponsePayloads.ts index 8c9a38cecf18d..0b4b99e7cecf5 100644 --- a/packages/shared/src/mcpResponsePayloads.ts +++ b/packages/shared/src/mcpResponsePayloads.ts @@ -3,28 +3,40 @@ import * as Effect from "effect/Effect"; import * as Stream from "effect/Stream"; const SSE_EVENT_SEPARATOR = /(?:\r\n|\r(?!\n)|\n){2}/u; +// A separator is at most four characters, so one that a new chunk completes starts within the +// last three characters already read. Scanning only those plus the chunk keeps parsing linear. +const SEPARATOR_OVERLAP = 3; + +function sseEventData(rawEvent: string): string { + return rawEvent + .split(/\r\n|\r|\n/u) + .filter((line) => line.startsWith("data:")) + .map((line) => line.slice("data:".length).trimStart()) + .join("\n"); +} async function* sseDataLines( reader: ReadableStreamDefaultReader, ): AsyncGenerator { const decoder = new TextDecoder(); - let buffered = ""; + let eventParts: string[] = []; + let unscanned = ""; while (true) { const { done, value } = await reader.read(); if (done) return; - buffered += decoder.decode(value, { stream: true }); - let separator = SSE_EVENT_SEPARATOR.exec(buffered); + let text = unscanned + decoder.decode(value, { stream: true }); + let separator = SSE_EVENT_SEPARATOR.exec(text); while (separator !== null) { - const rawEvent = buffered.slice(0, separator.index); - buffered = buffered.slice(separator.index + separator[0].length); - const data = rawEvent - .split(/\r\n|\r|\n/u) - .filter((line) => line.startsWith("data:")) - .map((line) => line.slice("data:".length).trimStart()) - .join("\n"); + eventParts.push(text.slice(0, separator.index)); + const data = sseEventData(eventParts.join("")); + eventParts = []; + text = text.slice(separator.index + separator[0].length); if (data.length > 0) yield data; - separator = SSE_EVENT_SEPARATOR.exec(buffered); + separator = SSE_EVENT_SEPARATOR.exec(text); } + const scannedLength = Math.max(0, text.length - SEPARATOR_OVERLAP); + eventParts.push(text.slice(0, scannedLength)); + unscanned = text.slice(scannedLength); } } From c717985a97c803b5f8abe9ec08d52fe7cac8e316 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:11:48 +0100 Subject: [PATCH 3/5] test(shared): widen the large SSE regression timeout margin Preserve the original regression case and add a 32 MiB event delivered in 256-byte chunks so quadratic parsing exceeds the project timeout by a wider margin. Co-authored-by: GPT 6.1 Sol --- .../shared/src/mcpResponsePayloads.test.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/shared/src/mcpResponsePayloads.test.ts b/packages/shared/src/mcpResponsePayloads.test.ts index d77eadcd34c50..c17dfa75c0ec7 100644 --- a/packages/shared/src/mcpResponsePayloads.test.ts +++ b/packages/shared/src/mcpResponsePayloads.test.ts @@ -95,6 +95,30 @@ it.effect("reads a multi-megabyte SSE event delivered in small chunks", () => }), ); +it.effect("reads a 32 MiB SSE event delivered in 256-byte chunks", () => + Effect.gen(function* () { + const text = "x".repeat(32 * 1024 * 1024); + const bytes = new TextEncoder().encode( + `data: ${JSON.stringify({ id: 1, result: { text } })}\n\n`, + ); + let offset = 0; + const response = new Response( + new ReadableStream({ + pull(controller) { + if (offset === bytes.length) return controller.close(); + controller.enqueue(bytes.subarray(offset, offset + 256)); + offset = Math.min(offset + 256, bytes.length); + }, + }), + { headers: { "content-type": "text/event-stream" } }, + ); + const payloads = yield* Stream.runCollect( + responsePayloads(response, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })), + ); + expect(Array.from(payloads)).toEqual([{ id: 1, result: { text } }]); + }), +); + it.effect("picks the SSE reader from the media type alone, whatever its case", () => Effect.gen(function* () { const response = new Response('data: {"id":1}\n\n', { From a35381797ff93b8e5fa8108a2798a1bb37161965 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:12:18 +0100 Subject: [PATCH 4/5] chore: credit the models that worked on this change Co-authored-by: Grok 4.7 From beef8702e18bc94522f1e093af52759e91bc7e3d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 17:52:31 +0000 Subject: [PATCH 5/5] test(shared): keep one large SSE regression case The 8 MiB case already exceeds the test timeout by a wide margin on the quadratic parser; the 32 MiB case would stall CI for about half an hour on a regression. Co-Authored-By: Claude Opus 5.5 --- .../shared/src/mcpResponsePayloads.test.ts | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/packages/shared/src/mcpResponsePayloads.test.ts b/packages/shared/src/mcpResponsePayloads.test.ts index c17dfa75c0ec7..d77eadcd34c50 100644 --- a/packages/shared/src/mcpResponsePayloads.test.ts +++ b/packages/shared/src/mcpResponsePayloads.test.ts @@ -95,30 +95,6 @@ it.effect("reads a multi-megabyte SSE event delivered in small chunks", () => }), ); -it.effect("reads a 32 MiB SSE event delivered in 256-byte chunks", () => - Effect.gen(function* () { - const text = "x".repeat(32 * 1024 * 1024); - const bytes = new TextEncoder().encode( - `data: ${JSON.stringify({ id: 1, result: { text } })}\n\n`, - ); - let offset = 0; - const response = new Response( - new ReadableStream({ - pull(controller) { - if (offset === bytes.length) return controller.close(); - controller.enqueue(bytes.subarray(offset, offset + 256)); - offset = Math.min(offset + 256, bytes.length); - }, - }), - { headers: { "content-type": "text/event-stream" } }, - ); - const payloads = yield* Stream.runCollect( - responsePayloads(response, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })), - ); - expect(Array.from(payloads)).toEqual([{ id: 1, result: { text } }]); - }), -); - it.effect("picks the SSE reader from the media type alone, whatever its case", () => Effect.gen(function* () { const response = new Response('data: {"id":1}\n\n', {