Skip to content

Studio: stop dialog open and close from rescanning a long chat for every shortcut - #16983

Merged
danielhanchen merged 1 commit into
mainfrom
studio-dialog-scan
Nov 7, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-dialog-scan

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

On long chats, opening and closing dialogs gets slower with chat length on every platform. Most of the extra time is not rendering. While a dialog opens or closes, Radix toggles aria-hidden / inert and the dialog registers and drops its shortcut triggers. Each of those changes re-ran every useShortcutAvailable subscriber, and every claims() check queried the whole document: modal backdrop, find scope and open dialog content. At 150 turns that came to about 120 queries and 170-190 ms per Settings or shortcuts dialog close, against under 5 ms on a 1-turn chat.

Real runners at 150 turns: Edge Settings / shortcuts dialogs take 217-376 ms, about 2-3x their 1-turn cost. A CPU profile puts querySelector at 86 of 193 ms.

Fix

  • use-shortcut.ts: availability updates coalesce into one pass per microtask. Within a pass, each shortcut id and each isSurfaceInForeground / isSurfaceBackgrounded selector is answered once and shared, because nothing changes the DOM between two listeners. Press-time checks and render-time snapshots are unchanged and always read the live DOM.
  • vite-plugin-preload-nonce.ts: Vite's preload helper looked up the CSP nonce meta on every lazy import with deps, a whole-document miss each time (19 on Settings open). The lookup now searches only <head>, the only place that meta can be.

Results

150 turns, about 55.5k nodes, same host, base and head alternated each rep (Chromium 3 reps, Playwright WebKit with a mac UA 5 reps). Medians:

Surface Engine Time to visible (ms) Max frame (ms) Shortcut-check queries
Settings close Chromium 275 -> 164 200 -> 83 120 -> 30
Shortcuts close Chromium 272 -> 157 217 -> 83 120 -> 30
Settings open Chromium 157 -> 150 150 -> 133 44 -> 31
Settings close WebKit 440 -> 359 232 -> 139 120 -> 30
Shortcuts close WebKit 384 -> 293 210 -> 130 120 -> 30
Settings open WebKit 536 -> 481 329 -> 310 44 -> 31

Command palette and tools menu are unchanged within noise. The remaining ~30 queries per close come from render-time reads, which are left live on purpose.

Behaviour

  • In the live app, an 18-step shortcut walk behaves identically on base and head in both engines:
    • the palette chord works with and without Settings open;
    • Escape closes the palette, shortcuts dialog and Settings;
    • Mod+B toggles the sidebar and is ignored under Settings;
    • the find bar hides under dialogs and returns after Escape.
  • playwright_keyboard_shortcuts.py (Chromium + WebKit, macOS / Windows / Linux layouts, including Escape decline / approve): 232/232 on base and head.
  • playwright_find_in_page.py: 452/452 on base and head.

Tests

Opening or closing a dialog registers and drops a shortcut trigger per
action and fires several aria-hidden / inert observer batches. Each one
re-ran every useShortcutAvailable subscriber, and each claims() walked
the whole document (modal backdrop, find scope, open dialog content).
On a 150-turn chat that was about 120 such queries and 170-190 ms per
Settings or shortcuts close.

Notifications now coalesce into one pass per microtask, and inside a
pass each shortcut id and each isSurfaceInForeground /
isSurfaceBackgrounded selector is answered once. Press-time checks and
render-time snapshots are unchanged and always read the live DOM.

