The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(ui-tui): flush the buffered streaming tail at message.complete
briandevans051d888e554c
diff --git a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts b/ui-tui/src/__tests__/createGatewayEventHandler.test.tsindex fa83c9687d1..a8afb08cbbe 100644--- a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts+++ b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts@@ -721,6 +721,57 @@ describe('createGatewayEventHandler', () => {expect(assistant?.text).toBe('First. second.')})+ // Narration → tool → tool-complete → more narration → message.complete with+ // its own `payload.text`. Nothing flushes the second narration block, so+ // before the fix message.complete cleared the buffer and the transcript lost+ // a block the user had already watched render.+ const streamTailTurn = (onEvent: ReturnType<typeof createGatewayEventHandler>) => {+ onEvent({ payload: {}, type: 'message.start' } as any)+ onEvent({ payload: { text: 'Checking the config first.' }, type: 'message.delta' } as any)+ onEvent({ payload: { context: 'config.yaml', name: 'read_file', tool_id: 'tool-1' }, type: 'tool.start' } as any)+ onEvent({ payload: { name: 'read_file', summary: 'read', tool_id: 'tool-1' }, type: 'tool.complete' } as any)+ onEvent({ payload: { text: 'The provider block looks wrong.' }, type: 'message.delta' } as any)+ }++ it('keeps streaming text buffered after a tool call, in order, at message.complete (#61520)', () => {+ const appended: Msg[] = []+ const onEvent = createGatewayEventHandler(buildCtx(appended))++ streamTailTurn(onEvent)+ onEvent({ payload: { text: 'Final answer.' }, type: 'message.complete' } as any)++ expect(appended.filter(msg => msg.role === 'assistant').map(msg => msg.text)).toEqual([+ 'Checking the config first.',+ 'The provider block looks wrong.',+ 'Final answer.'+ ])++ // The tail is flushed through the normal segment path, so the pending tool+ // shelf lands on it exactly once instead of being duplicated or dropped.+ const toolRows = appended.flatMap(msg => msg.tools ?? [])+ expect(toolRows).toHaveLength(1)+ expect(toolRows[0]).toContain('Read File')+ })++ it('does not duplicate the buffered tail when message.complete text already contains it (#61520)', () => {+ const appended: Msg[] = []+ const onEvent = createGatewayEventHandler(buildCtx(appended))++ streamTailTurn(onEvent)+ // Gateway replays the streamed tail as part of its authoritative final text.+ onEvent({+ payload: { text: 'The provider block looks wrong.\n\nFinal answer.' },+ type: 'message.complete'+ } as any)++ expect(appended.filter(msg => msg.role === 'assistant').map(msg => msg.text)).toEqual([+ 'Checking the config first.',+ 'The provider block looks wrong.',+ 'Final answer.'+ ])+ expect(appended.filter(msg => msg.text.includes('The provider block looks wrong.'))).toHaveLength(1)+ })+it('anchors inline_diff as its own segment where the edit happened', () => {const appended: Msg[] = []const onEvent = createGatewayEventHandler(buildCtx(appended))diff --git a/ui-tui/src/app/turnController.ts b/ui-tui/src/app/turnController.tsindex e91462e6b86..33e3471eb81 100644--- a/ui-tui/src/app/turnController.ts+++ b/ui-tui/src/app/turnController.ts@@ -604,7 +604,27 @@ class TurnController {// only when the gateway elected not to send any (#16391).// `text` is `str | JsonValue` on the wire (structured parts stay possible); only a string renders here.const wireText = typeof payload.text === 'string' ? payload.text : undefined- const rawText = (wireText ?? payload.rendered ?? this.bufRef).trimStart()+ const completionText = wireText ?? payload.rendered+ const rawText = (completionText ?? this.bufRef).trimStart()++ // When the gateway sent its own final text, whatever is still sitting in+ // `this.bufRef` is text the user watched stream AFTER the last segment+ // flush — typically narration between a tool call and the end of the turn.+ // `recordMessageComplete` was the only turn-end site that never flushed it,+ // so `idle()` below wiped it and the transcript lost a block the user had+ // already seen (#61520).+ //+ // Flush it as a segment rather than appending it to `finalMessages`: the+ // segment spread below already sits ahead of the final assistant text, so+ // stream order is preserved structurally, and `finalTail()` then strips the+ // tail out of `finalText` when the completion payload already contains it.+ // Skipped when `completionText` is absent, because then `rawText` IS the+ // buffer (the #16391 fallback) and flushing would publish the final answer+ // as a segment instead of the final message.+ if (completionText !== undefined && this.bufRef.trim()) {+ this.flushStreamingSegment()+ }+const split = splitReasoning(rawText)// Only dedupe segments AFTER the interim boundary — interim-sealed// segments are preserved even if the final text includes them.