Skip to content

fix(dom): raise on inverted Rect bounds instead of ignoring them - #6066

Open
Winoren wants to merge 3 commits into
browser-use:0.xfrom
Winoren:fix/rect-inverted-bounds-5987
Open

Winoren wants to merge 3 commits into
browser-use:0.xfrom
Winoren:fix/rect-inverted-bounds-5987

Conversation

@Winoren

@Winoren Winoren commented Oct 14, 2026 •

Copy link
Copy Markdown

What this fixes

Closes #5987

Rect.__post_init__ in browser_use/dom/serializer/paint_order.py did:

def __post_init__(self):
	if not (self.x1 <= self.x2 and self.y1 <= self.y2):
		return False

Python discards a __post_init__ return value, so this guard never rejected
anything — an inverted rectangle was constructed and kept.

The failure is quiet rather than obviously wrong: because both deltas flip sign,
area() still returns a positive number.

Rect(x1=100.0, y1=50.0, x2=20.0, y2=10.0).area()   # 3200.0

So the rect looks plausible, gets added to RectUnionPure, and corrupts the
occlusion result instead of failing loudly.

The change

Raise ValueError when the bounds are inverted, so invalid geometry surfaces at
construction instead of propagating into the paint-order union.

Degenerate zero-area rects stay valid (x1 == x2 and/or y1 == y2), because
collapsed and hidden elements are normal on real pages and are already handled
downstream. All existing Rect(...) construction sites in _split_diff preserve
the ordering by construction, so they are unaffected.

Deliberately not included: the second half of #5987 — filtering
non-positive-dimension nodes before they reach Rect(...) in
calculate_paint_order(). That changes which elements participate in occlusion,
so it deserves its own review rather than riding along here.

Validation

Baseline: c75e8476e26d18b7617643bc2ae082fae8eae431 on main.

  • New regression tests in tests/ci/test_dom_paint_order_serialization.py cover
    inverted x, inverted y, both inverted, and assert degenerate/normal rects still
    behave as before.
  • Reverting only the one-line source change makes those tests fail with
    Failed: DID NOT RAISE <class 'ValueError'>, so the tests genuinely gate the fix.
  • uv run pytest tests/ci/test_dom_paint_order_serialization.py → 5 passed
  • uv run pytest tests/ci/test_dom_visibility.py tests/ci/test_dom_paint_order_serialization.py tests/ci/test_ax_name_matching.py → 24 passed
  • uv run ruff check → All checks passed
  • uv run ruff format --check → 2 files already formatted

Disclosure

This patch was prepared with AI assistance. The reproduction, the before/after
regression check, the wider DOM suite and both linters were executed locally, and
the results above are the real output of those runs.

Happy to adjust the approach — in particular whether a hard ValueError is
preferred over skipping the node at the calculate_paint_order() call site.


Summary by cubic

Raises ValueError when Rect is constructed with inverted bounds. The previous guard (return False in __post_init__) was discarded by Python, so an inverted rectangle was silently built and, because both deltas flip sign, reported a plausible positive area and corrupted the paint-order occlusion result. Invalid geometry now surfaces at construction.

Degenerate zero-area rects (x1 == x2 or y1 == y2) stay valid, since collapsed and hidden elements are normal and handled downstream. All Rect(...) call sites in _split_diff already preserve bound ordering, and the branch is synced with upstream main, keeping the change scoped to paint_order.py.

Regression tests cover inverted x, inverted y, both inverted, and the degenerate/normal cases.

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

View guided diff

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/serializer/paint_order.py and its regression tests.

Validation after the sync:

  • uv run --no-sync pytest tests/ci/test_dom_paint_order_serialization.py tests/ci/integrations/test_anthropic_browser_use.py -q (16 passed, 1 skipped because the compatible SDK is installed)
  • uv run --no-sync pre-commit run --all-files (passed)

Rect.__post_init__ returned False when the bounds were inverted, but Python
discards a __post_init__ return value. The invalid rectangle was still built
and reported a plausible positive area, because both deltas flip sign and the
product stays positive. It then entered RectUnionPure and corrupted the
paint-order occlusion result instead of failing loudly.

Raise ValueError so invalid geometry surfaces at construction. Degenerate
zero-area rects stay valid, since collapsed and hidden elements are normal.

Closes browser-use#5987
@CLAassistant

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 sign our Contributor License Agreement before we can accept your contribution.
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.

No issues found across 2 files

View guided diff | Re-trigger cubic

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

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]: Dataclass Rect.__post_init__ silently ignores inverted bounds and calculate_paint_order permits non-positive dimensions

2 participants