fix(webui): admit history without dropping live output (NAN-311)

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-22 01:22:49 +08:00
1 parent df63526ddf
commit b37a23698d
21 files changed
+3026 -1607

No files matched your search

+37 -24
View File
@@ -5,7 +5,7 @@ import { describe, expect, it, vi } from "vitest";
import { useNanobotStream } from "@/hooks/useNanobotStream";
import { ThreadVisibilityContext } from "@/hooks/useThreadVisibility";
import { normalizeActivityTimeline } from "@/lib/activity-timeline";
import type { ThreadCheckpoint } from "@/lib/thread-checkpoint";
import { ThreadMessageCache } from "@/lib/thread-message-cache";
import type { StreamError } from "@/lib/nanobot-client";
import type {
ConnectionStatus,
@@ -205,20 +205,21 @@ async function flushStreamFrame() {
}
describe("useNanobotStream", () => {
it("resumes a merge_next checkpoint including reasoning queued before detach", () => {
it("resumes a merge_next cached snapshot including reasoning queued before detach", () => {
const fake = fakeClient();
let checkpoint: ThreadCheckpoint | undefined;
const first = renderHook(() => useNanobotStream("checkpoint", EMPTY_MESSAGES, false, undefined,
(value) => { checkpoint = value; }), { wrapper: wrap(fake.client) });
const cache = new ThreadMessageCache(() => false);
const first = renderHook(() => useNanobotStream("checkpoint", EMPTY_MESSAGES, false,
undefined, undefined, [], { key: null, cache }), { wrapper: wrap(fake.client) });
act(() => {
fake.emit("checkpoint", { event: "delta", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", turn_seq: 1, text: "First " });
fake.emit("checkpoint", { event: "stream_end", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", turn_seq: 2, text: "First ", resuming: true, merge_next: true });
fake.emit("checkpoint", { event: "reasoning_delta", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", turn_seq: 3, text: "Queued " });
});
first.unmount();
expect(checkpoint?.projection.messages[0].reasoning).toBe("Queued ");
const restored = renderHook(() => useNanobotStream("checkpoint", checkpoint!.projection.messages, false,
undefined, undefined, [], checkpoint), { wrapper: wrap(fake.client) });
const snapshot = cache.get("checkpoint")!;
expect(snapshot.projection.messages[0].reasoning).toBe("Queued ");
const restored = renderHook(() => useNanobotStream("checkpoint", EMPTY_MESSAGES, false,
undefined, undefined, [], { key: null, cache }), { wrapper: wrap(fake.client) });
act(() => {
fake.emit("checkpoint", { event: "reasoning_delta", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", turn_seq: 4, text: "Continue reasoning" });
fake.emit("checkpoint", { event: "reasoning_end", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", turn_seq: 5 });
@@ -230,8 +231,8 @@ describe("useNanobotStream", () => {
content: "First last", reasoning: "Queued Continue reasoning", streamId: "answer", reasoningStreamId: "answer",
projectionStartSeq: 1, turnSeq: 7,
});
expect(checkpoint?.projection.messages[0].content).toBe("First ");
expect(checkpoint?.projection.messages[0].reasoning).toBe("Queued ");
expect(snapshot.projection.messages[0].content).toBe("First ");
expect(snapshot.projection.messages[0].reasoning).toBe("Queued ");
});
it.each([true, false])("keeps answer segments across a provider-tool boundary (snapshot: %s)", (snapshot) => {
@@ -256,41 +257,50 @@ describe("useNanobotStream", () => {
it("captures the first sequence of coalesced chunks before a paint", () => {
const fake = fakeClient();
const { result } = renderHook(() => useNanobotStream("checkpoint", EMPTY_MESSAGES), { wrapper: wrap(fake.client) });
const cache = new ThreadMessageCache(() => false);
const first = renderHook(() => useNanobotStream("checkpoint", EMPTY_MESSAGES, false,
undefined, undefined, [], { key: null, cache }), { wrapper: wrap(fake.client) });
act(() => {
fake.emit("checkpoint", { event: "delta", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", turn_seq: 1, text: "First " });
fake.emit("checkpoint", { event: "delta", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", turn_seq: 2, text: "last" });
});
let checkpoint!: ThreadCheckpoint;
act(() => { checkpoint = result.current.getCheckpoint(); });
expect(checkpoint.projection.messages).toHaveLength(1);
expect(checkpoint.projection.messages[0]).toMatchObject({
expect(first.result.current.messages).toHaveLength(0);
first.unmount();
const snapshot = cache.get("checkpoint")!;
expect(snapshot.projection.messages).toHaveLength(1);
expect(snapshot.projection.messages[0]).toMatchObject({
content: "First last", streamId: "answer", projectionStartSeq: 1, turnSeq: 2,
});
const restored = renderHook(() => useNanobotStream("checkpoint", EMPTY_MESSAGES, false,
undefined, undefined, [], { key: null, cache }), { wrapper: wrap(fake.client) });
act(() => {
fake.emit("checkpoint", { event: "stream_end", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", turn_seq: 3, response_sources: [] });
});
expect(result.current.messages[0]).toMatchObject({ projectionStartSeq: 1, turnSeq: 3 });
expect(checkpoint.projection.closedAssistantIds.size).toBe(0);
expect(checkpoint.projection.closedAnswerStreamIds.size).toBe(0);
const closed = result.current.getCheckpoint().projection;
expect(restored.result.current.messages[0]).toMatchObject({ projectionStartSeq: 1, turnSeq: 3 });
expect(snapshot.projection.closedAssistantIds.size).toBe(0);
expect(snapshot.projection.closedAnswerStreamIds.size).toBe(0);
restored.unmount();
const closed = cache.get("checkpoint")!.projection;
expect(closed.closedAssistantIds.size).toBe(1);
expect(closed.closedAnswerStreamIds.size).toBe(1);
});
it("retains side-channel ownership when restoring an active answer checkpoint", async () => {
it("retains side-channel ownership when restoring an active answer from cache", async () => {
const fake = fakeClient();
const first = renderHook(() => useNanobotStream("checkpoint", EMPTY_MESSAGES), { wrapper: wrap(fake.client) });
const cache = new ThreadMessageCache(() => false);
const first = renderHook(() => useNanobotStream("checkpoint", EMPTY_MESSAGES, false,
undefined, undefined, [], { key: null, cache }), { wrapper: wrap(fake.client) });
act(() => {
fake.emit("checkpoint", { event: "delta", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", text: "Active answer" });
});
await flushStreamFrame();
act(() => { first.result.current.send("/model", undefined, { sideChannel: true }); });
const commandTurn = fake.client.sendMessage.mock.calls.at(-1)![3]?.turnId;
const checkpoint = first.result.current.getCheckpoint();
first.unmount();
const restored = renderHook(() => useNanobotStream("checkpoint", checkpoint.projection.messages, false,
undefined, undefined, [], checkpoint), { wrapper: wrap(fake.client) });
const snapshot = cache.get("checkpoint")!;
expect(snapshot.sideChannelTurnIds.has(commandTurn)).toBe(true);
const restored = renderHook(() => useNanobotStream("checkpoint", EMPTY_MESSAGES, false,
undefined, undefined, [], { key: null, cache }), { wrapper: wrap(fake.client) });
act(() => {
fake.emit("checkpoint", { event: "message", chat_id: "checkpoint", turn_id: commandTurn, text: "Current model" });
fake.emit("checkpoint", { event: "delta", chat_id: "checkpoint", turn_id: "turn", stream_id: "answer", text: " continued" });
@@ -298,6 +308,9 @@ describe("useNanobotStream", () => {
});
expect(restored.result.current.messages.filter((message) => message.role === "assistant").map((message) => message.content))
.toEqual(["Active answer continued", "Current model"]);
expect(snapshot.sideChannelTurnIds.has(commandTurn)).toBe(true);
restored.unmount();
expect(cache.get("checkpoint")!.sideChannelTurnIds.has(commandTurn)).toBe(false);
});
it("keeps invocation snapshots across streaming, recovery segments, completion and reload", async () => {