From 71e3d4d44f5c69d27c4afcaf6bc3bbd0687a1d4b Mon Sep 17 00:00:00 2001
From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com>
Date: Sat, 19 Sep 2026 04:52:08 -0700
Subject: [PATCH] desktop: stop refreshing and animating when the window is
hidden (#1496)
The desktop kept polling and re-parsing on its 60s timer even while minimized or hidden, and the flame wordmark animated a background-clip gradient forever, repainting every frame; together they pegged the app near 60% CPU at rest. The refresh timer now stops when the window is hidden and resumes with an immediate catch-up when it returns, and the wordmark gradient is static. Measured: all CodeBurn processes drop to 0% when hidden and the renderer idle drops from about 8% to 2%. Refresh speed and all numbers are unchanged; only when polls fire changed, never what is fetched. The remaining focused-with-active-agents full reparse is addressed separately by incremental re-parse.
---
app/renderer/App.test.tsx | 42 +++++++++++++++++-----------
app/renderer/hooks/usePolled.test.ts | 19 ++++++++-----
app/renderer/hooks/usePolled.ts | 24 ++++++++++------
app/renderer/styles/plain.css | 3 +-
4 files changed, 55 insertions(+), 33 deletions(-)
diff --git a/app/renderer/App.test.tsx b/app/renderer/App.test.tsx
index 8a226c59..b69d457d 100644
--- a/app/renderer/App.test.tsx
+++ b/app/renderer/App.test.tsx
@@ -1027,35 +1027,45 @@ describe('freshness: hidden-window polling', () => {
__resetPolledMemo()
})
- // An open-but-occluded app still owes the configured freshness contract.
- it('keeps interval refreshes running while hidden without a duplicate catch-up', async () => {
+ // A hidden/minimized window has no viewer to keep fresh; polling it was the
+ // app's dominant idle drain, so the interval stops while hidden and catches up
+ // on return.
+ it('stops interval refreshes while hidden and catches up on return', async () => {
vi.useFakeTimers()
try {
setVisibility('visible')
render()
- // Boot + three visible 30s cadences: the overview section's yield poll (a
- // pure usePolled interval, never prefetched) fires each cadence.
- await act(async () => { await vi.advanceTimersByTimeAsync(3_000) })
- await act(async () => { await vi.advanceTimersByTimeAsync(30_000 * 3) })
- const visibleYield = mocks.getYield.mock.calls.length
- expect(visibleYield).toBeGreaterThan(1) // polling while visible
+ // Boot and let the one-time provider prefetch warm queue fully drain, so
+ // the only remaining source of yield calls is the visible usePolled
+ // interval. Then measure its steady per-cadence rate as the baseline.
+ await act(async () => { await vi.advanceTimersByTimeAsync(30_000 * 8) })
+ const beforeCadence = mocks.getYield.mock.calls.length
+ await act(async () => { await vi.advanceTimersByTimeAsync(30_000) })
+ const perCadence = mocks.getYield.mock.calls.length - beforeCadence
+ expect(perCadence).toBeGreaterThan(0) // polling while visible
- // Hidden for five cadences: data polling continues.
+ // Hidden: the interval stops, so no CLI re-parse fires while unwatched.
setVisibility('hidden')
+ await act(async () => { document.dispatchEvent(new Event('visibilitychange')) })
const atHideYield = mocks.getYield.mock.calls.length
const atHideOverview = mocks.getOverview.mock.calls.length
await act(async () => { await vi.advanceTimersByTimeAsync(30_000 * 5) })
- expect(mocks.getYield.mock.calls.length).toBeGreaterThan(atHideYield)
- expect(mocks.getOverview.mock.calls.length).toBeGreaterThan(atHideOverview)
+ expect(mocks.getYield.mock.calls.length).toBe(atHideYield)
+ expect(mocks.getOverview.mock.calls.length).toBe(atHideOverview)
- // Back to visible: no extra catch-up is needed inside one cadence.
- const beforeVisibleYield = mocks.getYield.mock.calls.length
- const beforeVisibleOverview = mocks.getOverview.mock.calls.length
+ // Back to visible after well over one cadence hidden: an immediate catch-up
+ // refresh fires so the returning view is never stale.
setVisibility('visible')
await act(async () => { document.dispatchEvent(new Event('visibilitychange')) })
await act(async () => { await vi.advanceTimersByTimeAsync(0) })
- expect(mocks.getYield.mock.calls.length).toBe(beforeVisibleYield)
- expect(mocks.getOverview.mock.calls.length).toBe(beforeVisibleOverview)
+ expect(mocks.getYield.mock.calls.length).toBeGreaterThan(atHideYield)
+ expect(mocks.getOverview.mock.calls.length).toBeGreaterThan(atHideOverview)
+
+ // The resumed timer is single: one cadence adds exactly the baseline count,
+ // proving the hide/show cycle did not stack a second interval.
+ const afterCatchup = mocks.getYield.mock.calls.length
+ await act(async () => { await vi.advanceTimersByTimeAsync(30_000) })
+ expect(mocks.getYield.mock.calls.length - afterCatchup).toBe(perCadence)
} finally {
setVisibility('visible')
vi.useRealTimers()
diff --git a/app/renderer/hooks/usePolled.test.ts b/app/renderer/hooks/usePolled.test.ts
index 2a8a8e69..7cdeb7cd 100644
--- a/app/renderer/hooks/usePolled.test.ts
+++ b/app/renderer/hooks/usePolled.test.ts
@@ -426,7 +426,7 @@ describe('usePolled', () => {
}
})
- it('continues data refreshes while the document is hidden', async () => {
+ it('stops the interval while hidden, then resumes with a catch-up on return', async () => {
vi.useFakeTimers()
try {
setVisibility('visible')
@@ -435,18 +435,23 @@ describe('usePolled', () => {
expect(fetcher).toHaveBeenCalledTimes(1) // mount fetch
// Let the mount fetch resolve so lastSuccess is recorded.
await act(async () => { await vi.advanceTimersByTimeAsync(0) })
+ // One visible cadence ticks.
+ await act(async () => { await vi.advanceTimersByTimeAsync(1000) })
+ expect(fetcher).toHaveBeenCalledTimes(2)
- // Hidden/minimized is still an open product. Data refreshes continue so a
- // return hours later never exposes an arbitrarily stale snapshot.
+ // Hidden: the interval stops, so no CLI re-parse fires while unwatched.
setVisibility('hidden')
+ await act(async () => { document.dispatchEvent(new Event('visibilitychange')) })
await act(async () => { await vi.advanceTimersByTimeAsync(5000) })
- expect(fetcher).toHaveBeenCalledTimes(6)
+ expect(fetcher).toHaveBeenCalledTimes(2) // frozen while hidden
- // Returning visible does not duplicate a refresh because the latest hidden
- // interval already succeeded inside one cadence.
+ // Back to visible after more than one cadence: an immediate catch-up
+ // refresh fires so the returning view is never stale, then ticking resumes.
setVisibility('visible')
await act(async () => { document.dispatchEvent(new Event('visibilitychange')) })
- expect(fetcher).toHaveBeenCalledTimes(6)
+ expect(fetcher).toHaveBeenCalledTimes(3) // catch-up
+ await act(async () => { await vi.advanceTimersByTimeAsync(1000) })
+ expect(fetcher).toHaveBeenCalledTimes(4) // single resumed interval, no stack
} finally {
vi.useRealTimers()
}
diff --git a/app/renderer/hooks/usePolled.ts b/app/renderer/hooks/usePolled.ts
index 7a3eeef3..e43d071e 100644
--- a/app/renderer/hooks/usePolled.ts
+++ b/app/renderer/hooks/usePolled.ts
@@ -475,24 +475,32 @@ export function usePolled(
useEffect(() => {
load()
- // Data freshness is a product contract, including while the app is covered
- // or minimized. The CLI resident process coalesces reads, so keep the cadence
- // alive; purely visual animation remains visibility-gated elsewhere.
+ // Poll only while the window is visible. Each tick drives a CLI re-parse in
+ // the resident serve child, and on a machine with active agent sessions the
+ // watched roots change constantly, so every tick is a full-core parse. A
+ // minimized/occluded window has no viewer to keep fresh; letting it poll was
+ // the app's dominant idle energy drain. On return to visible the catch-up
+ // below refreshes immediately, so freshness while looking is unchanged.
const tick = () => load()
// Manual cadence (intervalMs == null) skips the interval entirely.
- const id = intervalMs != null ? setInterval(tick, intervalMs) : null
+ let id: ReturnType | null = null
+ const isVisible = () => typeof document === 'undefined' || document.visibilityState === 'visible'
+ const startTicking = () => { if (id == null && intervalMs != null) id = setInterval(tick, intervalMs) }
+ const stopTicking = () => { if (id != null) { clearInterval(id); id = null } }
+ if (isVisible()) startTicking()
// On return to visible, if the last success is older than a full cadence,
- // refresh once immediately instead of waiting up to intervalMs for the next
- // tick. Manual cadence has no catch-up (the user drives refresh).
+ // refresh once immediately instead of waiting up to intervalMs; then resume
+ // ticking. On hide, stop ticking so no CLI work happens while unwatched.
const onVisible = () => {
if (intervalMs == null) return
- if (typeof document === 'undefined' || document.visibilityState !== 'visible') return
+ if (!isVisible()) { stopTicking(); return }
const last = lastSuccessRef.current
if (last == null || Date.now() - last >= intervalMs) load()
+ startTicking()
}
if (typeof document !== 'undefined') document.addEventListener('visibilitychange', onVisible)
return () => {
- if (id != null) clearInterval(id)
+ stopTicking()
if (typeof document !== 'undefined') document.removeEventListener('visibilitychange', onVisible)
// Retire this generation so an in-flight fetch can't resolve into state
// after unmount or a deps change.
diff --git a/app/renderer/styles/plain.css b/app/renderer/styles/plain.css
index dd0291c5..91e34056 100644
--- a/app/renderer/styles/plain.css
+++ b/app/renderer/styles/plain.css
@@ -1391,9 +1391,9 @@ html.page-hidden *::after {
.flame-text, .app .flame-text {
background: linear-gradient(90deg, #ff6a00, #ffda44, #e8553a, #ff8c00, #ffda44, #ff6a00);
background-size: 300% 100%;
+ background-position: 50% 50%;
-webkit-background-clip: text; background-clip: text;
-webkit-text-fill-color: transparent;
- animation: flameShift 3s ease infinite;
}
.onboard-version {
position: absolute; right: 20px; bottom: 20px; pointer-events: none;
@@ -1448,7 +1448,6 @@ html.page-hidden *::after {
color: rgba(255, 255, 255, 0.78);
}
.onboard-quiet:hover, .onboard-quiet:focus-visible { color: #f8f5f0; }
-@keyframes flameShift { 0%, 100% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } }
@keyframes onboard-step-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
/* Short windows: the consent step is the tall one, so the lockup gives way first. */
@media (max-height: 720px) {