Skip to content

Timeline: one attach rule, and one landing decision for unread threads - #6253

Merged
ymichael merged 9 commits into
mainfrom
bb/debug-timeline-scroll-regressions-thr_9mxqmidzcg
Oct 26, 2026
Merged

ymichael merged 9 commits into
mainfrom
bb/debug-timeline-scroll-regressions-thr_9mxqmidzcg

Conversation

@ymichael

@ymichael ymichael commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Scroll-to-bottom button shows while at the bottom. Collapsing a tool call near the end, while scrolled up a little, clamps the view to the end, but the keeper stayed detached. It attached only through event-specific rules, and #5951 deliberately never attached on a content shrink. On touch the clamp usually falls inside the 3s momentum window after a drag, so that path missed too. Every attach bug since #5951 has had this shape: the view reached the end through a path no rule covered.

Unread threads did not open at NEW. Where the view landed on open came out of five independent pieces: the read-state snapshot, the loaded rows, the divider's own one-shot scroll, the saved-position restore, and the keeper. Each assumed different timing:

  • Clicking an unread thread in the sidebar supplied placeholder data to automatic read tracking before the opening snapshot was ready. Marking it read erased the unread cutoff before the divider could capture it.
  • Reopening a thread first renders a cached copy that still reads as read. When the fresh copy made the thread unread, the divider was treated as a live reply and not revealed.
  • The first page holds the latest 8 segments on phone and 20 on desktop, and every user-side row starts a segment, including messages from other agents. When the cutoff was older than that page, NEW sat above the oldest loaded row, and then moved as older rows loaded.
  • The reveal used estimated heights for windowed rows. Rows rendering around it pushed NEW down the screen.
  • A thread marked unread put NEW above the oldest loaded row, which in paged history is not the thread's start.

These predate #5951: the pre-#5951 build fails these cases as well.

What changed

Two structural changes replace the per-case rules.

One attach rule (scroll-position-keeper.ts). A detached view within the 4px threshold of the end attaches, whatever put it there, unless a restore is still waiting for its row.

  • It attaches at once when the reader's scroll, a navigation, or a completed restore put it there.
  • It attaches on the next frame when only layout did, so content that shrinks and regrows within a frame does not attach.
  • This replaces the wheel-down, touch-drag, End/PageDown and resize/restore-at-end rules. The touchmove listener goes too.
  • Moving up out of the end still detaches, and content changes still never detach.
  • A view the reader never placed, such as a tool output box that was not streaming when it opened, is attached by layout only when the reader or a navigation brings it to the end, as on main. Otherwise output loading in after the box opens would pull it down.

One landing decision.

  • useThreadUnreadDividerState takes its snapshot from the first fresh copy of the thread (hasFreshThread from useThreadDetailThread), never cached or placeholder data.
  • The same hook owns automatic read tracking and waits until the opening snapshot is ready before marking the thread read. Later refreshes retain that readiness, preserving normal manual-unread behavior.
  • TimelineRowsList decides once, against the loaded rows, whether the cutoff is inside them. If it is not, there is no divider, no automatic paging back, and it stays dropped when the reader pages back by hand.
  • If it is, the list lands through the keeper's existing restore path, with the divider as the anchor. The divider carries data-scroll-landmark-id, which the keeper can now anchor to like a row. The keeper then holds NEW at the top while rows render around it. A reader who already scrolled keeps their position.
  • Threads marked unread (no read time) get no divider and open like a read thread. The before-first placement is removed; ThreadTimelineUnreadDividerPlacement is now { cutoffAt }.
  • Removed: the divider's own scroll effect and retry loop, and scrollElementIntoViewClampedToMaxScroll (only the divider used it).

docs/timeline-pagination.md documents the attach rule and the landing decision. Client only; no wire, CLI or plugin API change.

How you verified

