Skip to content

Fix panic in project-memory loader on multibyte truncation (#46)#56

Draft
TYRMars wants to merge 1 commit into
mainfrom
claude/vibrant-dijkstra-a9cV2
Draft

Fix panic in project-memory loader on multibyte truncation (#46)#56
TYRMars wants to merge 1 commit into
mainfrom
claude/vibrant-dijkstra-a9cV2

Conversation

@TYRMars
Copy link
Copy Markdown
Owner

@TYRMars TYRMars commented May 30, 2026

Summary

Fixes #46 — a reproducible panic on the main chat path.

read_limited() and the memory-management view used String::truncate(max_bytes), which panics when max_bytes does not lie on a UTF-8 char boundary. Project-memory files are markdown that routinely contains CJK (the repo itself stores zh-CN project memory), so an oversized memory file with a multi-byte char straddling the cap crashed:

  • read_limited() — the hot path called by load_project_memory_prompt on every project-bound chat turn.
  • the management-view truncation at project_memory.rs:158.

Fix

Added truncate_on_char_boundary(&mut String, max_bytes), which rounds the cut point down to the nearest UTF-8 char boundary (walking back from max_bytes while the index is not a boundary) before truncating. Both call sites now use it instead of String::truncate.

Tests

Added three unit tests:

  • truncate_on_char_boundary_does_not_panic_on_multibyte — caps a CJK string mid-char.
  • truncate_on_char_boundary_is_noop_when_within_cap.
  • read_limited_truncates_cjk_without_panicking — end-to-end through the hot path with a CJK file just over the cap.

cargo test -p harness-server project_memory:: and cargo clippy -p harness-server --all-targets -- -D warnings both pass.

https://claude.ai/code/session_01QMsGz6LRWe9AvY17ZW4C9B


Generated by Claude Code

read_limited() and the memory-management view used String::truncate(max_bytes),
which panics when max_bytes does not lie on a UTF-8 char boundary. CJK-heavy
project-memory files (the repo stores zh-CN memory) routinely have a multi-byte
char straddling the cap, crashing every project-bound chat turn.

Add truncate_on_char_boundary() which rounds the cut point down to the nearest
char boundary, and use it in both read_limited() and the management view.

Fixes #46
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.

Project-memory loader panics (String::truncate) on multibyte files — crashes every project-bound chat turn

2 participants