Skip to content

The agent that grows with you

fix(desktop): a stopped turn binds its receipt identity

Teknium911a536e8b02
diff --git a/apps/desktop/e2e/core/tail-resync-merge.spec.ts b/apps/desktop/e2e/core/tail-resync-merge.spec.ts
index a76866907ac..b788d06e65b 100644
--- a/apps/desktop/e2e/core/tail-resync-merge.spec.ts
+++ b/apps/desktop/e2e/core/tail-resync-merge.spec.ts
@@ -238,6 +238,52 @@ test('a long chat keeps each finished tool turn once, in order, after the newest
})
})
+test('a long chat keeps a stopped turn and its resend once after the newest page merges', async () => {
+ test.setTimeout(900_000)
+
+ await withApp('tail-resync-stop', async ({ page, provider, ws }) => {
+ await test.step(`seed ${SEED_TURNS} wide tool turns (more than one page of rows)`, () =>
+ seedLongChat(page, provider, ws))
+
+ const shown: string[] = []
+
+ for (let c = 0; c < CHECKED_TURNS; c += 1) {
+ const n = SEED_TURNS + 1 + 2 * c
+
+ await test.step(`turn ${n}: stopped mid-answer, then turn ${n + 1} resends`, async () => {
+ const hold = gate()
+ provider.script(U(n), [
+ { text: words(N(n, 1), 'looking'), toolCalls: [echo(`s${n}`)] },
+ { text: words(A(n), 'partial', 'answer', 'that', 'never', 'ends'), holdAfterFirstChunk: hold }
+ ])
+ await send(page, `${U(n)} stop me`, 'Enter', ws)
+ await provider.streamStarted(U(n), 1)
+ await expect(viewport(page)).toContainText(A(n))
+ await stopButton(page).filter({ visible: true }).first().click()
+ hold.open()
+ await expect(stopButton(page)).toHaveCount(0, { timeout: 30_000 })
+
+ shown.push(U(n), N(n, 1), A(n))
+ const stopped = await observeTurn(page, shown, `stopped-${n}`)
+
+ expect(stopped.slice(0, 5), `stopped turn ${n} rendered once after the page merge`).toEqual([])
+
+ provider.script(U(n + 1), [
+ { text: words(N(n + 1, 1), 'again'), toolCalls: [echo(`r${n}`)] },
+ { text: words(A(n + 1), 'resent', 'answer') }
+ ])
+ await send(page, `${U(n + 1)} resend`, 'Enter', ws)
+ await settled(page, ws, A(n + 1))
+
+ shown.push(U(n + 1), N(n + 1, 1), A(n + 1))
+ const resent = await observeTurn(page, shown, `resent-${n + 1}`)
+
+ expect(resent.slice(0, 5), `resent turn ${n + 1} rendered once after the page merge`).toEqual([])
+ })
+ }
+ })
+})
+
test('a long chat keeps a steered tool turn once, in order, after the newest page merges', async () => {
test.setTimeout(900_000)
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 9d4734ea098..cc5717acc78 100644
--- a/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
@@ -2,6 +2,7 @@ import type { PersistedTurn } from '@hermes/shared'
import { describe, expect, it } from 'vitest'
import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill'
+import { bindStoppedTurnReceipt } from '@/app/session/hooks/use-message-stream/interrupted-reply'
import { type ChatMessage, chatMessageText, toChatMessages } from '@/lib/chat-messages'
import type { SessionMessage } from '@/types/hermes'
@@ -107,6 +108,37 @@ describe('a refreshed page that holds the live turn whole', () => {
}
})
+ it('draws a stopped turn once after its interrupted completion binds the receipt', () => {
+ const receipt: PersistedTurn = {
+ row_ids: [17, 18, 19, 20],
+ complete: false,
+ user_row_id: 17,
+ user_row_ids: [17],
+ submission_id: 'sub-stop'
+ }
+
+ const stopped = bindStoppedTurnReceipt(
+ [
+ ...toChatMessages(windowRows),
+ live('user-sub-stop', 'user', 'steered prompt'),
+ live('assistant-stream-1', 'assistant', 'live look partial')
+ ],
+ receipt
+ )
+
+ const page = toChatMessages([
+ ...olderTurn(1, 'old-a').slice(3),
+ ...olderTurn(9, 'old-b'),
+ ...liveRows,
+ answer(20, 'partial')
+ ])
+
+ const merged = texts(graftRefreshedTailOntoBackfill(page, stopped))
+
+ expect(count(merged, 'steered prompt')).toBe(1)
+ expect(count(merged, 'live look')).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/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
index 76c4630522f..64f11a883bd 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
@@ -49,7 +49,7 @@ import {
} from './collapse-duplicate-final'
import { useGatewayEventHandler } from './gateway-event'
import { handleServerRequest as dispatchServerRequest } from './gateway-event/server-requests'
-import { extendInterruptedReply } from './interrupted-reply'
+import { bindStoppedTurnReceipt, extendInterruptedReply } from './interrupted-reply'
import { bindReceiptUserRows } from './receipt-user-rows'
import { currentResponseParts, mergeCurrentResponseText } from './response-parts'
import { admitSegmentDelta, admitSegmentSeal, type SegmentCursor, type SegmentStamp } from './segment-identity'
@@ -776,8 +776,10 @@ export function useMessageStream({
if (state.interrupted) {
return {
...state,
- messages:
+ messages: bindStoppedTurnReceipt(
status === 'interrupted' ? extendInterruptedReply(state.messages, text, occurredAt) : state.messages,
+ persistedTurn
+ ),
awaitingResponse: false,
busy: false,
needsInput: false,
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 e2af0431a7b..4e78845502b 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,3 +1,5 @@
+import type { PersistedTurn } from '@hermes/shared'
+
import {
assistantTextPart,
type ChatMessage,
@@ -7,6 +9,8 @@ import {
} from '@/lib/chat-messages'
import { generatedImageEchoSources, stripGeneratedImageEchoes } from '@/lib/generated-images'
+import { bindReceiptUserRows } from './receipt-user-rows'
+
const flat = (text: string) => text.replace(/\s+/g, ' ').trim()
/**
@@ -64,3 +68,29 @@ export function extendInterruptedReply(messages: ChatMessage[], rawText: string,
}
]
}
+
+/**
+ * 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).
+ */
+export function bindStoppedTurnReceipt(
+ messages: ChatMessage[],
+ receipt: null | PersistedTurn | undefined
+): ChatMessage[] {
+ if (!receipt?.row_ids?.length) {
+ return messages
+ }
+
+ const bound = bindReceiptUserRows(messages, receipt)
+ const lastUserIndex = bound.findLastIndex(message => message.role === 'user')
+ const target = bound.findLastIndex(message => message.role === 'assistant' && !message.hidden)
+
+ if (target < 0 || target < lastUserIndex || bound[target].persistedTurn) {
+ return bound
+ }
+
+ return bound.map((message, index) => (index === target ? { ...message, persistedTurn: receipt } : message))
+}