Latest sidebar-opening fix:

  • Reproduced in the worktree app by clicking an unread thread in the sidebar: NEW was absent and the timeline opened at the bottom.
  • Extended the existing randomized opening test to include cached sidebar placeholders and the real automatic read-tracking hooks and mutation. The test failed before the fix with “no NEW although the read point is in the first page” and passed after it.
  • pnpm exec turbo run test --filter=@bb/app -- src/views/thread-detail/thread-opening.model.test.tsx src/views/thread-detail/useThreadUnreadDividerState.test.ts src/hooks/useThreadReadTracking.integration.test.tsx: 14 tests passed, including 40 randomized opening sequences.
  • pnpm exec turbo run lint typecheck --filter=@bb/app: passed; lint reports warnings, no errors. git diff --check passed.
  • Rebuilt with pnpm start:worktree and repeated the actual sidebar click in Chromium. NEW remained visible after the thread was marked read and landed within 1px of the viewport top. Server and daemon health checks passed.

Earlier branch verification (before the sidebar-opening fix):

  • Keeper tests:

    • attaches after a persisting shrink;
    • attaches after a collapse during momentum;
    • stays detached through a one-frame shrink;
    • attaches after a viewport grow, a restore landing, or a navigation at the end;
    • lands on a landmark and holds it at the top while a row above it resizes.
    • The touch-drag-at-end test was removed with its rule; the one-rule tests cover that case.
  • Divider tests:

    • snapshot from the fresh copy after reopening on cached read state;
    • no reveal for a reply after the thread settled;
    • a thread marked unread gets no divider;
    • a cutoff outside the loaded rows is dropped and stays dropped after older rows load.
  • New randomised test scroll-position-keeper.model.test.ts.

    • What it drives: random sequences of wheel and touch scrolls, row toggles held at the header, streaming, removals, prepends, regroups, one-frame shrinks, viewport resizes, navigations, restores and jumps to the bottom. It runs with and without iOS write deferral, and fires the browser's clamp scroll both before and after the resize callbacks.
    • What it checks after every step:
      • attached exactly when the view is at the end;
      • content changes never detach;
      • the reading row never moves without the reader, unless the content cannot hold it;
      • a reader's scroll is never moved.
    • On main's keeper, 75 of 300 sequences fail with "detached while at the end": 43 triggered by rows removed at the end, 13 by a toggle, and 19 in tool-output-box configuration.
    • On this keeper, 0 of 5,000 fail. CI runs 200 sequences (about 7s); set KEEPER_MODEL_SEEDS for deeper runs.
  • New randomised opening test thread-opening.model.test.tsx.

    • What it renders: the timeline with the thread view's own loading path. That covers the bootstrap and thread queries (moved into useThreadDetailThread, which ThreadDetailView now uses), the divider state, the timeline controller, the scroll body and the keeper.
    • The fake server pages a random history the way the real one does.
    • Each run picks a fresh open or a return after random time away, a random read state, phone or desktop page size, and a random order for the bootstrap, thread and timeline responses. Sometimes the reader scrolls while they load.
    • What it checks: NEW sits above the first unread row of the whole history; it is at the top unless the reader scrolled or the view is at the end; it shows whenever the cutoff is inside the first page; otherwise the saved position or the bottom stands.
    • Coverage over 200 runs:
      • NEW landed at the top 43 times (16 returns, 22 phone), with the view at the end 53 times;
      • opened at the bottom 70 times, and at a saved or scrolled spot 25 times;
      • the reader scrolled while loading 18 times.
    • Reintroducing each opening bug fails it:
      • trusting the cached thread copy (NEW left 2513px down after a return);
      • NEW above the first loaded row;
      • the one-shot reveal (NEW 156px above the top once real heights replace estimates).
    • CI runs 40 sequences (about 8s); set OPENING_MODEL_SEEDS for more.
  • pnpm exec turbo run typecheck lint test --filter=@bb/app: 647 files, 6312 tests passed.

  • Real-browser A/B against a live server: headless Chromium; phone runs at 402px with touch emulation. Each build was served through a proxy that rewrote an idle 46-segment thread's read state. "back" means switching to another thread and returning. Where NEW landed, relative to the viewport top ("bottom" = attached at the end with no divider):

    Cutoff Size main fresh / back branch fresh / back Expected
    After the last message desktop 0 / -231 0 / 0 NEW at top
    After the last message phone 0 / -733 0 / 0 NEW at top
    2 segments back desktop 1 / -2627 0 / 0 NEW at top
    2 segments back phone 0 / -4587 0 / 0 NEW at top
    26 segments back desktop -4512 / -15548 bottom / bottom bottom
    26 segments back phone -12197 / -10604 bottom / bottom bottom
    Marked unread desktop -16086 / -15629 bottom / bottom bottom
    Marked unread phone -14405 / -10604 bottom / bottom bottom
  • Collapse bug A/B:

    Scenario main branch
    Desktop: wheel up 40px, then collapse at end, detached (button shows) attached
    Phone: touch drag 60px, then tap to collapse at end, detached attached
    Wheel up 200px / drag 250px, then collapse stays detached, 106 / 157px from end same, header in the same place
  • Guards, identical on main and branch:

    • wheeling back through history: 24 steps, 0 jumps;
    • switching away and back restores the same row at the same offset (-358);
    • a mid-thread toggle keeps the header fixed (443 → 443 → 443);
    • scrolling up 300px detaches and shows the button, and scrolling back down re-attaches and hides it.

