Skip to content

Add a real-browser render invariant suite to CI - #7601

Merged
ymichael merged 2 commits into
mainfrom
bb/add-a-real-browser-render-invariant-suite-to-ci-thr_86pzjwtyfu
Nov 16, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/add-a-real-browser-render-invariant-suite-to-ci-thr_86pzjwtyfu

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Nothing in CI exercised rendering in a real browser. The existing render tests run in jsdom without the React Compiler and mock heavily, and two of them pin exact commit counts. The client perf audit found the sidebar re-rendering every row on any push, the composer prompt area re-rendering on every keystroke, and every finished timeline row re-rendering on each streamed delta; none of these were caught, and nothing would stop them returning once fixed.

What changed

  • tests/render-invariants (@bb/render-invariants): a Vitest suite that drives the compiled app in headless Chromium (playwright-core, pinned headless shell).
    • Fixture: the integration harness (real server and host daemon in process, scripted echo provider, fresh temp data dir) serves the profiling build and seeds through the public API: 500 sidebar threads in one project, a 40-turn timeline (markdown, code fences, tables, tool calls), a pending Ask User Question and a push-target thread. Seeding takes about 20 s.
    • Probe (render-probe.js): a React DevTools global hook that records every component that rendered in each commit and attributes it to a DOM region (sidebar row by a[data-sidebar-thread-id], timeline row by data-timeline-row-id, or a container). Assertions are sets of forbidden renders that must be empty; no counts, budgets or timings.
    • On in CI: idle 10 s renders nothing; typing an Ask User Question answer renders nothing outside the question form.
    • Pending (skipped unless BB_RENDER_INVARIANTS_PENDING=1, because main violates them): composer typing renders nothing outside the composer form; renaming thread X / a turn in thread X re-renders no sidebar row but X's; a streaming push re-renders no finished timeline row. Each owner thread enables its test with the fix.
  • apps/app: build:profiling (vite.profiling.config.ts) builds the normal config, React Compiler included, with react-dom/client aliased to react-dom/profiling, unminified, into dist-profiling/.
  • Scripted echo provider: a stream:<chunks>:<intervalMs> directive that streams one-line agent text deltas; covered by the bridge conformance suite.
  • CI: a Render invariants job (4 vCPU Linux, 20 min cap). scripts/plan-ci.mjs selects it for changes to apps/app, packages/{client-core,core-ui,plugin-sdk,shared-ui,thread-view}, plugins whose package.json declares bb.app, and the suite's own inputs; main pushes and manual runs always run it.
  • Turbo tasks: @bb/app#build:profiling, @bb/render-invariants#test:render-invariants, #typecheck, #serve (start the seeded fixture by hand).
  • Docs: docs/render-invariants.md (how it works, running locally, writing an invariant, the pending list); docs/ci-performance.md notes the selection.

No wire, CLI or user-facing configuration changes.

How you verified

  • Suite in CI mode, 5 local runs after the last fix: 2 passed, 4 skipped each time; the invariant run takes 40–50 s after a 40 s cold profiling build.
  • Each enabled invariant fails on the bug it guards (profiling build rebuilt with an injected regression, then restored):
    • idle: ThreadMachineStatus subscribing to useSecondTick → the page never settles and the failure names ThreadMachineStatus and the components it re-renders.
    • Ask User Question: the sidebar entries provider subscribing to bb.question-draft.changed (the mechanism suspected in the Discord report) → fails listing every mounted sidebar row; idle stays green.
  • Pending invariants on main (BB_RENDER_INVARIANTS_PENDING=1): all four fail on the audited bugs.
    • composer: 26 components outside the composer form re-render per keystroke (ThreadDetailPromptArea, FollowUpPromptBox, ComposerBox, the banner slots, VisibilityObserver twice).
    • rename / turn in X: every other mounted sidebar row re-renders (5 and 15 renders per row component).
    • streaming: all 14 finished mounted rows re-render 27 times between chunk 3 and chunk 30.
  • An earlier idle flake (1 in 3) came from the app's startup probe of the local host daemon helper, which the in-process fixture does not serve and which retries twice at 1 s; the page now settles for 3 s after load, and the suite then passed every run.
  • pnpm exec turbo run typecheck lint:type-aware --filter=@bb/render-invariants --filter=@bb/app --filter=bb-plugin-scripted-echo-provider, turbo run test --filter=bb-plugin-scripted-echo-provider, and packages/scripts/test/ci-selection.test.mjs (28 passed, including the new selection cases).

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 2 commits November 15, 2026 14:06
Drive the compiled app (React Compiler on, profiling React DOM) in headless
Chromium against a seeded in-process server and assert render invariants:
idle renders nothing, and typing an Ask User Question answer renders nothing
outside the question form. Composer typing, sidebar pushes and streaming are
added as pending invariants that main violates today.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit e46c91c into main Nov 16, 2026
52 checks passed
@ymichael
ymichael deleted the bb/add-a-real-browser-render-invariant-suite-to-ci-thr_86pzjwtyfu branch November 16, 2026 19:03
ymichael added a commit that referenced this pull request Nov 16, 2026
## Human comments

