Skip to content

The agent that grows with you

fix(desktop): keep a populated active transcript through an empty refresh page

Calvin Ngc785dd8001d0
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
index 9adae150638..b869d233102 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
@@ -1137,3 +1137,80 @@ describe('isTypingBurstActive', () => {
expect(isTypingBurstActive(1_000_000 + 1_500)).toBe(false)
})
})
+
+describe('reconcileActiveTranscript with an empty persisted page', () => {
+ // A backend respawn (or a state.db read racing the change event) answers a
+ // `sessions.changed` refresh with zero rows. That page is not proof the
+ // transcript is empty; accepting it blanks the view, flips the routed
+ // thread into its loading branch and re-runs the composer lifecycle.
+ function populatedFixture() {
+ const fixture = makeRefresh()
+
+ fixture.state.messages = [
+ { id: 'user-1', parts: [{ text: 'question', type: 'text' }], role: 'user' },
+ { id: 'assistant-1', parts: [{ text: 'answer', type: 'text' }], role: 'assistant' }
+ ]
+ publishSessionState(ACTIVE_RUNTIME_ID, fixture.state)
+
+ return fixture
+ }
+
+ const emptyPage = () => ({ messages: [], session_id: ACTIVE_STORED_ID })
+
+ it('keeps a populated active transcript when the refresh reads zero rows', async () => {
+ const fixture = populatedFixture()
+ vi.mocked(getLatestSessionMessages).mockResolvedValue(emptyPage() as never)
+
+ await fixture.refresh()
+
+ expect(fixture.updateSessionState).not.toHaveBeenCalled()
+ expect(fixture.states.get(ACTIVE_RUNTIME_ID)?.messages.map(message => message.id)).toEqual([
+ 'user-1',
+ 'assistant-1'
+ ])
+ })
+
+ it('does not record the ignored empty page as accepted, so the next usable page still lands', async () => {
+ const fixture = populatedFixture()
+ vi.mocked(getLatestSessionMessages).mockResolvedValueOnce(emptyPage() as never)
+
+ await fixture.refresh()
+
+ vi.mocked(getLatestSessionMessages).mockResolvedValueOnce(transcript('a newer answer') as never)
+
+ await fixture.refresh()
+
+ const messages = fixture.states.get(ACTIVE_RUNTIME_ID)?.messages ?? []
+
+ expect(fixture.updateSessionState).toHaveBeenCalledTimes(1)
+ expect(messages.flatMap(message => message.parts.map(part => ('text' in part ? part.text : '')))).toContain(
+ 'a newer answer'
+ )
+ })
+
+ it('still accepts an empty page for a session whose transcript is genuinely empty', async () => {
+ const fixture = makeRefresh()
+ publishSessionState(ACTIVE_RUNTIME_ID, fixture.state)
+ vi.mocked(getLatestSessionMessages).mockResolvedValue(emptyPage() as never)
+
+ await fixture.refresh()
+
+ expect(fixture.updateSessionState).toHaveBeenCalledTimes(1)
+ expect(fixture.states.get(ACTIVE_RUNTIME_ID)?.messages).toEqual([])
+ })
+
+ it('does not let a runtime bound to another stored session veto the requested page', async () => {
+ const fixture = makeRefresh()
+
+ const foreign = createClientSessionState('stored-other', [
+ { id: 'other-user', parts: [{ text: 'elsewhere', type: 'text' }], role: 'user' }
+ ])
+
+ publishSessionState(ACTIVE_RUNTIME_ID, foreign)
+ vi.mocked(getLatestSessionMessages).mockResolvedValue(emptyPage() as never)
+
+ await fixture.refresh()
+
+ expect(fixture.updateSessionState).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
index 136867a96b5..c4877929b7f 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -126,6 +126,17 @@ function transcriptChangedDuringRead(before: ChatMessage[] | undefined, after: C
return false
}
+/** Zero persisted rows read over a populated runtime bound to the SAME stored
+ * session. A runtime rebound to another stored id while the read was in
+ * flight holds no evidence about the requested transcript. */
+function emptyPageOverPopulatedTranscript(
+ page: unknown[],
+ current: ClientSessionState | undefined,
+ storedSessionId: string
+): boolean {
+ return page.length === 0 && Boolean(current?.messages.length) && current?.storedSessionId === storedSessionId
+}
+
type TileTranscriptTarget = { ownerRoute?: SessionOwnerRoute; storedSessionId: string; runtimeId?: string }
/** Signature key per tile — carries the owner route so two connections/profiles
@@ -381,6 +392,16 @@ export async function reconcileActiveTranscript({
])
: `${stored.profile ?? 'default'}:${storedSessionId}`
+ // An empty page is not proof the transcript is empty — it is also what a
+ // respawning backend (or a state.db read racing the change event) returns.
+ // Publishing it over a populated view blanks the thread, trips the routed
+ // loading branch and re-runs the composer lifecycle. Leave the signature
+ // untouched so the next usable page is not deduped away. Same rule as the
+ // warm-activation guard in use-session-actions/index.ts.
+ if (emptyPageOverPopulatedTranscript(latest.messages, $sessionStates.get()[runtimeSessionId], storedSessionId)) {
+ return
+ }
+
const signature = sessionMessagesSignature(latest.messages)
if (signatureRef.current.get(signatureKey) === signature) {
diff --git a/apps/desktop/src/store/transcript-tail.test.ts b/apps/desktop/src/store/transcript-tail.test.ts
index 020361192f9..29ab08e6836 100644
--- a/apps/desktop/src/store/transcript-tail.test.ts
+++ b/apps/desktop/src/store/transcript-tail.test.ts
@@ -4,7 +4,8 @@ import {
$transcriptTailBySessionId,
clearTranscriptTailPaging,
recordTranscriptTail,
- rewindTranscriptTail
+ rewindTranscriptTail,
+ transcriptTailState
} from './transcript-tail'
const page = (count: number, limit = 10) =>
@@ -137,3 +138,28 @@ describe('rewindTranscriptTail', () => {
expect(rewindTranscriptTail('s1', 4)).toBe(false)
})
})
+
+describe('recordTranscriptTail with an empty page', () => {
+ beforeEach(() => {
+ clearTranscriptTailPaging()
+ })
+
+ // The REST helper records the tail before the active refresh decides whether
+ // the page is authoritative. A transient zero-row read must not turn a
+ // known-truncated tail into "nothing earlier to show".
+ it('keeps an existing truncated entry so "Show earlier" stays armed', () => {
+ recordTranscriptTail('s1', page(10))
+
+ recordTranscriptTail('s1', page(0))
+
+ expect(transcriptTailState('s1')).toMatchObject({ nextOffset: 10, possiblyTruncated: true })
+ })
+
+ it('still records an empty page when nothing was truncated before', () => {
+ recordTranscriptTail('s1', page(3))
+
+ recordTranscriptTail('s1', page(0))
+
+ expect(transcriptTailState('s1')).toMatchObject({ nextOffset: 0, possiblyTruncated: false })
+ })
+})
diff --git a/apps/desktop/src/store/transcript-tail.ts b/apps/desktop/src/store/transcript-tail.ts
index 22a059ee6c0..797b928d208 100644
--- a/apps/desktop/src/store/transcript-tail.ts
+++ b/apps/desktop/src/store/transcript-tail.ts
@@ -177,7 +177,17 @@ export function recordTranscriptTail(
return
}
- setTranscriptTailEntry(transcriptTailKey(storedSessionId, owner), tailStateFromPage(page, route))
+ const key = transcriptTailKey(storedSessionId, owner)
+
+ // This runs before the active refresh decides whether the page is
+ // authoritative (use-background-sync). A transient zero-row read must not
+ // turn a known-truncated tail into "nothing earlier", or "Show earlier"
+ // disarms while the rows are still on the backend.
+ if (page.messages.length === 0 && $transcriptTailBySessionId.get()[key]?.possiblyTruncated) {
+ return
+ }
+
+ setTranscriptTailEntry(key, tailStateFromPage(page, route))
}
/** Advance the bookkeeping after one older backfill page landed. */