Skip to content

The agent that grows with you

fix(desktop): keep steering through stored id rotation

Mohamad Kansod94e7d153623
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/steering-recovery.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/steering-recovery.test.tsx
index 3b994b36755..fe61c7ab0f8 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/steering-recovery.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/steering-recovery.test.tsx
@@ -12,7 +12,9 @@ import {
$activeSessionStoredIdRotation,
$messages,
$selectedStoredSessionId,
+ $sessions,
setActiveSessionId,
+ setActiveSessionStoredIdRotation,
setAwaitingResponse,
setBusy,
setMessages,
@@ -146,6 +148,7 @@ afterEach(() => {
clearAllSessionStates()
clearSingleFlightSessionResumeState()
setActiveSessionId(null)
+ setActiveSessionStoredIdRotation(null)
setSelectedStoredSessionId(null)
setSessions([])
setBusy(false)
@@ -200,6 +203,116 @@ const steeringActions = [
{ action: 'injectHiddenPrompt' as const, method: 'session.steer', status: 'queued' }
]
+it.each(steeringActions)(
+ '$action ($status) keeps steering on a rotated session before its new row has loaded',
+ async ({ action, method, status }) => {
+ seed()
+
+ // session.info can bind the runtime to the continuation before the next
+ // sessions refresh carries its lineage. The open route and selection still
+ // name the previous id during this short handoff.
+ act(() => {
+ handle.cache.updateSessionState('rt-B', state => state, 'stored-B-tip')
+ })
+
+ expect($sessions.get().map(session => session.id)).toEqual(['stored-A', 'stored-B'])
+ expect($activeSessionStoredIdRotation.get()).toEqual({
+ nextStoredSessionId: 'stored-B-tip',
+ previousStoredSessionId: 'stored-B',
+ runtimeSessionId: 'rt-B'
+ })
+
+ vi.mocked(requestGatewayForAgent).mockResolvedValue({ status })
+ await act(async () => {
+ expect(await handle.actions[action]('rotated chat correction')).toBe(true)
+ })
+
+ expect(requestGatewayForAgent).toHaveBeenLastCalledWith('connection-B', 'default', method, {
+ session_id: 'rt-B',
+ text: 'rotated chat correction'
+ })
+ }
+)
+
+it.each(steeringActions)(
+ '$action ($status) keeps steering on a rotated session after its previous tip was evicted',
+ async ({ action, method, status }) => {
+ seed()
+
+ act(() => {
+ // First establish the tip that the route and selection were using.
+ handle.cache.updateSessionState('rt-B', state => state, 'stored-B-evicted-tip')
+ setActiveSessionStoredIdRotation(null)
+ routedStoredId = 'stored-B-evicted-tip'
+ setSelectedStoredSessionId('stored-B-evicted-tip')
+
+ // The next compression can replace that tip in the session list before
+ // the route-follow effect has consumed its rotation event.
+ setSessions([
+ {
+ id: 'stored-B-next',
+ _lineage_root_id: 'stored-B',
+ profile: 'default',
+ connection_id: 'connection-B',
+ source: 'desktop',
+ message_count: 2
+ }
+ ] as SessionInfo[])
+ handle.cache.updateSessionState('rt-B', state => state, 'stored-B-next')
+ })
+
+ expect($sessions.get().map(session => session.id)).toEqual(['stored-B-next'])
+ expect($activeSessionStoredIdRotation.get()).toEqual({
+ nextStoredSessionId: 'stored-B-next',
+ previousStoredSessionId: 'stored-B-evicted-tip',
+ runtimeSessionId: 'rt-B'
+ })
+
+ vi.mocked(requestGatewayForAgent).mockResolvedValue({ status })
+ await act(async () => {
+ expect(await handle.actions[action]('evicted tip correction')).toBe(true)
+ })
+
+ expect(requestGatewayForAgent).toHaveBeenLastCalledWith('connection-B', 'default', method, {
+ session_id: 'rt-B',
+ text: 'evicted tip correction'
+ })
+ }
+)
+
+it.each(['redirectPrompt', 'injectHiddenPrompt'] as const)(
+ '%s rejects an evicted selection without matching rotation proof',
+ async action => {
+ seed()
+
+ act(() => {
+ setSessions([
+ {
+ id: 'stored-B-next',
+ _lineage_root_id: 'stored-B',
+ profile: 'default',
+ connection_id: 'connection-B',
+ source: 'desktop',
+ message_count: 2
+ }
+ ] as SessionInfo[])
+ handle.cache.updateSessionState('rt-B', state => state, 'stored-B-next')
+ setActiveSessionStoredIdRotation(null)
+
+ // A stale route and selection alone must not make B's live runtime look
+ // like an unrelated, evicted conversation that happened to be on screen.
+ routedStoredId = 'stored-A-evicted-tip'
+ setSelectedStoredSessionId('stored-A-evicted-tip')
+ })
+
+ await act(async () => {
+ expect(await handle.actions[action]('wrong chat correction')).toBe(false)
+ })
+
+ expect(requestGatewayForAgent).not.toHaveBeenCalled()
+ }
+)
+
const recoveryCases = (['before-stale-error', 'during-resume', 'stay'] as const).flatMap(navigation =>
[false, true].flatMap(expiredRecovery => steeringActions.map(action => ({ ...action, navigation, expiredRecovery })))
)
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/steering-session.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/steering-session.ts
index a7fc3638e3d..1b171a30add 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/steering-session.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/steering-session.ts
@@ -2,7 +2,7 @@ import type { MutableRefObject } from 'react'
import { findStoredIdForRuntimeId } from '@/app/contrib/wiring-routing'
import type { ClientSessionState } from '@/app/types'
-import { $sessions, idsShareLineage, setActiveSessionId } from '@/store/session'
+import { $activeSessionStoredIdRotation, $sessions, idsShareLineage, setActiveSessionId } from '@/store/session'
import { requestForSessionProfile } from '@/store/session-request-router'
import { knownOwnerForSession } from '@/store/session-states'
@@ -37,9 +37,24 @@ export function captureSteeringSession(deps: SteeringSessionDeps) {
const bindings = runtimeIdByStoredSessionIdRef.current
const boundStoredSessionId = findStoredIdForRuntimeId(bindings, sessionId)
const sessions = $sessions.get()
+ const storedIdRotation = $activeSessionStoredIdRotation.get()
+
+ // A compression event can update the runtime binding before the sessions
+ // refresh exposes the new tip's lineage. The rotation records the exact
+ // runtime and old/new stored ids, so it is enough proof for that brief gap.
+ const rotationBridgesUnloadedTip = Boolean(
+ storedIdRotation &&
+ storedIdRotation.runtimeSessionId === sessionId &&
+ storedIdRotation.previousStoredSessionId === selectedStoredSessionId &&
+ storedIdRotation.nextStoredSessionId === boundStoredSessionId
+ )
const matchesSelection = (id: string) =>
- Boolean(selectedStoredSessionId && idsShareLineage(id, selectedStoredSessionId, sessions))
+ Boolean(
+ selectedStoredSessionId &&
+ (idsShareLineage(id, selectedStoredSessionId, sessions) ||
+ (rotationBridgesUnloadedTip && id === boundStoredSessionId))
+ )
// Navigation publishes route, selection and runtime independently. Unlike an
// ordinary Send, a mid-turn correction must never resolve another target and
@@ -64,7 +79,12 @@ export function captureSteeringSession(deps: SteeringSessionDeps) {
// A rebuilt runtime can bind the tip while selection/route keep the root.
// Use its proven stored id for writes; reapplying the root would rotate it back.
const storedSessionId = boundStoredSessionId ?? selectedStoredSessionId
- const owner = knownOwnerForSession(sessionId) ?? knownOwnerForSession(storedSessionId)
+
+ const owner =
+ knownOwnerForSession(sessionId) ??
+ knownOwnerForSession(storedSessionId) ??
+ (rotationBridgesUnloadedTip ? knownOwnerForSession(storedIdRotation?.previousStoredSessionId) : undefined)
+
let adoptedSessionId = sessionId
const requestGateway: GatewayRequest = (method, params, timeoutMs) =>