The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): unbind the outgoing runtime on a warm session switch
tim404x98ddc35699a9
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.tsxindex d10f94b0b88..5ef3cb2e8aa 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@@ -112,7 +112,11 @@ import { pinnedOwnerCount, pinnedStoredSessionIdsForOwner, releaseStoredSessionPimport { 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 {+ createPersistedDisplayTranscriptProvenance,+ suppressTranscriptForView,+ transcriptRowContentKey+} from './use-session-actions/transcript-provenance'import type { TranscriptViewCutoff } from './use-session-actions/transcript-provenance'import { useSessionStateCache } from './use-session-state-cache'@@ -2594,6 +2598,286 @@ describe('session.resume turn timer contract', () => {})})+// Real session cache + real view sync: the leak lives in the seam between+// resumeSession's foreground binding and the cache's view gate, so the fakes+// in ResumeHarness would hide it.+function WarmSwitchHarness({+ onReady,+ requestGateway+}: {+ onReady: (ready: {+ cache: ReturnType<typeof useSessionStateCache>+ resume: (storedSessionId: string, replaceRoute?: boolean) => Promise<unknown>+ }) => void+ requestGateway: <T>(method: string, params?: Record<string, unknown>) => Promise<T>+}) {+ const activeSessionId = useStore($activeSessionId)+ const selectedStoredSessionId = useStore($selectedStoredSessionId)+ const busyRef = useRef(false)++ const cache = useSessionStateCache({+ activeSessionId,+ busyRef,+ selectedStoredSessionId,+ setAwaitingResponse,+ setBusy,+ setMessages+ })++ const actions = useSessionActions({+ activeSessionId,+ activeSessionIdRef: cache.activeSessionIdRef,+ busyRef,+ creatingSessionRef: useRef(false),+ ensureSessionState: cache.ensureSessionState,+ getRouteToken: () => 'warm-switch',+ getRoutedStoredSessionId: () => null,+ holdSessionTranscriptView: cache.holdSessionTranscriptView,+ navigate: vi.fn() as never,+ requestGateway,+ resetViewSync: cache.resetViewSync,+ runtimeIdByStoredSessionIdRef: cache.runtimeIdByStoredSessionIdRef,+ selectedStoredSessionId,+ selectedStoredSessionIdRef: cache.selectedStoredSessionIdRef,+ sessionStateByRuntimeIdRef: cache.sessionStateByRuntimeIdRef,+ syncSessionStateToView: cache.syncSessionStateToView,+ updateSessionState: cache.updateSessionState+ })++ useEffect(() => {+ onReady({ cache, resume: actions.resumeSession })+ }, [actions.resumeSession, cache, onReady])++ return null+}++describe('resumeSession warm switch away from a streaming session (#89696)', () => {+ const textOf = (messages: readonly { parts: readonly unknown[] }[]) => JSON.stringify(messages)++ const turnA = [+ { id: 'a-user', role: 'user' as const, parts: [{ type: 'text' as const, text: 'A history question' }] },+ { id: 'a-answer', role: 'assistant' as const, parts: [{ type: 'text' as const, text: 'A history answer' }] }+ ]++ const turnB = [+ { id: 'b-user', role: 'user' as const, parts: [{ type: 'text' as const, text: 'B history question' }] },+ { id: 'b-answer', role: 'assistant' as const, parts: [{ type: 'text' as const, text: 'B history answer' }] }+ ]++ const persistedB = [+ { content: 'B history question', role: 'user', timestamp: 1 },+ { content: 'B history answer', role: 'assistant', timestamp: 2 }+ ]++ beforeEach(() => {+ vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback) => {+ callback(0)++ return null as unknown as number+ })+ vi.mocked(ensureGatewayProfile).mockReset().mockResolvedValue(undefined)+ vi.mocked(getSession).mockReset()+ vi.mocked(getLatestSessionMessages)+ .mockReset()+ .mockResolvedValue({ messages: persistedB, session_id: 'stored-B' } as never)+ setSessions([+ storedSession({ id: 'stored-A', message_count: 2, profile: 'default' }),+ storedSession({ id: 'stored-B', message_count: 2, profile: 'default' })+ ])+ })++ afterEach(() => {+ cleanup()+ setActiveSessionId(null)+ setSelectedStoredSessionId(null)+ setAwaitingResponse(false)+ setBusy(false)+ setMessages([])+ setSessions([])+ vi.mocked(ensureGatewayProfile).mockReset().mockResolvedValue(undefined)+ vi.mocked(requestGatewayForProfile).mockReset()+ vi.mocked(getLatestSessionMessages)+ .mockReset()+ .mockResolvedValue({ messages: [] } as never)+ vi.restoreAllMocks()+ })++ // A is the foreground chat, mid-turn; B is a warm, settled chat the user+ // opened earlier in this window.+ async function mountWithStreamingForegroundAndWarmTarget() {+ const requestGateway = vi.fn(async (method: string, _params?: Record<string, unknown>) => {+ if (method === 'session.activate') {+ return {+ session_id: 'rt-B',+ session_key: 'stored-B',+ resumed: 'stored-B',+ message_count: 2,+ messages: [],+ messages_omitted: true,+ running: false,+ info: {}+ } as never+ }++ return {} as never+ })++ let harness!: Parameters<Parameters<typeof WarmSwitchHarness>[0]['onReady']>[0]+ render(<WarmSwitchHarness onReady={ready => (harness = ready)} requestGateway={requestGateway} />)+ await waitFor(() => expect(harness).toBeDefined())+ // Profile-owned rows route session RPCs through the profile dispatcher.+ vi.mocked(requestGatewayForProfile).mockImplementation((_profile, method, params) => requestGateway(method, params))++ act(() => {+ setSelectedStoredSessionId('stored-A')+ setActiveSessionId('rt-A')+ })++ act(() => {+ harness.cache.updateSessionState(+ 'rt-A',+ state => ({ ...state, awaitingResponse: true, busy: true, messages: turnA, turnLive: true }),+ 'stored-A'+ )+ harness.cache.updateSessionState('rt-B', state => ({ ...state, messages: turnB }), 'stored-B')+ })++ expect(textOf($messages.get())).toContain('A history answer')++ return { harness, requestGateway }+ }++ function streamIntoA(harness: Parameters<Parameters<typeof WarmSwitchHarness>[0]['onReady']>[0]) {+ act(() => {+ harness.cache.updateSessionState(+ 'rt-A',+ state => ({+ ...state,+ messages: [+ ...state.messages,+ { id: 'user-a-live', role: 'user', parts: [{ type: 'text', text: 'A live prompt' }] },+ {+ id: 'assistant-stream-a',+ role: 'assistant',+ pending: true,+ parts: [{ type: 'text', text: 'A streaming reply' }]+ }+ ]+ }),+ 'stored-A'+ )+ })+ }++ it('stops painting the outgoing live turn while the warm target waits on its gateway', async () => {+ const { harness } = await mountWithStreamingForegroundAndWarmTarget()+ const gatewayReady = deferred<void>()+ vi.mocked(ensureGatewayProfile).mockReturnValueOnce(gatewayReady.promise)++ const pending = harness.resume('stored-B', true)+ await waitFor(() => expect(ensureGatewayProfile).toHaveBeenCalled())++ // B is selected but not bound yet. The runtime A just left must not own+ // the foreground: its deltas would keep flushing into the shared view.+ expect($selectedStoredSessionId.get()).toBe('stored-B')+ expect($activeSessionId.get()).not.toBe('rt-A')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).not.toContain('A history answer')++ streamIntoA(harness)++ expect(textOf($messages.get())).not.toContain('A streaming reply')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).not.toContain('A streaming reply')++ await act(async () => {+ gatewayReady.resolve()+ await pending+ })++ // A kept streaming in the background, into its own slice only.+ expect(textOf(harness.cache.sessionStateByRuntimeIdRef.current.get('rt-A')?.messages ?? [])).toContain(+ 'A streaming reply'+ )+ await waitFor(() => expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).toContain('B history answer'))+ expect($activeSessionId.get()).toBe('rt-B')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).not.toContain('A ')+ })++ it("never grafts the outgoing chat's pending turn into a warm target re-resumed mid-switch", async () => {+ const { harness } = await mountWithStreamingForegroundAndWarmTarget()+ const gatewayReady = deferred<void>()+ vi.mocked(ensureGatewayProfile).mockReturnValueOnce(gatewayReady.promise)++ const first = harness.resume('stored-B', true)+ await waitFor(() => expect(ensureGatewayProfile).toHaveBeenCalledTimes(1))+ streamIntoA(harness)++ // A second resume of the same target (route self-heal, reconnect, a+ // repeated click) snapshots the view as "this session's pending turn".+ await act(async () => {+ await harness.resume('stored-B', true)+ })++ const cachedB = harness.cache.sessionStateByRuntimeIdRef.current.get('rt-B')++ expect(textOf(cachedB?.messages ?? [])).not.toContain('A live prompt')+ expect(textOf(cachedB?.messages ?? [])).not.toContain('A streaming reply')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).not.toContain('A live prompt')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).not.toContain('A streaming reply')++ await act(async () => {+ gatewayReady.resolve()+ await first+ })++ expect($activeSessionId.get()).toBe('rt-B')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).not.toContain('A streaming reply')+ })++ it('paints a proven warm target at entry, never the outgoing live turn', async () => {+ const { harness } = await mountWithStreamingForegroundAndWarmTarget()++ act(() => {+ harness.cache.updateSessionState(+ 'rt-B',+ state => ({+ ...state,+ transcriptProvenance: createPersistedDisplayTranscriptProvenance({+ lineageRootId: null,+ scope: 'default',+ storedSessionId: 'stored-B'+ })+ }),+ 'stored-B'+ )+ })++ const gatewayReady = deferred<void>()+ vi.mocked(ensureGatewayProfile).mockReturnValueOnce(gatewayReady.promise)++ const pending = harness.resume('stored-B', true)++ // Display-only: B's persisted-display transcript is on screen before the+ // gateway swap resolves, with A's runtime already off the foreground.+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).toContain('B history answer')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).not.toContain('A history answer')+ expect($activeSessionId.get()).toBeNull()++ streamIntoA(harness)++ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).toContain('B history answer')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).not.toContain('A streaming reply')++ await act(async () => {+ gatewayReady.resolve()+ await pending+ })++ expect($activeSessionId.get()).toBe('rt-B')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).toContain('B history answer')+ expect(textOf(PRIMARY_SESSION_VIEW.$messages.get())).not.toContain('A ')+ })+})+function BranchHarness({activeSessionId = null,navigate = vi.fn(),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 a067e787074..187d734d843 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@@ -1331,12 +1331,11 @@ export function useSessionActions({// Paint the click before the profile-resolve / gateway-swap awaits below,// so there's zero dead air: highlight the row instantly (the sidebar reads- // $selectedStoredSessionId) and, for a cold target, drop the previous- // transcript so the thread shows its loader instead of the old session- // lingering until resume lands. A warm-cached target keeps its transcript —- // the cached fast-path repaints it this same tick. Setting the ref here is- // also what use-route-resume's self-heal assumes ("set synchronously at- // resume entry").+ // $selectedStoredSessionId) and drop the previous session's transcript so+ // the thread shows this session's proven warm transcript or its loader+ // instead of the old session lingering until resume lands. Setting the+ // ref here is also what use-route-resume's self-heal assumes ("set+ // synchronously at resume entry").setFreshDraftReady(false)clearNotifications()resetViewSync()@@ -1394,7 +1393,18 @@ export function useSessionActions({return { runtimeId, state }}- if (!takeWarmCache()) {+ // The selection moved above, but a warm runtime is only (re)bound after+ // the awaits below. Until then the foreground may stay on a runtime only+ // if it already IS this session's own warm runtime (a same-session+ // re-resume). Leaving another chat's runtime bound is #89696: the primary+ // view reads the active runtime's slice and the view sync admits only the+ // active runtime, so a turn still streaming there keeps painting under+ // this session's route, and a re-entrant resume of this target would+ // snapshot it as this session's local pending turn (resumeStartMessages).+ const warmAtEntry = takeWarmCache()+ const foregroundIsTarget = warmAtEntry !== null && warmAtEntry.runtimeId === activeSessionIdRef.current++ if (!foregroundIsTarget) {setActiveSessionId(null)activeSessionIdRef.current = null// History load is not turn-busy. Drop the previous session's leftover@@ -1434,6 +1444,23 @@ export function useSessionActions({provisional.paint(transcriptRestScope(ownerRoute, listedStored, ambientConnectionId))}+ // An unbound warm target whose transcript is already persisted-display+ // authority paints now, display-only, instead of waiting out the awaits+ // below; the fast path re-proves it against the resolved owner before+ // binding. An unproven warm cache can be a compressed runtime tail, so it+ // keeps the loader (#73646).+ if (warmAtEntry && !foregroundIsTarget && listedStored) {+ const entryProvenance = createPersistedDisplayTranscriptProvenance({+ lineageRootId: listedStored._lineage_root_id ?? null,+ scope: transcriptRestScope(ownerRoute, listedStored, ambientConnectionId),+ storedSessionId+ })++ if (hasPersistedDisplayTranscriptProvenance(warmAtEntry.state, entryProvenance)) {+ setMessages(warmAtEntry.state.messages)+ }+ }+const storedForProfile = await resolveStoredSession(storedSessionId, ownerRoute)const sessionProfile = storedForProfile?.profile