Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/renderer/src/components/native-chat/NativeChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
appendPendingSendCache,
launchPromptAsMessage,
pendingSendsAsMessages,
splitPendingMessagesAroundStreaming,
nextNativeChatPendingSendId,
prunePendingSends,
readPendingSendCache,
Expand Down Expand Up @@ -238,12 +239,16 @@ function NativeChatResolvedView({
sentAt,
afterMessageId: boundary?.id ?? null,
afterMessageTimestamp: boundary?.timestamp ?? null,
// Why (#15608): a send into an idle agent is the newest turn — its reply
// must stream BELOW the echo. Only sends queued behind live work sort
// below the streaming bubble.
...(liveWorking ? { queuedWhileWorking: true } : {}),
...(imagePaths ? { imagePaths } : {})
}
setPending(appendPendingSendCache(pendingScope, entry))
return entry.id
},
[pendingScope, session.messages]
[pendingScope, session.messages, liveWorking]
)
const onOptimisticSendCanceled = useCallback(
(pendingId: string) => {
Expand Down Expand Up @@ -286,12 +291,18 @@ function NativeChatResolvedView({
return new Set([id])
}, [paneLaunchPrompt?.failed, launchPromptMessage?.id, sessionAfterCommandBoundaries.messages])

// The streaming preview bubble (if any) sits after the transcript but before
// the optimistic user echoes — same order mobile uses.
// The streaming preview bubble (if any) sits after the transcript. Where it
// sits relative to the optimistic echoes depends on WHY an echo is pending
// (#15608): a send into an idle agent is the newest turn, so its reply
// streams BELOW it; a send queued behind live work stays below the bubble.
const pendingMessages = useMemo(
() => pendingSendsAsMessages(pending, sessionAfterCommandBoundaries.messages),
[pending, sessionAfterCommandBoundaries.messages]
)
const pendingAroundStreaming = useMemo(
() => splitPendingMessagesAroundStreaming(pendingMessages, pending),
[pendingMessages, pending]
)
const streamingText = useMemo(() => {
return deriveNativeChatStreamingText({
messages:
Expand All @@ -311,11 +322,12 @@ function NativeChatResolvedView({
messages: [
...sessionAfterCommandBoundaries.messages,
...commandMarkersAsMessages(commandMarkers),
...pendingAroundStreaming.beforeStreaming,
...(streamingText ? [nativeChatStreamingMessage(streamingText)] : []),
...pendingMessages
...pendingAroundStreaming.afterStreaming
]
}
}, [sessionAfterCommandBoundaries, pending, pendingMessages, commandMarkers, streamingText])
}, [sessionAfterCommandBoundaries, pendingAroundStreaming, commandMarkers, streamingText])
// Derive the view state from the pending-augmented session so a send into an
// otherwise-empty conversation flips to the list (showing the queued bubble)
// instead of staying on the empty state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
readPendingSendCache,
shouldPruneLaunchPrompt,
writePendingSendCache,
splitPendingMessagesAroundStreaming,
type NativeChatPendingSend
} from './native-chat-pending'
import {
Expand Down Expand Up @@ -808,3 +809,43 @@ describe('scope-cache key counts stay bounded (memory-leak regression)', () => {
)
})
})

describe('splitPendingMessagesAroundStreaming', () => {
const entry = (id: string, queuedWhileWorking?: boolean): NativeChatPendingSend => ({
id,
text: id,
sentAt: 1,
...(queuedWhileWorking ? { queuedWhileWorking: true } : {})
})
const message = (id: string): NativeChatMessage => ({
id: `pending:${id}`,
role: 'user',
blocks: [{ type: 'text', text: id }],
timestamp: 1,
source: 'scrape'
})

it('places an idle-time echo BEFORE the streaming bubble — the reply answers it (#15608)', () => {
const pending = [entry('idle-send')]
const messages = [message('idle-send')]
const split = splitPendingMessagesAroundStreaming(messages, pending)
expect(split.beforeStreaming.map((m) => m.id)).toEqual(['pending:idle-send'])
expect(split.afterStreaming).toEqual([])
})

it('keeps a send queued behind live work AFTER the streaming bubble', () => {
const pending = [entry('queued-send', true)]
const messages = [message('queued-send')]
const split = splitPendingMessagesAroundStreaming(messages, pending)
expect(split.beforeStreaming).toEqual([])
expect(split.afterStreaming.map((m) => m.id)).toEqual(['pending:queued-send'])
})

it('splits a mixed pending list while preserving relative order on both sides', () => {
const pending = [entry('idle-a'), entry('queued-b', true), entry('idle-c'), entry('queued-d', true)]
const messages = pending.map((e) => message(e.id))
const split = splitPendingMessagesAroundStreaming(messages, pending)
expect(split.beforeStreaming.map((m) => m.id)).toEqual(['pending:idle-a', 'pending:idle-c'])
expect(split.afterStreaming.map((m) => m.id)).toEqual(['pending:queued-b', 'pending:queued-d'])
})
})
34 changes: 34 additions & 0 deletions src/renderer/src/components/native-chat/native-chat-pending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export type NativeChatPendingSend = {
matchingOccurrence?: number
/** Shared time boundary when that message boundary is unavailable. */
matchingAfterTimestamp?: number
/**
* True when the agent was already working when this send was issued. Such an
* echo is queued BEHIND the in-flight turn; an idle-time echo is the newest
* turn, and whatever streams after it is its reply (#15608).
*/
queuedWhileWorking?: boolean
}

export type NativeChatPendingSendScope = {
Expand Down Expand Up @@ -267,6 +273,34 @@ export function isPendingMessageId(id: string): boolean {
return id.startsWith('pending:')
}

/**
* Split rendered optimistic echoes around the streaming reply bubble (#15608).
*
* An echo sent while the agent was idle is the newest turn of the conversation
* — the reply that streams next is its ANSWER, so the bubble belongs below it.
* An echo queued while the agent was already working waits behind that
* in-flight turn, so it stays below the bubble. The pending list order itself
* is preserved on both sides.
*/
export function splitPendingMessagesAroundStreaming(
pendingMessages: readonly NativeChatMessage[],
pending: readonly NativeChatPendingSend[]
): { beforeStreaming: NativeChatMessage[]; afterStreaming: NativeChatMessage[] } {
const queuedWhileWorkingIds = new Set(
pending.filter((entry) => entry.queuedWhileWorking).map((entry) => `pending:${entry.id}`)
)
const beforeStreaming: NativeChatMessage[] = []
const afterStreaming: NativeChatMessage[] = []
for (const message of pendingMessages) {
if (queuedWhileWorkingIds.has(message.id)) {
afterStreaming.push(message)
} else {
beforeStreaming.push(message)
}
}
return { beforeStreaming, afterStreaming }
}

// Why: the seeded prompt has a synthetic id that never matches the real turn's,
// so dedup/prune match on normalized user-message text instead — this hides the
// optimistic bubble once the transcript's own copy of the turn catches up.
Expand Down
Loading