The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): rewind the older-page offset in the backend's own units
kshitijk4poor7c799c26ce2e
diff --git a/apps/desktop/src/app/chat/transcript-retention.test.ts b/apps/desktop/src/app/chat/transcript-retention.test.tsindex 3a423105b48..79bd3fe10db 100644--- a/apps/desktop/src/app/chat/transcript-retention.test.ts+++ b/apps/desktop/src/app/chat/transcript-retention.test.ts@@ -57,7 +57,6 @@ describe('boundRetainedTranscript', () => {expect(retention.releasedRows).toBe(40 - keep)expect(retention.messages[0].id).toBe(`m${40 - keep}`)- expect(retention.retainedPersistedRows).toBe(messages.length - retention.releasedRows)expect(retention.messages).toHaveLength(messages.length - retention.releasedRows)})@@ -116,9 +115,10 @@ describe('boundRetainedTranscript', () => {}})- it('releases nothing when the retained rows carry no durable id', () => {- // Offset bookkeeping is measured from persisted rows; without one in the- // retained slice there is nothing to re-fetch against.+ it('releases nothing when a row older than the window cannot be fetched back', () => {+ // Rows 0-19 were never persisted. The older-page fetch is a row offset from+ // the newest, so a prefix holding an unpersisted row cannot be re-fetched in+ // full — the transcript is left whole rather than released in part.const messages = transcript(30, index => row(index, { persisted: index >= 20, textUnits: 100 }))expect(untouched(messages, messages[25].id)).toEqual({ released: false })diff --git a/apps/desktop/src/app/chat/transcript-retention.ts b/apps/desktop/src/app/chat/transcript-retention.tsindex 0420ebaec52..2fd14053cf7 100644--- a/apps/desktop/src/app/chat/transcript-retention.ts+++ b/apps/desktop/src/app/chat/transcript-retention.ts@@ -52,29 +52,15 @@ export type TranscriptRetention =released: true/** Transcript to keep in the store; everything before it was released. */messages: ChatMessage[]- /** Rows released this pass. */+ /** Rows released this pass. Every one of them is persisted, so the+ * caller can subtract them from the session's older-page offset — the+ * offset the backend itself reported, which the release must stay in the+ * currency of (the backend pages display history, not raw store rows). */releasedRows: number- /** Persisted rows still retained, counted from the newest. This is the- * `transcript-tail` offset the next older REST page starts at — the- * backend pages by persisted rows, so it is counted in the same- * currency. */- retainedPersistedRows: number}const NOTHING_RELEASED: TranscriptRetention = { released: false }-function persistedRowCount(messages: readonly ChatMessage[]): number {- let count = 0-- for (const message of messages) {- if (message.rowId !== undefined) {- count += 1- }- }-- return count-}-/*** How much of `messages` the store must keep, given the live window's first* message. Pure: the caller owns applying it (store write + tail rewind).@@ -122,13 +108,6 @@ export function boundRetainedTranscript(}const retained = messages.slice(boundary)- const retainedPersistedRows = persistedRowCount(retained)-- // Offset bookkeeping is measured from persisted rows. Without one in the- // retained slice there is no anchor to re-fetch against — keep everything.- if (retainedPersistedRows === 0) {- return NOTHING_RELEASED- }- return { messages: retained, releasedRows: boundary, released: true, retainedPersistedRows }+ return { messages: retained, releasedRows: boundary, released: true }}diff --git a/apps/desktop/src/app/chat/use-transcript-retention.test.tsx b/apps/desktop/src/app/chat/use-transcript-retention.test.tsxindex 6ebb70e93ae..e81305f7922 100644--- a/apps/desktop/src/app/chat/use-transcript-retention.test.tsx+++ b/apps/desktop/src/app/chat/use-transcript-retention.test.tsx@@ -111,13 +111,14 @@ describe('useTranscriptRetention — the store releases paged-through history',// The window cuts the transcript; everything older than it plus one page of// slack is released, and the released rows stay reachable over REST.const released = 60 - state.messages.length- // Where the rewind points the next older page: the retained prefix.- const rewoundOffset = state.messages.length+ // Where the rewind points the next older page: the hydrated offset minus the+ // rows the store gave up.+ const rewoundOffset = HYDRATED_ROWS - releasedexpect(released).toBeGreaterThan(1)expect($messages.get()).toHaveLength(state.messages.length)expect($transcriptTailBySessionId.get().stored).toMatchObject({- nextOffset: state.messages.length,+ nextOffset: rewoundOffset,possiblyTruncated: true})@@ -193,7 +194,7 @@ describe('useTranscriptRetention — direct', () => {vi.mocked(sessionTileDelegate).mockReturnValue({ updateSession } as never)recordTranscriptTail('stored', {- messages: [],+ messages: Array.from({ length: HYDRATED_ROWS }, (_, index) => ({ id: index, role: 'user', content: 'tail' })),pagination: { limit: HYDRATED_ROWS, offset: 0, order: 'latest', returned: HYDRATED_ROWS }})@@ -207,7 +208,7 @@ describe('useTranscriptRetention — direct', () => {expect(updateSession).toHaveBeenCalledTimes(1)expect(state.messages.length).toBeLessThan(60)expect($transcriptTailBySessionId.get().stored).toMatchObject({- nextOffset: state.messages.length,+ nextOffset: HYDRATED_ROWS - (60 - state.messages.length),possiblyTruncated: true})})diff --git a/apps/desktop/src/app/chat/use-transcript-retention.ts b/apps/desktop/src/app/chat/use-transcript-retention.tsindex 352bb402937..8dc2ebd7f72 100644--- a/apps/desktop/src/app/chat/use-transcript-retention.ts+++ b/apps/desktop/src/app/chat/use-transcript-retention.ts@@ -64,7 +64,7 @@ export function useTranscriptRetention({}// Nothing may be released until the rows are fetchable again.- if (!rewindTranscriptTail(storedSessionId, retention.retainedPersistedRows, profile)) {+ if (!rewindTranscriptTail(storedSessionId, retention.releasedRows, profile)) {return state}diff --git a/apps/desktop/src/store/transcript-tail.test.ts b/apps/desktop/src/store/transcript-tail.test.tsindex 9432792ae1b..8f9946b9f0d 100644--- a/apps/desktop/src/store/transcript-tail.test.ts+++ b/apps/desktop/src/store/transcript-tail.test.ts@@ -73,11 +73,13 @@ describe('rewindTranscriptTail', () => {$transcriptTailBySessionId.set({})})- it('points the next older page at the retained prefix', () => {+ it('decrements the recorded offset by the rows the store released', () => {+ // page(10) records nextOffset 10; releasing 4 of those rows leaves the next+ // older page starting 4 rows earlier, in the backend's own units.recordTranscriptTail('s1', page(10))expect(rewindTranscriptTail('s1', 4)).toBe(true)- expect($transcriptTailBySessionId.get().s1).toMatchObject({ nextOffset: 4, possiblyTruncated: true })+ expect($transcriptTailBySessionId.get().s1).toMatchObject({ nextOffset: 6, possiblyTruncated: true })})it('keeps a rewind on the same route the tail was hydrated with', () => {@@ -87,10 +89,24 @@ describe('rewindTranscriptTail', () => {const entry = $transcriptTailBySessionId.get()[JSON.stringify(['c1', 'work', 's1'])]- expect(entry).toMatchObject({ nextOffset: 4, possiblyTruncated: true })+ expect(entry).toMatchObject({ nextOffset: 6, possiblyTruncated: true })expect(entry.profile).toEqual({ connectionId: 'c1', profile: 'work' })})+ it('never rewinds past the start of the transcript', () => {+ recordTranscriptTail('s1', page(3))++ expect(rewindTranscriptTail('s1', 9)).toBe(true)+ expect($transcriptTailBySessionId.get().s1).toMatchObject({ nextOffset: 0, possiblyTruncated: true })+ })++ it('refuses a rewind that would release nothing', () => {+ recordTranscriptTail('s1', page(10))++ expect(rewindTranscriptTail('s1', 0)).toBe(false)+ expect($transcriptTailBySessionId.get().s1).toMatchObject({ nextOffset: 10 })+ })+it('refuses to rewind a session with no recorded page route', () => {// No entry means no route to fetch a page from: the caller must keep its// rows rather than release history nothing can bring back.diff --git a/apps/desktop/src/store/transcript-tail.ts b/apps/desktop/src/store/transcript-tail.tsindex f1674bd6f66..39559ccd0ae 100644--- a/apps/desktop/src/store/transcript-tail.ts+++ b/apps/desktop/src/store/transcript-tail.ts@@ -200,9 +200,17 @@ export function recordTranscriptBackfillPage(** `boundRetainedTranscript` (app/chat/transcript-retention) drops rows that are* older than the live window once they are persisted — but they stay reachable- * only while the transcript still reports older rows as fetchable. Point the- * entry at the retained prefix so the next "Show earlier" fetches them back- * instead of treating the in-memory store as the whole transcript.+ * only while the transcript still reports older rows as fetchable. Rewind the+ * entry's offset by the released rows so the next "Show earlier" fetches them+ * back instead of treating the in-memory store as the whole transcript.+ *+ * The offset is decremented RELATIVE to what the backend reported, never+ * recomputed from the store's own row count: the backend pages display history+ * (`include_compacted` reads are grouped by display order, and inactive rows are+ * not counted), so a locally counted row total is not guaranteed to be the same+ * unit. Subtracting from the backend's own number can only overlap a page that+ * is already in memory — which the merge dedupes — where an over-counted+ * absolute offset would skip rows the reader can then never reach.** Returns false when the session has no entry: without one there is no route* recorded to fetch a page from, so the caller must keep its rows rather than@@ -210,10 +218,10 @@ export function recordTranscriptBackfillPage(*/export function rewindTranscriptTail(storedSessionId: string,- retainedRows: number,+ releasedRows: number,profile?: TranscriptProfileScope): boolean {- if (!storedSessionId || retainedRows < 0) {+ if (!storedSessionId || releasedRows <= 0) {return false}@@ -224,7 +232,7 @@ export function rewindTranscriptTail(}setTranscriptTailEntry(entry[0], {- nextOffset: retainedRows,+ nextOffset: Math.max(0, entry[1].nextOffset - releasedRows),possiblyTruncated: true,profile: entry[1].profile})