diff --git a/src/main/runtime/orca-runtime-headless-hydration-repo-gate.test.ts b/src/main/runtime/orca-runtime-headless-hydration-repo-gate.test.ts new file mode 100644 index 000000000..8bca2c77e --- /dev/null +++ b/src/main/runtime/orca-runtime-headless-hydration-repo-gate.test.ts @@ -0,0 +1,67 @@ +import { describe, expect, it, vi } from 'vitest' +import { FLOATING_TERMINAL_WORKTREE_ID, getDefaultWorkspaceSession } from '../../shared/constants' +import type { WorkspaceSessionState } from '../../shared/types' +import { OrcaRuntimeService } from './orca-runtime' + +const WORKTREE_ID = 'repo::/worktree' +const REPO_ID = 'repo' + +function makeSession(worktreeId: string): WorkspaceSessionState { + return { + ...getDefaultWorkspaceSession(), + tabsByWorktree: { + [worktreeId]: [ + { + id: 'tab', + ptyId: 'pty-1', + worktreeId, + title: 'Terminal', + customTitle: null, + color: null, + sortOrder: 0, + createdAt: 1 + } + ] + } + } +} + +// The repo gate (#9343) skips persisted session keys whose repo is gone. These +// pin the two ways that gate must not overreach. +describe('headless mobile session hydration repo gate', () => { + it('hydrates when the store cannot report repos at all', async () => { + const runtime = new OrcaRuntimeService({ + getWorkspaceSession: () => makeSession(WORKTREE_ID) + } as never) + + // No getRepos on the store: an unavailable inventory must not read as + // "every repo is gone" and silently drop every persisted tab. + const result = await runtime.listMobileSessionTabs(`id:${WORKTREE_ID}`) + expect(result.tabs.length).toBeGreaterThan(0) + }) + + it('still skips a key whose repo is absent from a known inventory', async () => { + const runtime = new OrcaRuntimeService({ + getWorkspaceSession: () => makeSession('ghost-repo::/worktree'), + getRepos: () => [{ id: REPO_ID, path: '/repo', name: 'repo' }] + } as never) + + const result = await runtime.listMobileSessionTabs('id:ghost-repo::/worktree') + expect(result.tabs).toEqual([]) + }) + + it('does not read the repo inventory for an unparseable worktree id', async () => { + const getRepos = vi.fn(() => [{ id: REPO_ID, path: '/repo', name: 'repo' }]) + const runtime = new OrcaRuntimeService({ + getWorkspaceSession: () => makeSession(FLOATING_TERMINAL_WORKTREE_ID), + // A separator-less id is validated against getRepo (singular) first. + getRepo: () => null, + getRepos + } as never) + + // Floating terminals carry no `repoId::path` identity, and this hydrate runs + // on a hot poll path — it must not enumerate repos. + await runtime.listMobileSessionTabs(`id:${FLOATING_TERMINAL_WORKTREE_ID}`) + expect(getRepos).not.toHaveBeenCalled() + }) +}) diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index c82a6383b..43fce4b4f 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -4118,11 +4118,20 @@ export class OrcaRuntimeService { // or a stale browser-persisted session). Hydrating such a key would surface a // phantom "unknown"/duplicate workspace with no live repo behind it. Only // hydrate sessions whose repo still exists; leave unparseable keys alone. - const liveRepoIds = new Set((this.store?.getRepos?.() ?? []).map((repo) => repo.id)) + // Resolved lazily so unparseable keys (floating terminals) never pay for a + // repo inventory on the hot poll path, and `null` when the store cannot + // report repos — an unavailable list must not read as "every repo is gone". + let liveRepoIds: Set | null | undefined for (const [entryWorktreeId, persistedTabs] of entries) { const ownerRepoId = splitWorktreeIdForFilesystem(entryWorktreeId)?.repoId - if (ownerRepoId && !liveRepoIds.has(ownerRepoId)) { - continue + if (ownerRepoId) { + if (liveRepoIds === undefined) { + const knownRepos = this.store?.getRepos?.() + liveRepoIds = knownRepos ? new Set(knownRepos.map((repo) => repo.id)) : null + } + if (liveRepoIds && !liveRepoIds.has(ownerRepoId)) { + continue + } } const existing = this.mobileSessionTabsByWorktree.get(entryWorktreeId) if ( diff --git a/src/renderer/src/components/editor/RichMarkdownEditor.tsx b/src/renderer/src/components/editor/RichMarkdownEditor.tsx index 49a7db12c..94174b735 100644 --- a/src/renderer/src/components/editor/RichMarkdownEditor.tsx +++ b/src/renderer/src/components/editor/RichMarkdownEditor.tsx @@ -24,7 +24,7 @@ import { runRichMarkdownContextCommand } from './rich-markdown-context-command-routing' import { useRichMarkdownSpellcheckAttribute } from './rich-markdown-spellcheck' -import { autoFocusRichEditor } from './rich-markdown-auto-focus' +import { useRichMarkdownPendingFocus } from './useRichMarkdownPendingFocus' import { useRichMarkdownSuperscriptLinkSetup } from './useRichMarkdownSuperscriptLinkSetup' import { formatSelectedHtmlSuperscriptLinkStatus, @@ -58,11 +58,6 @@ export default function RichMarkdownEditor({ const richMarkdownSpellcheckEnabled = settings?.richMarkdownSpellcheckEnabled ?? true const editorFontZoomLevel = useAppStore((s) => s.editorFontZoomLevel) const activateMarkdownLink = useAppStore((s) => s.activateMarkdownLink) - const pendingEditorFocusRequest = useAppStore((s) => { - const request = s.pendingEditorFocusRequest - return request?.fileId === fileId && request.worktreeId === worktreeId ? request : null - }) - const consumeEditorFocusRequest = useAppStore((s) => s.consumeEditorFocusRequest) const addDiffComment = useAppStore((s) => s.addDiffComment) const deleteDiffComment = useAppStore((s) => s.deleteDiffComment) const updateDiffComment = useAppStore((s) => s.updateDiffComment) @@ -245,14 +240,7 @@ export default function RichMarkdownEditor({ setDocLinkMenu: menu.setDocLinkMenu }) - useEffect(() => { - if (!editor || !pendingEditorFocusRequest) { - return - } - cancelAutoFocusRef.current?.() - cancelAutoFocusRef.current = autoFocusRichEditor(editor, rootRef.current, true) - consumeEditorFocusRequest(pendingEditorFocusRequest.token) - }, [consumeEditorFocusRequest, editor, pendingEditorFocusRequest]) + useRichMarkdownPendingFocus({ editor, fileId, worktreeId, rootRef, cancelAutoFocusRef }) // Why: useEditor defaults shouldRerenderOnTransaction to false, so selection-only // citation NodeSelections would leave aria status stale without useEditorState. diff --git a/src/renderer/src/components/editor/useRichMarkdownPendingFocus.ts b/src/renderer/src/components/editor/useRichMarkdownPendingFocus.ts new file mode 100644 index 000000000..372c22e64 --- /dev/null +++ b/src/renderer/src/components/editor/useRichMarkdownPendingFocus.ts @@ -0,0 +1,39 @@ +import { useEffect, type RefObject } from 'react' +import type { Editor } from '@tiptap/react' +import { useAppStore } from '@/store' +import { autoFocusRichEditor } from './rich-markdown-auto-focus' + +type PendingFocusOptions = { + editor: Editor | null + fileId: string + worktreeId: string + rootRef: RefObject + cancelAutoFocusRef: RefObject<(() => void) | null> +} + +/** + * Focuses the editor when the Explorer opens this document for find (issue #8083), then consumes + * the request so a later remount of the same file does not steal focus again. + */ +export function useRichMarkdownPendingFocus({ + editor, + fileId, + worktreeId, + rootRef, + cancelAutoFocusRef +}: PendingFocusOptions): void { + const pendingEditorFocusRequest = useAppStore((s) => { + const request = s.pendingEditorFocusRequest + return request?.fileId === fileId && request.worktreeId === worktreeId ? request : null + }) + const consumeEditorFocusRequest = useAppStore((s) => s.consumeEditorFocusRequest) + + useEffect(() => { + if (!editor || !pendingEditorFocusRequest) { + return + } + cancelAutoFocusRef.current?.() + cancelAutoFocusRef.current = autoFocusRichEditor(editor, rootRef.current, true) + consumeEditorFocusRequest(pendingEditorFocusRequest.token) + }, [cancelAutoFocusRef, consumeEditorFocusRequest, editor, pendingEditorFocusRequest, rootRef]) +}