From c1ed649b81906394e35d553ae2a3319ebfe7b935 Mon Sep 17 00:00:00 2001 From: chengyongru <61816729+chengyongru@users.noreply.github.com> Date: Mon, 2 Nov 2026 17:42:16 +0800 Subject: [PATCH] fix(webui): avoid remounting outgoing conversations (#6540) * fix(webui): avoid remounting outgoing conversations Reset subtask disclosure state within its session scope instead of keying the entire transcript before the stream message handoff. Cover the outgoing DOM lifecycle and disclosure reset with unit and browser regressions. Resolves NAN-396 * fix(webui): reset transcript UI on message ownership changes Keep the selected conversation separate from the owner of currently rendered messages. Reset the message subtree only when its owner changes, so cached and forked conversations get fresh UI state without remounting outgoing diffs. Extend the history-only caller and cover cached fork activity isolation while retaining the outgoing-DOM regression. --- webui/browser/history-boundary-fixture.tsx | 3 +- webui/browser/history-hydration.spec.ts | 96 +++++++++++++++++++ .../components/thread/SubagentTaskResults.tsx | 23 ++--- webui/src/components/thread/ThreadShell.tsx | 2 + .../src/components/thread/ThreadViewport.tsx | 4 + webui/src/hooks/useNanobotStream.ts | 3 + webui/src/hooks/useSessions.ts | 4 +- webui/src/tests/subagent-results.test.tsx | 42 +++++++- webui/src/tests/useNanobotStream.test.tsx | 2 + webui/src/tests/useSessions.test.tsx | 2 + 10 files changed, 165 insertions(+), 16 deletions(-) diff --git a/webui/browser/history-boundary-fixture.tsx b/webui/browser/history-boundary-fixture.tsx index 74c2aca66..76d316e11 100644 --- a/webui/browser/history-boundary-fixture.tsx +++ b/webui/browser/history-boundary-fixture.tsx @@ -26,7 +26,8 @@ export function HistoryBoundaryFixture({ local = false, navigate = false, tasks }}> History boundary fixture} /> diff --git a/webui/browser/history-hydration.spec.ts b/webui/browser/history-hydration.spec.ts index d53dd5dbb..4b1a1d45d 100644 --- a/webui/browser/history-hydration.spec.ts +++ b/webui/browser/history-hydration.spec.ts @@ -270,6 +270,102 @@ test("synthetic HTTP-first cumulative text suppresses delayed covered chunks but backend.healthy(); }); +test("switching away from file edits does not rebuild the outgoing diff before history hands over", async ({ page }) => { + await page.addInitScript(() => { + localStorage.setItem("nanobot-webui.settings-preferences", JSON.stringify({ + activityMode: "expanded", fileEditDisplayMode: "diff", + })); + }); + const { backend, body } = await openFixture(page); + const edited = history([ + input("edited-turn", "Review these synthetic edits"), + { ...at("edited-turn", 1, "activity"), event: "file_edit", edits: [ + { call_id: "edit-1", tool: "edit_file", path: "src/example.ts", phase: "end", + status: "done", added: 1, deleted: 1, approximate: false, + diff: { format: "unified", text: "--- a/src/example.ts\n+++ b/src/example.ts\n@@ -1 +1 @@\n-const previous = 1;\n+const updated = 2;\n" } }, + ] }, + { ...at("edited-turn", 2, "answer"), event: "message", text: "Synthetic edits complete." }, + { ...at("edited-turn", 3, "complete"), event: "turn_end" }, + ]); + await backend.release(await backend.read(), edited); + await expect(body.getByTestId("syntax-highlighted-diff-hunk")).toBeVisible(); + const additions = await page.evaluateHandle(() => { + const state = { diffs: 0 }; + const observer = new MutationObserver((records) => { + for (const record of records) for (const node of record.addedNodes) { + if (!(node instanceof Element)) continue; + state.diffs += Number(node.matches('[data-testid="file-edit-diff"]')) + + node.querySelectorAll('[data-testid="file-edit-diff"]').length; + } + }); + observer.observe(document.body, { childList: true, subtree: true }); + return { state, observer }; + }); + await page.getByRole("button", { name: "Conversation B", exact: true }).click(); + const next = await backend.read(B); + expect(await additions.evaluate(({ state, observer }) => { + observer.disconnect(); + return state.diffs; + })).toBe(0); + await additions.dispose(); + await backend.release(next, history(closedTurn("other-turn", "Other question", "Other answer.", B), { chatId: B })); + await expect(body.getByText("Other answer.", { exact: true })).toBeVisible(); + await expect(body.getByTestId("file-edit-diff")).toHaveCount(0); + + await page.getByRole("button", { name: "Conversation A", exact: true }).click(); + await backend.release(await backend.read(A, null, 1), edited); + await expect(body.getByTestId("syntax-highlighted-diff-hunk")).toBeVisible(); + await expect(body.getByText("Synthetic edits complete.", { exact: true })).toBeVisible(); + backend.healthy(); +}); + +test("cached conversations with a shared fork prefix reset activity disclosures on message handoff", async ({ page }) => { + const { backend, body } = await openFixture(page); + const prefix: ThreadProjectionEvent[] = [ + input("shared-turn", "Shared question"), + { ...at("shared-turn", 1, "activity"), event: "message", kind: "tool_hint", text: "Shared activity detail" }, + { ...at("shared-turn", 2, "answer"), event: "message", text: "Shared answer." }, + { ...at("shared-turn", 3, "complete"), event: "turn_end" }, + ]; + const source = history([...prefix, ...closedTurn("source-turn", "Source question", "Source answer.")]); + const fork = history([ + ...prefix.map((event) => ({ ...event, chat_id: B })), + ...closedTurn("fork-turn", "Fork question", "Fork answer.", B), + ], { chatId: B }); + await backend.release(await backend.read(), source); + await expect(body.getByText("Source answer.", { exact: true })).toBeVisible(); + await page.getByRole("button", { name: "Conversation B", exact: true }).click(); + await backend.release(await backend.read(B), fork); + await expect(body.getByText("Fork answer.", { exact: true })).toBeVisible(); + await page.getByRole("button", { name: "Conversation A", exact: true }).click(); + await backend.read(A, null, 1); + await expect(body.getByText("Source answer.", { exact: true })).toBeVisible(); + const sharedBlock = body.locator("[data-thread-display-unit]").filter({ has: page.getByText("Shared answer.", { exact: true }) }); + await sharedBlock.hover(); + await sharedBlock.getByRole("button", { name: "Message actions", exact: true }).click(); + const activity = page.locator("[data-message-block-activity-action]"); + await expect(activity).toHaveAttribute("aria-expanded", "false"); + await activity.click(); + await expect(sharedBlock.getByTestId("agent-activity-content")).toBeVisible(); + + await page.getByRole("button", { name: "Conversation B", exact: true }).click(); + const forkRead = await backend.read(B, null, 1); + await expect(body.getByText("Fork answer.", { exact: true })).toBeVisible(); + await sharedBlock.hover(); + await sharedBlock.getByRole("button", { name: "Message actions", exact: true }).click(); + await expect(activity).toHaveAttribute("aria-expanded", "false"); + await page.keyboard.press("Escape"); + await backend.release(forkRead, fork); + + await page.getByRole("button", { name: "Conversation A", exact: true }).click(); + await backend.release(await backend.read(A, null, 2), source); + await expect(body.getByText("Source answer.", { exact: true })).toBeVisible(); + await sharedBlock.hover(); + await sharedBlock.getByRole("button", { name: "Message actions", exact: true }).click(); + await expect(activity).toHaveAttribute("aria-expanded", "false"); + backend.healthy(); +}); + test("synthetic cache return retains unpainted chunks and the absent conversation's buffered wire tail", async ({ page }) => { const { backend, body } = await openFixture(page); const initial = history([input(TURN, "Cache question"), end(3, "Cache answer", "cache-wire", true)], { active: TURN }); diff --git a/webui/src/components/thread/SubagentTaskResults.tsx b/webui/src/components/thread/SubagentTaskResults.tsx index 6857868fe..c755257cc 100644 --- a/webui/src/components/thread/SubagentTaskResults.tsx +++ b/webui/src/components/thread/SubagentTaskResults.tsx @@ -43,23 +43,24 @@ function groupResults(messages: UIMessage[], tasks: ObservedSubagentTask[]) { export function SubagentThreadMessages(props: ComponentProps) { const context = useSubagentTasks(); - return ; -} - -function ResultsThreadMessages({ tasks, ...props }: ComponentProps & { tasks: ObservedSubagentTask[] }) { - const groups = useMemo(() => groupResults(props.messages, tasks), [props.messages, tasks]); - const [expandedTaskIds, setExpandedTaskIds] = useState>(() => new Set()); - const expanded = (results: ObservedSubagentTask[]) => results.some(task => expandedTaskIds.has(task.task_id)); + const sessionKey = context?.sessionKey; + const groups = useMemo(() => groupResults(props.messages, context?.tasks ?? []), [props.messages, context?.tasks]); + const [archive, setArchive] = useState(() => ({ sessionKey, expanded: new Set() })); + // Reset only the disclosures; the outgoing transcript must not remount during handoff. + if (archive.sessionKey !== sessionKey) { + setArchive({ sessionKey, expanded: new Set() }); + } + const expanded = (results: ObservedSubagentTask[]) => results.some(task => archive.expanded.has(task.task_id)); const toggle = (results: ObservedSubagentTask[]) => { props.onActivityToggle?.(); - setExpandedTaskIds(current => { - const next = new Set(current); - const close = results.some(task => current.has(task.task_id)); + setArchive(current => { + const next = new Set(current.expanded); + const close = results.some(task => current.expanded.has(task.task_id)); for (const task of results) { if (close) next.delete(task.task_id); else next.add(task.task_id); } - return next; + return { sessionKey, expanded: next }; }); }; diff --git a/webui/src/components/thread/ThreadShell.tsx b/webui/src/components/thread/ThreadShell.tsx index 0689bbedc..01759f667 100644 --- a/webui/src/components/thread/ThreadShell.tsx +++ b/webui/src/components/thread/ThreadShell.tsx @@ -589,6 +589,7 @@ export function ThreadShell({ }, [chatId, onTurnEnd]); const { messages, + messagesKey, messagesReady, isStreaming, runStartedAt, @@ -1499,6 +1500,7 @@ export function ThreadShell({ {useHasSubagentResults() ? "yes" : "no"}; } -function layout({ threadMessages = messages, liveEvents = true, historyEnabled = false, onActivityToggle }: { +function layout({ threadMessages = messages, sessionKey = "websocket:a", liveEvents = true, historyEnabled = false, onActivityToggle }: { threadMessages?: UIMessage[]; + sessionKey?: string; liveEvents?: boolean; historyEnabled?: boolean; onActivityToggle?: () => void; } = {}) { - return + return