Skip to content

studio: collapse the reasoning pane with grid rows instead of a measured height, behind a flag - #9423

Merged
danielhanchen merged 9 commits into
mainfrom
studio-grid-collapse
Aug 21, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
studio-grid-collapse

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

Collapsing the reasoning pane currently costs a synchronous full-document layout. This replaces the measured-height animation with a grid-template-rows transition, which needs no measurement. Behind a flag, defaulting off.

The mechanism

This is not a smoothness change. It removes a forced synchronous layout.

Radix's CollapsibleContentImpl runs this on every open change (react-collapsible 1.1.12, dist/index.mjs):

useLayoutEffect(() => {
  const node = ref.current;
  if (node) {
    node.style.transitionDuration = "0s";      // write
    node.style.animationName = "none";         // write
    const rect = node.getBoundingClientRect(); // READ -> synchronous layout
    heightRef.current = rect.height;
    widthRef.current = rect.width;
    ...                                        // write back
  }
}, [context.open, present]);

It publishes the result as --radix-collapsible-content-height, which the animate-collapsible-down / animate-collapsible-up keyframes consume.

Two facts make that expensive:

  1. The read is unconditional. It does not check whether any stylesheet actually references the variable it produces. So swapping the keyframes alone removes the consumer of the measurement and leaves the measurement itself exactly where it was.
  2. Blink's layout is O(total layout objects), not O(dirty objects). Measured on a long thread: 355.9 ms of layout for dirtyObjects=67 against totalObjects=195,631. The toggle dirties a handful of nodes and is charged for the whole document.

So every reasoning-pane toggle in a long thread forces a full-document relayout. A grid-template-rows transition from 0fr to 1fr animates without ever asking for a measurement: 1fr resolves against the content on its own, every frame.

Because point 1 means the keyframe swap is not enough on its own, this adds a local collapsible primitive (unmeasured-collapsible.tsx) that keeps Radix's public shape (data-state, data-disabled, aria-expanded, aria-controls, generated content id, hidden when closed, children unmounted while closed) and drops only the geometry read.

Measurement

Confirmed at the 100K rung, standard tier, n=8, against a concurrent in-band null control, scored through the three gates in tests/studio/studiobench/sweep/floor_table.py.

metric delta floor spread n verdict
reasoning_toggle.open_ms -24.8% 8.2% 9.5% 8 faster
jank_index -21.1% 9.2% 10.1% 8 faster

No regressions were scored. The three gates a number has to clear to be quotable: it must exceed the per-metric floor (max(abs(null delta), null spread)), the paired ratios must agree on sign, and the effect must exceed its own scatter.

An earlier n=4 run of the same comparison agreed on both metrics and on direction, at reasoning_toggle.open_ms -34.8% (floor 2.0%, spread 27.2%) and jank_index -25.0% (floor 12.0%, spread 23.2%). The n=8 run above is the one to quote.

One number deliberately not claimed: reasoning_toggle.close_ms read -22.4%, but its paired ratios disagree on sign, so it VOIDs on gate 2 and is not a result.

The floor is measured in band, concurrently with the comparison it judges, because session-to-session drift on this metric set is around 8%, which is larger than most real effects.

Scope, and why it is behind a flag

GRID_COLLAPSE_REASONING_ENABLED in thread-feature-flags.ts defaults to false. Both paths stay wired, so flipping that one constant is the only edit needed to enable it.

Off by default because the change alters the rendered DOM: the grid technique needs a wrapper with min-height: 0 (or the automatic minimum size floors the row at the content height and 0fr never reaches zero) and overflow: hidden (or content paints outside the zero-height row). That wrapper is one element more than Radix renders, so this wants screenshots as well as the parity digest before it goes on.

Scoped to the reasoning pane only. tool-group.tsx, tool-fallback.tsx and the shared components/ui/collapsible.tsx default still animate height, for two reasons: app-sidebar.tsx keys its scroll fade off onAnimationEnd with animationName === "collapsible-down" | "collapsible-up", which a transition does not fire; and changing three collapsibles at once would make an A/B on the reasoning toggle unattributable.

