Skip to content

The agent that grows with you

fix(desktop): hydrate transcript after reconnect attach

Flownium19251ac9e3b3
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
index 8924d8ed9aa..34140037486 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
@@ -2619,6 +2619,93 @@ describe('resumeSession warm-cache mapping integrity', () => {
expect($clarifyRequests.get()['rt-A']).toMatchObject({ requestId: 'req-newer' })
})
+ it('reads the terminal transcript after warm reconnect transport reattachment', async () => {
+ const runtimeIdByStoredSessionIdRef: MutableRefObject<Map<string, string>> = {
+ current: new Map([['stored-A', 'rt-A']])
+ }
+
+ const state = clientState('stored-A')
+ state.messages = [
+ {
+ id: 'cached-user',
+ role: 'user',
+ parts: [{ type: 'text', text: 'long running prompt' }]
+ },
+ {
+ id: 'cached-assistant',
+ role: 'assistant',
+ parts: [{ type: 'text', text: 'partial before disconnect' }],
+ pending: true
+ }
+ ]
+
+ const sessionStateByRuntimeIdRef: MutableRefObject<Map<string, ClientSessionState>> = {
+ current: new Map([['rt-A', state]])
+ }
+
+ let transportAttached = false
+ let attachedWhenTranscriptRead: boolean | null = null
+
+ vi.mocked(getLatestSessionMessages).mockReset()
+ vi.mocked(getLatestSessionMessages).mockImplementation(async () => {
+ attachedWhenTranscriptRead = transportAttached
+
+ return {
+ messages: [
+ { content: 'long running prompt', role: 'user', timestamp: 1 },
+ {
+ content: transportAttached ? 'complete answer persisted during disconnect' : 'partial before disconnect',
+ role: 'assistant',
+ timestamp: 2
+ }
+ ],
+ session_id: 'stored-A'
+ } as never
+ })
+
+ const requestGateway = vi.fn(async (method: string) => {
+ if (method === 'session.activate') {
+ // The backend rebinds the session transport before returning this
+ // terminal snapshot. A transcript read issued earlier can miss the
+ // final persisted row and no live event will arrive to repair it.
+ transportAttached = true
+
+ return {
+ session_id: 'rt-A',
+ session_key: 'stored-A',
+ resumed: 'stored-A',
+ message_count: 2,
+ messages: [],
+ messages_omitted: true,
+ running: false,
+ info: {}
+ } as never
+ }
+
+ return {} as never
+ })
+
+ let resumedState: ClientSessionState | undefined
+ let resume: ((storedSessionId: string, replaceRoute?: boolean) => Promise<unknown>) | null = null
+
+ render(
+ <ResumeHarness
+ onReady={ready => (resume = ready)}
+ onStateUpdate={(_sessionId, next) => (resumedState = next)}
+ requestGateway={requestGateway}
+ runtimeIdByStoredSessionIdRef={runtimeIdByStoredSessionIdRef}
+ sessionStateByRuntimeIdRef={sessionStateByRuntimeIdRef}
+ />
+ )
+ await waitFor(() => expect(resume).not.toBeNull())
+ await resume!('stored-A', true)
+
+ expect(attachedWhenTranscriptRead).toBe(true)
+ expect(getLatestSessionMessages).toHaveBeenCalledTimes(1)
+ expect(JSON.stringify(resumedState?.messages)).toContain('complete answer persisted during disconnect')
+ expect(JSON.stringify(resumedState?.messages)).not.toContain('partial before disconnect')
+ })
+
it('preserves cached image attachments through an idle persisted transcript refresh', async () => {
const runtimeIdByStoredSessionIdRef: MutableRefObject<Map<string, string>> = {
current: new Map([['stored-A', 'rt-A']])
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index 7b1da935491..51867499709 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -905,16 +905,14 @@ export function useSessionActions({
sessionStateByRuntimeIdRef.current.delete(cachedRuntimeId)
dropSessionState(cachedRuntimeId)
} else {
- // Paint the warm cache immediately, but also refresh the persisted
- // transcript in parallel. A resumed runtime carries the agent's
- // compression projection, which can have the same row count as the
- // stored conversation while containing different rows. Trusting that
- // projection alone made completed prompts disappear after an app
- // restart whenever this warm path short-circuited the cold REST
- // prefetch. Watch mirrors stay live-only by design.
- const persistedTranscriptPromise = isWatchWindow()
- ? null
- : getLatestSessionMessages(storedSessionId, sessionRestScope).catch(() => null)
+ // Paint the warm cache immediately. The persisted transcript still
+ // needs a refresh because a resumed runtime may carry only the
+ // agent's compressed projection, but that read must start after
+ // session.activate reattaches the live transport. Otherwise a turn
+ // can finish between the early REST snapshot and the reattach: its
+ // terminal events go to the detached socket while the stale snapshot
+ // leaves Desktop showing only the pre-disconnect partial answer.
+ const shouldRefreshPersistedTranscript = !isWatchWindow()
setFreshDraftReady(false)
clearNotifications()
@@ -975,6 +973,16 @@ export function useSessionActions({
sessionStateByRuntimeIdRef.current.delete(cachedRuntimeId)
dropSessionState(cachedRuntimeId)
} else {
+ // session.activate is the ordering barrier for reconnect recovery:
+ // it atomically rebinds a running turn before returning. If the
+ // turn is already terminal, this post-barrier REST read sees its
+ // durable final row; if it is still running, later deltas/finish
+ // events arrive on the newly attached transport and the
+ // concurrent-overlay step below preserves them.
+ const persistedTranscriptPromise = shouldRefreshPersistedTranscript
+ ? getLatestSessionMessages(storedSessionId, sessionRestScope).catch(() => null)
+ : null
+
const pendingApproval = restorePendingApproval(activated, cachedRuntimeId)
const pendingClarifyState = restorePendingClarifyFromSnapshot(