Repository navigation
Description
Problem
Auto-scroll works reliably when markdown is streaming but fails when tool calls, tool results, or large custom elements (widgets, embedded cards) appear in the chat layout.
Expected Behavior
The chat should auto-scroll to keep the latest content visible regardless of whether the new content is streaming text or a discrete block addition (tool call, widget, etc.).
Actual Behavior
- ✅ Streaming markdown: auto-scroll follows (content grows character-by-character)
- ❌ Tool calls appearing: no auto-scroll
- ❌ Large custom elements / widgets: no auto-scroll
- ❌ Tool results expanding: no auto-scroll
Root Cause Analysis
The auto-scroll system uses a ResizeObserver on the content wrapper div with a requestAnimationFrame debounce:
function onResize() {
if (rafId != null) return; // ← Deduplicates: only one pending rAF at a time
rafId = requestAnimationFrame(() => {
rafId = null;
performScroll();
});
}
const observer = new ResizeObserver(onResize);
observer.observe(contentEl);Why this works for streaming text:
Each text delta causes continuous DOM growth → ResizeObserver fires repeatedly → auto-scroll keeps up.
Why this fails for discrete block additions:
-
rAF deduplication drops resize events — if a resize fires while a previous rAF is already queued, the new one is silently ignored.
-
Batched state updates — multiple events applied in a single
setState()create DOM elements in one React commit. The height change happens atomically rather than incrementally. -
Fixed-height containers — tool piles and embedded cards use fixed dimensions /
overflow: hidden, so child content changes don't propagate upward as continuous resizes. -
overflow: hiddenon column wrappers — each rendered block is wrapped in a div withoverflow-hidden, preventing child content from influencing parent layout measurements beyond the initial render.
Suggested Fix
Supplement the ResizeObserver with one of:
-
MutationObserver on the content wrapper watching
childListchanges — triggerperformScroll()after a microtask when children are added. -
Effect-based scroll on block count — track total block count and trigger scroll when it increases.
-
Explicit scroll trigger in the event processor — after state updates for tool_call/tool_result events, schedule a scroll via
queueMicrotask. -
Remove the rAF deduplication guard — or use a time-based throttle instead so rapid resizes still eventually scroll.
Reproduction
- Open any active chat session
- Send a message that triggers multiple tool calls
- Observe: the chat does not auto-scroll to show tool call blocks as they appear
- Compare: send a message that produces a long markdown response — auto-scroll works fine
Activity
test: add failing tests for late-mount contentRef bug (#2282)
fix(Chat): attach ResizeObserver after late content mount (#2282)
fix(Chat): use callback ref for content observation (#2282)
fix(Chat): use callback ref for content observation (#2282) (#2285)
fix(Chat): use callback ref for content observation (#2282) (#2285)
fix(Chat): use callback ref for content observation (#2282) (#2285)
feat(lab): experimental ChatVirtualizer (#5461)