Fix panic on non-ASCII input by using byte offsets instead of char in…#1
Merged
grainier merged 1 commit intoagents-sh:mainfrom Mar 28, 2026
Merged
Conversation
…dices in heuristic JSON boundary detection
grainier
approved these changes
Mar 28, 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.
Problem
Any LLM response containing multibyte UTF-8 characters (Thai, Chinese,
Japanese, Arabic, emoji, accented Latin) caused a hard panic:
byte index 77 is not a char boundary; it is inside 'ก' (bytes 75..78)
find_balanced_boundariescollected chars withchars()and stored theloop index
i— a character count — directly into theboundariesvecas a byte offset. For ASCII the two are identical. For any multibyte
codepoint they diverge, so the subsequent
&input[start..end]slicelanded inside a codepoint and panicked. The same bug existed in both
HeuristicStrategy(heuristic.rs) andHeuristicExtractor(extractor.rs).Fix
Replace
chars()withchar_indices()throughout both implementations.char_indices()yields(byte_offset, char)pairs, sobyte_startisread directly from the tuple and
byte_endis derived from the startoffset of the next character (or
input.len()when at the end of thestring).
find_matching_closeis updated to accept&[(usize, char)]and destructure accordingly; its return value remains a vec index (not a
byte offset), which is correct and unchanged.
What is not changed
char_indices()has the same cost aschars()Tests added
8 new regression tests across both files covering Thai (3-byte), CJK
(3-byte), emoji (4-byte), accented Latin (2-byte), and multibyte
characters appearing in prose before the JSON object (the case where
char index and byte offset diverge for the opening
{itself).