Skip to content

fix: keep balanced trailing brackets in URLs extracted from task text - #5576

Merged
sauravpanda merged 3 commits into
browser-use:mainfrom
aniketwaghh:fix_url_trailing_bracket_truncation
Aug 29, 2026
Merged

sauravpanda merged 3 commits into
browser-use:mainfrom
aniketwaghh:fix_url_trailing_bracket_truncation

Conversation

@aniketwaghh

@aniketwaghh aniketwaghh commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5575

What was wrong

sanitize_url_candidate() drops trailing prose punctuation so that "Go to https://example.com/docs." does not navigate with the sentence's period attached. It did that with a single character class:

return re.sub(r'[.,;:!?()\[\]]+$', '', candidate)

which also strips closing brackets the URL opened itself. Agent._extract_start_url() runs every URL it finds in the task through this before auto-navigating, so:

Summarize https://en.wikipedia.org/wiki/Python_(programming_language)

navigated to https://en.wikipedia.org/wiki/Python_(programming_language and landed on the wrong page. Wikipedia disambiguation links are the common shape.

The fix

Whether a trailing bracket belongs to the URL is a question of balance. .../Python_(programming_language) closes a bracket the URL opened, so it is part of the path. In "See the docs (https://example.com/guide)" the prose opened it, so dropping it stays correct. The loop strips trailing punctuation as before and only drops a closing bracket when the candidate has more closers than openers.

Before and after

from browser_use.agent.service import Agent
Agent._extract_start_url(None, 'Summarize https://en.wikipedia.org/wiki/Python_(programming_language)')
task text before after
Summarize https://en.wikipedia.org/wiki/Python_(programming_language) ...Python_(programming_language ...Python_(programming_language)
Check https://example.com/a[1] please https://example.com/a[1 https://example.com/a[1]
Go to https://example.com/docs. Then stop. https://example.com/docs https://example.com/docs
See the docs (https://example.com/guide) for details. https://example.com/guide https://example.com/guide

The last two rows are the behaviour that had to be preserved: a sentence period is still dropped, and a bracket the prose opened is still dropped.

Bracket totals are counted once and decremented while trimming, with an end index instead of reslicing, so a candidate ending in a long run of brackets stays linear like the regex it replaces. 200,000 trailing ) takes 0.011s.

Testing

Added assertions to test_beta_agent_exposes_task_helper_methods, next to the existing _extract_start_url cases, covering both directions: the bracket the URL opened is kept, the one the prose opened is not. They fail on main and pass here.

Full tests/ci, before and after, on the same machine:

clean tree:  2 failed, 1094 passed, 34 skipped
this branch: 2 failed, 1095 passed, 34 skipped

The two failures are pre-existing and unrelated, both in test_beta_agent.py (test_beta_agent_constructor_type_hints_match_browser_use_core_params and test_beta_agent_initializes_tools_and_action_models, Tools class-identity assertions). They fail identically on a clean tree. I also diffed the skipped-test lists between the two runs and they are identical, so nothing changed state.

uv run ruff check browser_use/utils.py tests/ci/test_beta_agent.py   ->  All checks passed!
uv run ruff format --check ...                                      ->  2 files already formatted
uv run pyright browser_use/utils.py                                 ->  0 errors, 0 warnings, 0 informations

No new warnings.

@CLAassistant

CLAassistant commented Aug 28, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@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 with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread browser_use/utils.py Resolved
sanitize_url_candidate() strips trailing prose punctuation so that
"Go to https://example.com/docs." does not navigate with the sentence's
period attached. It also stripped every trailing ) and ], including the
ones the URL opened itself, so a task like

    Summarize https://en.wikipedia.org/wiki/Python_(programming_language)

auto-navigated to .../Python_(programming_language and landed on the wrong
page. Wikipedia disambiguation links are the common case.

Whether the bracket belongs to the URL is decided by balance: a closing
bracket with a matching opener inside the candidate is part of the path,
while one the prose opened, as in "(see https://example.com/guide)", is not.
Strip trailing punctuation as before, and only drop a closing bracket when
the candidate has more of them than openers.

Fixes browser-use#5575
The trimming loop counted brackets across the whole candidate on every
iteration, so a candidate ending in many unmatched brackets rescanned it
once per bracket. 50,000 trailing ')' took 0.9s, where the regex this
replaced was linear.

Count the four bracket characters once and decrement as characters are
trimmed, tracking the end index instead of reslicing. Same results, and
200,000 trailing ')' now takes 0.011s.
@aniketwaghh
aniketwaghh force-pushed the fix_url_trailing_bracket_truncation branch from 3290a65 to 3c565af Compare August 28, 2026 04:20
@sauravpanda
sauravpanda merged commit 2e32d26 into browser-use:main Aug 29, 2026
112 checks passed
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: URLs ending in a closing bracket are truncated before auto-navigation

3 participants