diff --git a/packages/app/component-tests/timeline-virtualizer.fixture.tsx b/packages/app/component-tests/timeline-virtualizer.fixture.tsx index d7b667667fa..95d87d34782 100644 --- a/packages/app/component-tests/timeline-virtualizer.fixture.tsx +++ b/packages/app/component-tests/timeline-virtualizer.fixture.tsx @@ -50,9 +50,10 @@ export function mountTimelineVirtualizer(input: { viewport = element resize.observe(element, { box: "border-box" }) }, - setContentRef: (element) => { - content = element - reveal.observe(element, { attributes: true, attributeFilter: ["style"] }) + onContentResize: () => { + if (content) return + content = host.querySelector("[data-timeline-virtual-content]")! + reveal.observe(content, { attributes: true, attributeFilter: ["style"] }) }, onPin: () => setState("pinned", true), onUnpin: () => setState("pinned", false), diff --git a/packages/app/e2e/regression/timeline-stream.spec.ts b/packages/app/e2e/regression/timeline-stream.spec.ts index 707e47552e0..ef0608d0c8a 100644 --- a/packages/app/e2e/regression/timeline-stream.spec.ts +++ b/packages/app/e2e/regression/timeline-stream.spec.ts @@ -15,6 +15,7 @@ import { inboxDelivered, inboxEnqueued, messageUpdated, + partDelta, partUpdated, reasoningPart, renderedPartID, @@ -1010,6 +1011,33 @@ test.describe("Working", () => { }) }) +test("grows a streaming row without a ResizeObserver loop error", async ({ page }) => { + // Each streamed paragraph grows its row. An observer on the content that holds every row saw that height inside the + // row observer's own delivery, and Chrome reported every such frame as an uncaught error. + await page.addInitScript(() => { + window.addEventListener("error", (event) => { + if (!event.message.startsWith("ResizeObserver loop")) return + document.documentElement.dataset.resizeObserverLoops = String( + Number(document.documentElement.dataset.resizeObserverLoops ?? 0) + 1, + ) + }) + }) + + const timeline = await setupTimeline(page, { + messages: [userMessage(), assistantMessage([textPart("prt_growth", "Streaming.")], { completed: false })], + }) + + const text = page.locator(`[data-timeline-part-id="${renderedPartID("prt_growth")}"]`) + + for (const line of [1, 2, 3, 4, 5, 6]) { + await timeline.send(partDelta("prt_growth", `\n\nParagraph ${line}.`)) + await expect(text).toContainText(`Paragraph ${line}.`) + } + + await expect(text.locator("p")).toHaveCount(7) + expect(await page.evaluate(() => document.documentElement.dataset.resizeObserverLoops)).toBeUndefined() +}) + test.describe("background shortcut", () => { test("offers a standalone running subagent to the background with Ctrl+B", async ({ page }) => { await setupTimeline(page, { diff --git a/packages/app/src/session/screen.tsx b/packages/app/src/session/screen.tsx index e6bc2384307..f32fb2421f2 100644 --- a/packages/app/src/session/screen.tsx +++ b/packages/app/src/session/screen.tsx @@ -314,7 +314,7 @@ function SessionScreenContent(props: { pinned={timeline.view.pinned()} centered={screen.centered()} reserveReviewToggle={!sideVisible()} - setContentRef={timeline.view.setContentRef} + onContentResize={timeline.view.contentResized} bottomInset={timeline.inset()} setVisibleRef={timeline.view.setVisibleRef} anchor={timeline.view.anchor} diff --git a/packages/app/src/session/timeline/interaction.ts b/packages/app/src/session/timeline/interaction.ts index 2ded811ab59..d1c5d928d5f 100644 --- a/packages/app/src/session/timeline/interaction.ts +++ b/packages/app/src/session/timeline/interaction.ts @@ -23,7 +23,6 @@ export function createSessionTimelineInteraction(session: SessionModel) { inset: number refs: { scroller: HTMLDivElement | undefined - content: HTMLDivElement | undefined dock: HTMLDivElement | undefined visible: HTMLDivElement | undefined } @@ -41,7 +40,6 @@ export function createSessionTimelineInteraction(session: SessionModel) { inset: 0, refs: { scroller: undefined, - content: undefined, dock: undefined, visible: undefined, }, @@ -361,13 +359,6 @@ export function createSessionTimelineInteraction(session: SessionModel) { { defer: true }, ), ) - createResizeObserver( - () => state.refs.content, - () => { - if (scroller) scheduleScrollState(scroller) - fill() - }, - ) createResizeObserver( () => state.refs.dock, (_rect, _element, entry) => { @@ -426,10 +417,9 @@ export function createSessionTimelineInteraction(session: SessionModel) { pinned, selectionInteraction, scheduleScrollState, - setContentRef: (element: HTMLDivElement | undefined) => { - setState("refs", "content", element) - + contentResized: () => { if (scroller) scheduleScrollState(scroller) + fill() }, setDockRef: (element: HTMLDivElement | undefined) => { setState("refs", "dock", element) diff --git a/packages/app/src/session/timeline/message-timeline.tsx b/packages/app/src/session/timeline/message-timeline.tsx index 0de087a90bb..122e28620a4 100644 --- a/packages/app/src/session/timeline/message-timeline.tsx +++ b/packages/app/src/session/timeline/message-timeline.tsx @@ -91,7 +91,7 @@ type MessageTimelineProps = { pinned: boolean centered: boolean reserveReviewToggle: boolean - setContentRef: (el: HTMLDivElement) => void + onContentResize: () => void bottomInset?: number setVisibleRef?: (el: HTMLDivElement | undefined) => void anchor: (id: string) => string @@ -193,7 +193,7 @@ function MessageTimelineView( scroll: () => props.scroll, onResumeScroll: props.onResumeScroll, setScrollRef: props.setScrollRef, - setContentRef: props.setContentRef, + onContentResize: props.onContentResize, bottomInset: () => props.bottomInset ?? 0, setVisibleRef: props.setVisibleRef, onScheduleScrollState: props.onScheduleScrollState, diff --git a/packages/app/src/session/timeline/virtualizer.tsx b/packages/app/src/session/timeline/virtualizer.tsx index e46df6de4e2..201caf3fe4c 100644 --- a/packages/app/src/session/timeline/virtualizer.tsx +++ b/packages/app/src/session/timeline/virtualizer.tsx @@ -75,7 +75,8 @@ type Input = { scroll: Accessor<{ overflow: boolean; jump: boolean }> onResumeScroll: (behavior?: ScrollBehavior) => void setScrollRef: (element: HTMLDivElement | undefined) => void - setContentRef: (element: HTMLDivElement) => void + /** The rows' content changed height. */ + onContentResize: () => void /** Height of the composer region floating over the timeline's bottom edge, where the timeline fades out. */ bottomInset?: Accessor setVisibleRef?: (element: HTMLDivElement | undefined) => void @@ -554,8 +555,6 @@ export function createTimelineVirtualizer(input: Input) { const root = listRoot() if (root) input.setScrollRef(root) - - if (virtualContent) input.setContentRef(virtualContent) input.setVisibleRef?.(visibleArea()) queueMicrotask(() => { if (!active() || !root?.isConnected) return @@ -630,6 +629,15 @@ export function createTimelineVirtualizer(input: Input) { ), ) + // The content is as tall as the rows the virtualizer sizes, so it reports that height rather than the session + // observing the element. A ResizeObserver there sees the height a row's own observer commits during its delivery, and + // Chrome reports every such frame as "ResizeObserver loop completed with undelivered notifications." + createEffect( + on([active, () => virtualizer.getTotalSize() - rendering.scrollAdjustment], () => { + if (active()) input.onContentResize() + }), + ) + // Only the viewport carries the inset as a variable, for its fade and scroll padding. The variable does not inherit // (see index.css): the inset changes on every frame while the composer region grows, and an inherited variable would // restyle every rendered row each time. @@ -1030,11 +1038,7 @@ export function createTimelineVirtualizer(input: Input) {
{ - virtualContent = element - - if (active()) input.setContentRef(element) - }} + ref={virtualContent} style={{ height: `${virtualizer.getTotalSize() - rendering.scrollAdjustment}px`, position: "relative",