Skip to content

The agent that grows with you

fix(desktop): pin transcript viewport while text is selected (#115464)

whyyagswhy7ba1b4361aea
diff --git a/apps/desktop/src/components/assistant-ui/thread/list.test.ts b/apps/desktop/src/components/assistant-ui/thread/list.test.ts
index f69e0b633ed..c97776f2a94 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list.test.ts
+++ b/apps/desktop/src/components/assistant-ui/thread/list.test.ts
@@ -1,4 +1,4 @@
-import { afterEach, describe, expect, it, vi } from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
buildGroups,
@@ -7,6 +7,7 @@ import {
LIVE_TAIL_MIN_GROUPS,
LIVE_TAIL_PARTS,
liveTailStart,
+ hasTranscriptTextSelection,
type MessageGroup,
resolveThreadScrollTarget,
RUN_START_SNAP_THRESHOLD_PX,
@@ -416,3 +417,97 @@ describe('shouldRePinOnTranscriptReload', () => {
expect(shouldRePinOnTranscriptReload({ sessionSwitched: false, settledNonEmpty: false })).toBe(true)
})
})
+
+describe('hasTranscriptTextSelection', () => {
+ let transcript: HTMLDivElement
+ let outside: HTMLDivElement
+
+ beforeEach(() => {
+ transcript = document.createElement('div')
+ transcript.textContent = 'streamed transcript text'
+ outside = document.createElement('div')
+ outside.textContent = 'composer text'
+ document.body.append(transcript, outside)
+ })
+
+ afterEach(() => {
+ window.getSelection()?.removeAllRanges()
+ transcript.remove()
+ outside.remove()
+ })
+
+ const selectContents = (node: Node) => {
+ const range = document.createRange()
+ range.selectNodeContents(node)
+ window.getSelection()?.addRange(range)
+ }
+
+ it('returns false with no selection', () => {
+ expect(hasTranscriptTextSelection(transcript)).toBe(false)
+ })
+
+ it('returns false for a collapsed caret inside the transcript', () => {
+ const range = document.createRange()
+ range.setStart(transcript.firstChild!, 1)
+ range.collapse(true)
+ window.getSelection()?.addRange(range)
+
+ expect(window.getSelection()!.isCollapsed).toBe(true)
+ expect(hasTranscriptTextSelection(transcript)).toBe(false)
+ })
+
+ it('returns true for a non-collapsed selection inside the transcript', () => {
+ selectContents(transcript)
+
+ expect(window.getSelection()!.isCollapsed).toBe(false)
+ expect(hasTranscriptTextSelection(transcript)).toBe(true)
+ })
+
+ it('returns false when the selection is outside the transcript', () => {
+ selectContents(outside)
+
+ expect(window.getSelection()!.isCollapsed).toBe(false)
+ expect(hasTranscriptTextSelection(transcript)).toBe(false)
+ })
+})
+
+describe('resolveThreadScrollTarget while selecting', () => {
+ let scrollElement: HTMLDivElement
+
+ beforeEach(() => {
+ scrollElement = document.createElement('div')
+ scrollElement.textContent = 'streamed transcript text'
+ document.body.append(scrollElement)
+ // No layout in the test DOM: pin a scroll offset as an own property.
+ Object.defineProperty(scrollElement, 'scrollTop', { configurable: true, value: 100 })
+ })
+
+ afterEach(() => {
+ window.getSelection()?.removeAllRanges()
+ scrollElement.remove()
+ })
+
+ const contextFor = () => ({
+ contentElement: document.createElement('div'),
+ scrollElement
+ })
+
+ it('holds position instead of following while a selection lives in the transcript', () => {
+ const range = document.createRange()
+ range.selectNodeContents(scrollElement)
+ window.getSelection()?.addRange(range)
+
+ expect(resolveThreadScrollTarget(999, contextFor())).toBe(100)
+ })
+
+ it('resumes following once the selection collapses', () => {
+ const range = document.createRange()
+ range.selectNodeContents(scrollElement)
+ window.getSelection()?.addRange(range)
+ expect(resolveThreadScrollTarget(999, contextFor())).toBe(100)
+
+ window.getSelection()?.removeAllRanges()
+
+ expect(resolveThreadScrollTarget(999, contextFor())).toBe(999)
+ })
+})
diff --git a/apps/desktop/src/components/assistant-ui/thread/list.tsx b/apps/desktop/src/components/assistant-ui/thread/list.tsx
index 9d5c20ee9a5..5056d5f84d9 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/list.tsx
@@ -161,7 +161,39 @@ export const transcriptBackfillFrameCount = (
// streamed content normally.
const SCROLL_TARGET_EPSILON_PX = 0.5
+// True while the user holds a non-collapsed text selection inside the
+// transcript. use-stick-to-bottom only pauses for a selection while the mouse
+// button is still down — a selection that persists after mouse-up must also
+// pin the viewport, or streaming growth yanks it out from under the user
+// (#115464). A collapsed caret (or a selection outside the transcript, e.g.
+// in the composer) never pins.
+export function hasTranscriptTextSelection(scrollElement?: Element | null): boolean {
+ if (typeof document === 'undefined') {
+ return false
+ }
+
+ const selection = document.getSelection()
+
+ if (!selection || selection.isCollapsed || selection.rangeCount === 0) {
+ return false
+ }
+
+ if (!scrollElement) {
+ return true
+ }
+
+ const { anchorNode, focusNode } = selection
+
+ return Boolean(
+ (anchorNode && scrollElement.contains(anchorNode)) || (focusNode && scrollElement.contains(focusNode))
+ )
+}
+
export const resolveThreadScrollTarget: GetTargetScrollTop = (targetScrollTop, { scrollElement }) => {
+ if (hasTranscriptTextSelection(scrollElement)) {
+ return scrollElement.scrollTop
+ }
+
const currentScrollTop = scrollElement.scrollTop
const remaining = targetScrollTop - currentScrollTop
@@ -469,6 +501,20 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
targetScrollTop: resolveThreadScrollTarget
})
+ // #115464: explicit snaps go through the same selection pin as the resize
+ // follow above — without this a snap still flips isAtBottom and kicks an
+ // animation while the user is selecting transcript text.
+ const scrollToBottomUnlessSelecting = useCallback(
+ (...args: Parameters<typeof scrollToBottom>) => {
+ if (hasTranscriptTextSelection(scrollRef.current)) {
+ return undefined
+ }
+
+ return scrollToBottom(...args)
+ },
+ [scrollRef, scrollToBottom]
+ )
+
const { olderAvailable, expandWindow, isHistorical, returnToLatest } = useTranscriptWindow()
useEffect(() => {
@@ -698,10 +744,10 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
if (jumpRestoreRef.current) {
jumpRestoreRef.current()
} else {
- void scrollToBottom()
+ void scrollToBottomUnlessSelecting()
}
}, scrollSessionId),
- [scrollToBottom, scrollSessionId, isHistorical, returnToLatest]
+ [scrollToBottomUnlessSelecting, scrollSessionId, isHistorical, returnToLatest]
)
// Waking from display: hidden (HUD mode hides the main window; OS hide does
@@ -717,9 +763,9 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
() =>
subscribeToThreadForeground(
() => isAtBottom,
- () => void scrollToBottom()
+ () => void scrollToBottomUnlessSelecting()
),
- [isAtBottom, scrollToBottom]
+ [isAtBottom, scrollToBottomUnlessSelecting]
)
const endEditHold = useCallback(() => {
@@ -749,7 +795,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
const el = scrollRef.current
if (el && shouldSnapOnRunStart(el.scrollHeight - el.scrollTop - el.clientHeight)) {
- scrollToBottom()
+ scrollToBottomUnlessSelecting()
}
})
@@ -978,7 +1024,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
if (target.kind === 'bottom') {
// Hand back to use-stick-to-bottom locked, so late async growth
// (images, highlight) keeps following the bottom.
- void scrollToBottom('instant')
+ void scrollToBottomUnlessSelecting('instant')
loadSettledRef.current = true
} else if (clamped) {
// Content hasn't finished arriving (the backfill transition is still
@@ -1107,7 +1153,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
resizeObserver.observe(contentRef.current)
}
- void scrollToBottom('instant')
+ void scrollToBottomUnlessSelecting('instant')
}
el.addEventListener('wheel', onWheel, { passive: true })
@@ -1127,7 +1173,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
cancelAnimationFrame(rafId)
record()
}
- }, [contentRef, hasGroups, paneVisible, scrollRef, scrollStorageKey, scrollToBottom, sessionKey, stopScroll])
+ }, [contentRef, hasGroups, paneVisible, scrollRef, scrollStorageKey, scrollToBottomUnlessSelecting, sessionKey, stopScroll])
// A thread can mount with a run already active, without a runStart event.
useEffect(() => {