Skip to content

fix(ui-chat): prevent downward wheel scroll snapback in long virtualized transcripts - #1288

Merged
iruriQ merged 2 commits into
mainfrom
fix/scroll-down-snapback
Nov 27, 2026
Merged

iruriQ merged 2 commits into
mainfrom
fix/scroll-down-snapback

Conversation

@iruriQ

@iruriQ iruriQ commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Purpose

Fixes cross-engine (WebKit/WKWebView + Chromium) scroll snapback where scrolling downward with the mouse wheel in a long virtualized conversation repeatedly snaps scrollTop back upward and prevents reaching the bottom.

No visual changes: this change fixes scroll ownership, virtualization height stability, and container-query layout boundaries without altering visual design; wide-table geometry (1136px / left 15px in WebKit, 1121px / left 15px in Chromium) and TurnNavigator preview layout are pixel-identical before and after.

Root Causes & Fixes

  1. Container query boundary wrapping the virtualized transcript (ChatView.module.css, AssistantMarkdown.module.css, use-chat-viewport.ts):
    • Removed container-type: inline-size from .frame so the virtualized .root -> .scroll -> .column flow is completely outside container-query interleaved layout (avoiding WebKit Bug 308027 transient scroll-range clamping in reading mode).
    • Replaced .md-table-wide's 100cqw lookup with --dsh-chat-viewport-width published on .scroll (list) by ChatViewport (synced on attach and resize). TurnNavigator already owns an independent height: 0 sibling .slot container and stays unaffected.
  2. Paging anchor preserve() reverting reader downward scroll & composer wheel intent (use-chat-viewport.ts, use-chat-reading.ts, use-chat-scroll.ts):
    • Guarded ChatViewport.preserve() and onScroll with isPagingDisplaced(metrics) so reader displacement immediately releases paging ownership instead of writing the old scrollTop back or clobbering observation.top, while preserving pure prepend history anchoring (both with and without native scroll anchoring).
    • Updated ChatViewport.onIntent so WheelEvent over [data-composer-seat] (including edge-forwarded editor wheel events from view-binding.ts) releases retained paging anchors whenever no inner scrollport consumes deltaY, while preserving non-wheel composer input guards.
    • Latched movedByReader across ChatReading's 500ms sampling window so intermediate baseline updates cannot erase confirmed reader motion.
    • Added ChatViewport.shiftObservation(delta) (useChatScroll.adjustBaseline) so virtualization's geometric scroll compensation never gets misattributed as reader motion or upward autoLoadEarlier triggers.
  3. Stale unmount placeholder heights, asymmetric compensation & overflow-anchor fighting virtualization (use-transcript-virtualization.ts, ChatView.tsx, ChatView.module.css):
    • Sampled live DOM height with subpixel precision (0.01px) right before commit in placeholderHeight() whenever a mounted row transitions out of the mounted set, avoiding stale cache collapse and cumulative Math.round drift.
    • Extended overflow-anchor: none to active virtualized sessions (data-chat-virtualizing) so both Chromium and WebKit share one deterministic geometric compensation path.
    • Made reading-mode pre-commit anchor compensation symmetric for both above-viewport mounts and above-viewport unmounts, fixed the nearTail vs followingTailAttr check, and guarded compensation with a passive scrollVersion counter so synchronous layout clamps recover cleanly without conflicting with asynchronous scrolls.