prefers-reduced-motion still reaches this path: index.css forces transition-duration: 0.01ms !important on every element, and this is a transition.

Reproducing the measurement

The studiobench harness is not on main yet, so this runs from the harness branch:

# 100K rung, standard tier
python -m tests.studio.studiobench --tier standard

# score a treatment payload against a concurrently captured null control
python -m tests.studio.studiobench.sweep.floor_table --floor OUT_NULL OUT_MINE

The treatment build is this branch with GRID_COLLAPSE_REASONING_ENABLED = true; the base is main. Both arms are paired within one session and interleaved, and the null control is captured in the same session as the comparison it scores.

For the mechanism itself, rather than the end-to-end effect, this PR also commits the probe driver:

python tests/studio/playwright_collapse_layout.py

It drives studio/frontend/smoke-collapse-layout.html, whose pane content is identical in every run so that only the surrounding document grows. It sweeps four arms and reports script-forced layouts separately from animation-frame layouts, which matters because height and grid-template-rows are both layout-inducing, so a raw layout count ranks the arms backwards. The radix-grid arm exists to be disproved: it takes the grid transition but keeps Radix's content, and it still forces the full-document layout, which is what shows a keyframe swap alone is not the fix.

Tests

studio/frontend/tests/reasoning-grid-collapse.test.ts adds 11 tests, covering that the primitive reads no geometry at all, that the animating child carries min-height: 0 and overflow: hidden, that the collapse is a 0fr to 1fr transition, that the accessible contract and unmount-while-closed behavior match Radix, that the close path unmounts on transitionend for the right property with a timeout backstop, that the flag is off by default, and that the height keyframes stay in use everywhere else.

Frontend green on this branch: npm run typecheck clean, npm run build clean, npm test 4237 passed / 0 failed.

…ight, behind a flag

The reasoning toggle forces a synchronous full-document layout. The cause is named, not
inferred: Radix's CollapsibleContentImpl runs a useLayoutEffect on every open change that
writes transitionDuration/animationName, reads getBoundingClientRect, and writes the styles
back, to publish --radix-collapsible-content-height for the animate-collapsible-* keyframes.
Blink's layout is O(total layout objects), so that one read is charged the whole thread.

Swapping the keyframes for a grid-template-rows 0fr -> 1fr transition is not on its own
enough, and this was measured rather than assumed. Radix's read is unconditional: it does not
check whether any stylesheet consumes the variable. A CSS-only arm still made two
getBoundingClientRect calls per toggle and its commit window still scaled with document size,
5.00 -> 12.40 ms across 4.2K -> 252K layout objects.

So the flag selects a local collapsible primitive as well. It keeps Radix's contract --
data-state, data-disabled, aria-expanded, aria-controls, the generated content id, hidden when
closed, children unmounted while closed -- and drops the measurement, which grid does not need
because 1fr re-resolves against the content every frame.

On the real reasoning pane, commit window, median of 5, Chromium:

                        4.2K objs   63K objs   252K objs   gBCR per toggle
  open   flag off         5.70 ms    8.10 ms    14.40 ms   2
  open   flag on          1.20 ms    1.20 ms     1.60 ms   0
  close  flag off         4.00 ms    4.60 ms     5.10 ms   1
  close  flag on          2.00 ms    1.90 ms     2.60 ms   0

Honest limit: this removes the synchronous forced layout, not every layout. The transition
itself still relayouts the document each frame, because a pane expanding in flow moves
everything below it. Total layout time per toggle falls about 2x, it does not collapse.

Scoped to reasoning. tool-group, tool-fallback and the shared collapsible default still animate
height, because app-sidebar keys its scroll fade off animationName === "collapsible-down" |
"collapsible-up", which a transition does not fire.

Flag defaults off. Flag-off renders byte-identical markup; flag-on adds one wrapper element
inside the content, which is the min-height:0 / overflow:hidden child the grid technique
requires, so it needs screenshots as well as the parity digest.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 20, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

pre-commit-ci Bot and others added 2 commits August 20, 2026 17:24
…ation

