The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): prevent background session rotation from stealing foreground route (#86106)
Christopherced5f36b2a4c
diff --git a/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx b/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsxindex 72f7ac31bca..38d08ab1c2c 100644--- a/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx+++ b/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx@@ -19,6 +19,7 @@ import {setCurrentProvider,setCurrentReasoningEffort,setCurrentServiceTier,+ setSelectedStoredSessionId,setTurnStartedAt} from '@/store/session'import {@@ -44,12 +45,13 @@ describe('useSessionStateCache — stored-id rotation provenance', () => {cleanup()setActiveSessionId(null)setActiveSessionStoredIdRotation(null)+ setSelectedStoredSessionId(null)})-it('emits the previous, next, and runtime ids and removes the stale reverse mapping', () => {let cache!: CachesetActiveSessionId('runtime-A')+ setSelectedStoredSessionId('stored-A')render(<Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId="stored-A" />)@@ -85,6 +87,25 @@ describe('useSessionStateCache — stored-id rotation provenance', () => {expect(cache.runtimeIdByStoredSessionIdRef.current.has('stored-A')).toBe(false)expect(cache.runtimeIdByStoredSessionIdRef.current.get('stored-A-next')).toBe('runtime-A')})++ it('does not steal the foreground route when a fast A -> B switch beats A\'s rotation (#86106)', () => {+ let cache!: Cache++ setActiveSessionId('runtime-A')+ setSelectedStoredSessionId('stored-B')+ render(+ <Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId="stored-B" />+ )++ act(() => {+ cache.updateSessionState('runtime-A', state => state, 'stored-A')+ cache.updateSessionState('runtime-A', state => state, 'stored-A-next')+ })++ expect($activeSessionStoredIdRotation.get()).toBeNull()+ expect(cache.runtimeIdByStoredSessionIdRef.current.has('stored-A')).toBe(false)+ expect(cache.runtimeIdByStoredSessionIdRef.current.get('stored-A-next')).toBe('runtime-A')+ })})function Harness({ activeSessionId, onReady, selectedStoredSessionId }: HarnessProps) {diff --git a/apps/desktop/src/app/session/hooks/use-session-state-cache.ts b/apps/desktop/src/app/session/hooks/use-session-state-cache.tsindex 3f2e198d3ae..6a3f0dd97d7 100644--- a/apps/desktop/src/app/session/hooks/use-session-state-cache.ts+++ b/apps/desktop/src/app/session/hooks/use-session-state-cache.ts@@ -21,7 +21,13 @@ import {setTurnStartedAt,setYoloActive} from '@/store/session'-import { $sessionStates, $sessionTiles, publishSessionState, releaseSessionTranscript } from '@/store/session-states'+import {+ $sessionStates,+ $sessionTiles,+ isSessionInForeground,+ publishSessionState,+ releaseSessionTranscript+} from '@/store/session-states'import type { ClientSessionState } from '../../types'import { SessionStateCache } from '../session-state-cache'@@ -165,7 +171,7 @@ export function useSessionStateCache({// A rotation event needs a real next id — a null/cleared stored id// is a detach, not a rotation the route-follow effect should chase.- if (storedSessionId && sessionId === $activeSessionId.get()) {+ if (storedSessionId && sessionId === $activeSessionId.get() && isSessionInForeground(existing.storedSessionId)) {setActiveSessionStoredIdRotation({nextStoredSessionId: storedSessionId,previousStoredSessionId: existing.storedSessionId,diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.tsindex a86741caecb..56db58bbbee 100644--- a/apps/desktop/src/store/session-states.ts+++ b/apps/desktop/src/store/session-states.ts@@ -19,6 +19,7 @@import { type GatewayEvent, LOCAL_CONNECTION_ID, registryBackendScopeKey } from '@hermes/shared'import { atom, computed } from 'nanostores'+import { routeSessionId } from '@/app/routes'import type { ClientSessionState } from '@/app/types'import { findGroupOfPane, type LayoutNode } from '@/components/pane-shell/tree/model'import {@@ -413,15 +414,39 @@ export function getRecentlySettledSessionIds(now: number = Date.now()): string[]return live}+/** Whether the user is still focused on a session that belongs to the same+ * durable lineage as the given stored id. Used to decide whether a+ * backgrounded session's delayed id-rotation may follow the route/selection+ * to its new tip, or whether the user has already navigated away.+ */+export function isSessionInForeground(storedSessionId: string): boolean {+ const sessions = $sessions.get()+ const foregroundIds = new Set(lineageAliases(storedSessionId, sessions))++ if (typeof window !== 'undefined') {+ const routed = routeSessionId(window.location.pathname)++ if (routed !== null) {+ return foregroundIds.has(routed)+ }+ }++ const selected = $selectedStoredSessionId.get()++ return selected !== null && foregroundIds.has(selected)+}+// --- Transition detection (called automatically from publishSessionState) ---function handleTransition(previous: ClientSessionState | null, next: ClientSessionState, runtimeId: string) {// Compression id rotation: signal the route-follow effect with enough// provenance (previous id + runtime) that the consumer can reject the event// if the user navigated elsewhere before React handled it. A bare next id// could let a background session's delayed rotation steal the foreground- // route.+ // route. Re-validate against the current route/selection, not just the+ // runtime id, so a fast A -> B switch while A is still busy does not get+ // pulled back to A's new tip (#86106).if (previous?.storedSessionId && next.storedSessionId && previous.storedSessionId !== next.storedSessionId) {- if (runtimeId === $activeSessionId.get()) {+ if (runtimeId === $activeSessionId.get() && isSessionInForeground(previous.storedSessionId)) {setActiveSessionStoredIdRotation({nextStoredSessionId: next.storedSessionId,previousStoredSessionId: previous.storedSessionId,