Skip to content

fix(app): restore chat scroll position per session on tab switch - #2176

Open
jacktrnr wants to merge 1 commit into
mainfrom
fix/1251-scroll-restore
Open

jacktrnr wants to merge 1 commit into
mainfrom
fix/1251-scroll-restore

Conversation

@jacktrnr

Copy link
Copy Markdown
Contributor

Closes #1251

Important

Needs a hands-on UI check before merge. The DOM-coupled timeline (virtualizer + auto-scroll hook + resize observer) cannot be rendered in the unit suite, so the fix is covered by behavior tests on the extracted pieces plus source-wiring assertions. Please verify in the app:

  1. In a long session, scroll up mid-chat, switch to another session tab, switch back: the chat reopens at the same reading position.
  2. Do the same in two sessions at different positions: each keeps its own.
  3. While scrolled up in a session that is streaming, new output does not pull you down.
  4. At the bottom of a streaming session, autoscroll still follows; switching away and back from a session left at the bottom reopens at the bottom.
  5. After restoring, scrolling back to the bottom re-engages autoscroll.

Root cause

Two things combined:

  • session.tsx cleared the shared autoScroll.userScrolled flag on every session switch, so shouldAnchorBottom() was true for the incoming session.
  • MessageTimeline remounts per session (keyed Show) and could only start at the bottom or the top. Every anchor path (initial offset, mount scrollToEnd, follow-on-append, resize anchoring, maybeAnchorBottom) read shouldAnchorBottom(), so the reader was pulled down. The outgoing timeline also saw the cleared flag and could be scrolled to the bottom during the switch, which erased any position it held.

Earlier attempts (see the issue) failed because they used pause() (bails with a detached scroll element), raced the remount, or guarded only some of the anchor paths.

Fix

  • scroll-posture.ts (new): a per-session posture store (scrollTop saved while the reader is off the bottom, cleared at the bottom) and a bounded restore loop that re-applies the position for a few frames while late measurement settles. A user gesture ends it at once.
  • message-timeline.tsx: every bottom-pull now goes through one anchorBottom() that is vetoed while a posture is being restored and once the route has moved to another session. The saved posture seeds initialOffset and is restored on mount, only when the measurement snapshot survived. Posture is recorded on scroll for the owning session only, and is dropped when its snapshot is evicted from timelineCache.
  • session.tsx: on switch, a session with a saved posture gets bottom-follow held off (holdUserScrolled) instead of cleared; the timeline re-asserts the hold on mount and when the restore finishes, so effect ordering does not matter.
  • createAutoScroll: new holdUserScrolled(), which unlike pause() works with no live scroll element.

New-message behavior is unchanged: a held reader has userScrolled = true, so streaming follow, follow-on-append and resize anchoring stay off; a reader at the bottom has no saved posture and follows as before.

Not addressed: the reload-oscillation variant from the Sep 21 comment (a different mechanism, on a cold mount with no saved posture). Postures live in memory, so a window reload reopens at the bottom as before.

Relation to #2031: I read it as input only. This PR is cut from main and keeps the same broad shape (posture store, restore veto, bounded restore). It differs in three ways: it uses a hold that works without a scroll element instead of pause(), it adds the owner-session guard so the outgoing timeline can't erase its posture, and it makes the session page hold on switch rather than relying only on the remounted timeline.

Tests

Overlay app tests, run from the materialized tree (bun test --conditions=solid --preload ./happydom.ts):

  • scroll-posture.test.ts: 9 pass (per-session save/clear, bottom and no-overflow cases, re-apply after a late bottom-pull, bounded window, user gesture wins, cancel on unmount)
  • auto-scroll-hold.test.ts: 2 pass (hold with no scroll element; a held reader is not pulled down by streaming follow)
  • message-timeline-scroll-posture.test.ts: 6 pass (wiring)
  • Full app unit suite: 2137 pass / 23 fail. All 23 failures also fail on main (2120 pass / 23 fail, identical failing set: i18n parity, rows-current, observe-element-offset and others unrelated to this change).
  • overlay_typecheck_gate.sh: clean across 7 packages
  • refresh_manifest + drift_gate: PASS

🤖 Generated with Claude Code

Switching away from a session tab and back forced the chat to the bottom.
The session page cleared the shared auto-scroll "user scrolled" flag on
every switch, and the per-session MessageTimeline remount could only start
at the bottom or the top, so every anchor path (initial offset, mount
scrollToEnd, follow-on-append, resize anchoring, maybeAnchorBottom) pulled
the reader down. The outgoing timeline could also be scrolled to the
bottom during the switch, since it still saw the cleared flag.

- scroll-posture.ts: a per-session posture store (saved scrollTop while off
  the bottom, cleared at the bottom) and a bounded restore loop that
  re-applies the position while late measurement settles; a user gesture
  ends it at once.
- message-timeline.tsx: route every bottom-pull through one anchorBottom()
  that is vetoed while restoring and once the route has moved to another
  session; restore from the measurement snapshot; record posture on scroll
  for the owning session only; drop a posture when its snapshot is evicted.
- session.tsx: on switch, hold bottom-follow off for a session with a saved
  posture instead of clearing it; pass the hold to the timeline.
- createAutoScroll: add holdUserScrolled(), which (unlike pause()) works with
  the scroll element detached.

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

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8263cdbe-f4cc-420e-bfca-d217e316012b
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@jacktrnr
jacktrnr marked this pull request as ready for review November 23, 2026 22:08
@jacktrnr

Copy link
Copy Markdown
Contributor Author

Verified hands-on in the combined 0.4.1-beta test build (test/0.4.1-beta, Developer Tools → Rebuild Locally, macOS arm64) on 2026-11-23: all checks in this PR's manual checklist pass.

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.

bug(app): chat scroll position resets to bottom on session tab switch

1 participant