fix(guard): use UTF-8 safe string truncation in output preview logging#3713
Merged
fix(guard): use UTF-8 safe string truncation in output preview logging#3713
Conversation
The label_response function panics when serialized JSON contains multi-byte UTF-8 characters (CJK, emoji, etc.) and byte index 500 falls in the middle of a code point. The panic causes a WASM trap that permanently poisons the guard instance — all subsequent MCP calls to that server fail for the rest of the session. Extract a safe_preview(s, max_bytes) helper that uses str::floor_char_boundary() (stable since Rust 1.80) to find the nearest valid character boundary at or before the limit. Replace all three preview truncation sites in label_response: - Line ~808: path-specific output preview (was panicking) - Line ~939: general output preview (was panicking) - Line ~752: input preview (was silently dropping the log) Add 8 unit tests covering ASCII, CJK (3-byte), emoji (4-byte), accented (2-byte), mixed content, empty strings, and boundary conditions. Fixes #3711 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a session-killing panic in the GitHub WASM guard caused by truncating &str log previews at a raw byte offset that may split multi-byte UTF-8 characters. It introduces a UTF-8-safe preview helper and updates label_response logging to use it, plus adds focused unit tests to prevent regressions.
Changes:
- Added
safe_preview(s, max_bytes)and a sharedPREVIEW_MAX_BYTESconstant for UTF-8-safe log truncation. - Replaced unsafe string slicing in
label_responseoutput previews withsafe_preview. - Added unit tests covering ASCII and multi-byte UTF-8 boundary cases (CJK, emoji, accented characters, mixed content, empty string).
Show a summary per file
| File | Description |
|---|---|
guards/github-guard/rust-guard/src/lib.rs |
Adds UTF-8-safe truncation helper, updates preview logging call sites to avoid panics, and introduces unit tests for boundary conditions. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 3
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This was referenced Apr 13, 2026
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.
Summary
Fixes #3711 — the WASM guard panics on multi-byte UTF-8 characters in tool response previews, poisoning the entire session.
Root cause
label_responseinlib.rstruncates serialized JSON for debug logging using byte-index slicing (&output_json[..500]). When byte 500 falls in the middle of a multi-byte UTF-8 code point (CJK = 3 bytes, emoji = 4 bytes), Rust panics with "byte index is not a char boundary". Since this runs inside the WASM guest, the panic becomes a trap that permanently poisons the guard instance.Changes
New helper —
safe_preview(s, max_bytes) -> &str:str::floor_char_boundary()(stable since Rust 1.80) to find the nearest valid character boundary at or before the byte limitThree call sites fixed in
label_response:&output_json[..500]— panicssafe_preview(&output_json, 500)&output_json[..500]— panicssafe_preview(&output_json, 500)from_utf8(&bytes[..500])— silent dropsafe_preview(full_str, 500)— always logs8 unit tests covering:
Evidence
Discovered in
moeru-ai/airiPR triage workflow (run #24311673575) — a PR with a Chinese body caused the guard to crash, and all subsequent MCP calls failed with "WASM guard is unavailable after a previous trap".Verification
make agent-finishedpasses — all unit + integration tests green.