Skip to content

The agent that grows with you

fix(desktop): a partial receipt never drops an answer the page does not hold

Tekniumc1458ee7aecc
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 58a6d6a6d4d..3ef98d555b1 100644
--- a/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
@@ -95,6 +95,29 @@ describe('a refreshed page that holds the live turn whole', () => {
}
})
+ it('keeps an answer the receipt does not cover when the turn ended without storing it', () => {
+ // The final answer failed to store: the partial receipt addresses only rows 17-19, and a later
+ // stored turn follows, so the page reaches past the receipt without carrying the answer.
+ const receipt: PersistedTurn = { row_ids: [17, 18, 19], complete: false, user_row_id: 17, submission_id: 'sub-lost' }
+
+ const window = [
+ ...toChatMessages(windowRows),
+ live('user-sub-lost', 'user', 'steered prompt', { rowId: 17 }),
+ live('assistant-stream-1', 'assistant', 'live look', { interim: false }),
+ live('assistant-stream-2', 'assistant', 'unstored answer', { persistedTurn: receipt })
+ ]
+
+ const page = toChatMessages([
+ ...olderTurn(1, 'old-a').slice(3),
+ ...olderTurn(9, 'old-b'),
+ ...liveRows,
+ user(20, 'later prompt'),
+ answer(21, 'later answer')
+ ])
+
+ expect(count(texts(graftRefreshedTailOntoBackfill(page, window)), 'unstored answer')).toBe(1)
+ })
+
it('draws an older turn the page boundary cuts once', () => {
const window = [...toChatMessages(windowRows), live('user-x', 'user', 'next prompt', { rowId: 17 })]
diff --git a/apps/desktop/src/app/chat/transcript-receipt-turns.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.ts
index 018fc9113dc..16f7ad62a6f 100644
--- a/apps/desktop/src/app/chat/transcript-receipt-turns.ts
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.ts
@@ -23,13 +23,10 @@
import type { PersistedTurn } from '@hermes/shared'
-import { isTurnUserBubble, receiptUserRowIds } from '@/app/session/hooks/use-message-stream/receipt-user-rows'
+import { isRowId, isTurnUserBubble, receiptUserRowIds } from '@/app/session/hooks/use-message-stream/receipt-user-rows'
import { transcriptRowIds } from '@/app/session/hooks/use-session-actions/pending-turn-identity'
import { type ChatMessage, isLocalSystemNotice } from '@/lib/chat-messages'
-const isRowId = (value: unknown): value is number =>
- typeof value === 'number' && Number.isSafeInteger(value) && value > 0
-
/** Index of the turn's prompt in the window, by the receipt's own identity. */
function receiptPromptIndex(window: ChatMessage[], receipt: PersistedTurn, before: number): number {
const submission = typeof receipt.submission_id === 'string' && receipt.submission_id ? receipt.submission_id : ''
@@ -96,11 +93,42 @@ function windowTurnIndices(window: ChatMessage[], end: number, first: number, la
return indices
}
+/** Stored ids of the reply text rows the page paints (an answer or a stopped partial, never a tool row). */
+const pageReplyRows = (page: ChatMessage[]): Set<number> =>
+ new Set(
+ page.flatMap(message =>
+ message.role === 'assistant'
+ ? message.parts.flatMap(part => (part.type === 'text' && part.sourceRowId !== undefined ? [part.sourceRowId] : []))
+ : []
+ )
+ )
+
+/**
+ * Whether the page's copy of the turn includes its reply. A receipt that is not `complete` can
+ * address only the rows that were stored (agent/persisted_turn_receipt.py): when the final answer
+ * failed to store, the turn's last stored row is a tool row and the window's bubble is the only copy
+ * of the answer. Proven by a complete receipt, by the page painting the turn's last stored row as
+ * reply text, or by a stopped turn (everything it produced before the stop is stored).
+ */
+function pageHoldsTheReply(message: ChatMessage, last: number, replyRows: Set<number>): boolean {
+ return Boolean(message.persistedTurn?.complete || message.interrupted) || replyRows.has(last)
+}
+
/** Window indices of the receipt turn ending at `end` that the page holds whole. */
-function receiptTurnIndices(window: ChatMessage[], end: number, pageRows: Set<number>): number[] {
+function receiptTurnIndices(
+ window: ChatMessage[],
+ end: number,
+ pageRows: Set<number>,
+ replyRows: Set<number>
+): number[] {
const bounds = receiptBounds(window[end])
- if (!bounds || !pageRows.has(bounds.first) || Math.max(...pageRows) < bounds.last) {
+ if (
+ !bounds ||
+ !pageRows.has(bounds.first) ||
+ Math.max(...pageRows) < bounds.last ||
+ !pageHoldsTheReply(window[end], bounds.last, replyRows)
+ ) {
return []
}
@@ -168,13 +196,14 @@ export function dropReceiptTurnsThePageHolds(window: ChatMessage[], page: ChatMe
}
const dropped = new Set<number>()
+ const replyRows = pageReplyRows(page)
window.forEach((message, index) => {
if (!message.persistedTurn || message.pending) {
return
}
- const indices = receiptTurnIndices(window, index, pageRows)
+ const indices = receiptTurnIndices(window, index, pageRows, replyRows)
indices.forEach(at => dropped.add(at))