fix(memory): prevent infinite loop in chunk_text when overlap rewinds start#3027
Merged
fix(memory): prevent infinite loop in chunk_text when overlap rewinds start#3027
Conversation
… start
When rfind("\n\n") found a match near the beginning of the sliding window,
`end` advanced by fewer bytes than CHUNK_OVERLAP_CHARS. The subsequent
`end.saturating_sub(CHUNK_OVERLAP_CHARS)` produced a value less than the
current `start`, and `ceil_char_boundary` returned a position behind it.
The old safeguard (`if start >= end`) did not fire because new_start <
start < end, so `start` regressed and the loop ran forever.
Fix: guarantee forward progress by taking `end` when `new_start <= start`.
This was the root cause of 100% CPU / GB RAM consumption at startup: the
embed backfill spawned background tasks for unembedded messages (including
an 85 KB tool_result), all of which entered the infinite loop simultaneously,
saturating all tokio worker threads and blocking log flushing.
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.
Summary
chunk_textinzeph-memoryhad an infinite loop triggered by messages whererfind("\n\n")found a match very early in the 1600-byte sliding windowendadvanced by fewer bytes thanCHUNK_OVERLAP_CHARS; the overlap subtraction produced anew_startbehind the currentstartif start >= end) did not fire becausenew_start < start < end, sostartregressed and the loop ran foreverstart = if new_start > start { new_start } else { end }— always guarantees forward progressRoot Cause Evidence
macOS
sampleprofiler on the live zeph process (CPU: 1126%, RAM: ~1 GB) showed all 12 tokio worker threads stuck in:SQLite confirmed 12 unembedded messages, including an 85 KB
tool_result(line-numbered Rust source). The embed backfill spawned background tasks for all of them at startup; all entered the infinite loop simultaneously, saturating the thread pool and blocking log flushing — explaining both the CPU/RAM spike and the frozen TUI status ("Connecting tools...").Test plan
cargo nextest run -p zeph-memory --lib— 1114 tests passcargo +nightly fmt --check— cleancargo clippy -p zeph-memory -- -D warnings— clean