AGENT GENERATED

ymichael and others added 6 commits October 24, 2026 17:21
…e end

#5951 dropped shrink re-attach, so a reader who scrolled up a few pixels to
collapse a tool call landed at the very bottom but stayed detached, with the
jump-to-bottom button showing. Attach once the clamp to the end persists into
the next frame; a one-frame shrink still keeps the reader on their row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sture

The previous commit only attached through the clamp-settle path, which a
touch drag's three-second momentum window bypasses: the browser's clamp was
folded in as the reader's own scroll and re-anchored at the end. On phones,
dragging up to a tool call and tapping it closed left the view at the bottom
with the jump button showing. Any reconcile in which the content shrank and
the detached view sits at the end now attaches on the next frame if it is
still there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Opening an unread thread could leave NEW mid-screen or off-screen instead
of at the top:

- Reopening a thread showed its cached copy first. The detail query is only
  live while the thread is open, so that copy still read as read, and the
  fresh fetch's divider was treated as a live reply with no reveal. The
  thread now counts as opening until its post-mount fetch settles.
- When the cutoff was older than the first loaded segment, the divider sat
  above the oldest loaded row, the reveal went there, and the divider moved
  once older rows loaded. The list now holds the divider back and loads
  older rows until the cutoff is covered.
- The reveal ran once against estimated heights for windowed rows; rows
  rendering around it pushed NEW down. It now repeats until the divider's
  position is stable, like search reveals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A thread with no read time, which only Mark as unread produces, placed NEW
above the first loaded row. With paged history that is not the thread's
start, so the view landed mid-thread with NEW off-screen once older rows
loaded. Such threads now get no divider and open like a read thread; the
unread dot is unchanged. The before-first placement is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Instead of loading older pages until the read cutoff is covered, the
divider is placed only when the cutoff falls inside the rows loaded when it
is first evaluated. Otherwise the thread opens at the bottom with no
divider, and that placement stays dropped, so paging back by hand later
neither shows NEW nor pulls the view to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he keeper

Attachment: the keeper attached from seven places, most tied to an event
(wheel down, touch drag, End/PageDown, resize or restore at the end, a
collapse that shrank content). Each new way of reaching the end needed a new
rule. Now one rule covers them: a detached view within the bottom threshold
attaches, unless a restore is still waiting for its row. It attaches at once
when the reader's scroll, a navigation, or a completed restore put it there,
and on the next frame when only layout did, so a shrink that regrows within a
frame does not attach.

