Skip to content

Subscribe plugin slot consumers to the slot kinds they read - #7609

Merged
ymichael merged 2 commits into
mainfrom
bb/plugin-slot-kind-subscriptions-thr_edin4mcbec
Nov 16, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/plugin-slot-kind-subscriptions-thr_edin4mcbec

Conversation

@ymichael

@ymichael ymichael commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

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

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

ymichael and others added 2 commits November 15, 2026 14:44
usePluginSlots() returned the whole slot snapshot, so registering any
plugin contribution re-rendered every consumer, including per-row ones
such as ThreadRowMetadata and the thread action registry.
usePluginSlotKind(kind) subscribes to one kind; the snapshot already keeps
each kind's array identity when another kind changes. Every consumer
moves to it except the plugin capabilities page, which lists every kind.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 3163ce3 into main Nov 16, 2026
33 checks passed
@ymichael
ymichael deleted the bb/plugin-slot-kind-subscriptions-thr_edin4mcbec branch November 16, 2026 19:32
ymichael added a commit that referenced this pull request Nov 17, 2026
#7602)

## 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 #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 #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

---------

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