The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): isolate hidden composer selection
Jerry Gooch26ed08c23e50
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsxindex a42edda424b..90a93a6e596 100644--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx@@ -9,6 +9,7 @@ import { $connection } from '@/store/session'import { useComposerActions } from '../../hooks/use-composer-actions'import type { QueueEditState } from '../composer-utils'import { type ComposerTarget, getActiveComposer, markActiveComposer } from '../focus'+import { composerPlainText } from '../rich-editor'import { type ComposerScope, ComposerScopeProvider, MAIN_COMPOSER_SCOPE } from '../scope'import { useComposerDraft } from './use-composer-draft'@@ -350,4 +351,94 @@ describe('useComposerDraft — a hidden keep-alive tab never auto-focuses its coexpect(getActiveComposer()).toBe('tile:vis')})++ function renderHiddenDraftHarness() {+ let hiddenDraft!: ReturnType<typeof useComposerDraft>++ function HiddenDraftHarness() {+ hiddenDraft = useComposerDraft({+ activeQueueSessionKey: 'session-hidden',+ focusKey: null,+ inputDisabled: false,+ queueEditRef: { current: null as QueueEditState | null },+ sessionId: 'session-hidden'+ })++ return <div contentEditable data-slot="composer-rich-input" ref={hiddenDraft.editorRef} />+ }++ render(+ <PaneVisibleContext.Provider value={false}>+ <ComposerScopeProvider value={{ ...MAIN_COMPOSER_SCOPE, target: 'tile:hidden' }}>+ <HiddenDraftHarness />+ </ComposerScopeProvider>+ </PaneVisibleContext.Provider>+ )++ return () => hiddenDraft+ }++ function createForegroundSelection() {+ const visibleEditor = globalThis.document.createElement('div')+ const visibleText = globalThis.document.createTextNode('foreground draft')+ visibleEditor.contentEditable = 'true'+ visibleEditor.tabIndex = 0+ visibleEditor.appendChild(visibleText)+ globalThis.document.body.appendChild(visibleEditor)+ visibleEditor.focus()+ expect(globalThis.document.activeElement).toBe(visibleEditor)++ const range = globalThis.document.createRange()+ range.setStart(visibleText, visibleText.textContent?.length ?? 0)+ range.collapse(true)+ window.getSelection()?.removeAllRanges()+ window.getSelection()?.addRange(range)++ return {+ editor: visibleEditor,+ startContainer: range.startContainer,+ startOffset: range.startOffset,+ endContainer: range.endContainer,+ endOffset: range.endOffset+ }+ }++ function expectForegroundSelectionPreserved(foreground: ReturnType<typeof createForegroundSelection>) {+ const selection = window.getSelection()+ expect(globalThis.document.activeElement).toBe(foreground.editor)+ expect(selection?.rangeCount).toBe(1)++ const range = selection!.getRangeAt(0)+ expect(range.startContainer).toBe(foreground.startContainer)+ expect(range.startOffset).toBe(foreground.startOffset)+ expect(range.endContainer).toBe(foreground.endContainer)+ expect(range.endOffset).toBe(foreground.endOffset)+ }++ it('does not move the document selection when a hidden composer reloads or clears its draft', () => {+ const getHiddenDraft = renderHiddenDraftHarness()+ const foreground = createForegroundSelection()++ act(() => getHiddenDraft().loadIntoComposer('background update', []))++ expect(composerPlainText(getHiddenDraft().editorRef.current!)).toBe('background update')+ expectForegroundSelectionPreserved(foreground)++ act(() => getHiddenDraft().clearDraft())++ expect(getHiddenDraft().editorRef.current?.textContent).toBe('')+ expectForegroundSelectionPreserved(foreground)+ foreground.editor.remove()+ })++ it('does not move the document selection when hidden composer refs are requested', () => {+ const getHiddenDraft = renderHiddenDraftHarness()+ const foreground = createForegroundSelection()++ act(() => getHiddenDraft().insertInlineRefs(['@file:`src/background.ts`']))++ expect(composerPlainText(getHiddenDraft().editorRef.current!)).toContain('@file:`src/background.ts`')+ expectForegroundSelectionPreserved(foreground)+ foreground.editor.remove()+ })})diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.tsindex 67e9b47aac9..4b04f6925cf 100644--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts@@ -147,14 +147,22 @@ export function useComposerDraft({if (editor) {renderComposerContents(editor, next, { trailingCommitted: true })- placeCaretEnd(editor)++ // Selection is document-global: a keep-alive composer in a hidden tab+ // may repaint when its background session updates, but moving its caret+ // here steals the selection from the visible composer without changing+ // document.activeElement. The foreground then still looks focused while+ // printable keydowns produce no input.+ if (paneVisible) {+ placeCaretEnd(editor)+ }}if (focus) {requestMainFocus()}},- [requestMainFocus, setComposerText]+ [paneVisible, requestMainFocus, setComposerText])const appendExternalText = useCallback(@@ -263,9 +271,12 @@ export function useComposerDraft({if (editorRef.current) {renderComposerContents(editorRef.current, '')- placeCaretEnd(editorRef.current)++ if (paneVisible) {+ placeCaretEnd(editorRef.current)+ }}- }, [setComposerText])+ }, [paneVisible, setComposerText])// Read the editor's current plain text into draftRef + composer state. This// closes the "queued rAF flush hasn't run yet" window so scope-swap/pagehide@@ -364,7 +375,7 @@ export function useComposerDraft({return false}- const nextDraft = insertInlineRefsIntoEditor(editor, refs)+ const nextDraft = insertInlineRefsIntoEditor(editor, refs, { interactive: paneVisible })if (nextDraft === null) {return false@@ -372,7 +383,10 @@ export function useComposerDraft({draftRef.current = nextDraftsetComposerText(nextDraft)- requestMainFocus()++ if (paneVisible) {+ requestMainFocus()+ }return true}diff --git a/apps/desktop/src/app/chat/composer/inline-refs.ts b/apps/desktop/src/app/chat/composer/inline-refs.tsindex 618ce69b3f6..a50d389e806 100644--- a/apps/desktop/src/app/chat/composer/inline-refs.ts+++ b/apps/desktop/src/app/chat/composer/inline-refs.ts@@ -134,7 +134,11 @@ function buildRefFragment(return fragment}-export function insertInlineRefsIntoEditor(editor: HTMLDivElement, refs: readonly InlineRefInput[]) {+export function insertInlineRefsIntoEditor(+ editor: HTMLDivElement,+ refs: readonly InlineRefInput[],+ { interactive = true }: { interactive?: boolean } = {}+) {const parsed = refs.map(parseInlineRef).filter((ref): ref is NonNullable<typeof ref> => ref !== null)const hasEmptySentinel = editor.childNodes.length === 1 && editor.firstChild?.nodeName === 'BR'@@ -146,9 +150,11 @@ export function insertInlineRefsIntoEditor(editor: HTMLDivElement, refs: readonleditor.replaceChildren()}- editor.focus({ preventScroll: true })+ if (interactive) {+ editor.focus({ preventScroll: true })+ }- const selection = window.getSelection()+ const selection = interactive ? window.getSelection() : nullconst range =!hasEmptySentinel && selection?.rangeCount && editor.contains(selection.getRangeAt(0).commonAncestorContainer)@@ -177,7 +183,10 @@ export function insertInlineRefsIntoEditor(editor: HTMLDivElement, refs: readonlneedsBeforeSpace: current.length > 0 && !/\s$/.test(current)}))- placeCaretEnd(editor)++ if (interactive) {+ placeCaretEnd(editor)+ }}normalizeComposerEditorDom(editor)diff --git a/website/docs/user-guide/desktop.md b/website/docs/user-guide/desktop.mdindex 0ab8acd2618..e9e284fc49e 100644--- a/website/docs/user-guide/desktop.md+++ b/website/docs/user-guide/desktop.md@@ -46,6 +46,7 @@ The center of the app. You get:- **Markdown line breaks** follow Markdown semantics: two trailing spaces create a hard line break; an ordinary newline stays a soft break. Media and preview extraction preserve text outside removed attachment spans, including first-line code indentation and unfinished fenced-code spacing. Code display and Copy preserve leading blank lines, trailing spaces, and terminal blank lines from the Markdown parser.- **The same conversation history** as every other Hermes surface — sessions started here resume in the CLI/TUI and vice versa.- **Drag-and-drop files** anywhere in the chat area to attach them to your next message.+- **Independent background drafts** — hidden chat tabs can update their drafts without moving the caret or selection in the visible composer.- **A right-hand preview rail** — render web pages, files, and tool outputs side by side while you keep chatting.- **Comment mode in the in-app browser** — click **Annotate** in the preview browser bar, then click any element (or drag a box) on the live page and type a note; each saved comment stays as a numbered pin on the page. Saving a pin never sends a turn — when you're done, **Add N comments** attaches a cropped screenshot per pin and a short prompt naming each comment to the composer, and you still hit send yourself. Each element comment carries its CSS selector, its markup, and the computed styles that matter for layout, so the agent can find the element in your source instead of guessing from the picture. Password and hidden field values, and any attribute that looks like a key or token, are redacted on the page before the markup leaves it. Larger batches arrive grouped by which part of the page each comment sits in, so twenty-odd comments become a handful of pieces of work rather than one task each — and because the groups are separate DOM subtrees they usually touch separate files, which is what makes handing them to parallel workers safe. Pin numbers hold steady if you delete one, and switching chats clears the stack.- **Composer history and queue editing** — press the up/down arrow keys in an empty composer to recall and reuse previous prompts, and edit messages you've queued up before they're sent. Pressing Stop (or Esc) while turns are queued pauses the queue and expands it above the composer; resume it from there, or send, edit, and delete individual entries.