The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a prune no longer cancels the redial of the route it is pruning
John Paul Solivac382333e25e2
diff --git a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts b/apps/desktop/src/store/gateway-connection-lifecycle.test.tsindex 665fe86f074..9592285f9fc 100644--- a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts+++ b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts@@ -99,6 +99,43 @@ afterEach(() => {delete (window as unknown as { hermesDesktop?: unknown }).hermesDesktop})+describe('a redial of the active route', () => {+ it('is not cancelled by a prune that runs while it is dialing', async () => {+ // Editing the connection you are viewing defers the redial until its lease drops, then+ // disposes the entry and re-activates the same scope asynchronously. While that is in flight+ // the entry is gone from the map, so the pruner's safety net sees an active scope with no+ // entry and calls setActive(primary) — which bumps the activation epoch and turns the redial's+ // own applyActive(epoch) into a no-op. The window then sits on the primary backend and the+ // redial is discarded silently.+ installDesktop({+ getConnectionFor: vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) =>+ descriptorFor(connectionId, profile)+ )+ })++ await ensureGatewayForAgent('homelab', 'writer')+ const release = await retainGatewayForAgent('homelab', 'writer')++ disposeSecondariesForConnection('homelab', { redial: true })++ let openDial = () => {}++ const dialing = new Promise<void>(resolve => {+ openDial = resolve+ })++ gatewayMocks.connect.mockImplementationOnce(async () => dialing)++ release() // drains the pending redial: dispose, evict, re-activate+ pruneSecondaryGateways(new Set()) // the steal, while the redial is still suspended+ openDial()++ await vi.waitFor(() => {+ expect(activeGateway()).toBe(gatewayMocks.instances.at(-1))+ })+ })+})+describe('disposeSecondariesForConnection', () => {it('keeps the previous source socket alive when another source becomes foreground', async () => {const getConnectionFor = vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) =>diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.tsindex a4e34dc6f13..5445a118eee 100644--- a/apps/desktop/src/store/gateway.ts+++ b/apps/desktop/src/store/gateway.ts@@ -195,6 +195,8 @@ interface GatewayRegistryState {reauthFailures: Map<string, { connectionId: string | null; error: Error }>/** Scopes that opened in this renderer generation, even if later pruned. */openedSecondaryScopes?: Set<string>+ /** Scopes whose re-activation after a connection redial has not landed yet. */+ reactivatingScopes?: Set<string>/** Routed prompt sockets held until their terminal turn event arrives. */turnLeases: Map<string, () => void>/** Debounced releases so an immediate chained turn can reuse its lease. */@@ -217,6 +219,7 @@ function createRegistryState(): GatewayRegistryState {secondaries: new Map<string, Secondary>(),reauthFailures: new Map(),openedSecondaryScopes: new Set<string>(),+ reactivatingScopes: new Set<string>(),turnLeases: new Map<string, () => void>(),turnLeaseReleaseTimers: new Map<string, ReturnType<typeof setTimeout>>(),// The active gateway instance, exposed for inline message-stream@@ -261,6 +264,8 @@ const g = gatewayState()// Dev HMR can hand a newer module an older state-container shape. Keep the// generation ledger lazy so an already-open socket still survives the update.const openedSecondaryScopes = (): Set<string> => (g.openedSecondaryScopes ??= new Set<string>())+// Dev-HMR states predate this field, so read it through the same lazy accessor pattern.+const reactivatingScopes = (): Set<string> => (g.reactivatingScopes ??= new Set<string>())// Re-exported as a stable binding: the atom instance lives in `g`, so every hot// reload of this module hands back the SAME atom subscribers are already wired@@ -1251,11 +1256,23 @@ function drainPendingConnectionRedial(entry: Secondary): boolean {disposeSecondary(entry)g.secondaries.delete(entry.scope)- const reopen = wasActive- ? ensureGatewayForAgent(entry.connectionId, entry.profile)- : openGatewayForAgent(entry.connectionId, entry.profile)+ if (!wasActive) {+ void openGatewayForAgent(entry.connectionId, entry.profile).catch(() => undefined)- void reopen.catch(() => undefined)+ return true+ }++ // The re-activation is asynchronous and the entry is already out of the map. Until it lands,+ // restoreActiveToPrimaryIfEvicted sees an active scope with no entry, calls setActive(primary)+ // and bumps the activation epoch — which turns this redial's own applyActive(epoch) into a+ // no-op. The window would then sit on the primary backend with the redial silently discarded,+ // after nothing more than an edit to the connection being viewed.+ reactivatingScopes().add(entry.scope)+ void ensureGatewayForAgent(entry.connectionId, entry.profile)+ .catch(() => undefined)+ .finally(() => {+ reactivatingScopes().delete(entry.scope)+ })return true}@@ -1969,7 +1986,14 @@ function disposeSecondary(entry: Secondary): void {// (closeSecondaryGateways in use-gateway-boot) left activeKey pointing at an// evicted registry scope and every call silently hit the primary backend.function restoreActiveToPrimaryIfEvicted(): void {- if (g.activeKey !== g.primaryProfile && !g.secondaries.has(g.activeKey)) {+ if (+ g.activeKey !== g.primaryProfile &&+ !g.secondaries.has(g.activeKey) &&+ // A redial evicts the entry and re-activates the same scope moments later; taking the+ // activation in between cancels it through the epoch. The redial's own finally always clears+ // this, so a failed redial still falls back to the primary.+ !reactivatingScopes().has(g.activeKey)+ ) {setActive(g.primaryProfile)}}