Skip to content

fix(app): restore session timeline scroll position after tab switches - #48125

Closed
cdz620 wants to merge 1 commit into
anomalyco:devfrom
cdz620:session-scroll-restore
Closed

cdz620 wants to merge 1 commit into
anomalyco:devfrom
cdz620:session-scroll-restore

Conversation

@cdz620

@cdz620 cdz620 commented Sep 9, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #45212

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Switching away from a session tab and back loses the reading position and lands at the bottom of the timeline.

Root cause: the per-session timeline cache only preserves row measurements across unmounts. On remount the viewport re-anchors to the bottom (initial offset plus anchor-bottom effects), and if the session was mid-stream the position is gone entirely - repro of the worst case: scroll up in a long session while it streams, switch to another tab, let the stream finish, switch back. The viewport lands at the bottom instead of the reading position.

Fix: on unmount, capture the first-visible-row anchor plus its pixel offset into the existing per-session timeline cache. On remount, use the saved offset as the virtualizer's initial offset and re-apply the anchor until measurements settle (same convergence pattern as the existing prepend-anchor logic). While the restore is in flight, all anchor-bottom paths (mount scrollToEnd, maybeAnchorBottom, resize-triggered anchor) are suppressed. Because programmatic scrolls don't count as gestures, the restore also routes through the normal scroll handler once, so bottom-follow detaches when landing away from the bottom and re-engages when the restored position is at the bottom. Sessions that were following the bottom keep following it (no restore is attempted), and hash/message navigation still takes precedence.

How did you verify your code works?

Verified interactively in Chromium against bun dev + a local server, using wheel gestures over long sessions (~100k px timelines):

  • Idle session, scrolled up: switch away and back - position restored exactly (same anchor row, same offset).
  • Session busy streaming, scrolled up, stream finishes while on another tab: switch back - position restored exactly. This is the reported bug scenario; on dev it lands at the bottom.
  • At the bottom following the stream: switch away and back - still following the bottom.
  • Full app unit suite passes; the one failure is a pre-existing i18n test that also fails on dev.

Screenshots / recordings

Happy to attach a recording on request; verification steps above are reproducible with any long session.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

The timeline cache only preserved row measurements across unmounts, so
remounting a session (e.g. switching tabs) re-anchored the viewport to
the bottom. Capture the first-visible-row anchor plus pixel offset on
unmount, keyed per session, and re-apply it on remount until
measurements settle. Bottom-follow is detached through the normal
scroll handler since programmatic restores are not gestures, and all
anchor-bottom paths are suppressed while the restore is in flight.
Sessions that were following the bottom keep following it.

Repro of the fixed case: scroll up in a long session while it streams,
switch to another tab, let the stream finish, then switch back - the
viewport now returns to the exact reading position instead of the
bottom.
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found a related PR that addresses a similar scroll position restoration issue:

Related PR:

The current PR #48125 is specifically fixing the tab-switch scenario, which is a distinct case from the parent/child navigation in #42809, so these are complementary fixes rather than duplicates.

@cdz620

cdz620 commented Sep 9, 2026

Copy link
Copy Markdown
Author

Note on the related PR flagged above: #42809 covers scroll restoration when navigating between parent and child sessions, while this one covers switching between session tabs (the scenario from #45212). The two are complementary - this PR anchors restoration on the per-session timeline cache used when a session's timeline unmounts and remounts on tab switches.

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant