fix(desktop): calm background sessions Closes #73287

This commit is contained in:
finn763
2026-09-04 01:16:12 +08:00
committed by kshitij
parent a74e76632c
commit b4ccbccf9c
3 changed files with 115 additions and 4 deletions
+19
View File
@@ -0,0 +1,19 @@
// Repro #73287: a hidden tile must not fire the 5s process.list safety-net
// poll. Runs the hidden-pane test before (guard stashed) and after the fix.
import { execSync } from 'node:child_process'
import { fileURLToPath } from 'node:url'
const root = fileURLToPath(new URL('.', import.meta.url))
const fix = '../src/app/chat/composer/status-stack/index.tsx'
const test = 'src/app/chat/composer/status-stack/polling-guard.test.tsx -t "hidden tile"'
const check = () => {
try {
execSync(`npx vitest run --project ui ${test}`, { cwd: root, stdio: 'pipe' })
return 'PASS (hidden tile: 1 process.list call, mount seed only)'
} catch (e) {
return `FAIL (${(String(e.stdout ?? '') + String(e.stderr ?? '')).replace(/\[[0-9;]*m/g, '').match(/expected \d+ to be \d+/)?.[0] ?? 'see output'})`
}
}
execSync(`git stash push -q -- ${fix}`, { cwd: root })
console.log('before (no guard):', check())
execSync('git stash pop -q', { cwd: root })
console.log('after (guard): ', check())
@@ -8,6 +8,7 @@ import type { SubmitTextOptions } from '@/app/session/hooks/use-prompt-actions/u
import { BillingBanner } from '@/components/billing-banner'
import { composerDockCard } from '@/components/chat/composer-dock'
import { StatusSection } from '@/components/chat/status-section'
import { usePaneVisible } from '@/components/pane-shell/pane-visibility'
import { Button } from '@/components/ui/button'
import { Codicon } from '@/components/ui/codicon'
import { GlyphSpinner } from '@/components/ui/glyph-spinner'
@@ -141,15 +142,26 @@ export function ComposerStatusStack({ onSubmit, queue, sessionId }: ComposerStat
// dead `localhost:5174` chips stick around. On-disk file previews are kept.
const visiblePreviews = previews.filter(item => hasRunningBackground || !isLocalhostPreview(item.target))
// Keep-alive keeps every ever-active tab mounted, so without this gate each
// background tile's safety-net poll fires every 5s — N sessions means N
// gateway round-trips plus shared-map churn forever. Hidden tabs skip the
// poll (event-driven refreshes in use-message-stream still land through the
// store) and resume it on reveal via `paneVisible` in the dep array.
const paneVisible = usePaneVisible()
useEffect(() => {
if (!sessionId || !hasRunningBackground) {
if (!sessionId || !hasRunningBackground || !paneVisible) {
return
}
const timer = setInterval(() => void refreshBackgroundProcesses(sessionId), BACKGROUND_POLL_MS)
const timer = setInterval(() => {
if (document.visibilityState === 'visible') {
void refreshBackgroundProcesses(sessionId)
}
}, BACKGROUND_POLL_MS)
return () => clearInterval(timer)
}, [hasRunningBackground, sessionId])
}, [hasRunningBackground, sessionId, paneVisible])
const openAgents = () => navigate(AGENTS_ROUTE)
@@ -2,8 +2,9 @@ import { cleanup, render } from '@testing-library/react'
import { MemoryRouter } from 'react-router'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { PaneVisibleContext } from '@/components/pane-shell/pane-visibility'
import { I18nProvider } from '@/i18n'
import { resetBackgroundPollingGuard } from '@/store/composer-status'
import { $backgroundStatusBySession, resetBackgroundPollingGuard } from '@/store/composer-status'
import { $gateway } from '@/store/gateway'
import { ComposerStatusStack } from './index'
@@ -76,3 +77,82 @@ describe('ComposerStatusStack dead-runtime remount', () => {
second.unmount()
})
})
// #73287: keep-alive keeps every ever-active tab mounted, so each background
// tile's 5s safety-net poll used to fire even while its tab was hidden — N
// sessions meant N gateway round-trips plus shared-map churn forever.
describe('ComposerStatusStack hidden-pane poll', () => {
const SID_BG = 'sess-bg-poll'
const runningList = vi.fn(async (method: string) =>
method === 'process.list'
? { processes: [{ command: 'dev server', session_id: 'bg1', status: 'running' }] }
: {}
)
const processListCalls = () => runningList.mock.calls.filter(([method]) => method === 'process.list').length
function renderStackBg(visible: boolean) {
return render(
<MemoryRouter>
<I18nProvider configClient={null} initialLocale="en">
<PaneVisibleContext.Provider value={visible}>
<ComposerStatusStack queue={null} sessionId={SID_BG} />
</PaneVisibleContext.Provider>
</I18nProvider>
</MemoryRouter>
)
}
beforeEach(() => {
vi.useFakeTimers()
// jsdom defaults to 'prerender', which the in-tick document check skips.
vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('visible')
runningList.mockClear()
$gateway.set({ request: runningList } as never)
})
afterEach(() => {
cleanup()
vi.useRealTimers()
vi.restoreAllMocks()
$gateway.set(null as never)
$backgroundStatusBySession.set({})
resetBackgroundPollingGuard()
})
it('hidden tile skips the 5s poll; visible tile polls; reveal resumes', async () => {
const hidden = renderStackBg(false)
await vi.advanceTimersByTimeAsync(0)
await vi.advanceTimersByTimeAsync(15_000)
// Mount seed only — the interval never armed while hidden.
expect(processListCalls()).toBe(1)
hidden.unmount()
const shown = renderStackBg(true)
await vi.advanceTimersByTimeAsync(0)
await vi.advanceTimersByTimeAsync(15_000)
// Mount seed + three 5s ticks.
expect(processListCalls()).toBe(1 + 1 + 3)
shown.unmount()
})
it('revealing a hidden tile arms the poll', async () => {
const view = renderStackBg(false)
await vi.advanceTimersByTimeAsync(0)
await vi.advanceTimersByTimeAsync(15_000)
expect(processListCalls()).toBe(1)
view.rerender(
<MemoryRouter>
<I18nProvider configClient={null} initialLocale="en">
<PaneVisibleContext.Provider value>
<ComposerStatusStack queue={null} sessionId={SID_BG} />
</PaneVisibleContext.Provider>
</I18nProvider>
</MemoryRouter>
)
await vi.advanceTimersByTimeAsync(10_000)
expect(processListCalls()).toBeGreaterThan(1)
view.unmount()
})
})