Open
Conversation
…l position When loading older messages, the assistant-ui library's autoScroll feature was interfering with our manual scroll position restoration, causing the viewport to jump to the top instead of maintaining position. Root cause: ThreadPrimitive.Viewport's autoScroll triggers scrollToBottom() on content resize when autoScroll=true and isAtBottom=true. This conflicts with our useLayoutEffect scroll position restoration. Fix: Temporarily disable autoScroll during history load: 1. Set autoScrollEnabled=false when handleLoadMore starts 2. Restore scroll position in useLayoutEffect as before 3. Re-enable autoScroll after position is restored 4. Handle error cases to ensure autoScroll is always re-enabled via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
Add detailed logging to understand why scroll position jumps to top when loading older messages instead of maintaining position. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
The previous implementation tried to restore scroll position immediately when messagesVersion changed, but the new messages hadn't been rendered to DOM yet, causing delta to be 0 and scroll position to remain at top. Fix: Use requestAnimationFrame to wait for DOM update and check if scrollHeight has actually changed before restoring position. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
Remove temporary debug logging added during development. The fix is working correctly - scroll position is now properly maintained when loading older messages. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
Address code review feedback: 1. Preserve autoScroll state across history loads - Save autoScrollEnabled state before disabling it - Restore previous state instead of forcing true - Prevents unwanted scroll-to-bottom when user is viewing middle content 2. Add max attempts limit to requestAnimationFrame loop - Limit to 5 attempts to prevent infinite loop - Gracefully handle cases where DOM doesn't update - Improves performance and prevents browser hang via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
Wrap setAutoScrollEnabled calls in setTimeout to ensure they execute after all DOM operations complete. This prevents 'removeChild' errors that occur when React tries to reconcile while DOM is being modified. - Use setTimeout(fn, 0) for max attempts case - Use setTimeout(fn, 100) for normal case to ensure DOM stability via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
| loadLockRef.current = false | ||
| // Re-enable autoScroll if loading finished but useLayoutEffect didn't run | ||
| setAutoScrollEnabled(true) | ||
| } |
There was a problem hiding this comment.
[MAJOR] Correctness 强制开启 autoScroll 导致滚动位置被重置
Why this is a problem: 在 useLayoutEffect 未触发时,当前实现会无条件 setAutoScrollEnabled(true),即便用户在加载前并不在底部,也会被强制打开自动滚动,导致加载历史后滚动跳到底部/位置丢失。
Suggested fix:
// 恢复为加载前的状态,避免强制开启 autoScroll
setAutoScrollEnabled(prevAutoScrollEnabledRef.current)There was a problem hiding this comment.
Findings
- [Major] 回退路径强制打开 autoScroll,可能把用户滚动拉回底部,破坏历史位置恢复,evidence
web/src/components/AssistantChat/HappyThread.tsx:298
Suggested fix:setAutoScrollEnabled(prevAutoScrollEnabledRef.current)
Summary
- 1 个问题:加载结束的兜底分支会强制开启 autoScroll,可能引起滚动跳底。
Testing
- Not run (automation)
HAPI Bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
在加载历史消息时,自动滚动功能会干扰滚动位置的恢复。
修复内容
via HAPI
Co-Authored-By: HAPI noreply@hapi.run