Skip to content

fix(parser): use byte offsets instead of char indices in extract_json_object - #2751

Merged
KuSh merged 2 commits into
rtk-ai:developfrom
guyoron1:fix/cjk-emoji-char-boundary-panic
Jul 1, 2026
Merged

KuSh merged 2 commits into
rtk-ai:developfrom
guyoron1:fix/cjk-emoji-char-boundary-panic

Conversation

@guyoron1

Copy link
Copy Markdown
Contributor

Summary

  • extract_json_object() used char indices from enumerate() as byte offsets for string slicing
  • Multi-byte UTF-8 characters (CJK, emoji) caused 'not a char boundary' panic
  • Switched from Vec<char> + enumerate() to char_indices() which yields correct byte offsets

Fixes #2509

Test plan

  • Added regression tests for CJK values, emoji values, CJK prefix, and mixed multi-byte nested JSON
  • All 15 parser tests pass
  • cargo fmt --all && cargo clippy --all-targets && cargo test --all passes (2 pre-existing failures in rewrite_cmd unrelated to this change)

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for your PR. It contains an unrelated change that should be withdrawn; otherwise, LGTM

Comment thread scripts/benchmark.sh

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated to the PR’s subject

@KuSh KuSh self-assigned this Jun 30, 2026
@guyoron1
guyoron1 force-pushed the fix/cjk-emoji-char-boundary-panic branch from ea9507a to b8a5919 Compare July 1, 2026 03:51
…_object

extract_json_object() used char indices from enumerate() as byte offsets
when slicing the input string. For multi-byte UTF-8 characters (CJK,
emoji), this causes a 'not a char boundary' panic because char index !=
byte offset.

Switch from Vec<char> + enumerate() to char_indices() which yields
correct byte offsets. Add regression tests for CJK values, emoji values,
CJK prefixes, and mixed multi-byte nested JSON.

Fixes rtk-ai#2509
@guyoron1
guyoron1 force-pushed the fix/cjk-emoji-char-boundary-panic branch from b8a5919 to b1b7de5 Compare July 1, 2026 06:03
@guyoron1

guyoron1 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Heyaaa : )

This benchmark test hits httpbin.org/robots.txt live and compares raw vs filtered token counts.
The result is non-deterministic as in the earlier CI run on this same PR (before the UV fix commits), the same test showed 8 → 8 (+0%).

I think that the difference is may be due to varying response headers/content from httpbin.org between runs?
what do you think?

The changes in this PR only touch registry.rs (rewrite logic) and main.rs , so no curl filter code was modified.

@KuSh

KuSh commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

httpbin.org seems a bit erratic; it can take 20+ seconds to respond or even return a 503. That said, this PR should stay focused on a single subject.

Also, you’ve added two new commits (191c8f8 and 566f856) that don’t seem related to the PR’s topic. Please revert them, if this is a real issue that needs fixing, could you please open a separate PR with this two commits, a clear description of the problem and the fix?

@guyoron1
guyoron1 force-pushed the fix/cjk-emoji-char-boundary-panic branch from 566f856 to 27f9739 Compare July 1, 2026 08:12
@guyoron1

guyoron1 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Oh thanks for the heads up and review !
I've removed the unrelated UV commits and opened #2768 for those separately.
This PR now contains only the parser fix (single commit 27f9739).

The clippy CI failure is a pre-existing issue on develop — uv_cmd.rs:61 calls print_with_hint with 4 args but the signature expects 5 (missing guard_raw).
PR #2768 includes the fix for that.

@guyoron1

Copy link
Copy Markdown
Contributor Author

This also supersedes my earlier #2749 which tackled the same char_indices fix with a different set of tests. Closing #2749 as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic 'not a char boundary' in output filter (src/parser/mod.rs:191) on large CJK output — affects all wrapped commands (same root as #1837)

2 participants