Skip to content

Fix UTF-8 boundary panic in cortex_chat and discord reply truncation#276

Merged
jamiepine merged 4 commits intospacedriveapp:mainfrom
l33t0:fix/cortex-utf8-boundary-panic
Mar 3, 2026
Merged

Fix UTF-8 boundary panic in cortex_chat and discord reply truncation#276
jamiepine merged 4 commits intospacedriveapp:mainfrom
l33t0:fix/cortex-utf8-boundary-panic

Conversation

@l33t0
Copy link
Contributor

@l33t0 l33t0 commented Mar 1, 2026

Fixes #273

cortex_chat.rs:92 panics when truncating tool results containing multi-byte characters (CJK, emoji, etc.) because &result[..200] can slice inside a multi-byte UTF-8 sequence. The same pattern existed in discord.rs for reply content truncation.

Changes

  • src/agent/cortex_chat.rs&result[..200]&result[..result.floor_char_boundary(200)]
  • src/messaging/discord.rs&reply_content[..200]&reply_content[..reply_content.floor_char_boundary(200)]

floor_char_boundary rounds down to the nearest char boundary, which is the same approach used throughout the rest of the codebase (worker.rs, telegram.rs, channel.rs, etc.).

Testing

Audited all remaining [..N] slices in src/ — the others operate on ASCII-only data (UUIDs, hex digests, API tokens) and are safe.

…anics

Fixes spacedriveapp#273 — cortex_chat.rs panics on CJK memory content because
`&result[..200]` can land inside a multi-byte character. Same issue
existed in discord.rs reply content truncation.

Replace bare byte-index slices with `floor_char_boundary(200)` which
rounds down to the nearest char boundary.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 1, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 767799e and a611056.

📒 Files selected for processing (1)
  • src/agent/cortex_chat.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/agent/cortex_chat.rs

Walkthrough

Two files update truncation to be UTF-8 safe: previews now compute a character-boundary end via floor_char_boundary(200) before slicing, preventing panics when cutting multi-byte UTF-8 text (e.g., CJK) while preserving a 200‑character preview length.

Changes

Cohort / File(s) Summary
UTF-8 Boundary Safety in String Truncation
src/agent/cortex_chat.rs, src/messaging/discord.rs
Replaced direct byte-index slicing with a char-boundary-safe slice using floor_char_boundary(200) when creating 200-character previews/truncated quoted content to avoid slicing inside multi-byte UTF-8 characters.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main fixes: UTF-8 boundary panic resolution in cortex_chat and discord reply truncation, matching the changeset precisely.
Description check ✅ Passed The description is directly related to the changeset, explaining the UTF-8 boundary issue and the floor_char_boundary solution applied to both files.
Linked Issues check ✅ Passed The PR fully implements the fix requested in issue #273: replaces unsafe byte-index slicing with floor_char_boundary in cortex_chat.rs:92 and extends the same fix to discord.rs, directly addressing the panic.
Out of Scope Changes check ✅ Passed All changes are directly in scope: UTF-8 boundary fixes in cortex_chat.rs and discord.rs to prevent the panic described in issue #273, with no extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jamiepine jamiepine enabled auto-merge March 3, 2026 22:54
@jamiepine jamiepine merged commit 7b5bb90 into spacedriveapp:main Mar 3, 2026
4 checks passed
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.

Runtime panic in cortex_chat.rs:92: byte index not on char boundary when processing CJK characters in memory

2 participants