From f223009b25b40c7afd1cf0930395ae8b49b8247e Mon Sep 17 00:00:00 2001 From: Stefan Obradovic Date: Sun, 1 Mar 2026 23:50:48 +1000 Subject: [PATCH 1/2] fix: use floor_char_boundary for string truncation to prevent UTF-8 panics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #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. --- src/agent/cortex_chat.rs | 2 +- src/messaging/discord.rs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/agent/cortex_chat.rs b/src/agent/cortex_chat.rs index ed5ec0033..ad05db162 100644 --- a/src/agent/cortex_chat.rs +++ b/src/agent/cortex_chat.rs @@ -89,7 +89,7 @@ impl PromptHook for CortexChatHook { result: &str, ) -> HookAction { let preview = if result.len() > 200 { - format!("{}...", &result[..200]) + format!("{}...", &result[..result.floor_char_boundary(200)]) } else { result.to_string() }; diff --git a/src/messaging/discord.rs b/src/messaging/discord.rs index 7f2363450..ef692e301 100644 --- a/src/messaging/discord.rs +++ b/src/messaging/discord.rs @@ -930,7 +930,10 @@ async fn build_metadata( let reply_content = resolve_mentions(&referenced.content, &referenced.mentions); // Truncate to avoid bloating context with long quoted messages let truncated = if reply_content.len() > 200 { - format!("{}...", &reply_content[..200]) + format!( + "{}...", + &reply_content[..reply_content.floor_char_boundary(200)] + ) } else { reply_content }; From 767799e3ebc0c4c8cf419a93c4d6235b5ef87486 Mon Sep 17 00:00:00 2001 From: Stefan Obradovic Date: Sun, 1 Mar 2026 23:54:51 +1000 Subject: [PATCH 2/2] refactor: extract floor_char_boundary to local for readability --- src/agent/cortex_chat.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/agent/cortex_chat.rs b/src/agent/cortex_chat.rs index ad05db162..ac2ae06e1 100644 --- a/src/agent/cortex_chat.rs +++ b/src/agent/cortex_chat.rs @@ -89,7 +89,8 @@ impl PromptHook for CortexChatHook { result: &str, ) -> HookAction { let preview = if result.len() > 200 { - format!("{}...", &result[..result.floor_char_boundary(200)]) + let end = result.floor_char_boundary(200); + format!("{}...", &result[..end]) } else { result.to_string() };