π OpenHands: AI-Driven Development
fix(chat): de-duplicate streamed intermediate agent messages (#1552)
Vasco Schiavo8a3209682910
diff --git a/__tests__/components/conversation-events/chat/duplicate-streamed-message.test.tsx b/__tests__/components/conversation-events/chat/duplicate-streamed-message.test.tsxnew file mode 100644index 0000000000..b6ae6f522a--- /dev/null+++ b/__tests__/components/conversation-events/chat/duplicate-streamed-message.test.tsx@@ -0,0 +1,179 @@+import { describe, expect, it } from "vitest";+import { fireEvent, screen } from "@testing-library/react";+import { renderWithProviders } from "test-utils";+import {+ ActionEvent,+ MessageEvent,+ ObservationEvent,+ OpenHandsEvent,+ SecurityRisk,+} from "#/types/agent-server/core";+import { StreamingDeltaEvent } from "#/types/agent-server/core/events/streaming-delta-event";+import { handleEventForUI } from "#/utils/handle-event-for-ui";+import { Messages } from "#/components/conversation-events/chat/messages";++// Regression for issue #1534. With stream=true the agent streams its+// pre-tool-call text as a StreamingDeltaEvent and then emits an intermediate+// ActionEvent whose `thought` is that same text. The chat hoists the action's+// thought into its own message, so the leftover streaming delta used to render+// the identical text a second time. This test drives the real reducer+// (handleEventForUI) and renders the real <Messages> tree to assert the text+// shows exactly once.++const THOUGHT =+ "I'll create a webpage explaining what OpenHands can do. " ++ "Let me first gather accurate information from the official documentation.";++const countOccurrences = (haystack: string, needle: string): number =>+ haystack.split(needle).length - 1;++const userMessage: MessageEvent = {+ id: "user-1",+ timestamp: "2026-06-12T12:00:00Z",+ source: "user",+ llm_message: {+ role: "user",+ content: [{ type: "text", text: "Create a basic webpage." }],+ },+ activated_microagents: [],+ extended_content: [],+};++// The reasoning streamed as a delta; many models carry reasoning ONLY here, so+// the collapsible "Thinking" must survive the reconciliation.+const streamingDelta: StreamingDeltaEvent = {+ id: "delta-1",+ kind: "StreamingDeltaEvent",+ timestamp: "2026-06-12T12:00:01Z",+ source: "agent",+ content: THOUGHT,+ reasoning_content: "Considering the request before acting.",+};++const action: ActionEvent = {+ id: "action-1",+ timestamp: "2026-06-12T12:00:02Z",+ source: "agent",+ thought: [{ type: "text", text: THOUGHT }],+ thinking_blocks: [],+ action: {+ kind: "ExecuteBashAction",+ command: "ls",+ is_input: false,+ timeout: null,+ reset: false,+ },+ tool_name: "execute_bash",+ tool_call_id: "call_1",+ tool_call: {+ id: "call_1",+ type: "function",+ function: { name: "execute_bash", arguments: '{"command":"ls"}' },+ },+ llm_response_id: "resp-1",+ security_risk: SecurityRisk.UNKNOWN,+};++const observation: ObservationEvent = {+ id: "obs-1",+ timestamp: "2026-06-12T12:00:03Z",+ source: "environment",+ tool_name: "execute_bash",+ tool_call_id: "call_1",+ observation: {+ kind: "ExecuteBashObservation",+ content: [{ type: "text", text: "file.txt\n" }],+ command: "ls",+ exit_code: 0,+ error: false,+ timeout: false,+ metadata: {+ exit_code: 0,+ pid: 1,+ username: "u",+ hostname: "h",+ working_dir: "/",+ py_interpreter_path: null,+ prefix: "",+ suffix: "",+ },+ },+ action_id: "action-1",+};++const reduce = (events: OpenHandsEvent[]): OpenHandsEvent[] =>+ events.reduce<OpenHandsEvent[]>((ui, ev) => handleEventForUI(ev, ui), []);++describe("issue #1534 β streamed intermediate message duplication", () => {+ it("renders the intermediate thought text exactly once", () => {+ const allEvents = [userMessage, streamingDelta, action, observation];+ const uiEvents = reduce(allEvents);++ const { container } = renderWithProviders(+ <Messages messages={uiEvents} allEvents={allEvents} />,+ );++ expect(countOccurrences(container.textContent ?? "", THOUGHT)).toBe(1);+ });++ it("keeps the streamed reasoning visible (Thinking section survives)", () => {+ const allEvents = [userMessage, streamingDelta, action, observation];+ const uiEvents = reduce(allEvents);++ renderWithProviders(<Messages messages={uiEvents} allEvents={allEvents} />);++ // The collapsible "Thinking" section is still rendered (collapsed by+ // default); expanding it reveals the reasoning that was streamed on the+ // delta, proving the reconciliation kept the reasoning.+ fireEvent.click(screen.getByTestId("collapsible-thinking-toggle"));+ expect(+ screen.getByTestId("collapsible-thinking-content").textContent ?? "",+ ).toContain("Considering the request before acting.");+ });++ it("renders a single Thinking section when the action carries its own reasoning", () => {+ // When the finalized action also reports reasoning, the streamed delta's+ // reasoning would duplicate the action's "Thinking" β so the delta is+ // dropped and only one Thinking section renders.+ const reasoningAction: ActionEvent = {+ ...action,+ reasoning_content: "Considering the request before acting.",+ };+ const allEvents = [+ userMessage,+ streamingDelta,+ reasoningAction,+ observation,+ ];+ const uiEvents = reduce(allEvents);++ const { container } = renderWithProviders(+ <Messages messages={uiEvents} allEvents={allEvents} />,+ );++ expect(screen.getAllByTestId("collapsible-thinking")).toHaveLength(1);+ expect(countOccurrences(container.textContent ?? "", THOUGHT)).toBe(1);+ });++ it("still renders a final agent message exactly once (unchanged)", () => {+ const finalMessage: MessageEvent = {+ id: "agent-msg-1",+ timestamp: "2026-06-12T12:00:02Z",+ source: "agent",+ llm_message: {+ role: "assistant",+ content: [{ type: "text", text: THOUGHT }],+ },+ activated_microagents: [],+ extended_content: [],+ };+ const allEvents = [userMessage, streamingDelta, finalMessage];+ const uiEvents = reduce(allEvents);++ const { container } = renderWithProviders(+ <Messages messages={uiEvents} allEvents={allEvents} />,+ );++ expect(countOccurrences(container.textContent ?? "", THOUGHT)).toBe(1);+ });+});diff --git a/__tests__/utils/handle-event-for-ui.test.ts b/__tests__/utils/handle-event-for-ui.test.tsindex 26289de16a..30948afa0d 100644--- a/__tests__/utils/handle-event-for-ui.test.ts+++ b/__tests__/utils/handle-event-for-ui.test.ts@@ -530,4 +530,186 @@ describe("handleEventForUI", () => {expect(result).toEqual([mockMessageEvent]);expect(result).not.toBe(initialUiEvents);});++ // Regression for issue #1534: with stream=true an agent step streams its+ // pre-tool-call text as a StreamingDeltaEvent, then the step arrives as an+ // intermediate ActionEvent whose `thought` is that same text. The chat hoists+ // the action's thought into its own message, so the leftover streaming delta+ // must not also render the text or it appears twice.+ describe("intermediate action / streamed thought reconciliation (#1534)", () => {+ const makeThoughtAction = (id: string, thought: string): ActionEvent => ({+ ...mockActionEvent,+ id,+ tool_call_id: `call_${id}`,+ thought: [{ type: "text", text: thought }],+ });++ it("clears the streamed content from the delta, keeping its reasoning", () => {+ const thought = "Let me gather accurate information.";+ const delta: StreamingDeltaEvent = {+ ...makeStreamingDelta("delta-1", thought),+ reasoning_content: "pondering the request",+ };+ const action = makeThoughtAction("intermediate-1", thought);++ const result = handleEventForUI(action, [mockMessageEvent, delta]);++ expect(result).toEqual([+ mockMessageEvent,+ { ...delta, content: null },+ action,+ ]);+ });++ it("drops the delta entirely when it has no reasoning left to render", () => {+ const thought = "Let me gather accurate information.";+ const delta = makeStreamingDelta("delta-1", thought);+ const action = makeThoughtAction("intermediate-1", thought);++ const result = handleEventForUI(action, [mockMessageEvent, delta]);++ expect(result).toEqual([mockMessageEvent, action]);+ });++ it("reconciles across multiple streamed segments in order", () => {+ const first = makeStreamingDelta("delta-1", "Let me gather ");+ const merged = handleEventForUI(+ makeStreamingDelta("delta-2", "accurate information."),+ handleEventForUI(first, [mockMessageEvent]),+ );+ const action = makeThoughtAction(+ "intermediate-1",+ "Let me gather accurate information.",+ );++ const result = handleEventForUI(action, merged);++ // The merged content delta is fully streamed and has no reasoning, so it+ // is dropped and only the action remains.+ expect(result).toEqual([mockMessageEvent, action]);+ });++ it("leaves the delta untouched when streamed text does not match the thought", () => {+ const delta = makeStreamingDelta(+ "delta-1",+ "some unrelated streamed text",+ );+ const action = makeThoughtAction(+ "intermediate-1",+ "An unrelated thought.",+ );++ const result = handleEventForUI(action, [mockMessageEvent, delta]);++ expect(result).toEqual([mockMessageEvent, delta, action]);+ });++ it("does not reconcile a ThinkAction (its thought renders separately)", () => {+ const thought = "A reasoning step.";+ const delta = makeStreamingDelta("delta-1", thought);+ const thinkAction: ActionEvent = {+ ...mockActionEvent,+ id: "think-1",+ tool_name: "think",+ tool_call_id: "call_think_1",+ thought: [{ type: "text", text: thought }],+ action: { kind: "ThinkAction", thought },+ };++ const result = handleEventForUI(thinkAction, [mockMessageEvent, delta]);++ expect(result).toEqual([mockMessageEvent, delta, thinkAction]);+ });++ it("only reconciles the current turn's delta (after the last user message)", () => {+ const thought = "Let me gather accurate information.";+ const earlierTurnDelta = makeStreamingDelta("delta-old", thought);+ const laterUserMessage: MessageEvent = {+ ...mockMessageEvent,+ id: "user-2",+ };+ const action = makeThoughtAction("intermediate-1", thought);++ const result = handleEventForUI(action, [+ mockMessageEvent,+ earlierTurnDelta,+ laterUserMessage,+ ]);++ // The delta belongs to a turn before the latest user message, so it is+ // left intact and the action is appended normally.+ expect(result).toEqual([+ mockMessageEvent,+ earlierTurnDelta,+ laterUserMessage,+ action,+ ]);+ });++ it("reconciles even when the thought is whitespace-trimmed vs the stream", () => {+ // The SDK strips assistant text, so the action's thought can lack the+ // trailing newline the model streamed before the tool call.+ const delta = makeStreamingDelta(+ "delta-1",+ "Let me gather information.\n",+ );+ const action = makeThoughtAction(+ "intermediate-1",+ "Let me gather information.",+ );++ const result = handleEventForUI(action, [mockMessageEvent, delta]);++ // Match succeeds despite the trailing newline, so the delta is dropped.+ expect(result).toEqual([mockMessageEvent, action]);+ });++ it("only folds in the current step's trailing delta, not an earlier step's", () => {+ // An earlier step's delta still carrying content sits before this step's+ // observation; it must NOT be joined into the match.+ const earlierDelta = makeStreamingDelta(+ "delta-early",+ "Earlier step text.",+ );+ const currentDelta = makeStreamingDelta(+ "delta-current",+ "Current step text.",+ );+ const action = makeThoughtAction("intermediate-2", "Current step text.");++ const result = handleEventForUI(action, [+ mockMessageEvent,+ earlierDelta,+ mockObservationEvent,+ currentDelta,+ ]);++ // Only the trailing (current) delta is reconciled and dropped; the+ // earlier delta is untouched.+ expect(result).toEqual([+ mockMessageEvent,+ earlierDelta,+ mockObservationEvent,+ action,+ ]);+ });++ it("drops the delta instead of keeping its reasoning when the action carries reasoning", () => {+ const thought = "Let me gather accurate information.";+ const delta: StreamingDeltaEvent = {+ ...makeStreamingDelta("delta-1", thought),+ reasoning_content: "streamed reasoning",+ };+ const action: ActionEvent = {+ ...makeThoughtAction("intermediate-1", thought),+ reasoning_content: "finalized reasoning",+ };++ const result = handleEventForUI(action, [mockMessageEvent, delta]);++ // The action renders the Thinking section itself, so the delta is fully+ // redundant and dropped β no leftover reasoning-only delta.+ expect(result).toEqual([mockMessageEvent, action]);+ });+ });});diff --git a/src/utils/handle-event-for-ui.ts b/src/utils/handle-event-for-ui.tsindex 9d16bb0f15..19af184074 100644--- a/src/utils/handle-event-for-ui.ts+++ b/src/utils/handle-event-for-ui.ts@@ -1,4 +1,9 @@-import { MessageEvent, OpenHandsEvent } from "#/types/agent-server/core";+import {+ ActionEvent,+ ImageContent,+ OpenHandsEvent,+ TextContent,+} from "#/types/agent-server/core";import {isACPToolCallEvent,isActionEvent,@@ -7,6 +12,7 @@ import {isStreamingDeltaEvent,} from "#/types/agent-server/type-guards";import { StreamingDeltaEvent } from "#/types/agent-server/core/events/streaming-delta-event";+import { getReasoningContent } from "#/components/conversation-events/chat/event-thought-helpers";export const mergeStreamingDeltaEvent = (incoming: StreamingDeltaEvent,@@ -40,11 +46,11 @@ const findLastUserMessageIndex = (events: OpenHandsEvent[]): number => {// Join text blocks WITHOUT a separator: streaming deltas concatenate content// tokens directly with no separator between LLM content blocks, so using "\n"// here would cause startsWith/findTextSegmentsInOrder to miss when reconciling-// a multi-block MessageEvent against the already-rendered streaming delta.-const getAgentMessageText = (event: MessageEvent): string =>- event.llm_message.content- .filter((content) => content.type === "text")- .map((content) => content.text)+// a multi-block message/thought against the already-rendered streaming delta.+const joinTextBlocks = (blocks: (TextContent | ImageContent)[]): string =>+ blocks+ .filter((block) => block.type === "text")+ .map((block) => block.text).join("");const getFinalAgentText = (event: OpenHandsEvent): string | null => {@@ -53,7 +59,7 @@ const getFinalAgentText = (event: OpenHandsEvent): string | null => {}if (isMessageEvent(event) && event.source === "agent") {- return getAgentMessageText(event);+ return joinTextBlocks(event.llm_message.content);}return null;@@ -78,58 +84,93 @@ const findTextSegmentsInOrder = (return { matched: true, lastMatchEnd };};-const finalizeStreamingDeltasInPlace = (- finalEvent: OpenHandsEvent,+// Content-bearing streaming deltas of the current turn (after the last user+// message). Reasoning-only deltas are excluded: reasoning renders in its own+// collapsed bubble and never overlaps the message text being reconciled.+const getCurrentTurnContentDeltas = (uiEvents: OpenHandsEvent[],-): OpenHandsEvent[] | null => {+): { event: StreamingDeltaEvent; index: number }[] => {const lastUserMessageIndex = findLastUserMessageIndex(uiEvents);- const currentTurnStreamingDeltaIndexes = uiEvents- .map((uiEvent, index) => ({ uiEvent, index }))+ return uiEvents+ .map((event, index) => ({ event, index })).filter(- ({ uiEvent, index }) =>- index > lastUserMessageIndex && isStreamingDeltaEvent(uiEvent),- )- .map(({ index }) => index);+ (item): item is { event: StreamingDeltaEvent; index: number } =>+ item.index > lastUserMessageIndex &&+ isStreamingDeltaEvent(item.event) &&+ (item.event.content?.length ?? 0) > 0,+ );+};++// The current step's streaming delta(s): the trailing run at the end of+// `uiEvents`. Earlier steps' deltas are separated by their observations, so+// this never folds an earlier step's delta into the current one.+const getTrailingContentDeltas = (+ uiEvents: OpenHandsEvent[],+): { event: StreamingDeltaEvent; index: number }[] => {+ const deltas: { event: StreamingDeltaEvent; index: number }[] = [];+ for (let index = uiEvents.length - 1; index >= 0; index -= 1) {+ const event = uiEvents[index];+ if (!isStreamingDeltaEvent(event)) {+ break;+ }+ if ((event.content?.length ?? 0) > 0) {+ deltas.unshift({ event, index });+ }+ }+ return deltas;+};++// Whether the streamed `segments` (in order) reconcile against `targetText`.+// `lastMatchEnd` is the offset past the matched text, so callers can recover+// any not-yet-streamed suffix. The SDK strips the finalized text, so it may+// lack trailing whitespace the model streamed; the match tolerates that by+// also trying the trailing-trimmed streamed text.+const matchStreamedSegments = (+ targetText: string,+ segments: string[],+): { matched: boolean; lastMatchEnd: number } => {+ const streamedText = segments.join("");+ for (const candidate of [streamedText, streamedText.trimEnd()]) {+ if (candidate && targetText.startsWith(candidate)) {+ return { matched: true, lastMatchEnd: candidate.length };+ }+ }+ // Segments may be interleaved with not-yet-streamed text; locate them in+ // order, trimming the last segment's trailing whitespace.+ const lastIndex = segments.length - 1;+ const searchSegments = segments.map((segment, index) =>+ index === lastIndex ? segment.trimEnd() : segment,+ );+ return findTextSegmentsInOrder(targetText, searchSegments);+};- if (currentTurnStreamingDeltaIndexes.length === 0) {+const finalizeStreamingDeltasInPlace = (+ finalEvent: OpenHandsEvent,+ uiEvents: OpenHandsEvent[],+): OpenHandsEvent[] | null => {+ const contentStreamingDeltas = getCurrentTurnContentDeltas(uiEvents);+ if (contentStreamingDeltas.length === 0) {return null;}const finalText = getFinalAgentText(finalEvent);- // Only the regular `content` field participates in reconciliation.- // Reasoning-only deltas (those that carry only `reasoning_content`) produce- // an empty streamingSegments list, causing the function to return null so- // the finalEvent is appended normally. This is intentional: reasoning- // content renders in its own collapsed bubble and never overlaps with the- // assistant's regular message text in `FinishAction.message`.- const contentStreamingDeltas = currentTurnStreamingDeltaIndexes- .map((index) => ({ event: uiEvents[index], index }))- .filter(- (item): item is { event: StreamingDeltaEvent; index: number } =>- isStreamingDeltaEvent(item.event) &&- (item.event.content?.length ?? 0) > 0,- );+ if (!finalText) {+ return null;+ }+const streamingSegments = contentStreamingDeltas.map(({ event }) => event.content ?? "",);-- if (!finalText || streamingSegments.length === 0) {+ const { matched, lastMatchEnd } = matchStreamedSegments(+ finalText,+ streamingSegments,+ );+ if (!matched) {return null;}const nextUiEvents = [...uiEvents];- const streamedText = streamingSegments.join("");- let unstreamedSuffix = "";-- if (finalText.startsWith(streamedText)) {- unstreamedSuffix = finalText.slice(streamedText.length);- } else {- const match = findTextSegmentsInOrder(finalText, streamingSegments);- if (!match.matched) {- return null;- }- unstreamedSuffix = finalText.slice(match.lastMatchEnd);- }+ const unstreamedSuffix = finalText.slice(lastMatchEnd);const lastDeltaIndex = contentStreamingDeltas.at(-1)?.index;const lastDelta =@@ -154,6 +195,63 @@ const finalizeStreamingDeltasInPlace = (return nextUiEvents;};+/**+ * Reconcile the current turn's streaming delta when an intermediate+ * (tool-calling) `ActionEvent` arrives. With `stream=true` the step's+ * pre-tool-call text is streamed as delta `content`, then the action's+ * `thought` repeats it and the chat hoists that into its own message (see+ * `group-events.ts`), so the text would render twice (issue #1534).+ *+ * Unlike `finalizeStreamingDeltasInPlace` (which drops the final event and+ * keeps the delta), the action must stay β it owns the tool call. So the+ * streamed text is cleared from the delta instead, and the delta is kept only+ * to carry reasoning the action itself lacks (for many models the delta is the+ * sole reasoning carrier), otherwise dropped.+ *+ * Only the current step's trailing delta run is considered. Returns the updated+ * array, or `null` when there is nothing to reconcile.+ */+const supersedeStreamedThoughtWithAction = (+ action: ActionEvent,+ uiEvents: OpenHandsEvent[],+): OpenHandsEvent[] | null => {+ const thoughtText = joinTextBlocks(action.thought);+ if (!thoughtText) {+ return null;+ }++ const contentDeltas = getTrailingContentDeltas(uiEvents);+ if (contentDeltas.length === 0) {+ return null;+ }++ const streamingSegments = contentDeltas.map(+ ({ event }) => event.content ?? "",+ );++ // Only strip when the streamed text is the action's rendered thought.+ if (!matchStreamedSegments(thoughtText, streamingSegments).matched) {+ return null;+ }++ // Keeping the delta's reasoning would duplicate the action's own "Thinking".+ const actionRendersReasoning = getReasoningContent(action).trim().length > 0;+ const indexesToStrip = new Set(contentDeltas.map(({ index }) => index));+ const nextUiEvents: OpenHandsEvent[] = [];+ uiEvents.forEach((event, index) => {+ if (!indexesToStrip.has(index) || !isStreamingDeltaEvent(event)) {+ nextUiEvents.push(event);+ return;+ }+ // Keep the delta only to render reasoning the action itself lacks.+ if (!actionRendersReasoning && event.reasoning_content) {+ nextUiEvents.push({ ...event, content: null });+ }+ });++ return nextUiEvents;+};+/*** Handles adding an event to the UI events array* Replaces actions with observations when they arrive (so UI shows observation instead of action)@@ -206,6 +304,24 @@ export const handleEventForUI = (}}+ // Intermediate tool-calling action whose thought was streamed: clear the+ // duplicated text from the delta (issue #1534). ThinkAction is excluded β its+ // thought renders through its own collapsible, not a hoisted thought.+ if (+ isActionEvent(event) &&+ event.action.kind !== "FinishAction" &&+ event.action.kind !== "ThinkAction"+ ) {+ const reconciledUiEvents = supersedeStreamedThoughtWithAction(+ event,+ newUiEvents,+ );+ if (reconciledUiEvents) {+ reconciledUiEvents.push(event);+ return reconciledUiEvents;+ }+ }+if (isACPToolCallEvent(event)) {const existingIndex = newUiEvents.findIndex((uiEvent) =>