Skip to content

fix(dom): stop a trailing newline from creating a zero-width chunk - #6004

Open
Winoren wants to merge 4 commits into
browser-use:0.xfrom
Winoren:fix/markdown-chunk-trailing-newline
Open

Winoren wants to merge 4 commits into
browser-use:0.xfrom
Winoren:fix/markdown-chunk-trailing-newline

Conversation

@Winoren

@Winoren Winoren commented Oct 6, 2026 •

Copy link
Copy Markdown

Fixes #6003

What changed

_parse_atomic_blocks does lines = content.split('\n'). When the markdown ends in a trailing newline that produces a final empty element which has no character behind it in the source, but the walk still charges it len(line) + 1. That pushes the last block past the end of the content, and the clamp at the end of the function pulls char_end back to len(content) — leaving a block with char_start == char_end.

Phase 2 sizes blocks by char_end - char_start, so that zero-width block never overflows the limit and lands alone in its own chunk, returned with the rest of the page's has_more.

The fix drops the phantom element before the walk. Offsets stay exact, and the existing clamp keeps handling the case it was written for (content with no trailing newline).

Why it surfaces to the model

service.py reports next_start_char = chunk.char_offset_end, and the tool message tells the model to pass it back. For a page ending in a newline that offset is len(content), which hits the guard in chunk_markdown_by_structure:

if start_from_char >= len(content):
    return []

and the empty result becomes start_from_char (…) exceeds content length … characters. The chunk the model already had contained all remaining content, so the page is complete and the follow-up call should not exist.

On main, for '# Report\n\n' + 'Revenue grew across every region. ' * 900 + '\n':

idx=0 [0,10)        width=10    has_more=True
idx=1 [10,30611)    width=30601  has_more=True
idx=2 [30611,30611) width=0                <- zero-width

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

After:

idx=0 [0,10)     width=10    has_more=True
idx=1 [10,30611) width=30601  has_more=False

start=0  -> chunk 1/2 has_more=True
start=10 -> chunk 2/2 has_more=False

It reads as intermittent because two trailing newlines do not trigger it: the final BLANK block then has width 1 and absorbs the phantom element.

Verification

Five cases added to TestChunkMarkdownTrailingNewline in tests/ci/test_markdown_chunking.py, covering the chunk list, the terminal flag, the full pagination walk, content without a trailing newline, and content ending in blank lines.

  • Against main: test_trailing_newline_does_not_create_empty_chunk fails (expected 2 chunks, got 3) and test_pagination_reaches_the_end_without_erroring fails (no chunk at offset 30611). The other three pass, so they pin behaviour that must not change.
  • With the fix: 5/5 pass.
  • Differential fuzz over 1500 generated pages at five chunk limits: 1483 byte-identical. All 16 differences are the zero-width tail chunk disappearing; every real chunk keeps the same offsets, content and has_more. No new zero-width chunk appears anywhere. The one [0, 0) chunk in the run is the pre-existing if not content early return, untouched here.
  • ruff check and ruff format --check clean on both files.

Not verified interactively: I did not drive a live browser page through the extract tool. The reproduction replays the tool's exact call sequence against the real chunker on main.

Relation to #5028

#5028 addressed the clamp condition and was auto-closed as stale after 45 days without review. Its change is already on main. This is the remaining half of the same defect, fixed at the source instead of by widening the clamp.

AI assistance

Drafted with AI assistance. The reproduction, the chunk-by-chunk trace, the root cause and the differential results were all produced by running the code; I reviewed the diff and the tests line by line and can explain every line.


Summary by cubic

Fixes #6003: markdown ending in a single trailing newline produced a zero-width final chunk that broke the extract tool's pagination. _parse_atomic_blocks splits content on \n, and the trailing newline leaves a final empty element that the walk charges one character, pushing the last block past the content end; the clamp pulls char_end back to len(content), leaving a zero-width block that becomes its own chunk with has_more=True. The tool then reports next_start_char = len(content) and the follow-up call fails with "start_from_char exceeds content length." The fix drops the phantom element before the walk; the clamp stays for content without a trailing newline. Two trailing newlines never triggered this because the final blank block then has width 1, which made the bug read as intermittent.

Chunk text is now sliced from content by block offsets instead of re-joining block text, so chunk content always matches the reported offsets.

Verification

  • Five new cases in TestChunkMarkdownTrailingNewline cover the chunk list, the terminal flag, the pagination walk, content without a trailing newline, and trailing blank lines; two fail on main and pass with the fix.
  • Differential fuzz over 1500 generated pages at five chunk limits: all differences are the zero-width tail chunk disappearing, with real chunks keeping identical offsets, content, and has_more.
  • Synced with upstream main for Anthropic SDK compatibility; 41 tests pass and pre-commit is clean.

Written for commit 4d4399c. Summary will update on new commits.

View guided diff

Current revision validation

All 27 synchronous Markdown chunking tests pass. Every pre-commit hook for the changed files passes, including Pyright. The two browser integration tests were not run.

Follow-up maintenance

Synced the branch with upstream main at 3df5806a0d097a1546d92c49bcf1b28c9972c3bd. This brings in the upstream Anthropic browser-tool SDK rename that resolves the prior import/type failures while leaving this PR's change scoped to browser_use/dom/markdown_extractor.py and its regression tests.

Validation after the sync:

  • uv run --no-sync pytest tests/ci/test_markdown_chunking.py tests/ci/integrations/test_anthropic_browser_use.py -q (41 passed)
  • uv run --no-sync pre-commit run --all-files (passed)

chunk_markdown_by_structure emits an extra chunk at [len, len) whenever the
markdown ends in a single trailing newline. _parse_atomic_blocks charges that
phantom split element one character, so the last block overshoots the content and
the existing clamp pulls char_end back to len(content), leaving char_start ==
char_end. Phase 2 gives that zero-width block a chunk of its own and reports
has_more, so the extract tool hands the model next_start_char = len(content) and
the next call fails with "start_from_char exceeds content length".

Dropping the phantom element at the source keeps offsets exact and leaves the
clamp in place for the no-trailing-newline case it was written for.

Two trailing newlines never showed this, which is why it reads as intermittent.

Fixes browser-use#6003
@CLAassistant

CLAassistant commented Oct 6, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
0 out of 2 committers have signed the CLA.

❌ D41910
❌ Winoren


D41910 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@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.

All reported issues were addressed across 2 files

Reply to a comment to ask cubic a question or push back. It learns from your replies.
Click Fix with cubic on a comment, or reply @cubic-dev-ai fix this, and cubic writes the fix.

Fix all with cubic | Turn on auto-fix | Re-trigger cubic

Comment thread tests/ci/test_markdown_chunking.py Resolved
@mintlify

mintlify Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 4d4399c.

Approve preview

@Winoren
Winoren changed the base branch from main to 0.x November 15, 2026 05:48

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: extract pagination fails with "start_from_char exceeds content length" when page markdown ends in a newline

2 participants