Skip to content

The agent that grows with you

fix(ui-tui): only flush the streamed tail when message.complete text lacks it

kshitijk4poor0eecdb02ec0c
diff --git a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
index a8afb08cbbe..768123b5c2a 100644
--- a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
+++ b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
@@ -753,23 +753,28 @@ describe('createGatewayEventHandler', () => {
expect(toolRows[0]).toContain('Read File')
})
- it('does not duplicate the buffered tail when message.complete text already contains it (#61520)', () => {
+ it.each([
+ ['final text equals the streamed tail', { text: 'Answer.' }],
+ ['no final text (#16391 buffer fallback)', {}]
+ ])('keeps the tool shelf above the answer when %s (#61520)', (_label, payload) => {
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)
+ onEvent({ payload: {}, type: 'message.start' } as any)
+ onEvent({ payload: { text: 'Pre.' }, 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: 'Answer.' }, type: 'message.delta' } as any)
+ onEvent({ payload, 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)
+ const answerIdx = appended.findIndex(msg => msg.text === 'Answer.')
+ const toolIdx = appended.findIndex(msg => (msg.tools ?? []).length > 0)
+
+ expect(appended.filter(msg => msg.text === 'Answer.')).toHaveLength(1)
+ expect(appended[answerIdx]?.tools ?? []).toHaveLength(0)
+ expect(toolIdx).toBeGreaterThan(-1)
+ expect(toolIdx).toBeLessThan(answerIdx)
+ expect(answerIdx).toBe(appended.length - 1)
})
it('anchors inline_diff as its own segment where the edit happened', () => {
diff --git a/ui-tui/src/app/turnController.ts b/ui-tui/src/app/turnController.ts
index 99366708ae2..752e52460f6 100644
--- a/ui-tui/src/app/turnController.ts
+++ b/ui-tui/src/app/turnController.ts
@@ -610,21 +610,14 @@ class TurnController {
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()) {
+ // Text still in `this.bufRef` streamed after the last segment flush; `idle()`
+ // below would wipe it (#61520). Flush it as a segment only when the
+ // gateway's final text does not already carry it — otherwise the tail IS
+ // the answer and flushing would move the tool shelf/trail under it. Skipped
+ // when `completionText` is absent: `rawText` is then the buffer (#16391).
+ const tail = this.bufRef.trim()
+
+ if (tail && completionText != null && !completionText.includes(tail)) {
this.flushStreamingSegment()
}