After a hidden interval that added persisted messages (post-turn bg-review
writes, sibling-tab writes, the just-finished main turn flushing), switching
the tab back caused refreshActiveSessionIfExternallyUpdated('visible'|'focus')
to hit remoteCount !== localCount and call loadSession(sid, {force:true}),
which synchronously did S.messages = [] before awaiting the metadata +
messages fetches. The visible result was the entire conversation transcript
blanking for the round-trip and reappearing — '对话突然消失,重刷才回来'.
sessions.js itself warns about this 'disappear and reappear' tradeoff at the
top of the remoteCount/remoteLast guard, but only short-circuits the
metadata-only branch (#5061). #5122's 4-probe staged reconnect covers a
different path (SSE error with ready_state=2 on a visible tab) and does not
apply when the SSE error arrives while visibility_state='hidden' — that
bottoms out through _deferStreamErrorIfPageHidden before the reconnect block
ever runs.
Fix:
- refreshActiveSessionIfExternallyUpdated maps the visibility/focus recovery
reasons ({visible, focus}) to a new keepStaleUntilLoaded option, forwarded
to loadSession. The post-stream idle reconcile and the poll/external paths
keep their existing behaviour.
- loadSession ANDs opts.keepStaleUntilLoaded with sameSessionForceReload (so
cross-session switches still clear synchronously — leaving a prior
session's transcript on screen during a navigation is the original bug
the clear was written for) and, on that path, skips the synchronous
S.messages/S.toolCalls/_messagesTruncated/_oldestIdx clear. The new
transcript is SWAPPED into S.messages by _ensureMessagesLoaded(sid,
{force:true}), so the user sees old DOM directly replaced by new DOM in a
single render frame.
- _ensureMessagesLoaded grows an opts.force escape hatch so its 'messages
already populated' early-return cannot skip the swap when stale messages
are still in place.
Verified live on a source 8701 build:
- before: minHtmlObserved 65874, minKidsObserved 11 — 372 samples at 16ms,
DOM html length NEVER dipped below the pre-call value during the reload.
- loadSession was called with force:true keepStale:true reason:'visible',
outcome 'reloaded', sMsgs 21 → 1037, after kids 11 → 134 (single-frame
swap).
Tests:
- tests/test_issue5177_hidden_tab_blank_gap.py (new, 7 cases): source-locks
the keep-stale guard shape, the recovery-reason map, the
_ensureMessagesLoaded opts.force escape hatch, and that the synchronous
clear is wrapped in if(!_keepStaleUntilLoaded).
- Updated the two existing source-lock tests whose anchor strings included
the loadSession call signature
(tests/test_webui_external_refresh_frontend.py +
tests/test_tars_scroll_reset_regressions.py) to match the new
loadSession(... keepStaleUntilLoaded:_keepStaleUntilLoaded) form. No
behavioural assertions changed.
Local: node --check static/sessions.js OK. 122 passed across
test_issue5177_hidden_tab_blank_gap.py + the full adjacent set
(external-refresh / journal-frontend / scroll-reset / reconnect-chronology /
inflight-stream-reuse / sse-error-multi-probe / issue4295 / issue4856).
224 lines
12 KiB
Python
224 lines
12 KiB
Python
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
UI_JS = (REPO / "static" / "ui.js").read_text(encoding="utf-8")
|
|
SESSIONS_JS = (REPO / "static" / "sessions.js").read_text(encoding="utf-8")
|
|
|
|
|
|
def _function_body(src: str, signature: str) -> str:
|
|
start = src.index(signature)
|
|
brace = src.index("{", start)
|
|
depth = 0
|
|
for i in range(brace, len(src)):
|
|
if src[i] == "{":
|
|
depth += 1
|
|
elif src[i] == "}":
|
|
depth -= 1
|
|
if depth == 0:
|
|
return src[start : i + 1]
|
|
raise AssertionError(f"function body not found: {signature}")
|
|
|
|
|
|
def _scroll_listener_block() -> str:
|
|
start = UI_JS.index("el.addEventListener('scroll'")
|
|
return UI_JS[start : UI_JS.index("})();", start)]
|
|
|
|
|
|
def test_clicking_current_session_is_noop_before_load_session_side_effects():
|
|
load_session = _function_body(SESSIONS_JS, "async function loadSession")
|
|
|
|
current_idx = load_session.index("const currentSid = S.session ? S.session.session_id : null")
|
|
noop_idx = load_session.index("if(currentSid===sid && !forceReload) return")
|
|
loading_idx = load_session.index("_loadingSessionId = sid")
|
|
stop_idx = load_session.index("stopApprovalPolling")
|
|
|
|
assert current_idx < noop_idx < loading_idx < stop_idx, (
|
|
"clicking the already-open sidebar row must be a no-op before loadSession() "
|
|
"mutates loading/runtime state or scroll-affecting UI"
|
|
)
|
|
|
|
|
|
def test_scroll_to_bottom_settles_across_late_markdown_layout_growth():
|
|
settle = _function_body(UI_JS, "function _settleMessageScrollToBottom")
|
|
scroll = _function_body(UI_JS, "function scrollToBottom")
|
|
pinned = _function_body(UI_JS, "function scrollIfPinned")
|
|
|
|
# The settle survives late markdown layout growth (Prism/KaTeX/Mermaid/images)
|
|
# via a ResizeObserver on the growing content node (#msgInner) plus a 2s
|
|
# static-content fallback — this replaced the old [0,16,80,180]ms setTimeout
|
|
# fan-out + double-rAF scrollHeight polling (#3920, Firefox reflow jitter).
|
|
assert "requestAnimationFrame" in settle
|
|
assert "setTimeout" in settle
|
|
assert "new ResizeObserver" in settle
|
|
assert "getElementById('msgInner')" in settle, (
|
|
"the ResizeObserver must observe the growing content node (#msgInner), "
|
|
"not the fixed #messages scroll container"
|
|
)
|
|
assert "2000" in settle, "a 2s fallback must cover fully-static content that never resizes"
|
|
# scrollToBottom uses force=false so the observer runs, and explicit=true so the
|
|
# settle still runs even when Auto-follow is off (explicit user jump). The
|
|
# automatic scrollIfPinned() path passes no explicit flag (stays auto-gated).
|
|
assert "_settleMessageScrollToBottom(false, true)" in scroll
|
|
assert "_settleMessageScrollToBottom(false)" in pinned
|
|
assert "!_scrollPinned" in settle
|
|
assert "const token=++_bottomSettleToken" in settle
|
|
assert "token!==_bottomSettleToken" in settle
|
|
|
|
|
|
def test_scroll_to_bottom_writes_scroll_position_immediately_before_delayed_settle():
|
|
scroll = _function_body(UI_JS, "function scrollToBottom")
|
|
|
|
immediate_idx = scroll.index("_setMessageScrollToBottom();")
|
|
settle_idx = scroll.index("_settleMessageScrollToBottom(false, true)")
|
|
|
|
assert immediate_idx < settle_idx, (
|
|
"scrollToBottom() must write scrollTop synchronously before scheduling the "
|
|
"ResizeObserver settle; otherwise a DOM-rebuild scroll event can cancel the "
|
|
"delayed settle and strand the viewport at the top"
|
|
)
|
|
|
|
|
|
def test_message_scroll_listener_does_not_downgrade_explicit_bottom_pin_on_first_near_bottom_event():
|
|
listener_block = _scroll_listener_block()
|
|
set_bottom = _function_body(UI_JS, "function _setMessageScrollToBottom")
|
|
|
|
assert "_nearBottomCount=2" in set_bottom
|
|
assert "_scrollPinned=_nearBottomCount>=2" not in listener_block
|
|
assert "if(_nearBottomCount>=2){" in listener_block
|
|
assert "_scrollPinned=false" in listener_block
|
|
|
|
|
|
def test_user_scroll_cancels_delayed_bottom_settling():
|
|
listener_block = _scroll_listener_block()
|
|
record = _function_body(UI_JS, "function _recordNonMessageScrollIntent")
|
|
pinned = _function_body(UI_JS, "function scrollIfPinned")
|
|
final = _function_body(UI_JS, "function _settleFinalScroll")
|
|
|
|
assert "function _cancelBottomSettle" in UI_JS
|
|
assert "_cancelBottomSettle();" in listener_block
|
|
assert "e.deltaY< -30" in record
|
|
assert "_cancelBottomSettle();" in record
|
|
assert "_lastNonMessageScrollIntentMs=performance.now();" in record
|
|
assert "_scrollPinned=false" in record
|
|
assert "if(_messageUserUnpinned) return;" in pinned
|
|
assert "_messageUserUnpinned" in final and "return" in final
|
|
assert "_recentMessageUpwardIntent()" not in pinned
|
|
|
|
|
|
def test_external_active_refresh_defers_while_reader_is_manually_unpinned():
|
|
refresh = _function_body(SESSIONS_JS, "async function refreshActiveSessionIfExternallyUpdated")
|
|
|
|
assert "_isMessageReaderUnpinned" in UI_JS
|
|
assert "_deferActiveSessionExternalRefresh" in SESSIONS_JS
|
|
assert "typeof _isMessageReaderUnpinned==='function'&&_isMessageReaderUnpinned()" in refresh
|
|
assert "_deferActiveSessionExternalRefresh(reason||'poll');" in refresh
|
|
assert "await loadSession(sid, {force:true, externalRefreshReason:reason||'poll', keepStaleUntilLoaded:_keepStaleUntilLoaded});" in refresh
|
|
|
|
|
|
def test_session_switch_clears_deferred_active_refresh_reason():
|
|
load = _function_body(SESSIONS_JS, "async function loadSession")
|
|
assert "function _clearDeferredActiveSessionExternalRefresh()" in SESSIONS_JS
|
|
assert "_deferredActiveSessionExternalRefreshReason = '';" in SESSIONS_JS
|
|
assert "if (currentSid !== sid) {\n _clearDeferredActiveSessionExternalRefresh();\n }" in load
|
|
|
|
|
|
def test_deferred_active_refresh_keeps_idle_reconcile_over_poll():
|
|
defer = _function_body(SESSIONS_JS, "function _deferActiveSessionExternalRefresh")
|
|
assert "const nextReason = reason || 'poll';" in defer
|
|
assert "_deferredActiveSessionExternalRefreshReason==='idle-reconcile'&&nextReason==='poll'" in defer
|
|
assert "_deferredActiveSessionExternalRefreshReason = nextReason;" in defer
|
|
|
|
|
|
def test_scroll_to_bottom_flushes_deferred_active_refresh_after_explicit_repin():
|
|
scroll = _function_body(UI_JS, "function scrollToBottom")
|
|
|
|
assert "_flushDeferredActiveSessionExternalRefresh" in SESSIONS_JS
|
|
assert "_flushDeferredActiveSessionExternalRefresh" in scroll
|
|
|
|
|
|
def test_preserve_scroll_restores_unpinned_viewport_after_dom_rebuild():
|
|
render = _function_body(UI_JS, "function renderMessages")
|
|
after_render = _function_body(UI_JS, "function _scrollAfterMessageRender")
|
|
follow = _function_body(UI_JS, "function _followMessagesAfterDomReplace")
|
|
capture = _function_body(UI_JS, "function _captureMessageScrollSnapshot")
|
|
restore = _function_body(UI_JS, "function _restoreMessageScrollSnapshot")
|
|
|
|
snapshot_idx = render.index("const scrollSnapshot=(preserveScroll||_messageUserUnpinned)?_captureMessageScrollSnapshot():null")
|
|
inner_idx = render.index("const inner=$('msgInner')")
|
|
final_scroll_idx = render.rindex("_scrollAfterMessageRender(preserveScroll, scrollSnapshot)")
|
|
|
|
assert snapshot_idx < inner_idx < final_scroll_idx, (
|
|
"renderMessages({preserveScroll:true}) must capture #messages.scrollTop before "
|
|
"replacing transcript DOM, then pass that snapshot to the post-render scroll helper"
|
|
)
|
|
assert "if(!readerAwayFromBottom && !_messageUserUnpinned && _followMessagesAfterDomReplace()) return;" in after_render
|
|
assert "_restoreMessageScrollSnapshot(scrollSnapshot);\n _maybeShowNewMessageScrollCue(scrollSnapshot);" in after_render
|
|
assert "_shouldFollowMessagesOnDomReplace()" in follow
|
|
assert "scrollToBottom();" in follow
|
|
assert "anchor:(typeof _captureMessageViewportAnchor==='function')?_captureMessageViewportAnchor():null" in capture
|
|
assert "sessionIdx:Number.isFinite(sessionIdx)?sessionIdx:_messageSessionIndexForRawIdx(rawIdx)" in UI_JS
|
|
assert "key:row&&row.dataset?String(row.dataset.messageAnchorKey||''):''" in UI_JS
|
|
assert "row.dataset.sessionMsgIdx=_messageSessionIndexForRawIdx(rawIdx);" in UI_JS
|
|
assert "seg.dataset.sessionMsgIdx=_messageSessionIndexForRawIdx(rawIdx);" in UI_JS
|
|
assert "row.dataset.messageAnchorKey=_messageViewportAnchorKeyForMessage(m);" in UI_JS
|
|
assert "seg.dataset.messageAnchorKey=_messageViewportAnchorKeyForMessage(m);" in UI_JS
|
|
assert "container.querySelector(`[data-session-msg-idx=\"${sessionIdx}\"]`)" in UI_JS
|
|
assert "if(!row&&anchorKey) return false;" in UI_JS
|
|
assert "if(!row&&hasSessionIdx) return false;" in UI_JS
|
|
assert "_restoreMessageViewportAnchor(snapshot.anchor,0)" in restore
|
|
assert "if(!restoredViaAnchor){" in restore
|
|
assert "el.scrollTop=Math.max(0,Math.min(Number(snapshot.top)||0,maxTop))" in restore
|
|
assert "_programmaticScroll=true" in restore
|
|
|
|
|
|
def test_pinned_preserve_scroll_uses_bottom_distance_before_viewport_anchor():
|
|
"""Pinned/following streams must not remount an older viewport anchor.
|
|
|
|
Live Worklog/activity rebuilds call the same snapshot helpers as reader
|
|
preservation. For pinned followers, the stable invariant is tail-relative
|
|
bottom distance, not the first visible row. Restoring the semantic anchor in
|
|
that state can yank a long streaming transcript upward/topward.
|
|
"""
|
|
helper = _function_body(UI_JS, "function _restorePinnedMessageScrollSnapshot")
|
|
restore = _function_body(UI_JS, "function _restoreMessageScrollSnapshot")
|
|
same_frame = _function_body(UI_JS, "function _restoreMessageScrollSnapshotSameFrame")
|
|
|
|
assert "snapshot.pinned!==true||snapshot.userUnpinned===true" in helper
|
|
assert "maxTop-Math.max(0,bottom)" in helper
|
|
assert "_messageUserUnpinned=false;" in helper
|
|
assert "_scrollPinned=true;" in helper
|
|
assert "_nearBottomCount=2;" in helper
|
|
|
|
regular_guard_idx = restore.index("_restorePinnedMessageScrollSnapshot(snapshot)")
|
|
regular_anchor_idx = restore.index("_restoreMessageViewportAnchor(snapshot.anchor,0)")
|
|
same_guard_idx = same_frame.index("_restorePinnedMessageScrollSnapshot(snapshot)")
|
|
same_anchor_idx = same_frame.index("_restoreMessageViewportAnchor(snapshot.anchor,0)")
|
|
|
|
assert regular_guard_idx < regular_anchor_idx
|
|
assert same_guard_idx < same_anchor_idx
|
|
|
|
|
|
def test_same_session_reload_anchor_uses_absolute_session_message_index():
|
|
assert "function _messageSessionIndexBase()" in UI_JS
|
|
assert "return _messageSessionIndexBase()+n;" in UI_JS
|
|
assert "return n-_messageSessionIndexBase();" in UI_JS
|
|
assert "data-session-msg-idx" in UI_JS
|
|
assert "data-message-anchor-key" in UI_JS
|
|
assert "function _messageViewportAnchorKeyForMessage" in UI_JS
|
|
assert "function _messageVisibleIndexForAnchorKey" in UI_JS
|
|
assert "function _remountMessageViewportAnchor" in UI_JS
|
|
assert "visibleKeyNode=anchorKey" in UI_JS
|
|
assert "visIdx=anchorKey?_messageVisibleIndexForAnchorKey(anchorKey,visWithIdx):-1" in UI_JS
|
|
assert "if(!restoredViaAnchor&&typeof _remountMessageViewportAnchor==='function'&&_remountMessageViewportAnchor(snapshot.anchor))" in UI_JS
|
|
assert "const rawFromSession=_messageRawIdxForSessionIndex(sessionIdx);" in UI_JS
|
|
|
|
|
|
def test_refresh_session_updates_message_window_offset_before_rerender():
|
|
refresh = _function_body(UI_JS, "function refreshSession")
|
|
messages_idx = refresh.index("S.messages = data.session.messages || [];")
|
|
truncated_idx = refresh.index("_messagesTruncated = !!data.session._messages_truncated;")
|
|
offset_idx = refresh.index("_oldestIdx = data.session._messages_offset || 0;")
|
|
render_idx = refresh.index("_renderMessagesWithScrollSnapshot();")
|
|
|
|
assert messages_idx < truncated_idx < offset_idx < render_idx
|