Skip to content

Keep composer plugin consumers off the typing render path - #4655

Merged
SawyerHood merged 1 commit into
mainfrom
perf/composer-draft-subscriptions
Oct 1, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
perf/composer-draft-subscriptions

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

usePromptDraftStorage returned the live draft, so every component that needed any draft operation re-rendered on each keystroke: the composers, the plugin composer host with its banners and actions, and, through draft-presence ids threaded into the group builders, the sidebar tree. useComposer() subscribed every plugin composer contribution to draft and selection changes whether or not it read them.

What changed

Split out of #4532 (commits 087cbc3 and aab5c06 together, applied on current main). Part 6 of 6. Independent of the suggestion warm-up PR; the two merge cleanly.

  • apps/app/src/hooks/usePromptDraftStorage.ts: a stable usePromptDraftController plus opt-in usePromptDraftSnapshot and usePromptDraftInputEmpty. Debounce, flush on pagehide/visibilitychange, the cross-tab storage listener, clearIfCurrentMatches and restoreIfEmpty are unchanged.
  • apps/app/src/components/plugin/plugin-composer-host.tsx, PluginComposerBanners.tsx, PluginComposerActions.tsx, promptbox/PromptBoxActionsMenu.tsx: static scope/layout/run view split from the draft context; banner rows and the action list are memoized.
  • apps/app/src/lib/plugin-sdk-hooks.ts: useComposer() subscribes to the draft only once the caller reads text, draft, isEmpty or attachmentCount, and to selection only once it reads selection. Returned values are unchanged and no public SDK member is added.
  • apps/app/src/components/promptbox/NewThreadComposer.tsx: only LiveDraftNewThreadPromptBox follows the live draft.
  • plugins/thread-list: collapsed rows read draft presence from one published draft-id set (list/sidebarDraftPresence.ts) instead of a draftThreadIds argument through every group builder.

Not included from #4532: the re-added addQuote (removed on main in #4474 and unused), the ThreadDetailPromptArea memo (needs stable props from another commit), and the scheduled-send formatter caching.

Review note: lazy tracking writes two refs during the plugin's render, which adds one react(refs) lint warning at plugin-sdk-hooks.ts. A draft change between the render that first reads a field and its commit is seen on the following change.

How you verified

  • pnpm exec turbo run typecheck lint --filter=@bb/app --filter=bb-plugin-thread-list: pass, 0 lint errors (app warnings 208 vs 207 on main, the one above).
  • pnpm exec turbo run test --filter=@bb/app --filter=bb-plugin-thread-list --concurrency=1: @bb/app 5,185 passed, 6 skipped; thread-list 443/443.
  • Two new tests in plugin-slot-mounts.test.tsx: a plugin that starts reading text/isEmpty only after a local toggle keeps updating, and a plugin that reads only useComposer().selection re-renders on picker changes. Both fail with tracking disabled (the state of 087cbc3 alone).
  • Not re-measured on this base. Speed up thread switching, resume, realtime, and composer #4532 measured the first thread keystroke at 4× CPU throttle at 222 ms → 118 ms on its older baseline.

🤖 Generated with Claude Code

AGENT GENERATED

Split the prompt draft hook into a stable controller and opt-in snapshot
hooks so composers, plugin hosts and the new-thread surface no longer
re-render on every keystroke. Only the prompt box itself follows the
live draft.

Plugin composer slots read the draft through a separate context from the
static scope/layout/run view, and `useComposer()` subscribes to draft or
selection changes only after the calling component reads `text`,
`draft`, `isEmpty`, `attachmentCount` or `selection`. Published hook
return values are unchanged.

The thread list reads draft presence per collapsed row from one
published draft-id set instead of threading it through every group
builder, so typing in one thread does not rebuild the sidebar tree.

Co-authored-by: hemaaanth <[email protected]>
Co-Authored-By: Claude Code <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-01T22:07:21.927891Z 47db53e 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.

@SawyerHood
SawyerHood merged commit a722262 into main Oct 1, 2026
21 checks passed
@SawyerHood
SawyerHood deleted the perf/composer-draft-subscriptions branch October 1, 2026 22:40
ymichael added a commit that referenced this pull request Oct 20, 2026
)

## Human comments

## What was wrong

On a phone, tapping a sidebar destination (New thread, a thread row, a
plugin panel, back/forward) started the drawer's 220 ms slide
immediately, while react-router rendered the destination as a
transition. The slide revealed the old page first. When the slide
settled, the drawer ran `flushSync(setOpenMobile(false))`, a sync update
that interrupted the still-pending navigation render, so the new page
appeared only after the drawer was already gone. The navigation render
itself was also slower than it needed to be:
- The compact new-thread page mounted every recent thread at once (79
rows, about 1,430 elements, in a list 4,818 px tall, with about 4 rows
visible).
- `useComposer()` built its `useSyncExternalStore` snapshot from a ref
that the plugin flips while it renders (by reading `composer.text`).
React's end-of-render tearing check therefore saw a changed snapshot,
threw the concurrent render away, and redid the whole tree
synchronously.

## What changed

- `components/ui/sidebar.tsx`: `closeMobileSidebar` sets the closing
state inside `startTransition`, so it shares the lane of a navigation
started in the same event and commits with it. The slide starts in a
layout effect of that commit. Closes that are not part of a navigation
(backdrop, Escape, trigger) commit on the next frame.
- `lib/app-route-history.ts`, `HistoryCommandHandlers.tsx`,
`SidebarHistoryNavigationControls.tsx`: `goBack`/`goForward` take an
optional callback that fires on the `popstate` that lands the traversal.
The drawer closes there, in the same commit as the route.
- `views/RootComposeMobileRecents.tsx`: renders one screenful of rows in
the navigation commit and the rest in a deferred render
(`useDeferredValue(true, false)`, which defers the first mount only).
- `lib/plugin-sdk-hooks.ts`: `useComposer()` snapshots read the draft
and selection directly. The "only re-render plugins that read the text"
behaviour from #4655 moved into the subscription callbacks.

## How you verified

Real data from the local bb, a 390×844 phone viewport, tapping New
thread from the open drawer on a 7,900-event thread:

| | main | this PR |
| --- | --- | --- |
| iOS Simulator Safari (iPhone 16 Pro, iOS 18.4, 5 runs): new page
committed | 126–184 ms (median 132) | 67–73 ms |
| iOS Simulator Safari: new page first painted | 201–306 ms (median
260), after the drawer has already revealed the old page | 138–143 ms,
revealed by the slide |
| Headless Chrome 1× (4 runs): committed | 87–100 ms | 53–66 ms |
| Headless Chrome 2× CPU slowdown (4 runs): committed | 162–174 ms |
90–92 ms |

On main, the slide starts at 1–11 ms. With this PR it starts in the
commit that shows the new page. A CPU profile of the branch shows no
tearing re-render, and the forced layout on the new-thread page dropped
from about 26 ms to about 5 ms at 1×.

Tests, each of which fails on main and passes here:
- `sidebar.test.tsx`: while a navigation is still suspended, the drawer
must not start sliding. It slides in the commit that shows the
destination.
- `plugin-slot-mounts.test.tsx`: mounting a plugin that reads
`composer.text` inside a transition renders the rest of the page once
(main renders it twice).
- `HistoryCommandHandlers.test.tsx` now runs on `BrowserRouter`, so
back/forward close the drawer on the real `popstate`.

`pnpm exec turbo run typecheck lint test --filter=@bb/app` passes: 6,113
tests, with no new lint findings.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> AGENT GENERATED

Co-authored-by: Claude Opus 5.5 <[email protected]>
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