Skip to content

The agent that grows with you

fix(desktop): jump-button message count settles after a timeline rail jump

teknium173f3f64eb9ca
diff --git a/apps/desktop/src/components/assistant-ui/thread/use-messages-below.test.tsx b/apps/desktop/src/components/assistant-ui/thread/use-messages-below.test.tsx
index ae58614ee97..73167b37d37 100644
--- a/apps/desktop/src/components/assistant-ui/thread/use-messages-below.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/use-messages-below.test.tsx
@@ -56,17 +56,17 @@ describe('messages below the viewport', () => {
return vi.spyOn(message, 'getBoundingClientRect')
})
- expect(countMessagesBelow(viewport, content)).toBe(3)
+ expect(countMessagesBelow(viewport, content).count).toBe(3)
for (const measure of skippedRects) {
expect(measure).not.toHaveBeenCalled()
}
assistantRect.mockReturnValue(rect(200, 600))
- expect(countMessagesBelow(viewport, content)).toBe(2)
+ expect(countMessagesBelow(viewport, content).count).toBe(2)
vi.mocked(viewport.getBoundingClientRect).mockReturnValue(rect(0, 1500))
- expect(countMessagesBelow(viewport, content)).toBe(0)
+ expect(countMessagesBelow(viewport, content).count).toBe(0)
})
it('remeasures scroll and resize, ignores hidden panes, and clears at the bottom', () => {
@@ -138,4 +138,71 @@ describe('messages below the viewport', () => {
rerender({ ...options, isAtBottom: true })
expect($threadMessagesBelowBySession.get()['runtime-a'] ?? 0).toBe(0)
})
+
+ it('re-measures a frame later when the turn at the fold has no layout boxes yet', () => {
+ const { viewport, content, assistantRect } = transcript()
+ const straddling = window.document.createElement('div')
+ straddling.dataset.slot = 'aui_message-group'
+ content.append(straddling)
+ vi.spyOn(straddling, 'getBoundingClientRect').mockReturnValue(rect(500, 1500))
+
+ // A content-visibility skipped turn: the group keeps its placeholder box,
+ // its messages have none until relevancy updates on the next frame.
+ const empty = { top: 0, bottom: 0, height: 0, width: 0 } as DOMRect
+
+ const roots = ['aui_user-message-root', 'aui_assistant-message-root', 'aui_assistant-message-root'].map(slot => {
+ const message = window.document.createElement('div')
+ message.dataset.slot = slot
+ straddling.append(message)
+
+ return vi.spyOn(message, 'getBoundingClientRect').mockReturnValue(empty)
+ })
+
+ let frame: FrameRequestCallback | undefined
+ vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
+ frame = callback
+
+ return 1
+ })
+ vi.stubGlobal('cancelAnimationFrame', () => {
+ frame = undefined
+ })
+ vi.stubGlobal('ResizeObserver', class {
+ observe() {}
+ disconnect() {}
+ })
+
+ const flush = () =>
+ act(() => {
+ const callback = frame
+ frame = undefined
+ callback?.(0)
+ })
+
+ renderHook(() =>
+ useMessagesBelow({
+ scrollRef: { current: viewport },
+ contentRef: { current: content },
+ isAtBottom: false,
+ paneVisible: true,
+ rows: null,
+ sessionKey: 'stored-a',
+ sessionId: 'runtime-a'
+ })
+ )
+
+ // First frame: the straddling turn measures empty; nothing is published yet.
+ flush()
+ expect($threadMessagesBelowBySession.get()['runtime-a']).toBeUndefined()
+ expect(frame).toBeDefined()
+
+ // Relevancy updated: two of its three messages sit below the fold.
+ roots[0]!.mockReturnValue(rect(500, 580))
+ roots[1]!.mockReturnValue(rect(580, 900))
+ roots[2]!.mockReturnValue(rect(900, 1500))
+ assistantRect.mockReturnValue(rect(200, 500))
+ flush()
+ expect($threadMessagesBelowBySession.get()['runtime-a']).toBe(2)
+ expect(frame).toBeUndefined()
+ })
})
diff --git a/apps/desktop/src/components/assistant-ui/thread/use-messages-below.ts b/apps/desktop/src/components/assistant-ui/thread/use-messages-below.ts
index fb2a637fe35..2e2e1b2017c 100644
--- a/apps/desktop/src/components/assistant-ui/thread/use-messages-below.ts
+++ b/apps/desktop/src/components/assistant-ui/thread/use-messages-below.ts
@@ -15,10 +15,17 @@ interface MessagesBelowOptions {
sessionId: string | null
}
-/** Only measure inside the viewport's turn; leave skipped off-screen content asleep. */
-export function countMessagesBelow(viewport: HTMLElement, content: HTMLElement): number {
+/**
+ * Only measure inside the viewport's turn; leave skipped off-screen content
+ * asleep. `settled` is false when the turn straddling the fold has no layout
+ * boxes yet: content-visibility relevancy updates a frame after a programmatic
+ * scroll (a rail jump), and until then its messages measure as empty rects —
+ * counting them as "above the fold" undercounts by that turn.
+ */
+export function countMessagesBelow(viewport: HTMLElement, content: HTMLElement): { count: number; settled: boolean } {
const bottom = viewport.getBoundingClientRect().bottom
let count = 0
+ let settled = true
for (const group of content.querySelectorAll<HTMLElement>('[data-slot="aui_message-group"]')) {
const rect = group.getBoundingClientRect()
@@ -38,13 +45,15 @@ export function countMessagesBelow(viewport: HTMLElement, content: HTMLElement):
for (const message of messages) {
const messageRect = message.getBoundingClientRect()
- if (messageRect.height > 0 && messageRect.bottom > bottom + 1) {
+ if (messageRect.height === 0 && messageRect.width === 0) {
+ settled = false
+ } else if (messageRect.height > 0 && messageRect.bottom > bottom + 1) {
count++
}
}
}
- return count
+ return { count, settled }
}
export function useMessagesBelow({
@@ -75,10 +84,23 @@ export function useMessagesBelow({
}
let frame = 0
+ let retried = false
const measure = () => {
frame = 0
- publishThreadMessagesBelow(countMessagesBelow(viewport, content), { paneVisible, sessionId })
+ const { count, settled } = countMessagesBelow(viewport, content)
+
+ // One extra frame lets the skipped turn gain boxes; then publish what is
+ // there so an empty turn can never stall the count.
+ if (!settled && !retried) {
+ retried = true
+ schedule()
+
+ return
+ }
+
+ retried = false
+ publishThreadMessagesBelow(count, { paneVisible, sessionId })
}
const schedule = () => {