Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix(web): stabilize tail position on resize (#914)
Gener916c30ded831
diff --git a/src/web/src/hooks/community/use-scroll-anchor.repin.dom.test.ts b/src/web/src/hooks/community/use-scroll-anchor.repin.dom.test.tsindex 2ff2a7f46..04597b435 100644--- a/src/web/src/hooks/community/use-scroll-anchor.repin.dom.test.ts+++ b/src/web/src/hooks/community/use-scroll-anchor.repin.dom.test.ts@@ -1,4 +1,5 @@import { beforeEach, afterEach, describe, expect, it, vi } from "vitest"+import { createElement, Suspense, startTransition, use, type PropsWithChildren } from "react"import { act, fireEvent } from "@/test/react-dom-harness"import { bodyTop, installMessageScrollFixture, restoreMessageScrollFixture, message, mount, resize, runFrames, scrollFixture } from "@/test/message-scroll-fixture"@@ -887,6 +888,82 @@ describe("locked native adapter and existing message scroll owner", () => {resize()expect(h.root.scrollTop).toBe(before)})+ it.each(["commit", "discard"])("retires a width request after a rendered tail suspends before %s", async outcome => {+ let suspend = false+ let resume!: () => void+ const pending = new Promise<void>(resolve => { resume = resolve })+ function Gate() {+ if (suspend) use(pending)+ return null+ }+ function Wrapper({ children }: PropsWithChildren) {+ return createElement(Suspense, { fallback: createElement("div", { "data-testid": "pending-tail" }) },+ children, createElement(Gate))+ }+ scrollFixture.width = 639+ let layouts = 0+ const h = mount({ tailPaddingEnd: 40 }, false, () => { layouts += 1 }, Wrapper)+ const originalItems = h.input.items+ const native = scrollFixture.latest.virtualizer+ const count = native.options.count+ const end = vi.spyOn(native, "scrollToEnd")+ const offset = vi.spyOn(native, "scrollToOffset")+ scrollFixture.width = 266+ resize(0)+ expect(end).not.toHaveBeenCalled()+ const committedLayouts = layouts+ suspend = true+ await act(async () => {+ startTransition(() => h.stage({ items: [...originalItems, message("pending-tail")] }))+ })+ expect(layouts).toBe(committedLayouts)+ expect(native.options.count).toBe(count + 1)+ expect(native.options.getItemKey(count)).toBe("msg:pending-tail")+ expect(h.root.isConnected).toBe(true)+ expect(h.root.querySelector('[data-msg-id="m13"]')).not.toBeNull()+ expect(h.root.querySelector('[data-msg-id="pending-tail"]')).toBeNull()+ expect(h.view.queryByTestId("pending-tail")).toBeNull()+ await act(async () => {+ runFrames(1)+ expect(layouts).toBe(committedLayouts)+ expect(h.root.querySelector('[data-msg-id="pending-tail"]')).toBeNull()+ expect(h.view.queryByTestId("pending-tail")).toBeNull()+ expect(end).not.toHaveBeenCalled()+ expect(offset).not.toHaveBeenCalled()+ suspend = false+ if (outcome === "discard") h.stage({ items: originalItems })+ resume()+ })+ resize()+ expect(h.root.querySelector('[data-msg-id="pending-tail"]') !== null).toBe(outcome === "commit")+ expect(h.root.scrollHeight - h.root.clientHeight - h.root.scrollTop).toBeLessThanOrEqual(1)+ expect(scrollFixture.latest.readPositionReady).toBe(true)+ if (outcome === "discard") expect(end).not.toHaveBeenCalled()+ else expect(end).toHaveBeenCalledOnce()+ })+ it("does not revive a committed width request from late public frame callbacks", () => {+ scrollFixture.width = 639+ const h = mount({ tailPaddingEnd: 40 })+ const native = scrollFixture.latest.virtualizer+ const end = vi.spyOn(native, "scrollToEnd")+ const offset = vi.spyOn(native, "scrollToOffset")+ scrollFixture.width = 266+ resize(2)+ expect(end).toHaveBeenCalledOnce()+ const pending = [...scrollFixture.frames.values()]+ expect(pending.length).toBeGreaterThan(0)+ h.stage({ items: [...h.input.items.slice(0, -1), message("replacement")] })+ expect(offset).toHaveBeenCalledOnce()+ expect(h.root.querySelector('[data-msg-id="replacement"]')).not.toBeNull()+ resize()+ const calls = [end.mock.calls.length, offset.mock.calls.length, scrollFixture.scrollCalls.length]+ const top = h.root.scrollTop+ act(() => { for (const callback of pending) callback(performance.now()) })+ expect([end.mock.calls.length, offset.mock.calls.length, scrollFixture.scrollCalls.length]).toEqual(calls)+ expect(h.root.scrollTop).toBe(top)+ expect(h.root.querySelector('[data-msg-id="replacement"]')).not.toBeNull()+ expect(scrollFixture.latest.readPositionReady).toBe(true)+ })it("preserves a two-pixel DM tail distance through consecutive composer resizes", () => {const h = mount()scrollFixture.height -= 100diff --git a/src/web/src/hooks/community/use-scroll-anchor.ts b/src/web/src/hooks/community/use-scroll-anchor.tsindex 085035f70..319df4692 100644--- a/src/web/src/hooks/community/use-scroll-anchor.ts+++ b/src/web/src/hooks/community/use-scroll-anchor.ts@@ -247,6 +247,7 @@ export function useScrollAnchor({behavior: ScrollBehaviornotifyTarget: booleanloadedTailPresent: boolean+ singleEnd: { key: string | null; index: number } | nullviewport: { width: number; height: number }}type Geometry = {@@ -376,9 +377,9 @@ export function useScrollAnchor({? intent?.notifyTarget ? intent.id: !intent && positionedTargetRef.current !== targetIntentRef.current ? targetIntentRef.current : null: null- const wasReadable = positionKind.get() === "idle" && readReady.get()+ const wasReadable = (positionKind.get() === "idle" && readReady.get()) || !!intent?.singleEndclaimPosition("idle")- if (userHandover && wasReadable) setReadPositionReady(true)+ if ((userHandover || intent?.singleEnd) && wasReadable) setReadPositionReady(true)retireInitialPosition()positionedTargetRef.current = targetIntentRef.currentif (cancelledTarget) onTargetCancelledRef.current?.(cancelledTarget)@@ -515,21 +516,42 @@ export function useScrollAnchor({const root = scrollRef.currentconst native = virtualizerRef.currentif (!intent || intent.epoch !== owner.epoch || !root || !native) return+ const index = currentItemsRef.current.length - 1+ if (intent.singleEnd && (index < 0 || intent.singleEnd.index !== index+ || intent.singleEnd.key !== currentItemsRef.current[index]?.key)) {+ releasePosition()+ return+ }const viewport = root.getBoundingClientRect()if (root.clientHeight <= 0 || viewport.height <= 0) {- if (!intent.loadedTailPresent) initialSettleFrameRef.current = window.requestAnimationFrame(settle)+ if (!intent.loadedTailPresent || intent.singleEnd) initialSettleFrameRef.current = window.requestAnimationFrame(settle)+ return+ }+ if (intent.singleEnd && intent.index === null) {+ const geometry = readGeometry()+ const value = geometry ? JSON.stringify(geometry) : null+ const sample = geometrySampleRef.current+ geometrySampleRef.current = value === null ? null+ : { value, frames: sample?.value === value ? sample.frames + 1 : 1 }+ if (geometrySampleRef.current && geometrySampleRef.current.frames >= 2) {+ intent.index = index+ geometrySampleRef.current = null+ owner.nativeIndex = true+ native.scrollToEnd({ behavior: intent.behavior })+ }+ initialSettleFrameRef.current = window.requestAnimationFrame(settle)return}let landed = falseif (intent.type === "end") {- const index = currentItemsRef.current.length - 1- if (intent.index !== index) {+ if (!intent.singleEnd && intent.index !== index) {intent.index = indexintent.stableFrames = 0owner.nativeIndex = index >= 0native.scrollToEnd({ behavior: intent.behavior })}- landed = native.isAtEnd(1)+ landed = native.isAtEnd(1) && (!intent.singleEnd+ || Math.abs(Math.max(0, root.scrollHeight - root.clientHeight) - root.scrollTop) <= 1)} else if (intent.id) {const index = intent.type === "unread"? findMountScrollTargetIndex(currentItemsRef.current, intent.id)@@ -557,8 +579,15 @@ export function useScrollAnchor({}intent.stableFrames = landed ? intent.stableFrames + 1 : 0if (intent.stableFrames >= 2 && intent.type !== "end" && root.scrollTop <= 1) holdNativeOrigin(currentItemsRef.current[0]?.key ?? null)- const geometry = intent.stableFrames >= 2 ? readGeometry() : null- if (geometry) {+ const geometry = (intent.singleEnd || intent.stableFrames >= 2) ? readGeometry() : null+ if (intent.singleEnd) {+ const value = landed && geometry?.pinEligible ? JSON.stringify(geometry) : null+ const sample = geometrySampleRef.current+ geometrySampleRef.current = value === null ? null+ : { value, frames: sample?.value === value ? sample.frames + 1 : 1 }+ intent.stableFrames = geometrySampleRef.current?.frames ?? 0+ }+ if (intent.stableFrames >= 2 && geometry) {settlePresentation()if (positionKind.get() === "initial" && !initialScrollReadyRef.current) returnacceptedGeometryRef.current = geometry@@ -574,31 +603,32 @@ export function useScrollAnchor({scheduleGeometryRef.current()return}- if (!intent.loadedTailPresent || (landed && intent.stableFrames < 2)) {+ if (!intent.loadedTailPresent || intent.singleEnd || (landed && intent.stableFrames < 2)) {initialSettleFrameRef.current = window.requestAnimationFrame(settle)}}initialSettleFrameRef.current = window.requestAnimationFrame(settle)- }, [clearBudget, holdNativeOrigin, positionKind, readGeometry, setOwnerKind, setReadPositionReady, settlePresentation])+ }, [clearBudget, holdNativeOrigin, positionKind, readGeometry, releasePosition, setOwnerKind, setReadPositionReady, settlePresentation])- const startIntent = useCallback((type: Intent["type"], id: string | null, behavior: ScrollBehavior = "auto", notifyTarget = false, loadedTailPresent = false) => {+ const startIntent = useCallback((type: Intent["type"], id: string | null, behavior: ScrollBehavior = "auto", notifyTarget = false, loadedTailPresent = false, singleEnd = false) => {const owner = positionOwnerRef.currentconst index = type === "end" ? currentItemsRef.current.length - 1: id ? type === "unread" ? findMountScrollTargetIndex(currentItemsRef.current, id): findMessageIndex(currentItemsRef.current, id) : nullsemanticIntentRef.current = {- epoch: owner.epoch, type, id, index, stableFrames: 0, behavior, notifyTarget, loadedTailPresent,+ epoch: owner.epoch, type, id, index: singleEnd ? null : index, stableFrames: 0, behavior, notifyTarget, loadedTailPresent,+ singleEnd: singleEnd ? { key: currentItemsRef.current[index ?? -1]?.key ?? null, index: index ?? -1 } : null,viewport: { width: scrollRef.current?.clientWidth ?? 0, height: scrollRef.current?.clientHeight ?? 0 },}- if (type === "end") {+ if (type === "end" && !singleEnd) {owner.nativeIndex = currentItemsRef.current.length > 0virtualizerRef.current?.scrollToEnd({ behavior })}- else if (index !== null) {+ else if (type !== "end" && index !== null) {owner.nativeIndex = truevirtualizerRef.current?.scrollToIndex(index, { align: "center", behavior })}- if (!loadedTailPresent) armBudget()+ if (!loadedTailPresent || singleEnd) armBudget()scheduleInitialPositionSettled()}, [armBudget, scheduleInitialPositionSettled])@@ -608,7 +638,8 @@ export function useScrollAnchor({owner.active = trueconst intent = semanticIntentRef.currentif (intent && intent.epoch !== owner.epoch) {- startIntent(intent.type, intent.id, intent.behavior, intent.notifyTarget, intent.loadedTailPresent)+ if (intent.singleEnd) releasePosition()+ else startIntent(intent.type, intent.id, intent.behavior, intent.notifyTarget, intent.loadedTailPresent)} else if (positionKind.get() === "present" && presentIntentEpochRef.current !== null) {presentIntentEpochRef.current = owner.epocharmBudget()@@ -625,7 +656,7 @@ export function useScrollAnchor({if (geometryFrameRef.current !== null) window.cancelAnimationFrame(geometryFrameRef.current)geometryFrameRef.current = null}- }, [armBudget, cancelFrame, clearBudget, clearOlderMeasurementAnchor, positionKind, startIntent])+ }, [armBudget, cancelFrame, clearBudget, clearOlderMeasurementAnchor, positionKind, releasePosition, startIntent])useLayoutEffect(() => {const target = scrollToMessageId ?? null@@ -707,6 +738,13 @@ export function useScrollAnchor({if (input.touch || input.pointers.size > 0 || (native.isScrolling && !clampedByResize)|| root.scrollTop < 0 || root.scrollTop > max + 1) return true+ if (previous?.epoch === owner.epoch && readReady.get() && previous.pinEligible+ && previous.clientWidth !== root.clientWidth && !paginationRef.current) {+ claimPosition("present")+ retireInitialPosition()+ startIntent("end", null, "auto", false, true, true)+ return false+ }if (!next) return native.getVirtualItems().length > 0const virtualItems = native.getVirtualItems()const value = JSON.stringify(next)@@ -747,11 +785,6 @@ export function useScrollAnchor({input.handover = falsesetReadPositionReady(true)}- if (viewportResized && previous.pinEligible && previous.clientWidth !== next.clientWidth) {- owner.nativeIndex = true- native.scrollToEnd({ behavior: "auto" })- return true- }if (Math.abs(offset - root.scrollTop) > 0.5) native.scrollToOffset(offset, { behavior: "auto" })if (olderMeasurementAnchorRef.current) {clearOlderMeasurementAnchor()@@ -762,7 +795,7 @@ export function useScrollAnchor({setPaginationDirection(null)}return false- }, [clearOlderMeasurementAnchor, holdNativeOrigin, observeGeometry, positionKind, readGeometry, readReady, setPaginationDirection, setReadPositionReady, setShortGap])+ }, [claimPosition, clearOlderMeasurementAnchor, holdNativeOrigin, observeGeometry, positionKind, readGeometry, readReady, retireInitialPosition, setPaginationDirection, setReadPositionReady, setShortGap, startIntent])reconcileViewportRef.current = () => {const intent = semanticIntentRef.currentconst root = scrollRef.current@@ -771,7 +804,8 @@ export function useScrollAnchor({&& (intent.viewport.width !== root.clientWidth || intent.viewport.height !== root.clientHeight)) {intent.viewport = { width: root.clientWidth, height: root.clientHeight }intent.stableFrames = 0- if (root.clientHeight > 0 && !intent.loadedTailPresent) {+ if (intent.singleEnd) geometrySampleRef.current = null+ if (root.clientHeight > 0 && !intent.loadedTailPresent && !intent.singleEnd) {if (intent.type === "end") native.scrollToEnd({ behavior: intent.behavior })else if (intent.index !== null) native.scrollToIndex(intent.index, { align: "center", behavior: intent.behavior })scheduleInitialPositionSettled()@@ -892,6 +926,7 @@ export function useScrollAnchor({}, [holdNativeOrigin, nativeOrigin, observeGeometry, positionKind, readGeometry, readReady, reconcileGeometry, releasePosition, scheduleGeometry, scheduleInitialPositionSettled])const capturePageAnchor = useCallback((direction: "older" | "newer") => {+ if (semanticIntentRef.current?.singleEnd) releasePosition()clearOlderMeasurementAnchor()let anchor: NonNullable<typeof paginationRef.current>["anchor"]if (positionKind.get() === "idle") {@@ -913,7 +948,7 @@ export function useScrollAnchor({}paginationRef.current = { direction, phase: "pending", anchor }setPaginationDirection(direction)- }, [clearOlderMeasurementAnchor, positionKind, readGeometry, readPageMessageGeometry, setPaginationDirection])+ }, [clearOlderMeasurementAnchor, positionKind, readGeometry, readPageMessageGeometry, releasePosition, setPaginationDirection])const captureOlderPageAnchor = useCallback(() => capturePageAnchor("older"), [capturePageAnchor])const captureNewerPageAnchor = useCallback(() => capturePageAnchor("newer"), [capturePageAnchor])useLayoutEffect(() => {@@ -967,6 +1002,8 @@ export function useScrollAnchor({const root = scrollRef.currentconst viewportReady = !!root && root.clientHeight > 0 && virtualItems.length > 0&& !!root.querySelector("[data-index]")+ const singleEnd = semanticIntentRef.current?.singleEnd+ if (singleEnd && (singleEnd.index !== items.length - 1 || singleEnd.key !== items.at(-1)?.key)) releasePosition()if (positionKind.get() === "target" || positionKind.get() === "present") {stateRef.current = { ...stateRef.current, lastTailId: tailId }tailKeyRef.current = items.at(-1)?.key ?? null@@ -1010,7 +1047,7 @@ export function useScrollAnchor({startIntent("end", null)}} else if (semanticIntentRef.current) scheduleInitialPositionSettled()- }, [claimPosition, hasMoreNewer, initialScrollReady, isFetchingNewer, items, messages, newDividerBefore, paginationDirection, positionKind, readReady, retireInitialPosition, scheduleInitialPositionSettled, startIntent, tailId, viewerUserId, virtualItems, virtualizer])+ }, [claimPosition, hasMoreNewer, initialScrollReady, isFetchingNewer, items, messages, newDividerBefore, paginationDirection, positionKind, readReady, releasePosition, retireInitialPosition, scheduleInitialPositionSettled, startIntent, tailId, viewerUserId, virtualItems, virtualizer])const requestPresentPosition = useCallback(() => {presentIntentEpochRef.current = claimPosition("present")diff --git a/src/web/src/test/message-scroll-fixture.ts b/src/web/src/test/message-scroll-fixture.tsindex 47523b293..ae8dc1132 100644--- a/src/web/src/test/message-scroll-fixture.ts+++ b/src/web/src/test/message-scroll-fixture.ts@@ -1,6 +1,6 @@import { createElement, StrictMode, useLayoutEffect, useMemo } from "react"import { vi } from "vitest"-import { act, render, fireEvent } from "@/test/react-dom-harness"+import { act, render, fireEvent, type RenderOptions } from "@/test/react-dom-harness"import { VirtualRows } from "@/components/community/messages/virtual-cursor-list"import type { FlatItem } from "@/lib/community/message-list-items"import { useScrollAnchor } from "@/hooks/community/use-scroll-anchor"@@ -143,10 +143,10 @@ export function resize(frameCount = 26) {})runFrames(frameCount)}-export function mount(overrides: Partial<Input> = {}, strict = false, onLayout?: (result: Result) => void) {+export function mount(overrides: Partial<Input> = {}, strict = false, onLayout?: (result: Result) => void, wrapper?: RenderOptions["wrapper"]) {const input: Input = { items: Array.from({ length: 14 }, (_, i) => message(`m${i}`)), initialScrollReady: true, hasMoreOlder: true, ...overrides }const element = () => strict ? createElement(StrictMode, null, createElement(Probe, { input, onLayout })) : createElement(Probe, { input, onLayout })- const view = render(element())+ const view = render(element(), { wrapper })const root = view.getByTestId("scroll") as HTMLElementresize()const stage = (next: Partial<Input>) => { Object.assign(input, next); view.rerender(element()) }