Skip to content

The agent that grows with you

fix(desktop): preserve recovery refresh ownership

Zeus-Deus502298ef621b
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
index 05c13077909..3c592f346d6 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
@@ -11,7 +11,12 @@ import {
} from '@/store/connections'
import { closeSecondaryGateways, isActivePrimary } from '@/store/gateway'
import { reconnectGateway } from '@/store/gateway-reconnect'
-import { $gatewaySwitching, beginGatewaySwitch, endGatewaySwitch } from '@/store/gateway-switch'
+import {
+ $gatewaySwitching,
+ beginGatewaySwitch,
+ endGatewaySwitch,
+ recoverActiveSourceAfterFailedGatewaySwitch
+} from '@/store/gateway-switch'
import { notifyError } from '@/store/notifications'
import { $activeGatewayProfile, $profiles, ensureGatewayProfile } from '@/store/profile'
import {
@@ -765,6 +770,29 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>
expect($gatewaySwitching.get()).toBe(false)
})
+ it('forwards failed-switch recovery ownership through its registered lifecycle', async () => {
+ const refreshSessions = vi.fn(async (_shouldPublish?: () => boolean) => undefined)
+
+ render(<Harness refreshSessions={refreshSessions} />)
+ await flushAsync()
+
+ const failed = beginGatewaySwitch()
+
+ recoverActiveSourceAfterFailedGatewaySwitch(failed)
+ endGatewaySwitch(failed)
+ await vi.waitFor(() => expect(refreshSessions).toHaveBeenCalledTimes(2))
+
+ const shouldPublish = refreshSessions.mock.calls[1][0]
+
+ expect(shouldPublish).toBeTypeOf('function')
+ expect(shouldPublish?.()).toBe(true)
+
+ const newer = beginGatewaySwitch()
+
+ expect(shouldPublish?.()).toBe(false)
+ endGatewaySwitch(newer)
+ })
+
it('a superseded Settings switch cannot publish delayed cwd or config work after the winner', async () => {
const desktop = fakeDesktop() as ReturnType<typeof fakeDesktop> & Record<string, unknown>
const staleSanitize = deferred<{ cwd: string }>()
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 766816beac3..c39cb1107c7 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -186,7 +186,7 @@ export function useGatewayBoot({
// one reset, so the two doors can't drift apart again (#93937).
const offSwitchLifecycle = registerGatewaySwitchLifecycle({
beforeConnectionSwitch: () => callbacksRef.current.beforeConnectionSwitch(),
- refreshSessions: () => callbacksRef.current.refreshSessions()
+ refreshSessions: shouldPublish => callbacksRef.current.refreshSessions(shouldPublish)
})
// --- Reconnect-after-sleep machinery -------------------------------------
diff --git a/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx
index c9b949d899f..8a953a043a3 100644
--- a/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx
@@ -4,6 +4,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { SessionInfo, SidebarSessionsResponse } from '@/hermes'
import { $cronJobs, setCronJobs } from '@/store/cron'
+import {
+ beginGatewaySwitch,
+ endGatewaySwitch,
+ recoverActiveSourceAfterFailedGatewaySwitch,
+ registerGatewaySwitchLifecycle
+} from '@/store/gateway-switch'
import {
$cronSessions,
$messagingPlatformTotals,
@@ -302,6 +308,59 @@ describe('refreshSessions identity + loading hygiene', () => {
expect(getCronJobs).not.toHaveBeenCalled()
})
+ it('keeps failed-switch recovery from publishing through a newer switch', async () => {
+ const pending = deferred<SidebarSessionsResponse>()
+
+ listSidebarSessions.mockReturnValue(pending.promise)
+
+ const { result } = renderHook(() => useSessionListActions({ profileScope: 'default' }))
+
+ const off = registerGatewaySwitchLifecycle({
+ beforeConnectionSwitch: () => undefined,
+ refreshSessions: result.current.refreshSessions
+ })
+
+ let newer: number | undefined
+
+ try {
+ const failed = beginGatewaySwitch()
+
+ recoverActiveSourceAfterFailedGatewaySwitch(failed)
+ endGatewaySwitch(failed)
+ await vi.waitFor(() => expect(listSidebarSessions).toHaveBeenCalledTimes(1))
+
+ // A newer switch owns the freshly wiped lists and loading barrier while
+ // the failed switch's real sidebar publisher is still in flight.
+ newer = beginGatewaySwitch()
+
+ await act(async () => {
+ pending.resolve({
+ recents: {
+ profiles_truncated: { stale: true },
+ profiles_usage: { stale: { cost_usd: 1, tokens: 10 } },
+ sessions: [row('stale')]
+ },
+ cron: { sessions: [row('stale-cron', { source: 'cron' })] },
+ messaging: { sessions: [row('stale-message', { source: 'telegram' })] }
+ })
+ await pending.promise
+ })
+
+ expect($sessions.get()).toEqual([])
+ expect($cronSessions.get()).toEqual([])
+ expect($messagingSessions.get()).toEqual([])
+ expect($messagingTruncated.get()).toBe(false)
+ expect($sessionProfilesTruncated.get()).toEqual({})
+ expect($sessionProfilesUsage.get()).toEqual({})
+ expect($sessionsLoading.get()).toBe(true)
+ expect($cronJobs.get()).toEqual([])
+ expect(getCronJobs).not.toHaveBeenCalled()
+ } finally {
+ endGatewaySwitch(newer)
+ off()
+ }
+ })
+
it('clears initial loading after a failed source activation advances the gateway epoch', async () => {
const pending = deferred<SidebarSessionsResponse>()
listSidebarSessions.mockReturnValue(pending.promise)
diff --git a/apps/desktop/src/store/gateway-switch.ts b/apps/desktop/src/store/gateway-switch.ts
index cad23cf5af6..c241e00fef4 100644
--- a/apps/desktop/src/store/gateway-switch.ts
+++ b/apps/desktop/src/store/gateway-switch.ts
@@ -46,7 +46,7 @@ export const $gatewaySwitching = atom(false)
export interface GatewaySwitchLifecycle {
beforeConnectionSwitch: () => void
/** Re-pull the session lists from whichever backend is active NOW. */
- refreshSessions: () => Promise<void>
+ refreshSessions: (shouldPublish?: () => boolean) => Promise<void>
}
let switchLifecycle: GatewaySwitchLifecycle | null = null
@@ -152,7 +152,9 @@ export function recoverActiveSourceAfterFailedGatewaySwitch(token: GatewaySwitch
}
void Promise.resolve()
- .then(() => (isCurrentGatewaySwitch(token) ? lifecycle.refreshSessions() : undefined))
+ .then(() =>
+ isCurrentGatewaySwitch(token) ? lifecycle.refreshSessions(() => isCurrentGatewaySwitch(token)) : undefined
+ )
.catch(() => undefined)
.finally(() => {
if (isCurrentGatewaySwitch(token)) {