Affected Scope

  • Strictly scoped to apps/web-ui/packages/client/ui-chat/ (src/client/chat/* and test/*).

Validation

  • Focused unit tests (Red → Green): npx vitest run --config tests/vitest.config.js apps/web-ui/packages/client/ui-chat/test/ — 2 files, 20/20 passed (including 6 new regression tests that all failed before the fix).
  • Type check: npx tsc --noEmit -p apps/web-ui/tsconfig.json — passed.
  • Formatting: npx prettier --check on all changed files — passed.
  • Web UI build: npm run build:web:ui — passed.
  • Dual-engine browser verification (Playwright WebKit + Chromium):
    • Before fix: WebKit reproduced paging + composer-wheel snapback (2000 -> 2046, snappedBack2: true) and subpixel floor drift (43274 -> 43271).
    • After fix: Both WebKit and Chromium scrolled monotonically from scrollTop = 1200 to bottom (43310 / 43310, snapbacks: 0, 0px drift), snappedBack2: false (2000 -> 2300 WebKit, 2000 -> 2346 Chromium), frameContainerType: "normal", scrollerOverflowAnchor: "none", and wide-table geometry unchanged (1136px/15px WebKit, 1121px/15px Chromium).

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Summary

Summary by CodeRabbit

  • 问题修复
    • 修复聊天记录虚拟化和分页加载时滚动位置可能跳动或回退的问题,提升连续阅读体验。
    • 改进滚动意图识别,减少嵌套滚动区域和编辑器中的操作意外触发聊天记录导航。
    • 优化宽表格的可视区域计算,使其在聊天视口中显示更准确。

本次更新调整聊天视口宽度计算、分页锚点和滚动意图处理。虚拟化逻辑会报告实际滚动补偿量,并更新滚动归因基线。阅读采样会保留采样完成前的读者移动状态。新增测试覆盖这些行为。

Changes: 聊天滚动与虚拟化

Layer / File(s) Summary
视口宽度与表格布局
apps/web-ui/packages/client/ui-chat/src/client/chat/use-chat-viewport.ts, apps/web-ui/packages/client/ui-chat/src/client/chat/AssistantMarkdown.module.css, apps/web-ui/packages/client/ui-chat/src/client/chat/ChatView.module.css, apps/web-ui/packages/client/ui-chat/test/chat-scroll.test.ts
ChatViewport 在列表挂载和尺寸变化时发布 --dsh-chat-viewport-width,解绑时清除该变量。宽表格改用该变量计算宽度;.frame 不再设置 inline-size 容器。
分页锚点与滚动意图
apps/web-ui/packages/client/ui-chat/src/client/chat/use-chat-viewport.ts, apps/web-ui/packages/client/ui-chat/test/chat-scroll.test.ts
分页保留逻辑会检测读者是否移动,并在锚点位置改变时停止补偿。输入处理会区分滚轮和键盘方向,并检查嵌套滚动区域是否仍可滚动。测试覆盖锚点释放和滚轮交互。
虚拟化高度与滚动补偿
apps/web-ui/packages/client/ui-chat/src/client/chat/use-transcript-virtualization.ts, apps/web-ui/packages/client/ui-chat/src/client/chat/use-chat-scroll.ts, apps/web-ui/packages/client/ui-chat/src/client/chat/ChatView.tsx, apps/web-ui/packages/client/ui-chat/src/client/chat/ChatView.module.css, apps/web-ui/packages/client/ui-chat/test/transcript-virtualization.test.ts
虚拟化逻辑保留两位小数的行高,并根据滚动版本执行锚点补偿。实际补偿量经 ChatView 和 useChatScroll 更新视口归因基线。测试覆盖卸载行的实时高度和连续滚动补偿。
读者移动采样
apps/web-ui/packages/client/ui-chat/src/client/chat/use-chat-reading.ts, apps/web-ui/packages/client/ui-chat/test/chat-scroll.test.ts
ChatReading 在采样完成前保留读者移动状态,并将合并后的状态用于跟随策略和 ReadingSample。测试验证基线在采样定时器触发前更新时仍保留读者归因。

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant useTranscriptVirtualization
  participant ChatView
  participant useChatScroll
  participant ChatViewport
  useTranscriptVirtualization->>ChatView: 报告实际滚动补偿量
  ChatView->>useChatScroll: 调用 adjustBaseline(delta)
  useChatScroll->>ChatViewport: 调用 shiftObservation(delta)
Loading

Possibly related PRs

Suggested reviewers: bytepioneer-ai

  • 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

@iruriQ
iruriQ merged commit e06c3c6 into main Nov 27, 2026
5 of 7 checks passed
@iruriQ
iruriQ deleted the fix/scroll-down-snapback branch November 27, 2026 03:57
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.

2 participants