Skip to content

Keep the timeline still when a prompt banner appears - #6488

Merged
ymichael merged 1 commit into
mainfrom
bb/fix-timeline-vertical-shift-on-tab-switch-thr_ecc9zp52eq
Oct 28, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/fix-timeline-vertical-shift-on-tab-switch-thr_ecc9zp52eq

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Switching to a thread whose environment has git changes made the timeline jump up about 35px for one frame and then drop back, at the moment the "Committed · N files" bar appeared above the composer. The follow-up composer is meant to absorb a collapsed banner: it shrinks its editor by the prompt stack's measured height, so the footer height stays the same. That shrink runs from the stack's ResizeObserver, which was created after the scroll keeper's observer and so ran after it. In that pass the keeper first scrolled to the end of the taller footer. The composer then shrank back. The browser does not deliver a resize of an element at or above the shallowest depth it already handled in the same pass, so the keeper never saw the shrink before paint. Its floor element still held the taller scroll height, so one frame painted the timeline and composer 35px too high, with a gap underneath. Hiding a banner did the reverse.

What changed

  • apps/app/src/components/ui/scroll-position-keeper.ts: the keeper's resize handling (sync the floor, then reconcile) is now a public remeasure(). The keeper's own ResizeObserver uses it.
  • apps/app/src/components/promptbox/FollowUpPromptBox.tsx: after the stack observer applies a new stack height with flushSync, it calls positionKeeper.remeasure(), so the keeper reconciles the final footer height before paint, whichever observer runs first.

No wire, CLI or doc changes.

How you verified

  • Reproduced in Chromium with a temporary Ladle story: a real PageShell bottom-anchored timeline, the real FollowUpPromptBox, and the git context banner shown or hidden through flushSync. Each frame I sampled the last row's top and the composer's bounds:
    • Before: when the banner appears, one frame has the last row at 306px instead of 341px and the composer bottom at 549 instead of 584 (the jump). When it hides, one frame has the last row at 376px.
    • After: the last row stays at 341px and the composer bottom at 584 on every frame in both directions.
  • Decoded the original screen recording frame by frame. The jump is the single frame where the git bar first renders.
  • Side-by-side builds with and without the fix, against the same pnpm start:worktree server and threads.
  • pnpm exec turbo run typecheck lint --filter=@bb/app passes. The FollowUpPromptBox, scroll-position-keeper and bottom-anchored-scroll-body suites pass (124 tests). No regression test: jsdom does not implement ResizeObserver depth ordering, which is what triggers this.

AGENT GENERATED

🤖 Generated with Claude Code

The follow-up composer shrinks its editor when the prompt stack grows, so a
collapsed banner like the git changes strip should not change the footer
height. The shrink ran from the stack's ResizeObserver, after the scroll
keeper's observer had already scrolled for the taller footer. The browser
skips the keeper's follow-up notification in that pass, so one frame
painted the timeline and composer 35px too high before snapping back.

Re-measure the keeper right after the stack resize is applied.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 991b990 into main Oct 28, 2026
30 checks passed
@ymichael
ymichael deleted the bb/fix-timeline-vertical-shift-on-tab-switch-thr_ecc9zp52eq branch October 28, 2026 22:06
ymichael added a commit that referenced this pull request Nov 17, 2026
## Human comments

## What was wrong

A `ResizeObserver` callback runs after layout and before paint, but
React applies a plain `setState` from that callback in a later task,
after the browser has painted. Any layout derived from a measured size
therefore painted one frame with the old layout at the new size. #7497
(context card), #5067 and #6488 (composer prompt stack) each fixed one
instance by hand with `flushSync`. A survey found about 30 more sites
with the same pattern: TOC show/hide, header action overflow, message
action row count, "Show more" toggles, File Preview header buttons,
panel size limits, resource toolbar, overflow fades, and others across
`apps/app`, `shared-ui`, plugins and `apps/web`.

Measured in headless Chrome, crossing a width threshold 10 times:

