Skip to content

The agent that grows with you

perf(desktop): hidden-pane subagent polls use the sibling early-return shape

kshitijk4poord39caf45a0d5
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
index 731c615a95d..2f6c2b4d512 100644
--- 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
@@ -14,19 +14,20 @@ 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(
+function snapshotTree(visible: boolean) {
+ return (
<PaneVisibleContext.Provider value={visible}>
<SnapshotHarness sessionId={SID} />
</PaneVisibleContext.Provider>
)
}
-function renderTranscript(visible: boolean) {
- return render(
+function transcriptTree(visible: boolean) {
+ return (
<I18nProvider configClient={null} initialLocale="en">
<PaneVisibleContext.Provider value={visible}>
<SubagentTranscript sessionId={SID} subagentId="worker" />
@@ -36,8 +37,8 @@ function renderTranscript(visible: boolean) {
}
// #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().
+// === 'visible', so document.hidden alone never stops subagent.list (5s) or
+// subagent.tail (2s); both effects gate on usePaneVisible() and re-run on reveal.
describe('hidden-pane subagent polls', () => {
const request = vi.fn(async (_c: string, _p: string, method: string) => {
if (method === 'subagent.list') {
@@ -70,78 +71,43 @@ describe('hidden-pane subagent polls', () => {
_resetSessionOwnerHintsForTests()
})
- it('hidden tile seeds subagent.list once; visible tile polls; reveal resumes', async () => {
- const hidden = renderSnapshot(false)
- await vi.advanceTimersByTimeAsync(0)
+ it('hidden snapshot tile issues no subagent.list; reveal seeds once and polls every 5s; hiding again stops it', async () => {
+ const view = render(snapshotTree(false))
await vi.advanceTimersByTimeAsync(15_000)
- expect(listCalls()).toBe(1)
- hidden.unmount()
+ expect(listCalls()).toBe(0)
- request.mockClear()
- const shown = renderSnapshot(true)
+ view.rerender(snapshotTree(true))
await vi.advanceTimersByTimeAsync(0)
- await vi.advanceTimersByTimeAsync(15_000)
- // Mount seed + three 5s ticks.
+ expect(listCalls()).toBe(1)
+ await vi.advanceTimersByTimeAsync(15_100)
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)
+ view.rerender(snapshotTree(false))
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()
+ expect(listCalls()).toBe(1 + 3)
})
- it('hidden tile seeds subagent.tail once; visible tile polls; reveal resumes', async () => {
- const hidden = renderTranscript(false)
- await vi.advanceTimersByTimeAsync(0)
+ it('hidden transcript tile issues no subagent.tail; reveal seeds once and polls every 2s; hiding again stops it', async () => {
+ const view = render(transcriptTree(false))
await vi.advanceTimersByTimeAsync(6_000)
- expect(tailCalls()).toBe(1)
- hidden.unmount()
+ expect(tailCalls()).toBe(0)
- request.mockClear()
- const shown = renderTranscript(true)
+ view.rerender(transcriptTree(true))
await vi.advanceTimersByTimeAsync(0)
- await vi.advanceTimersByTimeAsync(6_000)
- // Mount seed + three 2s ticks.
+ expect(tailCalls()).toBe(1)
+ await vi.advanceTimersByTimeAsync(6_100)
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)
+ view.rerender(transcriptTree(false))
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()
+ expect(tailCalls()).toBe(1 + 3)
})
- it('visible snapshot tile skips in-tick polls when the document is hidden', async () => {
+ it('visible snapshot tile skips in-tick polls while the document is hidden', async () => {
vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('hidden')
- const shown = renderSnapshot(true)
+ render(snapshotTree(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 ca637f4f944..97a8cbf40e3 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
@@ -17,6 +17,11 @@ export function SubagentTranscript({ sessionId, subagentId }: { sessionId: strin
const paneVisible = usePaneVisible()
const [tail, setTail] = useState<Tail | null>(null)
useEffect(() => {
+ if (!paneVisible) {
+ // Keep-alive tiles stay mounted; only poll while this pane is the visible tab (reveal re-runs the effect).
+ return
+ }
+
let cancelled = false
let pending = false
const owner = JSON.stringify(knownOwnerForSession(sessionId))
@@ -51,15 +56,11 @@ export function SubagentTranscript({ sessionId, subagentId }: { sessionId: strin
}
void refresh()
- // 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
+ const timer = window.setInterval(() => void refresh(), 2000)
return () => {
cancelled = true
- if (timer !== undefined) {
- window.clearInterval(timer)
- }
+ window.clearInterval(timer)
}
}, [sessionId, subagentId, paneVisible])
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 3d586f209f6..d8564fde826 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
@@ -15,7 +15,8 @@ export function useSubagentSnapshot(sessionId: string | null) {
const gatewayState = useStore($gatewayState)
const paneVisible = usePaneVisible()
useEffect(() => {
- if (!sessionId) {
+ if (!sessionId || !paneVisible) {
+ // Keep-alive tiles stay mounted; only poll while this pane is the visible tab (reveal re-runs the effect).
return
}
@@ -59,15 +60,12 @@ export function useSubagentSnapshot(sessionId: string | null) {
}
void refresh()
- // 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 timer = window.setInterval(() => {
+ if (document.visibilityState === 'visible') {
+ void refresh()
+ }
+ }, 5000)
const retry = () => {
failures = 0
@@ -78,9 +76,7 @@ export function useSubagentSnapshot(sessionId: string | null) {
return () => {
cancelled = true
- if (timer !== undefined) {
- window.clearInterval(timer)
- }
+ window.clearInterval(timer)
window.removeEventListener('focus', retry)
}
}, [sessionId, gatewayState, paneVisible])