diff --git a/apps/sim/executor/handlers/mothership/handler.ts b/apps/sim/executor/handlers/mothership/handler.ts index 6c2cd963afc..c746f4e35f8 100644 --- a/apps/sim/executor/handlers/mothership/handler.ts +++ b/apps/sim/executor/handlers/mothership/handler.ts @@ -571,8 +571,9 @@ async function readMothershipExecuteResponse( const { done, value } = await reader.read() if (done) break - buffer += decoder.decode(value, { stream: true }) - const lines = buffer.split('\n') + // `buffer` never holds a newline, so only the new text needs splitting. + const lines = decoder.decode(value, { stream: true }).split('\n') + lines[0] = buffer + lines[0] buffer = lines.pop() ?? '' for (const line of lines) { await processLine(line) @@ -700,8 +701,9 @@ function createMothershipStreamingExecution( if (cancelled) return if (done) break - buffer += decoder.decode(value, { stream: true }) - const lines = buffer.split('\n') + // `buffer` never holds a newline, so only the new text needs splitting. + const lines = decoder.decode(value, { stream: true }).split('\n') + lines[0] = buffer + lines[0] buffer = lines.pop() ?? '' for (const line of lines) { await processLine(line) diff --git a/apps/sim/lib/core/utils/sse.ts b/apps/sim/lib/core/utils/sse.ts index b68af4bf783..871ad21b56c 100644 --- a/apps/sim/lib/core/utils/sse.ts +++ b/apps/sim/lib/core/utils/sse.ts @@ -166,8 +166,10 @@ export async function readSSELines(source: SSESource, options: ReadSSELinesOptio const { done, value } = await readChunk() - buffer += done ? decoder.decode() : decoder.decode(value, { stream: true }) - const lines = buffer.split('\n') + // `buffer` never holds a newline, so split only the new text: re-splitting + // the whole pending line on every chunk is quadratic in the event size. + const lines = (done ? decoder.decode() : decoder.decode(value, { stream: true })).split('\n') + lines[0] = buffer + lines[0] buffer = done ? '' : (lines.pop() ?? '') for (const rawLine of lines) { diff --git a/packages/sim-cli/src/commands/protocol/workflow-run-follow.ts b/packages/sim-cli/src/commands/protocol/workflow-run-follow.ts index ed43a1fb47f..c2eaac496d2 100644 --- a/packages/sim-cli/src/commands/protocol/workflow-run-follow.ts +++ b/packages/sim-cli/src/commands/protocol/workflow-run-follow.ts @@ -272,9 +272,9 @@ async function* sseData(body: ReadableStream): AsyncGenerator