Skip to content

Studio: viewport-gated highlight for the executed Python script (follow-up to #7240) - #7363

Merged
danielhanchen merged 3 commits into
mainfrom
studio-python-script-lazy-highlight
Jul 23, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-python-script-lazy-highlight

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What

Keep the executed Python script visible in the chat tool card, with Copy and a Download button, and highlight it lazily so long transcripts stay fast.

  • The script source is rendered directly under the tool trigger instead of only inside the collapsible run/output section. That section is unmounted when a card is collapsed from history, so previously the script disappeared on reopen ([Bug] Python code not always output in Chat #7165); now it stays visible.
  • Adds a client-side .py Download button next to Copy (builds a Blob and clicks an anchor, the same pattern the image / audio / attachment / chat-export buttons use).
  • shiki syntax-highlighting runs only once the script scrolls near the viewport (IntersectionObserver, 200px margin). Until then a plain monospace placeholder shows the same source with matching padding, so there is no layout jump. Falls back to immediate highlight when IntersectionObserver is unavailable (SSR / tests).

Why the viewport gating

Rendering the script always (rather than only on expand) means its highlighting would otherwise run for every Python tool call on load. Measured with the bundled shiki (3.23.0, dual github themes, python):

Script size highlight time
~2 KB (typical) ~8 ms
10 KB (max, truncated) ~39 ms

Eager highlighting of a long agentic transcript would add ~170 ms (20 calls) to ~420 ms (50 calls) of main-thread work on load. Viewport-gating bounds it to the handful of cards actually on screen (~15-35 ms) regardless of transcript length. Live/streaming chat is unchanged (the running card is already open).

Notes

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

…d + viewport-gated highlight

Always show the executed Python script under the tool card (not only inside the
collapsible run/output section, which is unmounted from history), with Copy and a
client-side .py Download button, so the script stays visible on reopen (#7165).

The script is rendered eagerly, but shiki syntax-highlighting only runs once the
block scrolls near the viewport (IntersectionObserver, 200px margin); until then a
plain monospace placeholder shows the same source with matching padding, so there
is no layout jump. This bounds highlighting to the cards actually on screen instead
of tokenizing every script up front. Measured shiki cost is ~8 ms per typical 2 KB
script, so eager highlighting of a long agentic transcript (20-50+ Python calls)
would add ~170-420 ms of main-thread work on load; viewport-gating keeps it to the
few visible cards (~15-35 ms) regardless of transcript length. Falls back to
immediate highlight when IntersectionObserver is unavailable (SSR / tests).

@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: b6ae307645

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

) : (
// Same p-3 padding as the highlighted code block so there is no layout
// jump when shiki swaps in.
<pre className="m-0 whitespace-pre-wrap break-words p-3 font-mono text-xs text-muted-foreground">

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 Preserve the placeholder's padding before highlighting

When IntersectionObserver is available, the initial placeholder does not actually keep the promised p-3 padding: the parent at line 157 applies [&_pre]:!p-0, whose !important overrides this <pre>'s p-3. The highlighted Streamdown variant instead receives !p-3 on its code-block wrapper, so every lazily highlighted script shifts by the missing padding when it enters the viewport, defeating the no-layout-jump behavior in normal browsers.

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.

Good catch, fixed. The placeholder is now a div (not a pre), so the container's [&_pre]:!p-0 no longer strips its padding, and it keeps the same p-3 the highlighted code block gets. The content no longer shifts when shiki swaps in.

@danielhanchen
danielhanchen force-pushed the studio-python-script-lazy-highlight branch from b6ae307 to f3bfd7e Compare July 23, 2026 09:41
@danielhanchen danielhanchen changed the title Studio: keep the executed Python script visible in chat with download + viewport-gated highlight Studio: viewport-gated highlight for the executed Python script (follow-up to #7240) Jul 23, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: f3bfd7e32d

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

… doesn't strip its p-3

The placeholder shares the highlighted block's p-3 padding to avoid a layout
jump, but as a <pre> it was caught by the container's [&_pre]:!p-0 !important
rule and rendered with no padding, so the script shifted by p-3 when shiki
swapped in. A plain div keeps the padding.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 2a911f6a0b

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

// A div, not a <pre>: the container's [&_pre]:!p-0 would override a
// <pre>'s padding and shift the content by p-3 when shiki swaps in. This
// keeps the same p-3 the highlighted code block gets, so there is no jump.
<div className="whitespace-pre-wrap break-words p-3 font-mono text-xs text-muted-foreground">

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 Keep placeholder layout consistent with highlighted code

When a script contains a long line, this placeholder wraps and forcibly breaks that line, while the subsequent Streamdown <pre> preserves it and uses the container's horizontal overflow. As soon as the observer fires, a placeholder that may be at the max-h-48 limit can collapse to a single unwrapped line (or otherwise change height), shifting the transcript under the reader; this defeats the stated no-layout-jump behavior for common generated code containing long literals or serialized data.

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.

Fixed. The placeholder now uses whitespace-pre (not pre-wrap), so a long line scrolls in the container's overflow-auto exactly like the highlighted <pre> instead of wrapping taller and then collapsing when shiki swaps in. The block height stays stable across the swap.

…t pre-wrap)

The placeholder wrapped long lines while the highlighted Streamdown <pre> keeps
them on one line and scrolls in the container's overflow-auto, so a script with a
long line changed height when shiki swapped in. Use whitespace-pre so the
placeholder scrolls the same way and the height stays stable.
@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: e9f92fe1d8

ℹ️ 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
danielhanchen merged commit beddfc9 into main Jul 23, 2026
35 of 40 checks passed
@danielhanchen
danielhanchen deleted the studio-python-script-lazy-highlight branch July 23, 2026 09:55
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: beddfc963e

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

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