Skip to content

The agent that grows with you

perf(desktop): stop hidden-pane subagent.list/tail polls

KoNit-K8c3ffa5cc15d
diff --git a/apps/desktop/src/app/chat/composer/status-stack/hidden-pane-subagent-polls.test.tsx b/apps/desktop/src/app/chat/composer/status-stack/hidden-pane-subagent-polls.test.tsx
new file mode 100644
index 00000000000..731c615a95d
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/status-stack/hidden-pane-subagent-polls.test.tsx
@@ -0,0 +1,147 @@
+import { cleanup, render } from '@testing-library/react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { PaneVisibleContext } from '@/components/pane-shell/pane-visibility'
+import { I18nProvider } from '@/i18n'
+import * as gateway from '@/store/gateway'
+import { _resetSessionOwnerHintsForTests, setSessionOwnerHint } from '@/store/session'
+import { $subagentsBySession } from '@/store/subagents'
+
+import { SubagentTranscript } from './subagent-transcript'
+import { useSubagentSnapshot } from './use-subagent-snapshot'
+
+const SID = 'sess-hidden-subagent-poll'
+
+function SnapshotHarness({ sessionId }: { sessionId: string }) {
+ useSubagentSnapshot(sessionId)
+ return <div data-testid="snapshot-harness" />
+}
+
+function renderSnapshot(visible: boolean) {
+ return render(
+ <PaneVisibleContext.Provider value={visible}>
+ <SnapshotHarness sessionId={SID} />
+ </PaneVisibleContext.Provider>
+ )
+}
+
+function renderTranscript(visible: boolean) {
+ return render(
+ <I18nProvider configClient={null} initialLocale="en">
+ <PaneVisibleContext.Provider value={visible}>
+ <SubagentTranscript sessionId={SID} subagentId="worker" />
+ </PaneVisibleContext.Provider>
+ </I18nProvider>
+ )
+}
+
+// #106686: keep-alive hidden tiles stay mounted with document.visibilityState
+// === 'visible'. document.hidden alone does not stop subagent.list (5s) or
+// subagent.tail (2s); both must gate the interval on usePaneVisible().
+describe('hidden-pane subagent polls', () => {
+ const request = vi.fn(async (_c: string, _p: string, method: string) => {
+ if (method === 'subagent.list') {
+ return { subagents: [] }
+ }
+
+ if (method === 'subagent.tail') {
+ return { available: true, text: 'tail', truncated: false }
+ }
+
+ return {}
+ })
+
+ const listCalls = () => request.mock.calls.filter(([, , method]) => method === 'subagent.list').length
+ const tailCalls = () => request.mock.calls.filter(([, , method]) => method === 'subagent.tail').length
+
+ beforeEach(() => {
+ vi.useFakeTimers()
+ vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('visible')
+ request.mockClear()
+ vi.spyOn(gateway, 'requestGatewayForAgent').mockImplementation(request as never)
+ setSessionOwnerHint(SID, { connectionId: 'remote-owner', profile: 'research' })
+ })
+
+ afterEach(() => {
+ cleanup()
+ vi.useRealTimers()
+ vi.restoreAllMocks()
+ $subagentsBySession.set({})
+ _resetSessionOwnerHintsForTests()
+ })
+
+ it('hidden tile seeds subagent.list once; visible tile polls; reveal resumes', async () => {
+ const hidden = renderSnapshot(false)
+ await vi.advanceTimersByTimeAsync(0)
+ await vi.advanceTimersByTimeAsync(15_000)
+ expect(listCalls()).toBe(1)
+ hidden.unmount()
+
+ request.mockClear()
+ const shown = renderSnapshot(true)
+ await vi.advanceTimersByTimeAsync(0)
+ await vi.advanceTimersByTimeAsync(15_000)
+ // Mount seed + three 5s ticks.
+ expect(listCalls()).toBe(1 + 3)
+ shown.unmount()
+ })
+
+ it('revealing a hidden snapshot tile arms the 5s poll', async () => {
+ const view = renderSnapshot(false)
+ await vi.advanceTimersByTimeAsync(0)
+ await vi.advanceTimersByTimeAsync(15_000)
+ expect(listCalls()).toBe(1)
+
+ view.rerender(
+ <PaneVisibleContext.Provider value>
+ <SnapshotHarness sessionId={SID} />
+ </PaneVisibleContext.Provider>
+ )
+ await vi.advanceTimersByTimeAsync(10_000)
+ expect(listCalls()).toBeGreaterThan(1)
+ view.unmount()
+ })
+
+ it('hidden tile seeds subagent.tail once; visible tile polls; reveal resumes', async () => {
+ const hidden = renderTranscript(false)
+ await vi.advanceTimersByTimeAsync(0)
+ await vi.advanceTimersByTimeAsync(6_000)
+ expect(tailCalls()).toBe(1)
+ hidden.unmount()
+
+ request.mockClear()
+ const shown = renderTranscript(true)
+ await vi.advanceTimersByTimeAsync(0)
+ await vi.advanceTimersByTimeAsync(6_000)
+ // Mount seed + three 2s ticks.
+ expect(tailCalls()).toBe(1 + 3)
+ shown.unmount()
+ })
+
+ it('revealing a hidden transcript tile arms the 2s poll', async () => {
+ const view = renderTranscript(false)
+ await vi.advanceTimersByTimeAsync(0)
+ await vi.advanceTimersByTimeAsync(6_000)
+ expect(tailCalls()).toBe(1)
+
+ view.rerender(
+ <I18nProvider configClient={null} initialLocale="en">
+ <PaneVisibleContext.Provider value>
+ <SubagentTranscript sessionId={SID} subagentId="worker" />
+ </PaneVisibleContext.Provider>
+ </I18nProvider>
+ )
+ await vi.advanceTimersByTimeAsync(4_000)
+ expect(tailCalls()).toBeGreaterThan(1)
+ view.unmount()
+ })
+
+ it('visible snapshot tile skips in-tick polls when the document is hidden', async () => {
+ vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('hidden')
+ const shown = renderSnapshot(true)
+ await vi.advanceTimersByTimeAsync(0)
+ await vi.advanceTimersByTimeAsync(15_000)
+ expect(listCalls()).toBe(1)
+ shown.unmount()
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/status-stack/subagent-transcript.tsx b/apps/desktop/src/app/chat/composer/status-stack/subagent-transcript.tsx
index 129bdf8d828..ca637f4f944 100644
--- a/apps/desktop/src/app/chat/composer/status-stack/subagent-transcript.tsx
+++ b/apps/desktop/src/app/chat/composer/status-stack/subagent-transcript.tsx
@@ -1,5 +1,6 @@
import { useEffect, useState } from 'react'
+import { usePaneVisible } from '@/components/pane-shell/pane-visibility'
import { useI18n } from '@/i18n'
import { knownOwnerForSession, requestForOwnedSession } from '@/store/session-states'
@@ -13,6 +14,7 @@ interface Tail {
export function SubagentTranscript({ sessionId, subagentId }: { sessionId: string; subagentId: string }) {
const { t } = useI18n()
+ const paneVisible = usePaneVisible()
const [tail, setTail] = useState<Tail | null>(null)
useEffect(() => {
let cancelled = false
@@ -49,13 +51,17 @@ export function SubagentTranscript({ sessionId, subagentId }: { sessionId: strin
}
void refresh()
- const timer = window.setInterval(() => void refresh(), 2000)
+ // Keep-alive tiles stay mounted; only arm the 2s tail poll while the pane
+ // is the visible tab. Reveal re-arms via `paneVisible` in the dep array.
+ const timer = paneVisible ? window.setInterval(() => void refresh(), 2000) : undefined
return () => {
cancelled = true
- window.clearInterval(timer)
+ if (timer !== undefined) {
+ window.clearInterval(timer)
+ }
}
- }, [sessionId, subagentId])
+ }, [sessionId, subagentId, paneVisible])
return (
<section className="mt-2 text-xs" data-slot="subagent-transcript">
diff --git a/apps/desktop/src/app/chat/composer/status-stack/use-subagent-snapshot.ts b/apps/desktop/src/app/chat/composer/status-stack/use-subagent-snapshot.ts
index 191f60f130b..3d586f209f6 100644
--- a/apps/desktop/src/app/chat/composer/status-stack/use-subagent-snapshot.ts
+++ b/apps/desktop/src/app/chat/composer/status-stack/use-subagent-snapshot.ts
@@ -1,6 +1,7 @@
import { useStore } from '@nanostores/react'
import { useEffect } from 'react'
+import { usePaneVisible } from '@/components/pane-shell/pane-visibility'
import { $gatewayState } from '@/store/session'
import { knownOwnerForSession, requestForOwnedSession } from '@/store/session-states'
import { $subagentsBySession, reconcileSubagentSnapshot, type SubagentPayload } from '@/store/subagents'
@@ -12,6 +13,7 @@ export const rejectUnownedSubagentRequest = async <T>(): Promise<T> => {
/** Hydrate even an empty composer; live events remain authoritative over reads. */
export function useSubagentSnapshot(sessionId: string | null) {
const gatewayState = useStore($gatewayState)
+ const paneVisible = usePaneVisible()
useEffect(() => {
if (!sessionId) {
return
@@ -57,7 +59,15 @@ export function useSubagentSnapshot(sessionId: string | null) {
}
void refresh()
- const timer = window.setInterval(() => void refresh(), 5000)
+ // Keep-alive tiles stay mounted; only arm the 5s safety-net while the pane
+ // is the visible tab. Reveal re-arms via `paneVisible` in the dep array.
+ const timer = paneVisible
+ ? window.setInterval(() => {
+ if (document.visibilityState === 'visible') {
+ void refresh()
+ }
+ }, 5000)
+ : undefined
const retry = () => {
failures = 0
@@ -68,8 +78,10 @@ export function useSubagentSnapshot(sessionId: string | null) {
return () => {
cancelled = true
- window.clearInterval(timer)
+ if (timer !== undefined) {
+ window.clearInterval(timer)
+ }
window.removeEventListener('focus', retry)
}
- }, [sessionId, gatewayState])
+ }, [sessionId, gatewayState, paneVisible])
}