Skip to content

The agent that grows with you

fix(desktop): no-op re-reads keep identity; a genuine scroll ends a pending restore

teknium128e0eab9acf7
diff --git a/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts b/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts
index f05979d3065..6c7845a10aa 100644
--- a/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts
+++ b/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts
@@ -273,4 +273,28 @@ describe('useStatusSnapshot', () => {
expect(requestGatewayMock).not.toHaveBeenCalled()
})
+
+ it('keeps the same snapshot reference across a content-equal 60s re-read; a real change publishes', async () => {
+ vi.mocked(getStatus).mockImplementation(async () => ({ version: '1.0.0' }) as never)
+ const requestGateway = vi.fn().mockResolvedValue({}) as unknown as GatewayRequester
+
+ const { result } = renderHook(() => useStatusSnapshot('open', requestGateway))
+ await flushAsync()
+
+ const first = result.current.statusSnapshot
+ expect(first).toEqual({ version: '1.0.0' })
+
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(60_000)
+ })
+ expect(getStatus).toHaveBeenCalledTimes(2)
+ // Identity preserved on a no-op: consumers keyed on the snapshot must not re-render.
+ expect(result.current.statusSnapshot).toBe(first)
+
+ vi.mocked(getStatus).mockImplementation(async () => ({ version: '1.0.1' }) as never)
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(60_000)
+ })
+ expect(result.current.statusSnapshot).toEqual({ version: '1.0.1' })
+ })
})
diff --git a/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts b/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
index bf8b3b6b163..f21df6f7cfc 100644
--- a/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
+++ b/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
@@ -106,7 +106,11 @@ export function useStatusSnapshot(
}
if (statusResult.status === 'fulfilled') {
- setStatusSnapshot(statusResult.value)
+ const next = statusResult.value
+ // Preserve reference identity on a no-op: the 60s tick re-reads a
+ // usually-unchanged snapshot, and a fresh object for the same content
+ // re-renders every consumer for nothing.
+ setStatusSnapshot(previous => (JSON.stringify(previous) === JSON.stringify(next) ? previous : next))
}
} finally {
scheduleRefresh()
diff --git a/apps/desktop/src/components/assistant-ui/thread/list-restore-composer-resize.test.tsx b/apps/desktop/src/components/assistant-ui/thread/list-restore-composer-resize.test.tsx
index b937bd87912..cb2740071fc 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list-restore-composer-resize.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/list-restore-composer-resize.test.tsx
@@ -206,4 +206,47 @@ describe('list post-settle restore vs composer-only resize', () => {
)
)
})
+
+ it('a genuine scroll cancels a settled bottom restore, so a later resize tick cannot yank the view back', async () => {
+ const { container } = render(<ScrollHarness messages={sessionMessages('b')} sessionKey="b" />)
+ const vp = viewportEl(container)
+
+ await settleScroll()
+ expect(vp.scrollTop).toBe(SCROLL_H - CLIENT_H)
+
+ // Not a wheel/pointer/key gesture: a rail jump or find-in-page reveal only fires `scroll`.
+ act(() => {
+ vp.scrollTop = 1000
+ vp.dispatchEvent(new Event('scroll'))
+ })
+
+ viewportScrollHeight = SCROLL_H + 600
+ fireContentResizes()
+
+ expect(vp.scrollTop).toBe(1000)
+ })
+
+ it('a settled bottom target ignores a composer-only resize but still follows transcript growth', async () => {
+ const { container } = render(<ScrollHarness messages={sessionMessages('c')} sessionKey="c" />)
+ const vp = viewportEl(container)
+
+ await settleScroll()
+ expect(vp.scrollTop).toBe(SCROLL_H - CLIENT_H)
+
+ // The view sits off the bottom without any cancel gesture having fired.
+ vp.scrollTop = 1000
+
+ viewportScrollHeight = SCROLL_H + 80
+ viewportClientHeight = CLIENT_H - 80
+ clearanceHeight = 200
+ fireContentResizes()
+
+ expect(vp.scrollTop).toBe(1000)
+
+ // CONTROL: real transcript growth re-pins the bottom target.
+ viewportScrollHeight = SCROLL_H + 80 + 600
+ fireContentResizes()
+
+ expect(vp.scrollTop).toBe(viewportScrollHeight - viewportClientHeight)
+ })
})
diff --git a/apps/desktop/src/components/assistant-ui/thread/list.tsx b/apps/desktop/src/components/assistant-ui/thread/list.tsx
index 2f2cdb2caf9..889fbda2e10 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/list.tsx
@@ -914,8 +914,17 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
stopScroll()
+ // Every scrollTop this effect writes is read back here, so the `scroll`
+ // event it fires is recognised as ours below and cannot cancel the restore.
+ let ownScrollTop: number | null = null
+
+ const applyTarget = (node: HTMLElement) => {
+ node.scrollTop = threadScrollTargetTop(target, node)
+ ownScrollTop = node.scrollTop
+ }
+
applyRestoreRef.current = () => {
- el.scrollTop = threadScrollTargetTop(target, el)
+ applyTarget(el)
}
applyRestoreRef.current()
@@ -949,7 +958,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
stableFrames = height === lastHeight && !clamped ? stableFrames + 1 : 0
lastHeight = height
- node.scrollTop = threadScrollTargetTop(target, node)
+ applyTarget(node)
// Most session switches are synchronous and stabilize within 2 frames;
// the old 90-frame ceiling was for slow async image loads. Cap at 15
@@ -1009,13 +1018,11 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
// ResizeObserver runs before paint; waiting for the next settle rAF
// exposes a frame at the old offset when deferred markdown grows.
- // Reading offsets still ignore composer-only resizes once settled.
- if (
- !loadSettledRef.current ||
- target.kind === 'bottom' ||
- shouldReapplyFrozenThreadScrollOffset(target, true, previous, next)
- ) {
- el.scrollTop = threadScrollTargetTop(target, el)
+ // Once settled, only a transcript-row height change may re-pin — for a
+ // bottom target too: a composer-only resize (every keystroke) used to
+ // yank a view the user had moved away from the bottom back down.
+ if (!loadSettledRef.current || shouldReapplyFrozenThreadScrollOffset(target, true, previous, next)) {
+ applyTarget(el)
liveScrollStateRef.current = threadScrollStateFromMetrics(el)
}
})
@@ -1050,6 +1057,18 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
}
}
+ // A position neither this effect nor use-stick-to-bottom produced is the
+ // user's (scrollbar drag, rail jump, find-in-page reveal) and ends the
+ // restore like wheel input does — otherwise the next ResizeObserver tick
+ // would rewrite scrollTop over a genuine scroll.
+ const onScroll = () => {
+ if (el.scrollTop === ownScrollTop || el.scrollTop === threadScrollTargetTop(target, el)) {
+ return
+ }
+
+ cancelRestore()
+ }
+
jumpRestoreRef.current = () => {
cancelRestore()
// A jump replaces reading intent, including an in-flight prepend anchor.
@@ -1059,11 +1078,11 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
liveScrollStateRef.current = target
applyRestoreRef.current = () => {
- el.scrollTop = threadScrollTargetTop(target, el)
+ applyTarget(el)
}
loadSettledRef.current = true
- el.scrollTop = threadScrollTargetTop(target, el)
+ applyTarget(el)
if (contentRef.current) {
resizeObserver.observe(contentRef.current)
@@ -1073,6 +1092,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
}
el.addEventListener('wheel', onWheel, { passive: true })
+ el.addEventListener('scroll', onScroll, { passive: true })
el.addEventListener('pointerdown', cancelRestore, { passive: true })
el.addEventListener('keydown', cancelRestore)
@@ -1082,6 +1102,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
resizeObserver.disconnect()
jumpRestoreRef.current = null
el.removeEventListener('wheel', onWheel)
+ el.removeEventListener('scroll', onScroll)
el.removeEventListener('pointerdown', cancelRestore)
el.removeEventListener('keydown', cancelRestore)
cancelAnimationFrame(rafId)
diff --git a/apps/desktop/src/store/thread-scroll-restore.test.ts b/apps/desktop/src/store/thread-scroll-restore.test.ts
index eced33ebda9..b28aae3223b 100644
--- a/apps/desktop/src/store/thread-scroll-restore.test.ts
+++ b/apps/desktop/src/store/thread-scroll-restore.test.ts
@@ -35,11 +35,14 @@ describe('shouldReapplyFrozenThreadScrollOffset', () => {
expect(threadScrollTargetTop(OFFSET, next)).toBe(5600 - 600 - 800)
})
- it('leaves kind:bottom / following restore alone', () => {
+ it('re-pins a settled bottom target only on transcript growth, never on a composer-only resize', () => {
const previous = { clearanceHeight: 120, clientHeight: 600, scrollHeight: 5000 }
- const next = { clearanceHeight: 120, clientHeight: 600, scrollHeight: 5600 }
- expect(shouldReapplyFrozenThreadScrollOffset(BOTTOM, true, previous, next)).toBe(false)
+ expect(shouldReapplyFrozenThreadScrollOffset(BOTTOM, true, previous, { ...previous, scrollHeight: 5600 })).toBe(true)
+ expect(
+ shouldReapplyFrozenThreadScrollOffset(BOTTOM, true, previous, { clearanceHeight: 200, scrollHeight: 5080 })
+ ).toBe(false)
+ expect(shouldReapplyFrozenThreadScrollOffset(BOTTOM, true, previous, { ...previous, scrollHeight: 4000 })).toBe(false)
})
it('does not re-pin while the session-switch settle loop is still running', () => {
diff --git a/apps/desktop/src/store/thread-scroll.ts b/apps/desktop/src/store/thread-scroll.ts
index f8754c28212..0f7de1486bc 100644
--- a/apps/desktop/src/store/thread-scroll.ts
+++ b/apps/desktop/src/store/thread-scroll.ts
@@ -199,9 +199,10 @@ export function threadScrollTranscriptHeight(
}
/**
- * Post-settle restore RO may re-pin a frozen offset only when transcript
- * rows actually changed height. Composer clearance / viewport-box resizes
- * and no-op RO deliveries must not rewrite scrollTop.
+ * Post-settle restore RO may re-pin the restored target only when transcript
+ * rows actually changed height. Composer clearance / viewport-box resizes and
+ * no-op RO deliveries must not rewrite scrollTop — for a bottom target either,
+ * or every keystroke yanks a view the user moved off the bottom back down.
*/
export function shouldReapplyFrozenThreadScrollOffset(
target: ThreadScrollState,
@@ -209,11 +210,15 @@ export function shouldReapplyFrozenThreadScrollOffset(
previous: Pick<ThreadScrollRestoreResizeMetrics, 'clearanceHeight' | 'scrollHeight'>,
next: Pick<ThreadScrollRestoreResizeMetrics, 'clearanceHeight' | 'scrollHeight'>
): boolean {
- if (target.kind !== 'offset' || !settled) {
+ if (!settled) {
return false
}
- return Math.round(threadScrollTranscriptHeight(previous)) !== Math.round(threadScrollTranscriptHeight(next))
+ const before = Math.round(threadScrollTranscriptHeight(previous))
+ const after = Math.round(threadScrollTranscriptHeight(next))
+
+ // A bottom target follows growth only — a shrink is clamped by the browser.
+ return target.kind === 'bottom' ? after > before : after !== before
}
// Storage is scoped per profile with the same `.profile.<encoded>` suffix the