fix(runtime): stop the headless hydration repo gate from dropping every tab (#10437)

* fix(runtime): stop the headless hydration repo gate from dropping every tab

#9343 gated headless mobile-session hydration on the live repo list, but read
it as `this.store?.getRepos?.() ?? []`. A store that cannot report repos then
yields an empty set, which reads as "every repo is gone" and skips every
parseable session key — so no tabs hydrate at all. It also called getRepos on
every hydrate, including the hot floating-tab poll path that is contractually
free of repo/provider inventory work.

Resolve the inventory lazily and only for keys that parse to a repoId, and keep
`null` (unavailable) distinct from an empty list (all repos really gone). An
absent list now fails open; a known list still prunes as #9343 intended.

Fixes 3 tests that have been failing on main since #9343 landed:
orca-runtime-terminal-retirement (2) and orca-runtime (1).

* refactor(editor): extract the pending-focus effect to clear the max-lines cap

#8083 pushed RichMarkdownEditor.tsx to 404 counted lines against the 400-line
.tsx cap, failing lint for every PR that merges current main. The Explorer
find-focus request is self-contained, so it moves to its own hook.
This commit is contained in:
Brennan Benson
2026-07-24 13:25:15 -07:00
committed by GitHub
parent 772081577e
commit 20ceeeb0f4
4 changed files with 120 additions and 17 deletions
@@ -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()
})
})
+12 -3
View File
@@ -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<string> | 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 (
@@ -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.
@@ -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<HTMLDivElement | null>
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])
}