The backstop is armed in the same passive-effect flush that queues setExpanded(false),
so it starts counting before React commits the 0fr class and before the browser starts
the transition. Armed at exactly closeDurationMs it always beat transitionend and cut
the close short, by more when a busy main thread delayed the commit.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…grid path only

The height keyframes animate a height captured at toggle time, so releasing max-h-64
mid-animation cannot change what they animate. 1fr resolves against the live content
every frame, so the same release grows the row in the middle of the collapse and
produces the jump the retention timer exists to prevent. The transition also starts a
render after the timer is armed, so an exact ANIMATION_DURATION lands inside it. The
default path keeps its exact duration.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…e grid path only

The lock is armed in the click handler, a commit before the grid transition starts, so
an exact ANIMATION_DURATION releases the scroll container while the row is still
shrinking and lets the remaining height change shift the thread. tool-group and
tool-fallback share the hook but keep the height keyframes, so they keep the plain
duration.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 20, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

The page documented itself as a harness for scripts/probe-collapse-layout.py, which was
never committed, so the experiment could not be reproduced from the repository. The
driver follows the tests/studio/playwright_<name>.py convention the other smoke pages
use, sweeps the four arms across two document sizes, and separates SCRIPT-FORCED layouts
from animation-frame layouts: both are layout-inducing here, so a raw layout count ranks
the arms backwards. It sets no budget, because the cost is hardware-dependent and a
threshold would be flaky rather than informative.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…oreign tree for the flag

createRoot renders concurrently, so a large fillers cell can still be mid-mount two
frames into the initial render; the element count taken there reports a far smaller
document than the one measured, and that count is the x axis of the experiment. It is
now published from an effect after App and its filler tree have committed, and the
driver re-reads it when the measurement actually starts.

The flag reader also promised None under SMOKE_BASE_URL and never checked it, so a
bundle built from another checkout could be labelled with this working tree's flag.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 20, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 20, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 20, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 20, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 20, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 20, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: de1f2b479b

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

CI note: the red checks on this branch are all pre-existing or environmental, none of them reachable from what this PR changes. Recording the evidence so nobody re-diagnoses them.

GGUF inference smoke (x2) and Chat UI, API, Update and GGUF inference
TypeError: Messages.create() got an unexpected keyword argument 'temperature', raised from the "Multi-turn determinism via OpenAI + Anthropic SDKs" step. The Anthropic SDK is installed unpinned, and it went 0.125.0 to 1.0.0 between yesterday's runs and these:

Same script, same step, different resolved SDK. It will hit every PR that runs from now on, so it wants a pin or a call-site update in the workflow rather than a fix here.

Repo tests (CPU)
tests/studio/test_chat_autoload_failure_gate.py fails on ['clearedServerTuningState', 'committedServerTuningState', 'serverTuningLoadPayload'], imported by chat-adapter.ts but absent from the harness PREAMBLE, plus assert 6 == 2 on modelChanged: slotsModelChanged,. This PR touches none of those files. It reproduces on a staging branch cut from fresh main with this PR merged in, and it is red on #9424, #9421 and #9419 too.

pre-commit.ci - pr
"835 files reformatted, 65 files left unchanged", then "a conflict occurred when applying fixes to the pr". main is unformatted against the pinned ruff-format-with-kwargs hook, so the bot heals whichever PR runs next; that is where the six studio/backend/ files in this diff came from. Running scripts/run_ruff_format.py on a clean checkout of this branch reproduces the bot's commit byte for byte. Also red on #9425, #9424 and #9421.

What is green
studio/frontend: npm run typecheck clean, npm run build clean, npm test 4239 passed / 0 failed. Cross-platform on a staging replica: ubuntu-latest, macos-15, windows-latest and the studio Playwright workflow all pass on this head.

test_reasoning_keeps_streaming_height_cap_through_automatic_collapse pinned the literal
'isReasoningStreaming ? 0 : ANIMATION_DURATION'. The grid path has to outlast a
transition that starts a render after this timer is armed, so the delay moved into a
closeDelay local that is ANIMATION_DURATION on the height-keyframe path and
ANIMATION_DURATION + CLOSE_FALLBACK_MARGIN_MS on the grid path.

