diff --git a/src/features/agents/session/Stream.tsx b/src/features/agents/session/Stream.tsx index 75b627244f..16c4f25ab5 100644 --- a/src/features/agents/session/Stream.tsx +++ b/src/features/agents/session/Stream.tsx @@ -544,8 +544,8 @@ export function Stream({ shownRow.current = null; holdShownRow(); }, [holdShownRow]); - /** A diagram that drew after the last streamed chunk grows the conversation: keep following. */ - const onDiagramDrawn = useCallback(() => { + /** Content that grew outside a render (a diagram drawn, a row laid out late): keep following. */ + const followEnd = useCallback(() => { const el = ref.current; if (!el) return; if (position.current.stick && !dragging.current && slack.current === 0) { @@ -617,10 +617,11 @@ export function Stream({ }); useEffect(() => { // A row also changes height on its own (a reasoning folding when its turn moves on, text - // revealed at a pace, an image loading): before it paints. + // revealed at a pace, an image loading, a just-sent message laid out after its render): + // before it paints. Following the end, the end stays in view. const el = ref.current; if (!el || typeof ResizeObserver === "undefined") return; - const resize = new ResizeObserver(() => holdShownRow()); + const resize = new ResizeObserver(() => (position.current.stick ? followEnd() : holdShownRow())); const watch = () => { for (const row of el.querySelectorAll(`:scope > .${styles.conversationRow}`)) resize.observe(row); }; @@ -631,7 +632,7 @@ export function Stream({ rows.disconnect(); resize.disconnect(); }; - }, [holdShownRow]); + }, [followEnd, holdShownRow]); useLayoutEffect(() => { const el = ref.current; if (!el) return; @@ -763,10 +764,10 @@ export function Stream({ working={row.index === lastIndex && busy} drafts={drafts} onOpenSubagent={onOpenSubagent} - onDiagramDrawn={onDiagramDrawn} + onDiagramDrawn={followEnd} /> ), - [task, turns, replies, times, thinking, lastIndex, busy, drafts, onOpenSubagent, onDiagramDrawn], + [task, turns, replies, times, thinking, lastIndex, busy, drafts, onOpenSubagent, followEnd], ); const renderConversationRow = useCallback( (row: ConversationRow): ReactNode => @@ -777,12 +778,12 @@ export function Stream({ lastIndex={lastIndex} busy={busy} thinking={thinking} - onDiagramDrawn={onDiagramDrawn} + onDiagramDrawn={followEnd} /> ) : ( renderRow(row) ), - [task, lastIndex, busy, thinking, onDiagramDrawn, renderRow], + [task, lastIndex, busy, thinking, followEnd, renderRow], ); const agentName = providerName(task.provider); // Switching tabs changes visibility without changing the conversation rows. diff --git a/src/features/agents/session/followEnd.test.tsx b/src/features/agents/session/followEnd.test.tsx new file mode 100644 index 0000000000..c83f9e1a51 --- /dev/null +++ b/src/features/agents/session/followEnd.test.tsx @@ -0,0 +1,126 @@ +import { act, render, waitFor } from "@testing-library/react"; +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import type { AgentEntry, AgentTask } from "@/bindings"; + +const ipc = vi.hoisted(() => ({ entries: vi.fn() })); +vi.mock("@/platform/ipc", async (original) => ({ + ...(await original()), + commands: { + agentEntries: ipc.entries, + agentChanges: async () => ({ status: "ok", data: [] }), + agentGroupedChanges: async () => ({ + status: "ok", + data: { uncommitted: [], commits: [] }, + }), + vcsBranchInfo: async () => ({ status: "ok", data: { head: "main" } }), + }, +})); +vi.mock("@tauri-apps/api/webview", () => ({ + getCurrentWebview: () => ({ + onDragDropEvent: () => Promise.resolve(() => {}), + }), +})); + +import styles from "../Session.module.css"; +import { SessionEditor } from "../SessionEditor"; +import { useAgents } from "../store"; + +const entries: AgentEntry[] = [ + { type: "agent", text: "one" }, + { type: "agent", text: "two" }, +]; + +const task: AgentTask = { + id: "follow", + title: "Test task", + provider: "codex", + status: "running", + isolation: "local", + model: null, + cwd: "/project", + branch: null, + base: null, + baseCommit: null, + error: null, + createdMs: 0, + updatedMs: 0, + config: [], + commands: [], + usage: null, + queue: [], + capabilities: { fork: true, rollback: true, steer: true, images: true }, +}; + +/** Row resize callbacks, so a test can report a row laid out after its render. */ +const observers: { callback: ResizeObserverCallback; targets: Set }[] = []; +const OriginalResizeObserver = globalThis.ResizeObserver; + +beforeEach(() => { + observers.length = 0; + globalThis.ResizeObserver = class { + private entry: { callback: ResizeObserverCallback; targets: Set }; + constructor(callback: ResizeObserverCallback) { + this.entry = { callback, targets: new Set() }; + observers.push(this.entry); + } + observe(target: Element) { + this.entry.targets.add(target); + } + unobserve(target: Element) { + this.entry.targets.delete(target); + } + disconnect() { + this.entry.targets.clear(); + } + } as unknown as typeof ResizeObserver; + ipc.entries.mockReset().mockResolvedValue({ status: "ok", data: entries }); + useAgents.setState({ + loaded: true, + tasks: new Map([[task.id, task]]), + entries: new Map(), + composers: new Map(), + scrollPositions: new Map(), + focus: null, + }); +}); + +afterEach(() => { + globalThis.ResizeObserver = OriginalResizeObserver; +}); + +it("keeps following the end when a row grows without a render", async () => { + const { container } = render( + , + ); + const row = await waitFor(() => { + const found = container.querySelectorAll(`.${styles.conversationRow}`); + expect(found.length).toBeGreaterThan(1); + return found[found.length - 1]!; + }); + const scroller = row.parentElement as HTMLElement; + let content = 1000; + let top = 0; + Object.defineProperty(scroller, "clientHeight", { + value: 400, + configurable: true, + }); + Object.defineProperty(scroller, "scrollHeight", { + get: () => content, + configurable: true, + }); + Object.defineProperty(scroller, "scrollTop", { + get: () => top, + set: (next: number) => { + top = Math.max(0, Math.min(next, content - 400)); + }, + configurable: true, + }); + scroller.scrollTop = 600; + + // The last row lays out taller after the render that added it. + content = 1080; + const rowObserver = observers.find((o) => o.targets.has(row)); + expect(rowObserver).toBeTruthy(); + act(() => rowObserver!.callback([], {} as ResizeObserver)); + expect(scroller.scrollTop).toBe(680); +});