From c455c576aee1e796a4ca86fc0ecb8dbd9e3c041a Mon Sep 17 00:00:00 2001 From: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com> Date: Fri, 12 Jun 2026 20:06:20 -0700 Subject: [PATCH] Fallback to system SSH on reachability errors (#5303) Co-authored-by: Orca --- src/main/ssh/ssh-connection-utils.test.ts | 23 +++++++++++ src/main/ssh/ssh-connection-utils.ts | 10 +++++ src/main/ssh/ssh-connection.test.ts | 50 ++++++++++++++++++++++- src/main/ssh/ssh-connection.ts | 15 +++++++ 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/main/ssh/ssh-connection-utils.test.ts b/src/main/ssh/ssh-connection-utils.test.ts index 0af7103b1..e12cb06dc 100644 --- a/src/main/ssh/ssh-connection-utils.test.ts +++ b/src/main/ssh/ssh-connection-utils.test.ts @@ -31,6 +31,7 @@ vi.mock('fs', () => ({ import { isTransientError, + isSystemSshFallbackError, isAuthError, isAgentFallbackError, sleep, @@ -144,6 +145,28 @@ describe('isTransientError', () => { }) }) +// ── isSystemSshFallbackError ───────────────────────────────────────── + +describe('isSystemSshFallbackError', () => { + it('returns true for local reachability errors that system ssh may bypass', () => { + const hostErr = new Error('host unreachable') as NodeJS.ErrnoException + hostErr.code = 'EHOSTUNREACH' + const netErr = new Error('net unreachable') as NodeJS.ErrnoException + netErr.code = 'ENETUNREACH' + + expect(isSystemSshFallbackError(hostErr)).toBe(true) + expect(isSystemSshFallbackError(netErr)).toBe(true) + }) + + it('returns false for transient errors that should keep the normal retry path', () => { + const refused = new Error('refused') as NodeJS.ErrnoException + refused.code = 'ECONNREFUSED' + + expect(isSystemSshFallbackError(refused)).toBe(false) + expect(isSystemSshFallbackError(new Error('connect ETIMEDOUT 1.2.3.4:22'))).toBe(false) + }) +}) + // ── isAuthError ────────────────────────────────────────────────────── describe('isAuthError', () => { diff --git a/src/main/ssh/ssh-connection-utils.ts b/src/main/ssh/ssh-connection-utils.ts index aa0a7cc6d..503fa08b6 100644 --- a/src/main/ssh/ssh-connection-utils.ts +++ b/src/main/ssh/ssh-connection-utils.ts @@ -74,6 +74,16 @@ export function isTransientError(err: Error): boolean { return false } +const SYSTEM_SSH_FALLBACK_ERROR_CODES = new Set(['EHOSTUNREACH', 'ENETUNREACH']) + +export function isSystemSshFallbackError(err: Error): boolean { + const code = (err as NodeJS.ErrnoException).code + if (code && SYSTEM_SSH_FALLBACK_ERROR_CODES.has(code)) { + return true + } + return err.message.includes('EHOSTUNREACH') || err.message.includes('ENETUNREACH') +} + export function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)) } diff --git a/src/main/ssh/ssh-connection.test.ts b/src/main/ssh/ssh-connection.test.ts index 1191e0a53..1db9dc852 100644 --- a/src/main/ssh/ssh-connection.test.ts +++ b/src/main/ssh/ssh-connection.test.ts @@ -9,6 +9,7 @@ import { join } from 'path' let eventHandlers: Map void>> let connectBehavior: 'ready' | 'error' = 'ready' let connectErrorMessage = '' +let connectErrorCode = '' let destroyErrorMessage = '' let connectSequence: ('ready' | Error)[] = [] let execBehavior: 'callback' | 'pending' = 'callback' @@ -68,7 +69,11 @@ vi.mock('ssh2', () => { return } if (connectBehavior === 'error') { - emitSshEvent('error', new Error(connectErrorMessage)) + const err = new Error(connectErrorMessage) as NodeJS.ErrnoException + if (connectErrorCode) { + err.code = connectErrorCode + } + emitSshEvent('error', err) } else { emitSshEvent('ready') } @@ -187,6 +192,7 @@ describe('SshConnection', () => { eventHandlers = new Map() connectBehavior = 'ready' connectErrorMessage = '' + connectErrorCode = '' destroyErrorMessage = '' connectSequence = [] execBehavior = 'callback' @@ -708,6 +714,48 @@ describe('SshConnection', () => { ) }) + it('falls back to system SSH when ssh2 hits a local network policy reachability error', async () => { + connectBehavior = 'error' + connectErrorMessage = 'connect EHOSTUNREACH 192.168.0.210:22 - Local (192.168.0.2:52112)' + connectErrorCode = 'EHOSTUNREACH' + const conn = new SshConnection( + createTarget({ host: '192.168.0.210', label: 'LAN Linux', username: 'hydra' }), + createCallbacks() + ) + + await conn.connect() + + expect(conn.getState().status).toBe('connected') + expect(conn.usesSystemSshTransport()).toBe(true) + expect(clientInstances).toHaveLength(1) + expect(spawnSystemSshCommandMock).toHaveBeenCalledWith( + expect.objectContaining({ host: '192.168.0.210' }), + 'echo ORCA-SYSTEM-SSH-OK', + { wrapCommand: false } + ) + }) + + it('keeps the original ssh2 reachability error when the system SSH probe fails', async () => { + connectBehavior = 'error' + connectErrorMessage = 'connect EHOSTUNREACH 192.168.0.210:22 - Local (192.168.0.2:52112)' + connectErrorCode = 'EHOSTUNREACH' + spawnSystemSshCommandMock.mockImplementation(() => { + throw new Error('No system ssh binary found. Install OpenSSH to use system SSH transport.') + }) + const conn = new SshConnection( + createTarget({ host: '192.168.0.210', label: 'LAN Linux', username: 'hydra' }), + createCallbacks() + ) + const privateConn = conn as unknown as { + attemptConnect: () => Promise + } + + await expect(privateConn.attemptConnect()).rejects.toThrow( + 'connect EHOSTUNREACH 192.168.0.210:22' + ) + expect(conn.usesSystemSshTransport()).toBe(false) + }) + it('passes the detected host platform to system SSH file operations', async () => { vi.mocked(resolveWithSshG).mockResolvedValueOnce({ hostname: 'example.com', diff --git a/src/main/ssh/ssh-connection.ts b/src/main/ssh/ssh-connection.ts index d14db4d26..249893a49 100644 --- a/src/main/ssh/ssh-connection.ts +++ b/src/main/ssh/ssh-connection.ts @@ -20,6 +20,7 @@ import { isTransientError, isAuthError, isAgentFallbackError, + isSystemSshFallbackError, isPassphraseError, sleep, buildConnectConfig, @@ -318,6 +319,20 @@ export class SshConnection { throw err } + if (isSystemSshFallbackError(err)) { + this.proxyProcess?.kill() + this.proxyProcess = null + try { + // Why: on macOS, per-app network policy can block Orca's direct + // TCP socket while the system OpenSSH binary is still allowed. + await this.doSystemSshProbe(connectGeneration) + return + } catch { + this.useSystemSshTransport = false + throw err + } + } + let authError = err let passphrasePromptHandled = false let credentialRetryConfig = config