Skip to content

The agent that grows with you

fix(webapp): keep naming the picked profile in the live-status poll after a reconnect

unsupportedpastels719583b227c3
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.tsx b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.tsx
index 5cd998ffdf3..93a0e4663d6 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.tsx
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.tsx
@@ -2,6 +2,7 @@ import { act, cleanup, renderHook } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { HermesConnection } from '@/global'
+import { installBrowserDesktopBridge } from '@/lib/browser-desktop-bridge'
import { createClientSessionState } from '@/lib/chat-runtime'
import { $sidebarShowArchived } from '@/store/layout'
import { $changeEventsAvailable, $cronChangeTick, $sessionsChangeTick } from '@/store/live-sync'
@@ -16,6 +17,7 @@ import {
publishSessionState
} from '@/store/session-states'
import { loadArchivedSessions } from '@/store/sidebar-archive'
+import { clearWebappSession, seedWebappSession } from '@/test/webapp-session'
import { useBackgroundSync } from './use-background-sync'
@@ -181,6 +183,41 @@ describe('useBackgroundSync live-status poll with no session open', () => {
cleanup()
expect(await polledParams(null)).toEqual([{}])
})
+
+ // A reconnect re-resolves the socket's own route and publishes the primary's descriptor
+ // (sharedPrimary false) while the picked profile stays selected; the poll must still name it.
+ it('keeps naming the picked profile after a reconnect republishes the primary descriptor', async () => {
+ const win = window as unknown as Window & { __HERMES_DASHBOARD_PROFILE__?: string }
+ win.__HERMES_DASHBOARD_PROFILE__ = 'default'
+ seedWebappSession('served-token')
+ window.history.replaceState(null, '', '/?profile=research#/')
+
+ try {
+ expect(installBrowserDesktopBridge()).toBe(true)
+ const desktop = window.hermesDesktop!
+
+ const request = vi.fn(async (_method: string, _params?: Record<string, unknown>) => ({ sessions: [] }))
+ $connection.set(await desktop.getConnection('research'))
+ render('research', 'local', async () => undefined, request as typeof requestGateway)
+ await act(async () => undefined)
+
+ $connection.set(await desktop.getConnection())
+ expect($connection.get()?.sharedPrimary).toBeFalsy()
+ await act(async () => {
+ $sessionsChangeTick.set(1)
+ })
+ await act(async () => undefined)
+
+ expect(
+ request.mock.calls.filter(([method]) => method === 'session.active_list').map(([, params]) => params)
+ ).toEqual([{ profile: 'research' }, { profile: 'research' }])
+ } finally {
+ delete win.__HERMES_DASHBOARD_PROFILE__
+ clearWebappSession()
+ Reflect.deleteProperty(window, 'hermesDesktop')
+ window.history.replaceState(null, '', '/#/')
+ }
+ })
})
describe('useBackgroundSync keeps a quiet working turn live', () => {
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
index d3f52087221..7dd4a88c4c2 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -19,11 +19,11 @@ import {
import { createClientSessionState } from '@/lib/chat-runtime'
import { sessionListFingerprint, sessionMessagesSignature } from '@/lib/session-signatures'
import { latestSessionTodos, latestSessionTodoSnapshot } from '@/lib/todos'
-import { pendingSessionReplay } from '@/store/gateway'
+import { pendingSessionReplay, sharedPrimaryRoute } from '@/store/gateway'
import { $sidebarShowArchived } from '@/store/layout'
import { $changeEventsAvailable, $cronChangeTick, $projectsChangeTick, $sessionsChangeTick } from '@/store/live-sync'
import { $onBattery, batteryPollInterval } from '@/store/power'
-import { refreshActiveProfile } from '@/store/profile'
+import { normalizeProfileKey, refreshActiveProfile } from '@/store/profile'
import { refreshProjects, refreshProjectTree } from '@/store/projects'
import {
$activeSessionId,
@@ -914,13 +914,30 @@ export function rehydrateLiveSessionStatuses(
* open it rides the active socket, and a profile that shares the primary socket
* (a Webapp tab's live pick, a global remote) is only reached by naming it: the
* socket answers an unnamed request for its own profile, and the sidebar would
- * paint that profile's turns instead of the selected one's. */
-function liveStatusParams(activeGatewayProfile: string): Record<string, unknown> {
- if ($focusedStoredSessionId.get() || $connection.get()?.sharedPrimary !== true) {
+ * paint that profile's turns instead of the selected one's.
+ *
+ * The published descriptor is not enough on its own: a reconnect republishes
+ * the primary's own descriptor (sharedPrimary false) while the picked profile
+ * stays selected. So a remote descriptor that isn't the selected profile's own
+ * asks main for the selected profile's route, the check requestGatewayForProfile
+ * routes by. A local descriptor never shares the primary and asks nothing. */
+async function liveStatusParams(activeGatewayProfile: string): Promise<Record<string, unknown>> {
+ const connection = $connection.get()
+
+ if ($focusedStoredSessionId.get() || !connection) {
return {}
}
- return { profile: activeGatewayProfile }
+ if (connection.sharedPrimary === true) {
+ return { profile: activeGatewayProfile }
+ }
+
+ const ownSocket =
+ connection.mode !== 'remote' ||
+ (Boolean(connection.profile?.trim()) &&
+ normalizeProfileKey(connection.profile) === normalizeProfileKey(activeGatewayProfile))
+
+ return !ownSocket && (await sharedPrimaryRoute(activeGatewayProfile)) ? { profile: activeGatewayProfile } : {}
}
/** Forget every profile's live-runtime bookkeeping. A gateway wipe already
@@ -1296,7 +1313,7 @@ export function useBackgroundSync({
try {
const response = await requestGateway<LiveSessionStatusResponse>(
'session.active_list',
- liveStatusParams(activeGatewayProfile)
+ await liveStatusParams(activeGatewayProfile)
)
if (!cancelled) {
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 1199833590d..c83afe99fdf 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -1124,7 +1124,7 @@ function createSecondary(profile: string, connectionId: null | string = null): S
// the second dial fails (tunnel/token are per-backend) and the closed socket
// poisons the active gateway with "not connected" even though the primary is
// open right next to it.
-async function sharedPrimaryRoute(profile: string): Promise<boolean> {
+export async function sharedPrimaryRoute(profile: string): Promise<boolean> {
const desktop = window.hermesDesktop
if (!desktop) {