Repository navigation
Description
Prerequisites
- I have searched the existing issues and this is not a duplicate.
- This is a bug, not a usage question.
Version
Latest main (7be96ed).
Description
chunk_markdown_by_structure produces an extra zero-width chunk whenever the page markdown ends in a single trailing newline. That chunk is returned with has_more=True, so the extract tool reports next_start_char = len(content) to the model. Feeding that offset back in — which is exactly what the tool message tells the model to do — hits the guard in chunk_markdown_by_structure:
if start_from_char >= len(content):
return []
and service.py turns the empty result into:
start_from_char (30611) exceeds content length 30611 characters.
So a model that follows the pagination instruction gets an error instead of the end of the document. The final chunk it already received contained all remaining content (has_more=True on a chunk covering the rest of the page is also misleading in the stats line).
Steps to reproduce
Content that ends in one newline and is long enough to split:
content = '# Report\n\n' + 'Revenue grew across every region. ' * 900 + '\n'
chunk_markdown_by_structure(content, max_chunk_chars=25_000)
Actual, on main:
idx=0 [0,10) width=10 has_more=True
idx=1 [10,30611) width=30601 has_more=True
idx=2 [30611,30611) width=0 has_more=False <- zero-width
Paging as the tool does (start_from_char = chunk.char_offset_end) then gives:
start=0 -> chunk 1/3 has_more=True
start=10 -> chunk 2/3 has_more=True
start=30611 -> ERROR: start_from_char exceeds content length
Root cause
_parse_atomic_blocks does lines = content.split('\n'). For content ending in a newline that yields a final empty element which has no character behind it in the source, but the loop still charges it len(line) + 1. That pushes the last block past the end of the content, and the existing clamp at the end of the function pulls char_end back to len(content) — leaving a block whose char_start == char_end. In Phase 2 that block has block_size == 0, so it never overflows the limit and ends up alone in its own chunk.
Expected behaviour
The trailing newline should not produce a chunk. Paging should terminate on the last real chunk.
Notes
- Two trailing newlines do not trigger it: the final BLANK block then has width 1 and absorbs the phantom element. This is why it looks intermittent.
- fix(dom): correct chunk offset overshoot when markdown ends with a newline #5028 previously fixed half of this (the clamp condition) and was closed as stale without review. This is the remaining half, addressed at the source rather than by loosening the clamp.
AI assistance
Drafted with AI assistance. The reproduction, the chunk-by-chunk trace and the root cause were all verified by running the code on main; the five regression cases fail on main and pass with the fix.
Activity