The contract is unchanged and is asserted directly: zero while streaming, the animation's
length once it stops, and the default path keeps its exact duration.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: abe9dab4ce

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


// Read `open` through a ref so the toggle identity does not change every time the pane opens.
const openRef = React.useRef(open);
openRef.current = open;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Update the toggle ref only after commits

When GRID_COLLAPSE_REASONING_ENABLED is enabled, assigning openRef.current during render can expose an uncommitted controlled state to the still-committed trigger. If React yields or abandons a concurrent render—for example while the reasoning stream changes open—a click on the currently displayed pane can negate that speculative value and call onOpenChange with the opposite of the visible state. Make the toggle callback close over the committed open value, or synchronize the ref in a layout effect, so the event handler and DOM commit cannot tear.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and confirmed against React's own guidance: refs must not be written during render, because a render can be abandoned or suspended and React does not roll the ref back, while the hook state around it is reset. The react-hooks refs lint flags updating a ref during render for exactly this reason. Fixed in 3bb3797 by closing the toggle over the committed open. Worth noting the ref was not buying what its comment claimed either: context is memoized on open, so it is rebuilt on every open change regardless of the callback's identity, which makes this a straight removal rather than a trade. A test now pins that no openRef comes back.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: abe9dab4ce

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…written in render

A render can be abandoned or suspended and React does not roll a ref back when that
happens, so a ref assigned in the render body can hold a value that was never committed
while the trigger on screen still shows the old one; the click would then toggle away
from the visible state. React's own guidance is not to write refs during render, and the
react-hooks refs lint flags exactly this.

The ref was not buying anything either: the context below is memoized on open, so it is
rebuilt on every open change whatever the callback's identity is.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 3bb3797772

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

Correcting my earlier CI note above, which said the red checks on this branch were all pre-existing or environmental and "none of them reachable from what this PR changes".

That claim was right about the causes it named and wrong as a conclusion, and I only found out by testing it instead of resting on it.

Repo tests (CPU) was already red on main for an unrelated reason (the #9410 fallout that #9437 repairs). A check that is already failing cannot tell you it started failing for a second reason as well. It was masking a real failure caused by this PR:

FAILED tests/studio/test_chat_response_details_ui_contract.py::
       test_reasoning_keeps_streaming_height_cap_through_automatic_collapse
AssertionError: assert 'isReasoningStreaming ? 0 : ANIMATION_DURATION' in ...

That test pins the streaming height cap's delay as a source literal. The fix for the cap release moved the delay into a closeDelay local, so the literal no longer matched. Mine, not main's. Fixed in abe9dab, with the contract asserted directly (zero while streaming, the animation's length once it stops, default path keeps its exact duration) rather than by pinning an expression that moves.

How it surfaced: rather than wait for the two main repairs to merge, I built main + #9432 + #9437 + this PR on a throwaway staging branch and ran full CI on it. The first run came back with Repo tests (CPU) still red, and that failure was this one. With it fixed the combined branch is 65 pass, 0 fail, including Repo tests (CPU), both GGUF inference smoke jobs and the frontend build.

So the substance of the earlier note stands, the three named causes are genuinely not this PR's, but the sentence a reviewer should trust is the staging result, not the argument. Worth recording plainly: a red shared check is not a neutral background, it is a place your own regressions can hide, and "unrelated" was not verifiable until the shared reds were repaired.

@danielhanchen
danielhanchen merged commit 47caced into main Aug 21, 2026
46 of 51 checks passed
@danielhanchen
danielhanchen deleted the studio-grid-collapse branch August 21, 2026 06:33
danielhanchen added a commit that referenced this pull request Aug 22, 2026
PR #9423 replaced Radix's measured-height collapsible with a local grid
primitive that never measures, and merged behind
GRID_COLLAPSE_REASONING_ENABLED defaulting off. The flag has been off ever
since, so nobody has been getting the improvement. This turns it on.

The flag file said OFF until the A/B has run, and asked for screenshots as
well as a parity digest because the change adds one wrapper element. Both
have now been done on current main.
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.

2 participants