Files
hermes-webui/tests/test_issue2246_question_jump.py
T
3a95d82531 fix(chat): prevent first response jump from snapping back (#6621)
* fix(chat): keep response jump position after session load

Cancel pending load-time bottom settling before an explicit response jump takes scroll ownership. Add a regression test covering the first-click race.

* fix: gate response jump ownership by destination

* fix: keep response jumps programmatic through smooth scroll

Native smooth-scroll frames from response jumps could reach the manual scroll listener and claim reader ownership before the final destination was known.

- own response-jump scrolling with a generation- and session-scoped lifecycle
- reconcile sticky ownership from final 80px tail geometry
- preserve current low-delta wheel takeover semantics when integrating with master
- cover visible assistant, user-row, and virtualized paths through the production scroll listener

* fix(scroll): hold reader off-bottom during jump owner + cancel jump on explicit End (#6621 gate fixes)

Codex gate found two defects in the jump-scroll ownership mechanism:
- Jump during an active stream snapped back to bottom on the next token: the
  owner suppressed the scroll listener but left the pre-jump pinned state, so
  scrollIfPinned() reclaimed the bottom. Now _beginMessageJumpScroll unpins for
  the ownership window and scrollIfPinned() no-ops while an owner exists.
- An explicit End click could be undone by the pending jump reconcile restoring
  the stale unpinned snapshot. scrollToBottom() now cancels the active jump
  owner first; _cancelMessageJumpScroll restores the preserved snapshot so a
  non-reconcile cancel doesn't leak the transient unpinned state.

* fix(scroll): drop undefined currentSid global ref in _messageJumpSessionId (#6621 brick-class scope-undef gate)

The PR's _messageJumpSessionId() referenced a bare 'currentSid' global that
does not exist in ui.js (everywhere else it's a local const from
S.session.session_id). test_static_js_scope_undef flagged it brick-class
(#3696). The S.session.session_id fallback already IS the canonical accessor;
removed the dead first line and updated the harness to drive the session-change
case via S.session.session_id.

* fix(scroll): round-2 gate fixes for jump-owner state transitions (#6621)

Re-gate found three more state-transition edge cases from the temp-unpin window:
- wheel-up interrupting an active jump owner after the programmatic latch
  expires now explicitly establishes the unpinned reader-owned state (was
  cancelling the jump but leaving pinned -> next token snapped to bottom).
- _resetStreamScrollFollow() now cancels the jump owner FIRST, before its
  pinned-state assignments, so a stream starting mid-jump can't have its pin
  undone by the snapshot restore -> auto-follow stays enabled.
- _finishMessageJumpScroll() flushes a deferred external-session refresh after
  reconciliation when the terminal state is pinned to the tail, so a refresh
  deferred during the temp-unpin window isn't stranded.

* fix(scroll): make jump-owner guards typeof-safe + widen static test window (#6621)

The two _messageJumpScrollOwner guards (scrollIfPinned, scrollToBottom) are
pulled into other scroll test harnesses (test_issue6414, test_issue4856) that
stub the scroll env without declaring the new global; bare references threw
ReferenceError. Guard with typeof (matches the PR's own jumpScrollOwned check).
Also widen test_low_delta_wheel_intent_is_tracked_separately's source-slice
window 1400->1800 to still contain the (unchanged) _lastMessageWheelIntentMs
line after the +9-line wheel-up-during-jump block was inserted.

* test(scroll): add streaming-frame-hold + wheel-during-jump regression tests (#6621 Fable finding ii)

Fable UX gate flagged that the PR's tests model only the stale load-time settle
callback, not the streaming case. Add two node-harness tests:
- a streaming render frame (scrollIfPinned) fired inside the jump-owner window
  must not snap the reader to the bottom (holds at target, 0 bottom-writes).
- a gentle wheel-up during the owner window after the programmatic latch stales
  hands ownership to the reader UNPINNED at their position, never pinned
  mid-transcript.

* fix(scroll): widen jump-owner takeover to downward + touch input (#6621 Fable S3)

Fable's re-review (on the fixed code) confirmed the streaming blocker + wheel-up
concern resolved, and probe-proved one remaining narrow regression: a gentle
wheel-DOWN or touch scroll during the owner window cancelled the owner and
restored the pinned snapshot but did NOT re-unpin (the takeover was gated on
wheelUp only), so the next streaming token yanked the reader to the bottom.
Widen the takeover to any message-pane scroll input during the owner window.
Add a wheel-down regression test; widen the #4970 static-slice window to 2000
to still contain the (unchanged) _lastMessageWheelIntentMs line.

---------

Co-authored-by: pxxD1998 <214340659+pxxD1998@users.noreply.github.com>
Co-authored-by: nesquena-hermes <nesquena-hermes@users.noreply.github.com>
2026-08-13 21:17:42 -07:00

110 lines
6.4 KiB
Python

"""Regression coverage for #2246 per-turn jump-to-question buttons."""
from pathlib import Path
REPO = Path(__file__).resolve().parents[1]
UI_JS = (REPO / "static" / "ui.js").read_text(encoding="utf-8")
STYLE_CSS = (REPO / "static" / "style.css").read_text(encoding="utf-8")
I18N_JS = (REPO / "static" / "i18n.js").read_text(encoding="utf-8")
def test_assistant_footer_gets_completed_turn_question_jump_button():
assert "function _questionJumpButtonHtml(questionRawIdx, assistantRawIdx)" in UI_JS
assert "function jumpToTurnQuestion(questionRawIdx, assistantRawIdx)" in UI_JS
assert "const questionRawIdxByAssistantRawIdx=new Map()" in UI_JS
assert "questionRawIdxByAssistantRawIdx.set(entry.rawIdx,lastQuestionRawIdx)" in UI_JS
assert "row.id=_userMessageDomId(rawIdx)" in UI_JS
assert "const isTurnFinalAssistant=!isUser&&(!nextRendered||!nextRendered.m||nextRendered.m.role!=='assistant')" in UI_JS
# #3114 superseded the turn-final-only gate: the jump-to-question button now
# renders on every assistant message that has a resolvable question target,
# not just the turn-final one (multi-step turns otherwise lost the affordance
# on intermediate assistant bubbles). The button is gated on a non-null
# resolved target instead of isTurnFinalAssistant.
assert "const _qJumpTarget=(!isUser&&!m._live)?questionRawIdxByAssistantRawIdx.get(rawIdx):undefined;" in UI_JS
assert "const questionJumpBtn = (_qJumpTarget!==undefined&&_qJumpTarget!==null)" in UI_JS
assert "_questionJumpButtonHtml(_qJumpTarget, assistantRawIdxByQuestionRawIdx.get(_qJumpTarget)??rawIdx)" in UI_JS
assert "msg-question-jump-btn session-jump-btn session-jump-btn--inline" in UI_JS
def test_multi_segment_turn_jumps_to_first_assistant_segment():
# #3852: the reverse map assistantRawIdxByQuestionRawIdx resolves the FIRST
# assistant segment for a given question so multi-step turns (tool_call ->
# assistant -> tool_call -> assistant) scroll to the start of the response.
assert "const assistantRawIdxByQuestionRawIdx=new Map()" in UI_JS
assert "if(!assistantRawIdxByQuestionRawIdx.has(qIdx)) assistantRawIdxByQuestionRawIdx.set(qIdx,aIdx)" in UI_JS
assert "assistantRawIdxByQuestionRawIdx.get(_qJumpTarget)" in UI_JS
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 "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
assert "row.scrollIntoView({block:'center',behavior:'smooth'})" in UI_JS
assert "_highlightQuestionRow(row)" in UI_JS
assert "msg-question-highlight" in UI_JS
def test_question_jump_button_matches_bottom_button_size_on_mobile():
assert ".msg-question-jump-btn" in STYLE_CSS
assert ".session-jump-btn--inline" in STYLE_CSS
assert "height: 32px;" in STYLE_CSS
assert "min-width: 32px;" in STYLE_CSS
assert "padding: 0 11px;" in STYLE_CSS
assert "margin-left: auto;" in STYLE_CSS
assert ".msg-question-highlight .msg-body" in STYLE_CSS
assert "@keyframes question-highlight-pulse" in STYLE_CSS
assert "@media (max-width: 600px)" in STYLE_CSS
assert "width: 32px;" in STYLE_CSS
assert "max-width: 32px;" in STYLE_CSS
assert ".msg-question-jump-btn span:last-child { display: none; }" in STYLE_CSS
def test_question_jump_footer_is_discoverable_on_desktop_without_exposing_actions():
# Desktop cannot rely on the mobile always-visible footer override, but the
# per-turn jump affordance is navigation, not quiet action chrome. Keep only
# the jump button discoverable at rest; reveal timestamp/actions on hover or
# keyboard focus.
block_anchor = "Desktop/tablet keeps the per-turn jump affordance discoverable"
block_start = STYLE_CSS.index(block_anchor)
block_end = STYLE_CSS.index(".assistant-turn .msg-foot-with-usage", block_start)
desktop_jump_block = STYLE_CSS[block_start:block_end]
assert "@media (min-width: 641px)" in desktop_jump_block
assert ".assistant-turn .msg-foot:has(.msg-question-jump-btn)" in desktop_jump_block
assert "opacity: 1;" in desktop_jump_block
assert ".msg-foot:has(.msg-question-jump-btn) .msg-time" in desktop_jump_block
assert ".msg-foot:has(.msg-question-jump-btn) .msg-actions" in desktop_jump_block
assert "pointer-events: none;" in desktop_jump_block
assert ".assistant-turn:hover .msg-foot:has(.msg-question-jump-btn) .msg-actions" in desktop_jump_block
assert ".assistant-turn:focus-within .msg-foot:has(.msg-question-jump-btn) .msg-actions" in desktop_jump_block
assert "pointer-events: auto;" in desktop_jump_block
def test_question_jump_text_is_localized():
for key in ("jump_to_question", "jump_to_question_label"):
assert I18N_JS.count(f"{key}:") >= 12
def test_question_jump_skips_hidden_first_segment_and_falls_back():
# #3934 (Codex gate): a single assistant rawIdx can render multiple segment
# nodes, and the first can be display:none (assistant-segment-worklog-source /
# assistant-segment-anchor). scrollIntoView() on a hidden node silently no-ops,
# so the jump must only treat a VISIBLE segment as a successful target and
# otherwise fall through to the question-row fallback instead of suppressing it.
# Guard against regressing back to the unconditional first-match return.
assert "querySelectorAll('[data-msg-idx=\"'+assistantRawIdx+'\"]')" in UI_JS
assert "seg.getClientRects().length>0" in UI_JS
# The single-element querySelector + unconditional return must be gone.
assert "const seg=hasAssistant?container.querySelector('[data-msg-idx=\"'+assistantRawIdx+'\"]'):null;" not in UI_JS
# display:none on the hidden segment classes is what makes the guard necessary.
assert ".assistant-segment-worklog-source{" in STYLE_CSS
assert ".assistant-segment-anchor { display: none; }" in STYLE_CSS