fix(chat): settle pinned scroll deliveries before layout growth

This commit is contained in:
Tianyi Cui committed 2026-09-07 13:24:04 +08:00
1 parent ef11a4e007
commit fa6bf62a98
9 files changed
+186 -6

No files matched your search

@@ -0,0 +1,6 @@
# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each
# side as of the last confirmed-consistent state. Both languages carry equal authority;
# after editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write .agents/notes/implemented/bug-fix/2026-09-07-pinned-scroll-delivery-before-layout.md
2026-09-07-pinned-scroll-delivery-before-layout.md: 7a00cf653824df13272fcf0cc2baf35b298f170c
2026-09-07-pinned-scroll-delivery-before-layout.zh.md: d78c388e4e7a8f6f2fa6070149e652e0e25cc358
@@ -0,0 +1,23 @@
# Agent Note: Settle pinned scroll deliveries before layout changes
Status: implemented
English | [中文](2026-09-07-pinned-scroll-delivery-before-layout.zh.md)
## Problem
A delayed scroll sample compares positions from different layouts. While Chat is pinned, a composer or transcript shrink can move the browser floor; subsequent growth can move the browser position again before `scrollend` or the sampling timer. Deferring follow during that interval leaves the observed-top ledger stale and can classify browser layout movement as reader input, disabling follow without a reader gesture.
## Decision
[ChatView](../../../../packages/client/ui-chat/src/client/chat/ChatView.tsx) samples pinned scroll deliveries synchronously through the same sample operation that clears pending work. This preserves the existing observed-top comparison for genuine reader movement and releases layout follow before further growth. Pinned samples use scroll metrics, not semantic-row geometry; moving away still disarms follow immediately. Away-reader samples remain coalesced at the existing interval or `scrollend`.
## Alternatives considered
**Defer every delivery.** Coalescing reduces geometry work while reading history, but a pinned browser position and its floor must be attributed in the same layout. A longer timeout or retry cannot recover ownership once the stale comparison disarms it.
**Sample every delivery synchronously.** This restores attribution but also repeats semantic-anchor and reading-line measurements throughout an away-reader scroll burst. Only pinned ownership needs the immediate path.
## Consequences
Pinned deliveries incur immediate scroll-metric reads. History reading retains its bounded sampling cadence, and explicit return-to-bottom deliveries clear any pending away sample. [Focused tests](../../../../packages/client/ui-chat/tests/chat-view.client.spec.tsx) cover shrink/regrowth before scrollend, observer growth without row measurements, repinning with a pending sample, timer and scrollend sampling, and unmount cancellation. The [keyless browser scenario](../../../../apps/web/tests/chat-scroll-contract.e2e.ts) covers pinned Send, real scroll-away input, streaming, and tool disclosure across the long transcript.
@@ -0,0 +1,23 @@
# Agent Note: 在布局变化前处理贴底滚动事件
Status: implemented
[English](2026-09-07-pinned-scroll-delivery-before-layout.md) | 中文
## Problem
延迟的滚动采样会比较来自不同布局的位置。Chat 贴底时,输入框或 transcript(文本记录)收缩可能改变浏览器底部位置;随后的增长又可能在 `scrollend` 或采样定时器触发前改变浏览器位置。在此期间推迟跟随会使已观察顶部位置记录过期,把浏览器布局移动误判为读者输入,在没有读者操作时关闭跟随。
## Decision
[ChatView](../../../../packages/client/ui-chat/src/client/chat/ChatView.tsx) 通过同一个清除待处理工作的采样操作,同步采样贴底滚动事件。该机制保留现有的已观察顶部位置比较来识别真实读者移动,并在后续增长前恢复布局跟随。贴底采样只读取滚动指标,不读取语义行几何;离底移动仍会立即关闭跟随。离底读者的采样仍合并到现有周期或 `scrollend` 时执行。
## Alternatives considered
**延迟所有事件。** 合并采样减少阅读历史时的几何计算,但贴底浏览器位置及其底部必须在同一布局中完成归因。过期比较关闭跟随后,延长超时或重试都无法恢复归属。
**同步采样所有事件。** 这能恢复归因,却也会在离底读者连续滚动时重复测量语义锚点和阅读线。只有贴底归属需要立即处理。
## Consequences
贴底事件会立即读取滚动指标。历史阅读保留有界采样节奏,显式回到底部的滚动事件会清除任何待处理的离底采样。[聚焦测试](../../../../packages/client/ui-chat/tests/chat-view.client.spec.tsx) 覆盖 scrollend 前的收缩与增长、无需行测量的观察器增长、存在待处理采样时重新贴底、定时器与 scrollend 采样,以及卸载取消。[无密钥浏览器场景](../../../../apps/web/tests/chat-scroll-contract.e2e.ts) 覆盖长 transcript 中贴底发送、真实离底输入、流式输出与工具详情展开。
@@ -660,6 +660,7 @@ describe('web e2e: long Chat scroll contract', () => {
await liveRow.waitFor({ timeout: 15_000 })
expect(await liveRow.getAttribute('data-state')).toBe('running')
await expectBottom(world.page)
expect(await world.page.getByRole('button', { name: 'Back to bottom', exact: true }).count()).toBe(0)
await wheelTranscript(world.page, -1_200)
await world.page.getByRole('button', { name: 'Back to bottom', exact: true }).waitFor({ timeout: 10_000 })
+2 -2
View File
@@ -2,5 +2,5 @@
# side as of the last confirmed-consistent state. Both languages carry equal authority;
# after editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write packages/client/ui-chat/README.md
README.md: 405e5095d75c3e8b752c028bbc187307a9647bb6
README.zh.md: 7f57bc3480ea0186e89d12e8e443926906677742
README.md: 34b66da24b35f8cfd4c26da9b759e7991c5cb856
README.zh.md: a9b7c8fca3e8137950dd02cd5398c11f15344757
+1 -1
View File
@@ -46,7 +46,7 @@ Settings → General exposes a persisted `Normal` / `Compact` conversation-displ
<a id="scroll-ownership"></a>
## Scroll ownership
Chat restores semantic anchors across history prepend and renderer remounts. While the reader is pinned to the floor, `ResizeObserver` follows the new floor and selects the latest loaded Turn without reading row geometry. Once the reader moves away, flow-height changes preserve the top position and the reading-line geometry selects the active Turn. Turn-rail previews paint above sticky Markdown code-block banners, while the rail frame remains inside the transcript band above the composer.
Chat restores semantic anchors across history prepend and renderer remounts. Pinned scroll deliveries update follow ownership immediately, before subsequent layout changes can invalidate their floor; away-reader anchor sampling remains coalesced until the sampling interval or `scrollend`. While the reader is pinned to the floor, `ResizeObserver` follows the new floor and selects the latest loaded Turn without reading row geometry. Once the reader moves away, flow-height changes preserve the top position and the reading-line geometry selects the active Turn. Turn-rail previews paint above sticky Markdown code-block banners, while the rail frame remains inside the transcript band above the composer.
-----
+1 -1
View File
@@ -46,7 +46,7 @@ Chat 会为非空的初始请求、显式消息序列起点、真实 system 字
<a id="scroll-ownership"></a>
## 滚动归属
Chat 会在历史前插与 renderer 重新挂载时恢复语义锚点。读者跟随底部时,`ResizeObserver` 追随新的底部,并且无需读取行几何就选中最后一个已加载 Turn;读者离开底部后,高度变化会保持顶部位置,再由阅读线几何选择活跃 Turn。轮次导航预览位于 Markdown 代码块粘性头栏上方,而导航外框始终处于 composer 上方的 transcript 区域内。
Chat 会在历史前插与 renderer 重新挂载时恢复语义锚点。贴底滚动事件会立即更新跟随归属,避免后续布局变化使其底部位置失效;离底读者的锚点采样仍合并到采样周期或 `scrollend` 时执行。读者跟随底部时,`ResizeObserver` 追随新的底部,并且无需读取行几何就选中最后一个已加载 Turn;读者离开底部后,高度变化会保持顶部位置,再由阅读线几何选择活跃 Turn。轮次导航预览位于 Markdown 代码块粘性头栏上方,而导航外框始终处于 composer 上方的 transcript 区域内。
-----
@@ -579,8 +579,9 @@ export function ChatView({
scheduleActiveTurn()
}
// Raw scroll events only schedule work. Geometry is sampled at most once
// per interval, with scrollend providing the final sample for a short burst.
// Pinned deliveries must settle before layout growth can invalidate their
// floor. Away-reader anchor geometry stays coalesced until the interval or
// scrollend; pinned samples read only scroll metrics unless the reader leaves.
useEffect(() => {
const local = listRef.current
/* v8 ignore next -- ref-null guard: effect runs after the list node commits. */
@@ -597,6 +598,10 @@ export function ChatView({
}
const onScroll = (): void => {
scrollSamplePendingRef.current = true
if (atBottomRef.current) {
sample()
return
}
sampleTimer ??= window.setTimeout(sample, SCROLL_SAMPLE_INTERVAL_MS)
}
el.addEventListener('scroll', onScroll, { passive: true })
@@ -2387,6 +2387,128 @@ describe('ChatView', () => {
expect(scroller.scrollTop).toBe(900)
})
it('keeps following when a shrink clamp regrows before scrollend', () => {
const h = makeHarness({ nodes: [user(1, 'q'), assistant(2, 'a')] })
const view = render(<h.ChatView {...h.props} />)
const scroller = view.container.querySelector('[class*="scroll"]') as HTMLDivElement
const metrics = installScrollMetrics(scroller, 1_000, 300)
scroller.scrollTop = 700
fireEvent.scroll(scroller)
fireEvent(scroller, new Event('scrollend'))
metrics.setLayout(800, 700)
fireEvent.scroll(scroller)
metrics.setHeight(962)
act(() => { h.setSession({ running: true }) })
fireEvent(scroller, new Event('scrollend'))
expect(scroller.scrollTop).toBe(662)
expect(view.queryByLabelText('回到底部')).toBeNull()
expect(h.chatScroll.read()).toBeNull()
})
it('settles pinned deliveries before observer growth without reading row geometry', () => {
let notify: (() => void) | undefined
class ResizeObserverStub {
constructor(callback: ResizeObserverCallback) {
notify = () => { callback([], this as unknown as ResizeObserver) }
}
observe = vi.fn()
disconnect = vi.fn()
}
vi.stubGlobal('ResizeObserver', ResizeObserverStub)
const h = makeHarness({ nodes: [user(1, 'q'), assistant(2, 'a')] })
const view = render(<h.ChatView {...h.props} />)
const scroller = view.container.querySelector('[class*="scroll"]') as HTMLDivElement
const metrics = installScrollMetrics(scroller, 9_931, 300)
expect(notify).toBeDefined()
scroller.scrollTop = 9_631
fireEvent.scroll(scroller)
fireEvent(scroller, new Event('scrollend'))
const rect = vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect')
rect.mockClear()
try {
metrics.setLayout(9_918, 9_631)
fireEvent.scroll(scroller)
metrics.setHeight(10_013)
act(() => { notify?.() })
expect(scroller.scrollTop).toBe(9_713)
fireEvent.scroll(scroller)
metrics.setHeight(10_093)
act(() => { notify?.() })
expect(scroller.scrollTop).toBe(9_793)
expect(rect).not.toHaveBeenCalled()
expect(h.chatScroll.read()).toBeNull()
} finally {
rect.mockRestore()
}
})
it('clears an away sample when a back-to-bottom delivery restores pinned ownership', () => {
const h = makeHarness({ nodes: [user(1, 'q'), assistant(2, 'a')] })
const view = render(<h.ChatView {...h.props} />)
const scroller = view.container.querySelector('[class*="scroll"]') as HTMLDivElement
const metrics = installScrollMetrics(scroller, 1_000, 300)
scroller.scrollTop = 700
fireEvent.scroll(scroller)
scroller.scrollTop = 500
fireEvent.scroll(scroller)
fireEvent(scroller, new Event('scrollend'))
scroller.scrollTop = 400
fireEvent.scroll(scroller)
fireEvent.click(view.getByLabelText('回到底部'))
fireEvent.scroll(scroller)
metrics.setHeight(1_200)
act(() => { h.setSession({ running: true }) })
expect(scroller.scrollTop).toBe(900)
expect(h.chatScroll.read()).toBeNull()
})
it('samples away-reader geometry on the interval or scrollend and cancels it on unmount', () => {
vi.useFakeTimers()
try {
const h = makeHarness({ nodes: [user(1, 'q'), assistant(2, 'a')] })
const view = render(<h.ChatView {...h.props} />)
const scroller = view.container.querySelector('[class*="scroll"]') as HTMLDivElement
installScrollMetrics(scroller, 1_000, 300)
scroller.scrollTop = 700
fireEvent.scroll(scroller)
scroller.scrollTop = 500
fireEvent.scroll(scroller)
expect(view.getByLabelText('回到底部')).toBeTruthy()
const rect = vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect')
try {
act(() => { vi.advanceTimersByTime(500) })
rect.mockClear()
scroller.scrollTop = 400
fireEvent.scroll(scroller)
scroller.scrollTop = 300
fireEvent.scroll(scroller)
act(() => { vi.advanceTimersByTime(499) })
expect(rect).not.toHaveBeenCalled()
act(() => { vi.advanceTimersByTime(1) })
expect(rect).toHaveBeenCalled()
rect.mockClear()
scroller.scrollTop = 200
fireEvent.scroll(scroller)
expect(rect).not.toHaveBeenCalled()
fireEvent(scroller, new Event('scrollend'))
expect(rect).toHaveBeenCalled()
scroller.scrollTop = 100
fireEvent.scroll(scroller)
view.unmount()
rect.mockClear()
act(() => { vi.advanceTimersByTime(500) })
expect(rect).not.toHaveBeenCalled()
} finally {
rect.mockRestore()
}
} finally {
vi.useRealTimers()
}
})
it('uses the last delivered top when compositor scrolling precedes scroll delivery', () => {
const h = makeHarness({ nodes: [user(1, 'q'), assistant(2, 'a')] })
const view = render(<h.ChatView {...h.props} />)