Skip to content

fix(desktop-backend): stop corrupting multi-byte characters in streamed chat replies - #9638

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/fix-chat-sse-utf8
Jul 13, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/fix-chat-sse-utf8

Conversation

@kodjima33

@kodjima33 kodjima33 commented Jul 13, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

Every streamed chat reply from the desktop backend can silently corrupt multi-byte characters — em dashes, curly quotes, ellipses, emoji, and all CJK/Cyrillic/accented text — replacing them with `` (U+FFFD).

Root cause

chat_completions.rs buffered the upstream Anthropic SSE stream as a String:

buffer.push_str(&String::from_utf8_lossy(&chunk));

chunk is a Bytes off bytes_stream() — an arbitrary network read boundary, not a char boundary. When a multi-byte character straddles two chunks, lossy-decoding each chunk before appending destroys it irrecoverably: the truncated prefix (E2 80) becomes U+FFFD, and the orphaned continuation byte (94) becomes another. The character can never be reassembled.

The corruption is silent because SSE/JSON syntax is all-ASCII: the event still parses, and the mangled text flows straight through TextDelta (reply text) and InputJsonDelta (tool-call arguments) to the client.

Reproduced against the old code path — data: {"text":"a—b"} split at byte 17 (inside the em dash) decodes to data: {"text":"a���b"}.

This is the only path that decodes rather than passes bytes through; the Gemini streams in proxy.rs use Body::from_stream and are unaffected.

Fix

Buffer raw bytes and only decode complete SSE event blocks. Event blocks always end on the ASCII \n\n, so a complete block is always valid UTF-8. Framing is extracted into drain_sse_events(), which is now unit-tested.

Verification

  • New test test_drain_sse_events_preserves_char_split_across_chunks feeds an em dash split mid-character across two chunks and asserts no U+FFFD — fails on the old code, passes on the new.
  • New test test_drain_sse_events_splits_on_blank_line covers framing and partial-event retention.
  • cargo test — 350 passed, 0 failed. cargo clippy -- -D warnings (CI's exact invocation) clean. cargo fmt --check clean.

Scope / risk

One file, +48/-6, no behavior change beyond the framing layer. Note that merging this deploys to dev only (desktop_backend_auto_dev.yml); prod promotion remains the deliberate manual desktop_promote_prod.yml step.

Follow-up spotted but deliberately left out of scope: chat_completions.rs:897 and :945 truncate an upstream error body with &body[..body.len().min(500)], which panics on a non-char-boundary (contained by CatchPanicLayer, so it degrades to a 500). tts.rs:85 and realtime.rs:209 already use the correct .chars().take(500) idiom.

🤖 automated by hourly watchdog

Review in cubic

…ed chat replies

The SSE translation loop decoded each network chunk with from_utf8_lossy
before appending it to the buffer. Chunk boundaries are arbitrary byte
offsets, not char boundaries, so a multi-byte character split across two
chunks was destroyed irrecoverably: the truncated prefix and the orphaned
continuation bytes each became U+FFFD. The surrounding JSON is ASCII, so
the event still parsed and the corruption flowed silently through to the
client's reply text and tool-call arguments.

Buffer raw bytes and only decode complete SSE event blocks, which always
end on the ASCII "\n\n" and are therefore valid UTF-8.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@kodjima33
kodjima33 merged commit 7f78738 into main Jul 13, 2026
22 of 25 checks passed
@kodjima33
kodjima33 deleted the watchdog/fix-chat-sse-utf8 branch July 13, 2026 17:00
Git-on-my-level added a commit that referenced this pull request Jul 15, 2026
## Summary
- Add a diff-scoped 1,500-line ratchet for Python, Swift, and Rust
product sources.
- Seed the committed snapshot with the 47 currently oversized files and
provide a downward-only updater for splits.
- Fold `RealtimeHubController.swift`'s overlapping size guard into the
shared ratchet; its extracted-type ownership guard remains.

## Root cause and durable guard
Oversized product files repeatedly concentrate collisions and costly
navigation (#9838; notably #9836 and #9384/#9638/#9692). The new shared
boundary freezes further growth, rejects smaller files that cross the
threshold, and makes any exceptional raise exact-count,
source-diff-bound, and one-line-justified.

## Verification
- `python3
.github/scripts/test_check_product_file_line_count_ratchet.py`
- `python3 desktop/macos/scripts/check-hub-controller-ratchet.py
--self-test`
- `make preflight` (rerun after applying the internal-only
`no-changelog-needed` label)


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/9851?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
@undivisible undivisible added the AI label Aug 10, 2026
undivisible pushed a commit to undivisible/omi that referenced this pull request Sep 29, 2026
…#9851)

## Summary
- Add a diff-scoped 1,500-line ratchet for Python, Swift, and Rust
product sources.
- Seed the committed snapshot with the 47 currently oversized files and
provide a downward-only updater for splits.
- Fold `RealtimeHubController.swift`'s overlapping size guard into the
shared ratchet; its extracted-type ownership guard remains.

## Root cause and durable guard
Oversized product files repeatedly concentrate collisions and costly
navigation (BasedHardware#9838; notably BasedHardware#9836 and BasedHardware#9384/BasedHardware#9638/BasedHardware#9692). The new shared
boundary freezes further growth, rejects smaller files that cross the
threshold, and makes any exceptional raise exact-count,
source-diff-bound, and one-line-justified.

## Verification
- `python3
.github/scripts/test_check_product_file_line_count_ratchet.py`
- `python3 desktop/macos/scripts/check-hub-controller-ratchet.py
--self-test`
- `make preflight` (rerun after applying the internal-only
`no-changelog-needed` label)


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/9851?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants