Skip to content

The agent that grows with you

fix(desktop): keep rotation proof available for steering

Mohamad Kanso17e23f9d35f7
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
index 35fa627da46..6d69e1c9f13 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
@@ -108,6 +108,7 @@ import { NEW_CHAT_ROUTE, sessionRoute } from '../../routes'
import type { ClientSessionState } from '../../types'
import { applySessionInfoStatePatch, sessionInfoStatePatch } from './use-message-stream/utils'
+import { captureSteeringSession } from './use-prompt-actions/steering-session'
import { useSessionActions } from './use-session-actions'
import { suppressTranscriptForView, transcriptRowContentKey } from './use-session-actions/transcript-provenance'
import type { TranscriptViewCutoff } from './use-session-actions/transcript-provenance'
@@ -727,7 +728,7 @@ describe('active stored-session id rotation routing', () => {
expect($activeSessionStoredIdRotation.get()).toBeNull()
})
- it('does not follow a rotation when an unrelated tile has focus', async () => {
+ it('keeps rotation proof for steering when focus moves to an unrelated tile', async () => {
const previousId = 'stored-A'
const nextId = 'stored-A-next'
const unrelatedId = 'stored-C'
@@ -764,9 +765,42 @@ describe('active stored-session id rotation routing', () => {
$activeTreeGroup.set('grp-main')
})
- await waitFor(() => expect($activeSessionStoredIdRotation.get()).toBeNull())
+ await waitFor(() =>
+ expect($activeSessionStoredIdRotation.get()).toEqual({
+ nextStoredSessionId: nextId,
+ previousStoredSessionId: previousId,
+ runtimeSessionId: runtimeId
+ })
+ )
expect($selectedStoredSessionId.get()).toBe(previousId)
expect(navigate).not.toHaveBeenCalled()
+
+ const captured = captureSteeringSession({
+ activeSessionIdRef: cache!.activeSessionIdRef,
+ getRoutedStoredSessionId: () => previousId,
+ requestGateway: async () => ({}) as never,
+ runtimeIdByStoredSessionIdRef: cache!.runtimeIdByStoredSessionIdRef,
+ selectedStoredSessionIdRef: cache!.selectedStoredSessionIdRef,
+ updateSessionState: cache!.updateSessionState
+ })
+
+ expect(captured?.sessionId).toBe(runtimeId)
+ expect(captured?.storedSessionId).toBe(nextId)
+
+ act(() => {
+ $sessionTiles.set([{ storedSessionId: nextId } as never])
+ $layoutTree.set(
+ group(['workspace', `session-tile:${nextId}`], {
+ active: `session-tile:${nextId}`,
+ id: 'grp-main'
+ })
+ )
+ $activeTreeGroup.set('grp-main')
+ })
+
+ await waitFor(() => expect($selectedStoredSessionId.get()).toBe(nextId))
+ expect($activeSessionStoredIdRotation.get()).toBeNull()
+ expect(navigate).toHaveBeenCalledWith(sessionRoute(nextId), { replace: true })
})
it('keeps draft on the previous tip when the new tip row is not loaded yet', async () => {
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 80c59febf98..75b10b74a65 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
@@ -489,6 +489,9 @@ export function useSessionActions({
// unconditionally; a fast A → B → C switch could therefore be overwritten
// by A's delayed session.info event and visibly jump back to A.
const storedIdRotation = useStore($activeSessionStoredIdRotation)
+ const storedSessions = useStore($sessions)
+ const focusedStoredSessionId = useStore($focusedStoredSessionId)
+ const routedStoredSessionId = getRoutedStoredSessionId()
// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
useEffect(() => {
@@ -496,46 +499,52 @@ export function useSessionActions({
return
}
- // Consume the event even when it is stale. Rotation is an edge, not durable
- // state; replaying it after a later remount/selection would steal focus.
- setActiveSessionStoredIdRotation(current => (current === storedIdRotation ? null : current))
-
- const selectedStoredSessionId = selectedStoredSessionIdRef.current
- const routedStoredSessionId = getRoutedStoredSessionId()
+ const selectedAtEffect = selectedStoredSessionIdRef.current
const previousId = storedIdRotation.previousStoredSessionId
const nextId = storedIdRotation.nextStoredSessionId
- const sessions = $sessions.get()
- const focusedStoredSessionId = $focusedStoredSessionId.get()
// A tile can adopt the exact successor before the refreshed sessions list
// contains it. The rotation itself proves that this focus is the same live
// runtime; unrelated focused chats still fail the foreground check.
const focusedOnRotatedLineage = Boolean(
focusedStoredSessionId &&
- (focusedStoredSessionId === nextId || idsShareLineage(focusedStoredSessionId, nextId, sessions))
+ (focusedStoredSessionId === nextId || idsShareLineage(focusedStoredSessionId, nextId, storedSessions))
)
- if (
+ const rotationIsStale =
activeSessionIdRef.current !== storedIdRotation.runtimeSessionId ||
- selectedStoredSessionId !== previousId ||
- (routedStoredSessionId !== null && routedStoredSessionId !== previousId) ||
- (!isSessionInForeground(previousId) && !focusedOnRotatedLineage)
- ) {
+ selectedAtEffect !== previousId ||
+ (routedStoredSessionId !== null && routedStoredSessionId !== previousId)
+
+ if (rotationIsStale) {
+ // The user moved to another conversation, so this proof must not replay.
+ setActiveSessionStoredIdRotation(current => (current === storedIdRotation ? null : current))
+
return
}
+ if (!isSessionInForeground(previousId) && !focusedOnRotatedLineage) {
+ // Focus moved to an unrelated tile, but route and selection still name
+ // this conversation. Keep the proof so steering can use it until focus
+ // or the session list catches up.
+ return
+ }
+
+ // Consume only once the successor can safely take over the visible session.
+ setActiveSessionStoredIdRotation(current => (current === storedIdRotation ? null : current))
+
// Park unsent draft/queue on the durable lineage key (not the new tip).
// ChatBar scopes composer state on resolveComposerSessionKey(); migrating
// onto the tip while the composer is still bound to the root can lose newer
// live editor text on a brief remount. If the new tip row is not in
// $sessions yet, resolveComposerSessionKey falls back to the tip id — prefer
// the previous id (usually the lineage root) in that gap.
- const resolvedNext = resolveComposerSessionKey(nextId, sessions)
+ const resolvedNext = resolveComposerSessionKey(nextId, storedSessions)
const durableKey =
resolvedNext && resolvedNext !== nextId
? resolvedNext
- : (resolveComposerSessionKey(previousId, sessions) ?? previousId)
+ : (resolveComposerSessionKey(previousId, storedSessions) ?? previousId)
migrateSessionDraft(previousId, durableKey)
migrateSessionDraft(nextId, durableKey)
@@ -551,7 +560,18 @@ export function useSessionActions({
if (routedStoredSessionId === previousId) {
navigate(sessionRoute(nextId), { replace: true })
}
- }, [activeSessionIdRef, getRoutedStoredSessionId, navigate, selectedStoredSessionIdRef, storedIdRotation])
+ }, [
+ activeSessionId,
+ activeSessionIdRef,
+ focusedStoredSessionId,
+ getRoutedStoredSessionId,
+ navigate,
+ routedStoredSessionId,
+ selectedStoredSessionId,
+ selectedStoredSessionIdRef,
+ storedIdRotation,
+ storedSessions
+ ])
const startFreshSessionDraft = useCallback(
(options: boolean | FreshSessionDraftOptions = false) => {