Skip to content

The agent that grows with you

fix(desktop): an older turn the newest page cuts draws once

Tekniumbf16cb1b4330
diff --git a/apps/desktop/src/app/chat/transcript-backfill.ts b/apps/desktop/src/app/chat/transcript-backfill.ts
index 194b7684089..bca00c3dfbc 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.ts
@@ -15,7 +15,7 @@
* drift on the next page.
*/
-import { dropReceiptTurnsThePageHolds } from '@/app/chat/transcript-receipt-turns'
+import { dropPageFoldsCutByTheBoundary, dropReceiptTurnsThePageHolds } from '@/app/chat/transcript-receipt-turns'
import { transcriptRowIds } from '@/app/session/hooks/use-session-actions/pending-turn-identity'
import { getOlderSessionMessages, getSessionMessages, type ProfileScope } from '@/hermes'
import { type ChatMessage, chatMessageText, toChatMessages } from '@/lib/chat-messages'
@@ -372,7 +372,7 @@ function mergeOverlappingTail(previous: ChatMessage[], refreshedTail: ChatMessag
// A turn the page holds whole is the page's to draw, whatever stored ids
// its live rows managed to bind (see transcript-receipt-turns).
const previousTrailing = place(dropReceiptTurnsThePageHolds(previous, refreshedTail), false)
- const refreshedTrailing = place(refreshedTail, true)
+ const refreshedTrailing = place(dropPageFoldsCutByTheBoundary(previous, refreshedTail), true)
const stored = [...byRowId.entries()]
.sort((left, right) => left[0] - right[0])
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 71afb3f7560..9d4734ea098 100644
--- a/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
@@ -94,6 +94,19 @@ describe('a refreshed page that holds the live turn whole', () => {
}
})
+ it('draws an older turn the page boundary cuts once', () => {
+ const window = [...toChatMessages(windowRows), live('user-x', 'user', 'next prompt', { rowId: 17 })]
+
+ // The page starts at the first turn's second narration row: its fold of
+ // that turn is addressed by a row the window's fold carries inside it.
+ const page = toChatMessages([...olderTurn(1, 'old-a').slice(3), ...olderTurn(9, 'old-b'), user(17, 'next prompt')])
+ const merged = texts(graftRefreshedTailOntoBackfill(page, window))
+
+ for (const text of ['old-a look one', 'old-a look two', 'old-a look three', 'old-a answer', 'next prompt']) {
+ expect(count(merged, text), text).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/chat/transcript-receipt-turns.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.ts
index e4d88f59aaa..f431f26f8f6 100644
--- a/apps/desktop/src/app/chat/transcript-receipt-turns.ts
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.ts
@@ -112,3 +112,35 @@ export function dropReceiptTurnsThePageHolds(window: ChatMessage[], page: ChatMe
return dropped.size ? window.filter((_, index) => !dropped.has(index)) : window
}
+
+/**
+ * The newest page can start inside an older tool turn. History folds that turn
+ * under its first stored row, so the page's fold carries only the rows from
+ * the page boundary on, under a different first row than the window's fold of
+ * the same turn. Paired by first row, both folds rendered and the turn's later
+ * narration and answer appeared again under the whole turn (#141771). A page
+ * fold whose stored rows the window's fold all carries, while the window's
+ * fold also carries rows older than the page, is that turn cut by the page
+ * boundary: the window's whole fold stays and the page's partial copy goes.
+ */
+export function dropPageFoldsCutByTheBoundary(window: ChatMessage[], page: ChatMessage[]): ChatMessage[] {
+ const pageRows = page.flatMap(transcriptRowIds)
+
+ if (!pageRows.length) {
+ return page
+ }
+
+ const pageFirst = Math.min(...pageRows)
+
+ const wholeFolds = window
+ .map(message => new Set(transcriptRowIds(message)))
+ .filter(rows => [...rows].some(id => id < pageFirst))
+
+ const cut = (message: ChatMessage) => {
+ const rows = transcriptRowIds(message)
+
+ return rows.length > 0 && wholeFolds.some(whole => rows.every(id => whole.has(id)))
+ }
+
+ return page.some(cut) ? page.filter(message => !cut(message)) : page
+}