Skip to content

The agent that grows with you

feat(desktop-sdk): data-session-switching marker on the chat surface

teknium1b33c32fcf32c
diff --git a/apps/desktop/src/components/assistant-ui/thread/index.tsx b/apps/desktop/src/components/assistant-ui/thread/index.tsx
index a1a27da760f..e33a77b2257 100644
--- a/apps/desktop/src/components/assistant-ui/thread/index.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/index.tsx
@@ -189,6 +189,7 @@ export const Thread = memo(function Thread({
scrollProfile={scrollProfile}
sessionId={sessionId}
sessionKey={sessionKey}
+ sessionLoading={loading === 'session'}
/>
{loading === 'session' && <CenteredThreadSpinner />}
<ThreadTimeline />
diff --git a/apps/desktop/src/components/assistant-ui/thread/list-session-scroll.test.tsx b/apps/desktop/src/components/assistant-ui/thread/list-session-scroll.test.tsx
index a37e8cad504..0bad18daf20 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list-session-scroll.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/list-session-scroll.test.tsx
@@ -16,6 +16,7 @@ import {
import { stubThreadEnvironment, stubThreadViewportSize } from '../test-utils'
+import { SESSION_SWITCHING_ATTRIBUTE } from './session-switching'
import { TranscriptWindowProvider, type TranscriptWindowValue } from './transcript-window'
import { Thread } from '.'
@@ -127,6 +128,28 @@ function ScrollHarness({
}
describe('list session-scroll restore', () => {
+ it('marks the chat surface data-session-switching while a switched transcript settles, then clears it', async () => {
+ // The documented theme hook for chat-switch transitions: present from the
+ // commit the new transcript lands until its scroll restore settles.
+ const surface = (key: string) => (
+ <div data-chat-surface="">
+ <ScrollHarness messages={sessionMessages(key, 3)} sessionKey={key} />
+ </div>
+ )
+
+ const { container, rerender } = render(surface('a'))
+ const root = container.querySelector<HTMLElement>('[data-chat-surface]')!
+
+ await settleScroll(10)
+ expect(root.getAttribute(SESSION_SWITCHING_ATTRIBUTE)).toBeNull()
+
+ rerender(surface('b'))
+ expect(root.getAttribute(SESSION_SWITCHING_ATTRIBUTE)).toBe('true')
+
+ await settleScroll(10)
+ expect(root.getAttribute(SESSION_SWITCHING_ATTRIBUTE)).toBeNull()
+ })
+
it('keeps a bottom-pinned reader pinned while a running turn grows the content (#118482)', async () => {
// use-stick-to-bottom follows a content resize on the next animation frame,
// so streamed growth paints at the stale scrollTop and the viewport drifts
diff --git a/apps/desktop/src/components/assistant-ui/thread/list.tsx b/apps/desktop/src/components/assistant-ui/thread/list.tsx
index 903bf29fd0f..f458c54438b 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/list.tsx
@@ -47,6 +47,7 @@ import { MessageRenderBoundary } from '../message-render-boundary'
import { PendingApprovalStack } from '../tool/approval'
import { responseMessageRole, ResponseMessages } from './response-group'
+import { holdSessionSwitching } from './session-switching'
import { resolveShowEarlierAction, shouldAutoShowEarlier, useTranscriptWindow } from './transcript-window'
import { useMessagesBelow } from './use-messages-below'
import { useStickyPromptClip } from './use-sticky-prompt-clip'
@@ -266,6 +267,9 @@ interface ThreadMessageListProps {
loadingIndicator?: ReactNode
sessionId?: string | null
sessionKey?: string | null
+ /** A routed session is still loading into this surface (the thread spinner
+ * is up). Holds `data-session-switching` for that phase. */
+ sessionLoading?: boolean
scrollProfile?: string
}
@@ -465,7 +469,8 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
loadingIndicator,
sessionId = null,
scrollProfile,
- sessionKey
+ sessionKey,
+ sessionLoading = false
}) => {
// TWO signatures, deliberately split. The STRUCTURAL one (ids/roles/count)
// changes only when messages are added/removed/swapped — it keys the error
@@ -1030,6 +1035,9 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
applyRestoreRef.current()
loadSettledRef.current = false
+ // The rows are in and about to be glued to their target: theme CSS may
+ // hide this phase (data-session-switching) so the jump never paints.
+ const releaseSwitching = holdSessionSwitching(el)
// An anchor captured for the OUTGOING transcript must not be applied to
// this one — a switch owns the position outright. The empty→non-empty
@@ -1065,6 +1073,8 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
// the old 90-frame ceiling was for slow async image loads. Cap at 15
// frames to minimize the settle-loop racing markdown paint on every switch.
if (stableFrames >= 2 || ++frame > 15) {
+ releaseSwitching()
+
if (target.kind === 'bottom') {
// Hand back to use-stick-to-bottom locked, so late async growth
// (images, highlight) keeps following the bottom.
@@ -1128,6 +1138,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
applyRestoreRef.current = null
resizeObserver.disconnect()
cancelAnimationFrame(rafId)
+ releaseSwitching()
if (loadSettledRef.current) {
return
@@ -1207,6 +1218,7 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
el.removeEventListener('pointerdown', cancelRestore)
el.removeEventListener('keydown', cancelRestore)
cancelAnimationFrame(rafId)
+ releaseSwitching()
record()
}
}, [
@@ -1220,6 +1232,16 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
stopScroll
])
+ // The load phase of a switch: the route names a session whose transcript
+ // has not arrived. The restore loop above takes over the marker when it does.
+ useLayoutEffect(() => {
+ if (!sessionLoading || !paneVisible) {
+ return
+ }
+
+ return holdSessionSwitching(scrollRef.current)
+ }, [paneVisible, scrollRef, sessionLoading])
+
// A thread can mount with a run already active, without a runStart event.
useEffect(() => {
if (isRunning) {
diff --git a/apps/desktop/src/components/assistant-ui/thread/session-switching.ts b/apps/desktop/src/components/assistant-ui/thread/session-switching.ts
new file mode 100644
index 00000000000..0ef84de0dba
--- /dev/null
+++ b/apps/desktop/src/components/assistant-ui/thread/session-switching.ts
@@ -0,0 +1,39 @@
+/**
+ * `data-session-switching` — the supported hook for styling a chat switch.
+ *
+ * Core sets it on the chat surface root (`[data-chat-surface]`, one per pane:
+ * the primary chat and every tile) while that surface is placing a transcript:
+ * from the frame a routed session starts loading, or a different transcript
+ * lands, until its rows are on screen and the restored scroll position has
+ * settled. Theme CSS may target the attribute (hide the transcript while it is
+ * set, fade it in when it goes); internal class names and the surface's inner
+ * structure are not a contract.
+ *
+ * Several phases can hold the marker at once (the load, then the scroll
+ * restore). Each holder releases only itself, and the attribute goes when the
+ * last one does, so a phase ending can never clear another's mark mid-switch.
+ */
+export const SESSION_SWITCHING_ATTRIBUTE = 'data-session-switching'
+
+const holders = new WeakMap<HTMLElement, Set<symbol>>()
+
+/** Mark the chat surface containing `from` as switching; returns the release. */
+export function holdSessionSwitching(from: HTMLElement | null): () => void {
+ const surface = from?.closest<HTMLElement>('[data-chat-surface]')
+
+ if (!surface) {
+ return () => {}
+ }
+
+ const held = holders.get(surface) ?? new Set<symbol>()
+ const token = Symbol('session-switch')
+ holders.set(surface, held)
+ held.add(token)
+ surface.setAttribute(SESSION_SWITCHING_ATTRIBUTE, 'true')
+
+ return () => {
+ if (held.delete(token) && held.size === 0) {
+ surface.removeAttribute(SESSION_SWITCHING_ATTRIBUTE)
+ }
+ }
+}