Skip to content

The agent that grows with you

fix(desktop): let the Reconnect action redial a rejected active gateway

kshitijk4poore0eb2ff0a88a
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 c84ee518fd2..128a78fa5ef 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -1014,7 +1014,7 @@ export function useGatewayBoot({
if (!isActivePrimary()) {
activeGateway()?.close()
- if (!(await ensureActiveGatewayOpen())) {
+ if (!(await ensureActiveGatewayOpen({ explicit: true }))) {
throw new Error('Hermes gateway is not connected')
}
diff --git a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
index 21e89ffab43..665fe86f074 100644
--- a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
+++ b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
@@ -832,6 +832,31 @@ describe('rejected secondary authentication', () => {
expect(activeGateway()?.connectionState).toBe('open')
expect(getGatewayWsUrlFor).toHaveBeenCalledTimes(calls + 2)
})
+
+ it('the explicit Reconnect action redials a parked active source', async () => {
+ vi.useFakeTimers()
+
+ const getConnectionFor = vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) => ({
+ ...descriptorFor(connectionId, profile), authMode: 'oauth'
+ }))
+
+ const getGatewayWsUrlFor = vi.fn(async () => ({ ok: true, wsUrl: 'wss://cloud.invalid/api/ws?ticket=fresh' }))
+ installDesktop({ getConnectionFor, getGatewayWsUrlFor })
+ await ensureGatewayForAgent('cloud', 'default')
+ gatewayMocks.instances[0].connectionState = 'closed'
+ getGatewayWsUrlFor.mockResolvedValue({ ok: false, needsOauthLogin: true, error: 'Sign in again' } as never)
+ const rejected = ensureActiveGatewayOpen()
+ await vi.advanceTimersByTimeAsync(8_000)
+ expect(await rejected).toBeNull()
+
+ // The user re-authenticated in Settings and pressed Reconnect on the same route.
+ getGatewayWsUrlFor.mockResolvedValue({ ok: true, wsUrl: 'wss://cloud.invalid/api/ws?ticket=new' })
+ const calls = getGatewayWsUrlFor.mock.calls.length
+ const recovered = ensureActiveGatewayOpen({ explicit: true })
+ await vi.advanceTimersByTimeAsync(8_000)
+ expect((await recovered)?.connectionState).toBe('open')
+ expect(getGatewayWsUrlFor).toHaveBeenCalledTimes(calls + 1)
+ })
})
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index cb1bcffd68a..4a98fc19bdf 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -1797,17 +1797,27 @@ export async function ensureGatewayForProfile(profile: string): Promise<void> {
// Reconnect the active gateway after a transient request failure. Primary
// reconnects are owned by use-gateway-boot, so we only drive secondaries here.
-export async function ensureActiveGatewayOpen(): Promise<HermesGateway | null> {
+// A scope parked on a rejected session stays parked for automatic request
+// retries; only a user gesture (`explicit`: the Reconnect action) may redial it.
+export async function ensureActiveGatewayOpen({ explicit = false } = {}): Promise<HermesGateway | null> {
if (g.activeKey === g.primaryProfile) {
return g.primaryGateway
}
const entry = g.secondaries.get(g.activeKey)
- if (!entry || g.reauthFailures.has(entry.scope)) {
+ if (!entry) {
return null
}
+ if (g.reauthFailures.has(entry.scope)) {
+ if (!explicit) {
+ return null
+ }
+
+ g.reauthFailures.delete(entry.scope)
+ }
+
if (!isOpen(entry.gateway)) {
// The viewed scope is an explicit recovery target (Reconnect action,
// request retry): a parked entry must dial again here, not stay parked.