Skip to content

fix: decode streamed text across chunk boundaries - #493

Merged
mrnasil merged 4 commits into
mainfrom
fix/stream-utf8-split
Nov 26, 2026
Merged

mrnasil merged 4 commits into
mainfrom
fix/stream-utf8-split

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

Three readers decoded each HTTP chunk on its own with from_utf8_lossy:

  • the SDK event stream;
  • the MCP HTTP/SSE reader;
  • the TUI remote chat.

A chunk boundary inside a multi-byte character (ç, 日, an emoji) turned that character into two U+FFFDs, garbling assistant text and tool results.

The fix:

  • New whycodes_core::Utf8Stream. It holds back an incomplete trailing character until the next chunk completes it, and still turns invalid bytes into U+FFFD immediately.
  • All three readers now use it.
  • The SDK gets it through a whycodes-protocol re-export, so it still depends only on whycodes-protocol.
  • The SDK also folds a \r\n that is split across two chunks.

Tests:

  • byte-by-byte decoding of aç日😀;
  • an invalid byte and a truncated trailing character;
  • an SDK frame whose ç and \r\n both straddle chunks;
  • MCP SseParser::push_bytes fed one byte at a time.

utf8_stream.rs and sdk/src/client.rs have 100% line coverage. The lines still missed in the mcp crate are also missed on main.

🤖 Generated with Claude Code

The SDK event stream, the MCP HTTP/SSE reader and the TUI remote chat
decoded every HTTP chunk on its own with from_utf8_lossy. A chunk cut
inside a multi-byte character (any non-ASCII text) turned it into two
replacement characters, garbling assistant text and tool results.

Add whycodes_core::Utf8Stream, which holds back an incomplete trailing
character until the next chunk completes it, and use it in all three
readers. The SDK reaches it through whycodes-protocol so it keeps its
single dependency, and now folds a `\r\n` split across chunks too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil enabled auto-merge (squash) November 25, 2026 15:17
@mrnasil
mrnasil merged commit 4d5858a into main Nov 26, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/stream-utf8-split branch November 26, 2026 00:42
@mrnasil mrnasil mentioned this pull request Nov 26, 2026
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.

1 participant