diff --git a/src/renderer/src/components/editor/EditorFileLoadErrorView.tsx b/src/renderer/src/components/editor/EditorFileLoadErrorView.tsx index 6425ca7937f..72c6605aaa1 100644 --- a/src/renderer/src/components/editor/EditorFileLoadErrorView.tsx +++ b/src/renderer/src/components/editor/EditorFileLoadErrorView.tsx @@ -6,7 +6,7 @@ import { formatFileLoadErrorMessage } from './file-load-error-copy' import { displayIpcErrorMessage } from '@/runtime/errors/ipc-error' import { isFileTooLargeCodeMessage } from '../../../../shared/file-write/file-write-disk-precondition' import { WORKTREE_HOST_UNRESOLVED_CODE } from './editor-panel-content-types' -import { useStoppedWslHost } from '../wsl-host-stopped/use-stopped-wsl-host' +import { useWslHostReadOutage } from '../wsl-host-stopped/use-stopped-wsl-host' import { WslHostStoppedNotice } from '../wsl-host-stopped/WslHostStoppedNotice' // Localize by machine code; stored English remains readable for logs without coupling upstream state checks to UI language. @@ -39,20 +39,21 @@ export function EditorFileLoadErrorView({ }: { message: string code?: string - /** The file's workspace; a stopped WSL host there gets Start instead of this error. */ + /** The file's workspace; a stopped or still-connecting WSL host there gets its notice instead of this error. */ worktreeId?: string onRetry: () => void /** Reloads after Start without asking to wake again: the click already booted the host. */ onHostStarted?: () => void }): React.JSX.Element { const t = useTranslate() - const stoppedWslHost = useStoppedWslHost(worktreeId) - if (stoppedWslHost) { + const wslHostOutage = useWslHostReadOutage(worktreeId) + if (wslHostOutage) { return (
diff --git a/src/renderer/src/components/editor/useEditorPanelFileLoadRetry.ts b/src/renderer/src/components/editor/useEditorPanelFileLoadRetry.ts index 77198df270f..a0bad189048 100644 --- a/src/renderer/src/components/editor/useEditorPanelFileLoadRetry.ts +++ b/src/renderer/src/components/editor/useEditorPanelFileLoadRetry.ts @@ -1,6 +1,9 @@ import { useEffect, useRef, type Dispatch, type MutableRefObject, type SetStateAction } from 'react' import type { OpenFile } from '@/store/slices/editor/types/open-file' import { useWorktreeHostConnection } from '@/lib/workspace/ownership/worktree-host-connection-phase' +import { selectWorkspaceRuntimeEnvironmentId } from '@/lib/workspace/ownership/workspace-runtime-environment' +import { useAppStateValue } from '@/store/retained-views/app-state-view' +import type { WslHostConnectionState } from '../../../../shared/wsl-host-types' import { displayIpcErrorMessage } from '@/runtime/errors/ipc-error' import { hasRuntimeRpcErrorCode } from '../../../../shared/runtime-rpc-error-code' import { isFileTooLargeMessage } from '../../../../shared/file-write/file-write-disk-precondition' @@ -60,6 +63,24 @@ export function shouldRetryFileLoadError(message: string, code?: string): boolea ) } +/** + * A WSL host's connection in the SSH phase's terms, so a read that failed while it was down waits + * for it and retries once it answers. Null for any other host. + */ +export function wslHostRetryConnection( + environmentId: string | null, + state: WslHostConnectionState | undefined +): { targetId: string; connecting: boolean; connectedEpoch: string | null } | null { + if (!environmentId || state === undefined) { + return null + } + return { + targetId: `wsl:${environmentId}`, + connecting: state === 'connecting' || state === 'reconnecting', + connectedEpoch: state === 'ready' ? `wsl:${environmentId}` : null + } +} + export function useEditorPanelFileLoadRetry({ activeFile, fileContents, @@ -76,9 +97,16 @@ export function useEditorPanelFileLoadRetry({ ? fileContents[activeFileLoadRetryId]?.loadErrorCode : undefined const hostConnection = useWorktreeHostConnection(activeFile?.worktreeId ?? null) - const hostTargetId = hostConnection.targetId - const hostConnecting = hostConnection.phase === 'connecting' - const connectedHostEpoch = hostConnection.connectedEpoch + const wslEnvironmentId = useAppStateValue((state) => + selectWorkspaceRuntimeEnvironmentId(state, activeFile?.worktreeId) + ) + const wslHostState = useAppStateValue((state) => + wslEnvironmentId ? state.wslHostStatuses[wslEnvironmentId]?.state : undefined + ) + const wslHost = wslHostRetryConnection(wslEnvironmentId, wslHostState) + const hostTargetId = wslHost?.targetId ?? hostConnection.targetId + const hostConnecting = wslHost?.connecting ?? hostConnection.phase === 'connecting' + const connectedHostEpoch = wslHost ? wslHost.connectedEpoch : hostConnection.connectedEpoch const seenHostRef = useRef({ targetId: hostTargetId, connectedEpoch: connectedHostEpoch }) useEffect(() => { diff --git a/src/renderer/src/components/editor/useEditorPanelFileLoadRetry.wsl-host.test.tsx b/src/renderer/src/components/editor/useEditorPanelFileLoadRetry.wsl-host.test.tsx new file mode 100644 index 00000000000..f8a63e1a6f6 --- /dev/null +++ b/src/renderer/src/components/editor/useEditorPanelFileLoadRetry.wsl-host.test.tsx @@ -0,0 +1,115 @@ +// @vitest-environment happy-dom + +import { act, useRef, useState } from 'react' +import { createRoot, type Root } from 'react-dom/client' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type { OpenFile } from '@/store/slices/editor/types/open-file' +import type { WslHostConnectionState } from '../../../../shared/wsl-host-types' +import type { FileContent } from './editor-panel-content-types' + +const host = vi.hoisted((): { state: string | undefined } => ({ state: undefined })) + +vi.mock( + '@/lib/workspace/ownership/worktree-host-connection-phase', + () => import('./local-host-test-fixture') +) +vi.mock('@/lib/workspace/ownership/workspace-runtime-environment', () => ({ + selectWorkspaceRuntimeEnvironmentId: () => 'wsl-env' +})) +vi.mock('@/store/retained-views/app-state-view', () => ({ + useAppStateValue: (select: (state: unknown) => T): T => + select({ + wslHostStatuses: host.state + ? { 'wsl-env': { environmentId: 'wsl-env', distro: 'demo', state: host.state } } + : {} + }) +})) + +import { useEditorPanelFileLoadRetry, wslHostRetryConnection } from './useEditorPanelFileLoadRetry' + +const FILE: OpenFile = { + id: 'file-1', + filePath: '/home/orca/demo/app/main.go', + relativePath: 'main.go', + worktreeId: 'repo::/home/orca/demo/app', + language: 'go', + isDirty: false, + mode: 'edit' +} +const UNREACHABLE = 'Could not connect to the remote Orca runtime.' + +function Probe({ loadFileContent }: { loadFileContent: () => Promise }): null { + const [fileContents, setFileContents] = useState>({ + [FILE.id]: { content: '', isBinary: false, loadError: UNREACHABLE } + }) + const openFilesRef = useRef([FILE]) + const fileLoadRetryAttemptsRef = useRef>({ [FILE.id]: 3 }) + useEditorPanelFileLoadRetry({ + activeFile: FILE, + fileContents, + fileLoadRetryAttemptsRef, + loadFileContent, + openFilesRef, + setFileContents + }) + return null +} + +describe('useEditorPanelFileLoadRetry on a WSL host', () => { + let container: HTMLDivElement + let root: Root + + beforeEach(() => { + vi.useFakeTimers() + container = document.createElement('div') + root = createRoot(container) + }) + + afterEach(() => { + act(() => root.unmount()) + vi.useRealTimers() + }) + + const render = (loadFileContent: () => Promise): void => + act(() => root.render()) + + it('waits while the host connects, then reloads once it answers', async () => { + const loadFileContent = vi.fn(async () => {}) + host.state = 'connecting' + render(loadFileContent) + await act(async () => { + await vi.advanceTimersByTimeAsync(10_000) + }) + expect(loadFileContent).not.toHaveBeenCalled() + + host.state = 'ready' + render(loadFileContent) + expect(loadFileContent).toHaveBeenCalledWith( + FILE.filePath, + FILE.id, + FILE.worktreeId, + FILE.relativePath + ) + }) +}) + +describe('wslHostRetryConnection', () => { + it.each<[WslHostConnectionState, boolean, string | null]>([ + ['connecting', true, null], + ['reconnecting', true, null], + ['ready', false, 'wsl:env-1'], + ['distro-stopped', false, null], + ['failed', false, null] + ])('%s', (state, connecting, connectedEpoch) => { + expect(wslHostRetryConnection('env-1', state)).toEqual({ + targetId: 'wsl:env-1', + connecting, + connectedEpoch + }) + }) + + it('is null for a host that is not a WSL host', () => { + expect(wslHostRetryConnection(null, undefined)).toBeNull() + expect(wslHostRetryConnection('ssh-env', undefined)).toBeNull() + }) +}) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/FileExplorer.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/FileExplorer.tsx index 2900f9ae2c3..9981fad48cf 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/FileExplorer.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/FileExplorer.tsx @@ -41,7 +41,7 @@ import { useFileExplorerBackgroundMenu } from './use-file-explorer-background-me import { useFileExplorerNameFilter } from './use-file-explorer-name-filter' import { useFileExplorerTreePaneState } from '../tree/use-file-explorer-tree-pane-state' import { useWslHostUnavailableReason } from '../../../../runtime/hosts/wsl/wsl-host-availability' -import { useStoppedWslHost } from '../../../wsl-host-stopped/use-stopped-wsl-host' +import { useWslHostReadOutage } from '../../../wsl-host-stopped/use-stopped-wsl-host' import { useTranslate } from '@/i18n/use-translate' import { useUiLanguageRevision } from '@/i18n/use-ui-language-revision' @@ -57,7 +57,7 @@ function FileExplorerFiles(): React.JSX.Element { runtimeEnvironmentIdOrLocalUnlessTwin(s, activeWorktreeId) ) const wslHostUnavailable = useWslHostUnavailableReason(activeWorktreeId) - const stoppedWslHost = useStoppedWslHost(activeWorktreeId) + const stoppedWslHost = useWslHostReadOutage(activeWorktreeId) const activeWorktree = useActiveWorktree() const activeRepo = useRepoById(activeWorktree?.repoId ?? null) // Why: a folder workspace has no repo row, so only the catalog owner names its SSH host. diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/FileExplorerFilesTreePane.tsx b/src/renderer/src/components/right-sidebar/file-explorer/tree/FileExplorerFilesTreePane.tsx index 5e247d7139c..14a84742b89 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/tree/FileExplorerFilesTreePane.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/FileExplorerFilesTreePane.tsx @@ -17,7 +17,7 @@ import type { useFileExplorerSelection } from '../selection/useFileExplorerSelec import type { useFileExplorerTree } from './useFileExplorerTree' import type { useFileExplorerTreePaneState } from './use-file-explorer-tree-pane-state' import type { WslHostUnavailableReason } from '../../../../runtime/hosts/wsl/wsl-host-availability' -import type { StoppedWslHost } from '../../../wsl-host-stopped/use-stopped-wsl-host' +import type { WslHostReadOutage } from '../../../wsl-host-stopped/use-stopped-wsl-host' type FileExplorerFilesTreePaneProps = { activeRepo: Repo | null @@ -39,7 +39,7 @@ type FileExplorerFilesTreePaneProps = { handleExplorerBackgroundContextMenuCapture: (event: React.MouseEvent) => void handleExplorerBackgroundDoubleClick: (event: React.MouseEvent) => void wslHostUnavailable?: WslHostUnavailableReason | null - stoppedWslHost?: StoppedWslHost | null + stoppedWslHost?: WslHostReadOutage | null } export function FileExplorerFilesTreePane({ diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/FileExplorerTreeStatus.tsx b/src/renderer/src/components/right-sidebar/file-explorer/tree/FileExplorerTreeStatus.tsx index 12cbcf2c5ae..69594ffcb3a 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/tree/FileExplorerTreeStatus.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/FileExplorerTreeStatus.tsx @@ -7,7 +7,7 @@ import { getWslHostsTurnedOffLabel, getWslHostsTurnOnLabel } from '../../../sidebar/wsl-host-row-status' -import type { StoppedWslHost } from '../../../wsl-host-stopped/use-stopped-wsl-host' +import type { WslHostReadOutage } from '../../../wsl-host-stopped/use-stopped-wsl-host' import { WslHostStoppedNotice } from '../../../wsl-host-stopped/WslHostStoppedNotice' type FileExplorerTreeStatusProps = { @@ -18,8 +18,8 @@ type FileExplorerTreeStatusProps = { emptyMessage?: string /** The workspace's WSL host is turned off: offer the switch instead of a read error. */ wslHostsTurnedOff?: boolean - /** The workspace's WSL host is stopped: offer Start instead of a read error. */ - stoppedWslHost?: StoppedWslHost | null + /** The workspace's WSL host is stopped or still connecting: its notice instead of a read error. */ + stoppedWslHost?: WslHostReadOutage | null onWslHostStarted?: () => void } @@ -62,6 +62,7 @@ export function FileExplorerTreeStatus({ diff --git a/src/renderer/src/components/right-sidebar/source-control/listing/content-status.tsx b/src/renderer/src/components/right-sidebar/source-control/listing/content-status.tsx index 29efa174c67..3cec349ba3f 100644 --- a/src/renderer/src/components/right-sidebar/source-control/listing/content-status.tsx +++ b/src/renderer/src/components/right-sidebar/source-control/listing/content-status.tsx @@ -7,7 +7,7 @@ import { ConflictSummaryCard, OperationBanner } from './conflict-status-cards' import { EmptyState } from './empty-state' import { StatusRetryButton } from './status-retry-button' import { TooManyChangesBanner } from './too-many-changes-banner' -import { useStoppedWslHost } from '../../../wsl-host-stopped/use-stopped-wsl-host' +import { useWslHostReadOutage } from '../../../wsl-host-stopped/use-stopped-wsl-host' import { WslHostStoppedNotice } from '../../../wsl-host-stopped/WslHostStoppedNotice' import { useSourceControlScope } from '../panel/worktree-scope' import { shortRefLabel } from '../../../../../../shared/source-control/short-ref-label' @@ -62,8 +62,8 @@ export function SourceControlContentStatus({ filterQuery: string }): React.JSX.Element { const t = useTranslate() - // Why: a stopped host can't answer git, so its read error or "clean" result is not the workspace's. - const stoppedWslHost = useStoppedWslHost(worktreeId) + // Why: a stopped or still-connecting host can't answer git, so its read error or "clean" result is not the workspace's. + const stoppedWslHost = useWslHostReadOutage(worktreeId) // Folder sections keep to the plain message and Retry; raw Git output stays in the worktree's own panel. const showRawStatusError = useSourceControlScope() === null return ( @@ -72,6 +72,7 @@ export function SourceControlContentStatus({ )} diff --git a/src/renderer/src/components/wsl-host-stopped/WslHostStoppedNotice.test.tsx b/src/renderer/src/components/wsl-host-stopped/WslHostStoppedNotice.test.tsx new file mode 100644 index 00000000000..5e1c02f613b --- /dev/null +++ b/src/renderer/src/components/wsl-host-stopped/WslHostStoppedNotice.test.tsx @@ -0,0 +1,36 @@ +// @vitest-environment happy-dom + +import { act } from 'react' +import { createRoot, type Root } from 'react-dom/client' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +vi.mock('@/runtime/hosts/wsl/wsl-host-user-start', () => ({ + startWslHostAndReloadProjects: vi.fn(async () => true) +})) + +import { WslHostStoppedNotice } from './WslHostStoppedNotice' + +describe('WslHostStoppedNotice', () => { + let container: HTMLDivElement + let root: Root + + beforeEach(() => { + container = document.createElement('div') + root = createRoot(container) + }) + + afterEach(() => act(() => root.unmount())) + + it('offers Start while the host is stopped', () => { + act(() => root.render()) + expect(container.textContent).toContain('demo is stopped') + expect(container.querySelector('button')?.textContent).toBe('Start') + }) + + it('shows Connecting with no Start while the host boots', () => { + act(() => root.render()) + expect(container.textContent).toContain('Connecting to demo…') + expect(container.querySelector('[data-wsl-host-connecting]')).not.toBeNull() + expect(container.querySelector('button')).toBeNull() + }) +}) diff --git a/src/renderer/src/components/wsl-host-stopped/WslHostStoppedNotice.tsx b/src/renderer/src/components/wsl-host-stopped/WslHostStoppedNotice.tsx index 1d8a7d3589f..b8162d6bc53 100644 --- a/src/renderer/src/components/wsl-host-stopped/WslHostStoppedNotice.tsx +++ b/src/renderer/src/components/wsl-host-stopped/WslHostStoppedNotice.tsx @@ -7,16 +7,21 @@ import { useTranslate } from '@/i18n/use-translate' import { cn } from '@/lib/class-names' import { startWslHostAndReloadProjects } from '@/runtime/hosts/wsl/wsl-host-user-start' -/** " is stopped" with Start, where a surface would otherwise show a read error. Only the click boots (E9). */ +/** + * " is stopped" with Start, where a surface would otherwise show a read error. Only the click + * boots (E9). `connecting` swaps in "Connecting to …" while it boots, so the stale error never shows. + */ export function WslHostStoppedNotice({ environmentId, distro, + connecting = false, size = 'xs', className, onStarted }: { environmentId: string distro: string + connecting?: boolean size?: 'xs' | 'sm' className?: string onStarted?: () => void @@ -48,15 +53,26 @@ export function WslHostStoppedNotice({ className="mb-1 size-7 text-muted-foreground" data-wsl-host-stopped-icon="" /> - - {t('components.native-chat.hostOutage.stopped', '{{distro}} is stopped', { - distro - })} - - + {connecting ? ( + + + {t('components.native-chat.hostOutage.connecting', 'Connecting to {{distro}}…', { + distro + })} + + ) : ( + <> + + {t('components.native-chat.hostOutage.stopped', '{{distro}} is stopped', { + distro + })} + + + + )}
) } diff --git a/src/renderer/src/components/wsl-host-stopped/use-stopped-wsl-host.ts b/src/renderer/src/components/wsl-host-stopped/use-stopped-wsl-host.ts index 320b45092a9..8f510d1b0c5 100644 --- a/src/renderer/src/components/wsl-host-stopped/use-stopped-wsl-host.ts +++ b/src/renderer/src/components/wsl-host-stopped/use-stopped-wsl-host.ts @@ -1,11 +1,16 @@ import { useAppStateValue } from '@/store/retained-views/app-state-view' import { useMemo } from 'react' import type { AppState } from '@/store/types' -import { selectStoppedWslDistro } from '@/runtime/hosts/wsl/wsl-host-stopped-status' +import { + selectConnectingWslDistro, + selectStoppedWslDistro +} from '@/runtime/hosts/wsl/wsl-host-stopped-status' import { selectWorkspaceRuntimeEnvironmentId } from '../../lib/workspace/ownership/workspace-runtime-environment' import { selectWslHostEnvironmentUnavailableReason } from '../../runtime/hosts/wsl/wsl-host-availability' export type StoppedWslHost = { environmentId: string; distro: string } +/** A host a read surface waits on instead of showing its read error; `connecting` once Start (or a reconnect) is under way. */ +export type WslHostReadOutage = StoppedWslHost & { connecting: boolean } /** A stopped distro Start can boot; a host that cannot run (turned off, server files missing) keeps its own notice. */ export function selectStartableStoppedWslDistro( @@ -40,3 +45,27 @@ export function useStoppedWslHost(worktreeId: string | null | undefined): Stoppe [environmentId, distro] ) } + +/** + * For surfaces that mask a read error: a read that failed while the host was down stays stale + * until the host answers, so a connecting host keeps the notice up instead of that error. + */ +export function useWslHostReadOutage( + worktreeId: string | null | undefined +): WslHostReadOutage | null { + const stopped = useStoppedWslHost(worktreeId) + const environmentId = useAppStateValue((state) => + selectWorkspaceRuntimeEnvironmentId(state, worktreeId) + ) + const connectingDistro = useAppStateValue((state) => + selectConnectingWslDistro(state, environmentId) + ) + return useMemo(() => { + if (stopped) { + return { ...stopped, connecting: false } + } + return environmentId && connectingDistro + ? { environmentId, distro: connectingDistro, connecting: true } + : null + }, [stopped, environmentId, connectingDistro]) +} diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 48e2502cb50..2abec80a0f5 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -21169,6 +21169,7 @@ "reconnect": "Reconnect", "stopped": "{{distro}} is stopped", "start": "Start", + "connecting": "Connecting to {{distro}}…", "startFailed": "Couldn’t start {{distro}}. Check it in Settings → Remote Orca Servers.", "unverifiable": "Orca couldn't check whether {{distro}} is running", "startToSend": "Start {{distro}} to send.", diff --git a/src/renderer/src/runtime/hosts/wsl/wsl-host-stopped-status.test.ts b/src/renderer/src/runtime/hosts/wsl/wsl-host-stopped-status.test.ts new file mode 100644 index 00000000000..f7f8c70ecfb --- /dev/null +++ b/src/renderer/src/runtime/hosts/wsl/wsl-host-stopped-status.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from 'vitest' +import type { WslHostConnectionState } from '../../../../../shared/wsl-host-types' +import { selectConnectingWslDistro, selectStoppedWslDistro } from './wsl-host-stopped-status' + +const stateWith = (state: WslHostConnectionState) => ({ + wslHostStatuses: { env: { environmentId: 'env', distro: 'demo', state, detail: null } } +}) + +describe('selectConnectingWslDistro', () => { + it.each<[WslHostConnectionState, string | null]>([ + ['connecting', 'demo'], + ['reconnecting', 'demo'], + ['ready', null], + ['distro-stopped', null], + ['failed', null] + ])('%s', (state, expected) => { + expect(selectConnectingWslDistro(stateWith(state), 'env')).toBe(expected) + }) + + it('is null for an unknown host and never overlaps stopped', () => { + expect(selectConnectingWslDistro(stateWith('connecting'), 'other')).toBeNull() + expect(selectConnectingWslDistro(stateWith('connecting'), null)).toBeNull() + expect(selectStoppedWslDistro(stateWith('connecting'), 'env')).toBeNull() + }) +}) diff --git a/src/renderer/src/runtime/hosts/wsl/wsl-host-stopped-status.ts b/src/renderer/src/runtime/hosts/wsl/wsl-host-stopped-status.ts index 6e814a8ee1e..892431c1a92 100644 --- a/src/renderer/src/runtime/hosts/wsl/wsl-host-stopped-status.ts +++ b/src/renderer/src/runtime/hosts/wsl/wsl-host-stopped-status.ts @@ -9,6 +9,15 @@ export function selectStoppedWslDistro( return status?.state === 'distro-stopped' ? status.distro : null } +/** The distro of a WSL host still connecting, such as after Start while it boots or updates its server. */ +export function selectConnectingWslDistro( + state: Pick, + environmentId: string | null | undefined +): string | null { + const status = environmentId ? state.wslHostStatuses[environmentId] : undefined + return status?.state === 'connecting' || status?.state === 'reconnecting' ? status.distro : null +} + /** * Whether a pane shows the stopped distro's Start card. A pane whose create failed has no server * yet and is still the workspace's; one on another server, or a host that cannot run, keeps its own