fix(webui): avoid phantom reasoning on empty streams

This commit is contained in:
chengyongru authored and chengyongru committed 2026-11-12 17:34:07 +08:00
1 parent c3403e6a6f
commit b49218b6a3
8 files changed
+299 -50

No files matched your search

@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import { createThreadProjectionState, projectThreadEvent, projectThreadEvents } from "@/lib/thread-event-projection";
import { normalizeActivityTimeline } from "@/lib/activity-timeline";
import { hasPendingAgentActivity, isReasoningOnlyAssistant, normalizeActivityTimeline } from "@/lib/activity-timeline";
import { ThreadProjectionIndex } from "@/lib/thread-projection-index";
import type { ThreadProjectionEvent, UIMessage } from "@/lib/types";
import projectionFixture from "./fixtures/live-replay-event-projection.json";
@@ -99,6 +99,33 @@ describe("canonical thread event projection", () => {
expect(state.messages[2]).toMatchObject({ projectionStartSeq: 4, turnSeq: 6, streamId: "second" });
});
it("keeps empty answer stream state without presenting it as reasoning", () => {
let state = createThreadProjectionState();
const emit = (event: ThreadProjectionEvent, persisted = false) => {
state = projectThreadEvent(state, event, { createId: () => `live-${state.messages.length}`, now: 100, persisted });
};
emit({ event: "user_message", chat_id: "chat", turn_id: "turn", turn_seq: 1, text: "Question", starts_turn: true }, true);
// Persisted stream snapshots contain text: "" even when the live frame does not.
emit({ event: "stream_end", chat_id: "chat", turn_id: "turn", turn_seq: 2, stream_id: "empty", text: "", resuming: true }, true);
const placeholder = state.messages[1];
expect(placeholder).toMatchObject({ content: "", isStreaming: true, streamId: "empty" });
expect(hasPendingAgentActivity(state.messages)).toBe(true);
expect(isReasoningOnlyAssistant(placeholder)).toBe(false);
expect(normalizeActivityTimeline(state.messages)).toMatchObject([
{ type: "message", message: { content: "Question" }, sourceMessageCount: 2 },
]);
emit({ event: "delta", chat_id: "chat", turn_id: "turn", turn_seq: 3, stream_id: "empty", text: "Stale" });
emit({ event: "message", chat_id: "chat", turn_id: "turn", turn_seq: 4, kind: "tool_hint", text: "read_file(first)" });
expect(hasPendingAgentActivity(state.messages)).toBe(true);
emit({ event: "reasoning_delta", chat_id: "chat", turn_id: "turn", turn_seq: 5, stream_id: "thought", text: "Real thought" });
emit({ event: "reasoning_end", chat_id: "chat", turn_id: "turn", turn_seq: 6, stream_id: "thought", text: "Real thought" });
emit({ event: "delta", chat_id: "chat", turn_id: "turn", turn_seq: 7, stream_id: "answer", text: "Answer" });
const units = normalizeActivityTimeline(state.messages);
expect(units.map((unit) => unit.type === "message" ? unit.message.content : unit.messages.map((row) => row.reasoning || row.content)))
.toEqual(["Question", ["read_file(first)", "Real thought"], "Answer"]);
});
it("can resume an earlier checkpoint after another branch closes the same stream", () => {
const index = new ThreadProjectionIndex();
const options = { createId: () => "answer", now: 100, index };
+81 -14
View File
@@ -523,7 +523,7 @@ describe("ThreadMessages", () => {
expect(document.querySelectorAll("[data-block-context-rail]")).toHaveLength(0);
});
it("ignores a completed empty answer frame without splitting contiguous activity", () => {
it.each([false, true])("ignores an empty answer frame without splitting contiguous activity (streaming=%s)", (isStreaming) => {
const turnId = "turn-empty-answer-frame";
const segmentId = "activity-1";
const messages: UIMessage[] = [
@@ -574,7 +574,7 @@ describe("ThreadMessages", () => {
id: "empty-answer-frame",
role: "assistant",
content: "",
isStreaming: false,
isStreaming,
turnId,
turnPhase: "answer",
turnSeq: 10,
@@ -637,12 +637,12 @@ describe("ThreadMessages", () => {
expect(screen.getByTestId("agent-activity-content")).toHaveTextContent("Completed Second");
});
it("keeps empty frame source counts on the nearest visible unit", () => {
it.each([false, true])("keeps empty frame source counts on the nearest visible unit (streaming=%s)", (isStreaming) => {
const emptyFrame: UIMessage = {
id: "empty",
role: "assistant",
content: "",
isStreaming: false,
isStreaming,
turnPhase: "answer",
createdAt: 2,
};
@@ -667,16 +667,72 @@ describe("ThreadMessages", () => {
sourceMessageCount: 2,
}]);
const streamingUnits = buildDisplayUnits([{
...emptyFrame,
id: "streaming-placeholder",
isStreaming: true,
}]);
expect(streamingUnits).toMatchObject([{
type: "activity",
messages: [{ id: "streaming-placeholder" }],
sourceMessageCount: 1,
}]);
const leadingFrameUnits = buildDisplayUnits([
{ ...emptyFrame, turnId: "original" },
{ id: "guidance", role: "user", content: "also check", turnId: "guidance", createdAt: 3 },
{ id: "answer", role: "assistant", content: "done", turnId: "guidance", createdAt: 4 },
]);
expect(leadingFrameUnits).toMatchObject([
{ type: "message", message: { id: "guidance" }, sourceMessageCount: 2 },
{ type: "message", message: { id: "answer" }, sourceMessageCount: 1 },
]);
const afterNoticeUnits = buildDisplayUnits([
{ id: "user", role: "user", content: "check", createdAt: 1 },
{ id: "compact", role: "assistant", kind: "compaction", content: "", compaction: { id: "compact", phase: "started" }, createdAt: 2 },
emptyFrame,
]);
expect(afterNoticeUnits).toMatchObject([
{ type: "message", message: { id: "user" }, sourceMessageCount: 1 },
{ type: "message", message: { id: "compact" }, sourceMessageCount: 2 },
]);
});
it("keeps generic turn progress while an empty answer frame has no reasoning", () => {
const messages: UIMessage[] = [
{ id: "user", role: "user", content: "check", turnId: "turn", createdAt: 1 },
{ id: "empty", role: "assistant", content: "", isStreaming: true, turnId: "turn", turnPhase: "answer", createdAt: 2 },
];
render(<ThreadMessages messages={messages} isStreaming activeTurnId="turn" />);
expect(screen.getByRole("status", { name: /^Working for / })).toBeInTheDocument();
expect(screen.queryByTestId("activity-reasoning-marker")).not.toBeInTheDocument();
});
it("keeps generic progress for an active turn without a user prompt", () => {
vi.useFakeTimers();
const now = 1_800_000_000_000;
vi.setSystemTime(now);
const messages: UIMessage[] = [
{ id: "old-tool", role: "tool", kind: "trace", content: "old_search()", turnId: "old-turn", createdAt: now - 11_000 },
{ id: "previous", role: "assistant", content: "Previous answer", turnId: "old-turn", createdAt: now - 10_000 },
{ id: "empty", role: "assistant", content: "", isStreaming: true, turnId: "automation", turnPhase: "answer", createdAt: now },
];
const { rerender } = render(
<ThreadMessages messages={messages} isStreaming activeTurnId="automation" runStartedAt={(now - 3_000) / 1000} />,
);
expect(screen.getByRole("status", { name: "Working for 3s" })).toBeInTheDocument();
expect(screen.queryByTestId("activity-reasoning-marker")).not.toBeInTheDocument();
rerender(<ThreadMessages messages={[
...messages,
{ id: "tool", role: "tool", kind: "trace", content: "web_search(query)", turnId: "automation", createdAt: now },
]} isStreaming activeTurnId="automation" runStartedAt={(now - 3_000) / 1000} />);
expect(screen.getByTestId("agent-activity-content")).toBeInTheDocument();
expect(screen.getByRole("button", { name: /^Working for 3s/ })).toBeInTheDocument();
expect(screen.queryByTestId("activity-reasoning-marker")).not.toBeInTheDocument();
});
it("keeps generic progress when the only activity is a hidden subagent create", () => {
const messages: UIMessage[] = [
{ id: "user", role: "user", content: "check", turnId: "turn", createdAt: 1 },
{ id: "empty", role: "assistant", content: "", isStreaming: true, turnId: "turn", createdAt: 2 },
{ id: "create", role: "tool", kind: "trace", content: 'subagent({"action":"create"})',
traces: ['subagent({"action":"create"})'], turnId: "turn", createdAt: 3,
toolEvents: [{ call_id: "call-1", name: "subagent", phase: "end", arguments: { action: "create" } }] },
];
render(<ThreadMessages messages={messages} isStreaming activeTurnId="turn" />);
expect(screen.getByRole("status", { name: /^Working for / })).toBeInTheDocument();
expect(screen.queryByTestId("activity-reasoning-marker")).not.toBeInTheDocument();
});
it("offers a follow-up action for text selected within one completed answer", async () => {
@@ -1180,6 +1236,17 @@ describe("ThreadMessages", () => {
expect(dividerIndex).toBe(branchPromptIndex - 1);
});
it("keeps a fork boundary before a prompt when the visible window starts with an empty frame", () => {
const messages: UIMessage[] = [
{ id: "empty", role: "assistant", content: "", isStreaming: true, turnId: "original", createdAt: 1 },
{ id: "branch", role: "user", content: "branch prompt", turnId: "branch", createdAt: 2 },
{ id: "answer", role: "assistant", content: "branch answer", turnId: "branch", createdAt: 3 },
];
render(<ThreadMessages messages={messages} forkBoundaryMessageCount={1} />);
const divider = screen.getByText("Forked from history");
expect(divider.compareDocumentPosition(screen.getByText("branch prompt")) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
});
it("hides redundant task creation while retaining failed creates and other tool activity", async () => {
const messages = projectThreadEvents([
{ event: "user_message", chat_id: "a", starts_turn: true, turn_id: "turn", text: "Inspect" },
+19
View File
@@ -2494,6 +2494,25 @@ describe("ThreadViewport", () => {
expect(visible).toHaveLength(INITIAL_HISTORY_WINDOW + 2);
});
it.each([false, true])("keeps an activity cluster together across an empty answer frame (streaming=%s)", (isStreaming) => {
const activity: UIMessage[] = [
{ id: "prompt", role: "user", content: "question", createdAt: 1 },
{ id: "first", role: "tool", kind: "trace", content: "first()", createdAt: 2 },
{ id: "empty", role: "assistant", content: "", turnPhase: "answer", isStreaming, createdAt: 3 },
{ id: "second", role: "tool", kind: "trace", content: "second()", createdAt: 4 },
];
expect(windowMessages(activity, 1).map((message) => message.id)).toEqual(["first", "empty", "second"]);
});
it("keeps compaction notices as activity window boundaries", () => {
const activity: UIMessage[] = [
{ id: "first", role: "tool", kind: "trace", content: "first()", createdAt: 1 },
{ id: "compact", role: "assistant", kind: "compaction", content: "", compaction: { id: "compact", phase: "started" }, createdAt: 2 },
{ id: "second", role: "tool", kind: "trace", content: "second()", createdAt: 3 },
];
expect(windowMessages(activity, 1).map((message) => message.id)).toEqual(["second"]);
});
it("resets to the bottom when opening a different conversation", async () => {
const scrollTo = vi.fn();
const { container, rerender } = render(