Skip to content

The agent that grows with you

fix(desktop): keep the chat composer mounted through transient session loaders

teknium116b749f61f26
diff --git a/apps/desktop/src/app/chat/composer/rich-editor.test.ts b/apps/desktop/src/app/chat/composer/rich-editor.test.ts
index f618b1f9e1c..e9b70501af3 100644
--- a/apps/desktop/src/app/chat/composer/rich-editor.test.ts
+++ b/apps/desktop/src/app/chat/composer/rich-editor.test.ts
@@ -8,6 +8,8 @@ import {
deleteSelectionInEditor,
insertComposerContentsAtCaret,
normalizeComposerEditorDom,
+ placeCaretAtOffset,
+ placeCaretEnd,
refChipElement,
renderComposerContents,
replaceBeforeCaret,
@@ -383,3 +385,27 @@ describe('deleteSelectionInEditor', () => {
editor.remove()
})
})
+
+describe('caret placement on a detached editor', () => {
+ it('leaves the document selection alone instead of selecting into a detached node', () => {
+ const attached = document.createElement('div')
+ attached.textContent = 'visible composer'
+ document.body.append(attached)
+ placeCaretAtEnd(attached)
+
+ const detached = document.createElement('div')
+ detached.dataset.slot = RICH_INPUT_SLOT
+ detached.textContent = 'unmounted composer'
+
+ const selection = window.getSelection()
+ const before = selection?.getRangeAt(0).startContainer
+
+ expect(() => placeCaretEnd(detached)).not.toThrow()
+ expect(() => placeCaretAtOffset(detached, 3)).not.toThrow()
+ expect(selection?.rangeCount).toBe(1)
+ expect(selection?.getRangeAt(0).startContainer).toBe(before)
+ expect(detached.contains(selection?.anchorNode ?? null)).toBe(false)
+
+ attached.remove()
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/rich-editor.ts b/apps/desktop/src/app/chat/composer/rich-editor.ts
index a7d8e040675..9258da250de 100644
--- a/apps/desktop/src/app/chat/composer/rich-editor.ts
+++ b/apps/desktop/src/app/chat/composer/rich-editor.ts
@@ -578,6 +578,14 @@ export function composerPlainText(node: Node): string {
}
export function placeCaretEnd(element: HTMLElement) {
+ // A repaint can land on an editor React has already unmounted (the chat
+ // bar toggles with the thread's loading gate). Selecting into a detached
+ // node throws `addRange(): The given range isn't in document` from inside
+ // the commit phase, and React re-renders in a loop on it (#117285).
+ if (!element.isConnected) {
+ return
+ }
+
const range = document.createRange()
const selection = window.getSelection()
@@ -675,9 +683,9 @@ export function placeCaretAtOffset(editor: HTMLElement, offset: number) {
return null
}
- const range = walk(editor)
+ const range = editor.isConnected ? walk(editor) : null
- if (range) {
+ if (range?.startContainer.isConnected) {
selection.removeAllRanges()
selection.addRange(range)
diff --git a/apps/desktop/src/app/chat/index.tsx b/apps/desktop/src/app/chat/index.tsx
index a4544479da0..85822116dbb 100644
--- a/apps/desktop/src/app/chat/index.tsx
+++ b/apps/desktop/src/app/chat/index.tsx
@@ -73,7 +73,7 @@ import { useRuntimeMessageRepository } from './runtime-repository'
import { ScrollToBottomButton } from './scroll-to-bottom-button'
import { useSessionView } from './session-view'
import { SessionActionsMenu } from './sidebar/session-actions-menu'
-import { routedSessionIsLoading, threadLoadingState } from './thread-loading'
+import { composerStaysMounted, routedSessionIsLoading, threadLoadingState } from './thread-loading'
import {
backfillOlderTranscriptPage,
mergeOlderTranscriptPage,
@@ -641,7 +641,25 @@ const ChatViewContent = memo(function ChatViewContent({
// Hide the composer in the exhausted error state too: there's no live runtime
// to send to until a retry rebinds one. Watch windows are pure spectators of a
// subagent run driven elsewhere — no composer, transcript is read-only.
- const showChatBar = !loadingSession && !resumeExhausted && !isWatchWindow()
+ //
+ // Once this route has rendered with its composer, a later transient loader
+ // (periodic list/status refresh, hydrate through an empty frame) must not
+ // unmount it again — see composerStaysMounted (#117375).
+ const settledRoutedSessionRef = useRef<null | string>(null)
+
+ if (!loadingSession && isRoutedSessionView) {
+ settledRoutedSessionRef.current = routedSessionId
+ } else if (!isRoutedSessionView) {
+ settledRoutedSessionRef.current = null
+ }
+
+ const showChatBar = composerStaysMounted({
+ hideComposer: resumeExhausted || isWatchWindow(),
+ loadingSession,
+ routedSessionId,
+ settledRoutedSessionId: settledRoutedSessionRef.current
+ })
+
const threadKey = selectedSessionId || activeSessionId || (isRoutedSessionView ? location.pathname : 'new')
const modelOptionsQuery = useQuery<ModelOptionsResult>({
diff --git a/apps/desktop/src/app/chat/thread-loading.test.ts b/apps/desktop/src/app/chat/thread-loading.test.ts
index db4d7117a70..c99499fc4e7 100644
--- a/apps/desktop/src/app/chat/thread-loading.test.ts
+++ b/apps/desktop/src/app/chat/thread-loading.test.ts
@@ -2,7 +2,12 @@ import { describe, expect, it } from 'vitest'
import type { ChatMessage } from '@/lib/chat-messages'
-import { lastVisibleMessageIsUser, routedSessionIsLoading, threadLoadingState } from './thread-loading'
+import {
+ composerStaysMounted,
+ lastVisibleMessageIsUser,
+ routedSessionIsLoading,
+ threadLoadingState
+} from './thread-loading'
function message(id: string, role: ChatMessage['role'], hidden = false): ChatMessage {
return {
@@ -62,3 +67,35 @@ describe('routedSessionIsLoading', () => {
).toBe(false)
})
})
+
+describe('composerStaysMounted', () => {
+ it('keeps the composer mounted through a transient loader on the settled route', () => {
+ expect(
+ composerStaysMounted({
+ hideComposer: false,
+ loadingSession: true,
+ routedSessionId: 'stored-1',
+ settledRoutedSessionId: 'stored-1'
+ })
+ ).toBe(true)
+ })
+
+ it('hides the composer while a different route is still loading', () => {
+ expect(
+ composerStaysMounted({
+ hideComposer: false,
+ loadingSession: true,
+ routedSessionId: 'stored-2',
+ settledRoutedSessionId: 'stored-1'
+ })
+ ).toBe(false)
+ expect(
+ composerStaysMounted({
+ hideComposer: true,
+ loadingSession: false,
+ routedSessionId: 'stored-1',
+ settledRoutedSessionId: 'stored-1'
+ })
+ ).toBe(false)
+ })
+})
diff --git a/apps/desktop/src/app/chat/thread-loading.ts b/apps/desktop/src/app/chat/thread-loading.ts
index 6dbfc0a29bb..96efc71d459 100644
--- a/apps/desktop/src/app/chat/thread-loading.ts
+++ b/apps/desktop/src/app/chat/thread-loading.ts
@@ -30,6 +30,38 @@ export function threadLoadingState(
return undefined
}
+/** Whether the chat bar stays mounted for this render.
+ *
+ * The loader can flip on for a session the user is already looking at: a
+ * periodic sidebar / live-status refresh briefly drops the routed row from the
+ * session list, or a hydrate swaps the transcript through an empty frame. Both
+ * read as `loadingSession` for one render and used to unmount the composer,
+ * so the input (draft, attachments, caret, focus) vanished and reappeared
+ * every ~30s (#117375). Once a routed session has rendered with its composer
+ * once, a later transient loader for the SAME route keeps it mounted; only a
+ * route change (or the exhausted / watch-window states) hides it again. */
+export function composerStaysMounted({
+ hideComposer,
+ loadingSession,
+ routedSessionId,
+ settledRoutedSessionId
+}: {
+ hideComposer: boolean
+ loadingSession: boolean
+ routedSessionId: null | string
+ settledRoutedSessionId: null | string
+}): boolean {
+ if (hideComposer) {
+ return false
+ }
+
+ if (!loadingSession) {
+ return true
+ }
+
+ return Boolean(routedSessionId) && routedSessionId === settledRoutedSessionId
+}
+
export function routedSessionIsLoading({
activeSessionId,
knownHistory,