Skip to content

The agent that grows with you

fix(desktop): stop the session-control countdown clock on a hidden pane

chelsealong4375a36d81da
diff --git a/apps/desktop/src/app/chat/composer/status-stack/session-control-heartbeat.test.tsx b/apps/desktop/src/app/chat/composer/status-stack/session-control-heartbeat.test.tsx
new file mode 100644
index 00000000000..1a4b6b56e4d
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/status-stack/session-control-heartbeat.test.tsx
@@ -0,0 +1,69 @@
+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 type { SessionControlHeartbeat } from '@/store/session-control'
+
+import { SessionControlHeartbeatSection } from './session-control-heartbeat'
+
+const heartbeat: SessionControlHeartbeat = {
+ created_at: Math.floor(Date.now() / 1000),
+ fire_count: 2,
+ interval_seconds: 3600,
+ last_fired_at: 0,
+ prompt: 'keep going',
+ status: 'active'
+}
+
+function tree(visible: boolean) {
+ return (
+ <I18nProvider configClient={null} initialLocale="en">
+ <PaneVisibleContext.Provider value={visible}>
+ <SessionControlHeartbeatSection
+ heartbeat={heartbeat}
+ onFeedback={() => {}}
+ pendingAction={null}
+ sessionId="sess-heartbeat"
+ />
+ </PaneVisibleContext.Provider>
+ </I18nProvider>
+ )
+}
+
+// #122413: keep-alive keeps every ever-active tab mounted, so a background
+// tile's heartbeat countdown used to tick every second forever regardless of
+// whether its tab was on screen — the same class of bug ./index.tsx's
+// background-process poll and ./use-subagent-snapshot.ts already guard against.
+describe('SessionControlHeartbeatSection hidden-pane clock', () => {
+ beforeEach(() => {
+ vi.useFakeTimers()
+ vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('visible')
+ })
+
+ afterEach(() => {
+ cleanup()
+ vi.useRealTimers()
+ vi.restoreAllMocks()
+ })
+
+ const headerText = (container: HTMLElement) => container.querySelector('.status-section-trigger')?.textContent ?? ''
+
+ it('hidden tile freezes the countdown', async () => {
+ const hidden = render(tree(false))
+ const initial = headerText(hidden.container)
+
+ await vi.advanceTimersByTimeAsync(5_000)
+ expect(headerText(hidden.container)).toBe(initial)
+ hidden.unmount()
+ })
+
+ it('visible tile keeps the countdown ticking', async () => {
+ const shown = render(tree(true))
+ const initial = headerText(shown.container)
+
+ await vi.advanceTimersByTimeAsync(3_000)
+ expect(headerText(shown.container)).not.toBe(initial)
+ shown.unmount()
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/status-stack/session-control-utils.ts b/apps/desktop/src/app/chat/composer/status-stack/session-control-utils.ts
index 025ce05202b..79880bf8a0a 100644
--- a/apps/desktop/src/app/chat/composer/status-stack/session-control-utils.ts
+++ b/apps/desktop/src/app/chat/composer/status-stack/session-control-utils.ts
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useState, useSyncExternalStore } from 'react'
+import { usePaneVisible } from '@/components/pane-shell/pane-visibility'
import type { Translations } from '@/i18n'
interface SessionSliceStore<T> {
@@ -14,20 +15,29 @@ export function useSessionValue<T>(store: SessionSliceStore<T>, sessionId: strin
return useSyncExternalStore(subscribe, () => (sessionId ? store.get()[sessionId] : undefined))
}
-/** Wall clock that re-renders once a second while ``active``, for the heartbeat and loop countdowns. */
+/** Wall clock that re-renders once a second while ``active``, for the heartbeat and loop countdowns.
+ * Keep-alive keeps every ever-active tab mounted, so it also stops for a hidden pane or window
+ * (same gate as the background-process poll in ./index.tsx); a shown pane re-reads the clock. */
export function useCountdownClock(active: boolean): number {
const [now, setNow] = useState(() => Date.now())
+ const paneVisible = usePaneVisible()
useEffect(() => {
- if (!active) {
+ if (!active || !paneVisible) {
return
}
- const tick = () => setNow(Date.now())
+ const tick = () => {
+ if (document.visibilityState === 'visible') {
+ setNow(Date.now())
+ }
+ }
+
+ tick()
const interval = window.setInterval(tick, 1_000)
return () => window.clearInterval(interval)
- }, [active])
+ }, [active, paneVisible])
return now
}