Repository navigation
fix(desktop-backend): stop corrupting multi-byte characters in streamed chat replies - #9638
Merged
Merged
Conversation
…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]>
This was referenced Jul 15, 2026
Closed
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
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. -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.rsbuffered the upstream Anthropic SSE stream as aString:chunkis aBytesoffbytes_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) andInputJsonDelta(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 todata: {"text":"a���b"}.This is the only path that decodes rather than passes bytes through; the Gemini streams in
proxy.rsuseBody::from_streamand 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 intodrain_sse_events(), which is now unit-tested.Verification
test_drain_sse_events_preserves_char_split_across_chunksfeeds an em dash split mid-character across two chunks and asserts no U+FFFD — fails on the old code, passes on the new.test_drain_sse_events_splits_on_blank_linecovers framing and partial-event retention.cargo test— 350 passed, 0 failed.cargo clippy -- -D warnings(CI's exact invocation) clean.cargo fmt --checkclean.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 manualdesktop_promote_prod.ymlstep.Follow-up spotted but deliberately left out of scope:
chat_completions.rs:897and:945truncate an upstream error body with&body[..body.len().min(500)], which panics on a non-char-boundary (contained byCatchPanicLayer, so it degrades to a 500).tts.rs:85andrealtime.rs:209already use the correct.chars().take(500)idiom.🤖 automated by hourly watchdog