Repository navigation
Conversation
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
|
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. |
Contributor
There was a problem hiding this comment.
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
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
This branch has not been deployed
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.
Fixes #6003
What changed
_parse_atomic_blocksdoeslines = 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 itlen(line) + 1. That pushes the last block past the end of the content, and the clamp at the end of the function pullschar_endback tolen(content)— leaving a block withchar_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'shas_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.pyreportsnext_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 islen(content), which hits the guard inchunk_markdown_by_structure: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':After:
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
TestChunkMarkdownTrailingNewlineintests/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.main:test_trailing_newline_does_not_create_empty_chunkfails (expected 2 chunks, got 3) andtest_pagination_reaches_the_end_without_erroringfails (no chunk at offset 30611). The other three pass, so they pin behaviour that must not change.has_more. No new zero-width chunk appears anywhere. The one[0, 0)chunk in the run is the pre-existingif not contentearly return, untouched here.ruff checkandruff format --checkclean on both files.Not verified interactively: I did not drive a live browser page through the
extracttool. The reproduction replays the tool's exact call sequence against the real chunker onmain.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_blockssplits 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 pullschar_endback tolen(content), leaving a zero-width block that becomes its own chunk withhas_more=True. The tool then reportsnext_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
TestChunkMarkdownTrailingNewlinecover the chunk list, the terminal flag, the pagination walk, content without a trailing newline, and trailing blank lines; two fail onmainand pass with the fix.has_more.mainfor Anthropic SDK compatibility; 41 tests pass and pre-commit is clean.Written for commit 4d4399c. Summary will update on new commits.
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
mainat3df5806a0d097a1546d92c49bcf1b28c9972c3bd. This brings in the upstream Anthropic browser-tool SDK rename that resolves the prior import/type failures while leaving this PR's change scoped tobrowser_use/dom/markdown_extractor.pyand 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)