Fix overlapped chunks exceeding chunk_size when separators count as tokens#25
Open
lntutor wants to merge 1 commit into
Open
Fix overlapped chunks exceeding chunk_size when separators count as tokens#25lntutor wants to merge 1 commit into
lntutor wants to merge 1 commit into
Conversation
…okens When overlap is enabled, output chunks are built by spanning several subchunks by offset, which re-includes the whitespace separators between them (stripped when the subchunks were formed). With a whitespace- significant token counter (e.g. a character counter), those separators count as tokens and can push a merged chunk past chunk_size, breaking the documented maximum -- e.g. text='aa bb cc dd', chunk_size=4, overlap=0.5 produced ['aa bb', ...] at 6 tokens each. Replace the fixed-width offset window with a token-count-aware greedy window that shrinks to stay within chunk_size while still guaranteeing full content coverage (stride never skips past the last included subchunk). Identical to the previous behavior whenever no shrink is needed. Applies to both the AI and non-AI overlap paths. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N6RtoHuxrDqTUo9Mw9h4Cv
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.
What's broken
When
overlapis enabled, output chunks are built by spanning several subchunks by offset:That span re-includes the whitespace separators between subchunks (which were stripped when the subchunks were formed). With a whitespace-significant token counter (e.g. a character counter), those separators count as tokens and push the merged chunk past
chunk_size, breaking the documented maximum:The code's own NOTE comments use
floor(notceil) specifically so the chunk size is not exceeded; this overflow defeats that intent. Content coverage and offset reconstruction are never broken — only the size bound.Fix
Replace the fixed-width offset window with a token-count-aware greedy window that grows up to
subchunks_per_chunksubchunks, then shrinks to stay withinchunk_size; the stride never advances past the last included subchunk, so no subchunk is skipped (full content coverage preserved). It is identical to the previous behavior whenever no shrink is needed (the word/subword counters, where whitespace is 0 tokens, are unaffected). Applies to both the AI and non-AI overlap paths (identical code).Tests
Adds
test_overlap_never_exceeds_chunk_sizetotests/test_semchunk.py. Verified via a 20k+-case fuzz over the char counter: 0 size violations, 0 reconstruction mismatches; non-overlap and word-counter overlap paths unchanged.