Also scope the Vite preload helper's CSP nonce lookup to <head>, where
the meta can only live; it ran a whole-document miss on every lazy
import with deps (19 on Settings open).
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-07T02:36:59.776402Z
We couldn’t complete this review. Comment @codex review to try again.
fc3a467 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 7, 2026
…ng (unslothai#16985)

Follow-up to unslothai#16931. The long-chat gate fails a PR on one reading of the
dialog/menu lock cost. On a shared runner that reading can catch the
page mid-settle.

## Problem

Staging for unslothai#16983 failed on `Inline images (windows-latest)`: the lock
check read 125 ms of style recalc per toggle at 200 turns, against a 30
ms budget. The same commit read 14.3 ms in `Composer (windows-latest)`
and 15-16 ms on Ubuntu and macOS. That page also read 0.2 ms/key of
typing style recalc (normally 8) and about 400 extra DOM nodes, so it
was not in its usual state. unslothai#16983 does not touch CSS.

## Fix

When the lock reading is over budget, measure the 200-turn page once
more on a fresh page and keep the lower value. A real regression reads
high twice, so it still fails. Runs under budget are unchanged and do no
extra work.

## Testing

- With `LONG_THREAD_MAX_LOCK_MS=1` the gate remeasures, prints
`dialog/menu lock 11 ms is over budget; measured again on a fresh page:
11 ms`, and fails as before.
- Under the normal budget the over-budget branch is never taken, so
passing runs are unaffected.
@danielhanchen
danielhanchen merged commit f5133de into main Nov 7, 2026
@danielhanchen
danielhanchen deleted the studio-dialog-scan branch November 7, 2026 04:28
rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Nov 7, 2026
… not the whole document (unslothai#17005)

## Problem

Every shortcut availability check (`claims()` on a press, and the
`useShortcutAvailable` snapshots the menus re-read) answered "is a modal
open", "is the find scope / sidebar / composer covered" with a
document-wide `querySelector` / `querySelectorAll`. On a long chat each
one walks the whole thread. unslothai#16983 coalesced the snapshot passes, but
every pass and every press still walked the document:

- Mod+B on a 150-turn chat: 11 document-wide queries (4 modal backdrop,
3 open dialog content, 4 `[data-find-scope]`), 33-102 ms on Chromium.
- Command palette open / close: 8 / 16 such queries. Rename chat open /
close: 10 / 13.
- At 1 turn the same queries cost about 1 ms, so the cost grows with the
chat.

## Fix

The elements those checks look for now register themselves, and the
checks read the registered elements instead of the document.

- New `src/lib/surface-registry.ts`: a refcounted element registry keyed
by surface, plus `useSurfaceRef(key, forwardedRef)`, a callback ref that
registers the element while mounted and still forwards the caller's ref.
- Producers register: Dialog / AlertDialog / Sheet overlays and
contents, the guided tour overlay, the browser overlay (`aria-modal`),
both blocking screens, the find scope, the sidebar (desktop, mobile
sheet, `collapsible="none"`), and both composer inputs.
- `isSurfaceInForeground` / `isSurfaceBackgrounded` (composer, sidebar,
find scope selectors), `isFindScopeBackgrounded` and the sidebar toggle
claim read the registry. Each check still evaluates the same selector on
each element (`el.matches(selector)`, `isConnected`), so `data-state`
flips and attribute changes are read live, as before.
- Anything mounted outside the app root (third-party portals such as the
streamdown fullscreen table, which sets `aria-modal="true"`) is still
queried, but only outside `#root`: about 220 nodes on a 55k-node chat.
- `main.tsx` turns the registry on for `#root`. Without that call (other
entries, unit tests with a fake document) every check reads the document
exactly as before.
- `SheetContent` now merges a caller's `ref` with its own instead of
dropping its own (no call site passed one before; the mobile sidebar
does now).

## Results

Local, same host, 150-turn chat (55k nodes) vs 1 turn, base
(origin/main) vs head interleaved, 3 reps x 2 cycles per arm, Chromium
and Playwright WebKit with a mac UA. The host was heavily loaded (load
average 120-160), so read counts and ratios, not absolute ms.

Shortcut-check document queries per action, 150 turns (same counts at 1
turn and on WebKit):

| action | base | head |
|---|---|---|
| Mod+B (toggle sidebar) | 11 | 0 |
| Mod+B back | 11 | 0 |
| palette open | 8 | 0 |
| palette close | 16 | 0 |
| rename open | 10 | 0 |
| rename close | 13 | 0 |

Time in those queries, 150 turns, median [range]:

| action | Chromium base | WebKit base | head (both) |
|---|---|---|---|
| Mod+B | 68.8 ms [33-102] | 39 ms [30-98] | 0 |
| palette open | 59.8 ms [35-88] | 26 ms [22-58] | 0 |
| rename open | 70.7 ms [37-81] | 38 ms [23-57] | 0 |

Chromium longest task, 150 turns / 1 turn (median of per-rep ratios),
and head / base at 150 turns:

| action | base long/short | head long/short | head / base at 150 |
|---|---|---|---|
| Mod+B | 4.20x | 1.88x | 0.73x |
| Mod+B back | 3.23x | 1.65x | 0.75x |
| palette open | 1.87x | 1.28x | 0.61x |
| palette close | 3.76x | 1.98x | 0.56x |
| rename open | 3.42x | 1.74x | 0.54x |
| rename close | 4.43x | 1.11x | 0.29x |

Elements restyled per action are unchanged (head / base 1.00x to 1.02x).
On WebKit the max frame head / base at 150 turns is 0.71x (Mod+B), 0.80x
(Mod+B back), 0.86x to 1.00x for palette and rename; the queries were a
smaller share of those frames, and these deltas are within the run to
run spread on this host.

Still document-wide on head and not part of this change: Radix's own
`[data-radix-focus-guard]` and the `aria-hidden` package's `[aria-live],
script` walk on modal open (1 call each).

## Behaviour

Unchanged. A 25-step script on the 150-turn chat (palette, shortcuts
dialog, Settings, rename, find bar; Mod+B, Mod+Alt+R, Mod+F, Mod+P and
Escape with and without each surface open) gives identical states on
base and head in every rep, Chromium and WebKit. Shortcuts that are
blocked under Settings, the palette, the shortcuts dialog and the find
bar are still blocked; the same ones still work.

## Tests

- New `tests/shortcut-surface-registry.test.ts`: on a fake 3000-row
chat, every check (find scope backgrounded, open dialog, composer
foreground, sidebar backgrounded) gives the same answer as the document
query while making zero document queries and walking no node inside the
root; covers Dialog / AlertDialog / Sheet open, closing (still mounted,
`data-state="closed"`), unmounted, an unregistered `aria-modal` portal
outside the root, and a detached registered element; refcounted,
idempotent unregister; the untrusted fallback still queries the
document; and a source scan that fails if any element in `src/` that a
check can match renders without `useSurfaceRef`. Fails on main.
- Updated source-pattern tests for the two changed claim lines, and the
toast-dismiss stubs for the new import.
- The shortcuts and find-in-page smoke harnesses turn the registry on
and register their composer and find scope, so the browser smokes run
the same path as the app.
- `npm test`: 15117 pass, 1 fail (the KaTeX stylesheet Vite test, which
also fails on main with a symlinked node_modules).
- `node scripts/typecheck.mjs --tests`: clean.
- `playwright_keyboard_shortcuts.py` (Chromium + WebKit, macOS / Windows
/ Linux): 232/232 on base and head.
- `playwright_find_in_page.py` (Chromium + WebKit): 452/452 on base and
head.
- `playwright_long_thread_gate.py`: PASS.
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.

1 participant