Skip to content

The agent that grows with you

fix(desktop): park a secondary whose local gateway ensure refuses permanently

Tekniumdcdfacc0e3ec
diff --git a/apps/desktop/src/lib/local-gateway-ensure-failure.ts b/apps/desktop/src/lib/local-gateway-ensure-failure.ts
new file mode 100644
index 00000000000..d127ceb3e78
--- /dev/null
+++ b/apps/desktop/src/lib/local-gateway-ensure-failure.ts
@@ -0,0 +1,21 @@
+// Main-process refusals from a local `hermes gateway ensure` (electron/local-gateway.ts) that no
+// automatic redial can fix: every redial of the scope re-spawns the same ensure client against the
+// same install and gets the same verdict. They need an update, a profile fix or a path repair.
+// The renderer sees them wrapped by ipcRenderer.invoke ("Error invoking remote method …: Error: …"),
+// so match the stable message heads, never the whole string.
+const PERMANENT_ENSURE_FAILURES = [
+ // ensureLocalGateway: protocol verdict `incompatible` (runtime_protocol, session authority missing).
+ 'Gateway incompatible (',
+ // parseGatewayEnsureOutput: the installed `hermes` has no ensure protocol, or the profile is missing.
+ 'hermes gateway ensure produced no result',
+ // assertLocalGatewayEndpoint: a ready verdict that is not a loopback session-authority endpoint.
+ 'Invalid local gateway endpoint',
+ // privateNode / the group-writable ticket refusal: needs a chmod/chown, never a re-ensure.
+ 'Unsafe gateway control path'
+]
+
+export function isPermanentLocalGatewayEnsureError(error: unknown): boolean {
+ const message = error instanceof Error ? error.message : String(error ?? '')
+
+ return PERMANENT_ENSURE_FAILURES.some(head => message.includes(head))
+}
diff --git a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
index 07d261c5abf..3083e7307ad 100644
--- a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
+++ b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
@@ -775,6 +775,38 @@ describe('secondary stalled-dial budget', () => {
expect(getConnectionFor.mock.calls.length).toBe(dialsAfterParking + 1)
})
+ it('parks a scope at once when its local gateway ensure fails permanently, and an explicit open re-arms it', async () => {
+ vi.useFakeTimers()
+
+ // What main's ensureLocalGateway / parseGatewayEnsureOutput / assertLocalGatewayEndpoint throw,
+ // as they cross the IPC boundary. Each re-dial spawns another `hermes gateway ensure`.
+ for (const permanent of [
+ "Error invoking remote method 'hermes:connection:get-for': Error: Gateway incompatible (runtime_protocol). Use hermes gateway status for recovery.",
+ 'hermes gateway ensure produced no result (exit 2): invalid choice. Update Hermes or check the profile, then retry.',
+ 'Invalid local gateway endpoint'
+ ]) {
+ closeSecondaryGateways()
+ gatewayMocks.instances.length = 0
+ const getConnectionFor = vi.fn().mockResolvedValueOnce(descriptorFor('homelab', 'bot-a')).mockRejectedValue(new Error(permanent))
+ installDesktop({ getConnectionFor })
+
+ await ensureGatewayForAgent('homelab', 'bot-a')
+ const socket = gatewayMocks.instances[0] as unknown as { connectionState: string }
+ socket.connectionState = 'closed'
+ reconnectSecondaryGateways()
+
+ for (let index = 0; index < 20; index += 1) {
+ await vi.advanceTimersByTimeAsync(20_000)
+ }
+
+ expect(getConnectionFor.mock.calls.length, permanent).toBe(2)
+
+ getConnectionFor.mockResolvedValue(descriptorFor('homelab', 'bot-a'))
+ await ensureGatewayForAgent('homelab', 'bot-a')
+ expect(getConnectionFor.mock.calls.length, permanent).toBe(3)
+ }
+ })
+
it('does not let an interleaved fast failure refill the stall budget', async () => {
vi.useFakeTimers()
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index a5c1f582d5e..1199833590d 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -21,6 +21,7 @@ import {
} from '@/lib/gateway-liveness-policy'
import { isMissingRpcMethod } from '@/lib/gateway-rpc'
import { traceIdentityChange } from '@/lib/identity-trace'
+import { isPermanentLocalGatewayEnsureError } from '@/lib/local-gateway-ensure-failure'
import {
isTimeoutError,
RECONNECT_ATTEMPT_TIMEOUT_MS,
@@ -990,10 +991,18 @@ async function reconnectSecondary(entry: Secondary): Promise<void> {
return
}
- // Only a successful open resets the stall budget (the 'open' state
- // listener): a treadmill that alternates slot-wait timeouts with a
- // spawned-but-unresponsive socket must still run out of budget.
- if (isStalledDialError(error)) {
+ // An ensure refusal no redial can fix (incompatible runtime, no ensure protocol, an invalid
+ // endpoint, an unsafe control path): each automatic redial would spawn another
+ // `hermes gateway ensure` against the same install forever. Park at once, keeping the entry:
+ // an explicit open, focus/wake nudge or Reconnect re-arms it after the user's repair.
+ if (isPermanentLocalGatewayEnsureError(error)) {
+ console.warn(`[gateway] parking scope="${entry.scope}": local gateway ensure refused permanently`, error)
+ entry.wantOpen = false
+ entry.stalledDials = 0
+ } else if (isStalledDialError(error)) {
+ // Only a successful open resets the stall budget (the 'open' state
+ // listener): a treadmill that alternates slot-wait timeouts with a
+ // spawned-but-unresponsive socket must still run out of budget.
entry.stalledDials += 1
if (entry.stalledDials >= SECONDARY_STALLED_DIAL_BUDGET) {