perf(subagent): read saved task history by loaded turns (#6634)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-12 21:37:08 +08:00
1 parent 1196698ecb
commit eb8546c321
20 files changed
+893 -97

No files matched your search

+11 -2
View File
@@ -1,4 +1,4 @@
import { Profiler, useState } from "react";
import { Profiler, useCallback, useState } from "react";
import { ThreadViewport } from "@/components/thread/ThreadViewport";
import { SubagentTasksProvider } from "@/components/thread/SubagentTasks";
import { useClient } from "@/providers/ClientProvider";
@@ -17,6 +17,12 @@ export function HistoryBoundaryFixture({ local = false, navigate = false, tasks
const history = useSessionHistory(local ? null : key);
const messages = local ? localMessages : history.messages;
const navigation = usePromptHistory(local ? null : key, history.version, messages);
const [taskWindow, setTaskWindow] = useState<{ key: string; turnIds: string[] } | null>(null);
const reportVisibleMessages = useCallback((visible: UIMessage[]) => {
const turnIds = [...new Set(visible.flatMap(message => message.role === "user" && message.turnId ? [message.turnId] : []))];
setTaskWindow(current => current?.key === key && current.turnIds.length === turnIds.length
&& current.turnIds.every((turnId, index) => turnId === turnIds[index]) ? current : { key, turnIds });
}, [key]);
return <main className="bg-background text-foreground" style={{ height: "100vh", display: "flex", flexDirection: "column" }}>
<div className="flex h-10 shrink-0 items-center gap-4 px-4 text-sm">
<button onClick={() => setKey((current) => current.endsWith("-a") ? "websocket:boundary-b" : "websocket:boundary-a")}>Switch conversation</button>
@@ -24,10 +30,13 @@ export function HistoryBoundaryFixture({ local = false, navigate = false, tasks
<Profiler id="history-boundary" onRender={(_, phase, actualDuration) => {
window.dispatchEvent(new CustomEvent("history-boundary-render", { detail: { phase, actualDuration } }));
}}>
<SubagentTasksProvider client={client} token={token} sessionKey={key} enabled={tasks} liveEvents={false}>
<SubagentTasksProvider client={client} token={token} sessionKey={key} enabled={tasks} liveEvents={false}
turnIds={taskWindow?.key === key ? taskWindow.turnIds : []}
windowReady={taskWindow?.key === key && (!history.loading || messages.length > 0)}>
<ThreadViewport messages={messages} isStreaming={false} conversationKey={key}
messagesKey={local ? key : history.messagesKey}
conversationReady={local || history.messagesReady} hasMoreBefore={history.hasMoreBefore}
onVisibleMessagesChange={tasks ? reportVisibleMessages : undefined}
initialLoading={history.loading} initialError={history.error} onRetryInitial={history.refresh}
loadingOlder={history.loadingOlder} olderError={history.olderError} onLoadOlder={history.loadOlder}
navigation={navigate ? navigation : undefined} userMessageOffset={history.userMessageOffset}
+30 -14
View File
@@ -8,7 +8,7 @@ function historyPage(start: number, count = 12, more = start > 0) {
has_more_before: more, has_more_after: start + count < 52 } };
}
async function installHistory(page: Page, initialStart = 40, initialCount = 12) {
async function installHistory(page: Page, initialStart = 40, initialCount = 12, pageContent = historyPage) {
const reads: string[] = [];
const pending: Array<(value: "success" | "error") => void> = [];
let fastResponse = false;
@@ -19,9 +19,9 @@ async function installHistory(page: Page, initialStart = 40, initialCount = 12)
prompts: Array.from({ length: 52 }, (_, i) => ({ id: `u${i}`, preview: `Question ${i}`, answerPreview: "Historical answer", turnId: `turn${i}`, cursor: String(i), createdAt: i + 1 })),
} });
if (!url.pathname.endsWith("/webui-thread")) return route.fulfill({ json: {} });
if (url.pathname.includes("boundary-b")) return route.fulfill({ json: historyPage(0, 2, false) });
if (url.pathname.includes("boundary-b")) return route.fulfill({ json: pageContent(0, 2, false) });
const cursor = url.searchParams.get("before") ?? url.searchParams.get("after");
if (cursor === null) return route.fulfill({ json: historyPage(initialStart, initialCount) });
if (cursor === null) return route.fulfill({ json: pageContent(initialStart, initialCount) });
reads.push(url.search);
const outcome = fastResponse
? await new Promise<"success">((resolve) => { releaseFastResponse = () => resolve("success"); })
@@ -29,7 +29,7 @@ async function installHistory(page: Page, initialStart = 40, initialCount = 12)
if (outcome === "error") return route.fulfill({ status: 503, body: "History unavailable" });
const start = url.searchParams.has("after") ? Number(cursor) : Math.max(0, Number(cursor) - 12);
const count = url.searchParams.has("after") ? 12 : Number(cursor) - start;
await route.fulfill({ json: historyPage(start, count, start > 0) });
await route.fulfill({ json: pageContent(start, count, start > 0) });
});
return {
reads,
@@ -305,16 +305,28 @@ test("wheel loading hands off to a retained 48px slot; prepend and delayed layou
});
test("task results stay with their prompts and retain the reading boundary when earlier messages load", async ({ page }) => {
const history = await installHistory(page);
await page.route("**/api/sessions/**/subagents", (route) => route.fulfill({ json: { tasks: [
{ task_id: "current", label: "Current check", origin_message_id: "u40" },
{ task_id: "earlier", label: "Earlier check", origin_message_id: "u28" },
].map((task) => ({
...task, revision: 1, task_description: "Check work",
state: "done", phase: "completed", elapsed_seconds: 2, iteration: 1,
tool_events: [], usage: null, receipts: {}, result: "Verified", partial: false,
stop_reason: null, error: null, origin_turn_id: null, created_at: 100, completed_at: 102,
})) } }));
const history = await installHistory(page, 40, 12, (start, count, more) => {
const payload = historyPage(start, count, more);
return { ...payload, events: payload.events.map(event => ({
...event, turn_id: `turn${event.projection_id.slice(1)}`,
})) };
});
const taskReads: string[][] = [];
await page.route("**/api/sessions/**/subagents?*", (route) => {
const query = new URL(route.request().url()).searchParams;
expect(query.get("scope")).toBe("window");
const turns = query.getAll("turn_id");
taskReads.push(turns);
return route.fulfill({ json: { tasks: [
{ task_id: "current", label: "Current check", origin_turn_id: "turn40" },
{ task_id: "earlier", label: "Earlier check", origin_turn_id: "turn28" },
].filter(task => turns.includes(task.origin_turn_id)).map((task) => ({
...task, revision: 1, task_description: "Check work",
state: "done", phase: "completed", elapsed_seconds: 2, iteration: 1,
tool_events: [], usage: null, receipts: {}, result: "Verified", partial: false,
stop_reason: null, error: null, origin_message_id: null, created_at: 100, completed_at: 102,
})) } });
});
await page.goto("/browser/fixture.html?case=history-boundary&tasks");
const body = page.getByTestId("thread-message-region");
const currentResults = body.locator('[data-user-prompt-id="u40"] + [data-subagent-results]');
@@ -347,6 +359,10 @@ test("task results stay with their prompts and retain the reading boundary when
expect(history.reads).toHaveLength(1);
await earlierResults.locator("[data-subagent-results-summary]").click();
await expect(earlierResults.getByRole("button", { name: /Earlier check Completed/ })).toBeVisible();
expect(taskReads).toEqual([
Array.from({ length: 12 }, (_, index) => `turn${40 + index}`),
Array.from({ length: 12 }, (_, index) => `turn${28 + index}`),
]);
});
test("touch reveals a static partial ready header, arms only on release, and retains a stable full loading header", async ({ browser, baseURL }) => {