Skip to content

The agent that grows with you

fix(desktop): gate each return-refresh leg on its own signal

liuhao1024745246a260fe
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
index 0206d93004a..c47e851560f 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
@@ -757,6 +757,33 @@ describe('active transcript refresh', () => {
expect(refresh).toHaveBeenCalledTimes(1)
})
+ it('refreshes on the visibility leg alone when the window is visible but unfocused (#125532 review)', () => {
+ // A visible-but-unfocused window (another app in front, read without
+ // clicking) never receives `focus`; the visibility leg must stand on
+ // `visible` alone or the return refresh is lost entirely.
+ vi.spyOn(document, 'hasFocus').mockReturnValue(false)
+ $changeEventsAvailable.set(true)
+ const refresh = vi.fn(async () => undefined)
+
+ renderSync(refresh)
+ refresh.mockClear() // drop the connect-time pull; this test is about returns
+
+ act(() => document.dispatchEvent(new Event('visibilitychange')))
+ expect(refresh).toHaveBeenCalledTimes(1)
+ })
+
+ it('does not refresh on a visibilitychange to hidden (#125532 review)', () => {
+ vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('hidden')
+ $changeEventsAvailable.set(true)
+ const refresh = vi.fn(async () => undefined)
+
+ renderSync(refresh)
+ refresh.mockClear()
+
+ act(() => document.dispatchEvent(new Event('visibilitychange')))
+ expect(refresh).not.toHaveBeenCalled()
+ })
+
it('throttles return refreshes inside the minimum gap (#125532)', () => {
vi.useFakeTimers()
$changeEventsAvailable.set(true)
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
index d57e0fc9763..c87784d1f38 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -1170,8 +1170,18 @@ export function useBackgroundSync({
let lastRefreshAt = 0
- const refreshOnReturn = () => {
- if (!windowIsActivelyViewed({ focused: document.hasFocus(), visibilityState: document.visibilityState })) {
+ const refreshOnReturn = (event?: Event) => {
+ // Each leg gates on its own signal: a window that is visible but never
+ // focused (another app in front, a secondary space, read without
+ // clicking) drops the visibility leg under a combined focused gate, and
+ // the focus leg it would wait for never arrives — the return refresh is
+ // lost for a window the user is reading.
+ const viewed =
+ event?.type === 'visibilitychange'
+ ? document.visibilityState === 'visible'
+ : document.hasFocus()
+
+ if (!viewed) {
return
}