Skip to content

The agent that grows with you

fix(desktop): drop a settled live reply the page still carries as its streaming copy

Brooklyn Nicholson4fba38e2dab9
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
index 4433ee8add9..eff319eb6a4 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
@@ -2183,6 +2183,16 @@ describe('overlayConcurrentMessageChanges', () => {
)
expect(overlaid.map(message => message.id)).toEqual(['3-user', '4-assistant'])
+
+ // A completion without a row receipt still matches the folded bubble's answer.
+ const unbound = { ...settled, rowId: undefined, parts: [{ type: 'text' as const, text: 'A2 finished while away' }] }
+
+ expect(
+ overlayConcurrentMessageChanges([msg('3-user', 'user', 'prompt b', { rowId: 3 }), folded], baseline, [
+ baseline[0],
+ unbound
+ ]).map(message => message.id)
+ ).toEqual(['3-user', '4-assistant'])
})
// Same race, the order the e2e hit: the store already swapped its live rows
@@ -2209,6 +2219,29 @@ describe('overlayConcurrentMessageChanges', () => {
'3-user',
'4-assistant'
])
+
+ // Or the store still holds that row, now settled onto the reply's stored
+ // row, while the page carries its older streaming copy under the same id.
+ const settled = msg('assistant-stream-1-3', 'assistant', 'A2 finished', {
+ pending: false,
+ rowId: 6,
+ parts: [{ type: 'text', text: 'A2 finished', sourceRowId: 6 }]
+ })
+
+ const folded = msg('4-assistant', 'assistant', '', {
+ rowId: 4,
+ parts: [
+ { type: 'text', text: 'A2i checking', sourceRowId: 4 },
+ { type: 'tool-call', toolCallId: 'call-1', toolName: 'terminal', args: {}, argsText: '{}', result: 'ok' },
+ { type: 'text', text: 'A2 finished', sourceRowId: 6 }
+ ]
+ } as Partial<ChatMessage>)
+
+ expect(
+ overlayConcurrentMessageChanges([committed[0], folded, live], baseline, [baseline[0], baseline[1], settled]).map(
+ m => m.id
+ )
+ ).toEqual(['3-user', '4-assistant'])
})
it('does not project a stale snapshot over a committed tool turn', () => {
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
index a5145e31394..27492d36a54 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
@@ -129,15 +129,19 @@ export function isStrictAnswerTextExtension(next: string, previous: string): boo
* mid-turn commit ends on a tool call and the turn is still running.
*/
function committedReplyCovers(message: ChatMessage, snapshot: string): boolean {
- const lastTool = message.parts.findLastIndex(part => part.type === 'tool-call')
- const reply = chatMessageText({ ...message, parts: message.parts.slice(lastTool + 1) })
-
const text = message.parts
.flatMap(part => (part.type === 'text' ? [part.text] : []))
.join(' ')
.replace(/\s+/g, ' ')
- return Boolean(reply.trim()) && isStrictAnswerTextExtension(text, snapshot.replace(/\s+/g, ' '))
+ return Boolean(replyAfterLastTool(message).trim()) && isStrictAnswerTextExtension(text, snapshot.replace(/\s+/g, ' '))
+}
+
+/** A folded tool-turn bubble's answer: its text after the last tool call. */
+function replyAfterLastTool(message: ChatMessage): string {
+ const lastTool = message.parts.findLastIndex(part => part.type === 'tool-call')
+
+ return chatMessageText({ ...message, parts: message.parts.slice(lastTool + 1) })
}
/**
@@ -1637,42 +1641,26 @@ export function overlayConcurrentMessageChanges(
const nextIndexById = new Map(nextMessages.map((message, index) => [message.id, index]))
let changed = false
const overlaid = [...nextMessages]
+ const dropped = new Set<string>()
let activationStreamIndex = overlaid.findIndex(
message =>
message.role === 'assistant' && message.id.startsWith('assistant-stream-') && !baselineById.has(message.id)
)
- for (const current of currentMessages) {
- const baseline = baselineById.get(current.id)
- const changedSinceBaseline = !baseline || !chatMessagesEquivalent(baseline, current)
-
- if (!changedSinceBaseline) {
- continue
- }
-
- const nextIndex = nextIndexById.get(current.id)
-
- if (nextIndex !== undefined) {
- if (!chatMessagesEquivalent(overlaid[nextIndex], current)) {
- overlaid[nextIndex] = current
- changed = true
- }
-
- continue
- }
-
- // message.complete settled this stream row while REST was in flight, and
- // the page already carries the same reply under its committed id (#70209).
- // Only a row the page newly added counts: one already in the baseline is an
- // earlier turn's answer (a resent prompt can repeat it word for word). An
- // errored row carries a failure the committed text cannot show.
- if (current.role === 'assistant' && current.pending !== true && !current.error && isLiveTailReplyId(current.id)) {
- const text = textWithoutReferenceLines(chatMessageText(current)).trim()
- const lastUser = overlaid.findLastIndex(message => message.role === 'user')
- const liveRows = transcriptRowIds(current)
-
- const committed = overlaid.some((message, index) => {
+ // message.complete settled this stream row while REST was in flight, and
+ // the page already carries the same reply under its committed id (#70209).
+ // Only a row the page newly added counts: one already in the baseline is an
+ // earlier turn's answer (a resent prompt can repeat it word for word). An
+ // errored row carries a failure the committed text cannot show.
+ const committedOnPage = (current: ChatMessage): boolean => {
+ const text = textWithoutReferenceLines(chatMessageText(current)).trim()
+ const lastUser = overlaid.findLastIndex(message => message.role === 'user')
+ const liveRows = transcriptRowIds(current)
+
+ return (
+ Boolean(text) &&
+ overlaid.some((message, index) => {
if (
!(index > lastUser) ||
message.role !== 'assistant' ||
@@ -1693,19 +1681,54 @@ export function overlayConcurrentMessageChanges(
// at two moments while it kept streaming, so neither side is
// guaranteed to be textually identical: accept either as a forward
// text-extension of the other, the same trade
- // removeRepresentedLocalLiveProjection made in 2494b95929.
- const candidate = textWithoutReferenceLines(chatMessageText(message)).trim()
-
- return (
- candidate === text ||
- isStrictAnswerTextExtension(candidate, text) ||
- isStrictAnswerTextExtension(text, candidate)
- )
+ // removeRepresentedLocalLiveProjection made in 2494b95929. A folded
+ // tool turn is compared by its answer, the text after its last tool.
+ return [chatMessageText(message), replyAfterLastTool(message)].some(full => {
+ const candidate = textWithoutReferenceLines(full).trim()
+
+ return (
+ candidate === text ||
+ isStrictAnswerTextExtension(candidate, text) ||
+ isStrictAnswerTextExtension(text, candidate)
+ )
+ })
})
+ )
+ }
- if (text && committed) {
- continue
+ for (const current of currentMessages) {
+ const baseline = baselineById.get(current.id)
+ const changedSinceBaseline = !baseline || !chatMessagesEquivalent(baseline, current)
+
+ if (!changedSinceBaseline) {
+ continue
+ }
+
+ const nextIndex = nextIndexById.get(current.id)
+
+ // The page can still carry the row's older streaming copy by id when it
+ // was composed from the baseline; the committed row replaces both.
+ if (
+ current.role === 'assistant' &&
+ current.pending !== true &&
+ !current.error &&
+ isLiveTailReplyId(current.id) &&
+ committedOnPage(current)
+ ) {
+ if (nextIndex !== undefined) {
+ dropped.add(current.id)
}
+
+ continue
+ }
+
+ if (nextIndex !== undefined) {
+ if (!chatMessagesEquivalent(overlaid[nextIndex], current)) {
+ overlaid[nextIndex] = current
+ changed = true
+ }
+
+ continue
}
if (activationStreamIndex >= 0 && current.role === 'assistant' && current.id.startsWith('assistant-stream-')) {
@@ -1738,7 +1761,7 @@ export function overlayConcurrentMessageChanges(
const currentIds = new Set(currentMessages.map(message => message.id))
const retired = (message: ChatMessage) =>
- isLiveTailRow(message) && baselineById.has(message.id) && !currentIds.has(message.id)
+ dropped.has(message.id) || (isLiveTailRow(message) && baselineById.has(message.id) && !currentIds.has(message.id))
if (currentMessages.length && overlaid.some(retired)) {
return overlaid.filter(message => !retired(message))