Skip to content

Give the composer a draft store so keystrokes stay inside the composer - #7602

Merged
ymichael merged 4 commits into
mainfrom
bb/composer-stable-api-thr_edin4mcbec
Nov 17, 2026
Merged

ymichael merged 4 commits into
mainfrom
bb/composer-stable-api-thr_edin4mcbec

Conversation

@ymichael

@ymichael ymichael commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

ComposerBox received the draft as value, mentionRanges and onChange props. The thread composer's owner, ThreadDetailPromptArea, subscribed to the whole prompt draft to provide those props. As a result, every keystroke re-rendered the prompt area, every banner (context, todos, workflows, machine status), the execution controls, FollowUpPromptBox and ComposerBox. ComposerBox also rebuilt registerEditor on every render, so ComposerRoot re-registered the editor from a layout effect. That republished the editor bridge and caused a second synchronous commit of the composer subtree (ComposerRoot, ComposerPluginActions, VoiceInputButton, MicrophonePreferencesPopoverContent, plugin slots). Found in the client perf audit (thr_qgyt9gkt9f).

What changed

  • plugin-composer-host.tsx: adds ComposerDraftStore, which has PluginComposerHost's getCurrent/subscribeDraft/setDraft shape, so any host is a store. It also adds useComposerDraftSelector (a cached useSyncExternalStore selector) and createComposerDraftStore. PluginComposerViewProvider now takes a static view plus a draft source. useOptionalPluginComposerView() derives the draft part through the selector, so only readers of the draft re-render.
  • ComposerBox: takes draft: ComposerDraftStore in place of value/mentionRanges/onChange. AttachmentsConfig.items and HistoryConfig.currentDraft are gone, because the box reads them from the store.
    • The box subscribes to the store itself. It builds history.currentDraft, the retired view member and the attachments from the current draft on every keystroke, exactly as before, so the plugin-facing renderer API behaves as it did on main. Only the box re-renders while typing.
    • registerEditor and attachEditor keep their identity across keystrokes, so ComposerRoot re-registers the editor only when the host, key, layout or editor state changes. The bridge's submit reads the latest submission and voice state when called.
  • FollowUpPromptBox, NewThreadPromptBox/NewThreadComposer, ComposerExtensionHost and usePromptModePermissionDisplay take the store. Plan-mode detection is a boolean selector, using planPermissionDisplay, which is now exported from client-core.
  • ThreadDetailPromptArea / useActiveComposerDraft: the main composer passes its prompt-draft host. The queued and sent-message editors pass their own hosts. The prompt area subscribes only to whether the draft has input.
  • No plugin API or SDK change. The composer plugin is untouched, and composer forks that react to history changing keep working.
  • Render invariants: "typing in the composer renders nothing outside the composer box" from Add a real-browser render invariant suite to CI #7601 is on in CI. Its region is the composer box ([data-composer-box]) rather than the inner form, because the box's own host re-renders once per keystroke to hand plugins the current draft.
  • Tests and stories: ControlledComposerBox (in src/test/fixtures) keeps the existing controlled-value editor tests running on the store API. One race test was rewritten to the production path, where the store write and the focus-end key land in one batch. The history navigation harness restores entries through a real draft store, as the thread and new-thread composers do. The new tests record renders with src/test/render-recorder.ts, which main already has from Subscribe plugin slot consumers to the slot kinds they read #7609.

The inline queued-message and sent-message editors still hold their draft in React state, so typing in them still re-renders their owner. Only the main and new-thread composers changed.

How you verified

Production build behind pnpm start:worktree, typing 20 characters into a thread composer with a React DevTools-hook recorder counting commits and component renders. Every run gave identical counts:

main this PR
Commits 40 (2 per key) 20 (1 per key)
Component renders 5,400 2,280
Renders outside the composer form 1,040 200

