chunker: the last chunk ends at the file's last line, not one past it - #44
Merged
Conversation
chunkFile splits content on "\n". A file that ends with a newline, which is nearly every file, leaves a trailing empty string, so lines.length was the real line count plus one. Both span builders (fixedWindows and packSegments) end their last span at lines.length, so the last chunk's endLine, and the end_line stored in the index, ran one line past the file. On the bench repo MAX(end_line) was `wc -l` plus one for every file (Cargo.toml 443 vs 442, src/lib.rs 220 vs 219, README.md 554 vs 553). A hit's path:start-end citation for the last chunk pointed past the file, and any answer that read a file's length off MAX(end_line) was off by one. Drop exactly one trailing empty element before computing spans. Blank lines ahead of the final newline are still lines; a CRLF file leaves the same empty element, so it is covered without changing the split. How tree-sitter definition rows map to lines (d.row + 1) and the empty-span guard are unchanged, and spans still tile the file. The regression test covers fixed windows, tree-sitter, and markdown, with and without the trailing newline; it fails before this change. Existing indexes keep the old end_line values until they are reindexed.
ashishmishra26
approved these changes
Sep 5, 2026
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.
chunkFile splits content on "\n". A file that ends with a newline, which is nearly every file, leaves a trailing empty string, so lines.length was the real line count plus one. Both span builders (fixedWindows and packSegments) end their last span at lines.length, so the last chunk's endLine, and the end_line stored in the index, ran one line past the file. On the bench repo MAX(end_line) was
wc -lplus one for every file (Cargo.toml 443 vs 442, src/lib.rs 220 vs 219, README.md 554 vs 553). A hit's path:start-end citation for the last chunk pointed past the file, and any answer that read a file's length off MAX(end_line) was off by one.Drop exactly one trailing empty element before computing spans. Blank lines ahead of the final newline are still lines; a CRLF file leaves the same empty element, so it is covered without changing the split. How tree-sitter definition rows map to lines (d.row + 1) and the empty-span guard are unchanged, and spans still tile the file.
The regression test covers fixed windows, tree-sitter, and markdown, with and without the trailing newline; it fails before this change.
Existing indexes keep the old end_line values until they are reindexed.