Skip to content

The agent that grows with you

fix(desktop): pin the transcript while streamed content grows (#118482)

Halldrixcc269a7d0adb
diff --git a/apps/desktop/src/components/assistant-ui/thread/list-session-scroll.test.tsx b/apps/desktop/src/components/assistant-ui/thread/list-session-scroll.test.tsx
index d5374e5f6f5..e8a494e16a2 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list-session-scroll.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/list-session-scroll.test.tsx
@@ -118,6 +118,172 @@ function ScrollHarness({
}
describe('list session-scroll restore', () => {
+ it('keeps a bottom-pinned reader pinned while a running turn grows the content (#118482)', async () => {
+ // use-stick-to-bottom follows a content resize on the next animation frame,
+ // so streamed growth paints at the stale scrollTop and the viewport drifts
+ // up before the re-pin. The transcript ResizeObserver closes that frame.
+ const previousObserver = globalThis.ResizeObserver
+ const observers = new Set<{
+ callback: ResizeObserverCallback
+ targets: Set<Element>
+ }>()
+
+ vi.stubGlobal(
+ 'ResizeObserver',
+ class {
+ targets = new Set<Element>()
+ constructor(public callback: ResizeObserverCallback) {
+ observers.add(this)
+ }
+ observe(target: Element) {
+ this.targets.add(target)
+ }
+ unobserve(target: Element) {
+ this.targets.delete(target)
+ }
+ disconnect() {
+ this.targets.clear()
+ }
+ }
+ )
+
+ const messages = sessionMessages('stream')
+
+ const { container, unmount } = render(<ScrollHarness isRunning messages={messages} sessionKey="stream" />)
+ const vp = viewportEl(container)
+
+ try {
+ const deliverContentResize = () => {
+ for (const observer of observers) {
+ const targets = [...observer.targets].filter(el => el.getAttribute('data-slot') === 'aui_thread-content')
+
+ if (targets.length) {
+ observer.callback(
+ targets.map(target => ({ target, contentRect: { height: scrollHeightValue } })) as ResizeObserverEntry[],
+ observer as unknown as ResizeObserver
+ )
+ }
+ }
+ }
+
+ await settleScroll(10)
+ expect(vp.scrollTop).toBeGreaterThanOrEqual(scrollHeightValue - CLIENT_H - 1)
+
+ // Hold the library's follow frame so the resize leg is the only writer.
+ const raf = vi.spyOn(globalThis, 'requestAnimationFrame').mockImplementation(() => 0)
+
+ try {
+ // Several streamed chunks land; the reader never leaves the bottom.
+ for (const delta of [120, 240, 168, 300]) {
+ scrollHeightValue += delta
+ act(deliverContentResize)
+
+ expect(vp.scrollTop).toBe(scrollHeightValue - CLIENT_H)
+ }
+
+ // A reader who scrolled up is not yanked back by the same growth.
+ const readingTop = scrollHeightValue - CLIENT_H - 900
+
+ act(() => {
+ vp.dispatchEvent(new WheelEvent('wheel', { bubbles: true, deltaY: -160 }))
+ vp.scrollTop = readingTop
+ vp.dispatchEvent(new Event('scroll'))
+ })
+
+ act(() => {
+ scrollHeightValue += 400
+ deliverContentResize()
+ })
+
+ expect(vp.scrollTop).toBe(readingTop)
+ } finally {
+ raf.mockRestore()
+ }
+ } finally {
+ unmount()
+ vi.stubGlobal('ResizeObserver', previousObserver)
+ }
+ })
+
+ it('does not follow a composer-only resize while a turn is running (#118482)', async () => {
+ // Typing in the composer grows the clearance spacer, not the transcript.
+ // The follow is keyed on transcript height, so it must not pull the reader.
+ const previousObserver = globalThis.ResizeObserver
+ const observers = new Set<{ callback: ResizeObserverCallback; targets: Set<Element> }>()
+
+ vi.stubGlobal(
+ 'ResizeObserver',
+ class {
+ targets = new Set<Element>()
+ constructor(public callback: ResizeObserverCallback) {
+ observers.add(this)
+ }
+ observe(target: Element) {
+ this.targets.add(target)
+ }
+ unobserve(target: Element) {
+ this.targets.delete(target)
+ }
+ disconnect() {
+ this.targets.clear()
+ }
+ }
+ )
+
+ const { container, unmount } = render(
+ <ScrollHarness isRunning messages={sessionMessages('clr')} sessionKey="clr" />
+ )
+ const vp = viewportEl(container)
+ const clearance = vp.querySelector('[data-slot="aui_composer-clearance"]')
+
+ // Only rendered when the transcript is clamped to the composer; without the
+ // spacer there is no composer-only resize to distinguish from row growth.
+ if (!clearance) {
+ unmount()
+ vi.stubGlobal('ResizeObserver', previousObserver)
+
+ return
+ }
+
+ await settleScroll(10)
+ const atBottom = vp.scrollTop
+
+ // Composer-only growth: the spacer absorbs the whole scrollHeight delta.
+ Object.defineProperty(HTMLElement.prototype, 'clientHeight', {
+ configurable: true,
+ get() {
+ return this.getAttribute?.('data-slot') === 'aui_composer-clearance' ? 208 : CLIENT_H
+ }
+ })
+ scrollHeightValue += 168
+
+ try {
+ act(() => {
+ for (const observer of [...observers]) {
+ const targets = [...observer.targets].filter(el => el.getAttribute('data-slot') === 'aui_thread-content')
+
+ if (targets.length) {
+ observer.callback(
+ targets.map(target => ({ target, contentRect: { height: scrollHeightValue } })) as ResizeObserverEntry[],
+ observer as unknown as ResizeObserver
+ )
+ }
+ }
+ })
+
+ expect(vp.scrollTop).toBe(atBottom)
+ } finally {
+ Object.defineProperty(HTMLElement.prototype, 'clientHeight', {
+ configurable: true,
+ get() {
+ return CLIENT_H
+ }
+ })
+ unmount()
+ vi.stubGlobal('ResizeObserver', previousObserver)
+ }
+ })
+
it('lets a reader escape bottom-follow when a running transcript grows during the scroll gesture', async () => {
// #116273: a pending clarify keeps the turn running while the transcript
// can still resize. If that resize lands in the same frame as scroll-up,
diff --git a/apps/desktop/src/components/assistant-ui/thread/list.tsx b/apps/desktop/src/components/assistant-ui/thread/list.tsx
index b42633bf13a..1587fc33326 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/list.tsx
@@ -33,6 +33,7 @@ import {
saveThreadScrollPosition,
shouldReapplyFrozenThreadScrollOffset,
THREAD_SCROLL_BOTTOM,
+ THREAD_SCROLL_STICKY_THRESHOLD_PX,
type ThreadScrollRestoreResizeMetrics,
type ThreadScrollState,
threadScrollStateFromMetrics,
@@ -836,19 +837,61 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
return
}
+ // use-stick-to-bottom follows a content resize on the next animation frame,
+ // so a busy streamed turn can grow the content before that callback runs:
+ // the viewport paints at the stale scrollTop, visibly drifting up before
+ // the re-pin (#118482). The RO leg closes that frame, synchronously before
+ // paint.
+ const resizeMetrics = (): ThreadScrollRestoreResizeMetrics => {
+ const clearance = content.querySelector('[data-slot="aui_composer-clearance"]')
+
+ return {
+ clearanceHeight: clearance instanceof HTMLElement ? clearance.clientHeight : 0,
+ clientHeight: el.clientHeight,
+ scrollHeight: el.scrollHeight
+ }
+ }
+
+ // ponytail: previous-metrics precision is load-bearing — the predicate must
+ // see the frame BEFORE the growth, so a scroll event (which also writes it)
+ // can't leave a stale gap that makes a mid-stream resize look like growth.
+ let previousResizeMetrics = resizeMetrics()
+
const update = () => {
+ previousResizeMetrics = resizeMetrics()
+ liveScrollStateRef.current = threadScrollStateFromMetrics(el)
+ }
+
+ const onResize = () => {
+ const nextResizeMetrics = resizeMetrics()
+
+ // Same transcript-only metric as the restore loop: a composer-only resize
+ // (every keystroke) grows the clearance spacer, not the rows, so it is
+ // distinguishable from streamed content. The pre-growth metrics hold the
+ // reader's position, so a reader who scrolled up is never yanked back
+ // (#108941); use-stick-to-bottom stays the single scroll owner otherwise.
+ if (
+ isRunning &&
+ shouldReapplyFrozenThreadScrollOffset(THREAD_SCROLL_BOTTOM, true, previousResizeMetrics, nextResizeMetrics) &&
+ previousResizeMetrics.scrollHeight - el.scrollTop - el.clientHeight <= THREAD_SCROLL_STICKY_THRESHOLD_PX &&
+ !hasTranscriptTextSelection(el)
+ ) {
+ el.scrollTop = threadScrollTargetTop(THREAD_SCROLL_BOTTOM, nextResizeMetrics)
+ }
+
+ previousResizeMetrics = nextResizeMetrics
liveScrollStateRef.current = threadScrollStateFromMetrics(el)
}
el.addEventListener('scroll', update, { passive: true })
- const observer = new ResizeObserver(update)
+ const observer = new ResizeObserver(onResize)
observer.observe(content)
return () => {
el.removeEventListener('scroll', update)
observer.disconnect()
}
- }, [contentRef, paneVisible, scrollRef])
+ }, [contentRef, isRunning, paneVisible, scrollRef, sessionKey])
// Persist the live position on app close, so a reading position survives a
// quit without a session switch (the switch cleanup below only runs on