Skip to content

fix(dom): correct chunk offset overshoot when markdown ends with a newline - #5028

Closed
Osamaali313 wants to merge 1 commit into
browser-use:mainfrom
Osamaali313:fix/chunk-offset-trailing-newline
Closed

Osamaali313 wants to merge 1 commit into
browser-use:mainfrom
Osamaali313:fix/chunk-offset-trailing-newline

Conversation

@Osamaali313

@Osamaali313 Osamaali313 commented Jun 12, 2026 •

Copy link
Copy Markdown

Summary

chunk_markdown_by_structure could report a char_offset_end one 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 gets line_len = len(line) + 1 to account for the \n that str.split('\n') removed. But when content ends 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's char_end — and therefore the last chunk's char_offset_end — becomes len(content) + 1.

The existing fix-up only handled the opposite case (content without a trailing newline):

if blocks and content and not content.endswith('\n'):
    blocks[-1] = _AtomicBlock(..., char_end=len(content))

So trailing-newline content (the common case for extracted page markdown) slipped through.

Fix

Clamp the final block's char_end to len(content) in both cases:

if blocks and content and blocks[-1].char_end != len(content):
    blocks[-1] = _AtomicBlock(..., char_end=len(content))

Impact

  • Offsets now always stay within [0, len(content)].
  • start_from_char resume is corrected: passing start_from_char == len(content) now returns no chunks (fully consumed) instead of wrongly re-returning the final chunk, because the resume filter compares chunk.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) and ruff check pass.

uv run pytest tests/ci/test_markdown_chunking.py   # 24 passed
uv run ruff check browser_use/dom/markdown_extractor.py tests/ci/test_markdown_chunking.py  # All checks passed

Summary by cubic

Fixes an off-by-one in markdown chunking when the content ends with a newline. The last chunk’s char_offset_end now equals len(content), which also fixes end-of-content start_from_char resume.

  • Bug Fixes
    • Clamp the final block’s char_end to len(content) in _parse_atomic_blocks for both trailing and non-trailing newline cases.
    • Add a regression test to ensure the final char_offset_end never overshoots and start_from_char == len(content) returns no chunks.

Written for commit eb6f640. Summary will update on new commits.

Review in cubic

…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.
Copilot AI review requested due to automatic review settings June 12, 2026 22:26
@CLAassistant

CLAassistant commented Jun 12, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_end to len(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.

Comment on lines +368 to +373
# 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):

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

@github-actions

Copy link
Copy Markdown

👋 This PR has been automatically marked as stale because it hasn't had activity for 45 days.

To keep this PR open:

  • Rebase against the latest main branch
  • Address any review feedback or merge conflicts
  • Add a comment explaining the current status
  • Add the work-in-progress label if you're still actively working on this

This will be automatically closed in 14 days if no further activity occurs.

Thanks for contributing to browser-use! 🤖

@github-actions github-actions Bot added the stale label Jul 28, 2026
@github-actions

Copy link
Copy Markdown

🔒 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:

  • Reopen this PR and rebase against main
  • Create a fresh PR with updated changes
  • Join our Discord if you need help

Thanks for contributing to browser-use! 🤖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants