Skip to content

The agent that grows with you

fix(desktop): sort an overlapping history page by stored id

josephsellers454d1b6d0713
diff --git a/apps/desktop/src/app/chat/transcript-backfill.test.ts b/apps/desktop/src/app/chat/transcript-backfill.test.ts
index 0317dcb65bb..6dfee1021f2 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.test.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.test.ts
@@ -177,6 +177,60 @@ describe('graftRefreshedTailOntoBackfill', () => {
const refreshed = [chat('p', 200), chat('q', 201)]
expect(graftRefreshedTailOntoBackfill(refreshed, previous)).toBe(refreshed)
+ expect(graftRefreshedTailOntoBackfill(refreshed, previous).map(message => message.rowId)).toEqual([200, 201])
+ })
+
+ it('puts earlier stored ids in front and later ones at the tail', () => {
+ const previous = [chat('tail-a', 700), chat('tail-b', 701), chat('tail-c', 746)]
+
+ const refreshed = [
+ chat('kcsie', 662),
+ chat('tail-a', 700),
+ chat('tail-b', 701),
+ chat('tail-c', 746),
+ chat('next', 747)
+ ]
+
+ expect(graftRefreshedTailOntoBackfill(refreshed, previous).map(message => message.rowId)).toEqual([
+ 662, 700, 701, 746, 747
+ ])
+ })
+
+ it('moves an older row that was glued on after the tail back to stored order', () => {
+ const previous = [chat('tail-a', 700), chat('tail-b', 701), chat('tail-c', 746), chat('kcsie', 662)]
+ const refreshed = [chat('kcsie', 662), chat('tail-a', 700), chat('tail-b', 701), chat('tail-c', 746)]
+
+ expect(graftRefreshedTailOntoBackfill(refreshed, previous).map(message => message.rowId)).toEqual([
+ 662, 700, 701, 746
+ ])
+ })
+
+ it('replaces the window when the page covers every on-screen row', () => {
+ const previous = [chat('tail-a', 700), chat('tail-b', 701)]
+ const refreshed = [chat('kcsie', 662), chat('tail-a', 700), chat('tail-b', 701)]
+
+ expect(graftRefreshedTailOntoBackfill(refreshed, previous)).toBe(refreshed)
+ })
+
+ it('keeps a live row the page does not cover at the end of the stored order', () => {
+ const previous = [chat('tail-c', 746), chat('live'), chat('kcsie', 662)]
+ const refreshed = [chat('kcsie', 662), chat('tail-c', 746)]
+
+ expect(graftRefreshedTailOntoBackfill(refreshed, previous).map(message => message.id)).toEqual([
+ 'kcsie',
+ 'tail-c',
+ 'live'
+ ])
+ })
+
+ it('keeps the fresh page copy when the same stored id is on both sides', () => {
+ const previous = [chat('tail-a', 700), chat('stale-b', 701), chat('tail-c', 746)]
+ const refreshed = [chat('kcsie', 662), chat('fresh-b', 701), chat('tail-c', 746)]
+
+ const merged = graftRefreshedTailOntoBackfill(refreshed, previous)
+
+ expect(merged.map(message => message.rowId)).toEqual([662, 700, 701, 746])
+ expect(merged.find(message => message.rowId === 701)).toBe(refreshed[1])
})
it('keeps the earlier transcript when a page-local fold precedes a shared durable row', () => {
diff --git a/apps/desktop/src/app/chat/transcript-backfill.ts b/apps/desktop/src/app/chat/transcript-backfill.ts
index 9c1d97f78a5..91bc70ec5f1 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.ts
@@ -112,10 +112,68 @@ export function mergeOlderTranscriptPage(existing: ChatMessage[], olderPage: Cha
* pages. Background refreshes and post-turn rehydrates re-read only the
* newest page; replacing the store with that page outright would silently
* drop everything "Show earlier" already loaded. Find where the refreshed
- * tail begins inside the previous transcript and keep the older prefix.
- * When no anchor is found (compaction rewrite, different session), the
- * refreshed tail is authoritative — same behavior as before backfill existed.
+ * tail begins inside the previous transcript and keep the older prefix when
+ * that prefix is actually earlier. An anchor on the first on-screen row is a
+ * real match: the page replaces the window from there. A page that already
+ * contains every on-screen row replaces the window. When the page overlaps
+ * the screen but that splice would put an older stored id after a newer one,
+ * merge by stored id. The fresh page wins where both sides share an id, and
+ * a row with no stored id stays at the end. A page that shares no stored id
+ * is the transcript now on screen — a compaction rewrite or a different
+ * session — and replaces the window.
*/
+function pageCoversWindow(previous: ChatMessage[], refreshedIds: Set<string>, refreshedRowIds: Set<number>): boolean {
+ return previous.every(
+ message => (message.rowId !== undefined && refreshedRowIds.has(message.rowId)) || refreshedIds.has(message.id)
+ )
+}
+
+/** 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)
+ }
+ }
+
+ let sharesStoredId = false
+
+ for (const message of refreshedTail) {
+ if (message.rowId !== undefined && windowRowIds.has(message.rowId)) {
+ sharesStoredId = true
+
+ break
+ }
+ }
+
+ // Compaction, rewind, or a different session arrives as new stored ids.
+ // This function is the path that puts that page on screen.
+ if (!sharesStoredId) {
+ return refreshedTail
+ }
+
+ const byRowId = new Map<number, ChatMessage>()
+
+ for (const message of previous) {
+ if (message.rowId !== undefined && !byRowId.has(message.rowId)) {
+ byRowId.set(message.rowId, message)
+ }
+ }
+
+ for (const message of refreshedTail) {
+ if (message.rowId !== undefined) {
+ byRowId.set(message.rowId, message)
+ }
+ }
+
+ const stored = [...byRowId.entries()].sort((left, right) => left[0] - right[0]).map(([, message]) => message)
+ const unstored = previous.filter(message => message.rowId === undefined)
+
+ return unstored.length === 0 ? stored : [...stored, ...unstored]
+}
+
export function graftRefreshedTailOntoBackfill(refreshedTail: ChatMessage[], previous: ChatMessage[]): ChatMessage[] {
if (refreshedTail.length === 0 || previous.length === 0) {
return refreshedTail
@@ -127,13 +185,39 @@ export function graftRefreshedTailOntoBackfill(refreshedTail: ChatMessage[], pre
refreshedTail.flatMap(message => (message.rowId === undefined ? [] : [message.rowId]))
)
- const anchor = previous.findIndex(message => message.rowId !== undefined && refreshedRowIds.has(message.rowId))
+ const firstDurable = refreshedTail.find(message => message.rowId !== undefined)
+
+ const anchor =
+ firstDurable === undefined ? -1 : previous.findIndex(message => message.rowId === firstDurable.rowId)
+
+ const anchorRowId = firstDurable?.rowId
+
+ // A hit on the first row, or a hit after a prefix whose stored ids are all
+ // earlier, is the backfill anchor. A hit further down on a row that was
+ // glued on late is not: the prefix is newer than the match.
+ const prefixIsEarlier =
+ anchor > 0 &&
+ anchorRowId !== undefined &&
+ previous.slice(0, anchor).every(message => message.rowId === undefined || message.rowId < anchorRowId)
+
+ if (anchor === 0) {
+ return refreshedTail
+ }
+
+ if (prefixIsEarlier) {
+ return [...previous.slice(0, anchor), ...refreshedTail]
+ }
+
+ const refreshedIds = new Set(refreshedTail.map(message => message.id))
- if (anchor <= 0) {
+ // The page already contains everything on screen, including a live row the
+ // tail really did cover. Take the page. This is what keeps a finished reply
+ // through a long tool turn.
+ if (pageCoversWindow(previous, refreshedIds, refreshedRowIds)) {
return refreshedTail
}
- return [...previous.slice(0, anchor), ...refreshedTail]
+ return mergeOverlappingTail(previous, refreshedTail)
}
const REFRESH_OVERLAP_PAGE_LIMIT = 4