Landing: opening an unread thread was decided by five independent pieces (the
read-state snapshot, loaded rows, the divider's own scroll effect with a
retry loop, the saved-position restore, and the keeper). Now the divider
snapshot is taken from the first fresh copy of the thread, placeability is
decided once against the loaded rows, and the landing goes through the
keeper's restore path with the divider as a landmark anchor. The keeper then
holds NEW at the top while rows render around it. This removes the divider's
scroll effect, the frame-retry loop, the opening flag, and
scrollElementIntoViewClampedToMaxScroll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ymichael ymichael changed the title Keep the timeline attached after collapses and open unread threads at NEW Timeline: one attach rule, and one landing decision for unread threads Oct 25, 2026
ymichael and others added 3 commits October 24, 2026 20:25
…tach

scroll-position-keeper.model.test.ts drives the keeper through random
sequences of wheel and touch scrolls, row toggles, streaming, removals,
prepends, regroups, one-frame shrinks, viewport resizes, navigations,
restores, and jumps to the bottom, with and without iOS write deferral and
with the browser's clamp scroll before or after the resize callbacks. After
each step it checks that the keeper is attached exactly when the view is at
the end, that content changes never detach, that the reading row does not
move without the reader unless the content cannot hold it, and that a
reader's scroll is not moved. On main's keeper 75 of 300 sequences end
detached at the end; on this keeper none of 5000 do.

The test also showed the one attach rule would pull a tool output box that
was not streaming when it opened down to the bottom as its output loaded
in, which main never does. Layout alone now attaches only a view with a
reading position; a view the reader never placed still attaches when the
reader or a navigation brings it to the end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
thread-opening.model.test.tsx renders the timeline with the thread view's
own loading path: the bootstrap and thread queries (now shared with
ThreadDetailView through useThreadDetailThread), the divider state, the
timeline controller, the scroll body, and the keeper. A fake server pages a
random history the way the real one does. Each sequence opens or returns to
the thread with a random read state, page size, and time away, resolves the
requests in a random order, and sometimes scrolls while they load. Once
settled, NEW must sit above the first unread row of the whole history, be at
the top unless the reader scrolled or the view is at the end, and show
whenever the cutoff is inside the first page; otherwise the saved position
or the bottom stands.

