Skip to content

The agent that grows with you

fix(webapp): ask the selected profile for live status with no session open

unsupportedpastels837e659011e1
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 a8cc4e3d950..5cd998ffdf3 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
@@ -1,11 +1,12 @@
import { act, cleanup, renderHook } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import type { HermesConnection } from '@/global'
import { createClientSessionState } from '@/lib/chat-runtime'
import { $sidebarShowArchived } from '@/store/layout'
import { $changeEventsAvailable, $cronChangeTick, $sessionsChangeTick } from '@/store/live-sync'
import { $onBattery } from '@/store/power'
-import { $activeSessionId } from '@/store/session'
+import { $activeSessionId, $connection, $selectedStoredSessionId } from '@/store/session'
import {
$sessionStates,
$workingSessionIds,
@@ -135,6 +136,53 @@ describe('useBackgroundSync profile-scoped session refresh', () => {
})
})
+describe('useBackgroundSync live-status poll with no session open', () => {
+ // A profile picked on top of the shared primary socket (a Webapp tab's live pick, a global remote)
+ // is a view of that socket: a request that names no profile is answered for the socket's own
+ // profile, so the sidebar dot would show that profile's turns instead of the selected one's.
+ const sharedResearch = { profile: 'research', sharedPrimary: true } as HermesConnection
+
+ async function polledParams(connection: HermesConnection | null) {
+ $connection.set(connection)
+ const request = vi.fn(async (_method: string, _params?: Record<string, unknown>) => ({ sessions: [] }))
+ render('research', 'local', async () => undefined, request as typeof requestGateway)
+ await act(async () => undefined)
+
+ return request.mock.calls.filter(([method]) => method === 'session.active_list').map(([, params]) => params)
+ }
+
+ beforeEach(() => {
+ vi.useFakeTimers()
+ $activeSessionId.set(null)
+ $selectedStoredSessionId.set(null)
+ $changeEventsAvailable.set(false)
+ $sessionsChangeTick.set(0)
+ })
+
+ afterEach(() => {
+ cleanup()
+ $connection.set(null)
+ $selectedStoredSessionId.set(null)
+ vi.useRealTimers()
+ })
+
+ it('names the selected profile when it rides the shared primary socket', async () => {
+ expect(await polledParams(sharedResearch)).toEqual([{ profile: 'research' }])
+ })
+
+ it('asks what it asked before while a session is open (the session owner routes it)', async () => {
+ $selectedStoredSessionId.set('s-open')
+
+ expect(await polledParams(sharedResearch)).toEqual([{}])
+ })
+
+ it('names no profile on a socket of the selected profile’s own', async () => {
+ expect(await polledParams({ profile: 'research' } as HermesConnection)).toEqual([{}])
+ cleanup()
+ expect(await polledParams(null)).toEqual([{}])
+ })
+})
+
describe('useBackgroundSync keeps a quiet working turn live', () => {
// A foreground tool emits nothing between tool.start and tool.complete; the
// backend is the only witness that the turn is still running.
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 537f331d586..d3f52087221 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -28,6 +28,7 @@ import { refreshProjects, refreshProjectTree } from '@/store/projects'
import {
$activeSessionId,
$busy,
+ $connection,
$currentCwd,
$messagingSessions,
$selectedStoredSessionId,
@@ -37,6 +38,7 @@ import {
sessionMatchesStoredId,
setCurrentCwd
} from '@/store/session'
+import { $focusedStoredSessionId } from '@/store/session-focus'
import type { SessionOwnerRoute } from '@/store/session-request-router'
import {
$sessionStates,
@@ -907,6 +909,20 @@ export function rehydrateLiveSessionStatuses(
confirmReconnectSettlesExcept(workingStoredIds)
}
+/** Params for the live-status poll. With a session open the session RPC
+ * dispatcher routes it to that session's owner, so it names nothing. With none
+ * 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) {
+ return {}
+ }
+
+ return { profile: activeGatewayProfile }
+}
+
/** Forget every profile's live-runtime bookkeeping. A gateway wipe already
* drops the session states these ids point at, so a carried-over set would
* only reap runtimes that no longer exist. */
@@ -1278,7 +1294,10 @@ export function useBackgroundSync({
const stateAtRequest = $sessionStates.get()
try {
- const response = await requestGateway<LiveSessionStatusResponse>('session.active_list', {})
+ const response = await requestGateway<LiveSessionStatusResponse>(
+ 'session.active_list',
+ liveStatusParams(activeGatewayProfile)
+ )
if (!cancelled) {
rehydrateLiveSessionStatuses(response, Date.now(), activeGatewayProfile, stateAtRequest)