## What was wrong

A change to one thread re-rendered every mounted sidebar row, along with
each row's context-menu, dropdown and tooltip subtree. Rows subscribed
to whole-sidebar collections:
- the sidebar entries `Map` context;
- `SidebarProjectsContext`, used for a project-name lookup;
- `useThreadSelectionMenu` → `useSidebarData()`;
- the title-mention resources context (`threadById`).

On top of that, `useSidebarThreads` rebuilt every thread DTO whenever
those resources changed, and the thread-list model rebuilt every tree
node, DnD state object and sortable id array, so `memo(ThreadRow)` never
held. This came out of the Discord report of Ask User Question typing
lag with ~500 threads. Typing in the question form did not reproduce (0
sidebar renders on `main` and on v0.49.0); the sidebar fan-out the
reporter's agent described did. Repro and numbers are in the issue draft
in bb thread `thr_fhvv59u7d7`.

## What changed

- `apps/app/src/lib/sidebar-thread-entries.ts`: the provider and context
are gone. A per-QueryClient store reads the sidebar-navigation and
archived caches, falling back to the cached bootstrap like the query's
`placeholderData`, and notifies only the thread ids whose entry changed.
`useSidebarThreadEntry` is a keyed `useSyncExternalStore`.
`useSidebarThreadActions` and split-drag mentions look entries up when
they run, not during render.
- `plugin-sidebar-hooks.ts`: thread DTOs are cached on their entry plus
the derived host name and display title.
- `ThreadTitleMentions.tsx`: `ThreadTitleMentionsContent` reads the
resources with conditional `use()` only when the title can contain a
mention. A stable getter context lets `useSidebarThreadSplit` resolve
the drag label when the drag starts.
- thread-list plugin:
- tree nodes, items, environment groups and section groups are reused
when their thread and children are unchanged;
  - `collapsedThreadIds` no longer depends on `threads`;
  - rows get a project-name map instead of all projects;
  - selection targets live in the selection store;
  - `sectionDnd` and `SortableContext` ids stay stable while unchanged.
- DnD behavior change: while no drag is active, a reorder keeps the
previous sortable id order until a drag starts. dnd-kit only uses that
order during a drag. The pinned ids, which also drive render order,
always update.
- No wire, CLI or public SDK changes. Plugin SDK hook behavior is
unchanged apart from fewer re-renders.

## How you verified

Same 501-thread fixture on a `pnpm start:worktree` instance,
`react-dom/profiling` build, fiber-counting hook; 39 sidebar rows
mounted.

| Scenario | `main` | This PR |
|---|---|---|
| Rename another thread | 39 rows, 3,568 renders, 16–19 ms | target row
only*, 533 renders, 6–8 ms |
| Another thread runs a turn (and moves to the top) | 39 rows, 10,529
renders, 40–41 ms | target row only*, 1,277 renders, 15 ms |
| Type 20 chars in an Ask User Question answer | 0 rows, 1,070 renders,
7 ms | 0 rows, 1,070 renders, 7 ms |

*For a rename of a child thread, its parent row also re-renders, because
it shows its children's rollup.

Tests:
- New `PluginThreadList.row-isolation.test.tsx` uses the real
thread-list plugin with the app's title-mention resources. It checks
that a thread update renders only that thread's row, that typing in a
question form renders no row, and that a row still moves when its sort
order changes while idle. The first check fails on `main` with 59 rows.
- `sidebar-thread-entries.test.tsx`: changing one entry re-renders only
that entry's consumers; the cached bootstrap is read before the query
has data.
- `useSectionThreadDnd.projection.test.tsx`: the pinned order follows a
pin reorder while idle. This fails without the pinned exception.
- Rebased on main, which now includes #7601. This PR turns on the render
invariants "renaming thread X re-renders no sidebar row but X's" and "a
turn in thread X re-renders no sidebar row but X's", so CI runs them.
They fail on main, where all 41 measured sidebar rows re-render, and
pass on this branch.
- `pnpm exec turbo run test typecheck lint
--filter=bb-plugin-thread-list`, `turbo run typecheck lint
--filter=@bb/app`, and targeted `@bb/app` sidebar, plugin-title, overlay
and title-mention tests pass.
- Manual QA in the built app: after a turn reordered the list while
idle, I dragged that row into a section and dragged a pinned thread
above another. Both persisted (checked with `bb thread list`) and the
pinned order updated live.

> AGENT GENERATED

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Nov 17, 2026
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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Nov 17, 2026
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 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