Reintroducing each opening bug fails it: trusting the cached thread copy
(NEW left 2513px down after a return), placing NEW above the first loaded
row, and the one-shot reveal (NEW 156px above the top once real heights
replace estimates).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ymichael
ymichael merged commit 3c9af6e into main Oct 26, 2026
52 checks passed
@ymichael
ymichael deleted the bb/debug-timeline-scroll-regressions-thr_9mxqmidzcg branch October 26, 2026 17:22
ymichael added a commit that referenced this pull request Oct 26, 2026
…final assistant message (#6327)

## Human comments

## What was wrong

When finished turns are flat (Claude Code's default), the unread NEW
divider went above the first row after the read point. For a turn that
ran while the reader was away, that row is the turn's first tool call.
Opening the thread landed on "Listed files in ." and the reader had to
scroll through every tool call and note to reach the answer. With
collapsed turns the same rule put NEW above the "Worked for …" row.
That's harmless there, but it isn't where reading resumes either.

## What changed

New experiment `unreadDividerAtAnswer` (Settings → Experiments → **NEW
divider before final assistant message**, or `bb settings experiment
unreadDividerAtAnswer true`). It is off by default and stays off for
early adopters.

- `ThreadTimelineUnreadDividerPlacement` gains `anchor:
"first-unread-row" | "turn-answer"`. `useThreadDetailThread` sets it
from the experiment, and `useThreadUnreadDividerState` keeps it in the
opening snapshot, so the landing logic from #6253 is unchanged.
- With `turn-answer`, `findUnreadDividerIndex` starts from the same
first unread row and moves forward to the first row where reading
resumes:
- the final assistant message of a settled turn (the last assistant row
with that turn id);
  - an error that ended the turn;
- a message that starts a new turn, such as an agent report (steers
excluded);
  - any row of a turn with no reply.
- Setup system rows such as "Provisioned thread" are skipped. The
running turn (when `scopeActive`) keeps the old placement, so NEW never
jumps onto a streaming reply. If nothing qualifies, it falls back to the
first unread row. NEW still never sits on the reader's own message.
- With `first-unread-row` (experiment off), placement is unchanged.
- Docs: `docs/configuration.md` (Experiments) and `bb guide
customization`.
- Client and settings only. No wire change and no
`HOST_DAEMON_PROTOCOL_VERSION` bump. The experiment key is added to
`@bb/domain`, so the existing experiments route and CLI accept it.

## How you verified

- New unit cases in `ThreadTimelineRows.unread-divider.test.ts`
(turn-answer anchor):
  - final answer of a flat turn and of a collapsed turn;
  - setup rows skipped;
  - an agent message that starts a turn;
  - an error that ended the turn;
  - a turn with no reply;
  - a running turn;
  - a partly seen work group;
  - the reader's own message.
- `main`'s existing cases still run against the `first-unread-row`
anchor.
- `pnpm exec turbo run typecheck lint --filter='...[origin/main]'`:
146/146 tasks.
- `turbo run test --filter=@bb/app`: 6,332 tests. `--filter=@bb/domain`:
259. `apps/server` `test/system`: 237, including the experiment
defaults, PUT and early-adopter tests.
- Real app on `pnpm start:worktree`:
- Threads were real Claude Code and Codex turns spawned with the dev
`bb` CLI.
- Compared against an `origin/main` app build served on the same server
and data, with the same read point. Each thread was opened from the
sidebar at 1280×800, and the row after NEW was read from the DOM.

| Build | Experiment | Thread | Row after NEW |
|---|---|---|---|
| `origin/main` | n/a | Claude Code (flat) | Listed files in . (first
tool call) |
| branch | off | Claude Code (flat) | Listed files in . |
| branch | on | Claude Code (flat) | Final assistant message (step note
+ summary) |
| `origin/main` | n/a | Codex (collapsed) | Worked for 24s |
| branch | off | Codex (collapsed) | Worked for 24s |
| branch | on | Codex (collapsed) | The numbered answer |

- Regression checks on the branch with the experiment on:
- a thread marked unread (`bb thread unread`) and a read thread both
open with no NEW, as on `main`;
  - the Settings switch flips the server value both ways;
  - turning on the early-adopter setting leaves the experiment off;
  - the CLI rejects `maybe` and a misspelled key.
- Not changed here: a turn that finishes while its thread is open can
still show NEW afterwards. `main` does the same, and the branch only
moves that divider to the reply.

> AGENT GENERATED

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

Co-authored-by: review <review@local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ymichael added a commit that referenced this pull request Oct 27, 2026
)

## Human comments

## What was wrong

A thread scrolled to the bottom unpinned itself with no input: the view
jumped up by about the composer's height and the ↓ button appeared
([Discord
report](https://discord.com/channels/1522478522332090528/1564676088465199184/1564676088465199184):
"jumps up 225 pixels after a few idle seconds", iPad and iPhone). The
timeline composer footer is a `sticky` element inside the scroll
content. When the footer or a row collapses and regrows within one task
while something forces a layout, the browser clamps `scrollTop` to the
shrunken extent. The content regrows before the frame ends, so no
ResizeObserver fires. The clamp scroll then arrives with no input and no
size change, which the keeper reads as navigation, so it detaches. #6253
did not change this path: its attach rule only applies within 4 px of
the end.

A second source of the same no-input scrolls: `buildMarkdownComponents`
defined `MarkdownLink`, `MarkdownCodeRenderer`, image/video/source and
the mention elements as closures. Whenever link routing or mentions
changed identity, every link, inline code element and pill in every
message remounted. On main this happens twice on every thread open (52
code elements each time in the fixture) and again on background events.
iOS WebKit moves the scroller when links remount, as found while working
on #6253.

## What changed

- **Scroll extent floor** (`scroll-position-keeper.ts`,
`bottom-anchored-scroll-body.tsx`): the first child of the timeline's
scroll content is an invisible spacer. Its height is the content's
height at the last ResizeObserver delivery, cancelled by an equal
negative `margin-bottom`. It takes no space, but the scroll extent can
no longer drop for a one-task collapse, so the browser has nothing to
clamp. A shrink that lasts reaches the ResizeObserver, which lowers the
floor before reconciling. A scroll with no input now counts as layout
when the extent changed or when the content height no longer matches the
floor. This preserves the existing rule for native scroll adjustments
that come with a size change. The keeper's detach rule is otherwise
unchanged, so find in page and focus moves still navigate. Tool output
boxes pass no floor.
- **Stable markdown components** (`markdown-preview.tsx`,
`markdown-thread-mentions.tsx`, `markdown-prompt-mentions.tsx`,
`QueuedMessagesList.tsx`): the components are module-level and read
per-message options (link routing, image policy and offset, theme,
localhost rewriting, mentions) from context. Option changes re-render
them instead of remounting them. The components map depends only on
table layout and which mention and directive kinds are present.
`buildThreadMentionComponent` and `buildPromptMentionComponent` are
replaced by `ThreadMentionElement` and `PromptMentionElement` with their
contexts.
- **Test layout** (`test/fake-flow-layout.ts`): honors `margin-bottom`,
counts overflowing boxes toward `scrollHeight`, and clamps `scrollTop`
when the extent shrinks, as browsers do. `takeClamp()` reports a clamp
so harnesses can fire its scroll event. Tests that stood in for a
ResizeObserver delivery by calling `reconcile()` now flush the
observers.
- `docs/timeline-pagination.md` documents the floor.

Client only; no wire, CLI or plugin API change.

## How you verified

- **iOS 26.5 Simulator Safari, iPad Pro 11 and iPhone 17 Pro, dev
server.** I used a fixture like the reporter's (long message with inline
code, worktree environment with the git "Committed" bar) and a
probe-injecting proxy. Collapsing `[data-scroll-footer]` for one task
(hide it, read `scrollHeight`, restore it):
- main: the view jumped 203 px on iPad and 113 px on iPhone, exactly the
footer height, and the button appeared and stayed. Collapsing the last
row jumped the full distance.
- branch: `scrollHeight` and `scrollTop` were unchanged and the view
stayed pinned, on both devices and for both footer and row.
- **Markdown remounts on load** (probe counts `<a>` and `<code>` removed
inside rows):
- main: 52 code elements twice per open, or 91 links plus 91 code
elements on a links fixture;
  - branch: 0.
- **Real touch on the branch (Maestro):** a swipe up unpins and shows
the button; a swipe back down re-pins and hides it.
- **Desktop and phone-width Chromium:** `scrollHeight` equals the
content height, and the footer sits flush at the viewport bottom, so the
floor leaves no gap.
- **Tests that fail on main and pass here:**
- `scroll-position-keeper.test.ts`: stays attached when content shrinks
and regrows within a frame and the clamp scroll arrives late (with and
without iOS write deferral); follows content that stays shrunk to the
new end;
- `scroll-position-keeper.model.test.ts`: new random step that shrinks a
row, forces layout, regrows it, then delivers the late clamp. On main's
keeper it fails with "a content change detached the reader";
- `markdown-preview.test.tsx`: links and inline code stay mounted when
link routing and thread mentions change identity (with and without
incremental blocks); a link routes through the latest handler.
- `ConversationMessageContent.streaming.test.tsx`: when the settled
prefix grows, the live tail is now parsed as two pieces instead of one
whole-tail parse, because the piece cache is no longer reset by a
component-map change. The rendered output is identical.
- `pnpm exec turbo run typecheck lint test --filter=@bb/app`: 638 files,
6313 tests passed.

> AGENT GENERATED

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

---------

Co-authored-by: Michael Yong <review@local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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