Skip to content

The agent that grows with you

fix(desktop): bound the identical-interim match to the current occurrence

jiangkoumof82e617ff369
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/collapse-duplicate-final.ts b/apps/desktop/src/app/session/hooks/use-message-stream/collapse-duplicate-final.ts
index b9573668260..592faef344e 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/collapse-duplicate-final.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/collapse-duplicate-final.ts
@@ -74,17 +74,28 @@ export function collapseDuplicateFinalAfterToolInterim(
* rows the transcript must keep. Only the nearest sealed interim is considered
* (the loop stops at the first one): anything further back is an earlier
* segment of the turn.
+ *
+ * `interimBoundaryPending` bounds the scan to the CURRENT occurrence. The
+ * prompt row is not the only boundary: `message.start` also starts one while
+ * keeping the prior messages (a chained turn, a prompt-less turn), and it
+ * resets that flag. Without it, `interim('X') → message.start → delta('X') →
+ * complete('X')` reaches back past the boundary, settles the PREVIOUS turn's
+ * interim and deletes this turn's live bubble. The flag is exactly "an interim
+ * was sealed since the last `message.start`", so a false value means the only
+ * interims in range belong to an earlier occurrence and must not be touched.
*/
export function identicalInterimSiblingIndex(
messages: ChatMessage[],
boundaryIndex: number,
finalText: string,
- excludeIndex = -1
+ options: { interimBoundaryPending: boolean; excludeIndex?: number }
): number {
- if (!finalText) {
+ if (!finalText || !options.interimBoundaryPending) {
return -1
}
+ const excludeIndex = options.excludeIndex ?? -1
+
for (let index = messages.length - 1; index > boundaryIndex; index -= 1) {
if (index === excludeIndex) {
continue
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.ts
index 032f01b9cd2..04fdcc5b10e 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
@@ -872,7 +872,13 @@ export function useMessageStream({
// A sealed text-only interim carrying exactly this reply IS the reply
// (#123801) — see identicalInterimSiblingIndex. Settling it keeps one
// bubble for the one stored row instead of painting the text twice.
- const identicalInterimIndex = identicalInterimSiblingIndex(prev, lastUserIndex, finalText, streamIndex)
+ // Gated on the occurrence boundary: an interim sealed before a
+ // `message.start` belongs to a previous turn and must not be reached
+ // (the new turn's bubble would be the one deleted).
+ const identicalInterimIndex = identicalInterimSiblingIndex(prev, lastUserIndex, finalText, {
+ interimBoundaryPending,
+ excludeIndex: streamIndex
+ })
if (streamIndex >= 0) {
collapsed =
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/interim-redelivery-duplicate.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/interim-redelivery-duplicate.test.tsx
index bc952206b70..6ad0614b979 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/interim-redelivery-duplicate.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/interim-redelivery-duplicate.test.tsx
@@ -82,6 +82,22 @@ describe('a reply the turn re-streams after sealing it as an interim paints once
expect(visibleAssistantTexts()).toEqual([REPLY])
})
+ it('never reaches across a message.start boundary to settle an earlier occurrence', async () => {
+ mountStream()
+ await start()
+ await interim(REPLY)
+ // A chained / prompt-less turn begins: message.start starts a new
+ // occurrence and resets interimBoundaryPending while keeping the messages,
+ // so the sealed interim above is now the PREVIOUS turn's segment. Matching
+ // it would delete this turn's live bubble and complete the old one.
+ await start()
+ await delta(REPLY)
+ await flushDeltas()
+ await complete(REPLY)
+
+ expect(visibleAssistantTexts()).toEqual([REPLY, REPLY])
+ })
+
it('still keeps a distinct interim segment beside the reply', async () => {
mountStream()
await start()