The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): anchor the first-paint backfill so long sessions stop lurching (#99920)
teknium169fd61b0efbe
diff --git a/apps/desktop/src/components/assistant-ui/thread/list-auto-show-earlier.test.tsx b/apps/desktop/src/components/assistant-ui/thread/list-auto-show-earlier.test.tsxindex 04e35c6a9a6..dd4632b1145 100644--- a/apps/desktop/src/components/assistant-ui/thread/list-auto-show-earlier.test.tsx+++ b/apps/desktop/src/components/assistant-ui/thread/list-auto-show-earlier.test.tsx@@ -9,12 +9,15 @@ import { Thread } from '.'stubThreadEnvironment()stubThreadViewportSize()-const SCROLL_H = 20000const CLIENT_H = 600+// A prepend grows the content: the backfill anchor is consumed only by a+// taller tree, so the fixture's scrollHeight must track what is mounted.+const GROUP_H = 400+const scrollHeightNow = () => CLIENT_H + document.querySelectorAll('[data-slot="aui_message-group"]').length * GROUP_HObject.defineProperty(HTMLElement.prototype, 'scrollHeight', {configurable: true,- get: () => SCROLL_H+ get: scrollHeightNow})Object.defineProperty(HTMLElement.prototype, 'clientHeight', {configurable: true,@@ -101,7 +104,7 @@ describe('top-edge auto Show earlier', () => {expect(windowed).toBeLessThan(turns)// Leave the bottom lock the way a reader does: a scroll-up event.- viewport.scrollTop = SCROLL_H - CLIENT_H+ viewport.scrollTop = scrollHeightNow() - CLIENT_Hact(() => viewport.dispatchEvent(new Event('scroll')))viewport.scrollTop = 2000act(() => viewport.dispatchEvent(new Event('scroll')))diff --git a/apps/desktop/src/components/assistant-ui/thread/list.test.ts b/apps/desktop/src/components/assistant-ui/thread/list.test.tsindex 7518ea12b0e..7979a6e583f 100644--- a/apps/desktop/src/components/assistant-ui/thread/list.test.ts+++ b/apps/desktop/src/components/assistant-ui/thread/list.test.ts@@ -10,6 +10,7 @@ import {type MessageGroup,resolveThreadScrollTarget,RUN_START_SNAP_THRESHOLD_PX,+ shouldAnchorBeforePrepend,shouldClampTranscriptBudget,shouldRePinOnTranscriptReload,shouldSnapOnRunStart,@@ -115,6 +116,21 @@ describe('shouldClampTranscriptBudget', () => {})})+describe('shouldAnchorBeforePrepend', () => {+ // Regression for #99920: the settle loop hands a bottom-pinned load back at+ // the first-paint height, BEFORE the backfill commits; skipping the anchor+ // there left the prepend re-pinned only by a ResizeObserver frames later —+ // a full-viewport lurch on every long-session switch.+ it('anchors an unsettled load that is pinned to the bottom', () => {+ expect(shouldAnchorBeforePrepend(false, { kind: 'bottom' })).toBe(true)+ })++ it('never anchors an unsettled offset restore still being applied', () => {+ expect(shouldAnchorBeforePrepend(false, { fromBottom: 3000, kind: 'offset' })).toBe(false)+ expect(shouldAnchorBeforePrepend(true, { fromBottom: 3000, kind: 'offset' })).toBe(true)+ })+})+describe('buildGroups', () => {it('returns no groups for an empty signature', () => {expect(buildGroups('')).toEqual([])diff --git a/apps/desktop/src/components/assistant-ui/thread/list.tsx b/apps/desktop/src/components/assistant-ui/thread/list.tsxindex f1540e239e7..edd63de406f 100644--- a/apps/desktop/src/components/assistant-ui/thread/list.tsx+++ b/apps/desktop/src/components/assistant-ui/thread/list.tsx@@ -122,6 +122,16 @@ export const transcriptPaneBudget = (mountedPanes: number, hidden: boolean): num// is a no-op. Parked panes are unmounted, so they never hit this path.export const shouldClampTranscriptBudget = (hidden: boolean, renderBudget: number, paneBudget: number): boolean =>hidden && renderBudget > paneBudget++// Whether a backfill step may record its distance-from-bottom anchor. A+// settled load has a position the user chose. An UNSETTLED load only has one+// when it is pinned to the bottom: the settle loop rewrites scrollTop to the+// bottom every frame, so the measured distance is the truth and the restore+// effect re-pins in the same commit the taller tree lands in. An unsettled+// OFFSET load is still being applied — recording it would clobber the+// remembered offset with a way-point (#99920 regressed exactly this way).+export const shouldAnchorBeforePrepend = (settled: boolean, target: ThreadScrollState): boolean =>+ settled || target.kind === 'bottom'// Units the backfill adds per committed step (see the backfill effect). A// 60-unit step produced ~10 visible prepend frames after FIRST_PAINT_BUDGET// retune (#83681). 290 fills a 600-unit page in two interruptible commits —@@ -492,10 +502,19 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({// Where to land after a prepend, in distance-from-bottom (survives the// height change). Shared by "Show earlier" and the budget backfill below.const restoreFromBottomRef = useRef<number | null>(null)+ // scrollHeight when the anchor was recorded. The restore effect below also+ // runs on commits that do not grow the content (the transcript arriving+ // under the first-paint budget; a "Show earlier" whose page comes from the+ // store a commit later); consuming the anchor there spends it as a no-op and+ // leaves the real prepend unanchored. Only a taller tree is the prepend.+ const anchorHeightRef = useRef<number | null>(null)// False from a session switch until the settle loop below parks the// transcript at its true bottom. While false, scrollTop is a way-point of a// load in progress, not a reading position anyone chose — never anchor to it.const loadSettledRef = useRef(false)+ // What the in-flight load is steering toward; decides whether a backfill+ // step may anchor while the load is still unsettled.+ const loadTargetRef = useRef<ThreadScrollState>(THREAD_SCROLL_BOTTOM)const cancelRestoreRef = useRef<(() => void) | null>(null)const isRunning = useAuiState(s => s.thread.isRunning)// Session the settle loop last armed for, so a re-arm within the same load@@ -503,23 +522,58 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({const settleKeyRef = useRef(sessionKey)// Record where the view should land once a prepend has grown the content,- // measured from the BOTTOM so the added height doesn't invalidate it. Only a- // settled load has an offset the user chose; while the settle loop is still- // running, scrollTop is a way-point of a load in progress (or a restored- // offset the loop is applying) — never anchor to it. Recording 0 here would- // make the restore effect clobber a restored offset with the bottom once the- // backfill lands; the settle loop re-writes its own target every frame, so- // skipping is safe.+ // measured from the BOTTOM so the added height doesn't invalidate it. A+ // bottom-pinned load anchors even before it settles: the settle loop hands+ // back at the FIRST-PAINT height, before the backfill transition commits, so+ // without an anchor that commit prepends thousands of px with nothing+ // re-pinning the view until use-stick-to-bottom's ResizeObserver catches up+ // frames later — the full-viewport lurch of #99920. An unsettled OFFSET load+ // never anchors: the settle loop is still applying the remembered offset,+ // and parks it itself on a clamped exit.const anchorBeforePrepend = useCallback(() => {const el = scrollRef.current- if (!el || !loadSettledRef.current) {+ if (!el || !shouldAnchorBeforePrepend(loadSettledRef.current, loadTargetRef.current)) {return}restoreFromBottomRef.current = el.scrollHeight - el.scrollTop+ anchorHeightRef.current = el.scrollHeight}, [scrollRef])+ // Weights (part count + visible character cost) fold into the BUDGET only.+ // Group identity stays structural, so a streaming append re-runs this cheap+ // sum — not the row JSX. Settled content hits messagePaintWeight's WeakMap.+ const weightedGroups = useMemo(() => {+ const weights = weightSignature.split(',').map(w => Number(w) || 1)++ return groups.map(group => ({+ ...group,+ weight:+ group.kind === 'turn'+ ? group.indices.reduce((sum, index) => sum + (weights[index] ?? 1), 0)+ : (weights[group.index] ?? 1)+ }))+ }, [groups, weightSignature])++ // The turn floor applies to a real page only. During the first-paint budget+ // the point is a small synchronous commit; forcing 8 turns into it would put+ // back exactly the freeze FIRST_PAINT_BUDGET exists to avoid, and the rAF+ // backfill a frame later fills them in anyway.+ const hiddenCount = firstVisibleGroupIndex(+ weightedGroups,+ renderBudget,+ renderBudget >= paneBudget ? MIN_VISIBLE_GROUPS : 0+ )++ // Memoized for IDENTITY, not to save the slice: `rows` below keys off this+ // array, and an inline slice handed it a fresh array every render — so the+ // moment a transcript outgrew the render budget (hiddenCount > 0), every+ // streamed token rebuilt every visible row's JSX and re-rendered the whole+ // mounted transcript. Under the budget the raw `groups` identity made the+ // memo hold; heavy sessions lost it exactly when they could least afford to.+ const visibleGroups = useMemo(() => (hiddenCount > 0 ? groups.slice(hiddenCount) : groups), [groups, hiddenCount])+// Backfill from FIRST_PAINT_BUDGET to the full budget after the small// commit painted — as a TRANSITION, so the heavy markdown + syntax// highlight render of the older turns is interruptible instead of one long@@ -535,8 +589,12 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({// interrupting the transition, which finally landed whole, seconds later,// mid-stream). Each step commits at most BACKFILL_STEP units; the effect// re-arms off the committed budget, so steps pace one per frame.+ //+ // Nothing to backfill while the transcript is still empty (a COLD switch);+ // an anchor measured against the empty viewport would be consumed by the+ // first-paint commit and leave the real prepend unanchored.useEffect(() => {- if (renderBudget >= paneBudget) {+ if (!hasGroups || renderBudget >= paneBudget) {return}@@ -546,8 +604,12 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({// it in the same commit the taller tree lands in — otherwise the view is// stranded near the TOP until use-stick-to-bottom's ResizeObserver// catches up a frame or two later (measured: an 11.5k px jump showing- // ~160ms of unrelated old turns, on every session load).- anchorBeforePrepend()+ // ~160ms of unrelated old turns, on every session load). A step with+ // nothing left hidden prepends nothing: an anchor recorded for it would+ // never be consumed and would re-pin a later append instead.+ if (hiddenCount > 0) {+ anchorBeforePrepend()+ }// Functional max, not a plain set: an urgent "Show earlier" click can// land between scheduling and committing this transition, and a plain@@ -556,40 +618,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({})return () => cancelAnimationFrame(rafId)- }, [anchorBeforePrepend, paneBudget, renderBudget])-- // Weights (part count + visible character cost) fold into the BUDGET only.- // Group identity stays structural, so a streaming append re-runs this cheap- // sum — not the row JSX. Settled content hits messagePaintWeight's WeakMap.- const weightedGroups = useMemo(() => {- const weights = weightSignature.split(',').map(w => Number(w) || 1)-- return groups.map(group => ({- ...group,- weight:- group.kind === 'turn'- ? group.indices.reduce((sum, index) => sum + (weights[index] ?? 1), 0)- : (weights[group.index] ?? 1)- }))- }, [groups, weightSignature])-- // The turn floor applies to a real page only. During the first-paint budget- // the point is a small synchronous commit; forcing 8 turns into it would put- // back exactly the freeze FIRST_PAINT_BUDGET exists to avoid, and the rAF- // backfill a frame later fills them in anyway.- const hiddenCount = firstVisibleGroupIndex(- weightedGroups,- renderBudget,- renderBudget >= paneBudget ? MIN_VISIBLE_GROUPS : 0- )-- // Memoized for IDENTITY, not to save the slice: `rows` below keys off this- // array, and an inline slice handed it a fresh array every render — so the- // moment a transcript outgrew the render budget (hiddenCount > 0), every- // streamed token rebuilt every visible row's JSX and re-rendered the whole- // mounted transcript. Under the budget the raw `groups` identity made the- // memo hold; heavy sessions lost it exactly when they could least afford to.- const visibleGroups = useMemo(() => (hiddenCount > 0 ? groups.slice(hiddenCount) : groups), [groups, hiddenCount])+ }, [anchorBeforePrepend, hasGroups, hiddenCount, paneBudget, renderBudget])// Where the always-rendered live tail begins. Derived from the WEIGHTED// groups (render cost, not turns) so the tail is a viewport's worth of content —@@ -783,15 +812,13 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({// DOM collapse clamps scrollTop to garbage, so forget the restore gate —// when content (re)arrives, reapply from memory. The previous session's// real state was already recorded by its own cleanup just before this.- // An anchor captured for the OUTGOING transcript must not be applied to- // this one — a switch owns the position outright. The empty→non-empty- // re-arm is the SAME load, whose in-flight anchor is still correct.+ // Whatever anchor exists was measured against the tree that just+ // collapsed (the OUTGOING transcript, or stale rows shown under this key+ // before the swap) and would re-pin the arriving one to garbage. The+ // re-arm below restores from the remembered position instead.loadSettledRef.current = false-- if (settleKeyRef.current !== sessionKey) {- settleKeyRef.current = sessionKey- restoreFromBottomRef.current = null- }+ settleKeyRef.current = sessionKey+ restoreFromBottomRef.current = nullreturn record}@@ -807,6 +834,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({// The previous session's parting state must not leak into this one: from// here every scroll/RO event describes the restored session.liveScrollStateRef.current = target+ loadTargetRef.current = targetstopScroll()el.scrollTop = threadScrollTargetTop(target, el)@@ -860,6 +888,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({// can't be overwritten by a mid-load anchor measurement. The restore// effect flips settled once it consumes the parked value.restoreFromBottomRef.current = target.fromBottom + node.clientHeight+ anchorHeightRef.current = height} else {loadSettledRef.current = true}@@ -998,7 +1027,12 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({const el = scrollRef.currentconst restoreFromBottom = restoreFromBottomRef.current- if (el && restoreFromBottom != null && el.scrollHeight >= restoreFromBottom) {+ if (+ el &&+ restoreFromBottom != null &&+ el.scrollHeight > (anchorHeightRef.current ?? 0) &&+ el.scrollHeight >= restoreFromBottom+ ) {el.scrollTop = el.scrollHeight - restoreFromBottomrestoreFromBottomRef.current = null// Consuming a parked offset (clamped-exit) means the view just landed at