Fallback to system SSH on reachability errors (#5303)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -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', () => {
|
||||
|
||||
@@ -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<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import { join } from 'path'
|
||||
let eventHandlers: Map<string, Set<(...args: unknown[]) => 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<void>
|
||||
}
|
||||
|
||||
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',
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user