The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): stop finished replies rendering twice after settle
Brooklyn Nicholsonf9a57d5f9106
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/index.tsindex 9d178f32ff6..d6486d681f7 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts+++ b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts@@ -812,9 +812,30 @@ export function useMessageStream({// is the same failure, so it settles onto that card.const failureRepeatsErrorCard = Boolean(completionError && existing.error && !existingText)+ // The terminal frame names the stored row it settled; this+ // trailing bubble is a still-unsettled display-only interim that+ // never carried a durable rowId of its own. A rewritten final+ // (response_previewed / response_transformed) shares no prefix+ // with the streamed interim text, so the heuristics above miss+ // and the volatile boundary flag cannot speak for it once a+ // chained message.start reset it (#74560's ordering). The receipt+ // is the durable identity the flag never was: settle onto the+ // interim instead of painting a second bubble for one row+ // (#124128). A frame with no receipt keeps the rules below, so a+ // genuinely distinct reply still appends its own bubble.+ const finalRowId = persistedTurn?.final_assistant_row_id++ const settlesPersistedRow =+ existing.interim === true &&+ existing.rowId === undefined &&+ typeof finalRowId === 'number' &&+ Number.isSafeInteger(finalRowId) &&+ finalRowId > 0+if (existing.pending ||failureRepeatsErrorCard ||+ settlesPersistedRow ||(!interimBoundaryPending && finalText && existingText === finalText)) {nextMessages = settleAt(index)@@ -823,9 +844,12 @@ export function useMessageStream({finalContinuesInterim) {// Settle the interim in place instead of creating a duplicate —- // the DB has one row, so the live UI must agree. Two distinct+ // the DB has one row, so the live UI must agree. Three distinct// settle paths with different boundary requirements://+ // • settlesPersistedRow (above) keys on the frame's own durable+ // row address, so it needs no boundary flag at all.+ //// • responsePreviewed covers the verify-on-stop continuation-// budget case, where the final may be a rewrite sharing no// prefix with the interim. Because there is no continuitydiff --git a/apps/desktop/src/app/session/hooks/use-message-stream/interim-sealing.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/interim-sealing.test.tsxindex 80805d3119a..822e90a8b2f 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/interim-sealing.test.tsx+++ b/apps/desktop/src/app/session/hooks/use-message-stream/interim-sealing.test.tsx@@ -35,6 +35,33 @@ const completePreviewed = (text: string) =>stream.handleEvent({ payload: { text, response_previewed: true }, session_id: SID, type: 'message.complete' }))+/** A terminal frame that names the stored rows it settled, as a complete+ * `persisted_turn` receipt does in production. */+const receipt = (finalRowId: number) => ({+ row_ids: [finalRowId - 1, finalRowId],+ user_row_id: finalRowId - 1,+ final_assistant_row_id: finalRowId,+ complete: true+})++const completePreviewedWithReceipt = (text: string, finalRowId: number) =>+ act(() =>+ stream.handleEvent({+ payload: { persisted_turn: receipt(finalRowId), response_previewed: true, text },+ session_id: SID,+ type: 'message.complete'+ })+ )++const completeTransformedWithReceipt = (text: string, finalRowId: number) =>+ act(() =>+ stream.handleEvent({+ payload: { persisted_turn: receipt(finalRowId), response_transformed: true, text },+ session_id: SID,+ type: 'message.complete'+ })+ )+function getState(): ClientSessionState {return stream.state()}@@ -248,6 +275,42 @@ describe('useMessageStream interim text sealing', () => {expect(texts).toHaveLength(2)})+ // The pinned case above has no receipt, so no durable identity to settle on.+ // With one, a rewritten final after a chained message.start is the SAME+ // turn's reply: the interim never carried a rowId of its own, and the frame+ // names the row it settled — settle onto the interim instead of appending a+ // second bubble for one stored row (#124128).+ it('settles a rewritten previewed final onto the display-only interim after a chained message.start', async () => {+ mountStream()+ await start()+ await delta('Checking the lease file now.')+ await interim('Checking the lease file now.')+ await start()+ await completePreviewedWithReceipt('The lease file shows a stale lock.', 8)++ const texts = assistantMessages()+ expect(texts).toHaveLength(1)+ expect(texts[0]).toBe('The lease file shows a stale lock.')+ expect(+ getState()+ .messages.filter(m => m.role === 'assistant')+ .at(-1)?.interim+ ).toBeFalsy()+ })++ it('settles a rewritten transformed final onto the display-only interim after a chained message.start', async () => {+ mountStream()+ await start()+ await delta('TOKEN_1 holds the stale lock.')+ await interim('TOKEN_1 holds the stale lock.')+ await start()+ await completeTransformedWithReceipt('example-service.internal holds the stale lock.', 8)++ const texts = assistantMessages()+ expect(texts).toHaveLength(1)+ expect(texts[0]).toBe('example-service.internal holds the stale lock.')+ })+it('appends a genuinely different final as its own bubble (two real assistant segments)', async () => {mountStream()await start()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.tsindex cfe711723d9..fb0fe16330f 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@@ -2117,6 +2117,42 @@ describe('overlayConcurrentMessageChanges', () => {expect(overlayConcurrentMessageChanges(page, [page[0]], [page[0], errored]).at(-1)).toBe(errored)})++ // The committed row and the settled live row capture the same reply at two+ // moments while it kept streaming, so one is routinely a prefix of the+ // other (#123993): accept either as a forward extension, as the sibling+ // removeRepresentedLocalLiveProjection already does (2494b95929).+ it('folds a settled live row that lags behind the committed row into one reply', () => {+ const page = [+ msg('3-user', 'user', 'prompt b', { rowId: 3 }),+ msg('4-assistant', 'assistant', 'A2 finished while away', { rowId: 4 })+ ]++ const current = [page[0], msg('assistant-stream-1-2', 'assistant', 'A2 finished', { pending: false })]++ const overlaid = overlayConcurrentMessageChanges(page, [], current)++ expect(overlaid.map(message => [message.id, chatMessageText(message)])).toEqual([+ ['3-user', 'prompt b'],+ ['4-assistant', 'A2 finished while away']+ ])+ })++ it('folds a settled live row that ran past the committed row into one reply', () => {+ const page = [+ msg('3-user', 'user', 'prompt b', { rowId: 3 }),+ msg('4-assistant', 'assistant', 'A2 finished', { rowId: 4 })+ ]++ const current = [page[0], msg('assistant-stream-1-2', 'assistant', 'A2 finished while away', { pending: false })]++ const overlaid = overlayConcurrentMessageChanges(page, [], current)++ expect(overlaid.map(message => [message.id, chatMessageText(message)])).toEqual([+ ['3-user', 'prompt b'],+ ['4-assistant', 'A2 finished']+ ])+ })})describe('preserveEquivalentTranscript', () => {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.tsindex 831606269e4..2dd506cc06b 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@@ -1546,14 +1546,29 @@ export function overlayConcurrentMessageChanges(const text = textWithoutReferenceLines(chatMessageText(current)).trim()const lastUser = overlaid.findLastIndex(message => message.role === 'user')- const committed = overlaid.some(- (message, index) =>- index > lastUser &&- message.role === 'assistant' &&- !baselineById.has(message.id) &&- !isLiveTailRow(message) &&- textWithoutReferenceLines(chatMessageText(message)).trim() === text- )+ const committed = overlaid.some((message, index) => {+ if (+ !(index > lastUser) ||+ message.role !== 'assistant' ||+ baselineById.has(message.id) ||+ isLiveTailRow(message)+ ) {+ return false+ }++ // The committed row and the settled live row capture the same reply+ // 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)+ )+ })if (text && committed) {continue