fix(chat): keep streaming replies after their prompts (#37773)

* fix(chat): keep streaming replies after their prompts

* fix(chat): retain queued placement through turn transitions

* fix(chat): preserve queued sends through read errors

* test(chat): keep act callbacks void

* test(chat): align transcript tiers with prompt order
This commit is contained in:
Kelvin Amoaba
2026-11-03 10:15:37 +00:00
committed by GitHub
parent ee532e571c
commit debbb5b496
22 changed files with 448 additions and 86 deletions
@@ -0,0 +1,119 @@
import { describe, expect, it } from 'vitest'
import type { NativeChatMessage } from '../native-chat-types'
import { deriveNativeChatStreamingText, nativeChatStreamingMessage } from '../native-chat-streaming'
import { orderNativeChatDisplayMessages } from './native-chat-message-order'
import { pendingSendsAsMessages, type NativeChatPendingSend } from './native-chat-pending-sends'
const answer: NativeChatMessage = {
id: 'old-answer',
role: 'assistant',
blocks: [{ type: 'text', text: 'Earlier answer that is longer than the new reply' }],
timestamp: 9_000_000,
source: 'transcript',
transcriptOrder: 1
}
const active: NativeChatPendingSend = {
id: 'active',
text: 'Run the tests',
sentAt: 10,
afterMessageId: answer.id
}
const queued: NativeChatPendingSend = {
id: 'follow-up',
text: 'Then update the docs',
sentAt: 1,
afterMessageId: answer.id,
queuedDuringWorkingEpoch: 500
}
const preview = nativeChatStreamingMessage('Starting the tests')
function tail(
messages: NativeChatMessage[],
pending: NativeChatPendingSend[],
epoch: number | null
) {
const echoes = pendingSendsAsMessages(pending, messages, epoch)
return {
messages: orderNativeChatDisplayMessages([...messages, preview, ...echoes]),
preview: deriveNativeChatStreamingText({
messages: [...messages, ...echoes],
previewText: 'Starting the tests',
working: true
})
}
}
describe('terminal chat causal order', () => {
it('keeps idle sends and launch prompts before their answer and queued sends after it despite clock skew', () => {
const echoes = pendingSendsAsMessages([active, queued], [answer], 500)
const rows = orderNativeChatDisplayMessages([
answer,
preview,
...echoes,
{ ...answer, id: 'launch-pending:tab', role: 'user', timestamp: 99_000_000 },
{ ...answer, id: 'host-queue', role: 'user', queued: true }
])
expect(rows.map((row) => row.id)).toEqual([
'old-answer',
'pending:active',
'launch-pending:tab',
'streaming',
'pending:follow-up',
'host-queue'
])
})
it('does not promote follow-ups when the active prompt lands, and suppresses the caught-up preview', () => {
expect(tail([answer], [active, queued], 500).preview).toBe('Starting the tests')
const landed: NativeChatMessage = {
...answer,
id: 'prompt',
transcriptOrder: 2,
role: 'user',
blocks: [{ type: 'text', text: active.text }]
}
const caughtUp: NativeChatMessage = {
...preview,
id: 'answer',
source: 'transcript',
transcriptOrder: 3
}
const result = tail(
[answer, landed, caughtUp],
[active, queued, { ...queued, id: 'last', text: 'Then summarize', sentAt: 0 }],
500
)
expect(result.messages.map((row) => row.id)).toEqual([
'old-answer',
'prompt',
'answer',
'streaming',
'pending:follow-up',
'pending:last'
])
expect(result.preview).toBeNull()
})
it('makes a follow-up the active prompt when a later host epoch starts', () => {
const result = tail([answer], [queued], 501)
expect(result.messages.map((row) => row.id)).toEqual([
'old-answer',
'pending:follow-up',
'streaming'
])
expect(result.preview).toBe('Starting the tests')
})
it.each([
[null, null],
[500, null],
[null, 500]
])('keeps queued placement without durable evidence of a new turn (%s → %s)', (sent, current) => {
const result = tail([answer], [{ ...queued, queuedDuringWorkingEpoch: sent }], current)
expect(result.messages.map((row) => row.id)).toEqual([
'old-answer',
'streaming',
'pending:follow-up'
])
})
})
@@ -0,0 +1,50 @@
import type { NativeChatMessage } from '../native-chat-types'
import { NATIVE_CHAT_STREAMING_ID } from '../native-chat-streaming'
import { isLaunchPromptMessageId, isPendingMessageId } from './native-chat-pending-sends'
import {
compareNativeChatTranscriptMessages,
orderNativeChatTranscriptEntries
} from './native-chat-transcript-projection'
// Clocks cannot order a null-timestamp preview against a client send or a remote transcript.
function messageSortRank(message: NativeChatMessage): number {
if (message.queued || message.pendingSend === 'queued') {
return 3
}
if (message.id === NATIVE_CHAT_STREAMING_ID) {
return 2
}
if (
message.pendingSend ||
isPendingMessageId(message.id) ||
isLaunchPromptMessageId(message.id)
) {
return 1
}
return 0
}
export function compareNativeChatDisplayMessages(
a: NativeChatMessage,
b: NativeChatMessage
): number {
const rank = messageSortRank(a)
const difference = rank - messageSortRank(b)
return difference || (rank === 0 ? compareNativeChatTranscriptMessages(a, b) : 0)
}
export function orderNativeChatDisplayEntries<T>(
entries: readonly T[],
messageOf: (entry: T) => NativeChatMessage
): T[] {
return [0, 1, 2, 3].flatMap((rank) => {
const tier = entries.filter((entry) => messageSortRank(messageOf(entry)) === rank)
return rank === 0 ? orderNativeChatTranscriptEntries(tier, messageOf) : tier
})
}
export function orderNativeChatDisplayMessages(
messages: readonly NativeChatMessage[]
): NativeChatMessage[] {
return orderNativeChatDisplayEntries(messages, (message) => message)
}
@@ -25,8 +25,10 @@ export type NativeChatPendingSend = {
writeUnconfirmedAt?: number
text: string
imagePaths?: string[]
/** Epoch ms when the send was issued, so the queued bubble sorts to the end. */
/** Client clock at send time; display placement never compares it with the host clock. */
sentAt: number
/** Host working epoch observed at send time; absent for an idle send, null for an unknown epoch. */
queuedDuringWorkingEpoch?: number | null
/** Match only after the last authoritative row at send time so repeated prompts cannot bind older turns. */
afterMessageId?: string | null
/** Timestamp of that boundary in the transcript host's clock domain. */
@@ -204,10 +206,24 @@ export function visibleNativeChatPendingSends<T extends NativeChatPendingSend>(
})
}
/** Scrape-source echoes yield to real transcript rows; send timestamps keep queued bubbles at the end. */
export function nativeChatPendingSendPlacement(
entry: Pick<NativeChatPendingSend, 'queuedDuringWorkingEpoch'>,
workingEpoch?: number | null
): 'active' | 'queued' {
return entry.queuedDuringWorkingEpoch !== undefined &&
workingEpoch !== undefined &&
(entry.queuedDuringWorkingEpoch === null ||
workingEpoch === null ||
entry.queuedDuringWorkingEpoch === workingEpoch)
? 'queued'
: 'active'
}
/** Scrape-source echoes yield to real transcript rows. */
export function pendingSendsAsMessages(
pending: NativeChatPendingSend[],
existingMessages: readonly NativeChatMessage[] = []
existingMessages: readonly NativeChatMessage[] = [],
workingEpoch?: number | null
): NativeChatMessage[] {
return visibleNativeChatPendingSends(pending, existingMessages).map((entry) => ({
id: `pending:${entry.id}`,
@@ -217,6 +233,7 @@ export function pendingSendsAsMessages(
...(entry.text.trim().length > 0 ? [{ type: 'text' as const, text: entry.text }] : [])
],
timestamp: entry.sentAt,
pendingSend: nativeChatPendingSendPlacement(entry, workingEpoch),
source: 'scrape' as const
}))
}
@@ -224,3 +241,7 @@ export function pendingSendsAsMessages(
export function isPendingMessageId(id: string): boolean {
return id.startsWith('pending:')
}
export function isLaunchPromptMessageId(id: string): boolean {
return id.startsWith('launch-pending:')
}