Skip to content

fix(sessions): keep streaming live when the window is backgrounded#3116

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-backgrounded-agent-stream-flush
Draft

fix(sessions): keep streaming live when the window is backgrounded#3116
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-backgrounded-agent-stream-flush

Conversation

@posthog

@posthog posthog Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Problem

When the agent enters a waiting/looping state (e.g. "Loop over this PR until all checks succeed" → "I'll check back in ~10 min once CI finishes"), the conversation appears to hang. The messages the agent produces afterward stay hidden and then appear all at once the moment the user sends a follow-up.

The streamed events are not lost — they are buffered in the renderer and committed to the conversation store on a short setTimeout flush (a frame-sized coalescing window that keeps streaming from re-rendering per token). Chromium clamps, and once the window is occluded/hidden effectively freezes, renderer timers when the window is backgrounded. Backgrounding the app is exactly what you do during a 10-minute CI wait, so the flush timer stops firing: buffered events pile up and only drain in a burst when the timer thaws on refocus (sending a follow-up focuses the window). This matches the reported "hidden messages appear the instant a follow-up is sent."

Changes

Set backgroundThrottling: false on the main BrowserWindow so the renderer keeps running at full speed while backgrounded. This keeps the streamed-event flush (and other timer/rAF-driven UI) working during long-running agent runs, so a waiting/looping agent no longer looks hung.

How did you test this?

I traced the full local streaming path (agent stream tap → tRPC subscription → renderer flush → zustand store → conversation view) and confirmed the only deferral point is the renderer setTimeout flush in SessionService; every other hop delivers immediately. backgroundThrottling was unset (defaults to true), and SessionService (which owns the flush) is bound in the renderer container, so the flush timer is subject to Chromium background throttling — the mechanism behind the symptom.

I was not able to run the live multi-minute backgrounded repro in this headless environment, so this has not been verified end-to-end in a running app; the fix is the standard Electron remedy for a backgrounded renderer batching updates until refocus.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code from an inbox report.

Streamed agent events are committed to the conversation store via a short
setTimeout flush in the renderer. Chromium clamps and (when the window is
occluded/hidden) freezes renderer timers, so while the app is backgrounded that
flush never runs: streamed events pile up unflushed and only render in a burst
when the timer thaws on refocus. This is why a waiting/looping agent appears to
hang and its messages all appear at once after the user sends a follow-up.

Set backgroundThrottling: false on the main window so renderer timers keep
running at full speed while backgrounded, letting live streaming flow during
long-running agent runs.

Generated-By: PostHog Code
Task-Id: d38d3951-6357-4d57-809c-74d47581a163
@trunk-io

trunk-io Bot commented Jul 3, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit fffce79.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants