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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
studiobench: delete through the reply's More menu, where #12735 moved it (#12748)
Daniel Hanf423a8ba57dd
diff --git a/tests/studio/studiobench/scene/actions.py b/tests/studio/studiobench/scene/actions.pyindex da1ad957918..12dfe27119d 100644--- a/tests/studio/studiobench/scene/actions.py+++ b/tests/studio/studiobench/scene/actions.py@@ -709,9 +709,20 @@ OWN_TURN_TEXT = "one more"#: Remove the throwaway turn: assistant first, then the user turn, because deleting the user#: message can take the reply with it and leave the count ambiguous. Reports rather than asserts.+#: The cleanup reaches Delete through the reply's More menu (#12735), so it first waits for that+#: menu to mount. A paint or two in practice; the bound matters only when the menu is slow or never+#: comes. `stop_generation` passes what is left of its slot after the settle, clamped to these, so a+#: slow menu cannot carry the turn into the next action's window. The floor is a few paints, so a+#: slot already spent still tries once rather than leaving the turn in the thread.+CLEANUP_MENU_MAX_MS = 1000+CLEANUP_MENU_MIN_MS = 100+#: What the slot still owes after the menu opens: the 200 ms settle and the delete itself.+CLEANUP_AFTER_MENU_MS = 300+STOP_CLEANUP_JS = """-async (timeoutMs) => {+async (opts) => {const D = window.__sb.dom;+ const timeoutMs = opts.timeoutMs;// threadTotal, not messageCount. Identical on the shipped build; under a windowed mount the// window refills as the message leaves it, so a cleanup that worked reports after == before.const before = D.threadTotal();@@ -719,8 +730,15 @@ async (timeoutMs) => {const target = D.lastAssistantMessage();if (!target) return false;target.dispatchEvent(new PointerEvent("pointerover", { bubbles: true, pointerType: "mouse" }));- const button = D.actionButton("Delete message");- if (!button) return false;+ // Delete is the More menu's last item since #12735; see DELETE_JS.+ const trigger = D.actionButton("More");+ if (!trigger) return false;+ const button = (await D.openMenuAndFind(trigger, "Delete", opts.menuWaitMs)).item;+ if (!button) {+ document.dispatchEvent(new KeyboardEvent("keydown",+ { key: "Escape", bubbles: true, cancelable: true }));+ return false;+ }const started = performance.now();button.click();while (performance.now() - started < timeoutMs) {@@ -814,7 +832,15 @@ def _reclaim_pending_turn(and isinstance(messages_after, int)and messages_after > messages_before)- removed = _ev(ctx, STOP_CLEANUP_JS, SETTLE_TIMEOUT_MS) if grew else None+ removed = (+ _ev(+ ctx,+ STOP_CLEANUP_JS,+ {"timeoutMs": SETTLE_TIMEOUT_MS, "menuWaitMs": CLEANUP_MENU_MAX_MS},+ )+ if grew+ else None+ )if removed is not None:ctx.page.wait_for_timeout(200)@@ -957,7 +983,15 @@ def stop_generation(ctx: ActionContext) -> ActionResult:# comparison all measure.removed = Noneif own_generation:- removed = _ev(ctx, STOP_CLEANUP_JS, SETTLE_TIMEOUT_MS)+ menu_wait_ms = min(+ CLEANUP_MENU_MAX_MS,+ max(CLEANUP_MENU_MIN_MS, remaining_ms() - CLEANUP_AFTER_MENU_MS),+ )+ removed = _ev(+ ctx,+ STOP_CLEANUP_JS,+ {"timeoutMs": SETTLE_TIMEOUT_MS, "menuWaitMs": menu_wait_ms},+ )ctx.page.wait_for_timeout(200)return ActionResult(ran = True,@@ -2339,23 +2373,40 @@ async (opts) => {// "a running message hides it" was already the diagnosis in the line this replaced, and the// action still reported NOT RUN on the first sample rather than waiting for the running message// to stop running. Same wait, same bound and same reporting as `message_menu`.- const found = await D.waitForActionButton("Delete message", opts.waitForButtonMs);- const button = found.el;+ //+ // Delete is the last item of the reply's More menu since #12735, not a button on the bar, so+ // the wait is for the More trigger and the menu is opened BEFORE the clock starts: what this+ // action times is the delete, and `message_menu` already times the menu.+ const found = await D.waitForActionButton("More", opts.waitForButtonMs);const waitedMs = found.waitedMs;- if (!button) {+ if (!found.el) {return {ran: false,waitedMs,running: found.running,reason:- "no Delete button after waiting " + waitedMs + "ms" ++ "no More button after waiting " + waitedMs + "ms" +(found.running ? ": the thread was still generating, which unmounts the action bar" : ""),};}+ const menu = await D.openMenuAndFind(found.el, "Delete", opts.waitForButtonMs);+ const button = menu.item;+ if (!button) {+ document.dispatchEvent(new KeyboardEvent("keydown",+ { key: "Escape", bubbles: true, cancelable: true }));+ return {+ ran: false,+ waitedMs,+ reason:+ "no Delete item in the More menu (opened=" + menu.opened + ", items=" + menu.items ++ ", after " + menu.openMs + "ms)",+ };+ }const target = D.lastAssistantMessage();const before = D.threadTotal();const mountedBefore = D.messageCount();const started = performance.now();+ // A Radix menu item selects on click, unlike its trigger.button.click();let ms = null;// isConnected on the captured node is O(1). Re-counting [data-role] every frame would put an@@ -2375,7 +2426,8 @@ async (opts) => {// alongside rather than replacing it: on a windowed mount `messageCount()` is the size of the// window and a recycled node would read as a delete. `waitedMs` is how long the bar was waited// for, which the row reports so a slot that opened too early is visible.- return { ran: true, waitedMs, ms: ms === null ? null : Math.round(ms * 10) / 10,+ return { ran: true, waitedMs, menuOpenMs: menu.openMs,+ ms: ms === null ? null : Math.round(ms * 10) / 10,before, after: D.threadTotal(),mountedBefore, mountedAfter: D.messageCount() };}@@ -2407,6 +2459,7 @@ def delete_message(ctx: ActionContext) -> ActionResult:"mounted_before": raw.get("mountedBefore"),"mounted_after": raw.get("mountedAfter"),"action_bar_wait_ms": raw.get("waitedMs"),+ "menu_open_ms": raw.get("menuOpenMs"),},timings = {"delete_ms": raw["ms"]},reason = None if ok else f"the message count went {raw['before']} -> {raw['after']}",diff --git a/tests/studio/studiobench/scene/dom.js b/tests/studio/studiobench/scene/dom.jsindex 1a092451a38..ad47b217834 100644--- a/tests/studio/studiobench/scene/dom.js+++ b/tests/studio/studiobench/scene/dom.js@@ -307,6 +307,58 @@const menu = D.openMenu();return menu ? qa(".aui-action-bar-more-item", menu).length : 0;},+ // An item of the open More menu, by its text. Delete lives here, last and in red, since #12735+ // took it off the reply's action bar; a prompt's More menu carries it too.+ menuItem(name) {+ const menu = D.openMenu();+ return menu ? byName(".aui-action-bar-more-item", name, menu) : null;+ },+ // Open a More menu from its trigger and wait, bounded, for `name` in it. pointerdown/up, not+ // click(): the Radix trigger opens on pointerdown, so click() leaves the menu shut.+ //+ // NO DOCUMENT-WIDE LOOKUP PER PAINT. The menu is portaled to the end of document.body, so+ // finding it is a whole-document query, and repeating it every paint inside the action's+ // window is the harness cost MENU_JS avoids with the same MutationObserver. The portal is+ // looked for only when body's children changed; once it is there, the item is looked for+ // inside it, which is O(the menu).+ async openMenuAndFind(trigger, name, waitMs) {+ const pointer = { bubbles: true, cancelable: true, composed: true, button: 0,+ pointerId: 1, pointerType: "mouse", isPrimary: true };+ const budget = Math.max(0, Number(waitMs) || 0);+ const nextPaint = () =>+ window.__sbNextPaint ? window.__sbNextPaint() : new Promise((r) => setTimeout(r, 16));+ let bodyChanged = true;+ const watcher = new MutationObserver(() => { bodyChanged = true; });+ watcher.observe(document.body, { childList: true, subtree: false });+ let menu = null;+ let item = null;+ const look = () => {+ if (!menu || !menu.isConnected) {+ if (!bodyChanged) return;+ bodyChanged = false;+ menu = D.openMenu();+ }+ if (menu) item = byName(".aui-action-bar-more-item", name, menu);+ };+ const started = performance.now();+ try {+ trigger.dispatchEvent(new PointerEvent("pointerdown", { ...pointer, buttons: 1 }));+ trigger.dispatchEvent(new PointerEvent("pointerup", { ...pointer, buttons: 0 }));+ look();+ while (!item && performance.now() - started < budget) {+ await nextPaint();+ look();+ }+ } finally {+ watcher.disconnect();+ }+ return {+ item,+ opened: Boolean(menu),+ items: menu ? qa(".aui-action-bar-more-item", menu).length : 0,+ openMs: Math.round((performance.now() - started) * 10) / 10,+ };+ },settingsTrigger() {return q('button[aria-label="Settings"]');diff --git a/tests/studio/studiobench/scene/selftest/test_studiobench_delete_through_more_menu.py b/tests/studio/studiobench/scene/selftest/test_studiobench_delete_through_more_menu.pynew file mode 100644index 00000000000..c5cc43ade37--- /dev/null+++ b/tests/studio/studiobench/scene/selftest/test_studiobench_delete_through_more_menu.py@@ -0,0 +1,252 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""`delete_message` reaches Delete where the app puts it: the last item of the reply's More menu.++#12735 took Delete off the assistant action bar and made it the More menu's last item. The scene+still looked for a "Delete message" button on the bar, waited out `ACTION_BAR_WAIT_MS` for it, and+reported `delete_message: NOT RUN -- no Delete button`, which fails the real-path session's+liveness gate on every pull request.++Two checks. The first runs the shipped `DELETE_JS` and `dom.js` in node against a shim of the+handful of DOM calls they make, so the menu walk is exercised rather than re-implemented. The+second reads thread.tsx and requires every control name the scene asks for to still be rendered+there, so the next move of a control fails here, with its name, instead of as a NOT RUN in a+browser job.+"""++from __future__ import annotations++import json+import re+import shutil+import subprocess+import sys+import tempfile+from pathlib import Path++import pytest++sys.path.insert(0, str(Path(__file__).resolve().parents[3]))++from studiobench.scene.actions import DELETE_JS # noqa: E402++SCENE = Path(__file__).resolve().parents[1]+DOM_JS = SCENE / "dom.js"+REPO = Path(__file__).resolve().parents[5]+THREAD_TSX = REPO / "studio" / "frontend" / "src" / "components" / "assistant-ui" / "thread.tsx"++HARNESS_JS = r"""+const fs = require("fs");+const domSrc = fs.readFileSync(process.argv[2], "utf8");+const deleteSrc = fs.readFileSync(process.argv[3], "utf8");+const menuNames = JSON.parse(process.argv[4]);++// A node matches a selector when the selector is in its `sel` list: dom.js reads the app through+// a short fixed list of selectors, and a real matcher would only be a second thing to get wrong.+const node = (sel, attrs, kids) => {+ const self = {+ sel: sel,+ kids: kids || [],+ isConnected: true,+ getAttribute: (k) => (attrs && k in attrs ? attrs[k] : null),+ textContent: (attrs && attrs.text) || "",+ dispatchEvent: (ev) => { if (attrs && attrs.on) attrs.on(ev); return true; },+ click: () => { if (attrs && attrs.click) attrs.click(); },+ closest: () => null,+ };+ const all = (s) => {+ const out = [];+ for (const k of self.kids) {+ if (k.sel.indexOf(s) >= 0) out.push(k);+ for (const g of k.querySelectorAll(s)) out.push(g);+ }+ return out;+ };+ self.querySelectorAll = all;+ self.querySelector = (s) => all(s)[0] || null;+ return self;+};++const state = { menuOpen: false, escapes: 0, menuLookups: 0 };+// The portal mounting is a change to body's children, delivered to observers as the app's would be.+const observers = [];+class MutationObserver {+ constructor(cb) { this.cb = cb; }+ observe() { observers.push(this); }+ disconnect() { const i = observers.indexOf(this); if (i >= 0) observers.splice(i, 1); }+}+const setMenu = (open) => {+ state.menuOpen = open;+ for (const o of observers.slice()) o.cb();+};+// The menu mounts this many paints after the trigger is pressed, as a portal does a commit later.+let paintsUntilMenu = 0;+const user = node(['[data-role]', '[data-role="user"]'], {});+let messages = [user];++// Radix: the trigger opens on pointerdown, an item selects on click.+const more = node(["button"], {+ "aria-label": "More",+ on: (ev) => { if (ev && ev.type === "pointerdown") paintsUntilMenu = 3; },+});+const bar = node([".aui-assistant-action-bar-root"], {}, [more]);+const assistant = node(['[data-role]', '[data-role="assistant"]'], {}, [bar]);+messages.push(assistant);++const items = menuNames.map((name) => node([".aui-action-bar-more-item"], {+ text: name,+ click: () => {+ if (name !== "Delete") return;+ setMenu(false);+ assistant.isConnected = false;+ messages = messages.filter((m) => m !== assistant);+ },+}));+const menu = node([".aui-action-bar-more-content"], {}, items);++const document = {+ body: { sel: ["body"] },+ querySelector: (s) => document.querySelectorAll(s)[0] || null,+ querySelectorAll: (s) => {+ if (s === "[data-role]") return messages;+ if (s === '[data-role="assistant"]') return messages.filter((m) => m === assistant);+ if (s === ".aui-action-bar-more-content") {+ state.menuLookups += 1;+ return state.menuOpen ? [menu] : [];+ }+ return [];+ },+ dispatchEvent: (ev) => {+ if (ev && ev.key === "Escape") { setMenu(false); state.escapes += 1; }+ return true;+ },+};++class PointerEvent { constructor(type, init) { this.type = type; Object.assign(this, init || {}); } }+class KeyboardEvent { constructor(type, init) { this.type = type; Object.assign(this, init || {}); } }++const window = {};+window.__sbNextPaint = () => new Promise((r) => setTimeout(() => {+ if (paintsUntilMenu > 0 && --paintsUntilMenu === 0) setMenu(true);+ r();+}, 5));+window.addEventListener = () => {};++(new Function("window", "document", "PointerEvent", "MutationObserver", domSrc))(+ window, document, PointerEvent, MutationObserver);++const run = (new Function(+ "window", "document", "PointerEvent", "KeyboardEvent", "performance",+ "return (" + deleteSrc + ")",+))(window, document, PointerEvent, KeyboardEvent, performance);++run({ timeoutMs: 2000, waitForButtonMs: 300 }).then((out) => {+ out.menuOpenAfter = state.menuOpen;+ out.escapes = state.escapes;+ out.menuLookups = state.menuLookups;+ console.log(JSON.stringify(out));+ process.exit(0);+}, (err) => { console.error(String((err && err.stack) || err)); process.exit(1); });+"""+++def _run(menu_names: list[str]) -> dict:+ node = shutil.which("node")+ if node is None:+ pytest.skip("node is not installed")+ with tempfile.TemporaryDirectory() as tmp:+ harness = Path(tmp) / "harness.cjs"+ harness.write_text(HARNESS_JS, encoding = "utf-8")+ delete_js = Path(tmp) / "delete.js"+ delete_js.write_text(DELETE_JS.strip(), encoding = "utf-8")+ done = subprocess.run(+ [node, str(harness), str(DOM_JS), str(delete_js), json.dumps(menu_names)],+ capture_output = True,+ text = True,+ timeout = 60,+ )+ assert done.returncode == 0, done.stderr+ return json.loads(done.stdout.strip().splitlines()[-1])+++def test_delete_opens_the_more_menu_and_selects_its_delete_item():+ out = _run(["Edit response", "Fork in new chat", "Delete"])+ assert out["ran"] is True, out+ assert out["ms"] is not None, "the reply never left the document after Delete was selected"+ assert (out["before"], out["after"]) == (2, 1), out+ # The menu mounts three paints late. The document-wide lookup for it runs on the first look+ # and when body's children change, not once per paint while waiting.+ assert out["menuLookups"] <= 2, out+++def test_a_menu_without_delete_is_reported_and_closed():+ out = _run(["Edit response", "Fork in new chat"])+ assert out["ran"] is False+ assert "no Delete item in the More menu" in out["reason"], out["reason"]+ assert (+ out["menuOpenAfter"] is False and out["escapes"] == 1+ ), "a menu the action opened and could not use must be closed, or it covers the next action"+++def _names_the_scene_asks_for() -> tuple[set[str], set[str]]:+ text = (SCENE / "actions.py").read_text(encoding = "utf-8") + (DOM_JS).read_text(encoding = "utf-8")+ buttons = set(re.findall(r"\b(?:waitForActionButton|actionButton)\(\"([^\"]+)\"", text))+ items = set(re.findall(r"\bopenMenuAndFind\([^,]+,\s*\"([^\"]+)\"", text))+ return buttons, items+++def _component_body(tsx: str, name: str) -> str | None:+ """The source of a component defined in thread.tsx, `const X: FC = ...` or `function X(`."""+ match = re.search(rf"^(?:const {name}\b[^\n]*=>\s*\{{|function {name}\()", tsx, re.M)+ if not match:+ return None+ end = tsx.find("\n}", match.end())+ return tsx[match.start() : end if end != -1 else len(tsx)]+++def _assistant_bar_source(tsx: str) -> str:+ """AssistantActionBar and every thread.tsx component it renders, transitively.++ The scene acts on the last ASSISTANT message, so a control only counts if the assistant bar+ renders it: `More` and `Delete` also exist in the user message's menu and as reusable+ components, and a whole-file search would still find them after the assistant bar dropped them.+ """+ seen: dict[str, str] = {}+ queue = ["AssistantActionBar"]+ while queue:+ name = queue.pop()+ if name in seen:+ continue+ body = _component_body(tsx, name)+ if body is None:+ continue+ seen[name] = body+ queue.extend(re.findall(r"<([A-Z][A-Za-z0-9]*)\b(?!\.)", body))+ assert "AssistantActionBar" in seen, "could not find AssistantActionBar in thread.tsx"+ return "\n".join(seen.values())+++def test_every_control_the_scene_asks_for_is_still_rendered():+ """Bar buttons by their tooltip, menu items by their text, read out of what the assistant+ message's action bar renders in thread.tsx."""+ tsx = _assistant_bar_source(THREAD_TSX.read_text(encoding = "utf-8"))+ buttons, items = _names_the_scene_asks_for()+ assert (+ {"More", "Copy"} <= buttons and "Delete" in items+ ), "parsed fewer control names than the scene uses; this check would pass on nothing"+ tooltips = set(re.findall(r"tooltip=\"([^\"]+)\"", tsx))+ # An item's label is its last line of text before the closing tag. Its attributes can hold+ # arrow functions, so the opening tag cannot be skipped with a `[^>]*` match.+ rendered_items = set()+ for body in re.findall(+ r"<ActionBarMorePrimitive\.Item\b([\s\S]*?)</ActionBarMorePrimitive\.Item>", tsx+ ):+ lines = [line.strip() for line in body.splitlines() if line.strip()]+ if lines and not lines[-1].startswith(("<", "{", "/")):+ rendered_items.add(lines[-1])+ missing = sorted(buttons - tooltips) + sorted(items - rendered_items)+ assert not missing, (+ f"the studiobench scene asks for {missing}, which the assistant action bar in thread.tsx "+ "no longer renders as a tooltip or a More-menu item, so that action would report NOT RUN"+ )diff --git a/tests/studio/studiobench/scene/selftest/test_studiobench_stop_slot_budget.py b/tests/studio/studiobench/scene/selftest/test_studiobench_stop_slot_budget.pyindex 2bd4a79cbe9..feb7d83f03e 100644--- a/tests/studio/studiobench/scene/selftest/test_studiobench_stop_slot_budget.py+++ b/tests/studio/studiobench/scene/selftest/test_studiobench_stop_slot_budget.py@@ -145,6 +145,7 @@ class _Page:stop_ms: float = STOP_MS,cleanup_ms: float = CLEANUP_MS,accepts_send: bool = True,+ menu_never_opens: bool = False,) -> None:self._clock = clockself._entered = clock.t@@ -154,6 +155,9 @@ class _Page:self._stop_ms = stop_msself._cleanup_ms = cleanup_msself.accepts_send = accepts_send+ # The cleanup reaches Delete through the More menu (#12735); a menu that never mounts costs+ # the whole wait the action allowed it.+ self.menu_never_opens = menu_never_opensself.running = Trueself.filled: list[str] = []self.composer = ""@@ -193,6 +197,14 @@ class _Page:):self.charge()if arg is not None: # STOP_CLEANUP_JS, which is the only call that takes one+ if self.menu_never_opens:+ self.charge(arg["menuWaitMs"])+ return {+ "removed": False,+ "before": self.messages,+ "after": self.messages,+ "reason": "no Delete control on the throwaway turn",+ }self.charge(self._cleanup_ms)before = self.messagesif self.messages:@@ -335,6 +347,27 @@ def test_no_moment_the_reply_can_drain_lets_the_action_spend_the_next_slot(monke)[email protected]("scene", [FAST, QUICK, STANDARD], ids = lambda s: s.name)+def test_a_cleanup_menu_that_never_opens_still_ends_inside_the_slot(monkeypatch, scene):+ """The cleanup now opens the reply's More menu before it can select Delete, and that wait was not+ priced into `OWN_TURN_RESERVE_MS`. It is bounded by what is left of the slot instead, so the+ worst case, a menu that never mounts, is swept over every drain time like the case above."""++ stop, nxt, slack_ms = _stop_slot(scene)+ for drained_at in range(0, stop.budget_ms, 50):+ result, page = _run(+ monkeypatch,+ budget_ms = stop.budget_ms,+ drain_after_ms = drained_at,+ menu_never_opens = True,+ )+ assert page.elapsed_ms <= stop.budget_ms + slack_ms, (+ f"{scene.name}: a reply draining {drained_at}ms into the slot, then a cleanup menu "+ f"that never opened, left stop_generation spending {page.elapsed_ms:.0f}ms of a "+ f"{stop.budget_ms}ms slot with only {slack_ms}ms before {nxt.action} opens"+ )++@pytest.mark.parametrize("scene", [FAST, QUICK, STANDARD], ids = lambda s: s.name)def test_a_send_the_app_refused_is_bounded_by_the_slot_and_not_by_eight_seconds(monkeypatch, scene):"""The other unbounded wait after the drain, on the one path where cutting it really is free.