Skip to content

The agent that grows with you

fix(desktop): refresh the open transcript when the window is viewed again

liuhao10249ff5f0602367
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 0efb4e23736..0206d93004a 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
@@ -36,6 +36,7 @@ import {
rehydrateLiveSessionStatuses,
resetTypingActivityTracking,
resolveActiveTranscriptSession,
+ TRANSCRIPT_RETURN_REFRESH_MIN_GAP_MS,
useBackgroundSync
} from './use-background-sync'
@@ -741,6 +742,58 @@ describe('active transcript refresh', () => {
expect(refresh).toHaveBeenCalledTimes(2)
})
+ it('refreshes the open transcript when the window is viewed again (#125532)', () => {
+ $changeEventsAvailable.set(true)
+ const refresh = vi.fn(async () => undefined)
+
+ renderSync(refresh)
+ refresh.mockClear() // drop the connect-time pull; this test is about returns
+
+ // One return fires both visibilitychange and focus — a single read.
+ act(() => {
+ document.dispatchEvent(new Event('visibilitychange'))
+ window.dispatchEvent(new Event('focus'))
+ })
+ expect(refresh).toHaveBeenCalledTimes(1)
+ })
+
+ it('throttles return refreshes inside the minimum gap (#125532)', () => {
+ vi.useFakeTimers()
+ $changeEventsAvailable.set(true)
+ const refresh = vi.fn(async () => undefined)
+
+ renderSync(refresh)
+ refresh.mockClear()
+
+ act(() => window.dispatchEvent(new Event('focus')))
+ expect(refresh).toHaveBeenCalledTimes(1)
+
+ // A rapid cmd-tab pair inside the gap adds no read...
+ act(() => window.dispatchEvent(new Event('focus')))
+ expect(refresh).toHaveBeenCalledTimes(1)
+
+ // ...but the next return after the gap catches up again.
+ act(() => {
+ vi.advanceTimersByTime(TRANSCRIPT_RETURN_REFRESH_MIN_GAP_MS)
+ window.dispatchEvent(new Event('focus'))
+ })
+ expect(refresh).toHaveBeenCalledTimes(2)
+ })
+
+ it('does not refresh on return while the gateway is closed (#125532)', () => {
+ $changeEventsAvailable.set(true)
+ const refresh = vi.fn(async () => undefined)
+
+ renderSync(refresh, { gatewayState: 'closed' })
+ refresh.mockClear()
+
+ act(() => {
+ document.dispatchEvent(new Event('visibilitychange'))
+ window.dispatchEvent(new Event('focus'))
+ })
+ expect(refresh).not.toHaveBeenCalled()
+ })
+
it('coalesces a burst of global session-change ticks', async () => {
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 c9c6b3ffe0d..d57e0fc9763 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -682,6 +682,12 @@ const SESSIONS_LIST_TICK_GAP_MS = 10_000
// backstops) keep their cadence because they carry liveness, not the heavy
// list reconciliation.
const TYPING_BURST_QUIET_MS = 1_500
+// Returning to the window fires both `visibilitychange` and `focus`, and a
+// rapid cmd-tab switch fires another pair seconds later. The return refresh
+// (#125532) is signature-gated downstream, but collapses to one read per
+// return instead — the transcript it protects is only readable behind a
+// socket the window just regained.
+export const TRANSCRIPT_RETURN_REFRESH_MIN_GAP_MS = 5_000
interface LiveSessionStatusItem {
id?: string
@@ -1150,6 +1156,44 @@ export function useBackgroundSync({
[requestGateway, updateSessionState]
)
+ // Wake/return backstop (#125532): a socket that zombie-survives laptop sleep
+ // never transitions gatewayState, so the reconnect backstop above cannot
+ // fire, and replies that completed while the machine slept leave the open
+ // transcript behind — the first send after wake then bounces off the
+ // stale-send guard (#65047) once. Catch up when the window is viewed again;
+ // the reconcile is signature-gated, so an unchanged transcript costs one
+ // cheap tail read. Messaging transcripts keep their own visible poll below.
+ useEffect(() => {
+ if (gatewayState !== 'open' || activeIsMessaging || !activeSessionId || !activeStoredSessionId) {
+ return
+ }
+
+ let lastRefreshAt = 0
+
+ const refreshOnReturn = () => {
+ if (!windowIsActivelyViewed({ focused: document.hasFocus(), visibilityState: document.visibilityState })) {
+ return
+ }
+
+ const now = Date.now()
+
+ if (now - lastRefreshAt < TRANSCRIPT_RETURN_REFRESH_MIN_GAP_MS) {
+ return
+ }
+
+ lastRefreshAt = now
+ requestActiveTranscriptRefresh(true)
+ }
+
+ document.addEventListener('visibilitychange', refreshOnReturn)
+ window.addEventListener('focus', refreshOnReturn)
+
+ return () => {
+ document.removeEventListener('visibilitychange', refreshOnReturn)
+ window.removeEventListener('focus', refreshOnReturn)
+ }
+ }, [activeIsMessaging, activeSessionId, activeStoredSessionId, gatewayState, requestActiveTranscriptRefresh])
+
// A reconnect loses renderer-only working/attention atoms while the backend
// keeps the actual turns alive. Re-seed from the gateway's in-memory session
// registry immediately, then re-pull on every sessions.changed broadcast; a