Repository navigation
fix(dom): correct chunk offset overshoot when markdown ends with a newline - #5028
Osamaali313 wants to merge 1 commit into
Conversation
…wline
_parse_atomic_blocks adds +1 per line for the newline removed by
str.split('\n'). When content ends with a trailing newline, split()
produces a spurious empty final element, so the last block's char_end
(and the last chunk's char_offset_end) became len(content) + 1 - one
character past the end of the content.
The previous fix-up only corrected the no-trailing-newline case. Clamp
the final block's char_end to len(content) for both cases. This keeps all
offsets within bounds and fixes start_from_char resume: passing
start_from_char == len(content) now correctly returns no chunks instead
of re-returning the final chunk.
Adds a regression test for trailing-newline content.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR tightens markdown chunk offset correctness by ensuring the final parsed block ends exactly at len(content), and adds a regression test for trailing-newline content.
Changes:
- Add a test covering offset behavior when markdown content ends with a trailing newline.
- Update atomic block parsing to normalize the last block’s
char_endtolen(content)when newline accounting causes a mismatch.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/ci/test_markdown_chunking.py | Adds regression coverage to ensure chunk offsets don’t overshoot with trailing newline input. |
| browser_use/dom/markdown_extractor.py | Adjusts last-block char_end normalization logic to prevent newline accounting from producing out-of-range offsets. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Fix last block char_end. Adding +1 per split line for the newline overshoots | ||
| # the true length by one in two cases: content has no trailing newline (the | ||
| # final line's +1 has no newline to account for), or content ends with a | ||
| # newline (str.split('\n') yields a spurious empty final element). Either way | ||
| # the last block must end exactly at len(content). | ||
| if blocks and content and blocks[-1].char_end != len(content): |
| # Last chunk ends at content length | ||
| assert chunks[-1].char_offset_end == len(content) | ||
|
|
||
| def test_offsets_dont_overshoot_with_trailing_newline(self): |
|
👋 This PR has been automatically marked as stale because it hasn't had activity for 45 days. To keep this PR open:
This will be automatically closed in 14 days if no further activity occurs. Thanks for contributing to browser-use! 🤖 |
|
🔒 This PR was automatically closed because it was stale for 14 days with no activity. Don't worry! If you'd like to continue this work:
Thanks for contributing to browser-use! 🤖 |
Summary
chunk_markdown_by_structurecould report achar_offset_endone character past the end of the content for the final chunk whenever the markdown ends with a trailing newline.Root cause
In
_parse_atomic_blocks, each line getsline_len = len(line) + 1to account for the\nthatstr.split('\n')removed. But whencontentends with a newline,split('\n')yields a spurious empty final element ("a\n".split("\n") == ["a", ""]). That phantom element adds an extra+1, so the last block'schar_end— and therefore the last chunk'schar_offset_end— becomeslen(content) + 1.The existing fix-up only handled the opposite case (content without a trailing newline):
So trailing-newline content (the common case for extracted page markdown) slipped through.
Fix
Clamp the final block's
char_endtolen(content)in both cases:Impact
[0, len(content)].start_from_charresume is corrected: passingstart_from_char == len(content)now returns no chunks (fully consumed) instead of wrongly re-returning the final chunk, because the resume filter compareschunk.char_offset_end > start_from_char.Test
Added
test_offsets_dont_overshoot_with_trailing_newline, which fails on the old code (Last offset 42 overshoots content length 41) and passes with the fix. Full chunking suite (24 tests) andruff checkpass.Summary by cubic
Fixes an off-by-one in markdown chunking when the content ends with a newline. The last chunk’s
char_offset_endnow equalslen(content), which also fixes end-of-contentstart_from_charresume.char_endtolen(content)in_parse_atomic_blocksfor both trailing and non-trailing newline cases.char_offset_endnever overshoots andstart_from_char == len(content)returns no chunks.Written for commit eb6f640. Summary will update on new commits.