| How the measured size reaches React | Frames painted with the old
layout |
|---|---|
| plain `setState` in the observer | 10 of 10 |
| `flushSync` | 0 |
| `useSyncExternalStore` | 0 |

## What changed

- **One path for element sizes.** `observeSharedResize` moved from
`apps/app/src/lib` to `@bb/shared-ui/lib/shared-resize-observer`, which
also makes it a component-registry item for forkable plugins. It keeps
the one-`flushSync`-per-batch writes and `settleAfterSharedResizeWrites`
that #7612 added, so React state from a resize renders before paint. It
now also:
  - accepts a list of targets and runs each registration once per batch;
  - returns a no-op where `ResizeObserver` is missing.
- **`useMeasuredValue(target, read, initial)`** in
`@bb/shared-ui/hooks/use-measured-value` for the simple ref-to-value
case. It replaces usehooks-ts `useResizeObserver` in the git plugin.
- **Lint guard.** `bb/no-raw-resize-observer` in
`scripts/oxlint-plugin.mjs` rejects `new ResizeObserver` and usehooks-ts
`useResizeObserver` outside the shared module, apart from tests and
stories. Two files are exempt in `.oxlintrc.json` because their own
callback batching only writes the DOM and doesn't set React state:
- `scroll-position-keeper.ts`: the timeline engine's own observer, which
reconciles once per batch.
  - the landing hero feed animation in `apps/web`.
- **Migration.** 58 sites across `apps/app`, `shared-ui`, 13 plugins and
`apps/web` now use the shared path. Read steps only read layout; write
steps write the DOM and set state.
- The `flushSync` workarounds in the sidebar header slot and context
card are gone.
- The composer prompt stack keeps its inner `flushSync`, because
`positionKeeper.remeasure()` must run after the new height commits.
- #7612's new call sites (timeline row heights, `useIsOverflowing`,
markdown table geometry) import the moved scheduler.
- TOC visibility and the background-commands card now measure on first
paint instead of waiting for the observer's first callback.
- `ThreadDetailHeader` keeps a narrow/wide flag instead of the pane
width, so a divider drag no longer re-renders the header every frame.
- The component registry is regenerated: a new `shared-resize-observer`
item, and expandable-text, question-form, resource-pagination and
resource-toolbar now depend on it.

No wire, CLI or user-facing configuration changes.

## How you verified

- New tests in
`packages/shared-ui/src/lib/shared-resize-observer.test.tsx`:
- state written by a resize renders before the observer callback
returns;
  - `useMeasuredValue` measures on mount and on each resize;
  - a registration with several targets runs once per batch.
  - The first two fail with the `flushSync` batching removed.
- The lint rule flags the original code of the past fixes at the exact
line each one changed: `PluginThreadContextCard.tsx:371` (#7497),
`FollowUpPromptBox.tsx:668` (#5067) and `FollowUpPromptBox.tsx:685`
(#6488).
- `pnpm exec turbo run typecheck lint --filter='...[origin/main]'`:
180/180 passed.
- `pnpm exec turbo run test` on every affected package passed after the
fixes below:
- Two `@bb/app` tests failed because their `ResizeObserver` stubs called
the callback without entries; I fixed the stubs.
  - These failed under load and passed on a rerun alone:
    - `bb-plugin-connect` `connect.test.ts` (72/72)
    - four `@bb/server` files (131/131)
    - `CommandPalette.test.tsx`
    - `marketplace-exposure.test.tsx`
- `plugin-guide.test.ts` failed because an interrupted bundling run had
left a gitignored `plugins/.bundled-stage-*` copy of navigation.
Removing it made it pass.
- Live, with `pnpm start:worktree`, the experiment on and the context
card pinned, I recorded every resize frame while opening and closing the
right panel twice at 1440, 1600 and 2560px:
  - main before #7497: 1 stale frame per band crossing;
  - this branch: 0 at every width.

> AGENT GENERATED

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

---------

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