diff --git a/src/web-ui/src/flow_chat/services/flow-chat-manager/SessionModule.test.ts b/src/web-ui/src/flow_chat/services/flow-chat-manager/SessionModule.test.ts index 1763a1139d..c2f7a390b7 100644 --- a/src/web-ui/src/flow_chat/services/flow-chat-manager/SessionModule.test.ts +++ b/src/web-ui/src/flow_chat/services/flow-chat-manager/SessionModule.test.ts @@ -230,6 +230,7 @@ function createContext( state = { ...state, activeSessionId: sessionId }; }), loadSessionHistory: vi.fn(), + ensureSessionFullHistory: vi.fn(async () => true), ensurePersistedSessionMetadata: vi.fn(async () => true), getCascadeSessionIds: vi.fn((sessionId: string) => ( options?.getCascadeSessionIdsImpl?.(sessionId) ?? [sessionId] @@ -996,6 +997,32 @@ describe('SessionModule historical session coordination', () => { await load.promise; }); + it('completes a ready partial session after switching back to its in-memory tail', async () => { + const session = createSession({ + isHistorical: false, + historyState: 'ready', + isPartial: true, + dialogTurns: [{ + id: 'turn-tail', + userMessage: { id: 'user-turn-tail', content: 'Latest prompt', timestamp: 1 }, + modelRounds: [{ items: [{ type: 'text', text: 'Latest answer' }] }], + status: 'completed', + } as any], + loadedTurnCount: 1, + totalTurnCount: 20, + }); + const { context, flowChatStore } = createContext(session, { activeSessionId: 'other-session' }); + persistenceMocks.touchSessionActivity.mockResolvedValue(undefined); + + await switchChatSession(context, session.sessionId); + + expect(flowChatStore.switchSession).toHaveBeenCalledWith(session.sessionId); + expect(flowChatStore.ensureSessionFullHistory).toHaveBeenCalledWith( + session.sessionId, + 'session-switch', + ); + }); + it('touches only the latest active session during rapid switches', async () => { const firstSession = createSession({ sessionId: 'history-1', diff --git a/src/web-ui/src/flow_chat/services/flow-chat-manager/SessionModule.ts b/src/web-ui/src/flow_chat/services/flow-chat-manager/SessionModule.ts index 47882f5c72..711eb9445c 100644 --- a/src/web-ui/src/flow_chat/services/flow-chat-manager/SessionModule.ts +++ b/src/web-ui/src/flow_chat/services/flow-chat-manager/SessionModule.ts @@ -326,6 +326,16 @@ function shouldHydrateHistoricalSessionBeforeSwitch(session: Session | undefined return !hasRenderableSessionContent(session); } +function shouldCompletePartialHistoryAfterSwitch(session: Session | undefined): boolean { + // A live session can retain only its restored tail while still being marked + // non-historical. Completing it after an explicit switch restores the + // assistant rounds that cannot come from the user-only turn catalog. + return session?.isHistorical !== true + && session?.historyState === 'ready' + && session.isPartial === true + && session.externalHistory?.mode !== 'follow'; +} + function shouldRetryActiveStaleHydrate(context: FlowChatContext, sessionId: string): boolean { const state = context.flowChatStore.getState(); if (state.activeSessionId !== sessionId) { @@ -550,6 +560,7 @@ export async function switchChatSession( const isRemoteSession = isRemoteTraceContext(session?.remoteConnectionId, session?.remoteSshHost); const shouldHydrateBeforeSwitch = session?.externalHistory?.mode === 'follow' || shouldHydrateHistoricalSessionBeforeSwitch(session); + const shouldCompletePartialHistory = shouldCompletePartialHistoryAfterSwitch(session); const shouldActivateBeforeHydrate = shouldHydrateBeforeSwitch && consumeRecentHistorySessionOpenIntent(sessionId); @@ -560,6 +571,7 @@ export async function switchChatSession( historyState: session?.historyState, hasRenderableContent: session ? hasRenderableSessionContent(session) : false, shouldHydrateBeforeSwitch, + shouldCompletePartialHistory, shouldActivateBeforeHydrate, }); @@ -664,6 +676,27 @@ export async function switchChatSession( } }); } + + if (shouldCompletePartialHistory) { + recordHistorySessionDiagnosticEvent(sessionId, 'switch_partial_history_completion_started', { + switchRequestId, + }); + void context.flowChatStore.ensureSessionFullHistory(sessionId, 'session-switch') + .then(ready => { + recordHistorySessionDiagnosticEvent(sessionId, 'switch_partial_history_completion_finished', { + switchRequestId, + ready, + }); + }) + .catch(error => { + if (!isSurfaceChangedError(error)) { + log.debug('Partial session history completion after switch failed', { sessionId, error }); + recordHistorySessionDiagnosticEvent(sessionId, 'switch_partial_history_completion_failed', { + switchRequestId, + }); + } + }); + } } catch (error) { if (isSurfaceChangedError(error)) { throw error;