Skip to content

fix: emit complete SSE chunks immediately - #302

Open
OllieinCanada wants to merge 1 commit into
cohere-ai:mainfrom
OllieinCanada:agent/emit-terminal-sse-chunks
Open

fix: emit complete SSE chunks immediately#302
OllieinCanada wants to merge 1 commit into
cohere-ai:mainfrom
OllieinCanada:agent/emit-terminal-sse-chunks

Conversation

@OllieinCanada

@OllieinCanada OllieinCanada commented Aug 15, 2026

Copy link
Copy Markdown

Fixes #301

Summary

Emit complete SSE events immediately when their blank-line delimiter ends at a network chunk boundary.

Root cause

The delimiter scanner skipped the final two-byte candidate, so \n\n and \r\r at the end of a chunk were not recognized until more data arrived. The line decoder also retained a terminal carriage return even after the SSE chunker had identified a complete event block.

Fix

  • scan the final possible two-byte delimiter position
  • flush the line decoder after each complete SSE event block
  • preserve existing support for LF, CR, and CRLF delimiters

Tests

A deterministic in-memory async stream verifies that events terminated by \n\n, \r\r, and \r\n\r\n are emitted before the reader asks for another network chunk.

Validation:

  • focused regression tests: 3 passed
  • offline unit suite: 552 passed
  • TypeScript typecheck passed
  • Biome check passed for the regression test
  • git diff --check passed

No external API or credentials were used.


Note

Low Risk
Internal streaming parser fix with focused regression tests; no API or auth changes.

Overview
Fixes delayed SSE delivery when a complete event’s blank-line delimiter (\n\n, \r\r, or \r\n\r\n) lands at the end of a network chunk.

findDoubleNewlineIndex now scans through the last possible two-byte delimiter position (buffer.length - 1 instead of buffer.length - 2), so trailing \n\n / \r\r at a chunk edge are recognized immediately.

_iterSSEMessages flushes the LineDecoder after each complete SSE block from iterSSEChunks, so a terminal carriage return is not held until the next read.

Adds regression tests in streaming-utils.test.ts that assert an event is yielded before the stream reader is asked for a second chunk for all three delimiter styles.

Reviewed by Cursor Bugbot for commit 3575fa6. Bugbot is set up for automated code reviews on this repo. Configure here.

Signed-off-by: Oliver Slapinski <olliefromcanada@gmail.com>
@OllieinCanada
OllieinCanada marked this pull request as ready for review August 15, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSE event waits for another chunk at delimiter boundary

1 participant