Skip to content

The agent that grows with you

fix(desktop): hold a reconnect re-resume behind its runtime's replay

teknium11a0b6ff786ae
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 3feafb7f131..f43d4248791 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
@@ -1016,13 +1016,29 @@ export function useSessionActions({
const routeToken = getRouteToken()
resumeRequestRef.current = requestId
const resumedSameSelectedSession = selectedStoredSessionIdRef.current === storedSessionId
- const resumeStartMessages = resumedSameSelectedSession ? $messages.get() : []
const isCurrentResume = () =>
resumeRequestRef.current === requestId &&
selectedStoredSessionIdRef.current === storedSessionId &&
getRouteToken() === routeToken
+ // A reconnect re-resumes the runtime this view is streaming. Let its
+ // replay land while that runtime still owns the view. Otherwise the REST
+ // read paints the finished turn first and the replayed rows are then
+ // overlaid onto it as concurrent runtime changes: the turn shows twice.
+ const viewRuntimeId = resumedSameSelectedSession ? activeSessionIdRef.current : null
+ const viewReplay = viewRuntimeId ? pendingSessionReplay(viewRuntimeId) : undefined
+
+ if (viewReplay) {
+ await viewReplay
+
+ if (!isCurrentResume()) {
+ return
+ }
+ }
+
+ const resumeStartMessages = resumedSameSelectedSession ? $messages.get() : []
+
// Paint the click before the profile-resolve / gateway-swap awaits below,
// so there's zero dead air: highlight the row instantly (the sidebar reads
// $selectedStoredSessionId) and, for a cold target, drop the previous
diff --git a/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx b/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx
index b348449d2ce..c54fd479a75 100644
--- a/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx
+++ b/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx
@@ -10,6 +10,7 @@ import { resetInFlightTurnJournalStateForTests } from '@/lib/inflight-turn-journ
import { setPrimaryGateway } from '@/store/gateway'
import { $activeGatewayProfile } from '@/store/profile'
import {
+ $messages,
_resetSessionOwnerHintsForTests,
setActiveSessionId,
setAwaitingResponse,
@@ -278,3 +279,40 @@ it('still settles a warm resume through session.activate when its replay socket
client.close()
}
})
+
+it('holds a reconnect re-resume of the selected session behind its replay (cold path)', async () => {
+ const { result, client, requestGateway, second, request } = await mountWithPendingReplay()
+ const { activeSessionIdRef, runtimeIdByStoredSessionIdRef, selectedStoredSessionIdRef } = result.current.cache
+
+ // The view is streaming this runtime, but the warm mapping is gone, so the
+ // route's reconnect re-resume takes the cold REST + session.resume path.
+ selectedStoredSessionIdRef.current = storedId
+ activeSessionIdRef.current = runtimeId
+ runtimeIdByStoredSessionIdRef.current.delete(storedId)
+
+ try {
+ let pending!: Promise<void>
+ await act(async () => {
+ pending = result.current.actions.resumeSession(storedId, true)
+ })
+ expect(getLatestSessionMessages).not.toHaveBeenCalled()
+ expect(requestGateway).not.toHaveBeenCalled()
+
+ await act(async () => {
+ second.frame({ id: request.id, jsonrpc: '2.0', result: { events: replay } })
+ await pending
+ })
+
+ expect(getLatestSessionMessages).toHaveBeenCalledTimes(1)
+ expect(requestGateway.mock.calls.some(([method]) => method === 'session.resume')).toBe(true)
+ expect(
+ $messages
+ .get()
+ .map(chatMessageText)
+ .join('\n')
+ .match(/Finished result\./g)
+ ).toHaveLength(1)
+ } finally {
+ client.close()
+ }
+})