fix(desktop): preserve routed transcript during selection churn

This commit is contained in:
KoNit-K
2026-09-12 23:44:46 +08:00
committed by Teknium
parent fedaad0cc4
commit 3bad7e72db
3 changed files with 85 additions and 7 deletions
+14 -2
View File
@@ -49,7 +49,7 @@ import {
sessionPinId,
shouldMigrateComposerScope
} from '@/store/session'
import { $focusedStoredSessionId, sessionTileDelegate } from '@/store/session-states'
import { $focusedStoredSessionId, $sessionStates, sessionTileDelegate } from '@/store/session-states'
import { $transcriptTailBySessionId, transcriptTailState } from '@/store/transcript-tail'
import { isAuxiliaryWindow, isWatchWindow } from '@/store/windows'
@@ -420,6 +420,11 @@ const ChatViewContent = memo(function ChatViewContent({
const composerSurfaceId = useComposerSurfaceId()
const isPrimary = view.kind === 'primary'
const activeSessionId = useStore(view.$runtimeId)
const transcriptStoredSessionId = useStoreSelector($sessionStates, states =>
activeSessionId ? (states[activeSessionId]?.storedSessionId ?? null) : null
)
const storedId = useStore(view.$storedId)
// Multi-pane dimming: only the focused surface paints at full strength, so
// two sessions side by side read as "this one, and that one over there".
@@ -513,7 +518,14 @@ const ChatViewContent = memo(function ChatViewContent({
// direct nav). Derived in render so the swap reads instantly: the same frame
// the id changes we drop the old transcript and show the loader, instead of
// waiting for the resume effect (which paints a frame later) to clear them.
const routeSessionMismatch = isPrimary ? isRouteSessionMismatch(routedSessionId, selectedSessionId, sessions) : false
const routeSessionMismatch = isPrimary
? isRouteSessionMismatch(routedSessionId, selectedSessionId, sessions, {
activeRuntimeId: activeSessionId,
contextSwitching: Boolean(gatewaySwapTarget),
messagesEmpty,
transcriptStoredSessionId
})
: false
// The compact new-session pop-out skips the wordmark/tagline intro — it's a
// scratch window, not the full-height empty state. The Appearance toggle
@@ -1,5 +1,7 @@
import { describe, expect, it } from 'vitest'
import { routeSessionId, sessionRoute } from '../routes'
import { isRouteSessionMismatch } from './route-session-state'
describe('isRouteSessionMismatch', () => {
@@ -21,4 +23,44 @@ describe('isRouteSessionMismatch', () => {
expect(isRouteSessionMismatch('same', 'same', [])).toBe(false)
expect(isRouteSessionMismatch(null, 'same', [])).toBe(false)
})
it('keeps only the routed chat whose active view owns an existing transcript during selection churn', () => {
const routedSessionId = routeSessionId(sessionRoute('session-a'))
const sessions = [
{ id: 'session-a', _lineage_root_id: null },
{ id: 'session-b', _lineage_root_id: null }
]
const activeTranscript = {
activeRuntimeId: 'runtime-a',
contextSwitching: false,
messagesEmpty: false,
transcriptStoredSessionId: 'session-a'
}
expect(isRouteSessionMismatch(routedSessionId, null, sessions, activeTranscript)).toBe(false)
expect(isRouteSessionMismatch(routedSessionId, 'session-b', sessions, activeTranscript)).toBe(false)
expect(
isRouteSessionMismatch('session-b', 'session-a', sessions, activeTranscript),
'genuine navigation must suppress session A'
).toBe(true)
expect(
isRouteSessionMismatch(routedSessionId, null, sessions, { ...activeTranscript, contextSwitching: true }),
'profile or connection switches must not retain the prior context'
).toBe(true)
expect(
isRouteSessionMismatch(routedSessionId, null, sessions, { ...activeTranscript, messagesEmpty: true }),
'a route with no prior transcript must keep loading'
).toBe(true)
expect(
isRouteSessionMismatch(routedSessionId, null, sessions, {
...activeTranscript,
activeRuntimeId: 'runtime-b',
transcriptStoredSessionId: 'session-b'
}),
'a background chat must never publish into the routed foreground'
).toBe(true)
})
})
@@ -1,6 +1,13 @@
import { sessionMatchesStoredId } from '@/store/session'
import type { SessionInfo } from '@/types/hermes'
interface ActiveTranscriptState {
activeRuntimeId: null | string
contextSwitching: boolean
messagesEmpty: boolean
transcriptStoredSessionId: null | string
}
/**
* Whether the route points at a different conversation than the selected view.
*
@@ -14,17 +21,34 @@ import type { SessionInfo } from '@/types/hermes'
export function isRouteSessionMismatch(
routedSessionId: null | string,
selectedSessionId: null | string,
sessions: readonly Pick<SessionInfo, '_lineage_root_id' | 'id'>[]
sessions: readonly Pick<SessionInfo, '_lineage_root_id' | 'id'>[],
activeTranscript?: ActiveTranscriptState
): boolean {
if (!routedSessionId || routedSessionId === selectedSessionId) {
if (!routedSessionId) {
return false
}
if (!selectedSessionId) {
if (activeTranscript?.contextSwitching) {
return true
}
return !sessions.some(
session => sessionMatchesStoredId(session, routedSessionId) && sessionMatchesStoredId(session, selectedSessionId)
const matchesRoute = (storedSessionId: null | string) =>
storedSessionId === routedSessionId ||
Boolean(
storedSessionId &&
sessions.some(
session =>
sessionMatchesStoredId(session, routedSessionId) && sessionMatchesStoredId(session, storedSessionId)
)
)
if (matchesRoute(selectedSessionId)) {
return false
}
return !(
activeTranscript?.activeRuntimeId &&
!activeTranscript.messagesEmpty &&
matchesRoute(activeTranscript.transcriptStoredSessionId)
)
}