fix(webui): add spring feedback at the history boundary (#6322)

* fix(webui): add spring feedback at the history boundary

Refs NAN-287.

* fix(webui): refine history boundary states and retry feedback

* fix(webui): share history loading states with subagents
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-20 15:32:21 +08:00
1 parent a1bca7dc50
commit 816c9221a0
27 files changed
+910 -209

No files matched your search

+20 -7
View File
@@ -174,21 +174,34 @@ describe("session-owned task UI", () => {
expect(await within(screen.getByRole("dialog")).findByText("Latest child response")).toBeVisible();
});
it("retries a failed history read through the shared history controls", async () => {
let offline = true;
it("shares history loading, actual failure details and keyboard retry with the main thread", async () => {
let rejectRead!: (reason: Error) => void;
let resolveRead!: (value: Response) => void;
let reads = 0;
vi.stubGlobal("fetch", vi.fn(async (url: string) => {
if (!url.endsWith("/webui-thread")) return response([task()]);
if (offline) throw new Error("offline");
return childThread("Recovered child response");
reads += 1;
return new Promise<Response>((resolve, reject) => { resolveRead = resolve; rejectRead = reject; });
}));
const user = userEvent.setup();
render(layout({ historyEnabled: true }));
await user.click(await screen.findByRole("button", { name: /Config check Running/ }));
const detail = screen.getByRole("dialog");
expect(await within(detail).findByRole("alert")).toHaveTextContent("Loading failed.");
offline = false;
await user.click(within(detail).getByRole("button", { name: "Retry" }));
const loading = within(detail).getByRole("status");
expect(loading.querySelectorAll(".thread-history-blocks > span")).toHaveLength(9);
expect(loading.closest(".thread-history-status")).toHaveAttribute("data-state", "loading");
await act(async () => { rejectRead(new Error("offline")); });
const retry = await within(detail).findByRole("button", { name: "Loading failed. Retry" });
expect(retry.closest(".thread-history-status")).toHaveAttribute("data-state", "error");
expect(retry.querySelectorAll(".thread-history-blocks > span")).toHaveLength(9);
act(() => { retry.focus(); });
expect(screen.getByRole("tooltip")).toHaveTextContent("offline");
await user.keyboard("{Enter}");
expect(reads).toBe(2);
expect(within(detail).getByRole("status").closest(".thread-history-status")).toHaveAttribute("data-state", "loading");
await act(async () => { resolveRead(childThread("Recovered child response")); });
expect(await within(detail).findByText("Recovered child response")).toBeVisible();
expect(within(detail).queryByRole("status")).not.toBeInTheDocument();
});
it("keeps the same work block under its prompt, folds completed work and restores it after refresh", async () => {
@@ -0,0 +1,99 @@
import { describe, expect, it, vi } from "vitest";
import { HISTORY_PULL_LIMIT_PX, ThreadHistorySpring } from "@/components/thread/thread-history-spring";
function harness(reduced = false) {
let now = 0;
let nextId = 0;
let distance = 0;
const frames = new Map<number, FrameRequestCallback>();
const scheduler = {
now: () => now,
request: vi.fn((callback: FrameRequestCallback) => { frames.set(++nextId, callback); return nextId; }),
cancel: (id: number) => { frames.delete(id); },
};
const spring = new ThreadHistorySpring({ scheduler, prefersReducedMotion: () => reduced,
onUpdate: (next) => { distance = next; } });
const advance = (milliseconds = 16) => {
now += milliseconds;
const callbacks = [...frames.values()];
frames.clear();
callbacks.forEach((callback) => callback(now));
return distance;
};
const settle = () => { for (let i = 0; i < 100 && frames.size; i++) advance(); };
return { spring, scheduler, advance, settle, frames, distance: () => distance };
}
describe("history boundary spring", () => {
it("resists a short touch and returns continuously without triggering a request", () => {
const h = harness();
h.spring.startTouch();
h.spring.moveTouch(40);
const pulled = h.distance();
expect(pulled).toBeGreaterThan(0);
expect(pulled).toBeLessThan(40);
expect(h.spring.endTouch()).toBe(false);
expect(h.distance()).toBe(pulled);
expect(h.advance()).toBeGreaterThan(0);
expect(h.distance()).toBeLessThan(pulled);
h.settle();
expect(h.distance()).toBe(0);
expect(h.frames.size).toBe(0);
});
it("holds actual loading still, lets a new touch take over, and converges immediately after long frame gaps", () => {
const h = harness();
h.spring.startTouch();
h.spring.moveTouch(120);
expect(h.spring.endTouch()).toBe(true);
h.spring.setLoading(true);
h.settle();
expect(h.distance()).toBe(20);
expect(h.frames.size).toBe(0);
h.spring.startTouch();
expect(h.distance()).toBe(20);
h.spring.moveTouch(20);
expect(h.distance()).toBeGreaterThan(20);
h.spring.endTouch();
h.spring.setLoading(false);
h.advance(30_000);
expect(h.distance()).toBe(0);
expect(h.frames.size).toBe(0);
});
it("merges decelerating wheel input into one bounded spring and one boundary request", () => {
const h = harness();
let requests = 0;
for (const delta of [100, 70, 48, 32, 20, 12, 6, 2]) {
if (h.spring.wheel(delta)) requests++;
expect(h.frames.size).toBe(1);
h.advance();
}
expect(requests).toBe(1);
expect(h.distance()).toBeLessThan(HISTORY_PULL_LIMIT_PX);
h.settle();
expect(h.distance()).toBe(0);
expect(h.spring.wheel(100)).toBe(true);
});
it("cancels task-owned motion on conversation changes", () => {
const h = harness();
h.spring.wheel(100);
h.spring.setLoading(true);
h.spring.reset();
expect(h.frames.size).toBe(0);
expect(h.distance()).toBe(0);
expect(h.spring.wheel(100)).toBe(true);
});
it("keeps the touch trigger functional with reduced motion and no display travel", () => {
const h = harness(true);
h.spring.startTouch();
h.spring.moveTouch(120);
expect(h.distance()).toBe(0);
expect(h.spring.endTouch()).toBe(true);
h.spring.setLoading(true);
expect(h.frames.size).toBe(0);
expect(h.distance()).toBe(0);
});
});
+34 -39
View File
@@ -1,51 +1,46 @@
import { act, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { ThreadHistoryStatus } from "@/components/thread/ThreadHistoryStatus";
afterEach(() => {
vi.useRealTimers();
});
describe("ThreadHistoryStatus", () => {
it("delays the loading indicator so fast history requests do not flash", () => {
vi.useFakeTimers();
render(<ThreadHistoryStatus loading error={null} onRetry={vi.fn()} />);
expect(screen.queryByText("Loading earlier messages…")).not.toBeInTheDocument();
act(() => vi.advanceTimersByTime(179));
expect(screen.queryByText("Loading earlier messages…")).not.toBeInTheDocument();
act(() => vi.advanceTimersByTime(1));
expect(screen.getByText("Loading earlier messages…")).toHaveClass("sr-only");
expect(screen.getByRole("status").querySelector("svg")).toHaveClass("animate-spin");
});
it("cancels the delay on completion and starts a fresh delay for the next request", () => {
vi.useFakeTimers();
it("exposes real loading immediately and switches to a distinct exhausted state on completion", () => {
const props = { error: null, onRetry: vi.fn() };
const { rerender } = render(<ThreadHistoryStatus {...props} loading />);
act(() => vi.advanceTimersByTime(100));
rerender(<ThreadHistoryStatus {...props} loading={false} />);
act(() => vi.advanceTimersByTime(500));
expect(screen.getByRole("status")).toBeEmptyDOMElement();
rerender(<ThreadHistoryStatus {...props} loading />);
act(() => vi.advanceTimersByTime(180));
const { container, rerender } = render(<ThreadHistoryStatus {...props} loading />);
expect(screen.getByText("Loading earlier messages…")).toHaveClass("sr-only");
expect(screen.getByRole("status").querySelector("svg")).toHaveClass("animate-spin");
rerender(<ThreadHistoryStatus {...props} loading={false} />);
expect(screen.getByRole("status")).toBeEmptyDOMElement();
rerender(<ThreadHistoryStatus {...props} loading />);
expect(screen.getByRole("status")).toBeEmptyDOMElement();
expect(container.firstElementChild).toHaveAttribute("data-state", "loading");
expect(screen.getByRole("status").querySelectorAll(".thread-history-blocks > span")).toHaveLength(9);
rerender(<ThreadHistoryStatus {...props} loading={false} exhausted />);
expect(screen.getByText("No earlier messages")).toHaveClass("text-xs");
expect(container.firstElementChild).toHaveAttribute("data-state", "exhausted");
});
it("offers an explicit retry after loading earlier history fails", () => {
it("uses one loading display before and during a request while preserving keyboard activation", () => {
const onRetry = vi.fn();
render(<ThreadHistoryStatus loading={false} error="offline" onRetry={onRetry} />);
expect(screen.getByText("Loading failed.")).toBeVisible();
expect(screen.getByRole("status").parentElement).toHaveClass("thread-message-row");
fireEvent.click(screen.getByRole("button", { name: "Retry" }));
const { container, rerender } = render(<ThreadHistoryStatus loading={false} error={null} onRetry={onRetry} />);
expect(container.firstElementChild).toHaveAttribute("data-state", "loading");
const load = screen.getByRole("button", { name: "Load earlier messages" });
expect(load.querySelectorAll(".thread-history-blocks > span")).toHaveLength(9);
fireEvent.click(load);
expect(onRetry).toHaveBeenCalledTimes(1);
rerender(<ThreadHistoryStatus loading error={null} onRetry={onRetry} />);
expect(container.firstElementChild).toHaveAttribute("data-state", "loading");
expect(screen.getByRole("status").querySelectorAll(".thread-history-blocks > span")).toHaveLength(9);
});
it("offers error Blocks with the actual failure on keyboard focus and resumes loading on retry", () => {
const onRetry = vi.fn();
const { container, rerender } = render(<ThreadHistoryStatus loading={false} error="offline" onRetry={onRetry} />);
const retry = screen.getByRole("button", { name: "Loading failed. Retry" });
expect(retry.querySelectorAll(".thread-history-blocks > span")).toHaveLength(9);
expect(screen.getByText("Loading failed. Retry")).toHaveClass("sr-only");
fireEvent.focus(retry);
expect(screen.getByRole("tooltip")).toHaveTextContent("offline");
fireEvent.click(retry);
expect(onRetry).toHaveBeenCalledTimes(1);
rerender(<ThreadHistoryStatus loading error={null} onRetry={onRetry} />);
expect(container.firstElementChild).toHaveAttribute("data-state", "loading");
expect(screen.getByRole("status").querySelectorAll(".thread-history-blocks > span")).toHaveLength(9);
});
});
+42 -19
View File
@@ -1,4 +1,4 @@
import { createRef, useRef } from "react";
import { createRef, Profiler, useRef } from "react";
import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
@@ -40,12 +40,17 @@ function dispatchUserScroll(scroller: HTMLElement): void {
scroller.dispatchEvent(new Event("scroll"));
}
function historyPull(scroller: HTMLElement): number {
return Number.parseFloat(/translateY\(([-.\d]+)px\)/.exec(scroller.style.transform)?.[1] ?? "0");
}
function dispatchHistoryInput(scroller: HTMLElement, input: "wheel" | "touch" | "keyboard") {
if (input === "wheel") fireEvent.wheel(scroller, { deltaY: -100 });
if (input === "keyboard") fireEvent.keyDown(scroller, { key: "PageUp" });
if (input === "touch") {
fireEvent.touchStart(scroller, { touches: [{ clientX: 100, clientY: 200 }] });
fireEvent.touchMove(scroller, { touches: [{ clientX: 100, clientY: 300 }] });
fireEvent.touchEnd(scroller);
}
}
@@ -1542,7 +1547,7 @@ describe("ThreadViewport", () => {
expect(screen.queryByText(`message ${firstVisible - 1}`)).not.toBeInTheDocument();
expect(screen.getByText(`message ${firstVisible}`)).toBeInTheDocument();
expect(screen.getByText("message 299")).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Load earlier messages" })).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Load earlier messages" })).toBeInTheDocument();
});
it("automatically expands earlier local history near the top", () => {
@@ -1712,7 +1717,7 @@ describe("ThreadViewport", () => {
const scroller = getScroller(container);
dispatchHistoryInput(scroller, input);
expect(onLoadOlder).not.toHaveBeenCalled();
expect(scroller.style.transform).toBe("");
if ("conversationReady" in blocked && blocked.conversationReady === false) expect(historyPull(scroller)).toBe(0);
});
});
@@ -1762,11 +1767,11 @@ describe("ThreadViewport", () => {
});
expect(onLoadOlder).not.toHaveBeenCalled();
fireEvent.click(screen.getByRole("button", { name: "Retry" }));
fireEvent.click(screen.getByRole("button", { name: "Loading failed. Retry" }));
expect(onLoadOlder).toHaveBeenCalledTimes(1);
});
it("hides history feedback away from the top without fetching again", async () => {
it("keeps real loading visible after leaving the top without fetching again", async () => {
const onLoadOlder = vi.fn();
const { container, rerender } = render(
<ThreadViewport messages={messages} isStreaming={false}
@@ -1783,13 +1788,13 @@ describe("ThreadViewport", () => {
scroller.scrollTop = 800;
dispatchUserScroll(scroller);
});
expect(screen.queryByText("Loading earlier messages…")).not.toBeInTheDocument();
expect(screen.getByText("Loading earlier messages…")).toBeInTheDocument();
rerender(<ThreadViewport messages={messages} isStreaming={false}
conversationKey="another" hasMoreBefore onLoadOlder={onLoadOlder} />);
expect(screen.queryByText("Loading earlier messages…")).not.toBeInTheDocument();
});
it("reveals touch feedback before fetching, cancels short pulls, and fetches once per gesture", () => {
it("reveals touch feedback, springs back on cancellation, and requests only on an armed release", async () => {
const onLoadOlder = vi.fn();
const props = { messages, isStreaming: false, hasMoreBefore: true, onLoadOlder };
const { container, rerender } = render(<ThreadViewport {...props} />);
@@ -1797,24 +1802,25 @@ describe("ThreadViewport", () => {
const touch = (y: number) => ({ touches: [{ clientX: 100, clientY: y }] });
fireEvent.touchStart(scroller, touch(200));
fireEvent.touchMove(scroller, touch(240));
expect(scroller).toHaveStyle({ transform: "translateY(20px)" });
expect(screen.getByRole("status").querySelector("svg")).not.toBeNull();
expect(historyPull(scroller)).toBeGreaterThan(0);
expect(screen.getByRole("status").querySelectorAll(".thread-history-blocks > span")).toHaveLength(9);
expect(onLoadOlder).not.toHaveBeenCalled();
fireEvent.touchEnd(scroller);
expect(scroller.style.transform).toBe("");
expect(historyPull(scroller)).toBeGreaterThan(0);
await waitFor(() => expect(historyPull(scroller)).toBe(0));
fireEvent.touchStart(scroller, touch(200));
fireEvent.touchMove(scroller, touch(300));
expect(onLoadOlder).not.toHaveBeenCalled();
expect(screen.getByRole("status").querySelectorAll(".thread-history-blocks > span")).toHaveLength(9);
fireEvent.touchEnd(scroller);
expect(onLoadOlder).toHaveBeenCalledTimes(1);
rerender(<ThreadViewport {...props} loadingOlder />);
fireEvent.touchMove(scroller, touch(350));
fireEvent.scroll(scroller);
expect(onLoadOlder).toHaveBeenCalledTimes(1);
expect(screen.getByRole("status").querySelector("svg")).toHaveClass("animate-spin");
fireEvent.touchEnd(scroller);
expect(scroller).toHaveStyle({ transform: "translateY(48px)" });
expect(screen.getByText("Loading earlier messages…")).toBeInTheDocument();
rerender(<ThreadViewport {...props} />);
expect(scroller.style.transform).toBe("");
await waitFor(() => expect(historyPull(scroller)).toBe(0));
});
it("does not fetch on touch scroll alone or horizontal gestures and clears pull on session change", () => {
@@ -1825,23 +1831,40 @@ describe("ThreadViewport", () => {
fireEvent.touchStart(scroller, { touches: [{ clientX: 100, clientY: 200 }] });
fireEvent.touchMove(scroller, { touches: [{ clientX: 300, clientY: 220 }] });
fireEvent.scroll(scroller);
expect(scroller.style.transform).toBe("");
expect(historyPull(scroller)).toBe(0);
expect(onLoadOlder).not.toHaveBeenCalled();
fireEvent.touchEnd(scroller);
fireEvent.touchStart(scroller, { touches: [{ clientX: 100, clientY: 200 }] });
fireEvent.touchMove(scroller, { touches: [{ clientX: 100, clientY: 240 }] });
rerender(<ThreadViewport {...props} conversationKey="b" />);
expect(scroller.style.transform).toBe("");
expect(historyPull(scroller)).toBe(0);
expect(onLoadOlder).not.toHaveBeenCalled();
});
it("leaves completed history quiet", () => {
it("animates boundary frames without rerendering the message tree", async () => {
const onRender = vi.fn();
const { container } = render(<Profiler id="history" onRender={onRender}>
<ThreadViewport messages={messages} isStreaming={false} />
</Profiler>);
await flushAnimationFrame();
await flushAnimationFrame();
fireEvent.wheel(getScroller(container), { deltaY: -100 });
await flushAnimationFrame();
onRender.mockClear();
await flushAnimationFrame();
await flushAnimationFrame();
expect(historyPull(getScroller(container))).toBeGreaterThan(0);
expect(onRender).not.toHaveBeenCalled();
});
it("shows the confirmed start of conversation with boundary feedback", async () => {
const onLoadOlder = vi.fn();
const { container } = render(<ThreadViewport messages={messages} isStreaming={false}
onLoadOlder={onLoadOlder} />);
fireEvent.wheel(getScroller(container), { deltaY: -100 });
expect(screen.getByText("hello")).toBeVisible();
expect(screen.queryByRole("status")).not.toBeInTheDocument();
expect(screen.getByText("No earlier messages")).toBeVisible();
await waitFor(() => expect(historyPull(getScroller(container))).toBeGreaterThan(0));
expect(onLoadOlder).not.toHaveBeenCalled();
});