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.

Stop the rapid-submit chat UI check failing on a fast runner (#7972)

Daniel Han4ce9bf0ff1b4
diff --git a/tests/studio/playwright_chat_ui.py b/tests/studio/playwright_chat_ui.py
index 56668d111f7..1c513a7f18e 100644
--- a/tests/studio/playwright_chat_ui.py
+++ b/tests/studio/playwright_chat_ui.py
@@ -48,6 +48,10 @@ STRICT = os.environ.get("STUDIO_UI_STRICT", "0") == "1"
# Per-turn assistant-bubble wait. The free macos-14 runner is ~3-5x
# slower at gemma-3-270m CPU inference; this lets it bump the timeout.
TURN_TIMEOUT_MS = int(os.environ.get("STUDIO_UI_TURN_TIMEOUT_MS", "180000"))
+# How long the rapid-submit step holds the first turn's response. Only needs to
+# outlast the 100 ms follow-up wait; kept well clear of it so a loaded runner
+# cannot close the gap, and paid once per run.
+RAPID_FIRST_TURN_HOLD_S = 3.0
# Wall-clock cap for the whole script (healthy run is 5-9 min).
WALL_TIMEOUT_S = float(os.environ.get("STUDIO_UI_WALL_TIMEOUT_S", "720"))
@@ -1060,19 +1064,165 @@ with sync_playwright() as p:
step("rapid submit: 100 ms follow-up queues behind the first turn")
rapid_bubbles_before = _bubble_count()
composer_form = page.locator('form:has(textarea[aria-label="Message input"])').first
+ # How long a reply takes is not ours to decide: sampling settings, whatever
+ # GGUF_REPO points at and an early EOS all move it, and a short answer can
+ # finish inside the follow-up delay on a fast runner, leaving nothing to
+ # queue behind. So hold the first turn's response open rather than hope it
+ # is slow.
+ #
+ # The follow-up and the observation both run in the page, not here. The sync
+ # Playwright route handler runs on this thread, so a wait inside it blocks
+ # the test: the handler would fire during a wait_for_timeout, finish, and
+ # release the request before a Python-side second submit could happen, which
+ # puts the hold entirely before the follow-up instead of across it. Page
+ # timers keep running while this thread is parked in the handler.
+ # Everything here runs in the page. Playwright's sync route handler runs on
+ # this thread, so holding a request there blocks the test itself and the
+ # follow-up cannot be sent while the hold is in effect; and the page cannot
+ # observe a Playwright interception, so no in-page timer can be aligned with
+ # one. Wrapping fetch solves both: the page sees the exact moment the first
+ # turn's request goes out, sends the follow-up then, and delays the response
+ # itself, so the turn is provably still running with no timing assumption.
+ page.evaluate(
+ """(args) => {
+ const [secondPrompt, holdMs] = args;
+ window.__unslothRapid = {
+ intercepted: false, submitted: false, queueSeen: false,
+ observed: false, error: null, seen: [], holdUntil: 0,
+ };
+ const state = window.__unslothRapid;
+ const realFetch = window.fetch;
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
+
+ const sendFollowUp = (deadline) => {
+ if (state.submitted || state.error) return;
+ // Re-query, and retry: this is the chat's first message, so
+ // sending it swaps the welcome composer for the dock composer.
+ // A node captured earlier is detached, and for a short window
+ // there is no connected composer at all.
+ const composer = document.querySelector(
+ 'textarea[aria-label="Message input"]'
+ );
+ if (!composer || !composer.isConnected || !composer.form) {
+ if (deadline === undefined) deadline = Date.now() + 5000;
+ // Never retry past the hold. The response is released when
+ // it expires, so a submit after that races a buffered reply
+ // finishing first and would report a queue failure for an
+ // application that behaved correctly.
+ if (state.holdUntil) {
+ deadline = Math.min(deadline, state.holdUntil - 250);
+ }
+ if (Date.now() > deadline) {
+ state.error = "no connected composer for the follow-up";
+ return;
+ }
+ setTimeout(() => sendFollowUp(deadline), 25);
+ return;
+ }
+ // React tracks the value on the node, so a plain assignment is
+ // reverted on the next render.
+ const setValue = Object.getOwnPropertyDescriptor(
+ window.HTMLTextAreaElement.prototype, "value"
+ ).set;
+ setValue.call(composer, secondPrompt);
+ composer.dispatchEvent(new Event("input", { bubbles: true }));
+ composer.form.requestSubmit();
+ state.submitted = true;
+ };
+
+ window.fetch = async (...a) => {
+ const url = String(
+ (a[0] && a[0].url) ? a[0].url : a[0]
+ );
+ const isTurn = url.includes("chat/completions");
+ if (isTurn && !state.intercepted) {
+ state.intercepted = true;
+ state.holdUntil = Date.now() + holdMs;
+ state.seen.push(url);
+ const response = realFetch(...a);
+ // The request is out and the turn is running. Send the
+ // follow-up now, then keep the response pending so it
+ // cannot complete first.
+ setTimeout(() => sendFollowUp(), 0);
+ await sleep(holdMs);
+ return response;
+ }
+ return realFetch(...a);
+ };
+
+ // Always restore. A wrapper left in place for the rest of the
+ // run is a monkeypatch with no teardown, and every later turn would
+ // pay for it.
+ window.__unslothRapidArm = () => setTimeout(() => sendFollowUp(), 100);
+
+ window.__unslothRapidRestore = () => {
+ window.fetch = realFetch;
+ clearInterval(poll);
+ };
+
+ const poll = setInterval(() => {
+ if (document.querySelector(
+ 'button[aria-label="Remove queued prompt 1"]'
+ )) {
+ state.queueSeen = true;
+ clearInterval(poll);
+ }
+ }, 25);
+ setTimeout(() => {
+ clearInterval(poll);
+ if (!state.queueSeen && !state.error) {
+ // Resolve the wait rather than let it die on a generic
+ // Playwright timeout. That path is the regression this step
+ // exists to catch, and leaving it unresolved makes the
+ // screenshot, the explicit message and the fetch teardown
+ // below unreachable in exactly that case.
+ state.observed = true;
+ }
+ }, 20000);
+ }""",
+ ["Reply with exactly: rapid-second", int(RAPID_FIRST_TURN_HOLD_S * 1000)],
+ )
+
composer.fill("Reply with exactly: rapid-first")
composer_form.evaluate("form => form.requestSubmit()")
- page.wait_for_timeout(100)
- composer.fill("Reply with exactly: rapid-second")
- composer_form.evaluate("form => form.requestSubmit()")
+ # Arm the 100 ms path now that the first turn has been submitted. Whichever
+ # fires first wins and the other is a no-op, so this still covers the
+ # pre-render interval the step is named after, while the fetch path keeps
+ # the guarantee when persistence delays the request.
+ page.evaluate("() => window.__unslothRapidArm && window.__unslothRapidArm()")
- # The follow-up must materialize as queued work instead of a second direct
- # append. This control exists only while that queued item is cancellable.
- page.wait_for_selector(
- 'button[aria-label="Remove queued prompt 1"]',
- state = "attached",
- timeout = 10_000,
+ page.wait_for_function(
+ """() => window.__unslothRapid
+ && (window.__unslothRapid.queueSeen
+ || window.__unslothRapid.observed
+ || window.__unslothRapid.error)""",
+ timeout = 60_000,
)
+ state = page.evaluate("() => window.__unslothRapid")
+ page.evaluate("() => { if (window.__unslothRapidRestore) window.__unslothRapidRestore(); }")
+ if state["error"]:
+ shoot("04-rapid-submit-no-composer")
+ fail(f"could not send the follow-up: {state['error']}")
+ # queueSeen is the property under test; the hold is only the means of
+ # guaranteeing the first turn was still running. If the queue formed, it
+ # formed, whether or not the hold was needed. Only demand the interception
+ # when it did not, so an unheld run cannot report a silent pass.
+ if not state["queueSeen"] and not state["intercepted"]:
+ fail(
+ "the first turn's request was never seen, so it was never held, "
+ f"and no queue formed; saw {state['seen']}"
+ )
+ # The follow-up went out after the first turn's request was issued and while
+ # its response was still held, so that turn was necessarily running. A
+ # missing queue control is therefore a real regression, not timing.
+ if not state["queueSeen"]:
+ shoot("04-rapid-submit-no-queue")
+ fail(
+ "follow-up sent during a held first turn did not appear as queued "
+ f"work (submitted={state['submitted']}, intercepted="
+ f"{state['intercepted']})"
+ )
+
page.wait_for_function(
"""(want) => {
const replies = Array.from(
@@ -1844,7 +1994,7 @@ with sync_playwright() as p:
try:
refresh_status = int(refresh_proc.stdout.strip())
except ValueError:
- fail(f"curl refresh-token check returned invalid status: " f"{refresh_proc.stdout!r}")
+ fail(f"curl refresh-token check returned invalid status: {refresh_proc.stdout!r}")
if refresh_status == 200:
fail(f"/api/auth/refresh should fail after CLI rotation; got 200")
info(