The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): gate rotation follow on focused surface and hash route
Christopher6d7a8005d046
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.tsindex a59532638a7..0231259a8ea 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@@ -117,6 +117,7 @@ import {dropSessionState,focusOpenSession,holdSessionOwnerUntilForeground,+ isSessionInForeground,openSessionTile,patchSessionTile,publishSessionState,@@ -437,7 +438,8 @@ export function useSessionActions({if (activeSessionIdRef.current !== storedIdRotation.runtimeSessionId ||selectedStoredSessionId !== storedIdRotation.previousStoredSessionId ||- (routedStoredSessionId !== null && routedStoredSessionId !== storedIdRotation.previousStoredSessionId)+ (routedStoredSessionId !== null && routedStoredSessionId !== storedIdRotation.previousStoredSessionId) ||+ !isSessionInForeground(storedIdRotation.previousStoredSessionId)) {return}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 904c8cf357f..322a0fdedb4 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@@ -3,6 +3,8 @@ import { type MutableRefObject, useLayoutEffect } from 'react'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import type { ChatMessage } from '@/lib/chat-messages'+import { group } from '@/components/pane-shell/tree/model'+import { $layoutTree, noteActiveTreeGroup } from '@/components/pane-shell/tree/store'import {$activeSessionStoredIdRotation,$currentFastMode,@@ -20,10 +22,13 @@ import {setCurrentReasoningEffort,setCurrentServiceTier,setSelectedStoredSessionId,+ setSessions,setTurnStartedAt} from '@/store/session'import {+ $focusedStoredSessionId,$sessionStates,+ $sessionTiles,clearAllSessionStates,reconcileBusyStatesOnReconnect,type SessionTileDelegate,@@ -46,6 +51,10 @@ describe('useSessionStateCache — stored-id rotation provenance', () => {setActiveSessionId(null)setActiveSessionStoredIdRotation(null)setSelectedStoredSessionId(null)+ setSessions([])+ $sessionTiles.set([])+ $layoutTree.set(null)+ noteActiveTreeGroup(null)window.history.pushState({}, '', '/')})it('emits the previous, next, and runtime ids and removes the stale reverse mapping', () => {@@ -111,7 +120,8 @@ describe('useSessionStateCache — stored-id rotation provenance', () => {it('does not steal the foreground when the primary route is another session', () => {let cache!: Cache- window.history.pushState({}, '', '/stored-B')+ // Desktop mounts HashRouter: the app route lives in location.hash.+ window.history.pushState({}, '', '/#/stored-B')setActiveSessionId('runtime-A')setSelectedStoredSessionId(null)render(@@ -129,7 +139,7 @@ describe('useSessionStateCache — stored-id rotation provenance', () => {it('follows A -> A-next when the primary route is that same session', () => {let cache!: Cache- window.history.pushState({}, '', '/stored-A')+ window.history.pushState({}, '', '/#/stored-A')setActiveSessionId('runtime-A')setSelectedStoredSessionId(null)render(@@ -169,6 +179,63 @@ describe('useSessionStateCache — stored-id rotation provenance', () => {runtimeSessionId: 'runtime-A'})})++ it('does not steal the focused session tile when primary A rotates (#86106)', () => {+ let cache!: Cache++ // Route and selection still name A while tile B holds the layout focus —+ // the shape the review called out: A's rotation must not run+ // setSelectedStoredSessionId(A-next) and reveal the workspace over B. The+ // selection is armed BEFORE the layout: its listener homes focus to the+ // workspace, so the tile focus must be noted last.+ setSessions([{ id: 'stored-A' }, { id: 'stored-B' }] as never)+ $sessionTiles.set([{ storedSessionId: 'stored-B' }])+ setActiveSessionId('runtime-A')+ setSelectedStoredSessionId('stored-A')+ window.history.pushState({}, '', '/#/stored-A')+ $layoutTree.set(group(['workspace', 'session-tile:stored-B'], { active: 'session-tile:stored-B', id: 'grp-main' }))+ noteActiveTreeGroup('grp-main')+ render(+ <Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId="stored-A" />+ )++ expect($focusedStoredSessionId.get()).toBe('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.get('stored-A-next')).toBe('runtime-A')+ })++ it('follows A -> A-next when the focused tile belongs to the same lineage', () => {+ let cache!: Cache++ // A tile keyed by an OLDER segment id of the same conversation still counts+ // as the foreground: the rotation carries the surface to the new tip.+ setSessions([{ _lineage_ids: ['stored-A', 'stored-A-next'], _lineage_root_id: 'stored-A', id: 'stored-A-next' }] as never)+ $sessionTiles.set([{ storedSessionId: 'stored-A' }])+ setActiveSessionId('runtime-A')+ setSelectedStoredSessionId(null)+ $layoutTree.set(group(['workspace', 'session-tile:stored-A'], { active: 'session-tile:stored-A', id: 'grp-main' }))+ noteActiveTreeGroup('grp-main')+ render(+ <Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId={null} />+ )++ act(() => {+ cache.updateSessionState('runtime-A', state => state, 'stored-A')+ cache.updateSessionState('runtime-A', state => state, 'stored-A-next')+ })++ expect($activeSessionStoredIdRotation.get()).toEqual({+ nextStoredSessionId: 'stored-A-next',+ previousStoredSessionId: 'stored-A',+ runtimeSessionId: 'runtime-A'+ })+ })})function Harness({ activeSessionId, onReady, selectedStoredSessionId }: HarnessProps) {diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.tsindex 3979ef3558f..788a84caf14 100644--- a/apps/desktop/src/store/session-states.ts+++ b/apps/desktop/src/store/session-states.ts@@ -414,32 +414,48 @@ export function getRecentlySettledSessionIds(now: number = Date.now()): string[]return live}+/** The session id the live HashRouter route names, or null when the route has+ * no session opinion (new-chat draft, reserved/overlay/contributed page, or+ * no hash at all). Desktop mounts HashRouter, so the app route lives in+ * `location.hash` (`#/stored-A`); `location.pathname` is always the+ * document's own path and never carries the session segment. */+function windowRouteSessionId(): string | null {+ if (typeof window === 'undefined') {+ return null+ }++ return routeSessionId(window.location.hash.replace(/^#/, ''))+}+/** 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.- */+ *+ * Every surface that can name the on-screen session must agree: the focused+ * tile or primary (`$focusedStoredSessionId` already folds the layout's+ * interaction tracker, an open tile, and the primary selection into one+ * answer) AND the HashRouter route. A fast A -> B switch can leave route and+ * selection on A while tile B holds focus; either surface naming a session+ * outside the lineage means the user has already moved on (#86106). */export function isSessionInForeground(storedSessionId: string): boolean {const sessions = $sessions.get()const foregroundIds = new Set(lineageAliases(storedSessionId, sessions))+ const focused = $focusedStoredSessionId.get()- if (typeof window !== 'undefined') {- const routed = routeSessionId(window.location.pathname)-- if (routed !== null) {- return foregroundIds.has(routed)- }+ if (focused !== null && !foregroundIds.has(focused)) {+ return false}- const selected = $selectedStoredSessionId.get()+ const routed = windowRouteSessionId()- if (selected !== null) {- return foregroundIds.has(selected)+ if (routed !== null && !foregroundIds.has(routed)) {+ return false}- // No route and no store selection: a fresh unpersisted chat is still- // the thing on screen. The caller already requires the rotating runtime- // to be $activeSessionId, so allow that session's own A -> A-next.+ // Neither surface names a session: a fresh unpersisted chat is still the+ // thing on screen. The caller already requires the rotating runtime to be+ // $activeSessionId, so allow that session's own A -> A-next.return true}