Skip to content

The agent that grows with you

fix(desktop): stable-open rule owns all three reconnect counters and covers secondary sockets

kshitijk4poor018531a87122
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
index 479bdcc5867..7fadcce1d74 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -2,6 +2,7 @@ import {
type GatewayEvent,
isGatewayReauthRequired,
isGatewayWebSocketUrl,
+ isStableOpen,
JSON_RPC_METHOD_NOT_FOUND,
JsonRpcGatewayError,
reconnectBackoffDelayMs,
@@ -129,11 +130,6 @@ const RECONNECT_ESCALATE_AFTER_MS = 300_000
// window instead of tearing down every secondary socket on each signal (#94769).
const WAKE_RECONNECT_HOLDOFF_MS = 15_000
-// A socket only counts as recovered once it has stayed open this long. A proxy
-// that ACCEPTS the upgrade and then closes on the first frame (#83134) would
-// otherwise reset the backoff on every 'open' and get redialed at attempt 0
-// forever (~150ms mean) — the reconnect storm the backoff exists to prevent.
-const RECONNECT_STABLE_OPEN_MS = 5_000
// Bounded self-heal for a failed REMOTE boot (#82679): main classifies every
// fault it can see (via getBootProgress().retryable); the renderer adds the one
@@ -277,9 +273,9 @@ export function useGatewayBoot({
let reconnecting = false
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
let reconnectAttempt = 0
- // Wall-clock of the current socket's 'open'; null while not open. The
- // backoff counters above/below reset only once an open proves stable
- // (RECONNECT_STABLE_OPEN_MS), judged when the socket closes.
+ // Wall-clock of the current socket's 'open'; null while not open.
+ // reconnectAttempt, reconnectFailingSince and escalated reset only once an
+ // open proves stable (isStableOpen), judged when the socket closes.
let openedAt: number | null = null
// Consecutive unanswered liveness probes (#95327): a busy-but-healthy
// backend can fail one probe; only a STREAK proves a genuinely dead
@@ -314,7 +310,8 @@ export function useGatewayBoot({
// Raised once the reconnect loop has been failing for
// RECONNECT_ESCALATE_AFTER_MS so we fire a single non-blocking toast.
- // Reset on a clean open or a manual/wake-driven reconnect.
+ // Reset together with the backoff counters: on a STABLE open (isStableOpen)
+ // or a manual/wake-driven reconnect — never on a bare 'open' that dies.
let escalated = false
// Bounded automatic boot retry for transient REMOTE failures (#82679).
let bootRetryAttempt = 0
@@ -628,6 +625,7 @@ export function useGatewayBoot({
async function getWindowBackend(startup = false): Promise<HermesConnection> {
const profile = windowProfileOverride()
const peer = isPeerInstanceWindow()
+
const route = profile
? { profile, connectionId: peer ? new URLSearchParams(window.location.search).get('connectionId') : null }
: startup && !peer
@@ -978,7 +976,6 @@ export function useGatewayBoot({
openedAt = Date.now()
reauthNotified = false
primaryReauthError = null
- escalated = false
livenessProbeFailures = 0
clearReconnectTimer()
clearLivenessReprobeTimer()
@@ -992,9 +989,10 @@ export function useGatewayBoot({
completeDesktopBoot()
}
} else if (st === 'closed' || st === 'error') {
- if (openedAt !== null && Date.now() - openedAt >= RECONNECT_STABLE_OPEN_MS) {
+ if (isStableOpen(openedAt)) {
reconnectAttempt = 0
reconnectFailingSince = null
+ escalated = false
}
openedAt = null
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index d318c3df3a7..922f1a313a9 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -2,6 +2,7 @@ import {
type ConnectionState,
type GatewayEvent,
isGatewayReauthRequired,
+ isStableOpen,
reconnectBackoffDelayMs,
registryBackendScopeKey,
resolveGatewayWsUrl,
@@ -642,6 +643,7 @@ async function openSecondary(entry: Secondary, spawnPriority: SpawnPriority = 'b
const desktop = window.hermesDesktop
const reauthError = g.reauthFailures.get(entry.scope)?.error
+
if (reauthError) {
throw reauthError
}
@@ -790,6 +792,7 @@ async function openSecondary(entry: Secondary, spawnPriority: SpawnPriority = 'b
entry.wantOpen = false
clearTimer(entry)
}
+
throw error
} finally {
if (entry.connectPromise === pending) {
@@ -822,9 +825,11 @@ function isStalledDialError(error: unknown): boolean {
function rearmSecondary(entry: Secondary, priority: SpawnPriority = 'foreground'): void {
const reauthError = g.reauthFailures.get(entry.scope)?.error
+
if (reauthError && priority !== 'foreground') {
throw reauthError
}
+
g.reauthFailures.delete(entry.scope)
if (entry.retiredByPool && priority !== 'foreground') {
@@ -860,10 +865,10 @@ async function reconnectSecondary(entry: Secondary): Promise<void> {
try {
await openSecondary(entry)
- entry.reconnectAttempt = 0
} catch (error) {
if (isGatewayReauthRequired(error)) {
notifyError(error, translateNow('boot.errors.gatewaySignInRequired'), { action: RECOVERY_ACTIONS.openGateways() })
+
return
}
@@ -975,10 +980,15 @@ function createSecondary(profile: string, connectionId: null | string = null): S
reportGatewayState(scope, state)
if (state === 'open') {
- entry.reconnectAttempt = 0
entry.stalledDials = 0
clearTimer(entry)
} else if (state === 'closed' || state === 'error') {
+ // Same stable-open rule as the primary (#83134): an accept-then-close socket
+ // is a failed attempt, so the ladder resets only after a socket that lived.
+ if (entry.lastOpenedAt > 0 && isStableOpen(entry.lastOpenedAt)) {
+ entry.reconnectAttempt = 0
+ }
+
// A dead socket cannot emit the terminal event that normally releases
// its turn lease. Drop the orphaned lease before deciding whether this
// route is still retained/active enough to reconnect.
@@ -1344,6 +1354,7 @@ export function retainGatewayForRelay(connectionId: null | string, profile: stri
}
entry.relayRetainCount += 1
+
if (!g.reauthFailures.has(entry.scope)) {
rearmSecondary(entry)
}
@@ -1983,6 +1994,7 @@ function probeSecondaryLiveness(entry: Secondary): void {
// before the turn ends, so a foreground turn mid tool call shows
// activeRequests 0. The registry's live-scope hook supplies the turn.
const liveScopes = g.config?.liveScopes?.()
+
const turnInFlight =
liveScopes && (liveScopes.has(entry.scope) || (!entry.connectionId && liveScopes.has(entry.profile))) ? 1 : 0
@@ -2286,6 +2298,7 @@ export function closeLegacySecondaryGateways(): void {
g.reauthFailures.delete(scope)
}
}
+
closeSecondariesWhere(isLegacySecondary)
}
diff --git a/apps/shared/src/index.ts b/apps/shared/src/index.ts
index c9c018149f6..aada831da1a 100644
--- a/apps/shared/src/index.ts
+++ b/apps/shared/src/index.ts
@@ -105,7 +105,12 @@ export {
type ReasoningEffort,
type ReasoningEffortValue
} from './reasoning-effort'
-export { reconnectBackoffDelayMs, type ReconnectBackoffOptions } from './reconnect-backoff'
+export {
+ isStableOpen,
+ RECONNECT_STABLE_OPEN_MS,
+ reconnectBackoffDelayMs,
+ type ReconnectBackoffOptions
+} from './reconnect-backoff'
export { skillInvocationText } from './skill-scaffold'
export {
type HermesSkin,
diff --git a/apps/shared/src/reconnect-backoff.ts b/apps/shared/src/reconnect-backoff.ts
index 8a1dd756b5e..86b83a91abd 100644
--- a/apps/shared/src/reconnect-backoff.ts
+++ b/apps/shared/src/reconnect-backoff.ts
@@ -25,6 +25,18 @@ export interface ReconnectBackoffOptions {
}
const DEFAULT_BASE_DELAY_MS = 300
+/**
+ * A socket that opens and dies inside this window (an accept-then-close proxy, a gateway that
+ * refuses the first frame) counts as a FAILED attempt: resetting the ladder on every such
+ * "open" redials at attempt 0 forever (#83134: 55 sockets in 12 s).
+ */
+export const RECONNECT_STABLE_OPEN_MS = 5_000
+
+/** True when a socket opened at `openedAt` stayed up long enough to reset the backoff ladder. */
+export function isStableOpen(openedAt: number | null, now = Date.now()): boolean {
+ return openedAt !== null && now - openedAt >= RECONNECT_STABLE_OPEN_MS
+}
+
const DEFAULT_CAP_MS = 15_000
// 2 ** attempt overflows to Infinity long before it matters (attempt would
// need to be ~1024) and Math.min against a finite cap keeps the ceiling sane