mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2472] [Issue]: Streaming responses corrupt multi-byte UTF-8 that straddles a network chunk boundary (mojibake) #1216
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1216
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Romuromylus on GitHub (Jun 6, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2472
Originally assigned to: @SpicyMarinara on GitHub.
Summary
Severity: Medium — correctness regression. Every streaming provider path decodes each network chunk independently with
String::from_utf8_lossy, so a multi-byte UTF-8 codepoint (emoji, CJK, accented letter, em dash, smart quote) whose bytes straddle a chunk boundary is corrupted into U+FFFD replacement characters and streamed to the chat as mojibake.Expected behavior
Decode the byte stream statefully across chunks (carry trailing incomplete bytes to the next chunk), like main's
TextDecoder(...).decode(value, { stream: true }), so split codepoints survive.Actual behavior
All five SSE streaming loops push each chunk through
from_utf8_lossybefore SSE block assembly:A codepoint split across two chunks becomes invalid bytes on both sides → U+FFFD, before
take_sse_blockever assembles the event, so SSE buffering cannot rescue it. The corrupted token is then emitted viaemit(json!({"type":"token", ...})). The non-streaming path is correctly immune because it buffers the full body first, then callsfrom_utf8_lossyonce on the complete byte vector. There is noencoding_rs/incremental decoder /valid_up_tocarry-over anywhere in the streaming path.Steps to reproduce
�/garbled characters where a glyph landed on a chunk boundary; the more non-ASCII content and chunks, the more frequent.Impact area
Environment
1fae2c42Logs, screenshots, or video
Static trace:
crates/llm/src/lib.rs:1998, 2122, 2551, 4286, 4999(per-chunkfrom_utf8_lossyin the streaming loops); contrast the full-body decode inread_limited_provider_text.Regression notes
Regression vs main, which decodes statefully:
anthropic.provider.ts/google.provider.ts/openai.provider.tsusenew TextDecoder().decode(value, { stream: true }), which holds back trailing incomplete bytes across chunks. The refactor's per-chunk lossy decode reintroduced the corruption. (The recent provider commits —7e393a78,70e76291,80931cfc,22fe2d73— only touched the SSE block parsers and request shaping, not the byte-decode layer.)Additional context
Fix: replace the per-chunk
String::from_utf8_lossywith an incremental decoder that carries trailing 1-3 incomplete bytes into the next iteration — e.g.encoding_rs::UTF_8.new_decoder()withlast = false, or a manualstr::from_utf8(...).valid_up_to()split.Template check
Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:
@SpicyMarinara commented on GitHub (Jun 6, 2026):
Fixed on refactor in
038b7220.Provider streaming now carries partial UTF-8 bytes across network chunks before decoding, preventing mojibake when multi-byte characters straddle chunk boundaries.
Verification: pnpm typecheck; pnpm build; pnpm check:architecture; cargo check --manifest-path src-tauri/Cargo.toml --workspace; focused Rust backup/profile tests; git diff --check; no .test.ts/.test.tsx files present.