Repository navigation
Give the composer a draft store so keystrokes stay inside the composer - #7602
Conversation
|
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
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 What it cost: working around it in the plugin (one 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
|
## 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]>
4ecd1b1 to
9105554
Compare
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]>
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]>
c257b03 to
c78247e
Compare
Human comments
What was wrong
ComposerBoxreceived the draft asvalue,mentionRangesandonChangeprops. 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,FollowUpPromptBoxandComposerBox.ComposerBoxalso rebuiltregisterEditoron every render, soComposerRootre-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: addsComposerDraftStore, which hasPluginComposerHost'sgetCurrent/subscribeDraft/setDraftshape, so any host is a store. It also addsuseComposerDraftSelector(a cacheduseSyncExternalStoreselector) andcreateComposerDraftStore.PluginComposerViewProvidernow 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: takesdraft: ComposerDraftStorein place ofvalue/mentionRanges/onChange.AttachmentsConfig.itemsandHistoryConfig.currentDraftare gone, because the box reads them from the store.history.currentDraft, the retiredviewmember 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.registerEditorandattachEditorkeep their identity across keystrokes, soComposerRootre-registers the editor only when the host, key, layout or editor state changes. The bridge'ssubmitreads the latest submission and voice state when called.FollowUpPromptBox,NewThreadPromptBox/NewThreadComposer,ComposerExtensionHostandusePromptModePermissionDisplaytake the store. Plan-mode detection is a boolean selector, usingplanPermissionDisplay, 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.historychanging keep working.[data-composer-box]) rather than the innerform, because the box's own host re-renders once per keystroke to hand plugins the current draft.ControlledComposerBox(insrc/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 withsrc/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: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 newhistorywhosecurrentDraftis the typed text. The once-per-keystroke check fails (6 renders for 3 keys) whenregisterEditorloses 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.bb-plugin-bb--composertests 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.