Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -560,6 +571,7 @@ export async function switchChatSession(
historyState: session?.historyState,
hasRenderableContent: session ? hasRenderableSessionContent(session) : false,
shouldHydrateBeforeSwitch,
shouldCompletePartialHistory,
shouldActivateBeforeHydrate,
});

Expand Down Expand Up @@ -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;
Expand Down
Loading