Skip to content

fix(api): assemble SSE lines from bytes before decoding UTF-8 - #223

Merged
BunsDev merged 1 commit into
mainfrom
fix/sse-byte-boundaries
Oct 15, 2026
Merged

BunsDev merged 1 commit into
mainfrom
fix/sse-byte-boundaries

Conversation

@BunsDev

@BunsDev BunsDev commented Oct 15, 2026

Copy link
Copy Markdown
Member

Summary

Fix SSE parsing in both streaming paths (Anthropic raw client and Codex provider). Unicode split across network chunks is no longer replaced with U+FFFD, and a chunk boundary right after a field line no longer dispatches a frame early.

Context

  • Related request: Coven Code surface-reduction goal, unit 22 ("SSE byte boundaries and shared line assembly" in the surface inventory ledger).
  • Scope: crates/api/src/lib.rs (process_sse_stream), crates/api/src/providers/codex.rs (stream parsing), and new crate-private crates/api/src/sse_lines.rs.
  • Non-goals: full EventSource compliance (bare CR, BOM, field whitespace and empty data need separate disposition), the unimplemented public stream_parser placeholder types, and bounding pending line length (unbounded, as before).

Changes

  • Both loops ran from_utf8_lossy per chunk, then split('\n'). That caused two defects:
    • A codepoint split across chunks was irreversibly replaced.
    • A chunk ending right after a single newline produced a synthetic empty line, which flushed the event before the protocol's blank line. In a live headless HTTP/1.1 reproduction (old base), this dropped the response and ended with "Model repeatedly ended without completing the turn".
  • New private SseLines::feed scans with split_inclusive(b'\n'), keeps unfinished bytes, emits one decoded line per actual LF, and reuses its buffer. It is used by both loops.
  • The raw reader now takes a typed byte stream, and the Codex private parser separates response acquisition from mapping. Per-provider CR trimming, frame and event mappings, lossy decoding of malformed UTF-8, and discarding unfinished frames at EOF are all preserved. No public API or dependency change.
  • Port note: on current main, the raw path keeps fix(api): stop the SSE reader when its receiver is dropped or closed #215's outer tx.closed() select and main's inner per-chunk tx.closed() select. Only the line assembly changes.
  • Nine tests drive the actual raw and Codex parser loops plus the decoder. They cover every single split position and one-byte chunks, 2/3/4-byte characters, CRLF, text/tool-JSON/error payloads, raw callback/channel equality, multiline data gated on a real blank line, malformed-UTF-8 lossiness, and an unfinished line or frame at EOF.

Surface (production lines excluding test modules): API lib 1262 -> 1247 (-15), Codex 976 -> 968 (-8), new helper 22, net -1. Two duplicated chunk-to-line algorithms become one private boundary.

Validation

  • RED (old base): all four framing/Unicode tests failed in the actual raw and Codex loops; live headless probes showed replacement characters and the early dispatch.
  • cargo test --locked -p claurst-api: 74 + 5 passed, 0 failed (includes the 9 new tests and fix(api): stop the SSE reader when its receiver is dropped or closed #215's stream_lifecycle).
  • cargo fmt --all -- --check, cargo check --workspace --locked, cargo clippy --workspace --all-targets --locked -- -D warnings: pass on this branch (current main).
  • git diff --check: clean.
  • cargo test --workspace: hosted Rust CI.
  • Independent specification/quality review accepted the final patch. The spec reference is WHATWG HTML §9.2.5-9.2.6 (UTF-8 decoding, blank-line dispatch, discard of an incomplete event at EOF).

PR Readiness

  • Diff is limited to the intended files.
  • Generated files were not edited by hand.
  • User-facing docs: not needed.
  • No secrets, credentials, local paths, or unrelated logs are included.
  • Remaining risks: remote Codex OAuth transport and Windows live behavior were not exercised.

The Anthropic raw stream and the Codex provider each decoded every
network chunk with from_utf8_lossy before splitting lines, so a
codepoint split across chunks became U+FFFD, and a chunk ending right
after a field line's newline produced a synthetic empty line that
dispatched the frame early (in a live headless run the turn ended with
"Model repeatedly ended without completing the turn").

Replace both copies with one crate-private SseLines decoder that keeps
unfinished bytes and emits exactly one decoded line per real LF. Both
production loops take a byte stream so tests can drive them directly.
Per-provider CR trimming, event mappings, lossy handling of malformed
UTF-8 and discarding unfinished frames at EOF are unchanged.

Nine tests drive the real raw and Codex parser loops across every split
position, one-byte chunks, 2/3/4-byte characters and CRLF.

Coven-Harness: Claude Code / claude-opus-5-5
@vercel

vercel Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
docs Ready Ready Preview Oct 15, 2026 7:46am UTC

Request Review

@BunsDev
BunsDev merged commit 27a37dd into main Oct 15, 2026
6 checks passed
@BunsDev
BunsDev deleted the fix/sse-byte-boundaries branch October 18, 2026 07:50

This branch was successfully deployed

1 active deployment
Preview — 7768a49a Deployed Oct 15, 2026 by vercel[bot]
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