Skip to content

The agent that grows with you

fix(desktop): preserve terminal session state ordering

fangliquanflq08ab3ec589b3
diff --git a/apps/desktop/src/app/contrib/hooks/live-status-spinner.test.ts b/apps/desktop/src/app/contrib/hooks/live-status-spinner.test.ts
index 4fcad3b1a2e..ac2ac23ae70 100644
--- a/apps/desktop/src/app/contrib/hooks/live-status-spinner.test.ts
+++ b/apps/desktop/src/app/contrib/hooks/live-status-spinner.test.ts
@@ -1,7 +1,13 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { createClientSessionState } from '@/lib/chat-runtime'
import { $selectedStoredSessionId, $unreadFinishedSessionIds } from '@/store/session'
-import { $workingSessionIds, clearAllSessionStates } from '@/store/session-states'
+import {
+ $sessionStates,
+ $workingSessionIds,
+ clearAllSessionStates,
+ publishSessionState
+} from '@/store/session-states'
import { rehydrateLiveSessionStatuses, resetLiveRuntimeTracking } from './use-background-sync'
@@ -48,6 +54,50 @@ describe('rehydrateLiveSessionStatuses — seeding a turn the renderer never saw
expect($workingSessionIds.get()).toContain('stored-cold')
})
+ it('ignores an idle snapshot captured before a live turn event', () => {
+ const stateAtRequest = $sessionStates.get()
+
+ publishSessionState('runtime-race', {
+ ...createClientSessionState('stored-race'),
+ busy: true,
+ sawAssistantPayload: true,
+ turnLive: true
+ })
+
+ rehydrateLiveSessionStatuses(
+ { sessions: [{ id: 'runtime-race', session_key: 'stored-race', status: 'idle' }] },
+ Date.now(),
+ 'default',
+ stateAtRequest
+ )
+
+ expect($workingSessionIds.get()).toContain('stored-race')
+ expect($unreadFinishedSessionIds.get()).not.toContain('stored-race')
+ })
+
+ it('ignores a working snapshot captured before the terminal event', () => {
+ const live = {
+ ...createClientSessionState('stored-race'),
+ busy: true,
+ sawAssistantPayload: true,
+ turnLive: true
+ }
+
+ publishSessionState('runtime-race', live)
+ const stateAtRequest = $sessionStates.get()
+ publishSessionState('runtime-race', { ...live, busy: false, turnLive: false })
+
+ rehydrateLiveSessionStatuses(
+ { sessions: [{ id: 'runtime-race', session_key: 'stored-race', status: 'working' }] },
+ Date.now(),
+ 'default',
+ stateAtRequest
+ )
+
+ expect($workingSessionIds.get()).not.toContain('stored-race')
+ expect($unreadFinishedSessionIds.get()).toContain('stored-race')
+ })
+
it('shows the spinner when a runtime id is recycled onto a new stored session', () => {
// A respawned backend can mint the same runtime id for a different stored
// session. The row for the NEW stored id must light up, not the stale one.
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 4523e1664f3..e7e04b8cfb5 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -399,7 +399,8 @@ export function resetTypingActivityTracking(): void {
export function rehydrateLiveSessionStatuses(
response: LiveSessionStatusResponse,
nowMs = Date.now(),
- profileKey = 'default'
+ profileKey = 'default',
+ stateAtRequest = $sessionStates.get()
): void {
const seen = new Set<string>()
@@ -417,6 +418,14 @@ export function rehydrateLiveSessionStatuses(
const existing = $sessionStates.get()[runtimeSessionId]
+ // The active-list response is an async snapshot. Stream events can start
+ // or finish this turn after the request begins but before its response is
+ // applied. In that case the event state is newer: an old idle row must not
+ // finish a live turn, and an old working row must not revive a terminal one.
+ if (existing !== stateAtRequest[runtimeSessionId]) {
+ continue
+ }
+
// A turn we just submitted is not yet running as far as the backend is
// concerned, so the snapshot honestly reports it idle — but the local
// stream is already waiting on its first token, and it is the newer
@@ -475,6 +484,10 @@ export function rehydrateLiveSessionStatuses(
const existing = $sessionStates.get()[runtimeSessionId]
+ if (existing !== stateAtRequest[runtimeSessionId]) {
+ continue
+ }
+
if (existing?.busy || existing?.needsInput || existing?.awaitingResponse) {
publishSessionState(runtimeSessionId, {
...existing,
@@ -716,12 +729,13 @@ export function useBackgroundSync({
}
inFlight = true
+ const stateAtRequest = $sessionStates.get()
try {
const response = await requestGateway<LiveSessionStatusResponse>('session.active_list', {})
if (!cancelled) {
- rehydrateLiveSessionStatuses(response, Date.now(), activeGatewayProfile)
+ rehydrateLiveSessionStatuses(response, Date.now(), activeGatewayProfile, stateAtRequest)
}
} catch {
// Older gateways may not expose session.active_list. Live stream events