Skip to content

The agent that grows with you

fix(desktop): cover route-based foreground and unselected A-next rotation

Christopher32bb86ff7279
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.tsx
index 38d08ab1c2c..904c8cf357f 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
@@ -46,6 +46,7 @@ describe('useSessionStateCache — stored-id rotation provenance', () => {
setActiveSessionId(null)
setActiveSessionStoredIdRotation(null)
setSelectedStoredSessionId(null)
+ window.history.pushState({}, '', '/')
})
it('emits the previous, next, and runtime ids and removes the stale reverse mapping', () => {
let cache!: Cache
@@ -106,6 +107,68 @@ 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 when the primary route is another session', () => {
+ let cache!: Cache
+
+ window.history.pushState({}, '', '/stored-B')
+ setActiveSessionId('runtime-A')
+ setSelectedStoredSessionId(null)
+ 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()).toBeNull()
+ })
+
+ it('follows A -> A-next when the primary route is that same session', () => {
+ let cache!: Cache
+
+ window.history.pushState({}, '', '/stored-A')
+ setActiveSessionId('runtime-A')
+ setSelectedStoredSessionId(null)
+ 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'
+ })
+ })
+
+ it('follows A -> A-next when there is no route and no store selection', () => {
+ let cache!: Cache
+
+ window.history.pushState({}, '', '/')
+ setActiveSessionId('runtime-A')
+ setSelectedStoredSessionId(null)
+ 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.ts
index 56db58bbbee..3979ef3558f 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -433,7 +433,14 @@ export function isSessionInForeground(storedSessionId: string): boolean {
const selected = $selectedStoredSessionId.get()
- return selected !== null && foregroundIds.has(selected)
+ if (selected !== null) {
+ return foregroundIds.has(selected)
+ }
+
+ // 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.
+ return true
}
// --- Transition detection (called automatically from publishSessionState) ---