feat(cli): rotate hub entries after repeated connect failures (#2383)

Self-hosted hubs are commonly reachable through several origins (an
edge-proxied domain and a direct one); cross-border interference can
randomly break TLS on either for hours. A runner pinned to a single
HAPI_API_URL sat dead through those windows.

When HAPI_API_FALLBACK_URLS is set, the machine client keeps a rotation
ring (primary + fallbacks) and, after HAPI_API_FAILOVER_AFTER (default 5)
consecutive connect failures, tears the socket down and reconnects
through the next entry. A successful connection on a fallback schedules
a return-to-primary attempt after 30 minutes. Without the env, behavior
is byte-for-byte unchanged.
This commit is contained in:
admin
2026-11-11 20:30:23 +08:00
committed by GitHub
parent 9fe29431a0
commit eff515e2e0
2 changed files with 556 additions and 1 deletions
+421
View File
@@ -0,0 +1,421 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const ioMock = vi.hoisted(() => vi.fn())
const listOpencodeModelsForCwdMock = vi.hoisted(() => vi.fn())
const listOpencodeModelVariantsMock = vi.hoisted(() => vi.fn())
const listGrokModelsForCwdMock = vi.hoisted(() => vi.fn())
const listCopilotModelsForCwdMock = vi.hoisted(() => vi.fn())
const listKimiModelsForCwdMock = vi.hoisted(() => vi.fn())
const inspectCursorChatStoreMock = vi.hoisted(() => vi.fn())
vi.mock('socket.io-client', () => ({
io: ioMock
}))
vi.mock('../modules/common/opencodeModels', () => ({
listOpencodeModelsForCwd: listOpencodeModelsForCwdMock
}))
vi.mock('../modules/common/opencodeModelVariants', () => ({
listOpencodeModelVariants: listOpencodeModelVariantsMock
}))
vi.mock('../modules/common/grokModels', () => ({
listGrokModelsForCwd: listGrokModelsForCwdMock
}))
vi.mock('../modules/common/copilotModels', () => ({
listCopilotModelsForCwd: listCopilotModelsForCwdMock
}))
vi.mock('../modules/common/kimiModels', () => ({
listKimiModelsForCwd: listKimiModelsForCwdMock
}))
vi.mock('@/cursor/cursorChatStoreStatus', () => ({
inspectCursorChatStore: inspectCursorChatStoreMock
}))
import {
ApiMachineClient,
DEFAULT_FAILOVER_AFTER,
PRIMARY_ENTRY_RETRY_DELAY_MS,
buildHubEntryRotation,
resolveFailoverThreshold
} from './apiMachine'
import { configuration } from '@/configuration'
import { logger } from '@/ui/logger'
import type { Machine } from './types'
function makeMachine(id: string): Machine {
return {
id,
namespace: 'default',
seq: 1,
createdAt: 0,
updatedAt: 0,
active: true,
activeAt: 0,
metadata: null,
metadataVersion: 0,
runnerState: null,
runnerStateVersion: 0
}
}
describe('buildHubEntryRotation', () => {
it('returns only the primary entry when the fallback env is unset', () => {
expect(buildHubEntryRotation('https://primary.example', undefined))
.toEqual(['https://primary.example'])
})
it('returns only the primary entry when every fallback entry is blank', () => {
expect(buildHubEntryRotation('https://primary.example', ' , ,, '))
.toEqual(['https://primary.example'])
})
it('appends trimmed fallback entries after the primary', () => {
expect(buildHubEntryRotation('https://primary.example', ' https://a.example ,https://b.example '))
.toEqual(['https://primary.example', 'https://a.example', 'https://b.example'])
})
it('strips trailing slashes from fallback entries but keeps the primary verbatim', () => {
expect(buildHubEntryRotation('https://primary.example/', 'https://a.example/,,https://b.example///'))
.toEqual(['https://primary.example/', 'https://a.example', 'https://b.example'])
})
it('drops duplicates against the primary and within the fallback list', () => {
expect(buildHubEntryRotation('https://primary.example', 'https://primary.example,https://a.example,https://a.example/'))
.toEqual(['https://primary.example', 'https://a.example'])
})
})
describe('resolveFailoverThreshold', () => {
it(`defaults to ${DEFAULT_FAILOVER_AFTER} for unset, blank, non-numeric or non-positive values`, () => {
expect(resolveFailoverThreshold(undefined)).toBe(DEFAULT_FAILOVER_AFTER)
expect(resolveFailoverThreshold('')).toBe(DEFAULT_FAILOVER_AFTER)
expect(resolveFailoverThreshold(' ')).toBe(DEFAULT_FAILOVER_AFTER)
expect(resolveFailoverThreshold('not-a-number')).toBe(DEFAULT_FAILOVER_AFTER)
expect(resolveFailoverThreshold('0')).toBe(DEFAULT_FAILOVER_AFTER)
expect(resolveFailoverThreshold('-3')).toBe(DEFAULT_FAILOVER_AFTER)
})
it('accepts positive integers and trims whitespace', () => {
expect(resolveFailoverThreshold('3')).toBe(3)
expect(resolveFailoverThreshold(' 12 ')).toBe(12)
})
})
type FakeSocket = {
on: ReturnType<typeof vi.fn>
emit: ReturnType<typeof vi.fn>
emitWithAck: ReturnType<typeof vi.fn>
removeAllListeners: ReturnType<typeof vi.fn>
close: ReturnType<typeof vi.fn>
}
type CreatedSocket = {
url: string
options: Record<string, unknown>
socket: FakeSocket
fire: (event: string, ...args: unknown[]) => void
}
/**
* Creates a minimal socket.io double that records `on` handlers so tests can
* fire connect/connect_error like the real client would, and tracks every
* socket the client creates (URL + options) via the io mock.
*/
function trackSockets(): CreatedSocket[] {
const created: CreatedSocket[] = []
ioMock.mockImplementation((url: string, options: Record<string, unknown>) => {
const handlers = new Map<string, (...args: unknown[]) => void>()
const socket = {
on: vi.fn((event: string, handler: (...args: unknown[]) => void) => {
handlers.set(event, handler)
}),
emit: vi.fn(),
// Valid success ack for machine-update-state so the connect
// handler's backoff-wrapped RPC settles immediately.
emitWithAck: vi.fn().mockResolvedValue({
result: 'success',
version: 1,
runnerState: { status: 'running', capabilities: {} }
}),
removeAllListeners: vi.fn(),
close: vi.fn()
}
const fire = (event: string, ...args: unknown[]) => {
const handler = handlers.get(event)
if (!handler) throw new Error(`No fake-socket handler registered for "${event}"`)
handler(...args)
}
created.push({ url, options, socket, fire })
return socket
})
return created
}
function fireConnectError(entry: CreatedSocket, message = 'websocket error'): void {
entry.fire('connect_error', new Error(message))
}
function primaryRetryTimerOf(client: ApiMachineClient): unknown {
return (client as unknown as { primaryEntryRetryTimer: unknown }).primaryEntryRetryTimer
}
describe('ApiMachineClient hub entry failover', () => {
const originalFallbackEnv = process.env.HAPI_API_FALLBACK_URLS
const originalFailoverAfterEnv = process.env.HAPI_API_FAILOVER_AFTER
let debugSpy: ReturnType<typeof vi.spyOn>
beforeEach(() => {
vi.useFakeTimers()
ioMock.mockReset()
// Silence debug logging while keeping calls observable for assertions.
debugSpy = vi.spyOn(logger, 'debug').mockImplementation(() => {})
delete process.env.HAPI_API_FALLBACK_URLS
delete process.env.HAPI_API_FAILOVER_AFTER
})
afterEach(() => {
vi.clearAllTimers()
vi.useRealTimers()
vi.restoreAllMocks()
if (originalFallbackEnv === undefined) delete process.env.HAPI_API_FALLBACK_URLS
else process.env.HAPI_API_FALLBACK_URLS = originalFallbackEnv
if (originalFailoverAfterEnv === undefined) delete process.env.HAPI_API_FAILOVER_AFTER
else process.env.HAPI_API_FAILOVER_AFTER = originalFailoverAfterEnv
})
it('never rotates when no fallback entries are configured', () => {
const created = trackSockets()
const machine = makeMachine('failover-none')
const client = new ApiMachineClient('cli-token', machine)
try {
client.connect()
for (let i = 0; i < 12; i++) {
fireConnectError(created[0])
}
expect(ioMock).toHaveBeenCalledTimes(1)
expect(created[0].url).toBe(`${configuration.apiUrl}/cli`)
expect(created[0].socket.close).not.toHaveBeenCalled()
expect(created[0].socket.removeAllListeners).not.toHaveBeenCalled()
} finally {
client.shutdown()
}
})
it('treats an all-blank fallback env exactly like unset (zero rotation)', () => {
process.env.HAPI_API_FALLBACK_URLS = ' , , '
const created = trackSockets()
const client = new ApiMachineClient('cli-token', makeMachine('failover-blank'))
try {
client.connect()
for (let i = 0; i < 8; i++) {
fireConnectError(created[0])
}
expect(ioMock).toHaveBeenCalledTimes(1)
} finally {
client.shutdown()
}
})
it('rebuilds the socket on the fallback origin after the 5th consecutive connect failure', () => {
process.env.HAPI_API_FALLBACK_URLS = 'https://fallback-a.example,https://fallback-b.example'
const created = trackSockets()
const machine = makeMachine('failover-rotate')
const client = new ApiMachineClient('cli-token', machine)
try {
client.connect()
for (let i = 0; i < 4; i++) {
fireConnectError(created[0])
}
expect(ioMock).toHaveBeenCalledTimes(1)
fireConnectError(created[0]) // 5th consecutive failure
expect(ioMock).toHaveBeenCalledTimes(2)
expect(created[0].url).toBe(`${configuration.apiUrl}/cli`)
expect(created[1].url).toBe('https://fallback-a.example/cli')
// Old socket is torn down so its own reconnection loop cannot interfere.
expect(created[0].socket.removeAllListeners).toHaveBeenCalled()
expect(created[0].socket.close).toHaveBeenCalled()
// Rotation log line in the file's established style.
expect(debugSpy).toHaveBeenCalledWith(
`[API MACHINE] rotating hub entry ${configuration.apiUrl} -> https://fallback-a.example after 5 connect failures`
)
// The rebuilt socket carries identical origin-independent options.
expect(created[1].options).toMatchObject({
path: '/socket.io/',
auth: {
token: 'cli-token',
clientType: 'machine-scoped',
machineId: machine.id
}
})
} finally {
client.shutdown()
}
})
it('rotates around the ring and eventually wraps back to the primary entry', () => {
process.env.HAPI_API_FALLBACK_URLS = 'https://fallback-a.example,https://fallback-b.example'
const created = trackSockets()
const client = new ApiMachineClient('cli-token', makeMachine('failover-ring'))
try {
client.connect()
for (let i = 0; i < 5; i++) fireConnectError(created[0])
expect(created[1].url).toBe('https://fallback-a.example/cli')
for (let i = 0; i < 5; i++) fireConnectError(created[1])
expect(created[2].url).toBe('https://fallback-b.example/cli')
for (let i = 0; i < 5; i++) fireConnectError(created[2])
expect(created[3].url).toBe(`${configuration.apiUrl}/cli`)
expect(ioMock).toHaveBeenCalledTimes(4)
} finally {
client.shutdown()
}
})
it('honors HAPI_API_FAILOVER_AFTER when set', () => {
process.env.HAPI_API_FALLBACK_URLS = 'https://fallback-a.example'
process.env.HAPI_API_FAILOVER_AFTER = '2'
const created = trackSockets()
const client = new ApiMachineClient('cli-token', makeMachine('failover-threshold'))
try {
client.connect()
fireConnectError(created[0])
expect(ioMock).toHaveBeenCalledTimes(1)
fireConnectError(created[0])
expect(ioMock).toHaveBeenCalledTimes(2)
expect(created[1].url).toBe('https://fallback-a.example/cli')
} finally {
client.shutdown()
}
})
it('resets the failure streak after a successful connect', () => {
process.env.HAPI_API_FALLBACK_URLS = 'https://fallback-a.example'
const created = trackSockets()
const client = new ApiMachineClient('cli-token', makeMachine('failover-reset'))
try {
client.connect()
for (let i = 0; i < 3; i++) {
fireConnectError(created[0])
}
// A completed handshake on the current entry clears the streak.
created[0].fire('connect')
for (let i = 0; i < 4; i++) {
fireConnectError(created[0])
}
expect(ioMock).toHaveBeenCalledTimes(1)
// The 5th failure after the reset triggers the rotation.
fireConnectError(created[0])
expect(ioMock).toHaveBeenCalledTimes(2)
expect(created[1].url).toBe('https://fallback-a.example/cli')
} finally {
client.shutdown()
}
})
it('arms the primary retry timer after connecting on a fallback and returns to the primary when it fires', () => {
process.env.HAPI_API_FALLBACK_URLS = 'https://fallback-a.example'
const created = trackSockets()
const client = new ApiMachineClient('cli-token', makeMachine('failover-primary-retry'))
try {
client.connect()
for (let i = 0; i < 5; i++) fireConnectError(created[0])
expect(created[1].url).toBe('https://fallback-a.example/cli')
// No timer while never connected on the fallback.
expect(primaryRetryTimerOf(client)).toBeNull()
created[1].fire('connect')
expect(primaryRetryTimerOf(client)).not.toBeNull()
// One ms before the dwell elapses: still on the fallback.
vi.advanceTimersByTime(PRIMARY_ENTRY_RETRY_DELAY_MS - 1)
expect(ioMock).toHaveBeenCalledTimes(2)
vi.advanceTimersByTime(1)
expect(ioMock).toHaveBeenCalledTimes(3)
expect(created[2].url).toBe(`${configuration.apiUrl}/cli`)
expect(created[1].socket.close).toHaveBeenCalled()
expect(primaryRetryTimerOf(client)).toBeNull()
// Dwelling on the primary never re-arms a retry timer.
created[2].fire('connect')
expect(primaryRetryTimerOf(client)).toBeNull()
vi.advanceTimersByTime(PRIMARY_ENTRY_RETRY_DELAY_MS * 2)
expect(ioMock).toHaveBeenCalledTimes(3)
} finally {
client.shutdown()
}
})
it('clears the pending primary retry timer when the connection rotates again, then re-arms on the new fallback', () => {
process.env.HAPI_API_FALLBACK_URLS = 'https://fallback-a.example,https://fallback-b.example'
const created = trackSockets()
const client = new ApiMachineClient('cli-token', makeMachine('failover-timer-cleanup'))
try {
client.connect()
for (let i = 0; i < 5; i++) fireConnectError(created[0])
created[1].fire('connect') // connected on fallback-a, timer armed
expect(primaryRetryTimerOf(client)).not.toBeNull()
vi.advanceTimersByTime(10 * 60 * 1000)
// Fallback-a dies too: rotation must clear its pending timer first.
for (let i = 0; i < 5; i++) fireConnectError(created[1])
expect(created[2].url).toBe('https://fallback-b.example/cli')
expect(primaryRetryTimerOf(client)).toBeNull()
// Nothing fires from the stale timer.
vi.advanceTimersByTime(PRIMARY_ENTRY_RETRY_DELAY_MS)
expect(ioMock).toHaveBeenCalledTimes(3)
// Connecting on fallback-b arms a fresh 30min timer.
created[2].fire('connect')
expect(primaryRetryTimerOf(client)).not.toBeNull()
vi.advanceTimersByTime(PRIMARY_ENTRY_RETRY_DELAY_MS)
expect(ioMock).toHaveBeenCalledTimes(4)
expect(created[3].url).toBe(`${configuration.apiUrl}/cli`)
} finally {
client.shutdown()
}
})
it('clears the pending primary retry timer on shutdown', () => {
process.env.HAPI_API_FALLBACK_URLS = 'https://fallback-a.example'
const created = trackSockets()
const client = new ApiMachineClient('cli-token', makeMachine('failover-shutdown'))
try {
client.connect()
for (let i = 0; i < 5; i++) fireConnectError(created[0])
created[1].fire('connect')
expect(primaryRetryTimerOf(client)).not.toBeNull()
client.shutdown()
expect(primaryRetryTimerOf(client)).toBeNull()
vi.advanceTimersByTime(PRIMARY_ENTRY_RETRY_DELAY_MS * 2)
expect(ioMock).toHaveBeenCalledTimes(2)
} finally {
client.shutdown()
}
})
})
+135 -1
View File
@@ -118,10 +118,56 @@ function formatWorkspaceRoots(paths?: string[]): string {
return paths?.length ? paths.join(', ') : '(none)'
}
/** Default number of consecutive connect failures before rotating to the next hub entry. */
export const DEFAULT_FAILOVER_AFTER = 5
/** How long to dwell on a fallback hub entry before retrying the primary entry. */
export const PRIMARY_ENTRY_RETRY_DELAY_MS = 30 * 60 * 1000
/**
* Builds the hub entry rotation ring: the primary entry (HAPI_API_URL) first,
* then the comma-separated HAPI_API_FALLBACK_URLS entries. Trailing slashes are
* stripped from fallback entries so they compose cleanly into the `<origin>/cli`
* socket URL; the primary entry is kept verbatim. Duplicates (against the
* primary or within the list) are dropped. When the env is unset or every entry
* is blank the ring holds only the primary, which disables rotation entirely
* (single-URL behavior, zero regression).
*/
export function buildHubEntryRotation(primaryUrl: string, fallbackUrlsEnv: string | undefined): string[] {
const primary = primaryUrl.trim()
const fallbacks: string[] = []
for (const rawEntry of (fallbackUrlsEnv ?? '').split(',')) {
const entry = rawEntry.trim().replace(/\/+$/, '')
if (!entry || entry === primary || fallbacks.includes(entry)) continue
fallbacks.push(entry)
}
return [primary, ...fallbacks]
}
/**
* Parses HAPI_API_FAILOVER_AFTER (minimum 1). Unset, non-numeric or
* non-positive values fall back to the default.
*/
export function resolveFailoverThreshold(envValue: string | undefined): number {
const parsed = Number.parseInt((envValue ?? '').trim(), 10)
if (!Number.isFinite(parsed) || parsed < 1) {
return DEFAULT_FAILOVER_AFTER
}
return parsed
}
export class ApiMachineClient {
private socket!: Socket<ServerToClientEvents, ClientToServerEvents>
private keepAliveInterval: NodeJS.Timeout | null = null
private keepAliveStartTimeout: ReturnType<typeof setTimeout> | null = null
// Hub entry rotation ring: primary (HAPI_API_URL) first, fallbacks from
// HAPI_API_FALLBACK_URLS after. A single-entry ring disables rotation and
// keeps the client identical to the old single-URL implementation.
private hubEntries: string[] = [configuration.apiUrl]
private activeEntryIndex = 0
private connectFailureStreak = 0
private failoverAfter = DEFAULT_FAILOVER_AFTER
private primaryEntryRetryTimer: ReturnType<typeof setTimeout> | null = null
private rpcHandlerManager: RpcHandlerManager
private readonly pathPolicy: MachinePathPolicy
@@ -627,7 +673,24 @@ export class ApiMachineClient {
}
connect(): void {
this.socket = io(`${configuration.apiUrl}/cli`, {
// Snapshot the rotation ring on connect: primary entry first, fallback
// entries from env after. Unset/blank env yields a single-entry ring,
// which keeps the client exactly on the primary URL (zero regression).
this.hubEntries = buildHubEntryRotation(configuration.apiUrl, process.env.HAPI_API_FALLBACK_URLS)
this.failoverAfter = resolveFailoverThreshold(process.env.HAPI_API_FAILOVER_AFTER)
this.activeEntryIndex = 0
this.connectFailureStreak = 0
this.createSocket()
}
/**
* Creates — and, after hub entry rotation, re-creates — the machine socket
* against the currently active entry. Everything origin-independent
* (auth token, transport options, extra headers) is rebuilt identically
* for every entry because they all flow through this single path.
*/
private createSocket(): void {
this.socket = io(`${this.hubEntries[this.activeEntryIndex]}/cli`, {
transports: ['websocket'],
auth: {
token: this.token,
@@ -643,6 +706,11 @@ export class ApiMachineClient {
this.socket.on('connect', () => {
logger.debug('[API MACHINE] Connected to bot')
// A completed handshake proves the active entry works: reset the
// failure streak and, while dwelling on a fallback entry, arm the
// periodic retry of the primary entry.
this.connectFailureStreak = 0
this.schedulePrimaryEntryRetry()
this.rpcHandlerManager.onSocketConnect(this.socket)
this.updateRunnerState((state) => ({
...(state ?? {}),
@@ -733,6 +801,13 @@ export class ApiMachineClient {
this.socket.on('connect_error', (error) => {
logger.debug(`[API MACHINE] Connection error: ${error.message}`)
// socket.io retries the same origin forever, so cross-border
// interference on the active entry needs our own rotation: count
// consecutive failures and switch origins once enough pile up.
this.connectFailureStreak += 1
if (this.hubEntries.length > 1 && this.connectFailureStreak >= this.failoverAfter) {
this.rotateToNextHubEntry()
}
})
this.socket.on('error', (payload) => {
@@ -782,8 +857,67 @@ export class ApiMachineClient {
}
}
/**
* Rotates to the next hub entry after `failoverAfter` consecutive connect
* failures and rebuilds the socket against the new origin. No-op for a
* single-entry ring (rotation disabled).
*/
private rotateToNextHubEntry(): void {
if (this.hubEntries.length <= 1) return
const previousEntry = this.hubEntries[this.activeEntryIndex]
const failures = this.connectFailureStreak
this.activeEntryIndex = (this.activeEntryIndex + 1) % this.hubEntries.length
logger.debug(`[API MACHINE] rotating hub entry ${previousEntry} -> ${this.hubEntries[this.activeEntryIndex]} after ${failures} connect failures`)
this.rebuildSocket()
}
/**
* While connected through a fallback entry, arms a timer that periodically
* tears the connection down and retries the primary entry. If the primary
* is still unreachable the regular failure rotation carries the client
* back around the ring to a working entry, so no extra fallback
* bookkeeping is needed here.
*/
private schedulePrimaryEntryRetry(): void {
if (this.activeEntryIndex === 0) return
if (this.primaryEntryRetryTimer) return
this.primaryEntryRetryTimer = setTimeout(() => {
this.primaryEntryRetryTimer = null
const fallbackEntry = this.hubEntries[this.activeEntryIndex]
this.activeEntryIndex = 0
logger.debug(`[API MACHINE] retrying primary hub entry ${this.hubEntries[0]} after dwelling on fallback ${fallbackEntry}`)
this.rebuildSocket()
}, PRIMARY_ENTRY_RETRY_DELAY_MS)
}
private clearPrimaryEntryRetryTimer(): void {
if (this.primaryEntryRetryTimer) {
clearTimeout(this.primaryEntryRetryTimer)
this.primaryEntryRetryTimer = null
}
}
/**
* Tears the current socket down (stopping its own reconnection loop so it
* cannot fight the rotation) and reconnects through the active entry via
* the shared construction path in createSocket(). Also clears every timer
* tied to the abandoned socket so rotations can never leak them.
*/
private rebuildSocket(): void {
this.clearPrimaryEntryRetryTimer()
this.stopKeepAlive()
// The old socket's listeners are dropped on purpose: after rotation
// they would only observe the abandoned entry's lifecycle.
this.socket.removeAllListeners()
this.socket.close()
this.rpcHandlerManager.onSocketDisconnect()
this.connectFailureStreak = 0
this.createSocket()
}
shutdown(): void {
this.stopKeepAlive()
this.clearPrimaryEntryRetryTimer()
// The listener holds this client, and the socket is about to close.
setAgyCatalogChangeListener(null)
if (this.socket) {