The 200 renders outside the form are 20 renders of ComposerRendererHostProvider (the box's host, once per key) and the composer plugin's own portaled dialog components.

Plugin compatibility: with main's composer plugin code, ArrowUp history works as on main. Recalling an entry, editing it, clearing the box and pressing ArrowUp recalls the newest entry. Editing a recalled entry, undoing the edit and pressing ArrowUp leaves the draft in place.

Tests (each fails when the regression it names is reintroduced):

  • composer-contract.test.tsx: on every surface (follow-up, embedded chat, queued, sent-message editor, steering, new thread), typing renders nothing outside the composer box, renders the plugin composer once per keystroke, and hands plugins a new history whose currentDraft is the typed text. The once-per-keystroke check fails (6 renders for 3 keys) when registerEditor loses its identity.
  • ThreadDetailPromptArea.keystrokes.test.tsx: once the draft has text, keystrokes render nothing outside the composer input.
  • PluginNewThreadComposer.test.tsx: typing re-renders neither the composer surface nor the prompt box.
  • tests/render-invariants: the composer typing invariant passes in a real browser; the suite passes with it on.

Commands:

  • pnpm exec turbo run typecheck --filter='...[origin/main]': 108 tasks passed.
  • pnpm exec turbo run lint --filter=@bb/app: 0 errors.
  • Affected app tests (promptbox, plugin, thread-detail, thread composer, RootCompose, plugin slots): 130 files passed.
  • bb-plugin-bb--composer tests passed.
  • pnpm exec turbo run test:render-invariants --filter=@bb/render-invariants: passed.

Manual check on the rebuilt dev instance: typing, Enter to send (composer clears, the reply arrives), ArrowUp history recall, editing a recalled entry, the sent-message editor (typing, Escape keeps the bottom draft) and typing in the new-thread composer all work.

AGENT GENERATED

@smsunarto

Copy link
Copy Markdown
Contributor

Numbers from the plugin side, in case they help check coverage. Measured on packaged 0.49.0 (Linux, headless Chrome) with a third-party composer plugin registered through experimental_composer and built without the React Compiler, using render counters inside the plugin's components.

Scenario on 0.49.0 Renders of each useComposer() caller
Type 20 characters in a thread composer 40 (twice per key)
One streamed turn, about 9 s, draft untouched 63

Callers that never read the draft got both renders too, so for them the second one is not the draft notifier. Reading the source, the first comes from the new boxHost object in PluginComposerHostProvider, and the second from the editor bridge: registerEditor is a new function each render, so ComposerRoot's layout effect re-registers and publishComposerEditorBridge notifies every useComposer() and useComposers() subscriber. useComposers() callers outside the composer (a timeline block, a navigation plugin) rendered twice per key as well.

What it cost: working around it in the plugin (one useComposer() reader that shares the handle through context, so the model and permission pickers skip typing) took typing from 6.0 to 4.24 ms of main-thread time per key. That is the median of 9 DevTools traces per build, 61 characters each, builds alternated.

The case this PR's table does not show is the streamed turn. The draft never changes there, so the draft store does not help; the renders come from ComposerBox re-rendering on pushes and re-registering the editor each time. I read #7603 as the fix for that, since it compiles useComposerRendererHostApi and registerEditor then keeps its identity, provided submission, onSubmit and voice are stable across pushes. I have not measured a build of either PR. A useComposer() caller that reads nothing, counted across a streamed turn, might be worth adding to the #7601 suite.

AGENT GENERATED

ymichael added a commit that referenced this pull request Nov 16, 2026
## Human comments

## What was wrong

`usePluginSlots()` subscribed to the whole plugin slot snapshot, and 57
consumers called it, including per-row ones (`ThreadRowMetadata`, the
thread action registry used by every thread row and menu) and
`ThreadDetailViewInternal`. Any plugin registration, removal or
re-registration (enable, disable, reload, hot reload) re-rendered all of
them, even when the change touched an unrelated slot kind. Found in the
client perf audit (thr_qgyt9gkt9f).

## What changed

- `lib/plugin-slots.ts`: adds `usePluginSlotKind(kind)` and exports
`PluginSlotKind`. `buildSnapshot` already keeps each kind's array
identity when another kind changes, so a consumer of one kind now
re-renders only when that kind changes.
- Every consumer moves to `usePluginSlotKind` for the kinds it reads,
including the hand-written per-kind `useSyncExternalStore` calls in
`ThreadTimelineRows` (four), `composer-slot-hooks.ts` and
`PluginTimelineRendererBody.tsx`, which now use the hook. The plugin
capabilities page keeps `usePluginSlots()`, because it lists every kind
for one plugin.
- Two tests that mocked `@/lib/plugin-slots` mock `usePluginSlotKind`
instead.
- `src/test/render-recorder.ts`: a test helper that records renders
through a React DevTools hook (identical to the copy in #7599, #7602 and
#7606).

## How you verified

Profiling build of the app (react-dom/profiling, compiler on) behind
`pnpm start:worktree`, with a thread view open. Test: `bb plugin disable
bb--github-star`, then `bb plugin enable bb--github-star`. Two runs
each:

| | main | this PR |
|---|---|---|
| Commits | 15 / 15 | 13 / 13 |
| Total renders | 3,511 / 3,511 | 2,613 / 2,613 |
| React `actualDuration` | 15.6 / 17.0 ms | 12.2 / 12.2 ms |
| `ThreadDetailViewInternal` renders | 6 / 6 | 4 / 4 |

Test added: `lib/plugin-slots.render.test.tsx`. Registering a plugin
that contributes only a settings section re-renders no
`ThreadRowMetadata` row. Registering row metadata re-renders each row.
The test fails when `ThreadRowMetadata` uses the whole snapshot again.

Commands:
- `pnpm exec turbo run typecheck lint --filter=@bb/app`
- After the follow-up commit: composer, timeline-renderer and
plugin-slot render tests (10 files, 106 tests).
- App tests under `src/components`, `src/lib`, `src/views` and
`src/hooks`: 661 files, 6,983 tests passed.

> AGENT GENERATED

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/composer-stable-api-thr_edin4mcbec branch from 4ecd1b1 to 9105554 Compare November 17, 2026 03:58
ymichael and others added 4 commits November 16, 2026 20:47
ComposerBox took the draft as value/mentionRanges/onChange props, so every
keystroke re-rendered the prompt area, its banners and execution controls,
rebuilt the composer renderer API, and then notified useComposer() readers
from a layout effect, which committed the composer subtree a second time.

ComposerBox, FollowUpPromptBox and NewThreadPromptBox now take a draft
store (getCurrent/subscribeDraft/setDraft, the same shape as a plugin
composer host). The main and new-thread composers pass the prompt draft
store, the queued and sent-message editors pass their hosts. The composer
reads attachments and plan-mode state through selectors, the view's draft
through the store, and history.currentDraft and the retired view member
through getters, so the renderer API keeps its identity while typing. The
prompt area reads only whether the draft has input.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ComposerBox subscribes to the draft store itself and builds history and
view from the current draft on every keystroke, as before, so composer
plugins that react to history changing keep working. Only the composer
box re-renders while typing; the prompt area and banners still do not.

registerEditor and attachEditor keep their identity across keystrokes,
so ComposerRoot no longer republishes the editor bridge on each key and
typing stays at one commit per keystroke. The bridge's submit reads the
latest submission state when called.

The composer plugin, the SDK contract comment and the SDK version bump
return to main. The history navigation test restores through a real
draft store, as the thread and new-thread composers do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Typing in the thread composer now re-renders only the composer box, so
the invariant from #7601 runs in CI. Its region is the composer box
rather than the inner form: the box's own host re-renders per keystroke
to hand plugins the current draft through the renderer API.

With the streaming invariant already on, nothing is pending, so the
unused pendingInvariant helper goes and the docs show how to declare a
pending invariant inline.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/composer-stable-api-thr_edin4mcbec branch from c257b03 to c78247e Compare November 17, 2026 04:50
@ymichael
ymichael merged commit 51b7647 into main Nov 17, 2026
52 checks passed
@ymichael
ymichael deleted the bb/composer-stable-api-thr_edin4mcbec branch November 17, 2026 04:56
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