Skip to content

The agent that grows with you

fix(desktop): a stopped turn a refresh already drew from the page draws once

Teknium0e34164fdcda
diff --git a/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
index cc5717acc78..5bb607b6809 100644
--- a/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
@@ -139,6 +139,38 @@ describe('a refreshed page that holds the live turn whole', () => {
expect(count(merged, 'live look')).toBe(1)
})
+ it('draws a stopped turn once when a page merged before its interrupted completion', () => {
+ const receipt: PersistedTurn = {
+ row_ids: [17, 18, 19, 20],
+ complete: false,
+ user_row_id: 17,
+ user_row_ids: [17],
+ submission_id: 'sub-race'
+ }
+
+ const page = toChatMessages([
+ ...olderTurn(1, 'old-a').slice(3),
+ ...olderTurn(9, 'old-b'),
+ ...liveRows,
+ answer(20, 'partial')
+ ])
+
+ // Stop settled the turn on screen, a refresh merged the page (which holds
+ // the turn's stored rows) while the live rows had no stored id, and only
+ // then did the interrupted completion bring the receipt.
+ const refreshed = graftRefreshedTailOntoBackfill(page, [
+ ...toChatMessages(windowRows),
+ live('user-sub-race', 'user', 'steered prompt'),
+ live('assistant-stream-1', 'assistant', 'live look partial')
+ ])
+
+ const stopped = texts(bindStoppedTurnReceipt(refreshed, receipt))
+
+ expect(count(stopped, 'steered prompt')).toBe(1)
+ expect(count(stopped, 'live look')).toBe(1)
+ expect(count(stopped, 'old-b answer')).toBe(1)
+ })
+
it('keeps a live turn the page does not reach yet', () => {
const receipt: PersistedTurn = { row_ids: [17, 18, 19, 20, 21], complete: false, submission_id: 'sub-late' }
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/interrupted-reply.ts b/apps/desktop/src/app/session/hooks/use-message-stream/interrupted-reply.ts
index 4e78845502b..d2a54e35e25 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/interrupted-reply.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/interrupted-reply.ts
@@ -1,5 +1,6 @@
import type { PersistedTurn } from '@hermes/shared'
+import { transcriptRowIds } from '@/app/session/hooks/use-session-actions/pending-turn-identity'
import {
assistantTextPart,
type ChatMessage,
@@ -69,12 +70,45 @@ export function extendInterruptedReply(messages: ChatMessage[], rawText: string,
]
}
+const isRowId = (value: unknown): value is number =>
+ typeof value === 'number' && Number.isSafeInteger(value) && value > 0
+
+/**
+ * Stop settles the turn on screen before the backend's interrupted completion
+ * arrives, so a refresh can merge the newest page in between. That page already
+ * holds the stopped turn's stored rows while the live rows have no stored id
+ * yet, so both copies were kept and the turn, prompt included, drew twice
+ * (#139741). Once the receipt names the turn's rows, a row in front of the
+ * sender's own prompt whose stored ids all belong to the turn is that stored
+ * copy. The live copy, which now carries the receipt, stays: it is what the
+ * user watched, and the next page read replaces it the usual way.
+ */
+function dropStoredCopiesOfTheTurn(messages: ChatMessage[], receipt: PersistedTurn): ChatMessage[] {
+ const submission = typeof receipt.submission_id === 'string' ? receipt.submission_id : ''
+ const prompt = submission ? messages.findIndex(message => message.id === `user-${submission}`) : -1
+
+ if (prompt < 0) {
+ return messages
+ }
+
+ const turnRows = new Set(receipt.row_ids.filter(isRowId))
+
+ const storedCopy = (message: ChatMessage, index: number) => {
+ const ids = transcriptRowIds(message)
+
+ return index < prompt && ids.length > 0 && ids.every(id => turnRows.has(id))
+ }
+
+ return messages.some(storedCopy) ? messages.filter((message, index) => !storedCopy(message, index)) : messages
+}
+
/**
* A stopped turn keeps the stored identity its interrupted `message.complete`
- * names: the turn's user bubbles take their stored row ids and its last reply
- * bubble carries the receipt. Without them every row of the stopped turn is an
- * unstored live row, and a later read of a page that holds the turn draws it a
- * second time, prompt included (#139741).
+ * names: the turn's user bubbles take their stored row ids, its last reply
+ * bubble carries the receipt, and a stored copy of the turn a refresh already
+ * drew goes. Without them every row of the stopped turn is an unstored live
+ * row, and a read of a page that holds the turn draws it a second time, prompt
+ * included (#139741).
*/
export function bindStoppedTurnReceipt(
messages: ChatMessage[],
@@ -84,7 +118,7 @@ export function bindStoppedTurnReceipt(
return messages
}
- const bound = bindReceiptUserRows(messages, receipt)
+ const bound = dropStoredCopiesOfTheTurn(bindReceiptUserRows(messages, receipt), receipt)
const lastUserIndex = bound.findLastIndex(message => message.role === 'user')
const target = bound.findLastIndex(message => message.role === 'assistant' && !message.hidden)