Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

studiobench: image_upload closes the menu it opened when it gives up (#11892)

Daniel Han944fbd0cf0d0
diff --git a/tests/studio/studiobench/scene/actions.py b/tests/studio/studiobench/scene/actions.py
index 816c617e46b..da1ad957918 100644
--- a/tests/studio/studiobench/scene/actions.py
+++ b/tests/studio/studiobench/scene/actions.py
@@ -1639,6 +1639,46 @@ _COUNT_COMPOSER_ATTACHMENT_CONTAINERS_JS = (
)
+#: The composer's "Tools and attachments" menu is a MODAL Radix dropdown, and a modal one sets
+#: `pointer-events: none` on everything outside itself for as long as it is open. Menus are told apart
+#: by identity, not presence: the chat UI also has non-modal menus, whose outside pointerdown is let
+#: through, so the attachments click can dismiss one of those and open its own in the same moment.
+_MARK_MENUS_BEFORE_JS = """() => {
+ window.__sbMenusBefore = new WeakSet(document.querySelectorAll('[role="menu"]'));
+}"""
+_NEW_MENU_OPEN_JS = """() => {
+ const before = window.__sbMenusBefore || new WeakSet();
+ return Array.from(document.querySelectorAll('[role="menu"]')).some(m => !before.has(m));
+}"""
+
+
+def _close_open_menu(ctx: ActionContext) -> Optional[bool]:
+ """Escape until no menu opened by this attempt is left, bounded. True when one was open and is
+ now closed, False when none was, None when one is still open after the attempts.
+
+ An action that gives up must leave the page as it found it. A menu this action opened and then
+ abandoned is not this action's failure alone: the next action's click hit-tests to nothing, it
+ reports the control as unclickable, and a run that allows this action not to run still fails on
+ the one after it. A menu that was already open is not this action's to close: it can be the very
+ thing that made the click time out, and it belongs to whatever opened it. Escape dismisses the top
+ layer first, which is the one this attempt opened, so the loop stops before reaching an older one.
+ """
+ if _ev(ctx, _NEW_MENU_OPEN_JS) is not True:
+ return False
+ for _ in range(3):
+ ctx.page.keyboard.press("Escape")
+ ctx.page.wait_for_timeout(100)
+ if _ev(ctx, _NEW_MENU_OPEN_JS) is not True:
+ return True
+ return None
+
+
+def _left_menu_note(closed: Optional[bool]) -> str:
+ if closed is None:
+ return " (a menu it opened is still open after Escape)"
+ return " (closed the menu it opened)" if closed else ""
+
+
@register_action(name = "image_upload", default_budget_ms = 12000)
def image_upload(ctx: ActionContext) -> ActionResult:
"""Attach an image through the composer's file chooser.
@@ -1675,12 +1715,20 @@ def image_upload(ctx: ActionContext) -> ActionResult:
+ json.dumps(_ev(ctx, IMAGE_BUTTON_DIAGNOSTIC) or {})
)
before = _ev(ctx, _COUNT_COMPOSER_ATTACHMENTS_JS)
+ _ev(ctx, _MARK_MENUS_BEFORE_JS)
started = time.monotonic()
# Bounded by what is left of the slot, never by Playwright's 30s default.
try:
plus.click(timeout = max(500, min(ctx.budget_ms // 3, 5000)))
except Exception as exc: # noqa: BLE001
- return not_run(f"the attachments button could not be clicked: {type(exc).__name__}")
+ # A click can open the menu and still time out: Radix opens it on pointerdown. Left open, it
+ # blocked the next action's New chat button (thread_reopen NOT RUN, "no point on the control
+ # hit-tests to it") on a run that allowed only this action not to run.
+ closed = _close_open_menu(ctx)
+ return not_run(
+ f"the attachments button could not be clicked: {type(exc).__name__}"
+ + _left_menu_note(closed)
+ )
ctx.page.wait_for_timeout(200)
try:
with ctx.page.expect_file_chooser(timeout = 6000) as fc:
@@ -1690,8 +1738,10 @@ def image_upload(ctx: ActionContext) -> ActionResult:
}""")
fc.value.set_files(png)
except Exception as exc: # noqa: BLE001
- ctx.page.keyboard.press("Escape")
- return not_run(f"the file chooser never opened: {type(exc).__name__}: {exc}")
+ closed = _close_open_menu(ctx)
+ return not_run(
+ f"the file chooser never opened: {type(exc).__name__}: {exc}" + _left_menu_note(closed)
+ )
ctx.page.wait_for_timeout(800)
after = _ev(ctx, _COUNT_COMPOSER_ATTACHMENTS_JS)
elapsed = (time.monotonic() - started) * 1000
diff --git a/tests/studio/studiobench/scene/selftest/test_studiobench_image_upload_leaves_no_menu.py b/tests/studio/studiobench/scene/selftest/test_studiobench_image_upload_leaves_no_menu.py
new file mode 100644
index 00000000000..b6cdbef5f1f
--- /dev/null
+++ b/tests/studio/studiobench/scene/selftest/test_studiobench_image_upload_leaves_no_menu.py
@@ -0,0 +1,183 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""`image_upload` must not leave behind a menu it opened when it gives up.
+
+The composer's "Tools and attachments" menu is a modal Radix dropdown, which sets
+`pointer-events: none` on everything outside itself while it is open, and Radix opens it on
+pointerdown, so a click can open it and still time out. On 2026-09-25 that happened on #11887's
+studiobench run: `image_upload` reported "the attachments button could not be clicked:
+TimeoutError" and returned, the parity digest showed `[role="menu"]` open from that action on, and
+the next action failed with "button[aria-label="New chat"] was not clickable ... no point on the
+control hit-tests to it". CI passes `--allow-not-run image_upload`, so the failure it was excused
+for turned into one it was not: thread_reopen NOT RUN, and the job red.
+"""
+
+from __future__ import annotations
+
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
+
+
+class _Keyboard:
+ def __init__(self, page: "_Page") -> None:
+ self._page = page
+ self.pressed: list[str] = []
+
+ def press(self, key: str) -> None:
+ self.pressed.append(key)
+ # Escape dismisses the top layer only.
+ if key == "Escape" and self._page.menus and self._page.menu_closes_on_escape:
+ self._page.menus.pop()
+
+
+class _Button:
+ def __init__(self, page: "_Page") -> None:
+ self._page = page
+
+ def click(self, timeout = None) -> None:
+ page = self._page
+ if page.click_dismisses_open_menu:
+ page.menus.clear() # a non-modal menu lets the outside pointerdown through and closes
+ if page.click_opens_menu:
+ page.menus.append("attachments") # Radix opens on pointerdown
+ raise TimeoutError(f"Timeout {timeout}ms exceeded.")
+
+
+class _Locator:
+ def __init__(self, page: "_Page") -> None:
+ self._page = page
+
+ @property
+ def first(self) -> "_Locator":
+ return self
+
+ def element_handle(self, timeout = None) -> _Button:
+ return _Button(self._page)
+
+
+class _Page:
+ """The page calls `image_upload` makes up to and including a click that times out.
+
+ Open menus are a stack of identities, so a menu that replaced another is a different one.
+ """
+
+ def __init__(
+ self,
+ *,
+ click_opens_menu: bool,
+ menu_closes_on_escape: bool = True,
+ already_open: tuple = (),
+ click_dismisses_open_menu: bool = False,
+ ) -> None:
+ self.click_opens_menu = click_opens_menu
+ self.menu_closes_on_escape = menu_closes_on_escape
+ self.click_dismisses_open_menu = click_dismisses_open_menu
+ self.menus: list[str] = list(already_open)
+ self._before: set[str] = set()
+ self.keyboard = _Keyboard(self)
+
+ def locator(self, _selector: str) -> _Locator:
+ return _Locator(self)
+
+ def evaluate(
+ self,
+ script,
+ arg = None,
+ ):
+ if "__sbMenusBefore = " in script:
+ self._before = set(self.menus)
+ return None
+ if "__sbMenusBefore" in script:
+ return any(menu not in self._before for menu in self.menus)
+ if '[role="menu"]' in script:
+ return bool(self.menus) # a presence-only query
+ return 0 # the attachment count
+
+ def wait_for_timeout(self, _ms) -> None:
+ return None
+
+
+def _upload(page: _Page):
+ from studiobench.runtime.types import ActionContext
+ from studiobench.scene.actions import image_upload
+
+ ctx = ActionContext(
+ page = page,
+ cdp = None,
+ cell = None,
+ window = None,
+ args = {"image_path": "probe.png"},
+ budget_ms = 12_000,
+ dom = None,
+ log = lambda _m: None,
+ )
+ return image_upload(ctx)
+
+
+def test_a_click_that_opened_the_menu_and_timed_out_closes_it():
+ page = _Page(click_opens_menu = True)
+ result = _upload(page)
+
+ assert result.ran is False
+ assert "could not be clicked: TimeoutError" in result.reason
+ assert page.menus == [], "the modal menu was left open over the next action"
+ assert page.keyboard.pressed == ["Escape"]
+ assert "closed the menu it opened" in result.reason
+
+
+def test_a_click_that_opened_nothing_touches_nothing():
+ """No stray Escape: with nothing open it could close something the page legitimately shows."""
+ page = _Page(click_opens_menu = False)
+ result = _upload(page)
+
+ assert result.ran is False
+ assert page.keyboard.pressed == []
+ assert result.reason == "the attachments button could not be clicked: TimeoutError"
+
+
+def test_a_menu_that_will_not_close_is_reported_and_bounded():
+ page = _Page(click_opens_menu = True, menu_closes_on_escape = False)
+ result = _upload(page)
+
+ assert result.ran is False
+ assert page.keyboard.pressed == ["Escape"] * 3
+ assert "still open after Escape" in result.reason
+
+
+def test_a_menu_that_was_already_open_is_left_alone():
+ """Not this action's to close: it can be what blocked the click, and it belongs to its opener."""
+ page = _Page(click_opens_menu = False, already_open = ("model-picker",))
+ result = _upload(page)
+
+ assert result.ran is False
+ assert page.keyboard.pressed == []
+ assert page.menus == ["model-picker"]
+ assert result.reason == "the attachments button could not be clicked: TimeoutError"
+
+
+def test_a_menu_that_replaced_an_open_one_is_still_closed():
+ """A non-modal menu lets the click through and closes, and the attachments menu opens in its
+ place: something was open before and something is open after, and they are not the same menu."""
+ page = _Page(
+ click_opens_menu = True,
+ already_open = ("model-picker",),
+ click_dismisses_open_menu = True,
+ )
+ result = _upload(page)
+
+ assert result.ran is False
+ assert page.menus == [], "the attachments menu that replaced the old one was left open"
+ assert "closed the menu it opened" in result.reason
+
+
+def test_only_the_menu_this_attempt_opened_is_closed():
+ """Escape takes the top layer, which is the new one, and stops before an older one below it."""
+ page = _Page(click_opens_menu = True, already_open = ("model-picker",))
+ result = _upload(page)
+
+ assert result.ran is False
+ assert page.keyboard.pressed == ["Escape"]
+ assert page.menus == ["model-picker"]