Skip to content

The agent that grows with you

fix(desktop): keep unstored rows anchored in the stored-id refresh merge

kshitijk4poor96ea97d422e8
diff --git a/apps/desktop/src/app/chat/transcript-backfill.test.ts b/apps/desktop/src/app/chat/transcript-backfill.test.ts
index 8dd70a7804c..c648f868bff 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.test.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.test.ts
@@ -186,6 +186,21 @@ describe('graftRefreshedTailOntoBackfill', () => {
expect(graftRefreshedTailOntoBackfill(refreshed, previous).map(message => message.rowId)).toEqual([
662, 700, 701, 746
])
+
+ // A page that starts mid-turn opens with a tool fold that has no stored
+ // id. It stays in front of the stored row it preceded.
+ const behindEarlier = [chat('earlier', 500), ...previous]
+ const withFold = [chat('fold-tools'), ...refreshed, chat('next', 747)]
+
+ expect(graftRefreshedTailOntoBackfill(withFold, behindEarlier).map(message => message.id)).toEqual([
+ 'earlier',
+ 'fold-tools',
+ 'kcsie',
+ 'tail-a',
+ 'tail-b',
+ 'tail-c',
+ 'next'
+ ])
})
it('keeps the fresh page copy when the same stored id is on both sides', () => {
diff --git a/apps/desktop/src/app/chat/transcript-backfill.ts b/apps/desktop/src/app/chat/transcript-backfill.ts
index 91bc70ec5f1..f11418cb24c 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.ts
@@ -128,50 +128,77 @@ function pageCoversWindow(previous: ChatMessage[], refreshedIds: Set<string>, re
)
}
-/** Stored-id merge for a page that overlaps the window but does not anchor in front of it. */
-function mergeOverlappingTail(previous: ChatMessage[], refreshedTail: ChatMessage[]): ChatMessage[] {
- const windowRowIds = new Set<number>()
-
- for (const message of previous) {
- if (message.rowId !== undefined) {
- windowRowIds.add(message.rowId)
- }
- }
+function durableRowIds(messages: ChatMessage[]): Set<number> {
+ return new Set(messages.flatMap(message => (message.rowId === undefined ? [] : [message.rowId])))
+}
- let sharesStoredId = false
+function sharesDurableRow(first: ChatMessage[], second: ChatMessage[]): boolean {
+ const rowIds = durableRowIds(first)
- for (const message of refreshedTail) {
- if (message.rowId !== undefined && windowRowIds.has(message.rowId)) {
- sharesStoredId = true
+ return second.some(message => message.rowId !== undefined && rowIds.has(message.rowId))
+}
- break
- }
- }
+interface StoredRowSlot {
+ message: ChatMessage
+ /** Rows without a stored id (e.g. a page-local tool fold) that precede this row. */
+ leading: ChatMessage[]
+}
+/**
+ * Stored-id merge for a page that overlaps the window but does not anchor in
+ * front of it. A row with no stored id travels with the next stored row after
+ * it, so a page-local fold stays in front of the row it preceded. Rows with no
+ * stored id after the last stored row stay at the end (page first, then live).
+ */
+function mergeOverlappingTail(previous: ChatMessage[], refreshedTail: ChatMessage[]): ChatMessage[] {
// Compaction, rewind, or a different session arrives as new stored ids.
// This function is the path that puts that page on screen.
- if (!sharesStoredId) {
+ if (!sharesDurableRow(previous, refreshedTail)) {
return refreshedTail
}
- const byRowId = new Map<number, ChatMessage>()
+ const refreshedIds = new Set(refreshedTail.map(message => message.id))
+ const byRowId = new Map<number, StoredRowSlot>()
- for (const message of previous) {
- if (message.rowId !== undefined && !byRowId.has(message.rowId)) {
- byRowId.set(message.rowId, message)
- }
- }
+ const place = (messages: ChatMessage[], fresh: boolean): ChatMessage[] => {
+ let pending: ChatMessage[] = []
- for (const message of refreshedTail) {
- if (message.rowId !== undefined) {
- byRowId.set(message.rowId, message)
+ for (const message of messages) {
+ if (message.rowId === undefined) {
+ // The fresh page's copy of an unstored row wins over the window's.
+ if (fresh || !refreshedIds.has(message.id)) {
+ pending.push(message)
+ }
+
+ continue
+ }
+
+ const existing = byRowId.get(message.rowId)
+
+ if (!fresh && existing) {
+ pending = []
+
+ continue
+ }
+
+ // The fresh page replaces the row; keep the window's leading rows when
+ // the page brought none of its own for it.
+ const leading = fresh && existing && pending.length === 0 ? existing.leading : pending
+ byRowId.set(message.rowId, { message, leading })
+ pending = []
}
+
+ return pending
}
- const stored = [...byRowId.entries()].sort((left, right) => left[0] - right[0]).map(([, message]) => message)
- const unstored = previous.filter(message => message.rowId === undefined)
+ const previousTrailing = place(previous, false)
+ const refreshedTrailing = place(refreshedTail, true)
+
+ const stored = [...byRowId.entries()]
+ .sort((left, right) => left[0] - right[0])
+ .flatMap(([, { leading, message }]) => [...leading, message])
- return unstored.length === 0 ? stored : [...stored, ...unstored]
+ return [...stored, ...refreshedTrailing, ...previousTrailing]
}
export function graftRefreshedTailOntoBackfill(refreshedTail: ChatMessage[], previous: ChatMessage[]): ChatMessage[] {
@@ -181,9 +208,7 @@ export function graftRefreshedTailOntoBackfill(refreshedTail: ChatMessage[], pre
// The first rendered message can be a page-local tool fold whose id is not
// durable. Anchor on the first shared persisted row anywhere in the page.
- const refreshedRowIds = new Set(
- refreshedTail.flatMap(message => (message.rowId === undefined ? [] : [message.rowId]))
- )
+ const refreshedRowIds = durableRowIds(refreshedTail)
const firstDurable = refreshedTail.find(message => message.rowId !== undefined)
@@ -222,12 +247,6 @@ export function graftRefreshedTailOntoBackfill(refreshedTail: ChatMessage[], pre
const REFRESH_OVERLAP_PAGE_LIMIT = 4
-function sharesDurableRow(first: ChatMessage[], second: ChatMessage[]): boolean {
- const rowIds = new Set(first.flatMap(message => (message.rowId === undefined ? [] : [message.rowId])))
-
- return second.some(message => message.rowId !== undefined && rowIds.has(message.rowId))
-}
-
/**
* A refresh begins at the newest persisted row. A tool-heavy turn can fill
* that page entirely, putting its first durable row after the rendered