Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ describe('useMessageStream delta flush scheduling', () => {
expect(assistantText()).toBe('still streaming')
})

it('flushes queued text immediately when a hidden window becomes visible', () => {
vi.mocked(performance.now).mockReturnValue(0)
mountStream()

act(() => appendAssistantDelta!(SID, 'caught up on focus'))
expect(assistantText()).toBe('')
expect(vi.getTimerCount()).toBe(1)

Object.defineProperty(globalThis.document, 'visibilityState', {
configurable: true,
value: 'visible'
})

act(() => globalThis.document.dispatchEvent(new Event('visibilitychange')))

expect(assistantText()).toBe('caught up on focus')
expect(vi.getTimerCount()).toBe(0)
})

it('flushes queued text on focus when visibility remains visible', () => {
vi.mocked(performance.now).mockReturnValue(0)
Object.defineProperty(globalThis.document, 'visibilityState', {
configurable: true,
value: 'visible'
})
mountStream()

act(() => appendAssistantDelta!(SID, 'focused without visibility change'))
expect(assistantText()).toBe('')
expect(vi.getTimerCount()).toBe(1)

act(() => globalThis.window.dispatchEvent(new Event('focus')))

expect(assistantText()).toBe('focused without visibility change')
})

it('cancels the pending timer on unmount and flushes exactly once', async () => {
vi.mocked(performance.now).mockReturnValue(0)
mountStream()
Expand Down
28 changes: 28 additions & 0 deletions apps/desktop/src/app/session/hooks/use-message-stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,34 @@ export function useMessageStream({
[flushQueuedDeltas]
)

// Page Visibility does not report every Windows/Linux focus transition.
// Flush queued deltas on both signals so returning to a chat cannot leave a
// completed chunk waiting for the next throttled timer.
useEffect(() => {
const flushPendingDeltas = () => {
if (flushHandleRef.current !== null) {
window.clearTimeout(flushHandleRef.current)
flushHandleRef.current = null
}

flushQueuedDeltas()
}

const flushWhenVisible = () => {
if (document.visibilityState === 'visible') {
flushPendingDeltas()
}
}

document.addEventListener('visibilitychange', flushWhenVisible)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only observes Page Visibility. Electron documents that Windows/Linux do not change visibility for occlusion, so an unfocused/occluded chat can regain attention without this callback; that is the Windows streaming-stall path fixed by b07b7894ec. Please add a cross-platform reactivation signal (such as window focus) and a regression test before restoring background throttling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than "occluded", I agree out of focus would have ben a better selection of words.

window.addEventListener('focus', flushPendingDeltas)

return () => {
document.removeEventListener('visibilitychange', flushWhenVisible)
window.removeEventListener('focus', flushPendingDeltas)
}
}, [flushQueuedDeltas])

const appendAssistantDelta = useCallback(
(sessionId: string, delta: string) => {
if (!delta) {
Expand Down