Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 148 additions & 8 deletions static/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,11 @@ function _highlightQuestionRow(row){
async function jumpToTurnQuestion(questionRawIdx, assistantRawIdx){
const container=$('messages');
if(!container||typeof questionRawIdx!=='number'||questionRawIdx<0) return;
const clampTargetScrollTop=(scrollTop)=>{
const maxTop=Math.max(0,container.scrollHeight-container.clientHeight);
const n=Number(scrollTop);
return Math.max(0,Math.min(Number.isFinite(n)?n:container.scrollTop,maxTop));
};
const scrollToTarget=()=>{
const hasAssistant=typeof assistantRawIdx==='number'&&assistantRawIdx>=0;
if(hasAssistant){
Expand All @@ -1682,14 +1687,18 @@ async function jumpToTurnQuestion(questionRawIdx, assistantRawIdx){
_highlightQuestionRow(row);
return true;
};
// Cancel load-time bottom settling before any visible or virtualized target
// path can return. The jump owner keeps every native smooth-scroll frame out
// of the manual-reader listener, then reconciles against the final geometry.
_cancelBottomSettle();
_beginMessageJumpScroll(container);
if(scrollToTarget()) return;
const visWithIdx=_getVisibleMessagesWithIdx();
const visibleIdx=_messageVisibleIndexForRawIdx(questionRawIdx, visWithIdx);
if(visibleIdx>=0){
_scrollPinned=false;
_messageUserUnpinned=true;
_programmaticScroll=true;_programmaticScrollSetAt=performance.now();
container.scrollTop=_messageVirtualScrollTopForVisibleIdx(visWithIdx, visibleIdx, container);
const virtualTarget=clampTargetScrollTop(_messageVirtualScrollTopForVisibleIdx(visWithIdx, visibleIdx, container));
container.scrollTop=virtualTarget;
Comment on lines +1700 to +1701

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Virtualized middle targets stay unmounted

When a response lies in the unrendered middle of a long virtualized transcript, clamping its full-transcript offset to the current DOM range makes the preserve-scroll renders continue selecting windows that exclude the requested row, leaving the viewport at the bottom instead of jumping to that response.

_messageVirtualWindowKey='';
renderMessages({ preserveScroll:true });
requestAnimationFrame(()=>{
Expand Down Expand Up @@ -5761,6 +5770,96 @@ function _freshProgrammaticScrollActive(){
return true;
}
function _deferClearProgrammaticScroll(ms){clearTimeout(_programmaticScrollResetTimer);_programmaticScrollResetTimer=setTimeout(()=>{_programmaticScroll=false;},ms||80);}
let _messageJumpScrollGeneration=0;
let _messageJumpScrollOwner=null;
let _messageJumpScrollSettleTimer=0;
function _messageJumpSessionId(){
if(typeof S!=='undefined'&&S.session&&S.session.session_id) return String(S.session.session_id);
return '';
}
function _scheduleMessageJumpScrollReconcile(generation,ms){
if(!_messageJumpScrollOwner||_messageJumpScrollOwner.generation!==generation) return;
clearTimeout(_messageJumpScrollSettleTimer);
_messageJumpScrollSettleTimer=setTimeout(()=>_finishMessageJumpScroll(generation),ms||220);
}
function _beginMessageJumpScroll(container){
const previous=_messageJumpScrollOwner;
const preserved=previous?previous.preserved:{
scrollPinned:_scrollPinned,
messageUserUnpinned:_messageUserUnpinned,
nearBottomCount:_nearBottomCount,
};
clearTimeout(_messageJumpScrollSettleTimer);
const generation=++_messageJumpScrollGeneration;
_messageJumpScrollOwner={generation,container,sessionId:_messageJumpSessionId(),preserved};
// While the jump owner is active, temporarily release the reader pin so a
// live token arriving between smooth-scroll frames cannot let scrollIfPinned()
// reclaim the bottom and snap the reader off the jump target (#6621). The
// preserved snapshot above is what _finishMessageJumpScroll() reconciles
// against once the jump settles.
_scrollPinned=false;
_messageUserUnpinned=true;
_nearBottomCount=0;
_programmaticScroll=true;
_programmaticScrollSetAt=performance.now();
_scheduleMessageJumpScrollReconcile(generation,300);
return generation;
}
function _finishMessageJumpScroll(generation){
const owner=_messageJumpScrollOwner;
if(!owner||owner.generation!==generation) return;
if(owner.sessionId!==_messageJumpSessionId()){
_cancelMessageJumpScroll();
return;
}
clearTimeout(_messageJumpScrollSettleTimer);
_messageJumpScrollSettleTimer=0;
const container=owner.container;
const maxTop=Math.max(0,container.scrollHeight-container.clientHeight);
const top=Math.max(0,Math.min(Number(container.scrollTop)||0,maxTop));
const bottomDistance=maxTop-top;
if(bottomDistance>80){
_scrollPinned=false;
_messageUserUnpinned=true;
_nearBottomCount=0;
}else{
_scrollPinned=owner.preserved.scrollPinned;
_messageUserUnpinned=owner.preserved.messageUserUnpinned;
_nearBottomCount=owner.preserved.nearBottomCount;
}
_lastScrollTop=container.scrollTop;
_lastMessageClientHeight=container.clientHeight;
_messageJumpScrollOwner=null;
_programmaticScroll=false;
if(typeof _syncScrollToBottomCue==='function'){
_syncScrollToBottomCue(!_scrollPinned&&bottomDistance>80,{newMessage:_newMessageCueVisible});
}
if(typeof _updateSessionStartJumpButton==='function') _updateSessionStartJumpButton();
// An external-session refresh deferred while the reader was temporarily
// unpinned for the jump window (#6621) would otherwise stay stranded once the
// near-tail reconciliation restores follow mode. Flush it when the terminal
// state is genuinely pinned to the tail.
if(_scrollPinned&&!_messageUserUnpinned&&typeof _flushDeferredActiveSessionExternalRefresh==='function'){
_flushDeferredActiveSessionExternalRefresh();
}
}
function _cancelMessageJumpScroll(){
++_messageJumpScrollGeneration;
clearTimeout(_messageJumpScrollSettleTimer);
_messageJumpScrollSettleTimer=0;
// _beginMessageJumpScroll temporarily unpins the reader for the ownership
// window (#6621); a cancel that isn't a reconcile must put the pre-jump pin
// state back, or the transient unpinned state would leak. Callers that want a
// different terminal pin state (e.g. scrollToBottom) set it right after this.
const owner=_messageJumpScrollOwner;
if(owner&&owner.preserved){
_scrollPinned=owner.preserved.scrollPinned;
_messageUserUnpinned=owner.preserved.messageUserUnpinned;
_nearBottomCount=owner.preserved.nearBottomCount;
}
_messageJumpScrollOwner=null;
_programmaticScroll=false;
}
let _nearBottomCount=0;
let _lastScrollTop=null;
let _lastMessageClientHeight=null; // #4702: track scroller height to ignore iOS portrait toolbar-settle reflows (a clientHeight increase fires a scroll event with decreased scrollTop that is NOT a user scroll)
Expand Down Expand Up @@ -5806,7 +5905,7 @@ let _lastMessageRenderAt=-Infinity;
function _recentMessageRenderArtifactWindow(ms){
return performance.now()-_lastMessageRenderAt<(ms||1400);
}
function _cancelBottomSettle(){ _bottomSettleToken++; if(_settleRO){ _settleRO.disconnect(); _settleRO=null; } clearTimeout(_settleTimer); clearTimeout(_settleFinalTimer); cancelAnimationFrame(_settleRAF); }
function _cancelBottomSettle(){ _cancelMessageJumpScroll(); _bottomSettleToken++; if(_settleRO){ _settleRO.disconnect(); _settleRO=null; } clearTimeout(_settleTimer); clearTimeout(_settleFinalTimer); cancelAnimationFrame(_settleRAF); }
function _markMessageTouchScrollIntent(active=true){
_messageTouchScrollActive=!!active;
_lastMessageTouchScrollIntentMs=performance.now();
Expand Down Expand Up @@ -5859,15 +5958,33 @@ function _recordNonMessageScrollIntent(e){
const target=e&&e.target;
if(!el||!target) return;
if(!el.contains(target)){ _lastNonMessageScrollIntentMs=performance.now(); return; }
// Capture the guards before cancelling the active owner: cancellation clears
// the programmatic flag and jump owner, but a low-delta upward wheel must still
// count as reader takeover when it interrupted an owned scroll.
const wheelUp=typeof e.deltaY==='number'&&e.deltaY<0;
const guardedWheelUp=wheelUp&&_freshProgrammaticScrollActive();
const jumpScrollOwned=typeof _messageJumpScrollOwner!=='undefined'&&!!_messageJumpScrollOwner;
if(e.type==='touchmove'||(typeof e.deltaY==='number'&&e.deltaY!==0)){
if(typeof _messageScrollInputGeneration==='number') _messageScrollInputGeneration++;
if(jumpScrollOwned||e.type==='touchmove'||(typeof e.deltaY==='number'&&e.deltaY< -30)||guardedWheelUp){
if(typeof _cancelBottomSettle==='function') _cancelBottomSettle();
}
}
// Any message-pane scroll input that interrupts an active jump owner is a
// reader takeover, regardless of direction or the programmatic-latch age
// (#6621): _cancelBottomSettle above restores the pre-jump snapshot, so
// without this a gentle wheel-up OR wheel-down (or a touch scroll) after the
// latch expires would leave the reader pinned and let the next token snap to
// the bottom. Establish the unpinned reader-owned state explicitly; a reader
// who wants the bottom re-pins by reaching it (<=80px) or pressing End.
if(jumpScrollOwned&&(wheelUp||e.type==='touchmove'||(typeof e.deltaY==='number'&&e.deltaY!==0))){
_messageUserUnpinned=true;
_scrollPinned=false;
_nearBottomCount=0;
}
if(typeof e.deltaY==='number'&&e.deltaY<0) _lastMessageWheelIntentMs=performance.now();
// Keep e.deltaY< -30 as the ordinary direct sticky-unpin threshold.
const wheelUp=typeof e.deltaY==='number'&&e.deltaY<0;
const guardedWheelUp=wheelUp&&_freshProgrammaticScrollActive();
if(e.type==='touchmove'||(typeof e.deltaY==='number'&&e.deltaY< -30)||guardedWheelUp){
_cancelBottomSettle();
if(e.type==='touchmove') _markMessageTouchScrollIntent(true);
if((typeof e.deltaY==='number'&&e.deltaY< -30)||guardedWheelUp){
_messageUserUnpinned=true;
Expand Down Expand Up @@ -5959,6 +6076,7 @@ if(typeof document!=='undefined'){
// prevent the new chat's first scroll comparing against the previous chat's
// scrollTop (Opus stage-302 SHOULD-FIX, #1731 follow-up).
function _resetScrollDirectionTracker(){
_cancelMessageJumpScroll();
_clearNewMessageScrollCue();
_lastScrollTop=null;
_lastMessageClientHeight=null;
Expand All @@ -5980,6 +6098,11 @@ function _resetScrollDirectionTracker(){
_deferredOlderMessagesTimer=0;
}
function _resetStreamScrollFollow(){
// Cancel any in-flight jump owner FIRST: a new stream is a definitive
// pin-to-follow, and _cancelMessageJumpScroll() restores the pre-jump snapshot
// (#6621), so it must run BEFORE the pinned-state assignments below or it would
// undo them and silently disable auto-follow for the new stream.
_cancelBottomSettle();
_clearNewMessageScrollCue();
_messageUserUnpinned=false;
_scrollPinned=true;
Expand All @@ -5992,7 +6115,6 @@ function _resetStreamScrollFollow(){
_lastMessageScrollIntentMs=-Infinity;
// #4970 review (greptile P1): same hygiene for keyboard scroll intent.
_lastMessageKeyScrollIntentMs=-Infinity;
_cancelBottomSettle();
}
if(typeof window!=='undefined'){
window._resetScrollDirectionTracker=_resetScrollDirectionTracker;
Expand Down Expand Up @@ -6072,6 +6194,7 @@ if(typeof window!=='undefined'){
if(!el) return;
el.addEventListener('pointerdown',(e)=>{
if(e.target===el&&e.offsetX>=el.clientWidth){
if(typeof _cancelBottomSettle==='function') _cancelBottomSettle();
_scrollbarDragActive=true;
if(typeof _messageScrollInputGeneration==='number') _messageScrollInputGeneration++;
}
Expand Down Expand Up @@ -6119,6 +6242,7 @@ if(typeof window!=='undefined'){
// Count only when the message pane itself is the scroll target: it is focused,
// contains the focus, or the pointer is over it (keyboard scroll w/o focus).
if(a===el||el.contains(a)||el.matches(':hover')){
if(typeof _cancelBottomSettle==='function') _cancelBottomSettle();
const now=performance.now();
if(typeof _messageScrollInputGeneration==='number') _messageScrollInputGeneration++;
_lastMessageKeyScrollIntentMs=now;
Expand All @@ -6129,6 +6253,10 @@ if(typeof window!=='undefined'){
let _scrollRaf=0;
el.addEventListener('scroll',()=>{
_scheduleMessageVirtualizedRender();
if(_messageJumpScrollOwner){
_scheduleMessageJumpScrollReconcile(_messageJumpScrollOwner.generation);
return;
}
if(_freshProgrammaticScrollActive()) return;
_markMessageVirtualScrollActive();
cancelAnimationFrame(_scrollRaf);
Expand Down Expand Up @@ -6987,6 +7115,11 @@ function _settleFinalScroll(token){
}
function scrollIfPinned(){
if(!window._autoScrollFollow) return;
// A jump-to-question owner is mid-flight: it deliberately holds the reader at
// the jump target across smooth-scroll frames, so never let a live token
// reclaim the bottom while it is active (#6621). _finishMessageJumpScroll()
// reconciles the pin state once the jump settles.
if(typeof _messageJumpScrollOwner!=='undefined'&&_messageJumpScrollOwner) return;
if(_messageUserUnpinned){
// Only scrollToBottom() cleared this flag, so one scroll-up permanently
// killed auto-follow. Re-pin ONLY when the reader has genuinely returned to
Expand All @@ -7011,6 +7144,13 @@ function scrollIfPinned(){
_settleMessageScrollToBottom(false);
}
function scrollToBottom(){
// An explicit scroll-to-bottom (End button, or any definitive pin-to-bottom)
// supersedes a pending jump-to-question reconciliation: cancel the active jump
// owner first so its deferred _finishMessageJumpScroll() can't restore the
// pre-jump unpinned snapshot and silently undo this pin (#6621). The jump path
// itself never calls scrollToBottom(), and while a jump owner is active the
// reader is unpinned so the internal auto-follow callers don't reach here.
if(typeof _messageJumpScrollOwner!=='undefined'&&_messageJumpScrollOwner&&typeof _cancelMessageJumpScroll==='function') _cancelMessageJumpScroll();
_clearNewMessageScrollCue();
_scrollPinned=true;
_messageUserUnpinned=false;
Expand Down
4 changes: 3 additions & 1 deletion tests/test_issue2246_question_jump.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def test_question_jump_expands_windowed_history_and_highlights_question():
assert "function _messageVisibleIndexForRawIdx(rawIdx, visWithIdx)" in UI_JS
assert "function _messageVirtualScrollTopForVisibleIdx(visWithIdx, visibleIdx, container)" in UI_JS
assert "const visibleIdx=_messageVisibleIndexForRawIdx(questionRawIdx, visWithIdx);" in UI_JS
assert "container.scrollTop=_messageVirtualScrollTopForVisibleIdx(visWithIdx, visibleIdx, container);" in UI_JS
assert "const virtualTarget=clampTargetScrollTop(_messageVirtualScrollTopForVisibleIdx(visWithIdx, visibleIdx, container));" in UI_JS
assert "_beginMessageJumpScroll(container);" in UI_JS
assert "container.scrollTop=virtualTarget;" in UI_JS
assert "_messageVirtualWindowKey='';" in UI_JS
assert "_messageRenderWindowSize=Math.max(_currentMessageRenderWindowSize(),_messageRenderableMessageCount())" in UI_JS
assert "renderMessages({ preserveScroll:true })" in UI_JS
Expand Down
2 changes: 1 addition & 1 deletion tests/test_issue4856_android_scroll_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def test_low_delta_wheel_intent_is_tracked_separately():
assert "function _recentMessageWheelIntent" in UI_JS
rec_idx = UI_JS.find("function _recordNonMessageScrollIntent")
assert rec_idx != -1, "_recordNonMessageScrollIntent not found"
rec = UI_JS[rec_idx: rec_idx + 1400]
rec = UI_JS[rec_idx: rec_idx + 2000]
assert "e.deltaY<0) _lastMessageWheelIntentMs=performance.now()" in rec, (
"#4970: _recordNonMessageScrollIntent must record low-delta upward wheel "
"intent (deltaY<0) separately from the decisive deltaY<-30 unpin."
Expand Down
Loading
Loading